main.c 223 KB

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