common.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  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. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/types.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/init.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/delay.h>
  39. #include <linux/skbuff.h>
  40. #include <net/mac80211.h>
  41. #include "common.h"
  42. int
  43. _il_poll_bit(struct il_priv *il, u32 addr, u32 bits, u32 mask, int timeout)
  44. {
  45. const int interval = 10; /* microseconds */
  46. int t = 0;
  47. do {
  48. if ((_il_rd(il, addr) & mask) == (bits & mask))
  49. return t;
  50. udelay(interval);
  51. t += interval;
  52. } while (t < timeout);
  53. return -ETIMEDOUT;
  54. }
  55. EXPORT_SYMBOL(_il_poll_bit);
  56. void
  57. il_set_bit(struct il_priv *p, u32 r, u32 m)
  58. {
  59. unsigned long reg_flags;
  60. spin_lock_irqsave(&p->reg_lock, reg_flags);
  61. _il_set_bit(p, r, m);
  62. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  63. }
  64. EXPORT_SYMBOL(il_set_bit);
  65. void
  66. il_clear_bit(struct il_priv *p, u32 r, u32 m)
  67. {
  68. unsigned long reg_flags;
  69. spin_lock_irqsave(&p->reg_lock, reg_flags);
  70. _il_clear_bit(p, r, m);
  71. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  72. }
  73. EXPORT_SYMBOL(il_clear_bit);
  74. bool
  75. _il_grab_nic_access(struct il_priv *il)
  76. {
  77. int ret;
  78. u32 val;
  79. /* this bit wakes up the NIC */
  80. _il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  81. /*
  82. * These bits say the device is running, and should keep running for
  83. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  84. * but they do not indicate that embedded SRAM is restored yet;
  85. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  86. * to/from host DRAM when sleeping/waking for power-saving.
  87. * Each direction takes approximately 1/4 millisecond; with this
  88. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  89. * series of register accesses are expected (e.g. reading Event Log),
  90. * to keep device from sleeping.
  91. *
  92. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  93. * SRAM is okay/restored. We don't check that here because this call
  94. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  95. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  96. *
  97. */
  98. ret =
  99. _il_poll_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  100. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  101. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  102. if (unlikely(ret < 0)) {
  103. val = _il_rd(il, CSR_GP_CNTRL);
  104. WARN_ONCE(1, "Timeout waiting for ucode processor access "
  105. "(CSR_GP_CNTRL 0x%08x)\n", val);
  106. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  107. return false;
  108. }
  109. return true;
  110. }
  111. EXPORT_SYMBOL_GPL(_il_grab_nic_access);
  112. int
  113. il_poll_bit(struct il_priv *il, u32 addr, u32 mask, int timeout)
  114. {
  115. const int interval = 10; /* microseconds */
  116. int t = 0;
  117. do {
  118. if ((il_rd(il, addr) & mask) == mask)
  119. return t;
  120. udelay(interval);
  121. t += interval;
  122. } while (t < timeout);
  123. return -ETIMEDOUT;
  124. }
  125. EXPORT_SYMBOL(il_poll_bit);
  126. u32
  127. il_rd_prph(struct il_priv *il, u32 reg)
  128. {
  129. unsigned long reg_flags;
  130. u32 val;
  131. spin_lock_irqsave(&il->reg_lock, reg_flags);
  132. _il_grab_nic_access(il);
  133. val = _il_rd_prph(il, reg);
  134. _il_release_nic_access(il);
  135. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  136. return val;
  137. }
  138. EXPORT_SYMBOL(il_rd_prph);
  139. void
  140. il_wr_prph(struct il_priv *il, u32 addr, u32 val)
  141. {
  142. unsigned long reg_flags;
  143. spin_lock_irqsave(&il->reg_lock, reg_flags);
  144. if (likely(_il_grab_nic_access(il))) {
  145. _il_wr_prph(il, addr, val);
  146. _il_release_nic_access(il);
  147. }
  148. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  149. }
  150. EXPORT_SYMBOL(il_wr_prph);
  151. u32
  152. il_read_targ_mem(struct il_priv *il, u32 addr)
  153. {
  154. unsigned long reg_flags;
  155. u32 value;
  156. spin_lock_irqsave(&il->reg_lock, reg_flags);
  157. _il_grab_nic_access(il);
  158. _il_wr(il, HBUS_TARG_MEM_RADDR, addr);
  159. value = _il_rd(il, HBUS_TARG_MEM_RDAT);
  160. _il_release_nic_access(il);
  161. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  162. return value;
  163. }
  164. EXPORT_SYMBOL(il_read_targ_mem);
  165. void
  166. il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
  167. {
  168. unsigned long reg_flags;
  169. spin_lock_irqsave(&il->reg_lock, reg_flags);
  170. if (likely(_il_grab_nic_access(il))) {
  171. _il_wr(il, HBUS_TARG_MEM_WADDR, addr);
  172. _il_wr(il, HBUS_TARG_MEM_WDAT, val);
  173. _il_release_nic_access(il);
  174. }
  175. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  176. }
  177. EXPORT_SYMBOL(il_write_targ_mem);
  178. const char *
  179. il_get_cmd_string(u8 cmd)
  180. {
  181. switch (cmd) {
  182. IL_CMD(N_ALIVE);
  183. IL_CMD(N_ERROR);
  184. IL_CMD(C_RXON);
  185. IL_CMD(C_RXON_ASSOC);
  186. IL_CMD(C_QOS_PARAM);
  187. IL_CMD(C_RXON_TIMING);
  188. IL_CMD(C_ADD_STA);
  189. IL_CMD(C_REM_STA);
  190. IL_CMD(C_WEPKEY);
  191. IL_CMD(N_3945_RX);
  192. IL_CMD(C_TX);
  193. IL_CMD(C_RATE_SCALE);
  194. IL_CMD(C_LEDS);
  195. IL_CMD(C_TX_LINK_QUALITY_CMD);
  196. IL_CMD(C_CHANNEL_SWITCH);
  197. IL_CMD(N_CHANNEL_SWITCH);
  198. IL_CMD(C_SPECTRUM_MEASUREMENT);
  199. IL_CMD(N_SPECTRUM_MEASUREMENT);
  200. IL_CMD(C_POWER_TBL);
  201. IL_CMD(N_PM_SLEEP);
  202. IL_CMD(N_PM_DEBUG_STATS);
  203. IL_CMD(C_SCAN);
  204. IL_CMD(C_SCAN_ABORT);
  205. IL_CMD(N_SCAN_START);
  206. IL_CMD(N_SCAN_RESULTS);
  207. IL_CMD(N_SCAN_COMPLETE);
  208. IL_CMD(N_BEACON);
  209. IL_CMD(C_TX_BEACON);
  210. IL_CMD(C_TX_PWR_TBL);
  211. IL_CMD(C_BT_CONFIG);
  212. IL_CMD(C_STATS);
  213. IL_CMD(N_STATS);
  214. IL_CMD(N_CARD_STATE);
  215. IL_CMD(N_MISSED_BEACONS);
  216. IL_CMD(C_CT_KILL_CONFIG);
  217. IL_CMD(C_SENSITIVITY);
  218. IL_CMD(C_PHY_CALIBRATION);
  219. IL_CMD(N_RX_PHY);
  220. IL_CMD(N_RX_MPDU);
  221. IL_CMD(N_RX);
  222. IL_CMD(N_COMPRESSED_BA);
  223. default:
  224. return "UNKNOWN";
  225. }
  226. }
  227. EXPORT_SYMBOL(il_get_cmd_string);
  228. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  229. static void
  230. il_generic_cmd_callback(struct il_priv *il, struct il_device_cmd *cmd,
  231. struct il_rx_pkt *pkt)
  232. {
  233. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  234. IL_ERR("Bad return from %s (0x%08X)\n",
  235. il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  236. return;
  237. }
  238. #ifdef CONFIG_IWLEGACY_DEBUG
  239. switch (cmd->hdr.cmd) {
  240. case C_TX_LINK_QUALITY_CMD:
  241. case C_SENSITIVITY:
  242. D_HC_DUMP("back from %s (0x%08X)\n",
  243. il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  244. break;
  245. default:
  246. D_HC("back from %s (0x%08X)\n", il_get_cmd_string(cmd->hdr.cmd),
  247. pkt->hdr.flags);
  248. }
  249. #endif
  250. }
  251. static int
  252. il_send_cmd_async(struct il_priv *il, struct il_host_cmd *cmd)
  253. {
  254. int ret;
  255. BUG_ON(!(cmd->flags & CMD_ASYNC));
  256. /* An asynchronous command can not expect an SKB to be set. */
  257. BUG_ON(cmd->flags & CMD_WANT_SKB);
  258. /* Assign a generic callback if one is not provided */
  259. if (!cmd->callback)
  260. cmd->callback = il_generic_cmd_callback;
  261. if (test_bit(S_EXIT_PENDING, &il->status))
  262. return -EBUSY;
  263. ret = il_enqueue_hcmd(il, cmd);
  264. if (ret < 0) {
  265. IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
  266. il_get_cmd_string(cmd->id), ret);
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. int
  272. il_send_cmd_sync(struct il_priv *il, struct il_host_cmd *cmd)
  273. {
  274. int cmd_idx;
  275. int ret;
  276. lockdep_assert_held(&il->mutex);
  277. BUG_ON(cmd->flags & CMD_ASYNC);
  278. /* A synchronous command can not have a callback set. */
  279. BUG_ON(cmd->callback);
  280. D_INFO("Attempting to send sync command %s\n",
  281. il_get_cmd_string(cmd->id));
  282. set_bit(S_HCMD_ACTIVE, &il->status);
  283. D_INFO("Setting HCMD_ACTIVE for command %s\n",
  284. il_get_cmd_string(cmd->id));
  285. cmd_idx = il_enqueue_hcmd(il, cmd);
  286. if (cmd_idx < 0) {
  287. ret = cmd_idx;
  288. IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
  289. il_get_cmd_string(cmd->id), ret);
  290. goto out;
  291. }
  292. ret = wait_event_timeout(il->wait_command_queue,
  293. !test_bit(S_HCMD_ACTIVE, &il->status),
  294. HOST_COMPLETE_TIMEOUT);
  295. if (!ret) {
  296. if (test_bit(S_HCMD_ACTIVE, &il->status)) {
  297. IL_ERR("Error sending %s: time out after %dms.\n",
  298. il_get_cmd_string(cmd->id),
  299. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  300. clear_bit(S_HCMD_ACTIVE, &il->status);
  301. D_INFO("Clearing HCMD_ACTIVE for command %s\n",
  302. il_get_cmd_string(cmd->id));
  303. ret = -ETIMEDOUT;
  304. goto cancel;
  305. }
  306. }
  307. if (test_bit(S_RF_KILL_HW, &il->status)) {
  308. IL_ERR("Command %s aborted: RF KILL Switch\n",
  309. il_get_cmd_string(cmd->id));
  310. ret = -ECANCELED;
  311. goto fail;
  312. }
  313. if (test_bit(S_FW_ERROR, &il->status)) {
  314. IL_ERR("Command %s failed: FW Error\n",
  315. il_get_cmd_string(cmd->id));
  316. ret = -EIO;
  317. goto fail;
  318. }
  319. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  320. IL_ERR("Error: Response NULL in '%s'\n",
  321. il_get_cmd_string(cmd->id));
  322. ret = -EIO;
  323. goto cancel;
  324. }
  325. ret = 0;
  326. goto out;
  327. cancel:
  328. if (cmd->flags & CMD_WANT_SKB) {
  329. /*
  330. * Cancel the CMD_WANT_SKB flag for the cmd in the
  331. * TX cmd queue. Otherwise in case the cmd comes
  332. * in later, it will possibly set an invalid
  333. * address (cmd->meta.source).
  334. */
  335. il->txq[il->cmd_queue].meta[cmd_idx].flags &= ~CMD_WANT_SKB;
  336. }
  337. fail:
  338. if (cmd->reply_page) {
  339. il_free_pages(il, cmd->reply_page);
  340. cmd->reply_page = 0;
  341. }
  342. out:
  343. return ret;
  344. }
  345. EXPORT_SYMBOL(il_send_cmd_sync);
  346. int
  347. il_send_cmd(struct il_priv *il, struct il_host_cmd *cmd)
  348. {
  349. if (cmd->flags & CMD_ASYNC)
  350. return il_send_cmd_async(il, cmd);
  351. return il_send_cmd_sync(il, cmd);
  352. }
  353. EXPORT_SYMBOL(il_send_cmd);
  354. int
  355. il_send_cmd_pdu(struct il_priv *il, u8 id, u16 len, const void *data)
  356. {
  357. struct il_host_cmd cmd = {
  358. .id = id,
  359. .len = len,
  360. .data = data,
  361. };
  362. return il_send_cmd_sync(il, &cmd);
  363. }
  364. EXPORT_SYMBOL(il_send_cmd_pdu);
  365. int
  366. il_send_cmd_pdu_async(struct il_priv *il, u8 id, u16 len, const void *data,
  367. void (*callback) (struct il_priv *il,
  368. struct il_device_cmd *cmd,
  369. struct il_rx_pkt *pkt))
  370. {
  371. struct il_host_cmd cmd = {
  372. .id = id,
  373. .len = len,
  374. .data = data,
  375. };
  376. cmd.flags |= CMD_ASYNC;
  377. cmd.callback = callback;
  378. return il_send_cmd_async(il, &cmd);
  379. }
  380. EXPORT_SYMBOL(il_send_cmd_pdu_async);
  381. /* default: IL_LED_BLINK(0) using blinking idx table */
  382. static int led_mode;
  383. module_param(led_mode, int, S_IRUGO);
  384. MODULE_PARM_DESC(led_mode,
  385. "0=system default, " "1=On(RF On)/Off(RF Off), 2=blinking");
  386. /* Throughput OFF time(ms) ON time (ms)
  387. * >300 25 25
  388. * >200 to 300 40 40
  389. * >100 to 200 55 55
  390. * >70 to 100 65 65
  391. * >50 to 70 75 75
  392. * >20 to 50 85 85
  393. * >10 to 20 95 95
  394. * >5 to 10 110 110
  395. * >1 to 5 130 130
  396. * >0 to 1 167 167
  397. * <=0 SOLID ON
  398. */
  399. static const struct ieee80211_tpt_blink il_blink[] = {
  400. {.throughput = 0, .blink_time = 334},
  401. {.throughput = 1 * 1024 - 1, .blink_time = 260},
  402. {.throughput = 5 * 1024 - 1, .blink_time = 220},
  403. {.throughput = 10 * 1024 - 1, .blink_time = 190},
  404. {.throughput = 20 * 1024 - 1, .blink_time = 170},
  405. {.throughput = 50 * 1024 - 1, .blink_time = 150},
  406. {.throughput = 70 * 1024 - 1, .blink_time = 130},
  407. {.throughput = 100 * 1024 - 1, .blink_time = 110},
  408. {.throughput = 200 * 1024 - 1, .blink_time = 80},
  409. {.throughput = 300 * 1024 - 1, .blink_time = 50},
  410. };
  411. /*
  412. * Adjust led blink rate to compensate on a MAC Clock difference on every HW
  413. * Led blink rate analysis showed an average deviation of 0% on 3945,
  414. * 5% on 4965 HW.
  415. * Need to compensate on the led on/off time per HW according to the deviation
  416. * to achieve the desired led frequency
  417. * The calculation is: (100-averageDeviation)/100 * blinkTime
  418. * For code efficiency the calculation will be:
  419. * compensation = (100 - averageDeviation) * 64 / 100
  420. * NewBlinkTime = (compensation * BlinkTime) / 64
  421. */
  422. static inline u8
  423. il_blink_compensation(struct il_priv *il, u8 time, u16 compensation)
  424. {
  425. if (!compensation) {
  426. IL_ERR("undefined blink compensation: "
  427. "use pre-defined blinking time\n");
  428. return time;
  429. }
  430. return (u8) ((time * compensation) >> 6);
  431. }
  432. /* Set led pattern command */
  433. static int
  434. il_led_cmd(struct il_priv *il, unsigned long on, unsigned long off)
  435. {
  436. struct il_led_cmd led_cmd = {
  437. .id = IL_LED_LINK,
  438. .interval = IL_DEF_LED_INTRVL
  439. };
  440. int ret;
  441. if (!test_bit(S_READY, &il->status))
  442. return -EBUSY;
  443. if (il->blink_on == on && il->blink_off == off)
  444. return 0;
  445. if (off == 0) {
  446. /* led is SOLID_ON */
  447. on = IL_LED_SOLID;
  448. }
  449. D_LED("Led blink time compensation=%u\n",
  450. il->cfg->led_compensation);
  451. led_cmd.on =
  452. il_blink_compensation(il, on,
  453. il->cfg->led_compensation);
  454. led_cmd.off =
  455. il_blink_compensation(il, off,
  456. il->cfg->led_compensation);
  457. ret = il->ops->led->cmd(il, &led_cmd);
  458. if (!ret) {
  459. il->blink_on = on;
  460. il->blink_off = off;
  461. }
  462. return ret;
  463. }
  464. static void
  465. il_led_brightness_set(struct led_classdev *led_cdev,
  466. enum led_brightness brightness)
  467. {
  468. struct il_priv *il = container_of(led_cdev, struct il_priv, led);
  469. unsigned long on = 0;
  470. if (brightness > 0)
  471. on = IL_LED_SOLID;
  472. il_led_cmd(il, on, 0);
  473. }
  474. static int
  475. il_led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on,
  476. unsigned long *delay_off)
  477. {
  478. struct il_priv *il = container_of(led_cdev, struct il_priv, led);
  479. return il_led_cmd(il, *delay_on, *delay_off);
  480. }
  481. void
  482. il_leds_init(struct il_priv *il)
  483. {
  484. int mode = led_mode;
  485. int ret;
  486. if (mode == IL_LED_DEFAULT)
  487. mode = il->cfg->led_mode;
  488. il->led.name =
  489. kasprintf(GFP_KERNEL, "%s-led", wiphy_name(il->hw->wiphy));
  490. il->led.brightness_set = il_led_brightness_set;
  491. il->led.blink_set = il_led_blink_set;
  492. il->led.max_brightness = 1;
  493. switch (mode) {
  494. case IL_LED_DEFAULT:
  495. WARN_ON(1);
  496. break;
  497. case IL_LED_BLINK:
  498. il->led.default_trigger =
  499. ieee80211_create_tpt_led_trigger(il->hw,
  500. IEEE80211_TPT_LEDTRIG_FL_CONNECTED,
  501. il_blink,
  502. ARRAY_SIZE(il_blink));
  503. break;
  504. case IL_LED_RF_STATE:
  505. il->led.default_trigger = ieee80211_get_radio_led_name(il->hw);
  506. break;
  507. }
  508. ret = led_classdev_register(&il->pci_dev->dev, &il->led);
  509. if (ret) {
  510. kfree(il->led.name);
  511. return;
  512. }
  513. il->led_registered = true;
  514. }
  515. EXPORT_SYMBOL(il_leds_init);
  516. void
  517. il_leds_exit(struct il_priv *il)
  518. {
  519. if (!il->led_registered)
  520. return;
  521. led_classdev_unregister(&il->led);
  522. kfree(il->led.name);
  523. }
  524. EXPORT_SYMBOL(il_leds_exit);
  525. /************************** EEPROM BANDS ****************************
  526. *
  527. * The il_eeprom_band definitions below provide the mapping from the
  528. * EEPROM contents to the specific channel number supported for each
  529. * band.
  530. *
  531. * For example, il_priv->eeprom.band_3_channels[4] from the band_3
  532. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  533. * The specific geography and calibration information for that channel
  534. * is contained in the eeprom map itself.
  535. *
  536. * During init, we copy the eeprom information and channel map
  537. * information into il->channel_info_24/52 and il->channel_map_24/52
  538. *
  539. * channel_map_24/52 provides the idx in the channel_info array for a
  540. * given channel. We have to have two separate maps as there is channel
  541. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  542. * band_2
  543. *
  544. * A value of 0xff stored in the channel_map indicates that the channel
  545. * is not supported by the hardware at all.
  546. *
  547. * A value of 0xfe in the channel_map indicates that the channel is not
  548. * valid for Tx with the current hardware. This means that
  549. * while the system can tune and receive on a given channel, it may not
  550. * be able to associate or transmit any frames on that
  551. * channel. There is no corresponding channel information for that
  552. * entry.
  553. *
  554. *********************************************************************/
  555. /* 2.4 GHz */
  556. const u8 il_eeprom_band_1[14] = {
  557. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  558. };
  559. /* 5.2 GHz bands */
  560. static const u8 il_eeprom_band_2[] = { /* 4915-5080MHz */
  561. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  562. };
  563. static const u8 il_eeprom_band_3[] = { /* 5170-5320MHz */
  564. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  565. };
  566. static const u8 il_eeprom_band_4[] = { /* 5500-5700MHz */
  567. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  568. };
  569. static const u8 il_eeprom_band_5[] = { /* 5725-5825MHz */
  570. 145, 149, 153, 157, 161, 165
  571. };
  572. static const u8 il_eeprom_band_6[] = { /* 2.4 ht40 channel */
  573. 1, 2, 3, 4, 5, 6, 7
  574. };
  575. static const u8 il_eeprom_band_7[] = { /* 5.2 ht40 channel */
  576. 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
  577. };
  578. /******************************************************************************
  579. *
  580. * EEPROM related functions
  581. *
  582. ******************************************************************************/
  583. static int
  584. il_eeprom_verify_signature(struct il_priv *il)
  585. {
  586. u32 gp = _il_rd(il, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  587. int ret = 0;
  588. D_EEPROM("EEPROM signature=0x%08x\n", gp);
  589. switch (gp) {
  590. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  591. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  592. break;
  593. default:
  594. IL_ERR("bad EEPROM signature," "EEPROM_GP=0x%08x\n", gp);
  595. ret = -ENOENT;
  596. break;
  597. }
  598. return ret;
  599. }
  600. const u8 *
  601. il_eeprom_query_addr(const struct il_priv *il, size_t offset)
  602. {
  603. BUG_ON(offset >= il->cfg->eeprom_size);
  604. return &il->eeprom[offset];
  605. }
  606. EXPORT_SYMBOL(il_eeprom_query_addr);
  607. u16
  608. il_eeprom_query16(const struct il_priv *il, size_t offset)
  609. {
  610. if (!il->eeprom)
  611. return 0;
  612. return (u16) il->eeprom[offset] | ((u16) il->eeprom[offset + 1] << 8);
  613. }
  614. EXPORT_SYMBOL(il_eeprom_query16);
  615. /**
  616. * il_eeprom_init - read EEPROM contents
  617. *
  618. * Load the EEPROM contents from adapter into il->eeprom
  619. *
  620. * NOTE: This routine uses the non-debug IO access functions.
  621. */
  622. int
  623. il_eeprom_init(struct il_priv *il)
  624. {
  625. __le16 *e;
  626. u32 gp = _il_rd(il, CSR_EEPROM_GP);
  627. int sz;
  628. int ret;
  629. u16 addr;
  630. /* allocate eeprom */
  631. sz = il->cfg->eeprom_size;
  632. D_EEPROM("NVM size = %d\n", sz);
  633. il->eeprom = kzalloc(sz, GFP_KERNEL);
  634. if (!il->eeprom) {
  635. ret = -ENOMEM;
  636. goto alloc_err;
  637. }
  638. e = (__le16 *) il->eeprom;
  639. il->ops->lib->apm_ops.init(il);
  640. ret = il_eeprom_verify_signature(il);
  641. if (ret < 0) {
  642. IL_ERR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  643. ret = -ENOENT;
  644. goto err;
  645. }
  646. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  647. ret = il->ops->lib->eeprom_ops.acquire_semaphore(il);
  648. if (ret < 0) {
  649. IL_ERR("Failed to acquire EEPROM semaphore.\n");
  650. ret = -ENOENT;
  651. goto err;
  652. }
  653. /* eeprom is an array of 16bit values */
  654. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  655. u32 r;
  656. _il_wr(il, CSR_EEPROM_REG,
  657. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  658. ret =
  659. _il_poll_bit(il, CSR_EEPROM_REG,
  660. CSR_EEPROM_REG_READ_VALID_MSK,
  661. CSR_EEPROM_REG_READ_VALID_MSK,
  662. IL_EEPROM_ACCESS_TIMEOUT);
  663. if (ret < 0) {
  664. IL_ERR("Time out reading EEPROM[%d]\n", addr);
  665. goto done;
  666. }
  667. r = _il_rd(il, CSR_EEPROM_REG);
  668. e[addr / 2] = cpu_to_le16(r >> 16);
  669. }
  670. D_EEPROM("NVM Type: %s, version: 0x%x\n", "EEPROM",
  671. il_eeprom_query16(il, EEPROM_VERSION));
  672. ret = 0;
  673. done:
  674. il->ops->lib->eeprom_ops.release_semaphore(il);
  675. err:
  676. if (ret)
  677. il_eeprom_free(il);
  678. /* Reset chip to save power until we load uCode during "up". */
  679. il_apm_stop(il);
  680. alloc_err:
  681. return ret;
  682. }
  683. EXPORT_SYMBOL(il_eeprom_init);
  684. void
  685. il_eeprom_free(struct il_priv *il)
  686. {
  687. kfree(il->eeprom);
  688. il->eeprom = NULL;
  689. }
  690. EXPORT_SYMBOL(il_eeprom_free);
  691. static void
  692. il_init_band_reference(const struct il_priv *il, int eep_band,
  693. int *eeprom_ch_count,
  694. const struct il_eeprom_channel **eeprom_ch_info,
  695. const u8 **eeprom_ch_idx)
  696. {
  697. u32 offset =
  698. il->ops->lib->eeprom_ops.regulatory_bands[eep_band - 1];
  699. switch (eep_band) {
  700. case 1: /* 2.4GHz band */
  701. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_1);
  702. *eeprom_ch_info =
  703. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  704. offset);
  705. *eeprom_ch_idx = il_eeprom_band_1;
  706. break;
  707. case 2: /* 4.9GHz band */
  708. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_2);
  709. *eeprom_ch_info =
  710. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  711. offset);
  712. *eeprom_ch_idx = il_eeprom_band_2;
  713. break;
  714. case 3: /* 5.2GHz band */
  715. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_3);
  716. *eeprom_ch_info =
  717. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  718. offset);
  719. *eeprom_ch_idx = il_eeprom_band_3;
  720. break;
  721. case 4: /* 5.5GHz band */
  722. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_4);
  723. *eeprom_ch_info =
  724. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  725. offset);
  726. *eeprom_ch_idx = il_eeprom_band_4;
  727. break;
  728. case 5: /* 5.7GHz band */
  729. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_5);
  730. *eeprom_ch_info =
  731. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  732. offset);
  733. *eeprom_ch_idx = il_eeprom_band_5;
  734. break;
  735. case 6: /* 2.4GHz ht40 channels */
  736. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_6);
  737. *eeprom_ch_info =
  738. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  739. offset);
  740. *eeprom_ch_idx = il_eeprom_band_6;
  741. break;
  742. case 7: /* 5 GHz ht40 channels */
  743. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_7);
  744. *eeprom_ch_info =
  745. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  746. offset);
  747. *eeprom_ch_idx = il_eeprom_band_7;
  748. break;
  749. default:
  750. BUG();
  751. }
  752. }
  753. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  754. ? # x " " : "")
  755. /**
  756. * il_mod_ht40_chan_info - Copy ht40 channel info into driver's il.
  757. *
  758. * Does not set up a command, or touch hardware.
  759. */
  760. static int
  761. il_mod_ht40_chan_info(struct il_priv *il, enum ieee80211_band band, u16 channel,
  762. const struct il_eeprom_channel *eeprom_ch,
  763. u8 clear_ht40_extension_channel)
  764. {
  765. struct il_channel_info *ch_info;
  766. ch_info =
  767. (struct il_channel_info *)il_get_channel_info(il, band, channel);
  768. if (!il_is_channel_valid(ch_info))
  769. return -1;
  770. D_EEPROM("HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
  771. " Ad-Hoc %ssupported\n", ch_info->channel,
  772. il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
  773. CHECK_AND_PRINT(IBSS), CHECK_AND_PRINT(ACTIVE),
  774. CHECK_AND_PRINT(RADAR), CHECK_AND_PRINT(WIDE),
  775. CHECK_AND_PRINT(DFS), eeprom_ch->flags,
  776. eeprom_ch->max_power_avg,
  777. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS) &&
  778. !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ? "" : "not ");
  779. ch_info->ht40_eeprom = *eeprom_ch;
  780. ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
  781. ch_info->ht40_flags = eeprom_ch->flags;
  782. if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
  783. ch_info->ht40_extension_channel &=
  784. ~clear_ht40_extension_channel;
  785. return 0;
  786. }
  787. #define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  788. ? # x " " : "")
  789. /**
  790. * il_init_channel_map - Set up driver's info for all possible channels
  791. */
  792. int
  793. il_init_channel_map(struct il_priv *il)
  794. {
  795. int eeprom_ch_count = 0;
  796. const u8 *eeprom_ch_idx = NULL;
  797. const struct il_eeprom_channel *eeprom_ch_info = NULL;
  798. int band, ch;
  799. struct il_channel_info *ch_info;
  800. if (il->channel_count) {
  801. D_EEPROM("Channel map already initialized.\n");
  802. return 0;
  803. }
  804. D_EEPROM("Initializing regulatory info from EEPROM\n");
  805. il->channel_count =
  806. ARRAY_SIZE(il_eeprom_band_1) + ARRAY_SIZE(il_eeprom_band_2) +
  807. ARRAY_SIZE(il_eeprom_band_3) + ARRAY_SIZE(il_eeprom_band_4) +
  808. ARRAY_SIZE(il_eeprom_band_5);
  809. D_EEPROM("Parsing data for %d channels.\n", il->channel_count);
  810. il->channel_info =
  811. kzalloc(sizeof(struct il_channel_info) * il->channel_count,
  812. GFP_KERNEL);
  813. if (!il->channel_info) {
  814. IL_ERR("Could not allocate channel_info\n");
  815. il->channel_count = 0;
  816. return -ENOMEM;
  817. }
  818. ch_info = il->channel_info;
  819. /* Loop through the 5 EEPROM bands adding them in order to the
  820. * channel map we maintain (that contains additional information than
  821. * what just in the EEPROM) */
  822. for (band = 1; band <= 5; band++) {
  823. il_init_band_reference(il, band, &eeprom_ch_count,
  824. &eeprom_ch_info, &eeprom_ch_idx);
  825. /* Loop through each band adding each of the channels */
  826. for (ch = 0; ch < eeprom_ch_count; ch++) {
  827. ch_info->channel = eeprom_ch_idx[ch];
  828. ch_info->band =
  829. (band ==
  830. 1) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  831. /* permanently store EEPROM's channel regulatory flags
  832. * and max power in channel info database. */
  833. ch_info->eeprom = eeprom_ch_info[ch];
  834. /* Copy the run-time flags so they are there even on
  835. * invalid channels */
  836. ch_info->flags = eeprom_ch_info[ch].flags;
  837. /* First write that ht40 is not enabled, and then enable
  838. * one by one */
  839. ch_info->ht40_extension_channel =
  840. IEEE80211_CHAN_NO_HT40;
  841. if (!(il_is_channel_valid(ch_info))) {
  842. D_EEPROM("Ch. %d Flags %x [%sGHz] - "
  843. "No traffic\n", ch_info->channel,
  844. ch_info->flags,
  845. il_is_channel_a_band(ch_info) ? "5.2" :
  846. "2.4");
  847. ch_info++;
  848. continue;
  849. }
  850. /* Initialize regulatory-based run-time data */
  851. ch_info->max_power_avg = ch_info->curr_txpow =
  852. eeprom_ch_info[ch].max_power_avg;
  853. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  854. ch_info->min_power = 0;
  855. D_EEPROM("Ch. %d [%sGHz] " "%s%s%s%s%s%s(0x%02x %ddBm):"
  856. " Ad-Hoc %ssupported\n", ch_info->channel,
  857. il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
  858. CHECK_AND_PRINT_I(VALID),
  859. CHECK_AND_PRINT_I(IBSS),
  860. CHECK_AND_PRINT_I(ACTIVE),
  861. CHECK_AND_PRINT_I(RADAR),
  862. CHECK_AND_PRINT_I(WIDE),
  863. CHECK_AND_PRINT_I(DFS),
  864. eeprom_ch_info[ch].flags,
  865. eeprom_ch_info[ch].max_power_avg,
  866. ((eeprom_ch_info[ch].
  867. flags & EEPROM_CHANNEL_IBSS) &&
  868. !(eeprom_ch_info[ch].
  869. flags & EEPROM_CHANNEL_RADAR)) ? "" :
  870. "not ");
  871. ch_info++;
  872. }
  873. }
  874. /* Check if we do have HT40 channels */
  875. if (il->ops->lib->eeprom_ops.regulatory_bands[5] ==
  876. EEPROM_REGULATORY_BAND_NO_HT40 &&
  877. il->ops->lib->eeprom_ops.regulatory_bands[6] ==
  878. EEPROM_REGULATORY_BAND_NO_HT40)
  879. return 0;
  880. /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
  881. for (band = 6; band <= 7; band++) {
  882. enum ieee80211_band ieeeband;
  883. il_init_band_reference(il, band, &eeprom_ch_count,
  884. &eeprom_ch_info, &eeprom_ch_idx);
  885. /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
  886. ieeeband =
  887. (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  888. /* Loop through each band adding each of the channels */
  889. for (ch = 0; ch < eeprom_ch_count; ch++) {
  890. /* Set up driver's info for lower half */
  891. il_mod_ht40_chan_info(il, ieeeband, eeprom_ch_idx[ch],
  892. &eeprom_ch_info[ch],
  893. IEEE80211_CHAN_NO_HT40PLUS);
  894. /* Set up driver's info for upper half */
  895. il_mod_ht40_chan_info(il, ieeeband,
  896. eeprom_ch_idx[ch] + 4,
  897. &eeprom_ch_info[ch],
  898. IEEE80211_CHAN_NO_HT40MINUS);
  899. }
  900. }
  901. return 0;
  902. }
  903. EXPORT_SYMBOL(il_init_channel_map);
  904. /*
  905. * il_free_channel_map - undo allocations in il_init_channel_map
  906. */
  907. void
  908. il_free_channel_map(struct il_priv *il)
  909. {
  910. kfree(il->channel_info);
  911. il->channel_count = 0;
  912. }
  913. EXPORT_SYMBOL(il_free_channel_map);
  914. /**
  915. * il_get_channel_info - Find driver's ilate channel info
  916. *
  917. * Based on band and channel number.
  918. */
  919. const struct il_channel_info *
  920. il_get_channel_info(const struct il_priv *il, enum ieee80211_band band,
  921. u16 channel)
  922. {
  923. int i;
  924. switch (band) {
  925. case IEEE80211_BAND_5GHZ:
  926. for (i = 14; i < il->channel_count; i++) {
  927. if (il->channel_info[i].channel == channel)
  928. return &il->channel_info[i];
  929. }
  930. break;
  931. case IEEE80211_BAND_2GHZ:
  932. if (channel >= 1 && channel <= 14)
  933. return &il->channel_info[channel - 1];
  934. break;
  935. default:
  936. BUG();
  937. }
  938. return NULL;
  939. }
  940. EXPORT_SYMBOL(il_get_channel_info);
  941. /*
  942. * Setting power level allows the card to go to sleep when not busy.
  943. *
  944. * We calculate a sleep command based on the required latency, which
  945. * we get from mac80211. In order to handle thermal throttling, we can
  946. * also use pre-defined power levels.
  947. */
  948. /*
  949. * This defines the old power levels. They are still used by default
  950. * (level 1) and for thermal throttle (levels 3 through 5)
  951. */
  952. struct il_power_vec_entry {
  953. struct il_powertable_cmd cmd;
  954. u8 no_dtim; /* number of skip dtim */
  955. };
  956. static void
  957. il_power_sleep_cam_cmd(struct il_priv *il, struct il_powertable_cmd *cmd)
  958. {
  959. memset(cmd, 0, sizeof(*cmd));
  960. if (il->power_data.pci_pm)
  961. cmd->flags |= IL_POWER_PCI_PM_MSK;
  962. D_POWER("Sleep command for CAM\n");
  963. }
  964. static int
  965. il_set_power(struct il_priv *il, struct il_powertable_cmd *cmd)
  966. {
  967. D_POWER("Sending power/sleep command\n");
  968. D_POWER("Flags value = 0x%08X\n", cmd->flags);
  969. D_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  970. D_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  971. D_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  972. le32_to_cpu(cmd->sleep_interval[0]),
  973. le32_to_cpu(cmd->sleep_interval[1]),
  974. le32_to_cpu(cmd->sleep_interval[2]),
  975. le32_to_cpu(cmd->sleep_interval[3]),
  976. le32_to_cpu(cmd->sleep_interval[4]));
  977. return il_send_cmd_pdu(il, C_POWER_TBL,
  978. sizeof(struct il_powertable_cmd), cmd);
  979. }
  980. int
  981. il_power_set_mode(struct il_priv *il, struct il_powertable_cmd *cmd, bool force)
  982. {
  983. int ret;
  984. bool update_chains;
  985. lockdep_assert_held(&il->mutex);
  986. /* Don't update the RX chain when chain noise calibration is running */
  987. update_chains = il->chain_noise_data.state == IL_CHAIN_NOISE_DONE ||
  988. il->chain_noise_data.state == IL_CHAIN_NOISE_ALIVE;
  989. if (!memcmp(&il->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
  990. return 0;
  991. if (!il_is_ready_rf(il))
  992. return -EIO;
  993. /* scan complete use sleep_power_next, need to be updated */
  994. memcpy(&il->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
  995. if (test_bit(S_SCANNING, &il->status) && !force) {
  996. D_INFO("Defer power set mode while scanning\n");
  997. return 0;
  998. }
  999. if (cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  1000. set_bit(S_POWER_PMI, &il->status);
  1001. ret = il_set_power(il, cmd);
  1002. if (!ret) {
  1003. if (!(cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  1004. clear_bit(S_POWER_PMI, &il->status);
  1005. if (il->ops->lib->update_chain_flags && update_chains)
  1006. il->ops->lib->update_chain_flags(il);
  1007. else if (il->ops->lib->update_chain_flags)
  1008. D_POWER("Cannot update the power, chain noise "
  1009. "calibration running: %d\n",
  1010. il->chain_noise_data.state);
  1011. memcpy(&il->power_data.sleep_cmd, cmd, sizeof(*cmd));
  1012. } else
  1013. IL_ERR("set power fail, ret = %d", ret);
  1014. return ret;
  1015. }
  1016. int
  1017. il_power_update_mode(struct il_priv *il, bool force)
  1018. {
  1019. struct il_powertable_cmd cmd;
  1020. il_power_sleep_cam_cmd(il, &cmd);
  1021. return il_power_set_mode(il, &cmd, force);
  1022. }
  1023. EXPORT_SYMBOL(il_power_update_mode);
  1024. /* initialize to default */
  1025. void
  1026. il_power_initialize(struct il_priv *il)
  1027. {
  1028. u16 lctl = il_pcie_link_ctl(il);
  1029. il->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  1030. il->power_data.debug_sleep_level_override = -1;
  1031. memset(&il->power_data.sleep_cmd, 0, sizeof(il->power_data.sleep_cmd));
  1032. }
  1033. EXPORT_SYMBOL(il_power_initialize);
  1034. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  1035. * sending probe req. This should be set long enough to hear probe responses
  1036. * from more than one AP. */
  1037. #define IL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  1038. #define IL_ACTIVE_DWELL_TIME_52 (20)
  1039. #define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  1040. #define IL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  1041. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  1042. * Must be set longer than active dwell time.
  1043. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  1044. #define IL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  1045. #define IL_PASSIVE_DWELL_TIME_52 (10)
  1046. #define IL_PASSIVE_DWELL_BASE (100)
  1047. #define IL_CHANNEL_TUNE_TIME 5
  1048. static int
  1049. il_send_scan_abort(struct il_priv *il)
  1050. {
  1051. int ret;
  1052. struct il_rx_pkt *pkt;
  1053. struct il_host_cmd cmd = {
  1054. .id = C_SCAN_ABORT,
  1055. .flags = CMD_WANT_SKB,
  1056. };
  1057. /* Exit instantly with error when device is not ready
  1058. * to receive scan abort command or it does not perform
  1059. * hardware scan currently */
  1060. if (!test_bit(S_READY, &il->status) ||
  1061. !test_bit(S_GEO_CONFIGURED, &il->status) ||
  1062. !test_bit(S_SCAN_HW, &il->status) ||
  1063. test_bit(S_FW_ERROR, &il->status) ||
  1064. test_bit(S_EXIT_PENDING, &il->status))
  1065. return -EIO;
  1066. ret = il_send_cmd_sync(il, &cmd);
  1067. if (ret)
  1068. return ret;
  1069. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1070. if (pkt->u.status != CAN_ABORT_STATUS) {
  1071. /* The scan abort will return 1 for success or
  1072. * 2 for "failure". A failure condition can be
  1073. * due to simply not being in an active scan which
  1074. * can occur if we send the scan abort before we
  1075. * the microcode has notified us that a scan is
  1076. * completed. */
  1077. D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
  1078. ret = -EIO;
  1079. }
  1080. il_free_pages(il, cmd.reply_page);
  1081. return ret;
  1082. }
  1083. static void
  1084. il_complete_scan(struct il_priv *il, bool aborted)
  1085. {
  1086. /* check if scan was requested from mac80211 */
  1087. if (il->scan_request) {
  1088. D_SCAN("Complete scan in mac80211\n");
  1089. ieee80211_scan_completed(il->hw, aborted);
  1090. }
  1091. il->scan_vif = NULL;
  1092. il->scan_request = NULL;
  1093. }
  1094. void
  1095. il_force_scan_end(struct il_priv *il)
  1096. {
  1097. lockdep_assert_held(&il->mutex);
  1098. if (!test_bit(S_SCANNING, &il->status)) {
  1099. D_SCAN("Forcing scan end while not scanning\n");
  1100. return;
  1101. }
  1102. D_SCAN("Forcing scan end\n");
  1103. clear_bit(S_SCANNING, &il->status);
  1104. clear_bit(S_SCAN_HW, &il->status);
  1105. clear_bit(S_SCAN_ABORTING, &il->status);
  1106. il_complete_scan(il, true);
  1107. }
  1108. static void
  1109. il_do_scan_abort(struct il_priv *il)
  1110. {
  1111. int ret;
  1112. lockdep_assert_held(&il->mutex);
  1113. if (!test_bit(S_SCANNING, &il->status)) {
  1114. D_SCAN("Not performing scan to abort\n");
  1115. return;
  1116. }
  1117. if (test_and_set_bit(S_SCAN_ABORTING, &il->status)) {
  1118. D_SCAN("Scan abort in progress\n");
  1119. return;
  1120. }
  1121. ret = il_send_scan_abort(il);
  1122. if (ret) {
  1123. D_SCAN("Send scan abort failed %d\n", ret);
  1124. il_force_scan_end(il);
  1125. } else
  1126. D_SCAN("Successfully send scan abort\n");
  1127. }
  1128. /**
  1129. * il_scan_cancel - Cancel any currently executing HW scan
  1130. */
  1131. int
  1132. il_scan_cancel(struct il_priv *il)
  1133. {
  1134. D_SCAN("Queuing abort scan\n");
  1135. queue_work(il->workqueue, &il->abort_scan);
  1136. return 0;
  1137. }
  1138. EXPORT_SYMBOL(il_scan_cancel);
  1139. /**
  1140. * il_scan_cancel_timeout - Cancel any currently executing HW scan
  1141. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1142. *
  1143. */
  1144. int
  1145. il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
  1146. {
  1147. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  1148. lockdep_assert_held(&il->mutex);
  1149. D_SCAN("Scan cancel timeout\n");
  1150. il_do_scan_abort(il);
  1151. while (time_before_eq(jiffies, timeout)) {
  1152. if (!test_bit(S_SCAN_HW, &il->status))
  1153. break;
  1154. msleep(20);
  1155. }
  1156. return test_bit(S_SCAN_HW, &il->status);
  1157. }
  1158. EXPORT_SYMBOL(il_scan_cancel_timeout);
  1159. /* Service response to C_SCAN (0x80) */
  1160. static void
  1161. il_hdl_scan(struct il_priv *il, struct il_rx_buf *rxb)
  1162. {
  1163. #ifdef CONFIG_IWLEGACY_DEBUG
  1164. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1165. struct il_scanreq_notification *notif =
  1166. (struct il_scanreq_notification *)pkt->u.raw;
  1167. D_SCAN("Scan request status = 0x%x\n", notif->status);
  1168. #endif
  1169. }
  1170. /* Service N_SCAN_START (0x82) */
  1171. static void
  1172. il_hdl_scan_start(struct il_priv *il, struct il_rx_buf *rxb)
  1173. {
  1174. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1175. struct il_scanstart_notification *notif =
  1176. (struct il_scanstart_notification *)pkt->u.raw;
  1177. il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  1178. D_SCAN("Scan start: " "%d [802.11%s] "
  1179. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", notif->channel,
  1180. notif->band ? "bg" : "a", le32_to_cpu(notif->tsf_high),
  1181. le32_to_cpu(notif->tsf_low), notif->status, notif->beacon_timer);
  1182. }
  1183. /* Service N_SCAN_RESULTS (0x83) */
  1184. static void
  1185. il_hdl_scan_results(struct il_priv *il, struct il_rx_buf *rxb)
  1186. {
  1187. #ifdef CONFIG_IWLEGACY_DEBUG
  1188. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1189. struct il_scanresults_notification *notif =
  1190. (struct il_scanresults_notification *)pkt->u.raw;
  1191. D_SCAN("Scan ch.res: " "%d [802.11%s] " "(TSF: 0x%08X:%08X) - %d "
  1192. "elapsed=%lu usec\n", notif->channel, notif->band ? "bg" : "a",
  1193. le32_to_cpu(notif->tsf_high), le32_to_cpu(notif->tsf_low),
  1194. le32_to_cpu(notif->stats[0]),
  1195. le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
  1196. #endif
  1197. }
  1198. /* Service N_SCAN_COMPLETE (0x84) */
  1199. static void
  1200. il_hdl_scan_complete(struct il_priv *il, struct il_rx_buf *rxb)
  1201. {
  1202. #ifdef CONFIG_IWLEGACY_DEBUG
  1203. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1204. struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  1205. #endif
  1206. D_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  1207. scan_notif->scanned_channels, scan_notif->tsf_low,
  1208. scan_notif->tsf_high, scan_notif->status);
  1209. /* The HW is no longer scanning */
  1210. clear_bit(S_SCAN_HW, &il->status);
  1211. D_SCAN("Scan on %sGHz took %dms\n",
  1212. (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  1213. jiffies_to_msecs(jiffies - il->scan_start));
  1214. queue_work(il->workqueue, &il->scan_completed);
  1215. }
  1216. void
  1217. il_setup_rx_scan_handlers(struct il_priv *il)
  1218. {
  1219. /* scan handlers */
  1220. il->handlers[C_SCAN] = il_hdl_scan;
  1221. il->handlers[N_SCAN_START] = il_hdl_scan_start;
  1222. il->handlers[N_SCAN_RESULTS] = il_hdl_scan_results;
  1223. il->handlers[N_SCAN_COMPLETE] = il_hdl_scan_complete;
  1224. }
  1225. EXPORT_SYMBOL(il_setup_rx_scan_handlers);
  1226. inline u16
  1227. il_get_active_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1228. u8 n_probes)
  1229. {
  1230. if (band == IEEE80211_BAND_5GHZ)
  1231. return IL_ACTIVE_DWELL_TIME_52 +
  1232. IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  1233. else
  1234. return IL_ACTIVE_DWELL_TIME_24 +
  1235. IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  1236. }
  1237. EXPORT_SYMBOL(il_get_active_dwell_time);
  1238. u16
  1239. il_get_passive_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1240. struct ieee80211_vif *vif)
  1241. {
  1242. u16 value;
  1243. u16 passive =
  1244. (band ==
  1245. IEEE80211_BAND_2GHZ) ? IL_PASSIVE_DWELL_BASE +
  1246. IL_PASSIVE_DWELL_TIME_24 : IL_PASSIVE_DWELL_BASE +
  1247. IL_PASSIVE_DWELL_TIME_52;
  1248. if (il_is_any_associated(il)) {
  1249. /*
  1250. * If we're associated, we clamp the maximum passive
  1251. * dwell time to be 98% of the smallest beacon interval
  1252. * (minus 2 * channel tune time)
  1253. */
  1254. value = il->vif ? il->vif->bss_conf.beacon_int : 0;
  1255. if (value > IL_PASSIVE_DWELL_BASE || !value)
  1256. value = IL_PASSIVE_DWELL_BASE;
  1257. value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
  1258. passive = min(value, passive);
  1259. }
  1260. return passive;
  1261. }
  1262. EXPORT_SYMBOL(il_get_passive_dwell_time);
  1263. void
  1264. il_init_scan_params(struct il_priv *il)
  1265. {
  1266. u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
  1267. if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
  1268. il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  1269. if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
  1270. il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  1271. }
  1272. EXPORT_SYMBOL(il_init_scan_params);
  1273. static int
  1274. il_scan_initiate(struct il_priv *il, struct ieee80211_vif *vif)
  1275. {
  1276. int ret;
  1277. lockdep_assert_held(&il->mutex);
  1278. if (WARN_ON(!il->ops->utils->request_scan))
  1279. return -EOPNOTSUPP;
  1280. cancel_delayed_work(&il->scan_check);
  1281. if (!il_is_ready_rf(il)) {
  1282. IL_WARN("Request scan called when driver not ready.\n");
  1283. return -EIO;
  1284. }
  1285. if (test_bit(S_SCAN_HW, &il->status)) {
  1286. D_SCAN("Multiple concurrent scan requests in parallel.\n");
  1287. return -EBUSY;
  1288. }
  1289. if (test_bit(S_SCAN_ABORTING, &il->status)) {
  1290. D_SCAN("Scan request while abort pending.\n");
  1291. return -EBUSY;
  1292. }
  1293. D_SCAN("Starting scan...\n");
  1294. set_bit(S_SCANNING, &il->status);
  1295. il->scan_start = jiffies;
  1296. ret = il->ops->utils->request_scan(il, vif);
  1297. if (ret) {
  1298. clear_bit(S_SCANNING, &il->status);
  1299. return ret;
  1300. }
  1301. queue_delayed_work(il->workqueue, &il->scan_check,
  1302. IL_SCAN_CHECK_WATCHDOG);
  1303. return 0;
  1304. }
  1305. int
  1306. il_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1307. struct cfg80211_scan_request *req)
  1308. {
  1309. struct il_priv *il = hw->priv;
  1310. int ret;
  1311. D_MAC80211("enter\n");
  1312. if (req->n_channels == 0)
  1313. return -EINVAL;
  1314. mutex_lock(&il->mutex);
  1315. if (test_bit(S_SCANNING, &il->status)) {
  1316. D_SCAN("Scan already in progress.\n");
  1317. ret = -EAGAIN;
  1318. goto out_unlock;
  1319. }
  1320. /* mac80211 will only ask for one band at a time */
  1321. il->scan_request = req;
  1322. il->scan_vif = vif;
  1323. il->scan_band = req->channels[0]->band;
  1324. ret = il_scan_initiate(il, vif);
  1325. D_MAC80211("leave\n");
  1326. out_unlock:
  1327. mutex_unlock(&il->mutex);
  1328. return ret;
  1329. }
  1330. EXPORT_SYMBOL(il_mac_hw_scan);
  1331. static void
  1332. il_bg_scan_check(struct work_struct *data)
  1333. {
  1334. struct il_priv *il =
  1335. container_of(data, struct il_priv, scan_check.work);
  1336. D_SCAN("Scan check work\n");
  1337. /* Since we are here firmware does not finish scan and
  1338. * most likely is in bad shape, so we don't bother to
  1339. * send abort command, just force scan complete to mac80211 */
  1340. mutex_lock(&il->mutex);
  1341. il_force_scan_end(il);
  1342. mutex_unlock(&il->mutex);
  1343. }
  1344. /**
  1345. * il_fill_probe_req - fill in all required fields and IE for probe request
  1346. */
  1347. u16
  1348. il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
  1349. const u8 *ta, const u8 *ies, int ie_len, int left)
  1350. {
  1351. int len = 0;
  1352. u8 *pos = NULL;
  1353. /* Make sure there is enough space for the probe request,
  1354. * two mandatory IEs and the data */
  1355. left -= 24;
  1356. if (left < 0)
  1357. return 0;
  1358. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1359. memcpy(frame->da, il_bcast_addr, ETH_ALEN);
  1360. memcpy(frame->sa, ta, ETH_ALEN);
  1361. memcpy(frame->bssid, il_bcast_addr, ETH_ALEN);
  1362. frame->seq_ctrl = 0;
  1363. len += 24;
  1364. /* ...next IE... */
  1365. pos = &frame->u.probe_req.variable[0];
  1366. /* fill in our indirect SSID IE */
  1367. left -= 2;
  1368. if (left < 0)
  1369. return 0;
  1370. *pos++ = WLAN_EID_SSID;
  1371. *pos++ = 0;
  1372. len += 2;
  1373. if (WARN_ON(left < ie_len))
  1374. return len;
  1375. if (ies && ie_len) {
  1376. memcpy(pos, ies, ie_len);
  1377. len += ie_len;
  1378. }
  1379. return (u16) len;
  1380. }
  1381. EXPORT_SYMBOL(il_fill_probe_req);
  1382. static void
  1383. il_bg_abort_scan(struct work_struct *work)
  1384. {
  1385. struct il_priv *il = container_of(work, struct il_priv, abort_scan);
  1386. D_SCAN("Abort scan work\n");
  1387. /* We keep scan_check work queued in case when firmware will not
  1388. * report back scan completed notification */
  1389. mutex_lock(&il->mutex);
  1390. il_scan_cancel_timeout(il, 200);
  1391. mutex_unlock(&il->mutex);
  1392. }
  1393. static void
  1394. il_bg_scan_completed(struct work_struct *work)
  1395. {
  1396. struct il_priv *il = container_of(work, struct il_priv, scan_completed);
  1397. bool aborted;
  1398. D_SCAN("Completed scan.\n");
  1399. cancel_delayed_work(&il->scan_check);
  1400. mutex_lock(&il->mutex);
  1401. aborted = test_and_clear_bit(S_SCAN_ABORTING, &il->status);
  1402. if (aborted)
  1403. D_SCAN("Aborted scan completed.\n");
  1404. if (!test_and_clear_bit(S_SCANNING, &il->status)) {
  1405. D_SCAN("Scan already completed.\n");
  1406. goto out_settings;
  1407. }
  1408. il_complete_scan(il, aborted);
  1409. out_settings:
  1410. /* Can we still talk to firmware ? */
  1411. if (!il_is_ready_rf(il))
  1412. goto out;
  1413. /*
  1414. * We do not commit power settings while scan is pending,
  1415. * do it now if the settings changed.
  1416. */
  1417. il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
  1418. il_set_tx_power(il, il->tx_power_next, false);
  1419. il->ops->utils->post_scan(il);
  1420. out:
  1421. mutex_unlock(&il->mutex);
  1422. }
  1423. void
  1424. il_setup_scan_deferred_work(struct il_priv *il)
  1425. {
  1426. INIT_WORK(&il->scan_completed, il_bg_scan_completed);
  1427. INIT_WORK(&il->abort_scan, il_bg_abort_scan);
  1428. INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
  1429. }
  1430. EXPORT_SYMBOL(il_setup_scan_deferred_work);
  1431. void
  1432. il_cancel_scan_deferred_work(struct il_priv *il)
  1433. {
  1434. cancel_work_sync(&il->abort_scan);
  1435. cancel_work_sync(&il->scan_completed);
  1436. if (cancel_delayed_work_sync(&il->scan_check)) {
  1437. mutex_lock(&il->mutex);
  1438. il_force_scan_end(il);
  1439. mutex_unlock(&il->mutex);
  1440. }
  1441. }
  1442. EXPORT_SYMBOL(il_cancel_scan_deferred_work);
  1443. /* il->sta_lock must be held */
  1444. static void
  1445. il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
  1446. {
  1447. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
  1448. IL_ERR("ACTIVATE a non DRIVER active station id %u addr %pM\n",
  1449. sta_id, il->stations[sta_id].sta.sta.addr);
  1450. if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
  1451. D_ASSOC("STA id %u addr %pM already present"
  1452. " in uCode (according to driver)\n", sta_id,
  1453. il->stations[sta_id].sta.sta.addr);
  1454. } else {
  1455. il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
  1456. D_ASSOC("Added STA id %u addr %pM to uCode\n", sta_id,
  1457. il->stations[sta_id].sta.sta.addr);
  1458. }
  1459. }
  1460. static int
  1461. il_process_add_sta_resp(struct il_priv *il, struct il_addsta_cmd *addsta,
  1462. struct il_rx_pkt *pkt, bool sync)
  1463. {
  1464. u8 sta_id = addsta->sta.sta_id;
  1465. unsigned long flags;
  1466. int ret = -EIO;
  1467. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1468. IL_ERR("Bad return from C_ADD_STA (0x%08X)\n", pkt->hdr.flags);
  1469. return ret;
  1470. }
  1471. D_INFO("Processing response for adding station %u\n", sta_id);
  1472. spin_lock_irqsave(&il->sta_lock, flags);
  1473. switch (pkt->u.add_sta.status) {
  1474. case ADD_STA_SUCCESS_MSK:
  1475. D_INFO("C_ADD_STA PASSED\n");
  1476. il_sta_ucode_activate(il, sta_id);
  1477. ret = 0;
  1478. break;
  1479. case ADD_STA_NO_ROOM_IN_TBL:
  1480. IL_ERR("Adding station %d failed, no room in table.\n", sta_id);
  1481. break;
  1482. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  1483. IL_ERR("Adding station %d failed, no block ack resource.\n",
  1484. sta_id);
  1485. break;
  1486. case ADD_STA_MODIFY_NON_EXIST_STA:
  1487. IL_ERR("Attempting to modify non-existing station %d\n",
  1488. sta_id);
  1489. break;
  1490. default:
  1491. D_ASSOC("Received C_ADD_STA:(0x%08X)\n", pkt->u.add_sta.status);
  1492. break;
  1493. }
  1494. D_INFO("%s station id %u addr %pM\n",
  1495. il->stations[sta_id].sta.mode ==
  1496. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", sta_id,
  1497. il->stations[sta_id].sta.sta.addr);
  1498. /*
  1499. * XXX: The MAC address in the command buffer is often changed from
  1500. * the original sent to the device. That is, the MAC address
  1501. * written to the command buffer often is not the same MAC address
  1502. * read from the command buffer when the command returns. This
  1503. * issue has not yet been resolved and this debugging is left to
  1504. * observe the problem.
  1505. */
  1506. D_INFO("%s station according to cmd buffer %pM\n",
  1507. il->stations[sta_id].sta.mode ==
  1508. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", addsta->sta.addr);
  1509. spin_unlock_irqrestore(&il->sta_lock, flags);
  1510. return ret;
  1511. }
  1512. static void
  1513. il_add_sta_callback(struct il_priv *il, struct il_device_cmd *cmd,
  1514. struct il_rx_pkt *pkt)
  1515. {
  1516. struct il_addsta_cmd *addsta = (struct il_addsta_cmd *)cmd->cmd.payload;
  1517. il_process_add_sta_resp(il, addsta, pkt, false);
  1518. }
  1519. int
  1520. il_send_add_sta(struct il_priv *il, struct il_addsta_cmd *sta, u8 flags)
  1521. {
  1522. struct il_rx_pkt *pkt = NULL;
  1523. int ret = 0;
  1524. u8 data[sizeof(*sta)];
  1525. struct il_host_cmd cmd = {
  1526. .id = C_ADD_STA,
  1527. .flags = flags,
  1528. .data = data,
  1529. };
  1530. u8 sta_id __maybe_unused = sta->sta.sta_id;
  1531. D_INFO("Adding sta %u (%pM) %ssynchronously\n", sta_id, sta->sta.addr,
  1532. flags & CMD_ASYNC ? "a" : "");
  1533. if (flags & CMD_ASYNC)
  1534. cmd.callback = il_add_sta_callback;
  1535. else {
  1536. cmd.flags |= CMD_WANT_SKB;
  1537. might_sleep();
  1538. }
  1539. cmd.len = il->ops->utils->build_addsta_hcmd(sta, data);
  1540. ret = il_send_cmd(il, &cmd);
  1541. if (ret || (flags & CMD_ASYNC))
  1542. return ret;
  1543. if (ret == 0) {
  1544. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1545. ret = il_process_add_sta_resp(il, sta, pkt, true);
  1546. }
  1547. il_free_pages(il, cmd.reply_page);
  1548. return ret;
  1549. }
  1550. EXPORT_SYMBOL(il_send_add_sta);
  1551. static void
  1552. il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
  1553. {
  1554. struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
  1555. __le32 sta_flags;
  1556. u8 mimo_ps_mode;
  1557. if (!sta || !sta_ht_inf->ht_supported)
  1558. goto done;
  1559. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  1560. D_ASSOC("spatial multiplexing power save mode: %s\n",
  1561. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ? "static" :
  1562. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ? "dynamic" :
  1563. "disabled");
  1564. sta_flags = il->stations[idx].sta.station_flags;
  1565. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  1566. switch (mimo_ps_mode) {
  1567. case WLAN_HT_CAP_SM_PS_STATIC:
  1568. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  1569. break;
  1570. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  1571. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  1572. break;
  1573. case WLAN_HT_CAP_SM_PS_DISABLED:
  1574. break;
  1575. default:
  1576. IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
  1577. break;
  1578. }
  1579. sta_flags |=
  1580. cpu_to_le32((u32) sta_ht_inf->
  1581. ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  1582. sta_flags |=
  1583. cpu_to_le32((u32) sta_ht_inf->
  1584. ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  1585. if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
  1586. sta_flags |= STA_FLG_HT40_EN_MSK;
  1587. else
  1588. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  1589. il->stations[idx].sta.station_flags = sta_flags;
  1590. done:
  1591. return;
  1592. }
  1593. /**
  1594. * il_prep_station - Prepare station information for addition
  1595. *
  1596. * should be called with sta_lock held
  1597. */
  1598. u8
  1599. il_prep_station(struct il_priv *il, const u8 *addr, bool is_ap,
  1600. struct ieee80211_sta *sta)
  1601. {
  1602. struct il_station_entry *station;
  1603. int i;
  1604. u8 sta_id = IL_INVALID_STATION;
  1605. u16 rate;
  1606. if (is_ap)
  1607. sta_id = IL_AP_ID;
  1608. else if (is_broadcast_ether_addr(addr))
  1609. sta_id = il->hw_params.bcast_id;
  1610. else
  1611. for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
  1612. if (!compare_ether_addr
  1613. (il->stations[i].sta.sta.addr, addr)) {
  1614. sta_id = i;
  1615. break;
  1616. }
  1617. if (!il->stations[i].used &&
  1618. sta_id == IL_INVALID_STATION)
  1619. sta_id = i;
  1620. }
  1621. /*
  1622. * These two conditions have the same outcome, but keep them
  1623. * separate
  1624. */
  1625. if (unlikely(sta_id == IL_INVALID_STATION))
  1626. return sta_id;
  1627. /*
  1628. * uCode is not able to deal with multiple requests to add a
  1629. * station. Keep track if one is in progress so that we do not send
  1630. * another.
  1631. */
  1632. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1633. D_INFO("STA %d already in process of being added.\n", sta_id);
  1634. return sta_id;
  1635. }
  1636. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1637. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
  1638. !compare_ether_addr(il->stations[sta_id].sta.sta.addr, addr)) {
  1639. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1640. sta_id, addr);
  1641. return sta_id;
  1642. }
  1643. station = &il->stations[sta_id];
  1644. station->used = IL_STA_DRIVER_ACTIVE;
  1645. D_ASSOC("Add STA to driver ID %d: %pM\n", sta_id, addr);
  1646. il->num_stations++;
  1647. /* Set up the C_ADD_STA command to send to device */
  1648. memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
  1649. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  1650. station->sta.mode = 0;
  1651. station->sta.sta.sta_id = sta_id;
  1652. station->sta.station_flags = 0;
  1653. /*
  1654. * OK to call unconditionally, since local stations (IBSS BSSID
  1655. * STA and broadcast STA) pass in a NULL sta, and mac80211
  1656. * doesn't allow HT IBSS.
  1657. */
  1658. il_set_ht_add_station(il, sta_id, sta);
  1659. /* 3945 only */
  1660. rate = (il->band == IEEE80211_BAND_5GHZ) ? RATE_6M_PLCP : RATE_1M_PLCP;
  1661. /* Turn on both antennas for the station... */
  1662. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  1663. return sta_id;
  1664. }
  1665. EXPORT_SYMBOL_GPL(il_prep_station);
  1666. #define STA_WAIT_TIMEOUT (HZ/2)
  1667. /**
  1668. * il_add_station_common -
  1669. */
  1670. int
  1671. il_add_station_common(struct il_priv *il, const u8 *addr, bool is_ap,
  1672. struct ieee80211_sta *sta, u8 *sta_id_r)
  1673. {
  1674. unsigned long flags_spin;
  1675. int ret = 0;
  1676. u8 sta_id;
  1677. struct il_addsta_cmd sta_cmd;
  1678. *sta_id_r = 0;
  1679. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1680. sta_id = il_prep_station(il, addr, is_ap, sta);
  1681. if (sta_id == IL_INVALID_STATION) {
  1682. IL_ERR("Unable to prepare station %pM for addition\n", addr);
  1683. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1684. return -EINVAL;
  1685. }
  1686. /*
  1687. * uCode is not able to deal with multiple requests to add a
  1688. * station. Keep track if one is in progress so that we do not send
  1689. * another.
  1690. */
  1691. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1692. D_INFO("STA %d already in process of being added.\n", sta_id);
  1693. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1694. return -EEXIST;
  1695. }
  1696. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1697. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1698. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1699. sta_id, addr);
  1700. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1701. return -EEXIST;
  1702. }
  1703. il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
  1704. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1705. sizeof(struct il_addsta_cmd));
  1706. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1707. /* Add station to device's station table */
  1708. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1709. if (ret) {
  1710. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1711. IL_ERR("Adding station %pM failed.\n",
  1712. il->stations[sta_id].sta.sta.addr);
  1713. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1714. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  1715. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1716. }
  1717. *sta_id_r = sta_id;
  1718. return ret;
  1719. }
  1720. EXPORT_SYMBOL(il_add_station_common);
  1721. /**
  1722. * il_sta_ucode_deactivate - deactivate ucode status for a station
  1723. *
  1724. * il->sta_lock must be held
  1725. */
  1726. static void
  1727. il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
  1728. {
  1729. /* Ucode must be active and driver must be non active */
  1730. if ((il->stations[sta_id].
  1731. used & (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
  1732. IL_STA_UCODE_ACTIVE)
  1733. IL_ERR("removed non active STA %u\n", sta_id);
  1734. il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
  1735. memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
  1736. D_ASSOC("Removed STA %u\n", sta_id);
  1737. }
  1738. static int
  1739. il_send_remove_station(struct il_priv *il, const u8 * addr, int sta_id,
  1740. bool temporary)
  1741. {
  1742. struct il_rx_pkt *pkt;
  1743. int ret;
  1744. unsigned long flags_spin;
  1745. struct il_rem_sta_cmd rm_sta_cmd;
  1746. struct il_host_cmd cmd = {
  1747. .id = C_REM_STA,
  1748. .len = sizeof(struct il_rem_sta_cmd),
  1749. .flags = CMD_SYNC,
  1750. .data = &rm_sta_cmd,
  1751. };
  1752. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  1753. rm_sta_cmd.num_sta = 1;
  1754. memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
  1755. cmd.flags |= CMD_WANT_SKB;
  1756. ret = il_send_cmd(il, &cmd);
  1757. if (ret)
  1758. return ret;
  1759. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1760. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1761. IL_ERR("Bad return from C_REM_STA (0x%08X)\n", pkt->hdr.flags);
  1762. ret = -EIO;
  1763. }
  1764. if (!ret) {
  1765. switch (pkt->u.rem_sta.status) {
  1766. case REM_STA_SUCCESS_MSK:
  1767. if (!temporary) {
  1768. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1769. il_sta_ucode_deactivate(il, sta_id);
  1770. spin_unlock_irqrestore(&il->sta_lock,
  1771. flags_spin);
  1772. }
  1773. D_ASSOC("C_REM_STA PASSED\n");
  1774. break;
  1775. default:
  1776. ret = -EIO;
  1777. IL_ERR("C_REM_STA failed\n");
  1778. break;
  1779. }
  1780. }
  1781. il_free_pages(il, cmd.reply_page);
  1782. return ret;
  1783. }
  1784. /**
  1785. * il_remove_station - Remove driver's knowledge of station.
  1786. */
  1787. int
  1788. il_remove_station(struct il_priv *il, const u8 sta_id, const u8 * addr)
  1789. {
  1790. unsigned long flags;
  1791. if (!il_is_ready(il)) {
  1792. D_INFO("Unable to remove station %pM, device not ready.\n",
  1793. addr);
  1794. /*
  1795. * It is typical for stations to be removed when we are
  1796. * going down. Return success since device will be down
  1797. * soon anyway
  1798. */
  1799. return 0;
  1800. }
  1801. D_ASSOC("Removing STA from driver:%d %pM\n", sta_id, addr);
  1802. if (WARN_ON(sta_id == IL_INVALID_STATION))
  1803. return -EINVAL;
  1804. spin_lock_irqsave(&il->sta_lock, flags);
  1805. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  1806. D_INFO("Removing %pM but non DRIVER active\n", addr);
  1807. goto out_err;
  1808. }
  1809. if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1810. D_INFO("Removing %pM but non UCODE active\n", addr);
  1811. goto out_err;
  1812. }
  1813. if (il->stations[sta_id].used & IL_STA_LOCAL) {
  1814. kfree(il->stations[sta_id].lq);
  1815. il->stations[sta_id].lq = NULL;
  1816. }
  1817. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1818. il->num_stations--;
  1819. BUG_ON(il->num_stations < 0);
  1820. spin_unlock_irqrestore(&il->sta_lock, flags);
  1821. return il_send_remove_station(il, addr, sta_id, false);
  1822. out_err:
  1823. spin_unlock_irqrestore(&il->sta_lock, flags);
  1824. return -EINVAL;
  1825. }
  1826. EXPORT_SYMBOL_GPL(il_remove_station);
  1827. /**
  1828. * il_clear_ucode_stations - clear ucode station table bits
  1829. *
  1830. * This function clears all the bits in the driver indicating
  1831. * which stations are active in the ucode. Call when something
  1832. * other than explicit station management would cause this in
  1833. * the ucode, e.g. unassociated RXON.
  1834. */
  1835. void
  1836. il_clear_ucode_stations(struct il_priv *il)
  1837. {
  1838. int i;
  1839. unsigned long flags_spin;
  1840. bool cleared = false;
  1841. D_INFO("Clearing ucode stations in driver\n");
  1842. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1843. for (i = 0; i < il->hw_params.max_stations; i++) {
  1844. if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
  1845. D_INFO("Clearing ucode active for station %d\n", i);
  1846. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1847. cleared = true;
  1848. }
  1849. }
  1850. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1851. if (!cleared)
  1852. D_INFO("No active stations found to be cleared\n");
  1853. }
  1854. EXPORT_SYMBOL(il_clear_ucode_stations);
  1855. /**
  1856. * il_restore_stations() - Restore driver known stations to device
  1857. *
  1858. * All stations considered active by driver, but not present in ucode, is
  1859. * restored.
  1860. *
  1861. * Function sleeps.
  1862. */
  1863. void
  1864. il_restore_stations(struct il_priv *il)
  1865. {
  1866. struct il_addsta_cmd sta_cmd;
  1867. struct il_link_quality_cmd lq;
  1868. unsigned long flags_spin;
  1869. int i;
  1870. bool found = false;
  1871. int ret;
  1872. bool send_lq;
  1873. if (!il_is_ready(il)) {
  1874. D_INFO("Not ready yet, not restoring any stations.\n");
  1875. return;
  1876. }
  1877. D_ASSOC("Restoring all known stations ... start.\n");
  1878. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1879. for (i = 0; i < il->hw_params.max_stations; i++) {
  1880. if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
  1881. !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
  1882. D_ASSOC("Restoring sta %pM\n",
  1883. il->stations[i].sta.sta.addr);
  1884. il->stations[i].sta.mode = 0;
  1885. il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
  1886. found = true;
  1887. }
  1888. }
  1889. for (i = 0; i < il->hw_params.max_stations; i++) {
  1890. if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
  1891. memcpy(&sta_cmd, &il->stations[i].sta,
  1892. sizeof(struct il_addsta_cmd));
  1893. send_lq = false;
  1894. if (il->stations[i].lq) {
  1895. memcpy(&lq, il->stations[i].lq,
  1896. sizeof(struct il_link_quality_cmd));
  1897. send_lq = true;
  1898. }
  1899. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1900. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1901. if (ret) {
  1902. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1903. IL_ERR("Adding station %pM failed.\n",
  1904. il->stations[i].sta.sta.addr);
  1905. il->stations[i].used &= ~IL_STA_DRIVER_ACTIVE;
  1906. il->stations[i].used &=
  1907. ~IL_STA_UCODE_INPROGRESS;
  1908. spin_unlock_irqrestore(&il->sta_lock,
  1909. flags_spin);
  1910. }
  1911. /*
  1912. * Rate scaling has already been initialized, send
  1913. * current LQ command
  1914. */
  1915. if (send_lq)
  1916. il_send_lq_cmd(il, &lq, CMD_SYNC, true);
  1917. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1918. il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
  1919. }
  1920. }
  1921. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1922. if (!found)
  1923. D_INFO("Restoring all known stations"
  1924. " .... no stations to be restored.\n");
  1925. else
  1926. D_INFO("Restoring all known stations" " .... complete.\n");
  1927. }
  1928. EXPORT_SYMBOL(il_restore_stations);
  1929. int
  1930. il_get_free_ucode_key_idx(struct il_priv *il)
  1931. {
  1932. int i;
  1933. for (i = 0; i < il->sta_key_max_num; i++)
  1934. if (!test_and_set_bit(i, &il->ucode_key_table))
  1935. return i;
  1936. return WEP_INVALID_OFFSET;
  1937. }
  1938. EXPORT_SYMBOL(il_get_free_ucode_key_idx);
  1939. void
  1940. il_dealloc_bcast_stations(struct il_priv *il)
  1941. {
  1942. unsigned long flags;
  1943. int i;
  1944. spin_lock_irqsave(&il->sta_lock, flags);
  1945. for (i = 0; i < il->hw_params.max_stations; i++) {
  1946. if (!(il->stations[i].used & IL_STA_BCAST))
  1947. continue;
  1948. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1949. il->num_stations--;
  1950. BUG_ON(il->num_stations < 0);
  1951. kfree(il->stations[i].lq);
  1952. il->stations[i].lq = NULL;
  1953. }
  1954. spin_unlock_irqrestore(&il->sta_lock, flags);
  1955. }
  1956. EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
  1957. #ifdef CONFIG_IWLEGACY_DEBUG
  1958. static void
  1959. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1960. {
  1961. int i;
  1962. D_RATE("lq station id 0x%x\n", lq->sta_id);
  1963. D_RATE("lq ant 0x%X 0x%X\n", lq->general_params.single_stream_ant_msk,
  1964. lq->general_params.dual_stream_ant_msk);
  1965. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1966. D_RATE("lq idx %d 0x%X\n", i, lq->rs_table[i].rate_n_flags);
  1967. }
  1968. #else
  1969. static inline void
  1970. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1971. {
  1972. }
  1973. #endif
  1974. /**
  1975. * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
  1976. *
  1977. * It sometimes happens when a HT rate has been in use and we
  1978. * loose connectivity with AP then mac80211 will first tell us that the
  1979. * current channel is not HT anymore before removing the station. In such a
  1980. * scenario the RXON flags will be updated to indicate we are not
  1981. * communicating HT anymore, but the LQ command may still contain HT rates.
  1982. * Test for this to prevent driver from sending LQ command between the time
  1983. * RXON flags are updated and when LQ command is updated.
  1984. */
  1985. static bool
  1986. il_is_lq_table_valid(struct il_priv *il, struct il_link_quality_cmd *lq)
  1987. {
  1988. int i;
  1989. if (il->ht.enabled)
  1990. return true;
  1991. D_INFO("Channel %u is not an HT channel\n", il->active.channel);
  1992. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  1993. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  1994. D_INFO("idx %d of LQ expects HT channel\n", i);
  1995. return false;
  1996. }
  1997. }
  1998. return true;
  1999. }
  2000. /**
  2001. * il_send_lq_cmd() - Send link quality command
  2002. * @init: This command is sent as part of station initialization right
  2003. * after station has been added.
  2004. *
  2005. * The link quality command is sent as the last step of station creation.
  2006. * This is the special case in which init is set and we call a callback in
  2007. * this case to clear the state indicating that station creation is in
  2008. * progress.
  2009. */
  2010. int
  2011. il_send_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq,
  2012. u8 flags, bool init)
  2013. {
  2014. int ret = 0;
  2015. unsigned long flags_spin;
  2016. struct il_host_cmd cmd = {
  2017. .id = C_TX_LINK_QUALITY_CMD,
  2018. .len = sizeof(struct il_link_quality_cmd),
  2019. .flags = flags,
  2020. .data = lq,
  2021. };
  2022. if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
  2023. return -EINVAL;
  2024. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2025. if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  2026. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2027. return -EINVAL;
  2028. }
  2029. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2030. il_dump_lq_cmd(il, lq);
  2031. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  2032. if (il_is_lq_table_valid(il, lq))
  2033. ret = il_send_cmd(il, &cmd);
  2034. else
  2035. ret = -EINVAL;
  2036. if (cmd.flags & CMD_ASYNC)
  2037. return ret;
  2038. if (init) {
  2039. D_INFO("init LQ command complete,"
  2040. " clearing sta addition status for sta %d\n",
  2041. lq->sta_id);
  2042. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2043. il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  2044. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2045. }
  2046. return ret;
  2047. }
  2048. EXPORT_SYMBOL(il_send_lq_cmd);
  2049. int
  2050. il_mac_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2051. struct ieee80211_sta *sta)
  2052. {
  2053. struct il_priv *il = hw->priv;
  2054. struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
  2055. int ret;
  2056. D_INFO("received request to remove station %pM\n", sta->addr);
  2057. mutex_lock(&il->mutex);
  2058. D_INFO("proceeding to remove station %pM\n", sta->addr);
  2059. ret = il_remove_station(il, sta_common->sta_id, sta->addr);
  2060. if (ret)
  2061. IL_ERR("Error removing station %pM\n", sta->addr);
  2062. mutex_unlock(&il->mutex);
  2063. return ret;
  2064. }
  2065. EXPORT_SYMBOL(il_mac_sta_remove);
  2066. /************************** RX-FUNCTIONS ****************************/
  2067. /*
  2068. * Rx theory of operation
  2069. *
  2070. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  2071. * each of which point to Receive Buffers to be filled by the NIC. These get
  2072. * used not only for Rx frames, but for any command response or notification
  2073. * from the NIC. The driver and NIC manage the Rx buffers by means
  2074. * of idxes into the circular buffer.
  2075. *
  2076. * Rx Queue Indexes
  2077. * The host/firmware share two idx registers for managing the Rx buffers.
  2078. *
  2079. * The READ idx maps to the first position that the firmware may be writing
  2080. * to -- the driver can read up to (but not including) this position and get
  2081. * good data.
  2082. * The READ idx is managed by the firmware once the card is enabled.
  2083. *
  2084. * The WRITE idx maps to the last position the driver has read from -- the
  2085. * position preceding WRITE is the last slot the firmware can place a packet.
  2086. *
  2087. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2088. * WRITE = READ.
  2089. *
  2090. * During initialization, the host sets up the READ queue position to the first
  2091. * IDX position, and WRITE to the last (READ - 1 wrapped)
  2092. *
  2093. * When the firmware places a packet in a buffer, it will advance the READ idx
  2094. * and fire the RX interrupt. The driver can then query the READ idx and
  2095. * process as many packets as possible, moving the WRITE idx forward as it
  2096. * resets the Rx queue buffers with new memory.
  2097. *
  2098. * The management in the driver is as follows:
  2099. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2100. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2101. * to replenish the iwl->rxq->rx_free.
  2102. * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
  2103. * iwl->rxq is replenished and the READ IDX is updated (updating the
  2104. * 'processed' and 'read' driver idxes as well)
  2105. * + A received packet is processed and handed to the kernel network stack,
  2106. * detached from the iwl->rxq. The driver 'processed' idx is updated.
  2107. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2108. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2109. * IDX is not incremented and iwl->status(RX_STALLED) is set. If there
  2110. * were enough free buffers and RX_STALLED is set it is cleared.
  2111. *
  2112. *
  2113. * Driver sequence:
  2114. *
  2115. * il_rx_queue_alloc() Allocates rx_free
  2116. * il_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2117. * il_rx_queue_restock
  2118. * il_rx_queue_restock() Moves available buffers from rx_free into Rx
  2119. * queue, updates firmware pointers, and updates
  2120. * the WRITE idx. If insufficient rx_free buffers
  2121. * are available, schedules il_rx_replenish
  2122. *
  2123. * -- enable interrupts --
  2124. * ISR - il_rx() Detach il_rx_bufs from pool up to the
  2125. * READ IDX, detaching the SKB from the pool.
  2126. * Moves the packet buffer from queue to rx_used.
  2127. * Calls il_rx_queue_restock to refill any empty
  2128. * slots.
  2129. * ...
  2130. *
  2131. */
  2132. /**
  2133. * il_rx_queue_space - Return number of free slots available in queue.
  2134. */
  2135. int
  2136. il_rx_queue_space(const struct il_rx_queue *q)
  2137. {
  2138. int s = q->read - q->write;
  2139. if (s <= 0)
  2140. s += RX_QUEUE_SIZE;
  2141. /* keep some buffer to not confuse full and empty queue */
  2142. s -= 2;
  2143. if (s < 0)
  2144. s = 0;
  2145. return s;
  2146. }
  2147. EXPORT_SYMBOL(il_rx_queue_space);
  2148. /**
  2149. * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  2150. */
  2151. void
  2152. il_rx_queue_update_write_ptr(struct il_priv *il, struct il_rx_queue *q)
  2153. {
  2154. unsigned long flags;
  2155. u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
  2156. u32 reg;
  2157. spin_lock_irqsave(&q->lock, flags);
  2158. if (q->need_update == 0)
  2159. goto exit_unlock;
  2160. /* If power-saving is in use, make sure device is awake */
  2161. if (test_bit(S_POWER_PMI, &il->status)) {
  2162. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2163. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2164. D_INFO("Rx queue requesting wakeup," " GP1 = 0x%x\n",
  2165. reg);
  2166. il_set_bit(il, CSR_GP_CNTRL,
  2167. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2168. goto exit_unlock;
  2169. }
  2170. q->write_actual = (q->write & ~0x7);
  2171. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2172. /* Else device is assumed to be awake */
  2173. } else {
  2174. /* Device expects a multiple of 8 */
  2175. q->write_actual = (q->write & ~0x7);
  2176. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2177. }
  2178. q->need_update = 0;
  2179. exit_unlock:
  2180. spin_unlock_irqrestore(&q->lock, flags);
  2181. }
  2182. EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
  2183. int
  2184. il_rx_queue_alloc(struct il_priv *il)
  2185. {
  2186. struct il_rx_queue *rxq = &il->rxq;
  2187. struct device *dev = &il->pci_dev->dev;
  2188. int i;
  2189. spin_lock_init(&rxq->lock);
  2190. INIT_LIST_HEAD(&rxq->rx_free);
  2191. INIT_LIST_HEAD(&rxq->rx_used);
  2192. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  2193. rxq->bd =
  2194. dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
  2195. GFP_KERNEL);
  2196. if (!rxq->bd)
  2197. goto err_bd;
  2198. rxq->rb_stts =
  2199. dma_alloc_coherent(dev, sizeof(struct il_rb_status),
  2200. &rxq->rb_stts_dma, GFP_KERNEL);
  2201. if (!rxq->rb_stts)
  2202. goto err_rb;
  2203. /* Fill the rx_used queue with _all_ of the Rx buffers */
  2204. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  2205. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  2206. /* Set us so that we have processed and used all buffers, but have
  2207. * not restocked the Rx queue with fresh buffers */
  2208. rxq->read = rxq->write = 0;
  2209. rxq->write_actual = 0;
  2210. rxq->free_count = 0;
  2211. rxq->need_update = 0;
  2212. return 0;
  2213. err_rb:
  2214. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  2215. rxq->bd_dma);
  2216. err_bd:
  2217. return -ENOMEM;
  2218. }
  2219. EXPORT_SYMBOL(il_rx_queue_alloc);
  2220. void
  2221. il_hdl_spectrum_measurement(struct il_priv *il, struct il_rx_buf *rxb)
  2222. {
  2223. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2224. struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2225. if (!report->state) {
  2226. D_11H("Spectrum Measure Notification: Start\n");
  2227. return;
  2228. }
  2229. memcpy(&il->measure_report, report, sizeof(*report));
  2230. il->measurement_status |= MEASUREMENT_READY;
  2231. }
  2232. EXPORT_SYMBOL(il_hdl_spectrum_measurement);
  2233. /*
  2234. * returns non-zero if packet should be dropped
  2235. */
  2236. int
  2237. il_set_decrypted_flag(struct il_priv *il, struct ieee80211_hdr *hdr,
  2238. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2239. {
  2240. u16 fc = le16_to_cpu(hdr->frame_control);
  2241. /*
  2242. * All contexts have the same setting here due to it being
  2243. * a module parameter, so OK to check any context.
  2244. */
  2245. if (il->active.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2246. return 0;
  2247. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2248. return 0;
  2249. D_RX("decrypt_res:0x%x\n", decrypt_res);
  2250. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2251. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2252. /* The uCode has got a bad phase 1 Key, pushes the packet.
  2253. * Decryption will be done in SW. */
  2254. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2255. RX_RES_STATUS_BAD_KEY_TTAK)
  2256. break;
  2257. case RX_RES_STATUS_SEC_TYPE_WEP:
  2258. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2259. RX_RES_STATUS_BAD_ICV_MIC) {
  2260. /* bad ICV, the packet is destroyed since the
  2261. * decryption is inplace, drop it */
  2262. D_RX("Packet destroyed\n");
  2263. return -1;
  2264. }
  2265. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2266. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2267. RX_RES_STATUS_DECRYPT_OK) {
  2268. D_RX("hw decrypt successfully!!!\n");
  2269. stats->flag |= RX_FLAG_DECRYPTED;
  2270. }
  2271. break;
  2272. default:
  2273. break;
  2274. }
  2275. return 0;
  2276. }
  2277. EXPORT_SYMBOL(il_set_decrypted_flag);
  2278. /**
  2279. * il_txq_update_write_ptr - Send new write idx to hardware
  2280. */
  2281. void
  2282. il_txq_update_write_ptr(struct il_priv *il, struct il_tx_queue *txq)
  2283. {
  2284. u32 reg = 0;
  2285. int txq_id = txq->q.id;
  2286. if (txq->need_update == 0)
  2287. return;
  2288. /* if we're trying to save power */
  2289. if (test_bit(S_POWER_PMI, &il->status)) {
  2290. /* wake up nic if it's powered down ...
  2291. * uCode will wake up, and interrupt us again, so next
  2292. * time we'll skip this part. */
  2293. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2294. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2295. D_INFO("Tx queue %d requesting wakeup," " GP1 = 0x%x\n",
  2296. txq_id, reg);
  2297. il_set_bit(il, CSR_GP_CNTRL,
  2298. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2299. return;
  2300. }
  2301. il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2302. /*
  2303. * else not in power-save mode,
  2304. * uCode will never sleep when we're
  2305. * trying to tx (during RFKILL, we're not trying to tx).
  2306. */
  2307. } else
  2308. _il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2309. txq->need_update = 0;
  2310. }
  2311. EXPORT_SYMBOL(il_txq_update_write_ptr);
  2312. /**
  2313. * il_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  2314. */
  2315. void
  2316. il_tx_queue_unmap(struct il_priv *il, int txq_id)
  2317. {
  2318. struct il_tx_queue *txq = &il->txq[txq_id];
  2319. struct il_queue *q = &txq->q;
  2320. if (q->n_bd == 0)
  2321. return;
  2322. while (q->write_ptr != q->read_ptr) {
  2323. il->ops->lib->txq_free_tfd(il, txq);
  2324. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2325. }
  2326. }
  2327. EXPORT_SYMBOL(il_tx_queue_unmap);
  2328. /**
  2329. * il_tx_queue_free - Deallocate DMA queue.
  2330. * @txq: Transmit queue to deallocate.
  2331. *
  2332. * Empty queue by removing and destroying all BD's.
  2333. * Free all buffers.
  2334. * 0-fill, but do not free "txq" descriptor structure.
  2335. */
  2336. void
  2337. il_tx_queue_free(struct il_priv *il, int txq_id)
  2338. {
  2339. struct il_tx_queue *txq = &il->txq[txq_id];
  2340. struct device *dev = &il->pci_dev->dev;
  2341. int i;
  2342. il_tx_queue_unmap(il, txq_id);
  2343. /* De-alloc array of command/tx buffers */
  2344. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  2345. kfree(txq->cmd[i]);
  2346. /* De-alloc circular buffer of TFDs */
  2347. if (txq->q.n_bd)
  2348. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2349. txq->tfds, txq->q.dma_addr);
  2350. /* De-alloc array of per-TFD driver data */
  2351. kfree(txq->skbs);
  2352. txq->skbs = NULL;
  2353. /* deallocate arrays */
  2354. kfree(txq->cmd);
  2355. kfree(txq->meta);
  2356. txq->cmd = NULL;
  2357. txq->meta = NULL;
  2358. /* 0-fill queue descriptor structure */
  2359. memset(txq, 0, sizeof(*txq));
  2360. }
  2361. EXPORT_SYMBOL(il_tx_queue_free);
  2362. /**
  2363. * il_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
  2364. */
  2365. void
  2366. il_cmd_queue_unmap(struct il_priv *il)
  2367. {
  2368. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2369. struct il_queue *q = &txq->q;
  2370. int i;
  2371. if (q->n_bd == 0)
  2372. return;
  2373. while (q->read_ptr != q->write_ptr) {
  2374. i = il_get_cmd_idx(q, q->read_ptr, 0);
  2375. if (txq->meta[i].flags & CMD_MAPPED) {
  2376. pci_unmap_single(il->pci_dev,
  2377. dma_unmap_addr(&txq->meta[i], mapping),
  2378. dma_unmap_len(&txq->meta[i], len),
  2379. PCI_DMA_BIDIRECTIONAL);
  2380. txq->meta[i].flags = 0;
  2381. }
  2382. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2383. }
  2384. i = q->n_win;
  2385. if (txq->meta[i].flags & CMD_MAPPED) {
  2386. pci_unmap_single(il->pci_dev,
  2387. dma_unmap_addr(&txq->meta[i], mapping),
  2388. dma_unmap_len(&txq->meta[i], len),
  2389. PCI_DMA_BIDIRECTIONAL);
  2390. txq->meta[i].flags = 0;
  2391. }
  2392. }
  2393. EXPORT_SYMBOL(il_cmd_queue_unmap);
  2394. /**
  2395. * il_cmd_queue_free - Deallocate DMA queue.
  2396. * @txq: Transmit queue to deallocate.
  2397. *
  2398. * Empty queue by removing and destroying all BD's.
  2399. * Free all buffers.
  2400. * 0-fill, but do not free "txq" descriptor structure.
  2401. */
  2402. void
  2403. il_cmd_queue_free(struct il_priv *il)
  2404. {
  2405. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2406. struct device *dev = &il->pci_dev->dev;
  2407. int i;
  2408. il_cmd_queue_unmap(il);
  2409. /* De-alloc array of command/tx buffers */
  2410. for (i = 0; i <= TFD_CMD_SLOTS; i++)
  2411. kfree(txq->cmd[i]);
  2412. /* De-alloc circular buffer of TFDs */
  2413. if (txq->q.n_bd)
  2414. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2415. txq->tfds, txq->q.dma_addr);
  2416. /* deallocate arrays */
  2417. kfree(txq->cmd);
  2418. kfree(txq->meta);
  2419. txq->cmd = NULL;
  2420. txq->meta = NULL;
  2421. /* 0-fill queue descriptor structure */
  2422. memset(txq, 0, sizeof(*txq));
  2423. }
  2424. EXPORT_SYMBOL(il_cmd_queue_free);
  2425. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  2426. * DMA services
  2427. *
  2428. * Theory of operation
  2429. *
  2430. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  2431. * of buffer descriptors, each of which points to one or more data buffers for
  2432. * the device to read from or fill. Driver and device exchange status of each
  2433. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  2434. * entries in each circular buffer, to protect against confusing empty and full
  2435. * queue states.
  2436. *
  2437. * The device reads or writes the data in the queues via the device's several
  2438. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  2439. *
  2440. * For Tx queue, there are low mark and high mark limits. If, after queuing
  2441. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  2442. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  2443. * Tx queue resumed.
  2444. *
  2445. * See more detailed info in 4965.h.
  2446. ***************************************************/
  2447. int
  2448. il_queue_space(const struct il_queue *q)
  2449. {
  2450. int s = q->read_ptr - q->write_ptr;
  2451. if (q->read_ptr > q->write_ptr)
  2452. s -= q->n_bd;
  2453. if (s <= 0)
  2454. s += q->n_win;
  2455. /* keep some reserve to not confuse empty and full situations */
  2456. s -= 2;
  2457. if (s < 0)
  2458. s = 0;
  2459. return s;
  2460. }
  2461. EXPORT_SYMBOL(il_queue_space);
  2462. /**
  2463. * il_queue_init - Initialize queue's high/low-water and read/write idxes
  2464. */
  2465. static int
  2466. il_queue_init(struct il_priv *il, struct il_queue *q, int count, int slots_num,
  2467. u32 id)
  2468. {
  2469. q->n_bd = count;
  2470. q->n_win = slots_num;
  2471. q->id = id;
  2472. /* count must be power-of-two size, otherwise il_queue_inc_wrap
  2473. * and il_queue_dec_wrap are broken. */
  2474. BUG_ON(!is_power_of_2(count));
  2475. /* slots_num must be power-of-two size, otherwise
  2476. * il_get_cmd_idx is broken. */
  2477. BUG_ON(!is_power_of_2(slots_num));
  2478. q->low_mark = q->n_win / 4;
  2479. if (q->low_mark < 4)
  2480. q->low_mark = 4;
  2481. q->high_mark = q->n_win / 8;
  2482. if (q->high_mark < 2)
  2483. q->high_mark = 2;
  2484. q->write_ptr = q->read_ptr = 0;
  2485. return 0;
  2486. }
  2487. /**
  2488. * il_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  2489. */
  2490. static int
  2491. il_tx_queue_alloc(struct il_priv *il, struct il_tx_queue *txq, u32 id)
  2492. {
  2493. struct device *dev = &il->pci_dev->dev;
  2494. size_t tfd_sz = il->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  2495. /* Driver ilate data, only for Tx (not command) queues,
  2496. * not shared with device. */
  2497. if (id != il->cmd_queue) {
  2498. txq->skbs = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(struct skb *),
  2499. GFP_KERNEL);
  2500. if (!txq->skbs) {
  2501. IL_ERR("Fail to alloc skbs\n");
  2502. goto error;
  2503. }
  2504. } else
  2505. txq->skbs = NULL;
  2506. /* Circular buffer of transmit frame descriptors (TFDs),
  2507. * shared with device */
  2508. txq->tfds =
  2509. dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr, GFP_KERNEL);
  2510. if (!txq->tfds) {
  2511. IL_ERR("Fail to alloc TFDs\n");
  2512. goto error;
  2513. }
  2514. txq->q.id = id;
  2515. return 0;
  2516. error:
  2517. kfree(txq->skbs);
  2518. txq->skbs = NULL;
  2519. return -ENOMEM;
  2520. }
  2521. /**
  2522. * il_tx_queue_init - Allocate and initialize one tx/cmd queue
  2523. */
  2524. int
  2525. il_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq, int slots_num,
  2526. u32 txq_id)
  2527. {
  2528. int i, len;
  2529. int ret;
  2530. int actual_slots = slots_num;
  2531. /*
  2532. * Alloc buffer array for commands (Tx or other types of commands).
  2533. * For the command queue (#4/#9), allocate command space + one big
  2534. * command for scan, since scan command is very huge; the system will
  2535. * not have two scans at the same time, so only one is needed.
  2536. * For normal Tx queues (all other queues), no super-size command
  2537. * space is needed.
  2538. */
  2539. if (txq_id == il->cmd_queue)
  2540. actual_slots++;
  2541. txq->meta =
  2542. kzalloc(sizeof(struct il_cmd_meta) * actual_slots, GFP_KERNEL);
  2543. txq->cmd =
  2544. kzalloc(sizeof(struct il_device_cmd *) * actual_slots, GFP_KERNEL);
  2545. if (!txq->meta || !txq->cmd)
  2546. goto out_free_arrays;
  2547. len = sizeof(struct il_device_cmd);
  2548. for (i = 0; i < actual_slots; i++) {
  2549. /* only happens for cmd queue */
  2550. if (i == slots_num)
  2551. len = IL_MAX_CMD_SIZE;
  2552. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  2553. if (!txq->cmd[i])
  2554. goto err;
  2555. }
  2556. /* Alloc driver data array and TFD circular buffer */
  2557. ret = il_tx_queue_alloc(il, txq, txq_id);
  2558. if (ret)
  2559. goto err;
  2560. txq->need_update = 0;
  2561. /*
  2562. * For the default queues 0-3, set up the swq_id
  2563. * already -- all others need to get one later
  2564. * (if they need one at all).
  2565. */
  2566. if (txq_id < 4)
  2567. il_set_swq_id(txq, txq_id, txq_id);
  2568. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  2569. * il_queue_inc_wrap and il_queue_dec_wrap are broken. */
  2570. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  2571. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2572. il_queue_init(il, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  2573. /* Tell device where to find queue */
  2574. il->ops->lib->txq_init(il, txq);
  2575. return 0;
  2576. err:
  2577. for (i = 0; i < actual_slots; i++)
  2578. kfree(txq->cmd[i]);
  2579. out_free_arrays:
  2580. kfree(txq->meta);
  2581. kfree(txq->cmd);
  2582. return -ENOMEM;
  2583. }
  2584. EXPORT_SYMBOL(il_tx_queue_init);
  2585. void
  2586. il_tx_queue_reset(struct il_priv *il, struct il_tx_queue *txq, int slots_num,
  2587. u32 txq_id)
  2588. {
  2589. int actual_slots = slots_num;
  2590. if (txq_id == il->cmd_queue)
  2591. actual_slots++;
  2592. memset(txq->meta, 0, sizeof(struct il_cmd_meta) * actual_slots);
  2593. txq->need_update = 0;
  2594. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2595. il_queue_init(il, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  2596. /* Tell device where to find queue */
  2597. il->ops->lib->txq_init(il, txq);
  2598. }
  2599. EXPORT_SYMBOL(il_tx_queue_reset);
  2600. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  2601. /**
  2602. * il_enqueue_hcmd - enqueue a uCode command
  2603. * @il: device ilate data point
  2604. * @cmd: a point to the ucode command structure
  2605. *
  2606. * The function returns < 0 values to indicate the operation is
  2607. * failed. On success, it turns the idx (> 0) of command in the
  2608. * command queue.
  2609. */
  2610. int
  2611. il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  2612. {
  2613. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2614. struct il_queue *q = &txq->q;
  2615. struct il_device_cmd *out_cmd;
  2616. struct il_cmd_meta *out_meta;
  2617. dma_addr_t phys_addr;
  2618. unsigned long flags;
  2619. int len;
  2620. u32 idx;
  2621. u16 fix_size;
  2622. cmd->len = il->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  2623. fix_size = (u16) (cmd->len + sizeof(out_cmd->hdr));
  2624. /* If any of the command structures end up being larger than
  2625. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  2626. * we will need to increase the size of the TFD entries
  2627. * Also, check to see if command buffer should not exceed the size
  2628. * of device_cmd and max_cmd_size. */
  2629. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  2630. !(cmd->flags & CMD_SIZE_HUGE));
  2631. BUG_ON(fix_size > IL_MAX_CMD_SIZE);
  2632. if (il_is_rfkill(il) || il_is_ctkill(il)) {
  2633. IL_WARN("Not sending command - %s KILL\n",
  2634. il_is_rfkill(il) ? "RF" : "CT");
  2635. return -EIO;
  2636. }
  2637. spin_lock_irqsave(&il->hcmd_lock, flags);
  2638. if (il_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  2639. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2640. IL_ERR("Restarting adapter due to command queue full\n");
  2641. queue_work(il->workqueue, &il->restart);
  2642. return -ENOSPC;
  2643. }
  2644. idx = il_get_cmd_idx(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
  2645. out_cmd = txq->cmd[idx];
  2646. out_meta = &txq->meta[idx];
  2647. if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  2648. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2649. return -ENOSPC;
  2650. }
  2651. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  2652. out_meta->flags = cmd->flags | CMD_MAPPED;
  2653. if (cmd->flags & CMD_WANT_SKB)
  2654. out_meta->source = cmd;
  2655. if (cmd->flags & CMD_ASYNC)
  2656. out_meta->callback = cmd->callback;
  2657. out_cmd->hdr.cmd = cmd->id;
  2658. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  2659. /* At this point, the out_cmd now has all of the incoming cmd
  2660. * information */
  2661. out_cmd->hdr.flags = 0;
  2662. out_cmd->hdr.sequence =
  2663. cpu_to_le16(QUEUE_TO_SEQ(il->cmd_queue) | IDX_TO_SEQ(q->write_ptr));
  2664. if (cmd->flags & CMD_SIZE_HUGE)
  2665. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  2666. len = sizeof(struct il_device_cmd);
  2667. if (idx == TFD_CMD_SLOTS)
  2668. len = IL_MAX_CMD_SIZE;
  2669. #ifdef CONFIG_IWLEGACY_DEBUG
  2670. switch (out_cmd->hdr.cmd) {
  2671. case C_TX_LINK_QUALITY_CMD:
  2672. case C_SENSITIVITY:
  2673. D_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
  2674. "%d bytes at %d[%d]:%d\n",
  2675. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2676. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  2677. q->write_ptr, idx, il->cmd_queue);
  2678. break;
  2679. default:
  2680. D_HC("Sending command %s (#%x), seq: 0x%04X, "
  2681. "%d bytes at %d[%d]:%d\n",
  2682. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2683. le16_to_cpu(out_cmd->hdr.sequence), fix_size, q->write_ptr,
  2684. idx, il->cmd_queue);
  2685. }
  2686. #endif
  2687. txq->need_update = 1;
  2688. if (il->ops->lib->txq_update_byte_cnt_tbl)
  2689. /* Set up entry in queue's byte count circular buffer */
  2690. il->ops->lib->txq_update_byte_cnt_tbl(il, txq, 0);
  2691. phys_addr =
  2692. pci_map_single(il->pci_dev, &out_cmd->hdr, fix_size,
  2693. PCI_DMA_BIDIRECTIONAL);
  2694. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  2695. dma_unmap_len_set(out_meta, len, fix_size);
  2696. il->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr, fix_size, 1,
  2697. U32_PAD(cmd->len));
  2698. /* Increment and update queue's write idx */
  2699. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  2700. il_txq_update_write_ptr(il, txq);
  2701. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2702. return idx;
  2703. }
  2704. /**
  2705. * il_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  2706. *
  2707. * When FW advances 'R' idx, all entries between old and new 'R' idx
  2708. * need to be reclaimed. As result, some free space forms. If there is
  2709. * enough free space (> low mark), wake the stack that feeds us.
  2710. */
  2711. static void
  2712. il_hcmd_queue_reclaim(struct il_priv *il, int txq_id, int idx, int cmd_idx)
  2713. {
  2714. struct il_tx_queue *txq = &il->txq[txq_id];
  2715. struct il_queue *q = &txq->q;
  2716. int nfreed = 0;
  2717. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  2718. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  2719. "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
  2720. q->write_ptr, q->read_ptr);
  2721. return;
  2722. }
  2723. for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  2724. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2725. if (nfreed++ > 0) {
  2726. IL_ERR("HCMD skipped: idx (%d) %d %d\n", idx,
  2727. q->write_ptr, q->read_ptr);
  2728. queue_work(il->workqueue, &il->restart);
  2729. }
  2730. }
  2731. }
  2732. /**
  2733. * il_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2734. * @rxb: Rx buffer to reclaim
  2735. *
  2736. * If an Rx buffer has an async callback associated with it the callback
  2737. * will be executed. The attached skb (if present) will only be freed
  2738. * if the callback returns 1
  2739. */
  2740. void
  2741. il_tx_cmd_complete(struct il_priv *il, struct il_rx_buf *rxb)
  2742. {
  2743. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2744. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2745. int txq_id = SEQ_TO_QUEUE(sequence);
  2746. int idx = SEQ_TO_IDX(sequence);
  2747. int cmd_idx;
  2748. bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  2749. struct il_device_cmd *cmd;
  2750. struct il_cmd_meta *meta;
  2751. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2752. unsigned long flags;
  2753. /* If a Tx command is being handled and it isn't in the actual
  2754. * command queue then there a command routing bug has been introduced
  2755. * in the queue management code. */
  2756. if (WARN
  2757. (txq_id != il->cmd_queue,
  2758. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  2759. txq_id, il->cmd_queue, sequence, il->txq[il->cmd_queue].q.read_ptr,
  2760. il->txq[il->cmd_queue].q.write_ptr)) {
  2761. il_print_hex_error(il, pkt, 32);
  2762. return;
  2763. }
  2764. cmd_idx = il_get_cmd_idx(&txq->q, idx, huge);
  2765. cmd = txq->cmd[cmd_idx];
  2766. meta = &txq->meta[cmd_idx];
  2767. txq->time_stamp = jiffies;
  2768. pci_unmap_single(il->pci_dev, dma_unmap_addr(meta, mapping),
  2769. dma_unmap_len(meta, len), PCI_DMA_BIDIRECTIONAL);
  2770. /* Input error checking is done when commands are added to queue. */
  2771. if (meta->flags & CMD_WANT_SKB) {
  2772. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  2773. rxb->page = NULL;
  2774. } else if (meta->callback)
  2775. meta->callback(il, cmd, pkt);
  2776. spin_lock_irqsave(&il->hcmd_lock, flags);
  2777. il_hcmd_queue_reclaim(il, txq_id, idx, cmd_idx);
  2778. if (!(meta->flags & CMD_ASYNC)) {
  2779. clear_bit(S_HCMD_ACTIVE, &il->status);
  2780. D_INFO("Clearing HCMD_ACTIVE for command %s\n",
  2781. il_get_cmd_string(cmd->hdr.cmd));
  2782. wake_up(&il->wait_command_queue);
  2783. }
  2784. /* Mark as unmapped */
  2785. meta->flags = 0;
  2786. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2787. }
  2788. EXPORT_SYMBOL(il_tx_cmd_complete);
  2789. MODULE_DESCRIPTION("iwl-legacy: common functions for 3945 and 4965");
  2790. MODULE_VERSION(IWLWIFI_VERSION);
  2791. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  2792. MODULE_LICENSE("GPL");
  2793. /*
  2794. * set bt_coex_active to true, uCode will do kill/defer
  2795. * every time the priority line is asserted (BT is sending signals on the
  2796. * priority line in the PCIx).
  2797. * set bt_coex_active to false, uCode will ignore the BT activity and
  2798. * perform the normal operation
  2799. *
  2800. * User might experience transmit issue on some platform due to WiFi/BT
  2801. * co-exist problem. The possible behaviors are:
  2802. * Able to scan and finding all the available AP
  2803. * Not able to associate with any AP
  2804. * On those platforms, WiFi communication can be restored by set
  2805. * "bt_coex_active" module parameter to "false"
  2806. *
  2807. * default: bt_coex_active = true (BT_COEX_ENABLE)
  2808. */
  2809. static bool bt_coex_active = true;
  2810. module_param(bt_coex_active, bool, S_IRUGO);
  2811. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
  2812. u32 il_debug_level;
  2813. EXPORT_SYMBOL(il_debug_level);
  2814. const u8 il_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  2815. EXPORT_SYMBOL(il_bcast_addr);
  2816. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  2817. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  2818. static void
  2819. il_init_ht_hw_capab(const struct il_priv *il,
  2820. struct ieee80211_sta_ht_cap *ht_info,
  2821. enum ieee80211_band band)
  2822. {
  2823. u16 max_bit_rate = 0;
  2824. u8 rx_chains_num = il->hw_params.rx_chains_num;
  2825. u8 tx_chains_num = il->hw_params.tx_chains_num;
  2826. ht_info->cap = 0;
  2827. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  2828. ht_info->ht_supported = true;
  2829. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  2830. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  2831. if (il->hw_params.ht40_channel & BIT(band)) {
  2832. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2833. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  2834. ht_info->mcs.rx_mask[4] = 0x01;
  2835. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  2836. }
  2837. if (il->cfg->mod_params->amsdu_size_8K)
  2838. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  2839. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  2840. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  2841. ht_info->mcs.rx_mask[0] = 0xFF;
  2842. if (rx_chains_num >= 2)
  2843. ht_info->mcs.rx_mask[1] = 0xFF;
  2844. if (rx_chains_num >= 3)
  2845. ht_info->mcs.rx_mask[2] = 0xFF;
  2846. /* Highest supported Rx data rate */
  2847. max_bit_rate *= rx_chains_num;
  2848. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  2849. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  2850. /* Tx MCS capabilities */
  2851. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2852. if (tx_chains_num != rx_chains_num) {
  2853. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  2854. ht_info->mcs.tx_params |=
  2855. ((tx_chains_num -
  2856. 1) << IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  2857. }
  2858. }
  2859. /**
  2860. * il_init_geos - Initialize mac80211's geo/channel info based from eeprom
  2861. */
  2862. int
  2863. il_init_geos(struct il_priv *il)
  2864. {
  2865. struct il_channel_info *ch;
  2866. struct ieee80211_supported_band *sband;
  2867. struct ieee80211_channel *channels;
  2868. struct ieee80211_channel *geo_ch;
  2869. struct ieee80211_rate *rates;
  2870. int i = 0;
  2871. s8 max_tx_power = 0;
  2872. if (il->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  2873. il->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  2874. D_INFO("Geography modes already initialized.\n");
  2875. set_bit(S_GEO_CONFIGURED, &il->status);
  2876. return 0;
  2877. }
  2878. channels =
  2879. kzalloc(sizeof(struct ieee80211_channel) * il->channel_count,
  2880. GFP_KERNEL);
  2881. if (!channels)
  2882. return -ENOMEM;
  2883. rates =
  2884. kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
  2885. GFP_KERNEL);
  2886. if (!rates) {
  2887. kfree(channels);
  2888. return -ENOMEM;
  2889. }
  2890. /* 5.2GHz channels start after the 2.4GHz channels */
  2891. sband = &il->bands[IEEE80211_BAND_5GHZ];
  2892. sband->channels = &channels[ARRAY_SIZE(il_eeprom_band_1)];
  2893. /* just OFDM */
  2894. sband->bitrates = &rates[IL_FIRST_OFDM_RATE];
  2895. sband->n_bitrates = RATE_COUNT_LEGACY - IL_FIRST_OFDM_RATE;
  2896. if (il->cfg->sku & IL_SKU_N)
  2897. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_5GHZ);
  2898. sband = &il->bands[IEEE80211_BAND_2GHZ];
  2899. sband->channels = channels;
  2900. /* OFDM & CCK */
  2901. sband->bitrates = rates;
  2902. sband->n_bitrates = RATE_COUNT_LEGACY;
  2903. if (il->cfg->sku & IL_SKU_N)
  2904. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_2GHZ);
  2905. il->ieee_channels = channels;
  2906. il->ieee_rates = rates;
  2907. for (i = 0; i < il->channel_count; i++) {
  2908. ch = &il->channel_info[i];
  2909. if (!il_is_channel_valid(ch))
  2910. continue;
  2911. sband = &il->bands[ch->band];
  2912. geo_ch = &sband->channels[sband->n_channels++];
  2913. geo_ch->center_freq =
  2914. ieee80211_channel_to_frequency(ch->channel, ch->band);
  2915. geo_ch->max_power = ch->max_power_avg;
  2916. geo_ch->max_antenna_gain = 0xff;
  2917. geo_ch->hw_value = ch->channel;
  2918. if (il_is_channel_valid(ch)) {
  2919. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  2920. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  2921. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  2922. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  2923. if (ch->flags & EEPROM_CHANNEL_RADAR)
  2924. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  2925. geo_ch->flags |= ch->ht40_extension_channel;
  2926. if (ch->max_power_avg > max_tx_power)
  2927. max_tx_power = ch->max_power_avg;
  2928. } else {
  2929. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  2930. }
  2931. D_INFO("Channel %d Freq=%d[%sGHz] %s flag=0x%X\n", ch->channel,
  2932. geo_ch->center_freq,
  2933. il_is_channel_a_band(ch) ? "5.2" : "2.4",
  2934. geo_ch->
  2935. flags & IEEE80211_CHAN_DISABLED ? "restricted" : "valid",
  2936. geo_ch->flags);
  2937. }
  2938. il->tx_power_device_lmt = max_tx_power;
  2939. il->tx_power_user_lmt = max_tx_power;
  2940. il->tx_power_next = max_tx_power;
  2941. if (il->bands[IEEE80211_BAND_5GHZ].n_channels == 0 &&
  2942. (il->cfg->sku & IL_SKU_A)) {
  2943. IL_INFO("Incorrectly detected BG card as ABG. "
  2944. "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
  2945. il->pci_dev->device, il->pci_dev->subsystem_device);
  2946. il->cfg->sku &= ~IL_SKU_A;
  2947. }
  2948. IL_INFO("Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  2949. il->bands[IEEE80211_BAND_2GHZ].n_channels,
  2950. il->bands[IEEE80211_BAND_5GHZ].n_channels);
  2951. set_bit(S_GEO_CONFIGURED, &il->status);
  2952. return 0;
  2953. }
  2954. EXPORT_SYMBOL(il_init_geos);
  2955. /*
  2956. * il_free_geos - undo allocations in il_init_geos
  2957. */
  2958. void
  2959. il_free_geos(struct il_priv *il)
  2960. {
  2961. kfree(il->ieee_channels);
  2962. kfree(il->ieee_rates);
  2963. clear_bit(S_GEO_CONFIGURED, &il->status);
  2964. }
  2965. EXPORT_SYMBOL(il_free_geos);
  2966. static bool
  2967. il_is_channel_extension(struct il_priv *il, enum ieee80211_band band,
  2968. u16 channel, u8 extension_chan_offset)
  2969. {
  2970. const struct il_channel_info *ch_info;
  2971. ch_info = il_get_channel_info(il, band, channel);
  2972. if (!il_is_channel_valid(ch_info))
  2973. return false;
  2974. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  2975. return !(ch_info->
  2976. ht40_extension_channel & IEEE80211_CHAN_NO_HT40PLUS);
  2977. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  2978. return !(ch_info->
  2979. ht40_extension_channel & IEEE80211_CHAN_NO_HT40MINUS);
  2980. return false;
  2981. }
  2982. bool
  2983. il_is_ht40_tx_allowed(struct il_priv *il, struct ieee80211_sta_ht_cap *ht_cap)
  2984. {
  2985. if (!il->ht.enabled || !il->ht.is_40mhz)
  2986. return false;
  2987. /*
  2988. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  2989. * the bit will not set if it is pure 40MHz case
  2990. */
  2991. if (ht_cap && !ht_cap->ht_supported)
  2992. return false;
  2993. #ifdef CONFIG_IWLEGACY_DEBUGFS
  2994. if (il->disable_ht40)
  2995. return false;
  2996. #endif
  2997. return il_is_channel_extension(il, il->band,
  2998. le16_to_cpu(il->staging.channel),
  2999. il->ht.extension_chan_offset);
  3000. }
  3001. EXPORT_SYMBOL(il_is_ht40_tx_allowed);
  3002. static u16
  3003. il_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  3004. {
  3005. u16 new_val;
  3006. u16 beacon_factor;
  3007. /*
  3008. * If mac80211 hasn't given us a beacon interval, program
  3009. * the default into the device.
  3010. */
  3011. if (!beacon_val)
  3012. return DEFAULT_BEACON_INTERVAL;
  3013. /*
  3014. * If the beacon interval we obtained from the peer
  3015. * is too large, we'll have to wake up more often
  3016. * (and in IBSS case, we'll beacon too much)
  3017. *
  3018. * For example, if max_beacon_val is 4096, and the
  3019. * requested beacon interval is 7000, we'll have to
  3020. * use 3500 to be able to wake up on the beacons.
  3021. *
  3022. * This could badly influence beacon detection stats.
  3023. */
  3024. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  3025. new_val = beacon_val / beacon_factor;
  3026. if (!new_val)
  3027. new_val = max_beacon_val;
  3028. return new_val;
  3029. }
  3030. int
  3031. il_send_rxon_timing(struct il_priv *il)
  3032. {
  3033. u64 tsf;
  3034. s32 interval_tm, rem;
  3035. struct ieee80211_conf *conf = NULL;
  3036. u16 beacon_int;
  3037. struct ieee80211_vif *vif = il->vif;
  3038. conf = &il->hw->conf;
  3039. lockdep_assert_held(&il->mutex);
  3040. memset(&il->timing, 0, sizeof(struct il_rxon_time_cmd));
  3041. il->timing.timestamp = cpu_to_le64(il->timestamp);
  3042. il->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  3043. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  3044. /*
  3045. * TODO: For IBSS we need to get atim_win from mac80211,
  3046. * for now just always use 0
  3047. */
  3048. il->timing.atim_win = 0;
  3049. beacon_int =
  3050. il_adjust_beacon_interval(beacon_int,
  3051. il->hw_params.max_beacon_itrvl *
  3052. TIME_UNIT);
  3053. il->timing.beacon_interval = cpu_to_le16(beacon_int);
  3054. tsf = il->timestamp; /* tsf is modifed by do_div: copy it */
  3055. interval_tm = beacon_int * TIME_UNIT;
  3056. rem = do_div(tsf, interval_tm);
  3057. il->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  3058. il->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ? : 1) : 1;
  3059. D_ASSOC("beacon interval %d beacon timer %d beacon tim %d\n",
  3060. le16_to_cpu(il->timing.beacon_interval),
  3061. le32_to_cpu(il->timing.beacon_init_val),
  3062. le16_to_cpu(il->timing.atim_win));
  3063. return il_send_cmd_pdu(il, C_RXON_TIMING, sizeof(il->timing),
  3064. &il->timing);
  3065. }
  3066. EXPORT_SYMBOL(il_send_rxon_timing);
  3067. void
  3068. il_set_rxon_hwcrypto(struct il_priv *il, int hw_decrypt)
  3069. {
  3070. struct il_rxon_cmd *rxon = &il->staging;
  3071. if (hw_decrypt)
  3072. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  3073. else
  3074. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  3075. }
  3076. EXPORT_SYMBOL(il_set_rxon_hwcrypto);
  3077. /* validate RXON structure is valid */
  3078. int
  3079. il_check_rxon_cmd(struct il_priv *il)
  3080. {
  3081. struct il_rxon_cmd *rxon = &il->staging;
  3082. bool error = false;
  3083. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  3084. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  3085. IL_WARN("check 2.4G: wrong narrow\n");
  3086. error = true;
  3087. }
  3088. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  3089. IL_WARN("check 2.4G: wrong radar\n");
  3090. error = true;
  3091. }
  3092. } else {
  3093. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  3094. IL_WARN("check 5.2G: not short slot!\n");
  3095. error = true;
  3096. }
  3097. if (rxon->flags & RXON_FLG_CCK_MSK) {
  3098. IL_WARN("check 5.2G: CCK!\n");
  3099. error = true;
  3100. }
  3101. }
  3102. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  3103. IL_WARN("mac/bssid mcast!\n");
  3104. error = true;
  3105. }
  3106. /* make sure basic rates 6Mbps and 1Mbps are supported */
  3107. if ((rxon->ofdm_basic_rates & RATE_6M_MASK) == 0 &&
  3108. (rxon->cck_basic_rates & RATE_1M_MASK) == 0) {
  3109. IL_WARN("neither 1 nor 6 are basic\n");
  3110. error = true;
  3111. }
  3112. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  3113. IL_WARN("aid > 2007\n");
  3114. error = true;
  3115. }
  3116. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) ==
  3117. (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  3118. IL_WARN("CCK and short slot\n");
  3119. error = true;
  3120. }
  3121. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) ==
  3122. (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  3123. IL_WARN("CCK and auto detect");
  3124. error = true;
  3125. }
  3126. if ((rxon->
  3127. flags & (RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK)) ==
  3128. RXON_FLG_TGG_PROTECT_MSK) {
  3129. IL_WARN("TGg but no auto-detect\n");
  3130. error = true;
  3131. }
  3132. if (error)
  3133. IL_WARN("Tuning to channel %d\n", le16_to_cpu(rxon->channel));
  3134. if (error) {
  3135. IL_ERR("Invalid RXON\n");
  3136. return -EINVAL;
  3137. }
  3138. return 0;
  3139. }
  3140. EXPORT_SYMBOL(il_check_rxon_cmd);
  3141. /**
  3142. * il_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  3143. * @il: staging_rxon is compared to active_rxon
  3144. *
  3145. * If the RXON structure is changing enough to require a new tune,
  3146. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  3147. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  3148. */
  3149. int
  3150. il_full_rxon_required(struct il_priv *il)
  3151. {
  3152. const struct il_rxon_cmd *staging = &il->staging;
  3153. const struct il_rxon_cmd *active = &il->active;
  3154. #define CHK(cond) \
  3155. if ((cond)) { \
  3156. D_INFO("need full RXON - " #cond "\n"); \
  3157. return 1; \
  3158. }
  3159. #define CHK_NEQ(c1, c2) \
  3160. if ((c1) != (c2)) { \
  3161. D_INFO("need full RXON - " \
  3162. #c1 " != " #c2 " - %d != %d\n", \
  3163. (c1), (c2)); \
  3164. return 1; \
  3165. }
  3166. /* These items are only settable from the full RXON command */
  3167. CHK(!il_is_associated(il));
  3168. CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
  3169. CHK(compare_ether_addr(staging->node_addr, active->node_addr));
  3170. CHK(compare_ether_addr
  3171. (staging->wlap_bssid_addr, active->wlap_bssid_addr));
  3172. CHK_NEQ(staging->dev_type, active->dev_type);
  3173. CHK_NEQ(staging->channel, active->channel);
  3174. CHK_NEQ(staging->air_propagation, active->air_propagation);
  3175. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  3176. active->ofdm_ht_single_stream_basic_rates);
  3177. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  3178. active->ofdm_ht_dual_stream_basic_rates);
  3179. CHK_NEQ(staging->assoc_id, active->assoc_id);
  3180. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  3181. * be updated with the RXON_ASSOC command -- however only some
  3182. * flag transitions are allowed using RXON_ASSOC */
  3183. /* Check if we are not switching bands */
  3184. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  3185. active->flags & RXON_FLG_BAND_24G_MSK);
  3186. /* Check if we are switching association toggle */
  3187. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  3188. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  3189. #undef CHK
  3190. #undef CHK_NEQ
  3191. return 0;
  3192. }
  3193. EXPORT_SYMBOL(il_full_rxon_required);
  3194. u8
  3195. il_get_lowest_plcp(struct il_priv *il)
  3196. {
  3197. /*
  3198. * Assign the lowest rate -- should really get this from
  3199. * the beacon skb from mac80211.
  3200. */
  3201. if (il->staging.flags & RXON_FLG_BAND_24G_MSK)
  3202. return RATE_1M_PLCP;
  3203. else
  3204. return RATE_6M_PLCP;
  3205. }
  3206. EXPORT_SYMBOL(il_get_lowest_plcp);
  3207. static void
  3208. _il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
  3209. {
  3210. struct il_rxon_cmd *rxon = &il->staging;
  3211. if (!il->ht.enabled) {
  3212. rxon->flags &=
  3213. ~(RXON_FLG_CHANNEL_MODE_MSK |
  3214. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK | RXON_FLG_HT40_PROT_MSK
  3215. | RXON_FLG_HT_PROT_MSK);
  3216. return;
  3217. }
  3218. rxon->flags |=
  3219. cpu_to_le32(il->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  3220. /* Set up channel bandwidth:
  3221. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  3222. /* clear the HT channel mode before set the mode */
  3223. rxon->flags &=
  3224. ~(RXON_FLG_CHANNEL_MODE_MSK | RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3225. if (il_is_ht40_tx_allowed(il, NULL)) {
  3226. /* pure ht40 */
  3227. if (il->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  3228. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  3229. /* Note: control channel is opposite of extension channel */
  3230. switch (il->ht.extension_chan_offset) {
  3231. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3232. rxon->flags &=
  3233. ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3234. break;
  3235. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3236. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3237. break;
  3238. }
  3239. } else {
  3240. /* Note: control channel is opposite of extension channel */
  3241. switch (il->ht.extension_chan_offset) {
  3242. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3243. rxon->flags &=
  3244. ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3245. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3246. break;
  3247. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3248. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3249. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3250. break;
  3251. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  3252. default:
  3253. /* channel location only valid if in Mixed mode */
  3254. IL_ERR("invalid extension channel offset\n");
  3255. break;
  3256. }
  3257. }
  3258. } else {
  3259. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  3260. }
  3261. if (il->ops->hcmd->set_rxon_chain)
  3262. il->ops->hcmd->set_rxon_chain(il);
  3263. D_ASSOC("rxon flags 0x%X operation mode :0x%X "
  3264. "extension channel offset 0x%x\n", le32_to_cpu(rxon->flags),
  3265. il->ht.protection, il->ht.extension_chan_offset);
  3266. }
  3267. void
  3268. il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
  3269. {
  3270. _il_set_rxon_ht(il, ht_conf);
  3271. }
  3272. EXPORT_SYMBOL(il_set_rxon_ht);
  3273. /* Return valid, unused, channel for a passive scan to reset the RF */
  3274. u8
  3275. il_get_single_channel_number(struct il_priv *il, enum ieee80211_band band)
  3276. {
  3277. const struct il_channel_info *ch_info;
  3278. int i;
  3279. u8 channel = 0;
  3280. u8 min, max;
  3281. if (band == IEEE80211_BAND_5GHZ) {
  3282. min = 14;
  3283. max = il->channel_count;
  3284. } else {
  3285. min = 0;
  3286. max = 14;
  3287. }
  3288. for (i = min; i < max; i++) {
  3289. channel = il->channel_info[i].channel;
  3290. if (channel == le16_to_cpu(il->staging.channel))
  3291. continue;
  3292. ch_info = il_get_channel_info(il, band, channel);
  3293. if (il_is_channel_valid(ch_info))
  3294. break;
  3295. }
  3296. return channel;
  3297. }
  3298. EXPORT_SYMBOL(il_get_single_channel_number);
  3299. /**
  3300. * il_set_rxon_channel - Set the band and channel values in staging RXON
  3301. * @ch: requested channel as a pointer to struct ieee80211_channel
  3302. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  3303. * in the staging RXON flag structure based on the ch->band
  3304. */
  3305. int
  3306. il_set_rxon_channel(struct il_priv *il, struct ieee80211_channel *ch)
  3307. {
  3308. enum ieee80211_band band = ch->band;
  3309. u16 channel = ch->hw_value;
  3310. if (le16_to_cpu(il->staging.channel) == channel && il->band == band)
  3311. return 0;
  3312. il->staging.channel = cpu_to_le16(channel);
  3313. if (band == IEEE80211_BAND_5GHZ)
  3314. il->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  3315. else
  3316. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3317. il->band = band;
  3318. D_INFO("Staging channel set to %d [%d]\n", channel, band);
  3319. return 0;
  3320. }
  3321. EXPORT_SYMBOL(il_set_rxon_channel);
  3322. void
  3323. il_set_flags_for_band(struct il_priv *il, enum ieee80211_band band,
  3324. struct ieee80211_vif *vif)
  3325. {
  3326. if (band == IEEE80211_BAND_5GHZ) {
  3327. il->staging.flags &=
  3328. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK |
  3329. RXON_FLG_CCK_MSK);
  3330. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3331. } else {
  3332. /* Copied from il_post_associate() */
  3333. if (vif && vif->bss_conf.use_short_slot)
  3334. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3335. else
  3336. il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  3337. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3338. il->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  3339. il->staging.flags &= ~RXON_FLG_CCK_MSK;
  3340. }
  3341. }
  3342. EXPORT_SYMBOL(il_set_flags_for_band);
  3343. /*
  3344. * initialize rxon structure with default values from eeprom
  3345. */
  3346. void
  3347. il_connection_init_rx_config(struct il_priv *il)
  3348. {
  3349. const struct il_channel_info *ch_info;
  3350. memset(&il->staging, 0, sizeof(il->staging));
  3351. if (!il->vif) {
  3352. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3353. } else if (il->vif->type == NL80211_IFTYPE_STATION) {
  3354. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3355. il->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  3356. } else if (il->vif->type == NL80211_IFTYPE_ADHOC) {
  3357. il->staging.dev_type = RXON_DEV_TYPE_IBSS;
  3358. il->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  3359. il->staging.filter_flags =
  3360. RXON_FILTER_BCON_AWARE_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  3361. } else {
  3362. IL_ERR("Unsupported interface type %d\n", il->vif->type);
  3363. return;
  3364. }
  3365. #if 0
  3366. /* TODO: Figure out when short_preamble would be set and cache from
  3367. * that */
  3368. if (!hw_to_local(il->hw)->short_preamble)
  3369. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3370. else
  3371. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3372. #endif
  3373. ch_info =
  3374. il_get_channel_info(il, il->band, le16_to_cpu(il->active.channel));
  3375. if (!ch_info)
  3376. ch_info = &il->channel_info[0];
  3377. il->staging.channel = cpu_to_le16(ch_info->channel);
  3378. il->band = ch_info->band;
  3379. il_set_flags_for_band(il, il->band, il->vif);
  3380. il->staging.ofdm_basic_rates =
  3381. (IL_OFDM_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3382. il->staging.cck_basic_rates =
  3383. (IL_CCK_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3384. /* clear both MIX and PURE40 mode flag */
  3385. il->staging.flags &=
  3386. ~(RXON_FLG_CHANNEL_MODE_MIXED | RXON_FLG_CHANNEL_MODE_PURE_40);
  3387. if (il->vif)
  3388. memcpy(il->staging.node_addr, il->vif->addr, ETH_ALEN);
  3389. il->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  3390. il->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  3391. }
  3392. EXPORT_SYMBOL(il_connection_init_rx_config);
  3393. void
  3394. il_set_rate(struct il_priv *il)
  3395. {
  3396. const struct ieee80211_supported_band *hw = NULL;
  3397. struct ieee80211_rate *rate;
  3398. int i;
  3399. hw = il_get_hw_mode(il, il->band);
  3400. if (!hw) {
  3401. IL_ERR("Failed to set rate: unable to get hw mode\n");
  3402. return;
  3403. }
  3404. il->active_rate = 0;
  3405. for (i = 0; i < hw->n_bitrates; i++) {
  3406. rate = &(hw->bitrates[i]);
  3407. if (rate->hw_value < RATE_COUNT_LEGACY)
  3408. il->active_rate |= (1 << rate->hw_value);
  3409. }
  3410. D_RATE("Set active_rate = %0x\n", il->active_rate);
  3411. il->staging.cck_basic_rates =
  3412. (IL_CCK_BASIC_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3413. il->staging.ofdm_basic_rates =
  3414. (IL_OFDM_BASIC_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3415. }
  3416. EXPORT_SYMBOL(il_set_rate);
  3417. void
  3418. il_chswitch_done(struct il_priv *il, bool is_success)
  3419. {
  3420. if (test_bit(S_EXIT_PENDING, &il->status))
  3421. return;
  3422. if (test_and_clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3423. ieee80211_chswitch_done(il->vif, is_success);
  3424. }
  3425. EXPORT_SYMBOL(il_chswitch_done);
  3426. void
  3427. il_hdl_csa(struct il_priv *il, struct il_rx_buf *rxb)
  3428. {
  3429. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3430. struct il_csa_notification *csa = &(pkt->u.csa_notif);
  3431. struct il_rxon_cmd *rxon = (void *)&il->active;
  3432. if (!test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3433. return;
  3434. if (!le32_to_cpu(csa->status) && csa->channel == il->switch_channel) {
  3435. rxon->channel = csa->channel;
  3436. il->staging.channel = csa->channel;
  3437. D_11H("CSA notif: channel %d\n", le16_to_cpu(csa->channel));
  3438. il_chswitch_done(il, true);
  3439. } else {
  3440. IL_ERR("CSA notif (fail) : channel %d\n",
  3441. le16_to_cpu(csa->channel));
  3442. il_chswitch_done(il, false);
  3443. }
  3444. }
  3445. EXPORT_SYMBOL(il_hdl_csa);
  3446. #ifdef CONFIG_IWLEGACY_DEBUG
  3447. void
  3448. il_print_rx_config_cmd(struct il_priv *il)
  3449. {
  3450. struct il_rxon_cmd *rxon = &il->staging;
  3451. D_RADIO("RX CONFIG:\n");
  3452. il_print_hex_dump(il, IL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3453. D_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3454. D_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3455. D_RADIO("u32 filter_flags: 0x%08x\n", le32_to_cpu(rxon->filter_flags));
  3456. D_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3457. D_RADIO("u8 ofdm_basic_rates: 0x%02x\n", rxon->ofdm_basic_rates);
  3458. D_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3459. D_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
  3460. D_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  3461. D_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3462. }
  3463. EXPORT_SYMBOL(il_print_rx_config_cmd);
  3464. #endif
  3465. /**
  3466. * il_irq_handle_error - called for HW or SW error interrupt from card
  3467. */
  3468. void
  3469. il_irq_handle_error(struct il_priv *il)
  3470. {
  3471. /* Set the FW error flag -- cleared on il_down */
  3472. set_bit(S_FW_ERROR, &il->status);
  3473. /* Cancel currently queued command. */
  3474. clear_bit(S_HCMD_ACTIVE, &il->status);
  3475. IL_ERR("Loaded firmware version: %s\n", il->hw->wiphy->fw_version);
  3476. il->ops->lib->dump_nic_error_log(il);
  3477. if (il->ops->lib->dump_fh)
  3478. il->ops->lib->dump_fh(il, NULL, false);
  3479. #ifdef CONFIG_IWLEGACY_DEBUG
  3480. if (il_get_debug_level(il) & IL_DL_FW_ERRORS)
  3481. il_print_rx_config_cmd(il);
  3482. #endif
  3483. wake_up(&il->wait_command_queue);
  3484. /* Keep the restart process from trying to send host
  3485. * commands by clearing the INIT status bit */
  3486. clear_bit(S_READY, &il->status);
  3487. if (!test_bit(S_EXIT_PENDING, &il->status)) {
  3488. IL_DBG(IL_DL_FW_ERRORS,
  3489. "Restarting adapter due to uCode error.\n");
  3490. if (il->cfg->mod_params->restart_fw)
  3491. queue_work(il->workqueue, &il->restart);
  3492. }
  3493. }
  3494. EXPORT_SYMBOL(il_irq_handle_error);
  3495. static int
  3496. il_apm_stop_master(struct il_priv *il)
  3497. {
  3498. int ret = 0;
  3499. /* stop device's busmaster DMA activity */
  3500. il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  3501. ret =
  3502. _il_poll_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
  3503. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  3504. if (ret)
  3505. IL_WARN("Master Disable Timed Out, 100 usec\n");
  3506. D_INFO("stop master\n");
  3507. return ret;
  3508. }
  3509. void
  3510. il_apm_stop(struct il_priv *il)
  3511. {
  3512. D_INFO("Stop card, put in low power state\n");
  3513. /* Stop device's DMA activity */
  3514. il_apm_stop_master(il);
  3515. /* Reset the entire device */
  3516. il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  3517. udelay(10);
  3518. /*
  3519. * Clear "initialization complete" bit to move adapter from
  3520. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  3521. */
  3522. il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3523. }
  3524. EXPORT_SYMBOL(il_apm_stop);
  3525. /*
  3526. * Start up NIC's basic functionality after it has been reset
  3527. * (e.g. after platform boot, or shutdown via il_apm_stop())
  3528. * NOTE: This does not load uCode nor start the embedded processor
  3529. */
  3530. int
  3531. il_apm_init(struct il_priv *il)
  3532. {
  3533. int ret = 0;
  3534. u16 lctl;
  3535. D_INFO("Init card's basic functions\n");
  3536. /*
  3537. * Use "set_bit" below rather than "write", to preserve any hardware
  3538. * bits already set by default after reset.
  3539. */
  3540. /* Disable L0S exit timer (platform NMI Work/Around) */
  3541. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3542. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  3543. /*
  3544. * Disable L0s without affecting L1;
  3545. * don't wait for ICH L0s (ICH bug W/A)
  3546. */
  3547. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3548. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  3549. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  3550. il_set_bit(il, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  3551. /*
  3552. * Enable HAP INTA (interrupt from management bus) to
  3553. * wake device's PCI Express link L1a -> L0s
  3554. * NOTE: This is no-op for 3945 (non-existent bit)
  3555. */
  3556. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  3557. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  3558. /*
  3559. * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
  3560. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  3561. * If so (likely), disable L0S, so device moves directly L0->L1;
  3562. * costs negligible amount of power savings.
  3563. * If not (unlikely), enable L0S, so there is at least some
  3564. * power savings, even without L1.
  3565. */
  3566. if (il->cfg->set_l0s) {
  3567. lctl = il_pcie_link_ctl(il);
  3568. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  3569. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  3570. /* L1-ASPM enabled; disable(!) L0S */
  3571. il_set_bit(il, CSR_GIO_REG,
  3572. CSR_GIO_REG_VAL_L0S_ENABLED);
  3573. D_POWER("L1 Enabled; Disabling L0S\n");
  3574. } else {
  3575. /* L1-ASPM disabled; enable(!) L0S */
  3576. il_clear_bit(il, CSR_GIO_REG,
  3577. CSR_GIO_REG_VAL_L0S_ENABLED);
  3578. D_POWER("L1 Disabled; Enabling L0S\n");
  3579. }
  3580. }
  3581. /* Configure analog phase-lock-loop before activating to D0A */
  3582. if (il->cfg->pll_cfg_val)
  3583. il_set_bit(il, CSR_ANA_PLL_CFG,
  3584. il->cfg->pll_cfg_val);
  3585. /*
  3586. * Set "initialization complete" bit to move adapter from
  3587. * D0U* --> D0A* (powered-up active) state.
  3588. */
  3589. il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3590. /*
  3591. * Wait for clock stabilization; once stabilized, access to
  3592. * device-internal resources is supported, e.g. il_wr_prph()
  3593. * and accesses to uCode SRAM.
  3594. */
  3595. ret =
  3596. _il_poll_bit(il, CSR_GP_CNTRL,
  3597. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  3598. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  3599. if (ret < 0) {
  3600. D_INFO("Failed to init the card\n");
  3601. goto out;
  3602. }
  3603. /*
  3604. * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
  3605. * BSM (Boostrap State Machine) is only in 3945 and 4965.
  3606. *
  3607. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  3608. * do not disable clocks. This preserves any hardware bits already
  3609. * set by default in "CLK_CTRL_REG" after reset.
  3610. */
  3611. if (il->cfg->use_bsm)
  3612. il_wr_prph(il, APMG_CLK_EN_REG,
  3613. APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
  3614. else
  3615. il_wr_prph(il, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  3616. udelay(20);
  3617. /* Disable L1-Active */
  3618. il_set_bits_prph(il, APMG_PCIDEV_STT_REG,
  3619. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  3620. out:
  3621. return ret;
  3622. }
  3623. EXPORT_SYMBOL(il_apm_init);
  3624. int
  3625. il_set_tx_power(struct il_priv *il, s8 tx_power, bool force)
  3626. {
  3627. int ret;
  3628. s8 prev_tx_power;
  3629. bool defer;
  3630. lockdep_assert_held(&il->mutex);
  3631. if (il->tx_power_user_lmt == tx_power && !force)
  3632. return 0;
  3633. if (!il->ops->lib->send_tx_power)
  3634. return -EOPNOTSUPP;
  3635. /* 0 dBm mean 1 milliwatt */
  3636. if (tx_power < 0) {
  3637. IL_WARN("Requested user TXPOWER %d below 1 mW.\n", tx_power);
  3638. return -EINVAL;
  3639. }
  3640. if (tx_power > il->tx_power_device_lmt) {
  3641. IL_WARN("Requested user TXPOWER %d above upper limit %d.\n",
  3642. tx_power, il->tx_power_device_lmt);
  3643. return -EINVAL;
  3644. }
  3645. if (!il_is_ready_rf(il))
  3646. return -EIO;
  3647. /* scan complete and commit_rxon use tx_power_next value,
  3648. * it always need to be updated for newest request */
  3649. il->tx_power_next = tx_power;
  3650. /* do not set tx power when scanning or channel changing */
  3651. defer = test_bit(S_SCANNING, &il->status) ||
  3652. memcmp(&il->active, &il->staging, sizeof(il->staging));
  3653. if (defer && !force) {
  3654. D_INFO("Deferring tx power set\n");
  3655. return 0;
  3656. }
  3657. prev_tx_power = il->tx_power_user_lmt;
  3658. il->tx_power_user_lmt = tx_power;
  3659. ret = il->ops->lib->send_tx_power(il);
  3660. /* if fail to set tx_power, restore the orig. tx power */
  3661. if (ret) {
  3662. il->tx_power_user_lmt = prev_tx_power;
  3663. il->tx_power_next = prev_tx_power;
  3664. }
  3665. return ret;
  3666. }
  3667. EXPORT_SYMBOL(il_set_tx_power);
  3668. void
  3669. il_send_bt_config(struct il_priv *il)
  3670. {
  3671. struct il_bt_cmd bt_cmd = {
  3672. .lead_time = BT_LEAD_TIME_DEF,
  3673. .max_kill = BT_MAX_KILL_DEF,
  3674. .kill_ack_mask = 0,
  3675. .kill_cts_mask = 0,
  3676. };
  3677. if (!bt_coex_active)
  3678. bt_cmd.flags = BT_COEX_DISABLE;
  3679. else
  3680. bt_cmd.flags = BT_COEX_ENABLE;
  3681. D_INFO("BT coex %s\n",
  3682. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  3683. if (il_send_cmd_pdu(il, C_BT_CONFIG, sizeof(struct il_bt_cmd), &bt_cmd))
  3684. IL_ERR("failed to send BT Coex Config\n");
  3685. }
  3686. EXPORT_SYMBOL(il_send_bt_config);
  3687. int
  3688. il_send_stats_request(struct il_priv *il, u8 flags, bool clear)
  3689. {
  3690. struct il_stats_cmd stats_cmd = {
  3691. .configuration_flags = clear ? IL_STATS_CONF_CLEAR_STATS : 0,
  3692. };
  3693. if (flags & CMD_ASYNC)
  3694. return il_send_cmd_pdu_async(il, C_STATS, sizeof(struct il_stats_cmd),
  3695. &stats_cmd, NULL);
  3696. else
  3697. return il_send_cmd_pdu(il, C_STATS, sizeof(struct il_stats_cmd),
  3698. &stats_cmd);
  3699. }
  3700. EXPORT_SYMBOL(il_send_stats_request);
  3701. void
  3702. il_hdl_pm_sleep(struct il_priv *il, struct il_rx_buf *rxb)
  3703. {
  3704. #ifdef CONFIG_IWLEGACY_DEBUG
  3705. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3706. struct il_sleep_notification *sleep = &(pkt->u.sleep_notif);
  3707. D_RX("sleep mode: %d, src: %d\n",
  3708. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  3709. #endif
  3710. }
  3711. EXPORT_SYMBOL(il_hdl_pm_sleep);
  3712. void
  3713. il_hdl_pm_debug_stats(struct il_priv *il, struct il_rx_buf *rxb)
  3714. {
  3715. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3716. u32 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
  3717. D_RADIO("Dumping %d bytes of unhandled notification for %s:\n", len,
  3718. il_get_cmd_string(pkt->hdr.cmd));
  3719. il_print_hex_dump(il, IL_DL_RADIO, pkt->u.raw, len);
  3720. }
  3721. EXPORT_SYMBOL(il_hdl_pm_debug_stats);
  3722. void
  3723. il_hdl_error(struct il_priv *il, struct il_rx_buf *rxb)
  3724. {
  3725. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3726. IL_ERR("Error Reply type 0x%08X cmd %s (0x%02X) "
  3727. "seq 0x%04X ser 0x%08X\n",
  3728. le32_to_cpu(pkt->u.err_resp.error_type),
  3729. il_get_cmd_string(pkt->u.err_resp.cmd_id),
  3730. pkt->u.err_resp.cmd_id,
  3731. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  3732. le32_to_cpu(pkt->u.err_resp.error_info));
  3733. }
  3734. EXPORT_SYMBOL(il_hdl_error);
  3735. void
  3736. il_clear_isr_stats(struct il_priv *il)
  3737. {
  3738. memset(&il->isr_stats, 0, sizeof(il->isr_stats));
  3739. }
  3740. int
  3741. il_mac_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
  3742. const struct ieee80211_tx_queue_params *params)
  3743. {
  3744. struct il_priv *il = hw->priv;
  3745. unsigned long flags;
  3746. int q;
  3747. D_MAC80211("enter\n");
  3748. if (!il_is_ready_rf(il)) {
  3749. D_MAC80211("leave - RF not ready\n");
  3750. return -EIO;
  3751. }
  3752. if (queue >= AC_NUM) {
  3753. D_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  3754. return 0;
  3755. }
  3756. q = AC_NUM - 1 - queue;
  3757. spin_lock_irqsave(&il->lock, flags);
  3758. il->qos_data.def_qos_parm.ac[q].cw_min =
  3759. cpu_to_le16(params->cw_min);
  3760. il->qos_data.def_qos_parm.ac[q].cw_max =
  3761. cpu_to_le16(params->cw_max);
  3762. il->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  3763. il->qos_data.def_qos_parm.ac[q].edca_txop =
  3764. cpu_to_le16((params->txop * 32));
  3765. il->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  3766. spin_unlock_irqrestore(&il->lock, flags);
  3767. D_MAC80211("leave\n");
  3768. return 0;
  3769. }
  3770. EXPORT_SYMBOL(il_mac_conf_tx);
  3771. int
  3772. il_mac_tx_last_beacon(struct ieee80211_hw *hw)
  3773. {
  3774. struct il_priv *il = hw->priv;
  3775. return il->ibss_manager == IL_IBSS_MANAGER;
  3776. }
  3777. EXPORT_SYMBOL_GPL(il_mac_tx_last_beacon);
  3778. static int
  3779. il_set_mode(struct il_priv *il)
  3780. {
  3781. il_connection_init_rx_config(il);
  3782. if (il->ops->hcmd->set_rxon_chain)
  3783. il->ops->hcmd->set_rxon_chain(il);
  3784. return il_commit_rxon(il);
  3785. }
  3786. int
  3787. il_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3788. {
  3789. struct il_priv *il = hw->priv;
  3790. int err;
  3791. D_MAC80211("enter: type %d, addr %pM\n", vif->type, vif->addr);
  3792. mutex_lock(&il->mutex);
  3793. if (!il_is_ready_rf(il)) {
  3794. IL_WARN("Try to add interface when device not ready\n");
  3795. err = -EINVAL;
  3796. goto out;
  3797. }
  3798. if (il->vif) {
  3799. err = -EOPNOTSUPP;
  3800. goto out;
  3801. }
  3802. il->vif = vif;
  3803. il->iw_mode = vif->type;
  3804. err = il_set_mode(il);
  3805. if (err) {
  3806. il->vif = NULL;
  3807. il->iw_mode = NL80211_IFTYPE_STATION;
  3808. }
  3809. out:
  3810. mutex_unlock(&il->mutex);
  3811. D_MAC80211("leave\n");
  3812. return err;
  3813. }
  3814. EXPORT_SYMBOL(il_mac_add_interface);
  3815. static void
  3816. il_teardown_interface(struct il_priv *il, struct ieee80211_vif *vif,
  3817. bool mode_change)
  3818. {
  3819. lockdep_assert_held(&il->mutex);
  3820. if (il->scan_vif == vif) {
  3821. il_scan_cancel_timeout(il, 200);
  3822. il_force_scan_end(il);
  3823. }
  3824. if (!mode_change)
  3825. il_set_mode(il);
  3826. }
  3827. void
  3828. il_mac_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3829. {
  3830. struct il_priv *il = hw->priv;
  3831. D_MAC80211("enter\n");
  3832. mutex_lock(&il->mutex);
  3833. WARN_ON(il->vif != vif);
  3834. il->vif = NULL;
  3835. il_teardown_interface(il, vif, false);
  3836. memset(il->bssid, 0, ETH_ALEN);
  3837. mutex_unlock(&il->mutex);
  3838. D_MAC80211("leave\n");
  3839. }
  3840. EXPORT_SYMBOL(il_mac_remove_interface);
  3841. int
  3842. il_alloc_txq_mem(struct il_priv *il)
  3843. {
  3844. if (!il->txq)
  3845. il->txq =
  3846. kzalloc(sizeof(struct il_tx_queue) *
  3847. il->cfg->num_of_queues, GFP_KERNEL);
  3848. if (!il->txq) {
  3849. IL_ERR("Not enough memory for txq\n");
  3850. return -ENOMEM;
  3851. }
  3852. return 0;
  3853. }
  3854. EXPORT_SYMBOL(il_alloc_txq_mem);
  3855. void
  3856. il_txq_mem(struct il_priv *il)
  3857. {
  3858. kfree(il->txq);
  3859. il->txq = NULL;
  3860. }
  3861. EXPORT_SYMBOL(il_txq_mem);
  3862. #ifdef CONFIG_IWLEGACY_DEBUGFS
  3863. #define IL_TRAFFIC_DUMP_SIZE (IL_TRAFFIC_ENTRY_SIZE * IL_TRAFFIC_ENTRIES)
  3864. void
  3865. il_reset_traffic_log(struct il_priv *il)
  3866. {
  3867. il->tx_traffic_idx = 0;
  3868. il->rx_traffic_idx = 0;
  3869. if (il->tx_traffic)
  3870. memset(il->tx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
  3871. if (il->rx_traffic)
  3872. memset(il->rx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
  3873. }
  3874. int
  3875. il_alloc_traffic_mem(struct il_priv *il)
  3876. {
  3877. u32 traffic_size = IL_TRAFFIC_DUMP_SIZE;
  3878. if (il_debug_level & IL_DL_TX) {
  3879. if (!il->tx_traffic) {
  3880. il->tx_traffic = kzalloc(traffic_size, GFP_KERNEL);
  3881. if (!il->tx_traffic)
  3882. return -ENOMEM;
  3883. }
  3884. }
  3885. if (il_debug_level & IL_DL_RX) {
  3886. if (!il->rx_traffic) {
  3887. il->rx_traffic = kzalloc(traffic_size, GFP_KERNEL);
  3888. if (!il->rx_traffic)
  3889. return -ENOMEM;
  3890. }
  3891. }
  3892. il_reset_traffic_log(il);
  3893. return 0;
  3894. }
  3895. EXPORT_SYMBOL(il_alloc_traffic_mem);
  3896. void
  3897. il_free_traffic_mem(struct il_priv *il)
  3898. {
  3899. kfree(il->tx_traffic);
  3900. il->tx_traffic = NULL;
  3901. kfree(il->rx_traffic);
  3902. il->rx_traffic = NULL;
  3903. }
  3904. EXPORT_SYMBOL(il_free_traffic_mem);
  3905. void
  3906. il_dbg_log_tx_data_frame(struct il_priv *il, u16 length,
  3907. struct ieee80211_hdr *header)
  3908. {
  3909. __le16 fc;
  3910. u16 len;
  3911. if (likely(!(il_debug_level & IL_DL_TX)))
  3912. return;
  3913. if (!il->tx_traffic)
  3914. return;
  3915. fc = header->frame_control;
  3916. if (ieee80211_is_data(fc)) {
  3917. len =
  3918. (length >
  3919. IL_TRAFFIC_ENTRY_SIZE) ? IL_TRAFFIC_ENTRY_SIZE : length;
  3920. memcpy((il->tx_traffic +
  3921. (il->tx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)), header,
  3922. len);
  3923. il->tx_traffic_idx =
  3924. (il->tx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
  3925. }
  3926. }
  3927. EXPORT_SYMBOL(il_dbg_log_tx_data_frame);
  3928. void
  3929. il_dbg_log_rx_data_frame(struct il_priv *il, u16 length,
  3930. struct ieee80211_hdr *header)
  3931. {
  3932. __le16 fc;
  3933. u16 len;
  3934. if (likely(!(il_debug_level & IL_DL_RX)))
  3935. return;
  3936. if (!il->rx_traffic)
  3937. return;
  3938. fc = header->frame_control;
  3939. if (ieee80211_is_data(fc)) {
  3940. len =
  3941. (length >
  3942. IL_TRAFFIC_ENTRY_SIZE) ? IL_TRAFFIC_ENTRY_SIZE : length;
  3943. memcpy((il->rx_traffic +
  3944. (il->rx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)), header,
  3945. len);
  3946. il->rx_traffic_idx =
  3947. (il->rx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
  3948. }
  3949. }
  3950. EXPORT_SYMBOL(il_dbg_log_rx_data_frame);
  3951. const char *
  3952. il_get_mgmt_string(int cmd)
  3953. {
  3954. switch (cmd) {
  3955. IL_CMD(MANAGEMENT_ASSOC_REQ);
  3956. IL_CMD(MANAGEMENT_ASSOC_RESP);
  3957. IL_CMD(MANAGEMENT_REASSOC_REQ);
  3958. IL_CMD(MANAGEMENT_REASSOC_RESP);
  3959. IL_CMD(MANAGEMENT_PROBE_REQ);
  3960. IL_CMD(MANAGEMENT_PROBE_RESP);
  3961. IL_CMD(MANAGEMENT_BEACON);
  3962. IL_CMD(MANAGEMENT_ATIM);
  3963. IL_CMD(MANAGEMENT_DISASSOC);
  3964. IL_CMD(MANAGEMENT_AUTH);
  3965. IL_CMD(MANAGEMENT_DEAUTH);
  3966. IL_CMD(MANAGEMENT_ACTION);
  3967. default:
  3968. return "UNKNOWN";
  3969. }
  3970. }
  3971. const char *
  3972. il_get_ctrl_string(int cmd)
  3973. {
  3974. switch (cmd) {
  3975. IL_CMD(CONTROL_BACK_REQ);
  3976. IL_CMD(CONTROL_BACK);
  3977. IL_CMD(CONTROL_PSPOLL);
  3978. IL_CMD(CONTROL_RTS);
  3979. IL_CMD(CONTROL_CTS);
  3980. IL_CMD(CONTROL_ACK);
  3981. IL_CMD(CONTROL_CFEND);
  3982. IL_CMD(CONTROL_CFENDACK);
  3983. default:
  3984. return "UNKNOWN";
  3985. }
  3986. }
  3987. void
  3988. il_clear_traffic_stats(struct il_priv *il)
  3989. {
  3990. memset(&il->tx_stats, 0, sizeof(struct traffic_stats));
  3991. memset(&il->rx_stats, 0, sizeof(struct traffic_stats));
  3992. }
  3993. /*
  3994. * if CONFIG_IWLEGACY_DEBUGFS defined,
  3995. * il_update_stats function will
  3996. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass
  3997. * Use debugFs to display the rx/rx_stats
  3998. * if CONFIG_IWLEGACY_DEBUGFS not being defined, then no MGMT and CTRL
  3999. * information will be recorded, but DATA pkt still will be recorded
  4000. * for the reason of il_led.c need to control the led blinking based on
  4001. * number of tx and rx data.
  4002. *
  4003. */
  4004. void
  4005. il_update_stats(struct il_priv *il, bool is_tx, __le16 fc, u16 len)
  4006. {
  4007. struct traffic_stats *stats;
  4008. if (is_tx)
  4009. stats = &il->tx_stats;
  4010. else
  4011. stats = &il->rx_stats;
  4012. if (ieee80211_is_mgmt(fc)) {
  4013. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4014. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  4015. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  4016. break;
  4017. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  4018. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  4019. break;
  4020. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  4021. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  4022. break;
  4023. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  4024. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  4025. break;
  4026. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  4027. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  4028. break;
  4029. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  4030. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  4031. break;
  4032. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  4033. stats->mgmt[MANAGEMENT_BEACON]++;
  4034. break;
  4035. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  4036. stats->mgmt[MANAGEMENT_ATIM]++;
  4037. break;
  4038. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  4039. stats->mgmt[MANAGEMENT_DISASSOC]++;
  4040. break;
  4041. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  4042. stats->mgmt[MANAGEMENT_AUTH]++;
  4043. break;
  4044. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  4045. stats->mgmt[MANAGEMENT_DEAUTH]++;
  4046. break;
  4047. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  4048. stats->mgmt[MANAGEMENT_ACTION]++;
  4049. break;
  4050. }
  4051. } else if (ieee80211_is_ctl(fc)) {
  4052. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4053. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  4054. stats->ctrl[CONTROL_BACK_REQ]++;
  4055. break;
  4056. case cpu_to_le16(IEEE80211_STYPE_BACK):
  4057. stats->ctrl[CONTROL_BACK]++;
  4058. break;
  4059. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  4060. stats->ctrl[CONTROL_PSPOLL]++;
  4061. break;
  4062. case cpu_to_le16(IEEE80211_STYPE_RTS):
  4063. stats->ctrl[CONTROL_RTS]++;
  4064. break;
  4065. case cpu_to_le16(IEEE80211_STYPE_CTS):
  4066. stats->ctrl[CONTROL_CTS]++;
  4067. break;
  4068. case cpu_to_le16(IEEE80211_STYPE_ACK):
  4069. stats->ctrl[CONTROL_ACK]++;
  4070. break;
  4071. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  4072. stats->ctrl[CONTROL_CFEND]++;
  4073. break;
  4074. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  4075. stats->ctrl[CONTROL_CFENDACK]++;
  4076. break;
  4077. }
  4078. } else {
  4079. /* data */
  4080. stats->data_cnt++;
  4081. stats->data_bytes += len;
  4082. }
  4083. }
  4084. EXPORT_SYMBOL(il_update_stats);
  4085. #endif
  4086. int
  4087. il_force_reset(struct il_priv *il, bool external)
  4088. {
  4089. struct il_force_reset *force_reset;
  4090. if (test_bit(S_EXIT_PENDING, &il->status))
  4091. return -EINVAL;
  4092. force_reset = &il->force_reset;
  4093. force_reset->reset_request_count++;
  4094. if (!external) {
  4095. if (force_reset->last_force_reset_jiffies &&
  4096. time_after(force_reset->last_force_reset_jiffies +
  4097. force_reset->reset_duration, jiffies)) {
  4098. D_INFO("force reset rejected\n");
  4099. force_reset->reset_reject_count++;
  4100. return -EAGAIN;
  4101. }
  4102. }
  4103. force_reset->reset_success_count++;
  4104. force_reset->last_force_reset_jiffies = jiffies;
  4105. /*
  4106. * if the request is from external(ex: debugfs),
  4107. * then always perform the request in regardless the module
  4108. * parameter setting
  4109. * if the request is from internal (uCode error or driver
  4110. * detect failure), then fw_restart module parameter
  4111. * need to be check before performing firmware reload
  4112. */
  4113. if (!external && !il->cfg->mod_params->restart_fw) {
  4114. D_INFO("Cancel firmware reload based on "
  4115. "module parameter setting\n");
  4116. return 0;
  4117. }
  4118. IL_ERR("On demand firmware reload\n");
  4119. /* Set the FW error flag -- cleared on il_down */
  4120. set_bit(S_FW_ERROR, &il->status);
  4121. wake_up(&il->wait_command_queue);
  4122. /*
  4123. * Keep the restart process from trying to send host
  4124. * commands by clearing the INIT status bit
  4125. */
  4126. clear_bit(S_READY, &il->status);
  4127. queue_work(il->workqueue, &il->restart);
  4128. return 0;
  4129. }
  4130. int
  4131. il_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4132. enum nl80211_iftype newtype, bool newp2p)
  4133. {
  4134. struct il_priv *il = hw->priv;
  4135. int err;
  4136. if (newp2p)
  4137. return -EOPNOTSUPP;
  4138. mutex_lock(&il->mutex);
  4139. if (!il->vif || !il_is_ready_rf(il)) {
  4140. /*
  4141. * Huh? But wait ... this can maybe happen when
  4142. * we're in the middle of a firmware restart!
  4143. */
  4144. err = -EBUSY;
  4145. goto out;
  4146. }
  4147. /* success */
  4148. il_teardown_interface(il, vif, true);
  4149. vif->type = newtype;
  4150. vif->p2p = false;
  4151. err = il_set_mode(il);
  4152. WARN_ON(err);
  4153. /*
  4154. * We've switched internally, but submitting to the
  4155. * device may have failed for some reason. Mask this
  4156. * error, because otherwise mac80211 will not switch
  4157. * (and set the interface type back) and we'll be
  4158. * out of sync with it.
  4159. */
  4160. err = 0;
  4161. out:
  4162. mutex_unlock(&il->mutex);
  4163. return err;
  4164. }
  4165. EXPORT_SYMBOL(il_mac_change_interface);
  4166. /*
  4167. * On every watchdog tick we check (latest) time stamp. If it does not
  4168. * change during timeout period and queue is not empty we reset firmware.
  4169. */
  4170. static int
  4171. il_check_stuck_queue(struct il_priv *il, int cnt)
  4172. {
  4173. struct il_tx_queue *txq = &il->txq[cnt];
  4174. struct il_queue *q = &txq->q;
  4175. unsigned long timeout;
  4176. int ret;
  4177. if (q->read_ptr == q->write_ptr) {
  4178. txq->time_stamp = jiffies;
  4179. return 0;
  4180. }
  4181. timeout =
  4182. txq->time_stamp +
  4183. msecs_to_jiffies(il->cfg->wd_timeout);
  4184. if (time_after(jiffies, timeout)) {
  4185. IL_ERR("Queue %d stuck for %u ms.\n", q->id,
  4186. il->cfg->wd_timeout);
  4187. ret = il_force_reset(il, false);
  4188. return (ret == -EAGAIN) ? 0 : 1;
  4189. }
  4190. return 0;
  4191. }
  4192. /*
  4193. * Making watchdog tick be a quarter of timeout assure we will
  4194. * discover the queue hung between timeout and 1.25*timeout
  4195. */
  4196. #define IL_WD_TICK(timeout) ((timeout) / 4)
  4197. /*
  4198. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  4199. * we reset the firmware. If everything is fine just rearm the timer.
  4200. */
  4201. void
  4202. il_bg_watchdog(unsigned long data)
  4203. {
  4204. struct il_priv *il = (struct il_priv *)data;
  4205. int cnt;
  4206. unsigned long timeout;
  4207. if (test_bit(S_EXIT_PENDING, &il->status))
  4208. return;
  4209. timeout = il->cfg->wd_timeout;
  4210. if (timeout == 0)
  4211. return;
  4212. /* monitor and check for stuck cmd queue */
  4213. if (il_check_stuck_queue(il, il->cmd_queue))
  4214. return;
  4215. /* monitor and check for other stuck queues */
  4216. if (il_is_any_associated(il)) {
  4217. for (cnt = 0; cnt < il->hw_params.max_txq_num; cnt++) {
  4218. /* skip as we already checked the command queue */
  4219. if (cnt == il->cmd_queue)
  4220. continue;
  4221. if (il_check_stuck_queue(il, cnt))
  4222. return;
  4223. }
  4224. }
  4225. mod_timer(&il->watchdog,
  4226. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4227. }
  4228. EXPORT_SYMBOL(il_bg_watchdog);
  4229. void
  4230. il_setup_watchdog(struct il_priv *il)
  4231. {
  4232. unsigned int timeout = il->cfg->wd_timeout;
  4233. if (timeout)
  4234. mod_timer(&il->watchdog,
  4235. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4236. else
  4237. del_timer(&il->watchdog);
  4238. }
  4239. EXPORT_SYMBOL(il_setup_watchdog);
  4240. /*
  4241. * extended beacon time format
  4242. * time in usec will be changed into a 32-bit value in extended:internal format
  4243. * the extended part is the beacon counts
  4244. * the internal part is the time in usec within one beacon interval
  4245. */
  4246. u32
  4247. il_usecs_to_beacons(struct il_priv *il, u32 usec, u32 beacon_interval)
  4248. {
  4249. u32 quot;
  4250. u32 rem;
  4251. u32 interval = beacon_interval * TIME_UNIT;
  4252. if (!interval || !usec)
  4253. return 0;
  4254. quot =
  4255. (usec /
  4256. interval) & (il_beacon_time_mask_high(il,
  4257. il->hw_params.
  4258. beacon_time_tsf_bits) >> il->
  4259. hw_params.beacon_time_tsf_bits);
  4260. rem =
  4261. (usec % interval) & il_beacon_time_mask_low(il,
  4262. il->hw_params.
  4263. beacon_time_tsf_bits);
  4264. return (quot << il->hw_params.beacon_time_tsf_bits) + rem;
  4265. }
  4266. EXPORT_SYMBOL(il_usecs_to_beacons);
  4267. /* base is usually what we get from ucode with each received frame,
  4268. * the same as HW timer counter counting down
  4269. */
  4270. __le32
  4271. il_add_beacon_time(struct il_priv *il, u32 base, u32 addon,
  4272. u32 beacon_interval)
  4273. {
  4274. u32 base_low = base & il_beacon_time_mask_low(il,
  4275. il->hw_params.
  4276. beacon_time_tsf_bits);
  4277. u32 addon_low = addon & il_beacon_time_mask_low(il,
  4278. il->hw_params.
  4279. beacon_time_tsf_bits);
  4280. u32 interval = beacon_interval * TIME_UNIT;
  4281. u32 res = (base & il_beacon_time_mask_high(il,
  4282. il->hw_params.
  4283. beacon_time_tsf_bits)) +
  4284. (addon & il_beacon_time_mask_high(il,
  4285. il->hw_params.
  4286. beacon_time_tsf_bits));
  4287. if (base_low > addon_low)
  4288. res += base_low - addon_low;
  4289. else if (base_low < addon_low) {
  4290. res += interval + base_low - addon_low;
  4291. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4292. } else
  4293. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4294. return cpu_to_le32(res);
  4295. }
  4296. EXPORT_SYMBOL(il_add_beacon_time);
  4297. #ifdef CONFIG_PM
  4298. int
  4299. il_pci_suspend(struct device *device)
  4300. {
  4301. struct pci_dev *pdev = to_pci_dev(device);
  4302. struct il_priv *il = pci_get_drvdata(pdev);
  4303. /*
  4304. * This function is called when system goes into suspend state
  4305. * mac80211 will call il_mac_stop() from the mac80211 suspend function
  4306. * first but since il_mac_stop() has no knowledge of who the caller is,
  4307. * it will not call apm_ops.stop() to stop the DMA operation.
  4308. * Calling apm_ops.stop here to make sure we stop the DMA.
  4309. */
  4310. il_apm_stop(il);
  4311. return 0;
  4312. }
  4313. EXPORT_SYMBOL(il_pci_suspend);
  4314. int
  4315. il_pci_resume(struct device *device)
  4316. {
  4317. struct pci_dev *pdev = to_pci_dev(device);
  4318. struct il_priv *il = pci_get_drvdata(pdev);
  4319. bool hw_rfkill = false;
  4320. /*
  4321. * We disable the RETRY_TIMEOUT register (0x41) to keep
  4322. * PCI Tx retries from interfering with C3 CPU state.
  4323. */
  4324. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  4325. il_enable_interrupts(il);
  4326. if (!(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  4327. hw_rfkill = true;
  4328. if (hw_rfkill)
  4329. set_bit(S_RF_KILL_HW, &il->status);
  4330. else
  4331. clear_bit(S_RF_KILL_HW, &il->status);
  4332. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rfkill);
  4333. return 0;
  4334. }
  4335. EXPORT_SYMBOL(il_pci_resume);
  4336. const struct dev_pm_ops il_pm_ops = {
  4337. .suspend = il_pci_suspend,
  4338. .resume = il_pci_resume,
  4339. .freeze = il_pci_suspend,
  4340. .thaw = il_pci_resume,
  4341. .poweroff = il_pci_suspend,
  4342. .restore = il_pci_resume,
  4343. };
  4344. EXPORT_SYMBOL(il_pm_ops);
  4345. #endif /* CONFIG_PM */
  4346. static void
  4347. il_update_qos(struct il_priv *il)
  4348. {
  4349. if (test_bit(S_EXIT_PENDING, &il->status))
  4350. return;
  4351. il->qos_data.def_qos_parm.qos_flags = 0;
  4352. if (il->qos_data.qos_active)
  4353. il->qos_data.def_qos_parm.qos_flags |=
  4354. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  4355. if (il->ht.enabled)
  4356. il->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  4357. D_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  4358. il->qos_data.qos_active, il->qos_data.def_qos_parm.qos_flags);
  4359. il_send_cmd_pdu_async(il, C_QOS_PARAM, sizeof(struct il_qosparam_cmd),
  4360. &il->qos_data.def_qos_parm, NULL);
  4361. }
  4362. /**
  4363. * il_mac_config - mac80211 config callback
  4364. */
  4365. int
  4366. il_mac_config(struct ieee80211_hw *hw, u32 changed)
  4367. {
  4368. struct il_priv *il = hw->priv;
  4369. const struct il_channel_info *ch_info;
  4370. struct ieee80211_conf *conf = &hw->conf;
  4371. struct ieee80211_channel *channel = conf->channel;
  4372. struct il_ht_config *ht_conf = &il->current_ht_config;
  4373. unsigned long flags = 0;
  4374. int ret = 0;
  4375. u16 ch;
  4376. int scan_active = 0;
  4377. bool ht_changed = false;
  4378. if (WARN_ON(!il->ops->legacy))
  4379. return -EOPNOTSUPP;
  4380. mutex_lock(&il->mutex);
  4381. D_MAC80211("enter to channel %d changed 0x%X\n", channel->hw_value,
  4382. changed);
  4383. if (unlikely(test_bit(S_SCANNING, &il->status))) {
  4384. scan_active = 1;
  4385. D_MAC80211("scan active\n");
  4386. }
  4387. if (changed &
  4388. (IEEE80211_CONF_CHANGE_SMPS | IEEE80211_CONF_CHANGE_CHANNEL)) {
  4389. /* mac80211 uses static for non-HT which is what we want */
  4390. il->current_ht_config.smps = conf->smps_mode;
  4391. /*
  4392. * Recalculate chain counts.
  4393. *
  4394. * If monitor mode is enabled then mac80211 will
  4395. * set up the SM PS mode to OFF if an HT channel is
  4396. * configured.
  4397. */
  4398. if (il->ops->hcmd->set_rxon_chain)
  4399. il->ops->hcmd->set_rxon_chain(il);
  4400. }
  4401. /* during scanning mac80211 will delay channel setting until
  4402. * scan finish with changed = 0
  4403. */
  4404. if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  4405. if (scan_active)
  4406. goto set_ch_out;
  4407. ch = channel->hw_value;
  4408. ch_info = il_get_channel_info(il, channel->band, ch);
  4409. if (!il_is_channel_valid(ch_info)) {
  4410. D_MAC80211("leave - invalid channel\n");
  4411. ret = -EINVAL;
  4412. goto set_ch_out;
  4413. }
  4414. if (il->iw_mode == NL80211_IFTYPE_ADHOC &&
  4415. !il_is_channel_ibss(ch_info)) {
  4416. D_MAC80211("leave - not IBSS channel\n");
  4417. ret = -EINVAL;
  4418. goto set_ch_out;
  4419. }
  4420. spin_lock_irqsave(&il->lock, flags);
  4421. /* Configure HT40 channels */
  4422. if (il->ht.enabled != conf_is_ht(conf)) {
  4423. il->ht.enabled = conf_is_ht(conf);
  4424. ht_changed = true;
  4425. }
  4426. if (il->ht.enabled) {
  4427. if (conf_is_ht40_minus(conf)) {
  4428. il->ht.extension_chan_offset =
  4429. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  4430. il->ht.is_40mhz = true;
  4431. } else if (conf_is_ht40_plus(conf)) {
  4432. il->ht.extension_chan_offset =
  4433. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  4434. il->ht.is_40mhz = true;
  4435. } else {
  4436. il->ht.extension_chan_offset =
  4437. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  4438. il->ht.is_40mhz = false;
  4439. }
  4440. } else
  4441. il->ht.is_40mhz = false;
  4442. /*
  4443. * Default to no protection. Protection mode will
  4444. * later be set from BSS config in il_ht_conf
  4445. */
  4446. il->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  4447. /* if we are switching from ht to 2.4 clear flags
  4448. * from any ht related info since 2.4 does not
  4449. * support ht */
  4450. if ((le16_to_cpu(il->staging.channel) != ch))
  4451. il->staging.flags = 0;
  4452. il_set_rxon_channel(il, channel);
  4453. il_set_rxon_ht(il, ht_conf);
  4454. il_set_flags_for_band(il, channel->band, il->vif);
  4455. spin_unlock_irqrestore(&il->lock, flags);
  4456. if (il->ops->legacy->update_bcast_stations)
  4457. ret = il->ops->legacy->update_bcast_stations(il);
  4458. set_ch_out:
  4459. /* The list of supported rates and rate mask can be different
  4460. * for each band; since the band may have changed, reset
  4461. * the rate mask to what mac80211 lists */
  4462. il_set_rate(il);
  4463. }
  4464. if (changed & (IEEE80211_CONF_CHANGE_PS | IEEE80211_CONF_CHANGE_IDLE)) {
  4465. ret = il_power_update_mode(il, false);
  4466. if (ret)
  4467. D_MAC80211("Error setting sleep level\n");
  4468. }
  4469. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  4470. D_MAC80211("TX Power old=%d new=%d\n", il->tx_power_user_lmt,
  4471. conf->power_level);
  4472. il_set_tx_power(il, conf->power_level, false);
  4473. }
  4474. if (!il_is_ready(il)) {
  4475. D_MAC80211("leave - not ready\n");
  4476. goto out;
  4477. }
  4478. if (scan_active)
  4479. goto out;
  4480. if (memcmp(&il->active, &il->staging, sizeof(il->staging)))
  4481. il_commit_rxon(il);
  4482. else
  4483. D_INFO("Not re-sending same RXON configuration.\n");
  4484. if (ht_changed)
  4485. il_update_qos(il);
  4486. out:
  4487. D_MAC80211("leave\n");
  4488. mutex_unlock(&il->mutex);
  4489. return ret;
  4490. }
  4491. EXPORT_SYMBOL(il_mac_config);
  4492. void
  4493. il_mac_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4494. {
  4495. struct il_priv *il = hw->priv;
  4496. unsigned long flags;
  4497. if (WARN_ON(!il->ops->legacy))
  4498. return;
  4499. mutex_lock(&il->mutex);
  4500. D_MAC80211("enter\n");
  4501. spin_lock_irqsave(&il->lock, flags);
  4502. memset(&il->current_ht_config, 0, sizeof(struct il_ht_config));
  4503. spin_unlock_irqrestore(&il->lock, flags);
  4504. spin_lock_irqsave(&il->lock, flags);
  4505. /* new association get rid of ibss beacon skb */
  4506. if (il->beacon_skb)
  4507. dev_kfree_skb(il->beacon_skb);
  4508. il->beacon_skb = NULL;
  4509. il->timestamp = 0;
  4510. spin_unlock_irqrestore(&il->lock, flags);
  4511. il_scan_cancel_timeout(il, 100);
  4512. if (!il_is_ready_rf(il)) {
  4513. D_MAC80211("leave - not ready\n");
  4514. mutex_unlock(&il->mutex);
  4515. return;
  4516. }
  4517. /* we are restarting association process
  4518. * clear RXON_FILTER_ASSOC_MSK bit
  4519. */
  4520. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4521. il_commit_rxon(il);
  4522. il_set_rate(il);
  4523. mutex_unlock(&il->mutex);
  4524. D_MAC80211("leave\n");
  4525. }
  4526. EXPORT_SYMBOL(il_mac_reset_tsf);
  4527. static void
  4528. il_ht_conf(struct il_priv *il, struct ieee80211_vif *vif)
  4529. {
  4530. struct il_ht_config *ht_conf = &il->current_ht_config;
  4531. struct ieee80211_sta *sta;
  4532. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  4533. D_ASSOC("enter:\n");
  4534. if (!il->ht.enabled)
  4535. return;
  4536. il->ht.protection =
  4537. bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  4538. il->ht.non_gf_sta_present =
  4539. !!(bss_conf->
  4540. ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  4541. ht_conf->single_chain_sufficient = false;
  4542. switch (vif->type) {
  4543. case NL80211_IFTYPE_STATION:
  4544. rcu_read_lock();
  4545. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  4546. if (sta) {
  4547. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  4548. int maxstreams;
  4549. maxstreams =
  4550. (ht_cap->mcs.
  4551. tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  4552. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  4553. maxstreams += 1;
  4554. if (ht_cap->mcs.rx_mask[1] == 0 &&
  4555. ht_cap->mcs.rx_mask[2] == 0)
  4556. ht_conf->single_chain_sufficient = true;
  4557. if (maxstreams <= 1)
  4558. ht_conf->single_chain_sufficient = true;
  4559. } else {
  4560. /*
  4561. * If at all, this can only happen through a race
  4562. * when the AP disconnects us while we're still
  4563. * setting up the connection, in that case mac80211
  4564. * will soon tell us about that.
  4565. */
  4566. ht_conf->single_chain_sufficient = true;
  4567. }
  4568. rcu_read_unlock();
  4569. break;
  4570. case NL80211_IFTYPE_ADHOC:
  4571. ht_conf->single_chain_sufficient = true;
  4572. break;
  4573. default:
  4574. break;
  4575. }
  4576. D_ASSOC("leave\n");
  4577. }
  4578. static inline void
  4579. il_set_no_assoc(struct il_priv *il, struct ieee80211_vif *vif)
  4580. {
  4581. /*
  4582. * inform the ucode that there is no longer an
  4583. * association and that no more packets should be
  4584. * sent
  4585. */
  4586. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4587. il->staging.assoc_id = 0;
  4588. il_commit_rxon(il);
  4589. }
  4590. static void
  4591. il_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4592. {
  4593. struct il_priv *il = hw->priv;
  4594. unsigned long flags;
  4595. __le64 timestamp;
  4596. struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
  4597. if (!skb)
  4598. return;
  4599. D_MAC80211("enter\n");
  4600. lockdep_assert_held(&il->mutex);
  4601. if (!il->beacon_enabled) {
  4602. IL_ERR("update beacon with no beaconing enabled\n");
  4603. dev_kfree_skb(skb);
  4604. return;
  4605. }
  4606. spin_lock_irqsave(&il->lock, flags);
  4607. if (il->beacon_skb)
  4608. dev_kfree_skb(il->beacon_skb);
  4609. il->beacon_skb = skb;
  4610. timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  4611. il->timestamp = le64_to_cpu(timestamp);
  4612. D_MAC80211("leave\n");
  4613. spin_unlock_irqrestore(&il->lock, flags);
  4614. if (!il_is_ready_rf(il)) {
  4615. D_MAC80211("leave - RF not ready\n");
  4616. return;
  4617. }
  4618. il->ops->legacy->post_associate(il);
  4619. }
  4620. void
  4621. il_mac_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4622. struct ieee80211_bss_conf *bss_conf, u32 changes)
  4623. {
  4624. struct il_priv *il = hw->priv;
  4625. int ret;
  4626. if (WARN_ON(!il->ops->legacy))
  4627. return;
  4628. D_MAC80211("changes = 0x%X\n", changes);
  4629. mutex_lock(&il->mutex);
  4630. if (!il_is_alive(il)) {
  4631. mutex_unlock(&il->mutex);
  4632. return;
  4633. }
  4634. if (changes & BSS_CHANGED_QOS) {
  4635. unsigned long flags;
  4636. spin_lock_irqsave(&il->lock, flags);
  4637. il->qos_data.qos_active = bss_conf->qos;
  4638. il_update_qos(il);
  4639. spin_unlock_irqrestore(&il->lock, flags);
  4640. }
  4641. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4642. /* FIXME: can we remove beacon_enabled ? */
  4643. if (vif->bss_conf.enable_beacon)
  4644. il->beacon_enabled = true;
  4645. else
  4646. il->beacon_enabled = false;
  4647. }
  4648. if (changes & BSS_CHANGED_BSSID) {
  4649. D_MAC80211("BSSID %pM\n", bss_conf->bssid);
  4650. /*
  4651. * If there is currently a HW scan going on in the
  4652. * background then we need to cancel it else the RXON
  4653. * below/in post_associate will fail.
  4654. */
  4655. if (il_scan_cancel_timeout(il, 100)) {
  4656. IL_WARN("Aborted scan still in progress after 100ms\n");
  4657. D_MAC80211("leaving - scan abort failed.\n");
  4658. mutex_unlock(&il->mutex);
  4659. return;
  4660. }
  4661. /* mac80211 only sets assoc when in STATION mode */
  4662. if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
  4663. memcpy(il->staging.bssid_addr, bss_conf->bssid,
  4664. ETH_ALEN);
  4665. /* currently needed in a few places */
  4666. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4667. } else {
  4668. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4669. }
  4670. }
  4671. /*
  4672. * This needs to be after setting the BSSID in case
  4673. * mac80211 decides to do both changes at once because
  4674. * it will invoke post_associate.
  4675. */
  4676. if (vif->type == NL80211_IFTYPE_ADHOC && (changes & BSS_CHANGED_BEACON))
  4677. il_beacon_update(hw, vif);
  4678. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  4679. D_MAC80211("ERP_PREAMBLE %d\n", bss_conf->use_short_preamble);
  4680. if (bss_conf->use_short_preamble)
  4681. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  4682. else
  4683. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  4684. }
  4685. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  4686. D_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  4687. if (bss_conf->use_cts_prot && il->band != IEEE80211_BAND_5GHZ)
  4688. il->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  4689. else
  4690. il->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  4691. if (bss_conf->use_cts_prot)
  4692. il->staging.flags |= RXON_FLG_SELF_CTS_EN;
  4693. else
  4694. il->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  4695. }
  4696. if (changes & BSS_CHANGED_BASIC_RATES) {
  4697. /* XXX use this information
  4698. *
  4699. * To do that, remove code from il_set_rate() and put something
  4700. * like this here:
  4701. *
  4702. if (A-band)
  4703. il->staging.ofdm_basic_rates =
  4704. bss_conf->basic_rates;
  4705. else
  4706. il->staging.ofdm_basic_rates =
  4707. bss_conf->basic_rates >> 4;
  4708. il->staging.cck_basic_rates =
  4709. bss_conf->basic_rates & 0xF;
  4710. */
  4711. }
  4712. if (changes & BSS_CHANGED_HT) {
  4713. il_ht_conf(il, vif);
  4714. if (il->ops->hcmd->set_rxon_chain)
  4715. il->ops->hcmd->set_rxon_chain(il);
  4716. }
  4717. if (changes & BSS_CHANGED_ASSOC) {
  4718. D_MAC80211("ASSOC %d\n", bss_conf->assoc);
  4719. if (bss_conf->assoc) {
  4720. il->timestamp = bss_conf->timestamp;
  4721. if (!il_is_rfkill(il))
  4722. il->ops->legacy->post_associate(il);
  4723. } else
  4724. il_set_no_assoc(il, vif);
  4725. }
  4726. if (changes && il_is_associated(il) && bss_conf->aid) {
  4727. D_MAC80211("Changes (%#x) while associated\n", changes);
  4728. ret = il_send_rxon_assoc(il);
  4729. if (!ret) {
  4730. /* Sync active_rxon with latest change. */
  4731. memcpy((void *)&il->active, &il->staging,
  4732. sizeof(struct il_rxon_cmd));
  4733. }
  4734. }
  4735. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4736. if (vif->bss_conf.enable_beacon) {
  4737. memcpy(il->staging.bssid_addr, bss_conf->bssid,
  4738. ETH_ALEN);
  4739. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4740. il->ops->legacy->config_ap(il);
  4741. } else
  4742. il_set_no_assoc(il, vif);
  4743. }
  4744. if (changes & BSS_CHANGED_IBSS) {
  4745. ret =
  4746. il->ops->legacy->manage_ibss_station(il, vif,
  4747. bss_conf->ibss_joined);
  4748. if (ret)
  4749. IL_ERR("failed to %s IBSS station %pM\n",
  4750. bss_conf->ibss_joined ? "add" : "remove",
  4751. bss_conf->bssid);
  4752. }
  4753. mutex_unlock(&il->mutex);
  4754. D_MAC80211("leave\n");
  4755. }
  4756. EXPORT_SYMBOL(il_mac_bss_info_changed);
  4757. irqreturn_t
  4758. il_isr(int irq, void *data)
  4759. {
  4760. struct il_priv *il = data;
  4761. u32 inta, inta_mask;
  4762. u32 inta_fh;
  4763. unsigned long flags;
  4764. if (!il)
  4765. return IRQ_NONE;
  4766. spin_lock_irqsave(&il->lock, flags);
  4767. /* Disable (but don't clear!) interrupts here to avoid
  4768. * back-to-back ISRs and sporadic interrupts from our NIC.
  4769. * If we have something to service, the tasklet will re-enable ints.
  4770. * If we *don't* have something, we'll re-enable before leaving here. */
  4771. inta_mask = _il_rd(il, CSR_INT_MASK); /* just for debug */
  4772. _il_wr(il, CSR_INT_MASK, 0x00000000);
  4773. /* Discover which interrupts are active/pending */
  4774. inta = _il_rd(il, CSR_INT);
  4775. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  4776. /* Ignore interrupt if there's nothing in NIC to service.
  4777. * This may be due to IRQ shared with another device,
  4778. * or due to sporadic interrupts thrown from our NIC. */
  4779. if (!inta && !inta_fh) {
  4780. D_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  4781. goto none;
  4782. }
  4783. if (inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0) {
  4784. /* Hardware disappeared. It might have already raised
  4785. * an interrupt */
  4786. IL_WARN("HARDWARE GONE?? INTA == 0x%08x\n", inta);
  4787. goto unplugged;
  4788. }
  4789. D_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta, inta_mask,
  4790. inta_fh);
  4791. inta &= ~CSR_INT_BIT_SCD;
  4792. /* il_irq_tasklet() will service interrupts and re-enable them */
  4793. if (likely(inta || inta_fh))
  4794. tasklet_schedule(&il->irq_tasklet);
  4795. unplugged:
  4796. spin_unlock_irqrestore(&il->lock, flags);
  4797. return IRQ_HANDLED;
  4798. none:
  4799. /* re-enable interrupts here since we don't have anything to service. */
  4800. /* only Re-enable if disabled by irq */
  4801. if (test_bit(S_INT_ENABLED, &il->status))
  4802. il_enable_interrupts(il);
  4803. spin_unlock_irqrestore(&il->lock, flags);
  4804. return IRQ_NONE;
  4805. }
  4806. EXPORT_SYMBOL(il_isr);
  4807. /*
  4808. * il_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
  4809. * function.
  4810. */
  4811. void
  4812. il_tx_cmd_protection(struct il_priv *il, struct ieee80211_tx_info *info,
  4813. __le16 fc, __le32 *tx_flags)
  4814. {
  4815. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  4816. *tx_flags |= TX_CMD_FLG_RTS_MSK;
  4817. *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  4818. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4819. if (!ieee80211_is_mgmt(fc))
  4820. return;
  4821. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4822. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  4823. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  4824. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  4825. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  4826. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4827. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4828. break;
  4829. }
  4830. } else if (info->control.rates[0].
  4831. flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  4832. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4833. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4834. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4835. }
  4836. }
  4837. EXPORT_SYMBOL(il_tx_cmd_protection);