main.c 214 KB

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