main.c 211 KB

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