main.c 210 KB

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