mwl8k.c 154 KB

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