mwl8k.c 138 KB

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