main.c 224 KB

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