common.c 147 KB

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