common.c 143 KB

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