main.c 224 KB

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