main.c 225 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523
  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. #include <linux/pci_ids.h>
  17. #include <linux/if_ether.h>
  18. #include <net/mac80211.h>
  19. #include <brcm_hw_ids.h>
  20. #include <aiutils.h>
  21. #include <chipcommon.h>
  22. #include "rate.h"
  23. #include "scb.h"
  24. #include "phy/phy_hal.h"
  25. #include "channel.h"
  26. #include "antsel.h"
  27. #include "stf.h"
  28. #include "ampdu.h"
  29. #include "mac80211_if.h"
  30. #include "ucode_loader.h"
  31. #include "main.h"
  32. #include "soc.h"
  33. /*
  34. * Indication for txflowcontrol that all priority bits in
  35. * TXQ_STOP_FOR_PRIOFC_MASK are to be considered.
  36. */
  37. #define ALLPRIO -1
  38. /* watchdog timer, in unit of ms */
  39. #define TIMER_INTERVAL_WATCHDOG 1000
  40. /* radio monitor timer, in unit of ms */
  41. #define TIMER_INTERVAL_RADIOCHK 800
  42. /* beacon interval, in unit of 1024TU */
  43. #define BEACON_INTERVAL_DEFAULT 100
  44. /* n-mode support capability */
  45. /* 2x2 includes both 1x1 & 2x2 devices
  46. * reserved #define 2 for future when we want to separate 1x1 & 2x2 and
  47. * control it independently
  48. */
  49. #define WL_11N_2x2 1
  50. #define WL_11N_3x3 3
  51. #define WL_11N_4x4 4
  52. #define EDCF_ACI_MASK 0x60
  53. #define EDCF_ACI_SHIFT 5
  54. #define EDCF_ECWMIN_MASK 0x0f
  55. #define EDCF_ECWMAX_SHIFT 4
  56. #define EDCF_AIFSN_MASK 0x0f
  57. #define EDCF_AIFSN_MAX 15
  58. #define EDCF_ECWMAX_MASK 0xf0
  59. #define EDCF_AC_BE_TXOP_STA 0x0000
  60. #define EDCF_AC_BK_TXOP_STA 0x0000
  61. #define EDCF_AC_VO_ACI_STA 0x62
  62. #define EDCF_AC_VO_ECW_STA 0x32
  63. #define EDCF_AC_VI_ACI_STA 0x42
  64. #define EDCF_AC_VI_ECW_STA 0x43
  65. #define EDCF_AC_BK_ECW_STA 0xA4
  66. #define EDCF_AC_VI_TXOP_STA 0x005e
  67. #define EDCF_AC_VO_TXOP_STA 0x002f
  68. #define EDCF_AC_BE_ACI_STA 0x03
  69. #define EDCF_AC_BE_ECW_STA 0xA4
  70. #define EDCF_AC_BK_ACI_STA 0x27
  71. #define EDCF_AC_VO_TXOP_AP 0x002f
  72. #define EDCF_TXOP2USEC(txop) ((txop) << 5)
  73. #define EDCF_ECW2CW(exp) ((1 << (exp)) - 1)
  74. #define APHY_SYMBOL_TIME 4
  75. #define APHY_PREAMBLE_TIME 16
  76. #define APHY_SIGNAL_TIME 4
  77. #define APHY_SIFS_TIME 16
  78. #define APHY_SERVICE_NBITS 16
  79. #define APHY_TAIL_NBITS 6
  80. #define BPHY_SIFS_TIME 10
  81. #define BPHY_PLCP_SHORT_TIME 96
  82. #define PREN_PREAMBLE 24
  83. #define PREN_MM_EXT 12
  84. #define PREN_PREAMBLE_EXT 4
  85. #define DOT11_MAC_HDR_LEN 24
  86. #define DOT11_ACK_LEN 10
  87. #define DOT11_BA_LEN 4
  88. #define DOT11_OFDM_SIGNAL_EXTENSION 6
  89. #define DOT11_MIN_FRAG_LEN 256
  90. #define DOT11_RTS_LEN 16
  91. #define DOT11_CTS_LEN 10
  92. #define DOT11_BA_BITMAP_LEN 128
  93. #define DOT11_MIN_BEACON_PERIOD 1
  94. #define DOT11_MAX_BEACON_PERIOD 0xFFFF
  95. #define DOT11_MAXNUMFRAGS 16
  96. #define DOT11_MAX_FRAG_LEN 2346
  97. #define BPHY_PLCP_TIME 192
  98. #define RIFS_11N_TIME 2
  99. /* length of the BCN template area */
  100. #define BCN_TMPL_LEN 512
  101. /* brcms_bss_info flag bit values */
  102. #define BRCMS_BSS_HT 0x0020 /* BSS is HT (MIMO) capable */
  103. /* chip rx buffer offset */
  104. #define BRCMS_HWRXOFF 38
  105. /* rfdisable delay timer 500 ms, runs of ALP clock */
  106. #define RFDISABLE_DEFAULT 10000000
  107. #define BRCMS_TEMPSENSE_PERIOD 10 /* 10 second timeout */
  108. /* precedences numbers for wlc queues. These are twice as may levels as
  109. * 802.1D priorities.
  110. * Odd numbers are used for HI priority traffic at same precedence levels
  111. * These constants are used ONLY by wlc_prio2prec_map. Do not use them
  112. * elsewhere.
  113. */
  114. #define _BRCMS_PREC_NONE 0 /* None = - */
  115. #define _BRCMS_PREC_BK 2 /* BK - Background */
  116. #define _BRCMS_PREC_BE 4 /* BE - Best-effort */
  117. #define _BRCMS_PREC_EE 6 /* EE - Excellent-effort */
  118. #define _BRCMS_PREC_CL 8 /* CL - Controlled Load */
  119. #define _BRCMS_PREC_VI 10 /* Vi - Video */
  120. #define _BRCMS_PREC_VO 12 /* Vo - Voice */
  121. #define _BRCMS_PREC_NC 14 /* NC - Network Control */
  122. /* synthpu_dly times in us */
  123. #define SYNTHPU_DLY_APHY_US 3700
  124. #define SYNTHPU_DLY_BPHY_US 1050
  125. #define SYNTHPU_DLY_NPHY_US 2048
  126. #define SYNTHPU_DLY_LPPHY_US 300
  127. #define ANTCNT 10 /* vanilla M_MAX_ANTCNT val */
  128. /* Per-AC retry limit register definitions; uses defs.h bitfield macros */
  129. #define EDCF_SHORT_S 0
  130. #define EDCF_SFB_S 4
  131. #define EDCF_LONG_S 8
  132. #define EDCF_LFB_S 12
  133. #define EDCF_SHORT_M BITFIELD_MASK(4)
  134. #define EDCF_SFB_M BITFIELD_MASK(4)
  135. #define EDCF_LONG_M BITFIELD_MASK(4)
  136. #define EDCF_LFB_M BITFIELD_MASK(4)
  137. #define RETRY_SHORT_DEF 7 /* Default Short retry Limit */
  138. #define RETRY_SHORT_MAX 255 /* Maximum Short retry Limit */
  139. #define RETRY_LONG_DEF 4 /* Default Long retry count */
  140. #define RETRY_SHORT_FB 3 /* Short count for fb rate */
  141. #define RETRY_LONG_FB 2 /* Long count for fb rate */
  142. #define APHY_CWMIN 15
  143. #define PHY_CWMAX 1023
  144. #define EDCF_AIFSN_MIN 1
  145. #define FRAGNUM_MASK 0xF
  146. #define APHY_SLOT_TIME 9
  147. #define BPHY_SLOT_TIME 20
  148. #define WL_SPURAVOID_OFF 0
  149. #define WL_SPURAVOID_ON1 1
  150. #define WL_SPURAVOID_ON2 2
  151. /* invalid core flags, use the saved coreflags */
  152. #define BRCMS_USE_COREFLAGS 0xffffffff
  153. /* values for PLCPHdr_override */
  154. #define BRCMS_PLCP_AUTO -1
  155. #define BRCMS_PLCP_SHORT 0
  156. #define BRCMS_PLCP_LONG 1
  157. /* values for g_protection_override and n_protection_override */
  158. #define BRCMS_PROTECTION_AUTO -1
  159. #define BRCMS_PROTECTION_OFF 0
  160. #define BRCMS_PROTECTION_ON 1
  161. #define BRCMS_PROTECTION_MMHDR_ONLY 2
  162. #define BRCMS_PROTECTION_CTS_ONLY 3
  163. /* values for g_protection_control and n_protection_control */
  164. #define BRCMS_PROTECTION_CTL_OFF 0
  165. #define BRCMS_PROTECTION_CTL_LOCAL 1
  166. #define BRCMS_PROTECTION_CTL_OVERLAP 2
  167. /* values for n_protection */
  168. #define BRCMS_N_PROTECTION_OFF 0
  169. #define BRCMS_N_PROTECTION_OPTIONAL 1
  170. #define BRCMS_N_PROTECTION_20IN40 2
  171. #define BRCMS_N_PROTECTION_MIXEDMODE 3
  172. /* values for band specific 40MHz capabilities */
  173. #define BRCMS_N_BW_20ALL 0
  174. #define BRCMS_N_BW_40ALL 1
  175. #define BRCMS_N_BW_20IN2G_40IN5G 2
  176. /* bitflags for SGI support (sgi_rx iovar) */
  177. #define BRCMS_N_SGI_20 0x01
  178. #define BRCMS_N_SGI_40 0x02
  179. /* defines used by the nrate iovar */
  180. /* MSC in use,indicates b0-6 holds an mcs */
  181. #define NRATE_MCS_INUSE 0x00000080
  182. /* rate/mcs value */
  183. #define NRATE_RATE_MASK 0x0000007f
  184. /* stf mode mask: siso, cdd, stbc, sdm */
  185. #define NRATE_STF_MASK 0x0000ff00
  186. /* stf mode shift */
  187. #define NRATE_STF_SHIFT 8
  188. /* bit indicate to override mcs only */
  189. #define NRATE_OVERRIDE_MCS_ONLY 0x40000000
  190. #define NRATE_SGI_MASK 0x00800000 /* sgi mode */
  191. #define NRATE_SGI_SHIFT 23 /* sgi mode */
  192. #define NRATE_LDPC_CODING 0x00400000 /* adv coding in use */
  193. #define NRATE_LDPC_SHIFT 22 /* ldpc shift */
  194. #define NRATE_STF_SISO 0 /* stf mode SISO */
  195. #define NRATE_STF_CDD 1 /* stf mode CDD */
  196. #define NRATE_STF_STBC 2 /* stf mode STBC */
  197. #define NRATE_STF_SDM 3 /* stf mode SDM */
  198. #define MAX_DMA_SEGS 4
  199. /* Max # of entries in Tx FIFO based on 4kb page size */
  200. #define NTXD 256
  201. /* Max # of entries in Rx FIFO based on 4kb page size */
  202. #define NRXD 256
  203. /* try to keep this # rbufs posted to the chip */
  204. #define NRXBUFPOST 32
  205. /* data msg txq hiwat mark */
  206. #define BRCMS_DATAHIWAT 50
  207. /* max # frames to process in brcms_c_recv() */
  208. #define RXBND 8
  209. /* max # tx status to process in wlc_txstatus() */
  210. #define TXSBND 8
  211. /* brcmu_format_flags() bit description structure */
  212. struct brcms_c_bit_desc {
  213. u32 bit;
  214. const char *name;
  215. };
  216. /*
  217. * The following table lists the buffer memory allocated to xmt fifos in HW.
  218. * the size is in units of 256bytes(one block), total size is HW dependent
  219. * ucode has default fifo partition, sw can overwrite if necessary
  220. *
  221. * This is documented in twiki under the topic UcodeTxFifo. Please ensure
  222. * the twiki is updated before making changes.
  223. */
  224. /* Starting corerev for the fifo size table */
  225. #define XMTFIFOTBL_STARTREV 20
  226. struct d11init {
  227. __le16 addr;
  228. __le16 size;
  229. __le32 value;
  230. };
  231. struct edcf_acparam {
  232. u8 ACI;
  233. u8 ECW;
  234. u16 TXOP;
  235. } __packed;
  236. const u8 prio2fifo[NUMPRIO] = {
  237. TX_AC_BE_FIFO, /* 0 BE AC_BE Best Effort */
  238. TX_AC_BK_FIFO, /* 1 BK AC_BK Background */
  239. TX_AC_BK_FIFO, /* 2 -- AC_BK Background */
  240. TX_AC_BE_FIFO, /* 3 EE AC_BE Best Effort */
  241. TX_AC_VI_FIFO, /* 4 CL AC_VI Video */
  242. TX_AC_VI_FIFO, /* 5 VI AC_VI Video */
  243. TX_AC_VO_FIFO, /* 6 VO AC_VO Voice */
  244. TX_AC_VO_FIFO /* 7 NC AC_VO Voice */
  245. };
  246. /* debug/trace */
  247. uint brcm_msg_level =
  248. #if defined(BCMDBG)
  249. LOG_ERROR_VAL;
  250. #else
  251. 0;
  252. #endif /* BCMDBG */
  253. /* TX FIFO number to WME/802.1E Access Category */
  254. static const u8 wme_fifo2ac[] = {
  255. IEEE80211_AC_BK,
  256. IEEE80211_AC_BE,
  257. IEEE80211_AC_VI,
  258. IEEE80211_AC_VO,
  259. IEEE80211_AC_BE,
  260. IEEE80211_AC_BE
  261. };
  262. /* ieee80211 Access Category to TX FIFO number */
  263. static const u8 wme_ac2fifo[] = {
  264. TX_AC_VO_FIFO,
  265. TX_AC_VI_FIFO,
  266. TX_AC_BE_FIFO,
  267. TX_AC_BK_FIFO
  268. };
  269. /* 802.1D Priority to precedence queue mapping */
  270. const u8 wlc_prio2prec_map[] = {
  271. _BRCMS_PREC_BE, /* 0 BE - Best-effort */
  272. _BRCMS_PREC_BK, /* 1 BK - Background */
  273. _BRCMS_PREC_NONE, /* 2 None = - */
  274. _BRCMS_PREC_EE, /* 3 EE - Excellent-effort */
  275. _BRCMS_PREC_CL, /* 4 CL - Controlled Load */
  276. _BRCMS_PREC_VI, /* 5 Vi - Video */
  277. _BRCMS_PREC_VO, /* 6 Vo - Voice */
  278. _BRCMS_PREC_NC, /* 7 NC - Network Control */
  279. };
  280. static const u16 xmtfifo_sz[][NFIFO] = {
  281. /* corerev 20: 5120, 49152, 49152, 5376, 4352, 1280 */
  282. {20, 192, 192, 21, 17, 5},
  283. /* corerev 21: 2304, 14848, 5632, 3584, 3584, 1280 */
  284. {9, 58, 22, 14, 14, 5},
  285. /* corerev 22: 5120, 49152, 49152, 5376, 4352, 1280 */
  286. {20, 192, 192, 21, 17, 5},
  287. /* corerev 23: 5120, 49152, 49152, 5376, 4352, 1280 */
  288. {20, 192, 192, 21, 17, 5},
  289. /* corerev 24: 2304, 14848, 5632, 3584, 3584, 1280 */
  290. {9, 58, 22, 14, 14, 5},
  291. };
  292. #ifdef BCMDBG
  293. static const char * const fifo_names[] = {
  294. "AC_BK", "AC_BE", "AC_VI", "AC_VO", "BCMC", "ATIM" };
  295. #else
  296. static const char fifo_names[6][0];
  297. #endif
  298. #ifdef BCMDBG
  299. /* pointer to most recently allocated wl/wlc */
  300. static struct brcms_c_info *wlc_info_dbg = (struct brcms_c_info *) (NULL);
  301. #endif
  302. /* Find basic rate for a given rate */
  303. static u8 brcms_basic_rate(struct brcms_c_info *wlc, u32 rspec)
  304. {
  305. if (is_mcs_rate(rspec))
  306. return wlc->band->basic_rate[mcs_table[rspec & RSPEC_RATE_MASK]
  307. .leg_ofdm];
  308. return wlc->band->basic_rate[rspec & RSPEC_RATE_MASK];
  309. }
  310. static u16 frametype(u32 rspec, u8 mimoframe)
  311. {
  312. if (is_mcs_rate(rspec))
  313. return mimoframe;
  314. return is_cck_rate(rspec) ? FT_CCK : FT_OFDM;
  315. }
  316. /* currently the best mechanism for determining SIFS is the band in use */
  317. static u16 get_sifs(struct brcms_band *band)
  318. {
  319. return band->bandtype == BRCM_BAND_5G ? APHY_SIFS_TIME :
  320. BPHY_SIFS_TIME;
  321. }
  322. /*
  323. * Detect Card removed.
  324. * Even checking an sbconfig register read will not false trigger when the core
  325. * is in reset it breaks CF address mechanism. Accessing gphy phyversion will
  326. * cause SB error if aphy is in reset on 4306B0-DB. Need a simple accessible
  327. * reg with fixed 0/1 pattern (some platforms return all 0).
  328. * If clocks are present, call the sb routine which will figure out if the
  329. * device is removed.
  330. */
  331. static bool brcms_deviceremoved(struct brcms_c_info *wlc)
  332. {
  333. u32 macctrl;
  334. if (!wlc->hw->clk)
  335. return ai_deviceremoved(wlc->hw->sih);
  336. macctrl = bcma_read32(wlc->hw->d11core,
  337. D11REGOFFS(maccontrol));
  338. return (macctrl & (MCTL_PSM_JMP_0 | MCTL_IHR_EN)) != MCTL_IHR_EN;
  339. }
  340. /* sum the individual fifo tx pending packet counts */
  341. static s16 brcms_txpktpendtot(struct brcms_c_info *wlc)
  342. {
  343. return wlc->core->txpktpend[0] + wlc->core->txpktpend[1] +
  344. wlc->core->txpktpend[2] + wlc->core->txpktpend[3];
  345. }
  346. static bool brcms_is_mband_unlocked(struct brcms_c_info *wlc)
  347. {
  348. return wlc->pub->_nbands > 1 && !wlc->bandlocked;
  349. }
  350. static int brcms_chspec_bw(u16 chanspec)
  351. {
  352. if (CHSPEC_IS40(chanspec))
  353. return BRCMS_40_MHZ;
  354. if (CHSPEC_IS20(chanspec))
  355. return BRCMS_20_MHZ;
  356. return BRCMS_10_MHZ;
  357. }
  358. static void brcms_c_bsscfg_mfree(struct brcms_bss_cfg *cfg)
  359. {
  360. if (cfg == NULL)
  361. return;
  362. kfree(cfg->current_bss);
  363. kfree(cfg);
  364. }
  365. static void brcms_c_detach_mfree(struct brcms_c_info *wlc)
  366. {
  367. if (wlc == NULL)
  368. return;
  369. brcms_c_bsscfg_mfree(wlc->bsscfg);
  370. kfree(wlc->pub);
  371. kfree(wlc->modulecb);
  372. kfree(wlc->default_bss);
  373. kfree(wlc->protection);
  374. kfree(wlc->stf);
  375. kfree(wlc->bandstate[0]);
  376. kfree(wlc->corestate->macstat_snapshot);
  377. kfree(wlc->corestate);
  378. kfree(wlc->hw->bandstate[0]);
  379. kfree(wlc->hw);
  380. /* free the wlc */
  381. kfree(wlc);
  382. wlc = NULL;
  383. }
  384. static struct brcms_bss_cfg *brcms_c_bsscfg_malloc(uint unit)
  385. {
  386. struct brcms_bss_cfg *cfg;
  387. cfg = kzalloc(sizeof(struct brcms_bss_cfg), GFP_ATOMIC);
  388. if (cfg == NULL)
  389. goto fail;
  390. cfg->current_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  391. if (cfg->current_bss == NULL)
  392. goto fail;
  393. return cfg;
  394. fail:
  395. brcms_c_bsscfg_mfree(cfg);
  396. return NULL;
  397. }
  398. static struct brcms_c_info *
  399. brcms_c_attach_malloc(uint unit, uint *err, uint devid)
  400. {
  401. struct brcms_c_info *wlc;
  402. wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
  403. if (wlc == NULL) {
  404. *err = 1002;
  405. goto fail;
  406. }
  407. /* allocate struct brcms_c_pub state structure */
  408. wlc->pub = kzalloc(sizeof(struct brcms_pub), GFP_ATOMIC);
  409. if (wlc->pub == NULL) {
  410. *err = 1003;
  411. goto fail;
  412. }
  413. wlc->pub->wlc = wlc;
  414. /* allocate struct brcms_hardware state structure */
  415. wlc->hw = kzalloc(sizeof(struct brcms_hardware), GFP_ATOMIC);
  416. if (wlc->hw == NULL) {
  417. *err = 1005;
  418. goto fail;
  419. }
  420. wlc->hw->wlc = wlc;
  421. wlc->hw->bandstate[0] =
  422. kzalloc(sizeof(struct brcms_hw_band) * MAXBANDS, GFP_ATOMIC);
  423. if (wlc->hw->bandstate[0] == NULL) {
  424. *err = 1006;
  425. goto fail;
  426. } else {
  427. int i;
  428. for (i = 1; i < MAXBANDS; i++)
  429. wlc->hw->bandstate[i] = (struct brcms_hw_band *)
  430. ((unsigned long)wlc->hw->bandstate[0] +
  431. (sizeof(struct brcms_hw_band) * i));
  432. }
  433. wlc->modulecb =
  434. kzalloc(sizeof(struct modulecb) * BRCMS_MAXMODULES, GFP_ATOMIC);
  435. if (wlc->modulecb == NULL) {
  436. *err = 1009;
  437. goto fail;
  438. }
  439. wlc->default_bss = kzalloc(sizeof(struct brcms_bss_info), GFP_ATOMIC);
  440. if (wlc->default_bss == NULL) {
  441. *err = 1010;
  442. goto fail;
  443. }
  444. wlc->bsscfg = brcms_c_bsscfg_malloc(unit);
  445. if (wlc->bsscfg == NULL) {
  446. *err = 1011;
  447. goto fail;
  448. }
  449. wlc->protection = kzalloc(sizeof(struct brcms_protection),
  450. GFP_ATOMIC);
  451. if (wlc->protection == NULL) {
  452. *err = 1016;
  453. goto fail;
  454. }
  455. wlc->stf = kzalloc(sizeof(struct brcms_stf), GFP_ATOMIC);
  456. if (wlc->stf == NULL) {
  457. *err = 1017;
  458. goto fail;
  459. }
  460. wlc->bandstate[0] =
  461. kzalloc(sizeof(struct brcms_band)*MAXBANDS, GFP_ATOMIC);
  462. if (wlc->bandstate[0] == NULL) {
  463. *err = 1025;
  464. goto fail;
  465. } else {
  466. int i;
  467. for (i = 1; i < MAXBANDS; i++)
  468. wlc->bandstate[i] = (struct brcms_band *)
  469. ((unsigned long)wlc->bandstate[0]
  470. + (sizeof(struct brcms_band)*i));
  471. }
  472. wlc->corestate = kzalloc(sizeof(struct brcms_core), GFP_ATOMIC);
  473. if (wlc->corestate == NULL) {
  474. *err = 1026;
  475. goto fail;
  476. }
  477. wlc->corestate->macstat_snapshot =
  478. kzalloc(sizeof(struct macstat), GFP_ATOMIC);
  479. if (wlc->corestate->macstat_snapshot == NULL) {
  480. *err = 1027;
  481. goto fail;
  482. }
  483. return wlc;
  484. fail:
  485. brcms_c_detach_mfree(wlc);
  486. return NULL;
  487. }
  488. /*
  489. * Update the slot timing for standard 11b/g (20us slots)
  490. * or shortslot 11g (9us slots)
  491. * The PSM needs to be suspended for this call.
  492. */
  493. static void brcms_b_update_slot_timing(struct brcms_hardware *wlc_hw,
  494. bool shortslot)
  495. {
  496. struct bcma_device *core = wlc_hw->d11core;
  497. if (shortslot) {
  498. /* 11g short slot: 11a timing */
  499. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0207);
  500. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, APHY_SLOT_TIME);
  501. } else {
  502. /* 11g long slot: 11b timing */
  503. bcma_write16(core, D11REGOFFS(ifs_slot), 0x0212);
  504. brcms_b_write_shm(wlc_hw, M_DOT11_SLOT, BPHY_SLOT_TIME);
  505. }
  506. }
  507. /*
  508. * calculate frame duration of a given rate and length, return
  509. * time in usec unit
  510. */
  511. static uint brcms_c_calc_frame_time(struct brcms_c_info *wlc, u32 ratespec,
  512. u8 preamble_type, uint mac_len)
  513. {
  514. uint nsyms, dur = 0, Ndps, kNdps;
  515. uint rate = rspec2rate(ratespec);
  516. if (rate == 0) {
  517. wiphy_err(wlc->wiphy, "wl%d: WAR: using rate of 1 mbps\n",
  518. wlc->pub->unit);
  519. rate = BRCM_RATE_1M;
  520. }
  521. BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, len%d\n",
  522. wlc->pub->unit, ratespec, preamble_type, mac_len);
  523. if (is_mcs_rate(ratespec)) {
  524. uint mcs = ratespec & RSPEC_RATE_MASK;
  525. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  526. dur = PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  527. if (preamble_type == BRCMS_MM_PREAMBLE)
  528. dur += PREN_MM_EXT;
  529. /* 1000Ndbps = kbps * 4 */
  530. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  531. rspec_issgi(ratespec)) * 4;
  532. if (rspec_stc(ratespec) == 0)
  533. nsyms =
  534. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  535. APHY_TAIL_NBITS) * 1000, kNdps);
  536. else
  537. /* STBC needs to have even number of symbols */
  538. nsyms =
  539. 2 *
  540. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  541. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  542. dur += APHY_SYMBOL_TIME * nsyms;
  543. if (wlc->band->bandtype == BRCM_BAND_2G)
  544. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  545. } else if (is_ofdm_rate(rate)) {
  546. dur = APHY_PREAMBLE_TIME;
  547. dur += APHY_SIGNAL_TIME;
  548. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  549. Ndps = rate * 2;
  550. /* NSyms = CEILING((SERVICE + 8*NBytes + TAIL) / Ndbps) */
  551. nsyms =
  552. CEIL((APHY_SERVICE_NBITS + 8 * mac_len + APHY_TAIL_NBITS),
  553. Ndps);
  554. dur += APHY_SYMBOL_TIME * nsyms;
  555. if (wlc->band->bandtype == BRCM_BAND_2G)
  556. dur += DOT11_OFDM_SIGNAL_EXTENSION;
  557. } else {
  558. /*
  559. * calc # bits * 2 so factor of 2 in rate (1/2 mbps)
  560. * will divide out
  561. */
  562. mac_len = mac_len * 8 * 2;
  563. /* calc ceiling of bits/rate = microseconds of air time */
  564. dur = (mac_len + rate - 1) / rate;
  565. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  566. dur += BPHY_PLCP_SHORT_TIME;
  567. else
  568. dur += BPHY_PLCP_TIME;
  569. }
  570. return dur;
  571. }
  572. static void brcms_c_write_inits(struct brcms_hardware *wlc_hw,
  573. const struct d11init *inits)
  574. {
  575. struct bcma_device *core = wlc_hw->d11core;
  576. int i;
  577. uint offset;
  578. u16 size;
  579. u32 value;
  580. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  581. for (i = 0; inits[i].addr != cpu_to_le16(0xffff); i++) {
  582. size = le16_to_cpu(inits[i].size);
  583. offset = le16_to_cpu(inits[i].addr);
  584. value = le32_to_cpu(inits[i].value);
  585. if (size == 2)
  586. bcma_write16(core, offset, value);
  587. else if (size == 4)
  588. bcma_write32(core, offset, value);
  589. else
  590. break;
  591. }
  592. }
  593. static void brcms_c_write_mhf(struct brcms_hardware *wlc_hw, u16 *mhfs)
  594. {
  595. u8 idx;
  596. u16 addr[] = {
  597. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  598. M_HOST_FLAGS5
  599. };
  600. for (idx = 0; idx < MHFMAX; idx++)
  601. brcms_b_write_shm(wlc_hw, addr[idx], mhfs[idx]);
  602. }
  603. static void brcms_c_ucode_bsinit(struct brcms_hardware *wlc_hw)
  604. {
  605. struct wiphy *wiphy = wlc_hw->wlc->wiphy;
  606. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  607. /* init microcode host flags */
  608. brcms_c_write_mhf(wlc_hw, wlc_hw->band->mhfs);
  609. /* do band-specific ucode IHR, SHM, and SCR inits */
  610. if (D11REV_IS(wlc_hw->corerev, 23)) {
  611. if (BRCMS_ISNPHY(wlc_hw->band))
  612. brcms_c_write_inits(wlc_hw, ucode->d11n0bsinitvals16);
  613. else
  614. wiphy_err(wiphy, "%s: wl%d: unsupported phy in corerev"
  615. " %d\n", __func__, wlc_hw->unit,
  616. wlc_hw->corerev);
  617. } else {
  618. if (D11REV_IS(wlc_hw->corerev, 24)) {
  619. if (BRCMS_ISLCNPHY(wlc_hw->band))
  620. brcms_c_write_inits(wlc_hw,
  621. ucode->d11lcn0bsinitvals24);
  622. else
  623. wiphy_err(wiphy, "%s: wl%d: unsupported phy in"
  624. " core rev %d\n", __func__,
  625. wlc_hw->unit, wlc_hw->corerev);
  626. } else {
  627. wiphy_err(wiphy, "%s: wl%d: unsupported corerev %d\n",
  628. __func__, wlc_hw->unit, wlc_hw->corerev);
  629. }
  630. }
  631. }
  632. static void brcms_b_core_ioctl(struct brcms_hardware *wlc_hw, u32 m, u32 v)
  633. {
  634. struct bcma_device *core = wlc_hw->d11core;
  635. u32 ioctl = bcma_aread32(core, BCMA_IOCTL) & ~m;
  636. bcma_awrite32(core, BCMA_IOCTL, ioctl | v);
  637. }
  638. static void brcms_b_core_phy_clk(struct brcms_hardware *wlc_hw, bool clk)
  639. {
  640. BCMMSG(wlc_hw->wlc->wiphy, "wl%d: clk %d\n", wlc_hw->unit, clk);
  641. wlc_hw->phyclk = clk;
  642. if (OFF == clk) { /* clear gmode bit, put phy into reset */
  643. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC | SICF_GMODE),
  644. (SICF_PRST | SICF_FGC));
  645. udelay(1);
  646. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_PRST);
  647. udelay(1);
  648. } else { /* take phy out of reset */
  649. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_FGC), SICF_FGC);
  650. udelay(1);
  651. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  652. udelay(1);
  653. }
  654. }
  655. /* low-level band switch utility routine */
  656. static void brcms_c_setxband(struct brcms_hardware *wlc_hw, uint bandunit)
  657. {
  658. BCMMSG(wlc_hw->wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
  659. bandunit);
  660. wlc_hw->band = wlc_hw->bandstate[bandunit];
  661. /*
  662. * BMAC_NOTE:
  663. * until we eliminate need for wlc->band refs in low level code
  664. */
  665. wlc_hw->wlc->band = wlc_hw->wlc->bandstate[bandunit];
  666. /* set gmode core flag */
  667. if (wlc_hw->sbclk && !wlc_hw->noreset) {
  668. u32 gmode = 0;
  669. if (bandunit == 0)
  670. gmode = SICF_GMODE;
  671. brcms_b_core_ioctl(wlc_hw, SICF_GMODE, gmode);
  672. }
  673. }
  674. /* switch to new band but leave it inactive */
  675. static u32 brcms_c_setband_inact(struct brcms_c_info *wlc, uint bandunit)
  676. {
  677. struct brcms_hardware *wlc_hw = wlc->hw;
  678. u32 macintmask;
  679. u32 macctrl;
  680. BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
  681. macctrl = bcma_read32(wlc_hw->d11core,
  682. D11REGOFFS(maccontrol));
  683. WARN_ON((macctrl & MCTL_EN_MAC) != 0);
  684. /* disable interrupts */
  685. macintmask = brcms_intrsoff(wlc->wl);
  686. /* radio off */
  687. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  688. brcms_b_core_phy_clk(wlc_hw, OFF);
  689. brcms_c_setxband(wlc_hw, bandunit);
  690. return macintmask;
  691. }
  692. /* process an individual struct tx_status */
  693. static bool
  694. brcms_c_dotxstatus(struct brcms_c_info *wlc, struct tx_status *txs)
  695. {
  696. struct sk_buff *p;
  697. uint queue;
  698. struct d11txh *txh;
  699. struct scb *scb = NULL;
  700. bool free_pdu;
  701. int tx_rts, tx_frame_count, tx_rts_count;
  702. uint totlen, supr_status;
  703. bool lastframe;
  704. struct ieee80211_hdr *h;
  705. u16 mcl;
  706. struct ieee80211_tx_info *tx_info;
  707. struct ieee80211_tx_rate *txrate;
  708. int i;
  709. /* discard intermediate indications for ucode with one legitimate case:
  710. * e.g. if "useRTS" is set. ucode did a successful rts/cts exchange,
  711. * but the subsequent tx of DATA failed. so it will start rts/cts
  712. * from the beginning (resetting the rts transmission count)
  713. */
  714. if (!(txs->status & TX_STATUS_AMPDU)
  715. && (txs->status & TX_STATUS_INTERMEDIATE)) {
  716. wiphy_err(wlc->wiphy, "%s: INTERMEDIATE but not AMPDU\n",
  717. __func__);
  718. return false;
  719. }
  720. queue = txs->frameid & TXFID_QUEUE_MASK;
  721. if (queue >= NFIFO) {
  722. p = NULL;
  723. goto fatal;
  724. }
  725. p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);
  726. if (p == NULL)
  727. goto fatal;
  728. txh = (struct d11txh *) (p->data);
  729. mcl = le16_to_cpu(txh->MacTxControlLow);
  730. if (txs->phyerr) {
  731. if (brcm_msg_level & LOG_ERROR_VAL) {
  732. wiphy_err(wlc->wiphy, "phyerr 0x%x, rate 0x%x\n",
  733. txs->phyerr, txh->MainRates);
  734. brcms_c_print_txdesc(txh);
  735. }
  736. brcms_c_print_txstatus(txs);
  737. }
  738. if (txs->frameid != le16_to_cpu(txh->TxFrameID))
  739. goto fatal;
  740. tx_info = IEEE80211_SKB_CB(p);
  741. h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
  742. if (tx_info->control.sta)
  743. scb = &wlc->pri_scb;
  744. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  745. brcms_c_ampdu_dotxstatus(wlc->ampdu, scb, p, txs);
  746. return false;
  747. }
  748. supr_status = txs->status & TX_STATUS_SUPR_MASK;
  749. if (supr_status == TX_STATUS_SUPR_BADCH)
  750. BCMMSG(wlc->wiphy,
  751. "%s: Pkt tx suppressed, possibly channel %d\n",
  752. __func__, CHSPEC_CHANNEL(wlc->default_bss->chanspec));
  753. tx_rts = le16_to_cpu(txh->MacTxControlLow) & TXC_SENDRTS;
  754. tx_frame_count =
  755. (txs->status & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT;
  756. tx_rts_count =
  757. (txs->status & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT;
  758. lastframe = !ieee80211_has_morefrags(h->frame_control);
  759. if (!lastframe) {
  760. wiphy_err(wlc->wiphy, "Not last frame!\n");
  761. } else {
  762. /*
  763. * Set information to be consumed by Minstrel ht.
  764. *
  765. * The "fallback limit" is the number of tx attempts a given
  766. * MPDU is sent at the "primary" rate. Tx attempts beyond that
  767. * limit are sent at the "secondary" rate.
  768. * A 'short frame' does not exceed RTS treshold.
  769. */
  770. u16 sfbl, /* Short Frame Rate Fallback Limit */
  771. lfbl, /* Long Frame Rate Fallback Limit */
  772. fbl;
  773. if (queue < IEEE80211_NUM_ACS) {
  774. sfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  775. EDCF_SFB);
  776. lfbl = GFIELD(wlc->wme_retries[wme_fifo2ac[queue]],
  777. EDCF_LFB);
  778. } else {
  779. sfbl = wlc->SFBL;
  780. lfbl = wlc->LFBL;
  781. }
  782. txrate = tx_info->status.rates;
  783. if (txrate[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  784. fbl = lfbl;
  785. else
  786. fbl = sfbl;
  787. ieee80211_tx_info_clear_status(tx_info);
  788. if ((tx_frame_count > fbl) && (txrate[1].idx >= 0)) {
  789. /*
  790. * rate selection requested a fallback rate
  791. * and we used it
  792. */
  793. txrate[0].count = fbl;
  794. txrate[1].count = tx_frame_count - fbl;
  795. } else {
  796. /*
  797. * rate selection did not request fallback rate, or
  798. * we didn't need it
  799. */
  800. txrate[0].count = tx_frame_count;
  801. /*
  802. * rc80211_minstrel.c:minstrel_tx_status() expects
  803. * unused rates to be marked with idx = -1
  804. */
  805. txrate[1].idx = -1;
  806. txrate[1].count = 0;
  807. }
  808. /* clear the rest of the rates */
  809. for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {
  810. txrate[i].idx = -1;
  811. txrate[i].count = 0;
  812. }
  813. if (txs->status & TX_STATUS_ACK_RCV)
  814. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  815. }
  816. totlen = p->len;
  817. free_pdu = true;
  818. brcms_c_txfifo_complete(wlc, queue, 1);
  819. if (lastframe) {
  820. /* remove PLCP & Broadcom tx descriptor header */
  821. skb_pull(p, D11_PHY_HDR_LEN);
  822. skb_pull(p, D11_TXH_LEN);
  823. ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw, p);
  824. } else {
  825. wiphy_err(wlc->wiphy, "%s: Not last frame => not calling "
  826. "tx_status\n", __func__);
  827. }
  828. return false;
  829. fatal:
  830. if (p)
  831. brcmu_pkt_buf_free_skb(p);
  832. return true;
  833. }
  834. /* process tx completion events in BMAC
  835. * Return true if more tx status need to be processed. false otherwise.
  836. */
  837. static bool
  838. brcms_b_txstatus(struct brcms_hardware *wlc_hw, bool bound, bool *fatal)
  839. {
  840. bool morepending = false;
  841. struct brcms_c_info *wlc = wlc_hw->wlc;
  842. struct bcma_device *core;
  843. struct tx_status txstatus, *txs;
  844. u32 s1, s2;
  845. uint n = 0;
  846. /*
  847. * Param 'max_tx_num' indicates max. # tx status to process before
  848. * break out.
  849. */
  850. uint max_tx_num = bound ? TXSBND : -1;
  851. BCMMSG(wlc->wiphy, "wl%d\n", wlc_hw->unit);
  852. txs = &txstatus;
  853. core = wlc_hw->d11core;
  854. *fatal = false;
  855. s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
  856. while (!(*fatal)
  857. && (s1 & TXS_V)) {
  858. if (s1 == 0xffffffff) {
  859. wiphy_err(wlc->wiphy, "wl%d: %s: dead chip\n",
  860. wlc_hw->unit, __func__);
  861. return morepending;
  862. }
  863. s2 = bcma_read32(core, D11REGOFFS(frmtxstatus2));
  864. txs->status = s1 & TXS_STATUS_MASK;
  865. txs->frameid = (s1 & TXS_FID_MASK) >> TXS_FID_SHIFT;
  866. txs->sequence = s2 & TXS_SEQ_MASK;
  867. txs->phyerr = (s2 & TXS_PTX_MASK) >> TXS_PTX_SHIFT;
  868. txs->lasttxtime = 0;
  869. *fatal = brcms_c_dotxstatus(wlc_hw->wlc, txs);
  870. /* !give others some time to run! */
  871. if (++n >= max_tx_num)
  872. break;
  873. s1 = bcma_read32(core, D11REGOFFS(frmtxstatus));
  874. }
  875. if (*fatal)
  876. return 0;
  877. if (n >= max_tx_num)
  878. morepending = true;
  879. if (!pktq_empty(&wlc->pkt_queue->q))
  880. brcms_c_send_q(wlc);
  881. return morepending;
  882. }
  883. static void brcms_c_tbtt(struct brcms_c_info *wlc)
  884. {
  885. if (!wlc->bsscfg->BSS)
  886. /*
  887. * DirFrmQ is now valid...defer setting until end
  888. * of ATIM window
  889. */
  890. wlc->qvalid |= MCMD_DIRFRMQVAL;
  891. }
  892. /* set initial host flags value */
  893. static void
  894. brcms_c_mhfdef(struct brcms_c_info *wlc, u16 *mhfs, u16 mhf2_init)
  895. {
  896. struct brcms_hardware *wlc_hw = wlc->hw;
  897. memset(mhfs, 0, MHFMAX * sizeof(u16));
  898. mhfs[MHF2] |= mhf2_init;
  899. /* prohibit use of slowclock on multifunction boards */
  900. if (wlc_hw->boardflags & BFL_NOPLLDOWN)
  901. mhfs[MHF1] |= MHF1_FORCEFASTCLK;
  902. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_LT(wlc_hw->band->phyrev, 2)) {
  903. mhfs[MHF2] |= MHF2_NPHY40MHZ_WAR;
  904. mhfs[MHF1] |= MHF1_IQSWAP_WAR;
  905. }
  906. }
  907. static uint
  908. dmareg(uint direction, uint fifonum)
  909. {
  910. if (direction == DMA_TX)
  911. return offsetof(struct d11regs, fifo64regs[fifonum].dmaxmt);
  912. return offsetof(struct d11regs, fifo64regs[fifonum].dmarcv);
  913. }
  914. static bool brcms_b_attach_dmapio(struct brcms_c_info *wlc, uint j, bool wme)
  915. {
  916. uint i;
  917. char name[8];
  918. /*
  919. * ucode host flag 2 needed for pio mode, independent of band and fifo
  920. */
  921. u16 pio_mhf2 = 0;
  922. struct brcms_hardware *wlc_hw = wlc->hw;
  923. uint unit = wlc_hw->unit;
  924. struct wiphy *wiphy = wlc->wiphy;
  925. /* name and offsets for dma_attach */
  926. snprintf(name, sizeof(name), "wl%d", unit);
  927. if (wlc_hw->di[0] == NULL) { /* Init FIFOs */
  928. int dma_attach_err = 0;
  929. /*
  930. * FIFO 0
  931. * TX: TX_AC_BK_FIFO (TX AC Background data packets)
  932. * RX: RX_FIFO (RX data packets)
  933. */
  934. wlc_hw->di[0] = dma_attach(name, wlc_hw->sih, wlc_hw->d11core,
  935. (wme ? dmareg(DMA_TX, 0) : 0),
  936. dmareg(DMA_RX, 0),
  937. (wme ? NTXD : 0), NRXD,
  938. RXBUFSZ, -1, NRXBUFPOST,
  939. BRCMS_HWRXOFF, &brcm_msg_level);
  940. dma_attach_err |= (NULL == wlc_hw->di[0]);
  941. /*
  942. * FIFO 1
  943. * TX: TX_AC_BE_FIFO (TX AC Best-Effort data packets)
  944. * (legacy) TX_DATA_FIFO (TX data packets)
  945. * RX: UNUSED
  946. */
  947. wlc_hw->di[1] = dma_attach(name, wlc_hw->sih, wlc_hw->d11core,
  948. dmareg(DMA_TX, 1), 0,
  949. NTXD, 0, 0, -1, 0, 0,
  950. &brcm_msg_level);
  951. dma_attach_err |= (NULL == wlc_hw->di[1]);
  952. /*
  953. * FIFO 2
  954. * TX: TX_AC_VI_FIFO (TX AC Video data packets)
  955. * RX: UNUSED
  956. */
  957. wlc_hw->di[2] = dma_attach(name, wlc_hw->sih, wlc_hw->d11core,
  958. dmareg(DMA_TX, 2), 0,
  959. NTXD, 0, 0, -1, 0, 0,
  960. &brcm_msg_level);
  961. dma_attach_err |= (NULL == wlc_hw->di[2]);
  962. /*
  963. * FIFO 3
  964. * TX: TX_AC_VO_FIFO (TX AC Voice data packets)
  965. * (legacy) TX_CTL_FIFO (TX control & mgmt packets)
  966. */
  967. wlc_hw->di[3] = dma_attach(name, wlc_hw->sih, wlc_hw->d11core,
  968. dmareg(DMA_TX, 3),
  969. 0, NTXD, 0, 0, -1,
  970. 0, 0, &brcm_msg_level);
  971. dma_attach_err |= (NULL == wlc_hw->di[3]);
  972. /* Cleaner to leave this as if with AP defined */
  973. if (dma_attach_err) {
  974. wiphy_err(wiphy, "wl%d: wlc_attach: dma_attach failed"
  975. "\n", unit);
  976. return false;
  977. }
  978. /* get pointer to dma engine tx flow control variable */
  979. for (i = 0; i < NFIFO; i++)
  980. if (wlc_hw->di[i])
  981. wlc_hw->txavail[i] =
  982. (uint *) dma_getvar(wlc_hw->di[i],
  983. "&txavail");
  984. }
  985. /* initial ucode host flags */
  986. brcms_c_mhfdef(wlc, wlc_hw->band->mhfs, pio_mhf2);
  987. return true;
  988. }
  989. static void brcms_b_detach_dmapio(struct brcms_hardware *wlc_hw)
  990. {
  991. uint j;
  992. for (j = 0; j < NFIFO; j++) {
  993. if (wlc_hw->di[j]) {
  994. dma_detach(wlc_hw->di[j]);
  995. wlc_hw->di[j] = NULL;
  996. }
  997. }
  998. }
  999. /*
  1000. * Initialize brcms_c_info default values ...
  1001. * may get overrides later in this function
  1002. * BMAC_NOTES, move low out and resolve the dangling ones
  1003. */
  1004. static void brcms_b_info_init(struct brcms_hardware *wlc_hw)
  1005. {
  1006. struct brcms_c_info *wlc = wlc_hw->wlc;
  1007. /* set default sw macintmask value */
  1008. wlc->defmacintmask = DEF_MACINTMASK;
  1009. /* various 802.11g modes */
  1010. wlc_hw->shortslot = false;
  1011. wlc_hw->SFBL = RETRY_SHORT_FB;
  1012. wlc_hw->LFBL = RETRY_LONG_FB;
  1013. /* default mac retry limits */
  1014. wlc_hw->SRL = RETRY_SHORT_DEF;
  1015. wlc_hw->LRL = RETRY_LONG_DEF;
  1016. wlc_hw->chanspec = ch20mhz_chspec(1);
  1017. }
  1018. static void brcms_b_wait_for_wake(struct brcms_hardware *wlc_hw)
  1019. {
  1020. /* delay before first read of ucode state */
  1021. udelay(40);
  1022. /* wait until ucode is no longer asleep */
  1023. SPINWAIT((brcms_b_read_shm(wlc_hw, M_UCODE_DBGST) ==
  1024. DBGST_ASLEEP), wlc_hw->wlc->fastpwrup_dly);
  1025. }
  1026. /* control chip clock to save power, enable dynamic clock or force fast clock */
  1027. static void brcms_b_clkctl_clk(struct brcms_hardware *wlc_hw, uint mode)
  1028. {
  1029. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU) {
  1030. /* new chips with PMU, CCS_FORCEHT will distribute the HT clock
  1031. * on backplane, but mac core will still run on ALP(not HT) when
  1032. * it enters powersave mode, which means the FCA bit may not be
  1033. * set. Should wakeup mac if driver wants it to run on HT.
  1034. */
  1035. if (wlc_hw->clk) {
  1036. if (mode == CLK_FAST) {
  1037. bcma_set32(wlc_hw->d11core,
  1038. D11REGOFFS(clk_ctl_st),
  1039. CCS_FORCEHT);
  1040. udelay(64);
  1041. SPINWAIT(
  1042. ((bcma_read32(wlc_hw->d11core,
  1043. D11REGOFFS(clk_ctl_st)) &
  1044. CCS_HTAVAIL) == 0),
  1045. PMU_MAX_TRANSITION_DLY);
  1046. WARN_ON(!(bcma_read32(wlc_hw->d11core,
  1047. D11REGOFFS(clk_ctl_st)) &
  1048. CCS_HTAVAIL));
  1049. } else {
  1050. if ((ai_get_pmurev(wlc_hw->sih) == 0) &&
  1051. (bcma_read32(wlc_hw->d11core,
  1052. D11REGOFFS(clk_ctl_st)) &
  1053. (CCS_FORCEHT | CCS_HTAREQ)))
  1054. SPINWAIT(
  1055. ((bcma_read32(wlc_hw->d11core,
  1056. offsetof(struct d11regs,
  1057. clk_ctl_st)) &
  1058. CCS_HTAVAIL) == 0),
  1059. PMU_MAX_TRANSITION_DLY);
  1060. bcma_mask32(wlc_hw->d11core,
  1061. D11REGOFFS(clk_ctl_st),
  1062. ~CCS_FORCEHT);
  1063. }
  1064. }
  1065. wlc_hw->forcefastclk = (mode == CLK_FAST);
  1066. } else {
  1067. /* old chips w/o PMU, force HT through cc,
  1068. * then use FCA to verify mac is running fast clock
  1069. */
  1070. wlc_hw->forcefastclk = ai_clkctl_cc(wlc_hw->sih, mode);
  1071. /* check fast clock is available (if core is not in reset) */
  1072. if (wlc_hw->forcefastclk && wlc_hw->clk)
  1073. WARN_ON(!(bcma_aread32(wlc_hw->d11core, BCMA_IOST) &
  1074. SISF_FCLKA));
  1075. /*
  1076. * keep the ucode wake bit on if forcefastclk is on since we
  1077. * do not want ucode to put us back to slow clock when it dozes
  1078. * for PM mode. Code below matches the wake override bit with
  1079. * current forcefastclk state. Only setting bit in wake_override
  1080. * instead of waking ucode immediately since old code had this
  1081. * behavior. Older code set wlc->forcefastclk but only had the
  1082. * wake happen if the wakup_ucode work (protected by an up
  1083. * check) was executed just below.
  1084. */
  1085. if (wlc_hw->forcefastclk)
  1086. mboolset(wlc_hw->wake_override,
  1087. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1088. else
  1089. mboolclr(wlc_hw->wake_override,
  1090. BRCMS_WAKE_OVERRIDE_FORCEFAST);
  1091. }
  1092. }
  1093. /* set or clear ucode host flag bits
  1094. * it has an optimization for no-change write
  1095. * it only writes through shared memory when the core has clock;
  1096. * pre-CLK changes should use wlc_write_mhf to get around the optimization
  1097. *
  1098. *
  1099. * bands values are: BRCM_BAND_AUTO <--- Current band only
  1100. * BRCM_BAND_5G <--- 5G band only
  1101. * BRCM_BAND_2G <--- 2G band only
  1102. * BRCM_BAND_ALL <--- All bands
  1103. */
  1104. void
  1105. brcms_b_mhf(struct brcms_hardware *wlc_hw, u8 idx, u16 mask, u16 val,
  1106. int bands)
  1107. {
  1108. u16 save;
  1109. u16 addr[MHFMAX] = {
  1110. M_HOST_FLAGS1, M_HOST_FLAGS2, M_HOST_FLAGS3, M_HOST_FLAGS4,
  1111. M_HOST_FLAGS5
  1112. };
  1113. struct brcms_hw_band *band;
  1114. if ((val & ~mask) || idx >= MHFMAX)
  1115. return; /* error condition */
  1116. switch (bands) {
  1117. /* Current band only or all bands,
  1118. * then set the band to current band
  1119. */
  1120. case BRCM_BAND_AUTO:
  1121. case BRCM_BAND_ALL:
  1122. band = wlc_hw->band;
  1123. break;
  1124. case BRCM_BAND_5G:
  1125. band = wlc_hw->bandstate[BAND_5G_INDEX];
  1126. break;
  1127. case BRCM_BAND_2G:
  1128. band = wlc_hw->bandstate[BAND_2G_INDEX];
  1129. break;
  1130. default:
  1131. band = NULL; /* error condition */
  1132. }
  1133. if (band) {
  1134. save = band->mhfs[idx];
  1135. band->mhfs[idx] = (band->mhfs[idx] & ~mask) | val;
  1136. /* optimization: only write through if changed, and
  1137. * changed band is the current band
  1138. */
  1139. if (wlc_hw->clk && (band->mhfs[idx] != save)
  1140. && (band == wlc_hw->band))
  1141. brcms_b_write_shm(wlc_hw, addr[idx],
  1142. (u16) band->mhfs[idx]);
  1143. }
  1144. if (bands == BRCM_BAND_ALL) {
  1145. wlc_hw->bandstate[0]->mhfs[idx] =
  1146. (wlc_hw->bandstate[0]->mhfs[idx] & ~mask) | val;
  1147. wlc_hw->bandstate[1]->mhfs[idx] =
  1148. (wlc_hw->bandstate[1]->mhfs[idx] & ~mask) | val;
  1149. }
  1150. }
  1151. /* set the maccontrol register to desired reset state and
  1152. * initialize the sw cache of the register
  1153. */
  1154. static void brcms_c_mctrl_reset(struct brcms_hardware *wlc_hw)
  1155. {
  1156. /* IHR accesses are always enabled, PSM disabled, HPS off and WAKE on */
  1157. wlc_hw->maccontrol = 0;
  1158. wlc_hw->suspended_fifos = 0;
  1159. wlc_hw->wake_override = 0;
  1160. wlc_hw->mute_override = 0;
  1161. brcms_b_mctrl(wlc_hw, ~0, MCTL_IHR_EN | MCTL_WAKE);
  1162. }
  1163. /*
  1164. * write the software state of maccontrol and
  1165. * overrides to the maccontrol register
  1166. */
  1167. static void brcms_c_mctrl_write(struct brcms_hardware *wlc_hw)
  1168. {
  1169. u32 maccontrol = wlc_hw->maccontrol;
  1170. /* OR in the wake bit if overridden */
  1171. if (wlc_hw->wake_override)
  1172. maccontrol |= MCTL_WAKE;
  1173. /* set AP and INFRA bits for mute if needed */
  1174. if (wlc_hw->mute_override) {
  1175. maccontrol &= ~(MCTL_AP);
  1176. maccontrol |= MCTL_INFRA;
  1177. }
  1178. bcma_write32(wlc_hw->d11core, D11REGOFFS(maccontrol),
  1179. maccontrol);
  1180. }
  1181. /* set or clear maccontrol bits */
  1182. void brcms_b_mctrl(struct brcms_hardware *wlc_hw, u32 mask, u32 val)
  1183. {
  1184. u32 maccontrol;
  1185. u32 new_maccontrol;
  1186. if (val & ~mask)
  1187. return; /* error condition */
  1188. maccontrol = wlc_hw->maccontrol;
  1189. new_maccontrol = (maccontrol & ~mask) | val;
  1190. /* if the new maccontrol value is the same as the old, nothing to do */
  1191. if (new_maccontrol == maccontrol)
  1192. return;
  1193. /* something changed, cache the new value */
  1194. wlc_hw->maccontrol = new_maccontrol;
  1195. /* write the new values with overrides applied */
  1196. brcms_c_mctrl_write(wlc_hw);
  1197. }
  1198. void brcms_c_ucode_wake_override_set(struct brcms_hardware *wlc_hw,
  1199. u32 override_bit)
  1200. {
  1201. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE)) {
  1202. mboolset(wlc_hw->wake_override, override_bit);
  1203. return;
  1204. }
  1205. mboolset(wlc_hw->wake_override, override_bit);
  1206. brcms_c_mctrl_write(wlc_hw);
  1207. brcms_b_wait_for_wake(wlc_hw);
  1208. }
  1209. void brcms_c_ucode_wake_override_clear(struct brcms_hardware *wlc_hw,
  1210. u32 override_bit)
  1211. {
  1212. mboolclr(wlc_hw->wake_override, override_bit);
  1213. if (wlc_hw->wake_override || (wlc_hw->maccontrol & MCTL_WAKE))
  1214. return;
  1215. brcms_c_mctrl_write(wlc_hw);
  1216. }
  1217. /* When driver needs ucode to stop beaconing, it has to make sure that
  1218. * MCTL_AP is clear and MCTL_INFRA is set
  1219. * Mode MCTL_AP MCTL_INFRA
  1220. * AP 1 1
  1221. * STA 0 1 <--- This will ensure no beacons
  1222. * IBSS 0 0
  1223. */
  1224. static void brcms_c_ucode_mute_override_set(struct brcms_hardware *wlc_hw)
  1225. {
  1226. wlc_hw->mute_override = 1;
  1227. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1228. * override, then there is no change to write
  1229. */
  1230. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1231. return;
  1232. brcms_c_mctrl_write(wlc_hw);
  1233. }
  1234. /* Clear the override on AP and INFRA bits */
  1235. static void brcms_c_ucode_mute_override_clear(struct brcms_hardware *wlc_hw)
  1236. {
  1237. if (wlc_hw->mute_override == 0)
  1238. return;
  1239. wlc_hw->mute_override = 0;
  1240. /* if maccontrol already has AP == 0 and INFRA == 1 without this
  1241. * override, then there is no change to write
  1242. */
  1243. if ((wlc_hw->maccontrol & (MCTL_AP | MCTL_INFRA)) == MCTL_INFRA)
  1244. return;
  1245. brcms_c_mctrl_write(wlc_hw);
  1246. }
  1247. /*
  1248. * Write a MAC address to the given match reg offset in the RXE match engine.
  1249. */
  1250. static void
  1251. brcms_b_set_addrmatch(struct brcms_hardware *wlc_hw, int match_reg_offset,
  1252. const u8 *addr)
  1253. {
  1254. struct bcma_device *core = wlc_hw->d11core;
  1255. u16 mac_l;
  1256. u16 mac_m;
  1257. u16 mac_h;
  1258. BCMMSG(wlc_hw->wlc->wiphy, "wl%d: brcms_b_set_addrmatch\n",
  1259. wlc_hw->unit);
  1260. mac_l = addr[0] | (addr[1] << 8);
  1261. mac_m = addr[2] | (addr[3] << 8);
  1262. mac_h = addr[4] | (addr[5] << 8);
  1263. /* enter the MAC addr into the RXE match registers */
  1264. bcma_write16(core, D11REGOFFS(rcm_ctl),
  1265. RCM_INC_DATA | match_reg_offset);
  1266. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_l);
  1267. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_m);
  1268. bcma_write16(core, D11REGOFFS(rcm_mat_data), mac_h);
  1269. }
  1270. void
  1271. brcms_b_write_template_ram(struct brcms_hardware *wlc_hw, int offset, int len,
  1272. void *buf)
  1273. {
  1274. struct bcma_device *core = wlc_hw->d11core;
  1275. u32 word;
  1276. __le32 word_le;
  1277. __be32 word_be;
  1278. bool be_bit;
  1279. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  1280. bcma_write32(core, D11REGOFFS(tplatewrptr), offset);
  1281. /* if MCTL_BIGEND bit set in mac control register,
  1282. * the chip swaps data in fifo, as well as data in
  1283. * template ram
  1284. */
  1285. be_bit = (bcma_read32(core, D11REGOFFS(maccontrol)) & MCTL_BIGEND) != 0;
  1286. while (len > 0) {
  1287. memcpy(&word, buf, sizeof(u32));
  1288. if (be_bit) {
  1289. word_be = cpu_to_be32(word);
  1290. word = *(u32 *)&word_be;
  1291. } else {
  1292. word_le = cpu_to_le32(word);
  1293. word = *(u32 *)&word_le;
  1294. }
  1295. bcma_write32(core, D11REGOFFS(tplatewrdata), word);
  1296. buf = (u8 *) buf + sizeof(u32);
  1297. len -= sizeof(u32);
  1298. }
  1299. }
  1300. static void brcms_b_set_cwmin(struct brcms_hardware *wlc_hw, u16 newmin)
  1301. {
  1302. wlc_hw->band->CWmin = newmin;
  1303. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1304. OBJADDR_SCR_SEL | S_DOT11_CWMIN);
  1305. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1306. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmin);
  1307. }
  1308. static void brcms_b_set_cwmax(struct brcms_hardware *wlc_hw, u16 newmax)
  1309. {
  1310. wlc_hw->band->CWmax = newmax;
  1311. bcma_write32(wlc_hw->d11core, D11REGOFFS(objaddr),
  1312. OBJADDR_SCR_SEL | S_DOT11_CWMAX);
  1313. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(objaddr));
  1314. bcma_write32(wlc_hw->d11core, D11REGOFFS(objdata), newmax);
  1315. }
  1316. void brcms_b_bw_set(struct brcms_hardware *wlc_hw, u16 bw)
  1317. {
  1318. bool fastclk;
  1319. /* request FAST clock if not on */
  1320. fastclk = wlc_hw->forcefastclk;
  1321. if (!fastclk)
  1322. brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
  1323. wlc_phy_bw_state_set(wlc_hw->band->pi, bw);
  1324. brcms_b_phy_reset(wlc_hw);
  1325. wlc_phy_init(wlc_hw->band->pi, wlc_phy_chanspec_get(wlc_hw->band->pi));
  1326. /* restore the clk */
  1327. if (!fastclk)
  1328. brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
  1329. }
  1330. static void brcms_b_upd_synthpu(struct brcms_hardware *wlc_hw)
  1331. {
  1332. u16 v;
  1333. struct brcms_c_info *wlc = wlc_hw->wlc;
  1334. /* update SYNTHPU_DLY */
  1335. if (BRCMS_ISLCNPHY(wlc->band))
  1336. v = SYNTHPU_DLY_LPPHY_US;
  1337. else if (BRCMS_ISNPHY(wlc->band) && (NREV_GE(wlc->band->phyrev, 3)))
  1338. v = SYNTHPU_DLY_NPHY_US;
  1339. else
  1340. v = SYNTHPU_DLY_BPHY_US;
  1341. brcms_b_write_shm(wlc_hw, M_SYNTHPU_DLY, v);
  1342. }
  1343. static void brcms_c_ucode_txant_set(struct brcms_hardware *wlc_hw)
  1344. {
  1345. u16 phyctl;
  1346. u16 phytxant = wlc_hw->bmac_phytxant;
  1347. u16 mask = PHY_TXC_ANT_MASK;
  1348. /* set the Probe Response frame phy control word */
  1349. phyctl = brcms_b_read_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS);
  1350. phyctl = (phyctl & ~mask) | phytxant;
  1351. brcms_b_write_shm(wlc_hw, M_CTXPRS_BLK + C_CTX_PCTLWD_POS, phyctl);
  1352. /* set the Response (ACK/CTS) frame phy control word */
  1353. phyctl = brcms_b_read_shm(wlc_hw, M_RSP_PCTLWD);
  1354. phyctl = (phyctl & ~mask) | phytxant;
  1355. brcms_b_write_shm(wlc_hw, M_RSP_PCTLWD, phyctl);
  1356. }
  1357. static u16 brcms_b_ofdm_ratetable_offset(struct brcms_hardware *wlc_hw,
  1358. u8 rate)
  1359. {
  1360. uint i;
  1361. u8 plcp_rate = 0;
  1362. struct plcp_signal_rate_lookup {
  1363. u8 rate;
  1364. u8 signal_rate;
  1365. };
  1366. /* OFDM RATE sub-field of PLCP SIGNAL field, per 802.11 sec 17.3.4.1 */
  1367. const struct plcp_signal_rate_lookup rate_lookup[] = {
  1368. {BRCM_RATE_6M, 0xB},
  1369. {BRCM_RATE_9M, 0xF},
  1370. {BRCM_RATE_12M, 0xA},
  1371. {BRCM_RATE_18M, 0xE},
  1372. {BRCM_RATE_24M, 0x9},
  1373. {BRCM_RATE_36M, 0xD},
  1374. {BRCM_RATE_48M, 0x8},
  1375. {BRCM_RATE_54M, 0xC}
  1376. };
  1377. for (i = 0; i < ARRAY_SIZE(rate_lookup); i++) {
  1378. if (rate == rate_lookup[i].rate) {
  1379. plcp_rate = rate_lookup[i].signal_rate;
  1380. break;
  1381. }
  1382. }
  1383. /* Find the SHM pointer to the rate table entry by looking in the
  1384. * Direct-map Table
  1385. */
  1386. return 2 * brcms_b_read_shm(wlc_hw, M_RT_DIRMAP_A + (plcp_rate * 2));
  1387. }
  1388. static void brcms_upd_ofdm_pctl1_table(struct brcms_hardware *wlc_hw)
  1389. {
  1390. u8 rate;
  1391. u8 rates[8] = {
  1392. BRCM_RATE_6M, BRCM_RATE_9M, BRCM_RATE_12M, BRCM_RATE_18M,
  1393. BRCM_RATE_24M, BRCM_RATE_36M, BRCM_RATE_48M, BRCM_RATE_54M
  1394. };
  1395. u16 entry_ptr;
  1396. u16 pctl1;
  1397. uint i;
  1398. if (!BRCMS_PHY_11N_CAP(wlc_hw->band))
  1399. return;
  1400. /* walk the phy rate table and update the entries */
  1401. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  1402. rate = rates[i];
  1403. entry_ptr = brcms_b_ofdm_ratetable_offset(wlc_hw, rate);
  1404. /* read the SHM Rate Table entry OFDM PCTL1 values */
  1405. pctl1 =
  1406. brcms_b_read_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS);
  1407. /* modify the value */
  1408. pctl1 &= ~PHY_TXC1_MODE_MASK;
  1409. pctl1 |= (wlc_hw->hw_stf_ss_opmode << PHY_TXC1_MODE_SHIFT);
  1410. /* Update the SHM Rate Table entry OFDM PCTL1 values */
  1411. brcms_b_write_shm(wlc_hw, entry_ptr + M_RT_OFDM_PCTL1_POS,
  1412. pctl1);
  1413. }
  1414. }
  1415. /* band-specific init */
  1416. static void brcms_b_bsinit(struct brcms_c_info *wlc, u16 chanspec)
  1417. {
  1418. struct brcms_hardware *wlc_hw = wlc->hw;
  1419. BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
  1420. wlc_hw->band->bandunit);
  1421. brcms_c_ucode_bsinit(wlc_hw);
  1422. wlc_phy_init(wlc_hw->band->pi, chanspec);
  1423. brcms_c_ucode_txant_set(wlc_hw);
  1424. /*
  1425. * cwmin is band-specific, update hardware
  1426. * with value for current band
  1427. */
  1428. brcms_b_set_cwmin(wlc_hw, wlc_hw->band->CWmin);
  1429. brcms_b_set_cwmax(wlc_hw, wlc_hw->band->CWmax);
  1430. brcms_b_update_slot_timing(wlc_hw,
  1431. wlc_hw->band->bandtype == BRCM_BAND_5G ?
  1432. true : wlc_hw->shortslot);
  1433. /* write phytype and phyvers */
  1434. brcms_b_write_shm(wlc_hw, M_PHYTYPE, (u16) wlc_hw->band->phytype);
  1435. brcms_b_write_shm(wlc_hw, M_PHYVER, (u16) wlc_hw->band->phyrev);
  1436. /*
  1437. * initialize the txphyctl1 rate table since
  1438. * shmem is shared between bands
  1439. */
  1440. brcms_upd_ofdm_pctl1_table(wlc_hw);
  1441. brcms_b_upd_synthpu(wlc_hw);
  1442. }
  1443. /* Perform a soft reset of the PHY PLL */
  1444. void brcms_b_core_phypll_reset(struct brcms_hardware *wlc_hw)
  1445. {
  1446. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  1447. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_addr),
  1448. ~0, 0);
  1449. udelay(1);
  1450. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1451. 0x4, 0);
  1452. udelay(1);
  1453. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1454. 0x4, 4);
  1455. udelay(1);
  1456. ai_cc_reg(wlc_hw->sih, offsetof(struct chipcregs, chipcontrol_data),
  1457. 0x4, 0);
  1458. udelay(1);
  1459. }
  1460. /* light way to turn on phy clock without reset for NPHY only
  1461. * refer to brcms_b_core_phy_clk for full version
  1462. */
  1463. void brcms_b_phyclk_fgc(struct brcms_hardware *wlc_hw, bool clk)
  1464. {
  1465. /* support(necessary for NPHY and HYPHY) only */
  1466. if (!BRCMS_ISNPHY(wlc_hw->band))
  1467. return;
  1468. if (ON == clk)
  1469. brcms_b_core_ioctl(wlc_hw, SICF_FGC, SICF_FGC);
  1470. else
  1471. brcms_b_core_ioctl(wlc_hw, SICF_FGC, 0);
  1472. }
  1473. void brcms_b_macphyclk_set(struct brcms_hardware *wlc_hw, bool clk)
  1474. {
  1475. if (ON == clk)
  1476. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, SICF_MPCLKE);
  1477. else
  1478. brcms_b_core_ioctl(wlc_hw, SICF_MPCLKE, 0);
  1479. }
  1480. void brcms_b_phy_reset(struct brcms_hardware *wlc_hw)
  1481. {
  1482. struct brcms_phy_pub *pih = wlc_hw->band->pi;
  1483. u32 phy_bw_clkbits;
  1484. bool phy_in_reset = false;
  1485. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  1486. if (pih == NULL)
  1487. return;
  1488. phy_bw_clkbits = wlc_phy_clk_bwbits(wlc_hw->band->pi);
  1489. /* Specific reset sequence required for NPHY rev 3 and 4 */
  1490. if (BRCMS_ISNPHY(wlc_hw->band) && NREV_GE(wlc_hw->band->phyrev, 3) &&
  1491. NREV_LE(wlc_hw->band->phyrev, 4)) {
  1492. /* Set the PHY bandwidth */
  1493. brcms_b_core_ioctl(wlc_hw, SICF_BWMASK, phy_bw_clkbits);
  1494. udelay(1);
  1495. /* Perform a soft reset of the PHY PLL */
  1496. brcms_b_core_phypll_reset(wlc_hw);
  1497. /* reset the PHY */
  1498. brcms_b_core_ioctl(wlc_hw, (SICF_PRST | SICF_PCLKE),
  1499. (SICF_PRST | SICF_PCLKE));
  1500. phy_in_reset = true;
  1501. } else {
  1502. brcms_b_core_ioctl(wlc_hw,
  1503. (SICF_PRST | SICF_PCLKE | SICF_BWMASK),
  1504. (SICF_PRST | SICF_PCLKE | phy_bw_clkbits));
  1505. }
  1506. udelay(2);
  1507. brcms_b_core_phy_clk(wlc_hw, ON);
  1508. if (pih)
  1509. wlc_phy_anacore(pih, ON);
  1510. }
  1511. /* switch to and initialize new band */
  1512. static void brcms_b_setband(struct brcms_hardware *wlc_hw, uint bandunit,
  1513. u16 chanspec) {
  1514. struct brcms_c_info *wlc = wlc_hw->wlc;
  1515. u32 macintmask;
  1516. /* Enable the d11 core before accessing it */
  1517. if (!bcma_core_is_enabled(wlc_hw->d11core)) {
  1518. bcma_core_enable(wlc_hw->d11core, 0);
  1519. brcms_c_mctrl_reset(wlc_hw);
  1520. }
  1521. macintmask = brcms_c_setband_inact(wlc, bandunit);
  1522. if (!wlc_hw->up)
  1523. return;
  1524. brcms_b_core_phy_clk(wlc_hw, ON);
  1525. /* band-specific initializations */
  1526. brcms_b_bsinit(wlc, chanspec);
  1527. /*
  1528. * If there are any pending software interrupt bits,
  1529. * then replace these with a harmless nonzero value
  1530. * so brcms_c_dpc() will re-enable interrupts when done.
  1531. */
  1532. if (wlc->macintstatus)
  1533. wlc->macintstatus = MI_DMAINT;
  1534. /* restore macintmask */
  1535. brcms_intrsrestore(wlc->wl, macintmask);
  1536. /* ucode should still be suspended.. */
  1537. WARN_ON((bcma_read32(wlc_hw->d11core, D11REGOFFS(maccontrol)) &
  1538. MCTL_EN_MAC) != 0);
  1539. }
  1540. static bool brcms_c_isgoodchip(struct brcms_hardware *wlc_hw)
  1541. {
  1542. /* reject unsupported corerev */
  1543. if (!CONF_HAS(D11CONF, wlc_hw->corerev)) {
  1544. wiphy_err(wlc_hw->wlc->wiphy, "unsupported core rev %d\n",
  1545. wlc_hw->corerev);
  1546. return false;
  1547. }
  1548. return true;
  1549. }
  1550. /* Validate some board info parameters */
  1551. static bool brcms_c_validboardtype(struct brcms_hardware *wlc_hw)
  1552. {
  1553. uint boardrev = wlc_hw->boardrev;
  1554. /* 4 bits each for board type, major, minor, and tiny version */
  1555. uint brt = (boardrev & 0xf000) >> 12;
  1556. uint b0 = (boardrev & 0xf00) >> 8;
  1557. uint b1 = (boardrev & 0xf0) >> 4;
  1558. uint b2 = boardrev & 0xf;
  1559. /* voards from other vendors are always considered valid */
  1560. if (ai_get_boardvendor(wlc_hw->sih) != PCI_VENDOR_ID_BROADCOM)
  1561. return true;
  1562. /* do some boardrev sanity checks when boardvendor is Broadcom */
  1563. if (boardrev == 0)
  1564. return false;
  1565. if (boardrev <= 0xff)
  1566. return true;
  1567. if ((brt > 2) || (brt == 0) || (b0 > 9) || (b0 == 0) || (b1 > 9)
  1568. || (b2 > 9))
  1569. return false;
  1570. return true;
  1571. }
  1572. static char *brcms_c_get_macaddr(struct brcms_hardware *wlc_hw)
  1573. {
  1574. enum brcms_srom_id var_id = BRCMS_SROM_MACADDR;
  1575. char *macaddr;
  1576. /* If macaddr exists, use it (Sromrev4, CIS, ...). */
  1577. macaddr = getvar(wlc_hw->sih, var_id);
  1578. if (macaddr != NULL)
  1579. return macaddr;
  1580. if (wlc_hw->_nbands > 1)
  1581. var_id = BRCMS_SROM_ET1MACADDR;
  1582. else
  1583. var_id = BRCMS_SROM_IL0MACADDR;
  1584. macaddr = getvar(wlc_hw->sih, var_id);
  1585. if (macaddr == NULL)
  1586. wiphy_err(wlc_hw->wlc->wiphy, "wl%d: wlc_get_macaddr: macaddr "
  1587. "getvar(%d) not found\n", wlc_hw->unit, var_id);
  1588. return macaddr;
  1589. }
  1590. /* power both the pll and external oscillator on/off */
  1591. static void brcms_b_xtal(struct brcms_hardware *wlc_hw, bool want)
  1592. {
  1593. BCMMSG(wlc_hw->wlc->wiphy, "wl%d: want %d\n", wlc_hw->unit, want);
  1594. /*
  1595. * dont power down if plldown is false or
  1596. * we must poll hw radio disable
  1597. */
  1598. if (!want && wlc_hw->pllreq)
  1599. return;
  1600. if (wlc_hw->sih)
  1601. ai_clkctl_xtal(wlc_hw->sih, XTAL | PLL, want);
  1602. wlc_hw->sbclk = want;
  1603. if (!wlc_hw->sbclk) {
  1604. wlc_hw->clk = false;
  1605. if (wlc_hw->band && wlc_hw->band->pi)
  1606. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  1607. }
  1608. }
  1609. /*
  1610. * Return true if radio is disabled, otherwise false.
  1611. * hw radio disable signal is an external pin, users activate it asynchronously
  1612. * this function could be called when driver is down and w/o clock
  1613. * it operates on different registers depending on corerev and boardflag.
  1614. */
  1615. static bool brcms_b_radio_read_hwdisabled(struct brcms_hardware *wlc_hw)
  1616. {
  1617. bool v, clk, xtal;
  1618. u32 flags = 0;
  1619. xtal = wlc_hw->sbclk;
  1620. if (!xtal)
  1621. brcms_b_xtal(wlc_hw, ON);
  1622. /* may need to take core out of reset first */
  1623. clk = wlc_hw->clk;
  1624. if (!clk) {
  1625. /*
  1626. * mac no longer enables phyclk automatically when driver
  1627. * accesses phyreg throughput mac. This can be skipped since
  1628. * only mac reg is accessed below
  1629. */
  1630. flags |= SICF_PCLKE;
  1631. /*
  1632. * TODO: test suspend/resume
  1633. *
  1634. * AI chip doesn't restore bar0win2 on
  1635. * hibernation/resume, need sw fixup
  1636. */
  1637. bcma_core_enable(wlc_hw->d11core, flags);
  1638. brcms_c_mctrl_reset(wlc_hw);
  1639. }
  1640. v = ((bcma_read32(wlc_hw->d11core,
  1641. D11REGOFFS(phydebug)) & PDBG_RFD) != 0);
  1642. /* put core back into reset */
  1643. if (!clk)
  1644. bcma_core_disable(wlc_hw->d11core, 0);
  1645. if (!xtal)
  1646. brcms_b_xtal(wlc_hw, OFF);
  1647. return v;
  1648. }
  1649. static bool wlc_dma_rxreset(struct brcms_hardware *wlc_hw, uint fifo)
  1650. {
  1651. struct dma_pub *di = wlc_hw->di[fifo];
  1652. return dma_rxreset(di);
  1653. }
  1654. /* d11 core reset
  1655. * ensure fask clock during reset
  1656. * reset dma
  1657. * reset d11(out of reset)
  1658. * reset phy(out of reset)
  1659. * clear software macintstatus for fresh new start
  1660. * one testing hack wlc_hw->noreset will bypass the d11/phy reset
  1661. */
  1662. void brcms_b_corereset(struct brcms_hardware *wlc_hw, u32 flags)
  1663. {
  1664. uint i;
  1665. bool fastclk;
  1666. if (flags == BRCMS_USE_COREFLAGS)
  1667. flags = (wlc_hw->band->pi ? wlc_hw->band->core_flags : 0);
  1668. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  1669. /* request FAST clock if not on */
  1670. fastclk = wlc_hw->forcefastclk;
  1671. if (!fastclk)
  1672. brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
  1673. /* reset the dma engines except first time thru */
  1674. if (bcma_core_is_enabled(wlc_hw->d11core)) {
  1675. for (i = 0; i < NFIFO; i++)
  1676. if ((wlc_hw->di[i]) && (!dma_txreset(wlc_hw->di[i])))
  1677. wiphy_err(wlc_hw->wlc->wiphy, "wl%d: %s: "
  1678. "dma_txreset[%d]: cannot stop dma\n",
  1679. wlc_hw->unit, __func__, i);
  1680. if ((wlc_hw->di[RX_FIFO])
  1681. && (!wlc_dma_rxreset(wlc_hw, RX_FIFO)))
  1682. wiphy_err(wlc_hw->wlc->wiphy, "wl%d: %s: dma_rxreset"
  1683. "[%d]: cannot stop dma\n",
  1684. wlc_hw->unit, __func__, RX_FIFO);
  1685. }
  1686. /* if noreset, just stop the psm and return */
  1687. if (wlc_hw->noreset) {
  1688. wlc_hw->wlc->macintstatus = 0; /* skip wl_dpc after down */
  1689. brcms_b_mctrl(wlc_hw, MCTL_PSM_RUN | MCTL_EN_MAC, 0);
  1690. return;
  1691. }
  1692. /*
  1693. * mac no longer enables phyclk automatically when driver accesses
  1694. * phyreg throughput mac, AND phy_reset is skipped at early stage when
  1695. * band->pi is invalid. need to enable PHY CLK
  1696. */
  1697. flags |= SICF_PCLKE;
  1698. /*
  1699. * reset the core
  1700. * In chips with PMU, the fastclk request goes through d11 core
  1701. * reg 0x1e0, which is cleared by the core_reset. have to re-request it.
  1702. *
  1703. * This adds some delay and we can optimize it by also requesting
  1704. * fastclk through chipcommon during this period if necessary. But
  1705. * that has to work coordinate with other driver like mips/arm since
  1706. * they may touch chipcommon as well.
  1707. */
  1708. wlc_hw->clk = false;
  1709. bcma_core_enable(wlc_hw->d11core, flags);
  1710. wlc_hw->clk = true;
  1711. if (wlc_hw->band && wlc_hw->band->pi)
  1712. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, true);
  1713. brcms_c_mctrl_reset(wlc_hw);
  1714. if (ai_get_cccaps(wlc_hw->sih) & CC_CAP_PMU)
  1715. brcms_b_clkctl_clk(wlc_hw, CLK_FAST);
  1716. brcms_b_phy_reset(wlc_hw);
  1717. /* turn on PHY_PLL */
  1718. brcms_b_core_phypll_ctl(wlc_hw, true);
  1719. /* clear sw intstatus */
  1720. wlc_hw->wlc->macintstatus = 0;
  1721. /* restore the clk setting */
  1722. if (!fastclk)
  1723. brcms_b_clkctl_clk(wlc_hw, CLK_DYNAMIC);
  1724. }
  1725. /* txfifo sizes needs to be modified(increased) since the newer cores
  1726. * have more memory.
  1727. */
  1728. static void brcms_b_corerev_fifofixup(struct brcms_hardware *wlc_hw)
  1729. {
  1730. struct bcma_device *core = wlc_hw->d11core;
  1731. u16 fifo_nu;
  1732. u16 txfifo_startblk = TXFIFO_START_BLK, txfifo_endblk;
  1733. u16 txfifo_def, txfifo_def1;
  1734. u16 txfifo_cmd;
  1735. /* tx fifos start at TXFIFO_START_BLK from the Base address */
  1736. txfifo_startblk = TXFIFO_START_BLK;
  1737. /* sequence of operations: reset fifo, set fifo size, reset fifo */
  1738. for (fifo_nu = 0; fifo_nu < NFIFO; fifo_nu++) {
  1739. txfifo_endblk = txfifo_startblk + wlc_hw->xmtfifo_sz[fifo_nu];
  1740. txfifo_def = (txfifo_startblk & 0xff) |
  1741. (((txfifo_endblk - 1) & 0xff) << TXFIFO_FIFOTOP_SHIFT);
  1742. txfifo_def1 = ((txfifo_startblk >> 8) & 0x1) |
  1743. ((((txfifo_endblk -
  1744. 1) >> 8) & 0x1) << TXFIFO_FIFOTOP_SHIFT);
  1745. txfifo_cmd =
  1746. TXFIFOCMD_RESET_MASK | (fifo_nu << TXFIFOCMD_FIFOSEL_SHIFT);
  1747. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1748. bcma_write16(core, D11REGOFFS(xmtfifodef), txfifo_def);
  1749. bcma_write16(core, D11REGOFFS(xmtfifodef1), txfifo_def1);
  1750. bcma_write16(core, D11REGOFFS(xmtfifocmd), txfifo_cmd);
  1751. txfifo_startblk += wlc_hw->xmtfifo_sz[fifo_nu];
  1752. }
  1753. /*
  1754. * need to propagate to shm location to be in sync since ucode/hw won't
  1755. * do this
  1756. */
  1757. brcms_b_write_shm(wlc_hw, M_FIFOSIZE0,
  1758. wlc_hw->xmtfifo_sz[TX_AC_BE_FIFO]);
  1759. brcms_b_write_shm(wlc_hw, M_FIFOSIZE1,
  1760. wlc_hw->xmtfifo_sz[TX_AC_VI_FIFO]);
  1761. brcms_b_write_shm(wlc_hw, M_FIFOSIZE2,
  1762. ((wlc_hw->xmtfifo_sz[TX_AC_VO_FIFO] << 8) | wlc_hw->
  1763. xmtfifo_sz[TX_AC_BK_FIFO]));
  1764. brcms_b_write_shm(wlc_hw, M_FIFOSIZE3,
  1765. ((wlc_hw->xmtfifo_sz[TX_ATIM_FIFO] << 8) | wlc_hw->
  1766. xmtfifo_sz[TX_BCMC_FIFO]));
  1767. }
  1768. /* This function is used for changing the tsf frac register
  1769. * If spur avoidance mode is off, the mac freq will be 80/120/160Mhz
  1770. * If spur avoidance mode is on1, the mac freq will be 82/123/164Mhz
  1771. * If spur avoidance mode is on2, the mac freq will be 84/126/168Mhz
  1772. * HTPHY Formula is 2^26/freq(MHz) e.g.
  1773. * For spuron2 - 126MHz -> 2^26/126 = 532610.0
  1774. * - 532610 = 0x82082 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x2082
  1775. * For spuron: 123MHz -> 2^26/123 = 545600.5
  1776. * - 545601 = 0x85341 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x5341
  1777. * For spur off: 120MHz -> 2^26/120 = 559240.5
  1778. * - 559241 = 0x88889 => tsf_clk_frac_h = 0x8, tsf_clk_frac_l = 0x8889
  1779. */
  1780. void brcms_b_switch_macfreq(struct brcms_hardware *wlc_hw, u8 spurmode)
  1781. {
  1782. struct bcma_device *core = wlc_hw->d11core;
  1783. if ((ai_get_chip_id(wlc_hw->sih) == BCM43224_CHIP_ID) ||
  1784. (ai_get_chip_id(wlc_hw->sih) == BCM43225_CHIP_ID)) {
  1785. if (spurmode == WL_SPURAVOID_ON2) { /* 126Mhz */
  1786. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x2082);
  1787. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1788. } else if (spurmode == WL_SPURAVOID_ON1) { /* 123Mhz */
  1789. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x5341);
  1790. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1791. } else { /* 120Mhz */
  1792. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x8889);
  1793. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0x8);
  1794. }
  1795. } else if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1796. if (spurmode == WL_SPURAVOID_ON1) { /* 82Mhz */
  1797. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0x7CE0);
  1798. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1799. } else { /* 80Mhz */
  1800. bcma_write16(core, D11REGOFFS(tsf_clk_frac_l), 0xCCCD);
  1801. bcma_write16(core, D11REGOFFS(tsf_clk_frac_h), 0xC);
  1802. }
  1803. }
  1804. }
  1805. /* Initialize GPIOs that are controlled by D11 core */
  1806. static void brcms_c_gpio_init(struct brcms_c_info *wlc)
  1807. {
  1808. struct brcms_hardware *wlc_hw = wlc->hw;
  1809. u32 gc, gm;
  1810. /* use GPIO select 0 to get all gpio signals from the gpio out reg */
  1811. brcms_b_mctrl(wlc_hw, MCTL_GPOUT_SEL_MASK, 0);
  1812. /*
  1813. * Common GPIO setup:
  1814. * G0 = LED 0 = WLAN Activity
  1815. * G1 = LED 1 = WLAN 2.4 GHz Radio State
  1816. * G2 = LED 2 = WLAN 5 GHz Radio State
  1817. * G4 = radio disable input (HI enabled, LO disabled)
  1818. */
  1819. gc = gm = 0;
  1820. /* Allocate GPIOs for mimo antenna diversity feature */
  1821. if (wlc_hw->antsel_type == ANTSEL_2x3) {
  1822. /* Enable antenna diversity, use 2x3 mode */
  1823. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1824. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1825. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE,
  1826. MHF3_ANTSEL_MODE, BRCM_BAND_ALL);
  1827. /* init superswitch control */
  1828. wlc_phy_antsel_init(wlc_hw->band->pi, false);
  1829. } else if (wlc_hw->antsel_type == ANTSEL_2x4) {
  1830. gm |= gc |= (BOARD_GPIO_12 | BOARD_GPIO_13);
  1831. /*
  1832. * The board itself is powered by these GPIOs
  1833. * (when not sending pattern) so set them high
  1834. */
  1835. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_oe),
  1836. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1837. bcma_set16(wlc_hw->d11core, D11REGOFFS(psm_gpio_out),
  1838. (BOARD_GPIO_12 | BOARD_GPIO_13));
  1839. /* Enable antenna diversity, use 2x4 mode */
  1840. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_EN,
  1841. MHF3_ANTSEL_EN, BRCM_BAND_ALL);
  1842. brcms_b_mhf(wlc_hw, MHF3, MHF3_ANTSEL_MODE, 0,
  1843. BRCM_BAND_ALL);
  1844. /* Configure the desired clock to be 4Mhz */
  1845. brcms_b_write_shm(wlc_hw, M_ANTSEL_CLKDIV,
  1846. ANTSEL_CLKDIV_4MHZ);
  1847. }
  1848. /*
  1849. * gpio 9 controls the PA. ucode is responsible
  1850. * for wiggling out and oe
  1851. */
  1852. if (wlc_hw->boardflags & BFL_PACTRL)
  1853. gm |= gc |= BOARD_GPIO_PACTRL;
  1854. /* apply to gpiocontrol register */
  1855. ai_gpiocontrol(wlc_hw->sih, gm, gc, GPIO_DRV_PRIORITY);
  1856. }
  1857. static void brcms_ucode_write(struct brcms_hardware *wlc_hw,
  1858. const __le32 ucode[], const size_t nbytes)
  1859. {
  1860. struct bcma_device *core = wlc_hw->d11core;
  1861. uint i;
  1862. uint count;
  1863. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  1864. count = (nbytes / sizeof(u32));
  1865. bcma_write32(core, D11REGOFFS(objaddr),
  1866. OBJADDR_AUTO_INC | OBJADDR_UCM_SEL);
  1867. (void)bcma_read32(core, D11REGOFFS(objaddr));
  1868. for (i = 0; i < count; i++)
  1869. bcma_write32(core, D11REGOFFS(objdata), le32_to_cpu(ucode[i]));
  1870. }
  1871. static void brcms_ucode_download(struct brcms_hardware *wlc_hw)
  1872. {
  1873. struct brcms_c_info *wlc;
  1874. struct brcms_ucode *ucode = &wlc_hw->wlc->wl->ucode;
  1875. wlc = wlc_hw->wlc;
  1876. if (wlc_hw->ucode_loaded)
  1877. return;
  1878. if (D11REV_IS(wlc_hw->corerev, 23)) {
  1879. if (BRCMS_ISNPHY(wlc_hw->band)) {
  1880. brcms_ucode_write(wlc_hw, ucode->bcm43xx_16_mimo,
  1881. ucode->bcm43xx_16_mimosz);
  1882. wlc_hw->ucode_loaded = true;
  1883. } else
  1884. wiphy_err(wlc->wiphy, "%s: wl%d: unsupported phy in "
  1885. "corerev %d\n",
  1886. __func__, wlc_hw->unit, wlc_hw->corerev);
  1887. } else if (D11REV_IS(wlc_hw->corerev, 24)) {
  1888. if (BRCMS_ISLCNPHY(wlc_hw->band)) {
  1889. brcms_ucode_write(wlc_hw, ucode->bcm43xx_24_lcn,
  1890. ucode->bcm43xx_24_lcnsz);
  1891. wlc_hw->ucode_loaded = true;
  1892. } else {
  1893. wiphy_err(wlc->wiphy, "%s: wl%d: unsupported phy in "
  1894. "corerev %d\n",
  1895. __func__, wlc_hw->unit, wlc_hw->corerev);
  1896. }
  1897. }
  1898. }
  1899. void brcms_b_txant_set(struct brcms_hardware *wlc_hw, u16 phytxant)
  1900. {
  1901. /* update sw state */
  1902. wlc_hw->bmac_phytxant = phytxant;
  1903. /* push to ucode if up */
  1904. if (!wlc_hw->up)
  1905. return;
  1906. brcms_c_ucode_txant_set(wlc_hw);
  1907. }
  1908. u16 brcms_b_get_txant(struct brcms_hardware *wlc_hw)
  1909. {
  1910. return (u16) wlc_hw->wlc->stf->txant;
  1911. }
  1912. void brcms_b_antsel_type_set(struct brcms_hardware *wlc_hw, u8 antsel_type)
  1913. {
  1914. wlc_hw->antsel_type = antsel_type;
  1915. /* Update the antsel type for phy module to use */
  1916. wlc_phy_antsel_type_set(wlc_hw->band->pi, antsel_type);
  1917. }
  1918. static void brcms_b_fifoerrors(struct brcms_hardware *wlc_hw)
  1919. {
  1920. bool fatal = false;
  1921. uint unit;
  1922. uint intstatus, idx;
  1923. struct bcma_device *core = wlc_hw->d11core;
  1924. struct wiphy *wiphy = wlc_hw->wlc->wiphy;
  1925. unit = wlc_hw->unit;
  1926. for (idx = 0; idx < NFIFO; idx++) {
  1927. /* read intstatus register and ignore any non-error bits */
  1928. intstatus =
  1929. bcma_read32(core,
  1930. D11REGOFFS(intctrlregs[idx].intstatus)) &
  1931. I_ERRORS;
  1932. if (!intstatus)
  1933. continue;
  1934. BCMMSG(wlc_hw->wlc->wiphy, "wl%d: intstatus%d 0x%x\n",
  1935. unit, idx, intstatus);
  1936. if (intstatus & I_RO) {
  1937. wiphy_err(wiphy, "wl%d: fifo %d: receive fifo "
  1938. "overflow\n", unit, idx);
  1939. fatal = true;
  1940. }
  1941. if (intstatus & I_PC) {
  1942. wiphy_err(wiphy, "wl%d: fifo %d: descriptor error\n",
  1943. unit, idx);
  1944. fatal = true;
  1945. }
  1946. if (intstatus & I_PD) {
  1947. wiphy_err(wiphy, "wl%d: fifo %d: data error\n", unit,
  1948. idx);
  1949. fatal = true;
  1950. }
  1951. if (intstatus & I_DE) {
  1952. wiphy_err(wiphy, "wl%d: fifo %d: descriptor protocol "
  1953. "error\n", unit, idx);
  1954. fatal = true;
  1955. }
  1956. if (intstatus & I_RU)
  1957. wiphy_err(wiphy, "wl%d: fifo %d: receive descriptor "
  1958. "underflow\n", idx, unit);
  1959. if (intstatus & I_XU) {
  1960. wiphy_err(wiphy, "wl%d: fifo %d: transmit fifo "
  1961. "underflow\n", idx, unit);
  1962. fatal = true;
  1963. }
  1964. if (fatal) {
  1965. brcms_fatal_error(wlc_hw->wlc->wl); /* big hammer */
  1966. break;
  1967. } else
  1968. bcma_write32(core,
  1969. D11REGOFFS(intctrlregs[idx].intstatus),
  1970. intstatus);
  1971. }
  1972. }
  1973. void brcms_c_intrson(struct brcms_c_info *wlc)
  1974. {
  1975. struct brcms_hardware *wlc_hw = wlc->hw;
  1976. wlc->macintmask = wlc->defmacintmask;
  1977. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  1978. }
  1979. u32 brcms_c_intrsoff(struct brcms_c_info *wlc)
  1980. {
  1981. struct brcms_hardware *wlc_hw = wlc->hw;
  1982. u32 macintmask;
  1983. if (!wlc_hw->clk)
  1984. return 0;
  1985. macintmask = wlc->macintmask; /* isr can still happen */
  1986. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), 0);
  1987. (void)bcma_read32(wlc_hw->d11core, D11REGOFFS(macintmask));
  1988. udelay(1); /* ensure int line is no longer driven */
  1989. wlc->macintmask = 0;
  1990. /* return previous macintmask; resolve race between us and our isr */
  1991. return wlc->macintstatus ? 0 : macintmask;
  1992. }
  1993. void brcms_c_intrsrestore(struct brcms_c_info *wlc, u32 macintmask)
  1994. {
  1995. struct brcms_hardware *wlc_hw = wlc->hw;
  1996. if (!wlc_hw->clk)
  1997. return;
  1998. wlc->macintmask = macintmask;
  1999. bcma_write32(wlc_hw->d11core, D11REGOFFS(macintmask), wlc->macintmask);
  2000. }
  2001. /* assumes that the d11 MAC is enabled */
  2002. static void brcms_b_tx_fifo_suspend(struct brcms_hardware *wlc_hw,
  2003. uint tx_fifo)
  2004. {
  2005. u8 fifo = 1 << tx_fifo;
  2006. /* Two clients of this code, 11h Quiet period and scanning. */
  2007. /* only suspend if not already suspended */
  2008. if ((wlc_hw->suspended_fifos & fifo) == fifo)
  2009. return;
  2010. /* force the core awake only if not already */
  2011. if (wlc_hw->suspended_fifos == 0)
  2012. brcms_c_ucode_wake_override_set(wlc_hw,
  2013. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2014. wlc_hw->suspended_fifos |= fifo;
  2015. if (wlc_hw->di[tx_fifo]) {
  2016. /*
  2017. * Suspending AMPDU transmissions in the middle can cause
  2018. * underflow which may result in mismatch between ucode and
  2019. * driver so suspend the mac before suspending the FIFO
  2020. */
  2021. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2022. brcms_c_suspend_mac_and_wait(wlc_hw->wlc);
  2023. dma_txsuspend(wlc_hw->di[tx_fifo]);
  2024. if (BRCMS_PHY_11N_CAP(wlc_hw->band))
  2025. brcms_c_enable_mac(wlc_hw->wlc);
  2026. }
  2027. }
  2028. static void brcms_b_tx_fifo_resume(struct brcms_hardware *wlc_hw,
  2029. uint tx_fifo)
  2030. {
  2031. /* BMAC_NOTE: BRCMS_TX_FIFO_ENAB is done in brcms_c_dpc() for DMA case
  2032. * but need to be done here for PIO otherwise the watchdog will catch
  2033. * the inconsistency and fire
  2034. */
  2035. /* Two clients of this code, 11h Quiet period and scanning. */
  2036. if (wlc_hw->di[tx_fifo])
  2037. dma_txresume(wlc_hw->di[tx_fifo]);
  2038. /* allow core to sleep again */
  2039. if (wlc_hw->suspended_fifos == 0)
  2040. return;
  2041. else {
  2042. wlc_hw->suspended_fifos &= ~(1 << tx_fifo);
  2043. if (wlc_hw->suspended_fifos == 0)
  2044. brcms_c_ucode_wake_override_clear(wlc_hw,
  2045. BRCMS_WAKE_OVERRIDE_TXFIFO);
  2046. }
  2047. }
  2048. /* precondition: requires the mac core to be enabled */
  2049. static void brcms_b_mute(struct brcms_hardware *wlc_hw, bool mute_tx)
  2050. {
  2051. static const u8 null_ether_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2052. if (mute_tx) {
  2053. /* suspend tx fifos */
  2054. brcms_b_tx_fifo_suspend(wlc_hw, TX_DATA_FIFO);
  2055. brcms_b_tx_fifo_suspend(wlc_hw, TX_CTL_FIFO);
  2056. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_BK_FIFO);
  2057. brcms_b_tx_fifo_suspend(wlc_hw, TX_AC_VI_FIFO);
  2058. /* zero the address match register so we do not send ACKs */
  2059. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
  2060. null_ether_addr);
  2061. } else {
  2062. /* resume tx fifos */
  2063. brcms_b_tx_fifo_resume(wlc_hw, TX_DATA_FIFO);
  2064. brcms_b_tx_fifo_resume(wlc_hw, TX_CTL_FIFO);
  2065. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_BK_FIFO);
  2066. brcms_b_tx_fifo_resume(wlc_hw, TX_AC_VI_FIFO);
  2067. /* Restore address */
  2068. brcms_b_set_addrmatch(wlc_hw, RCM_MAC_OFFSET,
  2069. wlc_hw->etheraddr);
  2070. }
  2071. wlc_phy_mute_upd(wlc_hw->band->pi, mute_tx, 0);
  2072. if (mute_tx)
  2073. brcms_c_ucode_mute_override_set(wlc_hw);
  2074. else
  2075. brcms_c_ucode_mute_override_clear(wlc_hw);
  2076. }
  2077. void
  2078. brcms_c_mute(struct brcms_c_info *wlc, bool mute_tx)
  2079. {
  2080. brcms_b_mute(wlc->hw, mute_tx);
  2081. }
  2082. /*
  2083. * Read and clear macintmask and macintstatus and intstatus registers.
  2084. * This routine should be called with interrupts off
  2085. * Return:
  2086. * -1 if brcms_deviceremoved(wlc) evaluates to true;
  2087. * 0 if the interrupt is not for us, or we are in some special cases;
  2088. * device interrupt status bits otherwise.
  2089. */
  2090. static inline u32 wlc_intstatus(struct brcms_c_info *wlc, bool in_isr)
  2091. {
  2092. struct brcms_hardware *wlc_hw = wlc->hw;
  2093. struct bcma_device *core = wlc_hw->d11core;
  2094. u32 macintstatus;
  2095. /* macintstatus includes a DMA interrupt summary bit */
  2096. macintstatus = bcma_read32(core, D11REGOFFS(macintstatus));
  2097. BCMMSG(wlc->wiphy, "wl%d: macintstatus: 0x%x\n", wlc_hw->unit,
  2098. macintstatus);
  2099. /* detect cardbus removed, in power down(suspend) and in reset */
  2100. if (brcms_deviceremoved(wlc))
  2101. return -1;
  2102. /* brcms_deviceremoved() succeeds even when the core is still resetting,
  2103. * handle that case here.
  2104. */
  2105. if (macintstatus == 0xffffffff)
  2106. return 0;
  2107. /* defer unsolicited interrupts */
  2108. macintstatus &= (in_isr ? wlc->macintmask : wlc->defmacintmask);
  2109. /* if not for us */
  2110. if (macintstatus == 0)
  2111. return 0;
  2112. /* interrupts are already turned off for CFE build
  2113. * Caution: For CFE Turning off the interrupts again has some undesired
  2114. * consequences
  2115. */
  2116. /* turn off the interrupts */
  2117. bcma_write32(core, D11REGOFFS(macintmask), 0);
  2118. (void)bcma_read32(core, D11REGOFFS(macintmask));
  2119. wlc->macintmask = 0;
  2120. /* clear device interrupts */
  2121. bcma_write32(core, D11REGOFFS(macintstatus), macintstatus);
  2122. /* MI_DMAINT is indication of non-zero intstatus */
  2123. if (macintstatus & MI_DMAINT)
  2124. /*
  2125. * only fifo interrupt enabled is I_RI in
  2126. * RX_FIFO. If MI_DMAINT is set, assume it
  2127. * is set and clear the interrupt.
  2128. */
  2129. bcma_write32(core, D11REGOFFS(intctrlregs[RX_FIFO].intstatus),
  2130. DEF_RXINTMASK);
  2131. return macintstatus;
  2132. }
  2133. /* Update wlc->macintstatus and wlc->intstatus[]. */
  2134. /* Return true if they are updated successfully. false otherwise */
  2135. bool brcms_c_intrsupd(struct brcms_c_info *wlc)
  2136. {
  2137. u32 macintstatus;
  2138. /* read and clear macintstatus and intstatus registers */
  2139. macintstatus = wlc_intstatus(wlc, false);
  2140. /* device is removed */
  2141. if (macintstatus == 0xffffffff)
  2142. return false;
  2143. /* update interrupt status in software */
  2144. wlc->macintstatus |= macintstatus;
  2145. return true;
  2146. }
  2147. /*
  2148. * First-level interrupt processing.
  2149. * Return true if this was our interrupt, false otherwise.
  2150. * *wantdpc will be set to true if further brcms_c_dpc() processing is required,
  2151. * false otherwise.
  2152. */
  2153. bool brcms_c_isr(struct brcms_c_info *wlc, bool *wantdpc)
  2154. {
  2155. struct brcms_hardware *wlc_hw = wlc->hw;
  2156. u32 macintstatus;
  2157. *wantdpc = false;
  2158. if (!wlc_hw->up || !wlc->macintmask)
  2159. return false;
  2160. /* read and clear macintstatus and intstatus registers */
  2161. macintstatus = wlc_intstatus(wlc, true);
  2162. if (macintstatus == 0xffffffff)
  2163. wiphy_err(wlc->wiphy, "DEVICEREMOVED detected in the ISR code"
  2164. " path\n");
  2165. /* it is not for us */
  2166. if (macintstatus == 0)
  2167. return false;
  2168. *wantdpc = true;
  2169. /* save interrupt status bits */
  2170. wlc->macintstatus = macintstatus;
  2171. return true;
  2172. }
  2173. void brcms_c_suspend_mac_and_wait(struct brcms_c_info *wlc)
  2174. {
  2175. struct brcms_hardware *wlc_hw = wlc->hw;
  2176. struct bcma_device *core = wlc_hw->d11core;
  2177. u32 mc, mi;
  2178. struct wiphy *wiphy = wlc->wiphy;
  2179. BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
  2180. wlc_hw->band->bandunit);
  2181. /*
  2182. * Track overlapping suspend requests
  2183. */
  2184. wlc_hw->mac_suspend_depth++;
  2185. if (wlc_hw->mac_suspend_depth > 1)
  2186. return;
  2187. /* force the core awake */
  2188. brcms_c_ucode_wake_override_set(wlc_hw, BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2189. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2190. if (mc == 0xffffffff) {
  2191. wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2192. __func__);
  2193. brcms_down(wlc->wl);
  2194. return;
  2195. }
  2196. WARN_ON(mc & MCTL_PSM_JMP_0);
  2197. WARN_ON(!(mc & MCTL_PSM_RUN));
  2198. WARN_ON(!(mc & MCTL_EN_MAC));
  2199. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2200. if (mi == 0xffffffff) {
  2201. wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2202. __func__);
  2203. brcms_down(wlc->wl);
  2204. return;
  2205. }
  2206. WARN_ON(mi & MI_MACSSPNDD);
  2207. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, 0);
  2208. SPINWAIT(!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD),
  2209. BRCMS_MAX_MAC_SUSPEND);
  2210. if (!(bcma_read32(core, D11REGOFFS(macintstatus)) & MI_MACSSPNDD)) {
  2211. wiphy_err(wiphy, "wl%d: wlc_suspend_mac_and_wait: waited %d uS"
  2212. " and MI_MACSSPNDD is still not on.\n",
  2213. wlc_hw->unit, BRCMS_MAX_MAC_SUSPEND);
  2214. wiphy_err(wiphy, "wl%d: psmdebug 0x%08x, phydebug 0x%08x, "
  2215. "psm_brc 0x%04x\n", wlc_hw->unit,
  2216. bcma_read32(core, D11REGOFFS(psmdebug)),
  2217. bcma_read32(core, D11REGOFFS(phydebug)),
  2218. bcma_read16(core, D11REGOFFS(psm_brc)));
  2219. }
  2220. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2221. if (mc == 0xffffffff) {
  2222. wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
  2223. __func__);
  2224. brcms_down(wlc->wl);
  2225. return;
  2226. }
  2227. WARN_ON(mc & MCTL_PSM_JMP_0);
  2228. WARN_ON(!(mc & MCTL_PSM_RUN));
  2229. WARN_ON(mc & MCTL_EN_MAC);
  2230. }
  2231. void brcms_c_enable_mac(struct brcms_c_info *wlc)
  2232. {
  2233. struct brcms_hardware *wlc_hw = wlc->hw;
  2234. struct bcma_device *core = wlc_hw->d11core;
  2235. u32 mc, mi;
  2236. BCMMSG(wlc->wiphy, "wl%d: bandunit %d\n", wlc_hw->unit,
  2237. wlc->band->bandunit);
  2238. /*
  2239. * Track overlapping suspend requests
  2240. */
  2241. wlc_hw->mac_suspend_depth--;
  2242. if (wlc_hw->mac_suspend_depth > 0)
  2243. return;
  2244. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2245. WARN_ON(mc & MCTL_PSM_JMP_0);
  2246. WARN_ON(mc & MCTL_EN_MAC);
  2247. WARN_ON(!(mc & MCTL_PSM_RUN));
  2248. brcms_b_mctrl(wlc_hw, MCTL_EN_MAC, MCTL_EN_MAC);
  2249. bcma_write32(core, D11REGOFFS(macintstatus), MI_MACSSPNDD);
  2250. mc = bcma_read32(core, D11REGOFFS(maccontrol));
  2251. WARN_ON(mc & MCTL_PSM_JMP_0);
  2252. WARN_ON(!(mc & MCTL_EN_MAC));
  2253. WARN_ON(!(mc & MCTL_PSM_RUN));
  2254. mi = bcma_read32(core, D11REGOFFS(macintstatus));
  2255. WARN_ON(mi & MI_MACSSPNDD);
  2256. brcms_c_ucode_wake_override_clear(wlc_hw,
  2257. BRCMS_WAKE_OVERRIDE_MACSUSPEND);
  2258. }
  2259. void brcms_b_band_stf_ss_set(struct brcms_hardware *wlc_hw, u8 stf_mode)
  2260. {
  2261. wlc_hw->hw_stf_ss_opmode = stf_mode;
  2262. if (wlc_hw->clk)
  2263. brcms_upd_ofdm_pctl1_table(wlc_hw);
  2264. }
  2265. static bool brcms_b_validate_chip_access(struct brcms_hardware *wlc_hw)
  2266. {
  2267. struct bcma_device *core = wlc_hw->d11core;
  2268. u32 w, val;
  2269. struct wiphy *wiphy = wlc_hw->wlc->wiphy;
  2270. BCMMSG(wiphy, "wl%d\n", wlc_hw->unit);
  2271. /* Validate dchip register access */
  2272. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2273. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2274. w = bcma_read32(core, D11REGOFFS(objdata));
  2275. /* Can we write and read back a 32bit register? */
  2276. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2277. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2278. bcma_write32(core, D11REGOFFS(objdata), (u32) 0xaa5555aa);
  2279. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2280. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2281. val = bcma_read32(core, D11REGOFFS(objdata));
  2282. if (val != (u32) 0xaa5555aa) {
  2283. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2284. "expected 0xaa5555aa\n", wlc_hw->unit, val);
  2285. return false;
  2286. }
  2287. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2288. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2289. bcma_write32(core, D11REGOFFS(objdata), (u32) 0x55aaaa55);
  2290. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2291. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2292. val = bcma_read32(core, D11REGOFFS(objdata));
  2293. if (val != (u32) 0x55aaaa55) {
  2294. wiphy_err(wiphy, "wl%d: validate_chip_access: SHM = 0x%x, "
  2295. "expected 0x55aaaa55\n", wlc_hw->unit, val);
  2296. return false;
  2297. }
  2298. bcma_write32(core, D11REGOFFS(objaddr), OBJADDR_SHM_SEL | 0);
  2299. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2300. bcma_write32(core, D11REGOFFS(objdata), w);
  2301. /* clear CFPStart */
  2302. bcma_write32(core, D11REGOFFS(tsf_cfpstart), 0);
  2303. w = bcma_read32(core, D11REGOFFS(maccontrol));
  2304. if ((w != (MCTL_IHR_EN | MCTL_WAKE)) &&
  2305. (w != (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE))) {
  2306. wiphy_err(wiphy, "wl%d: validate_chip_access: maccontrol = "
  2307. "0x%x, expected 0x%x or 0x%x\n", wlc_hw->unit, w,
  2308. (MCTL_IHR_EN | MCTL_WAKE),
  2309. (MCTL_IHR_EN | MCTL_GMODE | MCTL_WAKE));
  2310. return false;
  2311. }
  2312. return true;
  2313. }
  2314. #define PHYPLL_WAIT_US 100000
  2315. void brcms_b_core_phypll_ctl(struct brcms_hardware *wlc_hw, bool on)
  2316. {
  2317. struct bcma_device *core = wlc_hw->d11core;
  2318. u32 tmp;
  2319. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  2320. tmp = 0;
  2321. if (on) {
  2322. if ((ai_get_chip_id(wlc_hw->sih) == BCM4313_CHIP_ID)) {
  2323. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2324. CCS_ERSRC_REQ_HT |
  2325. CCS_ERSRC_REQ_D11PLL |
  2326. CCS_ERSRC_REQ_PHYPLL);
  2327. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2328. CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT,
  2329. PHYPLL_WAIT_US);
  2330. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2331. if ((tmp & CCS_ERSRC_AVAIL_HT) != CCS_ERSRC_AVAIL_HT)
  2332. wiphy_err(wlc_hw->wlc->wiphy, "%s: turn on PHY"
  2333. " PLL failed\n", __func__);
  2334. } else {
  2335. bcma_set32(core, D11REGOFFS(clk_ctl_st),
  2336. tmp | CCS_ERSRC_REQ_D11PLL |
  2337. CCS_ERSRC_REQ_PHYPLL);
  2338. SPINWAIT((bcma_read32(core, D11REGOFFS(clk_ctl_st)) &
  2339. (CCS_ERSRC_AVAIL_D11PLL |
  2340. CCS_ERSRC_AVAIL_PHYPLL)) !=
  2341. (CCS_ERSRC_AVAIL_D11PLL |
  2342. CCS_ERSRC_AVAIL_PHYPLL), PHYPLL_WAIT_US);
  2343. tmp = bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2344. if ((tmp &
  2345. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2346. !=
  2347. (CCS_ERSRC_AVAIL_D11PLL | CCS_ERSRC_AVAIL_PHYPLL))
  2348. wiphy_err(wlc_hw->wlc->wiphy, "%s: turn on "
  2349. "PHY PLL failed\n", __func__);
  2350. }
  2351. } else {
  2352. /*
  2353. * Since the PLL may be shared, other cores can still
  2354. * be requesting it; so we'll deassert the request but
  2355. * not wait for status to comply.
  2356. */
  2357. bcma_mask32(core, D11REGOFFS(clk_ctl_st),
  2358. ~CCS_ERSRC_REQ_PHYPLL);
  2359. (void)bcma_read32(core, D11REGOFFS(clk_ctl_st));
  2360. }
  2361. }
  2362. static void brcms_c_coredisable(struct brcms_hardware *wlc_hw)
  2363. {
  2364. bool dev_gone;
  2365. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  2366. dev_gone = brcms_deviceremoved(wlc_hw->wlc);
  2367. if (dev_gone)
  2368. return;
  2369. if (wlc_hw->noreset)
  2370. return;
  2371. /* radio off */
  2372. wlc_phy_switch_radio(wlc_hw->band->pi, OFF);
  2373. /* turn off analog core */
  2374. wlc_phy_anacore(wlc_hw->band->pi, OFF);
  2375. /* turn off PHYPLL to save power */
  2376. brcms_b_core_phypll_ctl(wlc_hw, false);
  2377. wlc_hw->clk = false;
  2378. bcma_core_disable(wlc_hw->d11core, 0);
  2379. wlc_phy_hw_clk_state_upd(wlc_hw->band->pi, false);
  2380. }
  2381. static void brcms_c_flushqueues(struct brcms_c_info *wlc)
  2382. {
  2383. struct brcms_hardware *wlc_hw = wlc->hw;
  2384. uint i;
  2385. /* free any posted tx packets */
  2386. for (i = 0; i < NFIFO; i++)
  2387. if (wlc_hw->di[i]) {
  2388. dma_txreclaim(wlc_hw->di[i], DMA_RANGE_ALL);
  2389. wlc->core->txpktpend[i] = 0;
  2390. BCMMSG(wlc->wiphy, "pktpend fifo %d clrd\n", i);
  2391. }
  2392. /* free any posted rx packets */
  2393. dma_rxreclaim(wlc_hw->di[RX_FIFO]);
  2394. }
  2395. static u16
  2396. brcms_b_read_objmem(struct brcms_hardware *wlc_hw, uint offset, u32 sel)
  2397. {
  2398. struct bcma_device *core = wlc_hw->d11core;
  2399. u16 objoff = D11REGOFFS(objdata);
  2400. bcma_write32(core, D11REGOFFS(objaddr), sel | (offset >> 2));
  2401. (void)bcma_read32(core, D11REGOFFS(objaddr));
  2402. if (offset & 2)
  2403. objoff += 2;
  2404. return bcma_read16(core, objoff);
  2405. ;
  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 BCMDBG
  2555. u16 delta;
  2556. u16 rxf0ovfl;
  2557. u16 txfunfl[NFIFO];
  2558. #endif /* BCMDBG */
  2559. /* if driver down, make no sense to update stats */
  2560. if (!wlc->pub->up)
  2561. return;
  2562. #ifdef BCMDBG
  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 /* BCMDBG */
  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 BCMDBG
  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 /* BCMDBG */
  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 == true)
  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 == true)
  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. #ifdef BCMDBG
  4775. static const char * const supr_reason[] = {
  4776. "None", "PMQ Entry", "Flush request",
  4777. "Previous frag failure", "Channel mismatch",
  4778. "Lifetime Expiry", "Underflow"
  4779. };
  4780. static void brcms_c_print_txs_status(u16 s)
  4781. {
  4782. printk(KERN_DEBUG "[15:12] %d frame attempts\n",
  4783. (s & TX_STATUS_FRM_RTX_MASK) >> TX_STATUS_FRM_RTX_SHIFT);
  4784. printk(KERN_DEBUG " [11:8] %d rts attempts\n",
  4785. (s & TX_STATUS_RTS_RTX_MASK) >> TX_STATUS_RTS_RTX_SHIFT);
  4786. printk(KERN_DEBUG " [7] %d PM mode indicated\n",
  4787. ((s & TX_STATUS_PMINDCTD) ? 1 : 0));
  4788. printk(KERN_DEBUG " [6] %d intermediate status\n",
  4789. ((s & TX_STATUS_INTERMEDIATE) ? 1 : 0));
  4790. printk(KERN_DEBUG " [5] %d AMPDU\n",
  4791. (s & TX_STATUS_AMPDU) ? 1 : 0);
  4792. printk(KERN_DEBUG " [4:2] %d Frame Suppressed Reason (%s)\n",
  4793. ((s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT),
  4794. supr_reason[(s & TX_STATUS_SUPR_MASK) >> TX_STATUS_SUPR_SHIFT]);
  4795. printk(KERN_DEBUG " [1] %d acked\n",
  4796. ((s & TX_STATUS_ACK_RCV) ? 1 : 0));
  4797. }
  4798. #endif /* BCMDBG */
  4799. void brcms_c_print_txstatus(struct tx_status *txs)
  4800. {
  4801. #if defined(BCMDBG)
  4802. u16 s = txs->status;
  4803. u16 ackphyrxsh = txs->ackphyrxsh;
  4804. printk(KERN_DEBUG "\ntxpkt (MPDU) Complete\n");
  4805. printk(KERN_DEBUG "FrameID: %04x ", txs->frameid);
  4806. printk(KERN_DEBUG "TxStatus: %04x", s);
  4807. printk(KERN_DEBUG "\n");
  4808. brcms_c_print_txs_status(s);
  4809. printk(KERN_DEBUG "LastTxTime: %04x ", txs->lasttxtime);
  4810. printk(KERN_DEBUG "Seq: %04x ", txs->sequence);
  4811. printk(KERN_DEBUG "PHYTxStatus: %04x ", txs->phyerr);
  4812. printk(KERN_DEBUG "RxAckRSSI: %04x ",
  4813. (ackphyrxsh & PRXS1_JSSI_MASK) >> PRXS1_JSSI_SHIFT);
  4814. printk(KERN_DEBUG "RxAckSQ: %04x",
  4815. (ackphyrxsh & PRXS1_SQ_MASK) >> PRXS1_SQ_SHIFT);
  4816. printk(KERN_DEBUG "\n");
  4817. #endif /* defined(BCMDBG) */
  4818. }
  4819. bool brcms_c_chipmatch(u16 vendor, u16 device)
  4820. {
  4821. if (vendor != PCI_VENDOR_ID_BROADCOM) {
  4822. pr_err("chipmatch: unknown vendor id %04x\n", vendor);
  4823. return false;
  4824. }
  4825. if (device == BCM43224_D11N_ID_VEN1)
  4826. return true;
  4827. if ((device == BCM43224_D11N_ID) || (device == BCM43225_D11N2G_ID))
  4828. return true;
  4829. if (device == BCM4313_D11N2G_ID)
  4830. return true;
  4831. if ((device == BCM43236_D11N_ID) || (device == BCM43236_D11N2G_ID))
  4832. return true;
  4833. pr_err("chipmatch: unknown device id %04x\n", device);
  4834. return false;
  4835. }
  4836. #if defined(BCMDBG)
  4837. void brcms_c_print_txdesc(struct d11txh *txh)
  4838. {
  4839. u16 mtcl = le16_to_cpu(txh->MacTxControlLow);
  4840. u16 mtch = le16_to_cpu(txh->MacTxControlHigh);
  4841. u16 mfc = le16_to_cpu(txh->MacFrameControl);
  4842. u16 tfest = le16_to_cpu(txh->TxFesTimeNormal);
  4843. u16 ptcw = le16_to_cpu(txh->PhyTxControlWord);
  4844. u16 ptcw_1 = le16_to_cpu(txh->PhyTxControlWord_1);
  4845. u16 ptcw_1_Fbr = le16_to_cpu(txh->PhyTxControlWord_1_Fbr);
  4846. u16 ptcw_1_Rts = le16_to_cpu(txh->PhyTxControlWord_1_Rts);
  4847. u16 ptcw_1_FbrRts = le16_to_cpu(txh->PhyTxControlWord_1_FbrRts);
  4848. u16 mainrates = le16_to_cpu(txh->MainRates);
  4849. u16 xtraft = le16_to_cpu(txh->XtraFrameTypes);
  4850. u8 *iv = txh->IV;
  4851. u8 *ra = txh->TxFrameRA;
  4852. u16 tfestfb = le16_to_cpu(txh->TxFesTimeFallback);
  4853. u8 *rtspfb = txh->RTSPLCPFallback;
  4854. u16 rtsdfb = le16_to_cpu(txh->RTSDurFallback);
  4855. u8 *fragpfb = txh->FragPLCPFallback;
  4856. u16 fragdfb = le16_to_cpu(txh->FragDurFallback);
  4857. u16 mmodelen = le16_to_cpu(txh->MModeLen);
  4858. u16 mmodefbrlen = le16_to_cpu(txh->MModeFbrLen);
  4859. u16 tfid = le16_to_cpu(txh->TxFrameID);
  4860. u16 txs = le16_to_cpu(txh->TxStatus);
  4861. u16 mnmpdu = le16_to_cpu(txh->MaxNMpdus);
  4862. u16 mabyte = le16_to_cpu(txh->MaxABytes_MRT);
  4863. u16 mabyte_f = le16_to_cpu(txh->MaxABytes_FBR);
  4864. u16 mmbyte = le16_to_cpu(txh->MinMBytes);
  4865. u8 *rtsph = txh->RTSPhyHeader;
  4866. struct ieee80211_rts rts = txh->rts_frame;
  4867. /* add plcp header along with txh descriptor */
  4868. printk(KERN_DEBUG "Raw TxDesc + plcp header:\n");
  4869. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  4870. txh, sizeof(struct d11txh) + 48);
  4871. printk(KERN_DEBUG "TxCtlLow: %04x ", mtcl);
  4872. printk(KERN_DEBUG "TxCtlHigh: %04x ", mtch);
  4873. printk(KERN_DEBUG "FC: %04x ", mfc);
  4874. printk(KERN_DEBUG "FES Time: %04x\n", tfest);
  4875. printk(KERN_DEBUG "PhyCtl: %04x%s ", ptcw,
  4876. (ptcw & PHY_TXC_SHORT_HDR) ? " short" : "");
  4877. printk(KERN_DEBUG "PhyCtl_1: %04x ", ptcw_1);
  4878. printk(KERN_DEBUG "PhyCtl_1_Fbr: %04x\n", ptcw_1_Fbr);
  4879. printk(KERN_DEBUG "PhyCtl_1_Rts: %04x ", ptcw_1_Rts);
  4880. printk(KERN_DEBUG "PhyCtl_1_Fbr_Rts: %04x\n", ptcw_1_FbrRts);
  4881. printk(KERN_DEBUG "MainRates: %04x ", mainrates);
  4882. printk(KERN_DEBUG "XtraFrameTypes: %04x ", xtraft);
  4883. printk(KERN_DEBUG "\n");
  4884. print_hex_dump_bytes("SecIV:", DUMP_PREFIX_OFFSET, iv, sizeof(txh->IV));
  4885. print_hex_dump_bytes("RA:", DUMP_PREFIX_OFFSET,
  4886. ra, sizeof(txh->TxFrameRA));
  4887. printk(KERN_DEBUG "Fb FES Time: %04x ", tfestfb);
  4888. print_hex_dump_bytes("Fb RTS PLCP:", DUMP_PREFIX_OFFSET,
  4889. rtspfb, sizeof(txh->RTSPLCPFallback));
  4890. printk(KERN_DEBUG "RTS DUR: %04x ", rtsdfb);
  4891. print_hex_dump_bytes("PLCP:", DUMP_PREFIX_OFFSET,
  4892. fragpfb, sizeof(txh->FragPLCPFallback));
  4893. printk(KERN_DEBUG "DUR: %04x", fragdfb);
  4894. printk(KERN_DEBUG "\n");
  4895. printk(KERN_DEBUG "MModeLen: %04x ", mmodelen);
  4896. printk(KERN_DEBUG "MModeFbrLen: %04x\n", mmodefbrlen);
  4897. printk(KERN_DEBUG "FrameID: %04x\n", tfid);
  4898. printk(KERN_DEBUG "TxStatus: %04x\n", txs);
  4899. printk(KERN_DEBUG "MaxNumMpdu: %04x\n", mnmpdu);
  4900. printk(KERN_DEBUG "MaxAggbyte: %04x\n", mabyte);
  4901. printk(KERN_DEBUG "MaxAggbyte_fb: %04x\n", mabyte_f);
  4902. printk(KERN_DEBUG "MinByte: %04x\n", mmbyte);
  4903. print_hex_dump_bytes("RTS PLCP:", DUMP_PREFIX_OFFSET,
  4904. rtsph, sizeof(txh->RTSPhyHeader));
  4905. print_hex_dump_bytes("RTS Frame:", DUMP_PREFIX_OFFSET,
  4906. (u8 *)&rts, sizeof(txh->rts_frame));
  4907. printk(KERN_DEBUG "\n");
  4908. }
  4909. #endif /* defined(BCMDBG) */
  4910. #if defined(BCMDBG)
  4911. static int
  4912. brcms_c_format_flags(const struct brcms_c_bit_desc *bd, u32 flags, char *buf,
  4913. int len)
  4914. {
  4915. int i;
  4916. char *p = buf;
  4917. char hexstr[16];
  4918. int slen = 0, nlen = 0;
  4919. u32 bit;
  4920. const char *name;
  4921. if (len < 2 || !buf)
  4922. return 0;
  4923. buf[0] = '\0';
  4924. for (i = 0; flags != 0; i++) {
  4925. bit = bd[i].bit;
  4926. name = bd[i].name;
  4927. if (bit == 0 && flags != 0) {
  4928. /* print any unnamed bits */
  4929. snprintf(hexstr, 16, "0x%X", flags);
  4930. name = hexstr;
  4931. flags = 0; /* exit loop */
  4932. } else if ((flags & bit) == 0)
  4933. continue;
  4934. flags &= ~bit;
  4935. nlen = strlen(name);
  4936. slen += nlen;
  4937. /* count btwn flag space */
  4938. if (flags != 0)
  4939. slen += 1;
  4940. /* need NULL char as well */
  4941. if (len <= slen)
  4942. break;
  4943. /* copy NULL char but don't count it */
  4944. strncpy(p, name, nlen + 1);
  4945. p += nlen;
  4946. /* copy btwn flag space and NULL char */
  4947. if (flags != 0)
  4948. p += snprintf(p, 2, " ");
  4949. len -= slen;
  4950. }
  4951. /* indicate the str was too short */
  4952. if (flags != 0) {
  4953. if (len < 2)
  4954. p -= 2 - len; /* overwrite last char */
  4955. p += snprintf(p, 2, ">");
  4956. }
  4957. return (int)(p - buf);
  4958. }
  4959. #endif /* defined(BCMDBG) */
  4960. #if defined(BCMDBG)
  4961. void brcms_c_print_rxh(struct d11rxhdr *rxh)
  4962. {
  4963. u16 len = rxh->RxFrameSize;
  4964. u16 phystatus_0 = rxh->PhyRxStatus_0;
  4965. u16 phystatus_1 = rxh->PhyRxStatus_1;
  4966. u16 phystatus_2 = rxh->PhyRxStatus_2;
  4967. u16 phystatus_3 = rxh->PhyRxStatus_3;
  4968. u16 macstatus1 = rxh->RxStatus1;
  4969. u16 macstatus2 = rxh->RxStatus2;
  4970. char flagstr[64];
  4971. char lenbuf[20];
  4972. static const struct brcms_c_bit_desc macstat_flags[] = {
  4973. {RXS_FCSERR, "FCSErr"},
  4974. {RXS_RESPFRAMETX, "Reply"},
  4975. {RXS_PBPRES, "PADDING"},
  4976. {RXS_DECATMPT, "DeCr"},
  4977. {RXS_DECERR, "DeCrErr"},
  4978. {RXS_BCNSENT, "Bcn"},
  4979. {0, NULL}
  4980. };
  4981. printk(KERN_DEBUG "Raw RxDesc:\n");
  4982. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, rxh,
  4983. sizeof(struct d11rxhdr));
  4984. brcms_c_format_flags(macstat_flags, macstatus1, flagstr, 64);
  4985. snprintf(lenbuf, sizeof(lenbuf), "0x%x", len);
  4986. printk(KERN_DEBUG "RxFrameSize: %6s (%d)%s\n", lenbuf, len,
  4987. (rxh->PhyRxStatus_0 & PRXS0_SHORTH) ? " short preamble" : "");
  4988. printk(KERN_DEBUG "RxPHYStatus: %04x %04x %04x %04x\n",
  4989. phystatus_0, phystatus_1, phystatus_2, phystatus_3);
  4990. printk(KERN_DEBUG "RxMACStatus: %x %s\n", macstatus1, flagstr);
  4991. printk(KERN_DEBUG "RXMACaggtype: %x\n",
  4992. (macstatus2 & RXS_AGGTYPE_MASK));
  4993. printk(KERN_DEBUG "RxTSFTime: %04x\n", rxh->RxTSFTime);
  4994. }
  4995. #endif /* defined(BCMDBG) */
  4996. u16 brcms_b_rate_shm_offset(struct brcms_hardware *wlc_hw, u8 rate)
  4997. {
  4998. u16 table_ptr;
  4999. u8 phy_rate, index;
  5000. /* get the phy specific rate encoding for the PLCP SIGNAL field */
  5001. if (is_ofdm_rate(rate))
  5002. table_ptr = M_RT_DIRMAP_A;
  5003. else
  5004. table_ptr = M_RT_DIRMAP_B;
  5005. /* for a given rate, the LS-nibble of the PLCP SIGNAL field is
  5006. * the index into the rate table.
  5007. */
  5008. phy_rate = rate_info[rate] & BRCMS_RATE_MASK;
  5009. index = phy_rate & 0xf;
  5010. /* Find the SHM pointer to the rate table entry by looking in the
  5011. * Direct-map Table
  5012. */
  5013. return 2 * brcms_b_read_shm(wlc_hw, table_ptr + (index * 2));
  5014. }
  5015. static bool
  5016. brcms_c_prec_enq_head(struct brcms_c_info *wlc, struct pktq *q,
  5017. struct sk_buff *pkt, int prec, bool head)
  5018. {
  5019. struct sk_buff *p;
  5020. int eprec = -1; /* precedence to evict from */
  5021. /* Determine precedence from which to evict packet, if any */
  5022. if (pktq_pfull(q, prec))
  5023. eprec = prec;
  5024. else if (pktq_full(q)) {
  5025. p = brcmu_pktq_peek_tail(q, &eprec);
  5026. if (eprec > prec) {
  5027. wiphy_err(wlc->wiphy, "%s: Failing: eprec %d > prec %d"
  5028. "\n", __func__, eprec, prec);
  5029. return false;
  5030. }
  5031. }
  5032. /* Evict if needed */
  5033. if (eprec >= 0) {
  5034. bool discard_oldest;
  5035. discard_oldest = ac_bitmap_tst(0, eprec);
  5036. /* Refuse newer packet unless configured to discard oldest */
  5037. if (eprec == prec && !discard_oldest) {
  5038. wiphy_err(wlc->wiphy, "%s: No where to go, prec == %d"
  5039. "\n", __func__, prec);
  5040. return false;
  5041. }
  5042. /* Evict packet according to discard policy */
  5043. p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
  5044. brcmu_pktq_pdeq_tail(q, eprec);
  5045. brcmu_pkt_buf_free_skb(p);
  5046. }
  5047. /* Enqueue */
  5048. if (head)
  5049. p = brcmu_pktq_penq_head(q, prec, pkt);
  5050. else
  5051. p = brcmu_pktq_penq(q, prec, pkt);
  5052. return true;
  5053. }
  5054. /*
  5055. * Attempts to queue a packet onto a multiple-precedence queue,
  5056. * if necessary evicting a lower precedence packet from the queue.
  5057. *
  5058. * 'prec' is the precedence number that has already been mapped
  5059. * from the packet priority.
  5060. *
  5061. * Returns true if packet consumed (queued), false if not.
  5062. */
  5063. static bool brcms_c_prec_enq(struct brcms_c_info *wlc, struct pktq *q,
  5064. struct sk_buff *pkt, int prec)
  5065. {
  5066. return brcms_c_prec_enq_head(wlc, q, pkt, prec, false);
  5067. }
  5068. void brcms_c_txq_enq(struct brcms_c_info *wlc, struct scb *scb,
  5069. struct sk_buff *sdu, uint prec)
  5070. {
  5071. struct brcms_txq_info *qi = wlc->pkt_queue; /* Check me */
  5072. struct pktq *q = &qi->q;
  5073. int prio;
  5074. prio = sdu->priority;
  5075. if (!brcms_c_prec_enq(wlc, q, sdu, prec)) {
  5076. /*
  5077. * we might hit this condtion in case
  5078. * packet flooding from mac80211 stack
  5079. */
  5080. brcmu_pkt_buf_free_skb(sdu);
  5081. }
  5082. }
  5083. /*
  5084. * bcmc_fid_generate:
  5085. * Generate frame ID for a BCMC packet. The frag field is not used
  5086. * for MC frames so is used as part of the sequence number.
  5087. */
  5088. static inline u16
  5089. bcmc_fid_generate(struct brcms_c_info *wlc, struct brcms_bss_cfg *bsscfg,
  5090. struct d11txh *txh)
  5091. {
  5092. u16 frameid;
  5093. frameid = le16_to_cpu(txh->TxFrameID) & ~(TXFID_SEQ_MASK |
  5094. TXFID_QUEUE_MASK);
  5095. frameid |=
  5096. (((wlc->
  5097. mc_fid_counter++) << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  5098. TX_BCMC_FIFO;
  5099. return frameid;
  5100. }
  5101. static uint
  5102. brcms_c_calc_ack_time(struct brcms_c_info *wlc, u32 rspec,
  5103. u8 preamble_type)
  5104. {
  5105. uint dur = 0;
  5106. BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d\n",
  5107. wlc->pub->unit, rspec, preamble_type);
  5108. /*
  5109. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  5110. * is less than or equal to the rate of the immediately previous
  5111. * frame in the FES
  5112. */
  5113. rspec = brcms_basic_rate(wlc, rspec);
  5114. /* ACK frame len == 14 == 2(fc) + 2(dur) + 6(ra) + 4(fcs) */
  5115. dur =
  5116. brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  5117. (DOT11_ACK_LEN + FCS_LEN));
  5118. return dur;
  5119. }
  5120. static uint
  5121. brcms_c_calc_cts_time(struct brcms_c_info *wlc, u32 rspec,
  5122. u8 preamble_type)
  5123. {
  5124. BCMMSG(wlc->wiphy, "wl%d: ratespec 0x%x, preamble_type %d\n",
  5125. wlc->pub->unit, rspec, preamble_type);
  5126. return brcms_c_calc_ack_time(wlc, rspec, preamble_type);
  5127. }
  5128. static uint
  5129. brcms_c_calc_ba_time(struct brcms_c_info *wlc, u32 rspec,
  5130. u8 preamble_type)
  5131. {
  5132. BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, "
  5133. "preamble_type %d\n", wlc->pub->unit, rspec, preamble_type);
  5134. /*
  5135. * Spec 9.6: ack rate is the highest rate in BSSBasicRateSet that
  5136. * is less than or equal to the rate of the immediately previous
  5137. * frame in the FES
  5138. */
  5139. rspec = brcms_basic_rate(wlc, rspec);
  5140. /* BA len == 32 == 16(ctl hdr) + 4(ba len) + 8(bitmap) + 4(fcs) */
  5141. return brcms_c_calc_frame_time(wlc, rspec, preamble_type,
  5142. (DOT11_BA_LEN + DOT11_BA_BITMAP_LEN +
  5143. FCS_LEN));
  5144. }
  5145. /* brcms_c_compute_frame_dur()
  5146. *
  5147. * Calculate the 802.11 MAC header DUR field for MPDU
  5148. * DUR for a single frame = 1 SIFS + 1 ACK
  5149. * DUR for a frame with following frags = 3 SIFS + 2 ACK + next frag time
  5150. *
  5151. * rate MPDU rate in unit of 500kbps
  5152. * next_frag_len next MPDU length in bytes
  5153. * preamble_type use short/GF or long/MM PLCP header
  5154. */
  5155. static u16
  5156. brcms_c_compute_frame_dur(struct brcms_c_info *wlc, u32 rate,
  5157. u8 preamble_type, uint next_frag_len)
  5158. {
  5159. u16 dur, sifs;
  5160. sifs = get_sifs(wlc->band);
  5161. dur = sifs;
  5162. dur += (u16) brcms_c_calc_ack_time(wlc, rate, preamble_type);
  5163. if (next_frag_len) {
  5164. /* Double the current DUR to get 2 SIFS + 2 ACKs */
  5165. dur *= 2;
  5166. /* add another SIFS and the frag time */
  5167. dur += sifs;
  5168. dur +=
  5169. (u16) brcms_c_calc_frame_time(wlc, rate, preamble_type,
  5170. next_frag_len);
  5171. }
  5172. return dur;
  5173. }
  5174. /* The opposite of brcms_c_calc_frame_time */
  5175. static uint
  5176. brcms_c_calc_frame_len(struct brcms_c_info *wlc, u32 ratespec,
  5177. u8 preamble_type, uint dur)
  5178. {
  5179. uint nsyms, mac_len, Ndps, kNdps;
  5180. uint rate = rspec2rate(ratespec);
  5181. BCMMSG(wlc->wiphy, "wl%d: rspec 0x%x, preamble_type %d, dur %d\n",
  5182. wlc->pub->unit, ratespec, preamble_type, dur);
  5183. if (is_mcs_rate(ratespec)) {
  5184. uint mcs = ratespec & RSPEC_RATE_MASK;
  5185. int tot_streams = mcs_2_txstreams(mcs) + rspec_stc(ratespec);
  5186. dur -= PREN_PREAMBLE + (tot_streams * PREN_PREAMBLE_EXT);
  5187. /* payload calculation matches that of regular ofdm */
  5188. if (wlc->band->bandtype == BRCM_BAND_2G)
  5189. dur -= DOT11_OFDM_SIGNAL_EXTENSION;
  5190. /* kNdbps = kbps * 4 */
  5191. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  5192. rspec_issgi(ratespec)) * 4;
  5193. nsyms = dur / APHY_SYMBOL_TIME;
  5194. mac_len =
  5195. ((nsyms * kNdps) -
  5196. ((APHY_SERVICE_NBITS + APHY_TAIL_NBITS) * 1000)) / 8000;
  5197. } else if (is_ofdm_rate(ratespec)) {
  5198. dur -= APHY_PREAMBLE_TIME;
  5199. dur -= APHY_SIGNAL_TIME;
  5200. /* Ndbps = Mbps * 4 = rate(500Kbps) * 2 */
  5201. Ndps = rate * 2;
  5202. nsyms = dur / APHY_SYMBOL_TIME;
  5203. mac_len =
  5204. ((nsyms * Ndps) -
  5205. (APHY_SERVICE_NBITS + APHY_TAIL_NBITS)) / 8;
  5206. } else {
  5207. if (preamble_type & BRCMS_SHORT_PREAMBLE)
  5208. dur -= BPHY_PLCP_SHORT_TIME;
  5209. else
  5210. dur -= BPHY_PLCP_TIME;
  5211. mac_len = dur * rate;
  5212. /* divide out factor of 2 in rate (1/2 mbps) */
  5213. mac_len = mac_len / 8 / 2;
  5214. }
  5215. return mac_len;
  5216. }
  5217. /*
  5218. * Return true if the specified rate is supported by the specified band.
  5219. * BRCM_BAND_AUTO indicates the current band.
  5220. */
  5221. static bool brcms_c_valid_rate(struct brcms_c_info *wlc, u32 rspec, int band,
  5222. bool verbose)
  5223. {
  5224. struct brcms_c_rateset *hw_rateset;
  5225. uint i;
  5226. if ((band == BRCM_BAND_AUTO) || (band == wlc->band->bandtype))
  5227. hw_rateset = &wlc->band->hw_rateset;
  5228. else if (wlc->pub->_nbands > 1)
  5229. hw_rateset = &wlc->bandstate[OTHERBANDUNIT(wlc)]->hw_rateset;
  5230. else
  5231. /* other band specified and we are a single band device */
  5232. return false;
  5233. /* check if this is a mimo rate */
  5234. if (is_mcs_rate(rspec)) {
  5235. if ((rspec & RSPEC_RATE_MASK) >= MCS_TABLE_SIZE)
  5236. goto error;
  5237. return isset(hw_rateset->mcs, (rspec & RSPEC_RATE_MASK));
  5238. }
  5239. for (i = 0; i < hw_rateset->count; i++)
  5240. if (hw_rateset->rates[i] == rspec2rate(rspec))
  5241. return true;
  5242. error:
  5243. if (verbose)
  5244. wiphy_err(wlc->wiphy, "wl%d: valid_rate: rate spec 0x%x "
  5245. "not in hw_rateset\n", wlc->pub->unit, rspec);
  5246. return false;
  5247. }
  5248. static u32
  5249. mac80211_wlc_set_nrate(struct brcms_c_info *wlc, struct brcms_band *cur_band,
  5250. u32 int_val)
  5251. {
  5252. u8 stf = (int_val & NRATE_STF_MASK) >> NRATE_STF_SHIFT;
  5253. u8 rate = int_val & NRATE_RATE_MASK;
  5254. u32 rspec;
  5255. bool ismcs = ((int_val & NRATE_MCS_INUSE) == NRATE_MCS_INUSE);
  5256. bool issgi = ((int_val & NRATE_SGI_MASK) >> NRATE_SGI_SHIFT);
  5257. bool override_mcs_only = ((int_val & NRATE_OVERRIDE_MCS_ONLY)
  5258. == NRATE_OVERRIDE_MCS_ONLY);
  5259. int bcmerror = 0;
  5260. if (!ismcs)
  5261. return (u32) rate;
  5262. /* validate the combination of rate/mcs/stf is allowed */
  5263. if ((wlc->pub->_n_enab & SUPPORT_11N) && ismcs) {
  5264. /* mcs only allowed when nmode */
  5265. if (stf > PHY_TXC1_MODE_SDM) {
  5266. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid stf\n",
  5267. wlc->pub->unit, __func__);
  5268. bcmerror = -EINVAL;
  5269. goto done;
  5270. }
  5271. /* mcs 32 is a special case, DUP mode 40 only */
  5272. if (rate == 32) {
  5273. if (!CHSPEC_IS40(wlc->home_chanspec) ||
  5274. ((stf != PHY_TXC1_MODE_SISO)
  5275. && (stf != PHY_TXC1_MODE_CDD))) {
  5276. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid mcs "
  5277. "32\n", wlc->pub->unit, __func__);
  5278. bcmerror = -EINVAL;
  5279. goto done;
  5280. }
  5281. /* mcs > 7 must use stf SDM */
  5282. } else if (rate > HIGHEST_SINGLE_STREAM_MCS) {
  5283. /* mcs > 7 must use stf SDM */
  5284. if (stf != PHY_TXC1_MODE_SDM) {
  5285. BCMMSG(wlc->wiphy, "wl%d: enabling "
  5286. "SDM mode for mcs %d\n",
  5287. wlc->pub->unit, rate);
  5288. stf = PHY_TXC1_MODE_SDM;
  5289. }
  5290. } else {
  5291. /*
  5292. * MCS 0-7 may use SISO, CDD, and for
  5293. * phy_rev >= 3 STBC
  5294. */
  5295. if ((stf > PHY_TXC1_MODE_STBC) ||
  5296. (!BRCMS_STBC_CAP_PHY(wlc)
  5297. && (stf == PHY_TXC1_MODE_STBC))) {
  5298. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid STBC"
  5299. "\n", wlc->pub->unit, __func__);
  5300. bcmerror = -EINVAL;
  5301. goto done;
  5302. }
  5303. }
  5304. } else if (is_ofdm_rate(rate)) {
  5305. if ((stf != PHY_TXC1_MODE_CDD) && (stf != PHY_TXC1_MODE_SISO)) {
  5306. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid OFDM\n",
  5307. wlc->pub->unit, __func__);
  5308. bcmerror = -EINVAL;
  5309. goto done;
  5310. }
  5311. } else if (is_cck_rate(rate)) {
  5312. if ((cur_band->bandtype != BRCM_BAND_2G)
  5313. || (stf != PHY_TXC1_MODE_SISO)) {
  5314. wiphy_err(wlc->wiphy, "wl%d: %s: Invalid CCK\n",
  5315. wlc->pub->unit, __func__);
  5316. bcmerror = -EINVAL;
  5317. goto done;
  5318. }
  5319. } else {
  5320. wiphy_err(wlc->wiphy, "wl%d: %s: Unknown rate type\n",
  5321. wlc->pub->unit, __func__);
  5322. bcmerror = -EINVAL;
  5323. goto done;
  5324. }
  5325. /* make sure multiple antennae are available for non-siso rates */
  5326. if ((stf != PHY_TXC1_MODE_SISO) && (wlc->stf->txstreams == 1)) {
  5327. wiphy_err(wlc->wiphy, "wl%d: %s: SISO antenna but !SISO "
  5328. "request\n", wlc->pub->unit, __func__);
  5329. bcmerror = -EINVAL;
  5330. goto done;
  5331. }
  5332. rspec = rate;
  5333. if (ismcs) {
  5334. rspec |= RSPEC_MIMORATE;
  5335. /* For STBC populate the STC field of the ratespec */
  5336. if (stf == PHY_TXC1_MODE_STBC) {
  5337. u8 stc;
  5338. stc = 1; /* Nss for single stream is always 1 */
  5339. rspec |= (stc << RSPEC_STC_SHIFT);
  5340. }
  5341. }
  5342. rspec |= (stf << RSPEC_STF_SHIFT);
  5343. if (override_mcs_only)
  5344. rspec |= RSPEC_OVERRIDE_MCS_ONLY;
  5345. if (issgi)
  5346. rspec |= RSPEC_SHORT_GI;
  5347. if ((rate != 0)
  5348. && !brcms_c_valid_rate(wlc, rspec, cur_band->bandtype, true))
  5349. return rate;
  5350. return rspec;
  5351. done:
  5352. return rate;
  5353. }
  5354. /*
  5355. * Compute PLCP, but only requires actual rate and length of pkt.
  5356. * Rate is given in the driver standard multiple of 500 kbps.
  5357. * le is set for 11 Mbps rate if necessary.
  5358. * Broken out for PRQ.
  5359. */
  5360. static void brcms_c_cck_plcp_set(struct brcms_c_info *wlc, int rate_500,
  5361. uint length, u8 *plcp)
  5362. {
  5363. u16 usec = 0;
  5364. u8 le = 0;
  5365. switch (rate_500) {
  5366. case BRCM_RATE_1M:
  5367. usec = length << 3;
  5368. break;
  5369. case BRCM_RATE_2M:
  5370. usec = length << 2;
  5371. break;
  5372. case BRCM_RATE_5M5:
  5373. usec = (length << 4) / 11;
  5374. if ((length << 4) - (usec * 11) > 0)
  5375. usec++;
  5376. break;
  5377. case BRCM_RATE_11M:
  5378. usec = (length << 3) / 11;
  5379. if ((length << 3) - (usec * 11) > 0) {
  5380. usec++;
  5381. if ((usec * 11) - (length << 3) >= 8)
  5382. le = D11B_PLCP_SIGNAL_LE;
  5383. }
  5384. break;
  5385. default:
  5386. wiphy_err(wlc->wiphy,
  5387. "brcms_c_cck_plcp_set: unsupported rate %d\n",
  5388. rate_500);
  5389. rate_500 = BRCM_RATE_1M;
  5390. usec = length << 3;
  5391. break;
  5392. }
  5393. /* PLCP signal byte */
  5394. plcp[0] = rate_500 * 5; /* r (500kbps) * 5 == r (100kbps) */
  5395. /* PLCP service byte */
  5396. plcp[1] = (u8) (le | D11B_PLCP_SIGNAL_LOCKED);
  5397. /* PLCP length u16, little endian */
  5398. plcp[2] = usec & 0xff;
  5399. plcp[3] = (usec >> 8) & 0xff;
  5400. /* PLCP CRC16 */
  5401. plcp[4] = 0;
  5402. plcp[5] = 0;
  5403. }
  5404. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5405. static void brcms_c_compute_mimo_plcp(u32 rspec, uint length, u8 *plcp)
  5406. {
  5407. u8 mcs = (u8) (rspec & RSPEC_RATE_MASK);
  5408. plcp[0] = mcs;
  5409. if (rspec_is40mhz(rspec) || (mcs == 32))
  5410. plcp[0] |= MIMO_PLCP_40MHZ;
  5411. BRCMS_SET_MIMO_PLCP_LEN(plcp, length);
  5412. plcp[3] = rspec_mimoplcp3(rspec); /* rspec already holds this byte */
  5413. plcp[3] |= 0x7; /* set smoothing, not sounding ppdu & reserved */
  5414. plcp[4] = 0; /* number of extension spatial streams bit 0 & 1 */
  5415. plcp[5] = 0;
  5416. }
  5417. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5418. static void
  5419. brcms_c_compute_ofdm_plcp(u32 rspec, u32 length, u8 *plcp)
  5420. {
  5421. u8 rate_signal;
  5422. u32 tmp = 0;
  5423. int rate = rspec2rate(rspec);
  5424. /*
  5425. * encode rate per 802.11a-1999 sec 17.3.4.1, with lsb
  5426. * transmitted first
  5427. */
  5428. rate_signal = rate_info[rate] & BRCMS_RATE_MASK;
  5429. memset(plcp, 0, D11_PHY_HDR_LEN);
  5430. D11A_PHY_HDR_SRATE((struct ofdm_phy_hdr *) plcp, rate_signal);
  5431. tmp = (length & 0xfff) << 5;
  5432. plcp[2] |= (tmp >> 16) & 0xff;
  5433. plcp[1] |= (tmp >> 8) & 0xff;
  5434. plcp[0] |= tmp & 0xff;
  5435. }
  5436. /* Rate: 802.11 rate code, length: PSDU length in octets */
  5437. static void brcms_c_compute_cck_plcp(struct brcms_c_info *wlc, u32 rspec,
  5438. uint length, u8 *plcp)
  5439. {
  5440. int rate = rspec2rate(rspec);
  5441. brcms_c_cck_plcp_set(wlc, rate, length, plcp);
  5442. }
  5443. static void
  5444. brcms_c_compute_plcp(struct brcms_c_info *wlc, u32 rspec,
  5445. uint length, u8 *plcp)
  5446. {
  5447. if (is_mcs_rate(rspec))
  5448. brcms_c_compute_mimo_plcp(rspec, length, plcp);
  5449. else if (is_ofdm_rate(rspec))
  5450. brcms_c_compute_ofdm_plcp(rspec, length, plcp);
  5451. else
  5452. brcms_c_compute_cck_plcp(wlc, rspec, length, plcp);
  5453. }
  5454. /* brcms_c_compute_rtscts_dur()
  5455. *
  5456. * Calculate the 802.11 MAC header DUR field for an RTS or CTS frame
  5457. * DUR for normal RTS/CTS w/ frame = 3 SIFS + 1 CTS + next frame time + 1 ACK
  5458. * DUR for CTS-TO-SELF w/ frame = 2 SIFS + next frame time + 1 ACK
  5459. *
  5460. * cts cts-to-self or rts/cts
  5461. * rts_rate rts or cts rate in unit of 500kbps
  5462. * rate next MPDU rate in unit of 500kbps
  5463. * frame_len next MPDU frame length in bytes
  5464. */
  5465. u16
  5466. brcms_c_compute_rtscts_dur(struct brcms_c_info *wlc, bool cts_only,
  5467. u32 rts_rate,
  5468. u32 frame_rate, u8 rts_preamble_type,
  5469. u8 frame_preamble_type, uint frame_len, bool ba)
  5470. {
  5471. u16 dur, sifs;
  5472. sifs = get_sifs(wlc->band);
  5473. if (!cts_only) {
  5474. /* RTS/CTS */
  5475. dur = 3 * sifs;
  5476. dur +=
  5477. (u16) brcms_c_calc_cts_time(wlc, rts_rate,
  5478. rts_preamble_type);
  5479. } else {
  5480. /* CTS-TO-SELF */
  5481. dur = 2 * sifs;
  5482. }
  5483. dur +=
  5484. (u16) brcms_c_calc_frame_time(wlc, frame_rate, frame_preamble_type,
  5485. frame_len);
  5486. if (ba)
  5487. dur +=
  5488. (u16) brcms_c_calc_ba_time(wlc, frame_rate,
  5489. BRCMS_SHORT_PREAMBLE);
  5490. else
  5491. dur +=
  5492. (u16) brcms_c_calc_ack_time(wlc, frame_rate,
  5493. frame_preamble_type);
  5494. return dur;
  5495. }
  5496. static u16 brcms_c_phytxctl1_calc(struct brcms_c_info *wlc, u32 rspec)
  5497. {
  5498. u16 phyctl1 = 0;
  5499. u16 bw;
  5500. if (BRCMS_ISLCNPHY(wlc->band)) {
  5501. bw = PHY_TXC1_BW_20MHZ;
  5502. } else {
  5503. bw = rspec_get_bw(rspec);
  5504. /* 10Mhz is not supported yet */
  5505. if (bw < PHY_TXC1_BW_20MHZ) {
  5506. wiphy_err(wlc->wiphy, "phytxctl1_calc: bw %d is "
  5507. "not supported yet, set to 20L\n", bw);
  5508. bw = PHY_TXC1_BW_20MHZ;
  5509. }
  5510. }
  5511. if (is_mcs_rate(rspec)) {
  5512. uint mcs = rspec & RSPEC_RATE_MASK;
  5513. /* bw, stf, coding-type is part of rspec_phytxbyte2 returns */
  5514. phyctl1 = rspec_phytxbyte2(rspec);
  5515. /* set the upper byte of phyctl1 */
  5516. phyctl1 |= (mcs_table[mcs].tx_phy_ctl3 << 8);
  5517. } else if (is_cck_rate(rspec) && !BRCMS_ISLCNPHY(wlc->band)
  5518. && !BRCMS_ISSSLPNPHY(wlc->band)) {
  5519. /*
  5520. * In CCK mode LPPHY overloads OFDM Modulation bits with CCK
  5521. * Data Rate. Eventually MIMOPHY would also be converted to
  5522. * this format
  5523. */
  5524. /* 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps */
  5525. phyctl1 = (bw | (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5526. } else { /* legacy OFDM/CCK */
  5527. s16 phycfg;
  5528. /* get the phyctl byte from rate phycfg table */
  5529. phycfg = brcms_c_rate_legacy_phyctl(rspec2rate(rspec));
  5530. if (phycfg == -1) {
  5531. wiphy_err(wlc->wiphy, "phytxctl1_calc: wrong "
  5532. "legacy OFDM/CCK rate\n");
  5533. phycfg = 0;
  5534. }
  5535. /* set the upper byte of phyctl1 */
  5536. phyctl1 =
  5537. (bw | (phycfg << 8) |
  5538. (rspec_stf(rspec) << PHY_TXC1_MODE_SHIFT));
  5539. }
  5540. return phyctl1;
  5541. }
  5542. /*
  5543. * Add struct d11txh, struct cck_phy_hdr.
  5544. *
  5545. * 'p' data must start with 802.11 MAC header
  5546. * 'p' must allow enough bytes of local headers to be "pushed" onto the packet
  5547. *
  5548. * headroom == D11_PHY_HDR_LEN + D11_TXH_LEN (D11_TXH_LEN is now 104 bytes)
  5549. *
  5550. */
  5551. static u16
  5552. brcms_c_d11hdrs_mac80211(struct brcms_c_info *wlc, struct ieee80211_hw *hw,
  5553. struct sk_buff *p, struct scb *scb, uint frag,
  5554. uint nfrags, uint queue, uint next_frag_len)
  5555. {
  5556. struct ieee80211_hdr *h;
  5557. struct d11txh *txh;
  5558. u8 *plcp, plcp_fallback[D11_PHY_HDR_LEN];
  5559. int len, phylen, rts_phylen;
  5560. u16 mch, phyctl, xfts, mainrates;
  5561. u16 seq = 0, mcl = 0, status = 0, frameid = 0;
  5562. u32 rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5563. u32 rts_rspec[2] = { BRCM_RATE_1M, BRCM_RATE_1M };
  5564. bool use_rts = false;
  5565. bool use_cts = false;
  5566. bool use_rifs = false;
  5567. bool short_preamble[2] = { false, false };
  5568. u8 preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5569. u8 rts_preamble_type[2] = { BRCMS_LONG_PREAMBLE, BRCMS_LONG_PREAMBLE };
  5570. u8 *rts_plcp, rts_plcp_fallback[D11_PHY_HDR_LEN];
  5571. struct ieee80211_rts *rts = NULL;
  5572. bool qos;
  5573. uint ac;
  5574. bool hwtkmic = false;
  5575. u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;
  5576. #define ANTCFG_NONE 0xFF
  5577. u8 antcfg = ANTCFG_NONE;
  5578. u8 fbantcfg = ANTCFG_NONE;
  5579. uint phyctl1_stf = 0;
  5580. u16 durid = 0;
  5581. struct ieee80211_tx_rate *txrate[2];
  5582. int k;
  5583. struct ieee80211_tx_info *tx_info;
  5584. bool is_mcs;
  5585. u16 mimo_txbw;
  5586. u8 mimo_preamble_type;
  5587. /* locate 802.11 MAC header */
  5588. h = (struct ieee80211_hdr *)(p->data);
  5589. qos = ieee80211_is_data_qos(h->frame_control);
  5590. /* compute length of frame in bytes for use in PLCP computations */
  5591. len = p->len;
  5592. phylen = len + FCS_LEN;
  5593. /* Get tx_info */
  5594. tx_info = IEEE80211_SKB_CB(p);
  5595. /* add PLCP */
  5596. plcp = skb_push(p, D11_PHY_HDR_LEN);
  5597. /* add Broadcom tx descriptor header */
  5598. txh = (struct d11txh *) skb_push(p, D11_TXH_LEN);
  5599. memset(txh, 0, D11_TXH_LEN);
  5600. /* setup frameid */
  5601. if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  5602. /* non-AP STA should never use BCMC queue */
  5603. if (queue == TX_BCMC_FIFO) {
  5604. wiphy_err(wlc->wiphy, "wl%d: %s: ASSERT queue == "
  5605. "TX_BCMC!\n", wlc->pub->unit, __func__);
  5606. frameid = bcmc_fid_generate(wlc, NULL, txh);
  5607. } else {
  5608. /* Increment the counter for first fragment */
  5609. if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  5610. scb->seqnum[p->priority]++;
  5611. /* extract fragment number from frame first */
  5612. seq = le16_to_cpu(h->seq_ctrl) & FRAGNUM_MASK;
  5613. seq |= (scb->seqnum[p->priority] << SEQNUM_SHIFT);
  5614. h->seq_ctrl = cpu_to_le16(seq);
  5615. frameid = ((seq << TXFID_SEQ_SHIFT) & TXFID_SEQ_MASK) |
  5616. (queue & TXFID_QUEUE_MASK);
  5617. }
  5618. }
  5619. frameid |= queue & TXFID_QUEUE_MASK;
  5620. /* set the ignpmq bit for all pkts tx'd in PS mode and for beacons */
  5621. if (ieee80211_is_beacon(h->frame_control))
  5622. mcl |= TXC_IGNOREPMQ;
  5623. txrate[0] = tx_info->control.rates;
  5624. txrate[1] = txrate[0] + 1;
  5625. /*
  5626. * if rate control algorithm didn't give us a fallback
  5627. * rate, use the primary rate
  5628. */
  5629. if (txrate[1]->idx < 0)
  5630. txrate[1] = txrate[0];
  5631. for (k = 0; k < hw->max_rates; k++) {
  5632. is_mcs = txrate[k]->flags & IEEE80211_TX_RC_MCS ? true : false;
  5633. if (!is_mcs) {
  5634. if ((txrate[k]->idx >= 0)
  5635. && (txrate[k]->idx <
  5636. hw->wiphy->bands[tx_info->band]->n_bitrates)) {
  5637. rspec[k] =
  5638. hw->wiphy->bands[tx_info->band]->
  5639. bitrates[txrate[k]->idx].hw_value;
  5640. short_preamble[k] =
  5641. txrate[k]->
  5642. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ?
  5643. true : false;
  5644. } else {
  5645. rspec[k] = BRCM_RATE_1M;
  5646. }
  5647. } else {
  5648. rspec[k] = mac80211_wlc_set_nrate(wlc, wlc->band,
  5649. NRATE_MCS_INUSE | txrate[k]->idx);
  5650. }
  5651. /*
  5652. * Currently only support same setting for primay and
  5653. * fallback rates. Unify flags for each rate into a
  5654. * single value for the frame
  5655. */
  5656. use_rts |=
  5657. txrate[k]->
  5658. flags & IEEE80211_TX_RC_USE_RTS_CTS ? true : false;
  5659. use_cts |=
  5660. txrate[k]->
  5661. flags & IEEE80211_TX_RC_USE_CTS_PROTECT ? true : false;
  5662. /*
  5663. * (1) RATE:
  5664. * determine and validate primary rate
  5665. * and fallback rates
  5666. */
  5667. if (!rspec_active(rspec[k])) {
  5668. rspec[k] = BRCM_RATE_1M;
  5669. } else {
  5670. if (!is_multicast_ether_addr(h->addr1)) {
  5671. /* set tx antenna config */
  5672. brcms_c_antsel_antcfg_get(wlc->asi, false,
  5673. false, 0, 0, &antcfg, &fbantcfg);
  5674. }
  5675. }
  5676. }
  5677. phyctl1_stf = wlc->stf->ss_opmode;
  5678. if (wlc->pub->_n_enab & SUPPORT_11N) {
  5679. for (k = 0; k < hw->max_rates; k++) {
  5680. /*
  5681. * apply siso/cdd to single stream mcs's or ofdm
  5682. * if rspec is auto selected
  5683. */
  5684. if (((is_mcs_rate(rspec[k]) &&
  5685. is_single_stream(rspec[k] & RSPEC_RATE_MASK)) ||
  5686. is_ofdm_rate(rspec[k]))
  5687. && ((rspec[k] & RSPEC_OVERRIDE_MCS_ONLY)
  5688. || !(rspec[k] & RSPEC_OVERRIDE))) {
  5689. rspec[k] &= ~(RSPEC_STF_MASK | RSPEC_STC_MASK);
  5690. /* For SISO MCS use STBC if possible */
  5691. if (is_mcs_rate(rspec[k])
  5692. && BRCMS_STF_SS_STBC_TX(wlc, scb)) {
  5693. u8 stc;
  5694. /* Nss for single stream is always 1 */
  5695. stc = 1;
  5696. rspec[k] |= (PHY_TXC1_MODE_STBC <<
  5697. RSPEC_STF_SHIFT) |
  5698. (stc << RSPEC_STC_SHIFT);
  5699. } else
  5700. rspec[k] |=
  5701. (phyctl1_stf << RSPEC_STF_SHIFT);
  5702. }
  5703. /*
  5704. * Is the phy configured to use 40MHZ frames? If
  5705. * so then pick the desired txbw
  5706. */
  5707. if (brcms_chspec_bw(wlc->chanspec) == BRCMS_40_MHZ) {
  5708. /* default txbw is 20in40 SB */
  5709. mimo_ctlchbw = mimo_txbw =
  5710. CHSPEC_SB_UPPER(wlc_phy_chanspec_get(
  5711. wlc->band->pi))
  5712. ? PHY_TXC1_BW_20MHZ_UP : PHY_TXC1_BW_20MHZ;
  5713. if (is_mcs_rate(rspec[k])) {
  5714. /* mcs 32 must be 40b/w DUP */
  5715. if ((rspec[k] & RSPEC_RATE_MASK)
  5716. == 32) {
  5717. mimo_txbw =
  5718. PHY_TXC1_BW_40MHZ_DUP;
  5719. /* use override */
  5720. } else if (wlc->mimo_40txbw != AUTO)
  5721. mimo_txbw = wlc->mimo_40txbw;
  5722. /* else check if dst is using 40 Mhz */
  5723. else if (scb->flags & SCB_IS40)
  5724. mimo_txbw = PHY_TXC1_BW_40MHZ;
  5725. } else if (is_ofdm_rate(rspec[k])) {
  5726. if (wlc->ofdm_40txbw != AUTO)
  5727. mimo_txbw = wlc->ofdm_40txbw;
  5728. } else if (wlc->cck_40txbw != AUTO) {
  5729. mimo_txbw = wlc->cck_40txbw;
  5730. }
  5731. } else {
  5732. /*
  5733. * mcs32 is 40 b/w only.
  5734. * This is possible for probe packets on
  5735. * a STA during SCAN
  5736. */
  5737. if ((rspec[k] & RSPEC_RATE_MASK) == 32)
  5738. /* mcs 0 */
  5739. rspec[k] = RSPEC_MIMORATE;
  5740. mimo_txbw = PHY_TXC1_BW_20MHZ;
  5741. }
  5742. /* Set channel width */
  5743. rspec[k] &= ~RSPEC_BW_MASK;
  5744. if ((k == 0) || ((k > 0) && is_mcs_rate(rspec[k])))
  5745. rspec[k] |= (mimo_txbw << RSPEC_BW_SHIFT);
  5746. else
  5747. rspec[k] |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  5748. /* Disable short GI, not supported yet */
  5749. rspec[k] &= ~RSPEC_SHORT_GI;
  5750. mimo_preamble_type = BRCMS_MM_PREAMBLE;
  5751. if (txrate[k]->flags & IEEE80211_TX_RC_GREEN_FIELD)
  5752. mimo_preamble_type = BRCMS_GF_PREAMBLE;
  5753. if ((txrate[k]->flags & IEEE80211_TX_RC_MCS)
  5754. && (!is_mcs_rate(rspec[k]))) {
  5755. wiphy_err(wlc->wiphy, "wl%d: %s: IEEE80211_TX_"
  5756. "RC_MCS != is_mcs_rate(rspec)\n",
  5757. wlc->pub->unit, __func__);
  5758. }
  5759. if (is_mcs_rate(rspec[k])) {
  5760. preamble_type[k] = mimo_preamble_type;
  5761. /*
  5762. * if SGI is selected, then forced mm
  5763. * for single stream
  5764. */
  5765. if ((rspec[k] & RSPEC_SHORT_GI)
  5766. && is_single_stream(rspec[k] &
  5767. RSPEC_RATE_MASK))
  5768. preamble_type[k] = BRCMS_MM_PREAMBLE;
  5769. }
  5770. /* should be better conditionalized */
  5771. if (!is_mcs_rate(rspec[0])
  5772. && (tx_info->control.rates[0].
  5773. flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
  5774. preamble_type[k] = BRCMS_SHORT_PREAMBLE;
  5775. }
  5776. } else {
  5777. for (k = 0; k < hw->max_rates; k++) {
  5778. /* Set ctrlchbw as 20Mhz */
  5779. rspec[k] &= ~RSPEC_BW_MASK;
  5780. rspec[k] |= (PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT);
  5781. /* for nphy, stf of ofdm frames must follow policies */
  5782. if (BRCMS_ISNPHY(wlc->band) && is_ofdm_rate(rspec[k])) {
  5783. rspec[k] &= ~RSPEC_STF_MASK;
  5784. rspec[k] |= phyctl1_stf << RSPEC_STF_SHIFT;
  5785. }
  5786. }
  5787. }
  5788. /* Reset these for use with AMPDU's */
  5789. txrate[0]->count = 0;
  5790. txrate[1]->count = 0;
  5791. /* (2) PROTECTION, may change rspec */
  5792. if ((ieee80211_is_data(h->frame_control) ||
  5793. ieee80211_is_mgmt(h->frame_control)) &&
  5794. (phylen > wlc->RTSThresh) && !is_multicast_ether_addr(h->addr1))
  5795. use_rts = true;
  5796. /* (3) PLCP: determine PLCP header and MAC duration,
  5797. * fill struct d11txh */
  5798. brcms_c_compute_plcp(wlc, rspec[0], phylen, plcp);
  5799. brcms_c_compute_plcp(wlc, rspec[1], phylen, plcp_fallback);
  5800. memcpy(&txh->FragPLCPFallback,
  5801. plcp_fallback, sizeof(txh->FragPLCPFallback));
  5802. /* Length field now put in CCK FBR CRC field */
  5803. if (is_cck_rate(rspec[1])) {
  5804. txh->FragPLCPFallback[4] = phylen & 0xff;
  5805. txh->FragPLCPFallback[5] = (phylen & 0xff00) >> 8;
  5806. }
  5807. /* MIMO-RATE: need validation ?? */
  5808. mainrates = is_ofdm_rate(rspec[0]) ?
  5809. D11A_PHY_HDR_GRATE((struct ofdm_phy_hdr *) plcp) :
  5810. plcp[0];
  5811. /* DUR field for main rate */
  5812. if (!ieee80211_is_pspoll(h->frame_control) &&
  5813. !is_multicast_ether_addr(h->addr1) && !use_rifs) {
  5814. durid =
  5815. brcms_c_compute_frame_dur(wlc, rspec[0], preamble_type[0],
  5816. next_frag_len);
  5817. h->duration_id = cpu_to_le16(durid);
  5818. } else if (use_rifs) {
  5819. /* NAV protect to end of next max packet size */
  5820. durid =
  5821. (u16) brcms_c_calc_frame_time(wlc, rspec[0],
  5822. preamble_type[0],
  5823. DOT11_MAX_FRAG_LEN);
  5824. durid += RIFS_11N_TIME;
  5825. h->duration_id = cpu_to_le16(durid);
  5826. }
  5827. /* DUR field for fallback rate */
  5828. if (ieee80211_is_pspoll(h->frame_control))
  5829. txh->FragDurFallback = h->duration_id;
  5830. else if (is_multicast_ether_addr(h->addr1) || use_rifs)
  5831. txh->FragDurFallback = 0;
  5832. else {
  5833. durid = brcms_c_compute_frame_dur(wlc, rspec[1],
  5834. preamble_type[1], next_frag_len);
  5835. txh->FragDurFallback = cpu_to_le16(durid);
  5836. }
  5837. /* (4) MAC-HDR: MacTxControlLow */
  5838. if (frag == 0)
  5839. mcl |= TXC_STARTMSDU;
  5840. if (!is_multicast_ether_addr(h->addr1))
  5841. mcl |= TXC_IMMEDACK;
  5842. if (wlc->band->bandtype == BRCM_BAND_5G)
  5843. mcl |= TXC_FREQBAND_5G;
  5844. if (CHSPEC_IS40(wlc_phy_chanspec_get(wlc->band->pi)))
  5845. mcl |= TXC_BW_40;
  5846. /* set AMIC bit if using hardware TKIP MIC */
  5847. if (hwtkmic)
  5848. mcl |= TXC_AMIC;
  5849. txh->MacTxControlLow = cpu_to_le16(mcl);
  5850. /* MacTxControlHigh */
  5851. mch = 0;
  5852. /* Set fallback rate preamble type */
  5853. if ((preamble_type[1] == BRCMS_SHORT_PREAMBLE) ||
  5854. (preamble_type[1] == BRCMS_GF_PREAMBLE)) {
  5855. if (rspec2rate(rspec[1]) != BRCM_RATE_1M)
  5856. mch |= TXC_PREAMBLE_DATA_FB_SHORT;
  5857. }
  5858. /* MacFrameControl */
  5859. memcpy(&txh->MacFrameControl, &h->frame_control, sizeof(u16));
  5860. txh->TxFesTimeNormal = cpu_to_le16(0);
  5861. txh->TxFesTimeFallback = cpu_to_le16(0);
  5862. /* TxFrameRA */
  5863. memcpy(&txh->TxFrameRA, &h->addr1, ETH_ALEN);
  5864. /* TxFrameID */
  5865. txh->TxFrameID = cpu_to_le16(frameid);
  5866. /*
  5867. * TxStatus, Note the case of recreating the first frag of a suppressed
  5868. * frame then we may need to reset the retry cnt's via the status reg
  5869. */
  5870. txh->TxStatus = cpu_to_le16(status);
  5871. /*
  5872. * extra fields for ucode AMPDU aggregation, the new fields are added to
  5873. * the END of previous structure so that it's compatible in driver.
  5874. */
  5875. txh->MaxNMpdus = cpu_to_le16(0);
  5876. txh->MaxABytes_MRT = cpu_to_le16(0);
  5877. txh->MaxABytes_FBR = cpu_to_le16(0);
  5878. txh->MinMBytes = cpu_to_le16(0);
  5879. /* (5) RTS/CTS: determine RTS/CTS PLCP header and MAC duration,
  5880. * furnish struct d11txh */
  5881. /* RTS PLCP header and RTS frame */
  5882. if (use_rts || use_cts) {
  5883. if (use_rts && use_cts)
  5884. use_cts = false;
  5885. for (k = 0; k < 2; k++) {
  5886. rts_rspec[k] = brcms_c_rspec_to_rts_rspec(wlc, rspec[k],
  5887. false,
  5888. mimo_ctlchbw);
  5889. }
  5890. if (!is_ofdm_rate(rts_rspec[0]) &&
  5891. !((rspec2rate(rts_rspec[0]) == BRCM_RATE_1M) ||
  5892. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5893. rts_preamble_type[0] = BRCMS_SHORT_PREAMBLE;
  5894. mch |= TXC_PREAMBLE_RTS_MAIN_SHORT;
  5895. }
  5896. if (!is_ofdm_rate(rts_rspec[1]) &&
  5897. !((rspec2rate(rts_rspec[1]) == BRCM_RATE_1M) ||
  5898. (wlc->PLCPHdr_override == BRCMS_PLCP_LONG))) {
  5899. rts_preamble_type[1] = BRCMS_SHORT_PREAMBLE;
  5900. mch |= TXC_PREAMBLE_RTS_FB_SHORT;
  5901. }
  5902. /* RTS/CTS additions to MacTxControlLow */
  5903. if (use_cts) {
  5904. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDCTS);
  5905. } else {
  5906. txh->MacTxControlLow |= cpu_to_le16(TXC_SENDRTS);
  5907. txh->MacTxControlLow |= cpu_to_le16(TXC_LONGFRAME);
  5908. }
  5909. /* RTS PLCP header */
  5910. rts_plcp = txh->RTSPhyHeader;
  5911. if (use_cts)
  5912. rts_phylen = DOT11_CTS_LEN + FCS_LEN;
  5913. else
  5914. rts_phylen = DOT11_RTS_LEN + FCS_LEN;
  5915. brcms_c_compute_plcp(wlc, rts_rspec[0], rts_phylen, rts_plcp);
  5916. /* fallback rate version of RTS PLCP header */
  5917. brcms_c_compute_plcp(wlc, rts_rspec[1], rts_phylen,
  5918. rts_plcp_fallback);
  5919. memcpy(&txh->RTSPLCPFallback, rts_plcp_fallback,
  5920. sizeof(txh->RTSPLCPFallback));
  5921. /* RTS frame fields... */
  5922. rts = (struct ieee80211_rts *)&txh->rts_frame;
  5923. durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec[0],
  5924. rspec[0], rts_preamble_type[0],
  5925. preamble_type[0], phylen, false);
  5926. rts->duration = cpu_to_le16(durid);
  5927. /* fallback rate version of RTS DUR field */
  5928. durid = brcms_c_compute_rtscts_dur(wlc, use_cts,
  5929. rts_rspec[1], rspec[1],
  5930. rts_preamble_type[1],
  5931. preamble_type[1], phylen, false);
  5932. txh->RTSDurFallback = cpu_to_le16(durid);
  5933. if (use_cts) {
  5934. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5935. IEEE80211_STYPE_CTS);
  5936. memcpy(&rts->ra, &h->addr2, ETH_ALEN);
  5937. } else {
  5938. rts->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  5939. IEEE80211_STYPE_RTS);
  5940. memcpy(&rts->ra, &h->addr1, 2 * ETH_ALEN);
  5941. }
  5942. /* mainrate
  5943. * low 8 bits: main frag rate/mcs,
  5944. * high 8 bits: rts/cts rate/mcs
  5945. */
  5946. mainrates |= (is_ofdm_rate(rts_rspec[0]) ?
  5947. D11A_PHY_HDR_GRATE(
  5948. (struct ofdm_phy_hdr *) rts_plcp) :
  5949. rts_plcp[0]) << 8;
  5950. } else {
  5951. memset((char *)txh->RTSPhyHeader, 0, D11_PHY_HDR_LEN);
  5952. memset((char *)&txh->rts_frame, 0,
  5953. sizeof(struct ieee80211_rts));
  5954. memset((char *)txh->RTSPLCPFallback, 0,
  5955. sizeof(txh->RTSPLCPFallback));
  5956. txh->RTSDurFallback = 0;
  5957. }
  5958. #ifdef SUPPORT_40MHZ
  5959. /* add null delimiter count */
  5960. if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && is_mcs_rate(rspec))
  5961. txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] =
  5962. brcm_c_ampdu_null_delim_cnt(wlc->ampdu, scb, rspec, phylen);
  5963. #endif
  5964. /*
  5965. * Now that RTS/RTS FB preamble types are updated, write
  5966. * the final value
  5967. */
  5968. txh->MacTxControlHigh = cpu_to_le16(mch);
  5969. /*
  5970. * MainRates (both the rts and frag plcp rates have
  5971. * been calculated now)
  5972. */
  5973. txh->MainRates = cpu_to_le16(mainrates);
  5974. /* XtraFrameTypes */
  5975. xfts = frametype(rspec[1], wlc->mimoft);
  5976. xfts |= (frametype(rts_rspec[0], wlc->mimoft) << XFTS_RTS_FT_SHIFT);
  5977. xfts |= (frametype(rts_rspec[1], wlc->mimoft) << XFTS_FBRRTS_FT_SHIFT);
  5978. xfts |= CHSPEC_CHANNEL(wlc_phy_chanspec_get(wlc->band->pi)) <<
  5979. XFTS_CHANNEL_SHIFT;
  5980. txh->XtraFrameTypes = cpu_to_le16(xfts);
  5981. /* PhyTxControlWord */
  5982. phyctl = frametype(rspec[0], wlc->mimoft);
  5983. if ((preamble_type[0] == BRCMS_SHORT_PREAMBLE) ||
  5984. (preamble_type[0] == BRCMS_GF_PREAMBLE)) {
  5985. if (rspec2rate(rspec[0]) != BRCM_RATE_1M)
  5986. phyctl |= PHY_TXC_SHORT_HDR;
  5987. }
  5988. /* phytxant is properly bit shifted */
  5989. phyctl |= brcms_c_stf_d11hdrs_phyctl_txant(wlc, rspec[0]);
  5990. txh->PhyTxControlWord = cpu_to_le16(phyctl);
  5991. /* PhyTxControlWord_1 */
  5992. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  5993. u16 phyctl1 = 0;
  5994. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[0]);
  5995. txh->PhyTxControlWord_1 = cpu_to_le16(phyctl1);
  5996. phyctl1 = brcms_c_phytxctl1_calc(wlc, rspec[1]);
  5997. txh->PhyTxControlWord_1_Fbr = cpu_to_le16(phyctl1);
  5998. if (use_rts || use_cts) {
  5999. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[0]);
  6000. txh->PhyTxControlWord_1_Rts = cpu_to_le16(phyctl1);
  6001. phyctl1 = brcms_c_phytxctl1_calc(wlc, rts_rspec[1]);
  6002. txh->PhyTxControlWord_1_FbrRts = cpu_to_le16(phyctl1);
  6003. }
  6004. /*
  6005. * For mcs frames, if mixedmode(overloaded with long preamble)
  6006. * is going to be set, fill in non-zero MModeLen and/or
  6007. * MModeFbrLen it will be unnecessary if they are separated
  6008. */
  6009. if (is_mcs_rate(rspec[0]) &&
  6010. (preamble_type[0] == BRCMS_MM_PREAMBLE)) {
  6011. u16 mmodelen =
  6012. brcms_c_calc_lsig_len(wlc, rspec[0], phylen);
  6013. txh->MModeLen = cpu_to_le16(mmodelen);
  6014. }
  6015. if (is_mcs_rate(rspec[1]) &&
  6016. (preamble_type[1] == BRCMS_MM_PREAMBLE)) {
  6017. u16 mmodefbrlen =
  6018. brcms_c_calc_lsig_len(wlc, rspec[1], phylen);
  6019. txh->MModeFbrLen = cpu_to_le16(mmodefbrlen);
  6020. }
  6021. }
  6022. ac = skb_get_queue_mapping(p);
  6023. if ((scb->flags & SCB_WMECAP) && qos && wlc->edcf_txop[ac]) {
  6024. uint frag_dur, dur, dur_fallback;
  6025. /* WME: Update TXOP threshold */
  6026. if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU) && frag == 0) {
  6027. frag_dur =
  6028. brcms_c_calc_frame_time(wlc, rspec[0],
  6029. preamble_type[0], phylen);
  6030. if (rts) {
  6031. /* 1 RTS or CTS-to-self frame */
  6032. dur =
  6033. brcms_c_calc_cts_time(wlc, rts_rspec[0],
  6034. rts_preamble_type[0]);
  6035. dur_fallback =
  6036. brcms_c_calc_cts_time(wlc, rts_rspec[1],
  6037. rts_preamble_type[1]);
  6038. /* (SIFS + CTS) + SIFS + frame + SIFS + ACK */
  6039. dur += le16_to_cpu(rts->duration);
  6040. dur_fallback +=
  6041. le16_to_cpu(txh->RTSDurFallback);
  6042. } else if (use_rifs) {
  6043. dur = frag_dur;
  6044. dur_fallback = 0;
  6045. } else {
  6046. /* frame + SIFS + ACK */
  6047. dur = frag_dur;
  6048. dur +=
  6049. brcms_c_compute_frame_dur(wlc, rspec[0],
  6050. preamble_type[0], 0);
  6051. dur_fallback =
  6052. brcms_c_calc_frame_time(wlc, rspec[1],
  6053. preamble_type[1],
  6054. phylen);
  6055. dur_fallback +=
  6056. brcms_c_compute_frame_dur(wlc, rspec[1],
  6057. preamble_type[1], 0);
  6058. }
  6059. /* NEED to set TxFesTimeNormal (hard) */
  6060. txh->TxFesTimeNormal = cpu_to_le16((u16) dur);
  6061. /*
  6062. * NEED to set fallback rate version of
  6063. * TxFesTimeNormal (hard)
  6064. */
  6065. txh->TxFesTimeFallback =
  6066. cpu_to_le16((u16) dur_fallback);
  6067. /*
  6068. * update txop byte threshold (txop minus intraframe
  6069. * overhead)
  6070. */
  6071. if (wlc->edcf_txop[ac] >= (dur - frag_dur)) {
  6072. uint newfragthresh;
  6073. newfragthresh =
  6074. brcms_c_calc_frame_len(wlc,
  6075. rspec[0], preamble_type[0],
  6076. (wlc->edcf_txop[ac] -
  6077. (dur - frag_dur)));
  6078. /* range bound the fragthreshold */
  6079. if (newfragthresh < DOT11_MIN_FRAG_LEN)
  6080. newfragthresh =
  6081. DOT11_MIN_FRAG_LEN;
  6082. else if (newfragthresh >
  6083. wlc->usr_fragthresh)
  6084. newfragthresh =
  6085. wlc->usr_fragthresh;
  6086. /* update the fragthresh and do txc update */
  6087. if (wlc->fragthresh[queue] !=
  6088. (u16) newfragthresh)
  6089. wlc->fragthresh[queue] =
  6090. (u16) newfragthresh;
  6091. } else {
  6092. wiphy_err(wlc->wiphy, "wl%d: %s txop invalid "
  6093. "for rate %d\n",
  6094. wlc->pub->unit, fifo_names[queue],
  6095. rspec2rate(rspec[0]));
  6096. }
  6097. if (dur > wlc->edcf_txop[ac])
  6098. wiphy_err(wlc->wiphy, "wl%d: %s: %s txop "
  6099. "exceeded phylen %d/%d dur %d/%d\n",
  6100. wlc->pub->unit, __func__,
  6101. fifo_names[queue],
  6102. phylen, wlc->fragthresh[queue],
  6103. dur, wlc->edcf_txop[ac]);
  6104. }
  6105. }
  6106. return 0;
  6107. }
  6108. void brcms_c_sendpkt_mac80211(struct brcms_c_info *wlc, struct sk_buff *sdu,
  6109. struct ieee80211_hw *hw)
  6110. {
  6111. u8 prio;
  6112. uint fifo;
  6113. struct scb *scb = &wlc->pri_scb;
  6114. struct ieee80211_hdr *d11_header = (struct ieee80211_hdr *)(sdu->data);
  6115. /*
  6116. * 802.11 standard requires management traffic
  6117. * to go at highest priority
  6118. */
  6119. prio = ieee80211_is_data(d11_header->frame_control) ? sdu->priority :
  6120. MAXPRIO;
  6121. fifo = prio2fifo[prio];
  6122. if (brcms_c_d11hdrs_mac80211(wlc, hw, sdu, scb, 0, 1, fifo, 0))
  6123. return;
  6124. brcms_c_txq_enq(wlc, scb, sdu, BRCMS_PRIO_TO_PREC(prio));
  6125. brcms_c_send_q(wlc);
  6126. }
  6127. void brcms_c_send_q(struct brcms_c_info *wlc)
  6128. {
  6129. struct sk_buff *pkt[DOT11_MAXNUMFRAGS];
  6130. int prec;
  6131. u16 prec_map;
  6132. int err = 0, i, count;
  6133. uint fifo;
  6134. struct brcms_txq_info *qi = wlc->pkt_queue;
  6135. struct pktq *q = &qi->q;
  6136. struct ieee80211_tx_info *tx_info;
  6137. prec_map = wlc->tx_prec_map;
  6138. /* Send all the enq'd pkts that we can.
  6139. * Dequeue packets with precedence with empty HW fifo only
  6140. */
  6141. while (prec_map && (pkt[0] = brcmu_pktq_mdeq(q, prec_map, &prec))) {
  6142. tx_info = IEEE80211_SKB_CB(pkt[0]);
  6143. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  6144. err = brcms_c_sendampdu(wlc->ampdu, qi, pkt, prec);
  6145. } else {
  6146. count = 1;
  6147. err = brcms_c_prep_pdu(wlc, pkt[0], &fifo);
  6148. if (!err) {
  6149. for (i = 0; i < count; i++)
  6150. brcms_c_txfifo(wlc, fifo, pkt[i], true,
  6151. 1);
  6152. }
  6153. }
  6154. if (err == -EBUSY) {
  6155. brcmu_pktq_penq_head(q, prec, pkt[0]);
  6156. /*
  6157. * If send failed due to any other reason than a
  6158. * change in HW FIFO condition, quit. Otherwise,
  6159. * read the new prec_map!
  6160. */
  6161. if (prec_map == wlc->tx_prec_map)
  6162. break;
  6163. prec_map = wlc->tx_prec_map;
  6164. }
  6165. }
  6166. }
  6167. void
  6168. brcms_c_txfifo(struct brcms_c_info *wlc, uint fifo, struct sk_buff *p,
  6169. bool commit, s8 txpktpend)
  6170. {
  6171. u16 frameid = INVALIDFID;
  6172. struct d11txh *txh;
  6173. txh = (struct d11txh *) (p->data);
  6174. /* When a BC/MC frame is being committed to the BCMC fifo
  6175. * via DMA (NOT PIO), update ucode or BSS info as appropriate.
  6176. */
  6177. if (fifo == TX_BCMC_FIFO)
  6178. frameid = le16_to_cpu(txh->TxFrameID);
  6179. /*
  6180. * Bump up pending count for if not using rpc. If rpc is
  6181. * used, this will be handled in brcms_b_txfifo()
  6182. */
  6183. if (commit) {
  6184. wlc->core->txpktpend[fifo] += txpktpend;
  6185. BCMMSG(wlc->wiphy, "pktpend inc %d to %d\n",
  6186. txpktpend, wlc->core->txpktpend[fifo]);
  6187. }
  6188. /* Commit BCMC sequence number in the SHM frame ID location */
  6189. if (frameid != INVALIDFID) {
  6190. /*
  6191. * To inform the ucode of the last mcast frame posted
  6192. * so that it can clear moredata bit
  6193. */
  6194. brcms_b_write_shm(wlc->hw, M_BCMC_FID, frameid);
  6195. }
  6196. if (dma_txfast(wlc->hw->di[fifo], p, commit) < 0)
  6197. wiphy_err(wlc->wiphy, "txfifo: fatal, toss frames !!!\n");
  6198. }
  6199. u32
  6200. brcms_c_rspec_to_rts_rspec(struct brcms_c_info *wlc, u32 rspec,
  6201. bool use_rspec, u16 mimo_ctlchbw)
  6202. {
  6203. u32 rts_rspec = 0;
  6204. if (use_rspec)
  6205. /* use frame rate as rts rate */
  6206. rts_rspec = rspec;
  6207. else if (wlc->band->gmode && wlc->protection->_g && !is_cck_rate(rspec))
  6208. /* Use 11Mbps as the g protection RTS target rate and fallback.
  6209. * Use the brcms_basic_rate() lookup to find the best basic rate
  6210. * under the target in case 11 Mbps is not Basic.
  6211. * 6 and 9 Mbps are not usually selected by rate selection, but
  6212. * even if the OFDM rate we are protecting is 6 or 9 Mbps, 11
  6213. * is more robust.
  6214. */
  6215. rts_rspec = brcms_basic_rate(wlc, BRCM_RATE_11M);
  6216. else
  6217. /* calculate RTS rate and fallback rate based on the frame rate
  6218. * RTS must be sent at a basic rate since it is a
  6219. * control frame, sec 9.6 of 802.11 spec
  6220. */
  6221. rts_rspec = brcms_basic_rate(wlc, rspec);
  6222. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  6223. /* set rts txbw to correct side band */
  6224. rts_rspec &= ~RSPEC_BW_MASK;
  6225. /*
  6226. * if rspec/rspec_fallback is 40MHz, then send RTS on both
  6227. * 20MHz channel (DUP), otherwise send RTS on control channel
  6228. */
  6229. if (rspec_is40mhz(rspec) && !is_cck_rate(rts_rspec))
  6230. rts_rspec |= (PHY_TXC1_BW_40MHZ_DUP << RSPEC_BW_SHIFT);
  6231. else
  6232. rts_rspec |= (mimo_ctlchbw << RSPEC_BW_SHIFT);
  6233. /* pick siso/cdd as default for ofdm */
  6234. if (is_ofdm_rate(rts_rspec)) {
  6235. rts_rspec &= ~RSPEC_STF_MASK;
  6236. rts_rspec |= (wlc->stf->ss_opmode << RSPEC_STF_SHIFT);
  6237. }
  6238. }
  6239. return rts_rspec;
  6240. }
  6241. void
  6242. brcms_c_txfifo_complete(struct brcms_c_info *wlc, uint fifo, s8 txpktpend)
  6243. {
  6244. wlc->core->txpktpend[fifo] -= txpktpend;
  6245. BCMMSG(wlc->wiphy, "pktpend dec %d to %d\n", txpktpend,
  6246. wlc->core->txpktpend[fifo]);
  6247. /* There is more room; mark precedences related to this FIFO sendable */
  6248. wlc->tx_prec_map |= wlc->fifo2prec_map[fifo];
  6249. /* figure out which bsscfg is being worked on... */
  6250. }
  6251. /* Update beacon listen interval in shared memory */
  6252. static void brcms_c_bcn_li_upd(struct brcms_c_info *wlc)
  6253. {
  6254. /* wake up every DTIM is the default */
  6255. if (wlc->bcn_li_dtim == 1)
  6256. brcms_b_write_shm(wlc->hw, M_BCN_LI, 0);
  6257. else
  6258. brcms_b_write_shm(wlc->hw, M_BCN_LI,
  6259. (wlc->bcn_li_dtim << 8) | wlc->bcn_li_bcn);
  6260. }
  6261. static void
  6262. brcms_b_read_tsf(struct brcms_hardware *wlc_hw, u32 *tsf_l_ptr,
  6263. u32 *tsf_h_ptr)
  6264. {
  6265. struct bcma_device *core = wlc_hw->d11core;
  6266. /* read the tsf timer low, then high to get an atomic read */
  6267. *tsf_l_ptr = bcma_read32(core, D11REGOFFS(tsf_timerlow));
  6268. *tsf_h_ptr = bcma_read32(core, D11REGOFFS(tsf_timerhigh));
  6269. }
  6270. /*
  6271. * recover 64bit TSF value from the 16bit TSF value in the rx header
  6272. * given the assumption that the TSF passed in header is within 65ms
  6273. * of the current tsf.
  6274. *
  6275. * 6 5 4 4 3 2 1
  6276. * 3.......6.......8.......0.......2.......4.......6.......8......0
  6277. * |<---------- tsf_h ----------->||<--- tsf_l -->||<-RxTSFTime ->|
  6278. *
  6279. * The RxTSFTime are the lowest 16 bits and provided by the ucode. The
  6280. * tsf_l is filled in by brcms_b_recv, which is done earlier in the
  6281. * receive call sequence after rx interrupt. Only the higher 16 bits
  6282. * are used. Finally, the tsf_h is read from the tsf register.
  6283. */
  6284. static u64 brcms_c_recover_tsf64(struct brcms_c_info *wlc,
  6285. struct d11rxhdr *rxh)
  6286. {
  6287. u32 tsf_h, tsf_l;
  6288. u16 rx_tsf_0_15, rx_tsf_16_31;
  6289. brcms_b_read_tsf(wlc->hw, &tsf_l, &tsf_h);
  6290. rx_tsf_16_31 = (u16)(tsf_l >> 16);
  6291. rx_tsf_0_15 = rxh->RxTSFTime;
  6292. /*
  6293. * a greater tsf time indicates the low 16 bits of
  6294. * tsf_l wrapped, so decrement the high 16 bits.
  6295. */
  6296. if ((u16)tsf_l < rx_tsf_0_15) {
  6297. rx_tsf_16_31 -= 1;
  6298. if (rx_tsf_16_31 == 0xffff)
  6299. tsf_h -= 1;
  6300. }
  6301. return ((u64)tsf_h << 32) | (((u32)rx_tsf_16_31 << 16) + rx_tsf_0_15);
  6302. }
  6303. static void
  6304. prep_mac80211_status(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  6305. struct sk_buff *p,
  6306. struct ieee80211_rx_status *rx_status)
  6307. {
  6308. int preamble;
  6309. int channel;
  6310. u32 rspec;
  6311. unsigned char *plcp;
  6312. /* fill in TSF and flag its presence */
  6313. rx_status->mactime = brcms_c_recover_tsf64(wlc, rxh);
  6314. rx_status->flag |= RX_FLAG_MACTIME_MPDU;
  6315. channel = BRCMS_CHAN_CHANNEL(rxh->RxChan);
  6316. if (channel > 14) {
  6317. rx_status->band = IEEE80211_BAND_5GHZ;
  6318. rx_status->freq = ieee80211_ofdm_chan_to_freq(
  6319. WF_CHAN_FACTOR_5_G/2, channel);
  6320. } else {
  6321. rx_status->band = IEEE80211_BAND_2GHZ;
  6322. rx_status->freq = ieee80211_dsss_chan_to_freq(channel);
  6323. }
  6324. rx_status->signal = wlc_phy_rssi_compute(wlc->hw->band->pi, rxh);
  6325. /* noise */
  6326. /* qual */
  6327. rx_status->antenna =
  6328. (rxh->PhyRxStatus_0 & PRXS0_RXANT_UPSUBBAND) ? 1 : 0;
  6329. plcp = p->data;
  6330. rspec = brcms_c_compute_rspec(rxh, plcp);
  6331. if (is_mcs_rate(rspec)) {
  6332. rx_status->rate_idx = rspec & RSPEC_RATE_MASK;
  6333. rx_status->flag |= RX_FLAG_HT;
  6334. if (rspec_is40mhz(rspec))
  6335. rx_status->flag |= RX_FLAG_40MHZ;
  6336. } else {
  6337. switch (rspec2rate(rspec)) {
  6338. case BRCM_RATE_1M:
  6339. rx_status->rate_idx = 0;
  6340. break;
  6341. case BRCM_RATE_2M:
  6342. rx_status->rate_idx = 1;
  6343. break;
  6344. case BRCM_RATE_5M5:
  6345. rx_status->rate_idx = 2;
  6346. break;
  6347. case BRCM_RATE_11M:
  6348. rx_status->rate_idx = 3;
  6349. break;
  6350. case BRCM_RATE_6M:
  6351. rx_status->rate_idx = 4;
  6352. break;
  6353. case BRCM_RATE_9M:
  6354. rx_status->rate_idx = 5;
  6355. break;
  6356. case BRCM_RATE_12M:
  6357. rx_status->rate_idx = 6;
  6358. break;
  6359. case BRCM_RATE_18M:
  6360. rx_status->rate_idx = 7;
  6361. break;
  6362. case BRCM_RATE_24M:
  6363. rx_status->rate_idx = 8;
  6364. break;
  6365. case BRCM_RATE_36M:
  6366. rx_status->rate_idx = 9;
  6367. break;
  6368. case BRCM_RATE_48M:
  6369. rx_status->rate_idx = 10;
  6370. break;
  6371. case BRCM_RATE_54M:
  6372. rx_status->rate_idx = 11;
  6373. break;
  6374. default:
  6375. wiphy_err(wlc->wiphy, "%s: Unknown rate\n", __func__);
  6376. }
  6377. /*
  6378. * For 5GHz, we should decrease the index as it is
  6379. * a subset of the 2.4G rates. See bitrates field
  6380. * of brcms_band_5GHz_nphy (in mac80211_if.c).
  6381. */
  6382. if (rx_status->band == IEEE80211_BAND_5GHZ)
  6383. rx_status->rate_idx -= BRCMS_LEGACY_5G_RATE_OFFSET;
  6384. /* Determine short preamble and rate_idx */
  6385. preamble = 0;
  6386. if (is_cck_rate(rspec)) {
  6387. if (rxh->PhyRxStatus_0 & PRXS0_SHORTH)
  6388. rx_status->flag |= RX_FLAG_SHORTPRE;
  6389. } else if (is_ofdm_rate(rspec)) {
  6390. rx_status->flag |= RX_FLAG_SHORTPRE;
  6391. } else {
  6392. wiphy_err(wlc->wiphy, "%s: Unknown modulation\n",
  6393. __func__);
  6394. }
  6395. }
  6396. if (plcp3_issgi(plcp[3]))
  6397. rx_status->flag |= RX_FLAG_SHORT_GI;
  6398. if (rxh->RxStatus1 & RXS_DECERR) {
  6399. rx_status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  6400. wiphy_err(wlc->wiphy, "%s: RX_FLAG_FAILED_PLCP_CRC\n",
  6401. __func__);
  6402. }
  6403. if (rxh->RxStatus1 & RXS_FCSERR) {
  6404. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  6405. wiphy_err(wlc->wiphy, "%s: RX_FLAG_FAILED_FCS_CRC\n",
  6406. __func__);
  6407. }
  6408. }
  6409. static void
  6410. brcms_c_recvctl(struct brcms_c_info *wlc, struct d11rxhdr *rxh,
  6411. struct sk_buff *p)
  6412. {
  6413. int len_mpdu;
  6414. struct ieee80211_rx_status rx_status;
  6415. memset(&rx_status, 0, sizeof(rx_status));
  6416. prep_mac80211_status(wlc, rxh, p, &rx_status);
  6417. /* mac header+body length, exclude CRC and plcp header */
  6418. len_mpdu = p->len - D11_PHY_HDR_LEN - FCS_LEN;
  6419. skb_pull(p, D11_PHY_HDR_LEN);
  6420. __skb_trim(p, len_mpdu);
  6421. memcpy(IEEE80211_SKB_RXCB(p), &rx_status, sizeof(rx_status));
  6422. ieee80211_rx_irqsafe(wlc->pub->ieee_hw, p);
  6423. }
  6424. /* calculate frame duration for Mixed-mode L-SIG spoofing, return
  6425. * number of bytes goes in the length field
  6426. *
  6427. * Formula given by HT PHY Spec v 1.13
  6428. * len = 3(nsyms + nstream + 3) - 3
  6429. */
  6430. u16
  6431. brcms_c_calc_lsig_len(struct brcms_c_info *wlc, u32 ratespec,
  6432. uint mac_len)
  6433. {
  6434. uint nsyms, len = 0, kNdps;
  6435. BCMMSG(wlc->wiphy, "wl%d: rate %d, len%d\n",
  6436. wlc->pub->unit, rspec2rate(ratespec), mac_len);
  6437. if (is_mcs_rate(ratespec)) {
  6438. uint mcs = ratespec & RSPEC_RATE_MASK;
  6439. int tot_streams = (mcs_2_txstreams(mcs) + 1) +
  6440. rspec_stc(ratespec);
  6441. /*
  6442. * the payload duration calculation matches that
  6443. * of regular ofdm
  6444. */
  6445. /* 1000Ndbps = kbps * 4 */
  6446. kNdps = mcs_2_rate(mcs, rspec_is40mhz(ratespec),
  6447. rspec_issgi(ratespec)) * 4;
  6448. if (rspec_stc(ratespec) == 0)
  6449. nsyms =
  6450. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6451. APHY_TAIL_NBITS) * 1000, kNdps);
  6452. else
  6453. /* STBC needs to have even number of symbols */
  6454. nsyms =
  6455. 2 *
  6456. CEIL((APHY_SERVICE_NBITS + 8 * mac_len +
  6457. APHY_TAIL_NBITS) * 1000, 2 * kNdps);
  6458. /* (+3) account for HT-SIG(2) and HT-STF(1) */
  6459. nsyms += (tot_streams + 3);
  6460. /*
  6461. * 3 bytes/symbol @ legacy 6Mbps rate
  6462. * (-3) excluding service bits and tail bits
  6463. */
  6464. len = (3 * nsyms) - 3;
  6465. }
  6466. return (u16) len;
  6467. }
  6468. static void
  6469. brcms_c_mod_prb_rsp_rate_table(struct brcms_c_info *wlc, uint frame_len)
  6470. {
  6471. const struct brcms_c_rateset *rs_dflt;
  6472. struct brcms_c_rateset rs;
  6473. u8 rate;
  6474. u16 entry_ptr;
  6475. u8 plcp[D11_PHY_HDR_LEN];
  6476. u16 dur, sifs;
  6477. uint i;
  6478. sifs = get_sifs(wlc->band);
  6479. rs_dflt = brcms_c_rateset_get_hwrs(wlc);
  6480. brcms_c_rateset_copy(rs_dflt, &rs);
  6481. brcms_c_rateset_mcs_upd(&rs, wlc->stf->txstreams);
  6482. /*
  6483. * walk the phy rate table and update MAC core SHM
  6484. * basic rate table entries
  6485. */
  6486. for (i = 0; i < rs.count; i++) {
  6487. rate = rs.rates[i] & BRCMS_RATE_MASK;
  6488. entry_ptr = brcms_b_rate_shm_offset(wlc->hw, rate);
  6489. /* Calculate the Probe Response PLCP for the given rate */
  6490. brcms_c_compute_plcp(wlc, rate, frame_len, plcp);
  6491. /*
  6492. * Calculate the duration of the Probe Response
  6493. * frame plus SIFS for the MAC
  6494. */
  6495. dur = (u16) brcms_c_calc_frame_time(wlc, rate,
  6496. BRCMS_LONG_PREAMBLE, frame_len);
  6497. dur += sifs;
  6498. /* Update the SHM Rate Table entry Probe Response values */
  6499. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS,
  6500. (u16) (plcp[0] + (plcp[1] << 8)));
  6501. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_PLCP_POS + 2,
  6502. (u16) (plcp[2] + (plcp[3] << 8)));
  6503. brcms_b_write_shm(wlc->hw, entry_ptr + M_RT_PRS_DUR_POS, dur);
  6504. }
  6505. }
  6506. /* Max buffering needed for beacon template/prb resp template is 142 bytes.
  6507. *
  6508. * PLCP header is 6 bytes.
  6509. * 802.11 A3 header is 24 bytes.
  6510. * Max beacon frame body template length is 112 bytes.
  6511. * Max probe resp frame body template length is 110 bytes.
  6512. *
  6513. * *len on input contains the max length of the packet available.
  6514. *
  6515. * The *len value is set to the number of bytes in buf used, and starts
  6516. * with the PLCP and included up to, but not including, the 4 byte FCS.
  6517. */
  6518. static void
  6519. brcms_c_bcn_prb_template(struct brcms_c_info *wlc, u16 type,
  6520. u32 bcn_rspec,
  6521. struct brcms_bss_cfg *cfg, u16 *buf, int *len)
  6522. {
  6523. static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};
  6524. struct cck_phy_hdr *plcp;
  6525. struct ieee80211_mgmt *h;
  6526. int hdr_len, body_len;
  6527. hdr_len = D11_PHY_HDR_LEN + DOT11_MAC_HDR_LEN;
  6528. /* calc buffer size provided for frame body */
  6529. body_len = *len - hdr_len;
  6530. /* return actual size */
  6531. *len = hdr_len + body_len;
  6532. /* format PHY and MAC headers */
  6533. memset((char *)buf, 0, hdr_len);
  6534. plcp = (struct cck_phy_hdr *) buf;
  6535. /*
  6536. * PLCP for Probe Response frames are filled in from
  6537. * core's rate table
  6538. */
  6539. if (type == IEEE80211_STYPE_BEACON)
  6540. /* fill in PLCP */
  6541. brcms_c_compute_plcp(wlc, bcn_rspec,
  6542. (DOT11_MAC_HDR_LEN + body_len + FCS_LEN),
  6543. (u8 *) plcp);
  6544. /* "Regular" and 16 MBSS but not for 4 MBSS */
  6545. /* Update the phytxctl for the beacon based on the rspec */
  6546. brcms_c_beacon_phytxctl_txant_upd(wlc, bcn_rspec);
  6547. h = (struct ieee80211_mgmt *)&plcp[1];
  6548. /* fill in 802.11 header */
  6549. h->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | type);
  6550. /* DUR is 0 for multicast bcn, or filled in by MAC for prb resp */
  6551. /* A1 filled in by MAC for prb resp, broadcast for bcn */
  6552. if (type == IEEE80211_STYPE_BEACON)
  6553. memcpy(&h->da, &ether_bcast, ETH_ALEN);
  6554. memcpy(&h->sa, &cfg->cur_etheraddr, ETH_ALEN);
  6555. memcpy(&h->bssid, &cfg->BSSID, ETH_ALEN);
  6556. /* SEQ filled in by MAC */
  6557. }
  6558. int brcms_c_get_header_len(void)
  6559. {
  6560. return TXOFF;
  6561. }
  6562. /*
  6563. * Update all beacons for the system.
  6564. */
  6565. void brcms_c_update_beacon(struct brcms_c_info *wlc)
  6566. {
  6567. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6568. if (bsscfg->up && !bsscfg->BSS)
  6569. /* Clear the soft intmask */
  6570. wlc->defmacintmask &= ~MI_BCNTPL;
  6571. }
  6572. /* Write ssid into shared memory */
  6573. static void
  6574. brcms_c_shm_ssid_upd(struct brcms_c_info *wlc, struct brcms_bss_cfg *cfg)
  6575. {
  6576. u8 *ssidptr = cfg->SSID;
  6577. u16 base = M_SSID;
  6578. u8 ssidbuf[IEEE80211_MAX_SSID_LEN];
  6579. /* padding the ssid with zero and copy it into shm */
  6580. memset(ssidbuf, 0, IEEE80211_MAX_SSID_LEN);
  6581. memcpy(ssidbuf, ssidptr, cfg->SSID_len);
  6582. brcms_c_copyto_shm(wlc, base, ssidbuf, IEEE80211_MAX_SSID_LEN);
  6583. brcms_b_write_shm(wlc->hw, M_SSIDLEN, (u16) cfg->SSID_len);
  6584. }
  6585. static void
  6586. brcms_c_bss_update_probe_resp(struct brcms_c_info *wlc,
  6587. struct brcms_bss_cfg *cfg,
  6588. bool suspend)
  6589. {
  6590. u16 prb_resp[BCN_TMPL_LEN / 2];
  6591. int len = BCN_TMPL_LEN;
  6592. /*
  6593. * write the probe response to hardware, or save in
  6594. * the config structure
  6595. */
  6596. /* create the probe response template */
  6597. brcms_c_bcn_prb_template(wlc, IEEE80211_STYPE_PROBE_RESP, 0,
  6598. cfg, prb_resp, &len);
  6599. if (suspend)
  6600. brcms_c_suspend_mac_and_wait(wlc);
  6601. /* write the probe response into the template region */
  6602. brcms_b_write_template_ram(wlc->hw, T_PRS_TPL_BASE,
  6603. (len + 3) & ~3, prb_resp);
  6604. /* write the length of the probe response frame (+PLCP/-FCS) */
  6605. brcms_b_write_shm(wlc->hw, M_PRB_RESP_FRM_LEN, (u16) len);
  6606. /* write the SSID and SSID length */
  6607. brcms_c_shm_ssid_upd(wlc, cfg);
  6608. /*
  6609. * Write PLCP headers and durations for probe response frames
  6610. * at all rates. Use the actual frame length covered by the
  6611. * PLCP header for the call to brcms_c_mod_prb_rsp_rate_table()
  6612. * by subtracting the PLCP len and adding the FCS.
  6613. */
  6614. len += (-D11_PHY_HDR_LEN + FCS_LEN);
  6615. brcms_c_mod_prb_rsp_rate_table(wlc, (u16) len);
  6616. if (suspend)
  6617. brcms_c_enable_mac(wlc);
  6618. }
  6619. void brcms_c_update_probe_resp(struct brcms_c_info *wlc, bool suspend)
  6620. {
  6621. struct brcms_bss_cfg *bsscfg = wlc->bsscfg;
  6622. /* update AP or IBSS probe responses */
  6623. if (bsscfg->up && !bsscfg->BSS)
  6624. brcms_c_bss_update_probe_resp(wlc, bsscfg, suspend);
  6625. }
  6626. /* prepares pdu for transmission. returns BCM error codes */
  6627. int brcms_c_prep_pdu(struct brcms_c_info *wlc, struct sk_buff *pdu, uint *fifop)
  6628. {
  6629. uint fifo;
  6630. struct d11txh *txh;
  6631. struct ieee80211_hdr *h;
  6632. struct scb *scb;
  6633. txh = (struct d11txh *) (pdu->data);
  6634. h = (struct ieee80211_hdr *)((u8 *) (txh + 1) + D11_PHY_HDR_LEN);
  6635. /* get the pkt queue info. This was put at brcms_c_sendctl or
  6636. * brcms_c_send for PDU */
  6637. fifo = le16_to_cpu(txh->TxFrameID) & TXFID_QUEUE_MASK;
  6638. scb = NULL;
  6639. *fifop = fifo;
  6640. /* return if insufficient dma resources */
  6641. if (*wlc->core->txavail[fifo] < MAX_DMA_SEGS) {
  6642. /* Mark precedences related to this FIFO, unsendable */
  6643. /* A fifo is full. Clear precedences related to that FIFO */
  6644. wlc->tx_prec_map &= ~(wlc->fifo2prec_map[fifo]);
  6645. return -EBUSY;
  6646. }
  6647. return 0;
  6648. }
  6649. int brcms_b_xmtfifo_sz_get(struct brcms_hardware *wlc_hw, uint fifo,
  6650. uint *blocks)
  6651. {
  6652. if (fifo >= NFIFO)
  6653. return -EINVAL;
  6654. *blocks = wlc_hw->xmtfifo_sz[fifo];
  6655. return 0;
  6656. }
  6657. void
  6658. brcms_c_set_addrmatch(struct brcms_c_info *wlc, int match_reg_offset,
  6659. const u8 *addr)
  6660. {
  6661. brcms_b_set_addrmatch(wlc->hw, match_reg_offset, addr);
  6662. if (match_reg_offset == RCM_BSSID_OFFSET)
  6663. memcpy(wlc->bsscfg->BSSID, addr, ETH_ALEN);
  6664. }
  6665. /*
  6666. * Flag 'scan in progress' to withhold dynamic phy calibration
  6667. */
  6668. void brcms_c_scan_start(struct brcms_c_info *wlc)
  6669. {
  6670. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, true);
  6671. }
  6672. void brcms_c_scan_stop(struct brcms_c_info *wlc)
  6673. {
  6674. wlc_phy_hold_upd(wlc->band->pi, PHY_HOLD_FOR_SCAN, false);
  6675. }
  6676. void brcms_c_associate_upd(struct brcms_c_info *wlc, bool state)
  6677. {
  6678. wlc->pub->associated = state;
  6679. wlc->bsscfg->associated = state;
  6680. }
  6681. /*
  6682. * When a remote STA/AP is removed by Mac80211, or when it can no longer accept
  6683. * AMPDU traffic, packets pending in hardware have to be invalidated so that
  6684. * when later on hardware releases them, they can be handled appropriately.
  6685. */
  6686. void brcms_c_inval_dma_pkts(struct brcms_hardware *hw,
  6687. struct ieee80211_sta *sta,
  6688. void (*dma_callback_fn))
  6689. {
  6690. struct dma_pub *dmah;
  6691. int i;
  6692. for (i = 0; i < NFIFO; i++) {
  6693. dmah = hw->di[i];
  6694. if (dmah != NULL)
  6695. dma_walk_packets(dmah, dma_callback_fn, sta);
  6696. }
  6697. }
  6698. int brcms_c_get_curband(struct brcms_c_info *wlc)
  6699. {
  6700. return wlc->band->bandunit;
  6701. }
  6702. void brcms_c_wait_for_tx_completion(struct brcms_c_info *wlc, bool drop)
  6703. {
  6704. int timeout = 20;
  6705. /* flush packet queue when requested */
  6706. if (drop)
  6707. brcmu_pktq_flush(&wlc->pkt_queue->q, false, NULL, NULL);
  6708. /* wait for queue and DMA fifos to run dry */
  6709. while (!pktq_empty(&wlc->pkt_queue->q) || brcms_txpktpendtot(wlc) > 0) {
  6710. brcms_msleep(wlc->wl, 1);
  6711. if (--timeout == 0)
  6712. break;
  6713. }
  6714. WARN_ON_ONCE(timeout == 0);
  6715. }
  6716. void brcms_c_set_beacon_listen_interval(struct brcms_c_info *wlc, u8 interval)
  6717. {
  6718. wlc->bcn_li_bcn = interval;
  6719. if (wlc->pub->up)
  6720. brcms_c_bcn_li_upd(wlc);
  6721. }
  6722. int brcms_c_set_tx_power(struct brcms_c_info *wlc, int txpwr)
  6723. {
  6724. uint qdbm;
  6725. /* Remove override bit and clip to max qdbm value */
  6726. qdbm = min_t(uint, txpwr * BRCMS_TXPWR_DB_FACTOR, 0xff);
  6727. return wlc_phy_txpower_set(wlc->band->pi, qdbm, false);
  6728. }
  6729. int brcms_c_get_tx_power(struct brcms_c_info *wlc)
  6730. {
  6731. uint qdbm;
  6732. bool override;
  6733. wlc_phy_txpower_get(wlc->band->pi, &qdbm, &override);
  6734. /* Return qdbm units */
  6735. return (int)(qdbm / BRCMS_TXPWR_DB_FACTOR);
  6736. }
  6737. /* Process received frames */
  6738. /*
  6739. * Return true if more frames need to be processed. false otherwise.
  6740. * Param 'bound' indicates max. # frames to process before break out.
  6741. */
  6742. static void brcms_c_recv(struct brcms_c_info *wlc, struct sk_buff *p)
  6743. {
  6744. struct d11rxhdr *rxh;
  6745. struct ieee80211_hdr *h;
  6746. uint len;
  6747. bool is_amsdu;
  6748. BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
  6749. /* frame starts with rxhdr */
  6750. rxh = (struct d11rxhdr *) (p->data);
  6751. /* strip off rxhdr */
  6752. skb_pull(p, BRCMS_HWRXOFF);
  6753. /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU subframes */
  6754. if (rxh->RxStatus1 & RXS_PBPRES) {
  6755. if (p->len < 2) {
  6756. wiphy_err(wlc->wiphy, "wl%d: recv: rcvd runt of "
  6757. "len %d\n", wlc->pub->unit, p->len);
  6758. goto toss;
  6759. }
  6760. skb_pull(p, 2);
  6761. }
  6762. h = (struct ieee80211_hdr *)(p->data + D11_PHY_HDR_LEN);
  6763. len = p->len;
  6764. if (rxh->RxStatus1 & RXS_FCSERR) {
  6765. if (!(wlc->filter_flags & FIF_FCSFAIL))
  6766. goto toss;
  6767. }
  6768. /* check received pkt has at least frame control field */
  6769. if (len < D11_PHY_HDR_LEN + sizeof(h->frame_control))
  6770. goto toss;
  6771. /* not supporting A-MSDU */
  6772. is_amsdu = rxh->RxStatus2 & RXS_AMSDU_MASK;
  6773. if (is_amsdu)
  6774. goto toss;
  6775. brcms_c_recvctl(wlc, rxh, p);
  6776. return;
  6777. toss:
  6778. brcmu_pkt_buf_free_skb(p);
  6779. }
  6780. /* Process received frames */
  6781. /*
  6782. * Return true if more frames need to be processed. false otherwise.
  6783. * Param 'bound' indicates max. # frames to process before break out.
  6784. */
  6785. static bool
  6786. brcms_b_recv(struct brcms_hardware *wlc_hw, uint fifo, bool bound)
  6787. {
  6788. struct sk_buff *p;
  6789. struct sk_buff *next = NULL;
  6790. struct sk_buff_head recv_frames;
  6791. uint n = 0;
  6792. uint bound_limit = bound ? RXBND : -1;
  6793. BCMMSG(wlc_hw->wlc->wiphy, "wl%d\n", wlc_hw->unit);
  6794. skb_queue_head_init(&recv_frames);
  6795. /* gather received frames */
  6796. while (dma_rx(wlc_hw->di[fifo], &recv_frames)) {
  6797. /* !give others some time to run! */
  6798. if (++n >= bound_limit)
  6799. break;
  6800. }
  6801. /* post more rbufs */
  6802. dma_rxfill(wlc_hw->di[fifo]);
  6803. /* process each frame */
  6804. skb_queue_walk_safe(&recv_frames, p, next) {
  6805. struct d11rxhdr_le *rxh_le;
  6806. struct d11rxhdr *rxh;
  6807. skb_unlink(p, &recv_frames);
  6808. rxh_le = (struct d11rxhdr_le *)p->data;
  6809. rxh = (struct d11rxhdr *)p->data;
  6810. /* fixup rx header endianness */
  6811. rxh->RxFrameSize = le16_to_cpu(rxh_le->RxFrameSize);
  6812. rxh->PhyRxStatus_0 = le16_to_cpu(rxh_le->PhyRxStatus_0);
  6813. rxh->PhyRxStatus_1 = le16_to_cpu(rxh_le->PhyRxStatus_1);
  6814. rxh->PhyRxStatus_2 = le16_to_cpu(rxh_le->PhyRxStatus_2);
  6815. rxh->PhyRxStatus_3 = le16_to_cpu(rxh_le->PhyRxStatus_3);
  6816. rxh->PhyRxStatus_4 = le16_to_cpu(rxh_le->PhyRxStatus_4);
  6817. rxh->PhyRxStatus_5 = le16_to_cpu(rxh_le->PhyRxStatus_5);
  6818. rxh->RxStatus1 = le16_to_cpu(rxh_le->RxStatus1);
  6819. rxh->RxStatus2 = le16_to_cpu(rxh_le->RxStatus2);
  6820. rxh->RxTSFTime = le16_to_cpu(rxh_le->RxTSFTime);
  6821. rxh->RxChan = le16_to_cpu(rxh_le->RxChan);
  6822. brcms_c_recv(wlc_hw->wlc, p);
  6823. }
  6824. return n >= bound_limit;
  6825. }
  6826. /* second-level interrupt processing
  6827. * Return true if another dpc needs to be re-scheduled. false otherwise.
  6828. * Param 'bounded' indicates if applicable loops should be bounded.
  6829. */
  6830. bool brcms_c_dpc(struct brcms_c_info *wlc, bool bounded)
  6831. {
  6832. u32 macintstatus;
  6833. struct brcms_hardware *wlc_hw = wlc->hw;
  6834. struct bcma_device *core = wlc_hw->d11core;
  6835. struct wiphy *wiphy = wlc->wiphy;
  6836. if (brcms_deviceremoved(wlc)) {
  6837. wiphy_err(wiphy, "wl%d: %s: dead chip\n", wlc_hw->unit,
  6838. __func__);
  6839. brcms_down(wlc->wl);
  6840. return false;
  6841. }
  6842. /* grab and clear the saved software intstatus bits */
  6843. macintstatus = wlc->macintstatus;
  6844. wlc->macintstatus = 0;
  6845. BCMMSG(wlc->wiphy, "wl%d: macintstatus 0x%x\n",
  6846. wlc_hw->unit, macintstatus);
  6847. WARN_ON(macintstatus & MI_PRQ); /* PRQ Interrupt in non-MBSS */
  6848. /* tx status */
  6849. if (macintstatus & MI_TFS) {
  6850. bool fatal;
  6851. if (brcms_b_txstatus(wlc->hw, bounded, &fatal))
  6852. wlc->macintstatus |= MI_TFS;
  6853. if (fatal) {
  6854. wiphy_err(wiphy, "MI_TFS: fatal\n");
  6855. goto fatal;
  6856. }
  6857. }
  6858. if (macintstatus & (MI_TBTT | MI_DTIM_TBTT))
  6859. brcms_c_tbtt(wlc);
  6860. /* ATIM window end */
  6861. if (macintstatus & MI_ATIMWINEND) {
  6862. BCMMSG(wlc->wiphy, "end of ATIM window\n");
  6863. bcma_set32(core, D11REGOFFS(maccommand), wlc->qvalid);
  6864. wlc->qvalid = 0;
  6865. }
  6866. /*
  6867. * received data or control frame, MI_DMAINT is
  6868. * indication of RX_FIFO interrupt
  6869. */
  6870. if (macintstatus & MI_DMAINT)
  6871. if (brcms_b_recv(wlc_hw, RX_FIFO, bounded))
  6872. wlc->macintstatus |= MI_DMAINT;
  6873. /* noise sample collected */
  6874. if (macintstatus & MI_BG_NOISE)
  6875. wlc_phy_noise_sample_intr(wlc_hw->band->pi);
  6876. if (macintstatus & MI_GP0) {
  6877. wiphy_err(wiphy, "wl%d: PSM microcode watchdog fired at %d "
  6878. "(seconds). Resetting.\n", wlc_hw->unit, wlc_hw->now);
  6879. printk_once("%s : PSM Watchdog, chipid 0x%x, chiprev 0x%x\n",
  6880. __func__, ai_get_chip_id(wlc_hw->sih),
  6881. ai_get_chiprev(wlc_hw->sih));
  6882. brcms_fatal_error(wlc_hw->wlc->wl);
  6883. }
  6884. /* gptimer timeout */
  6885. if (macintstatus & MI_TO)
  6886. bcma_write32(core, D11REGOFFS(gptimer), 0);
  6887. if (macintstatus & MI_RFDISABLE) {
  6888. BCMMSG(wlc->wiphy, "wl%d: BMAC Detected a change on the"
  6889. " RF Disable Input\n", wlc_hw->unit);
  6890. brcms_rfkill_set_hw_state(wlc->wl);
  6891. }
  6892. /* send any enq'd tx packets. Just makes sure to jump start tx */
  6893. if (!pktq_empty(&wlc->pkt_queue->q))
  6894. brcms_c_send_q(wlc);
  6895. /* it isn't done and needs to be resched if macintstatus is non-zero */
  6896. return wlc->macintstatus != 0;
  6897. fatal:
  6898. brcms_fatal_error(wlc_hw->wlc->wl);
  6899. return wlc->macintstatus != 0;
  6900. }
  6901. void brcms_c_init(struct brcms_c_info *wlc, bool mute_tx)
  6902. {
  6903. struct bcma_device *core = wlc->hw->d11core;
  6904. u16 chanspec;
  6905. BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
  6906. /*
  6907. * This will happen if a big-hammer was executed. In
  6908. * that case, we want to go back to the channel that
  6909. * we were on and not new channel
  6910. */
  6911. if (wlc->pub->associated)
  6912. chanspec = wlc->home_chanspec;
  6913. else
  6914. chanspec = brcms_c_init_chanspec(wlc);
  6915. brcms_b_init(wlc->hw, chanspec);
  6916. /* update beacon listen interval */
  6917. brcms_c_bcn_li_upd(wlc);
  6918. /* write ethernet address to core */
  6919. brcms_c_set_mac(wlc->bsscfg);
  6920. brcms_c_set_bssid(wlc->bsscfg);
  6921. /* Update tsf_cfprep if associated and up */
  6922. if (wlc->pub->associated && wlc->bsscfg->up) {
  6923. u32 bi;
  6924. /* get beacon period and convert to uS */
  6925. bi = wlc->bsscfg->current_bss->beacon_period << 10;
  6926. /*
  6927. * update since init path would reset
  6928. * to default value
  6929. */
  6930. bcma_write32(core, D11REGOFFS(tsf_cfprep),
  6931. bi << CFPREP_CBI_SHIFT);
  6932. /* Update maccontrol PM related bits */
  6933. brcms_c_set_ps_ctrl(wlc);
  6934. }
  6935. brcms_c_bandinit_ordered(wlc, chanspec);
  6936. /* init probe response timeout */
  6937. brcms_b_write_shm(wlc->hw, M_PRS_MAXTIME, wlc->prb_resp_timeout);
  6938. /* init max burst txop (framebursting) */
  6939. brcms_b_write_shm(wlc->hw, M_MBURST_TXOP,
  6940. (wlc->
  6941. _rifs ? (EDCF_AC_VO_TXOP_AP << 5) : MAXFRAMEBURST_TXOP));
  6942. /* initialize maximum allowed duty cycle */
  6943. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_ofdm, true, true);
  6944. brcms_c_duty_cycle_set(wlc, wlc->tx_duty_cycle_cck, false, true);
  6945. /*
  6946. * Update some shared memory locations related to
  6947. * max AMPDU size allowed to received
  6948. */
  6949. brcms_c_ampdu_shm_upd(wlc->ampdu);
  6950. /* band-specific inits */
  6951. brcms_c_bsinit(wlc);
  6952. /* Enable EDCF mode (while the MAC is suspended) */
  6953. bcma_set16(core, D11REGOFFS(ifs_ctl), IFS_USEEDCF);
  6954. brcms_c_edcf_setparams(wlc, false);
  6955. /* Init precedence maps for empty FIFOs */
  6956. brcms_c_tx_prec_map_init(wlc);
  6957. /* read the ucode version if we have not yet done so */
  6958. if (wlc->ucode_rev == 0) {
  6959. wlc->ucode_rev =
  6960. brcms_b_read_shm(wlc->hw, M_BOM_REV_MAJOR) << NBITS(u16);
  6961. wlc->ucode_rev |= brcms_b_read_shm(wlc->hw, M_BOM_REV_MINOR);
  6962. }
  6963. /* ..now really unleash hell (allow the MAC out of suspend) */
  6964. brcms_c_enable_mac(wlc);
  6965. /* suspend the tx fifos and mute the phy for preism cac time */
  6966. if (mute_tx)
  6967. brcms_b_mute(wlc->hw, true);
  6968. /* clear tx flow control */
  6969. brcms_c_txflowcontrol_reset(wlc);
  6970. /* enable the RF Disable Delay timer */
  6971. bcma_write32(core, D11REGOFFS(rfdisabledly), RFDISABLE_DEFAULT);
  6972. /*
  6973. * Initialize WME parameters; if they haven't been set by some other
  6974. * mechanism (IOVar, etc) then read them from the hardware.
  6975. */
  6976. if (GFIELD(wlc->wme_retries[0], EDCF_SHORT) == 0) {
  6977. /* Uninitialized; read from HW */
  6978. int ac;
  6979. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  6980. wlc->wme_retries[ac] =
  6981. brcms_b_read_shm(wlc->hw, M_AC_TXLMT_ADDR(ac));
  6982. }
  6983. }
  6984. /*
  6985. * The common driver entry routine. Error codes should be unique
  6986. */
  6987. struct brcms_c_info *
  6988. brcms_c_attach(struct brcms_info *wl, struct bcma_device *core, uint unit,
  6989. bool piomode, uint *perr)
  6990. {
  6991. struct brcms_c_info *wlc;
  6992. uint err = 0;
  6993. uint i, j;
  6994. struct brcms_pub *pub;
  6995. /* allocate struct brcms_c_info state and its substructures */
  6996. wlc = (struct brcms_c_info *) brcms_c_attach_malloc(unit, &err, 0);
  6997. if (wlc == NULL)
  6998. goto fail;
  6999. wlc->wiphy = wl->wiphy;
  7000. pub = wlc->pub;
  7001. #if defined(BCMDBG)
  7002. wlc_info_dbg = wlc;
  7003. #endif
  7004. wlc->band = wlc->bandstate[0];
  7005. wlc->core = wlc->corestate;
  7006. wlc->wl = wl;
  7007. pub->unit = unit;
  7008. pub->_piomode = piomode;
  7009. wlc->bandinit_pending = false;
  7010. /* populate struct brcms_c_info with default values */
  7011. brcms_c_info_init(wlc, unit);
  7012. /* update sta/ap related parameters */
  7013. brcms_c_ap_upd(wlc);
  7014. /*
  7015. * low level attach steps(all hw accesses go
  7016. * inside, no more in rest of the attach)
  7017. */
  7018. err = brcms_b_attach(wlc, core, unit, piomode);
  7019. if (err)
  7020. goto fail;
  7021. brcms_c_protection_upd(wlc, BRCMS_PROT_N_PAM_OVR, OFF);
  7022. pub->phy_11ncapable = BRCMS_PHY_11N_CAP(wlc->band);
  7023. /* disable allowed duty cycle */
  7024. wlc->tx_duty_cycle_ofdm = 0;
  7025. wlc->tx_duty_cycle_cck = 0;
  7026. brcms_c_stf_phy_chain_calc(wlc);
  7027. /* txchain 1: txant 0, txchain 2: txant 1 */
  7028. if (BRCMS_ISNPHY(wlc->band) && (wlc->stf->txstreams == 1))
  7029. wlc->stf->txant = wlc->stf->hw_txchain - 1;
  7030. /* push to BMAC driver */
  7031. wlc_phy_stf_chain_init(wlc->band->pi, wlc->stf->hw_txchain,
  7032. wlc->stf->hw_rxchain);
  7033. /* pull up some info resulting from the low attach */
  7034. for (i = 0; i < NFIFO; i++)
  7035. wlc->core->txavail[i] = wlc->hw->txavail[i];
  7036. memcpy(&wlc->perm_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  7037. memcpy(&pub->cur_etheraddr, &wlc->hw->etheraddr, ETH_ALEN);
  7038. for (j = 0; j < wlc->pub->_nbands; j++) {
  7039. wlc->band = wlc->bandstate[j];
  7040. if (!brcms_c_attach_stf_ant_init(wlc)) {
  7041. err = 24;
  7042. goto fail;
  7043. }
  7044. /* default contention windows size limits */
  7045. wlc->band->CWmin = APHY_CWMIN;
  7046. wlc->band->CWmax = PHY_CWMAX;
  7047. /* init gmode value */
  7048. if (wlc->band->bandtype == BRCM_BAND_2G) {
  7049. wlc->band->gmode = GMODE_AUTO;
  7050. brcms_c_protection_upd(wlc, BRCMS_PROT_G_USER,
  7051. wlc->band->gmode);
  7052. }
  7053. /* init _n_enab supported mode */
  7054. if (BRCMS_PHY_11N_CAP(wlc->band)) {
  7055. pub->_n_enab = SUPPORT_11N;
  7056. brcms_c_protection_upd(wlc, BRCMS_PROT_N_USER,
  7057. ((pub->_n_enab ==
  7058. SUPPORT_11N) ? WL_11N_2x2 :
  7059. WL_11N_3x3));
  7060. }
  7061. /* init per-band default rateset, depend on band->gmode */
  7062. brcms_default_rateset(wlc, &wlc->band->defrateset);
  7063. /* fill in hw_rateset */
  7064. brcms_c_rateset_filter(&wlc->band->defrateset,
  7065. &wlc->band->hw_rateset, false,
  7066. BRCMS_RATES_CCK_OFDM, BRCMS_RATE_MASK,
  7067. (bool) (wlc->pub->_n_enab & SUPPORT_11N));
  7068. }
  7069. /*
  7070. * update antenna config due to
  7071. * wlc->stf->txant/txchain/ant_rx_ovr change
  7072. */
  7073. brcms_c_stf_phy_txant_upd(wlc);
  7074. /* attach each modules */
  7075. err = brcms_c_attach_module(wlc);
  7076. if (err != 0)
  7077. goto fail;
  7078. if (!brcms_c_timers_init(wlc, unit)) {
  7079. wiphy_err(wl->wiphy, "wl%d: %s: init_timer failed\n", unit,
  7080. __func__);
  7081. err = 32;
  7082. goto fail;
  7083. }
  7084. /* depend on rateset, gmode */
  7085. wlc->cmi = brcms_c_channel_mgr_attach(wlc);
  7086. if (!wlc->cmi) {
  7087. wiphy_err(wl->wiphy, "wl%d: %s: channel_mgr_attach failed"
  7088. "\n", unit, __func__);
  7089. err = 33;
  7090. goto fail;
  7091. }
  7092. /* init default when all parameters are ready, i.e. ->rateset */
  7093. brcms_c_bss_default_init(wlc);
  7094. /*
  7095. * Complete the wlc default state initializations..
  7096. */
  7097. /* allocate our initial queue */
  7098. wlc->pkt_queue = brcms_c_txq_alloc(wlc);
  7099. if (wlc->pkt_queue == NULL) {
  7100. wiphy_err(wl->wiphy, "wl%d: %s: failed to malloc tx queue\n",
  7101. unit, __func__);
  7102. err = 100;
  7103. goto fail;
  7104. }
  7105. wlc->bsscfg->wlc = wlc;
  7106. wlc->mimoft = FT_HT;
  7107. wlc->mimo_40txbw = AUTO;
  7108. wlc->ofdm_40txbw = AUTO;
  7109. wlc->cck_40txbw = AUTO;
  7110. brcms_c_update_mimo_band_bwcap(wlc, BRCMS_N_BW_20IN2G_40IN5G);
  7111. /* Set default values of SGI */
  7112. if (BRCMS_SGI_CAP_PHY(wlc)) {
  7113. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  7114. BRCMS_N_SGI_40));
  7115. } else if (BRCMS_ISSSLPNPHY(wlc->band)) {
  7116. brcms_c_ht_update_sgi_rx(wlc, (BRCMS_N_SGI_20 |
  7117. BRCMS_N_SGI_40));
  7118. } else {
  7119. brcms_c_ht_update_sgi_rx(wlc, 0);
  7120. }
  7121. brcms_b_antsel_set(wlc->hw, wlc->asi->antsel_avail);
  7122. if (perr)
  7123. *perr = 0;
  7124. return wlc;
  7125. fail:
  7126. wiphy_err(wl->wiphy, "wl%d: %s: failed with err %d\n",
  7127. unit, __func__, err);
  7128. if (wlc)
  7129. brcms_c_detach(wlc);
  7130. if (perr)
  7131. *perr = err;
  7132. return NULL;
  7133. }