main.c 215 KB

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