main.c 224 KB

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