mwl8k.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030
  1. /*
  2. * drivers/net/wireless/mwl8k.c
  3. * Driver for Marvell TOPDOG 802.11 Wireless cards
  4. *
  5. * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/completion.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/slab.h>
  23. #include <net/mac80211.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/firmware.h>
  26. #include <linux/workqueue.h>
  27. #define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
  28. #define MWL8K_NAME KBUILD_MODNAME
  29. #define MWL8K_VERSION "0.13"
  30. /* Module parameters */
  31. static bool ap_mode_default;
  32. module_param(ap_mode_default, bool, 0);
  33. MODULE_PARM_DESC(ap_mode_default,
  34. "Set to 1 to make ap mode the default instead of sta mode");
  35. /* Register definitions */
  36. #define MWL8K_HIU_GEN_PTR 0x00000c10
  37. #define MWL8K_MODE_STA 0x0000005a
  38. #define MWL8K_MODE_AP 0x000000a5
  39. #define MWL8K_HIU_INT_CODE 0x00000c14
  40. #define MWL8K_FWSTA_READY 0xf0f1f2f4
  41. #define MWL8K_FWAP_READY 0xf1f2f4a5
  42. #define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
  43. #define MWL8K_HIU_SCRATCH 0x00000c40
  44. /* Host->device communications */
  45. #define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
  46. #define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
  47. #define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
  48. #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
  49. #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
  50. #define MWL8K_H2A_INT_DUMMY (1 << 20)
  51. #define MWL8K_H2A_INT_RESET (1 << 15)
  52. #define MWL8K_H2A_INT_DOORBELL (1 << 1)
  53. #define MWL8K_H2A_INT_PPA_READY (1 << 0)
  54. /* Device->host communications */
  55. #define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
  56. #define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
  57. #define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
  58. #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
  59. #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
  60. #define MWL8K_A2H_INT_DUMMY (1 << 20)
  61. #define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
  62. #define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
  63. #define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
  64. #define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
  65. #define MWL8K_A2H_INT_RADIO_ON (1 << 6)
  66. #define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
  67. #define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
  68. #define MWL8K_A2H_INT_OPC_DONE (1 << 2)
  69. #define MWL8K_A2H_INT_RX_READY (1 << 1)
  70. #define MWL8K_A2H_INT_TX_DONE (1 << 0)
  71. /* HW micro second timer register
  72. * located at offset 0xA600. This
  73. * will be used to timestamp tx
  74. * packets.
  75. */
  76. #define MWL8K_HW_TIMER_REGISTER 0x0000a600
  77. #define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
  78. MWL8K_A2H_INT_CHNL_SWITCHED | \
  79. MWL8K_A2H_INT_QUEUE_EMPTY | \
  80. MWL8K_A2H_INT_RADAR_DETECT | \
  81. MWL8K_A2H_INT_RADIO_ON | \
  82. MWL8K_A2H_INT_RADIO_OFF | \
  83. MWL8K_A2H_INT_MAC_EVENT | \
  84. MWL8K_A2H_INT_OPC_DONE | \
  85. MWL8K_A2H_INT_RX_READY | \
  86. MWL8K_A2H_INT_TX_DONE | \
  87. MWL8K_A2H_INT_BA_WATCHDOG)
  88. #define MWL8K_RX_QUEUES 1
  89. #define MWL8K_TX_WMM_QUEUES 4
  90. #define MWL8K_MAX_AMPDU_QUEUES 8
  91. #define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
  92. #define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
  93. /* txpriorities are mapped with hw queues.
  94. * Each hw queue has a txpriority.
  95. */
  96. #define TOTAL_HW_TX_QUEUES 8
  97. /* Each HW queue can have one AMPDU stream.
  98. * But, because one of the hw queue is reserved,
  99. * maximum AMPDU queues that can be created are
  100. * one short of total tx queues.
  101. */
  102. #define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
  103. struct rxd_ops {
  104. int rxd_size;
  105. void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
  106. void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
  107. int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
  108. __le16 *qos, s8 *noise);
  109. };
  110. struct mwl8k_device_info {
  111. char *part_name;
  112. char *helper_image;
  113. char *fw_image_sta;
  114. char *fw_image_ap;
  115. struct rxd_ops *ap_rxd_ops;
  116. u32 fw_api_ap;
  117. };
  118. struct mwl8k_rx_queue {
  119. int rxd_count;
  120. /* hw receives here */
  121. int head;
  122. /* refill descs here */
  123. int tail;
  124. void *rxd;
  125. dma_addr_t rxd_dma;
  126. struct {
  127. struct sk_buff *skb;
  128. DEFINE_DMA_UNMAP_ADDR(dma);
  129. } *buf;
  130. };
  131. struct mwl8k_tx_queue {
  132. /* hw transmits here */
  133. int head;
  134. /* sw appends here */
  135. int tail;
  136. unsigned int len;
  137. struct mwl8k_tx_desc *txd;
  138. dma_addr_t txd_dma;
  139. struct sk_buff **skb;
  140. };
  141. enum {
  142. AMPDU_NO_STREAM,
  143. AMPDU_STREAM_NEW,
  144. AMPDU_STREAM_IN_PROGRESS,
  145. AMPDU_STREAM_ACTIVE,
  146. };
  147. struct mwl8k_ampdu_stream {
  148. struct ieee80211_sta *sta;
  149. u8 tid;
  150. u8 state;
  151. u8 idx;
  152. };
  153. struct mwl8k_priv {
  154. struct ieee80211_hw *hw;
  155. struct pci_dev *pdev;
  156. int irq;
  157. struct mwl8k_device_info *device_info;
  158. void __iomem *sram;
  159. void __iomem *regs;
  160. /* firmware */
  161. const struct firmware *fw_helper;
  162. const struct firmware *fw_ucode;
  163. /* hardware/firmware parameters */
  164. bool ap_fw;
  165. struct rxd_ops *rxd_ops;
  166. struct ieee80211_supported_band band_24;
  167. struct ieee80211_channel channels_24[14];
  168. struct ieee80211_rate rates_24[14];
  169. struct ieee80211_supported_band band_50;
  170. struct ieee80211_channel channels_50[4];
  171. struct ieee80211_rate rates_50[9];
  172. u32 ap_macids_supported;
  173. u32 sta_macids_supported;
  174. /* Ampdu stream information */
  175. u8 num_ampdu_queues;
  176. spinlock_t stream_lock;
  177. struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
  178. struct work_struct watchdog_ba_handle;
  179. /* firmware access */
  180. struct mutex fw_mutex;
  181. struct task_struct *fw_mutex_owner;
  182. struct task_struct *hw_restart_owner;
  183. int fw_mutex_depth;
  184. struct completion *hostcmd_wait;
  185. atomic_t watchdog_event_pending;
  186. /* lock held over TX and TX reap */
  187. spinlock_t tx_lock;
  188. /* TX quiesce completion, protected by fw_mutex and tx_lock */
  189. struct completion *tx_wait;
  190. /* List of interfaces. */
  191. u32 macids_used;
  192. struct list_head vif_list;
  193. /* power management status cookie from firmware */
  194. u32 *cookie;
  195. dma_addr_t cookie_dma;
  196. u16 num_mcaddrs;
  197. u8 hw_rev;
  198. u32 fw_rev;
  199. /*
  200. * Running count of TX packets in flight, to avoid
  201. * iterating over the transmit rings each time.
  202. */
  203. int pending_tx_pkts;
  204. struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
  205. struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
  206. u32 txq_offset[MWL8K_MAX_TX_QUEUES];
  207. bool radio_on;
  208. bool radio_short_preamble;
  209. bool sniffer_enabled;
  210. bool wmm_enabled;
  211. /* XXX need to convert this to handle multiple interfaces */
  212. bool capture_beacon;
  213. u8 capture_bssid[ETH_ALEN];
  214. struct sk_buff *beacon_skb;
  215. /*
  216. * This FJ worker has to be global as it is scheduled from the
  217. * RX handler. At this point we don't know which interface it
  218. * belongs to until the list of bssids waiting to complete join
  219. * is checked.
  220. */
  221. struct work_struct finalize_join_worker;
  222. /* Tasklet to perform TX reclaim. */
  223. struct tasklet_struct poll_tx_task;
  224. /* Tasklet to perform RX. */
  225. struct tasklet_struct poll_rx_task;
  226. /* Most recently reported noise in dBm */
  227. s8 noise;
  228. /*
  229. * preserve the queue configurations so they can be restored if/when
  230. * the firmware image is swapped.
  231. */
  232. struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
  233. /* To perform the task of reloading the firmware */
  234. struct work_struct fw_reload;
  235. bool hw_restart_in_progress;
  236. /* async firmware loading state */
  237. unsigned fw_state;
  238. char *fw_pref;
  239. char *fw_alt;
  240. struct completion firmware_loading_complete;
  241. };
  242. #define MAX_WEP_KEY_LEN 13
  243. #define NUM_WEP_KEYS 4
  244. /* Per interface specific private data */
  245. struct mwl8k_vif {
  246. struct list_head list;
  247. struct ieee80211_vif *vif;
  248. /* Firmware macid for this vif. */
  249. int macid;
  250. /* Non AMPDU sequence number assigned by driver. */
  251. u16 seqno;
  252. /* Saved WEP keys */
  253. struct {
  254. u8 enabled;
  255. u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
  256. } wep_key_conf[NUM_WEP_KEYS];
  257. /* BSSID */
  258. u8 bssid[ETH_ALEN];
  259. /* A flag to indicate is HW crypto is enabled for this bssid */
  260. bool is_hw_crypto_enabled;
  261. };
  262. #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
  263. #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
  264. struct tx_traffic_info {
  265. u32 start_time;
  266. u32 pkts;
  267. };
  268. #define MWL8K_MAX_TID 8
  269. struct mwl8k_sta {
  270. /* Index into station database. Returned by UPDATE_STADB. */
  271. u8 peer_id;
  272. u8 is_ampdu_allowed;
  273. struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
  274. };
  275. #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
  276. static const struct ieee80211_channel mwl8k_channels_24[] = {
  277. { .center_freq = 2412, .hw_value = 1, },
  278. { .center_freq = 2417, .hw_value = 2, },
  279. { .center_freq = 2422, .hw_value = 3, },
  280. { .center_freq = 2427, .hw_value = 4, },
  281. { .center_freq = 2432, .hw_value = 5, },
  282. { .center_freq = 2437, .hw_value = 6, },
  283. { .center_freq = 2442, .hw_value = 7, },
  284. { .center_freq = 2447, .hw_value = 8, },
  285. { .center_freq = 2452, .hw_value = 9, },
  286. { .center_freq = 2457, .hw_value = 10, },
  287. { .center_freq = 2462, .hw_value = 11, },
  288. { .center_freq = 2467, .hw_value = 12, },
  289. { .center_freq = 2472, .hw_value = 13, },
  290. { .center_freq = 2484, .hw_value = 14, },
  291. };
  292. static const struct ieee80211_rate mwl8k_rates_24[] = {
  293. { .bitrate = 10, .hw_value = 2, },
  294. { .bitrate = 20, .hw_value = 4, },
  295. { .bitrate = 55, .hw_value = 11, },
  296. { .bitrate = 110, .hw_value = 22, },
  297. { .bitrate = 220, .hw_value = 44, },
  298. { .bitrate = 60, .hw_value = 12, },
  299. { .bitrate = 90, .hw_value = 18, },
  300. { .bitrate = 120, .hw_value = 24, },
  301. { .bitrate = 180, .hw_value = 36, },
  302. { .bitrate = 240, .hw_value = 48, },
  303. { .bitrate = 360, .hw_value = 72, },
  304. { .bitrate = 480, .hw_value = 96, },
  305. { .bitrate = 540, .hw_value = 108, },
  306. { .bitrate = 720, .hw_value = 144, },
  307. };
  308. static const struct ieee80211_channel mwl8k_channels_50[] = {
  309. { .center_freq = 5180, .hw_value = 36, },
  310. { .center_freq = 5200, .hw_value = 40, },
  311. { .center_freq = 5220, .hw_value = 44, },
  312. { .center_freq = 5240, .hw_value = 48, },
  313. };
  314. static const struct ieee80211_rate mwl8k_rates_50[] = {
  315. { .bitrate = 60, .hw_value = 12, },
  316. { .bitrate = 90, .hw_value = 18, },
  317. { .bitrate = 120, .hw_value = 24, },
  318. { .bitrate = 180, .hw_value = 36, },
  319. { .bitrate = 240, .hw_value = 48, },
  320. { .bitrate = 360, .hw_value = 72, },
  321. { .bitrate = 480, .hw_value = 96, },
  322. { .bitrate = 540, .hw_value = 108, },
  323. { .bitrate = 720, .hw_value = 144, },
  324. };
  325. /* Set or get info from Firmware */
  326. #define MWL8K_CMD_GET 0x0000
  327. #define MWL8K_CMD_SET 0x0001
  328. #define MWL8K_CMD_SET_LIST 0x0002
  329. /* Firmware command codes */
  330. #define MWL8K_CMD_CODE_DNLD 0x0001
  331. #define MWL8K_CMD_GET_HW_SPEC 0x0003
  332. #define MWL8K_CMD_SET_HW_SPEC 0x0004
  333. #define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
  334. #define MWL8K_CMD_GET_STAT 0x0014
  335. #define MWL8K_CMD_RADIO_CONTROL 0x001c
  336. #define MWL8K_CMD_RF_TX_POWER 0x001e
  337. #define MWL8K_CMD_TX_POWER 0x001f
  338. #define MWL8K_CMD_RF_ANTENNA 0x0020
  339. #define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
  340. #define MWL8K_CMD_SET_PRE_SCAN 0x0107
  341. #define MWL8K_CMD_SET_POST_SCAN 0x0108
  342. #define MWL8K_CMD_SET_RF_CHANNEL 0x010a
  343. #define MWL8K_CMD_SET_AID 0x010d
  344. #define MWL8K_CMD_SET_RATE 0x0110
  345. #define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
  346. #define MWL8K_CMD_RTS_THRESHOLD 0x0113
  347. #define MWL8K_CMD_SET_SLOT 0x0114
  348. #define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
  349. #define MWL8K_CMD_SET_WMM_MODE 0x0123
  350. #define MWL8K_CMD_MIMO_CONFIG 0x0125
  351. #define MWL8K_CMD_USE_FIXED_RATE 0x0126
  352. #define MWL8K_CMD_ENABLE_SNIFFER 0x0150
  353. #define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
  354. #define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
  355. #define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
  356. #define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
  357. #define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
  358. #define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
  359. #define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
  360. #define MWL8K_CMD_UPDATE_STADB 0x1123
  361. #define MWL8K_CMD_BASTREAM 0x1125
  362. static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
  363. {
  364. u16 command = le16_to_cpu(cmd);
  365. #define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
  366. snprintf(buf, bufsize, "%s", #x);\
  367. return buf;\
  368. } while (0)
  369. switch (command & ~0x8000) {
  370. MWL8K_CMDNAME(CODE_DNLD);
  371. MWL8K_CMDNAME(GET_HW_SPEC);
  372. MWL8K_CMDNAME(SET_HW_SPEC);
  373. MWL8K_CMDNAME(MAC_MULTICAST_ADR);
  374. MWL8K_CMDNAME(GET_STAT);
  375. MWL8K_CMDNAME(RADIO_CONTROL);
  376. MWL8K_CMDNAME(RF_TX_POWER);
  377. MWL8K_CMDNAME(TX_POWER);
  378. MWL8K_CMDNAME(RF_ANTENNA);
  379. MWL8K_CMDNAME(SET_BEACON);
  380. MWL8K_CMDNAME(SET_PRE_SCAN);
  381. MWL8K_CMDNAME(SET_POST_SCAN);
  382. MWL8K_CMDNAME(SET_RF_CHANNEL);
  383. MWL8K_CMDNAME(SET_AID);
  384. MWL8K_CMDNAME(SET_RATE);
  385. MWL8K_CMDNAME(SET_FINALIZE_JOIN);
  386. MWL8K_CMDNAME(RTS_THRESHOLD);
  387. MWL8K_CMDNAME(SET_SLOT);
  388. MWL8K_CMDNAME(SET_EDCA_PARAMS);
  389. MWL8K_CMDNAME(SET_WMM_MODE);
  390. MWL8K_CMDNAME(MIMO_CONFIG);
  391. MWL8K_CMDNAME(USE_FIXED_RATE);
  392. MWL8K_CMDNAME(ENABLE_SNIFFER);
  393. MWL8K_CMDNAME(SET_MAC_ADDR);
  394. MWL8K_CMDNAME(SET_RATEADAPT_MODE);
  395. MWL8K_CMDNAME(BSS_START);
  396. MWL8K_CMDNAME(SET_NEW_STN);
  397. MWL8K_CMDNAME(UPDATE_ENCRYPTION);
  398. MWL8K_CMDNAME(UPDATE_STADB);
  399. MWL8K_CMDNAME(BASTREAM);
  400. MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
  401. default:
  402. snprintf(buf, bufsize, "0x%x", cmd);
  403. }
  404. #undef MWL8K_CMDNAME
  405. return buf;
  406. }
  407. /* Hardware and firmware reset */
  408. static void mwl8k_hw_reset(struct mwl8k_priv *priv)
  409. {
  410. iowrite32(MWL8K_H2A_INT_RESET,
  411. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  412. iowrite32(MWL8K_H2A_INT_RESET,
  413. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  414. msleep(20);
  415. }
  416. /* Release fw image */
  417. static void mwl8k_release_fw(const struct firmware **fw)
  418. {
  419. if (*fw == NULL)
  420. return;
  421. release_firmware(*fw);
  422. *fw = NULL;
  423. }
  424. static void mwl8k_release_firmware(struct mwl8k_priv *priv)
  425. {
  426. mwl8k_release_fw(&priv->fw_ucode);
  427. mwl8k_release_fw(&priv->fw_helper);
  428. }
  429. /* states for asynchronous f/w loading */
  430. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
  431. enum {
  432. FW_STATE_INIT = 0,
  433. FW_STATE_LOADING_PREF,
  434. FW_STATE_LOADING_ALT,
  435. FW_STATE_ERROR,
  436. };
  437. /* Request fw image */
  438. static int mwl8k_request_fw(struct mwl8k_priv *priv,
  439. const char *fname, const struct firmware **fw,
  440. bool nowait)
  441. {
  442. /* release current image */
  443. if (*fw != NULL)
  444. mwl8k_release_fw(fw);
  445. if (nowait)
  446. return request_firmware_nowait(THIS_MODULE, 1, fname,
  447. &priv->pdev->dev, GFP_KERNEL,
  448. priv, mwl8k_fw_state_machine);
  449. else
  450. return request_firmware(fw, fname, &priv->pdev->dev);
  451. }
  452. static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
  453. bool nowait)
  454. {
  455. struct mwl8k_device_info *di = priv->device_info;
  456. int rc;
  457. if (di->helper_image != NULL) {
  458. if (nowait)
  459. rc = mwl8k_request_fw(priv, di->helper_image,
  460. &priv->fw_helper, true);
  461. else
  462. rc = mwl8k_request_fw(priv, di->helper_image,
  463. &priv->fw_helper, false);
  464. if (rc)
  465. printk(KERN_ERR "%s: Error requesting helper fw %s\n",
  466. pci_name(priv->pdev), di->helper_image);
  467. if (rc || nowait)
  468. return rc;
  469. }
  470. if (nowait) {
  471. /*
  472. * if we get here, no helper image is needed. Skip the
  473. * FW_STATE_INIT state.
  474. */
  475. priv->fw_state = FW_STATE_LOADING_PREF;
  476. rc = mwl8k_request_fw(priv, fw_image,
  477. &priv->fw_ucode,
  478. true);
  479. } else
  480. rc = mwl8k_request_fw(priv, fw_image,
  481. &priv->fw_ucode, false);
  482. if (rc) {
  483. printk(KERN_ERR "%s: Error requesting firmware file %s\n",
  484. pci_name(priv->pdev), fw_image);
  485. mwl8k_release_fw(&priv->fw_helper);
  486. return rc;
  487. }
  488. return 0;
  489. }
  490. struct mwl8k_cmd_pkt {
  491. __le16 code;
  492. __le16 length;
  493. __u8 seq_num;
  494. __u8 macid;
  495. __le16 result;
  496. char payload[0];
  497. } __packed;
  498. /*
  499. * Firmware loading.
  500. */
  501. static int
  502. mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
  503. {
  504. void __iomem *regs = priv->regs;
  505. dma_addr_t dma_addr;
  506. int loops;
  507. dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
  508. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  509. return -ENOMEM;
  510. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  511. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  512. iowrite32(MWL8K_H2A_INT_DOORBELL,
  513. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  514. iowrite32(MWL8K_H2A_INT_DUMMY,
  515. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  516. loops = 1000;
  517. do {
  518. u32 int_code;
  519. int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
  520. if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
  521. iowrite32(0, regs + MWL8K_HIU_INT_CODE);
  522. break;
  523. }
  524. cond_resched();
  525. udelay(1);
  526. } while (--loops);
  527. pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
  528. return loops ? 0 : -ETIMEDOUT;
  529. }
  530. static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
  531. const u8 *data, size_t length)
  532. {
  533. struct mwl8k_cmd_pkt *cmd;
  534. int done;
  535. int rc = 0;
  536. cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
  537. if (cmd == NULL)
  538. return -ENOMEM;
  539. cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
  540. cmd->seq_num = 0;
  541. cmd->macid = 0;
  542. cmd->result = 0;
  543. done = 0;
  544. while (length) {
  545. int block_size = length > 256 ? 256 : length;
  546. memcpy(cmd->payload, data + done, block_size);
  547. cmd->length = cpu_to_le16(block_size);
  548. rc = mwl8k_send_fw_load_cmd(priv, cmd,
  549. sizeof(*cmd) + block_size);
  550. if (rc)
  551. break;
  552. done += block_size;
  553. length -= block_size;
  554. }
  555. if (!rc) {
  556. cmd->length = 0;
  557. rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
  558. }
  559. kfree(cmd);
  560. return rc;
  561. }
  562. static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
  563. const u8 *data, size_t length)
  564. {
  565. unsigned char *buffer;
  566. int may_continue, rc = 0;
  567. u32 done, prev_block_size;
  568. buffer = kmalloc(1024, GFP_KERNEL);
  569. if (buffer == NULL)
  570. return -ENOMEM;
  571. done = 0;
  572. prev_block_size = 0;
  573. may_continue = 1000;
  574. while (may_continue > 0) {
  575. u32 block_size;
  576. block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
  577. if (block_size & 1) {
  578. block_size &= ~1;
  579. may_continue--;
  580. } else {
  581. done += prev_block_size;
  582. length -= prev_block_size;
  583. }
  584. if (block_size > 1024 || block_size > length) {
  585. rc = -EOVERFLOW;
  586. break;
  587. }
  588. if (length == 0) {
  589. rc = 0;
  590. break;
  591. }
  592. if (block_size == 0) {
  593. rc = -EPROTO;
  594. may_continue--;
  595. udelay(1);
  596. continue;
  597. }
  598. prev_block_size = block_size;
  599. memcpy(buffer, data + done, block_size);
  600. rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
  601. if (rc)
  602. break;
  603. }
  604. if (!rc && length != 0)
  605. rc = -EREMOTEIO;
  606. kfree(buffer);
  607. return rc;
  608. }
  609. static int mwl8k_load_firmware(struct ieee80211_hw *hw)
  610. {
  611. struct mwl8k_priv *priv = hw->priv;
  612. const struct firmware *fw = priv->fw_ucode;
  613. int rc;
  614. int loops;
  615. if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
  616. const struct firmware *helper = priv->fw_helper;
  617. if (helper == NULL) {
  618. printk(KERN_ERR "%s: helper image needed but none "
  619. "given\n", pci_name(priv->pdev));
  620. return -EINVAL;
  621. }
  622. rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
  623. if (rc) {
  624. printk(KERN_ERR "%s: unable to load firmware "
  625. "helper image\n", pci_name(priv->pdev));
  626. return rc;
  627. }
  628. msleep(20);
  629. rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
  630. } else {
  631. rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
  632. }
  633. if (rc) {
  634. printk(KERN_ERR "%s: unable to load firmware image\n",
  635. pci_name(priv->pdev));
  636. return rc;
  637. }
  638. iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
  639. loops = 500000;
  640. do {
  641. u32 ready_code;
  642. ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  643. if (ready_code == MWL8K_FWAP_READY) {
  644. priv->ap_fw = true;
  645. break;
  646. } else if (ready_code == MWL8K_FWSTA_READY) {
  647. priv->ap_fw = false;
  648. break;
  649. }
  650. cond_resched();
  651. udelay(1);
  652. } while (--loops);
  653. return loops ? 0 : -ETIMEDOUT;
  654. }
  655. /* DMA header used by firmware and hardware. */
  656. struct mwl8k_dma_data {
  657. __le16 fwlen;
  658. struct ieee80211_hdr wh;
  659. char data[0];
  660. } __packed;
  661. /* Routines to add/remove DMA header from skb. */
  662. static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
  663. {
  664. struct mwl8k_dma_data *tr;
  665. int hdrlen;
  666. tr = (struct mwl8k_dma_data *)skb->data;
  667. hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
  668. if (hdrlen != sizeof(tr->wh)) {
  669. if (ieee80211_is_data_qos(tr->wh.frame_control)) {
  670. memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
  671. *((__le16 *)(tr->data - 2)) = qos;
  672. } else {
  673. memmove(tr->data - hdrlen, &tr->wh, hdrlen);
  674. }
  675. }
  676. if (hdrlen != sizeof(*tr))
  677. skb_pull(skb, sizeof(*tr) - hdrlen);
  678. }
  679. #define REDUCED_TX_HEADROOM 8
  680. static void
  681. mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
  682. int head_pad, int tail_pad)
  683. {
  684. struct ieee80211_hdr *wh;
  685. int hdrlen;
  686. int reqd_hdrlen;
  687. struct mwl8k_dma_data *tr;
  688. /*
  689. * Add a firmware DMA header; the firmware requires that we
  690. * present a 2-byte payload length followed by a 4-address
  691. * header (without QoS field), followed (optionally) by any
  692. * WEP/ExtIV header (but only filled in for CCMP).
  693. */
  694. wh = (struct ieee80211_hdr *)skb->data;
  695. hdrlen = ieee80211_hdrlen(wh->frame_control);
  696. /*
  697. * Check if skb_resize is required because of
  698. * tx_headroom adjustment.
  699. */
  700. if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
  701. + REDUCED_TX_HEADROOM))) {
  702. if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
  703. wiphy_err(priv->hw->wiphy,
  704. "Failed to reallocate TX buffer\n");
  705. return;
  706. }
  707. skb->truesize += REDUCED_TX_HEADROOM;
  708. }
  709. reqd_hdrlen = sizeof(*tr) + head_pad;
  710. if (hdrlen != reqd_hdrlen)
  711. skb_push(skb, reqd_hdrlen - hdrlen);
  712. if (ieee80211_is_data_qos(wh->frame_control))
  713. hdrlen -= IEEE80211_QOS_CTL_LEN;
  714. tr = (struct mwl8k_dma_data *)skb->data;
  715. if (wh != &tr->wh)
  716. memmove(&tr->wh, wh, hdrlen);
  717. if (hdrlen != sizeof(tr->wh))
  718. memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
  719. /*
  720. * Firmware length is the length of the fully formed "802.11
  721. * payload". That is, everything except for the 802.11 header.
  722. * This includes all crypto material including the MIC.
  723. */
  724. tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
  725. }
  726. static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
  727. struct sk_buff *skb)
  728. {
  729. struct ieee80211_hdr *wh;
  730. struct ieee80211_tx_info *tx_info;
  731. struct ieee80211_key_conf *key_conf;
  732. int data_pad;
  733. int head_pad = 0;
  734. wh = (struct ieee80211_hdr *)skb->data;
  735. tx_info = IEEE80211_SKB_CB(skb);
  736. key_conf = NULL;
  737. if (ieee80211_is_data(wh->frame_control))
  738. key_conf = tx_info->control.hw_key;
  739. /*
  740. * Make sure the packet header is in the DMA header format (4-address
  741. * without QoS), and add head & tail padding when HW crypto is enabled.
  742. *
  743. * We have the following trailer padding requirements:
  744. * - WEP: 4 trailer bytes (ICV)
  745. * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
  746. * - CCMP: 8 trailer bytes (MIC)
  747. */
  748. data_pad = 0;
  749. if (key_conf != NULL) {
  750. head_pad = key_conf->iv_len;
  751. switch (key_conf->cipher) {
  752. case WLAN_CIPHER_SUITE_WEP40:
  753. case WLAN_CIPHER_SUITE_WEP104:
  754. data_pad = 4;
  755. break;
  756. case WLAN_CIPHER_SUITE_TKIP:
  757. data_pad = 12;
  758. break;
  759. case WLAN_CIPHER_SUITE_CCMP:
  760. data_pad = 8;
  761. break;
  762. }
  763. }
  764. mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
  765. }
  766. /*
  767. * Packet reception for 88w8366 AP firmware.
  768. */
  769. struct mwl8k_rxd_8366_ap {
  770. __le16 pkt_len;
  771. __u8 sq2;
  772. __u8 rate;
  773. __le32 pkt_phys_addr;
  774. __le32 next_rxd_phys_addr;
  775. __le16 qos_control;
  776. __le16 htsig2;
  777. __le32 hw_rssi_info;
  778. __le32 hw_noise_floor_info;
  779. __u8 noise_floor;
  780. __u8 pad0[3];
  781. __u8 rssi;
  782. __u8 rx_status;
  783. __u8 channel;
  784. __u8 rx_ctrl;
  785. } __packed;
  786. #define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
  787. #define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
  788. #define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
  789. #define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
  790. /* 8366 AP rx_status bits */
  791. #define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
  792. #define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
  793. #define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
  794. #define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
  795. #define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
  796. static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
  797. {
  798. struct mwl8k_rxd_8366_ap *rxd = _rxd;
  799. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  800. rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
  801. }
  802. static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
  803. {
  804. struct mwl8k_rxd_8366_ap *rxd = _rxd;
  805. rxd->pkt_len = cpu_to_le16(len);
  806. rxd->pkt_phys_addr = cpu_to_le32(addr);
  807. wmb();
  808. rxd->rx_ctrl = 0;
  809. }
  810. static int
  811. mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
  812. __le16 *qos, s8 *noise)
  813. {
  814. struct mwl8k_rxd_8366_ap *rxd = _rxd;
  815. if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
  816. return -1;
  817. rmb();
  818. memset(status, 0, sizeof(*status));
  819. status->signal = -rxd->rssi;
  820. *noise = -rxd->noise_floor;
  821. if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
  822. status->flag |= RX_FLAG_HT;
  823. if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
  824. status->flag |= RX_FLAG_40MHZ;
  825. status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
  826. } else {
  827. int i;
  828. for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
  829. if (mwl8k_rates_24[i].hw_value == rxd->rate) {
  830. status->rate_idx = i;
  831. break;
  832. }
  833. }
  834. }
  835. if (rxd->channel > 14) {
  836. status->band = IEEE80211_BAND_5GHZ;
  837. if (!(status->flag & RX_FLAG_HT))
  838. status->rate_idx -= 5;
  839. } else {
  840. status->band = IEEE80211_BAND_2GHZ;
  841. }
  842. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  843. status->band);
  844. *qos = rxd->qos_control;
  845. if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
  846. (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
  847. (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
  848. status->flag |= RX_FLAG_MMIC_ERROR;
  849. return le16_to_cpu(rxd->pkt_len);
  850. }
  851. static struct rxd_ops rxd_8366_ap_ops = {
  852. .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
  853. .rxd_init = mwl8k_rxd_8366_ap_init,
  854. .rxd_refill = mwl8k_rxd_8366_ap_refill,
  855. .rxd_process = mwl8k_rxd_8366_ap_process,
  856. };
  857. /*
  858. * Packet reception for STA firmware.
  859. */
  860. struct mwl8k_rxd_sta {
  861. __le16 pkt_len;
  862. __u8 link_quality;
  863. __u8 noise_level;
  864. __le32 pkt_phys_addr;
  865. __le32 next_rxd_phys_addr;
  866. __le16 qos_control;
  867. __le16 rate_info;
  868. __le32 pad0[4];
  869. __u8 rssi;
  870. __u8 channel;
  871. __le16 pad1;
  872. __u8 rx_ctrl;
  873. __u8 rx_status;
  874. __u8 pad2[2];
  875. } __packed;
  876. #define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
  877. #define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
  878. #define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
  879. #define MWL8K_STA_RATE_INFO_40MHZ 0x0004
  880. #define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
  881. #define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
  882. #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
  883. #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
  884. /* ICV=0 or MIC=1 */
  885. #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
  886. /* Key is uploaded only in failure case */
  887. #define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
  888. static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
  889. {
  890. struct mwl8k_rxd_sta *rxd = _rxd;
  891. rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
  892. rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
  893. }
  894. static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
  895. {
  896. struct mwl8k_rxd_sta *rxd = _rxd;
  897. rxd->pkt_len = cpu_to_le16(len);
  898. rxd->pkt_phys_addr = cpu_to_le32(addr);
  899. wmb();
  900. rxd->rx_ctrl = 0;
  901. }
  902. static int
  903. mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
  904. __le16 *qos, s8 *noise)
  905. {
  906. struct mwl8k_rxd_sta *rxd = _rxd;
  907. u16 rate_info;
  908. if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
  909. return -1;
  910. rmb();
  911. rate_info = le16_to_cpu(rxd->rate_info);
  912. memset(status, 0, sizeof(*status));
  913. status->signal = -rxd->rssi;
  914. *noise = -rxd->noise_level;
  915. status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
  916. status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
  917. if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
  918. status->flag |= RX_FLAG_SHORTPRE;
  919. if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
  920. status->flag |= RX_FLAG_40MHZ;
  921. if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
  922. status->flag |= RX_FLAG_SHORT_GI;
  923. if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
  924. status->flag |= RX_FLAG_HT;
  925. if (rxd->channel > 14) {
  926. status->band = IEEE80211_BAND_5GHZ;
  927. if (!(status->flag & RX_FLAG_HT))
  928. status->rate_idx -= 5;
  929. } else {
  930. status->band = IEEE80211_BAND_2GHZ;
  931. }
  932. status->freq = ieee80211_channel_to_frequency(rxd->channel,
  933. status->band);
  934. *qos = rxd->qos_control;
  935. if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
  936. (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
  937. status->flag |= RX_FLAG_MMIC_ERROR;
  938. return le16_to_cpu(rxd->pkt_len);
  939. }
  940. static struct rxd_ops rxd_sta_ops = {
  941. .rxd_size = sizeof(struct mwl8k_rxd_sta),
  942. .rxd_init = mwl8k_rxd_sta_init,
  943. .rxd_refill = mwl8k_rxd_sta_refill,
  944. .rxd_process = mwl8k_rxd_sta_process,
  945. };
  946. #define MWL8K_RX_DESCS 256
  947. #define MWL8K_RX_MAXSZ 3800
  948. static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
  949. {
  950. struct mwl8k_priv *priv = hw->priv;
  951. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  952. int size;
  953. int i;
  954. rxq->rxd_count = 0;
  955. rxq->head = 0;
  956. rxq->tail = 0;
  957. size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
  958. rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
  959. if (rxq->rxd == NULL) {
  960. wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
  961. return -ENOMEM;
  962. }
  963. memset(rxq->rxd, 0, size);
  964. rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
  965. if (rxq->buf == NULL) {
  966. wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
  967. pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
  968. return -ENOMEM;
  969. }
  970. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  971. int desc_size;
  972. void *rxd;
  973. int nexti;
  974. dma_addr_t next_dma_addr;
  975. desc_size = priv->rxd_ops->rxd_size;
  976. rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
  977. nexti = i + 1;
  978. if (nexti == MWL8K_RX_DESCS)
  979. nexti = 0;
  980. next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
  981. priv->rxd_ops->rxd_init(rxd, next_dma_addr);
  982. }
  983. return 0;
  984. }
  985. static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
  986. {
  987. struct mwl8k_priv *priv = hw->priv;
  988. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  989. int refilled;
  990. refilled = 0;
  991. while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
  992. struct sk_buff *skb;
  993. dma_addr_t addr;
  994. int rx;
  995. void *rxd;
  996. skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
  997. if (skb == NULL)
  998. break;
  999. addr = pci_map_single(priv->pdev, skb->data,
  1000. MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
  1001. rxq->rxd_count++;
  1002. rx = rxq->tail++;
  1003. if (rxq->tail == MWL8K_RX_DESCS)
  1004. rxq->tail = 0;
  1005. rxq->buf[rx].skb = skb;
  1006. dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
  1007. rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
  1008. priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
  1009. refilled++;
  1010. }
  1011. return refilled;
  1012. }
  1013. /* Must be called only when the card's reception is completely halted */
  1014. static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
  1015. {
  1016. struct mwl8k_priv *priv = hw->priv;
  1017. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1018. int i;
  1019. if (rxq->rxd == NULL)
  1020. return;
  1021. for (i = 0; i < MWL8K_RX_DESCS; i++) {
  1022. if (rxq->buf[i].skb != NULL) {
  1023. pci_unmap_single(priv->pdev,
  1024. dma_unmap_addr(&rxq->buf[i], dma),
  1025. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1026. dma_unmap_addr_set(&rxq->buf[i], dma, 0);
  1027. kfree_skb(rxq->buf[i].skb);
  1028. rxq->buf[i].skb = NULL;
  1029. }
  1030. }
  1031. kfree(rxq->buf);
  1032. rxq->buf = NULL;
  1033. pci_free_consistent(priv->pdev,
  1034. MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
  1035. rxq->rxd, rxq->rxd_dma);
  1036. rxq->rxd = NULL;
  1037. }
  1038. /*
  1039. * Scan a list of BSSIDs to process for finalize join.
  1040. * Allows for extension to process multiple BSSIDs.
  1041. */
  1042. static inline int
  1043. mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
  1044. {
  1045. return priv->capture_beacon &&
  1046. ieee80211_is_beacon(wh->frame_control) &&
  1047. ether_addr_equal(wh->addr3, priv->capture_bssid);
  1048. }
  1049. static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
  1050. struct sk_buff *skb)
  1051. {
  1052. struct mwl8k_priv *priv = hw->priv;
  1053. priv->capture_beacon = false;
  1054. memset(priv->capture_bssid, 0, ETH_ALEN);
  1055. /*
  1056. * Use GFP_ATOMIC as rxq_process is called from
  1057. * the primary interrupt handler, memory allocation call
  1058. * must not sleep.
  1059. */
  1060. priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
  1061. if (priv->beacon_skb != NULL)
  1062. ieee80211_queue_work(hw, &priv->finalize_join_worker);
  1063. }
  1064. static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
  1065. u8 *bssid)
  1066. {
  1067. struct mwl8k_vif *mwl8k_vif;
  1068. list_for_each_entry(mwl8k_vif,
  1069. vif_list, list) {
  1070. if (memcmp(bssid, mwl8k_vif->bssid,
  1071. ETH_ALEN) == 0)
  1072. return mwl8k_vif;
  1073. }
  1074. return NULL;
  1075. }
  1076. static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
  1077. {
  1078. struct mwl8k_priv *priv = hw->priv;
  1079. struct mwl8k_vif *mwl8k_vif = NULL;
  1080. struct mwl8k_rx_queue *rxq = priv->rxq + index;
  1081. int processed;
  1082. processed = 0;
  1083. while (rxq->rxd_count && limit--) {
  1084. struct sk_buff *skb;
  1085. void *rxd;
  1086. int pkt_len;
  1087. struct ieee80211_rx_status status;
  1088. struct ieee80211_hdr *wh;
  1089. __le16 qos;
  1090. skb = rxq->buf[rxq->head].skb;
  1091. if (skb == NULL)
  1092. break;
  1093. rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
  1094. pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
  1095. &priv->noise);
  1096. if (pkt_len < 0)
  1097. break;
  1098. rxq->buf[rxq->head].skb = NULL;
  1099. pci_unmap_single(priv->pdev,
  1100. dma_unmap_addr(&rxq->buf[rxq->head], dma),
  1101. MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
  1102. dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
  1103. rxq->head++;
  1104. if (rxq->head == MWL8K_RX_DESCS)
  1105. rxq->head = 0;
  1106. rxq->rxd_count--;
  1107. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1108. /*
  1109. * Check for a pending join operation. Save a
  1110. * copy of the beacon and schedule a tasklet to
  1111. * send a FINALIZE_JOIN command to the firmware.
  1112. */
  1113. if (mwl8k_capture_bssid(priv, (void *)skb->data))
  1114. mwl8k_save_beacon(hw, skb);
  1115. if (ieee80211_has_protected(wh->frame_control)) {
  1116. /* Check if hw crypto has been enabled for
  1117. * this bss. If yes, set the status flags
  1118. * accordingly
  1119. */
  1120. mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
  1121. wh->addr1);
  1122. if (mwl8k_vif != NULL &&
  1123. mwl8k_vif->is_hw_crypto_enabled) {
  1124. /*
  1125. * When MMIC ERROR is encountered
  1126. * by the firmware, payload is
  1127. * dropped and only 32 bytes of
  1128. * mwl8k Firmware header is sent
  1129. * to the host.
  1130. *
  1131. * We need to add four bytes of
  1132. * key information. In it
  1133. * MAC80211 expects keyidx set to
  1134. * 0 for triggering Counter
  1135. * Measure of MMIC failure.
  1136. */
  1137. if (status.flag & RX_FLAG_MMIC_ERROR) {
  1138. struct mwl8k_dma_data *tr;
  1139. tr = (struct mwl8k_dma_data *)skb->data;
  1140. memset((void *)&(tr->data), 0, 4);
  1141. pkt_len += 4;
  1142. }
  1143. if (!ieee80211_is_auth(wh->frame_control))
  1144. status.flag |= RX_FLAG_IV_STRIPPED |
  1145. RX_FLAG_DECRYPTED |
  1146. RX_FLAG_MMIC_STRIPPED;
  1147. }
  1148. }
  1149. skb_put(skb, pkt_len);
  1150. mwl8k_remove_dma_header(skb, qos);
  1151. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  1152. ieee80211_rx_irqsafe(hw, skb);
  1153. processed++;
  1154. }
  1155. return processed;
  1156. }
  1157. /*
  1158. * Packet transmission.
  1159. */
  1160. #define MWL8K_TXD_STATUS_OK 0x00000001
  1161. #define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
  1162. #define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
  1163. #define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
  1164. #define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
  1165. #define MWL8K_QOS_QLEN_UNSPEC 0xff00
  1166. #define MWL8K_QOS_ACK_POLICY_MASK 0x0060
  1167. #define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
  1168. #define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
  1169. #define MWL8K_QOS_EOSP 0x0010
  1170. struct mwl8k_tx_desc {
  1171. __le32 status;
  1172. __u8 data_rate;
  1173. __u8 tx_priority;
  1174. __le16 qos_control;
  1175. __le32 pkt_phys_addr;
  1176. __le16 pkt_len;
  1177. __u8 dest_MAC_addr[ETH_ALEN];
  1178. __le32 next_txd_phys_addr;
  1179. __le32 timestamp;
  1180. __le16 rate_info;
  1181. __u8 peer_id;
  1182. __u8 tx_frag_cnt;
  1183. } __packed;
  1184. #define MWL8K_TX_DESCS 128
  1185. static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
  1186. {
  1187. struct mwl8k_priv *priv = hw->priv;
  1188. struct mwl8k_tx_queue *txq = priv->txq + index;
  1189. int size;
  1190. int i;
  1191. txq->len = 0;
  1192. txq->head = 0;
  1193. txq->tail = 0;
  1194. size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
  1195. txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
  1196. if (txq->txd == NULL) {
  1197. wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
  1198. return -ENOMEM;
  1199. }
  1200. memset(txq->txd, 0, size);
  1201. txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
  1202. if (txq->skb == NULL) {
  1203. wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
  1204. pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
  1205. return -ENOMEM;
  1206. }
  1207. for (i = 0; i < MWL8K_TX_DESCS; i++) {
  1208. struct mwl8k_tx_desc *tx_desc;
  1209. int nexti;
  1210. tx_desc = txq->txd + i;
  1211. nexti = (i + 1) % MWL8K_TX_DESCS;
  1212. tx_desc->status = 0;
  1213. tx_desc->next_txd_phys_addr =
  1214. cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
  1215. }
  1216. return 0;
  1217. }
  1218. static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
  1219. {
  1220. iowrite32(MWL8K_H2A_INT_PPA_READY,
  1221. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1222. iowrite32(MWL8K_H2A_INT_DUMMY,
  1223. priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1224. ioread32(priv->regs + MWL8K_HIU_INT_CODE);
  1225. }
  1226. static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
  1227. {
  1228. struct mwl8k_priv *priv = hw->priv;
  1229. int i;
  1230. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  1231. struct mwl8k_tx_queue *txq = priv->txq + i;
  1232. int fw_owned = 0;
  1233. int drv_owned = 0;
  1234. int unused = 0;
  1235. int desc;
  1236. for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
  1237. struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
  1238. u32 status;
  1239. status = le32_to_cpu(tx_desc->status);
  1240. if (status & MWL8K_TXD_STATUS_FW_OWNED)
  1241. fw_owned++;
  1242. else
  1243. drv_owned++;
  1244. if (tx_desc->pkt_len == 0)
  1245. unused++;
  1246. }
  1247. wiphy_err(hw->wiphy,
  1248. "txq[%d] len=%d head=%d tail=%d "
  1249. "fw_owned=%d drv_owned=%d unused=%d\n",
  1250. i,
  1251. txq->len, txq->head, txq->tail,
  1252. fw_owned, drv_owned, unused);
  1253. }
  1254. }
  1255. /*
  1256. * Must be called with priv->fw_mutex held and tx queues stopped.
  1257. */
  1258. #define MWL8K_TX_WAIT_TIMEOUT_MS 5000
  1259. static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
  1260. {
  1261. struct mwl8k_priv *priv = hw->priv;
  1262. DECLARE_COMPLETION_ONSTACK(tx_wait);
  1263. int retry;
  1264. int rc;
  1265. might_sleep();
  1266. /* Since fw restart is in progress, allow only the firmware
  1267. * commands from the restart code and block the other
  1268. * commands since they are going to fail in any case since
  1269. * the firmware has crashed
  1270. */
  1271. if (priv->hw_restart_in_progress) {
  1272. if (priv->hw_restart_owner == current)
  1273. return 0;
  1274. else
  1275. return -EBUSY;
  1276. }
  1277. if (atomic_read(&priv->watchdog_event_pending))
  1278. return 0;
  1279. /*
  1280. * The TX queues are stopped at this point, so this test
  1281. * doesn't need to take ->tx_lock.
  1282. */
  1283. if (!priv->pending_tx_pkts)
  1284. return 0;
  1285. retry = 0;
  1286. rc = 0;
  1287. spin_lock_bh(&priv->tx_lock);
  1288. priv->tx_wait = &tx_wait;
  1289. while (!rc) {
  1290. int oldcount;
  1291. unsigned long timeout;
  1292. oldcount = priv->pending_tx_pkts;
  1293. spin_unlock_bh(&priv->tx_lock);
  1294. timeout = wait_for_completion_timeout(&tx_wait,
  1295. msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
  1296. if (atomic_read(&priv->watchdog_event_pending)) {
  1297. spin_lock_bh(&priv->tx_lock);
  1298. priv->tx_wait = NULL;
  1299. spin_unlock_bh(&priv->tx_lock);
  1300. return 0;
  1301. }
  1302. spin_lock_bh(&priv->tx_lock);
  1303. if (timeout) {
  1304. WARN_ON(priv->pending_tx_pkts);
  1305. if (retry)
  1306. wiphy_notice(hw->wiphy, "tx rings drained\n");
  1307. break;
  1308. }
  1309. if (priv->pending_tx_pkts < oldcount) {
  1310. wiphy_notice(hw->wiphy,
  1311. "waiting for tx rings to drain (%d -> %d pkts)\n",
  1312. oldcount, priv->pending_tx_pkts);
  1313. retry = 1;
  1314. continue;
  1315. }
  1316. priv->tx_wait = NULL;
  1317. wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
  1318. MWL8K_TX_WAIT_TIMEOUT_MS);
  1319. mwl8k_dump_tx_rings(hw);
  1320. priv->hw_restart_in_progress = true;
  1321. ieee80211_queue_work(hw, &priv->fw_reload);
  1322. rc = -ETIMEDOUT;
  1323. }
  1324. priv->tx_wait = NULL;
  1325. spin_unlock_bh(&priv->tx_lock);
  1326. return rc;
  1327. }
  1328. #define MWL8K_TXD_SUCCESS(status) \
  1329. ((status) & (MWL8K_TXD_STATUS_OK | \
  1330. MWL8K_TXD_STATUS_OK_RETRY | \
  1331. MWL8K_TXD_STATUS_OK_MORE_RETRY))
  1332. static int mwl8k_tid_queue_mapping(u8 tid)
  1333. {
  1334. BUG_ON(tid > 7);
  1335. switch (tid) {
  1336. case 0:
  1337. case 3:
  1338. return IEEE80211_AC_BE;
  1339. break;
  1340. case 1:
  1341. case 2:
  1342. return IEEE80211_AC_BK;
  1343. break;
  1344. case 4:
  1345. case 5:
  1346. return IEEE80211_AC_VI;
  1347. break;
  1348. case 6:
  1349. case 7:
  1350. return IEEE80211_AC_VO;
  1351. break;
  1352. default:
  1353. return -1;
  1354. break;
  1355. }
  1356. }
  1357. /* The firmware will fill in the rate information
  1358. * for each packet that gets queued in the hardware
  1359. * and these macros will interpret that info.
  1360. */
  1361. #define RI_FORMAT(a) (a & 0x0001)
  1362. #define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
  1363. static int
  1364. mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
  1365. {
  1366. struct mwl8k_priv *priv = hw->priv;
  1367. struct mwl8k_tx_queue *txq = priv->txq + index;
  1368. int processed;
  1369. processed = 0;
  1370. while (txq->len > 0 && limit--) {
  1371. int tx;
  1372. struct mwl8k_tx_desc *tx_desc;
  1373. unsigned long addr;
  1374. int size;
  1375. struct sk_buff *skb;
  1376. struct ieee80211_tx_info *info;
  1377. u32 status;
  1378. struct ieee80211_sta *sta;
  1379. struct mwl8k_sta *sta_info = NULL;
  1380. u16 rate_info;
  1381. struct ieee80211_hdr *wh;
  1382. tx = txq->head;
  1383. tx_desc = txq->txd + tx;
  1384. status = le32_to_cpu(tx_desc->status);
  1385. if (status & MWL8K_TXD_STATUS_FW_OWNED) {
  1386. if (!force)
  1387. break;
  1388. tx_desc->status &=
  1389. ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
  1390. }
  1391. txq->head = (tx + 1) % MWL8K_TX_DESCS;
  1392. BUG_ON(txq->len == 0);
  1393. txq->len--;
  1394. priv->pending_tx_pkts--;
  1395. addr = le32_to_cpu(tx_desc->pkt_phys_addr);
  1396. size = le16_to_cpu(tx_desc->pkt_len);
  1397. skb = txq->skb[tx];
  1398. txq->skb[tx] = NULL;
  1399. BUG_ON(skb == NULL);
  1400. pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
  1401. mwl8k_remove_dma_header(skb, tx_desc->qos_control);
  1402. wh = (struct ieee80211_hdr *) skb->data;
  1403. /* Mark descriptor as unused */
  1404. tx_desc->pkt_phys_addr = 0;
  1405. tx_desc->pkt_len = 0;
  1406. info = IEEE80211_SKB_CB(skb);
  1407. if (ieee80211_is_data(wh->frame_control)) {
  1408. rcu_read_lock();
  1409. sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
  1410. wh->addr2);
  1411. if (sta) {
  1412. sta_info = MWL8K_STA(sta);
  1413. BUG_ON(sta_info == NULL);
  1414. rate_info = le16_to_cpu(tx_desc->rate_info);
  1415. /* If rate is < 6.5 Mpbs for an ht station
  1416. * do not form an ampdu. If the station is a
  1417. * legacy station (format = 0), do not form an
  1418. * ampdu
  1419. */
  1420. if (RI_RATE_ID_MCS(rate_info) < 1 ||
  1421. RI_FORMAT(rate_info) == 0) {
  1422. sta_info->is_ampdu_allowed = false;
  1423. } else {
  1424. sta_info->is_ampdu_allowed = true;
  1425. }
  1426. }
  1427. rcu_read_unlock();
  1428. }
  1429. ieee80211_tx_info_clear_status(info);
  1430. /* Rate control is happening in the firmware.
  1431. * Ensure no tx rate is being reported.
  1432. */
  1433. info->status.rates[0].idx = -1;
  1434. info->status.rates[0].count = 1;
  1435. if (MWL8K_TXD_SUCCESS(status))
  1436. info->flags |= IEEE80211_TX_STAT_ACK;
  1437. ieee80211_tx_status_irqsafe(hw, skb);
  1438. processed++;
  1439. }
  1440. return processed;
  1441. }
  1442. /* must be called only when the card's transmit is completely halted */
  1443. static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
  1444. {
  1445. struct mwl8k_priv *priv = hw->priv;
  1446. struct mwl8k_tx_queue *txq = priv->txq + index;
  1447. if (txq->txd == NULL)
  1448. return;
  1449. mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
  1450. kfree(txq->skb);
  1451. txq->skb = NULL;
  1452. pci_free_consistent(priv->pdev,
  1453. MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
  1454. txq->txd, txq->txd_dma);
  1455. txq->txd = NULL;
  1456. }
  1457. /* caller must hold priv->stream_lock when calling the stream functions */
  1458. static struct mwl8k_ampdu_stream *
  1459. mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
  1460. {
  1461. struct mwl8k_ampdu_stream *stream;
  1462. struct mwl8k_priv *priv = hw->priv;
  1463. int i;
  1464. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1465. stream = &priv->ampdu[i];
  1466. if (stream->state == AMPDU_NO_STREAM) {
  1467. stream->sta = sta;
  1468. stream->state = AMPDU_STREAM_NEW;
  1469. stream->tid = tid;
  1470. stream->idx = i;
  1471. wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
  1472. sta->addr, tid);
  1473. return stream;
  1474. }
  1475. }
  1476. return NULL;
  1477. }
  1478. static int
  1479. mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1480. {
  1481. int ret;
  1482. /* if the stream has already been started, don't start it again */
  1483. if (stream->state != AMPDU_STREAM_NEW)
  1484. return 0;
  1485. ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
  1486. if (ret)
  1487. wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
  1488. "%d\n", stream->sta->addr, stream->tid, ret);
  1489. else
  1490. wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
  1491. stream->sta->addr, stream->tid);
  1492. return ret;
  1493. }
  1494. static void
  1495. mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
  1496. {
  1497. wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
  1498. stream->tid);
  1499. memset(stream, 0, sizeof(*stream));
  1500. }
  1501. static struct mwl8k_ampdu_stream *
  1502. mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
  1503. {
  1504. struct mwl8k_priv *priv = hw->priv;
  1505. int i;
  1506. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  1507. struct mwl8k_ampdu_stream *stream;
  1508. stream = &priv->ampdu[i];
  1509. if (stream->state == AMPDU_NO_STREAM)
  1510. continue;
  1511. if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
  1512. stream->tid == tid)
  1513. return stream;
  1514. }
  1515. return NULL;
  1516. }
  1517. #define MWL8K_AMPDU_PACKET_THRESHOLD 64
  1518. static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
  1519. {
  1520. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1521. struct tx_traffic_info *tx_stats;
  1522. BUG_ON(tid >= MWL8K_MAX_TID);
  1523. tx_stats = &sta_info->tx_stats[tid];
  1524. return sta_info->is_ampdu_allowed &&
  1525. tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
  1526. }
  1527. static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
  1528. {
  1529. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  1530. struct tx_traffic_info *tx_stats;
  1531. BUG_ON(tid >= MWL8K_MAX_TID);
  1532. tx_stats = &sta_info->tx_stats[tid];
  1533. if (tx_stats->start_time == 0)
  1534. tx_stats->start_time = jiffies;
  1535. /* reset the packet count after each second elapses. If the number of
  1536. * packets ever exceeds the ampdu_min_traffic threshold, we will allow
  1537. * an ampdu stream to be started.
  1538. */
  1539. if (jiffies - tx_stats->start_time > HZ) {
  1540. tx_stats->pkts = 0;
  1541. tx_stats->start_time = 0;
  1542. } else
  1543. tx_stats->pkts++;
  1544. }
  1545. /* The hardware ampdu queues start from 5.
  1546. * txpriorities for ampdu queues are
  1547. * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
  1548. * and queue 3 is lowest (queue 4 is reserved)
  1549. */
  1550. #define BA_QUEUE 5
  1551. static void
  1552. mwl8k_txq_xmit(struct ieee80211_hw *hw,
  1553. int index,
  1554. struct ieee80211_sta *sta,
  1555. struct sk_buff *skb)
  1556. {
  1557. struct mwl8k_priv *priv = hw->priv;
  1558. struct ieee80211_tx_info *tx_info;
  1559. struct mwl8k_vif *mwl8k_vif;
  1560. struct ieee80211_hdr *wh;
  1561. struct mwl8k_tx_queue *txq;
  1562. struct mwl8k_tx_desc *tx;
  1563. dma_addr_t dma;
  1564. u32 txstatus;
  1565. u8 txdatarate;
  1566. u16 qos;
  1567. int txpriority;
  1568. u8 tid = 0;
  1569. struct mwl8k_ampdu_stream *stream = NULL;
  1570. bool start_ba_session = false;
  1571. bool mgmtframe = false;
  1572. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1573. bool eapol_frame = false;
  1574. wh = (struct ieee80211_hdr *)skb->data;
  1575. if (ieee80211_is_data_qos(wh->frame_control))
  1576. qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
  1577. else
  1578. qos = 0;
  1579. if (skb->protocol == cpu_to_be16(ETH_P_PAE))
  1580. eapol_frame = true;
  1581. if (ieee80211_is_mgmt(wh->frame_control))
  1582. mgmtframe = true;
  1583. if (priv->ap_fw)
  1584. mwl8k_encapsulate_tx_frame(priv, skb);
  1585. else
  1586. mwl8k_add_dma_header(priv, skb, 0, 0);
  1587. wh = &((struct mwl8k_dma_data *)skb->data)->wh;
  1588. tx_info = IEEE80211_SKB_CB(skb);
  1589. mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
  1590. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1591. wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1592. wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
  1593. mwl8k_vif->seqno += 0x10;
  1594. }
  1595. /* Setup firmware control bit fields for each frame type. */
  1596. txstatus = 0;
  1597. txdatarate = 0;
  1598. if (ieee80211_is_mgmt(wh->frame_control) ||
  1599. ieee80211_is_ctl(wh->frame_control)) {
  1600. txdatarate = 0;
  1601. qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
  1602. } else if (ieee80211_is_data(wh->frame_control)) {
  1603. txdatarate = 1;
  1604. if (is_multicast_ether_addr(wh->addr1))
  1605. txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
  1606. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1607. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1608. qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
  1609. else
  1610. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1611. }
  1612. /* Queue ADDBA request in the respective data queue. While setting up
  1613. * the ampdu stream, mac80211 queues further packets for that
  1614. * particular ra/tid pair. However, packets piled up in the hardware
  1615. * for that ra/tid pair will still go out. ADDBA request and the
  1616. * related data packets going out from different queues asynchronously
  1617. * will cause a shift in the receiver window which might result in
  1618. * ampdu packets getting dropped at the receiver after the stream has
  1619. * been setup.
  1620. */
  1621. if (unlikely(ieee80211_is_action(wh->frame_control) &&
  1622. mgmt->u.action.category == WLAN_CATEGORY_BACK &&
  1623. mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
  1624. priv->ap_fw)) {
  1625. u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  1626. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1627. index = mwl8k_tid_queue_mapping(tid);
  1628. }
  1629. txpriority = index;
  1630. if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
  1631. ieee80211_is_data_qos(wh->frame_control)) {
  1632. tid = qos & 0xf;
  1633. mwl8k_tx_count_packet(sta, tid);
  1634. spin_lock(&priv->stream_lock);
  1635. stream = mwl8k_lookup_stream(hw, sta->addr, tid);
  1636. if (stream != NULL) {
  1637. if (stream->state == AMPDU_STREAM_ACTIVE) {
  1638. WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
  1639. txpriority = (BA_QUEUE + stream->idx) %
  1640. TOTAL_HW_TX_QUEUES;
  1641. if (stream->idx <= 1)
  1642. index = stream->idx +
  1643. MWL8K_TX_WMM_QUEUES;
  1644. } else if (stream->state == AMPDU_STREAM_NEW) {
  1645. /* We get here if the driver sends us packets
  1646. * after we've initiated a stream, but before
  1647. * our ampdu_action routine has been called
  1648. * with IEEE80211_AMPDU_TX_START to get the SSN
  1649. * for the ADDBA request. So this packet can
  1650. * go out with no risk of sequence number
  1651. * mismatch. No special handling is required.
  1652. */
  1653. } else {
  1654. /* Drop packets that would go out after the
  1655. * ADDBA request was sent but before the ADDBA
  1656. * response is received. If we don't do this,
  1657. * the recipient would probably receive it
  1658. * after the ADDBA request with SSN 0. This
  1659. * will cause the recipient's BA receive window
  1660. * to shift, which would cause the subsequent
  1661. * packets in the BA stream to be discarded.
  1662. * mac80211 queues our packets for us in this
  1663. * case, so this is really just a safety check.
  1664. */
  1665. wiphy_warn(hw->wiphy,
  1666. "Cannot send packet while ADDBA "
  1667. "dialog is underway.\n");
  1668. spin_unlock(&priv->stream_lock);
  1669. dev_kfree_skb(skb);
  1670. return;
  1671. }
  1672. } else {
  1673. /* Defer calling mwl8k_start_stream so that the current
  1674. * skb can go out before the ADDBA request. This
  1675. * prevents sequence number mismatch at the recepient
  1676. * as described above.
  1677. */
  1678. if (mwl8k_ampdu_allowed(sta, tid)) {
  1679. stream = mwl8k_add_stream(hw, sta, tid);
  1680. if (stream != NULL)
  1681. start_ba_session = true;
  1682. }
  1683. }
  1684. spin_unlock(&priv->stream_lock);
  1685. } else {
  1686. qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
  1687. qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
  1688. }
  1689. dma = pci_map_single(priv->pdev, skb->data,
  1690. skb->len, PCI_DMA_TODEVICE);
  1691. if (pci_dma_mapping_error(priv->pdev, dma)) {
  1692. wiphy_debug(hw->wiphy,
  1693. "failed to dma map skb, dropping TX frame.\n");
  1694. if (start_ba_session) {
  1695. spin_lock(&priv->stream_lock);
  1696. mwl8k_remove_stream(hw, stream);
  1697. spin_unlock(&priv->stream_lock);
  1698. }
  1699. dev_kfree_skb(skb);
  1700. return;
  1701. }
  1702. spin_lock_bh(&priv->tx_lock);
  1703. txq = priv->txq + index;
  1704. /* Mgmt frames that go out frequently are probe
  1705. * responses. Other mgmt frames got out relatively
  1706. * infrequently. Hence reserve 2 buffers so that
  1707. * other mgmt frames do not get dropped due to an
  1708. * already queued probe response in one of the
  1709. * reserved buffers.
  1710. */
  1711. if (txq->len >= MWL8K_TX_DESCS - 2) {
  1712. if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
  1713. if (start_ba_session) {
  1714. spin_lock(&priv->stream_lock);
  1715. mwl8k_remove_stream(hw, stream);
  1716. spin_unlock(&priv->stream_lock);
  1717. }
  1718. spin_unlock_bh(&priv->tx_lock);
  1719. pci_unmap_single(priv->pdev, dma, skb->len,
  1720. PCI_DMA_TODEVICE);
  1721. dev_kfree_skb(skb);
  1722. return;
  1723. }
  1724. }
  1725. BUG_ON(txq->skb[txq->tail] != NULL);
  1726. txq->skb[txq->tail] = skb;
  1727. tx = txq->txd + txq->tail;
  1728. tx->data_rate = txdatarate;
  1729. tx->tx_priority = txpriority;
  1730. tx->qos_control = cpu_to_le16(qos);
  1731. tx->pkt_phys_addr = cpu_to_le32(dma);
  1732. tx->pkt_len = cpu_to_le16(skb->len);
  1733. tx->rate_info = 0;
  1734. if (!priv->ap_fw && sta != NULL)
  1735. tx->peer_id = MWL8K_STA(sta)->peer_id;
  1736. else
  1737. tx->peer_id = 0;
  1738. if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
  1739. tx->timestamp = cpu_to_le32(ioread32(priv->regs +
  1740. MWL8K_HW_TIMER_REGISTER));
  1741. else
  1742. tx->timestamp = 0;
  1743. wmb();
  1744. tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
  1745. txq->len++;
  1746. priv->pending_tx_pkts++;
  1747. txq->tail++;
  1748. if (txq->tail == MWL8K_TX_DESCS)
  1749. txq->tail = 0;
  1750. mwl8k_tx_start(priv);
  1751. spin_unlock_bh(&priv->tx_lock);
  1752. /* Initiate the ampdu session here */
  1753. if (start_ba_session) {
  1754. spin_lock(&priv->stream_lock);
  1755. if (mwl8k_start_stream(hw, stream))
  1756. mwl8k_remove_stream(hw, stream);
  1757. spin_unlock(&priv->stream_lock);
  1758. }
  1759. }
  1760. /*
  1761. * Firmware access.
  1762. *
  1763. * We have the following requirements for issuing firmware commands:
  1764. * - Some commands require that the packet transmit path is idle when
  1765. * the command is issued. (For simplicity, we'll just quiesce the
  1766. * transmit path for every command.)
  1767. * - There are certain sequences of commands that need to be issued to
  1768. * the hardware sequentially, with no other intervening commands.
  1769. *
  1770. * This leads to an implementation of a "firmware lock" as a mutex that
  1771. * can be taken recursively, and which is taken by both the low-level
  1772. * command submission function (mwl8k_post_cmd) as well as any users of
  1773. * that function that require issuing of an atomic sequence of commands,
  1774. * and quiesces the transmit path whenever it's taken.
  1775. */
  1776. static int mwl8k_fw_lock(struct ieee80211_hw *hw)
  1777. {
  1778. struct mwl8k_priv *priv = hw->priv;
  1779. if (priv->fw_mutex_owner != current) {
  1780. int rc;
  1781. mutex_lock(&priv->fw_mutex);
  1782. ieee80211_stop_queues(hw);
  1783. rc = mwl8k_tx_wait_empty(hw);
  1784. if (rc) {
  1785. if (!priv->hw_restart_in_progress)
  1786. ieee80211_wake_queues(hw);
  1787. mutex_unlock(&priv->fw_mutex);
  1788. return rc;
  1789. }
  1790. priv->fw_mutex_owner = current;
  1791. }
  1792. priv->fw_mutex_depth++;
  1793. return 0;
  1794. }
  1795. static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
  1796. {
  1797. struct mwl8k_priv *priv = hw->priv;
  1798. if (!--priv->fw_mutex_depth) {
  1799. if (!priv->hw_restart_in_progress)
  1800. ieee80211_wake_queues(hw);
  1801. priv->fw_mutex_owner = NULL;
  1802. mutex_unlock(&priv->fw_mutex);
  1803. }
  1804. }
  1805. /*
  1806. * Command processing.
  1807. */
  1808. /* Timeout firmware commands after 10s */
  1809. #define MWL8K_CMD_TIMEOUT_MS 10000
  1810. static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
  1811. {
  1812. DECLARE_COMPLETION_ONSTACK(cmd_wait);
  1813. struct mwl8k_priv *priv = hw->priv;
  1814. void __iomem *regs = priv->regs;
  1815. dma_addr_t dma_addr;
  1816. unsigned int dma_size;
  1817. int rc;
  1818. unsigned long timeout = 0;
  1819. u8 buf[32];
  1820. cmd->result = (__force __le16) 0xffff;
  1821. dma_size = le16_to_cpu(cmd->length);
  1822. dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
  1823. PCI_DMA_BIDIRECTIONAL);
  1824. if (pci_dma_mapping_error(priv->pdev, dma_addr))
  1825. return -ENOMEM;
  1826. rc = mwl8k_fw_lock(hw);
  1827. if (rc) {
  1828. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1829. PCI_DMA_BIDIRECTIONAL);
  1830. return rc;
  1831. }
  1832. priv->hostcmd_wait = &cmd_wait;
  1833. iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
  1834. iowrite32(MWL8K_H2A_INT_DOORBELL,
  1835. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1836. iowrite32(MWL8K_H2A_INT_DUMMY,
  1837. regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
  1838. timeout = wait_for_completion_timeout(&cmd_wait,
  1839. msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
  1840. priv->hostcmd_wait = NULL;
  1841. mwl8k_fw_unlock(hw);
  1842. pci_unmap_single(priv->pdev, dma_addr, dma_size,
  1843. PCI_DMA_BIDIRECTIONAL);
  1844. if (!timeout) {
  1845. wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
  1846. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1847. MWL8K_CMD_TIMEOUT_MS);
  1848. rc = -ETIMEDOUT;
  1849. } else {
  1850. int ms;
  1851. ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
  1852. rc = cmd->result ? -EINVAL : 0;
  1853. if (rc)
  1854. wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
  1855. mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
  1856. le16_to_cpu(cmd->result));
  1857. else if (ms > 2000)
  1858. wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
  1859. mwl8k_cmd_name(cmd->code,
  1860. buf, sizeof(buf)),
  1861. ms);
  1862. }
  1863. return rc;
  1864. }
  1865. static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
  1866. struct ieee80211_vif *vif,
  1867. struct mwl8k_cmd_pkt *cmd)
  1868. {
  1869. if (vif != NULL)
  1870. cmd->macid = MWL8K_VIF(vif)->macid;
  1871. return mwl8k_post_cmd(hw, cmd);
  1872. }
  1873. /*
  1874. * Setup code shared between STA and AP firmware images.
  1875. */
  1876. static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
  1877. {
  1878. struct mwl8k_priv *priv = hw->priv;
  1879. BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
  1880. memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
  1881. BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
  1882. memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
  1883. priv->band_24.band = IEEE80211_BAND_2GHZ;
  1884. priv->band_24.channels = priv->channels_24;
  1885. priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
  1886. priv->band_24.bitrates = priv->rates_24;
  1887. priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
  1888. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
  1889. }
  1890. static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
  1891. {
  1892. struct mwl8k_priv *priv = hw->priv;
  1893. BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
  1894. memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
  1895. BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
  1896. memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
  1897. priv->band_50.band = IEEE80211_BAND_5GHZ;
  1898. priv->band_50.channels = priv->channels_50;
  1899. priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
  1900. priv->band_50.bitrates = priv->rates_50;
  1901. priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
  1902. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
  1903. }
  1904. /*
  1905. * CMD_GET_HW_SPEC (STA version).
  1906. */
  1907. struct mwl8k_cmd_get_hw_spec_sta {
  1908. struct mwl8k_cmd_pkt header;
  1909. __u8 hw_rev;
  1910. __u8 host_interface;
  1911. __le16 num_mcaddrs;
  1912. __u8 perm_addr[ETH_ALEN];
  1913. __le16 region_code;
  1914. __le32 fw_rev;
  1915. __le32 ps_cookie;
  1916. __le32 caps;
  1917. __u8 mcs_bitmap[16];
  1918. __le32 rx_queue_ptr;
  1919. __le32 num_tx_queues;
  1920. __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
  1921. __le32 caps2;
  1922. __le32 num_tx_desc_per_queue;
  1923. __le32 total_rxd;
  1924. } __packed;
  1925. #define MWL8K_CAP_MAX_AMSDU 0x20000000
  1926. #define MWL8K_CAP_GREENFIELD 0x08000000
  1927. #define MWL8K_CAP_AMPDU 0x04000000
  1928. #define MWL8K_CAP_RX_STBC 0x01000000
  1929. #define MWL8K_CAP_TX_STBC 0x00800000
  1930. #define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
  1931. #define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
  1932. #define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
  1933. #define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
  1934. #define MWL8K_CAP_DELAY_BA 0x00003000
  1935. #define MWL8K_CAP_MIMO 0x00000200
  1936. #define MWL8K_CAP_40MHZ 0x00000100
  1937. #define MWL8K_CAP_BAND_MASK 0x00000007
  1938. #define MWL8K_CAP_5GHZ 0x00000004
  1939. #define MWL8K_CAP_2GHZ4 0x00000001
  1940. static void
  1941. mwl8k_set_ht_caps(struct ieee80211_hw *hw,
  1942. struct ieee80211_supported_band *band, u32 cap)
  1943. {
  1944. int rx_streams;
  1945. int tx_streams;
  1946. band->ht_cap.ht_supported = 1;
  1947. if (cap & MWL8K_CAP_MAX_AMSDU)
  1948. band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  1949. if (cap & MWL8K_CAP_GREENFIELD)
  1950. band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
  1951. if (cap & MWL8K_CAP_AMPDU) {
  1952. hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  1953. band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  1954. band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
  1955. }
  1956. if (cap & MWL8K_CAP_RX_STBC)
  1957. band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
  1958. if (cap & MWL8K_CAP_TX_STBC)
  1959. band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  1960. if (cap & MWL8K_CAP_SHORTGI_40MHZ)
  1961. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  1962. if (cap & MWL8K_CAP_SHORTGI_20MHZ)
  1963. band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  1964. if (cap & MWL8K_CAP_DELAY_BA)
  1965. band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
  1966. if (cap & MWL8K_CAP_40MHZ)
  1967. band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  1968. rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
  1969. tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
  1970. band->ht_cap.mcs.rx_mask[0] = 0xff;
  1971. if (rx_streams >= 2)
  1972. band->ht_cap.mcs.rx_mask[1] = 0xff;
  1973. if (rx_streams >= 3)
  1974. band->ht_cap.mcs.rx_mask[2] = 0xff;
  1975. band->ht_cap.mcs.rx_mask[4] = 0x01;
  1976. band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1977. if (rx_streams != tx_streams) {
  1978. band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  1979. band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
  1980. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1981. }
  1982. }
  1983. static void
  1984. mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
  1985. {
  1986. struct mwl8k_priv *priv = hw->priv;
  1987. if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
  1988. mwl8k_setup_2ghz_band(hw);
  1989. if (caps & MWL8K_CAP_MIMO)
  1990. mwl8k_set_ht_caps(hw, &priv->band_24, caps);
  1991. }
  1992. if (caps & MWL8K_CAP_5GHZ) {
  1993. mwl8k_setup_5ghz_band(hw);
  1994. if (caps & MWL8K_CAP_MIMO)
  1995. mwl8k_set_ht_caps(hw, &priv->band_50, caps);
  1996. }
  1997. }
  1998. static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
  1999. {
  2000. struct mwl8k_priv *priv = hw->priv;
  2001. struct mwl8k_cmd_get_hw_spec_sta *cmd;
  2002. int rc;
  2003. int i;
  2004. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2005. if (cmd == NULL)
  2006. return -ENOMEM;
  2007. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2008. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2009. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2010. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2011. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2012. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2013. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  2014. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
  2015. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2016. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2017. rc = mwl8k_post_cmd(hw, &cmd->header);
  2018. if (!rc) {
  2019. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2020. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2021. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2022. priv->hw_rev = cmd->hw_rev;
  2023. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2024. priv->ap_macids_supported = 0x00000000;
  2025. priv->sta_macids_supported = 0x00000001;
  2026. }
  2027. kfree(cmd);
  2028. return rc;
  2029. }
  2030. /*
  2031. * CMD_GET_HW_SPEC (AP version).
  2032. */
  2033. struct mwl8k_cmd_get_hw_spec_ap {
  2034. struct mwl8k_cmd_pkt header;
  2035. __u8 hw_rev;
  2036. __u8 host_interface;
  2037. __le16 num_wcb;
  2038. __le16 num_mcaddrs;
  2039. __u8 perm_addr[ETH_ALEN];
  2040. __le16 region_code;
  2041. __le16 num_antenna;
  2042. __le32 fw_rev;
  2043. __le32 wcbbase0;
  2044. __le32 rxwrptr;
  2045. __le32 rxrdptr;
  2046. __le32 ps_cookie;
  2047. __le32 wcbbase1;
  2048. __le32 wcbbase2;
  2049. __le32 wcbbase3;
  2050. __le32 fw_api_version;
  2051. __le32 caps;
  2052. __le32 num_of_ampdu_queues;
  2053. __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
  2054. } __packed;
  2055. static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
  2056. {
  2057. struct mwl8k_priv *priv = hw->priv;
  2058. struct mwl8k_cmd_get_hw_spec_ap *cmd;
  2059. int rc, i;
  2060. u32 api_version;
  2061. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2062. if (cmd == NULL)
  2063. return -ENOMEM;
  2064. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
  2065. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2066. memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
  2067. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2068. rc = mwl8k_post_cmd(hw, &cmd->header);
  2069. if (!rc) {
  2070. int off;
  2071. api_version = le32_to_cpu(cmd->fw_api_version);
  2072. if (priv->device_info->fw_api_ap != api_version) {
  2073. printk(KERN_ERR "%s: Unsupported fw API version for %s."
  2074. " Expected %d got %d.\n", MWL8K_NAME,
  2075. priv->device_info->part_name,
  2076. priv->device_info->fw_api_ap,
  2077. api_version);
  2078. rc = -EINVAL;
  2079. goto done;
  2080. }
  2081. SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
  2082. priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
  2083. priv->fw_rev = le32_to_cpu(cmd->fw_rev);
  2084. priv->hw_rev = cmd->hw_rev;
  2085. mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
  2086. priv->ap_macids_supported = 0x000000ff;
  2087. priv->sta_macids_supported = 0x00000000;
  2088. priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
  2089. if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
  2090. wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
  2091. " but we only support %d.\n",
  2092. priv->num_ampdu_queues,
  2093. MWL8K_MAX_AMPDU_QUEUES);
  2094. priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
  2095. }
  2096. off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
  2097. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2098. off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
  2099. iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
  2100. priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
  2101. priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
  2102. priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
  2103. priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
  2104. for (i = 0; i < priv->num_ampdu_queues; i++)
  2105. priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
  2106. le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
  2107. }
  2108. done:
  2109. kfree(cmd);
  2110. return rc;
  2111. }
  2112. /*
  2113. * CMD_SET_HW_SPEC.
  2114. */
  2115. struct mwl8k_cmd_set_hw_spec {
  2116. struct mwl8k_cmd_pkt header;
  2117. __u8 hw_rev;
  2118. __u8 host_interface;
  2119. __le16 num_mcaddrs;
  2120. __u8 perm_addr[ETH_ALEN];
  2121. __le16 region_code;
  2122. __le32 fw_rev;
  2123. __le32 ps_cookie;
  2124. __le32 caps;
  2125. __le32 rx_queue_ptr;
  2126. __le32 num_tx_queues;
  2127. __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
  2128. __le32 flags;
  2129. __le32 num_tx_desc_per_queue;
  2130. __le32 total_rxd;
  2131. } __packed;
  2132. /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
  2133. * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
  2134. * the packets that are queued for more than 500ms, will be dropped in the
  2135. * hardware. This helps minimizing the issues caused due to head-of-line
  2136. * blocking where a slow client can hog the bandwidth and affect traffic to a
  2137. * faster client.
  2138. */
  2139. #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
  2140. #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
  2141. #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
  2142. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
  2143. #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
  2144. static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
  2145. {
  2146. struct mwl8k_priv *priv = hw->priv;
  2147. struct mwl8k_cmd_set_hw_spec *cmd;
  2148. int rc;
  2149. int i;
  2150. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2151. if (cmd == NULL)
  2152. return -ENOMEM;
  2153. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
  2154. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2155. cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
  2156. cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
  2157. cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
  2158. /*
  2159. * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
  2160. * that order. Firmware has Q3 as highest priority and Q0 as lowest
  2161. * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
  2162. * priority is interpreted the right way in firmware.
  2163. */
  2164. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  2165. int j = mwl8k_tx_queues(priv) - 1 - i;
  2166. cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
  2167. }
  2168. cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
  2169. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
  2170. MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
  2171. MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
  2172. MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
  2173. cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
  2174. cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
  2175. rc = mwl8k_post_cmd(hw, &cmd->header);
  2176. kfree(cmd);
  2177. return rc;
  2178. }
  2179. /*
  2180. * CMD_MAC_MULTICAST_ADR.
  2181. */
  2182. struct mwl8k_cmd_mac_multicast_adr {
  2183. struct mwl8k_cmd_pkt header;
  2184. __le16 action;
  2185. __le16 numaddr;
  2186. __u8 addr[0][ETH_ALEN];
  2187. };
  2188. #define MWL8K_ENABLE_RX_DIRECTED 0x0001
  2189. #define MWL8K_ENABLE_RX_MULTICAST 0x0002
  2190. #define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
  2191. #define MWL8K_ENABLE_RX_BROADCAST 0x0008
  2192. static struct mwl8k_cmd_pkt *
  2193. __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
  2194. struct netdev_hw_addr_list *mc_list)
  2195. {
  2196. struct mwl8k_priv *priv = hw->priv;
  2197. struct mwl8k_cmd_mac_multicast_adr *cmd;
  2198. int size;
  2199. int mc_count = 0;
  2200. if (mc_list)
  2201. mc_count = netdev_hw_addr_list_count(mc_list);
  2202. if (allmulti || mc_count > priv->num_mcaddrs) {
  2203. allmulti = 1;
  2204. mc_count = 0;
  2205. }
  2206. size = sizeof(*cmd) + mc_count * ETH_ALEN;
  2207. cmd = kzalloc(size, GFP_ATOMIC);
  2208. if (cmd == NULL)
  2209. return NULL;
  2210. cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
  2211. cmd->header.length = cpu_to_le16(size);
  2212. cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
  2213. MWL8K_ENABLE_RX_BROADCAST);
  2214. if (allmulti) {
  2215. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
  2216. } else if (mc_count) {
  2217. struct netdev_hw_addr *ha;
  2218. int i = 0;
  2219. cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
  2220. cmd->numaddr = cpu_to_le16(mc_count);
  2221. netdev_hw_addr_list_for_each(ha, mc_list) {
  2222. memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
  2223. }
  2224. }
  2225. return &cmd->header;
  2226. }
  2227. /*
  2228. * CMD_GET_STAT.
  2229. */
  2230. struct mwl8k_cmd_get_stat {
  2231. struct mwl8k_cmd_pkt header;
  2232. __le32 stats[64];
  2233. } __packed;
  2234. #define MWL8K_STAT_ACK_FAILURE 9
  2235. #define MWL8K_STAT_RTS_FAILURE 12
  2236. #define MWL8K_STAT_FCS_ERROR 24
  2237. #define MWL8K_STAT_RTS_SUCCESS 11
  2238. static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
  2239. struct ieee80211_low_level_stats *stats)
  2240. {
  2241. struct mwl8k_cmd_get_stat *cmd;
  2242. int rc;
  2243. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2244. if (cmd == NULL)
  2245. return -ENOMEM;
  2246. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
  2247. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2248. rc = mwl8k_post_cmd(hw, &cmd->header);
  2249. if (!rc) {
  2250. stats->dot11ACKFailureCount =
  2251. le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
  2252. stats->dot11RTSFailureCount =
  2253. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
  2254. stats->dot11FCSErrorCount =
  2255. le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
  2256. stats->dot11RTSSuccessCount =
  2257. le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
  2258. }
  2259. kfree(cmd);
  2260. return rc;
  2261. }
  2262. /*
  2263. * CMD_RADIO_CONTROL.
  2264. */
  2265. struct mwl8k_cmd_radio_control {
  2266. struct mwl8k_cmd_pkt header;
  2267. __le16 action;
  2268. __le16 control;
  2269. __le16 radio_on;
  2270. } __packed;
  2271. static int
  2272. mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
  2273. {
  2274. struct mwl8k_priv *priv = hw->priv;
  2275. struct mwl8k_cmd_radio_control *cmd;
  2276. int rc;
  2277. if (enable == priv->radio_on && !force)
  2278. return 0;
  2279. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2280. if (cmd == NULL)
  2281. return -ENOMEM;
  2282. cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
  2283. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2284. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2285. cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
  2286. cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
  2287. rc = mwl8k_post_cmd(hw, &cmd->header);
  2288. kfree(cmd);
  2289. if (!rc)
  2290. priv->radio_on = enable;
  2291. return rc;
  2292. }
  2293. static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
  2294. {
  2295. return mwl8k_cmd_radio_control(hw, 0, 0);
  2296. }
  2297. static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
  2298. {
  2299. return mwl8k_cmd_radio_control(hw, 1, 0);
  2300. }
  2301. static int
  2302. mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
  2303. {
  2304. struct mwl8k_priv *priv = hw->priv;
  2305. priv->radio_short_preamble = short_preamble;
  2306. return mwl8k_cmd_radio_control(hw, 1, 1);
  2307. }
  2308. /*
  2309. * CMD_RF_TX_POWER.
  2310. */
  2311. #define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
  2312. struct mwl8k_cmd_rf_tx_power {
  2313. struct mwl8k_cmd_pkt header;
  2314. __le16 action;
  2315. __le16 support_level;
  2316. __le16 current_level;
  2317. __le16 reserved;
  2318. __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
  2319. } __packed;
  2320. static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
  2321. {
  2322. struct mwl8k_cmd_rf_tx_power *cmd;
  2323. int rc;
  2324. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2325. if (cmd == NULL)
  2326. return -ENOMEM;
  2327. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
  2328. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2329. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2330. cmd->support_level = cpu_to_le16(dBm);
  2331. rc = mwl8k_post_cmd(hw, &cmd->header);
  2332. kfree(cmd);
  2333. return rc;
  2334. }
  2335. /*
  2336. * CMD_TX_POWER.
  2337. */
  2338. #define MWL8K_TX_POWER_LEVEL_TOTAL 12
  2339. struct mwl8k_cmd_tx_power {
  2340. struct mwl8k_cmd_pkt header;
  2341. __le16 action;
  2342. __le16 band;
  2343. __le16 channel;
  2344. __le16 bw;
  2345. __le16 sub_ch;
  2346. __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
  2347. } __packed;
  2348. static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
  2349. struct ieee80211_conf *conf,
  2350. unsigned short pwr)
  2351. {
  2352. struct ieee80211_channel *channel = conf->channel;
  2353. struct mwl8k_cmd_tx_power *cmd;
  2354. int rc;
  2355. int i;
  2356. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2357. if (cmd == NULL)
  2358. return -ENOMEM;
  2359. cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
  2360. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2361. cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
  2362. if (channel->band == IEEE80211_BAND_2GHZ)
  2363. cmd->band = cpu_to_le16(0x1);
  2364. else if (channel->band == IEEE80211_BAND_5GHZ)
  2365. cmd->band = cpu_to_le16(0x4);
  2366. cmd->channel = cpu_to_le16(channel->hw_value);
  2367. if (conf->channel_type == NL80211_CHAN_NO_HT ||
  2368. conf->channel_type == NL80211_CHAN_HT20) {
  2369. cmd->bw = cpu_to_le16(0x2);
  2370. } else {
  2371. cmd->bw = cpu_to_le16(0x4);
  2372. if (conf->channel_type == NL80211_CHAN_HT40MINUS)
  2373. cmd->sub_ch = cpu_to_le16(0x3);
  2374. else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
  2375. cmd->sub_ch = cpu_to_le16(0x1);
  2376. }
  2377. for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
  2378. cmd->power_level_list[i] = cpu_to_le16(pwr);
  2379. rc = mwl8k_post_cmd(hw, &cmd->header);
  2380. kfree(cmd);
  2381. return rc;
  2382. }
  2383. /*
  2384. * CMD_RF_ANTENNA.
  2385. */
  2386. struct mwl8k_cmd_rf_antenna {
  2387. struct mwl8k_cmd_pkt header;
  2388. __le16 antenna;
  2389. __le16 mode;
  2390. } __packed;
  2391. #define MWL8K_RF_ANTENNA_RX 1
  2392. #define MWL8K_RF_ANTENNA_TX 2
  2393. static int
  2394. mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
  2395. {
  2396. struct mwl8k_cmd_rf_antenna *cmd;
  2397. int rc;
  2398. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2399. if (cmd == NULL)
  2400. return -ENOMEM;
  2401. cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
  2402. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2403. cmd->antenna = cpu_to_le16(antenna);
  2404. cmd->mode = cpu_to_le16(mask);
  2405. rc = mwl8k_post_cmd(hw, &cmd->header);
  2406. kfree(cmd);
  2407. return rc;
  2408. }
  2409. /*
  2410. * CMD_SET_BEACON.
  2411. */
  2412. struct mwl8k_cmd_set_beacon {
  2413. struct mwl8k_cmd_pkt header;
  2414. __le16 beacon_len;
  2415. __u8 beacon[0];
  2416. };
  2417. static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
  2418. struct ieee80211_vif *vif, u8 *beacon, int len)
  2419. {
  2420. struct mwl8k_cmd_set_beacon *cmd;
  2421. int rc;
  2422. cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
  2423. if (cmd == NULL)
  2424. return -ENOMEM;
  2425. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
  2426. cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
  2427. cmd->beacon_len = cpu_to_le16(len);
  2428. memcpy(cmd->beacon, beacon, len);
  2429. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  2430. kfree(cmd);
  2431. return rc;
  2432. }
  2433. /*
  2434. * CMD_SET_PRE_SCAN.
  2435. */
  2436. struct mwl8k_cmd_set_pre_scan {
  2437. struct mwl8k_cmd_pkt header;
  2438. } __packed;
  2439. static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
  2440. {
  2441. struct mwl8k_cmd_set_pre_scan *cmd;
  2442. int rc;
  2443. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2444. if (cmd == NULL)
  2445. return -ENOMEM;
  2446. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
  2447. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2448. rc = mwl8k_post_cmd(hw, &cmd->header);
  2449. kfree(cmd);
  2450. return rc;
  2451. }
  2452. /*
  2453. * CMD_SET_POST_SCAN.
  2454. */
  2455. struct mwl8k_cmd_set_post_scan {
  2456. struct mwl8k_cmd_pkt header;
  2457. __le32 isibss;
  2458. __u8 bssid[ETH_ALEN];
  2459. } __packed;
  2460. static int
  2461. mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
  2462. {
  2463. struct mwl8k_cmd_set_post_scan *cmd;
  2464. int rc;
  2465. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2466. if (cmd == NULL)
  2467. return -ENOMEM;
  2468. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
  2469. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2470. cmd->isibss = 0;
  2471. memcpy(cmd->bssid, mac, ETH_ALEN);
  2472. rc = mwl8k_post_cmd(hw, &cmd->header);
  2473. kfree(cmd);
  2474. return rc;
  2475. }
  2476. /*
  2477. * CMD_SET_RF_CHANNEL.
  2478. */
  2479. struct mwl8k_cmd_set_rf_channel {
  2480. struct mwl8k_cmd_pkt header;
  2481. __le16 action;
  2482. __u8 current_channel;
  2483. __le32 channel_flags;
  2484. } __packed;
  2485. static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
  2486. struct ieee80211_conf *conf)
  2487. {
  2488. struct ieee80211_channel *channel = conf->channel;
  2489. struct mwl8k_cmd_set_rf_channel *cmd;
  2490. int rc;
  2491. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2492. if (cmd == NULL)
  2493. return -ENOMEM;
  2494. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
  2495. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2496. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2497. cmd->current_channel = channel->hw_value;
  2498. if (channel->band == IEEE80211_BAND_2GHZ)
  2499. cmd->channel_flags |= cpu_to_le32(0x00000001);
  2500. else if (channel->band == IEEE80211_BAND_5GHZ)
  2501. cmd->channel_flags |= cpu_to_le32(0x00000004);
  2502. if (conf->channel_type == NL80211_CHAN_NO_HT ||
  2503. conf->channel_type == NL80211_CHAN_HT20)
  2504. cmd->channel_flags |= cpu_to_le32(0x00000080);
  2505. else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
  2506. cmd->channel_flags |= cpu_to_le32(0x000001900);
  2507. else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
  2508. cmd->channel_flags |= cpu_to_le32(0x000000900);
  2509. rc = mwl8k_post_cmd(hw, &cmd->header);
  2510. kfree(cmd);
  2511. return rc;
  2512. }
  2513. /*
  2514. * CMD_SET_AID.
  2515. */
  2516. #define MWL8K_FRAME_PROT_DISABLED 0x00
  2517. #define MWL8K_FRAME_PROT_11G 0x07
  2518. #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
  2519. #define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
  2520. struct mwl8k_cmd_update_set_aid {
  2521. struct mwl8k_cmd_pkt header;
  2522. __le16 aid;
  2523. /* AP's MAC address (BSSID) */
  2524. __u8 bssid[ETH_ALEN];
  2525. __le16 protection_mode;
  2526. __u8 supp_rates[14];
  2527. } __packed;
  2528. static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
  2529. {
  2530. int i;
  2531. int j;
  2532. /*
  2533. * Clear nonstandard rates 4 and 13.
  2534. */
  2535. mask &= 0x1fef;
  2536. for (i = 0, j = 0; i < 14; i++) {
  2537. if (mask & (1 << i))
  2538. rates[j++] = mwl8k_rates_24[i].hw_value;
  2539. }
  2540. }
  2541. static int
  2542. mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
  2543. struct ieee80211_vif *vif, u32 legacy_rate_mask)
  2544. {
  2545. struct mwl8k_cmd_update_set_aid *cmd;
  2546. u16 prot_mode;
  2547. int rc;
  2548. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2549. if (cmd == NULL)
  2550. return -ENOMEM;
  2551. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
  2552. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2553. cmd->aid = cpu_to_le16(vif->bss_conf.aid);
  2554. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  2555. if (vif->bss_conf.use_cts_prot) {
  2556. prot_mode = MWL8K_FRAME_PROT_11G;
  2557. } else {
  2558. switch (vif->bss_conf.ht_operation_mode &
  2559. IEEE80211_HT_OP_MODE_PROTECTION) {
  2560. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  2561. prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
  2562. break;
  2563. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  2564. prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
  2565. break;
  2566. default:
  2567. prot_mode = MWL8K_FRAME_PROT_DISABLED;
  2568. break;
  2569. }
  2570. }
  2571. cmd->protection_mode = cpu_to_le16(prot_mode);
  2572. legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
  2573. rc = mwl8k_post_cmd(hw, &cmd->header);
  2574. kfree(cmd);
  2575. return rc;
  2576. }
  2577. /*
  2578. * CMD_SET_RATE.
  2579. */
  2580. struct mwl8k_cmd_set_rate {
  2581. struct mwl8k_cmd_pkt header;
  2582. __u8 legacy_rates[14];
  2583. /* Bitmap for supported MCS codes. */
  2584. __u8 mcs_set[16];
  2585. __u8 reserved[16];
  2586. } __packed;
  2587. static int
  2588. mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2589. u32 legacy_rate_mask, u8 *mcs_rates)
  2590. {
  2591. struct mwl8k_cmd_set_rate *cmd;
  2592. int rc;
  2593. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2594. if (cmd == NULL)
  2595. return -ENOMEM;
  2596. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
  2597. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2598. legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
  2599. memcpy(cmd->mcs_set, mcs_rates, 16);
  2600. rc = mwl8k_post_cmd(hw, &cmd->header);
  2601. kfree(cmd);
  2602. return rc;
  2603. }
  2604. /*
  2605. * CMD_FINALIZE_JOIN.
  2606. */
  2607. #define MWL8K_FJ_BEACON_MAXLEN 128
  2608. struct mwl8k_cmd_finalize_join {
  2609. struct mwl8k_cmd_pkt header;
  2610. __le32 sleep_interval; /* Number of beacon periods to sleep */
  2611. __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
  2612. } __packed;
  2613. static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
  2614. int framelen, int dtim)
  2615. {
  2616. struct mwl8k_cmd_finalize_join *cmd;
  2617. struct ieee80211_mgmt *payload = frame;
  2618. int payload_len;
  2619. int rc;
  2620. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2621. if (cmd == NULL)
  2622. return -ENOMEM;
  2623. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
  2624. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2625. cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
  2626. payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
  2627. if (payload_len < 0)
  2628. payload_len = 0;
  2629. else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
  2630. payload_len = MWL8K_FJ_BEACON_MAXLEN;
  2631. memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
  2632. rc = mwl8k_post_cmd(hw, &cmd->header);
  2633. kfree(cmd);
  2634. return rc;
  2635. }
  2636. /*
  2637. * CMD_SET_RTS_THRESHOLD.
  2638. */
  2639. struct mwl8k_cmd_set_rts_threshold {
  2640. struct mwl8k_cmd_pkt header;
  2641. __le16 action;
  2642. __le16 threshold;
  2643. } __packed;
  2644. static int
  2645. mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
  2646. {
  2647. struct mwl8k_cmd_set_rts_threshold *cmd;
  2648. int rc;
  2649. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2650. if (cmd == NULL)
  2651. return -ENOMEM;
  2652. cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
  2653. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2654. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2655. cmd->threshold = cpu_to_le16(rts_thresh);
  2656. rc = mwl8k_post_cmd(hw, &cmd->header);
  2657. kfree(cmd);
  2658. return rc;
  2659. }
  2660. /*
  2661. * CMD_SET_SLOT.
  2662. */
  2663. struct mwl8k_cmd_set_slot {
  2664. struct mwl8k_cmd_pkt header;
  2665. __le16 action;
  2666. __u8 short_slot;
  2667. } __packed;
  2668. static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
  2669. {
  2670. struct mwl8k_cmd_set_slot *cmd;
  2671. int rc;
  2672. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2673. if (cmd == NULL)
  2674. return -ENOMEM;
  2675. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
  2676. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2677. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2678. cmd->short_slot = short_slot_time;
  2679. rc = mwl8k_post_cmd(hw, &cmd->header);
  2680. kfree(cmd);
  2681. return rc;
  2682. }
  2683. /*
  2684. * CMD_SET_EDCA_PARAMS.
  2685. */
  2686. struct mwl8k_cmd_set_edca_params {
  2687. struct mwl8k_cmd_pkt header;
  2688. /* See MWL8K_SET_EDCA_XXX below */
  2689. __le16 action;
  2690. /* TX opportunity in units of 32 us */
  2691. __le16 txop;
  2692. union {
  2693. struct {
  2694. /* Log exponent of max contention period: 0...15 */
  2695. __le32 log_cw_max;
  2696. /* Log exponent of min contention period: 0...15 */
  2697. __le32 log_cw_min;
  2698. /* Adaptive interframe spacing in units of 32us */
  2699. __u8 aifs;
  2700. /* TX queue to configure */
  2701. __u8 txq;
  2702. } ap;
  2703. struct {
  2704. /* Log exponent of max contention period: 0...15 */
  2705. __u8 log_cw_max;
  2706. /* Log exponent of min contention period: 0...15 */
  2707. __u8 log_cw_min;
  2708. /* Adaptive interframe spacing in units of 32us */
  2709. __u8 aifs;
  2710. /* TX queue to configure */
  2711. __u8 txq;
  2712. } sta;
  2713. };
  2714. } __packed;
  2715. #define MWL8K_SET_EDCA_CW 0x01
  2716. #define MWL8K_SET_EDCA_TXOP 0x02
  2717. #define MWL8K_SET_EDCA_AIFS 0x04
  2718. #define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
  2719. MWL8K_SET_EDCA_TXOP | \
  2720. MWL8K_SET_EDCA_AIFS)
  2721. static int
  2722. mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
  2723. __u16 cw_min, __u16 cw_max,
  2724. __u8 aifs, __u16 txop)
  2725. {
  2726. struct mwl8k_priv *priv = hw->priv;
  2727. struct mwl8k_cmd_set_edca_params *cmd;
  2728. int rc;
  2729. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2730. if (cmd == NULL)
  2731. return -ENOMEM;
  2732. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
  2733. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2734. cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
  2735. cmd->txop = cpu_to_le16(txop);
  2736. if (priv->ap_fw) {
  2737. cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
  2738. cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
  2739. cmd->ap.aifs = aifs;
  2740. cmd->ap.txq = qnum;
  2741. } else {
  2742. cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
  2743. cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
  2744. cmd->sta.aifs = aifs;
  2745. cmd->sta.txq = qnum;
  2746. }
  2747. rc = mwl8k_post_cmd(hw, &cmd->header);
  2748. kfree(cmd);
  2749. return rc;
  2750. }
  2751. /*
  2752. * CMD_SET_WMM_MODE.
  2753. */
  2754. struct mwl8k_cmd_set_wmm_mode {
  2755. struct mwl8k_cmd_pkt header;
  2756. __le16 action;
  2757. } __packed;
  2758. static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
  2759. {
  2760. struct mwl8k_priv *priv = hw->priv;
  2761. struct mwl8k_cmd_set_wmm_mode *cmd;
  2762. int rc;
  2763. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2764. if (cmd == NULL)
  2765. return -ENOMEM;
  2766. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
  2767. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2768. cmd->action = cpu_to_le16(!!enable);
  2769. rc = mwl8k_post_cmd(hw, &cmd->header);
  2770. kfree(cmd);
  2771. if (!rc)
  2772. priv->wmm_enabled = enable;
  2773. return rc;
  2774. }
  2775. /*
  2776. * CMD_MIMO_CONFIG.
  2777. */
  2778. struct mwl8k_cmd_mimo_config {
  2779. struct mwl8k_cmd_pkt header;
  2780. __le32 action;
  2781. __u8 rx_antenna_map;
  2782. __u8 tx_antenna_map;
  2783. } __packed;
  2784. static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
  2785. {
  2786. struct mwl8k_cmd_mimo_config *cmd;
  2787. int rc;
  2788. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2789. if (cmd == NULL)
  2790. return -ENOMEM;
  2791. cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
  2792. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2793. cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
  2794. cmd->rx_antenna_map = rx;
  2795. cmd->tx_antenna_map = tx;
  2796. rc = mwl8k_post_cmd(hw, &cmd->header);
  2797. kfree(cmd);
  2798. return rc;
  2799. }
  2800. /*
  2801. * CMD_USE_FIXED_RATE (STA version).
  2802. */
  2803. struct mwl8k_cmd_use_fixed_rate_sta {
  2804. struct mwl8k_cmd_pkt header;
  2805. __le32 action;
  2806. __le32 allow_rate_drop;
  2807. __le32 num_rates;
  2808. struct {
  2809. __le32 is_ht_rate;
  2810. __le32 enable_retry;
  2811. __le32 rate;
  2812. __le32 retry_count;
  2813. } rate_entry[8];
  2814. __le32 rate_type;
  2815. __le32 reserved1;
  2816. __le32 reserved2;
  2817. } __packed;
  2818. #define MWL8K_USE_AUTO_RATE 0x0002
  2819. #define MWL8K_UCAST_RATE 0
  2820. static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
  2821. {
  2822. struct mwl8k_cmd_use_fixed_rate_sta *cmd;
  2823. int rc;
  2824. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2825. if (cmd == NULL)
  2826. return -ENOMEM;
  2827. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  2828. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2829. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  2830. cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
  2831. rc = mwl8k_post_cmd(hw, &cmd->header);
  2832. kfree(cmd);
  2833. return rc;
  2834. }
  2835. /*
  2836. * CMD_USE_FIXED_RATE (AP version).
  2837. */
  2838. struct mwl8k_cmd_use_fixed_rate_ap {
  2839. struct mwl8k_cmd_pkt header;
  2840. __le32 action;
  2841. __le32 allow_rate_drop;
  2842. __le32 num_rates;
  2843. struct mwl8k_rate_entry_ap {
  2844. __le32 is_ht_rate;
  2845. __le32 enable_retry;
  2846. __le32 rate;
  2847. __le32 retry_count;
  2848. } rate_entry[4];
  2849. u8 multicast_rate;
  2850. u8 multicast_rate_type;
  2851. u8 management_rate;
  2852. } __packed;
  2853. static int
  2854. mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
  2855. {
  2856. struct mwl8k_cmd_use_fixed_rate_ap *cmd;
  2857. int rc;
  2858. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2859. if (cmd == NULL)
  2860. return -ENOMEM;
  2861. cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
  2862. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2863. cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
  2864. cmd->multicast_rate = mcast;
  2865. cmd->management_rate = mgmt;
  2866. rc = mwl8k_post_cmd(hw, &cmd->header);
  2867. kfree(cmd);
  2868. return rc;
  2869. }
  2870. /*
  2871. * CMD_ENABLE_SNIFFER.
  2872. */
  2873. struct mwl8k_cmd_enable_sniffer {
  2874. struct mwl8k_cmd_pkt header;
  2875. __le32 action;
  2876. } __packed;
  2877. static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
  2878. {
  2879. struct mwl8k_cmd_enable_sniffer *cmd;
  2880. int rc;
  2881. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2882. if (cmd == NULL)
  2883. return -ENOMEM;
  2884. cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
  2885. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2886. cmd->action = cpu_to_le32(!!enable);
  2887. rc = mwl8k_post_cmd(hw, &cmd->header);
  2888. kfree(cmd);
  2889. return rc;
  2890. }
  2891. struct mwl8k_cmd_update_mac_addr {
  2892. struct mwl8k_cmd_pkt header;
  2893. union {
  2894. struct {
  2895. __le16 mac_type;
  2896. __u8 mac_addr[ETH_ALEN];
  2897. } mbss;
  2898. __u8 mac_addr[ETH_ALEN];
  2899. };
  2900. } __packed;
  2901. #define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
  2902. #define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
  2903. #define MWL8K_MAC_TYPE_PRIMARY_AP 2
  2904. #define MWL8K_MAC_TYPE_SECONDARY_AP 3
  2905. static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
  2906. struct ieee80211_vif *vif, u8 *mac, bool set)
  2907. {
  2908. struct mwl8k_priv *priv = hw->priv;
  2909. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  2910. struct mwl8k_cmd_update_mac_addr *cmd;
  2911. int mac_type;
  2912. int rc;
  2913. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  2914. if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
  2915. if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
  2916. mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
  2917. else
  2918. mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
  2919. } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
  2920. if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
  2921. mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
  2922. else
  2923. mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
  2924. }
  2925. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2926. if (cmd == NULL)
  2927. return -ENOMEM;
  2928. if (set)
  2929. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
  2930. else
  2931. cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
  2932. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2933. if (priv->ap_fw) {
  2934. cmd->mbss.mac_type = cpu_to_le16(mac_type);
  2935. memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
  2936. } else {
  2937. memcpy(cmd->mac_addr, mac, ETH_ALEN);
  2938. }
  2939. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  2940. kfree(cmd);
  2941. return rc;
  2942. }
  2943. /*
  2944. * MWL8K_CMD_SET_MAC_ADDR.
  2945. */
  2946. static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
  2947. struct ieee80211_vif *vif, u8 *mac)
  2948. {
  2949. return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
  2950. }
  2951. /*
  2952. * MWL8K_CMD_DEL_MAC_ADDR.
  2953. */
  2954. static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
  2955. struct ieee80211_vif *vif, u8 *mac)
  2956. {
  2957. return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
  2958. }
  2959. /*
  2960. * CMD_SET_RATEADAPT_MODE.
  2961. */
  2962. struct mwl8k_cmd_set_rate_adapt_mode {
  2963. struct mwl8k_cmd_pkt header;
  2964. __le16 action;
  2965. __le16 mode;
  2966. } __packed;
  2967. static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
  2968. {
  2969. struct mwl8k_cmd_set_rate_adapt_mode *cmd;
  2970. int rc;
  2971. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2972. if (cmd == NULL)
  2973. return -ENOMEM;
  2974. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
  2975. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2976. cmd->action = cpu_to_le16(MWL8K_CMD_SET);
  2977. cmd->mode = cpu_to_le16(mode);
  2978. rc = mwl8k_post_cmd(hw, &cmd->header);
  2979. kfree(cmd);
  2980. return rc;
  2981. }
  2982. /*
  2983. * CMD_GET_WATCHDOG_BITMAP.
  2984. */
  2985. struct mwl8k_cmd_get_watchdog_bitmap {
  2986. struct mwl8k_cmd_pkt header;
  2987. u8 bitmap;
  2988. } __packed;
  2989. static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
  2990. {
  2991. struct mwl8k_cmd_get_watchdog_bitmap *cmd;
  2992. int rc;
  2993. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  2994. if (cmd == NULL)
  2995. return -ENOMEM;
  2996. cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
  2997. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  2998. rc = mwl8k_post_cmd(hw, &cmd->header);
  2999. if (!rc)
  3000. *bitmap = cmd->bitmap;
  3001. kfree(cmd);
  3002. return rc;
  3003. }
  3004. #define MWL8K_WMM_QUEUE_NUMBER 3
  3005. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3006. u8 idx);
  3007. static void mwl8k_watchdog_ba_events(struct work_struct *work)
  3008. {
  3009. int rc;
  3010. u8 bitmap = 0, stream_index;
  3011. struct mwl8k_ampdu_stream *streams;
  3012. struct mwl8k_priv *priv =
  3013. container_of(work, struct mwl8k_priv, watchdog_ba_handle);
  3014. struct ieee80211_hw *hw = priv->hw;
  3015. int i;
  3016. u32 status = 0;
  3017. mwl8k_fw_lock(hw);
  3018. rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
  3019. if (rc)
  3020. goto done;
  3021. spin_lock(&priv->stream_lock);
  3022. /* the bitmap is the hw queue number. Map it to the ampdu queue. */
  3023. for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
  3024. if (bitmap & (1 << i)) {
  3025. stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
  3026. TOTAL_HW_TX_QUEUES;
  3027. streams = &priv->ampdu[stream_index];
  3028. if (streams->state == AMPDU_STREAM_ACTIVE) {
  3029. ieee80211_stop_tx_ba_session(streams->sta,
  3030. streams->tid);
  3031. spin_unlock(&priv->stream_lock);
  3032. mwl8k_destroy_ba(hw, stream_index);
  3033. spin_lock(&priv->stream_lock);
  3034. }
  3035. }
  3036. }
  3037. spin_unlock(&priv->stream_lock);
  3038. done:
  3039. atomic_dec(&priv->watchdog_event_pending);
  3040. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3041. iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
  3042. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3043. mwl8k_fw_unlock(hw);
  3044. return;
  3045. }
  3046. /*
  3047. * CMD_BSS_START.
  3048. */
  3049. struct mwl8k_cmd_bss_start {
  3050. struct mwl8k_cmd_pkt header;
  3051. __le32 enable;
  3052. } __packed;
  3053. static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
  3054. struct ieee80211_vif *vif, int enable)
  3055. {
  3056. struct mwl8k_cmd_bss_start *cmd;
  3057. int rc;
  3058. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3059. if (cmd == NULL)
  3060. return -ENOMEM;
  3061. cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
  3062. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3063. cmd->enable = cpu_to_le32(enable);
  3064. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3065. kfree(cmd);
  3066. return rc;
  3067. }
  3068. /*
  3069. * CMD_BASTREAM.
  3070. */
  3071. /*
  3072. * UPSTREAM is tx direction
  3073. */
  3074. #define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
  3075. #define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
  3076. enum ba_stream_action_type {
  3077. MWL8K_BA_CREATE,
  3078. MWL8K_BA_UPDATE,
  3079. MWL8K_BA_DESTROY,
  3080. MWL8K_BA_FLUSH,
  3081. MWL8K_BA_CHECK,
  3082. };
  3083. struct mwl8k_create_ba_stream {
  3084. __le32 flags;
  3085. __le32 idle_thrs;
  3086. __le32 bar_thrs;
  3087. __le32 window_size;
  3088. u8 peer_mac_addr[6];
  3089. u8 dialog_token;
  3090. u8 tid;
  3091. u8 queue_id;
  3092. u8 param_info;
  3093. __le32 ba_context;
  3094. u8 reset_seq_no_flag;
  3095. __le16 curr_seq_no;
  3096. u8 sta_src_mac_addr[6];
  3097. } __packed;
  3098. struct mwl8k_destroy_ba_stream {
  3099. __le32 flags;
  3100. __le32 ba_context;
  3101. } __packed;
  3102. struct mwl8k_cmd_bastream {
  3103. struct mwl8k_cmd_pkt header;
  3104. __le32 action;
  3105. union {
  3106. struct mwl8k_create_ba_stream create_params;
  3107. struct mwl8k_destroy_ba_stream destroy_params;
  3108. };
  3109. } __packed;
  3110. static int
  3111. mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3112. struct ieee80211_vif *vif)
  3113. {
  3114. struct mwl8k_cmd_bastream *cmd;
  3115. int rc;
  3116. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3117. if (cmd == NULL)
  3118. return -ENOMEM;
  3119. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3120. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3121. cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
  3122. cmd->create_params.queue_id = stream->idx;
  3123. memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
  3124. ETH_ALEN);
  3125. cmd->create_params.tid = stream->tid;
  3126. cmd->create_params.flags =
  3127. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
  3128. cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3129. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3130. kfree(cmd);
  3131. return rc;
  3132. }
  3133. static int
  3134. mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
  3135. u8 buf_size, struct ieee80211_vif *vif)
  3136. {
  3137. struct mwl8k_cmd_bastream *cmd;
  3138. int rc;
  3139. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3140. if (cmd == NULL)
  3141. return -ENOMEM;
  3142. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3143. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3144. cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
  3145. cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
  3146. cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
  3147. cmd->create_params.queue_id = stream->idx;
  3148. memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
  3149. cmd->create_params.tid = stream->tid;
  3150. cmd->create_params.curr_seq_no = cpu_to_le16(0);
  3151. cmd->create_params.reset_seq_no_flag = 1;
  3152. cmd->create_params.param_info =
  3153. (stream->sta->ht_cap.ampdu_factor &
  3154. IEEE80211_HT_AMPDU_PARM_FACTOR) |
  3155. ((stream->sta->ht_cap.ampdu_density << 2) &
  3156. IEEE80211_HT_AMPDU_PARM_DENSITY);
  3157. cmd->create_params.flags =
  3158. cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
  3159. BASTREAM_FLAG_DIRECTION_UPSTREAM);
  3160. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3161. wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
  3162. stream->sta->addr, stream->tid);
  3163. kfree(cmd);
  3164. return rc;
  3165. }
  3166. static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
  3167. u8 idx)
  3168. {
  3169. struct mwl8k_cmd_bastream *cmd;
  3170. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3171. if (cmd == NULL)
  3172. return;
  3173. cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
  3174. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3175. cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
  3176. cmd->destroy_params.ba_context = cpu_to_le32(idx);
  3177. mwl8k_post_cmd(hw, &cmd->header);
  3178. wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
  3179. kfree(cmd);
  3180. }
  3181. /*
  3182. * CMD_SET_NEW_STN.
  3183. */
  3184. struct mwl8k_cmd_set_new_stn {
  3185. struct mwl8k_cmd_pkt header;
  3186. __le16 aid;
  3187. __u8 mac_addr[6];
  3188. __le16 stn_id;
  3189. __le16 action;
  3190. __le16 rsvd;
  3191. __le32 legacy_rates;
  3192. __u8 ht_rates[4];
  3193. __le16 cap_info;
  3194. __le16 ht_capabilities_info;
  3195. __u8 mac_ht_param_info;
  3196. __u8 rev;
  3197. __u8 control_channel;
  3198. __u8 add_channel;
  3199. __le16 op_mode;
  3200. __le16 stbc;
  3201. __u8 add_qos_info;
  3202. __u8 is_qos_sta;
  3203. __le32 fw_sta_ptr;
  3204. } __packed;
  3205. #define MWL8K_STA_ACTION_ADD 0
  3206. #define MWL8K_STA_ACTION_REMOVE 2
  3207. static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
  3208. struct ieee80211_vif *vif,
  3209. struct ieee80211_sta *sta)
  3210. {
  3211. struct mwl8k_cmd_set_new_stn *cmd;
  3212. u32 rates;
  3213. int rc;
  3214. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3215. if (cmd == NULL)
  3216. return -ENOMEM;
  3217. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3218. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3219. cmd->aid = cpu_to_le16(sta->aid);
  3220. memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
  3221. cmd->stn_id = cpu_to_le16(sta->aid);
  3222. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
  3223. if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
  3224. rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
  3225. else
  3226. rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  3227. cmd->legacy_rates = cpu_to_le32(rates);
  3228. if (sta->ht_cap.ht_supported) {
  3229. cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
  3230. cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
  3231. cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
  3232. cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
  3233. cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
  3234. cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
  3235. ((sta->ht_cap.ampdu_density & 7) << 2);
  3236. cmd->is_qos_sta = 1;
  3237. }
  3238. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3239. kfree(cmd);
  3240. return rc;
  3241. }
  3242. static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
  3243. struct ieee80211_vif *vif)
  3244. {
  3245. struct mwl8k_cmd_set_new_stn *cmd;
  3246. int rc;
  3247. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3248. if (cmd == NULL)
  3249. return -ENOMEM;
  3250. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3251. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3252. memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
  3253. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3254. kfree(cmd);
  3255. return rc;
  3256. }
  3257. static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
  3258. struct ieee80211_vif *vif, u8 *addr)
  3259. {
  3260. struct mwl8k_cmd_set_new_stn *cmd;
  3261. struct mwl8k_priv *priv = hw->priv;
  3262. int rc, i;
  3263. u8 idx;
  3264. spin_lock(&priv->stream_lock);
  3265. /* Destroy any active ampdu streams for this sta */
  3266. for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
  3267. struct mwl8k_ampdu_stream *s;
  3268. s = &priv->ampdu[i];
  3269. if (s->state != AMPDU_NO_STREAM) {
  3270. if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
  3271. if (s->state == AMPDU_STREAM_ACTIVE) {
  3272. idx = s->idx;
  3273. spin_unlock(&priv->stream_lock);
  3274. mwl8k_destroy_ba(hw, idx);
  3275. spin_lock(&priv->stream_lock);
  3276. } else if (s->state == AMPDU_STREAM_NEW) {
  3277. mwl8k_remove_stream(hw, s);
  3278. }
  3279. }
  3280. }
  3281. }
  3282. spin_unlock(&priv->stream_lock);
  3283. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3284. if (cmd == NULL)
  3285. return -ENOMEM;
  3286. cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
  3287. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3288. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3289. cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
  3290. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3291. kfree(cmd);
  3292. return rc;
  3293. }
  3294. /*
  3295. * CMD_UPDATE_ENCRYPTION.
  3296. */
  3297. #define MAX_ENCR_KEY_LENGTH 16
  3298. #define MIC_KEY_LENGTH 8
  3299. struct mwl8k_cmd_update_encryption {
  3300. struct mwl8k_cmd_pkt header;
  3301. __le32 action;
  3302. __le32 reserved;
  3303. __u8 mac_addr[6];
  3304. __u8 encr_type;
  3305. } __packed;
  3306. struct mwl8k_cmd_set_key {
  3307. struct mwl8k_cmd_pkt header;
  3308. __le32 action;
  3309. __le32 reserved;
  3310. __le16 length;
  3311. __le16 key_type_id;
  3312. __le32 key_info;
  3313. __le32 key_id;
  3314. __le16 key_len;
  3315. __u8 key_material[MAX_ENCR_KEY_LENGTH];
  3316. __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
  3317. __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
  3318. __le16 tkip_rsc_low;
  3319. __le32 tkip_rsc_high;
  3320. __le16 tkip_tsc_low;
  3321. __le32 tkip_tsc_high;
  3322. __u8 mac_addr[6];
  3323. } __packed;
  3324. enum {
  3325. MWL8K_ENCR_ENABLE,
  3326. MWL8K_ENCR_SET_KEY,
  3327. MWL8K_ENCR_REMOVE_KEY,
  3328. MWL8K_ENCR_SET_GROUP_KEY,
  3329. };
  3330. #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
  3331. #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
  3332. #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
  3333. #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
  3334. #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
  3335. enum {
  3336. MWL8K_ALG_WEP,
  3337. MWL8K_ALG_TKIP,
  3338. MWL8K_ALG_CCMP,
  3339. };
  3340. #define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
  3341. #define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
  3342. #define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
  3343. #define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
  3344. #define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
  3345. static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
  3346. struct ieee80211_vif *vif,
  3347. u8 *addr,
  3348. u8 encr_type)
  3349. {
  3350. struct mwl8k_cmd_update_encryption *cmd;
  3351. int rc;
  3352. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3353. if (cmd == NULL)
  3354. return -ENOMEM;
  3355. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3356. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3357. cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
  3358. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3359. cmd->encr_type = encr_type;
  3360. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3361. kfree(cmd);
  3362. return rc;
  3363. }
  3364. static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
  3365. u8 *addr,
  3366. struct ieee80211_key_conf *key)
  3367. {
  3368. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
  3369. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3370. cmd->length = cpu_to_le16(sizeof(*cmd) -
  3371. offsetof(struct mwl8k_cmd_set_key, length));
  3372. cmd->key_id = cpu_to_le32(key->keyidx);
  3373. cmd->key_len = cpu_to_le16(key->keylen);
  3374. memcpy(cmd->mac_addr, addr, ETH_ALEN);
  3375. switch (key->cipher) {
  3376. case WLAN_CIPHER_SUITE_WEP40:
  3377. case WLAN_CIPHER_SUITE_WEP104:
  3378. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
  3379. if (key->keyidx == 0)
  3380. cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
  3381. break;
  3382. case WLAN_CIPHER_SUITE_TKIP:
  3383. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
  3384. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3385. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3386. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3387. cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
  3388. | MWL8K_KEY_FLAG_TSC_VALID);
  3389. break;
  3390. case WLAN_CIPHER_SUITE_CCMP:
  3391. cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
  3392. cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3393. ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
  3394. : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
  3395. break;
  3396. default:
  3397. return -ENOTSUPP;
  3398. }
  3399. return 0;
  3400. }
  3401. static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
  3402. struct ieee80211_vif *vif,
  3403. u8 *addr,
  3404. struct ieee80211_key_conf *key)
  3405. {
  3406. struct mwl8k_cmd_set_key *cmd;
  3407. int rc;
  3408. int keymlen;
  3409. u32 action;
  3410. u8 idx;
  3411. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3412. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3413. if (cmd == NULL)
  3414. return -ENOMEM;
  3415. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3416. if (rc < 0)
  3417. goto done;
  3418. idx = key->keyidx;
  3419. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  3420. action = MWL8K_ENCR_SET_KEY;
  3421. else
  3422. action = MWL8K_ENCR_SET_GROUP_KEY;
  3423. switch (key->cipher) {
  3424. case WLAN_CIPHER_SUITE_WEP40:
  3425. case WLAN_CIPHER_SUITE_WEP104:
  3426. if (!mwl8k_vif->wep_key_conf[idx].enabled) {
  3427. memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
  3428. sizeof(*key) + key->keylen);
  3429. mwl8k_vif->wep_key_conf[idx].enabled = 1;
  3430. }
  3431. keymlen = key->keylen;
  3432. action = MWL8K_ENCR_SET_KEY;
  3433. break;
  3434. case WLAN_CIPHER_SUITE_TKIP:
  3435. keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
  3436. break;
  3437. case WLAN_CIPHER_SUITE_CCMP:
  3438. keymlen = key->keylen;
  3439. break;
  3440. default:
  3441. rc = -ENOTSUPP;
  3442. goto done;
  3443. }
  3444. memcpy(cmd->key_material, key->key, keymlen);
  3445. cmd->action = cpu_to_le32(action);
  3446. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3447. done:
  3448. kfree(cmd);
  3449. return rc;
  3450. }
  3451. static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
  3452. struct ieee80211_vif *vif,
  3453. u8 *addr,
  3454. struct ieee80211_key_conf *key)
  3455. {
  3456. struct mwl8k_cmd_set_key *cmd;
  3457. int rc;
  3458. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3459. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3460. if (cmd == NULL)
  3461. return -ENOMEM;
  3462. rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
  3463. if (rc < 0)
  3464. goto done;
  3465. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  3466. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  3467. mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
  3468. cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
  3469. rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
  3470. done:
  3471. kfree(cmd);
  3472. return rc;
  3473. }
  3474. static int mwl8k_set_key(struct ieee80211_hw *hw,
  3475. enum set_key_cmd cmd_param,
  3476. struct ieee80211_vif *vif,
  3477. struct ieee80211_sta *sta,
  3478. struct ieee80211_key_conf *key)
  3479. {
  3480. int rc = 0;
  3481. u8 encr_type;
  3482. u8 *addr;
  3483. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3484. if (vif->type == NL80211_IFTYPE_STATION)
  3485. return -EOPNOTSUPP;
  3486. if (sta == NULL)
  3487. addr = vif->addr;
  3488. else
  3489. addr = sta->addr;
  3490. if (cmd_param == SET_KEY) {
  3491. rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
  3492. if (rc)
  3493. goto out;
  3494. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
  3495. || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
  3496. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
  3497. else
  3498. encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
  3499. rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
  3500. encr_type);
  3501. if (rc)
  3502. goto out;
  3503. mwl8k_vif->is_hw_crypto_enabled = true;
  3504. } else {
  3505. rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
  3506. if (rc)
  3507. goto out;
  3508. }
  3509. out:
  3510. return rc;
  3511. }
  3512. /*
  3513. * CMD_UPDATE_STADB.
  3514. */
  3515. struct ewc_ht_info {
  3516. __le16 control1;
  3517. __le16 control2;
  3518. __le16 control3;
  3519. } __packed;
  3520. struct peer_capability_info {
  3521. /* Peer type - AP vs. STA. */
  3522. __u8 peer_type;
  3523. /* Basic 802.11 capabilities from assoc resp. */
  3524. __le16 basic_caps;
  3525. /* Set if peer supports 802.11n high throughput (HT). */
  3526. __u8 ht_support;
  3527. /* Valid if HT is supported. */
  3528. __le16 ht_caps;
  3529. __u8 extended_ht_caps;
  3530. struct ewc_ht_info ewc_info;
  3531. /* Legacy rate table. Intersection of our rates and peer rates. */
  3532. __u8 legacy_rates[12];
  3533. /* HT rate table. Intersection of our rates and peer rates. */
  3534. __u8 ht_rates[16];
  3535. __u8 pad[16];
  3536. /* If set, interoperability mode, no proprietary extensions. */
  3537. __u8 interop;
  3538. __u8 pad2;
  3539. __u8 station_id;
  3540. __le16 amsdu_enabled;
  3541. } __packed;
  3542. struct mwl8k_cmd_update_stadb {
  3543. struct mwl8k_cmd_pkt header;
  3544. /* See STADB_ACTION_TYPE */
  3545. __le32 action;
  3546. /* Peer MAC address */
  3547. __u8 peer_addr[ETH_ALEN];
  3548. __le32 reserved;
  3549. /* Peer info - valid during add/update. */
  3550. struct peer_capability_info peer_info;
  3551. } __packed;
  3552. #define MWL8K_STA_DB_MODIFY_ENTRY 1
  3553. #define MWL8K_STA_DB_DEL_ENTRY 2
  3554. /* Peer Entry flags - used to define the type of the peer node */
  3555. #define MWL8K_PEER_TYPE_ACCESSPOINT 2
  3556. static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
  3557. struct ieee80211_vif *vif,
  3558. struct ieee80211_sta *sta)
  3559. {
  3560. struct mwl8k_cmd_update_stadb *cmd;
  3561. struct peer_capability_info *p;
  3562. u32 rates;
  3563. int rc;
  3564. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3565. if (cmd == NULL)
  3566. return -ENOMEM;
  3567. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3568. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3569. cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
  3570. memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
  3571. p = &cmd->peer_info;
  3572. p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
  3573. p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
  3574. p->ht_support = sta->ht_cap.ht_supported;
  3575. p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
  3576. p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
  3577. ((sta->ht_cap.ampdu_density & 7) << 2);
  3578. if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
  3579. rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
  3580. else
  3581. rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  3582. legacy_rate_mask_to_array(p->legacy_rates, rates);
  3583. memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
  3584. p->interop = 1;
  3585. p->amsdu_enabled = 0;
  3586. rc = mwl8k_post_cmd(hw, &cmd->header);
  3587. kfree(cmd);
  3588. return rc ? rc : p->station_id;
  3589. }
  3590. static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
  3591. struct ieee80211_vif *vif, u8 *addr)
  3592. {
  3593. struct mwl8k_cmd_update_stadb *cmd;
  3594. int rc;
  3595. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  3596. if (cmd == NULL)
  3597. return -ENOMEM;
  3598. cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
  3599. cmd->header.length = cpu_to_le16(sizeof(*cmd));
  3600. cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
  3601. memcpy(cmd->peer_addr, addr, ETH_ALEN);
  3602. rc = mwl8k_post_cmd(hw, &cmd->header);
  3603. kfree(cmd);
  3604. return rc;
  3605. }
  3606. /*
  3607. * Interrupt handling.
  3608. */
  3609. static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
  3610. {
  3611. struct ieee80211_hw *hw = dev_id;
  3612. struct mwl8k_priv *priv = hw->priv;
  3613. u32 status;
  3614. status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3615. if (!status)
  3616. return IRQ_NONE;
  3617. if (status & MWL8K_A2H_INT_TX_DONE) {
  3618. status &= ~MWL8K_A2H_INT_TX_DONE;
  3619. tasklet_schedule(&priv->poll_tx_task);
  3620. }
  3621. if (status & MWL8K_A2H_INT_RX_READY) {
  3622. status &= ~MWL8K_A2H_INT_RX_READY;
  3623. tasklet_schedule(&priv->poll_rx_task);
  3624. }
  3625. if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
  3626. iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
  3627. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3628. atomic_inc(&priv->watchdog_event_pending);
  3629. status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
  3630. ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
  3631. }
  3632. if (status)
  3633. iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3634. if (status & MWL8K_A2H_INT_OPC_DONE) {
  3635. if (priv->hostcmd_wait != NULL)
  3636. complete(priv->hostcmd_wait);
  3637. }
  3638. if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
  3639. if (!mutex_is_locked(&priv->fw_mutex) &&
  3640. priv->radio_on && priv->pending_tx_pkts)
  3641. mwl8k_tx_start(priv);
  3642. }
  3643. return IRQ_HANDLED;
  3644. }
  3645. static void mwl8k_tx_poll(unsigned long data)
  3646. {
  3647. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3648. struct mwl8k_priv *priv = hw->priv;
  3649. int limit;
  3650. int i;
  3651. limit = 32;
  3652. spin_lock_bh(&priv->tx_lock);
  3653. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3654. limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
  3655. if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
  3656. complete(priv->tx_wait);
  3657. priv->tx_wait = NULL;
  3658. }
  3659. spin_unlock_bh(&priv->tx_lock);
  3660. if (limit) {
  3661. writel(~MWL8K_A2H_INT_TX_DONE,
  3662. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3663. } else {
  3664. tasklet_schedule(&priv->poll_tx_task);
  3665. }
  3666. }
  3667. static void mwl8k_rx_poll(unsigned long data)
  3668. {
  3669. struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
  3670. struct mwl8k_priv *priv = hw->priv;
  3671. int limit;
  3672. limit = 32;
  3673. limit -= rxq_process(hw, 0, limit);
  3674. limit -= rxq_refill(hw, 0, limit);
  3675. if (limit) {
  3676. writel(~MWL8K_A2H_INT_RX_READY,
  3677. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  3678. } else {
  3679. tasklet_schedule(&priv->poll_rx_task);
  3680. }
  3681. }
  3682. /*
  3683. * Core driver operations.
  3684. */
  3685. static void mwl8k_tx(struct ieee80211_hw *hw,
  3686. struct ieee80211_tx_control *control,
  3687. struct sk_buff *skb)
  3688. {
  3689. struct mwl8k_priv *priv = hw->priv;
  3690. int index = skb_get_queue_mapping(skb);
  3691. if (!priv->radio_on) {
  3692. wiphy_debug(hw->wiphy,
  3693. "dropped TX frame since radio disabled\n");
  3694. dev_kfree_skb(skb);
  3695. return;
  3696. }
  3697. mwl8k_txq_xmit(hw, index, control->sta, skb);
  3698. }
  3699. static int mwl8k_start(struct ieee80211_hw *hw)
  3700. {
  3701. struct mwl8k_priv *priv = hw->priv;
  3702. int rc;
  3703. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  3704. IRQF_SHARED, MWL8K_NAME, hw);
  3705. if (rc) {
  3706. priv->irq = -1;
  3707. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  3708. return -EIO;
  3709. }
  3710. priv->irq = priv->pdev->irq;
  3711. /* Enable TX reclaim and RX tasklets. */
  3712. tasklet_enable(&priv->poll_tx_task);
  3713. tasklet_enable(&priv->poll_rx_task);
  3714. /* Enable interrupts */
  3715. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3716. iowrite32(MWL8K_A2H_EVENTS,
  3717. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  3718. rc = mwl8k_fw_lock(hw);
  3719. if (!rc) {
  3720. rc = mwl8k_cmd_radio_enable(hw);
  3721. if (!priv->ap_fw) {
  3722. if (!rc)
  3723. rc = mwl8k_cmd_enable_sniffer(hw, 0);
  3724. if (!rc)
  3725. rc = mwl8k_cmd_set_pre_scan(hw);
  3726. if (!rc)
  3727. rc = mwl8k_cmd_set_post_scan(hw,
  3728. "\x00\x00\x00\x00\x00\x00");
  3729. }
  3730. if (!rc)
  3731. rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
  3732. if (!rc)
  3733. rc = mwl8k_cmd_set_wmm_mode(hw, 0);
  3734. mwl8k_fw_unlock(hw);
  3735. }
  3736. if (rc) {
  3737. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3738. free_irq(priv->pdev->irq, hw);
  3739. priv->irq = -1;
  3740. tasklet_disable(&priv->poll_tx_task);
  3741. tasklet_disable(&priv->poll_rx_task);
  3742. } else {
  3743. ieee80211_wake_queues(hw);
  3744. }
  3745. return rc;
  3746. }
  3747. static void mwl8k_stop(struct ieee80211_hw *hw)
  3748. {
  3749. struct mwl8k_priv *priv = hw->priv;
  3750. int i;
  3751. if (!priv->hw_restart_in_progress)
  3752. mwl8k_cmd_radio_disable(hw);
  3753. ieee80211_stop_queues(hw);
  3754. /* Disable interrupts */
  3755. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  3756. if (priv->irq != -1) {
  3757. free_irq(priv->pdev->irq, hw);
  3758. priv->irq = -1;
  3759. }
  3760. /* Stop finalize join worker */
  3761. cancel_work_sync(&priv->finalize_join_worker);
  3762. cancel_work_sync(&priv->watchdog_ba_handle);
  3763. if (priv->beacon_skb != NULL)
  3764. dev_kfree_skb(priv->beacon_skb);
  3765. /* Stop TX reclaim and RX tasklets. */
  3766. tasklet_disable(&priv->poll_tx_task);
  3767. tasklet_disable(&priv->poll_rx_task);
  3768. /* Return all skbs to mac80211 */
  3769. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  3770. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  3771. }
  3772. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
  3773. static int mwl8k_add_interface(struct ieee80211_hw *hw,
  3774. struct ieee80211_vif *vif)
  3775. {
  3776. struct mwl8k_priv *priv = hw->priv;
  3777. struct mwl8k_vif *mwl8k_vif;
  3778. u32 macids_supported;
  3779. int macid, rc;
  3780. struct mwl8k_device_info *di;
  3781. /*
  3782. * Reject interface creation if sniffer mode is active, as
  3783. * STA operation is mutually exclusive with hardware sniffer
  3784. * mode. (Sniffer mode is only used on STA firmware.)
  3785. */
  3786. if (priv->sniffer_enabled) {
  3787. wiphy_info(hw->wiphy,
  3788. "unable to create STA interface because sniffer mode is enabled\n");
  3789. return -EINVAL;
  3790. }
  3791. di = priv->device_info;
  3792. switch (vif->type) {
  3793. case NL80211_IFTYPE_AP:
  3794. if (!priv->ap_fw && di->fw_image_ap) {
  3795. /* we must load the ap fw to meet this request */
  3796. if (!list_empty(&priv->vif_list))
  3797. return -EBUSY;
  3798. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  3799. if (rc)
  3800. return rc;
  3801. }
  3802. macids_supported = priv->ap_macids_supported;
  3803. break;
  3804. case NL80211_IFTYPE_STATION:
  3805. if (priv->ap_fw && di->fw_image_sta) {
  3806. /* we must load the sta fw to meet this request */
  3807. if (!list_empty(&priv->vif_list))
  3808. return -EBUSY;
  3809. rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
  3810. if (rc)
  3811. return rc;
  3812. }
  3813. macids_supported = priv->sta_macids_supported;
  3814. break;
  3815. default:
  3816. return -EINVAL;
  3817. }
  3818. macid = ffs(macids_supported & ~priv->macids_used);
  3819. if (!macid--)
  3820. return -EBUSY;
  3821. /* Setup driver private area. */
  3822. mwl8k_vif = MWL8K_VIF(vif);
  3823. memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
  3824. mwl8k_vif->vif = vif;
  3825. mwl8k_vif->macid = macid;
  3826. mwl8k_vif->seqno = 0;
  3827. memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
  3828. mwl8k_vif->is_hw_crypto_enabled = false;
  3829. /* Set the mac address. */
  3830. mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
  3831. if (priv->ap_fw)
  3832. mwl8k_cmd_set_new_stn_add_self(hw, vif);
  3833. priv->macids_used |= 1 << mwl8k_vif->macid;
  3834. list_add_tail(&mwl8k_vif->list, &priv->vif_list);
  3835. return 0;
  3836. }
  3837. static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
  3838. {
  3839. /* Has ieee80211_restart_hw re-added the removed interfaces? */
  3840. if (!priv->macids_used)
  3841. return;
  3842. priv->macids_used &= ~(1 << vif->macid);
  3843. list_del(&vif->list);
  3844. }
  3845. static void mwl8k_remove_interface(struct ieee80211_hw *hw,
  3846. struct ieee80211_vif *vif)
  3847. {
  3848. struct mwl8k_priv *priv = hw->priv;
  3849. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  3850. if (priv->ap_fw)
  3851. mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
  3852. mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
  3853. mwl8k_remove_vif(priv, mwl8k_vif);
  3854. }
  3855. static void mwl8k_hw_restart_work(struct work_struct *work)
  3856. {
  3857. struct mwl8k_priv *priv =
  3858. container_of(work, struct mwl8k_priv, fw_reload);
  3859. struct ieee80211_hw *hw = priv->hw;
  3860. struct mwl8k_device_info *di;
  3861. int rc;
  3862. /* If some command is waiting for a response, clear it */
  3863. if (priv->hostcmd_wait != NULL) {
  3864. complete(priv->hostcmd_wait);
  3865. priv->hostcmd_wait = NULL;
  3866. }
  3867. priv->hw_restart_owner = current;
  3868. di = priv->device_info;
  3869. mwl8k_fw_lock(hw);
  3870. if (priv->ap_fw)
  3871. rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
  3872. else
  3873. rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
  3874. if (rc)
  3875. goto fail;
  3876. priv->hw_restart_owner = NULL;
  3877. priv->hw_restart_in_progress = false;
  3878. /*
  3879. * This unlock will wake up the queues and
  3880. * also opens the command path for other
  3881. * commands
  3882. */
  3883. mwl8k_fw_unlock(hw);
  3884. ieee80211_restart_hw(hw);
  3885. wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
  3886. return;
  3887. fail:
  3888. mwl8k_fw_unlock(hw);
  3889. wiphy_err(hw->wiphy, "Firmware restart failed\n");
  3890. }
  3891. static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
  3892. {
  3893. struct ieee80211_conf *conf = &hw->conf;
  3894. struct mwl8k_priv *priv = hw->priv;
  3895. int rc;
  3896. if (conf->flags & IEEE80211_CONF_IDLE) {
  3897. mwl8k_cmd_radio_disable(hw);
  3898. return 0;
  3899. }
  3900. rc = mwl8k_fw_lock(hw);
  3901. if (rc)
  3902. return rc;
  3903. rc = mwl8k_cmd_radio_enable(hw);
  3904. if (rc)
  3905. goto out;
  3906. rc = mwl8k_cmd_set_rf_channel(hw, conf);
  3907. if (rc)
  3908. goto out;
  3909. if (conf->power_level > 18)
  3910. conf->power_level = 18;
  3911. if (priv->ap_fw) {
  3912. if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
  3913. rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
  3914. if (rc)
  3915. goto out;
  3916. }
  3917. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
  3918. if (rc)
  3919. wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
  3920. rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
  3921. if (rc)
  3922. wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
  3923. } else {
  3924. rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
  3925. if (rc)
  3926. goto out;
  3927. rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
  3928. }
  3929. out:
  3930. mwl8k_fw_unlock(hw);
  3931. return rc;
  3932. }
  3933. static void
  3934. mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3935. struct ieee80211_bss_conf *info, u32 changed)
  3936. {
  3937. struct mwl8k_priv *priv = hw->priv;
  3938. u32 ap_legacy_rates = 0;
  3939. u8 ap_mcs_rates[16];
  3940. int rc;
  3941. if (mwl8k_fw_lock(hw))
  3942. return;
  3943. /*
  3944. * No need to capture a beacon if we're no longer associated.
  3945. */
  3946. if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
  3947. priv->capture_beacon = false;
  3948. /*
  3949. * Get the AP's legacy and MCS rates.
  3950. */
  3951. if (vif->bss_conf.assoc) {
  3952. struct ieee80211_sta *ap;
  3953. rcu_read_lock();
  3954. ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
  3955. if (ap == NULL) {
  3956. rcu_read_unlock();
  3957. goto out;
  3958. }
  3959. if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
  3960. ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
  3961. } else {
  3962. ap_legacy_rates =
  3963. ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
  3964. }
  3965. memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
  3966. rcu_read_unlock();
  3967. }
  3968. if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
  3969. rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
  3970. if (rc)
  3971. goto out;
  3972. rc = mwl8k_cmd_use_fixed_rate_sta(hw);
  3973. if (rc)
  3974. goto out;
  3975. }
  3976. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  3977. rc = mwl8k_set_radio_preamble(hw,
  3978. vif->bss_conf.use_short_preamble);
  3979. if (rc)
  3980. goto out;
  3981. }
  3982. if (changed & BSS_CHANGED_ERP_SLOT) {
  3983. rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
  3984. if (rc)
  3985. goto out;
  3986. }
  3987. if (vif->bss_conf.assoc &&
  3988. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
  3989. BSS_CHANGED_HT))) {
  3990. rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
  3991. if (rc)
  3992. goto out;
  3993. }
  3994. if (vif->bss_conf.assoc &&
  3995. (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
  3996. /*
  3997. * Finalize the join. Tell rx handler to process
  3998. * next beacon from our BSSID.
  3999. */
  4000. memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
  4001. priv->capture_beacon = true;
  4002. }
  4003. out:
  4004. mwl8k_fw_unlock(hw);
  4005. }
  4006. static void
  4007. mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4008. struct ieee80211_bss_conf *info, u32 changed)
  4009. {
  4010. int rc;
  4011. if (mwl8k_fw_lock(hw))
  4012. return;
  4013. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4014. rc = mwl8k_set_radio_preamble(hw,
  4015. vif->bss_conf.use_short_preamble);
  4016. if (rc)
  4017. goto out;
  4018. }
  4019. if (changed & BSS_CHANGED_BASIC_RATES) {
  4020. int idx;
  4021. int rate;
  4022. /*
  4023. * Use lowest supported basic rate for multicasts
  4024. * and management frames (such as probe responses --
  4025. * beacons will always go out at 1 Mb/s).
  4026. */
  4027. idx = ffs(vif->bss_conf.basic_rates);
  4028. if (idx)
  4029. idx--;
  4030. if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
  4031. rate = mwl8k_rates_24[idx].hw_value;
  4032. else
  4033. rate = mwl8k_rates_50[idx].hw_value;
  4034. mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
  4035. }
  4036. if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
  4037. struct sk_buff *skb;
  4038. skb = ieee80211_beacon_get(hw, vif);
  4039. if (skb != NULL) {
  4040. mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
  4041. kfree_skb(skb);
  4042. }
  4043. }
  4044. if (changed & BSS_CHANGED_BEACON_ENABLED)
  4045. mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
  4046. out:
  4047. mwl8k_fw_unlock(hw);
  4048. }
  4049. static void
  4050. mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4051. struct ieee80211_bss_conf *info, u32 changed)
  4052. {
  4053. struct mwl8k_priv *priv = hw->priv;
  4054. if (!priv->ap_fw)
  4055. mwl8k_bss_info_changed_sta(hw, vif, info, changed);
  4056. else
  4057. mwl8k_bss_info_changed_ap(hw, vif, info, changed);
  4058. }
  4059. static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
  4060. struct netdev_hw_addr_list *mc_list)
  4061. {
  4062. struct mwl8k_cmd_pkt *cmd;
  4063. /*
  4064. * Synthesize and return a command packet that programs the
  4065. * hardware multicast address filter. At this point we don't
  4066. * know whether FIF_ALLMULTI is being requested, but if it is,
  4067. * we'll end up throwing this packet away and creating a new
  4068. * one in mwl8k_configure_filter().
  4069. */
  4070. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
  4071. return (unsigned long)cmd;
  4072. }
  4073. static int
  4074. mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
  4075. unsigned int changed_flags,
  4076. unsigned int *total_flags)
  4077. {
  4078. struct mwl8k_priv *priv = hw->priv;
  4079. /*
  4080. * Hardware sniffer mode is mutually exclusive with STA
  4081. * operation, so refuse to enable sniffer mode if a STA
  4082. * interface is active.
  4083. */
  4084. if (!list_empty(&priv->vif_list)) {
  4085. if (net_ratelimit())
  4086. wiphy_info(hw->wiphy,
  4087. "not enabling sniffer mode because STA interface is active\n");
  4088. return 0;
  4089. }
  4090. if (!priv->sniffer_enabled) {
  4091. if (mwl8k_cmd_enable_sniffer(hw, 1))
  4092. return 0;
  4093. priv->sniffer_enabled = true;
  4094. }
  4095. *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
  4096. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
  4097. FIF_OTHER_BSS;
  4098. return 1;
  4099. }
  4100. static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
  4101. {
  4102. if (!list_empty(&priv->vif_list))
  4103. return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
  4104. return NULL;
  4105. }
  4106. static void mwl8k_configure_filter(struct ieee80211_hw *hw,
  4107. unsigned int changed_flags,
  4108. unsigned int *total_flags,
  4109. u64 multicast)
  4110. {
  4111. struct mwl8k_priv *priv = hw->priv;
  4112. struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
  4113. /*
  4114. * AP firmware doesn't allow fine-grained control over
  4115. * the receive filter.
  4116. */
  4117. if (priv->ap_fw) {
  4118. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4119. kfree(cmd);
  4120. return;
  4121. }
  4122. /*
  4123. * Enable hardware sniffer mode if FIF_CONTROL or
  4124. * FIF_OTHER_BSS is requested.
  4125. */
  4126. if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
  4127. mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
  4128. kfree(cmd);
  4129. return;
  4130. }
  4131. /* Clear unsupported feature flags */
  4132. *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
  4133. if (mwl8k_fw_lock(hw)) {
  4134. kfree(cmd);
  4135. return;
  4136. }
  4137. if (priv->sniffer_enabled) {
  4138. mwl8k_cmd_enable_sniffer(hw, 0);
  4139. priv->sniffer_enabled = false;
  4140. }
  4141. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  4142. if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
  4143. /*
  4144. * Disable the BSS filter.
  4145. */
  4146. mwl8k_cmd_set_pre_scan(hw);
  4147. } else {
  4148. struct mwl8k_vif *mwl8k_vif;
  4149. const u8 *bssid;
  4150. /*
  4151. * Enable the BSS filter.
  4152. *
  4153. * If there is an active STA interface, use that
  4154. * interface's BSSID, otherwise use a dummy one
  4155. * (where the OUI part needs to be nonzero for
  4156. * the BSSID to be accepted by POST_SCAN).
  4157. */
  4158. mwl8k_vif = mwl8k_first_vif(priv);
  4159. if (mwl8k_vif != NULL)
  4160. bssid = mwl8k_vif->vif->bss_conf.bssid;
  4161. else
  4162. bssid = "\x01\x00\x00\x00\x00\x00";
  4163. mwl8k_cmd_set_post_scan(hw, bssid);
  4164. }
  4165. }
  4166. /*
  4167. * If FIF_ALLMULTI is being requested, throw away the command
  4168. * packet that ->prepare_multicast() built and replace it with
  4169. * a command packet that enables reception of all multicast
  4170. * packets.
  4171. */
  4172. if (*total_flags & FIF_ALLMULTI) {
  4173. kfree(cmd);
  4174. cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
  4175. }
  4176. if (cmd != NULL) {
  4177. mwl8k_post_cmd(hw, cmd);
  4178. kfree(cmd);
  4179. }
  4180. mwl8k_fw_unlock(hw);
  4181. }
  4182. static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  4183. {
  4184. return mwl8k_cmd_set_rts_threshold(hw, value);
  4185. }
  4186. static int mwl8k_sta_remove(struct ieee80211_hw *hw,
  4187. struct ieee80211_vif *vif,
  4188. struct ieee80211_sta *sta)
  4189. {
  4190. struct mwl8k_priv *priv = hw->priv;
  4191. if (priv->ap_fw)
  4192. return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
  4193. else
  4194. return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
  4195. }
  4196. static int mwl8k_sta_add(struct ieee80211_hw *hw,
  4197. struct ieee80211_vif *vif,
  4198. struct ieee80211_sta *sta)
  4199. {
  4200. struct mwl8k_priv *priv = hw->priv;
  4201. int ret;
  4202. int i;
  4203. struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
  4204. struct ieee80211_key_conf *key;
  4205. if (!priv->ap_fw) {
  4206. ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
  4207. if (ret >= 0) {
  4208. MWL8K_STA(sta)->peer_id = ret;
  4209. if (sta->ht_cap.ht_supported)
  4210. MWL8K_STA(sta)->is_ampdu_allowed = true;
  4211. ret = 0;
  4212. }
  4213. } else {
  4214. ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
  4215. }
  4216. for (i = 0; i < NUM_WEP_KEYS; i++) {
  4217. key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
  4218. if (mwl8k_vif->wep_key_conf[i].enabled)
  4219. mwl8k_set_key(hw, SET_KEY, vif, sta, key);
  4220. }
  4221. return ret;
  4222. }
  4223. static int mwl8k_conf_tx(struct ieee80211_hw *hw,
  4224. struct ieee80211_vif *vif, u16 queue,
  4225. const struct ieee80211_tx_queue_params *params)
  4226. {
  4227. struct mwl8k_priv *priv = hw->priv;
  4228. int rc;
  4229. rc = mwl8k_fw_lock(hw);
  4230. if (!rc) {
  4231. BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
  4232. memcpy(&priv->wmm_params[queue], params, sizeof(*params));
  4233. if (!priv->wmm_enabled)
  4234. rc = mwl8k_cmd_set_wmm_mode(hw, 1);
  4235. if (!rc) {
  4236. int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
  4237. rc = mwl8k_cmd_set_edca_params(hw, q,
  4238. params->cw_min,
  4239. params->cw_max,
  4240. params->aifs,
  4241. params->txop);
  4242. }
  4243. mwl8k_fw_unlock(hw);
  4244. }
  4245. return rc;
  4246. }
  4247. static int mwl8k_get_stats(struct ieee80211_hw *hw,
  4248. struct ieee80211_low_level_stats *stats)
  4249. {
  4250. return mwl8k_cmd_get_stat(hw, stats);
  4251. }
  4252. static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
  4253. struct survey_info *survey)
  4254. {
  4255. struct mwl8k_priv *priv = hw->priv;
  4256. struct ieee80211_conf *conf = &hw->conf;
  4257. if (idx != 0)
  4258. return -ENOENT;
  4259. survey->channel = conf->channel;
  4260. survey->filled = SURVEY_INFO_NOISE_DBM;
  4261. survey->noise = priv->noise;
  4262. return 0;
  4263. }
  4264. #define MAX_AMPDU_ATTEMPTS 5
  4265. static int
  4266. mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4267. enum ieee80211_ampdu_mlme_action action,
  4268. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  4269. u8 buf_size)
  4270. {
  4271. int i, rc = 0;
  4272. struct mwl8k_priv *priv = hw->priv;
  4273. struct mwl8k_ampdu_stream *stream;
  4274. u8 *addr = sta->addr, idx;
  4275. struct mwl8k_sta *sta_info = MWL8K_STA(sta);
  4276. if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
  4277. return -ENOTSUPP;
  4278. spin_lock(&priv->stream_lock);
  4279. stream = mwl8k_lookup_stream(hw, addr, tid);
  4280. switch (action) {
  4281. case IEEE80211_AMPDU_RX_START:
  4282. case IEEE80211_AMPDU_RX_STOP:
  4283. break;
  4284. case IEEE80211_AMPDU_TX_START:
  4285. /* By the time we get here the hw queues may contain outgoing
  4286. * packets for this RA/TID that are not part of this BA
  4287. * session. The hw will assign sequence numbers to these
  4288. * packets as they go out. So if we query the hw for its next
  4289. * sequence number and use that for the SSN here, it may end up
  4290. * being wrong, which will lead to sequence number mismatch at
  4291. * the recipient. To avoid this, we reset the sequence number
  4292. * to O for the first MPDU in this BA stream.
  4293. */
  4294. *ssn = 0;
  4295. if (stream == NULL) {
  4296. /* This means that somebody outside this driver called
  4297. * ieee80211_start_tx_ba_session. This is unexpected
  4298. * because we do our own rate control. Just warn and
  4299. * move on.
  4300. */
  4301. wiphy_warn(hw->wiphy, "Unexpected call to %s. "
  4302. "Proceeding anyway.\n", __func__);
  4303. stream = mwl8k_add_stream(hw, sta, tid);
  4304. }
  4305. if (stream == NULL) {
  4306. wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
  4307. rc = -EBUSY;
  4308. break;
  4309. }
  4310. stream->state = AMPDU_STREAM_IN_PROGRESS;
  4311. /* Release the lock before we do the time consuming stuff */
  4312. spin_unlock(&priv->stream_lock);
  4313. for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
  4314. /* Check if link is still valid */
  4315. if (!sta_info->is_ampdu_allowed) {
  4316. spin_lock(&priv->stream_lock);
  4317. mwl8k_remove_stream(hw, stream);
  4318. spin_unlock(&priv->stream_lock);
  4319. return -EBUSY;
  4320. }
  4321. rc = mwl8k_check_ba(hw, stream, vif);
  4322. /* If HW restart is in progress mwl8k_post_cmd will
  4323. * return -EBUSY. Avoid retrying mwl8k_check_ba in
  4324. * such cases
  4325. */
  4326. if (!rc || rc == -EBUSY)
  4327. break;
  4328. /*
  4329. * HW queues take time to be flushed, give them
  4330. * sufficient time
  4331. */
  4332. msleep(1000);
  4333. }
  4334. spin_lock(&priv->stream_lock);
  4335. if (rc) {
  4336. wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
  4337. " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
  4338. mwl8k_remove_stream(hw, stream);
  4339. rc = -EBUSY;
  4340. break;
  4341. }
  4342. ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
  4343. break;
  4344. case IEEE80211_AMPDU_TX_STOP_CONT:
  4345. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4346. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4347. if (stream) {
  4348. if (stream->state == AMPDU_STREAM_ACTIVE) {
  4349. idx = stream->idx;
  4350. spin_unlock(&priv->stream_lock);
  4351. mwl8k_destroy_ba(hw, idx);
  4352. spin_lock(&priv->stream_lock);
  4353. }
  4354. mwl8k_remove_stream(hw, stream);
  4355. }
  4356. ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
  4357. break;
  4358. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4359. BUG_ON(stream == NULL);
  4360. BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
  4361. spin_unlock(&priv->stream_lock);
  4362. rc = mwl8k_create_ba(hw, stream, buf_size, vif);
  4363. spin_lock(&priv->stream_lock);
  4364. if (!rc)
  4365. stream->state = AMPDU_STREAM_ACTIVE;
  4366. else {
  4367. idx = stream->idx;
  4368. spin_unlock(&priv->stream_lock);
  4369. mwl8k_destroy_ba(hw, idx);
  4370. spin_lock(&priv->stream_lock);
  4371. wiphy_debug(hw->wiphy,
  4372. "Failed adding stream for sta %pM tid %d\n",
  4373. addr, tid);
  4374. mwl8k_remove_stream(hw, stream);
  4375. }
  4376. break;
  4377. default:
  4378. rc = -ENOTSUPP;
  4379. }
  4380. spin_unlock(&priv->stream_lock);
  4381. return rc;
  4382. }
  4383. static const struct ieee80211_ops mwl8k_ops = {
  4384. .tx = mwl8k_tx,
  4385. .start = mwl8k_start,
  4386. .stop = mwl8k_stop,
  4387. .add_interface = mwl8k_add_interface,
  4388. .remove_interface = mwl8k_remove_interface,
  4389. .config = mwl8k_config,
  4390. .bss_info_changed = mwl8k_bss_info_changed,
  4391. .prepare_multicast = mwl8k_prepare_multicast,
  4392. .configure_filter = mwl8k_configure_filter,
  4393. .set_key = mwl8k_set_key,
  4394. .set_rts_threshold = mwl8k_set_rts_threshold,
  4395. .sta_add = mwl8k_sta_add,
  4396. .sta_remove = mwl8k_sta_remove,
  4397. .conf_tx = mwl8k_conf_tx,
  4398. .get_stats = mwl8k_get_stats,
  4399. .get_survey = mwl8k_get_survey,
  4400. .ampdu_action = mwl8k_ampdu_action,
  4401. };
  4402. static void mwl8k_finalize_join_worker(struct work_struct *work)
  4403. {
  4404. struct mwl8k_priv *priv =
  4405. container_of(work, struct mwl8k_priv, finalize_join_worker);
  4406. struct sk_buff *skb = priv->beacon_skb;
  4407. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  4408. int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  4409. const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
  4410. mgmt->u.beacon.variable, len);
  4411. int dtim_period = 1;
  4412. if (tim && tim[1] >= 2)
  4413. dtim_period = tim[3];
  4414. mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
  4415. dev_kfree_skb(skb);
  4416. priv->beacon_skb = NULL;
  4417. }
  4418. enum {
  4419. MWL8363 = 0,
  4420. MWL8687,
  4421. MWL8366,
  4422. };
  4423. #define MWL8K_8366_AP_FW_API 3
  4424. #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
  4425. #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
  4426. static struct mwl8k_device_info mwl8k_info_tbl[] = {
  4427. [MWL8363] = {
  4428. .part_name = "88w8363",
  4429. .helper_image = "mwl8k/helper_8363.fw",
  4430. .fw_image_sta = "mwl8k/fmimage_8363.fw",
  4431. },
  4432. [MWL8687] = {
  4433. .part_name = "88w8687",
  4434. .helper_image = "mwl8k/helper_8687.fw",
  4435. .fw_image_sta = "mwl8k/fmimage_8687.fw",
  4436. },
  4437. [MWL8366] = {
  4438. .part_name = "88w8366",
  4439. .helper_image = "mwl8k/helper_8366.fw",
  4440. .fw_image_sta = "mwl8k/fmimage_8366.fw",
  4441. .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
  4442. .fw_api_ap = MWL8K_8366_AP_FW_API,
  4443. .ap_rxd_ops = &rxd_8366_ap_ops,
  4444. },
  4445. };
  4446. MODULE_FIRMWARE("mwl8k/helper_8363.fw");
  4447. MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
  4448. MODULE_FIRMWARE("mwl8k/helper_8687.fw");
  4449. MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
  4450. MODULE_FIRMWARE("mwl8k/helper_8366.fw");
  4451. MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
  4452. MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
  4453. static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
  4454. { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
  4455. { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
  4456. { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
  4457. { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
  4458. { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
  4459. { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
  4460. { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
  4461. { },
  4462. };
  4463. MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
  4464. static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
  4465. {
  4466. int rc;
  4467. printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
  4468. "Trying alternative firmware %s\n", pci_name(priv->pdev),
  4469. priv->fw_pref, priv->fw_alt);
  4470. rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
  4471. if (rc) {
  4472. printk(KERN_ERR "%s: Error requesting alt fw %s\n",
  4473. pci_name(priv->pdev), priv->fw_alt);
  4474. return rc;
  4475. }
  4476. return 0;
  4477. }
  4478. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
  4479. static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
  4480. {
  4481. struct mwl8k_priv *priv = context;
  4482. struct mwl8k_device_info *di = priv->device_info;
  4483. int rc;
  4484. switch (priv->fw_state) {
  4485. case FW_STATE_INIT:
  4486. if (!fw) {
  4487. printk(KERN_ERR "%s: Error requesting helper fw %s\n",
  4488. pci_name(priv->pdev), di->helper_image);
  4489. goto fail;
  4490. }
  4491. priv->fw_helper = fw;
  4492. rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
  4493. true);
  4494. if (rc && priv->fw_alt) {
  4495. rc = mwl8k_request_alt_fw(priv);
  4496. if (rc)
  4497. goto fail;
  4498. priv->fw_state = FW_STATE_LOADING_ALT;
  4499. } else if (rc)
  4500. goto fail;
  4501. else
  4502. priv->fw_state = FW_STATE_LOADING_PREF;
  4503. break;
  4504. case FW_STATE_LOADING_PREF:
  4505. if (!fw) {
  4506. if (priv->fw_alt) {
  4507. rc = mwl8k_request_alt_fw(priv);
  4508. if (rc)
  4509. goto fail;
  4510. priv->fw_state = FW_STATE_LOADING_ALT;
  4511. } else
  4512. goto fail;
  4513. } else {
  4514. priv->fw_ucode = fw;
  4515. rc = mwl8k_firmware_load_success(priv);
  4516. if (rc)
  4517. goto fail;
  4518. else
  4519. complete(&priv->firmware_loading_complete);
  4520. }
  4521. break;
  4522. case FW_STATE_LOADING_ALT:
  4523. if (!fw) {
  4524. printk(KERN_ERR "%s: Error requesting alt fw %s\n",
  4525. pci_name(priv->pdev), di->helper_image);
  4526. goto fail;
  4527. }
  4528. priv->fw_ucode = fw;
  4529. rc = mwl8k_firmware_load_success(priv);
  4530. if (rc)
  4531. goto fail;
  4532. else
  4533. complete(&priv->firmware_loading_complete);
  4534. break;
  4535. default:
  4536. printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
  4537. MWL8K_NAME, priv->fw_state);
  4538. BUG_ON(1);
  4539. }
  4540. return;
  4541. fail:
  4542. priv->fw_state = FW_STATE_ERROR;
  4543. complete(&priv->firmware_loading_complete);
  4544. device_release_driver(&priv->pdev->dev);
  4545. mwl8k_release_firmware(priv);
  4546. }
  4547. #define MAX_RESTART_ATTEMPTS 1
  4548. static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
  4549. bool nowait)
  4550. {
  4551. struct mwl8k_priv *priv = hw->priv;
  4552. int rc;
  4553. int count = MAX_RESTART_ATTEMPTS;
  4554. retry:
  4555. /* Reset firmware and hardware */
  4556. mwl8k_hw_reset(priv);
  4557. /* Ask userland hotplug daemon for the device firmware */
  4558. rc = mwl8k_request_firmware(priv, fw_image, nowait);
  4559. if (rc) {
  4560. wiphy_err(hw->wiphy, "Firmware files not found\n");
  4561. return rc;
  4562. }
  4563. if (nowait)
  4564. return rc;
  4565. /* Load firmware into hardware */
  4566. rc = mwl8k_load_firmware(hw);
  4567. if (rc)
  4568. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  4569. /* Reclaim memory once firmware is successfully loaded */
  4570. mwl8k_release_firmware(priv);
  4571. if (rc && count) {
  4572. /* FW did not start successfully;
  4573. * lets try one more time
  4574. */
  4575. count--;
  4576. wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
  4577. msleep(20);
  4578. goto retry;
  4579. }
  4580. return rc;
  4581. }
  4582. static int mwl8k_init_txqs(struct ieee80211_hw *hw)
  4583. {
  4584. struct mwl8k_priv *priv = hw->priv;
  4585. int rc = 0;
  4586. int i;
  4587. for (i = 0; i < mwl8k_tx_queues(priv); i++) {
  4588. rc = mwl8k_txq_init(hw, i);
  4589. if (rc)
  4590. break;
  4591. if (priv->ap_fw)
  4592. iowrite32(priv->txq[i].txd_dma,
  4593. priv->sram + priv->txq_offset[i]);
  4594. }
  4595. return rc;
  4596. }
  4597. /* initialize hw after successfully loading a firmware image */
  4598. static int mwl8k_probe_hw(struct ieee80211_hw *hw)
  4599. {
  4600. struct mwl8k_priv *priv = hw->priv;
  4601. int rc = 0;
  4602. int i;
  4603. if (priv->ap_fw) {
  4604. priv->rxd_ops = priv->device_info->ap_rxd_ops;
  4605. if (priv->rxd_ops == NULL) {
  4606. wiphy_err(hw->wiphy,
  4607. "Driver does not have AP firmware image support for this hardware\n");
  4608. goto err_stop_firmware;
  4609. }
  4610. } else {
  4611. priv->rxd_ops = &rxd_sta_ops;
  4612. }
  4613. priv->sniffer_enabled = false;
  4614. priv->wmm_enabled = false;
  4615. priv->pending_tx_pkts = 0;
  4616. atomic_set(&priv->watchdog_event_pending, 0);
  4617. rc = mwl8k_rxq_init(hw, 0);
  4618. if (rc)
  4619. goto err_stop_firmware;
  4620. rxq_refill(hw, 0, INT_MAX);
  4621. /* For the sta firmware, we need to know the dma addresses of tx queues
  4622. * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
  4623. * prior to issuing this command. But for the AP case, we learn the
  4624. * total number of queues from the result CMD_GET_HW_SPEC, so for this
  4625. * case we must initialize the tx queues after.
  4626. */
  4627. priv->num_ampdu_queues = 0;
  4628. if (!priv->ap_fw) {
  4629. rc = mwl8k_init_txqs(hw);
  4630. if (rc)
  4631. goto err_free_queues;
  4632. }
  4633. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
  4634. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4635. iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
  4636. MWL8K_A2H_INT_BA_WATCHDOG,
  4637. priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
  4638. iowrite32(MWL8K_A2H_INT_OPC_DONE,
  4639. priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
  4640. rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
  4641. IRQF_SHARED, MWL8K_NAME, hw);
  4642. if (rc) {
  4643. wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
  4644. goto err_free_queues;
  4645. }
  4646. /*
  4647. * When hw restart is requested,
  4648. * mac80211 will take care of clearing
  4649. * the ampdu streams, so do not clear
  4650. * the ampdu state here
  4651. */
  4652. if (!priv->hw_restart_in_progress)
  4653. memset(priv->ampdu, 0, sizeof(priv->ampdu));
  4654. /*
  4655. * Temporarily enable interrupts. Initial firmware host
  4656. * commands use interrupts and avoid polling. Disable
  4657. * interrupts when done.
  4658. */
  4659. iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4660. /* Get config data, mac addrs etc */
  4661. if (priv->ap_fw) {
  4662. rc = mwl8k_cmd_get_hw_spec_ap(hw);
  4663. if (!rc)
  4664. rc = mwl8k_init_txqs(hw);
  4665. if (!rc)
  4666. rc = mwl8k_cmd_set_hw_spec(hw);
  4667. } else {
  4668. rc = mwl8k_cmd_get_hw_spec_sta(hw);
  4669. }
  4670. if (rc) {
  4671. wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
  4672. goto err_free_irq;
  4673. }
  4674. /* Turn radio off */
  4675. rc = mwl8k_cmd_radio_disable(hw);
  4676. if (rc) {
  4677. wiphy_err(hw->wiphy, "Cannot disable\n");
  4678. goto err_free_irq;
  4679. }
  4680. /* Clear MAC address */
  4681. rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
  4682. if (rc) {
  4683. wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
  4684. goto err_free_irq;
  4685. }
  4686. /* Disable interrupts */
  4687. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4688. free_irq(priv->pdev->irq, hw);
  4689. wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
  4690. priv->device_info->part_name,
  4691. priv->hw_rev, hw->wiphy->perm_addr,
  4692. priv->ap_fw ? "AP" : "STA",
  4693. (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
  4694. (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
  4695. return 0;
  4696. err_free_irq:
  4697. iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
  4698. free_irq(priv->pdev->irq, hw);
  4699. err_free_queues:
  4700. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4701. mwl8k_txq_deinit(hw, i);
  4702. mwl8k_rxq_deinit(hw, 0);
  4703. err_stop_firmware:
  4704. mwl8k_hw_reset(priv);
  4705. return rc;
  4706. }
  4707. /*
  4708. * invoke mwl8k_reload_firmware to change the firmware image after the device
  4709. * has already been registered
  4710. */
  4711. static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
  4712. {
  4713. int i, rc = 0;
  4714. struct mwl8k_priv *priv = hw->priv;
  4715. struct mwl8k_vif *vif, *tmp_vif;
  4716. mwl8k_stop(hw);
  4717. mwl8k_rxq_deinit(hw, 0);
  4718. /*
  4719. * All the existing interfaces are re-added by the ieee80211_reconfig;
  4720. * which means driver should remove existing interfaces before calling
  4721. * ieee80211_restart_hw
  4722. */
  4723. if (priv->hw_restart_in_progress)
  4724. list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
  4725. mwl8k_remove_vif(priv, vif);
  4726. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4727. mwl8k_txq_deinit(hw, i);
  4728. rc = mwl8k_init_firmware(hw, fw_image, false);
  4729. if (rc)
  4730. goto fail;
  4731. rc = mwl8k_probe_hw(hw);
  4732. if (rc)
  4733. goto fail;
  4734. if (priv->hw_restart_in_progress)
  4735. return rc;
  4736. rc = mwl8k_start(hw);
  4737. if (rc)
  4738. goto fail;
  4739. rc = mwl8k_config(hw, ~0);
  4740. if (rc)
  4741. goto fail;
  4742. for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
  4743. rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
  4744. if (rc)
  4745. goto fail;
  4746. }
  4747. return rc;
  4748. fail:
  4749. printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
  4750. return rc;
  4751. }
  4752. static const struct ieee80211_iface_limit ap_if_limits[] = {
  4753. { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
  4754. };
  4755. static const struct ieee80211_iface_combination ap_if_comb = {
  4756. .limits = ap_if_limits,
  4757. .n_limits = ARRAY_SIZE(ap_if_limits),
  4758. .max_interfaces = 8,
  4759. .num_different_channels = 1,
  4760. };
  4761. static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
  4762. {
  4763. struct ieee80211_hw *hw = priv->hw;
  4764. int i, rc;
  4765. rc = mwl8k_load_firmware(hw);
  4766. mwl8k_release_firmware(priv);
  4767. if (rc) {
  4768. wiphy_err(hw->wiphy, "Cannot start firmware\n");
  4769. return rc;
  4770. }
  4771. /*
  4772. * Extra headroom is the size of the required DMA header
  4773. * minus the size of the smallest 802.11 frame (CTS frame).
  4774. */
  4775. hw->extra_tx_headroom =
  4776. sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
  4777. hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
  4778. hw->channel_change_time = 10;
  4779. hw->queues = MWL8K_TX_WMM_QUEUES;
  4780. /* Set rssi values to dBm */
  4781. hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
  4782. /*
  4783. * Ask mac80211 to not to trigger PS mode
  4784. * based on PM bit of incoming frames.
  4785. */
  4786. if (priv->ap_fw)
  4787. hw->flags |= IEEE80211_HW_AP_LINK_PS;
  4788. hw->vif_data_size = sizeof(struct mwl8k_vif);
  4789. hw->sta_data_size = sizeof(struct mwl8k_sta);
  4790. priv->macids_used = 0;
  4791. INIT_LIST_HEAD(&priv->vif_list);
  4792. /* Set default radio state and preamble */
  4793. priv->radio_on = false;
  4794. priv->radio_short_preamble = false;
  4795. /* Finalize join worker */
  4796. INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
  4797. /* Handle watchdog ba events */
  4798. INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
  4799. /* To reload the firmware if it crashes */
  4800. INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
  4801. /* TX reclaim and RX tasklets. */
  4802. tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
  4803. tasklet_disable(&priv->poll_tx_task);
  4804. tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
  4805. tasklet_disable(&priv->poll_rx_task);
  4806. /* Power management cookie */
  4807. priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
  4808. if (priv->cookie == NULL)
  4809. return -ENOMEM;
  4810. mutex_init(&priv->fw_mutex);
  4811. priv->fw_mutex_owner = NULL;
  4812. priv->fw_mutex_depth = 0;
  4813. priv->hostcmd_wait = NULL;
  4814. spin_lock_init(&priv->tx_lock);
  4815. spin_lock_init(&priv->stream_lock);
  4816. priv->tx_wait = NULL;
  4817. rc = mwl8k_probe_hw(hw);
  4818. if (rc)
  4819. goto err_free_cookie;
  4820. hw->wiphy->interface_modes = 0;
  4821. if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
  4822. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
  4823. hw->wiphy->iface_combinations = &ap_if_comb;
  4824. hw->wiphy->n_iface_combinations = 1;
  4825. }
  4826. if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
  4827. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
  4828. rc = ieee80211_register_hw(hw);
  4829. if (rc) {
  4830. wiphy_err(hw->wiphy, "Cannot register device\n");
  4831. goto err_unprobe_hw;
  4832. }
  4833. return 0;
  4834. err_unprobe_hw:
  4835. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4836. mwl8k_txq_deinit(hw, i);
  4837. mwl8k_rxq_deinit(hw, 0);
  4838. err_free_cookie:
  4839. if (priv->cookie != NULL)
  4840. pci_free_consistent(priv->pdev, 4,
  4841. priv->cookie, priv->cookie_dma);
  4842. return rc;
  4843. }
  4844. static int mwl8k_probe(struct pci_dev *pdev,
  4845. const struct pci_device_id *id)
  4846. {
  4847. static int printed_version;
  4848. struct ieee80211_hw *hw;
  4849. struct mwl8k_priv *priv;
  4850. struct mwl8k_device_info *di;
  4851. int rc;
  4852. if (!printed_version) {
  4853. printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
  4854. printed_version = 1;
  4855. }
  4856. rc = pci_enable_device(pdev);
  4857. if (rc) {
  4858. printk(KERN_ERR "%s: Cannot enable new PCI device\n",
  4859. MWL8K_NAME);
  4860. return rc;
  4861. }
  4862. rc = pci_request_regions(pdev, MWL8K_NAME);
  4863. if (rc) {
  4864. printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
  4865. MWL8K_NAME);
  4866. goto err_disable_device;
  4867. }
  4868. pci_set_master(pdev);
  4869. hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
  4870. if (hw == NULL) {
  4871. printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
  4872. rc = -ENOMEM;
  4873. goto err_free_reg;
  4874. }
  4875. SET_IEEE80211_DEV(hw, &pdev->dev);
  4876. pci_set_drvdata(pdev, hw);
  4877. priv = hw->priv;
  4878. priv->hw = hw;
  4879. priv->pdev = pdev;
  4880. priv->device_info = &mwl8k_info_tbl[id->driver_data];
  4881. priv->sram = pci_iomap(pdev, 0, 0x10000);
  4882. if (priv->sram == NULL) {
  4883. wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
  4884. goto err_iounmap;
  4885. }
  4886. /*
  4887. * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
  4888. * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
  4889. */
  4890. priv->regs = pci_iomap(pdev, 1, 0x10000);
  4891. if (priv->regs == NULL) {
  4892. priv->regs = pci_iomap(pdev, 2, 0x10000);
  4893. if (priv->regs == NULL) {
  4894. wiphy_err(hw->wiphy, "Cannot map device registers\n");
  4895. goto err_iounmap;
  4896. }
  4897. }
  4898. /*
  4899. * Choose the initial fw image depending on user input. If a second
  4900. * image is available, make it the alternative image that will be
  4901. * loaded if the first one fails.
  4902. */
  4903. init_completion(&priv->firmware_loading_complete);
  4904. di = priv->device_info;
  4905. if (ap_mode_default && di->fw_image_ap) {
  4906. priv->fw_pref = di->fw_image_ap;
  4907. priv->fw_alt = di->fw_image_sta;
  4908. } else if (!ap_mode_default && di->fw_image_sta) {
  4909. priv->fw_pref = di->fw_image_sta;
  4910. priv->fw_alt = di->fw_image_ap;
  4911. } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
  4912. printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
  4913. priv->fw_pref = di->fw_image_sta;
  4914. } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
  4915. printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
  4916. priv->fw_pref = di->fw_image_ap;
  4917. }
  4918. rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
  4919. if (rc)
  4920. goto err_stop_firmware;
  4921. priv->hw_restart_in_progress = false;
  4922. return rc;
  4923. err_stop_firmware:
  4924. mwl8k_hw_reset(priv);
  4925. err_iounmap:
  4926. if (priv->regs != NULL)
  4927. pci_iounmap(pdev, priv->regs);
  4928. if (priv->sram != NULL)
  4929. pci_iounmap(pdev, priv->sram);
  4930. pci_set_drvdata(pdev, NULL);
  4931. ieee80211_free_hw(hw);
  4932. err_free_reg:
  4933. pci_release_regions(pdev);
  4934. err_disable_device:
  4935. pci_disable_device(pdev);
  4936. return rc;
  4937. }
  4938. static void mwl8k_remove(struct pci_dev *pdev)
  4939. {
  4940. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  4941. struct mwl8k_priv *priv;
  4942. int i;
  4943. if (hw == NULL)
  4944. return;
  4945. priv = hw->priv;
  4946. wait_for_completion(&priv->firmware_loading_complete);
  4947. if (priv->fw_state == FW_STATE_ERROR) {
  4948. mwl8k_hw_reset(priv);
  4949. goto unmap;
  4950. }
  4951. ieee80211_stop_queues(hw);
  4952. ieee80211_unregister_hw(hw);
  4953. /* Remove TX reclaim and RX tasklets. */
  4954. tasklet_kill(&priv->poll_tx_task);
  4955. tasklet_kill(&priv->poll_rx_task);
  4956. /* Stop hardware */
  4957. mwl8k_hw_reset(priv);
  4958. /* Return all skbs to mac80211 */
  4959. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4960. mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
  4961. for (i = 0; i < mwl8k_tx_queues(priv); i++)
  4962. mwl8k_txq_deinit(hw, i);
  4963. mwl8k_rxq_deinit(hw, 0);
  4964. pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
  4965. unmap:
  4966. pci_iounmap(pdev, priv->regs);
  4967. pci_iounmap(pdev, priv->sram);
  4968. pci_set_drvdata(pdev, NULL);
  4969. ieee80211_free_hw(hw);
  4970. pci_release_regions(pdev);
  4971. pci_disable_device(pdev);
  4972. }
  4973. static struct pci_driver mwl8k_driver = {
  4974. .name = MWL8K_NAME,
  4975. .id_table = mwl8k_pci_id_table,
  4976. .probe = mwl8k_probe,
  4977. .remove = mwl8k_remove,
  4978. };
  4979. module_pci_driver(mwl8k_driver);
  4980. MODULE_DESCRIPTION(MWL8K_DESC);
  4981. MODULE_VERSION(MWL8K_VERSION);
  4982. MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
  4983. MODULE_LICENSE("GPL");