main.c 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/pci_ids.h>
  18. #include <linux/if_ether.h>
  19. #include <net/cfg80211.h>
  20. #include <net/mac80211.h>
  21. #include <brcm_hw_ids.h>
  22. #include <aiutils.h>
  23. #include <chipcommon.h>
  24. #include "rate.h"
  25. #include "scb.h"
  26. #include "phy/phy_hal.h"
  27. #include "channel.h"
  28. #include "antsel.h"
  29. #include "stf.h"
  30. #include "ampdu.h"
  31. #include "mac80211_if.h"
  32. #include "ucode_loader.h"
  33. #include "main.h"
  34. #include "soc.h"
  35. #include "dma.h"
  36. #include "debug.h"
  37. #include "brcms_trace_events.h"
  38. /* watchdog timer, in unit of ms */
  39. #define TIMER_INTERVAL_WATCHDOG 1000
  40. /* radio monitor timer, in unit of ms */
  41. #define TIMER_INTERVAL_RADIOCHK 800
  42. /* beacon interval, in unit of 1024TU */
  43. #define BEACON_INTERVAL_DEFAULT 100
  44. /* n-mode support capability */
  45. /* 2x2 includes both 1x1 & 2x2 devices
  46. * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
  47. * control it independently
  48. */
  49. #define WL_11N_2x2 1
  50. #define WL_11N_3x3 3
  51. #define WL_11N_4x4 4
  52. #define EDCF_ACI_MASK 0x60
  53. #define EDCF_ACI_SHIFT 5
  54. #define EDCF_ECWMIN_MASK 0x0f
  55. #define EDCF_ECWMAX_SHIFT 4
  56. #define EDCF_AIFSN_MASK 0x0f
  57. #define EDCF_AIFSN_MAX 15
  58. #define EDCF_ECWMAX_MASK 0xf0
  59. #define EDCF_AC_BE_TXOP_STA 0x0000
  60. #define EDCF_AC_BK_TXOP_STA 0x0000
  61. #define EDCF_AC_VO_ACI_STA 0x62
  62. #define EDCF_AC_VO_ECW_STA 0x32
  63. #define EDCF_AC_VI_ACI_STA 0x42
  64. #define EDCF_AC_VI_ECW_STA 0x43
  65. #define EDCF_AC_BK_ECW_STA 0xA4
  66. #define EDCF_AC_VI_TXOP_STA 0x005e
  67. #define EDCF_AC_VO_TXOP_STA 0x002f
  68. #define EDCF_AC_BE_ACI_STA 0x03
  69. #define EDCF_AC_BE_ECW_STA 0xA4
  70. #define EDCF_AC_BK_ACI_STA 0x27
  71. #define EDCF_AC_VO_TXOP_AP 0x002f
  72. #define EDCF_TXOP2USEC(txop) ((txop) << 5)
  73. #define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
  74. #define APHY_SYMBOL_TIME 4
  75. #define APHY_PREAMBLE_TIME 16
  76. #define APHY_SIGNAL_TIME 4
  77. #define APHY_SIFS_TIME 16
  78. #define APHY_SERVICE_NBITS 16
  79. #define APHY_TAIL_NBITS 6
  80. #define BPHY_SIFS_TIME 10
  81. #define BPHY_PLCP_SHORT_TIME 96
  82. #define PREN_PREAMBLE 24
  83. #define PREN_MM_EXT 12
  84. #define PREN_PREAMBLE_EXT 4
  85. #define DOT11_MAC_HDR_LEN 24
  86. #define DOT11_ACK_LEN 10
  87. #define DOT11_BA_LEN 4
  88. #define DOT11_OFDM_SIGNAL_EXTENSION 6
  89. #define DOT11_MIN_FRAG_LEN 256
  90. #define DOT11_RTS_LEN 16
  91. #define DOT11_CTS_LEN 10
  92. #define DOT11_BA_BITMAP_LEN 128
  93. #define DOT11_MAXNUMFRAGS 16
  94. #define DOT11_MAX_FRAG_LEN 2346
  95. #define BPHY_PLCP_TIME 192
  96. #define RIFS_11N_TIME 2
  97. /* length of the BCN template area */
  98. #define BCN_TMPL_LEN 512
  99. /* brcms_bss_info flag bit values */
  100. #define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
  101. /* chip rx buffer offset */
  102. #define BRCMS_HWRXOFF 38
  103. /* rfdisable delay timer 500 ms, runs of ALP clock */
  104. #define RFDISABLE_DEFAULT 10000000
  105. #define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
  106. /* synthpu_dly times in us */
  107. #define SYNTHPU_DLY_APHY_US 3700
  108. #define SYNTHPU_DLY_BPHY_US 1050
  109. #define SYNTHPU_DLY_NPHY_US 2048
  110. #define SYNTHPU_DLY_LPPHY_US 300
  111. #define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
  112. /* Per-AC retry limit register definitions; uses defs.h bitfield macros */
  113. #define EDCF_SHORT_S 0
  114. #define EDCF_SFB_S 4
  115. #define EDCF_LONG_S 8
  116. #define EDCF_LFB_S 12
  117. #define EDCF_SHORT_M BITFIELD_MASK(4)
  118. #define EDCF_SFB_M BITFIELD_MASK(4)
  119. #define EDCF_LONG_M BITFIELD_MASK(4)
  120. #define EDCF_LFB_M BITFIELD_MASK(4)
  121. #define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
  122. #define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
  123. #define RETRY_LONG_DEF 4 /* Default Long retry count */
  124. #define RETRY_SHORT_FB 3 /* Short count for fb rate */
  125. #define RETRY_LONG_FB 2 /* Long count for fb rate */
  126. #define APHY_CWMIN 15
  127. #define PHY_CWMAX 1023
  128. #define EDCF_AIFSN_MIN 1
  129. #define FRAGNUM_MASK 0xF
  130. #define APHY_SLOT_TIME 9
  131. #define BPHY_SLOT_TIME 20
  132. #define WL_SPURAVOID_OFF 0
  133. #define WL_SPURAVOID_ON1 1
  134. #define WL_SPURAVOID_ON2 2
  135. /* invalid core flags, use the saved coreflags */
  136. #define BRCMS_USE_COREFLAGS 0xffffffff
  137. /* values for PLCPHdr_override */
  138. #define BRCMS_PLCP_AUTO -1
  139. #define BRCMS_PLCP_SHORT 0
  140. #define BRCMS_PLCP_LONG 1
  141. /* values for g_protection_override and n_protection_override */
  142. #define BRCMS_PROTECTION_AUTO -1
  143. #define BRCMS_PROTECTION_OFF 0
  144. #define BRCMS_PROTECTION_ON 1
  145. #define BRCMS_PROTECTION_MMHDR_ONLY 2
  146. #define BRCMS_PROTECTION_CTS_ONLY 3
  147. /* values for g_protection_control and n_protection_control */
  148. #define BRCMS_PROTECTION_CTL_OFF 0
  149. #define BRCMS_PROTECTION_CTL_LOCAL 1
  150. #define BRCMS_PROTECTION_CTL_OVERLAP 2
  151. /* values for n_protection */
  152. #define BRCMS_N_PROTECTION_OFF 0
  153. #define BRCMS_N_PROTECTION_OPTIONAL 1
  154. #define BRCMS_N_PROTECTION_20IN40 2
  155. #define BRCMS_N_PROTECTION_MIXEDMODE 3
  156. /* values for band specific 40MHz capabilities */
  157. #define BRCMS_N_BW_20ALL 0
  158. #define BRCMS_N_BW_40ALL 1
  159. #define BRCMS_N_BW_20IN2G_40IN5G 2
  160. /* bitflags for SGI support (sgi_rx iovar) */
  161. #define BRCMS_N_SGI_20 0x01
  162. #define BRCMS_N_SGI_40 0x02
  163. /* defines used by the nrate iovar */
  164. /* MSC in use,indicates b0-6 holds an mcs */
  165. #define NRATE_MCS_INUSE 0x00000080
  166. /* rate/mcs value */
  167. #define NRATE_RATE_MASK 0x0000007f
  168. /* stf mode mask: siso, cdd, stbc, sdm */
  169. #define NRATE_STF_MASK 0x0000ff00
  170. /* stf mode shift */
  171. #define NRATE_STF_SHIFT 8
  172. /* bit indicate to override mcs only */
  173. #define NRATE_OVERRIDE_MCS_ONLY 0x40000000
  174. #define NRATE_SGI_MASK 0x00800000 /* sgi mode */
  175. #define NRATE_SGI_SHIFT 23 /* sgi mode */
  176. #define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
  177. #define NRATE_LDPC_SHIFT 22 /* ldpc shift */
  178. #define NRATE_STF_SISO 0 /* stf mode SISO */
  179. #define NRATE_STF_CDD 1 /* stf mode CDD */
  180. #define NRATE_STF_STBC 2 /* stf mode STBC */
  181. #define NRATE_STF_SDM 3 /* stf mode SDM */
  182. #define MAX_DMA_SEGS 4
  183. /* # of entries in Tx FIFO */
  184. #define NTXD 64
  185. /* Max # of entries in Rx FIFO based on 4kb page size */
  186. #define NRXD 256
  187. /* Amount of headroom to leave in Tx FIFO */
  188. #define TX_HEADROOM 4
  189. /* try to keep this # rbufs posted to the chip */
  190. #define NRXBUFPOST 32
  191. /* max # frames to process in brcms_c_recv() */
  192. #define RXBND 8
  193. /* max # tx status to process in wlc_txstatus() */
  194. #define TXSBND 8
  195. /* brcmu_format_flags() bit description structure */
  196. struct brcms_c_bit_desc {
  197. u32 bit;
  198. const char *name;
  199. };
  200. /*
  201. * The following table lists the buffer memory allocated to xmt fifos in HW.
  202. * the size is in units of 256bytes(one block), total size is HW dependent
  203. * ucode has default fifo partition, sw can overwrite if necessary
  204. *
  205. * This is documented in twiki under the topic UcodeTxFifo. Please ensure
  206. * the twiki is updated before making changes.
  207. */
  208. /* Starting corerev for the fifo size table */
  209. #define XMTFIFOTBL_STARTREV 17
  210. struct d11init {
  211. __le16 addr;
  212. __le16 size;
  213. __le32 value;
  214. };
  215. struct edcf_acparam {
  216. u8 ACI;
  217. u8 ECW;
  218. u16 TXOP;
  219. } __packed;
  220. /* debug/trace */
  221. uint brcm_msg_level;
  222. /* TX FIFO number to WME/802.1E Access Category */
  223. static const u8 wme_fifo2ac[] = {
  224. IEEE80211_AC_BK,
  225. IEEE80211_AC_BE,
  226. IEEE80211_AC_VI,
  227. IEEE80211_AC_VO,
  228. IEEE80211_AC_BE,
  229. IEEE80211_AC_BE
  230. };
  231. /* ieee80211 Access Category to TX FIFO number */
  232. static const u8 wme_ac2fifo[] = {
  233. TX_AC_VO_FIFO,
  234. TX_AC_VI_FIFO,
  235. TX_AC_BE_FIFO,
  236. TX_AC_BK_FIFO
  237. };
  238. static const u16 xmtfifo_sz[][NFIFO] = {
  239. /* corerev 17: 5120, 49152, 49152, 5376, 4352, 1280 */
  240. {20, 192, 192, 21, 17, 5},
  241. /* corerev 18: */
  242. {0, 0, 0, 0, 0, 0},
  243. /* corerev 19: */
  244. {0, 0, 0, 0, 0, 0},
  245. /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
  246. {20, 192, 192, 21, 17, 5},
  247. /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
  248. {9, 58, 22, 14, 14, 5},
  249. /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
  250. {20, 192, 192, 21, 17, 5},
  251. /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
  252. {20, 192, 192, 21, 17, 5},
  253. /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
  254. {9, 58, 22, 14, 14, 5},
  255. /* corerev 25: */
  256. {0, 0, 0, 0, 0, 0},
  257. /* corerev 26: */
  258. {0, 0, 0, 0, 0, 0},
  259. /* corerev 27: */
  260. {0, 0, 0, 0, 0, 0},
  261. /* corerev 28: 2304, 14848, 5632, 3584, 3584, 1280 */
  262. {9, 58, 22, 14, 14, 5},
  263. };
  264. #ifdef DEBUG
  265. static const char * const fifo_names[] = {
  266. "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
  267. #else
  268. static const char fifo_names[6][0];
  269. #endif
  270. #ifdef DEBUG
  271. /* pointer to most recently allocated wl/wlc */
  272. static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
  273. #endif
  274. /* Mapping of ieee80211 AC numbers to tx fifos */
  275. static const u8 ac_to_fifo_mapping[IEEE80211_NUM_ACS] = {
  276. [IEEE80211_AC_VO] = TX_AC_VO_FIFO,
  277. [IEEE80211_AC_VI] = TX_AC_VI_FIFO,
  278. [IEEE80211_AC_BE] = TX_AC_BE_FIFO,
  279. [IEEE80211_AC_BK] = TX_AC_BK_FIFO,
  280. };
  281. /* Mapping of tx fifos to ieee80211 AC numbers */
  282. static const u8 fifo_to_ac_mapping[IEEE80211_NUM_ACS] = {
  283. [TX_AC_BK_FIFO] = IEEE80211_AC_BK,
  284. [TX_AC_BE_FIFO] = IEEE80211_AC_BE,
  285. [TX_AC_VI_FIFO] = IEEE80211_AC_VI,
  286. [TX_AC_VO_FIFO] = IEEE80211_AC_VO,
  287. };
  288. static u8 brcms_ac_to_fifo(u8 ac)
  289. {
  290. if (ac >= ARRAY_SIZE(ac_to_fifo_mapping))
  291. return TX_AC_BE_FIFO;
  292. return ac_to_fifo_mapping[ac];
  293. }
  294. static u8 brcms_fifo_to_ac(u8 fifo)
  295. {
  296. if (fifo >= ARRAY_SIZE(fifo_to_ac_mapping))
  297. return IEEE80211_AC_BE;
  298. return fifo_to_ac_mapping[fifo];
  299. }
  300. /* Find basic rate for a given rate */
  301. static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
  302. {
  303. if (is_mcs_rate(rspec))
  304. return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
  305. .leg_ofdm];
  306. return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
  307. }
  308. static u16 frametype(u32 rspec, u8 mimoframe)
  309. {
  310. if (is_mcs_rate(rspec))
  311. return mimoframe;
  312. return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
  313. }
  314. /* currently the best mechanism for determining SIFS is the band in use */
  315. static u16 get_sifs(struct brcms_band *band)
  316. {
  317. return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
  318. BPHY_SIFS_TIME;
  319. }
  320. /*
  321. * Detect Card removed.
  322. * Even checking an sbconfig register read will not false trigger when the core
  323. * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
  324. * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
  325. * reg with fixed 0/1 pattern (some platforms return all 0).
  326. * If clocks are present, call the sb routine which will figure out if the
  327. * device is removed.
  328. */
  329. static bool brcms_deviceremoved(struct brcms_c_info *wlc)
  330. {
  331. u32 macctrl;
  332. if (!wlc->hw->clk)
  333. return ai_deviceremoved(wlc->hw->sih);
  334. macctrl = bcma_read32(wlc->hw->d11core,
  335. D11REGOFFS(maccontrol));
  336. return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
  337. }
  338. /* sum the individual fifo tx pending packet counts */
  339. static int brcms_txpktpendtot(struct brcms_c_info *wlc)
  340. {
  341. int i;
  342. int pending = 0;
  343. for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
  344. if (wlc->hw->di[i])
  345. pending += dma_txpending(wlc->hw->di[i]);
  346. return pending;
  347. }
  348. static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
  349. {
  350. return wlc->pub->_nbands > 1 && !wlc->bandlocked;
  351. }
  352. static int brcms_chspec_bw(u16 chanspec)
  353. {
  354. if (CHSPEC_IS40(chanspec))
  355. return BRCMS_40_MHZ;
  356. if (CHSPEC_IS20(chanspec))
  357. return BRCMS_20_MHZ;
  358. return BRCMS_10_MHZ;
  359. }
  360. static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
  361. {
  362. if (cfg == NULL)
  363. return;
  364. kfree(cfg->current_bss);
  365. kfree(cfg);
  366. }
  367. static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
  368. {
  369. if (wlc == NULL)
  370. return;
  371. brcms_c_bsscfg_mfree(wlc->bsscfg);
  372. kfree(wlc->pub);
  373. kfree(wlc->modulecb);
  374. kfree(wlc->default_bss);
  375. kfree(wlc->protection);
  376. kfree(wlc->stf);
  377. kfree(wlc->bandstate[0]);
  378. kfree(wlc->corestate->macstat_snapshot);
  379. kfree(wlc->corestate);
  380. kfree(wlc->hw->bandstate[0]);
  381. kfree(wlc->hw);
  382. /* free the wlc */
  383. kfree(wlc);
  384. wlc = NULL;
  385. }
  386. static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
  387. {
  388. struct brcms_bss_cfg *cfg;
  389. cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
  390. if (cfg == NULL)
  391. goto fail;
  392. cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  393. if (cfg->current_bss == NULL)
  394. goto fail;
  395. return cfg;
  396. fail:
  397. brcms_c_bsscfg_mfree(cfg);
  398. return NULL;
  399. }
  400. static struct brcms_c_info *
  401. brcms_c_attach_malloc(uint unit, uint *err, uint devid)
  402. {
  403. struct brcms_c_info *wlc;
  404. wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
  405. if (wlc == NULL) {
  406. *err = 1002;
  407. goto fail;
  408. }
  409. /* allocate struct brcms_c_pub state structure */
  410. wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
  411. if (wlc->pub == NULL) {
  412. *err = 1003;
  413. goto fail;
  414. }
  415. wlc->pub->wlc = wlc;
  416. /* allocate struct brcms_hardware state structure */
  417. wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
  418. if (wlc->hw == NULL) {
  419. *err = 1005;
  420. goto fail;
  421. }
  422. wlc->hw->wlc = wlc;
  423. wlc->hw->bandstate[0] =
  424. kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
  425. if (wlc->hw->bandstate[0] == NULL) {
  426. *err = 1006;
  427. goto fail;
  428. } else {
  429. int i;
  430. for (i = 1; i < MAXBANDS; i++)
  431. wlc->hw->bandstate[i] = (struct brcms_hw_band *)
  432. ((unsigned long)wlc->hw->bandstate[0] +
  433. (sizeof(struct brcms_hw_band) * i));
  434. }
  435. wlc->modulecb =
  436. kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
  437. if (wlc->modulecb == NULL) {
  438. *err = 1009;
  439. goto fail;
  440. }
  441. wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  442. if (wlc->default_bss == NULL) {
  443. *err = 1010;
  444. goto fail;
  445. }
  446. wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
  447. if (wlc->bsscfg == NULL) {
  448. *err = 1011;
  449. goto fail;
  450. }
  451. wlc->protection = kzalloc(sizeof(struct brcms_protection),
  452. GFP_ATOMIC);
  453. if (wlc->protection == NULL) {
  454. *err = 1016;
  455. goto fail;
  456. }
  457. wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
  458. if (wlc->stf == NULL) {
  459. *err = 1017;
  460. goto fail;
  461. }
  462. wlc->bandstate[0] =
  463. kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
  464. if (wlc->bandstate[0] == NULL) {
  465. *err = 1025;
  466. goto fail;
  467. } else {
  468. int i;
  469. for (i = 1; i < MAXBANDS; i++)
  470. wlc->bandstate[i] = (struct brcms_band *)
  471. ((unsigned long)wlc->bandstate[0]
  472. + (sizeof(struct brcms_band)*i));
  473. }
  474. wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
  475. if (wlc->corestate == NULL) {
  476. *err = 1026;
  477. goto fail;
  478. }
  479. wlc->corestate->macstat_snapshot =
  480. kzalloc(sizeof(struct macstat), GFP_ATOMIC);
  481. if (wlc->corestate->macstat_snapshot == NULL) {
  482. *err = 1027;
  483. goto fail;
  484. }
  485. return wlc;
  486. fail:
  487. brcms_c_detach_mfree(wlc);
  488. return NULL;
  489. }
  490. /*
  491. * Update the slot timing for standard 11b/g (20us slots)
  492. * or shortslot 11g (9us slots)
  493. * The PSM needs to be suspended for this call.
  494. */
  495. static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
  496. bool shortslot)
  497. {
  498. struct bcma_device *core = wlc_hw->d11core;
  499. if (shortslot) {
  500. /* 11g short slot: 11a timing */
  501. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
  502. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
  503. } else {
  504. /* 11g long slot: 11b timing */
  505. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
  506. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
  507. }
  508. }
  509. /*
  510. * calculate frame duration of a given rate and length, return
  511. * time in usec unit
  512. */
  513. static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
  514. u8 preamble_type, uint mac_len)
  515. {
  516. uint nsyms, dur = 0, Ndps, kNdps;
  517. uint rate = rspec2rate(ratespec);
  518. if (rate == 0) {
  519. brcms_err(wlc->hw->d11core, "wl%d: WAR: using rate of 1 mbps\n",
  520. wlc->pub->unit);
  521. rate = BRCM_RATE_1M;
  522. }
  523. if (is_mcs_rate(ratespec)) {
  524. uint mcs = ratespec & RSPEC_RATE_MASK;
  525. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  526. dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  527. if (preamble_type == BRCMS_MM_PREAMBLE)
  528. dur += PREN_MM_EXT;
  529. /* 1000Ndbps = kbps * 4 */
  530. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  531. rspec_issgi(ratespec)) * 4;
  532. if (rspec_stc(ratespec) == 0)
  533. nsyms =
  534. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  535. APHY_TAIL_NBITS) * 1000, kNdps);
  536. else
  537. /* STBC needs to have even number of symbols */
  538. nsyms =
  539. 2 *
  540. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  541. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  542. dur += APHY_SYMBOL_TIME * nsyms;
  543. if (wlc->band->bandtype == BRCM_BAND_2G)
  544. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  545. } else if (is_ofdm_rate(rate)) {
  546. dur = APHY_PREAMBLE_TIME;
  547. dur += APHY_SIGNAL_TIME;
  548. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  549. Ndps = rate * 2;
  550. /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
  551. nsyms =
  552. CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
  553. Ndps);
  554. dur += APHY_SYMBOL_TIME * nsyms;
  555. if (wlc->band->bandtype == BRCM_BAND_2G)
  556. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  557. } else {
  558. /*
  559. * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
  560. * will divide out
  561. */
  562. mac_len = mac_len * 8 * 2;
  563. /* calc ceiling of bits/rate = microseconds of air time */
  564. dur = (mac_len + rate - 1) / rate;
  565. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  566. dur += BPHY_PLCP_SHORT_TIME;
  567. else
  568. dur += BPHY_PLCP_TIME;
  569. }
  570. return dur;
  571. }
  572. static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
  573. const struct d11init *inits)
  574. {
  575. struct bcma_device *core = wlc_hw->d11core;
  576. int i;
  577. uint offset;
  578. u16 size;
  579. u32 value;
  580. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  581. for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
  582. size = le16_to_cpu(inits[i].size);
  583. offset = le16_to_cpu(inits[i].addr);
  584. value = le32_to_cpu(inits[i].value);
  585. if (size == 2)
  586. bcma_write16(core, offset, value);
  587. else if (size == 4)
  588. bcma_write32(core, offset, value);
  589. else
  590. break;
  591. }
  592. }
  593. static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
  594. {
  595. u8 idx;
  596. u16 addr[] = {
  597. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  598. M_HOST_FLAGS5
  599. };
  600. for (idx = 0; idx < MHFMAX; idx++)
  601. brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
  602. }
  603. static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
  604. {
  605. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  606. /* init microcode host flags */
  607. brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
  608. /* do band-specific ucode IHR, SHM, and SCR inits */
  609. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  610. if (BRCMS_ISNPHY(wlc_hw->band))
  611. brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
  612. else
  613. brcms_err(wlc_hw->d11core,
  614. "%s: wl%d: unsupported phy in corerev %d\n",
  615. __func__, wlc_hw->unit,
  616. wlc_hw->corerev);
  617. } else {
  618. if (D11REV_IS(wlc_hw->corerev, 24)) {
  619. if (BRCMS_ISLCNPHY(wlc_hw->band))
  620. brcms_c_write_inits(wlc_hw,
  621. ucode->d11lcn0bsinitvals24);
  622. else
  623. brcms_err(wlc_hw->d11core,
  624. "%s: wl%d: unsupported phy in core rev %d\n",
  625. __func__, wlc_hw->unit,
  626. wlc_hw->corerev);
  627. } else {
  628. brcms_err(wlc_hw->d11core,
  629. "%s: wl%d: unsupported corerev %d\n",
  630. __func__, wlc_hw->unit, wlc_hw->corerev);
  631. }
  632. }
  633. }
  634. static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
  635. {
  636. struct bcma_device *core = wlc_hw->d11core;
  637. u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
  638. bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
  639. }
  640. static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
  641. {
  642. brcms_dbg_info(wlc_hw->d11core, "wl%d: clk %d\n", wlc_hw->unit, clk);
  643. wlc_hw->phyclk = clk;
  644. if (OFF == clk) { /* clear gmode bit, put phy into reset */
  645. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
  646. (SICF_PRST | SICF_FGC));
  647. udelay(1);
  648. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
  649. udelay(1);
  650. } else { /* take phy out of reset */
  651. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
  652. udelay(1);
  653. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  654. udelay(1);
  655. }
  656. }
  657. /* low-level band switch utility routine */
  658. static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
  659. {
  660. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
  661. bandunit);
  662. wlc_hw->band = wlc_hw->bandstate[bandunit];
  663. /*
  664. * BMAC_NOTE:
  665. * until we eliminate need for wlc->band refs in low level code
  666. */
  667. wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
  668. /* set gmode core flag */
  669. if (wlc_hw->sbclk && !wlc_hw->noreset) {
  670. u32 gmode = 0;
  671. if (bandunit == 0)
  672. gmode = SICF_GMODE;
  673. brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
  674. }
  675. }
  676. /* switch to new band but leave it inactive */
  677. static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
  678. {
  679. struct brcms_hardware *wlc_hw = wlc->hw;
  680. u32 macintmask;
  681. u32 macctrl;
  682. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  683. macctrl = bcma_read32(wlc_hw->d11core,
  684. D11REGOFFS(maccontrol));
  685. WARN_ON((macctrl & MCTL_EN_MAC) != 0);
  686. /* disable interrupts */
  687. macintmask = brcms_intrsoff(wlc->wl);
  688. /* radio off */
  689. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  690. brcms_b_core_phy_clk(wlc_hw, OFF);
  691. brcms_c_setxband(wlc_hw, bandunit);
  692. return macintmask;
  693. }
  694. /* process an individual struct tx_status */
  695. static bool
  696. brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
  697. {
  698. struct sk_buff *p = NULL;
  699. uint queue = NFIFO;
  700. struct dma_pub *dma = NULL;
  701. struct d11txh *txh = NULL;
  702. struct scb *scb = NULL;
  703. bool free_pdu;
  704. int tx_rts, tx_frame_count, tx_rts_count;
  705. uint totlen, supr_status;
  706. bool lastframe;
  707. struct ieee80211_hdr *h;
  708. u16 mcl;
  709. struct ieee80211_tx_info *tx_info;
  710. struct ieee80211_tx_rate *txrate;
  711. int i;
  712. bool fatal = true;
  713. trace_brcms_txstatus(&wlc->hw->d11core->dev, txs->framelen,
  714. txs->frameid, txs->status, txs->lasttxtime,
  715. txs->sequence, txs->phyerr, txs->ackphyrxsh);
  716. /* discard intermediate indications for ucode with one legitimate case:
  717. * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
  718. * but the subsequent tx of DATA failed. so it will start rts/cts
  719. * from the beginning (resetting the rts transmission count)
  720. */
  721. if (!(txs->status & TX_STATUS_AMPDU)
  722. && (txs->status & TX_STATUS_INTERMEDIATE)) {
  723. brcms_dbg_tx(wlc->hw->d11core, "INTERMEDIATE but not AMPDU\n");
  724. fatal = false;
  725. goto out;
  726. }
  727. queue = txs->frameid & TXFID_QUEUE_MASK;
  728. if (queue >= NFIFO) {
  729. brcms_err(wlc->hw->d11core, "queue %u >= NFIFO\n", queue);
  730. goto out;
  731. }
  732. dma = wlc->hw->di[queue];
  733. p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
  734. if (p == NULL) {
  735. brcms_err(wlc->hw->d11core, "dma_getnexttxp returned null!\n");
  736. goto out;
  737. }
  738. txh = (struct d11txh *) (p->data);
  739. mcl = le16_to_cpu(txh->MacTxControlLow);
  740. if (txs->phyerr)
  741. brcms_err(wlc->hw->d11core, "phyerr 0x%x, rate 0x%x\n",
  742. txs->phyerr, txh->MainRates);
  743. if (txs->frameid != le16_to_cpu(txh->TxFrameID)) {
  744. brcms_err(wlc->hw->d11core, "frameid != txh->TxFrameID\n");
  745. goto out;
  746. }
  747. tx_info = IEEE80211_SKB_CB(p);
  748. h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
  749. if (tx_info->rate_driver_data[0])
  750. scb = &wlc->pri_scb;
  751. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  752. brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
  753. fatal = false;
  754. goto out;
  755. }
  756. /*
  757. * brcms_c_ampdu_dotxstatus() will trace tx descriptors for AMPDU
  758. * frames; this traces them for the rest.
  759. */
  760. trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));
  761. supr_status = txs->status & TX_STATUS_SUPR_MASK;
  762. if (supr_status == TX_STATUS_SUPR_BADCH) {
  763. unsigned xfts = le16_to_cpu(txh->XtraFrameTypes);
  764. brcms_dbg_tx(wlc->hw->d11core,
  765. "Pkt tx suppressed, dest chan %u, current %d\n",
  766. (xfts >> XFTS_CHANNEL_SHIFT) & 0xff,
  767. CHSPEC_CHANNEL(wlc->default_bss->chanspec));
  768. }
  769. tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
  770. tx_frame_count =
  771. (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
  772. tx_rts_count =
  773. (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
  774. lastframe = !ieee80211_has_morefrags(h->frame_control);
  775. if (!lastframe) {
  776. brcms_err(wlc->hw->d11core, "Not last frame!\n");
  777. } else {
  778. /*
  779. * Set information to be consumed by Minstrel ht.
  780. *
  781. * The "fallback limit" is the number of tx attempts a given
  782. * MPDU is sent at the "primary" rate. Tx attempts beyond that
  783. * limit are sent at the "secondary" rate.
  784. * A 'short frame' does not exceed RTS treshold.
  785. */
  786. u16 sfbl, /* Short Frame Rate Fallback Limit */
  787. lfbl, /* Long Frame Rate Fallback Limit */
  788. fbl;
  789. if (queue < IEEE80211_NUM_ACS) {
  790. sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  791. EDCF_SFB);
  792. lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  793. EDCF_LFB);
  794. } else {
  795. sfbl = wlc->SFBL;
  796. lfbl = wlc->LFBL;
  797. }
  798. txrate = tx_info->status.rates;
  799. if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  800. fbl = lfbl;
  801. else
  802. fbl = sfbl;
  803. ieee80211_tx_info_clear_status(tx_info);
  804. if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
  805. /*
  806. * rate selection requested a fallback rate
  807. * and we used it
  808. */
  809. txrate[0].count = fbl;
  810. txrate[1].count = tx_frame_count - fbl;
  811. } else {
  812. /*
  813. * rate selection did not request fallback rate, or
  814. * we didn't need it
  815. */
  816. txrate[0].count = tx_frame_count;
  817. /*
  818. * rc80211_minstrel.c:minstrel_tx_status() expects
  819. * unused rates to be marked with idx = -1
  820. */
  821. txrate[1].idx = -1;
  822. txrate[1].count = 0;
  823. }
  824. /* clear the rest of the rates */
  825. for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
  826. txrate[i].idx = -1;
  827. txrate[i].count = 0;
  828. }
  829. if (txs->status & TX_STATUS_ACK_RCV)
  830. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  831. }
  832. totlen = p->len;
  833. free_pdu = true;
  834. if (lastframe) {
  835. /* remove PLCP & Broadcom tx descriptor header */
  836. skb_pull(p, D11_PHY_HDR_LEN);
  837. skb_pull(p, D11_TXH_LEN);
  838. ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
  839. } else {
  840. brcms_err(wlc->hw->d11core,
  841. "%s: Not last frame => not calling tx_status\n",
  842. __func__);
  843. }
  844. fatal = false;
  845. out:
  846. if (fatal) {
  847. if (txh)
  848. trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,
  849. sizeof(*txh));
  850. if (p)
  851. brcmu_pkt_buf_free_skb(p);
  852. }
  853. if (dma && queue < NFIFO) {
  854. u16 ac_queue = brcms_fifo_to_ac(queue);
  855. if (dma->txavail > TX_HEADROOM && queue < TX_BCMC_FIFO &&
  856. ieee80211_queue_stopped(wlc->pub->ieee_hw, ac_queue))
  857. ieee80211_wake_queue(wlc->pub->ieee_hw, ac_queue);
  858. dma_kick_tx(dma);
  859. }
  860. return fatal;
  861. }
  862. /* process tx completion events in BMAC
  863. * Return true if more tx status need to be processed. false otherwise.
  864. */
  865. static bool
  866. brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
  867. {
  868. struct bcma_device *core;
  869. struct tx_status txstatus, *txs;
  870. u32 s1, s2;
  871. uint n = 0;
  872. /*
  873. * Param 'max_tx_num' indicates max. # tx status to process before
  874. * break out.
  875. */
  876. uint max_tx_num = bound ? TXSBND : -1;
  877. txs = &txstatus;
  878. core = wlc_hw->d11core;
  879. *fatal = false;
  880. while (n < max_tx_num) {
  881. s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
  882. if (s1 == 0xffffffff) {
  883. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  884. __func__);
  885. *fatal = true;
  886. return false;
  887. }
  888. /* only process when valid */
  889. if (!(s1 & TXS_V))
  890. break;
  891. s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
  892. txs->status = s1 & TXS_STATUS_MASK;
  893. txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
  894. txs->sequence = s2 & TXS_SEQ_MASK;
  895. txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
  896. txs->lasttxtime = 0;
  897. *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
  898. if (*fatal == true)
  899. return false;
  900. n++;
  901. }
  902. return n >= max_tx_num;
  903. }
  904. static void brcms_c_tbtt(struct brcms_c_info *wlc)
  905. {
  906. if (!wlc->bsscfg->BSS)
  907. /*
  908. * DirFrmQ is now valid...defer setting until end
  909. * of ATIM window
  910. */
  911. wlc->qvalid |= MCMD_DIRFRMQVAL;
  912. }
  913. /* set initial host flags value */
  914. static void
  915. brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
  916. {
  917. struct brcms_hardware *wlc_hw = wlc->hw;
  918. memset(mhfs, 0, MHFMAX * sizeof(u16));
  919. mhfs[MHF2] |= mhf2_init;
  920. /* prohibit use of slowclock on multifunction boards */
  921. if (wlc_hw->boardflags & BFL_NOPLLDOWN)
  922. mhfs[MHF1] |= MHF1_FORCEFASTCLK;
  923. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
  924. mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
  925. mhfs[MHF1] |= MHF1_IQSWAP_WAR;
  926. }
  927. }
  928. static uint
  929. dmareg(uint direction, uint fifonum)
  930. {
  931. if (direction == DMA_TX)
  932. return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
  933. return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
  934. }
  935. static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
  936. {
  937. uint i;
  938. char name[8];
  939. /*
  940. * ucode host flag 2 needed for pio mode, independent of band and fifo
  941. */
  942. u16 pio_mhf2 = 0;
  943. struct brcms_hardware *wlc_hw = wlc->hw;
  944. uint unit = wlc_hw->unit;
  945. /* name and offsets for dma_attach */
  946. snprintf(name, sizeof(name), "wl%d", unit);
  947. if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
  948. int dma_attach_err = 0;
  949. /*
  950. * FIFO 0
  951. * TX: TX_AC_BK_FIFO (TX AC Background data packets)
  952. * RX: RX_FIFO (RX data packets)
  953. */
  954. wlc_hw->di[0] = dma_attach(name, wlc,
  955. (wme ? dmareg(DMA_TX, 0) : 0),
  956. dmareg(DMA_RX, 0),
  957. (wme ? NTXD : 0), NRXD,
  958. RXBUFSZ, -1, NRXBUFPOST,
  959. BRCMS_HWRXOFF);
  960. dma_attach_err |= (NULL == wlc_hw->di[0]);
  961. /*
  962. * FIFO 1
  963. * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
  964. * (legacy) TX_DATA_FIFO (TX data packets)
  965. * RX: UNUSED
  966. */
  967. wlc_hw->di[1] = dma_attach(name, wlc,
  968. dmareg(DMA_TX, 1), 0,
  969. NTXD, 0, 0, -1, 0, 0);
  970. dma_attach_err |= (NULL == wlc_hw->di[1]);
  971. /*
  972. * FIFO 2
  973. * TX: TX_AC_VI_FIFO (TX AC Video data packets)
  974. * RX: UNUSED
  975. */
  976. wlc_hw->di[2] = dma_attach(name, wlc,
  977. dmareg(DMA_TX, 2), 0,
  978. NTXD, 0, 0, -1, 0, 0);
  979. dma_attach_err |= (NULL == wlc_hw->di[2]);
  980. /*
  981. * FIFO 3
  982. * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
  983. * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
  984. */
  985. wlc_hw->di[3] = dma_attach(name, wlc,
  986. dmareg(DMA_TX, 3),
  987. 0, NTXD, 0, 0, -1,
  988. 0, 0);
  989. dma_attach_err |= (NULL == wlc_hw->di[3]);
  990. /* Cleaner to leave this as if with AP defined */
  991. if (dma_attach_err) {
  992. brcms_err(wlc_hw->d11core,
  993. "wl%d: wlc_attach: dma_attach failed\n",
  994. unit);
  995. return false;
  996. }
  997. /* get pointer to dma engine tx flow control variable */
  998. for (i = 0; i < NFIFO; i++)
  999. if (wlc_hw->di[i])
  1000. wlc_hw->txavail[i] =
  1001. (uint *) dma_getvar(wlc_hw->di[i],
  1002. "&txavail");
  1003. }
  1004. /* initial ucode host flags */
  1005. brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
  1006. return true;
  1007. }
  1008. static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
  1009. {
  1010. uint j;
  1011. for (j = 0; j < NFIFO; j++) {
  1012. if (wlc_hw->di[j]) {
  1013. dma_detach(wlc_hw->di[j]);
  1014. wlc_hw->di[j] = NULL;
  1015. }
  1016. }
  1017. }
  1018. /*
  1019. * Initialize brcms_c_info default values ...
  1020. * may get overrides later in this function
  1021. * BMAC_NOTES, move low out and resolve the dangling ones
  1022. */
  1023. static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
  1024. {
  1025. struct brcms_c_info *wlc = wlc_hw->wlc;
  1026. /* set default sw macintmask value */
  1027. wlc->defmacintmask = DEF_MACINTMASK;
  1028. /* various 802.11g modes */
  1029. wlc_hw->shortslot = false;
  1030. wlc_hw->SFBL = RETRY_SHORT_FB;
  1031. wlc_hw->LFBL = RETRY_LONG_FB;
  1032. /* default mac retry limits */
  1033. wlc_hw->SRL = RETRY_SHORT_DEF;
  1034. wlc_hw->LRL = RETRY_LONG_DEF;
  1035. wlc_hw->chanspec = ch20mhz_chspec(1);
  1036. }
  1037. static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
  1038. {
  1039. /* delay before first read of ucode state */
  1040. udelay(40);
  1041. /* wait until ucode is no longer asleep */
  1042. SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
  1043. DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
  1044. }
  1045. /* control chip clock to save power, enable dynamic clock or force fast clock */
  1046. static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, enum bcma_clkmode mode)
  1047. {
  1048. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
  1049. /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
  1050. * on backplane, but mac core will still run on ALP(not HT) when
  1051. * it enters powersave mode, which means the FCA bit may not be
  1052. * set. Should wakeup mac if driver wants it to run on HT.
  1053. */
  1054. if (wlc_hw->clk) {
  1055. if (mode == BCMA_CLKMODE_FAST) {
  1056. bcma_set32(wlc_hw->d11core,
  1057. D11REGOFFS(clk_ctl_st),
  1058. CCS_FORCEHT);
  1059. udelay(64);
  1060. SPINWAIT(
  1061. ((bcma_read32(wlc_hw->d11core,
  1062. D11REGOFFS(clk_ctl_st)) &
  1063. CCS_HTAVAIL) == 0),
  1064. PMU_MAX_TRANSITION_DLY);
  1065. WARN_ON(!(bcma_read32(wlc_hw->d11core,
  1066. D11REGOFFS(clk_ctl_st)) &
  1067. CCS_HTAVAIL));
  1068. } else {
  1069. if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
  1070. (bcma_read32(wlc_hw->d11core,
  1071. D11REGOFFS(clk_ctl_st)) &
  1072. (CCS_FORCEHT | CCS_HTAREQ)))
  1073. SPINWAIT(
  1074. ((bcma_read32(wlc_hw->d11core,
  1075. offsetof(struct d11regs,
  1076. clk_ctl_st)) &
  1077. CCS_HTAVAIL) == 0),
  1078. PMU_MAX_TRANSITION_DLY);
  1079. bcma_mask32(wlc_hw->d11core,
  1080. D11REGOFFS(clk_ctl_st),
  1081. ~CCS_FORCEHT);
  1082. }
  1083. }
  1084. wlc_hw->forcefastclk = (mode == BCMA_CLKMODE_FAST);
  1085. } else {
  1086. /* old chips w/o PMU, force HT through cc,
  1087. * then use FCA to verify mac is running fast clock
  1088. */
  1089. wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
  1090. /* check fast clock is available (if core is not in reset) */
  1091. if (wlc_hw->forcefastclk && wlc_hw->clk)
  1092. WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
  1093. SISF_FCLKA));
  1094. /*
  1095. * keep the ucode wake bit on if forcefastclk is on since we
  1096. * do not want ucode to put us back to slow clock when it dozes
  1097. * for PM mode. Code below matches the wake override bit with
  1098. * current forcefastclk state. Only setting bit in wake_override
  1099. * instead of waking ucode immediately since old code had this
  1100. * behavior. Older code set wlc->forcefastclk but only had the
  1101. * wake happen if the wakup_ucode work (protected by an up
  1102. * check) was executed just below.
  1103. */
  1104. if (wlc_hw->forcefastclk)
  1105. mboolset(wlc_hw->wake_override,
  1106. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1107. else
  1108. mboolclr(wlc_hw->wake_override,
  1109. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1110. }
  1111. }
  1112. /* set or clear ucode host flag bits
  1113. * it has an optimization for no-change write
  1114. * it only writes through shared memory when the core has clock;
  1115. * pre-CLK changes should use wlc_write_mhf to get around the optimization
  1116. *
  1117. *
  1118. * bands values are: BRCM_BAND_AUTO <--- Current band only
  1119. * BRCM_BAND_5G <--- 5G band only
  1120. * BRCM_BAND_2G <--- 2G band only
  1121. * BRCM_BAND_ALL <--- All bands
  1122. */
  1123. void
  1124. brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
  1125. int bands)
  1126. {
  1127. u16 save;
  1128. u16 addr[MHFMAX] = {
  1129. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  1130. M_HOST_FLAGS5
  1131. };
  1132. struct brcms_hw_band *band;
  1133. if ((val & ~mask) || idx >= MHFMAX)
  1134. return; /* error condition */
  1135. switch (bands) {
  1136. /* Current band only or all bands,
  1137. * then set the band to current band
  1138. */
  1139. case BRCM_BAND_AUTO:
  1140. case BRCM_BAND_ALL:
  1141. band = wlc_hw->band;
  1142. break;
  1143. case BRCM_BAND_5G:
  1144. band = wlc_hw->bandstate[BAND_5G_INDEX];
  1145. break;
  1146. case BRCM_BAND_2G:
  1147. band = wlc_hw->bandstate[BAND_2G_INDEX];
  1148. break;
  1149. default:
  1150. band = NULL; /* error condition */
  1151. }
  1152. if (band) {
  1153. save = band->mhfs[idx];
  1154. band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
  1155. /* optimization: only write through if changed, and
  1156. * changed band is the current band
  1157. */
  1158. if (wlc_hw->clk && (band->mhfs[idx] != save)
  1159. && (band == wlc_hw->band))
  1160. brcms_b_write_shm(wlc_hw, addr[idx],
  1161. (u16) band->mhfs[idx]);
  1162. }
  1163. if (bands == BRCM_BAND_ALL) {
  1164. wlc_hw->bandstate[0]->mhfs[idx] =
  1165. (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
  1166. wlc_hw->bandstate[1]->mhfs[idx] =
  1167. (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
  1168. }
  1169. }
  1170. /* set the maccontrol register to desired reset state and
  1171. * initialize the sw cache of the register
  1172. */
  1173. static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
  1174. {
  1175. /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
  1176. wlc_hw->maccontrol = 0;
  1177. wlc_hw->suspended_fifos = 0;
  1178. wlc_hw->wake_override = 0;
  1179. wlc_hw->mute_override = 0;
  1180. brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
  1181. }
  1182. /*
  1183. * write the software state of maccontrol and
  1184. * overrides to the maccontrol register
  1185. */
  1186. static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
  1187. {
  1188. u32 maccontrol = wlc_hw->maccontrol;
  1189. /* OR in the wake bit if overridden */
  1190. if (wlc_hw->wake_override)
  1191. maccontrol |= MCTL_WAKE;
  1192. /* set AP and INFRA bits for mute if needed */
  1193. if (wlc_hw->mute_override) {
  1194. maccontrol &= ~(MCTL_AP);
  1195. maccontrol |= MCTL_INFRA;
  1196. }
  1197. bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
  1198. maccontrol);
  1199. }
  1200. /* set or clear maccontrol bits */
  1201. void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
  1202. {
  1203. u32 maccontrol;
  1204. u32 new_maccontrol;
  1205. if (val & ~mask)
  1206. return; /* error condition */
  1207. maccontrol = wlc_hw->maccontrol;
  1208. new_maccontrol = (maccontrol & ~mask) | val;
  1209. /* if the new maccontrol value is the same as the old, nothing to do */
  1210. if (new_maccontrol == maccontrol)
  1211. return;
  1212. /* something changed, cache the new value */
  1213. wlc_hw->maccontrol = new_maccontrol;
  1214. /* write the new values with overrides applied */
  1215. brcms_c_mctrl_write(wlc_hw);
  1216. }
  1217. void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
  1218. u32 override_bit)
  1219. {
  1220. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
  1221. mboolset(wlc_hw->wake_override, override_bit);
  1222. return;
  1223. }
  1224. mboolset(wlc_hw->wake_override, override_bit);
  1225. brcms_c_mctrl_write(wlc_hw);
  1226. brcms_b_wait_for_wake(wlc_hw);
  1227. }
  1228. void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
  1229. u32 override_bit)
  1230. {
  1231. mboolclr(wlc_hw->wake_override, override_bit);
  1232. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
  1233. return;
  1234. brcms_c_mctrl_write(wlc_hw);
  1235. }
  1236. /* When driver needs ucode to stop beaconing, it has to make sure that
  1237. * MCTL_AP is clear and MCTL_INFRA is set
  1238. * Mode MCTL_AP MCTL_INFRA
  1239. * AP 1 1
  1240. * STA 0 1 <--- This will ensure no beacons
  1241. * IBSS 0 0
  1242. */
  1243. static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
  1244. {
  1245. wlc_hw->mute_override = 1;
  1246. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1247. * override, then there is no change to write
  1248. */
  1249. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1250. return;
  1251. brcms_c_mctrl_write(wlc_hw);
  1252. }
  1253. /* Clear the override on AP and INFRA bits */
  1254. static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
  1255. {
  1256. if (wlc_hw->mute_override == 0)
  1257. return;
  1258. wlc_hw->mute_override = 0;
  1259. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1260. * override, then there is no change to write
  1261. */
  1262. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1263. return;
  1264. brcms_c_mctrl_write(wlc_hw);
  1265. }
  1266. /*
  1267. * Write a MAC address to the given match reg offset in the RXE match engine.
  1268. */
  1269. static void
  1270. brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
  1271. const u8 *addr)
  1272. {
  1273. struct bcma_device *core = wlc_hw->d11core;
  1274. u16 mac_l;
  1275. u16 mac_m;
  1276. u16 mac_h;
  1277. brcms_dbg_rx(core, "wl%d: brcms_b_set_addrmatch\n", wlc_hw->unit);
  1278. mac_l = addr[0] | (addr[1] << 8);
  1279. mac_m = addr[2] | (addr[3] << 8);
  1280. mac_h = addr[4] | (addr[5] << 8);
  1281. /* enter the MAC addr into the RXE match registers */
  1282. bcma_write16(core, D11REGOFFS(rcm_ctl),
  1283. RCM_INC_DATA | match_reg_offset);
  1284. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
  1285. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
  1286. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
  1287. }
  1288. void
  1289. brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
  1290. void *buf)
  1291. {
  1292. struct bcma_device *core = wlc_hw->d11core;
  1293. u32 word;
  1294. __le32 word_le;
  1295. __be32 word_be;
  1296. bool be_bit;
  1297. brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
  1298. bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
  1299. /* if MCTL_BIGEND bit set in mac control register,
  1300. * the chip swaps data in fifo, as well as data in
  1301. * template ram
  1302. */
  1303. be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
  1304. while (len > 0) {
  1305. memcpy(&word, buf, sizeof(u32));
  1306. if (be_bit) {
  1307. word_be = cpu_to_be32(word);
  1308. word = *(u32 *)&word_be;
  1309. } else {
  1310. word_le = cpu_to_le32(word);
  1311. word = *(u32 *)&word_le;
  1312. }
  1313. bcma_write32(core, D11REGOFFS(tplatewrdata), word);
  1314. buf = (u8 *) buf + sizeof(u32);
  1315. len -= sizeof(u32);
  1316. }
  1317. }
  1318. static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
  1319. {
  1320. wlc_hw->band->CWmin = newmin;
  1321. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1322. OBJADDR_SCR_SEL | S_DOT11_CWMIN);
  1323. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1324. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
  1325. }
  1326. static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
  1327. {
  1328. wlc_hw->band->CWmax = newmax;
  1329. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1330. OBJADDR_SCR_SEL | S_DOT11_CWMAX);
  1331. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1332. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
  1333. }
  1334. void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
  1335. {
  1336. bool fastclk;
  1337. /* request FAST clock if not on */
  1338. fastclk = wlc_hw->forcefastclk;
  1339. if (!fastclk)
  1340. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1341. wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
  1342. brcms_b_phy_reset(wlc_hw);
  1343. wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
  1344. /* restore the clk */
  1345. if (!fastclk)
  1346. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  1347. }
  1348. static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
  1349. {
  1350. u16 v;
  1351. struct brcms_c_info *wlc = wlc_hw->wlc;
  1352. /* update SYNTHPU_DLY */
  1353. if (BRCMS_ISLCNPHY(wlc->band))
  1354. v = SYNTHPU_DLY_LPPHY_US;
  1355. else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
  1356. v = SYNTHPU_DLY_NPHY_US;
  1357. else
  1358. v = SYNTHPU_DLY_BPHY_US;
  1359. brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
  1360. }
  1361. static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
  1362. {
  1363. u16 phyctl;
  1364. u16 phytxant = wlc_hw->bmac_phytxant;
  1365. u16 mask = PHY_TXC_ANT_MASK;
  1366. /* set the Probe Response frame phy control word */
  1367. phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
  1368. phyctl = (phyctl & ~mask) | phytxant;
  1369. brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
  1370. /* set the Response (ACK/CTS) frame phy control word */
  1371. phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
  1372. phyctl = (phyctl & ~mask) | phytxant;
  1373. brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
  1374. }
  1375. static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
  1376. u8 rate)
  1377. {
  1378. uint i;
  1379. u8 plcp_rate = 0;
  1380. struct plcp_signal_rate_lookup {
  1381. u8 rate;
  1382. u8 signal_rate;
  1383. };
  1384. /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
  1385. const struct plcp_signal_rate_lookup rate_lookup[] = {
  1386. {BRCM_RATE_6M, 0xB},
  1387. {BRCM_RATE_9M, 0xF},
  1388. {BRCM_RATE_12M, 0xA},
  1389. {BRCM_RATE_18M, 0xE},
  1390. {BRCM_RATE_24M, 0x9},
  1391. {BRCM_RATE_36M, 0xD},
  1392. {BRCM_RATE_48M, 0x8},
  1393. {BRCM_RATE_54M, 0xC}
  1394. };
  1395. for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
  1396. if (rate == rate_lookup[i].rate) {
  1397. plcp_rate = rate_lookup[i].signal_rate;
  1398. break;
  1399. }
  1400. }
  1401. /* Find the SHM pointer to the rate table entry by looking in the
  1402. * Direct-map Table
  1403. */
  1404. return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
  1405. }
  1406. static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
  1407. {
  1408. u8 rate;
  1409. u8 rates[8] = {
  1410. BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
  1411. BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
  1412. };
  1413. u16 entry_ptr;
  1414. u16 pctl1;
  1415. uint i;
  1416. if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
  1417. return;
  1418. /* walk the phy rate table and update the entries */
  1419. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  1420. rate = rates[i];
  1421. entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
  1422. /* read the SHM Rate Table entry OFDM PCTL1 values */
  1423. pctl1 =
  1424. brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
  1425. /* modify the value */
  1426. pctl1 &= ~PHY_TXC1_MODE_MASK;
  1427. pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
  1428. /* Update the SHM Rate Table entry OFDM PCTL1 values */
  1429. brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
  1430. pctl1);
  1431. }
  1432. }
  1433. /* band-specific init */
  1434. static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
  1435. {
  1436. struct brcms_hardware *wlc_hw = wlc->hw;
  1437. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: bandunit %d\n", wlc_hw->unit,
  1438. wlc_hw->band->bandunit);
  1439. brcms_c_ucode_bsinit(wlc_hw);
  1440. wlc_phy_init(wlc_hw->band->pi, chanspec);
  1441. brcms_c_ucode_txant_set(wlc_hw);
  1442. /*
  1443. * cwmin is band-specific, update hardware
  1444. * with value for current band
  1445. */
  1446. brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
  1447. brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
  1448. brcms_b_update_slot_timing(wlc_hw,
  1449. wlc_hw->band->bandtype == BRCM_BAND_5G ?
  1450. true : wlc_hw->shortslot);
  1451. /* write phytype and phyvers */
  1452. brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
  1453. brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
  1454. /*
  1455. * initialize the txphyctl1 rate table since
  1456. * shmem is shared between bands
  1457. */
  1458. brcms_upd_ofdm_pctl1_table(wlc_hw);
  1459. brcms_b_upd_synthpu(wlc_hw);
  1460. }
  1461. /* Perform a soft reset of the PHY PLL */
  1462. void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
  1463. {
  1464. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
  1465. ~0, 0);
  1466. udelay(1);
  1467. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1468. 0x4, 0);
  1469. udelay(1);
  1470. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1471. 0x4, 4);
  1472. udelay(1);
  1473. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1474. 0x4, 0);
  1475. udelay(1);
  1476. }
  1477. /* light way to turn on phy clock without reset for NPHY only
  1478. * refer to brcms_b_core_phy_clk for full version
  1479. */
  1480. void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
  1481. {
  1482. /* support(necessary for NPHY and HYPHY) only */
  1483. if (!BRCMS_ISNPHY(wlc_hw->band))
  1484. return;
  1485. if (ON == clk)
  1486. brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
  1487. else
  1488. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  1489. }
  1490. void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
  1491. {
  1492. if (ON == clk)
  1493. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
  1494. else
  1495. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
  1496. }
  1497. void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
  1498. {
  1499. struct brcms_phy_pub *pih = wlc_hw->band->pi;
  1500. u32 phy_bw_clkbits;
  1501. bool phy_in_reset = false;
  1502. brcms_dbg_info(wlc_hw->d11core, "wl%d: reset phy\n", wlc_hw->unit);
  1503. if (pih == NULL)
  1504. return;
  1505. phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
  1506. /* Specific reset sequence required for NPHY rev 3 and 4 */
  1507. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
  1508. NREV_LE(wlc_hw->band->phyrev, 4)) {
  1509. /* Set the PHY bandwidth */
  1510. brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
  1511. udelay(1);
  1512. /* Perform a soft reset of the PHY PLL */
  1513. brcms_b_core_phypll_reset(wlc_hw);
  1514. /* reset the PHY */
  1515. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
  1516. (SICF_PRST | SICF_PCLKE));
  1517. phy_in_reset = true;
  1518. } else {
  1519. brcms_b_core_ioctl(wlc_hw,
  1520. (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
  1521. (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
  1522. }
  1523. udelay(2);
  1524. brcms_b_core_phy_clk(wlc_hw, ON);
  1525. if (pih)
  1526. wlc_phy_anacore(pih, ON);
  1527. }
  1528. /* switch to and initialize new band */
  1529. static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
  1530. u16 chanspec) {
  1531. struct brcms_c_info *wlc = wlc_hw->wlc;
  1532. u32 macintmask;
  1533. /* Enable the d11 core before accessing it */
  1534. if (!bcma_core_is_enabled(wlc_hw->d11core)) {
  1535. bcma_core_enable(wlc_hw->d11core, 0);
  1536. brcms_c_mctrl_reset(wlc_hw);
  1537. }
  1538. macintmask = brcms_c_setband_inact(wlc, bandunit);
  1539. if (!wlc_hw->up)
  1540. return;
  1541. brcms_b_core_phy_clk(wlc_hw, ON);
  1542. /* band-specific initializations */
  1543. brcms_b_bsinit(wlc, chanspec);
  1544. /*
  1545. * If there are any pending software interrupt bits,
  1546. * then replace these with a harmless nonzero value
  1547. * so brcms_c_dpc() will re-enable interrupts when done.
  1548. */
  1549. if (wlc->macintstatus)
  1550. wlc->macintstatus = MI_DMAINT;
  1551. /* restore macintmask */
  1552. brcms_intrsrestore(wlc->wl, macintmask);
  1553. /* ucode should still be suspended.. */
  1554. WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
  1555. MCTL_EN_MAC) != 0);
  1556. }
  1557. static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
  1558. {
  1559. /* reject unsupported corerev */
  1560. if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
  1561. wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
  1562. wlc_hw->corerev);
  1563. return false;
  1564. }
  1565. return true;
  1566. }
  1567. /* Validate some board info parameters */
  1568. static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
  1569. {
  1570. uint boardrev = wlc_hw->boardrev;
  1571. /* 4 bits each for board type, major, minor, and tiny version */
  1572. uint brt = (boardrev & 0xf000) >> 12;
  1573. uint b0 = (boardrev & 0xf00) >> 8;
  1574. uint b1 = (boardrev & 0xf0) >> 4;
  1575. uint b2 = boardrev & 0xf;
  1576. /* voards from other vendors are always considered valid */
  1577. if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
  1578. return true;
  1579. /* do some boardrev sanity checks when boardvendor is Broadcom */
  1580. if (boardrev == 0)
  1581. return false;
  1582. if (boardrev <= 0xff)
  1583. return true;
  1584. if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
  1585. || (b2 > 9))
  1586. return false;
  1587. return true;
  1588. }
  1589. static void brcms_c_get_macaddr(struct brcms_hardware *wlc_hw, u8 etheraddr[ETH_ALEN])
  1590. {
  1591. struct ssb_sprom *sprom = &wlc_hw->d11core->bus->sprom;
  1592. /* If macaddr exists, use it (Sromrev4, CIS, ...). */
  1593. if (!is_zero_ether_addr(sprom->il0mac)) {
  1594. memcpy(etheraddr, sprom->il0mac, 6);
  1595. return;
  1596. }
  1597. if (wlc_hw->_nbands > 1)
  1598. memcpy(etheraddr, sprom->et1mac, 6);
  1599. else
  1600. memcpy(etheraddr, sprom->il0mac, 6);
  1601. }
  1602. /* power both the pll and external oscillator on/off */
  1603. static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
  1604. {
  1605. brcms_dbg_info(wlc_hw->d11core, "wl%d: want %d\n", wlc_hw->unit, want);
  1606. /*
  1607. * dont power down if plldown is false or
  1608. * we must poll hw radio disable
  1609. */
  1610. if (!want && wlc_hw->pllreq)
  1611. return;
  1612. wlc_hw->sbclk = want;
  1613. if (!wlc_hw->sbclk) {
  1614. wlc_hw->clk = false;
  1615. if (wlc_hw->band && wlc_hw->band->pi)
  1616. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  1617. }
  1618. }
  1619. /*
  1620. * Return true if radio is disabled, otherwise false.
  1621. * hw radio disable signal is an external pin, users activate it asynchronously
  1622. * this function could be called when driver is down and w/o clock
  1623. * it operates on different registers depending on corerev and boardflag.
  1624. */
  1625. static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
  1626. {
  1627. bool v, clk, xtal;
  1628. u32 flags = 0;
  1629. xtal = wlc_hw->sbclk;
  1630. if (!xtal)
  1631. brcms_b_xtal(wlc_hw, ON);
  1632. /* may need to take core out of reset first */
  1633. clk = wlc_hw->clk;
  1634. if (!clk) {
  1635. /*
  1636. * mac no longer enables phyclk automatically when driver
  1637. * accesses phyreg throughput mac. This can be skipped since
  1638. * only mac reg is accessed below
  1639. */
  1640. if (D11REV_GE(wlc_hw->corerev, 18))
  1641. flags |= SICF_PCLKE;
  1642. /*
  1643. * TODO: test suspend/resume
  1644. *
  1645. * AI chip doesn't restore bar0win2 on
  1646. * hibernation/resume, need sw fixup
  1647. */
  1648. bcma_core_enable(wlc_hw->d11core, flags);
  1649. brcms_c_mctrl_reset(wlc_hw);
  1650. }
  1651. v = ((bcma_read32(wlc_hw->d11core,
  1652. D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
  1653. /* put core back into reset */
  1654. if (!clk)
  1655. bcma_core_disable(wlc_hw->d11core, 0);
  1656. if (!xtal)
  1657. brcms_b_xtal(wlc_hw, OFF);
  1658. return v;
  1659. }
  1660. static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
  1661. {
  1662. struct dma_pub *di = wlc_hw->di[fifo];
  1663. return dma_rxreset(di);
  1664. }
  1665. /* d11 core reset
  1666. * ensure fask clock during reset
  1667. * reset dma
  1668. * reset d11(out of reset)
  1669. * reset phy(out of reset)
  1670. * clear software macintstatus for fresh new start
  1671. * one testing hack wlc_hw->noreset will bypass the d11/phy reset
  1672. */
  1673. void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
  1674. {
  1675. uint i;
  1676. bool fastclk;
  1677. if (flags == BRCMS_USE_COREFLAGS)
  1678. flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
  1679. brcms_dbg_info(wlc_hw->d11core, "wl%d: core reset\n", wlc_hw->unit);
  1680. /* request FAST clock if not on */
  1681. fastclk = wlc_hw->forcefastclk;
  1682. if (!fastclk)
  1683. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1684. /* reset the dma engines except first time thru */
  1685. if (bcma_core_is_enabled(wlc_hw->d11core)) {
  1686. for (i = 0; i < NFIFO; i++)
  1687. if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
  1688. brcms_err(wlc_hw->d11core, "wl%d: %s: "
  1689. "dma_txreset[%d]: cannot stop dma\n",
  1690. wlc_hw->unit, __func__, i);
  1691. if ((wlc_hw->di[RX_FIFO])
  1692. && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
  1693. brcms_err(wlc_hw->d11core, "wl%d: %s: dma_rxreset"
  1694. "[%d]: cannot stop dma\n",
  1695. wlc_hw->unit, __func__, RX_FIFO);
  1696. }
  1697. /* if noreset, just stop the psm and return */
  1698. if (wlc_hw->noreset) {
  1699. wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
  1700. brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
  1701. return;
  1702. }
  1703. /*
  1704. * mac no longer enables phyclk automatically when driver accesses
  1705. * phyreg throughput mac, AND phy_reset is skipped at early stage when
  1706. * band->pi is invalid. need to enable PHY CLK
  1707. */
  1708. if (D11REV_GE(wlc_hw->corerev, 18))
  1709. flags |= SICF_PCLKE;
  1710. /*
  1711. * reset the core
  1712. * In chips with PMU, the fastclk request goes through d11 core
  1713. * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
  1714. *
  1715. * This adds some delay and we can optimize it by also requesting
  1716. * fastclk through chipcommon during this period if necessary. But
  1717. * that has to work coordinate with other driver like mips/arm since
  1718. * they may touch chipcommon as well.
  1719. */
  1720. wlc_hw->clk = false;
  1721. bcma_core_enable(wlc_hw->d11core, flags);
  1722. wlc_hw->clk = true;
  1723. if (wlc_hw->band && wlc_hw->band->pi)
  1724. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
  1725. brcms_c_mctrl_reset(wlc_hw);
  1726. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
  1727. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  1728. brcms_b_phy_reset(wlc_hw);
  1729. /* turn on PHY_PLL */
  1730. brcms_b_core_phypll_ctl(wlc_hw, true);
  1731. /* clear sw intstatus */
  1732. wlc_hw->wlc->macintstatus = 0;
  1733. /* restore the clk setting */
  1734. if (!fastclk)
  1735. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  1736. }
  1737. /* txfifo sizes needs to be modified(increased) since the newer cores
  1738. * have more memory.
  1739. */
  1740. static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
  1741. {
  1742. struct bcma_device *core = wlc_hw->d11core;
  1743. u16 fifo_nu;
  1744. u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
  1745. u16 txfifo_def, txfifo_def1;
  1746. u16 txfifo_cmd;
  1747. /* tx fifos start at TXFIFO_START_BLK from the Base address */
  1748. txfifo_startblk = TXFIFO_START_BLK;
  1749. /* sequence of operations: reset fifo, set fifo size, reset fifo */
  1750. for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
  1751. txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
  1752. txfifo_def = (txfifo_startblk & 0xff) |
  1753. (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
  1754. txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
  1755. ((((txfifo_endblk -
  1756. 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
  1757. txfifo_cmd =
  1758. TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
  1759. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1760. bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
  1761. bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
  1762. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1763. txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
  1764. }
  1765. /*
  1766. * need to propagate to shm location to be in sync since ucode/hw won't
  1767. * do this
  1768. */
  1769. brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
  1770. wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
  1771. brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
  1772. wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
  1773. brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
  1774. ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
  1775. xmtfifo_sz[TX_AC_BK_FIFO]));
  1776. brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
  1777. ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
  1778. xmtfifo_sz[TX_BCMC_FIFO]));
  1779. }
  1780. /* This function is used for changing the tsf frac register
  1781. * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
  1782. * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
  1783. * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
  1784. * HTPHY Formula is 2^26/freq(MHz) e.g.
  1785. * For spuron2 - 126MHz -> 2^26/126 = 532610.0
  1786. * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
  1787. * For spuron: 123MHz -> 2^26/123 = 545600.5
  1788. * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
  1789. * For spur off: 120MHz -> 2^26/120 = 559240.5
  1790. * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
  1791. */
  1792. void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
  1793. {
  1794. struct bcma_device *core = wlc_hw->d11core;
  1795. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43224) ||
  1796. (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225)) {
  1797. if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
  1798. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
  1799. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1800. } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
  1801. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
  1802. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1803. } else { /* 120Mhz */
  1804. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
  1805. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1806. }
  1807. } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1808. if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
  1809. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
  1810. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1811. } else { /* 80Mhz */
  1812. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
  1813. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1814. }
  1815. }
  1816. }
  1817. /* Initialize GPIOs that are controlled by D11 core */
  1818. static void brcms_c_gpio_init(struct brcms_c_info *wlc)
  1819. {
  1820. struct brcms_hardware *wlc_hw = wlc->hw;
  1821. u32 gc, gm;
  1822. /* use GPIO select 0 to get all gpio signals from the gpio out reg */
  1823. brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
  1824. /*
  1825. * Common GPIO setup:
  1826. * G0 = LED 0 = WLAN Activity
  1827. * G1 = LED 1 = WLAN 2.4 GHz Radio State
  1828. * G2 = LED 2 = WLAN 5 GHz Radio State
  1829. * G4 = radio disable input (HI enabled, LO disabled)
  1830. */
  1831. gc = gm = 0;
  1832. /* Allocate GPIOs for mimo antenna diversity feature */
  1833. if (wlc_hw->antsel_type == ANTSEL_2x3) {
  1834. /* Enable antenna diversity, use 2x3 mode */
  1835. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1836. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1837. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
  1838. MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
  1839. /* init superswitch control */
  1840. wlc_phy_antsel_init(wlc_hw->band->pi, false);
  1841. } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
  1842. gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
  1843. /*
  1844. * The board itself is powered by these GPIOs
  1845. * (when not sending pattern) so set them high
  1846. */
  1847. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
  1848. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1849. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
  1850. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1851. /* Enable antenna diversity, use 2x4 mode */
  1852. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1853. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1854. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
  1855. BRCM_BAND_ALL);
  1856. /* Configure the desired clock to be 4Mhz */
  1857. brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
  1858. ANTSEL_CLKDIV_4MHZ);
  1859. }
  1860. /*
  1861. * gpio 9 controls the PA. ucode is responsible
  1862. * for wiggling out and oe
  1863. */
  1864. if (wlc_hw->boardflags & BFL_PACTRL)
  1865. gm |= gc |= BOARD_GPIO_PACTRL;
  1866. /* apply to gpiocontrol register */
  1867. bcma_chipco_gpio_control(&wlc_hw->d11core->bus->drv_cc, gm, gc);
  1868. }
  1869. static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
  1870. const __le32 ucode[], const size_t nbytes)
  1871. {
  1872. struct bcma_device *core = wlc_hw->d11core;
  1873. uint i;
  1874. uint count;
  1875. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  1876. count = (nbytes / sizeof(u32));
  1877. bcma_write32(core, D11REGOFFS(objaddr),
  1878. OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
  1879. (void)bcma_read32(core, D11REGOFFS(objaddr));
  1880. for (i = 0; i < count; i++)
  1881. bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
  1882. }
  1883. static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
  1884. {
  1885. struct brcms_c_info *wlc;
  1886. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  1887. wlc = wlc_hw->wlc;
  1888. if (wlc_hw->ucode_loaded)
  1889. return;
  1890. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  1891. if (BRCMS_ISNPHY(wlc_hw->band)) {
  1892. brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
  1893. ucode->bcm43xx_16_mimosz);
  1894. wlc_hw->ucode_loaded = true;
  1895. } else
  1896. brcms_err(wlc_hw->d11core,
  1897. "%s: wl%d: unsupported phy in corerev %d\n",
  1898. __func__, wlc_hw->unit, wlc_hw->corerev);
  1899. } else if (D11REV_IS(wlc_hw->corerev, 24)) {
  1900. if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1901. brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
  1902. ucode->bcm43xx_24_lcnsz);
  1903. wlc_hw->ucode_loaded = true;
  1904. } else {
  1905. brcms_err(wlc_hw->d11core,
  1906. "%s: wl%d: unsupported phy in corerev %d\n",
  1907. __func__, wlc_hw->unit, wlc_hw->corerev);
  1908. }
  1909. }
  1910. }
  1911. void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
  1912. {
  1913. /* update sw state */
  1914. wlc_hw->bmac_phytxant = phytxant;
  1915. /* push to ucode if up */
  1916. if (!wlc_hw->up)
  1917. return;
  1918. brcms_c_ucode_txant_set(wlc_hw);
  1919. }
  1920. u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
  1921. {
  1922. return (u16) wlc_hw->wlc->stf->txant;
  1923. }
  1924. void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
  1925. {
  1926. wlc_hw->antsel_type = antsel_type;
  1927. /* Update the antsel type for phy module to use */
  1928. wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
  1929. }
  1930. static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
  1931. {
  1932. bool fatal = false;
  1933. uint unit;
  1934. uint intstatus, idx;
  1935. struct bcma_device *core = wlc_hw->d11core;
  1936. unit = wlc_hw->unit;
  1937. for (idx = 0; idx < NFIFO; idx++) {
  1938. /* read intstatus register and ignore any non-error bits */
  1939. intstatus =
  1940. bcma_read32(core,
  1941. D11REGOFFS(intctrlregs[idx].intstatus)) &
  1942. I_ERRORS;
  1943. if (!intstatus)
  1944. continue;
  1945. brcms_dbg_int(core, "wl%d: intstatus%d 0x%x\n",
  1946. unit, idx, intstatus);
  1947. if (intstatus & I_RO) {
  1948. brcms_err(core, "wl%d: fifo %d: receive fifo "
  1949. "overflow\n", unit, idx);
  1950. fatal = true;
  1951. }
  1952. if (intstatus & I_PC) {
  1953. brcms_err(core, "wl%d: fifo %d: descriptor error\n",
  1954. unit, idx);
  1955. fatal = true;
  1956. }
  1957. if (intstatus & I_PD) {
  1958. brcms_err(core, "wl%d: fifo %d: data error\n", unit,
  1959. idx);
  1960. fatal = true;
  1961. }
  1962. if (intstatus & I_DE) {
  1963. brcms_err(core, "wl%d: fifo %d: descriptor protocol "
  1964. "error\n", unit, idx);
  1965. fatal = true;
  1966. }
  1967. if (intstatus & I_RU)
  1968. brcms_err(core, "wl%d: fifo %d: receive descriptor "
  1969. "underflow\n", idx, unit);
  1970. if (intstatus & I_XU) {
  1971. brcms_err(core, "wl%d: fifo %d: transmit fifo "
  1972. "underflow\n", idx, unit);
  1973. fatal = true;
  1974. }
  1975. if (fatal) {
  1976. brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
  1977. break;
  1978. } else
  1979. bcma_write32(core,
  1980. D11REGOFFS(intctrlregs[idx].intstatus),
  1981. intstatus);
  1982. }
  1983. }
  1984. void brcms_c_intrson(struct brcms_c_info *wlc)
  1985. {
  1986. struct brcms_hardware *wlc_hw = wlc->hw;
  1987. wlc->macintmask = wlc->defmacintmask;
  1988. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  1989. }
  1990. u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
  1991. {
  1992. struct brcms_hardware *wlc_hw = wlc->hw;
  1993. u32 macintmask;
  1994. if (!wlc_hw->clk)
  1995. return 0;
  1996. macintmask = wlc->macintmask; /* isr can still happen */
  1997. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
  1998. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
  1999. udelay(1); /* ensure int line is no longer driven */
  2000. wlc->macintmask = 0;
  2001. /* return previous macintmask; resolve race between us and our isr */
  2002. return wlc->macintstatus ? 0 : macintmask;
  2003. }
  2004. void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
  2005. {
  2006. struct brcms_hardware *wlc_hw = wlc->hw;
  2007. if (!wlc_hw->clk)
  2008. return;
  2009. wlc->macintmask = macintmask;
  2010. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  2011. }
  2012. /* assumes that the d11 MAC is enabled */
  2013. static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
  2014. uint tx_fifo)
  2015. {
  2016. u8 fifo = 1 << tx_fifo;
  2017. /* Two clients of this code, 11h Quiet period and scanning. */
  2018. /* only suspend if not already suspended */
  2019. if ((wlc_hw->suspended_fifos & fifo) == fifo)
  2020. return;
  2021. /* force the core awake only if not already */
  2022. if (wlc_hw->suspended_fifos == 0)
  2023. brcms_c_ucode_wake_override_set(wlc_hw,
  2024. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2025. wlc_hw->suspended_fifos |= fifo;
  2026. if (wlc_hw->di[tx_fifo]) {
  2027. /*
  2028. * Suspending AMPDU transmissions in the middle can cause
  2029. * underflow which may result in mismatch between ucode and
  2030. * driver so suspend the mac before suspending the FIFO
  2031. */
  2032. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2033. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  2034. dma_txsuspend(wlc_hw->di[tx_fifo]);
  2035. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2036. brcms_c_enable_mac(wlc_hw->wlc);
  2037. }
  2038. }
  2039. static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
  2040. uint tx_fifo)
  2041. {
  2042. /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
  2043. * but need to be done here for PIO otherwise the watchdog will catch
  2044. * the inconsistency and fire
  2045. */
  2046. /* Two clients of this code, 11h Quiet period and scanning. */
  2047. if (wlc_hw->di[tx_fifo])
  2048. dma_txresume(wlc_hw->di[tx_fifo]);
  2049. /* allow core to sleep again */
  2050. if (wlc_hw->suspended_fifos == 0)
  2051. return;
  2052. else {
  2053. wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
  2054. if (wlc_hw->suspended_fifos == 0)
  2055. brcms_c_ucode_wake_override_clear(wlc_hw,
  2056. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2057. }
  2058. }
  2059. /* precondition: requires the mac core to be enabled */
  2060. static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
  2061. {
  2062. static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2063. u8 *ethaddr = wlc_hw->wlc->pub->cur_etheraddr;
  2064. if (mute_tx) {
  2065. /* suspend tx fifos */
  2066. brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
  2067. brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
  2068. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
  2069. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
  2070. /* zero the address match register so we do not send ACKs */
  2071. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, null_ether_addr);
  2072. } else {
  2073. /* resume tx fifos */
  2074. brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
  2075. brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
  2076. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
  2077. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
  2078. /* Restore address */
  2079. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET, ethaddr);
  2080. }
  2081. wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
  2082. if (mute_tx)
  2083. brcms_c_ucode_mute_override_set(wlc_hw);
  2084. else
  2085. brcms_c_ucode_mute_override_clear(wlc_hw);
  2086. }
  2087. void
  2088. brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
  2089. {
  2090. brcms_b_mute(wlc->hw, mute_tx);
  2091. }
  2092. /*
  2093. * Read and clear macintmask and macintstatus and intstatus registers.
  2094. * This routine should be called with interrupts off
  2095. * Return:
  2096. * -1 if brcms_deviceremoved(wlc) evaluates to true;
  2097. * 0 if the interrupt is not for us, or we are in some special cases;
  2098. * device interrupt status bits otherwise.
  2099. */
  2100. static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
  2101. {
  2102. struct brcms_hardware *wlc_hw = wlc->hw;
  2103. struct bcma_device *core = wlc_hw->d11core;
  2104. u32 macintstatus, mask;
  2105. /* macintstatus includes a DMA interrupt summary bit */
  2106. macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
  2107. mask = in_isr ? wlc->macintmask : wlc->defmacintmask;
  2108. trace_brcms_macintstatus(&core->dev, in_isr, macintstatus, mask);
  2109. /* detect cardbus removed, in power down(suspend) and in reset */
  2110. if (brcms_deviceremoved(wlc))
  2111. return -1;
  2112. /* brcms_deviceremoved() succeeds even when the core is still resetting,
  2113. * handle that case here.
  2114. */
  2115. if (macintstatus == 0xffffffff)
  2116. return 0;
  2117. /* defer unsolicited interrupts */
  2118. macintstatus &= mask;
  2119. /* if not for us */
  2120. if (macintstatus == 0)
  2121. return 0;
  2122. /* turn off the interrupts */
  2123. bcma_write32(core, D11REGOFFS(macintmask), 0);
  2124. (void)bcma_read32(core, D11REGOFFS(macintmask));
  2125. wlc->macintmask = 0;
  2126. /* clear device interrupts */
  2127. bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
  2128. /* MI_DMAINT is indication of non-zero intstatus */
  2129. if (macintstatus & MI_DMAINT)
  2130. /*
  2131. * only fifo interrupt enabled is I_RI in
  2132. * RX_FIFO. If MI_DMAINT is set, assume it
  2133. * is set and clear the interrupt.
  2134. */
  2135. bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
  2136. DEF_RXINTMASK);
  2137. return macintstatus;
  2138. }
  2139. /* Update wlc->macintstatus and wlc->intstatus[]. */
  2140. /* Return true if they are updated successfully. false otherwise */
  2141. bool brcms_c_intrsupd(struct brcms_c_info *wlc)
  2142. {
  2143. u32 macintstatus;
  2144. /* read and clear macintstatus and intstatus registers */
  2145. macintstatus = wlc_intstatus(wlc, false);
  2146. /* device is removed */
  2147. if (macintstatus == 0xffffffff)
  2148. return false;
  2149. /* update interrupt status in software */
  2150. wlc->macintstatus |= macintstatus;
  2151. return true;
  2152. }
  2153. /*
  2154. * First-level interrupt processing.
  2155. * Return true if this was our interrupt
  2156. * and if further brcms_c_dpc() processing is required,
  2157. * false otherwise.
  2158. */
  2159. bool brcms_c_isr(struct brcms_c_info *wlc)
  2160. {
  2161. struct brcms_hardware *wlc_hw = wlc->hw;
  2162. u32 macintstatus;
  2163. if (!wlc_hw->up || !wlc->macintmask)
  2164. return false;
  2165. /* read and clear macintstatus and intstatus registers */
  2166. macintstatus = wlc_intstatus(wlc, true);
  2167. if (macintstatus == 0xffffffff) {
  2168. brcms_err(wlc_hw->d11core,
  2169. "DEVICEREMOVED detected in the ISR code path\n");
  2170. return false;
  2171. }
  2172. /* it is not for us */
  2173. if (macintstatus == 0)
  2174. return false;
  2175. /* save interrupt status bits */
  2176. wlc->macintstatus = macintstatus;
  2177. return true;
  2178. }
  2179. void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
  2180. {
  2181. struct brcms_hardware *wlc_hw = wlc->hw;
  2182. struct bcma_device *core = wlc_hw->d11core;
  2183. u32 mc, mi;
  2184. brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
  2185. wlc_hw->band->bandunit);
  2186. /*
  2187. * Track overlapping suspend requests
  2188. */
  2189. wlc_hw->mac_suspend_depth++;
  2190. if (wlc_hw->mac_suspend_depth > 1)
  2191. return;
  2192. /* force the core awake */
  2193. brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2194. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2195. if (mc == 0xffffffff) {
  2196. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2197. __func__);
  2198. brcms_down(wlc->wl);
  2199. return;
  2200. }
  2201. WARN_ON(mc & MCTL_PSM_JMP_0);
  2202. WARN_ON(!(mc & MCTL_PSM_RUN));
  2203. WARN_ON(!(mc & MCTL_EN_MAC));
  2204. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2205. if (mi == 0xffffffff) {
  2206. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2207. __func__);
  2208. brcms_down(wlc->wl);
  2209. return;
  2210. }
  2211. WARN_ON(mi & MI_MACSSPNDD);
  2212. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
  2213. SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
  2214. BRCMS_MAX_MAC_SUSPEND);
  2215. if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
  2216. brcms_err(core, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
  2217. " and MI_MACSSPNDD is still not on.\n",
  2218. wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
  2219. brcms_err(core, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
  2220. "psm_brc 0x%04x\n", wlc_hw->unit,
  2221. bcma_read32(core, D11REGOFFS(psmdebug)),
  2222. bcma_read32(core, D11REGOFFS(phydebug)),
  2223. bcma_read16(core, D11REGOFFS(psm_brc)));
  2224. }
  2225. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2226. if (mc == 0xffffffff) {
  2227. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2228. __func__);
  2229. brcms_down(wlc->wl);
  2230. return;
  2231. }
  2232. WARN_ON(mc & MCTL_PSM_JMP_0);
  2233. WARN_ON(!(mc & MCTL_PSM_RUN));
  2234. WARN_ON(mc & MCTL_EN_MAC);
  2235. }
  2236. void brcms_c_enable_mac(struct brcms_c_info *wlc)
  2237. {
  2238. struct brcms_hardware *wlc_hw = wlc->hw;
  2239. struct bcma_device *core = wlc_hw->d11core;
  2240. u32 mc, mi;
  2241. brcms_dbg_mac80211(core, "wl%d: bandunit %d\n", wlc_hw->unit,
  2242. wlc->band->bandunit);
  2243. /*
  2244. * Track overlapping suspend requests
  2245. */
  2246. wlc_hw->mac_suspend_depth--;
  2247. if (wlc_hw->mac_suspend_depth > 0)
  2248. return;
  2249. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2250. WARN_ON(mc & MCTL_PSM_JMP_0);
  2251. WARN_ON(mc & MCTL_EN_MAC);
  2252. WARN_ON(!(mc & MCTL_PSM_RUN));
  2253. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
  2254. bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
  2255. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2256. WARN_ON(mc & MCTL_PSM_JMP_0);
  2257. WARN_ON(!(mc & MCTL_EN_MAC));
  2258. WARN_ON(!(mc & MCTL_PSM_RUN));
  2259. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2260. WARN_ON(mi & MI_MACSSPNDD);
  2261. brcms_c_ucode_wake_override_clear(wlc_hw,
  2262. BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2263. }
  2264. void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
  2265. {
  2266. wlc_hw->hw_stf_ss_opmode = stf_mode;
  2267. if (wlc_hw->clk)
  2268. brcms_upd_ofdm_pctl1_table(wlc_hw);
  2269. }
  2270. static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
  2271. {
  2272. struct bcma_device *core = wlc_hw->d11core;
  2273. u32 w, val;
  2274. struct wiphy *wiphy = wlc_hw->wlc->wiphy;
  2275. /* Validate dchip register access */
  2276. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2277. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2278. w = bcma_read32(core, D11REGOFFS(objdata));
  2279. /* Can we write and read back a 32bit register? */
  2280. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2281. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2282. bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
  2283. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2284. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2285. val = bcma_read32(core, D11REGOFFS(objdata));
  2286. if (val != (u32) 0xaa5555aa) {
  2287. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2288. "expected 0xaa5555aa\n", wlc_hw->unit, val);
  2289. return false;
  2290. }
  2291. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2292. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2293. bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
  2294. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2295. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2296. val = bcma_read32(core, D11REGOFFS(objdata));
  2297. if (val != (u32) 0x55aaaa55) {
  2298. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2299. "expected 0x55aaaa55\n", wlc_hw->unit, val);
  2300. return false;
  2301. }
  2302. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2303. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2304. bcma_write32(core, D11REGOFFS(objdata), w);
  2305. /* clear CFPStart */
  2306. bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
  2307. w = bcma_read32(core, D11REGOFFS(maccontrol));
  2308. if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
  2309. (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
  2310. wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
  2311. "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
  2312. (MCTL_IHR_EN | MCTL_WAKE),
  2313. (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
  2314. return false;
  2315. }
  2316. return true;
  2317. }
  2318. #define PHYPLL_WAIT_US 100000
  2319. void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
  2320. {
  2321. struct bcma_device *core = wlc_hw->d11core;
  2322. u32 tmp;
  2323. brcms_dbg_info(core, "wl%d\n", wlc_hw->unit);
  2324. tmp = 0;
  2325. if (on) {
  2326. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  2327. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2328. CCS_ERSRC_REQ_HT |
  2329. CCS_ERSRC_REQ_D11PLL |
  2330. CCS_ERSRC_REQ_PHYPLL);
  2331. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2332. CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
  2333. PHYPLL_WAIT_US);
  2334. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2335. if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
  2336. brcms_err(core, "%s: turn on PHY PLL failed\n",
  2337. __func__);
  2338. } else {
  2339. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2340. tmp | CCS_ERSRC_REQ_D11PLL |
  2341. CCS_ERSRC_REQ_PHYPLL);
  2342. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2343. (CCS_ERSRC_AVAIL_D11PLL |
  2344. CCS_ERSRC_AVAIL_PHYPLL)) !=
  2345. (CCS_ERSRC_AVAIL_D11PLL |
  2346. CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
  2347. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2348. if ((tmp &
  2349. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2350. !=
  2351. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2352. brcms_err(core, "%s: turn on PHY PLL failed\n",
  2353. __func__);
  2354. }
  2355. } else {
  2356. /*
  2357. * Since the PLL may be shared, other cores can still
  2358. * be requesting it; so we'll deassert the request but
  2359. * not wait for status to comply.
  2360. */
  2361. bcma_mask32(core, D11REGOFFS(clk_ctl_st),
  2362. ~CCS_ERSRC_REQ_PHYPLL);
  2363. (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2364. }
  2365. }
  2366. static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
  2367. {
  2368. bool dev_gone;
  2369. brcms_dbg_info(wlc_hw->d11core, "wl%d: disable core\n", wlc_hw->unit);
  2370. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  2371. if (dev_gone)
  2372. return;
  2373. if (wlc_hw->noreset)
  2374. return;
  2375. /* radio off */
  2376. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  2377. /* turn off analog core */
  2378. wlc_phy_anacore(wlc_hw->band->pi, OFF);
  2379. /* turn off PHYPLL to save power */
  2380. brcms_b_core_phypll_ctl(wlc_hw, false);
  2381. wlc_hw->clk = false;
  2382. bcma_core_disable(wlc_hw->d11core, 0);
  2383. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  2384. }
  2385. static void brcms_c_flushqueues(struct brcms_c_info *wlc)
  2386. {
  2387. struct brcms_hardware *wlc_hw = wlc->hw;
  2388. uint i;
  2389. /* free any posted tx packets */
  2390. for (i = 0; i < NFIFO; i++) {
  2391. if (wlc_hw->di[i]) {
  2392. dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
  2393. if (i < TX_BCMC_FIFO)
  2394. ieee80211_wake_queue(wlc->pub->ieee_hw,
  2395. brcms_fifo_to_ac(i));
  2396. }
  2397. }
  2398. /* free any posted rx packets */
  2399. dma_rxreclaim(wlc_hw->di[RX_FIFO]);
  2400. }
  2401. static u16
  2402. brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
  2403. {
  2404. struct bcma_device *core = wlc_hw->d11core;
  2405. u16 objoff = D11REGOFFS(objdata);
  2406. bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
  2407. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2408. if (offset & 2)
  2409. objoff += 2;
  2410. return bcma_read16(core, objoff);
  2411. }
  2412. static void
  2413. brcms_b_write_objmem(struct brcms_hardware *wlc_hw, uint offset, u16 v,
  2414. u32 sel)
  2415. {
  2416. struct bcma_device *core = wlc_hw->d11core;
  2417. u16 objoff = D11REGOFFS(objdata);
  2418. bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
  2419. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2420. if (offset & 2)
  2421. objoff += 2;
  2422. bcma_wflush16(core, objoff, v);
  2423. }
  2424. /*
  2425. * Read a single u16 from shared memory.
  2426. * SHM 'offset' needs to be an even address
  2427. */
  2428. u16 brcms_b_read_shm(struct brcms_hardware *wlc_hw, uint offset)
  2429. {
  2430. return brcms_b_read_objmem(wlc_hw, offset, OBJADDR_SHM_SEL);
  2431. }
  2432. /*
  2433. * Write a single u16 to shared memory.
  2434. * SHM 'offset' needs to be an even address
  2435. */
  2436. void brcms_b_write_shm(struct brcms_hardware *wlc_hw, uint offset, u16 v)
  2437. {
  2438. brcms_b_write_objmem(wlc_hw, offset, v, OBJADDR_SHM_SEL);
  2439. }
  2440. /*
  2441. * Copy a buffer to shared memory of specified type .
  2442. * SHM 'offset' needs to be an even address and
  2443. * Buffer length 'len' must be an even number of bytes
  2444. * 'sel' selects the type of memory
  2445. */
  2446. void
  2447. brcms_b_copyto_objmem(struct brcms_hardware *wlc_hw, uint offset,
  2448. const void *buf, int len, u32 sel)
  2449. {
  2450. u16 v;
  2451. const u8 *p = (const u8 *)buf;
  2452. int i;
  2453. if (len <= 0 || (offset & 1) || (len & 1))
  2454. return;
  2455. for (i = 0; i < len; i += 2) {
  2456. v = p[i] | (p[i + 1] << 8);
  2457. brcms_b_write_objmem(wlc_hw, offset + i, v, sel);
  2458. }
  2459. }
  2460. /*
  2461. * Copy a piece of shared memory of specified type to a buffer .
  2462. * SHM 'offset' needs to be an even address and
  2463. * Buffer length 'len' must be an even number of bytes
  2464. * 'sel' selects the type of memory
  2465. */
  2466. void
  2467. brcms_b_copyfrom_objmem(struct brcms_hardware *wlc_hw, uint offset, void *buf,
  2468. int len, u32 sel)
  2469. {
  2470. u16 v;
  2471. u8 *p = (u8 *) buf;
  2472. int i;
  2473. if (len <= 0 || (offset & 1) || (len & 1))
  2474. return;
  2475. for (i = 0; i < len; i += 2) {
  2476. v = brcms_b_read_objmem(wlc_hw, offset + i, sel);
  2477. p[i] = v & 0xFF;
  2478. p[i + 1] = (v >> 8) & 0xFF;
  2479. }
  2480. }
  2481. /* Copy a buffer to shared memory.
  2482. * SHM 'offset' needs to be an even address and
  2483. * Buffer length 'len' must be an even number of bytes
  2484. */
  2485. static void brcms_c_copyto_shm(struct brcms_c_info *wlc, uint offset,
  2486. const void *buf, int len)
  2487. {
  2488. brcms_b_copyto_objmem(wlc->hw, offset, buf, len, OBJADDR_SHM_SEL);
  2489. }
  2490. static void brcms_b_retrylimit_upd(struct brcms_hardware *wlc_hw,
  2491. u16 SRL, u16 LRL)
  2492. {
  2493. wlc_hw->SRL = SRL;
  2494. wlc_hw->LRL = LRL;
  2495. /* write retry limit to SCR, shouldn't need to suspend */
  2496. if (wlc_hw->up) {
  2497. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  2498. OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
  2499. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  2500. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->SRL);
  2501. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  2502. OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
  2503. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  2504. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), wlc_hw->LRL);
  2505. }
  2506. }
  2507. static void brcms_b_pllreq(struct brcms_hardware *wlc_hw, bool set, u32 req_bit)
  2508. {
  2509. if (set) {
  2510. if (mboolisset(wlc_hw->pllreq, req_bit))
  2511. return;
  2512. mboolset(wlc_hw->pllreq, req_bit);
  2513. if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
  2514. if (!wlc_hw->sbclk)
  2515. brcms_b_xtal(wlc_hw, ON);
  2516. }
  2517. } else {
  2518. if (!mboolisset(wlc_hw->pllreq, req_bit))
  2519. return;
  2520. mboolclr(wlc_hw->pllreq, req_bit);
  2521. if (mboolisset(wlc_hw->pllreq, BRCMS_PLLREQ_FLIP)) {
  2522. if (wlc_hw->sbclk)
  2523. brcms_b_xtal(wlc_hw, OFF);
  2524. }
  2525. }
  2526. }
  2527. static void brcms_b_antsel_set(struct brcms_hardware *wlc_hw, u32 antsel_avail)
  2528. {
  2529. wlc_hw->antsel_avail = antsel_avail;
  2530. }
  2531. /*
  2532. * conditions under which the PM bit should be set in outgoing frames
  2533. * and STAY_AWAKE is meaningful
  2534. */
  2535. static bool brcms_c_ps_allowed(struct brcms_c_info *wlc)
  2536. {
  2537. struct brcms_bss_cfg *cfg = wlc->bsscfg;
  2538. /* disallow PS when one of the following global conditions meets */
  2539. if (!wlc->pub->associated)
  2540. return false;
  2541. /* disallow PS when one of these meets when not scanning */
  2542. if (wlc->filter_flags & FIF_PROMISC_IN_BSS)
  2543. return false;
  2544. if (cfg->associated) {
  2545. /*
  2546. * disallow PS when one of the following
  2547. * bsscfg specific conditions meets
  2548. */
  2549. if (!cfg->BSS)
  2550. return false;
  2551. return false;
  2552. }
  2553. return true;
  2554. }
  2555. static void brcms_c_statsupd(struct brcms_c_info *wlc)
  2556. {
  2557. int i;
  2558. struct macstat macstats;
  2559. #ifdef DEBUG
  2560. u16 delta;
  2561. u16 rxf0ovfl;
  2562. u16 txfunfl[NFIFO];
  2563. #endif /* DEBUG */
  2564. /* if driver down, make no sense to update stats */
  2565. if (!wlc->pub->up)
  2566. return;
  2567. #ifdef DEBUG
  2568. /* save last rx fifo 0 overflow count */
  2569. rxf0ovfl = wlc->core->macstat_snapshot->rxf0ovfl;
  2570. /* save last tx fifo underflow count */
  2571. for (i = 0; i < NFIFO; i++)
  2572. txfunfl[i] = wlc->core->macstat_snapshot->txfunfl[i];
  2573. #endif /* DEBUG */
  2574. /* Read mac stats from contiguous shared memory */
  2575. brcms_b_copyfrom_objmem(wlc->hw, M_UCODE_MACSTAT, &macstats,
  2576. sizeof(struct macstat), OBJADDR_SHM_SEL);
  2577. #ifdef DEBUG
  2578. /* check for rx fifo 0 overflow */
  2579. delta = (u16) (wlc->core->macstat_snapshot->rxf0ovfl - rxf0ovfl);
  2580. if (delta)
  2581. brcms_err(wlc->hw->d11core, "wl%d: %u rx fifo 0 overflows!\n",
  2582. wlc->pub->unit, delta);
  2583. /* check for tx fifo underflows */
  2584. for (i = 0; i < NFIFO; i++) {
  2585. delta =
  2586. (u16) (wlc->core->macstat_snapshot->txfunfl[i] -
  2587. txfunfl[i]);
  2588. if (delta)
  2589. brcms_err(wlc->hw->d11core,
  2590. "wl%d: %u tx fifo %d underflows!\n",
  2591. wlc->pub->unit, delta, i);
  2592. }
  2593. #endif /* DEBUG */
  2594. /* merge counters from dma module */
  2595. for (i = 0; i < NFIFO; i++) {
  2596. if (wlc->hw->di[i])
  2597. dma_counterreset(wlc->hw->di[i]);
  2598. }
  2599. }
  2600. static void brcms_b_reset(struct brcms_hardware *wlc_hw)
  2601. {
  2602. /* reset the core */
  2603. if (!brcms_deviceremoved(wlc_hw->wlc))
  2604. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  2605. /* purge the dma rings */
  2606. brcms_c_flushqueues(wlc_hw->wlc);
  2607. }
  2608. void brcms_c_reset(struct brcms_c_info *wlc)
  2609. {
  2610. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  2611. /* slurp up hw mac counters before core reset */
  2612. brcms_c_statsupd(wlc);
  2613. /* reset our snapshot of macstat counters */
  2614. memset(wlc->core->macstat_snapshot, 0, sizeof(struct macstat));
  2615. brcms_b_reset(wlc->hw);
  2616. }
  2617. void brcms_c_init_scb(struct scb *scb)
  2618. {
  2619. int i;
  2620. memset(scb, 0, sizeof(struct scb));
  2621. scb->flags = SCB_WMECAP | SCB_HTCAP;
  2622. for (i = 0; i < NUMPRIO; i++) {
  2623. scb->seqnum[i] = 0;
  2624. scb->seqctl[i] = 0xFFFF;
  2625. }
  2626. scb->seqctl_nonqos = 0xFFFF;
  2627. scb->magic = SCB_MAGIC;
  2628. }
  2629. /* d11 core init
  2630. * reset PSM
  2631. * download ucode/PCM
  2632. * let ucode run to suspended
  2633. * download ucode inits
  2634. * config other core registers
  2635. * init dma
  2636. */
  2637. static void brcms_b_coreinit(struct brcms_c_info *wlc)
  2638. {
  2639. struct brcms_hardware *wlc_hw = wlc->hw;
  2640. struct bcma_device *core = wlc_hw->d11core;
  2641. u32 sflags;
  2642. u32 bcnint_us;
  2643. uint i = 0;
  2644. bool fifosz_fixup = false;
  2645. int err = 0;
  2646. u16 buf[NFIFO];
  2647. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  2648. brcms_dbg_info(core, "wl%d: core init\n", wlc_hw->unit);
  2649. /* reset PSM */
  2650. brcms_b_mctrl(wlc_hw, ~0, (MCTL_IHR_EN | MCTL_PSM_JMP_0 | MCTL_WAKE));
  2651. brcms_ucode_download(wlc_hw);
  2652. /*
  2653. * FIFOSZ fixup. driver wants to controls the fifo allocation.
  2654. */
  2655. fifosz_fixup = true;
  2656. /* let the PSM run to the suspended state, set mode to BSS STA */
  2657. bcma_write32(core, D11REGOFFS(macintstatus), -1);
  2658. brcms_b_mctrl(wlc_hw, ~0,
  2659. (MCTL_IHR_EN | MCTL_INFRA | MCTL_PSM_RUN | MCTL_WAKE));
  2660. /* wait for ucode to self-suspend after auto-init */
  2661. SPINWAIT(((bcma_read32(core, D11REGOFFS(macintstatus)) &
  2662. MI_MACSSPNDD) == 0), 1000 * 1000);
  2663. if ((bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD) == 0)
  2664. brcms_err(core, "wl%d: wlc_coreinit: ucode did not self-"
  2665. "suspend!\n", wlc_hw->unit);
  2666. brcms_c_gpio_init(wlc);
  2667. sflags = bcma_aread32(core, BCMA_IOST);
  2668. if (D11REV_IS(wlc_hw->corerev, 17) || D11REV_IS(wlc_hw->corerev, 23)) {
  2669. if (BRCMS_ISNPHY(wlc_hw->band))
  2670. brcms_c_write_inits(wlc_hw, ucode->d11n0initvals16);
  2671. else
  2672. brcms_err(core, "%s: wl%d: unsupported phy in corerev"
  2673. " %d\n", __func__, wlc_hw->unit,
  2674. wlc_hw->corerev);
  2675. } else if (D11REV_IS(wlc_hw->corerev, 24)) {
  2676. if (BRCMS_ISLCNPHY(wlc_hw->band))
  2677. brcms_c_write_inits(wlc_hw, ucode->d11lcn0initvals24);
  2678. else
  2679. brcms_err(core, "%s: wl%d: unsupported phy in corerev"
  2680. " %d\n", __func__, wlc_hw->unit,
  2681. wlc_hw->corerev);
  2682. } else {
  2683. brcms_err(core, "%s: wl%d: unsupported corerev %d\n",
  2684. __func__, wlc_hw->unit, wlc_hw->corerev);
  2685. }
  2686. /* For old ucode, txfifo sizes needs to be modified(increased) */
  2687. if (fifosz_fixup)
  2688. brcms_b_corerev_fifofixup(wlc_hw);
  2689. /* check txfifo allocations match between ucode and driver */
  2690. buf[TX_AC_BE_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE0);
  2691. if (buf[TX_AC_BE_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]) {
  2692. i = TX_AC_BE_FIFO;
  2693. err = -1;
  2694. }
  2695. buf[TX_AC_VI_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE1);
  2696. if (buf[TX_AC_VI_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]) {
  2697. i = TX_AC_VI_FIFO;
  2698. err = -1;
  2699. }
  2700. buf[TX_AC_BK_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE2);
  2701. buf[TX_AC_VO_FIFO] = (buf[TX_AC_BK_FIFO] >> 8) & 0xff;
  2702. buf[TX_AC_BK_FIFO] &= 0xff;
  2703. if (buf[TX_AC_BK_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_BK_FIFO]) {
  2704. i = TX_AC_BK_FIFO;
  2705. err = -1;
  2706. }
  2707. if (buf[TX_AC_VO_FIFO] != wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO]) {
  2708. i = TX_AC_VO_FIFO;
  2709. err = -1;
  2710. }
  2711. buf[TX_BCMC_FIFO] = brcms_b_read_shm(wlc_hw, M_FIFOSIZE3);
  2712. buf[TX_ATIM_FIFO] = (buf[TX_BCMC_FIFO] >> 8) & 0xff;
  2713. buf[TX_BCMC_FIFO] &= 0xff;
  2714. if (buf[TX_BCMC_FIFO] != wlc_hw->xmtfifo_sz[TX_BCMC_FIFO]) {
  2715. i = TX_BCMC_FIFO;
  2716. err = -1;
  2717. }
  2718. if (buf[TX_ATIM_FIFO] != wlc_hw->xmtfifo_sz[TX_ATIM_FIFO]) {
  2719. i = TX_ATIM_FIFO;
  2720. err = -1;
  2721. }
  2722. if (err != 0)
  2723. brcms_err(core, "wlc_coreinit: txfifo mismatch: ucode size %d"
  2724. " driver size %d index %d\n", buf[i],
  2725. wlc_hw->xmtfifo_sz[i], i);
  2726. /* make sure we can still talk to the mac */
  2727. WARN_ON(bcma_read32(core, D11REGOFFS(maccontrol)) == 0xffffffff);
  2728. /* band-specific inits done by wlc_bsinit() */
  2729. /* Set up frame burst size and antenna swap threshold init values */
  2730. brcms_b_write_shm(wlc_hw, M_MBURST_SIZE, MAXTXFRAMEBURST);
  2731. brcms_b_write_shm(wlc_hw, M_MAX_ANTCNT, ANTCNT);
  2732. /* enable one rx interrupt per received frame */
  2733. bcma_write32(core, D11REGOFFS(intrcvlazy[0]), (1 << IRL_FC_SHIFT));
  2734. /* set the station mode (BSS STA) */
  2735. brcms_b_mctrl(wlc_hw,
  2736. (MCTL_INFRA | MCTL_DISCARD_PMQ | MCTL_AP),
  2737. (MCTL_INFRA | MCTL_DISCARD_PMQ));
  2738. /* set up Beacon interval */
  2739. bcnint_us = 0x8000 << 10;
  2740. bcma_write32(core, D11REGOFFS(tsf_cfprep),
  2741. (bcnint_us << CFPREP_CBI_SHIFT));
  2742. bcma_write32(core, D11REGOFFS(tsf_cfpstart), bcnint_us);
  2743. bcma_write32(core, D11REGOFFS(macintstatus), MI_GP1);
  2744. /* write interrupt mask */
  2745. bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intmask),
  2746. DEF_RXINTMASK);
  2747. /* allow the MAC to control the PHY clock (dynamic on/off) */
  2748. brcms_b_macphyclk_set(wlc_hw, ON);
  2749. /* program dynamic clock control fast powerup delay register */
  2750. wlc->fastpwrup_dly = ai_clkctl_fast_pwrup_delay(wlc_hw->sih);
  2751. bcma_write16(core, D11REGOFFS(scc_fastpwrup_dly), wlc->fastpwrup_dly);
  2752. /* tell the ucode the corerev */
  2753. brcms_b_write_shm(wlc_hw, M_MACHW_VER, (u16) wlc_hw->corerev);
  2754. /* tell the ucode MAC capabilities */
  2755. brcms_b_write_shm(wlc_hw, M_MACHW_CAP_L,
  2756. (u16) (wlc_hw->machwcap & 0xffff));
  2757. brcms_b_write_shm(wlc_hw, M_MACHW_CAP_H,
  2758. (u16) ((wlc_hw->
  2759. machwcap >> 16) & 0xffff));
  2760. /* write retry limits to SCR, this done after PSM init */
  2761. bcma_write32(core, D11REGOFFS(objaddr),
  2762. OBJADDR_SCR_SEL | S_DOT11_SRC_LMT);
  2763. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2764. bcma_write32(core, D11REGOFFS(objdata), wlc_hw->SRL);
  2765. bcma_write32(core, D11REGOFFS(objaddr),
  2766. OBJADDR_SCR_SEL | S_DOT11_LRC_LMT);
  2767. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2768. bcma_write32(core, D11REGOFFS(objdata), wlc_hw->LRL);
  2769. /* write rate fallback retry limits */
  2770. brcms_b_write_shm(wlc_hw, M_SFRMTXCNTFBRTHSD, wlc_hw->SFBL);
  2771. brcms_b_write_shm(wlc_hw, M_LFRMTXCNTFBRTHSD, wlc_hw->LFBL);
  2772. bcma_mask16(core, D11REGOFFS(ifs_ctl), 0x0FFF);
  2773. bcma_write16(core, D11REGOFFS(ifs_aifsn), EDCF_AIFSN_MIN);
  2774. /* init the tx dma engines */
  2775. for (i = 0; i < NFIFO; i++) {
  2776. if (wlc_hw->di[i])
  2777. dma_txinit(wlc_hw->di[i]);
  2778. }
  2779. /* init the rx dma engine(s) and post receive buffers */
  2780. dma_rxinit(wlc_hw->di[RX_FIFO]);
  2781. dma_rxfill(wlc_hw->di[RX_FIFO]);
  2782. }
  2783. void
  2784. static brcms_b_init(struct brcms_hardware *wlc_hw, u16 chanspec) {
  2785. u32 macintmask;
  2786. bool fastclk;
  2787. struct brcms_c_info *wlc = wlc_hw->wlc;
  2788. /* request FAST clock if not on */
  2789. fastclk = wlc_hw->forcefastclk;
  2790. if (!fastclk)
  2791. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  2792. /* disable interrupts */
  2793. macintmask = brcms_intrsoff(wlc->wl);
  2794. /* set up the specified band and chanspec */
  2795. brcms_c_setxband(wlc_hw, chspec_bandunit(chanspec));
  2796. wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
  2797. /* do one-time phy inits and calibration */
  2798. wlc_phy_cal_init(wlc_hw->band->pi);
  2799. /* core-specific initialization */
  2800. brcms_b_coreinit(wlc);
  2801. /* band-specific inits */
  2802. brcms_b_bsinit(wlc, chanspec);
  2803. /* restore macintmask */
  2804. brcms_intrsrestore(wlc->wl, macintmask);
  2805. /* seed wake_override with BRCMS_WAKE_OVERRIDE_MACSUSPEND since the mac
  2806. * is suspended and brcms_c_enable_mac() will clear this override bit.
  2807. */
  2808. mboolset(wlc_hw->wake_override, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2809. /*
  2810. * initialize mac_suspend_depth to 1 to match ucode
  2811. * initial suspended state
  2812. */
  2813. wlc_hw->mac_suspend_depth = 1;
  2814. /* restore the clk */
  2815. if (!fastclk)
  2816. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  2817. }
  2818. static void brcms_c_set_phy_chanspec(struct brcms_c_info *wlc,
  2819. u16 chanspec)
  2820. {
  2821. /* Save our copy of the chanspec */
  2822. wlc->chanspec = chanspec;
  2823. /* Set the chanspec and power limits for this locale */
  2824. brcms_c_channel_set_chanspec(wlc->cmi, chanspec, BRCMS_TXPWR_MAX);
  2825. if (wlc->stf->ss_algosel_auto)
  2826. brcms_c_stf_ss_algo_channel_get(wlc, &wlc->stf->ss_algo_channel,
  2827. chanspec);
  2828. brcms_c_stf_ss_update(wlc, wlc->band);
  2829. }
  2830. static void
  2831. brcms_default_rateset(struct brcms_c_info *wlc, struct brcms_c_rateset *rs)
  2832. {
  2833. brcms_c_rateset_default(rs, NULL, wlc->band->phytype,
  2834. wlc->band->bandtype, false, BRCMS_RATE_MASK_FULL,
  2835. (bool) (wlc->pub->_n_enab & SUPPORT_11N),
  2836. brcms_chspec_bw(wlc->default_bss->chanspec),
  2837. wlc->stf->txstreams);
  2838. }
  2839. /* derive wlc->band->basic_rate[] table from 'rateset' */
  2840. static void brcms_c_rate_lookup_init(struct brcms_c_info *wlc,
  2841. struct brcms_c_rateset *rateset)
  2842. {
  2843. u8 rate;
  2844. u8 mandatory;
  2845. u8 cck_basic = 0;
  2846. u8 ofdm_basic = 0;
  2847. u8 *br = wlc->band->basic_rate;
  2848. uint i;
  2849. /* incoming rates are in 500kbps units as in 802.11 Supported Rates */
  2850. memset(br, 0, BRCM_MAXRATE + 1);
  2851. /* For each basic rate in the rates list, make an entry in the
  2852. * best basic lookup.
  2853. */
  2854. for (i = 0; i < rateset->count; i++) {
  2855. /* only make an entry for a basic rate */
  2856. if (!(rateset->rates[i] & BRCMS_RATE_FLAG))
  2857. continue;
  2858. /* mask off basic bit */
  2859. rate = (rateset->rates[i] & BRCMS_RATE_MASK);
  2860. if (rate > BRCM_MAXRATE) {
  2861. brcms_err(wlc->hw->d11core, "brcms_c_rate_lookup_init: "
  2862. "invalid rate 0x%X in rate set\n",
  2863. rateset->rates[i]);
  2864. continue;
  2865. }
  2866. br[rate] = rate;
  2867. }
  2868. /* The rate lookup table now has non-zero entries for each
  2869. * basic rate, equal to the basic rate: br[basicN] = basicN
  2870. *
  2871. * To look up the best basic rate corresponding to any
  2872. * particular rate, code can use the basic_rate table
  2873. * like this
  2874. *
  2875. * basic_rate = wlc->band->basic_rate[tx_rate]
  2876. *
  2877. * Make sure there is a best basic rate entry for
  2878. * every rate by walking up the table from low rates
  2879. * to high, filling in holes in the lookup table
  2880. */
  2881. for (i = 0; i < wlc->band->hw_rateset.count; i++) {
  2882. rate = wlc->band->hw_rateset.rates[i];
  2883. if (br[rate] != 0) {
  2884. /* This rate is a basic rate.
  2885. * Keep track of the best basic rate so far by
  2886. * modulation type.
  2887. */
  2888. if (is_ofdm_rate(rate))
  2889. ofdm_basic = rate;
  2890. else
  2891. cck_basic = rate;
  2892. continue;
  2893. }
  2894. /* This rate is not a basic rate so figure out the
  2895. * best basic rate less than this rate and fill in
  2896. * the hole in the table
  2897. */
  2898. br[rate] = is_ofdm_rate(rate) ? ofdm_basic : cck_basic;
  2899. if (br[rate] != 0)
  2900. continue;
  2901. if (is_ofdm_rate(rate)) {
  2902. /*
  2903. * In 11g and 11a, the OFDM mandatory rates
  2904. * are 6, 12, and 24 Mbps
  2905. */
  2906. if (rate >= BRCM_RATE_24M)
  2907. mandatory = BRCM_RATE_24M;
  2908. else if (rate >= BRCM_RATE_12M)
  2909. mandatory = BRCM_RATE_12M;
  2910. else
  2911. mandatory = BRCM_RATE_6M;
  2912. } else {
  2913. /* In 11b, all CCK rates are mandatory 1 - 11 Mbps */
  2914. mandatory = rate;
  2915. }
  2916. br[rate] = mandatory;
  2917. }
  2918. }
  2919. static void brcms_c_bandinit_ordered(struct brcms_c_info *wlc,
  2920. u16 chanspec)
  2921. {
  2922. struct brcms_c_rateset default_rateset;
  2923. uint parkband;
  2924. uint i, band_order[2];
  2925. /*
  2926. * We might have been bandlocked during down and the chip
  2927. * power-cycled (hibernate). Figure out the right band to park on
  2928. */
  2929. if (wlc->bandlocked || wlc->pub->_nbands == 1) {
  2930. /* updated in brcms_c_bandlock() */
  2931. parkband = wlc->band->bandunit;
  2932. band_order[0] = band_order[1] = parkband;
  2933. } else {
  2934. /* park on the band of the specified chanspec */
  2935. parkband = chspec_bandunit(chanspec);
  2936. /* order so that parkband initialize last */
  2937. band_order[0] = parkband ^ 1;
  2938. band_order[1] = parkband;
  2939. }
  2940. /* make each band operational, software state init */
  2941. for (i = 0; i < wlc->pub->_nbands; i++) {
  2942. uint j = band_order[i];
  2943. wlc->band = wlc->bandstate[j];
  2944. brcms_default_rateset(wlc, &default_rateset);
  2945. /* fill in hw_rate */
  2946. brcms_c_rateset_filter(&default_rateset, &wlc->band->hw_rateset,
  2947. false, BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
  2948. (bool) (wlc->pub->_n_enab & SUPPORT_11N));
  2949. /* init basic rate lookup */
  2950. brcms_c_rate_lookup_init(wlc, &default_rateset);
  2951. }
  2952. /* sync up phy/radio chanspec */
  2953. brcms_c_set_phy_chanspec(wlc, chanspec);
  2954. }
  2955. /*
  2956. * Set or clear filtering related maccontrol bits based on
  2957. * specified filter flags
  2958. */
  2959. void brcms_c_mac_promisc(struct brcms_c_info *wlc, uint filter_flags)
  2960. {
  2961. u32 promisc_bits = 0;
  2962. wlc->filter_flags = filter_flags;
  2963. if (filter_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  2964. promisc_bits |= MCTL_PROMISC;
  2965. if (filter_flags & FIF_BCN_PRBRESP_PROMISC)
  2966. promisc_bits |= MCTL_BCNS_PROMISC;
  2967. if (filter_flags & FIF_FCSFAIL)
  2968. promisc_bits |= MCTL_KEEPBADFCS;
  2969. if (filter_flags & (FIF_CONTROL | FIF_PSPOLL))
  2970. promisc_bits |= MCTL_KEEPCONTROL;
  2971. brcms_b_mctrl(wlc->hw,
  2972. MCTL_PROMISC | MCTL_BCNS_PROMISC |
  2973. MCTL_KEEPCONTROL | MCTL_KEEPBADFCS,
  2974. promisc_bits);
  2975. }
  2976. /*
  2977. * ucode, hwmac update
  2978. * Channel dependent updates for ucode and hw
  2979. */
  2980. static void brcms_c_ucode_mac_upd(struct brcms_c_info *wlc)
  2981. {
  2982. /* enable or disable any active IBSSs depending on whether or not
  2983. * we are on the home channel
  2984. */
  2985. if (wlc->home_chanspec == wlc_phy_chanspec_get(wlc->band->pi)) {
  2986. if (wlc->pub->associated) {
  2987. /*
  2988. * BMAC_NOTE: This is something that should be fixed
  2989. * in ucode inits. I think that the ucode inits set
  2990. * up the bcn templates and shm values with a bogus
  2991. * beacon. This should not be done in the inits. If
  2992. * ucode needs to set up a beacon for testing, the
  2993. * test routines should write it down, not expect the
  2994. * inits to populate a bogus beacon.
  2995. */
  2996. if (BRCMS_PHY_11N_CAP(wlc->band))
  2997. brcms_b_write_shm(wlc->hw,
  2998. M_BCN_TXTSF_OFFSET, 0);
  2999. }
  3000. } else {
  3001. /* disable an active IBSS if we are not on the home channel */
  3002. }
  3003. }
  3004. static void brcms_c_write_rate_shm(struct brcms_c_info *wlc, u8 rate,
  3005. u8 basic_rate)
  3006. {
  3007. u8 phy_rate, index;
  3008. u8 basic_phy_rate, basic_index;
  3009. u16 dir_table, basic_table;
  3010. u16 basic_ptr;
  3011. /* Shared memory address for the table we are reading */
  3012. dir_table = is_ofdm_rate(basic_rate) ? M_RT_DIRMAP_A : M_RT_DIRMAP_B;
  3013. /* Shared memory address for the table we are writing */
  3014. basic_table = is_ofdm_rate(rate) ? M_RT_BBRSMAP_A : M_RT_BBRSMAP_B;
  3015. /*
  3016. * for a given rate, the LS-nibble of the PLCP SIGNAL field is
  3017. * the index into the rate table.
  3018. */
  3019. phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
  3020. basic_phy_rate = rate_info[basic_rate] & BRCMS_RATE_MASK;
  3021. index = phy_rate & 0xf;
  3022. basic_index = basic_phy_rate & 0xf;
  3023. /* Find the SHM pointer to the ACK rate entry by looking in the
  3024. * Direct-map Table
  3025. */
  3026. basic_ptr = brcms_b_read_shm(wlc->hw, (dir_table + basic_index * 2));
  3027. /* Update the SHM BSS-basic-rate-set mapping table with the pointer
  3028. * to the correct basic rate for the given incoming rate
  3029. */
  3030. brcms_b_write_shm(wlc->hw, (basic_table + index * 2), basic_ptr);
  3031. }
  3032. static const struct brcms_c_rateset *
  3033. brcms_c_rateset_get_hwrs(struct brcms_c_info *wlc)
  3034. {
  3035. const struct brcms_c_rateset *rs_dflt;
  3036. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  3037. if (wlc->band->bandtype == BRCM_BAND_5G)
  3038. rs_dflt = &ofdm_mimo_rates;
  3039. else
  3040. rs_dflt = &cck_ofdm_mimo_rates;
  3041. } else if (wlc->band->gmode)
  3042. rs_dflt = &cck_ofdm_rates;
  3043. else
  3044. rs_dflt = &cck_rates;
  3045. return rs_dflt;
  3046. }
  3047. static void brcms_c_set_ratetable(struct brcms_c_info *wlc)
  3048. {
  3049. const struct brcms_c_rateset *rs_dflt;
  3050. struct brcms_c_rateset rs;
  3051. u8 rate, basic_rate;
  3052. uint i;
  3053. rs_dflt = brcms_c_rateset_get_hwrs(wlc);
  3054. brcms_c_rateset_copy(rs_dflt, &rs);
  3055. brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
  3056. /* walk the phy rate table and update SHM basic rate lookup table */
  3057. for (i = 0; i < rs.count; i++) {
  3058. rate = rs.rates[i] & BRCMS_RATE_MASK;
  3059. /* for a given rate brcms_basic_rate returns the rate at
  3060. * which a response ACK/CTS should be sent.
  3061. */
  3062. basic_rate = brcms_basic_rate(wlc, rate);
  3063. if (basic_rate == 0)
  3064. /* This should only happen if we are using a
  3065. * restricted rateset.
  3066. */
  3067. basic_rate = rs.rates[0] & BRCMS_RATE_MASK;
  3068. brcms_c_write_rate_shm(wlc, rate, basic_rate);
  3069. }
  3070. }
  3071. /* band-specific init */
  3072. static void brcms_c_bsinit(struct brcms_c_info *wlc)
  3073. {
  3074. brcms_dbg_info(wlc->hw->d11core, "wl%d: bandunit %d\n",
  3075. wlc->pub->unit, wlc->band->bandunit);
  3076. /* write ucode ACK/CTS rate table */
  3077. brcms_c_set_ratetable(wlc);
  3078. /* update some band specific mac configuration */
  3079. brcms_c_ucode_mac_upd(wlc);
  3080. /* init antenna selection */
  3081. brcms_c_antsel_init(wlc->asi);
  3082. }
  3083. /* formula: IDLE_BUSY_RATIO_X_16 = (100-duty_cycle)/duty_cycle*16 */
  3084. static int
  3085. brcms_c_duty_cycle_set(struct brcms_c_info *wlc, int duty_cycle, bool isOFDM,
  3086. bool writeToShm)
  3087. {
  3088. int idle_busy_ratio_x_16 = 0;
  3089. uint offset =
  3090. isOFDM ? M_TX_IDLE_BUSY_RATIO_X_16_OFDM :
  3091. M_TX_IDLE_BUSY_RATIO_X_16_CCK;
  3092. if (duty_cycle > 100 || duty_cycle < 0) {
  3093. brcms_err(wlc->hw->d11core,
  3094. "wl%d: duty cycle value off limit\n",
  3095. wlc->pub->unit);
  3096. return -EINVAL;
  3097. }
  3098. if (duty_cycle)
  3099. idle_busy_ratio_x_16 = (100 - duty_cycle) * 16 / duty_cycle;
  3100. /* Only write to shared memory when wl is up */
  3101. if (writeToShm)
  3102. brcms_b_write_shm(wlc->hw, offset, (u16) idle_busy_ratio_x_16);
  3103. if (isOFDM)
  3104. wlc->tx_duty_cycle_ofdm = (u16) duty_cycle;
  3105. else
  3106. wlc->tx_duty_cycle_cck = (u16) duty_cycle;
  3107. return 0;
  3108. }
  3109. /* push sw hps and wake state through hardware */
  3110. static void brcms_c_set_ps_ctrl(struct brcms_c_info *wlc)
  3111. {
  3112. u32 v1, v2;
  3113. bool hps;
  3114. bool awake_before;
  3115. hps = brcms_c_ps_allowed(wlc);
  3116. brcms_dbg_mac80211(wlc->hw->d11core, "wl%d: hps %d\n", wlc->pub->unit,
  3117. hps);
  3118. v1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(maccontrol));
  3119. v2 = MCTL_WAKE;
  3120. if (hps)
  3121. v2 |= MCTL_HPS;
  3122. brcms_b_mctrl(wlc->hw, MCTL_WAKE | MCTL_HPS, v2);
  3123. awake_before = ((v1 & MCTL_WAKE) || ((v1 & MCTL_HPS) == 0));
  3124. if (!awake_before)
  3125. brcms_b_wait_for_wake(wlc->hw);
  3126. }
  3127. /*
  3128. * Write this BSS config's MAC address to core.
  3129. * Updates RXE match engine.
  3130. */
  3131. static int brcms_c_set_mac(struct brcms_bss_cfg *bsscfg)
  3132. {
  3133. int err = 0;
  3134. struct brcms_c_info *wlc = bsscfg->wlc;
  3135. /* enter the MAC addr into the RXE match registers */
  3136. brcms_c_set_addrmatch(wlc, RCM_MAC_OFFSET, bsscfg->cur_etheraddr);
  3137. brcms_c_ampdu_macaddr_upd(wlc);
  3138. return err;
  3139. }
  3140. /* Write the BSS config's BSSID address to core (set_bssid in d11procs.tcl).
  3141. * Updates RXE match engine.
  3142. */
  3143. static void brcms_c_set_bssid(struct brcms_bss_cfg *bsscfg)
  3144. {
  3145. /* we need to update BSSID in RXE match registers */
  3146. brcms_c_set_addrmatch(bsscfg->wlc, RCM_BSSID_OFFSET, bsscfg->BSSID);
  3147. }
  3148. static void brcms_b_set_shortslot(struct brcms_hardware *wlc_hw, bool shortslot)
  3149. {
  3150. wlc_hw->shortslot = shortslot;
  3151. if (wlc_hw->band->bandtype == BRCM_BAND_2G && wlc_hw->up) {
  3152. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  3153. brcms_b_update_slot_timing(wlc_hw, shortslot);
  3154. brcms_c_enable_mac(wlc_hw->wlc);
  3155. }
  3156. }
  3157. /*
  3158. * Suspend the the MAC and update the slot timing
  3159. * for standard 11b/g (20us slots) or shortslot 11g (9us slots).
  3160. */
  3161. static void brcms_c_switch_shortslot(struct brcms_c_info *wlc, bool shortslot)
  3162. {
  3163. /* use the override if it is set */
  3164. if (wlc->shortslot_override != BRCMS_SHORTSLOT_AUTO)
  3165. shortslot = (wlc->shortslot_override == BRCMS_SHORTSLOT_ON);
  3166. if (wlc->shortslot == shortslot)
  3167. return;
  3168. wlc->shortslot = shortslot;
  3169. brcms_b_set_shortslot(wlc->hw, shortslot);
  3170. }
  3171. static void brcms_c_set_home_chanspec(struct brcms_c_info *wlc, u16 chanspec)
  3172. {
  3173. if (wlc->home_chanspec != chanspec) {
  3174. wlc->home_chanspec = chanspec;
  3175. if (wlc->bsscfg->associated)
  3176. wlc->bsscfg->current_bss->chanspec = chanspec;
  3177. }
  3178. }
  3179. void
  3180. brcms_b_set_chanspec(struct brcms_hardware *wlc_hw, u16 chanspec,
  3181. bool mute_tx, struct txpwr_limits *txpwr)
  3182. {
  3183. uint bandunit;
  3184. brcms_dbg_mac80211(wlc_hw->d11core, "wl%d: 0x%x\n", wlc_hw->unit,
  3185. chanspec);
  3186. wlc_hw->chanspec = chanspec;
  3187. /* Switch bands if necessary */
  3188. if (wlc_hw->_nbands > 1) {
  3189. bandunit = chspec_bandunit(chanspec);
  3190. if (wlc_hw->band->bandunit != bandunit) {
  3191. /* brcms_b_setband disables other bandunit,
  3192. * use light band switch if not up yet
  3193. */
  3194. if (wlc_hw->up) {
  3195. wlc_phy_chanspec_radio_set(wlc_hw->
  3196. bandstate[bandunit]->
  3197. pi, chanspec);
  3198. brcms_b_setband(wlc_hw, bandunit, chanspec);
  3199. } else {
  3200. brcms_c_setxband(wlc_hw, bandunit);
  3201. }
  3202. }
  3203. }
  3204. wlc_phy_initcal_enable(wlc_hw->band->pi, !mute_tx);
  3205. if (!wlc_hw->up) {
  3206. if (wlc_hw->clk)
  3207. wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr,
  3208. chanspec);
  3209. wlc_phy_chanspec_radio_set(wlc_hw->band->pi, chanspec);
  3210. } else {
  3211. wlc_phy_chanspec_set(wlc_hw->band->pi, chanspec);
  3212. wlc_phy_txpower_limit_set(wlc_hw->band->pi, txpwr, chanspec);
  3213. /* Update muting of the channel */
  3214. brcms_b_mute(wlc_hw, mute_tx);
  3215. }
  3216. }
  3217. /* switch to and initialize new band */
  3218. static void brcms_c_setband(struct brcms_c_info *wlc,
  3219. uint bandunit)
  3220. {
  3221. wlc->band = wlc->bandstate[bandunit];
  3222. if (!wlc->pub->up)
  3223. return;
  3224. /* wait for at least one beacon before entering sleeping state */
  3225. brcms_c_set_ps_ctrl(wlc);
  3226. /* band-specific initializations */
  3227. brcms_c_bsinit(wlc);
  3228. }
  3229. static void brcms_c_set_chanspec(struct brcms_c_info *wlc, u16 chanspec)
  3230. {
  3231. uint bandunit;
  3232. bool switchband = false;
  3233. u16 old_chanspec = wlc->chanspec;
  3234. if (!brcms_c_valid_chanspec_db(wlc->cmi, chanspec)) {
  3235. brcms_err(wlc->hw->d11core, "wl%d: %s: Bad channel %d\n",
  3236. wlc->pub->unit, __func__, CHSPEC_CHANNEL(chanspec));
  3237. return;
  3238. }
  3239. /* Switch bands if necessary */
  3240. if (wlc->pub->_nbands > 1) {
  3241. bandunit = chspec_bandunit(chanspec);
  3242. if (wlc->band->bandunit != bandunit || wlc->bandinit_pending) {
  3243. switchband = true;
  3244. if (wlc->bandlocked) {
  3245. brcms_err(wlc->hw->d11core,
  3246. "wl%d: %s: chspec %d band is locked!\n",
  3247. wlc->pub->unit, __func__,
  3248. CHSPEC_CHANNEL(chanspec));
  3249. return;
  3250. }
  3251. /*
  3252. * should the setband call come after the
  3253. * brcms_b_chanspec() ? if the setband updates
  3254. * (brcms_c_bsinit) use low level calls to inspect and
  3255. * set state, the state inspected may be from the wrong
  3256. * band, or the following brcms_b_set_chanspec() may
  3257. * undo the work.
  3258. */
  3259. brcms_c_setband(wlc, bandunit);
  3260. }
  3261. }
  3262. /* sync up phy/radio chanspec */
  3263. brcms_c_set_phy_chanspec(wlc, chanspec);
  3264. /* init antenna selection */
  3265. if (brcms_chspec_bw(old_chanspec) != brcms_chspec_bw(chanspec)) {
  3266. brcms_c_antsel_init(wlc->asi);
  3267. /* Fix the hardware rateset based on bw.
  3268. * Mainly add MCS32 for 40Mhz, remove MCS 32 for 20Mhz
  3269. */
  3270. brcms_c_rateset_bw_mcs_filter(&wlc->band->hw_rateset,
  3271. wlc->band->mimo_cap_40 ? brcms_chspec_bw(chanspec) : 0);
  3272. }
  3273. /* update some mac configuration since chanspec changed */
  3274. brcms_c_ucode_mac_upd(wlc);
  3275. }
  3276. /*
  3277. * This function changes the phytxctl for beacon based on current
  3278. * beacon ratespec AND txant setting as per this table:
  3279. * ratespec CCK ant = wlc->stf->txant
  3280. * OFDM ant = 3
  3281. */
  3282. void brcms_c_beacon_phytxctl_txant_upd(struct brcms_c_info *wlc,
  3283. u32 bcn_rspec)
  3284. {
  3285. u16 phyctl;
  3286. u16 phytxant = wlc->stf->phytxant;
  3287. u16 mask = PHY_TXC_ANT_MASK;
  3288. /* for non-siso rates or default setting, use the available chains */
  3289. if (BRCMS_PHY_11N_CAP(wlc->band))
  3290. phytxant = brcms_c_stf_phytxchain_sel(wlc, bcn_rspec);
  3291. phyctl = brcms_b_read_shm(wlc->hw, M_BCN_PCTLWD);
  3292. phyctl = (phyctl & ~mask) | phytxant;
  3293. brcms_b_write_shm(wlc->hw, M_BCN_PCTLWD, phyctl);
  3294. }
  3295. /*
  3296. * centralized protection config change function to simplify debugging, no
  3297. * consistency checking this should be called only on changes to avoid overhead
  3298. * in periodic function
  3299. */
  3300. void brcms_c_protection_upd(struct brcms_c_info *wlc, uint idx, int val)
  3301. {
  3302. /*
  3303. * Cannot use brcms_dbg_* here because this function is called
  3304. * before wlc is sufficiently initialized.
  3305. */
  3306. BCMMSG(wlc->wiphy, "idx %d, val %d\n", idx, val);
  3307. switch (idx) {
  3308. case BRCMS_PROT_G_SPEC:
  3309. wlc->protection->_g = (bool) val;
  3310. break;
  3311. case BRCMS_PROT_G_OVR:
  3312. wlc->protection->g_override = (s8) val;
  3313. break;
  3314. case BRCMS_PROT_G_USER:
  3315. wlc->protection->gmode_user = (u8) val;
  3316. break;
  3317. case BRCMS_PROT_OVERLAP:
  3318. wlc->protection->overlap = (s8) val;
  3319. break;
  3320. case BRCMS_PROT_N_USER:
  3321. wlc->protection->nmode_user = (s8) val;
  3322. break;
  3323. case BRCMS_PROT_N_CFG:
  3324. wlc->protection->n_cfg = (s8) val;
  3325. break;
  3326. case BRCMS_PROT_N_CFG_OVR:
  3327. wlc->protection->n_cfg_override = (s8) val;
  3328. break;
  3329. case BRCMS_PROT_N_NONGF:
  3330. wlc->protection->nongf = (bool) val;
  3331. break;
  3332. case BRCMS_PROT_N_NONGF_OVR:
  3333. wlc->protection->nongf_override = (s8) val;
  3334. break;
  3335. case BRCMS_PROT_N_PAM_OVR:
  3336. wlc->protection->n_pam_override = (s8) val;
  3337. break;
  3338. case BRCMS_PROT_N_OBSS:
  3339. wlc->protection->n_obss = (bool) val;
  3340. break;
  3341. default:
  3342. break;
  3343. }
  3344. }
  3345. static void brcms_c_ht_update_sgi_rx(struct brcms_c_info *wlc, int val)
  3346. {
  3347. if (wlc->pub->up) {
  3348. brcms_c_update_beacon(wlc);
  3349. brcms_c_update_probe_resp(wlc, true);
  3350. }
  3351. }
  3352. static void brcms_c_ht_update_ldpc(struct brcms_c_info *wlc, s8 val)
  3353. {
  3354. wlc->stf->ldpc = val;
  3355. if (wlc->pub->up) {
  3356. brcms_c_update_beacon(wlc);
  3357. brcms_c_update_probe_resp(wlc, true);
  3358. wlc_phy_ldpc_override_set(wlc->band->pi, (val ? true : false));
  3359. }
  3360. }
  3361. void brcms_c_wme_setparams(struct brcms_c_info *wlc, u16 aci,
  3362. const struct ieee80211_tx_queue_params *params,
  3363. bool suspend)
  3364. {
  3365. int i;
  3366. struct shm_acparams acp_shm;
  3367. u16 *shm_entry;
  3368. /* Only apply params if the core is out of reset and has clocks */
  3369. if (!wlc->clk) {
  3370. brcms_err(wlc->hw->d11core, "wl%d: %s : no-clock\n",
  3371. wlc->pub->unit, __func__);
  3372. return;
  3373. }
  3374. memset(&acp_shm, 0, sizeof(struct shm_acparams));
  3375. /* fill in shm ac params struct */
  3376. acp_shm.txop = params->txop;
  3377. /* convert from units of 32us to us for ucode */
  3378. wlc->edcf_txop[aci & 0x3] = acp_shm.txop =
  3379. EDCF_TXOP2USEC(acp_shm.txop);
  3380. acp_shm.aifs = (params->aifs & EDCF_AIFSN_MASK);
  3381. if (aci == IEEE80211_AC_VI && acp_shm.txop == 0
  3382. && acp_shm.aifs < EDCF_AIFSN_MAX)
  3383. acp_shm.aifs++;
  3384. if (acp_shm.aifs < EDCF_AIFSN_MIN
  3385. || acp_shm.aifs > EDCF_AIFSN_MAX) {
  3386. brcms_err(wlc->hw->d11core, "wl%d: edcf_setparams: bad "
  3387. "aifs %d\n", wlc->pub->unit, acp_shm.aifs);
  3388. } else {
  3389. acp_shm.cwmin = params->cw_min;
  3390. acp_shm.cwmax = params->cw_max;
  3391. acp_shm.cwcur = acp_shm.cwmin;
  3392. acp_shm.bslots =
  3393. bcma_read16(wlc->hw->d11core, D11REGOFFS(tsf_random)) &
  3394. acp_shm.cwcur;
  3395. acp_shm.reggap = acp_shm.bslots + acp_shm.aifs;
  3396. /* Indicate the new params to the ucode */
  3397. acp_shm.status = brcms_b_read_shm(wlc->hw, (M_EDCF_QINFO +
  3398. wme_ac2fifo[aci] *
  3399. M_EDCF_QLEN +
  3400. M_EDCF_STATUS_OFF));
  3401. acp_shm.status |= WME_STATUS_NEWAC;
  3402. /* Fill in shm acparam table */
  3403. shm_entry = (u16 *) &acp_shm;
  3404. for (i = 0; i < (int)sizeof(struct shm_acparams); i += 2)
  3405. brcms_b_write_shm(wlc->hw,
  3406. M_EDCF_QINFO +
  3407. wme_ac2fifo[aci] * M_EDCF_QLEN + i,
  3408. *shm_entry++);
  3409. }
  3410. if (suspend) {
  3411. brcms_c_suspend_mac_and_wait(wlc);
  3412. brcms_c_enable_mac(wlc);
  3413. }
  3414. }
  3415. static void brcms_c_edcf_setparams(struct brcms_c_info *wlc, bool suspend)
  3416. {
  3417. u16 aci;
  3418. int i_ac;
  3419. struct ieee80211_tx_queue_params txq_pars;
  3420. static const struct edcf_acparam default_edcf_acparams[] = {
  3421. {EDCF_AC_BE_ACI_STA, EDCF_AC_BE_ECW_STA, EDCF_AC_BE_TXOP_STA},
  3422. {EDCF_AC_BK_ACI_STA, EDCF_AC_BK_ECW_STA, EDCF_AC_BK_TXOP_STA},
  3423. {EDCF_AC_VI_ACI_STA, EDCF_AC_VI_ECW_STA, EDCF_AC_VI_TXOP_STA},
  3424. {EDCF_AC_VO_ACI_STA, EDCF_AC_VO_ECW_STA, EDCF_AC_VO_TXOP_STA}
  3425. }; /* ucode needs these parameters during its initialization */
  3426. const struct edcf_acparam *edcf_acp = &default_edcf_acparams[0];
  3427. for (i_ac = 0; i_ac < IEEE80211_NUM_ACS; i_ac++, edcf_acp++) {
  3428. /* find out which ac this set of params applies to */
  3429. aci = (edcf_acp->ACI & EDCF_ACI_MASK) >> EDCF_ACI_SHIFT;
  3430. /* fill in shm ac params struct */
  3431. txq_pars.txop = edcf_acp->TXOP;
  3432. txq_pars.aifs = edcf_acp->ACI;
  3433. /* CWmin = 2^(ECWmin) - 1 */
  3434. txq_pars.cw_min = EDCF_ECW2CW(edcf_acp->ECW & EDCF_ECWMIN_MASK);
  3435. /* CWmax = 2^(ECWmax) - 1 */
  3436. txq_pars.cw_max = EDCF_ECW2CW((edcf_acp->ECW & EDCF_ECWMAX_MASK)
  3437. >> EDCF_ECWMAX_SHIFT);
  3438. brcms_c_wme_setparams(wlc, aci, &txq_pars, suspend);
  3439. }
  3440. if (suspend) {
  3441. brcms_c_suspend_mac_and_wait(wlc);
  3442. brcms_c_enable_mac(wlc);
  3443. }
  3444. }
  3445. static void brcms_c_radio_monitor_start(struct brcms_c_info *wlc)
  3446. {
  3447. /* Don't start the timer if HWRADIO feature is disabled */
  3448. if (wlc->radio_monitor)
  3449. return;
  3450. wlc->radio_monitor = true;
  3451. brcms_b_pllreq(wlc->hw, true, BRCMS_PLLREQ_RADIO_MON);
  3452. brcms_add_timer(wlc->radio_timer, TIMER_INTERVAL_RADIOCHK, true);
  3453. }
  3454. static bool brcms_c_radio_monitor_stop(struct brcms_c_info *wlc)
  3455. {
  3456. if (!wlc->radio_monitor)
  3457. return true;
  3458. wlc->radio_monitor = false;
  3459. brcms_b_pllreq(wlc->hw, false, BRCMS_PLLREQ_RADIO_MON);
  3460. return brcms_del_timer(wlc->radio_timer);
  3461. }
  3462. /* read hwdisable state and propagate to wlc flag */
  3463. static void brcms_c_radio_hwdisable_upd(struct brcms_c_info *wlc)
  3464. {
  3465. if (wlc->pub->hw_off)
  3466. return;
  3467. if (brcms_b_radio_read_hwdisabled(wlc->hw))
  3468. mboolset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
  3469. else
  3470. mboolclr(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE);
  3471. }
  3472. /* update hwradio status and return it */
  3473. bool brcms_c_check_radio_disabled(struct brcms_c_info *wlc)
  3474. {
  3475. brcms_c_radio_hwdisable_upd(wlc);
  3476. return mboolisset(wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE) ?
  3477. true : false;
  3478. }
  3479. /* periodical query hw radio button while driver is "down" */
  3480. static void brcms_c_radio_timer(void *arg)
  3481. {
  3482. struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
  3483. if (brcms_deviceremoved(wlc)) {
  3484. brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
  3485. wlc->pub->unit, __func__);
  3486. brcms_down(wlc->wl);
  3487. return;
  3488. }
  3489. brcms_c_radio_hwdisable_upd(wlc);
  3490. }
  3491. /* common low-level watchdog code */
  3492. static void brcms_b_watchdog(struct brcms_c_info *wlc)
  3493. {
  3494. struct brcms_hardware *wlc_hw = wlc->hw;
  3495. if (!wlc_hw->up)
  3496. return;
  3497. /* increment second count */
  3498. wlc_hw->now++;
  3499. /* Check for FIFO error interrupts */
  3500. brcms_b_fifoerrors(wlc_hw);
  3501. /* make sure RX dma has buffers */
  3502. dma_rxfill(wlc->hw->di[RX_FIFO]);
  3503. wlc_phy_watchdog(wlc_hw->band->pi);
  3504. }
  3505. /* common watchdog code */
  3506. static void brcms_c_watchdog(struct brcms_c_info *wlc)
  3507. {
  3508. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  3509. if (!wlc->pub->up)
  3510. return;
  3511. if (brcms_deviceremoved(wlc)) {
  3512. brcms_err(wlc->hw->d11core, "wl%d: %s: dead chip\n",
  3513. wlc->pub->unit, __func__);
  3514. brcms_down(wlc->wl);
  3515. return;
  3516. }
  3517. /* increment second count */
  3518. wlc->pub->now++;
  3519. brcms_c_radio_hwdisable_upd(wlc);
  3520. /* if radio is disable, driver may be down, quit here */
  3521. if (wlc->pub->radio_disabled)
  3522. return;
  3523. brcms_b_watchdog(wlc);
  3524. /*
  3525. * occasionally sample mac stat counters to
  3526. * detect 16-bit counter wrap
  3527. */
  3528. if ((wlc->pub->now % SW_TIMER_MAC_STAT_UPD) == 0)
  3529. brcms_c_statsupd(wlc);
  3530. if (BRCMS_ISNPHY(wlc->band) &&
  3531. ((wlc->pub->now - wlc->tempsense_lasttime) >=
  3532. BRCMS_TEMPSENSE_PERIOD)) {
  3533. wlc->tempsense_lasttime = wlc->pub->now;
  3534. brcms_c_tempsense_upd(wlc);
  3535. }
  3536. }
  3537. static void brcms_c_watchdog_by_timer(void *arg)
  3538. {
  3539. struct brcms_c_info *wlc = (struct brcms_c_info *) arg;
  3540. brcms_c_watchdog(wlc);
  3541. }
  3542. static bool brcms_c_timers_init(struct brcms_c_info *wlc, int unit)
  3543. {
  3544. wlc->wdtimer = brcms_init_timer(wlc->wl, brcms_c_watchdog_by_timer,
  3545. wlc, "watchdog");
  3546. if (!wlc->wdtimer) {
  3547. wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for wdtimer "
  3548. "failed\n", unit);
  3549. goto fail;
  3550. }
  3551. wlc->radio_timer = brcms_init_timer(wlc->wl, brcms_c_radio_timer,
  3552. wlc, "radio");
  3553. if (!wlc->radio_timer) {
  3554. wiphy_err(wlc->wiphy, "wl%d: wl_init_timer for radio_timer "
  3555. "failed\n", unit);
  3556. goto fail;
  3557. }
  3558. return true;
  3559. fail:
  3560. return false;
  3561. }
  3562. /*
  3563. * Initialize brcms_c_info default values ...
  3564. * may get overrides later in this function
  3565. */
  3566. static void brcms_c_info_init(struct brcms_c_info *wlc, int unit)
  3567. {
  3568. int i;
  3569. /* Save our copy of the chanspec */
  3570. wlc->chanspec = ch20mhz_chspec(1);
  3571. /* various 802.11g modes */
  3572. wlc->shortslot = false;
  3573. wlc->shortslot_override = BRCMS_SHORTSLOT_AUTO;
  3574. brcms_c_protection_upd(wlc, BRCMS_PROT_G_OVR, BRCMS_PROTECTION_AUTO);
  3575. brcms_c_protection_upd(wlc, BRCMS_PROT_G_SPEC, false);
  3576. brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG_OVR,
  3577. BRCMS_PROTECTION_AUTO);
  3578. brcms_c_protection_upd(wlc, BRCMS_PROT_N_CFG, BRCMS_N_PROTECTION_OFF);
  3579. brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF_OVR,
  3580. BRCMS_PROTECTION_AUTO);
  3581. brcms_c_protection_upd(wlc, BRCMS_PROT_N_NONGF, false);
  3582. brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, AUTO);
  3583. brcms_c_protection_upd(wlc, BRCMS_PROT_OVERLAP,
  3584. BRCMS_PROTECTION_CTL_OVERLAP);
  3585. /* 802.11g draft 4.0 NonERP elt advertisement */
  3586. wlc->include_legacy_erp = true;
  3587. wlc->stf->ant_rx_ovr = ANT_RX_DIV_DEF;
  3588. wlc->stf->txant = ANT_TX_DEF;
  3589. wlc->prb_resp_timeout = BRCMS_PRB_RESP_TIMEOUT;
  3590. wlc->usr_fragthresh = DOT11_DEFAULT_FRAG_LEN;
  3591. for (i = 0; i < NFIFO; i++)
  3592. wlc->fragthresh[i] = DOT11_DEFAULT_FRAG_LEN;
  3593. wlc->RTSThresh = DOT11_DEFAULT_RTS_LEN;
  3594. /* default rate fallback retry limits */
  3595. wlc->SFBL = RETRY_SHORT_FB;
  3596. wlc->LFBL = RETRY_LONG_FB;
  3597. /* default mac retry limits */
  3598. wlc->SRL = RETRY_SHORT_DEF;
  3599. wlc->LRL = RETRY_LONG_DEF;
  3600. /* WME QoS mode is Auto by default */
  3601. wlc->pub->_ampdu = AMPDU_AGG_HOST;
  3602. wlc->pub->bcmerror = 0;
  3603. }
  3604. static uint brcms_c_attach_module(struct brcms_c_info *wlc)
  3605. {
  3606. uint err = 0;
  3607. uint unit;
  3608. unit = wlc->pub->unit;
  3609. wlc->asi = brcms_c_antsel_attach(wlc);
  3610. if (wlc->asi == NULL) {
  3611. wiphy_err(wlc->wiphy, "wl%d: attach: antsel_attach "
  3612. "failed\n", unit);
  3613. err = 44;
  3614. goto fail;
  3615. }
  3616. wlc->ampdu = brcms_c_ampdu_attach(wlc);
  3617. if (wlc->ampdu == NULL) {
  3618. wiphy_err(wlc->wiphy, "wl%d: attach: ampdu_attach "
  3619. "failed\n", unit);
  3620. err = 50;
  3621. goto fail;
  3622. }
  3623. if ((brcms_c_stf_attach(wlc) != 0)) {
  3624. wiphy_err(wlc->wiphy, "wl%d: attach: stf_attach "
  3625. "failed\n", unit);
  3626. err = 68;
  3627. goto fail;
  3628. }
  3629. fail:
  3630. return err;
  3631. }
  3632. struct brcms_pub *brcms_c_pub(struct brcms_c_info *wlc)
  3633. {
  3634. return wlc->pub;
  3635. }
  3636. /* low level attach
  3637. * run backplane attach, init nvram
  3638. * run phy attach
  3639. * initialize software state for each core and band
  3640. * put the whole chip in reset(driver down state), no clock
  3641. */
  3642. static int brcms_b_attach(struct brcms_c_info *wlc, struct bcma_device *core,
  3643. uint unit, bool piomode)
  3644. {
  3645. struct brcms_hardware *wlc_hw;
  3646. uint err = 0;
  3647. uint j;
  3648. bool wme = false;
  3649. struct shared_phy_params sha_params;
  3650. struct wiphy *wiphy = wlc->wiphy;
  3651. struct pci_dev *pcidev = core->bus->host_pci;
  3652. struct ssb_sprom *sprom = &core->bus->sprom;
  3653. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI)
  3654. brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
  3655. pcidev->vendor,
  3656. pcidev->device);
  3657. else
  3658. brcms_dbg_info(core, "wl%d: vendor 0x%x device 0x%x\n", unit,
  3659. core->bus->boardinfo.vendor,
  3660. core->bus->boardinfo.type);
  3661. wme = true;
  3662. wlc_hw = wlc->hw;
  3663. wlc_hw->wlc = wlc;
  3664. wlc_hw->unit = unit;
  3665. wlc_hw->band = wlc_hw->bandstate[0];
  3666. wlc_hw->_piomode = piomode;
  3667. /* populate struct brcms_hardware with default values */
  3668. brcms_b_info_init(wlc_hw);
  3669. /*
  3670. * Do the hardware portion of the attach. Also initialize software
  3671. * state that depends on the particular hardware we are running.
  3672. */
  3673. wlc_hw->sih = ai_attach(core->bus);
  3674. if (wlc_hw->sih == NULL) {
  3675. wiphy_err(wiphy, "wl%d: brcms_b_attach: si_attach failed\n",
  3676. unit);
  3677. err = 11;
  3678. goto fail;
  3679. }
  3680. /* verify again the device is supported */
  3681. if (!brcms_c_chipmatch(core)) {
  3682. wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported device\n",
  3683. unit);
  3684. err = 12;
  3685. goto fail;
  3686. }
  3687. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
  3688. wlc_hw->vendorid = pcidev->vendor;
  3689. wlc_hw->deviceid = pcidev->device;
  3690. } else {
  3691. wlc_hw->vendorid = core->bus->boardinfo.vendor;
  3692. wlc_hw->deviceid = core->bus->boardinfo.type;
  3693. }
  3694. wlc_hw->d11core = core;
  3695. wlc_hw->corerev = core->id.rev;
  3696. /* validate chip, chiprev and corerev */
  3697. if (!brcms_c_isgoodchip(wlc_hw)) {
  3698. err = 13;
  3699. goto fail;
  3700. }
  3701. /* initialize power control registers */
  3702. ai_clkctl_init(wlc_hw->sih);
  3703. /* request fastclock and force fastclock for the rest of attach
  3704. * bring the d11 core out of reset.
  3705. * For PMU chips, the first wlc_clkctl_clk is no-op since core-clk
  3706. * is still false; But it will be called again inside wlc_corereset,
  3707. * after d11 is out of reset.
  3708. */
  3709. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  3710. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  3711. if (!brcms_b_validate_chip_access(wlc_hw)) {
  3712. wiphy_err(wiphy, "wl%d: brcms_b_attach: validate_chip_access "
  3713. "failed\n", unit);
  3714. err = 14;
  3715. goto fail;
  3716. }
  3717. /* get the board rev, used just below */
  3718. j = sprom->board_rev;
  3719. /* promote srom boardrev of 0xFF to 1 */
  3720. if (j == BOARDREV_PROMOTABLE)
  3721. j = BOARDREV_PROMOTED;
  3722. wlc_hw->boardrev = (u16) j;
  3723. if (!brcms_c_validboardtype(wlc_hw)) {
  3724. wiphy_err(wiphy, "wl%d: brcms_b_attach: Unsupported Broadcom "
  3725. "board type (0x%x)" " or revision level (0x%x)\n",
  3726. unit, ai_get_boardtype(wlc_hw->sih),
  3727. wlc_hw->boardrev);
  3728. err = 15;
  3729. goto fail;
  3730. }
  3731. wlc_hw->sromrev = sprom->revision;
  3732. wlc_hw->boardflags = sprom->boardflags_lo + (sprom->boardflags_hi << 16);
  3733. wlc_hw->boardflags2 = sprom->boardflags2_lo + (sprom->boardflags2_hi << 16);
  3734. if (wlc_hw->boardflags & BFL_NOPLLDOWN)
  3735. brcms_b_pllreq(wlc_hw, true, BRCMS_PLLREQ_SHARED);
  3736. /* check device id(srom, nvram etc.) to set bands */
  3737. if (wlc_hw->deviceid == BCM43224_D11N_ID ||
  3738. wlc_hw->deviceid == BCM43224_D11N_ID_VEN1 ||
  3739. wlc_hw->deviceid == BCM43224_CHIP_ID)
  3740. /* Dualband boards */
  3741. wlc_hw->_nbands = 2;
  3742. else
  3743. wlc_hw->_nbands = 1;
  3744. if ((ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM43225))
  3745. wlc_hw->_nbands = 1;
  3746. /* BMAC_NOTE: remove init of pub values when brcms_c_attach()
  3747. * unconditionally does the init of these values
  3748. */
  3749. wlc->vendorid = wlc_hw->vendorid;
  3750. wlc->deviceid = wlc_hw->deviceid;
  3751. wlc->pub->sih = wlc_hw->sih;
  3752. wlc->pub->corerev = wlc_hw->corerev;
  3753. wlc->pub->sromrev = wlc_hw->sromrev;
  3754. wlc->pub->boardrev = wlc_hw->boardrev;
  3755. wlc->pub->boardflags = wlc_hw->boardflags;
  3756. wlc->pub->boardflags2 = wlc_hw->boardflags2;
  3757. wlc->pub->_nbands = wlc_hw->_nbands;
  3758. wlc_hw->physhim = wlc_phy_shim_attach(wlc_hw, wlc->wl, wlc);
  3759. if (wlc_hw->physhim == NULL) {
  3760. wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_shim_attach "
  3761. "failed\n", unit);
  3762. err = 25;
  3763. goto fail;
  3764. }
  3765. /* pass all the parameters to wlc_phy_shared_attach in one struct */
  3766. sha_params.sih = wlc_hw->sih;
  3767. sha_params.physhim = wlc_hw->physhim;
  3768. sha_params.unit = unit;
  3769. sha_params.corerev = wlc_hw->corerev;
  3770. sha_params.vid = wlc_hw->vendorid;
  3771. sha_params.did = wlc_hw->deviceid;
  3772. sha_params.chip = ai_get_chip_id(wlc_hw->sih);
  3773. sha_params.chiprev = ai_get_chiprev(wlc_hw->sih);
  3774. sha_params.chippkg = ai_get_chippkg(wlc_hw->sih);
  3775. sha_params.sromrev = wlc_hw->sromrev;
  3776. sha_params.boardtype = ai_get_boardtype(wlc_hw->sih);
  3777. sha_params.boardrev = wlc_hw->boardrev;
  3778. sha_params.boardflags = wlc_hw->boardflags;
  3779. sha_params.boardflags2 = wlc_hw->boardflags2;
  3780. /* alloc and save pointer to shared phy state area */
  3781. wlc_hw->phy_sh = wlc_phy_shared_attach(&sha_params);
  3782. if (!wlc_hw->phy_sh) {
  3783. err = 16;
  3784. goto fail;
  3785. }
  3786. /* initialize software state for each core and band */
  3787. for (j = 0; j < wlc_hw->_nbands; j++) {
  3788. /*
  3789. * band0 is always 2.4Ghz
  3790. * band1, if present, is 5Ghz
  3791. */
  3792. brcms_c_setxband(wlc_hw, j);
  3793. wlc_hw->band->bandunit = j;
  3794. wlc_hw->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
  3795. wlc->band->bandunit = j;
  3796. wlc->band->bandtype = j ? BRCM_BAND_5G : BRCM_BAND_2G;
  3797. wlc->core->coreidx = core->core_index;
  3798. wlc_hw->machwcap = bcma_read32(core, D11REGOFFS(machwcap));
  3799. wlc_hw->machwcap_backup = wlc_hw->machwcap;
  3800. /* init tx fifo size */
  3801. WARN_ON((wlc_hw->corerev - XMTFIFOTBL_STARTREV) < 0 ||
  3802. (wlc_hw->corerev - XMTFIFOTBL_STARTREV) >
  3803. ARRAY_SIZE(xmtfifo_sz));
  3804. wlc_hw->xmtfifo_sz =
  3805. xmtfifo_sz[(wlc_hw->corerev - XMTFIFOTBL_STARTREV)];
  3806. WARN_ON(!wlc_hw->xmtfifo_sz[0]);
  3807. /* Get a phy for this band */
  3808. wlc_hw->band->pi =
  3809. wlc_phy_attach(wlc_hw->phy_sh, core,
  3810. wlc_hw->band->bandtype,
  3811. wlc->wiphy);
  3812. if (wlc_hw->band->pi == NULL) {
  3813. wiphy_err(wiphy, "wl%d: brcms_b_attach: wlc_phy_"
  3814. "attach failed\n", unit);
  3815. err = 17;
  3816. goto fail;
  3817. }
  3818. wlc_phy_machwcap_set(wlc_hw->band->pi, wlc_hw->machwcap);
  3819. wlc_phy_get_phyversion(wlc_hw->band->pi, &wlc_hw->band->phytype,
  3820. &wlc_hw->band->phyrev,
  3821. &wlc_hw->band->radioid,
  3822. &wlc_hw->band->radiorev);
  3823. wlc_hw->band->abgphy_encore =
  3824. wlc_phy_get_encore(wlc_hw->band->pi);
  3825. wlc->band->abgphy_encore = wlc_phy_get_encore(wlc_hw->band->pi);
  3826. wlc_hw->band->core_flags =
  3827. wlc_phy_get_coreflags(wlc_hw->band->pi);
  3828. /* verify good phy_type & supported phy revision */
  3829. if (BRCMS_ISNPHY(wlc_hw->band)) {
  3830. if (NCONF_HAS(wlc_hw->band->phyrev))
  3831. goto good_phy;
  3832. else
  3833. goto bad_phy;
  3834. } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  3835. if (LCNCONF_HAS(wlc_hw->band->phyrev))
  3836. goto good_phy;
  3837. else
  3838. goto bad_phy;
  3839. } else {
  3840. bad_phy:
  3841. wiphy_err(wiphy, "wl%d: brcms_b_attach: unsupported "
  3842. "phy type/rev (%d/%d)\n", unit,
  3843. wlc_hw->band->phytype, wlc_hw->band->phyrev);
  3844. err = 18;
  3845. goto fail;
  3846. }
  3847. good_phy:
  3848. /*
  3849. * BMAC_NOTE: wlc->band->pi should not be set below and should
  3850. * be done in the high level attach. However we can not make
  3851. * that change until all low level access is changed to
  3852. * wlc_hw->band->pi. Instead do the wlc->band->pi init below,
  3853. * keeping wlc_hw->band->pi as well for incremental update of
  3854. * low level fns, and cut over low only init when all fns
  3855. * updated.
  3856. */
  3857. wlc->band->pi = wlc_hw->band->pi;
  3858. wlc->band->phytype = wlc_hw->band->phytype;
  3859. wlc->band->phyrev = wlc_hw->band->phyrev;
  3860. wlc->band->radioid = wlc_hw->band->radioid;
  3861. wlc->band->radiorev = wlc_hw->band->radiorev;
  3862. /* default contention windows size limits */
  3863. wlc_hw->band->CWmin = APHY_CWMIN;
  3864. wlc_hw->band->CWmax = PHY_CWMAX;
  3865. if (!brcms_b_attach_dmapio(wlc, j, wme)) {
  3866. err = 19;
  3867. goto fail;
  3868. }
  3869. }
  3870. /* disable core to match driver "down" state */
  3871. brcms_c_coredisable(wlc_hw);
  3872. /* Match driver "down" state */
  3873. ai_pci_down(wlc_hw->sih);
  3874. /* turn off pll and xtal to match driver "down" state */
  3875. brcms_b_xtal(wlc_hw, OFF);
  3876. /* *******************************************************************
  3877. * The hardware is in the DOWN state at this point. D11 core
  3878. * or cores are in reset with clocks off, and the board PLLs
  3879. * are off if possible.
  3880. *
  3881. * Beyond this point, wlc->sbclk == false and chip registers
  3882. * should not be touched.
  3883. *********************************************************************
  3884. */
  3885. /* init etheraddr state variables */
  3886. brcms_c_get_macaddr(wlc_hw, wlc_hw->etheraddr);
  3887. if (is_broadcast_ether_addr(wlc_hw->etheraddr) ||
  3888. is_zero_ether_addr(wlc_hw->etheraddr)) {
  3889. wiphy_err(wiphy, "wl%d: brcms_b_attach: bad macaddr\n",
  3890. unit);
  3891. err = 22;
  3892. goto fail;
  3893. }
  3894. brcms_dbg_info(wlc_hw->d11core, "deviceid 0x%x nbands %d board 0x%x\n",
  3895. wlc_hw->deviceid, wlc_hw->_nbands,
  3896. ai_get_boardtype(wlc_hw->sih));
  3897. return err;
  3898. fail:
  3899. wiphy_err(wiphy, "wl%d: brcms_b_attach: failed with err %d\n", unit,
  3900. err);
  3901. return err;
  3902. }
  3903. static void brcms_c_attach_antgain_init(struct brcms_c_info *wlc)
  3904. {
  3905. uint unit;
  3906. unit = wlc->pub->unit;
  3907. if ((wlc->band->antgain == -1) && (wlc->pub->sromrev == 1)) {
  3908. /* default antenna gain for srom rev 1 is 2 dBm (8 qdbm) */
  3909. wlc->band->antgain = 8;
  3910. } else if (wlc->band->antgain == -1) {
  3911. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
  3912. " srom, using 2dB\n", unit, __func__);
  3913. wlc->band->antgain = 8;
  3914. } else {
  3915. s8 gain, fract;
  3916. /* Older sroms specified gain in whole dbm only. In order
  3917. * be able to specify qdbm granularity and remain backward
  3918. * compatible the whole dbms are now encoded in only
  3919. * low 6 bits and remaining qdbms are encoded in the hi 2 bits.
  3920. * 6 bit signed number ranges from -32 - 31.
  3921. *
  3922. * Examples:
  3923. * 0x1 = 1 db,
  3924. * 0xc1 = 1.75 db (1 + 3 quarters),
  3925. * 0x3f = -1 (-1 + 0 quarters),
  3926. * 0x7f = -.75 (-1 + 1 quarters) = -3 qdbm.
  3927. * 0xbf = -.50 (-1 + 2 quarters) = -2 qdbm.
  3928. */
  3929. gain = wlc->band->antgain & 0x3f;
  3930. gain <<= 2; /* Sign extend */
  3931. gain >>= 2;
  3932. fract = (wlc->band->antgain & 0xc0) >> 6;
  3933. wlc->band->antgain = 4 * gain + fract;
  3934. }
  3935. }
  3936. static bool brcms_c_attach_stf_ant_init(struct brcms_c_info *wlc)
  3937. {
  3938. int aa;
  3939. uint unit;
  3940. int bandtype;
  3941. struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
  3942. unit = wlc->pub->unit;
  3943. bandtype = wlc->band->bandtype;
  3944. /* get antennas available */
  3945. if (bandtype == BRCM_BAND_5G)
  3946. aa = sprom->ant_available_a;
  3947. else
  3948. aa = sprom->ant_available_bg;
  3949. if ((aa < 1) || (aa > 15)) {
  3950. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid antennas available in"
  3951. " srom (0x%x), using 3\n", unit, __func__, aa);
  3952. aa = 3;
  3953. }
  3954. /* reset the defaults if we have a single antenna */
  3955. if (aa == 1) {
  3956. wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_0;
  3957. wlc->stf->txant = ANT_TX_FORCE_0;
  3958. } else if (aa == 2) {
  3959. wlc->stf->ant_rx_ovr = ANT_RX_DIV_FORCE_1;
  3960. wlc->stf->txant = ANT_TX_FORCE_1;
  3961. } else {
  3962. }
  3963. /* Compute Antenna Gain */
  3964. if (bandtype == BRCM_BAND_5G)
  3965. wlc->band->antgain = sprom->antenna_gain.a1;
  3966. else
  3967. wlc->band->antgain = sprom->antenna_gain.a0;
  3968. brcms_c_attach_antgain_init(wlc);
  3969. return true;
  3970. }
  3971. static void brcms_c_bss_default_init(struct brcms_c_info *wlc)
  3972. {
  3973. u16 chanspec;
  3974. struct brcms_band *band;
  3975. struct brcms_bss_info *bi = wlc->default_bss;
  3976. /* init default and target BSS with some sane initial values */
  3977. memset(bi, 0, sizeof(*bi));
  3978. bi->beacon_period = BEACON_INTERVAL_DEFAULT;
  3979. /* fill the default channel as the first valid channel
  3980. * starting from the 2G channels
  3981. */
  3982. chanspec = ch20mhz_chspec(1);
  3983. wlc->home_chanspec = bi->chanspec = chanspec;
  3984. /* find the band of our default channel */
  3985. band = wlc->band;
  3986. if (wlc->pub->_nbands > 1 &&
  3987. band->bandunit != chspec_bandunit(chanspec))
  3988. band = wlc->bandstate[OTHERBANDUNIT(wlc)];
  3989. /* init bss rates to the band specific default rate set */
  3990. brcms_c_rateset_default(&bi->rateset, NULL, band->phytype,
  3991. band->bandtype, false, BRCMS_RATE_MASK_FULL,
  3992. (bool) (wlc->pub->_n_enab & SUPPORT_11N),
  3993. brcms_chspec_bw(chanspec), wlc->stf->txstreams);
  3994. if (wlc->pub->_n_enab & SUPPORT_11N)
  3995. bi->flags |= BRCMS_BSS_HT;
  3996. }
  3997. static void brcms_c_update_mimo_band_bwcap(struct brcms_c_info *wlc, u8 bwcap)
  3998. {
  3999. uint i;
  4000. struct brcms_band *band;
  4001. for (i = 0; i < wlc->pub->_nbands; i++) {
  4002. band = wlc->bandstate[i];
  4003. if (band->bandtype == BRCM_BAND_5G) {
  4004. if ((bwcap == BRCMS_N_BW_40ALL)
  4005. || (bwcap == BRCMS_N_BW_20IN2G_40IN5G))
  4006. band->mimo_cap_40 = true;
  4007. else
  4008. band->mimo_cap_40 = false;
  4009. } else {
  4010. if (bwcap == BRCMS_N_BW_40ALL)
  4011. band->mimo_cap_40 = true;
  4012. else
  4013. band->mimo_cap_40 = false;
  4014. }
  4015. }
  4016. }
  4017. static void brcms_c_timers_deinit(struct brcms_c_info *wlc)
  4018. {
  4019. /* free timer state */
  4020. if (wlc->wdtimer) {
  4021. brcms_free_timer(wlc->wdtimer);
  4022. wlc->wdtimer = NULL;
  4023. }
  4024. if (wlc->radio_timer) {
  4025. brcms_free_timer(wlc->radio_timer);
  4026. wlc->radio_timer = NULL;
  4027. }
  4028. }
  4029. static void brcms_c_detach_module(struct brcms_c_info *wlc)
  4030. {
  4031. if (wlc->asi) {
  4032. brcms_c_antsel_detach(wlc->asi);
  4033. wlc->asi = NULL;
  4034. }
  4035. if (wlc->ampdu) {
  4036. brcms_c_ampdu_detach(wlc->ampdu);
  4037. wlc->ampdu = NULL;
  4038. }
  4039. brcms_c_stf_detach(wlc);
  4040. }
  4041. /*
  4042. * low level detach
  4043. */
  4044. static int brcms_b_detach(struct brcms_c_info *wlc)
  4045. {
  4046. uint i;
  4047. struct brcms_hw_band *band;
  4048. struct brcms_hardware *wlc_hw = wlc->hw;
  4049. int callbacks;
  4050. callbacks = 0;
  4051. brcms_b_detach_dmapio(wlc_hw);
  4052. band = wlc_hw->band;
  4053. for (i = 0; i < wlc_hw->_nbands; i++) {
  4054. if (band->pi) {
  4055. /* Detach this band's phy */
  4056. wlc_phy_detach(band->pi);
  4057. band->pi = NULL;
  4058. }
  4059. band = wlc_hw->bandstate[OTHERBANDUNIT(wlc)];
  4060. }
  4061. /* Free shared phy state */
  4062. kfree(wlc_hw->phy_sh);
  4063. wlc_phy_shim_detach(wlc_hw->physhim);
  4064. if (wlc_hw->sih) {
  4065. ai_detach(wlc_hw->sih);
  4066. wlc_hw->sih = NULL;
  4067. }
  4068. return callbacks;
  4069. }
  4070. /*
  4071. * Return a count of the number of driver callbacks still pending.
  4072. *
  4073. * General policy is that brcms_c_detach can only dealloc/free software states.
  4074. * It can NOT touch hardware registers since the d11core may be in reset and
  4075. * clock may not be available.
  4076. * One exception is sb register access, which is possible if crystal is turned
  4077. * on after "down" state, driver should avoid software timer with the exception
  4078. * of radio_monitor.
  4079. */
  4080. uint brcms_c_detach(struct brcms_c_info *wlc)
  4081. {
  4082. uint callbacks = 0;
  4083. if (wlc == NULL)
  4084. return 0;
  4085. callbacks += brcms_b_detach(wlc);
  4086. /* delete software timers */
  4087. if (!brcms_c_radio_monitor_stop(wlc))
  4088. callbacks++;
  4089. brcms_c_channel_mgr_detach(wlc->cmi);
  4090. brcms_c_timers_deinit(wlc);
  4091. brcms_c_detach_module(wlc);
  4092. brcms_c_detach_mfree(wlc);
  4093. return callbacks;
  4094. }
  4095. /* update state that depends on the current value of "ap" */
  4096. static void brcms_c_ap_upd(struct brcms_c_info *wlc)
  4097. {
  4098. /* STA-BSS; short capable */
  4099. wlc->PLCPHdr_override = BRCMS_PLCP_SHORT;
  4100. }
  4101. /* Initialize just the hardware when coming out of POR or S3/S5 system states */
  4102. static void brcms_b_hw_up(struct brcms_hardware *wlc_hw)
  4103. {
  4104. if (wlc_hw->wlc->pub->hw_up)
  4105. return;
  4106. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  4107. /*
  4108. * Enable pll and xtal, initialize the power control registers,
  4109. * and force fastclock for the remainder of brcms_c_up().
  4110. */
  4111. brcms_b_xtal(wlc_hw, ON);
  4112. ai_clkctl_init(wlc_hw->sih);
  4113. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4114. /*
  4115. * TODO: test suspend/resume
  4116. *
  4117. * AI chip doesn't restore bar0win2 on
  4118. * hibernation/resume, need sw fixup
  4119. */
  4120. /*
  4121. * Inform phy that a POR reset has occurred so
  4122. * it does a complete phy init
  4123. */
  4124. wlc_phy_por_inform(wlc_hw->band->pi);
  4125. wlc_hw->ucode_loaded = false;
  4126. wlc_hw->wlc->pub->hw_up = true;
  4127. if ((wlc_hw->boardflags & BFL_FEM)
  4128. && (ai_get_chip_id(wlc_hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  4129. if (!
  4130. (wlc_hw->boardrev >= 0x1250
  4131. && (wlc_hw->boardflags & BFL_FEM_BT)))
  4132. ai_epa_4313war(wlc_hw->sih);
  4133. }
  4134. }
  4135. static int brcms_b_up_prep(struct brcms_hardware *wlc_hw)
  4136. {
  4137. brcms_dbg_info(wlc_hw->d11core, "wl%d\n", wlc_hw->unit);
  4138. /*
  4139. * Enable pll and xtal, initialize the power control registers,
  4140. * and force fastclock for the remainder of brcms_c_up().
  4141. */
  4142. brcms_b_xtal(wlc_hw, ON);
  4143. ai_clkctl_init(wlc_hw->sih);
  4144. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4145. /*
  4146. * Configure pci/pcmcia here instead of in brcms_c_attach()
  4147. * to allow mfg hotswap: down, hotswap (chip power cycle), up.
  4148. */
  4149. bcma_core_pci_irq_ctl(&wlc_hw->d11core->bus->drv_pci[0], wlc_hw->d11core,
  4150. true);
  4151. /*
  4152. * Need to read the hwradio status here to cover the case where the
  4153. * system is loaded with the hw radio disabled. We do not want to
  4154. * bring the driver up in this case.
  4155. */
  4156. if (brcms_b_radio_read_hwdisabled(wlc_hw)) {
  4157. /* put SB PCI in down state again */
  4158. ai_pci_down(wlc_hw->sih);
  4159. brcms_b_xtal(wlc_hw, OFF);
  4160. return -ENOMEDIUM;
  4161. }
  4162. ai_pci_up(wlc_hw->sih);
  4163. /* reset the d11 core */
  4164. brcms_b_corereset(wlc_hw, BRCMS_USE_COREFLAGS);
  4165. return 0;
  4166. }
  4167. static int brcms_b_up_finish(struct brcms_hardware *wlc_hw)
  4168. {
  4169. wlc_hw->up = true;
  4170. wlc_phy_hw_state_upd(wlc_hw->band->pi, true);
  4171. /* FULLY enable dynamic power control and d11 core interrupt */
  4172. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_DYNAMIC);
  4173. brcms_intrson(wlc_hw->wlc->wl);
  4174. return 0;
  4175. }
  4176. /*
  4177. * Write WME tunable parameters for retransmit/max rate
  4178. * from wlc struct to ucode
  4179. */
  4180. static void brcms_c_wme_retries_write(struct brcms_c_info *wlc)
  4181. {
  4182. int ac;
  4183. /* Need clock to do this */
  4184. if (!wlc->clk)
  4185. return;
  4186. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  4187. brcms_b_write_shm(wlc->hw, M_AC_TXLMT_ADDR(ac),
  4188. wlc->wme_retries[ac]);
  4189. }
  4190. /* make interface operational */
  4191. int brcms_c_up(struct brcms_c_info *wlc)
  4192. {
  4193. struct ieee80211_channel *ch;
  4194. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  4195. /* HW is turned off so don't try to access it */
  4196. if (wlc->pub->hw_off || brcms_deviceremoved(wlc))
  4197. return -ENOMEDIUM;
  4198. if (!wlc->pub->hw_up) {
  4199. brcms_b_hw_up(wlc->hw);
  4200. wlc->pub->hw_up = true;
  4201. }
  4202. if ((wlc->pub->boardflags & BFL_FEM)
  4203. && (ai_get_chip_id(wlc->hw->sih) == BCMA_CHIP_ID_BCM4313)) {
  4204. if (wlc->pub->boardrev >= 0x1250
  4205. && (wlc->pub->boardflags & BFL_FEM_BT))
  4206. brcms_b_mhf(wlc->hw, MHF5, MHF5_4313_GPIOCTRL,
  4207. MHF5_4313_GPIOCTRL, BRCM_BAND_ALL);
  4208. else
  4209. brcms_b_mhf(wlc->hw, MHF4, MHF4_EXTPA_ENABLE,
  4210. MHF4_EXTPA_ENABLE, BRCM_BAND_ALL);
  4211. }
  4212. /*
  4213. * Need to read the hwradio status here to cover the case where the
  4214. * system is loaded with the hw radio disabled. We do not want to bring
  4215. * the driver up in this case. If radio is disabled, abort up, lower
  4216. * power, start radio timer and return 0(for NDIS) don't call
  4217. * radio_update to avoid looping brcms_c_up.
  4218. *
  4219. * brcms_b_up_prep() returns either 0 or -BCME_RADIOOFF only
  4220. */
  4221. if (!wlc->pub->radio_disabled) {
  4222. int status = brcms_b_up_prep(wlc->hw);
  4223. if (status == -ENOMEDIUM) {
  4224. if (!mboolisset
  4225. (wlc->pub->radio_disabled, WL_RADIO_HW_DISABLE)) {
  4226. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  4227. mboolset(wlc->pub->radio_disabled,
  4228. WL_RADIO_HW_DISABLE);
  4229. if (bsscfg->enable && bsscfg->BSS)
  4230. brcms_err(wlc->hw->d11core,
  4231. "wl%d: up: rfdisable -> "
  4232. "bsscfg_disable()\n",
  4233. wlc->pub->unit);
  4234. }
  4235. }
  4236. }
  4237. if (wlc->pub->radio_disabled) {
  4238. brcms_c_radio_monitor_start(wlc);
  4239. return 0;
  4240. }
  4241. /* brcms_b_up_prep has done brcms_c_corereset(). so clk is on, set it */
  4242. wlc->clk = true;
  4243. brcms_c_radio_monitor_stop(wlc);
  4244. /* Set EDCF hostflags */
  4245. brcms_b_mhf(wlc->hw, MHF1, MHF1_EDCF, MHF1_EDCF, BRCM_BAND_ALL);
  4246. brcms_init(wlc->wl);
  4247. wlc->pub->up = true;
  4248. if (wlc->bandinit_pending) {
  4249. ch = wlc->pub->ieee_hw->conf.channel;
  4250. brcms_c_suspend_mac_and_wait(wlc);
  4251. brcms_c_set_chanspec(wlc, ch20mhz_chspec(ch->hw_value));
  4252. wlc->bandinit_pending = false;
  4253. brcms_c_enable_mac(wlc);
  4254. }
  4255. brcms_b_up_finish(wlc->hw);
  4256. /* Program the TX wme params with the current settings */
  4257. brcms_c_wme_retries_write(wlc);
  4258. /* start one second watchdog timer */
  4259. brcms_add_timer(wlc->wdtimer, TIMER_INTERVAL_WATCHDOG, true);
  4260. wlc->WDarmed = true;
  4261. /* ensure antenna config is up to date */
  4262. brcms_c_stf_phy_txant_upd(wlc);
  4263. /* ensure LDPC config is in sync */
  4264. brcms_c_ht_update_ldpc(wlc, wlc->stf->ldpc);
  4265. return 0;
  4266. }
  4267. static uint brcms_c_down_del_timer(struct brcms_c_info *wlc)
  4268. {
  4269. uint callbacks = 0;
  4270. return callbacks;
  4271. }
  4272. static int brcms_b_bmac_down_prep(struct brcms_hardware *wlc_hw)
  4273. {
  4274. bool dev_gone;
  4275. uint callbacks = 0;
  4276. if (!wlc_hw->up)
  4277. return callbacks;
  4278. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  4279. /* disable interrupts */
  4280. if (dev_gone)
  4281. wlc_hw->wlc->macintmask = 0;
  4282. else {
  4283. /* now disable interrupts */
  4284. brcms_intrsoff(wlc_hw->wlc->wl);
  4285. /* ensure we're running on the pll clock again */
  4286. brcms_b_clkctl_clk(wlc_hw, BCMA_CLKMODE_FAST);
  4287. }
  4288. /* down phy at the last of this stage */
  4289. callbacks += wlc_phy_down(wlc_hw->band->pi);
  4290. return callbacks;
  4291. }
  4292. static int brcms_b_down_finish(struct brcms_hardware *wlc_hw)
  4293. {
  4294. uint callbacks = 0;
  4295. bool dev_gone;
  4296. if (!wlc_hw->up)
  4297. return callbacks;
  4298. wlc_hw->up = false;
  4299. wlc_phy_hw_state_upd(wlc_hw->band->pi, false);
  4300. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  4301. if (dev_gone) {
  4302. wlc_hw->sbclk = false;
  4303. wlc_hw->clk = false;
  4304. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  4305. /* reclaim any posted packets */
  4306. brcms_c_flushqueues(wlc_hw->wlc);
  4307. } else {
  4308. /* Reset and disable the core */
  4309. if (bcma_core_is_enabled(wlc_hw->d11core)) {
  4310. if (bcma_read32(wlc_hw->d11core,
  4311. D11REGOFFS(maccontrol)) & MCTL_EN_MAC)
  4312. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  4313. callbacks += brcms_reset(wlc_hw->wlc->wl);
  4314. brcms_c_coredisable(wlc_hw);
  4315. }
  4316. /* turn off primary xtal and pll */
  4317. if (!wlc_hw->noreset) {
  4318. ai_pci_down(wlc_hw->sih);
  4319. brcms_b_xtal(wlc_hw, OFF);
  4320. }
  4321. }
  4322. return callbacks;
  4323. }
  4324. /*
  4325. * Mark the interface nonoperational, stop the software mechanisms,
  4326. * disable the hardware, free any transient buffer state.
  4327. * Return a count of the number of driver callbacks still pending.
  4328. */
  4329. uint brcms_c_down(struct brcms_c_info *wlc)
  4330. {
  4331. uint callbacks = 0;
  4332. int i;
  4333. bool dev_gone = false;
  4334. brcms_dbg_info(wlc->hw->d11core, "wl%d\n", wlc->pub->unit);
  4335. /* check if we are already in the going down path */
  4336. if (wlc->going_down) {
  4337. brcms_err(wlc->hw->d11core,
  4338. "wl%d: %s: Driver going down so return\n",
  4339. wlc->pub->unit, __func__);
  4340. return 0;
  4341. }
  4342. if (!wlc->pub->up)
  4343. return callbacks;
  4344. wlc->going_down = true;
  4345. callbacks += brcms_b_bmac_down_prep(wlc->hw);
  4346. dev_gone = brcms_deviceremoved(wlc);
  4347. /* Call any registered down handlers */
  4348. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4349. if (wlc->modulecb[i].down_fn)
  4350. callbacks +=
  4351. wlc->modulecb[i].down_fn(wlc->modulecb[i].hdl);
  4352. }
  4353. /* cancel the watchdog timer */
  4354. if (wlc->WDarmed) {
  4355. if (!brcms_del_timer(wlc->wdtimer))
  4356. callbacks++;
  4357. wlc->WDarmed = false;
  4358. }
  4359. /* cancel all other timers */
  4360. callbacks += brcms_c_down_del_timer(wlc);
  4361. wlc->pub->up = false;
  4362. wlc_phy_mute_upd(wlc->band->pi, false, PHY_MUTE_ALL);
  4363. callbacks += brcms_b_down_finish(wlc->hw);
  4364. /* brcms_b_down_finish has done brcms_c_coredisable(). so clk is off */
  4365. wlc->clk = false;
  4366. wlc->going_down = false;
  4367. return callbacks;
  4368. }
  4369. /* Set the current gmode configuration */
  4370. int brcms_c_set_gmode(struct brcms_c_info *wlc, u8 gmode, bool config)
  4371. {
  4372. int ret = 0;
  4373. uint i;
  4374. struct brcms_c_rateset rs;
  4375. /* Default to 54g Auto */
  4376. /* Advertise and use shortslot (-1/0/1 Auto/Off/On) */
  4377. s8 shortslot = BRCMS_SHORTSLOT_AUTO;
  4378. bool shortslot_restrict = false; /* Restrict association to stations
  4379. * that support shortslot
  4380. */
  4381. bool ofdm_basic = false; /* Make 6, 12, and 24 basic rates */
  4382. /* Advertise and use short preambles (-1/0/1 Auto/Off/On) */
  4383. int preamble = BRCMS_PLCP_LONG;
  4384. bool preamble_restrict = false; /* Restrict association to stations
  4385. * that support short preambles
  4386. */
  4387. struct brcms_band *band;
  4388. /* if N-support is enabled, allow Gmode set as long as requested
  4389. * Gmode is not GMODE_LEGACY_B
  4390. */
  4391. if ((wlc->pub->_n_enab & SUPPORT_11N) && gmode == GMODE_LEGACY_B)
  4392. return -ENOTSUPP;
  4393. /* verify that we are dealing with 2G band and grab the band pointer */
  4394. if (wlc->band->bandtype == BRCM_BAND_2G)
  4395. band = wlc->band;
  4396. else if ((wlc->pub->_nbands > 1) &&
  4397. (wlc->bandstate[OTHERBANDUNIT(wlc)]->bandtype == BRCM_BAND_2G))
  4398. band = wlc->bandstate[OTHERBANDUNIT(wlc)];
  4399. else
  4400. return -EINVAL;
  4401. /* update configuration value */
  4402. if (config)
  4403. brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER, gmode);
  4404. /* Clear rateset override */
  4405. memset(&rs, 0, sizeof(rs));
  4406. switch (gmode) {
  4407. case GMODE_LEGACY_B:
  4408. shortslot = BRCMS_SHORTSLOT_OFF;
  4409. brcms_c_rateset_copy(&gphy_legacy_rates, &rs);
  4410. break;
  4411. case GMODE_LRS:
  4412. break;
  4413. case GMODE_AUTO:
  4414. /* Accept defaults */
  4415. break;
  4416. case GMODE_ONLY:
  4417. ofdm_basic = true;
  4418. preamble = BRCMS_PLCP_SHORT;
  4419. preamble_restrict = true;
  4420. break;
  4421. case GMODE_PERFORMANCE:
  4422. shortslot = BRCMS_SHORTSLOT_ON;
  4423. shortslot_restrict = true;
  4424. ofdm_basic = true;
  4425. preamble = BRCMS_PLCP_SHORT;
  4426. preamble_restrict = true;
  4427. break;
  4428. default:
  4429. /* Error */
  4430. brcms_err(wlc->hw->d11core, "wl%d: %s: invalid gmode %d\n",
  4431. wlc->pub->unit, __func__, gmode);
  4432. return -ENOTSUPP;
  4433. }
  4434. band->gmode = gmode;
  4435. wlc->shortslot_override = shortslot;
  4436. /* Use the default 11g rateset */
  4437. if (!rs.count)
  4438. brcms_c_rateset_copy(&cck_ofdm_rates, &rs);
  4439. if (ofdm_basic) {
  4440. for (i = 0; i < rs.count; i++) {
  4441. if (rs.rates[i] == BRCM_RATE_6M
  4442. || rs.rates[i] == BRCM_RATE_12M
  4443. || rs.rates[i] == BRCM_RATE_24M)
  4444. rs.rates[i] |= BRCMS_RATE_FLAG;
  4445. }
  4446. }
  4447. /* Set default bss rateset */
  4448. wlc->default_bss->rateset.count = rs.count;
  4449. memcpy(wlc->default_bss->rateset.rates, rs.rates,
  4450. sizeof(wlc->default_bss->rateset.rates));
  4451. return ret;
  4452. }
  4453. int brcms_c_set_nmode(struct brcms_c_info *wlc)
  4454. {
  4455. uint i;
  4456. s32 nmode = AUTO;
  4457. if (wlc->stf->txstreams == WL_11N_3x3)
  4458. nmode = WL_11N_3x3;
  4459. else
  4460. nmode = WL_11N_2x2;
  4461. /* force GMODE_AUTO if NMODE is ON */
  4462. brcms_c_set_gmode(wlc, GMODE_AUTO, true);
  4463. if (nmode == WL_11N_3x3)
  4464. wlc->pub->_n_enab = SUPPORT_HT;
  4465. else
  4466. wlc->pub->_n_enab = SUPPORT_11N;
  4467. wlc->default_bss->flags |= BRCMS_BSS_HT;
  4468. /* add the mcs rates to the default and hw ratesets */
  4469. brcms_c_rateset_mcs_build(&wlc->default_bss->rateset,
  4470. wlc->stf->txstreams);
  4471. for (i = 0; i < wlc->pub->_nbands; i++)
  4472. memcpy(wlc->bandstate[i]->hw_rateset.mcs,
  4473. wlc->default_bss->rateset.mcs, MCSSET_LEN);
  4474. return 0;
  4475. }
  4476. static int
  4477. brcms_c_set_internal_rateset(struct brcms_c_info *wlc,
  4478. struct brcms_c_rateset *rs_arg)
  4479. {
  4480. struct brcms_c_rateset rs, new;
  4481. uint bandunit;
  4482. memcpy(&rs, rs_arg, sizeof(struct brcms_c_rateset));
  4483. /* check for bad count value */
  4484. if ((rs.count == 0) || (rs.count > BRCMS_NUMRATES))
  4485. return -EINVAL;
  4486. /* try the current band */
  4487. bandunit = wlc->band->bandunit;
  4488. memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
  4489. if (brcms_c_rate_hwrs_filter_sort_validate
  4490. (&new, &wlc->bandstate[bandunit]->hw_rateset, true,
  4491. wlc->stf->txstreams))
  4492. goto good;
  4493. /* try the other band */
  4494. if (brcms_is_mband_unlocked(wlc)) {
  4495. bandunit = OTHERBANDUNIT(wlc);
  4496. memcpy(&new, &rs, sizeof(struct brcms_c_rateset));
  4497. if (brcms_c_rate_hwrs_filter_sort_validate(&new,
  4498. &wlc->
  4499. bandstate[bandunit]->
  4500. hw_rateset, true,
  4501. wlc->stf->txstreams))
  4502. goto good;
  4503. }
  4504. return -EBADE;
  4505. good:
  4506. /* apply new rateset */
  4507. memcpy(&wlc->default_bss->rateset, &new,
  4508. sizeof(struct brcms_c_rateset));
  4509. memcpy(&wlc->bandstate[bandunit]->defrateset, &new,
  4510. sizeof(struct brcms_c_rateset));
  4511. return 0;
  4512. }
  4513. static void brcms_c_ofdm_rateset_war(struct brcms_c_info *wlc)
  4514. {
  4515. u8 r;
  4516. bool war = false;
  4517. if (wlc->bsscfg->associated)
  4518. r = wlc->bsscfg->current_bss->rateset.rates[0];
  4519. else
  4520. r = wlc->default_bss->rateset.rates[0];
  4521. wlc_phy_ofdm_rateset_war(wlc->band->pi, war);
  4522. }
  4523. int brcms_c_set_channel(struct brcms_c_info *wlc, u16 channel)
  4524. {
  4525. u16 chspec = ch20mhz_chspec(channel);
  4526. if (channel < 0 || channel > MAXCHANNEL)
  4527. return -EINVAL;
  4528. if (!brcms_c_valid_chanspec_db(wlc->cmi, chspec))
  4529. return -EINVAL;
  4530. if (!wlc->pub->up && brcms_is_mband_unlocked(wlc)) {
  4531. if (wlc->band->bandunit != chspec_bandunit(chspec))
  4532. wlc->bandinit_pending = true;
  4533. else
  4534. wlc->bandinit_pending = false;
  4535. }
  4536. wlc->default_bss->chanspec = chspec;
  4537. /* brcms_c_BSSinit() will sanitize the rateset before
  4538. * using it.. */
  4539. if (wlc->pub->up && (wlc_phy_chanspec_get(wlc->band->pi) != chspec)) {
  4540. brcms_c_set_home_chanspec(wlc, chspec);
  4541. brcms_c_suspend_mac_and_wait(wlc);
  4542. brcms_c_set_chanspec(wlc, chspec);
  4543. brcms_c_enable_mac(wlc);
  4544. }
  4545. return 0;
  4546. }
  4547. int brcms_c_set_rate_limit(struct brcms_c_info *wlc, u16 srl, u16 lrl)
  4548. {
  4549. int ac;
  4550. if (srl < 1 || srl > RETRY_SHORT_MAX ||
  4551. lrl < 1 || lrl > RETRY_SHORT_MAX)
  4552. return -EINVAL;
  4553. wlc->SRL = srl;
  4554. wlc->LRL = lrl;
  4555. brcms_b_retrylimit_upd(wlc->hw, wlc->SRL, wlc->LRL);
  4556. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  4557. wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
  4558. EDCF_SHORT, wlc->SRL);
  4559. wlc->wme_retries[ac] = SFIELD(wlc->wme_retries[ac],
  4560. EDCF_LONG, wlc->LRL);
  4561. }
  4562. brcms_c_wme_retries_write(wlc);
  4563. return 0;
  4564. }
  4565. void brcms_c_get_current_rateset(struct brcms_c_info *wlc,
  4566. struct brcm_rateset *currs)
  4567. {
  4568. struct brcms_c_rateset *rs;
  4569. if (wlc->pub->associated)
  4570. rs = &wlc->bsscfg->current_bss->rateset;
  4571. else
  4572. rs = &wlc->default_bss->rateset;
  4573. /* Copy only legacy rateset section */
  4574. currs->count = rs->count;
  4575. memcpy(&currs->rates, &rs->rates, rs->count);
  4576. }
  4577. int brcms_c_set_rateset(struct brcms_c_info *wlc, struct brcm_rateset *rs)
  4578. {
  4579. struct brcms_c_rateset internal_rs;
  4580. int bcmerror;
  4581. if (rs->count > BRCMS_NUMRATES)
  4582. return -ENOBUFS;
  4583. memset(&internal_rs, 0, sizeof(internal_rs));
  4584. /* Copy only legacy rateset section */
  4585. internal_rs.count = rs->count;
  4586. memcpy(&internal_rs.rates, &rs->rates, internal_rs.count);
  4587. /* merge rateset coming in with the current mcsset */
  4588. if (wlc->pub->_n_enab & SUPPORT_11N) {
  4589. struct brcms_bss_info *mcsset_bss;
  4590. if (wlc->bsscfg->associated)
  4591. mcsset_bss = wlc->bsscfg->current_bss;
  4592. else
  4593. mcsset_bss = wlc->default_bss;
  4594. memcpy(internal_rs.mcs, &mcsset_bss->rateset.mcs[0],
  4595. MCSSET_LEN);
  4596. }
  4597. bcmerror = brcms_c_set_internal_rateset(wlc, &internal_rs);
  4598. if (!bcmerror)
  4599. brcms_c_ofdm_rateset_war(wlc);
  4600. return bcmerror;
  4601. }
  4602. int brcms_c_set_beacon_period(struct brcms_c_info *wlc, u16 period)
  4603. {
  4604. if (period == 0)
  4605. return -EINVAL;
  4606. wlc->default_bss->beacon_period = period;
  4607. return 0;
  4608. }
  4609. u16 brcms_c_get_phy_type(struct brcms_c_info *wlc, int phyidx)
  4610. {
  4611. return wlc->band->phytype;
  4612. }
  4613. void brcms_c_set_shortslot_override(struct brcms_c_info *wlc, s8 sslot_override)
  4614. {
  4615. wlc->shortslot_override = sslot_override;
  4616. /*
  4617. * shortslot is an 11g feature, so no more work if we are
  4618. * currently on the 5G band
  4619. */
  4620. if (wlc->band->bandtype == BRCM_BAND_5G)
  4621. return;
  4622. if (wlc->pub->up && wlc->pub->associated) {
  4623. /* let watchdog or beacon processing update shortslot */
  4624. } else if (wlc->pub->up) {
  4625. /* unassociated shortslot is off */
  4626. brcms_c_switch_shortslot(wlc, false);
  4627. } else {
  4628. /* driver is down, so just update the brcms_c_info
  4629. * value */
  4630. if (wlc->shortslot_override == BRCMS_SHORTSLOT_AUTO)
  4631. wlc->shortslot = false;
  4632. else
  4633. wlc->shortslot =
  4634. (wlc->shortslot_override ==
  4635. BRCMS_SHORTSLOT_ON);
  4636. }
  4637. }
  4638. /*
  4639. * register watchdog and down handlers.
  4640. */
  4641. int brcms_c_module_register(struct brcms_pub *pub,
  4642. const char *name, struct brcms_info *hdl,
  4643. int (*d_fn)(void *handle))
  4644. {
  4645. struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
  4646. int i;
  4647. /* find an empty entry and just add, no duplication check! */
  4648. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4649. if (wlc->modulecb[i].name[0] == '\0') {
  4650. strncpy(wlc->modulecb[i].name, name,
  4651. sizeof(wlc->modulecb[i].name) - 1);
  4652. wlc->modulecb[i].hdl = hdl;
  4653. wlc->modulecb[i].down_fn = d_fn;
  4654. return 0;
  4655. }
  4656. }
  4657. return -ENOSR;
  4658. }
  4659. /* unregister module callbacks */
  4660. int brcms_c_module_unregister(struct brcms_pub *pub, const char *name,
  4661. struct brcms_info *hdl)
  4662. {
  4663. struct brcms_c_info *wlc = (struct brcms_c_info *) pub->wlc;
  4664. int i;
  4665. if (wlc == NULL)
  4666. return -ENODATA;
  4667. for (i = 0; i < BRCMS_MAXMODULES; i++) {
  4668. if (!strcmp(wlc->modulecb[i].name, name) &&
  4669. (wlc->modulecb[i].hdl == hdl)) {
  4670. memset(&wlc->modulecb[i], 0, sizeof(wlc->modulecb[i]));
  4671. return 0;
  4672. }
  4673. }
  4674. /* table not found! */
  4675. return -ENODATA;
  4676. }
  4677. static bool brcms_c_chipmatch_pci(struct bcma_device *core)
  4678. {
  4679. struct pci_dev *pcidev = core->bus->host_pci;
  4680. u16 vendor = pcidev->vendor;
  4681. u16 device = pcidev->device;
  4682. if (vendor != PCI_VENDOR_ID_BROADCOM) {
  4683. pr_err("unknown vendor id %04x\n", vendor);
  4684. return false;
  4685. }
  4686. if (device == BCM43224_D11N_ID_VEN1 || device == BCM43224_CHIP_ID)
  4687. return true;
  4688. if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
  4689. return true;
  4690. if (device == BCM4313_D11N2G_ID)
  4691. return true;
  4692. if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
  4693. return true;
  4694. pr_err("unknown device id %04x\n", device);
  4695. return false;
  4696. }
  4697. static bool brcms_c_chipmatch_soc(struct bcma_device *core)
  4698. {
  4699. struct bcma_chipinfo *chipinfo = &core->bus->chipinfo;
  4700. if (chipinfo->id == BCMA_CHIP_ID_BCM4716)
  4701. return true;
  4702. pr_err("unknown chip id %04x\n", chipinfo->id);
  4703. return false;
  4704. }
  4705. bool brcms_c_chipmatch(struct bcma_device *core)
  4706. {
  4707. switch (core->bus->hosttype) {
  4708. case BCMA_HOSTTYPE_PCI:
  4709. return brcms_c_chipmatch_pci(core);
  4710. case BCMA_HOSTTYPE_SOC:
  4711. return brcms_c_chipmatch_soc(core);
  4712. default:
  4713. pr_err("unknown host type: %i\n", core->bus->hosttype);
  4714. return false;
  4715. }
  4716. }
  4717. u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
  4718. {
  4719. u16 table_ptr;
  4720. u8 phy_rate, index;
  4721. /* get the phy specific rate encoding for the PLCP SIGNAL field */
  4722. if (is_ofdm_rate(rate))
  4723. table_ptr = M_RT_DIRMAP_A;
  4724. else
  4725. table_ptr = M_RT_DIRMAP_B;
  4726. /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
  4727. * the index into the rate table.
  4728. */
  4729. phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
  4730. index = phy_rate & 0xf;
  4731. /* Find the SHM pointer to the rate table entry by looking in the
  4732. * Direct-map Table
  4733. */
  4734. return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
  4735. }
  4736. /*
  4737. * bcmc_fid_generate:
  4738. * Generate frame ID for a BCMC packet. The frag field is not used
  4739. * for MC frames so is used as part of the sequence number.
  4740. */
  4741. static inline u16
  4742. bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
  4743. struct d11txh *txh)
  4744. {
  4745. u16 frameid;
  4746. frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
  4747. TXFID_QUEUE_MASK);
  4748. frameid |=
  4749. (((wlc->
  4750. mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  4751. TX_BCMC_FIFO;
  4752. return frameid;
  4753. }
  4754. static uint
  4755. brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
  4756. u8 preamble_type)
  4757. {
  4758. uint dur = 0;
  4759. /*
  4760. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  4761. * is less than or equal to the rate of the immediately previous
  4762. * frame in the FES
  4763. */
  4764. rspec = brcms_basic_rate(wlc, rspec);
  4765. /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
  4766. dur =
  4767. brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  4768. (DOT11_ACK_LEN + FCS_LEN));
  4769. return dur;
  4770. }
  4771. static uint
  4772. brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
  4773. u8 preamble_type)
  4774. {
  4775. return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
  4776. }
  4777. static uint
  4778. brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
  4779. u8 preamble_type)
  4780. {
  4781. /*
  4782. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  4783. * is less than or equal to the rate of the immediately previous
  4784. * frame in the FES
  4785. */
  4786. rspec = brcms_basic_rate(wlc, rspec);
  4787. /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
  4788. return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  4789. (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
  4790. FCS_LEN));
  4791. }
  4792. /* brcms_c_compute_frame_dur()
  4793. *
  4794. * Calculate the 802.11 MAC header DUR field for MPDU
  4795. * DUR for a single frame = 1 SIFS + 1 ACK
  4796. * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
  4797. *
  4798. * rate MPDU rate in unit of 500kbps
  4799. * next_frag_len next MPDU length in bytes
  4800. * preamble_type use short/GF or long/MM PLCP header
  4801. */
  4802. static u16
  4803. brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
  4804. u8 preamble_type, uint next_frag_len)
  4805. {
  4806. u16 dur, sifs;
  4807. sifs = get_sifs(wlc->band);
  4808. dur = sifs;
  4809. dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
  4810. if (next_frag_len) {
  4811. /* Double the current DUR to get 2 SIFS + 2 ACKs */
  4812. dur *= 2;
  4813. /* add another SIFS and the frag time */
  4814. dur += sifs;
  4815. dur +=
  4816. (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
  4817. next_frag_len);
  4818. }
  4819. return dur;
  4820. }
  4821. /* The opposite of brcms_c_calc_frame_time */
  4822. static uint
  4823. brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
  4824. u8 preamble_type, uint dur)
  4825. {
  4826. uint nsyms, mac_len, Ndps, kNdps;
  4827. uint rate = rspec2rate(ratespec);
  4828. if (is_mcs_rate(ratespec)) {
  4829. uint mcs = ratespec & RSPEC_RATE_MASK;
  4830. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  4831. dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  4832. /* payload calculation matches that of regular ofdm */
  4833. if (wlc->band->bandtype == BRCM_BAND_2G)
  4834. dur -= DOT11_OFDM_SIGNAL_EXTENSION;
  4835. /* kNdbps = kbps * 4 */
  4836. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  4837. rspec_issgi(ratespec)) * 4;
  4838. nsyms = dur / APHY_SYMBOL_TIME;
  4839. mac_len =
  4840. ((nsyms * kNdps) -
  4841. ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
  4842. } else if (is_ofdm_rate(ratespec)) {
  4843. dur -= APHY_PREAMBLE_TIME;
  4844. dur -= APHY_SIGNAL_TIME;
  4845. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  4846. Ndps = rate * 2;
  4847. nsyms = dur / APHY_SYMBOL_TIME;
  4848. mac_len =
  4849. ((nsyms * Ndps) -
  4850. (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
  4851. } else {
  4852. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  4853. dur -= BPHY_PLCP_SHORT_TIME;
  4854. else
  4855. dur -= BPHY_PLCP_TIME;
  4856. mac_len = dur * rate;
  4857. /* divide out factor of 2 in rate (1/2 mbps) */
  4858. mac_len = mac_len / 8 / 2;
  4859. }
  4860. return mac_len;
  4861. }
  4862. /*
  4863. * Return true if the specified rate is supported by the specified band.
  4864. * BRCM_BAND_AUTO indicates the current band.
  4865. */
  4866. static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
  4867. bool verbose)
  4868. {
  4869. struct brcms_c_rateset *hw_rateset;
  4870. uint i;
  4871. if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
  4872. hw_rateset = &wlc->band->hw_rateset;
  4873. else if (wlc->pub->_nbands > 1)
  4874. hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
  4875. else
  4876. /* other band specified and we are a single band device */
  4877. return false;
  4878. /* check if this is a mimo rate */
  4879. if (is_mcs_rate(rspec)) {
  4880. if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
  4881. goto error;
  4882. return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
  4883. }
  4884. for (i = 0; i < hw_rateset->count; i++)
  4885. if (hw_rateset->rates[i] == rspec2rate(rspec))
  4886. return true;
  4887. error:
  4888. if (verbose)
  4889. brcms_err(wlc->hw->d11core, "wl%d: valid_rate: rate spec 0x%x "
  4890. "not in hw_rateset\n", wlc->pub->unit, rspec);
  4891. return false;
  4892. }
  4893. static u32
  4894. mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
  4895. u32 int_val)
  4896. {
  4897. struct bcma_device *core = wlc->hw->d11core;
  4898. u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
  4899. u8 rate = int_val & NRATE_RATE_MASK;
  4900. u32 rspec;
  4901. bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
  4902. bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
  4903. bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
  4904. == NRATE_OVERRIDE_MCS_ONLY);
  4905. int bcmerror = 0;
  4906. if (!ismcs)
  4907. return (u32) rate;
  4908. /* validate the combination of rate/mcs/stf is allowed */
  4909. if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
  4910. /* mcs only allowed when nmode */
  4911. if (stf > PHY_TXC1_MODE_SDM) {
  4912. brcms_err(core, "wl%d: %s: Invalid stf\n",
  4913. wlc->pub->unit, __func__);
  4914. bcmerror = -EINVAL;
  4915. goto done;
  4916. }
  4917. /* mcs 32 is a special case, DUP mode 40 only */
  4918. if (rate == 32) {
  4919. if (!CHSPEC_IS40(wlc->home_chanspec) ||
  4920. ((stf != PHY_TXC1_MODE_SISO)
  4921. && (stf != PHY_TXC1_MODE_CDD))) {
  4922. brcms_err(core, "wl%d: %s: Invalid mcs 32\n",
  4923. wlc->pub->unit, __func__);
  4924. bcmerror = -EINVAL;
  4925. goto done;
  4926. }
  4927. /* mcs > 7 must use stf SDM */
  4928. } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
  4929. /* mcs > 7 must use stf SDM */
  4930. if (stf != PHY_TXC1_MODE_SDM) {
  4931. brcms_dbg_mac80211(core, "wl%d: enabling "
  4932. "SDM mode for mcs %d\n",
  4933. wlc->pub->unit, rate);
  4934. stf = PHY_TXC1_MODE_SDM;
  4935. }
  4936. } else {
  4937. /*
  4938. * MCS 0-7 may use SISO, CDD, and for
  4939. * phy_rev >= 3 STBC
  4940. */
  4941. if ((stf > PHY_TXC1_MODE_STBC) ||
  4942. (!BRCMS_STBC_CAP_PHY(wlc)
  4943. && (stf == PHY_TXC1_MODE_STBC))) {
  4944. brcms_err(core, "wl%d: %s: Invalid STBC\n",
  4945. wlc->pub->unit, __func__);
  4946. bcmerror = -EINVAL;
  4947. goto done;
  4948. }
  4949. }
  4950. } else if (is_ofdm_rate(rate)) {
  4951. if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
  4952. brcms_err(core, "wl%d: %s: Invalid OFDM\n",
  4953. wlc->pub->unit, __func__);
  4954. bcmerror = -EINVAL;
  4955. goto done;
  4956. }
  4957. } else if (is_cck_rate(rate)) {
  4958. if ((cur_band->bandtype != BRCM_BAND_2G)
  4959. || (stf != PHY_TXC1_MODE_SISO)) {
  4960. brcms_err(core, "wl%d: %s: Invalid CCK\n",
  4961. wlc->pub->unit, __func__);
  4962. bcmerror = -EINVAL;
  4963. goto done;
  4964. }
  4965. } else {
  4966. brcms_err(core, "wl%d: %s: Unknown rate type\n",
  4967. wlc->pub->unit, __func__);
  4968. bcmerror = -EINVAL;
  4969. goto done;
  4970. }
  4971. /* make sure multiple antennae are available for non-siso rates */
  4972. if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
  4973. brcms_err(core, "wl%d: %s: SISO antenna but !SISO "
  4974. "request\n", wlc->pub->unit, __func__);
  4975. bcmerror = -EINVAL;
  4976. goto done;
  4977. }
  4978. rspec = rate;
  4979. if (ismcs) {
  4980. rspec |= RSPEC_MIMORATE;
  4981. /* For STBC populate the STC field of the ratespec */
  4982. if (stf == PHY_TXC1_MODE_STBC) {
  4983. u8 stc;
  4984. stc = 1; /* Nss for single stream is always 1 */
  4985. rspec |= (stc << RSPEC_STC_SHIFT);
  4986. }
  4987. }
  4988. rspec |= (stf << RSPEC_STF_SHIFT);
  4989. if (override_mcs_only)
  4990. rspec |= RSPEC_OVERRIDE_MCS_ONLY;
  4991. if (issgi)
  4992. rspec |= RSPEC_SHORT_GI;
  4993. if ((rate != 0)
  4994. && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
  4995. return rate;
  4996. return rspec;
  4997. done:
  4998. return rate;
  4999. }
  5000. /*
  5001. * Compute PLCP, but only requires actual rate and length of pkt.
  5002. * Rate is given in the driver standard multiple of 500 kbps.
  5003. * le is set for 11 Mbps rate if necessary.
  5004. * Broken out for PRQ.
  5005. */
  5006. static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
  5007. uint length, u8 *plcp)
  5008. {
  5009. u16 usec = 0;
  5010. u8 le = 0;
  5011. switch (rate_500) {
  5012. case BRCM_RATE_1M:
  5013. usec = length << 3;
  5014. break;
  5015. case BRCM_RATE_2M:
  5016. usec = length << 2;
  5017. break;
  5018. case BRCM_RATE_5M5:
  5019. usec = (length << 4) / 11;
  5020. if ((length << 4) - (usec * 11) > 0)
  5021. usec++;
  5022. break;
  5023. case BRCM_RATE_11M:
  5024. usec = (length << 3) / 11;
  5025. if ((length << 3) - (usec * 11) > 0) {
  5026. usec++;
  5027. if ((usec * 11) - (length << 3) >= 8)
  5028. le = D11B_PLCP_SIGNAL_LE;
  5029. }
  5030. break;
  5031. default:
  5032. brcms_err(wlc->hw->d11core,
  5033. "brcms_c_cck_plcp_set: unsupported rate %d\n",
  5034. rate_500);
  5035. rate_500 = BRCM_RATE_1M;
  5036. usec = length << 3;
  5037. break;
  5038. }
  5039. /* PLCP signal byte */
  5040. plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
  5041. /* PLCP service byte */
  5042. plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
  5043. /* PLCP length u16, little endian */
  5044. plcp[2] = usec & 0xff;
  5045. plcp[3] = (usec >> 8) & 0xff;
  5046. /* PLCP CRC16 */
  5047. plcp[4] = 0;
  5048. plcp[5] = 0;
  5049. }
  5050. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5051. static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
  5052. {
  5053. u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
  5054. plcp[0] = mcs;
  5055. if (rspec_is40mhz(rspec) || (mcs == 32))
  5056. plcp[0] |= MIMO_PLCP_40MHZ;
  5057. BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
  5058. plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
  5059. plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
  5060. plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
  5061. plcp[5] = 0;
  5062. }
  5063. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5064. static void
  5065. brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
  5066. {
  5067. u8 rate_signal;
  5068. u32 tmp = 0;
  5069. int rate = rspec2rate(rspec);
  5070. /*
  5071. * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
  5072. * transmitted first
  5073. */
  5074. rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
  5075. memset(plcp, 0, D11_PHY_HDR_LEN);
  5076. D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
  5077. tmp = (length & 0xfff) << 5;
  5078. plcp[2] |= (tmp >> 16) & 0xff;
  5079. plcp[1] |= (tmp >> 8) & 0xff;
  5080. plcp[0] |= tmp & 0xff;
  5081. }
  5082. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5083. static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
  5084. uint length, u8 *plcp)
  5085. {
  5086. int rate = rspec2rate(rspec);
  5087. brcms_c_cck_plcp_set(wlc, rate, length, plcp);
  5088. }
  5089. static void
  5090. brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
  5091. uint length, u8 *plcp)
  5092. {
  5093. if (is_mcs_rate(rspec))
  5094. brcms_c_compute_mimo_plcp(rspec, length, plcp);
  5095. else if (is_ofdm_rate(rspec))
  5096. brcms_c_compute_ofdm_plcp(rspec, length, plcp);
  5097. else
  5098. brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
  5099. }
  5100. /* brcms_c_compute_rtscts_dur()
  5101. *
  5102. * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
  5103. * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
  5104. * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
  5105. *
  5106. * cts cts-to-self or rts/cts
  5107. * rts_rate rts or cts rate in unit of 500kbps
  5108. * rate next MPDU rate in unit of 500kbps
  5109. * frame_len next MPDU frame length in bytes
  5110. */
  5111. u16
  5112. brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
  5113. u32 rts_rate,
  5114. u32 frame_rate, u8 rts_preamble_type,
  5115. u8 frame_preamble_type, uint frame_len, bool ba)
  5116. {
  5117. u16 dur, sifs;
  5118. sifs = get_sifs(wlc->band);
  5119. if (!cts_only) {
  5120. /* RTS/CTS */
  5121. dur = 3 * sifs;
  5122. dur +=
  5123. (u16) brcms_c_calc_cts_time(wlc, rts_rate,
  5124. rts_preamble_type);
  5125. } else {
  5126. /* CTS-TO-SELF */
  5127. dur = 2 * sifs;
  5128. }
  5129. dur +=
  5130. (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
  5131. frame_len);
  5132. if (ba)
  5133. dur +=
  5134. (u16) brcms_c_calc_ba_time(wlc, frame_rate,
  5135. BRCMS_SHORT_PREAMBLE);
  5136. else
  5137. dur +=
  5138. (u16) brcms_c_calc_ack_time(wlc, frame_rate,
  5139. frame_preamble_type);
  5140. return dur;
  5141. }
  5142. static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
  5143. {
  5144. u16 phyctl1 = 0;
  5145. u16 bw;
  5146. if (BRCMS_ISLCNPHY(wlc->band)) {
  5147. bw = PHY_TXC1_BW_20MHZ;
  5148. } else {
  5149. bw = rspec_get_bw(rspec);
  5150. /* 10Mhz is not supported yet */
  5151. if (bw < PHY_TXC1_BW_20MHZ) {
  5152. brcms_err(wlc->hw->d11core, "phytxctl1_calc: bw %d is "
  5153. "not supported yet, set to 20L\n", bw);
  5154. bw = PHY_TXC1_BW_20MHZ;
  5155. }
  5156. }
  5157. if (is_mcs_rate(rspec)) {
  5158. uint mcs = rspec & RSPEC_RATE_MASK;
  5159. /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
  5160. phyctl1 = rspec_phytxbyte2(rspec);
  5161. /* set the upper byte of phyctl1 */
  5162. phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
  5163. } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
  5164. && !BRCMS_ISSSLPNPHY(wlc->band)) {
  5165. /*
  5166. * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
  5167. * Data Rate. Eventually MIMOPHY would also be converted to
  5168. * this format
  5169. */
  5170. /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
  5171. phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5172. } else { /* legacy OFDM/CCK */
  5173. s16 phycfg;
  5174. /* get the phyctl byte from rate phycfg table */
  5175. phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
  5176. if (phycfg == -1) {
  5177. brcms_err(wlc->hw->d11core, "phytxctl1_calc: wrong "
  5178. "legacy OFDM/CCK rate\n");
  5179. phycfg = 0;
  5180. }
  5181. /* set the upper byte of phyctl1 */
  5182. phyctl1 =
  5183. (bw | (phycfg << 8) |
  5184. (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5185. }
  5186. return phyctl1;
  5187. }
  5188. /*
  5189. * Add struct d11txh, struct cck_phy_hdr.
  5190. *
  5191. * 'p' data must start with 802.11 MAC header
  5192. * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
  5193. *
  5194. * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
  5195. *
  5196. */
  5197. static u16
  5198. brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
  5199. struct sk_buff *p, struct scb *scb, uint frag,
  5200. uint nfrags, uint queue, uint next_frag_len)
  5201. {
  5202. struct ieee80211_hdr *h;
  5203. struct d11txh *txh;
  5204. u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
  5205. int len, phylen, rts_phylen;
  5206. u16 mch, phyctl, xfts, mainrates;
  5207. u16 seq = 0, mcl = 0, status = 0, frameid = 0;
  5208. u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5209. u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5210. bool use_rts = false;
  5211. bool use_cts = false;
  5212. bool use_rifs = false;
  5213. bool short_preamble[2] = { false, false };
  5214. u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5215. u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5216. u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
  5217. struct ieee80211_rts *rts = NULL;
  5218. bool qos;
  5219. uint ac;
  5220. bool hwtkmic = false;
  5221. u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
  5222. #define ANTCFG_NONE 0xFF
  5223. u8 antcfg = ANTCFG_NONE;
  5224. u8 fbantcfg = ANTCFG_NONE;
  5225. uint phyctl1_stf = 0;
  5226. u16 durid = 0;
  5227. struct ieee80211_tx_rate *txrate[2];
  5228. int k;
  5229. struct ieee80211_tx_info *tx_info;
  5230. bool is_mcs;
  5231. u16 mimo_txbw;
  5232. u8 mimo_preamble_type;
  5233. /* locate 802.11 MAC header */
  5234. h = (struct ieee80211_hdr *)(p->data);
  5235. qos = ieee80211_is_data_qos(h->frame_control);
  5236. /* compute length of frame in bytes for use in PLCP computations */
  5237. len = p->len;
  5238. phylen = len + FCS_LEN;
  5239. /* Get tx_info */
  5240. tx_info = IEEE80211_SKB_CB(p);
  5241. /* add PLCP */
  5242. plcp = skb_push(p, D11_PHY_HDR_LEN);
  5243. /* add Broadcom tx descriptor header */
  5244. txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
  5245. memset(txh, 0, D11_TXH_LEN);
  5246. /* setup frameid */
  5247. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  5248. /* non-AP STA should never use BCMC queue */
  5249. if (queue == TX_BCMC_FIFO) {
  5250. brcms_err(wlc->hw->d11core,
  5251. "wl%d: %s: ASSERT queue == TX_BCMC!\n",
  5252. wlc->pub->unit, __func__);
  5253. frameid = bcmc_fid_generate(wlc, NULL, txh);
  5254. } else {
  5255. /* Increment the counter for first fragment */
  5256. if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  5257. scb->seqnum[p->priority]++;
  5258. /* extract fragment number from frame first */
  5259. seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
  5260. seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
  5261. h->seq_ctrl = cpu_to_le16(seq);
  5262. frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  5263. (queue & TXFID_QUEUE_MASK);
  5264. }
  5265. }
  5266. frameid |= queue & TXFID_QUEUE_MASK;
  5267. /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
  5268. if (ieee80211_is_beacon(h->frame_control))
  5269. mcl |= TXC_IGNOREPMQ;
  5270. txrate[0] = tx_info->control.rates;
  5271. txrate[1] = txrate[0] + 1;
  5272. /*
  5273. * if rate control algorithm didn't give us a fallback
  5274. * rate, use the primary rate
  5275. */
  5276. if (txrate[1]->idx < 0)
  5277. txrate[1] = txrate[0];
  5278. for (k = 0; k < hw->max_rates; k++) {
  5279. is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
  5280. if (!is_mcs) {
  5281. if ((txrate[k]->idx >= 0)
  5282. && (txrate[k]->idx <
  5283. hw->wiphy->bands[tx_info->band]->n_bitrates)) {
  5284. rspec[k] =
  5285. hw->wiphy->bands[tx_info->band]->
  5286. bitrates[txrate[k]->idx].hw_value;
  5287. short_preamble[k] =
  5288. txrate[k]->
  5289. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
  5290. true : false;
  5291. } else {
  5292. rspec[k] = BRCM_RATE_1M;
  5293. }
  5294. } else {
  5295. rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
  5296. NRATE_MCS_INUSE | txrate[k]->idx);
  5297. }
  5298. /*
  5299. * Currently only support same setting for primay and
  5300. * fallback rates. Unify flags for each rate into a
  5301. * single value for the frame
  5302. */
  5303. use_rts |=
  5304. txrate[k]->
  5305. flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
  5306. use_cts |=
  5307. txrate[k]->
  5308. flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
  5309. /*
  5310. * (1) RATE:
  5311. * determine and validate primary rate
  5312. * and fallback rates
  5313. */
  5314. if (!rspec_active(rspec[k])) {
  5315. rspec[k] = BRCM_RATE_1M;
  5316. } else {
  5317. if (!is_multicast_ether_addr(h->addr1)) {
  5318. /* set tx antenna config */
  5319. brcms_c_antsel_antcfg_get(wlc->asi, false,
  5320. false, 0, 0, &antcfg, &fbantcfg);
  5321. }
  5322. }
  5323. }
  5324. phyctl1_stf = wlc->stf->ss_opmode;
  5325. if (wlc->pub->_n_enab & SUPPORT_11N) {
  5326. for (k = 0; k < hw->max_rates; k++) {
  5327. /*
  5328. * apply siso/cdd to single stream mcs's or ofdm
  5329. * if rspec is auto selected
  5330. */
  5331. if (((is_mcs_rate(rspec[k]) &&
  5332. is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
  5333. is_ofdm_rate(rspec[k]))
  5334. && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
  5335. || !(rspec[k] & RSPEC_OVERRIDE))) {
  5336. rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
  5337. /* For SISO MCS use STBC if possible */
  5338. if (is_mcs_rate(rspec[k])
  5339. && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
  5340. u8 stc;
  5341. /* Nss for single stream is always 1 */
  5342. stc = 1;
  5343. rspec[k] |= (PHY_TXC1_MODE_STBC <<
  5344. RSPEC_STF_SHIFT) |
  5345. (stc << RSPEC_STC_SHIFT);
  5346. } else
  5347. rspec[k] |=
  5348. (phyctl1_stf << RSPEC_STF_SHIFT);
  5349. }
  5350. /*
  5351. * Is the phy configured to use 40MHZ frames? If
  5352. * so then pick the desired txbw
  5353. */
  5354. if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
  5355. /* default txbw is 20in40 SB */
  5356. mimo_ctlchbw = mimo_txbw =
  5357. CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
  5358. wlc->band->pi))
  5359. ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
  5360. if (is_mcs_rate(rspec[k])) {
  5361. /* mcs 32 must be 40b/w DUP */
  5362. if ((rspec[k] & RSPEC_RATE_MASK)
  5363. == 32) {
  5364. mimo_txbw =
  5365. PHY_TXC1_BW_40MHZ_DUP;
  5366. /* use override */
  5367. } else if (wlc->mimo_40txbw != AUTO)
  5368. mimo_txbw = wlc->mimo_40txbw;
  5369. /* else check if dst is using 40 Mhz */
  5370. else if (scb->flags & SCB_IS40)
  5371. mimo_txbw = PHY_TXC1_BW_40MHZ;
  5372. } else if (is_ofdm_rate(rspec[k])) {
  5373. if (wlc->ofdm_40txbw != AUTO)
  5374. mimo_txbw = wlc->ofdm_40txbw;
  5375. } else if (wlc->cck_40txbw != AUTO) {
  5376. mimo_txbw = wlc->cck_40txbw;
  5377. }
  5378. } else {
  5379. /*
  5380. * mcs32 is 40 b/w only.
  5381. * This is possible for probe packets on
  5382. * a STA during SCAN
  5383. */
  5384. if ((rspec[k] & RSPEC_RATE_MASK) == 32)
  5385. /* mcs 0 */
  5386. rspec[k] = RSPEC_MIMORATE;
  5387. mimo_txbw = PHY_TXC1_BW_20MHZ;
  5388. }
  5389. /* Set channel width */
  5390. rspec[k] &= ~RSPEC_BW_MASK;
  5391. if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
  5392. rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
  5393. else
  5394. rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  5395. /* Disable short GI, not supported yet */
  5396. rspec[k] &= ~RSPEC_SHORT_GI;
  5397. mimo_preamble_type = BRCMS_MM_PREAMBLE;
  5398. if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
  5399. mimo_preamble_type = BRCMS_GF_PREAMBLE;
  5400. if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
  5401. && (!is_mcs_rate(rspec[k]))) {
  5402. brcms_warn(wlc->hw->d11core,
  5403. "wl%d: %s: IEEE80211_TX_RC_MCS != is_mcs_rate(rspec)\n",
  5404. wlc->pub->unit, __func__);
  5405. }
  5406. if (is_mcs_rate(rspec[k])) {
  5407. preamble_type[k] = mimo_preamble_type;
  5408. /*
  5409. * if SGI is selected, then forced mm
  5410. * for single stream
  5411. */
  5412. if ((rspec[k] & RSPEC_SHORT_GI)
  5413. && is_single_stream(rspec[k] &
  5414. RSPEC_RATE_MASK))
  5415. preamble_type[k] = BRCMS_MM_PREAMBLE;
  5416. }
  5417. /* should be better conditionalized */
  5418. if (!is_mcs_rate(rspec[0])
  5419. && (tx_info->control.rates[0].
  5420. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
  5421. preamble_type[k] = BRCMS_SHORT_PREAMBLE;
  5422. }
  5423. } else {
  5424. for (k = 0; k < hw->max_rates; k++) {
  5425. /* Set ctrlchbw as 20Mhz */
  5426. rspec[k] &= ~RSPEC_BW_MASK;
  5427. rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
  5428. /* for nphy, stf of ofdm frames must follow policies */
  5429. if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
  5430. rspec[k] &= ~RSPEC_STF_MASK;
  5431. rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
  5432. }
  5433. }
  5434. }
  5435. /* Reset these for use with AMPDU's */
  5436. txrate[0]->count = 0;
  5437. txrate[1]->count = 0;
  5438. /* (2) PROTECTION, may change rspec */
  5439. if ((ieee80211_is_data(h->frame_control) ||
  5440. ieee80211_is_mgmt(h->frame_control)) &&
  5441. (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
  5442. use_rts = true;
  5443. /* (3) PLCP: determine PLCP header and MAC duration,
  5444. * fill struct d11txh */
  5445. brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
  5446. brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
  5447. memcpy(&txh->FragPLCPFallback,
  5448. plcp_fallback, sizeof(txh->FragPLCPFallback));
  5449. /* Length field now put in CCK FBR CRC field */
  5450. if (is_cck_rate(rspec[1])) {
  5451. txh->FragPLCPFallback[4] = phylen & 0xff;
  5452. txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
  5453. }
  5454. /* MIMO-RATE: need validation ?? */
  5455. mainrates = is_ofdm_rate(rspec[0]) ?
  5456. D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
  5457. plcp[0];
  5458. /* DUR field for main rate */
  5459. if (!ieee80211_is_pspoll(h->frame_control) &&
  5460. !is_multicast_ether_addr(h->addr1) && !use_rifs) {
  5461. durid =
  5462. brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
  5463. next_frag_len);
  5464. h->duration_id = cpu_to_le16(durid);
  5465. } else if (use_rifs) {
  5466. /* NAV protect to end of next max packet size */
  5467. durid =
  5468. (u16) brcms_c_calc_frame_time(wlc, rspec[0],
  5469. preamble_type[0],
  5470. DOT11_MAX_FRAG_LEN);
  5471. durid += RIFS_11N_TIME;
  5472. h->duration_id = cpu_to_le16(durid);
  5473. }
  5474. /* DUR field for fallback rate */
  5475. if (ieee80211_is_pspoll(h->frame_control))
  5476. txh->FragDurFallback = h->duration_id;
  5477. else if (is_multicast_ether_addr(h->addr1) || use_rifs)
  5478. txh->FragDurFallback = 0;
  5479. else {
  5480. durid = brcms_c_compute_frame_dur(wlc, rspec[1],
  5481. preamble_type[1], next_frag_len);
  5482. txh->FragDurFallback = cpu_to_le16(durid);
  5483. }
  5484. /* (4) MAC-HDR: MacTxControlLow */
  5485. if (frag == 0)
  5486. mcl |= TXC_STARTMSDU;
  5487. if (!is_multicast_ether_addr(h->addr1))
  5488. mcl |= TXC_IMMEDACK;
  5489. if (wlc->band->bandtype == BRCM_BAND_5G)
  5490. mcl |= TXC_FREQBAND_5G;
  5491. if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
  5492. mcl |= TXC_BW_40;
  5493. /* set AMIC bit if using hardware TKIP MIC */
  5494. if (hwtkmic)
  5495. mcl |= TXC_AMIC;
  5496. txh->MacTxControlLow = cpu_to_le16(mcl);
  5497. /* MacTxControlHigh */
  5498. mch = 0;
  5499. /* Set fallback rate preamble type */
  5500. if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
  5501. (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
  5502. if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
  5503. mch |= TXC_PREAMBLE_DATA_FB_SHORT;
  5504. }
  5505. /* MacFrameControl */
  5506. memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
  5507. txh->TxFesTimeNormal = cpu_to_le16(0);
  5508. txh->TxFesTimeFallback = cpu_to_le16(0);
  5509. /* TxFrameRA */
  5510. memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
  5511. /* TxFrameID */
  5512. txh->TxFrameID = cpu_to_le16(frameid);
  5513. /*
  5514. * TxStatus, Note the case of recreating the first frag of a suppressed
  5515. * frame then we may need to reset the retry cnt's via the status reg
  5516. */
  5517. txh->TxStatus = cpu_to_le16(status);
  5518. /*
  5519. * extra fields for ucode AMPDU aggregation, the new fields are added to
  5520. * the END of previous structure so that it's compatible in driver.
  5521. */
  5522. txh->MaxNMpdus = cpu_to_le16(0);
  5523. txh->MaxABytes_MRT = cpu_to_le16(0);
  5524. txh->MaxABytes_FBR = cpu_to_le16(0);
  5525. txh->MinMBytes = cpu_to_le16(0);
  5526. /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
  5527. * furnish struct d11txh */
  5528. /* RTS PLCP header and RTS frame */
  5529. if (use_rts || use_cts) {
  5530. if (use_rts && use_cts)
  5531. use_cts = false;
  5532. for (k = 0; k < 2; k++) {
  5533. rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
  5534. false,
  5535. mimo_ctlchbw);
  5536. }
  5537. if (!is_ofdm_rate(rts_rspec[0]) &&
  5538. !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
  5539. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5540. rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
  5541. mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
  5542. }
  5543. if (!is_ofdm_rate(rts_rspec[1]) &&
  5544. !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
  5545. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5546. rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
  5547. mch |= TXC_PREAMBLE_RTS_FB_SHORT;
  5548. }
  5549. /* RTS/CTS additions to MacTxControlLow */
  5550. if (use_cts) {
  5551. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
  5552. } else {
  5553. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
  5554. txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
  5555. }
  5556. /* RTS PLCP header */
  5557. rts_plcp = txh->RTSPhyHeader;
  5558. if (use_cts)
  5559. rts_phylen = DOT11_CTS_LEN + FCS_LEN;
  5560. else
  5561. rts_phylen = DOT11_RTS_LEN + FCS_LEN;
  5562. brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
  5563. /* fallback rate version of RTS PLCP header */
  5564. brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
  5565. rts_plcp_fallback);
  5566. memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
  5567. sizeof(txh->RTSPLCPFallback));
  5568. /* RTS frame fields... */
  5569. rts = (struct ieee80211_rts *)&txh->rts_frame;
  5570. durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
  5571. rspec[0], rts_preamble_type[0],
  5572. preamble_type[0], phylen, false);
  5573. rts->duration = cpu_to_le16(durid);
  5574. /* fallback rate version of RTS DUR field */
  5575. durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
  5576. rts_rspec[1], rspec[1],
  5577. rts_preamble_type[1],
  5578. preamble_type[1], phylen, false);
  5579. txh->RTSDurFallback = cpu_to_le16(durid);
  5580. if (use_cts) {
  5581. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5582. IEEE80211_STYPE_CTS);
  5583. memcpy(&rts->ra, &h->addr2, ETH_ALEN);
  5584. } else {
  5585. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5586. IEEE80211_STYPE_RTS);
  5587. memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
  5588. }
  5589. /* mainrate
  5590. * low 8 bits: main frag rate/mcs,
  5591. * high 8 bits: rts/cts rate/mcs
  5592. */
  5593. mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
  5594. D11A_PHY_HDR_GRATE(
  5595. (struct ofdm_phy_hdr *) rts_plcp) :
  5596. rts_plcp[0]) << 8;
  5597. } else {
  5598. memset(txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
  5599. memset(&txh->rts_frame, 0, sizeof(struct ieee80211_rts));
  5600. memset(txh->RTSPLCPFallback, 0, sizeof(txh->RTSPLCPFallback));
  5601. txh->RTSDurFallback = 0;
  5602. }
  5603. #ifdef SUPPORT_40MHZ
  5604. /* add null delimiter count */
  5605. if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
  5606. txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
  5607. brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
  5608. #endif
  5609. /*
  5610. * Now that RTS/RTS FB preamble types are updated, write
  5611. * the final value
  5612. */
  5613. txh->MacTxControlHigh = cpu_to_le16(mch);
  5614. /*
  5615. * MainRates (both the rts and frag plcp rates have
  5616. * been calculated now)
  5617. */
  5618. txh->MainRates = cpu_to_le16(mainrates);
  5619. /* XtraFrameTypes */
  5620. xfts = frametype(rspec[1], wlc->mimoft);
  5621. xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
  5622. xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
  5623. xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
  5624. XFTS_CHANNEL_SHIFT;
  5625. txh->XtraFrameTypes = cpu_to_le16(xfts);
  5626. /* PhyTxControlWord */
  5627. phyctl = frametype(rspec[0], wlc->mimoft);
  5628. if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
  5629. (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
  5630. if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
  5631. phyctl |= PHY_TXC_SHORT_HDR;
  5632. }
  5633. /* phytxant is properly bit shifted */
  5634. phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
  5635. txh->PhyTxControlWord = cpu_to_le16(phyctl);
  5636. /* PhyTxControlWord_1 */
  5637. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  5638. u16 phyctl1 = 0;
  5639. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
  5640. txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
  5641. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
  5642. txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
  5643. if (use_rts || use_cts) {
  5644. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
  5645. txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
  5646. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
  5647. txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
  5648. }
  5649. /*
  5650. * For mcs frames, if mixedmode(overloaded with long preamble)
  5651. * is going to be set, fill in non-zero MModeLen and/or
  5652. * MModeFbrLen it will be unnecessary if they are separated
  5653. */
  5654. if (is_mcs_rate(rspec[0]) &&
  5655. (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
  5656. u16 mmodelen =
  5657. brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
  5658. txh->MModeLen = cpu_to_le16(mmodelen);
  5659. }
  5660. if (is_mcs_rate(rspec[1]) &&
  5661. (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
  5662. u16 mmodefbrlen =
  5663. brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
  5664. txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
  5665. }
  5666. }
  5667. ac = skb_get_queue_mapping(p);
  5668. if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
  5669. uint frag_dur, dur, dur_fallback;
  5670. /* WME: Update TXOP threshold */
  5671. if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
  5672. frag_dur =
  5673. brcms_c_calc_frame_time(wlc, rspec[0],
  5674. preamble_type[0], phylen);
  5675. if (rts) {
  5676. /* 1 RTS or CTS-to-self frame */
  5677. dur =
  5678. brcms_c_calc_cts_time(wlc, rts_rspec[0],
  5679. rts_preamble_type[0]);
  5680. dur_fallback =
  5681. brcms_c_calc_cts_time(wlc, rts_rspec[1],
  5682. rts_preamble_type[1]);
  5683. /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
  5684. dur += le16_to_cpu(rts->duration);
  5685. dur_fallback +=
  5686. le16_to_cpu(txh->RTSDurFallback);
  5687. } else if (use_rifs) {
  5688. dur = frag_dur;
  5689. dur_fallback = 0;
  5690. } else {
  5691. /* frame + SIFS + ACK */
  5692. dur = frag_dur;
  5693. dur +=
  5694. brcms_c_compute_frame_dur(wlc, rspec[0],
  5695. preamble_type[0], 0);
  5696. dur_fallback =
  5697. brcms_c_calc_frame_time(wlc, rspec[1],
  5698. preamble_type[1],
  5699. phylen);
  5700. dur_fallback +=
  5701. brcms_c_compute_frame_dur(wlc, rspec[1],
  5702. preamble_type[1], 0);
  5703. }
  5704. /* NEED to set TxFesTimeNormal (hard) */
  5705. txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
  5706. /*
  5707. * NEED to set fallback rate version of
  5708. * TxFesTimeNormal (hard)
  5709. */
  5710. txh->TxFesTimeFallback =
  5711. cpu_to_le16((u16) dur_fallback);
  5712. /*
  5713. * update txop byte threshold (txop minus intraframe
  5714. * overhead)
  5715. */
  5716. if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
  5717. uint newfragthresh;
  5718. newfragthresh =
  5719. brcms_c_calc_frame_len(wlc,
  5720. rspec[0], preamble_type[0],
  5721. (wlc->edcf_txop[ac] -
  5722. (dur - frag_dur)));
  5723. /* range bound the fragthreshold */
  5724. if (newfragthresh < DOT11_MIN_FRAG_LEN)
  5725. newfragthresh =
  5726. DOT11_MIN_FRAG_LEN;
  5727. else if (newfragthresh >
  5728. wlc->usr_fragthresh)
  5729. newfragthresh =
  5730. wlc->usr_fragthresh;
  5731. /* update the fragthresh and do txc update */
  5732. if (wlc->fragthresh[queue] !=
  5733. (u16) newfragthresh)
  5734. wlc->fragthresh[queue] =
  5735. (u16) newfragthresh;
  5736. } else {
  5737. brcms_warn(wlc->hw->d11core,
  5738. "wl%d: %s txop invalid for rate %d\n",
  5739. wlc->pub->unit, fifo_names[queue],
  5740. rspec2rate(rspec[0]));
  5741. }
  5742. if (dur > wlc->edcf_txop[ac])
  5743. brcms_warn(wlc->hw->d11core,
  5744. "wl%d: %s: %s txop exceeded phylen %d/%d dur %d/%d\n",
  5745. wlc->pub->unit, __func__,
  5746. fifo_names[queue],
  5747. phylen, wlc->fragthresh[queue],
  5748. dur, wlc->edcf_txop[ac]);
  5749. }
  5750. }
  5751. return 0;
  5752. }
  5753. static int brcms_c_tx(struct brcms_c_info *wlc, struct sk_buff *skb)
  5754. {
  5755. struct dma_pub *dma;
  5756. int fifo, ret = -ENOSPC;
  5757. struct d11txh *txh;
  5758. u16 frameid = INVALIDFID;
  5759. fifo = brcms_ac_to_fifo(skb_get_queue_mapping(skb));
  5760. dma = wlc->hw->di[fifo];
  5761. txh = (struct d11txh *)(skb->data);
  5762. if (dma->txavail == 0) {
  5763. /*
  5764. * We sometimes get a frame from mac80211 after stopping
  5765. * the queues. This only ever seems to be a single frame
  5766. * and is seems likely to be a race. TX_HEADROOM should
  5767. * ensure that we have enough space to handle these stray
  5768. * packets, so warn if there isn't. If we're out of space
  5769. * in the tx ring and the tx queue isn't stopped then
  5770. * we've really got a bug; warn loudly if that happens.
  5771. */
  5772. brcms_warn(wlc->hw->d11core,
  5773. "Received frame for tx with no space in DMA ring\n");
  5774. WARN_ON(!ieee80211_queue_stopped(wlc->pub->ieee_hw,
  5775. skb_get_queue_mapping(skb)));
  5776. return -ENOSPC;
  5777. }
  5778. /* When a BC/MC frame is being committed to the BCMC fifo
  5779. * via DMA (NOT PIO), update ucode or BSS info as appropriate.
  5780. */
  5781. if (fifo == TX_BCMC_FIFO)
  5782. frameid = le16_to_cpu(txh->TxFrameID);
  5783. /* Commit BCMC sequence number in the SHM frame ID location */
  5784. if (frameid != INVALIDFID) {
  5785. /*
  5786. * To inform the ucode of the last mcast frame posted
  5787. * so that it can clear moredata bit
  5788. */
  5789. brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
  5790. }
  5791. ret = brcms_c_txfifo(wlc, fifo, skb);
  5792. /*
  5793. * The only reason for brcms_c_txfifo to fail is because
  5794. * there weren't any DMA descriptors, but we've already
  5795. * checked for that. So if it does fail yell loudly.
  5796. */
  5797. WARN_ON_ONCE(ret);
  5798. return ret;
  5799. }
  5800. bool brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
  5801. struct ieee80211_hw *hw)
  5802. {
  5803. uint fifo;
  5804. struct scb *scb = &wlc->pri_scb;
  5805. fifo = brcms_ac_to_fifo(skb_get_queue_mapping(sdu));
  5806. brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0);
  5807. if (!brcms_c_tx(wlc, sdu))
  5808. return true;
  5809. /* packet discarded */
  5810. dev_kfree_skb_any(sdu);
  5811. return false;
  5812. }
  5813. int
  5814. brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p)
  5815. {
  5816. struct dma_pub *dma = wlc->hw->di[fifo];
  5817. int ret;
  5818. u16 queue;
  5819. ret = dma_txfast(wlc, dma, p);
  5820. if (ret < 0)
  5821. wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
  5822. /*
  5823. * Stop queue if DMA ring is full. Reserve some free descriptors,
  5824. * as we sometimes receive a frame from mac80211 after the queues
  5825. * are stopped.
  5826. */
  5827. queue = skb_get_queue_mapping(p);
  5828. if (dma->txavail <= TX_HEADROOM && fifo < TX_BCMC_FIFO &&
  5829. !ieee80211_queue_stopped(wlc->pub->ieee_hw, queue))
  5830. ieee80211_stop_queue(wlc->pub->ieee_hw, queue);
  5831. return ret;
  5832. }
  5833. u32
  5834. brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
  5835. bool use_rspec, u16 mimo_ctlchbw)
  5836. {
  5837. u32 rts_rspec = 0;
  5838. if (use_rspec)
  5839. /* use frame rate as rts rate */
  5840. rts_rspec = rspec;
  5841. else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
  5842. /* Use 11Mbps as the g protection RTS target rate and fallback.
  5843. * Use the brcms_basic_rate() lookup to find the best basic rate
  5844. * under the target in case 11 Mbps is not Basic.
  5845. * 6 and 9 Mbps are not usually selected by rate selection, but
  5846. * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
  5847. * is more robust.
  5848. */
  5849. rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
  5850. else
  5851. /* calculate RTS rate and fallback rate based on the frame rate
  5852. * RTS must be sent at a basic rate since it is a
  5853. * control frame, sec 9.6 of 802.11 spec
  5854. */
  5855. rts_rspec = brcms_basic_rate(wlc, rspec);
  5856. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  5857. /* set rts txbw to correct side band */
  5858. rts_rspec &= ~RSPEC_BW_MASK;
  5859. /*
  5860. * if rspec/rspec_fallback is 40MHz, then send RTS on both
  5861. * 20MHz channel (DUP), otherwise send RTS on control channel
  5862. */
  5863. if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
  5864. rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
  5865. else
  5866. rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  5867. /* pick siso/cdd as default for ofdm */
  5868. if (is_ofdm_rate(rts_rspec)) {
  5869. rts_rspec &= ~RSPEC_STF_MASK;
  5870. rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
  5871. }
  5872. }
  5873. return rts_rspec;
  5874. }
  5875. /* Update beacon listen interval in shared memory */
  5876. static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
  5877. {
  5878. /* wake up every DTIM is the default */
  5879. if (wlc->bcn_li_dtim == 1)
  5880. brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
  5881. else
  5882. brcms_b_write_shm(wlc->hw, M_BCN_LI,
  5883. (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
  5884. }
  5885. static void
  5886. brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
  5887. u32 *tsf_h_ptr)
  5888. {
  5889. struct bcma_device *core = wlc_hw->d11core;
  5890. /* read the tsf timer low, then high to get an atomic read */
  5891. *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
  5892. *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
  5893. }
  5894. /*
  5895. * recover 64bit TSF value from the 16bit TSF value in the rx header
  5896. * given the assumption that the TSF passed in header is within 65ms
  5897. * of the current tsf.
  5898. *
  5899. * 6 5 4 4 3 2 1
  5900. * 3.......6.......8.......0.......2.......4.......6.......8......0
  5901. * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
  5902. *
  5903. * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
  5904. * tsf_l is filled in by brcms_b_recv, which is done earlier in the
  5905. * receive call sequence after rx interrupt. Only the higher 16 bits
  5906. * are used. Finally, the tsf_h is read from the tsf register.
  5907. */
  5908. static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
  5909. struct d11rxhdr *rxh)
  5910. {
  5911. u32 tsf_h, tsf_l;
  5912. u16 rx_tsf_0_15, rx_tsf_16_31;
  5913. brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
  5914. rx_tsf_16_31 = (u16)(tsf_l >> 16);
  5915. rx_tsf_0_15 = rxh->RxTSFTime;
  5916. /*
  5917. * a greater tsf time indicates the low 16 bits of
  5918. * tsf_l wrapped, so decrement the high 16 bits.
  5919. */
  5920. if ((u16)tsf_l < rx_tsf_0_15) {
  5921. rx_tsf_16_31 -= 1;
  5922. if (rx_tsf_16_31 == 0xffff)
  5923. tsf_h -= 1;
  5924. }
  5925. return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
  5926. }
  5927. static void
  5928. prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  5929. struct sk_buff *p,
  5930. struct ieee80211_rx_status *rx_status)
  5931. {
  5932. int preamble;
  5933. int channel;
  5934. u32 rspec;
  5935. unsigned char *plcp;
  5936. /* fill in TSF and flag its presence */
  5937. rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
  5938. rx_status->flag |= RX_FLAG_MACTIME_START;
  5939. channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
  5940. rx_status->band =
  5941. channel > 14 ? IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
  5942. rx_status->freq =
  5943. ieee80211_channel_to_frequency(channel, rx_status->band);
  5944. rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
  5945. /* noise */
  5946. /* qual */
  5947. rx_status->antenna =
  5948. (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
  5949. plcp = p->data;
  5950. rspec = brcms_c_compute_rspec(rxh, plcp);
  5951. if (is_mcs_rate(rspec)) {
  5952. rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
  5953. rx_status->flag |= RX_FLAG_HT;
  5954. if (rspec_is40mhz(rspec))
  5955. rx_status->flag |= RX_FLAG_40MHZ;
  5956. } else {
  5957. switch (rspec2rate(rspec)) {
  5958. case BRCM_RATE_1M:
  5959. rx_status->rate_idx = 0;
  5960. break;
  5961. case BRCM_RATE_2M:
  5962. rx_status->rate_idx = 1;
  5963. break;
  5964. case BRCM_RATE_5M5:
  5965. rx_status->rate_idx = 2;
  5966. break;
  5967. case BRCM_RATE_11M:
  5968. rx_status->rate_idx = 3;
  5969. break;
  5970. case BRCM_RATE_6M:
  5971. rx_status->rate_idx = 4;
  5972. break;
  5973. case BRCM_RATE_9M:
  5974. rx_status->rate_idx = 5;
  5975. break;
  5976. case BRCM_RATE_12M:
  5977. rx_status->rate_idx = 6;
  5978. break;
  5979. case BRCM_RATE_18M:
  5980. rx_status->rate_idx = 7;
  5981. break;
  5982. case BRCM_RATE_24M:
  5983. rx_status->rate_idx = 8;
  5984. break;
  5985. case BRCM_RATE_36M:
  5986. rx_status->rate_idx = 9;
  5987. break;
  5988. case BRCM_RATE_48M:
  5989. rx_status->rate_idx = 10;
  5990. break;
  5991. case BRCM_RATE_54M:
  5992. rx_status->rate_idx = 11;
  5993. break;
  5994. default:
  5995. brcms_err(wlc->hw->d11core,
  5996. "%s: Unknown rate\n", __func__);
  5997. }
  5998. /*
  5999. * For 5GHz, we should decrease the index as it is
  6000. * a subset of the 2.4G rates. See bitrates field
  6001. * of brcms_band_5GHz_nphy (in mac80211_if.c).
  6002. */
  6003. if (rx_status->band == IEEE80211_BAND_5GHZ)
  6004. rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
  6005. /* Determine short preamble and rate_idx */
  6006. preamble = 0;
  6007. if (is_cck_rate(rspec)) {
  6008. if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
  6009. rx_status->flag |= RX_FLAG_SHORTPRE;
  6010. } else if (is_ofdm_rate(rspec)) {
  6011. rx_status->flag |= RX_FLAG_SHORTPRE;
  6012. } else {
  6013. brcms_err(wlc->hw->d11core, "%s: Unknown modulation\n",
  6014. __func__);
  6015. }
  6016. }
  6017. if (plcp3_issgi(plcp[3]))
  6018. rx_status->flag |= RX_FLAG_SHORT_GI;
  6019. if (rxh->RxStatus1 & RXS_DECERR) {
  6020. rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  6021. brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
  6022. __func__);
  6023. }
  6024. if (rxh->RxStatus1 & RXS_FCSERR) {
  6025. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  6026. brcms_err(wlc->hw->d11core, "%s: RX_FLAG_FAILED_FCS_CRC\n",
  6027. __func__);
  6028. }
  6029. }
  6030. static void
  6031. brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  6032. struct sk_buff *p)
  6033. {
  6034. int len_mpdu;
  6035. struct ieee80211_rx_status rx_status;
  6036. struct ieee80211_hdr *hdr;
  6037. memset(&rx_status, 0, sizeof(rx_status));
  6038. prep_mac80211_status(wlc, rxh, p, &rx_status);
  6039. /* mac header+body length, exclude CRC and plcp header */
  6040. len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
  6041. skb_pull(p, D11_PHY_HDR_LEN);
  6042. __skb_trim(p, len_mpdu);
  6043. /* unmute transmit */
  6044. if (wlc->hw->suspended_fifos) {
  6045. hdr = (struct ieee80211_hdr *)p->data;
  6046. if (ieee80211_is_beacon(hdr->frame_control))
  6047. brcms_b_mute(wlc->hw, false);
  6048. }
  6049. memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
  6050. ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
  6051. }
  6052. /* calculate frame duration for Mixed-mode L-SIG spoofing, return
  6053. * number of bytes goes in the length field
  6054. *
  6055. * Formula given by HT PHY Spec v 1.13
  6056. * len = 3(nsyms + nstream + 3) - 3
  6057. */
  6058. u16
  6059. brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
  6060. uint mac_len)
  6061. {
  6062. uint nsyms, len = 0, kNdps;
  6063. if (is_mcs_rate(ratespec)) {
  6064. uint mcs = ratespec & RSPEC_RATE_MASK;
  6065. int tot_streams = (mcs_2_txstreams(mcs) + 1) +
  6066. rspec_stc(ratespec);
  6067. /*
  6068. * the payload duration calculation matches that
  6069. * of regular ofdm
  6070. */
  6071. /* 1000Ndbps = kbps * 4 */
  6072. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  6073. rspec_issgi(ratespec)) * 4;
  6074. if (rspec_stc(ratespec) == 0)
  6075. nsyms =
  6076. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6077. APHY_TAIL_NBITS) * 1000, kNdps);
  6078. else
  6079. /* STBC needs to have even number of symbols */
  6080. nsyms =
  6081. 2 *
  6082. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6083. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  6084. /* (+3) account for HT-SIG(2) and HT-STF(1) */
  6085. nsyms += (tot_streams + 3);
  6086. /*
  6087. * 3 bytes/symbol @ legacy 6Mbps rate
  6088. * (-3) excluding service bits and tail bits
  6089. */
  6090. len = (3 * nsyms) - 3;
  6091. }
  6092. return (u16) len;
  6093. }
  6094. static void
  6095. brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
  6096. {
  6097. const struct brcms_c_rateset *rs_dflt;
  6098. struct brcms_c_rateset rs;
  6099. u8 rate;
  6100. u16 entry_ptr;
  6101. u8 plcp[D11_PHY_HDR_LEN];
  6102. u16 dur, sifs;
  6103. uint i;
  6104. sifs = get_sifs(wlc->band);
  6105. rs_dflt = brcms_c_rateset_get_hwrs(wlc);
  6106. brcms_c_rateset_copy(rs_dflt, &rs);
  6107. brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
  6108. /*
  6109. * walk the phy rate table and update MAC core SHM
  6110. * basic rate table entries
  6111. */
  6112. for (i = 0; i < rs.count; i++) {
  6113. rate = rs.rates[i] & BRCMS_RATE_MASK;
  6114. entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
  6115. /* Calculate the Probe Response PLCP for the given rate */
  6116. brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
  6117. /*
  6118. * Calculate the duration of the Probe Response
  6119. * frame plus SIFS for the MAC
  6120. */
  6121. dur = (u16) brcms_c_calc_frame_time(wlc, rate,
  6122. BRCMS_LONG_PREAMBLE, frame_len);
  6123. dur += sifs;
  6124. /* Update the SHM Rate Table entry Probe Response values */
  6125. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
  6126. (u16) (plcp[0] + (plcp[1] << 8)));
  6127. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
  6128. (u16) (plcp[2] + (plcp[3] << 8)));
  6129. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
  6130. }
  6131. }
  6132. /* Max buffering needed for beacon template/prb resp template is 142 bytes.
  6133. *
  6134. * PLCP header is 6 bytes.
  6135. * 802.11 A3 header is 24 bytes.
  6136. * Max beacon frame body template length is 112 bytes.
  6137. * Max probe resp frame body template length is 110 bytes.
  6138. *
  6139. * *len on input contains the max length of the packet available.
  6140. *
  6141. * The *len value is set to the number of bytes in buf used, and starts
  6142. * with the PLCP and included up to, but not including, the 4 byte FCS.
  6143. */
  6144. static void
  6145. brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
  6146. u32 bcn_rspec,
  6147. struct brcms_bss_cfg *cfg, u16 *buf, int *len)
  6148. {
  6149. static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
  6150. struct cck_phy_hdr *plcp;
  6151. struct ieee80211_mgmt *h;
  6152. int hdr_len, body_len;
  6153. hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
  6154. /* calc buffer size provided for frame body */
  6155. body_len = *len - hdr_len;
  6156. /* return actual size */
  6157. *len = hdr_len + body_len;
  6158. /* format PHY and MAC headers */
  6159. memset(buf, 0, hdr_len);
  6160. plcp = (struct cck_phy_hdr *) buf;
  6161. /*
  6162. * PLCP for Probe Response frames are filled in from
  6163. * core's rate table
  6164. */
  6165. if (type == IEEE80211_STYPE_BEACON)
  6166. /* fill in PLCP */
  6167. brcms_c_compute_plcp(wlc, bcn_rspec,
  6168. (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
  6169. (u8 *) plcp);
  6170. /* "Regular" and 16 MBSS but not for 4 MBSS */
  6171. /* Update the phytxctl for the beacon based on the rspec */
  6172. brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
  6173. h = (struct ieee80211_mgmt *)&plcp[1];
  6174. /* fill in 802.11 header */
  6175. h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
  6176. /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
  6177. /* A1 filled in by MAC for prb resp, broadcast for bcn */
  6178. if (type == IEEE80211_STYPE_BEACON)
  6179. memcpy(&h->da, &ether_bcast, ETH_ALEN);
  6180. memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
  6181. memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
  6182. /* SEQ filled in by MAC */
  6183. }
  6184. int brcms_c_get_header_len(void)
  6185. {
  6186. return TXOFF;
  6187. }
  6188. /*
  6189. * Update all beacons for the system.
  6190. */
  6191. void brcms_c_update_beacon(struct brcms_c_info *wlc)
  6192. {
  6193. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6194. if (bsscfg->up && !bsscfg->BSS)
  6195. /* Clear the soft intmask */
  6196. wlc->defmacintmask &= ~MI_BCNTPL;
  6197. }
  6198. /* Write ssid into shared memory */
  6199. static void
  6200. brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
  6201. {
  6202. u8 *ssidptr = cfg->SSID;
  6203. u16 base = M_SSID;
  6204. u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
  6205. /* padding the ssid with zero and copy it into shm */
  6206. memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
  6207. memcpy(ssidbuf, ssidptr, cfg->SSID_len);
  6208. brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
  6209. brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
  6210. }
  6211. static void
  6212. brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
  6213. struct brcms_bss_cfg *cfg,
  6214. bool suspend)
  6215. {
  6216. u16 *prb_resp;
  6217. int len = BCN_TMPL_LEN;
  6218. prb_resp = kmalloc(BCN_TMPL_LEN, GFP_ATOMIC);
  6219. if (!prb_resp)
  6220. return;
  6221. /*
  6222. * write the probe response to hardware, or save in
  6223. * the config structure
  6224. */
  6225. /* create the probe response template */
  6226. brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
  6227. cfg, prb_resp, &len);
  6228. if (suspend)
  6229. brcms_c_suspend_mac_and_wait(wlc);
  6230. /* write the probe response into the template region */
  6231. brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
  6232. (len + 3) & ~3, prb_resp);
  6233. /* write the length of the probe response frame (+PLCP/-FCS) */
  6234. brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
  6235. /* write the SSID and SSID length */
  6236. brcms_c_shm_ssid_upd(wlc, cfg);
  6237. /*
  6238. * Write PLCP headers and durations for probe response frames
  6239. * at all rates. Use the actual frame length covered by the
  6240. * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
  6241. * by subtracting the PLCP len and adding the FCS.
  6242. */
  6243. len += (-D11_PHY_HDR_LEN + FCS_LEN);
  6244. brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
  6245. if (suspend)
  6246. brcms_c_enable_mac(wlc);
  6247. kfree(prb_resp);
  6248. }
  6249. void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
  6250. {
  6251. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6252. /* update AP or IBSS probe responses */
  6253. if (bsscfg->up && !bsscfg->BSS)
  6254. brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
  6255. }
  6256. int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
  6257. uint *blocks)
  6258. {
  6259. if (fifo >= NFIFO)
  6260. return -EINVAL;
  6261. *blocks = wlc_hw->xmtfifo_sz[fifo];
  6262. return 0;
  6263. }
  6264. void
  6265. brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
  6266. const u8 *addr)
  6267. {
  6268. brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
  6269. if (match_reg_offset == RCM_BSSID_OFFSET)
  6270. memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
  6271. }
  6272. /*
  6273. * Flag 'scan in progress' to withhold dynamic phy calibration
  6274. */
  6275. void brcms_c_scan_start(struct brcms_c_info *wlc)
  6276. {
  6277. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
  6278. }
  6279. void brcms_c_scan_stop(struct brcms_c_info *wlc)
  6280. {
  6281. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
  6282. }
  6283. void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
  6284. {
  6285. wlc->pub->associated = state;
  6286. wlc->bsscfg->associated = state;
  6287. }
  6288. /*
  6289. * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
  6290. * AMPDU traffic, packets pending in hardware have to be invalidated so that
  6291. * when later on hardware releases them, they can be handled appropriately.
  6292. */
  6293. void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
  6294. struct ieee80211_sta *sta,
  6295. void (*dma_callback_fn))
  6296. {
  6297. struct dma_pub *dmah;
  6298. int i;
  6299. for (i = 0; i < NFIFO; i++) {
  6300. dmah = hw->di[i];
  6301. if (dmah != NULL)
  6302. dma_walk_packets(dmah, dma_callback_fn, sta);
  6303. }
  6304. }
  6305. int brcms_c_get_curband(struct brcms_c_info *wlc)
  6306. {
  6307. return wlc->band->bandunit;
  6308. }
  6309. bool brcms_c_tx_flush_completed(struct brcms_c_info *wlc)
  6310. {
  6311. int i;
  6312. /* Kick DMA to send any pending AMPDU */
  6313. for (i = 0; i < ARRAY_SIZE(wlc->hw->di); i++)
  6314. if (wlc->hw->di[i])
  6315. dma_kick_tx(wlc->hw->di[i]);
  6316. return !brcms_txpktpendtot(wlc);
  6317. }
  6318. void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
  6319. {
  6320. wlc->bcn_li_bcn = interval;
  6321. if (wlc->pub->up)
  6322. brcms_c_bcn_li_upd(wlc);
  6323. }
  6324. int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
  6325. {
  6326. uint qdbm;
  6327. /* Remove override bit and clip to max qdbm value */
  6328. qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
  6329. return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
  6330. }
  6331. int brcms_c_get_tx_power(struct brcms_c_info *wlc)
  6332. {
  6333. uint qdbm;
  6334. bool override;
  6335. wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
  6336. /* Return qdbm units */
  6337. return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
  6338. }
  6339. /* Process received frames */
  6340. /*
  6341. * Return true if more frames need to be processed. false otherwise.
  6342. * Param 'bound' indicates max. # frames to process before break out.
  6343. */
  6344. static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
  6345. {
  6346. struct d11rxhdr *rxh;
  6347. struct ieee80211_hdr *h;
  6348. uint len;
  6349. bool is_amsdu;
  6350. /* frame starts with rxhdr */
  6351. rxh = (struct d11rxhdr *) (p->data);
  6352. /* strip off rxhdr */
  6353. skb_pull(p, BRCMS_HWRXOFF);
  6354. /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
  6355. if (rxh->RxStatus1 & RXS_PBPRES) {
  6356. if (p->len < 2) {
  6357. brcms_err(wlc->hw->d11core,
  6358. "wl%d: recv: rcvd runt of len %d\n",
  6359. wlc->pub->unit, p->len);
  6360. goto toss;
  6361. }
  6362. skb_pull(p, 2);
  6363. }
  6364. h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
  6365. len = p->len;
  6366. if (rxh->RxStatus1 & RXS_FCSERR) {
  6367. if (!(wlc->filter_flags & FIF_FCSFAIL))
  6368. goto toss;
  6369. }
  6370. /* check received pkt has at least frame control field */
  6371. if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
  6372. goto toss;
  6373. /* not supporting A-MSDU */
  6374. is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
  6375. if (is_amsdu)
  6376. goto toss;
  6377. brcms_c_recvctl(wlc, rxh, p);
  6378. return;
  6379. toss:
  6380. brcmu_pkt_buf_free_skb(p);
  6381. }
  6382. /* Process received frames */
  6383. /*
  6384. * Return true if more frames need to be processed. false otherwise.
  6385. * Param 'bound' indicates max. # frames to process before break out.
  6386. */
  6387. static bool
  6388. brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
  6389. {
  6390. struct sk_buff *p;
  6391. struct sk_buff *next = NULL;
  6392. struct sk_buff_head recv_frames;
  6393. uint n = 0;
  6394. uint bound_limit = bound ? RXBND : -1;
  6395. bool morepending = false;
  6396. skb_queue_head_init(&recv_frames);
  6397. /* gather received frames */
  6398. do {
  6399. /* !give others some time to run! */
  6400. if (n >= bound_limit)
  6401. break;
  6402. morepending = dma_rx(wlc_hw->di[fifo], &recv_frames);
  6403. n++;
  6404. } while (morepending);
  6405. /* post more rbufs */
  6406. dma_rxfill(wlc_hw->di[fifo]);
  6407. /* process each frame */
  6408. skb_queue_walk_safe(&recv_frames, p, next) {
  6409. struct d11rxhdr_le *rxh_le;
  6410. struct d11rxhdr *rxh;
  6411. skb_unlink(p, &recv_frames);
  6412. rxh_le = (struct d11rxhdr_le *)p->data;
  6413. rxh = (struct d11rxhdr *)p->data;
  6414. /* fixup rx header endianness */
  6415. rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
  6416. rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
  6417. rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
  6418. rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
  6419. rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
  6420. rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
  6421. rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
  6422. rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
  6423. rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
  6424. rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
  6425. rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
  6426. brcms_c_recv(wlc_hw->wlc, p);
  6427. }
  6428. return morepending;
  6429. }
  6430. /* second-level interrupt processing
  6431. * Return true if another dpc needs to be re-scheduled. false otherwise.
  6432. * Param 'bounded' indicates if applicable loops should be bounded.
  6433. */
  6434. bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
  6435. {
  6436. u32 macintstatus;
  6437. struct brcms_hardware *wlc_hw = wlc->hw;
  6438. struct bcma_device *core = wlc_hw->d11core;
  6439. if (brcms_deviceremoved(wlc)) {
  6440. brcms_err(core, "wl%d: %s: dead chip\n", wlc_hw->unit,
  6441. __func__);
  6442. brcms_down(wlc->wl);
  6443. return false;
  6444. }
  6445. /* grab and clear the saved software intstatus bits */
  6446. macintstatus = wlc->macintstatus;
  6447. wlc->macintstatus = 0;
  6448. brcms_dbg_int(core, "wl%d: macintstatus 0x%x\n",
  6449. wlc_hw->unit, macintstatus);
  6450. WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
  6451. /* tx status */
  6452. if (macintstatus & MI_TFS) {
  6453. bool fatal;
  6454. if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
  6455. wlc->macintstatus |= MI_TFS;
  6456. if (fatal) {
  6457. brcms_err(core, "MI_TFS: fatal\n");
  6458. goto fatal;
  6459. }
  6460. }
  6461. if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
  6462. brcms_c_tbtt(wlc);
  6463. /* ATIM window end */
  6464. if (macintstatus & MI_ATIMWINEND) {
  6465. brcms_dbg_info(core, "end of ATIM window\n");
  6466. bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
  6467. wlc->qvalid = 0;
  6468. }
  6469. /*
  6470. * received data or control frame, MI_DMAINT is
  6471. * indication of RX_FIFO interrupt
  6472. */
  6473. if (macintstatus & MI_DMAINT)
  6474. if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
  6475. wlc->macintstatus |= MI_DMAINT;
  6476. /* noise sample collected */
  6477. if (macintstatus & MI_BG_NOISE)
  6478. wlc_phy_noise_sample_intr(wlc_hw->band->pi);
  6479. if (macintstatus & MI_GP0) {
  6480. brcms_err(core, "wl%d: PSM microcode watchdog fired at %d "
  6481. "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
  6482. printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
  6483. __func__, ai_get_chip_id(wlc_hw->sih),
  6484. ai_get_chiprev(wlc_hw->sih));
  6485. brcms_fatal_error(wlc_hw->wlc->wl);
  6486. }
  6487. /* gptimer timeout */
  6488. if (macintstatus & MI_TO)
  6489. bcma_write32(core, D11REGOFFS(gptimer), 0);
  6490. if (macintstatus & MI_RFDISABLE) {
  6491. brcms_dbg_info(core, "wl%d: BMAC Detected a change on the"
  6492. " RF Disable Input\n", wlc_hw->unit);
  6493. brcms_rfkill_set_hw_state(wlc->wl);
  6494. }
  6495. /* it isn't done and needs to be resched if macintstatus is non-zero */
  6496. return wlc->macintstatus != 0;
  6497. fatal:
  6498. brcms_fatal_error(wlc_hw->wlc->wl);
  6499. return wlc->macintstatus != 0;
  6500. }
  6501. void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
  6502. {
  6503. struct bcma_device *core = wlc->hw->d11core;
  6504. struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
  6505. u16 chanspec;
  6506. brcms_dbg_info(core, "wl%d\n", wlc->pub->unit);
  6507. chanspec = ch20mhz_chspec(ch->hw_value);
  6508. brcms_b_init(wlc->hw, chanspec);
  6509. /* update beacon listen interval */
  6510. brcms_c_bcn_li_upd(wlc);
  6511. /* write ethernet address to core */
  6512. brcms_c_set_mac(wlc->bsscfg);
  6513. brcms_c_set_bssid(wlc->bsscfg);
  6514. /* Update tsf_cfprep if associated and up */
  6515. if (wlc->pub->associated && wlc->bsscfg->up) {
  6516. u32 bi;
  6517. /* get beacon period and convert to uS */
  6518. bi = wlc->bsscfg->current_bss->beacon_period << 10;
  6519. /*
  6520. * update since init path would reset
  6521. * to default value
  6522. */
  6523. bcma_write32(core, D11REGOFFS(tsf_cfprep),
  6524. bi << CFPREP_CBI_SHIFT);
  6525. /* Update maccontrol PM related bits */
  6526. brcms_c_set_ps_ctrl(wlc);
  6527. }
  6528. brcms_c_bandinit_ordered(wlc, chanspec);
  6529. /* init probe response timeout */
  6530. brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
  6531. /* init max burst txop (framebursting) */
  6532. brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
  6533. (wlc->
  6534. _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
  6535. /* initialize maximum allowed duty cycle */
  6536. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
  6537. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
  6538. /*
  6539. * Update some shared memory locations related to
  6540. * max AMPDU size allowed to received
  6541. */
  6542. brcms_c_ampdu_shm_upd(wlc->ampdu);
  6543. /* band-specific inits */
  6544. brcms_c_bsinit(wlc);
  6545. /* Enable EDCF mode (while the MAC is suspended) */
  6546. bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
  6547. brcms_c_edcf_setparams(wlc, false);
  6548. /* read the ucode version if we have not yet done so */
  6549. if (wlc->ucode_rev == 0) {
  6550. wlc->ucode_rev =
  6551. brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
  6552. wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
  6553. }
  6554. /* ..now really unleash hell (allow the MAC out of suspend) */
  6555. brcms_c_enable_mac(wlc);
  6556. /* suspend the tx fifos and mute the phy for preism cac time */
  6557. if (mute_tx)
  6558. brcms_b_mute(wlc->hw, true);
  6559. /* enable the RF Disable Delay timer */
  6560. bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
  6561. /*
  6562. * Initialize WME parameters; if they haven't been set by some other
  6563. * mechanism (IOVar, etc) then read them from the hardware.
  6564. */
  6565. if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
  6566. /* Uninitialized; read from HW */
  6567. int ac;
  6568. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  6569. wlc->wme_retries[ac] =
  6570. brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
  6571. }
  6572. }
  6573. /*
  6574. * The common driver entry routine. Error codes should be unique
  6575. */
  6576. struct brcms_c_info *
  6577. brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
  6578. bool piomode, uint *perr)
  6579. {
  6580. struct brcms_c_info *wlc;
  6581. uint err = 0;
  6582. uint i, j;
  6583. struct brcms_pub *pub;
  6584. /* allocate struct brcms_c_info state and its substructures */
  6585. wlc = brcms_c_attach_malloc(unit, &err, 0);
  6586. if (wlc == NULL)
  6587. goto fail;
  6588. wlc->wiphy = wl->wiphy;
  6589. pub = wlc->pub;
  6590. #if defined(DEBUG)
  6591. wlc_info_dbg = wlc;
  6592. #endif
  6593. wlc->band = wlc->bandstate[0];
  6594. wlc->core = wlc->corestate;
  6595. wlc->wl = wl;
  6596. pub->unit = unit;
  6597. pub->_piomode = piomode;
  6598. wlc->bandinit_pending = false;
  6599. /* populate struct brcms_c_info with default values */
  6600. brcms_c_info_init(wlc, unit);
  6601. /* update sta/ap related parameters */
  6602. brcms_c_ap_upd(wlc);
  6603. /*
  6604. * low level attach steps(all hw accesses go
  6605. * inside, no more in rest of the attach)
  6606. */
  6607. err = brcms_b_attach(wlc, core, unit, piomode);
  6608. if (err)
  6609. goto fail;
  6610. brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
  6611. pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
  6612. /* disable allowed duty cycle */
  6613. wlc->tx_duty_cycle_ofdm = 0;
  6614. wlc->tx_duty_cycle_cck = 0;
  6615. brcms_c_stf_phy_chain_calc(wlc);
  6616. /* txchain 1: txant 0, txchain 2: txant 1 */
  6617. if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
  6618. wlc->stf->txant = wlc->stf->hw_txchain - 1;
  6619. /* push to BMAC driver */
  6620. wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
  6621. wlc->stf->hw_rxchain);
  6622. /* pull up some info resulting from the low attach */
  6623. for (i = 0; i < NFIFO; i++)
  6624. wlc->core->txavail[i] = wlc->hw->txavail[i];
  6625. memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  6626. memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  6627. for (j = 0; j < wlc->pub->_nbands; j++) {
  6628. wlc->band = wlc->bandstate[j];
  6629. if (!brcms_c_attach_stf_ant_init(wlc)) {
  6630. err = 24;
  6631. goto fail;
  6632. }
  6633. /* default contention windows size limits */
  6634. wlc->band->CWmin = APHY_CWMIN;
  6635. wlc->band->CWmax = PHY_CWMAX;
  6636. /* init gmode value */
  6637. if (wlc->band->bandtype == BRCM_BAND_2G) {
  6638. wlc->band->gmode = GMODE_AUTO;
  6639. brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
  6640. wlc->band->gmode);
  6641. }
  6642. /* init _n_enab supported mode */
  6643. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  6644. pub->_n_enab = SUPPORT_11N;
  6645. brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
  6646. ((pub->_n_enab ==
  6647. SUPPORT_11N) ? WL_11N_2x2 :
  6648. WL_11N_3x3));
  6649. }
  6650. /* init per-band default rateset, depend on band->gmode */
  6651. brcms_default_rateset(wlc, &wlc->band->defrateset);
  6652. /* fill in hw_rateset */
  6653. brcms_c_rateset_filter(&wlc->band->defrateset,
  6654. &wlc->band->hw_rateset, false,
  6655. BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
  6656. (bool) (wlc->pub->_n_enab & SUPPORT_11N));
  6657. }
  6658. /*
  6659. * update antenna config due to
  6660. * wlc->stf->txant/txchain/ant_rx_ovr change
  6661. */
  6662. brcms_c_stf_phy_txant_upd(wlc);
  6663. /* attach each modules */
  6664. err = brcms_c_attach_module(wlc);
  6665. if (err != 0)
  6666. goto fail;
  6667. if (!brcms_c_timers_init(wlc, unit)) {
  6668. wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
  6669. __func__);
  6670. err = 32;
  6671. goto fail;
  6672. }
  6673. /* depend on rateset, gmode */
  6674. wlc->cmi = brcms_c_channel_mgr_attach(wlc);
  6675. if (!wlc->cmi) {
  6676. wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
  6677. "\n", unit, __func__);
  6678. err = 33;
  6679. goto fail;
  6680. }
  6681. /* init default when all parameters are ready, i.e. ->rateset */
  6682. brcms_c_bss_default_init(wlc);
  6683. /*
  6684. * Complete the wlc default state initializations..
  6685. */
  6686. wlc->bsscfg->wlc = wlc;
  6687. wlc->mimoft = FT_HT;
  6688. wlc->mimo_40txbw = AUTO;
  6689. wlc->ofdm_40txbw = AUTO;
  6690. wlc->cck_40txbw = AUTO;
  6691. brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
  6692. /* Set default values of SGI */
  6693. if (BRCMS_SGI_CAP_PHY(wlc)) {
  6694. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  6695. BRCMS_N_SGI_40));
  6696. } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
  6697. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  6698. BRCMS_N_SGI_40));
  6699. } else {
  6700. brcms_c_ht_update_sgi_rx(wlc, 0);
  6701. }
  6702. brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
  6703. if (perr)
  6704. *perr = 0;
  6705. return wlc;
  6706. fail:
  6707. wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
  6708. unit, __func__, err);
  6709. if (wlc)
  6710. brcms_c_detach(wlc);
  6711. if (perr)
  6712. *perr = err;
  6713. return NULL;
  6714. }