common.c 141 KB

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