main.c 230 KB

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