main.c 226 KB

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