main.c 228 KB

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