main.c 210 KB

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