main.c 224 KB

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