main.c 223 KB

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