main.c 224 KB

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