mac.c 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "mac.h"
  18. #include <net/mac80211.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/acpi.h>
  21. #include "hif.h"
  22. #include "core.h"
  23. #include "debug.h"
  24. #include "wmi.h"
  25. #include "htt.h"
  26. #include "txrx.h"
  27. #include "testmode.h"
  28. #include "wmi.h"
  29. #include "wmi-tlv.h"
  30. #include "wmi-ops.h"
  31. #include "wow.h"
  32. /*********/
  33. /* Rates */
  34. /*********/
  35. static struct ieee80211_rate ath10k_rates[] = {
  36. { .bitrate = 10,
  37. .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
  38. { .bitrate = 20,
  39. .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
  40. .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
  41. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  42. { .bitrate = 55,
  43. .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
  44. .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
  45. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  46. { .bitrate = 110,
  47. .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
  48. .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
  49. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  50. { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
  51. { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
  52. { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
  53. { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
  54. { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
  55. { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
  56. { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
  57. { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
  58. };
  59. static struct ieee80211_rate ath10k_rates_rev2[] = {
  60. { .bitrate = 10,
  61. .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
  62. { .bitrate = 20,
  63. .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
  64. .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
  65. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  66. { .bitrate = 55,
  67. .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
  68. .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
  69. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  70. { .bitrate = 110,
  71. .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
  72. .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
  73. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  74. { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
  75. { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
  76. { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
  77. { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
  78. { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
  79. { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
  80. { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
  81. { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
  82. };
  83. #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
  84. #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
  85. #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
  86. ATH10K_MAC_FIRST_OFDM_RATE_IDX)
  87. #define ath10k_g_rates (ath10k_rates + 0)
  88. #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
  89. #define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
  90. #define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
  91. static bool ath10k_mac_bitrate_is_cck(int bitrate)
  92. {
  93. switch (bitrate) {
  94. case 10:
  95. case 20:
  96. case 55:
  97. case 110:
  98. return true;
  99. }
  100. return false;
  101. }
  102. static u8 ath10k_mac_bitrate_to_rate(int bitrate)
  103. {
  104. return DIV_ROUND_UP(bitrate, 5) |
  105. (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
  106. }
  107. u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
  108. u8 hw_rate, bool cck)
  109. {
  110. const struct ieee80211_rate *rate;
  111. int i;
  112. for (i = 0; i < sband->n_bitrates; i++) {
  113. rate = &sband->bitrates[i];
  114. if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
  115. continue;
  116. if (rate->hw_value == hw_rate)
  117. return i;
  118. else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
  119. rate->hw_value_short == hw_rate)
  120. return i;
  121. }
  122. return 0;
  123. }
  124. u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
  125. u32 bitrate)
  126. {
  127. int i;
  128. for (i = 0; i < sband->n_bitrates; i++)
  129. if (sband->bitrates[i].bitrate == bitrate)
  130. return i;
  131. return 0;
  132. }
  133. static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
  134. {
  135. switch ((mcs_map >> (2 * nss)) & 0x3) {
  136. case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
  137. case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
  138. case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
  139. }
  140. return 0;
  141. }
  142. static u32
  143. ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  144. {
  145. int nss;
  146. for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
  147. if (ht_mcs_mask[nss])
  148. return nss + 1;
  149. return 1;
  150. }
  151. static u32
  152. ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
  153. {
  154. int nss;
  155. for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
  156. if (vht_mcs_mask[nss])
  157. return nss + 1;
  158. return 1;
  159. }
  160. int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
  161. {
  162. enum wmi_host_platform_type platform_type;
  163. int ret;
  164. if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
  165. platform_type = WMI_HOST_PLATFORM_LOW_PERF;
  166. else
  167. platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
  168. ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
  169. if (ret && ret != -EOPNOTSUPP) {
  170. ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
  171. return ret;
  172. }
  173. return 0;
  174. }
  175. /**********/
  176. /* Crypto */
  177. /**********/
  178. static int ath10k_send_key(struct ath10k_vif *arvif,
  179. struct ieee80211_key_conf *key,
  180. enum set_key_cmd cmd,
  181. const u8 *macaddr, u32 flags)
  182. {
  183. struct ath10k *ar = arvif->ar;
  184. struct wmi_vdev_install_key_arg arg = {
  185. .vdev_id = arvif->vdev_id,
  186. .key_idx = key->keyidx,
  187. .key_len = key->keylen,
  188. .key_data = key->key,
  189. .key_flags = flags,
  190. .macaddr = macaddr,
  191. };
  192. lockdep_assert_held(&arvif->ar->conf_mutex);
  193. switch (key->cipher) {
  194. case WLAN_CIPHER_SUITE_CCMP:
  195. arg.key_cipher = WMI_CIPHER_AES_CCM;
  196. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
  197. break;
  198. case WLAN_CIPHER_SUITE_TKIP:
  199. arg.key_cipher = WMI_CIPHER_TKIP;
  200. arg.key_txmic_len = 8;
  201. arg.key_rxmic_len = 8;
  202. break;
  203. case WLAN_CIPHER_SUITE_WEP40:
  204. case WLAN_CIPHER_SUITE_WEP104:
  205. arg.key_cipher = WMI_CIPHER_WEP;
  206. break;
  207. case WLAN_CIPHER_SUITE_AES_CMAC:
  208. WARN_ON(1);
  209. return -EINVAL;
  210. default:
  211. ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
  212. return -EOPNOTSUPP;
  213. }
  214. if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  215. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  216. if (cmd == DISABLE_KEY) {
  217. arg.key_cipher = WMI_CIPHER_NONE;
  218. arg.key_data = NULL;
  219. }
  220. return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
  221. }
  222. static int ath10k_install_key(struct ath10k_vif *arvif,
  223. struct ieee80211_key_conf *key,
  224. enum set_key_cmd cmd,
  225. const u8 *macaddr, u32 flags)
  226. {
  227. struct ath10k *ar = arvif->ar;
  228. int ret;
  229. unsigned long time_left;
  230. lockdep_assert_held(&ar->conf_mutex);
  231. reinit_completion(&ar->install_key_done);
  232. if (arvif->nohwcrypt)
  233. return 1;
  234. ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
  235. if (ret)
  236. return ret;
  237. time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
  238. if (time_left == 0)
  239. return -ETIMEDOUT;
  240. return 0;
  241. }
  242. static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
  243. const u8 *addr)
  244. {
  245. struct ath10k *ar = arvif->ar;
  246. struct ath10k_peer *peer;
  247. int ret;
  248. int i;
  249. u32 flags;
  250. lockdep_assert_held(&ar->conf_mutex);
  251. if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
  252. arvif->vif->type != NL80211_IFTYPE_ADHOC &&
  253. arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
  254. return -EINVAL;
  255. spin_lock_bh(&ar->data_lock);
  256. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  257. spin_unlock_bh(&ar->data_lock);
  258. if (!peer)
  259. return -ENOENT;
  260. for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
  261. if (arvif->wep_keys[i] == NULL)
  262. continue;
  263. switch (arvif->vif->type) {
  264. case NL80211_IFTYPE_AP:
  265. flags = WMI_KEY_PAIRWISE;
  266. if (arvif->def_wep_key_idx == i)
  267. flags |= WMI_KEY_TX_USAGE;
  268. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  269. SET_KEY, addr, flags);
  270. if (ret < 0)
  271. return ret;
  272. break;
  273. case NL80211_IFTYPE_ADHOC:
  274. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  275. SET_KEY, addr,
  276. WMI_KEY_PAIRWISE);
  277. if (ret < 0)
  278. return ret;
  279. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  280. SET_KEY, addr, WMI_KEY_GROUP);
  281. if (ret < 0)
  282. return ret;
  283. break;
  284. default:
  285. WARN_ON(1);
  286. return -EINVAL;
  287. }
  288. spin_lock_bh(&ar->data_lock);
  289. peer->keys[i] = arvif->wep_keys[i];
  290. spin_unlock_bh(&ar->data_lock);
  291. }
  292. /* In some cases (notably with static WEP IBSS with multiple keys)
  293. * multicast Tx becomes broken. Both pairwise and groupwise keys are
  294. * installed already. Using WMI_KEY_TX_USAGE in different combinations
  295. * didn't seem help. Using def_keyid vdev parameter seems to be
  296. * effective so use that.
  297. *
  298. * FIXME: Revisit. Perhaps this can be done in a less hacky way.
  299. */
  300. if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
  301. return 0;
  302. if (arvif->def_wep_key_idx == -1)
  303. return 0;
  304. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  305. arvif->vdev_id,
  306. arvif->ar->wmi.vdev_param->def_keyid,
  307. arvif->def_wep_key_idx);
  308. if (ret) {
  309. ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
  310. arvif->vdev_id, ret);
  311. return ret;
  312. }
  313. return 0;
  314. }
  315. static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
  316. const u8 *addr)
  317. {
  318. struct ath10k *ar = arvif->ar;
  319. struct ath10k_peer *peer;
  320. int first_errno = 0;
  321. int ret;
  322. int i;
  323. u32 flags = 0;
  324. lockdep_assert_held(&ar->conf_mutex);
  325. spin_lock_bh(&ar->data_lock);
  326. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  327. spin_unlock_bh(&ar->data_lock);
  328. if (!peer)
  329. return -ENOENT;
  330. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  331. if (peer->keys[i] == NULL)
  332. continue;
  333. /* key flags are not required to delete the key */
  334. ret = ath10k_install_key(arvif, peer->keys[i],
  335. DISABLE_KEY, addr, flags);
  336. if (ret < 0 && first_errno == 0)
  337. first_errno = ret;
  338. if (ret < 0)
  339. ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
  340. i, ret);
  341. spin_lock_bh(&ar->data_lock);
  342. peer->keys[i] = NULL;
  343. spin_unlock_bh(&ar->data_lock);
  344. }
  345. return first_errno;
  346. }
  347. bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
  348. u8 keyidx)
  349. {
  350. struct ath10k_peer *peer;
  351. int i;
  352. lockdep_assert_held(&ar->data_lock);
  353. /* We don't know which vdev this peer belongs to,
  354. * since WMI doesn't give us that information.
  355. *
  356. * FIXME: multi-bss needs to be handled.
  357. */
  358. peer = ath10k_peer_find(ar, 0, addr);
  359. if (!peer)
  360. return false;
  361. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  362. if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
  363. return true;
  364. }
  365. return false;
  366. }
  367. static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
  368. struct ieee80211_key_conf *key)
  369. {
  370. struct ath10k *ar = arvif->ar;
  371. struct ath10k_peer *peer;
  372. u8 addr[ETH_ALEN];
  373. int first_errno = 0;
  374. int ret;
  375. int i;
  376. u32 flags = 0;
  377. lockdep_assert_held(&ar->conf_mutex);
  378. for (;;) {
  379. /* since ath10k_install_key we can't hold data_lock all the
  380. * time, so we try to remove the keys incrementally */
  381. spin_lock_bh(&ar->data_lock);
  382. i = 0;
  383. list_for_each_entry(peer, &ar->peers, list) {
  384. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  385. if (peer->keys[i] == key) {
  386. ether_addr_copy(addr, peer->addr);
  387. peer->keys[i] = NULL;
  388. break;
  389. }
  390. }
  391. if (i < ARRAY_SIZE(peer->keys))
  392. break;
  393. }
  394. spin_unlock_bh(&ar->data_lock);
  395. if (i == ARRAY_SIZE(peer->keys))
  396. break;
  397. /* key flags are not required to delete the key */
  398. ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
  399. if (ret < 0 && first_errno == 0)
  400. first_errno = ret;
  401. if (ret)
  402. ath10k_warn(ar, "failed to remove key for %pM: %d\n",
  403. addr, ret);
  404. }
  405. return first_errno;
  406. }
  407. static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
  408. struct ieee80211_key_conf *key)
  409. {
  410. struct ath10k *ar = arvif->ar;
  411. struct ath10k_peer *peer;
  412. int ret;
  413. lockdep_assert_held(&ar->conf_mutex);
  414. list_for_each_entry(peer, &ar->peers, list) {
  415. if (ether_addr_equal(peer->addr, arvif->vif->addr))
  416. continue;
  417. if (ether_addr_equal(peer->addr, arvif->bssid))
  418. continue;
  419. if (peer->keys[key->keyidx] == key)
  420. continue;
  421. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
  422. arvif->vdev_id, key->keyidx);
  423. ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
  424. if (ret) {
  425. ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
  426. arvif->vdev_id, peer->addr, ret);
  427. return ret;
  428. }
  429. }
  430. return 0;
  431. }
  432. /*********************/
  433. /* General utilities */
  434. /*********************/
  435. static inline enum wmi_phy_mode
  436. chan_to_phymode(const struct cfg80211_chan_def *chandef)
  437. {
  438. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  439. switch (chandef->chan->band) {
  440. case NL80211_BAND_2GHZ:
  441. switch (chandef->width) {
  442. case NL80211_CHAN_WIDTH_20_NOHT:
  443. if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
  444. phymode = MODE_11B;
  445. else
  446. phymode = MODE_11G;
  447. break;
  448. case NL80211_CHAN_WIDTH_20:
  449. phymode = MODE_11NG_HT20;
  450. break;
  451. case NL80211_CHAN_WIDTH_40:
  452. phymode = MODE_11NG_HT40;
  453. break;
  454. case NL80211_CHAN_WIDTH_5:
  455. case NL80211_CHAN_WIDTH_10:
  456. case NL80211_CHAN_WIDTH_80:
  457. case NL80211_CHAN_WIDTH_80P80:
  458. case NL80211_CHAN_WIDTH_160:
  459. phymode = MODE_UNKNOWN;
  460. break;
  461. }
  462. break;
  463. case NL80211_BAND_5GHZ:
  464. switch (chandef->width) {
  465. case NL80211_CHAN_WIDTH_20_NOHT:
  466. phymode = MODE_11A;
  467. break;
  468. case NL80211_CHAN_WIDTH_20:
  469. phymode = MODE_11NA_HT20;
  470. break;
  471. case NL80211_CHAN_WIDTH_40:
  472. phymode = MODE_11NA_HT40;
  473. break;
  474. case NL80211_CHAN_WIDTH_80:
  475. phymode = MODE_11AC_VHT80;
  476. break;
  477. case NL80211_CHAN_WIDTH_160:
  478. phymode = MODE_11AC_VHT160;
  479. break;
  480. case NL80211_CHAN_WIDTH_80P80:
  481. phymode = MODE_11AC_VHT80_80;
  482. break;
  483. case NL80211_CHAN_WIDTH_5:
  484. case NL80211_CHAN_WIDTH_10:
  485. phymode = MODE_UNKNOWN;
  486. break;
  487. }
  488. break;
  489. default:
  490. break;
  491. }
  492. WARN_ON(phymode == MODE_UNKNOWN);
  493. return phymode;
  494. }
  495. static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
  496. {
  497. /*
  498. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  499. * 0 for no restriction
  500. * 1 for 1/4 us
  501. * 2 for 1/2 us
  502. * 3 for 1 us
  503. * 4 for 2 us
  504. * 5 for 4 us
  505. * 6 for 8 us
  506. * 7 for 16 us
  507. */
  508. switch (mpdudensity) {
  509. case 0:
  510. return 0;
  511. case 1:
  512. case 2:
  513. case 3:
  514. /* Our lower layer calculations limit our precision to
  515. 1 microsecond */
  516. return 1;
  517. case 4:
  518. return 2;
  519. case 5:
  520. return 4;
  521. case 6:
  522. return 8;
  523. case 7:
  524. return 16;
  525. default:
  526. return 0;
  527. }
  528. }
  529. int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
  530. struct cfg80211_chan_def *def)
  531. {
  532. struct ieee80211_chanctx_conf *conf;
  533. rcu_read_lock();
  534. conf = rcu_dereference(vif->chanctx_conf);
  535. if (!conf) {
  536. rcu_read_unlock();
  537. return -ENOENT;
  538. }
  539. *def = conf->def;
  540. rcu_read_unlock();
  541. return 0;
  542. }
  543. static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
  544. struct ieee80211_chanctx_conf *conf,
  545. void *data)
  546. {
  547. int *num = data;
  548. (*num)++;
  549. }
  550. static int ath10k_mac_num_chanctxs(struct ath10k *ar)
  551. {
  552. int num = 0;
  553. ieee80211_iter_chan_contexts_atomic(ar->hw,
  554. ath10k_mac_num_chanctxs_iter,
  555. &num);
  556. return num;
  557. }
  558. static void
  559. ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
  560. struct ieee80211_chanctx_conf *conf,
  561. void *data)
  562. {
  563. struct cfg80211_chan_def **def = data;
  564. *def = &conf->def;
  565. }
  566. static int ath10k_peer_create(struct ath10k *ar,
  567. struct ieee80211_vif *vif,
  568. struct ieee80211_sta *sta,
  569. u32 vdev_id,
  570. const u8 *addr,
  571. enum wmi_peer_type peer_type)
  572. {
  573. struct ath10k_vif *arvif;
  574. struct ath10k_peer *peer;
  575. int num_peers = 0;
  576. int ret;
  577. lockdep_assert_held(&ar->conf_mutex);
  578. num_peers = ar->num_peers;
  579. /* Each vdev consumes a peer entry as well */
  580. list_for_each_entry(arvif, &ar->arvifs, list)
  581. num_peers++;
  582. if (num_peers >= ar->max_num_peers)
  583. return -ENOBUFS;
  584. ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
  585. if (ret) {
  586. ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
  587. addr, vdev_id, ret);
  588. return ret;
  589. }
  590. ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
  591. if (ret) {
  592. ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
  593. addr, vdev_id, ret);
  594. return ret;
  595. }
  596. spin_lock_bh(&ar->data_lock);
  597. peer = ath10k_peer_find(ar, vdev_id, addr);
  598. if (!peer) {
  599. spin_unlock_bh(&ar->data_lock);
  600. ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
  601. addr, vdev_id);
  602. ath10k_wmi_peer_delete(ar, vdev_id, addr);
  603. return -ENOENT;
  604. }
  605. peer->vif = vif;
  606. peer->sta = sta;
  607. spin_unlock_bh(&ar->data_lock);
  608. ar->num_peers++;
  609. return 0;
  610. }
  611. static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
  612. {
  613. struct ath10k *ar = arvif->ar;
  614. u32 param;
  615. int ret;
  616. param = ar->wmi.pdev_param->sta_kickout_th;
  617. ret = ath10k_wmi_pdev_set_param(ar, param,
  618. ATH10K_KICKOUT_THRESHOLD);
  619. if (ret) {
  620. ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
  621. arvif->vdev_id, ret);
  622. return ret;
  623. }
  624. param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
  625. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  626. ATH10K_KEEPALIVE_MIN_IDLE);
  627. if (ret) {
  628. ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
  629. arvif->vdev_id, ret);
  630. return ret;
  631. }
  632. param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
  633. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  634. ATH10K_KEEPALIVE_MAX_IDLE);
  635. if (ret) {
  636. ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
  637. arvif->vdev_id, ret);
  638. return ret;
  639. }
  640. param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
  641. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  642. ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
  643. if (ret) {
  644. ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
  645. arvif->vdev_id, ret);
  646. return ret;
  647. }
  648. return 0;
  649. }
  650. static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
  651. {
  652. struct ath10k *ar = arvif->ar;
  653. u32 vdev_param;
  654. vdev_param = ar->wmi.vdev_param->rts_threshold;
  655. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  656. }
  657. static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  658. {
  659. int ret;
  660. lockdep_assert_held(&ar->conf_mutex);
  661. ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
  662. if (ret)
  663. return ret;
  664. ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
  665. if (ret)
  666. return ret;
  667. ar->num_peers--;
  668. return 0;
  669. }
  670. static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
  671. {
  672. struct ath10k_peer *peer, *tmp;
  673. int peer_id;
  674. int i;
  675. lockdep_assert_held(&ar->conf_mutex);
  676. spin_lock_bh(&ar->data_lock);
  677. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  678. if (peer->vdev_id != vdev_id)
  679. continue;
  680. ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
  681. peer->addr, vdev_id);
  682. for_each_set_bit(peer_id, peer->peer_ids,
  683. ATH10K_MAX_NUM_PEER_IDS) {
  684. ar->peer_map[peer_id] = NULL;
  685. }
  686. /* Double check that peer is properly un-referenced from
  687. * the peer_map
  688. */
  689. for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
  690. if (ar->peer_map[i] == peer) {
  691. ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %pK idx %d)\n",
  692. peer->addr, peer, i);
  693. ar->peer_map[i] = NULL;
  694. }
  695. }
  696. list_del(&peer->list);
  697. kfree(peer);
  698. ar->num_peers--;
  699. }
  700. spin_unlock_bh(&ar->data_lock);
  701. }
  702. static void ath10k_peer_cleanup_all(struct ath10k *ar)
  703. {
  704. struct ath10k_peer *peer, *tmp;
  705. int i;
  706. lockdep_assert_held(&ar->conf_mutex);
  707. spin_lock_bh(&ar->data_lock);
  708. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  709. list_del(&peer->list);
  710. kfree(peer);
  711. }
  712. for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
  713. ar->peer_map[i] = NULL;
  714. spin_unlock_bh(&ar->data_lock);
  715. ar->num_peers = 0;
  716. ar->num_stations = 0;
  717. }
  718. static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
  719. struct ieee80211_sta *sta,
  720. enum wmi_tdls_peer_state state)
  721. {
  722. int ret;
  723. struct wmi_tdls_peer_update_cmd_arg arg = {};
  724. struct wmi_tdls_peer_capab_arg cap = {};
  725. struct wmi_channel_arg chan_arg = {};
  726. lockdep_assert_held(&ar->conf_mutex);
  727. arg.vdev_id = vdev_id;
  728. arg.peer_state = state;
  729. ether_addr_copy(arg.addr, sta->addr);
  730. cap.peer_max_sp = sta->max_sp;
  731. cap.peer_uapsd_queues = sta->uapsd_queues;
  732. if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
  733. !sta->tdls_initiator)
  734. cap.is_peer_responder = 1;
  735. ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
  736. if (ret) {
  737. ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
  738. arg.addr, vdev_id, ret);
  739. return ret;
  740. }
  741. return 0;
  742. }
  743. /************************/
  744. /* Interface management */
  745. /************************/
  746. void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
  747. {
  748. struct ath10k *ar = arvif->ar;
  749. lockdep_assert_held(&ar->data_lock);
  750. if (!arvif->beacon)
  751. return;
  752. if (!arvif->beacon_buf)
  753. dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
  754. arvif->beacon->len, DMA_TO_DEVICE);
  755. if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
  756. arvif->beacon_state != ATH10K_BEACON_SENT))
  757. return;
  758. dev_kfree_skb_any(arvif->beacon);
  759. arvif->beacon = NULL;
  760. arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
  761. }
  762. static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
  763. {
  764. struct ath10k *ar = arvif->ar;
  765. lockdep_assert_held(&ar->data_lock);
  766. ath10k_mac_vif_beacon_free(arvif);
  767. if (arvif->beacon_buf) {
  768. dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
  769. arvif->beacon_buf, arvif->beacon_paddr);
  770. arvif->beacon_buf = NULL;
  771. }
  772. }
  773. static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
  774. {
  775. unsigned long time_left;
  776. lockdep_assert_held(&ar->conf_mutex);
  777. if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
  778. return -ESHUTDOWN;
  779. time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
  780. ATH10K_VDEV_SETUP_TIMEOUT_HZ);
  781. if (time_left == 0)
  782. return -ETIMEDOUT;
  783. return 0;
  784. }
  785. static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
  786. {
  787. struct cfg80211_chan_def *chandef = NULL;
  788. struct ieee80211_channel *channel = NULL;
  789. struct wmi_vdev_start_request_arg arg = {};
  790. int ret = 0;
  791. lockdep_assert_held(&ar->conf_mutex);
  792. ieee80211_iter_chan_contexts_atomic(ar->hw,
  793. ath10k_mac_get_any_chandef_iter,
  794. &chandef);
  795. if (WARN_ON_ONCE(!chandef))
  796. return -ENOENT;
  797. channel = chandef->chan;
  798. arg.vdev_id = vdev_id;
  799. arg.channel.freq = channel->center_freq;
  800. arg.channel.band_center_freq1 = chandef->center_freq1;
  801. arg.channel.band_center_freq2 = chandef->center_freq2;
  802. /* TODO setup this dynamically, what in case we
  803. don't have any vifs? */
  804. arg.channel.mode = chan_to_phymode(chandef);
  805. arg.channel.chan_radar =
  806. !!(channel->flags & IEEE80211_CHAN_RADAR);
  807. arg.channel.min_power = 0;
  808. arg.channel.max_power = channel->max_power * 2;
  809. arg.channel.max_reg_power = channel->max_reg_power * 2;
  810. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  811. reinit_completion(&ar->vdev_setup_done);
  812. ret = ath10k_wmi_vdev_start(ar, &arg);
  813. if (ret) {
  814. ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
  815. vdev_id, ret);
  816. return ret;
  817. }
  818. ret = ath10k_vdev_setup_sync(ar);
  819. if (ret) {
  820. ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
  821. vdev_id, ret);
  822. return ret;
  823. }
  824. ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
  825. if (ret) {
  826. ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
  827. vdev_id, ret);
  828. goto vdev_stop;
  829. }
  830. ar->monitor_vdev_id = vdev_id;
  831. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
  832. ar->monitor_vdev_id);
  833. return 0;
  834. vdev_stop:
  835. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  836. if (ret)
  837. ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
  838. ar->monitor_vdev_id, ret);
  839. return ret;
  840. }
  841. static int ath10k_monitor_vdev_stop(struct ath10k *ar)
  842. {
  843. int ret = 0;
  844. lockdep_assert_held(&ar->conf_mutex);
  845. ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
  846. if (ret)
  847. ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
  848. ar->monitor_vdev_id, ret);
  849. reinit_completion(&ar->vdev_setup_done);
  850. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  851. if (ret)
  852. ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
  853. ar->monitor_vdev_id, ret);
  854. ret = ath10k_vdev_setup_sync(ar);
  855. if (ret)
  856. ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
  857. ar->monitor_vdev_id, ret);
  858. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
  859. ar->monitor_vdev_id);
  860. return ret;
  861. }
  862. static int ath10k_monitor_vdev_create(struct ath10k *ar)
  863. {
  864. int bit, ret = 0;
  865. lockdep_assert_held(&ar->conf_mutex);
  866. if (ar->free_vdev_map == 0) {
  867. ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
  868. return -ENOMEM;
  869. }
  870. bit = __ffs64(ar->free_vdev_map);
  871. ar->monitor_vdev_id = bit;
  872. ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
  873. WMI_VDEV_TYPE_MONITOR,
  874. 0, ar->mac_addr);
  875. if (ret) {
  876. ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
  877. ar->monitor_vdev_id, ret);
  878. return ret;
  879. }
  880. ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
  881. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
  882. ar->monitor_vdev_id);
  883. return 0;
  884. }
  885. static int ath10k_monitor_vdev_delete(struct ath10k *ar)
  886. {
  887. int ret = 0;
  888. lockdep_assert_held(&ar->conf_mutex);
  889. ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
  890. if (ret) {
  891. ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
  892. ar->monitor_vdev_id, ret);
  893. return ret;
  894. }
  895. ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
  896. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
  897. ar->monitor_vdev_id);
  898. return ret;
  899. }
  900. static int ath10k_monitor_start(struct ath10k *ar)
  901. {
  902. int ret;
  903. lockdep_assert_held(&ar->conf_mutex);
  904. ret = ath10k_monitor_vdev_create(ar);
  905. if (ret) {
  906. ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
  907. return ret;
  908. }
  909. ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
  910. if (ret) {
  911. ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
  912. ath10k_monitor_vdev_delete(ar);
  913. return ret;
  914. }
  915. ar->monitor_started = true;
  916. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
  917. return 0;
  918. }
  919. static int ath10k_monitor_stop(struct ath10k *ar)
  920. {
  921. int ret;
  922. lockdep_assert_held(&ar->conf_mutex);
  923. ret = ath10k_monitor_vdev_stop(ar);
  924. if (ret) {
  925. ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
  926. return ret;
  927. }
  928. ret = ath10k_monitor_vdev_delete(ar);
  929. if (ret) {
  930. ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
  931. return ret;
  932. }
  933. ar->monitor_started = false;
  934. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
  935. return 0;
  936. }
  937. static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
  938. {
  939. int num_ctx;
  940. /* At least one chanctx is required to derive a channel to start
  941. * monitor vdev on.
  942. */
  943. num_ctx = ath10k_mac_num_chanctxs(ar);
  944. if (num_ctx == 0)
  945. return false;
  946. /* If there's already an existing special monitor interface then don't
  947. * bother creating another monitor vdev.
  948. */
  949. if (ar->monitor_arvif)
  950. return false;
  951. return ar->monitor ||
  952. (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
  953. ar->running_fw->fw_file.fw_features) &&
  954. (ar->filter_flags & FIF_OTHER_BSS)) ||
  955. test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  956. }
  957. static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
  958. {
  959. int num_ctx;
  960. num_ctx = ath10k_mac_num_chanctxs(ar);
  961. /* FIXME: Current interface combinations and cfg80211/mac80211 code
  962. * shouldn't allow this but make sure to prevent handling the following
  963. * case anyway since multi-channel DFS hasn't been tested at all.
  964. */
  965. if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
  966. return false;
  967. return true;
  968. }
  969. static int ath10k_monitor_recalc(struct ath10k *ar)
  970. {
  971. bool needed;
  972. bool allowed;
  973. int ret;
  974. lockdep_assert_held(&ar->conf_mutex);
  975. needed = ath10k_mac_monitor_vdev_is_needed(ar);
  976. allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
  977. ath10k_dbg(ar, ATH10K_DBG_MAC,
  978. "mac monitor recalc started? %d needed? %d allowed? %d\n",
  979. ar->monitor_started, needed, allowed);
  980. if (WARN_ON(needed && !allowed)) {
  981. if (ar->monitor_started) {
  982. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
  983. ret = ath10k_monitor_stop(ar);
  984. if (ret)
  985. ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
  986. ret);
  987. /* not serious */
  988. }
  989. return -EPERM;
  990. }
  991. if (needed == ar->monitor_started)
  992. return 0;
  993. if (needed)
  994. return ath10k_monitor_start(ar);
  995. else
  996. return ath10k_monitor_stop(ar);
  997. }
  998. static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
  999. {
  1000. struct ath10k *ar = arvif->ar;
  1001. lockdep_assert_held(&ar->conf_mutex);
  1002. if (!arvif->is_started) {
  1003. ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
  1004. return false;
  1005. }
  1006. return true;
  1007. }
  1008. static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
  1009. {
  1010. struct ath10k *ar = arvif->ar;
  1011. u32 vdev_param;
  1012. lockdep_assert_held(&ar->conf_mutex);
  1013. vdev_param = ar->wmi.vdev_param->protection_mode;
  1014. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
  1015. arvif->vdev_id, arvif->use_cts_prot);
  1016. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  1017. arvif->use_cts_prot ? 1 : 0);
  1018. }
  1019. static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
  1020. {
  1021. struct ath10k *ar = arvif->ar;
  1022. u32 vdev_param, rts_cts = 0;
  1023. lockdep_assert_held(&ar->conf_mutex);
  1024. vdev_param = ar->wmi.vdev_param->enable_rtscts;
  1025. rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
  1026. if (arvif->num_legacy_stations > 0)
  1027. rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
  1028. WMI_RTSCTS_PROFILE);
  1029. else
  1030. rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
  1031. WMI_RTSCTS_PROFILE);
  1032. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
  1033. arvif->vdev_id, rts_cts);
  1034. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  1035. rts_cts);
  1036. }
  1037. static int ath10k_start_cac(struct ath10k *ar)
  1038. {
  1039. int ret;
  1040. lockdep_assert_held(&ar->conf_mutex);
  1041. set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  1042. ret = ath10k_monitor_recalc(ar);
  1043. if (ret) {
  1044. ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
  1045. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  1046. return ret;
  1047. }
  1048. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
  1049. ar->monitor_vdev_id);
  1050. return 0;
  1051. }
  1052. static int ath10k_stop_cac(struct ath10k *ar)
  1053. {
  1054. lockdep_assert_held(&ar->conf_mutex);
  1055. /* CAC is not running - do nothing */
  1056. if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
  1057. return 0;
  1058. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  1059. ath10k_monitor_stop(ar);
  1060. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
  1061. return 0;
  1062. }
  1063. static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
  1064. struct ieee80211_chanctx_conf *conf,
  1065. void *data)
  1066. {
  1067. bool *ret = data;
  1068. if (!*ret && conf->radar_enabled)
  1069. *ret = true;
  1070. }
  1071. static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
  1072. {
  1073. bool has_radar = false;
  1074. ieee80211_iter_chan_contexts_atomic(ar->hw,
  1075. ath10k_mac_has_radar_iter,
  1076. &has_radar);
  1077. return has_radar;
  1078. }
  1079. static void ath10k_recalc_radar_detection(struct ath10k *ar)
  1080. {
  1081. int ret;
  1082. lockdep_assert_held(&ar->conf_mutex);
  1083. ath10k_stop_cac(ar);
  1084. if (!ath10k_mac_has_radar_enabled(ar))
  1085. return;
  1086. if (ar->num_started_vdevs > 0)
  1087. return;
  1088. ret = ath10k_start_cac(ar);
  1089. if (ret) {
  1090. /*
  1091. * Not possible to start CAC on current channel so starting
  1092. * radiation is not allowed, make this channel DFS_UNAVAILABLE
  1093. * by indicating that radar was detected.
  1094. */
  1095. ath10k_warn(ar, "failed to start CAC: %d\n", ret);
  1096. ieee80211_radar_detected(ar->hw);
  1097. }
  1098. }
  1099. static int ath10k_vdev_stop(struct ath10k_vif *arvif)
  1100. {
  1101. struct ath10k *ar = arvif->ar;
  1102. int ret;
  1103. lockdep_assert_held(&ar->conf_mutex);
  1104. reinit_completion(&ar->vdev_setup_done);
  1105. ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
  1106. if (ret) {
  1107. ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
  1108. arvif->vdev_id, ret);
  1109. return ret;
  1110. }
  1111. ret = ath10k_vdev_setup_sync(ar);
  1112. if (ret) {
  1113. ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
  1114. arvif->vdev_id, ret);
  1115. return ret;
  1116. }
  1117. WARN_ON(ar->num_started_vdevs == 0);
  1118. if (ar->num_started_vdevs != 0) {
  1119. ar->num_started_vdevs--;
  1120. ath10k_recalc_radar_detection(ar);
  1121. }
  1122. return ret;
  1123. }
  1124. static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
  1125. const struct cfg80211_chan_def *chandef,
  1126. bool restart)
  1127. {
  1128. struct ath10k *ar = arvif->ar;
  1129. struct wmi_vdev_start_request_arg arg = {};
  1130. int ret = 0;
  1131. lockdep_assert_held(&ar->conf_mutex);
  1132. reinit_completion(&ar->vdev_setup_done);
  1133. arg.vdev_id = arvif->vdev_id;
  1134. arg.dtim_period = arvif->dtim_period;
  1135. arg.bcn_intval = arvif->beacon_interval;
  1136. arg.channel.freq = chandef->chan->center_freq;
  1137. arg.channel.band_center_freq1 = chandef->center_freq1;
  1138. arg.channel.band_center_freq2 = chandef->center_freq2;
  1139. arg.channel.mode = chan_to_phymode(chandef);
  1140. arg.channel.min_power = 0;
  1141. arg.channel.max_power = chandef->chan->max_power * 2;
  1142. arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
  1143. arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
  1144. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  1145. arg.ssid = arvif->u.ap.ssid;
  1146. arg.ssid_len = arvif->u.ap.ssid_len;
  1147. arg.hidden_ssid = arvif->u.ap.hidden_ssid;
  1148. /* For now allow DFS for AP mode */
  1149. arg.channel.chan_radar =
  1150. !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
  1151. } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  1152. arg.ssid = arvif->vif->bss_conf.ssid;
  1153. arg.ssid_len = arvif->vif->bss_conf.ssid_len;
  1154. }
  1155. ath10k_dbg(ar, ATH10K_DBG_MAC,
  1156. "mac vdev %d start center_freq %d phymode %s\n",
  1157. arg.vdev_id, arg.channel.freq,
  1158. ath10k_wmi_phymode_str(arg.channel.mode));
  1159. if (restart)
  1160. ret = ath10k_wmi_vdev_restart(ar, &arg);
  1161. else
  1162. ret = ath10k_wmi_vdev_start(ar, &arg);
  1163. if (ret) {
  1164. ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
  1165. arg.vdev_id, ret);
  1166. return ret;
  1167. }
  1168. ret = ath10k_vdev_setup_sync(ar);
  1169. if (ret) {
  1170. ath10k_warn(ar,
  1171. "failed to synchronize setup for vdev %i restart %d: %d\n",
  1172. arg.vdev_id, restart, ret);
  1173. return ret;
  1174. }
  1175. ar->num_started_vdevs++;
  1176. ath10k_recalc_radar_detection(ar);
  1177. return ret;
  1178. }
  1179. static int ath10k_vdev_start(struct ath10k_vif *arvif,
  1180. const struct cfg80211_chan_def *def)
  1181. {
  1182. return ath10k_vdev_start_restart(arvif, def, false);
  1183. }
  1184. static int ath10k_vdev_restart(struct ath10k_vif *arvif,
  1185. const struct cfg80211_chan_def *def)
  1186. {
  1187. return ath10k_vdev_start_restart(arvif, def, true);
  1188. }
  1189. static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
  1190. struct sk_buff *bcn)
  1191. {
  1192. struct ath10k *ar = arvif->ar;
  1193. struct ieee80211_mgmt *mgmt;
  1194. const u8 *p2p_ie;
  1195. int ret;
  1196. if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
  1197. return 0;
  1198. mgmt = (void *)bcn->data;
  1199. p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
  1200. mgmt->u.beacon.variable,
  1201. bcn->len - (mgmt->u.beacon.variable -
  1202. bcn->data));
  1203. if (!p2p_ie)
  1204. return -ENOENT;
  1205. ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
  1206. if (ret) {
  1207. ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
  1208. arvif->vdev_id, ret);
  1209. return ret;
  1210. }
  1211. return 0;
  1212. }
  1213. static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
  1214. u8 oui_type, size_t ie_offset)
  1215. {
  1216. size_t len;
  1217. const u8 *next;
  1218. const u8 *end;
  1219. u8 *ie;
  1220. if (WARN_ON(skb->len < ie_offset))
  1221. return -EINVAL;
  1222. ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
  1223. skb->data + ie_offset,
  1224. skb->len - ie_offset);
  1225. if (!ie)
  1226. return -ENOENT;
  1227. len = ie[1] + 2;
  1228. end = skb->data + skb->len;
  1229. next = ie + len;
  1230. if (WARN_ON(next > end))
  1231. return -EINVAL;
  1232. memmove(ie, next, end - next);
  1233. skb_trim(skb, skb->len - len);
  1234. return 0;
  1235. }
  1236. static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
  1237. {
  1238. struct ath10k *ar = arvif->ar;
  1239. struct ieee80211_hw *hw = ar->hw;
  1240. struct ieee80211_vif *vif = arvif->vif;
  1241. struct ieee80211_mutable_offsets offs = {};
  1242. struct sk_buff *bcn;
  1243. int ret;
  1244. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1245. return 0;
  1246. if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
  1247. arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
  1248. return 0;
  1249. bcn = ieee80211_beacon_get_template(hw, vif, &offs);
  1250. if (!bcn) {
  1251. ath10k_warn(ar, "failed to get beacon template from mac80211\n");
  1252. return -EPERM;
  1253. }
  1254. ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
  1255. if (ret) {
  1256. ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
  1257. kfree_skb(bcn);
  1258. return ret;
  1259. }
  1260. /* P2P IE is inserted by firmware automatically (as configured above)
  1261. * so remove it from the base beacon template to avoid duplicate P2P
  1262. * IEs in beacon frames.
  1263. */
  1264. ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
  1265. offsetof(struct ieee80211_mgmt,
  1266. u.beacon.variable));
  1267. ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
  1268. 0, NULL, 0);
  1269. kfree_skb(bcn);
  1270. if (ret) {
  1271. ath10k_warn(ar, "failed to submit beacon template command: %d\n",
  1272. ret);
  1273. return ret;
  1274. }
  1275. return 0;
  1276. }
  1277. static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
  1278. {
  1279. struct ath10k *ar = arvif->ar;
  1280. struct ieee80211_hw *hw = ar->hw;
  1281. struct ieee80211_vif *vif = arvif->vif;
  1282. struct sk_buff *prb;
  1283. int ret;
  1284. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1285. return 0;
  1286. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  1287. return 0;
  1288. prb = ieee80211_proberesp_get(hw, vif);
  1289. if (!prb) {
  1290. ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
  1291. return -EPERM;
  1292. }
  1293. ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
  1294. kfree_skb(prb);
  1295. if (ret) {
  1296. ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
  1297. ret);
  1298. return ret;
  1299. }
  1300. return 0;
  1301. }
  1302. static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
  1303. {
  1304. struct ath10k *ar = arvif->ar;
  1305. struct cfg80211_chan_def def;
  1306. int ret;
  1307. /* When originally vdev is started during assign_vif_chanctx() some
  1308. * information is missing, notably SSID. Firmware revisions with beacon
  1309. * offloading require the SSID to be provided during vdev (re)start to
  1310. * handle hidden SSID properly.
  1311. *
  1312. * Vdev restart must be done after vdev has been both started and
  1313. * upped. Otherwise some firmware revisions (at least 10.2) fail to
  1314. * deliver vdev restart response event causing timeouts during vdev
  1315. * syncing in ath10k.
  1316. *
  1317. * Note: The vdev down/up and template reinstallation could be skipped
  1318. * since only wmi-tlv firmware are known to have beacon offload and
  1319. * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
  1320. * response delivery. It's probably more robust to keep it as is.
  1321. */
  1322. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1323. return 0;
  1324. if (WARN_ON(!arvif->is_started))
  1325. return -EINVAL;
  1326. if (WARN_ON(!arvif->is_up))
  1327. return -EINVAL;
  1328. if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
  1329. return -EINVAL;
  1330. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1331. if (ret) {
  1332. ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
  1333. arvif->vdev_id, ret);
  1334. return ret;
  1335. }
  1336. /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
  1337. * firmware will crash upon vdev up.
  1338. */
  1339. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  1340. if (ret) {
  1341. ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
  1342. return ret;
  1343. }
  1344. ret = ath10k_mac_setup_prb_tmpl(arvif);
  1345. if (ret) {
  1346. ath10k_warn(ar, "failed to update presp template: %d\n", ret);
  1347. return ret;
  1348. }
  1349. ret = ath10k_vdev_restart(arvif, &def);
  1350. if (ret) {
  1351. ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
  1352. arvif->vdev_id, ret);
  1353. return ret;
  1354. }
  1355. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  1356. arvif->bssid);
  1357. if (ret) {
  1358. ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
  1359. arvif->vdev_id, ret);
  1360. return ret;
  1361. }
  1362. return 0;
  1363. }
  1364. static void ath10k_control_beaconing(struct ath10k_vif *arvif,
  1365. struct ieee80211_bss_conf *info)
  1366. {
  1367. struct ath10k *ar = arvif->ar;
  1368. int ret = 0;
  1369. lockdep_assert_held(&arvif->ar->conf_mutex);
  1370. if (!info->enable_beacon) {
  1371. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1372. if (ret)
  1373. ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
  1374. arvif->vdev_id, ret);
  1375. arvif->is_up = false;
  1376. spin_lock_bh(&arvif->ar->data_lock);
  1377. ath10k_mac_vif_beacon_free(arvif);
  1378. spin_unlock_bh(&arvif->ar->data_lock);
  1379. return;
  1380. }
  1381. arvif->tx_seq_no = 0x1000;
  1382. arvif->aid = 0;
  1383. ether_addr_copy(arvif->bssid, info->bssid);
  1384. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  1385. arvif->bssid);
  1386. if (ret) {
  1387. ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
  1388. arvif->vdev_id, ret);
  1389. return;
  1390. }
  1391. arvif->is_up = true;
  1392. ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
  1393. if (ret) {
  1394. ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
  1395. arvif->vdev_id, ret);
  1396. return;
  1397. }
  1398. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
  1399. }
  1400. static void ath10k_control_ibss(struct ath10k_vif *arvif,
  1401. struct ieee80211_bss_conf *info,
  1402. const u8 self_peer[ETH_ALEN])
  1403. {
  1404. struct ath10k *ar = arvif->ar;
  1405. u32 vdev_param;
  1406. int ret = 0;
  1407. lockdep_assert_held(&arvif->ar->conf_mutex);
  1408. if (!info->ibss_joined) {
  1409. if (is_zero_ether_addr(arvif->bssid))
  1410. return;
  1411. eth_zero_addr(arvif->bssid);
  1412. return;
  1413. }
  1414. vdev_param = arvif->ar->wmi.vdev_param->atim_window;
  1415. ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
  1416. ATH10K_DEFAULT_ATIM);
  1417. if (ret)
  1418. ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
  1419. arvif->vdev_id, ret);
  1420. }
  1421. static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
  1422. {
  1423. struct ath10k *ar = arvif->ar;
  1424. u32 param;
  1425. u32 value;
  1426. int ret;
  1427. lockdep_assert_held(&arvif->ar->conf_mutex);
  1428. if (arvif->u.sta.uapsd)
  1429. value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
  1430. else
  1431. value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
  1432. param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
  1433. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
  1434. if (ret) {
  1435. ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
  1436. value, arvif->vdev_id, ret);
  1437. return ret;
  1438. }
  1439. return 0;
  1440. }
  1441. static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
  1442. {
  1443. struct ath10k *ar = arvif->ar;
  1444. u32 param;
  1445. u32 value;
  1446. int ret;
  1447. lockdep_assert_held(&arvif->ar->conf_mutex);
  1448. if (arvif->u.sta.uapsd)
  1449. value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
  1450. else
  1451. value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
  1452. param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
  1453. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  1454. param, value);
  1455. if (ret) {
  1456. ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
  1457. value, arvif->vdev_id, ret);
  1458. return ret;
  1459. }
  1460. return 0;
  1461. }
  1462. static int ath10k_mac_num_vifs_started(struct ath10k *ar)
  1463. {
  1464. struct ath10k_vif *arvif;
  1465. int num = 0;
  1466. lockdep_assert_held(&ar->conf_mutex);
  1467. list_for_each_entry(arvif, &ar->arvifs, list)
  1468. if (arvif->is_started)
  1469. num++;
  1470. return num;
  1471. }
  1472. static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
  1473. {
  1474. struct ath10k *ar = arvif->ar;
  1475. struct ieee80211_vif *vif = arvif->vif;
  1476. struct ieee80211_conf *conf = &ar->hw->conf;
  1477. enum wmi_sta_powersave_param param;
  1478. enum wmi_sta_ps_mode psmode;
  1479. int ret;
  1480. int ps_timeout;
  1481. bool enable_ps;
  1482. lockdep_assert_held(&arvif->ar->conf_mutex);
  1483. if (arvif->vif->type != NL80211_IFTYPE_STATION)
  1484. return 0;
  1485. enable_ps = arvif->ps;
  1486. if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
  1487. !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
  1488. ar->running_fw->fw_file.fw_features)) {
  1489. ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
  1490. arvif->vdev_id);
  1491. enable_ps = false;
  1492. }
  1493. if (!arvif->is_started) {
  1494. /* mac80211 can update vif powersave state while disconnected.
  1495. * Firmware doesn't behave nicely and consumes more power than
  1496. * necessary if PS is disabled on a non-started vdev. Hence
  1497. * force-enable PS for non-running vdevs.
  1498. */
  1499. psmode = WMI_STA_PS_MODE_ENABLED;
  1500. } else if (enable_ps) {
  1501. psmode = WMI_STA_PS_MODE_ENABLED;
  1502. param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
  1503. ps_timeout = conf->dynamic_ps_timeout;
  1504. if (ps_timeout == 0) {
  1505. /* Firmware doesn't like 0 */
  1506. ps_timeout = ieee80211_tu_to_usec(
  1507. vif->bss_conf.beacon_int) / 1000;
  1508. }
  1509. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
  1510. ps_timeout);
  1511. if (ret) {
  1512. ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
  1513. arvif->vdev_id, ret);
  1514. return ret;
  1515. }
  1516. } else {
  1517. psmode = WMI_STA_PS_MODE_DISABLED;
  1518. }
  1519. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
  1520. arvif->vdev_id, psmode ? "enable" : "disable");
  1521. ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
  1522. if (ret) {
  1523. ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
  1524. psmode, arvif->vdev_id, ret);
  1525. return ret;
  1526. }
  1527. return 0;
  1528. }
  1529. static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
  1530. {
  1531. struct ath10k *ar = arvif->ar;
  1532. struct wmi_sta_keepalive_arg arg = {};
  1533. int ret;
  1534. lockdep_assert_held(&arvif->ar->conf_mutex);
  1535. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  1536. return 0;
  1537. if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
  1538. return 0;
  1539. /* Some firmware revisions have a bug and ignore the `enabled` field.
  1540. * Instead use the interval to disable the keepalive.
  1541. */
  1542. arg.vdev_id = arvif->vdev_id;
  1543. arg.enabled = 1;
  1544. arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
  1545. arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
  1546. ret = ath10k_wmi_sta_keepalive(ar, &arg);
  1547. if (ret) {
  1548. ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
  1549. arvif->vdev_id, ret);
  1550. return ret;
  1551. }
  1552. return 0;
  1553. }
  1554. static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
  1555. {
  1556. struct ath10k *ar = arvif->ar;
  1557. struct ieee80211_vif *vif = arvif->vif;
  1558. int ret;
  1559. lockdep_assert_held(&arvif->ar->conf_mutex);
  1560. if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
  1561. return;
  1562. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  1563. return;
  1564. if (!vif->csa_active)
  1565. return;
  1566. if (!arvif->is_up)
  1567. return;
  1568. if (!ieee80211_csa_is_complete(vif)) {
  1569. ieee80211_csa_update_counter(vif);
  1570. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  1571. if (ret)
  1572. ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
  1573. ret);
  1574. ret = ath10k_mac_setup_prb_tmpl(arvif);
  1575. if (ret)
  1576. ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
  1577. ret);
  1578. } else {
  1579. ieee80211_csa_finish(vif);
  1580. }
  1581. }
  1582. static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
  1583. {
  1584. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1585. ap_csa_work);
  1586. struct ath10k *ar = arvif->ar;
  1587. mutex_lock(&ar->conf_mutex);
  1588. ath10k_mac_vif_ap_csa_count_down(arvif);
  1589. mutex_unlock(&ar->conf_mutex);
  1590. }
  1591. static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
  1592. struct ieee80211_vif *vif)
  1593. {
  1594. struct sk_buff *skb = data;
  1595. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1596. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1597. if (vif->type != NL80211_IFTYPE_STATION)
  1598. return;
  1599. if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
  1600. return;
  1601. cancel_delayed_work(&arvif->connection_loss_work);
  1602. }
  1603. void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
  1604. {
  1605. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  1606. IEEE80211_IFACE_ITER_NORMAL,
  1607. ath10k_mac_handle_beacon_iter,
  1608. skb);
  1609. }
  1610. static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
  1611. struct ieee80211_vif *vif)
  1612. {
  1613. u32 *vdev_id = data;
  1614. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1615. struct ath10k *ar = arvif->ar;
  1616. struct ieee80211_hw *hw = ar->hw;
  1617. if (arvif->vdev_id != *vdev_id)
  1618. return;
  1619. if (!arvif->is_up)
  1620. return;
  1621. ieee80211_beacon_loss(vif);
  1622. /* Firmware doesn't report beacon loss events repeatedly. If AP probe
  1623. * (done by mac80211) succeeds but beacons do not resume then it
  1624. * doesn't make sense to continue operation. Queue connection loss work
  1625. * which can be cancelled when beacon is received.
  1626. */
  1627. ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
  1628. ATH10K_CONNECTION_LOSS_HZ);
  1629. }
  1630. void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
  1631. {
  1632. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  1633. IEEE80211_IFACE_ITER_NORMAL,
  1634. ath10k_mac_handle_beacon_miss_iter,
  1635. &vdev_id);
  1636. }
  1637. static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
  1638. {
  1639. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1640. connection_loss_work.work);
  1641. struct ieee80211_vif *vif = arvif->vif;
  1642. if (!arvif->is_up)
  1643. return;
  1644. ieee80211_connection_loss(vif);
  1645. }
  1646. /**********************/
  1647. /* Station management */
  1648. /**********************/
  1649. static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
  1650. struct ieee80211_vif *vif)
  1651. {
  1652. /* Some firmware revisions have unstable STA powersave when listen
  1653. * interval is set too high (e.g. 5). The symptoms are firmware doesn't
  1654. * generate NullFunc frames properly even if buffered frames have been
  1655. * indicated in Beacon TIM. Firmware would seldom wake up to pull
  1656. * buffered frames. Often pinging the device from AP would simply fail.
  1657. *
  1658. * As a workaround set it to 1.
  1659. */
  1660. if (vif->type == NL80211_IFTYPE_STATION)
  1661. return 1;
  1662. return ar->hw->conf.listen_interval;
  1663. }
  1664. static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
  1665. struct ieee80211_vif *vif,
  1666. struct ieee80211_sta *sta,
  1667. struct wmi_peer_assoc_complete_arg *arg)
  1668. {
  1669. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1670. u32 aid;
  1671. lockdep_assert_held(&ar->conf_mutex);
  1672. if (vif->type == NL80211_IFTYPE_STATION)
  1673. aid = vif->bss_conf.aid;
  1674. else
  1675. aid = sta->aid;
  1676. ether_addr_copy(arg->addr, sta->addr);
  1677. arg->vdev_id = arvif->vdev_id;
  1678. arg->peer_aid = aid;
  1679. arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
  1680. arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
  1681. arg->peer_num_spatial_streams = 1;
  1682. arg->peer_caps = vif->bss_conf.assoc_capability;
  1683. }
  1684. static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
  1685. struct ieee80211_vif *vif,
  1686. struct ieee80211_sta *sta,
  1687. struct wmi_peer_assoc_complete_arg *arg)
  1688. {
  1689. struct ieee80211_bss_conf *info = &vif->bss_conf;
  1690. struct cfg80211_chan_def def;
  1691. struct cfg80211_bss *bss;
  1692. const u8 *rsnie = NULL;
  1693. const u8 *wpaie = NULL;
  1694. lockdep_assert_held(&ar->conf_mutex);
  1695. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1696. return;
  1697. bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
  1698. IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
  1699. if (bss) {
  1700. const struct cfg80211_bss_ies *ies;
  1701. rcu_read_lock();
  1702. rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
  1703. ies = rcu_dereference(bss->ies);
  1704. wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  1705. WLAN_OUI_TYPE_MICROSOFT_WPA,
  1706. ies->data,
  1707. ies->len);
  1708. rcu_read_unlock();
  1709. cfg80211_put_bss(ar->hw->wiphy, bss);
  1710. }
  1711. /* FIXME: base on RSN IE/WPA IE is a correct idea? */
  1712. if (rsnie || wpaie) {
  1713. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
  1714. arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
  1715. }
  1716. if (wpaie) {
  1717. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
  1718. arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
  1719. }
  1720. if (sta->mfp &&
  1721. test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
  1722. ar->running_fw->fw_file.fw_features)) {
  1723. arg->peer_flags |= ar->wmi.peer_flags->pmf;
  1724. }
  1725. }
  1726. static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
  1727. struct ieee80211_vif *vif,
  1728. struct ieee80211_sta *sta,
  1729. struct wmi_peer_assoc_complete_arg *arg)
  1730. {
  1731. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1732. struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
  1733. struct cfg80211_chan_def def;
  1734. const struct ieee80211_supported_band *sband;
  1735. const struct ieee80211_rate *rates;
  1736. enum nl80211_band band;
  1737. u32 ratemask;
  1738. u8 rate;
  1739. int i;
  1740. lockdep_assert_held(&ar->conf_mutex);
  1741. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1742. return;
  1743. band = def.chan->band;
  1744. sband = ar->hw->wiphy->bands[band];
  1745. ratemask = sta->supp_rates[band];
  1746. ratemask &= arvif->bitrate_mask.control[band].legacy;
  1747. rates = sband->bitrates;
  1748. rateset->num_rates = 0;
  1749. for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
  1750. if (!(ratemask & 1))
  1751. continue;
  1752. rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
  1753. rateset->rates[rateset->num_rates] = rate;
  1754. rateset->num_rates++;
  1755. }
  1756. }
  1757. static bool
  1758. ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  1759. {
  1760. int nss;
  1761. for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
  1762. if (ht_mcs_mask[nss])
  1763. return false;
  1764. return true;
  1765. }
  1766. static bool
  1767. ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
  1768. {
  1769. int nss;
  1770. for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
  1771. if (vht_mcs_mask[nss])
  1772. return false;
  1773. return true;
  1774. }
  1775. static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
  1776. struct ieee80211_vif *vif,
  1777. struct ieee80211_sta *sta,
  1778. struct wmi_peer_assoc_complete_arg *arg)
  1779. {
  1780. const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1781. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1782. struct cfg80211_chan_def def;
  1783. enum nl80211_band band;
  1784. const u8 *ht_mcs_mask;
  1785. const u16 *vht_mcs_mask;
  1786. int i, n;
  1787. u8 max_nss;
  1788. u32 stbc;
  1789. lockdep_assert_held(&ar->conf_mutex);
  1790. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1791. return;
  1792. if (!ht_cap->ht_supported)
  1793. return;
  1794. band = def.chan->band;
  1795. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  1796. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  1797. if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
  1798. ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
  1799. return;
  1800. arg->peer_flags |= ar->wmi.peer_flags->ht;
  1801. arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1802. ht_cap->ampdu_factor)) - 1;
  1803. arg->peer_mpdu_density =
  1804. ath10k_parse_mpdudensity(ht_cap->ampdu_density);
  1805. arg->peer_ht_caps = ht_cap->cap;
  1806. arg->peer_rate_caps |= WMI_RC_HT_FLAG;
  1807. if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
  1808. arg->peer_flags |= ar->wmi.peer_flags->ldbc;
  1809. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  1810. arg->peer_flags |= ar->wmi.peer_flags->bw40;
  1811. arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
  1812. }
  1813. if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
  1814. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
  1815. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  1816. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
  1817. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  1818. }
  1819. if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
  1820. arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
  1821. arg->peer_flags |= ar->wmi.peer_flags->stbc;
  1822. }
  1823. if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
  1824. stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
  1825. stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
  1826. stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
  1827. arg->peer_rate_caps |= stbc;
  1828. arg->peer_flags |= ar->wmi.peer_flags->stbc;
  1829. }
  1830. if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
  1831. arg->peer_rate_caps |= WMI_RC_TS_FLAG;
  1832. else if (ht_cap->mcs.rx_mask[1])
  1833. arg->peer_rate_caps |= WMI_RC_DS_FLAG;
  1834. for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
  1835. if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
  1836. (ht_mcs_mask[i / 8] & BIT(i % 8))) {
  1837. max_nss = (i / 8) + 1;
  1838. arg->peer_ht_rates.rates[n++] = i;
  1839. }
  1840. /*
  1841. * This is a workaround for HT-enabled STAs which break the spec
  1842. * and have no HT capabilities RX mask (no HT RX MCS map).
  1843. *
  1844. * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
  1845. * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
  1846. *
  1847. * Firmware asserts if such situation occurs.
  1848. */
  1849. if (n == 0) {
  1850. arg->peer_ht_rates.num_rates = 8;
  1851. for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
  1852. arg->peer_ht_rates.rates[i] = i;
  1853. } else {
  1854. arg->peer_ht_rates.num_rates = n;
  1855. arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
  1856. }
  1857. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
  1858. arg->addr,
  1859. arg->peer_ht_rates.num_rates,
  1860. arg->peer_num_spatial_streams);
  1861. }
  1862. static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
  1863. struct ath10k_vif *arvif,
  1864. struct ieee80211_sta *sta)
  1865. {
  1866. u32 uapsd = 0;
  1867. u32 max_sp = 0;
  1868. int ret = 0;
  1869. lockdep_assert_held(&ar->conf_mutex);
  1870. if (sta->wme && sta->uapsd_queues) {
  1871. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
  1872. sta->uapsd_queues, sta->max_sp);
  1873. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1874. uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
  1875. WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
  1876. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1877. uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
  1878. WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
  1879. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1880. uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
  1881. WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
  1882. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1883. uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
  1884. WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
  1885. if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
  1886. max_sp = sta->max_sp;
  1887. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1888. sta->addr,
  1889. WMI_AP_PS_PEER_PARAM_UAPSD,
  1890. uapsd);
  1891. if (ret) {
  1892. ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
  1893. arvif->vdev_id, ret);
  1894. return ret;
  1895. }
  1896. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1897. sta->addr,
  1898. WMI_AP_PS_PEER_PARAM_MAX_SP,
  1899. max_sp);
  1900. if (ret) {
  1901. ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
  1902. arvif->vdev_id, ret);
  1903. return ret;
  1904. }
  1905. /* TODO setup this based on STA listen interval and
  1906. beacon interval. Currently we don't know
  1907. sta->listen_interval - mac80211 patch required.
  1908. Currently use 10 seconds */
  1909. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
  1910. WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
  1911. 10);
  1912. if (ret) {
  1913. ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
  1914. arvif->vdev_id, ret);
  1915. return ret;
  1916. }
  1917. }
  1918. return 0;
  1919. }
  1920. static u16
  1921. ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
  1922. const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
  1923. {
  1924. int idx_limit;
  1925. int nss;
  1926. u16 mcs_map;
  1927. u16 mcs;
  1928. for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
  1929. mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
  1930. vht_mcs_limit[nss];
  1931. if (mcs_map)
  1932. idx_limit = fls(mcs_map) - 1;
  1933. else
  1934. idx_limit = -1;
  1935. switch (idx_limit) {
  1936. case 0: /* fall through */
  1937. case 1: /* fall through */
  1938. case 2: /* fall through */
  1939. case 3: /* fall through */
  1940. case 4: /* fall through */
  1941. case 5: /* fall through */
  1942. case 6: /* fall through */
  1943. default:
  1944. /* see ath10k_mac_can_set_bitrate_mask() */
  1945. WARN_ON(1);
  1946. /* fall through */
  1947. case -1:
  1948. mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
  1949. break;
  1950. case 7:
  1951. mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
  1952. break;
  1953. case 8:
  1954. mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
  1955. break;
  1956. case 9:
  1957. mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
  1958. break;
  1959. }
  1960. tx_mcs_set &= ~(0x3 << (nss * 2));
  1961. tx_mcs_set |= mcs << (nss * 2);
  1962. }
  1963. return tx_mcs_set;
  1964. }
  1965. static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
  1966. struct ieee80211_vif *vif,
  1967. struct ieee80211_sta *sta,
  1968. struct wmi_peer_assoc_complete_arg *arg)
  1969. {
  1970. const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  1971. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  1972. struct cfg80211_chan_def def;
  1973. enum nl80211_band band;
  1974. const u16 *vht_mcs_mask;
  1975. u8 ampdu_factor;
  1976. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1977. return;
  1978. if (!vht_cap->vht_supported)
  1979. return;
  1980. band = def.chan->band;
  1981. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  1982. if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
  1983. return;
  1984. arg->peer_flags |= ar->wmi.peer_flags->vht;
  1985. if (def.chan->band == NL80211_BAND_2GHZ)
  1986. arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
  1987. arg->peer_vht_caps = vht_cap->cap;
  1988. ampdu_factor = (vht_cap->cap &
  1989. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
  1990. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  1991. /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
  1992. * zero in VHT IE. Using it would result in degraded throughput.
  1993. * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
  1994. * it if VHT max_mpdu is smaller. */
  1995. arg->peer_max_mpdu = max(arg->peer_max_mpdu,
  1996. (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1997. ampdu_factor)) - 1);
  1998. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1999. arg->peer_flags |= ar->wmi.peer_flags->bw80;
  2000. if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
  2001. arg->peer_flags |= ar->wmi.peer_flags->bw160;
  2002. arg->peer_vht_rates.rx_max_rate =
  2003. __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
  2004. arg->peer_vht_rates.rx_mcs_set =
  2005. __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  2006. arg->peer_vht_rates.tx_max_rate =
  2007. __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
  2008. arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
  2009. __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
  2010. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
  2011. sta->addr, arg->peer_max_mpdu, arg->peer_flags);
  2012. }
  2013. static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
  2014. struct ieee80211_vif *vif,
  2015. struct ieee80211_sta *sta,
  2016. struct wmi_peer_assoc_complete_arg *arg)
  2017. {
  2018. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2019. switch (arvif->vdev_type) {
  2020. case WMI_VDEV_TYPE_AP:
  2021. if (sta->wme)
  2022. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  2023. if (sta->wme && sta->uapsd_queues) {
  2024. arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
  2025. arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
  2026. }
  2027. break;
  2028. case WMI_VDEV_TYPE_STA:
  2029. if (vif->bss_conf.qos)
  2030. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  2031. break;
  2032. case WMI_VDEV_TYPE_IBSS:
  2033. if (sta->wme)
  2034. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  2035. break;
  2036. default:
  2037. break;
  2038. }
  2039. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
  2040. sta->addr, !!(arg->peer_flags &
  2041. arvif->ar->wmi.peer_flags->qos));
  2042. }
  2043. static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
  2044. {
  2045. return sta->supp_rates[NL80211_BAND_2GHZ] >>
  2046. ATH10K_MAC_FIRST_OFDM_RATE_IDX;
  2047. }
  2048. static enum wmi_phy_mode ath10k_mac_get_phymode_vht(struct ath10k *ar,
  2049. struct ieee80211_sta *sta)
  2050. {
  2051. if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
  2052. switch (sta->vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
  2053. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
  2054. return MODE_11AC_VHT160;
  2055. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
  2056. return MODE_11AC_VHT80_80;
  2057. default:
  2058. /* not sure if this is a valid case? */
  2059. return MODE_11AC_VHT160;
  2060. }
  2061. }
  2062. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  2063. return MODE_11AC_VHT80;
  2064. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  2065. return MODE_11AC_VHT40;
  2066. if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  2067. return MODE_11AC_VHT20;
  2068. return MODE_UNKNOWN;
  2069. }
  2070. static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
  2071. struct ieee80211_vif *vif,
  2072. struct ieee80211_sta *sta,
  2073. struct wmi_peer_assoc_complete_arg *arg)
  2074. {
  2075. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2076. struct cfg80211_chan_def def;
  2077. enum nl80211_band band;
  2078. const u8 *ht_mcs_mask;
  2079. const u16 *vht_mcs_mask;
  2080. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  2081. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  2082. return;
  2083. band = def.chan->band;
  2084. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  2085. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  2086. switch (band) {
  2087. case NL80211_BAND_2GHZ:
  2088. if (sta->vht_cap.vht_supported &&
  2089. !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
  2090. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  2091. phymode = MODE_11AC_VHT40;
  2092. else
  2093. phymode = MODE_11AC_VHT20;
  2094. } else if (sta->ht_cap.ht_supported &&
  2095. !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
  2096. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  2097. phymode = MODE_11NG_HT40;
  2098. else
  2099. phymode = MODE_11NG_HT20;
  2100. } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
  2101. phymode = MODE_11G;
  2102. } else {
  2103. phymode = MODE_11B;
  2104. }
  2105. break;
  2106. case NL80211_BAND_5GHZ:
  2107. /*
  2108. * Check VHT first.
  2109. */
  2110. if (sta->vht_cap.vht_supported &&
  2111. !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
  2112. phymode = ath10k_mac_get_phymode_vht(ar, sta);
  2113. } else if (sta->ht_cap.ht_supported &&
  2114. !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
  2115. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
  2116. phymode = MODE_11NA_HT40;
  2117. else
  2118. phymode = MODE_11NA_HT20;
  2119. } else {
  2120. phymode = MODE_11A;
  2121. }
  2122. break;
  2123. default:
  2124. break;
  2125. }
  2126. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
  2127. sta->addr, ath10k_wmi_phymode_str(phymode));
  2128. arg->peer_phymode = phymode;
  2129. WARN_ON(phymode == MODE_UNKNOWN);
  2130. }
  2131. static int ath10k_peer_assoc_prepare(struct ath10k *ar,
  2132. struct ieee80211_vif *vif,
  2133. struct ieee80211_sta *sta,
  2134. struct wmi_peer_assoc_complete_arg *arg)
  2135. {
  2136. lockdep_assert_held(&ar->conf_mutex);
  2137. memset(arg, 0, sizeof(*arg));
  2138. ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
  2139. ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
  2140. ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
  2141. ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
  2142. ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
  2143. ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
  2144. ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
  2145. return 0;
  2146. }
  2147. static const u32 ath10k_smps_map[] = {
  2148. [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
  2149. [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
  2150. [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
  2151. [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
  2152. };
  2153. static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
  2154. const u8 *addr,
  2155. const struct ieee80211_sta_ht_cap *ht_cap)
  2156. {
  2157. int smps;
  2158. if (!ht_cap->ht_supported)
  2159. return 0;
  2160. smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
  2161. smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
  2162. if (smps >= ARRAY_SIZE(ath10k_smps_map))
  2163. return -EINVAL;
  2164. return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
  2165. WMI_PEER_SMPS_STATE,
  2166. ath10k_smps_map[smps]);
  2167. }
  2168. static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
  2169. struct ieee80211_vif *vif,
  2170. struct ieee80211_sta_vht_cap vht_cap)
  2171. {
  2172. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2173. int ret;
  2174. u32 param;
  2175. u32 value;
  2176. if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
  2177. return 0;
  2178. if (!(ar->vht_cap_info &
  2179. (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  2180. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
  2181. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  2182. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
  2183. return 0;
  2184. param = ar->wmi.vdev_param->txbf;
  2185. value = 0;
  2186. if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
  2187. return 0;
  2188. /* The following logic is correct. If a remote STA advertises support
  2189. * for being a beamformer then we should enable us being a beamformee.
  2190. */
  2191. if (ar->vht_cap_info &
  2192. (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  2193. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
  2194. if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
  2195. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  2196. if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
  2197. value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
  2198. }
  2199. if (ar->vht_cap_info &
  2200. (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  2201. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
  2202. if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
  2203. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  2204. if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
  2205. value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
  2206. }
  2207. if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
  2208. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  2209. if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
  2210. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  2211. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
  2212. if (ret) {
  2213. ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
  2214. value, ret);
  2215. return ret;
  2216. }
  2217. return 0;
  2218. }
  2219. /* can be called only in mac80211 callbacks due to `key_count` usage */
  2220. static void ath10k_bss_assoc(struct ieee80211_hw *hw,
  2221. struct ieee80211_vif *vif,
  2222. struct ieee80211_bss_conf *bss_conf)
  2223. {
  2224. struct ath10k *ar = hw->priv;
  2225. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2226. struct ieee80211_sta_ht_cap ht_cap;
  2227. struct ieee80211_sta_vht_cap vht_cap;
  2228. struct wmi_peer_assoc_complete_arg peer_arg;
  2229. struct ieee80211_sta *ap_sta;
  2230. int ret;
  2231. lockdep_assert_held(&ar->conf_mutex);
  2232. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
  2233. arvif->vdev_id, arvif->bssid, arvif->aid);
  2234. rcu_read_lock();
  2235. ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
  2236. if (!ap_sta) {
  2237. ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
  2238. bss_conf->bssid, arvif->vdev_id);
  2239. rcu_read_unlock();
  2240. return;
  2241. }
  2242. /* ap_sta must be accessed only within rcu section which must be left
  2243. * before calling ath10k_setup_peer_smps() which might sleep. */
  2244. ht_cap = ap_sta->ht_cap;
  2245. vht_cap = ap_sta->vht_cap;
  2246. ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
  2247. if (ret) {
  2248. ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
  2249. bss_conf->bssid, arvif->vdev_id, ret);
  2250. rcu_read_unlock();
  2251. return;
  2252. }
  2253. rcu_read_unlock();
  2254. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  2255. if (ret) {
  2256. ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
  2257. bss_conf->bssid, arvif->vdev_id, ret);
  2258. return;
  2259. }
  2260. ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
  2261. if (ret) {
  2262. ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
  2263. arvif->vdev_id, ret);
  2264. return;
  2265. }
  2266. ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
  2267. if (ret) {
  2268. ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
  2269. arvif->vdev_id, bss_conf->bssid, ret);
  2270. return;
  2271. }
  2272. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2273. "mac vdev %d up (associated) bssid %pM aid %d\n",
  2274. arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
  2275. WARN_ON(arvif->is_up);
  2276. arvif->aid = bss_conf->aid;
  2277. ether_addr_copy(arvif->bssid, bss_conf->bssid);
  2278. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
  2279. if (ret) {
  2280. ath10k_warn(ar, "failed to set vdev %d up: %d\n",
  2281. arvif->vdev_id, ret);
  2282. return;
  2283. }
  2284. arvif->is_up = true;
  2285. /* Workaround: Some firmware revisions (tested with qca6174
  2286. * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
  2287. * poked with peer param command.
  2288. */
  2289. ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
  2290. WMI_PEER_DUMMY_VAR, 1);
  2291. if (ret) {
  2292. ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
  2293. arvif->bssid, arvif->vdev_id, ret);
  2294. return;
  2295. }
  2296. }
  2297. static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
  2298. struct ieee80211_vif *vif)
  2299. {
  2300. struct ath10k *ar = hw->priv;
  2301. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2302. struct ieee80211_sta_vht_cap vht_cap = {};
  2303. int ret;
  2304. lockdep_assert_held(&ar->conf_mutex);
  2305. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
  2306. arvif->vdev_id, arvif->bssid);
  2307. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  2308. if (ret)
  2309. ath10k_warn(ar, "failed to down vdev %i: %d\n",
  2310. arvif->vdev_id, ret);
  2311. arvif->def_wep_key_idx = -1;
  2312. ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
  2313. if (ret) {
  2314. ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
  2315. arvif->vdev_id, ret);
  2316. return;
  2317. }
  2318. arvif->is_up = false;
  2319. cancel_delayed_work_sync(&arvif->connection_loss_work);
  2320. }
  2321. static int ath10k_station_assoc(struct ath10k *ar,
  2322. struct ieee80211_vif *vif,
  2323. struct ieee80211_sta *sta,
  2324. bool reassoc)
  2325. {
  2326. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2327. struct wmi_peer_assoc_complete_arg peer_arg;
  2328. int ret = 0;
  2329. lockdep_assert_held(&ar->conf_mutex);
  2330. ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
  2331. if (ret) {
  2332. ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
  2333. sta->addr, arvif->vdev_id, ret);
  2334. return ret;
  2335. }
  2336. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  2337. if (ret) {
  2338. ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
  2339. sta->addr, arvif->vdev_id, ret);
  2340. return ret;
  2341. }
  2342. /* Re-assoc is run only to update supported rates for given station. It
  2343. * doesn't make much sense to reconfigure the peer completely.
  2344. */
  2345. if (!reassoc) {
  2346. ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
  2347. &sta->ht_cap);
  2348. if (ret) {
  2349. ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
  2350. arvif->vdev_id, ret);
  2351. return ret;
  2352. }
  2353. ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
  2354. if (ret) {
  2355. ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
  2356. sta->addr, arvif->vdev_id, ret);
  2357. return ret;
  2358. }
  2359. if (!sta->wme) {
  2360. arvif->num_legacy_stations++;
  2361. ret = ath10k_recalc_rtscts_prot(arvif);
  2362. if (ret) {
  2363. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  2364. arvif->vdev_id, ret);
  2365. return ret;
  2366. }
  2367. }
  2368. /* Plumb cached keys only for static WEP */
  2369. if (arvif->def_wep_key_idx != -1) {
  2370. ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
  2371. if (ret) {
  2372. ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
  2373. arvif->vdev_id, ret);
  2374. return ret;
  2375. }
  2376. }
  2377. }
  2378. return ret;
  2379. }
  2380. static int ath10k_station_disassoc(struct ath10k *ar,
  2381. struct ieee80211_vif *vif,
  2382. struct ieee80211_sta *sta)
  2383. {
  2384. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2385. int ret = 0;
  2386. lockdep_assert_held(&ar->conf_mutex);
  2387. if (!sta->wme) {
  2388. arvif->num_legacy_stations--;
  2389. ret = ath10k_recalc_rtscts_prot(arvif);
  2390. if (ret) {
  2391. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  2392. arvif->vdev_id, ret);
  2393. return ret;
  2394. }
  2395. }
  2396. ret = ath10k_clear_peer_keys(arvif, sta->addr);
  2397. if (ret) {
  2398. ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
  2399. arvif->vdev_id, ret);
  2400. return ret;
  2401. }
  2402. return ret;
  2403. }
  2404. /**************/
  2405. /* Regulatory */
  2406. /**************/
  2407. static int ath10k_update_channel_list(struct ath10k *ar)
  2408. {
  2409. struct ieee80211_hw *hw = ar->hw;
  2410. struct ieee80211_supported_band **bands;
  2411. enum nl80211_band band;
  2412. struct ieee80211_channel *channel;
  2413. struct wmi_scan_chan_list_arg arg = {0};
  2414. struct wmi_channel_arg *ch;
  2415. bool passive;
  2416. int len;
  2417. int ret;
  2418. int i;
  2419. lockdep_assert_held(&ar->conf_mutex);
  2420. bands = hw->wiphy->bands;
  2421. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2422. if (!bands[band])
  2423. continue;
  2424. for (i = 0; i < bands[band]->n_channels; i++) {
  2425. if (bands[band]->channels[i].flags &
  2426. IEEE80211_CHAN_DISABLED)
  2427. continue;
  2428. arg.n_channels++;
  2429. }
  2430. }
  2431. len = sizeof(struct wmi_channel_arg) * arg.n_channels;
  2432. arg.channels = kzalloc(len, GFP_KERNEL);
  2433. if (!arg.channels)
  2434. return -ENOMEM;
  2435. ch = arg.channels;
  2436. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2437. if (!bands[band])
  2438. continue;
  2439. for (i = 0; i < bands[band]->n_channels; i++) {
  2440. channel = &bands[band]->channels[i];
  2441. if (channel->flags & IEEE80211_CHAN_DISABLED)
  2442. continue;
  2443. ch->allow_ht = true;
  2444. /* FIXME: when should we really allow VHT? */
  2445. ch->allow_vht = true;
  2446. ch->allow_ibss =
  2447. !(channel->flags & IEEE80211_CHAN_NO_IR);
  2448. ch->ht40plus =
  2449. !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
  2450. ch->chan_radar =
  2451. !!(channel->flags & IEEE80211_CHAN_RADAR);
  2452. passive = channel->flags & IEEE80211_CHAN_NO_IR;
  2453. ch->passive = passive;
  2454. ch->freq = channel->center_freq;
  2455. ch->band_center_freq1 = channel->center_freq;
  2456. ch->min_power = 0;
  2457. ch->max_power = channel->max_power * 2;
  2458. ch->max_reg_power = channel->max_reg_power * 2;
  2459. ch->max_antenna_gain = channel->max_antenna_gain * 2;
  2460. ch->reg_class_id = 0; /* FIXME */
  2461. /* FIXME: why use only legacy modes, why not any
  2462. * HT/VHT modes? Would that even make any
  2463. * difference? */
  2464. if (channel->band == NL80211_BAND_2GHZ)
  2465. ch->mode = MODE_11G;
  2466. else
  2467. ch->mode = MODE_11A;
  2468. if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
  2469. continue;
  2470. ath10k_dbg(ar, ATH10K_DBG_WMI,
  2471. "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
  2472. ch - arg.channels, arg.n_channels,
  2473. ch->freq, ch->max_power, ch->max_reg_power,
  2474. ch->max_antenna_gain, ch->mode);
  2475. ch++;
  2476. }
  2477. }
  2478. ret = ath10k_wmi_scan_chan_list(ar, &arg);
  2479. kfree(arg.channels);
  2480. return ret;
  2481. }
  2482. static enum wmi_dfs_region
  2483. ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
  2484. {
  2485. switch (dfs_region) {
  2486. case NL80211_DFS_UNSET:
  2487. return WMI_UNINIT_DFS_DOMAIN;
  2488. case NL80211_DFS_FCC:
  2489. return WMI_FCC_DFS_DOMAIN;
  2490. case NL80211_DFS_ETSI:
  2491. return WMI_ETSI_DFS_DOMAIN;
  2492. case NL80211_DFS_JP:
  2493. return WMI_MKK4_DFS_DOMAIN;
  2494. }
  2495. return WMI_UNINIT_DFS_DOMAIN;
  2496. }
  2497. static void ath10k_regd_update(struct ath10k *ar)
  2498. {
  2499. struct reg_dmn_pair_mapping *regpair;
  2500. int ret;
  2501. enum wmi_dfs_region wmi_dfs_reg;
  2502. enum nl80211_dfs_regions nl_dfs_reg;
  2503. lockdep_assert_held(&ar->conf_mutex);
  2504. ret = ath10k_update_channel_list(ar);
  2505. if (ret)
  2506. ath10k_warn(ar, "failed to update channel list: %d\n", ret);
  2507. regpair = ar->ath_common.regulatory.regpair;
  2508. if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  2509. nl_dfs_reg = ar->dfs_detector->region;
  2510. wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
  2511. } else {
  2512. wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
  2513. }
  2514. /* Target allows setting up per-band regdomain but ath_common provides
  2515. * a combined one only */
  2516. ret = ath10k_wmi_pdev_set_regdomain(ar,
  2517. regpair->reg_domain,
  2518. regpair->reg_domain, /* 2ghz */
  2519. regpair->reg_domain, /* 5ghz */
  2520. regpair->reg_2ghz_ctl,
  2521. regpair->reg_5ghz_ctl,
  2522. wmi_dfs_reg);
  2523. if (ret)
  2524. ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
  2525. }
  2526. static void ath10k_reg_notifier(struct wiphy *wiphy,
  2527. struct regulatory_request *request)
  2528. {
  2529. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  2530. struct ath10k *ar = hw->priv;
  2531. bool result;
  2532. ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
  2533. if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  2534. ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
  2535. request->dfs_region);
  2536. result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
  2537. request->dfs_region);
  2538. if (!result)
  2539. ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
  2540. request->dfs_region);
  2541. }
  2542. mutex_lock(&ar->conf_mutex);
  2543. if (ar->state == ATH10K_STATE_ON)
  2544. ath10k_regd_update(ar);
  2545. mutex_unlock(&ar->conf_mutex);
  2546. }
  2547. /***************/
  2548. /* TX handlers */
  2549. /***************/
  2550. enum ath10k_mac_tx_path {
  2551. ATH10K_MAC_TX_HTT,
  2552. ATH10K_MAC_TX_HTT_MGMT,
  2553. ATH10K_MAC_TX_WMI_MGMT,
  2554. ATH10K_MAC_TX_UNKNOWN,
  2555. };
  2556. void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
  2557. {
  2558. lockdep_assert_held(&ar->htt.tx_lock);
  2559. WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
  2560. ar->tx_paused |= BIT(reason);
  2561. ieee80211_stop_queues(ar->hw);
  2562. }
  2563. static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
  2564. struct ieee80211_vif *vif)
  2565. {
  2566. struct ath10k *ar = data;
  2567. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2568. if (arvif->tx_paused)
  2569. return;
  2570. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  2571. }
  2572. void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
  2573. {
  2574. lockdep_assert_held(&ar->htt.tx_lock);
  2575. WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
  2576. ar->tx_paused &= ~BIT(reason);
  2577. if (ar->tx_paused)
  2578. return;
  2579. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  2580. IEEE80211_IFACE_ITER_RESUME_ALL,
  2581. ath10k_mac_tx_unlock_iter,
  2582. ar);
  2583. ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
  2584. }
  2585. void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
  2586. {
  2587. struct ath10k *ar = arvif->ar;
  2588. lockdep_assert_held(&ar->htt.tx_lock);
  2589. WARN_ON(reason >= BITS_PER_LONG);
  2590. arvif->tx_paused |= BIT(reason);
  2591. ieee80211_stop_queue(ar->hw, arvif->vdev_id);
  2592. }
  2593. void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
  2594. {
  2595. struct ath10k *ar = arvif->ar;
  2596. lockdep_assert_held(&ar->htt.tx_lock);
  2597. WARN_ON(reason >= BITS_PER_LONG);
  2598. arvif->tx_paused &= ~BIT(reason);
  2599. if (ar->tx_paused)
  2600. return;
  2601. if (arvif->tx_paused)
  2602. return;
  2603. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  2604. }
  2605. static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
  2606. enum wmi_tlv_tx_pause_id pause_id,
  2607. enum wmi_tlv_tx_pause_action action)
  2608. {
  2609. struct ath10k *ar = arvif->ar;
  2610. lockdep_assert_held(&ar->htt.tx_lock);
  2611. switch (action) {
  2612. case WMI_TLV_TX_PAUSE_ACTION_STOP:
  2613. ath10k_mac_vif_tx_lock(arvif, pause_id);
  2614. break;
  2615. case WMI_TLV_TX_PAUSE_ACTION_WAKE:
  2616. ath10k_mac_vif_tx_unlock(arvif, pause_id);
  2617. break;
  2618. default:
  2619. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  2620. "received unknown tx pause action %d on vdev %i, ignoring\n",
  2621. action, arvif->vdev_id);
  2622. break;
  2623. }
  2624. }
  2625. struct ath10k_mac_tx_pause {
  2626. u32 vdev_id;
  2627. enum wmi_tlv_tx_pause_id pause_id;
  2628. enum wmi_tlv_tx_pause_action action;
  2629. };
  2630. static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
  2631. struct ieee80211_vif *vif)
  2632. {
  2633. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2634. struct ath10k_mac_tx_pause *arg = data;
  2635. if (arvif->vdev_id != arg->vdev_id)
  2636. return;
  2637. ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
  2638. }
  2639. void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
  2640. enum wmi_tlv_tx_pause_id pause_id,
  2641. enum wmi_tlv_tx_pause_action action)
  2642. {
  2643. struct ath10k_mac_tx_pause arg = {
  2644. .vdev_id = vdev_id,
  2645. .pause_id = pause_id,
  2646. .action = action,
  2647. };
  2648. spin_lock_bh(&ar->htt.tx_lock);
  2649. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  2650. IEEE80211_IFACE_ITER_RESUME_ALL,
  2651. ath10k_mac_handle_tx_pause_iter,
  2652. &arg);
  2653. spin_unlock_bh(&ar->htt.tx_lock);
  2654. }
  2655. static enum ath10k_hw_txrx_mode
  2656. ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
  2657. struct ieee80211_vif *vif,
  2658. struct ieee80211_sta *sta,
  2659. struct sk_buff *skb)
  2660. {
  2661. const struct ieee80211_hdr *hdr = (void *)skb->data;
  2662. __le16 fc = hdr->frame_control;
  2663. if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
  2664. return ATH10K_HW_TXRX_RAW;
  2665. if (ieee80211_is_mgmt(fc))
  2666. return ATH10K_HW_TXRX_MGMT;
  2667. /* Workaround:
  2668. *
  2669. * NullFunc frames are mostly used to ping if a client or AP are still
  2670. * reachable and responsive. This implies tx status reports must be
  2671. * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
  2672. * come to a conclusion that the other end disappeared and tear down
  2673. * BSS connection or it can never disconnect from BSS/client (which is
  2674. * the case).
  2675. *
  2676. * Firmware with HTT older than 3.0 delivers incorrect tx status for
  2677. * NullFunc frames to driver. However there's a HTT Mgmt Tx command
  2678. * which seems to deliver correct tx reports for NullFunc frames. The
  2679. * downside of using it is it ignores client powersave state so it can
  2680. * end up disconnecting sleeping clients in AP mode. It should fix STA
  2681. * mode though because AP don't sleep.
  2682. */
  2683. if (ar->htt.target_version_major < 3 &&
  2684. (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
  2685. !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  2686. ar->running_fw->fw_file.fw_features))
  2687. return ATH10K_HW_TXRX_MGMT;
  2688. /* Workaround:
  2689. *
  2690. * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
  2691. * NativeWifi txmode - it selects AP key instead of peer key. It seems
  2692. * to work with Ethernet txmode so use it.
  2693. *
  2694. * FIXME: Check if raw mode works with TDLS.
  2695. */
  2696. if (ieee80211_is_data_present(fc) && sta && sta->tdls)
  2697. return ATH10K_HW_TXRX_ETHERNET;
  2698. if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  2699. return ATH10K_HW_TXRX_RAW;
  2700. return ATH10K_HW_TXRX_NATIVE_WIFI;
  2701. }
  2702. static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
  2703. struct sk_buff *skb)
  2704. {
  2705. const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2706. const struct ieee80211_hdr *hdr = (void *)skb->data;
  2707. const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
  2708. IEEE80211_TX_CTL_INJECTED;
  2709. if (!ieee80211_has_protected(hdr->frame_control))
  2710. return false;
  2711. if ((info->flags & mask) == mask)
  2712. return false;
  2713. if (vif)
  2714. return !((struct ath10k_vif *)vif->drv_priv)->nohwcrypt;
  2715. return true;
  2716. }
  2717. /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
  2718. * Control in the header.
  2719. */
  2720. static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
  2721. {
  2722. struct ieee80211_hdr *hdr = (void *)skb->data;
  2723. struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
  2724. u8 *qos_ctl;
  2725. if (!ieee80211_is_data_qos(hdr->frame_control))
  2726. return;
  2727. qos_ctl = ieee80211_get_qos_ctl(hdr);
  2728. memmove(skb->data + IEEE80211_QOS_CTL_LEN,
  2729. skb->data, (void *)qos_ctl - (void *)skb->data);
  2730. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  2731. /* Some firmware revisions don't handle sending QoS NullFunc well.
  2732. * These frames are mainly used for CQM purposes so it doesn't really
  2733. * matter whether QoS NullFunc or NullFunc are sent.
  2734. */
  2735. hdr = (void *)skb->data;
  2736. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  2737. cb->flags &= ~ATH10K_SKB_F_QOS;
  2738. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2739. }
  2740. static void ath10k_tx_h_8023(struct sk_buff *skb)
  2741. {
  2742. struct ieee80211_hdr *hdr;
  2743. struct rfc1042_hdr *rfc1042;
  2744. struct ethhdr *eth;
  2745. size_t hdrlen;
  2746. u8 da[ETH_ALEN];
  2747. u8 sa[ETH_ALEN];
  2748. __be16 type;
  2749. hdr = (void *)skb->data;
  2750. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  2751. rfc1042 = (void *)skb->data + hdrlen;
  2752. ether_addr_copy(da, ieee80211_get_DA(hdr));
  2753. ether_addr_copy(sa, ieee80211_get_SA(hdr));
  2754. type = rfc1042->snap_type;
  2755. skb_pull(skb, hdrlen + sizeof(*rfc1042));
  2756. skb_push(skb, sizeof(*eth));
  2757. eth = (void *)skb->data;
  2758. ether_addr_copy(eth->h_dest, da);
  2759. ether_addr_copy(eth->h_source, sa);
  2760. eth->h_proto = type;
  2761. }
  2762. static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
  2763. struct ieee80211_vif *vif,
  2764. struct sk_buff *skb)
  2765. {
  2766. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2767. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  2768. /* This is case only for P2P_GO */
  2769. if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
  2770. return;
  2771. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
  2772. spin_lock_bh(&ar->data_lock);
  2773. if (arvif->u.ap.noa_data)
  2774. if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
  2775. GFP_ATOMIC))
  2776. memcpy(skb_put(skb, arvif->u.ap.noa_len),
  2777. arvif->u.ap.noa_data,
  2778. arvif->u.ap.noa_len);
  2779. spin_unlock_bh(&ar->data_lock);
  2780. }
  2781. }
  2782. static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
  2783. struct ieee80211_vif *vif,
  2784. struct ieee80211_txq *txq,
  2785. struct sk_buff *skb)
  2786. {
  2787. struct ieee80211_hdr *hdr = (void *)skb->data;
  2788. struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
  2789. cb->flags = 0;
  2790. if (!ath10k_tx_h_use_hwcrypto(vif, skb))
  2791. cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
  2792. if (ieee80211_is_mgmt(hdr->frame_control))
  2793. cb->flags |= ATH10K_SKB_F_MGMT;
  2794. if (ieee80211_is_data_qos(hdr->frame_control))
  2795. cb->flags |= ATH10K_SKB_F_QOS;
  2796. cb->vif = vif;
  2797. cb->txq = txq;
  2798. }
  2799. bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
  2800. {
  2801. /* FIXME: Not really sure since when the behaviour changed. At some
  2802. * point new firmware stopped requiring creation of peer entries for
  2803. * offchannel tx (and actually creating them causes issues with wmi-htc
  2804. * tx credit replenishment and reliability). Assuming it's at least 3.4
  2805. * because that's when the `freq` was introduced to TX_FRM HTT command.
  2806. */
  2807. return (ar->htt.target_version_major >= 3 &&
  2808. ar->htt.target_version_minor >= 4 &&
  2809. ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
  2810. }
  2811. static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
  2812. {
  2813. struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
  2814. int ret = 0;
  2815. spin_lock_bh(&ar->data_lock);
  2816. if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
  2817. ath10k_warn(ar, "wmi mgmt tx queue is full\n");
  2818. ret = -ENOSPC;
  2819. goto unlock;
  2820. }
  2821. __skb_queue_tail(q, skb);
  2822. ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
  2823. unlock:
  2824. spin_unlock_bh(&ar->data_lock);
  2825. return ret;
  2826. }
  2827. static enum ath10k_mac_tx_path
  2828. ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
  2829. struct sk_buff *skb,
  2830. enum ath10k_hw_txrx_mode txmode)
  2831. {
  2832. switch (txmode) {
  2833. case ATH10K_HW_TXRX_RAW:
  2834. case ATH10K_HW_TXRX_NATIVE_WIFI:
  2835. case ATH10K_HW_TXRX_ETHERNET:
  2836. return ATH10K_MAC_TX_HTT;
  2837. case ATH10K_HW_TXRX_MGMT:
  2838. if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  2839. ar->running_fw->fw_file.fw_features))
  2840. return ATH10K_MAC_TX_WMI_MGMT;
  2841. else if (ar->htt.target_version_major >= 3)
  2842. return ATH10K_MAC_TX_HTT;
  2843. else
  2844. return ATH10K_MAC_TX_HTT_MGMT;
  2845. }
  2846. return ATH10K_MAC_TX_UNKNOWN;
  2847. }
  2848. static int ath10k_mac_tx_submit(struct ath10k *ar,
  2849. enum ath10k_hw_txrx_mode txmode,
  2850. enum ath10k_mac_tx_path txpath,
  2851. struct sk_buff *skb)
  2852. {
  2853. struct ath10k_htt *htt = &ar->htt;
  2854. int ret = -EINVAL;
  2855. switch (txpath) {
  2856. case ATH10K_MAC_TX_HTT:
  2857. ret = ath10k_htt_tx(htt, txmode, skb);
  2858. break;
  2859. case ATH10K_MAC_TX_HTT_MGMT:
  2860. ret = ath10k_htt_mgmt_tx(htt, skb);
  2861. break;
  2862. case ATH10K_MAC_TX_WMI_MGMT:
  2863. ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
  2864. break;
  2865. case ATH10K_MAC_TX_UNKNOWN:
  2866. WARN_ON_ONCE(1);
  2867. ret = -EINVAL;
  2868. break;
  2869. }
  2870. if (ret) {
  2871. ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
  2872. ret);
  2873. ieee80211_free_txskb(ar->hw, skb);
  2874. }
  2875. return ret;
  2876. }
  2877. /* This function consumes the sk_buff regardless of return value as far as
  2878. * caller is concerned so no freeing is necessary afterwards.
  2879. */
  2880. static int ath10k_mac_tx(struct ath10k *ar,
  2881. struct ieee80211_vif *vif,
  2882. enum ath10k_hw_txrx_mode txmode,
  2883. enum ath10k_mac_tx_path txpath,
  2884. struct sk_buff *skb)
  2885. {
  2886. struct ieee80211_hw *hw = ar->hw;
  2887. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2888. int ret;
  2889. /* We should disable CCK RATE due to P2P */
  2890. if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  2891. ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
  2892. switch (txmode) {
  2893. case ATH10K_HW_TXRX_MGMT:
  2894. case ATH10K_HW_TXRX_NATIVE_WIFI:
  2895. ath10k_tx_h_nwifi(hw, skb);
  2896. ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
  2897. ath10k_tx_h_seq_no(vif, skb);
  2898. break;
  2899. case ATH10K_HW_TXRX_ETHERNET:
  2900. ath10k_tx_h_8023(skb);
  2901. break;
  2902. case ATH10K_HW_TXRX_RAW:
  2903. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  2904. WARN_ON_ONCE(1);
  2905. ieee80211_free_txskb(hw, skb);
  2906. return -ENOTSUPP;
  2907. }
  2908. }
  2909. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
  2910. if (!ath10k_mac_tx_frm_has_freq(ar)) {
  2911. ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %pK\n",
  2912. skb);
  2913. skb_queue_tail(&ar->offchan_tx_queue, skb);
  2914. ieee80211_queue_work(hw, &ar->offchan_tx_work);
  2915. return 0;
  2916. }
  2917. }
  2918. ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
  2919. if (ret) {
  2920. ath10k_warn(ar, "failed to submit frame: %d\n", ret);
  2921. return ret;
  2922. }
  2923. return 0;
  2924. }
  2925. void ath10k_offchan_tx_purge(struct ath10k *ar)
  2926. {
  2927. struct sk_buff *skb;
  2928. for (;;) {
  2929. skb = skb_dequeue(&ar->offchan_tx_queue);
  2930. if (!skb)
  2931. break;
  2932. ieee80211_free_txskb(ar->hw, skb);
  2933. }
  2934. }
  2935. void ath10k_offchan_tx_work(struct work_struct *work)
  2936. {
  2937. struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
  2938. struct ath10k_peer *peer;
  2939. struct ath10k_vif *arvif;
  2940. enum ath10k_hw_txrx_mode txmode;
  2941. enum ath10k_mac_tx_path txpath;
  2942. struct ieee80211_hdr *hdr;
  2943. struct ieee80211_vif *vif;
  2944. struct ieee80211_sta *sta;
  2945. struct sk_buff *skb;
  2946. const u8 *peer_addr;
  2947. int vdev_id;
  2948. int ret;
  2949. unsigned long time_left;
  2950. bool tmp_peer_created = false;
  2951. /* FW requirement: We must create a peer before FW will send out
  2952. * an offchannel frame. Otherwise the frame will be stuck and
  2953. * never transmitted. We delete the peer upon tx completion.
  2954. * It is unlikely that a peer for offchannel tx will already be
  2955. * present. However it may be in some rare cases so account for that.
  2956. * Otherwise we might remove a legitimate peer and break stuff. */
  2957. for (;;) {
  2958. skb = skb_dequeue(&ar->offchan_tx_queue);
  2959. if (!skb)
  2960. break;
  2961. mutex_lock(&ar->conf_mutex);
  2962. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK\n",
  2963. skb);
  2964. hdr = (struct ieee80211_hdr *)skb->data;
  2965. peer_addr = ieee80211_get_DA(hdr);
  2966. spin_lock_bh(&ar->data_lock);
  2967. vdev_id = ar->scan.vdev_id;
  2968. peer = ath10k_peer_find(ar, vdev_id, peer_addr);
  2969. spin_unlock_bh(&ar->data_lock);
  2970. if (peer)
  2971. /* FIXME: should this use ath10k_warn()? */
  2972. ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
  2973. peer_addr, vdev_id);
  2974. if (!peer) {
  2975. ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
  2976. peer_addr,
  2977. WMI_PEER_TYPE_DEFAULT);
  2978. if (ret)
  2979. ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
  2980. peer_addr, vdev_id, ret);
  2981. tmp_peer_created = (ret == 0);
  2982. }
  2983. spin_lock_bh(&ar->data_lock);
  2984. reinit_completion(&ar->offchan_tx_completed);
  2985. ar->offchan_tx_skb = skb;
  2986. spin_unlock_bh(&ar->data_lock);
  2987. /* It's safe to access vif and sta - conf_mutex guarantees that
  2988. * sta_state() and remove_interface() are locked exclusively
  2989. * out wrt to this offchannel worker.
  2990. */
  2991. arvif = ath10k_get_arvif(ar, vdev_id);
  2992. if (arvif) {
  2993. vif = arvif->vif;
  2994. sta = ieee80211_find_sta(vif, peer_addr);
  2995. } else {
  2996. vif = NULL;
  2997. sta = NULL;
  2998. }
  2999. txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
  3000. txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
  3001. ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb);
  3002. if (ret) {
  3003. ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
  3004. ret);
  3005. /* not serious */
  3006. }
  3007. time_left =
  3008. wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
  3009. if (time_left == 0)
  3010. ath10k_warn(ar, "timed out waiting for offchannel skb %pK\n",
  3011. skb);
  3012. if (!peer && tmp_peer_created) {
  3013. ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
  3014. if (ret)
  3015. ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
  3016. peer_addr, vdev_id, ret);
  3017. }
  3018. mutex_unlock(&ar->conf_mutex);
  3019. }
  3020. }
  3021. void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
  3022. {
  3023. struct sk_buff *skb;
  3024. for (;;) {
  3025. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  3026. if (!skb)
  3027. break;
  3028. ieee80211_free_txskb(ar->hw, skb);
  3029. }
  3030. }
  3031. void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
  3032. {
  3033. struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
  3034. struct sk_buff *skb;
  3035. int ret;
  3036. for (;;) {
  3037. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  3038. if (!skb)
  3039. break;
  3040. ret = ath10k_wmi_mgmt_tx(ar, skb);
  3041. if (ret) {
  3042. ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
  3043. ret);
  3044. ieee80211_free_txskb(ar->hw, skb);
  3045. }
  3046. }
  3047. }
  3048. static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
  3049. {
  3050. struct ath10k_txq *artxq;
  3051. if (!txq)
  3052. return;
  3053. artxq = (void *)txq->drv_priv;
  3054. INIT_LIST_HEAD(&artxq->list);
  3055. }
  3056. static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
  3057. {
  3058. struct ath10k_txq *artxq;
  3059. struct ath10k_skb_cb *cb;
  3060. struct sk_buff *msdu;
  3061. int msdu_id;
  3062. if (!txq)
  3063. return;
  3064. artxq = (void *)txq->drv_priv;
  3065. spin_lock_bh(&ar->txqs_lock);
  3066. if (!list_empty(&artxq->list))
  3067. list_del_init(&artxq->list);
  3068. spin_unlock_bh(&ar->txqs_lock);
  3069. spin_lock_bh(&ar->htt.tx_lock);
  3070. idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
  3071. cb = ATH10K_SKB_CB(msdu);
  3072. if (cb->txq == txq)
  3073. cb->txq = NULL;
  3074. }
  3075. spin_unlock_bh(&ar->htt.tx_lock);
  3076. }
  3077. struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
  3078. u16 peer_id,
  3079. u8 tid)
  3080. {
  3081. struct ath10k_peer *peer;
  3082. lockdep_assert_held(&ar->data_lock);
  3083. peer = ar->peer_map[peer_id];
  3084. if (!peer)
  3085. return NULL;
  3086. if (peer->removed)
  3087. return NULL;
  3088. if (peer->sta)
  3089. return peer->sta->txq[tid];
  3090. else if (peer->vif)
  3091. return peer->vif->txq;
  3092. else
  3093. return NULL;
  3094. }
  3095. static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
  3096. struct ieee80211_txq *txq)
  3097. {
  3098. struct ath10k *ar = hw->priv;
  3099. struct ath10k_txq *artxq = (void *)txq->drv_priv;
  3100. /* No need to get locks */
  3101. if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
  3102. return true;
  3103. if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
  3104. return true;
  3105. if (artxq->num_fw_queued < artxq->num_push_allowed)
  3106. return true;
  3107. return false;
  3108. }
  3109. int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
  3110. struct ieee80211_txq *txq)
  3111. {
  3112. struct ath10k *ar = hw->priv;
  3113. struct ath10k_htt *htt = &ar->htt;
  3114. struct ath10k_txq *artxq = (void *)txq->drv_priv;
  3115. struct ieee80211_vif *vif = txq->vif;
  3116. struct ieee80211_sta *sta = txq->sta;
  3117. enum ath10k_hw_txrx_mode txmode;
  3118. enum ath10k_mac_tx_path txpath;
  3119. struct sk_buff *skb;
  3120. struct ieee80211_hdr *hdr;
  3121. size_t skb_len;
  3122. bool is_mgmt, is_presp;
  3123. int ret;
  3124. spin_lock_bh(&ar->htt.tx_lock);
  3125. ret = ath10k_htt_tx_inc_pending(htt);
  3126. spin_unlock_bh(&ar->htt.tx_lock);
  3127. if (ret)
  3128. return ret;
  3129. skb = ieee80211_tx_dequeue(hw, txq);
  3130. if (!skb) {
  3131. spin_lock_bh(&ar->htt.tx_lock);
  3132. ath10k_htt_tx_dec_pending(htt);
  3133. spin_unlock_bh(&ar->htt.tx_lock);
  3134. return -ENOENT;
  3135. }
  3136. ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
  3137. skb_len = skb->len;
  3138. txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
  3139. txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
  3140. is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
  3141. if (is_mgmt) {
  3142. hdr = (struct ieee80211_hdr *)skb->data;
  3143. is_presp = ieee80211_is_probe_resp(hdr->frame_control);
  3144. spin_lock_bh(&ar->htt.tx_lock);
  3145. ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
  3146. if (ret) {
  3147. ath10k_htt_tx_dec_pending(htt);
  3148. spin_unlock_bh(&ar->htt.tx_lock);
  3149. return ret;
  3150. }
  3151. spin_unlock_bh(&ar->htt.tx_lock);
  3152. }
  3153. ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb);
  3154. if (unlikely(ret)) {
  3155. ath10k_warn(ar, "failed to push frame: %d\n", ret);
  3156. spin_lock_bh(&ar->htt.tx_lock);
  3157. ath10k_htt_tx_dec_pending(htt);
  3158. if (is_mgmt)
  3159. ath10k_htt_tx_mgmt_dec_pending(htt);
  3160. spin_unlock_bh(&ar->htt.tx_lock);
  3161. return ret;
  3162. }
  3163. spin_lock_bh(&ar->htt.tx_lock);
  3164. artxq->num_fw_queued++;
  3165. spin_unlock_bh(&ar->htt.tx_lock);
  3166. return skb_len;
  3167. }
  3168. void ath10k_mac_tx_push_pending(struct ath10k *ar)
  3169. {
  3170. struct ieee80211_hw *hw = ar->hw;
  3171. struct ieee80211_txq *txq;
  3172. struct ath10k_txq *artxq;
  3173. struct ath10k_txq *last;
  3174. int ret;
  3175. int max;
  3176. if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
  3177. return;
  3178. spin_lock_bh(&ar->txqs_lock);
  3179. rcu_read_lock();
  3180. last = list_last_entry(&ar->txqs, struct ath10k_txq, list);
  3181. while (!list_empty(&ar->txqs)) {
  3182. artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
  3183. txq = container_of((void *)artxq, struct ieee80211_txq,
  3184. drv_priv);
  3185. /* Prevent aggressive sta/tid taking over tx queue */
  3186. max = 16;
  3187. ret = 0;
  3188. while (ath10k_mac_tx_can_push(hw, txq) && max--) {
  3189. ret = ath10k_mac_tx_push_txq(hw, txq);
  3190. if (ret < 0)
  3191. break;
  3192. }
  3193. list_del_init(&artxq->list);
  3194. if (ret != -ENOENT)
  3195. list_add_tail(&artxq->list, &ar->txqs);
  3196. ath10k_htt_tx_txq_update(hw, txq);
  3197. if (artxq == last || (ret < 0 && ret != -ENOENT))
  3198. break;
  3199. }
  3200. rcu_read_unlock();
  3201. spin_unlock_bh(&ar->txqs_lock);
  3202. }
  3203. /************/
  3204. /* Scanning */
  3205. /************/
  3206. void __ath10k_scan_finish(struct ath10k *ar)
  3207. {
  3208. lockdep_assert_held(&ar->data_lock);
  3209. switch (ar->scan.state) {
  3210. case ATH10K_SCAN_IDLE:
  3211. break;
  3212. case ATH10K_SCAN_RUNNING:
  3213. case ATH10K_SCAN_ABORTING:
  3214. if (!ar->scan.is_roc) {
  3215. struct cfg80211_scan_info info = {
  3216. .aborted = (ar->scan.state ==
  3217. ATH10K_SCAN_ABORTING),
  3218. };
  3219. ieee80211_scan_completed(ar->hw, &info);
  3220. } else if (ar->scan.roc_notify) {
  3221. ieee80211_remain_on_channel_expired(ar->hw);
  3222. }
  3223. /* fall through */
  3224. case ATH10K_SCAN_STARTING:
  3225. ar->scan.state = ATH10K_SCAN_IDLE;
  3226. ar->scan_channel = NULL;
  3227. ar->scan.roc_freq = 0;
  3228. ath10k_offchan_tx_purge(ar);
  3229. cancel_delayed_work(&ar->scan.timeout);
  3230. complete(&ar->scan.completed);
  3231. break;
  3232. }
  3233. }
  3234. void ath10k_scan_finish(struct ath10k *ar)
  3235. {
  3236. spin_lock_bh(&ar->data_lock);
  3237. __ath10k_scan_finish(ar);
  3238. spin_unlock_bh(&ar->data_lock);
  3239. }
  3240. static int ath10k_scan_stop(struct ath10k *ar)
  3241. {
  3242. struct wmi_stop_scan_arg arg = {
  3243. .req_id = 1, /* FIXME */
  3244. .req_type = WMI_SCAN_STOP_ONE,
  3245. .u.scan_id = ATH10K_SCAN_ID,
  3246. };
  3247. int ret;
  3248. lockdep_assert_held(&ar->conf_mutex);
  3249. ret = ath10k_wmi_stop_scan(ar, &arg);
  3250. if (ret) {
  3251. ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
  3252. goto out;
  3253. }
  3254. ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
  3255. if (ret == 0) {
  3256. ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
  3257. ret = -ETIMEDOUT;
  3258. } else if (ret > 0) {
  3259. ret = 0;
  3260. }
  3261. out:
  3262. /* Scan state should be updated upon scan completion but in case
  3263. * firmware fails to deliver the event (for whatever reason) it is
  3264. * desired to clean up scan state anyway. Firmware may have just
  3265. * dropped the scan completion event delivery due to transport pipe
  3266. * being overflown with data and/or it can recover on its own before
  3267. * next scan request is submitted.
  3268. */
  3269. spin_lock_bh(&ar->data_lock);
  3270. if (ar->scan.state != ATH10K_SCAN_IDLE)
  3271. __ath10k_scan_finish(ar);
  3272. spin_unlock_bh(&ar->data_lock);
  3273. return ret;
  3274. }
  3275. static void ath10k_scan_abort(struct ath10k *ar)
  3276. {
  3277. int ret;
  3278. lockdep_assert_held(&ar->conf_mutex);
  3279. spin_lock_bh(&ar->data_lock);
  3280. switch (ar->scan.state) {
  3281. case ATH10K_SCAN_IDLE:
  3282. /* This can happen if timeout worker kicked in and called
  3283. * abortion while scan completion was being processed.
  3284. */
  3285. break;
  3286. case ATH10K_SCAN_STARTING:
  3287. case ATH10K_SCAN_ABORTING:
  3288. ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
  3289. ath10k_scan_state_str(ar->scan.state),
  3290. ar->scan.state);
  3291. break;
  3292. case ATH10K_SCAN_RUNNING:
  3293. ar->scan.state = ATH10K_SCAN_ABORTING;
  3294. spin_unlock_bh(&ar->data_lock);
  3295. ret = ath10k_scan_stop(ar);
  3296. if (ret)
  3297. ath10k_warn(ar, "failed to abort scan: %d\n", ret);
  3298. spin_lock_bh(&ar->data_lock);
  3299. break;
  3300. }
  3301. spin_unlock_bh(&ar->data_lock);
  3302. }
  3303. void ath10k_scan_timeout_work(struct work_struct *work)
  3304. {
  3305. struct ath10k *ar = container_of(work, struct ath10k,
  3306. scan.timeout.work);
  3307. mutex_lock(&ar->conf_mutex);
  3308. ath10k_scan_abort(ar);
  3309. mutex_unlock(&ar->conf_mutex);
  3310. }
  3311. static int ath10k_start_scan(struct ath10k *ar,
  3312. const struct wmi_start_scan_arg *arg)
  3313. {
  3314. int ret;
  3315. lockdep_assert_held(&ar->conf_mutex);
  3316. ret = ath10k_wmi_start_scan(ar, arg);
  3317. if (ret)
  3318. return ret;
  3319. ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
  3320. if (ret == 0) {
  3321. ret = ath10k_scan_stop(ar);
  3322. if (ret)
  3323. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  3324. return -ETIMEDOUT;
  3325. }
  3326. /* If we failed to start the scan, return error code at
  3327. * this point. This is probably due to some issue in the
  3328. * firmware, but no need to wedge the driver due to that...
  3329. */
  3330. spin_lock_bh(&ar->data_lock);
  3331. if (ar->scan.state == ATH10K_SCAN_IDLE) {
  3332. spin_unlock_bh(&ar->data_lock);
  3333. return -EINVAL;
  3334. }
  3335. spin_unlock_bh(&ar->data_lock);
  3336. return 0;
  3337. }
  3338. /**********************/
  3339. /* mac80211 callbacks */
  3340. /**********************/
  3341. static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
  3342. struct ieee80211_tx_control *control,
  3343. struct sk_buff *skb)
  3344. {
  3345. struct ath10k *ar = hw->priv;
  3346. struct ath10k_htt *htt = &ar->htt;
  3347. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  3348. struct ieee80211_vif *vif = info->control.vif;
  3349. struct ieee80211_sta *sta = control->sta;
  3350. struct ieee80211_txq *txq = NULL;
  3351. struct ieee80211_hdr *hdr = (void *)skb->data;
  3352. enum ath10k_hw_txrx_mode txmode;
  3353. enum ath10k_mac_tx_path txpath;
  3354. bool is_htt;
  3355. bool is_mgmt;
  3356. bool is_presp;
  3357. int ret;
  3358. ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
  3359. txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
  3360. txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
  3361. is_htt = (txpath == ATH10K_MAC_TX_HTT ||
  3362. txpath == ATH10K_MAC_TX_HTT_MGMT);
  3363. is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
  3364. if (is_htt) {
  3365. spin_lock_bh(&ar->htt.tx_lock);
  3366. is_presp = ieee80211_is_probe_resp(hdr->frame_control);
  3367. ret = ath10k_htt_tx_inc_pending(htt);
  3368. if (ret) {
  3369. ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
  3370. ret);
  3371. spin_unlock_bh(&ar->htt.tx_lock);
  3372. ieee80211_free_txskb(ar->hw, skb);
  3373. return;
  3374. }
  3375. ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
  3376. if (ret) {
  3377. ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
  3378. ret);
  3379. ath10k_htt_tx_dec_pending(htt);
  3380. spin_unlock_bh(&ar->htt.tx_lock);
  3381. ieee80211_free_txskb(ar->hw, skb);
  3382. return;
  3383. }
  3384. spin_unlock_bh(&ar->htt.tx_lock);
  3385. }
  3386. ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb);
  3387. if (ret) {
  3388. ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
  3389. if (is_htt) {
  3390. spin_lock_bh(&ar->htt.tx_lock);
  3391. ath10k_htt_tx_dec_pending(htt);
  3392. if (is_mgmt)
  3393. ath10k_htt_tx_mgmt_dec_pending(htt);
  3394. spin_unlock_bh(&ar->htt.tx_lock);
  3395. }
  3396. return;
  3397. }
  3398. }
  3399. static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
  3400. struct ieee80211_txq *txq)
  3401. {
  3402. struct ath10k *ar = hw->priv;
  3403. struct ath10k_txq *artxq = (void *)txq->drv_priv;
  3404. struct ieee80211_txq *f_txq;
  3405. struct ath10k_txq *f_artxq;
  3406. int ret = 0;
  3407. int max = 16;
  3408. spin_lock_bh(&ar->txqs_lock);
  3409. if (list_empty(&artxq->list))
  3410. list_add_tail(&artxq->list, &ar->txqs);
  3411. f_artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
  3412. f_txq = container_of((void *)f_artxq, struct ieee80211_txq, drv_priv);
  3413. list_del_init(&f_artxq->list);
  3414. while (ath10k_mac_tx_can_push(hw, f_txq) && max--) {
  3415. ret = ath10k_mac_tx_push_txq(hw, f_txq);
  3416. if (ret)
  3417. break;
  3418. }
  3419. if (ret != -ENOENT)
  3420. list_add_tail(&f_artxq->list, &ar->txqs);
  3421. spin_unlock_bh(&ar->txqs_lock);
  3422. ath10k_htt_tx_txq_update(hw, f_txq);
  3423. ath10k_htt_tx_txq_update(hw, txq);
  3424. }
  3425. /* Must not be called with conf_mutex held as workers can use that also. */
  3426. void ath10k_drain_tx(struct ath10k *ar)
  3427. {
  3428. /* make sure rcu-protected mac80211 tx path itself is drained */
  3429. synchronize_net();
  3430. ath10k_offchan_tx_purge(ar);
  3431. ath10k_mgmt_over_wmi_tx_purge(ar);
  3432. cancel_work_sync(&ar->offchan_tx_work);
  3433. cancel_work_sync(&ar->wmi_mgmt_tx_work);
  3434. }
  3435. void ath10k_halt(struct ath10k *ar)
  3436. {
  3437. struct ath10k_vif *arvif;
  3438. lockdep_assert_held(&ar->conf_mutex);
  3439. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  3440. ar->filter_flags = 0;
  3441. ar->monitor = false;
  3442. ar->monitor_arvif = NULL;
  3443. if (ar->monitor_started)
  3444. ath10k_monitor_stop(ar);
  3445. ar->monitor_started = false;
  3446. ar->tx_paused = 0;
  3447. ath10k_scan_finish(ar);
  3448. ath10k_peer_cleanup_all(ar);
  3449. ath10k_core_stop(ar);
  3450. ath10k_hif_power_down(ar);
  3451. spin_lock_bh(&ar->data_lock);
  3452. list_for_each_entry(arvif, &ar->arvifs, list)
  3453. ath10k_mac_vif_beacon_cleanup(arvif);
  3454. spin_unlock_bh(&ar->data_lock);
  3455. }
  3456. static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  3457. {
  3458. struct ath10k *ar = hw->priv;
  3459. mutex_lock(&ar->conf_mutex);
  3460. *tx_ant = ar->cfg_tx_chainmask;
  3461. *rx_ant = ar->cfg_rx_chainmask;
  3462. mutex_unlock(&ar->conf_mutex);
  3463. return 0;
  3464. }
  3465. static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
  3466. {
  3467. /* It is not clear that allowing gaps in chainmask
  3468. * is helpful. Probably it will not do what user
  3469. * is hoping for, so warn in that case.
  3470. */
  3471. if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
  3472. return;
  3473. ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
  3474. dbg, cm);
  3475. }
  3476. static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
  3477. {
  3478. int nsts = ar->vht_cap_info;
  3479. nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
  3480. nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
  3481. /* If firmware does not deliver to host number of space-time
  3482. * streams supported, assume it support up to 4 BF STS and return
  3483. * the value for VHT CAP: nsts-1)
  3484. */
  3485. if (nsts == 0)
  3486. return 3;
  3487. return nsts;
  3488. }
  3489. static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
  3490. {
  3491. int sound_dim = ar->vht_cap_info;
  3492. sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
  3493. sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
  3494. /* If the sounding dimension is not advertised by the firmware,
  3495. * let's use a default value of 1
  3496. */
  3497. if (sound_dim == 0)
  3498. return 1;
  3499. return sound_dim;
  3500. }
  3501. static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
  3502. {
  3503. struct ieee80211_sta_vht_cap vht_cap = {0};
  3504. u16 mcs_map;
  3505. u32 val;
  3506. int i;
  3507. vht_cap.vht_supported = 1;
  3508. vht_cap.cap = ar->vht_cap_info;
  3509. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  3510. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
  3511. val = ath10k_mac_get_vht_cap_bf_sts(ar);
  3512. val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
  3513. val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
  3514. vht_cap.cap |= val;
  3515. }
  3516. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  3517. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
  3518. val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
  3519. val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
  3520. val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
  3521. vht_cap.cap |= val;
  3522. }
  3523. /* Currently the firmware seems to be buggy, don't enable 80+80
  3524. * mode until that's resolved.
  3525. */
  3526. if ((ar->vht_cap_info & IEEE80211_VHT_CAP_SHORT_GI_160) &&
  3527. !(ar->vht_cap_info & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
  3528. vht_cap.cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  3529. mcs_map = 0;
  3530. for (i = 0; i < 8; i++) {
  3531. if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
  3532. mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
  3533. else
  3534. mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
  3535. }
  3536. if (ar->cfg_tx_chainmask <= 1)
  3537. vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
  3538. vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  3539. vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  3540. return vht_cap;
  3541. }
  3542. static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
  3543. {
  3544. int i;
  3545. struct ieee80211_sta_ht_cap ht_cap = {0};
  3546. if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
  3547. return ht_cap;
  3548. ht_cap.ht_supported = 1;
  3549. ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  3550. ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  3551. ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  3552. ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
  3553. ht_cap.cap |=
  3554. WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
  3555. if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
  3556. ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  3557. if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
  3558. ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  3559. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
  3560. u32 smps;
  3561. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  3562. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  3563. ht_cap.cap |= smps;
  3564. }
  3565. if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
  3566. ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  3567. if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
  3568. u32 stbc;
  3569. stbc = ar->ht_cap_info;
  3570. stbc &= WMI_HT_CAP_RX_STBC;
  3571. stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
  3572. stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
  3573. stbc &= IEEE80211_HT_CAP_RX_STBC;
  3574. ht_cap.cap |= stbc;
  3575. }
  3576. if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
  3577. ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
  3578. if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
  3579. ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
  3580. /* max AMSDU is implicitly taken from vht_cap_info */
  3581. if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
  3582. ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  3583. for (i = 0; i < ar->num_rf_chains; i++) {
  3584. if (ar->cfg_rx_chainmask & BIT(i))
  3585. ht_cap.mcs.rx_mask[i] = 0xFF;
  3586. }
  3587. ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  3588. return ht_cap;
  3589. }
  3590. static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
  3591. {
  3592. struct ieee80211_supported_band *band;
  3593. struct ieee80211_sta_vht_cap vht_cap;
  3594. struct ieee80211_sta_ht_cap ht_cap;
  3595. ht_cap = ath10k_get_ht_cap(ar);
  3596. vht_cap = ath10k_create_vht_cap(ar);
  3597. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  3598. band = &ar->mac.sbands[NL80211_BAND_2GHZ];
  3599. band->ht_cap = ht_cap;
  3600. }
  3601. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  3602. band = &ar->mac.sbands[NL80211_BAND_5GHZ];
  3603. band->ht_cap = ht_cap;
  3604. band->vht_cap = vht_cap;
  3605. }
  3606. }
  3607. static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
  3608. {
  3609. int ret;
  3610. lockdep_assert_held(&ar->conf_mutex);
  3611. ath10k_check_chain_mask(ar, tx_ant, "tx");
  3612. ath10k_check_chain_mask(ar, rx_ant, "rx");
  3613. ar->cfg_tx_chainmask = tx_ant;
  3614. ar->cfg_rx_chainmask = rx_ant;
  3615. if ((ar->state != ATH10K_STATE_ON) &&
  3616. (ar->state != ATH10K_STATE_RESTARTED))
  3617. return 0;
  3618. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
  3619. tx_ant);
  3620. if (ret) {
  3621. ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
  3622. ret, tx_ant);
  3623. return ret;
  3624. }
  3625. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
  3626. rx_ant);
  3627. if (ret) {
  3628. ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
  3629. ret, rx_ant);
  3630. return ret;
  3631. }
  3632. /* Reload HT/VHT capability */
  3633. ath10k_mac_setup_ht_vht_cap(ar);
  3634. return 0;
  3635. }
  3636. static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  3637. {
  3638. struct ath10k *ar = hw->priv;
  3639. int ret;
  3640. mutex_lock(&ar->conf_mutex);
  3641. ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
  3642. mutex_unlock(&ar->conf_mutex);
  3643. return ret;
  3644. }
  3645. static int ath10k_start(struct ieee80211_hw *hw)
  3646. {
  3647. struct ath10k *ar = hw->priv;
  3648. u32 param;
  3649. int ret = 0;
  3650. /*
  3651. * This makes sense only when restarting hw. It is harmless to call
  3652. * unconditionally. This is necessary to make sure no HTT/WMI tx
  3653. * commands will be submitted while restarting.
  3654. */
  3655. ath10k_drain_tx(ar);
  3656. mutex_lock(&ar->conf_mutex);
  3657. switch (ar->state) {
  3658. case ATH10K_STATE_OFF:
  3659. ar->state = ATH10K_STATE_ON;
  3660. break;
  3661. case ATH10K_STATE_RESTARTING:
  3662. ar->state = ATH10K_STATE_RESTARTED;
  3663. break;
  3664. case ATH10K_STATE_ON:
  3665. case ATH10K_STATE_RESTARTED:
  3666. case ATH10K_STATE_WEDGED:
  3667. WARN_ON(1);
  3668. ret = -EINVAL;
  3669. goto err;
  3670. case ATH10K_STATE_UTF:
  3671. ret = -EBUSY;
  3672. goto err;
  3673. }
  3674. ret = ath10k_hif_power_up(ar);
  3675. if (ret) {
  3676. ath10k_err(ar, "Could not init hif: %d\n", ret);
  3677. goto err_off;
  3678. }
  3679. ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
  3680. &ar->normal_mode_fw);
  3681. if (ret) {
  3682. ath10k_err(ar, "Could not init core: %d\n", ret);
  3683. goto err_power_down;
  3684. }
  3685. param = ar->wmi.pdev_param->pmf_qos;
  3686. ret = ath10k_wmi_pdev_set_param(ar, param, 1);
  3687. if (ret) {
  3688. ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
  3689. goto err_core_stop;
  3690. }
  3691. param = ar->wmi.pdev_param->dynamic_bw;
  3692. ret = ath10k_wmi_pdev_set_param(ar, param, 1);
  3693. if (ret) {
  3694. ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
  3695. goto err_core_stop;
  3696. }
  3697. if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
  3698. ret = ath10k_wmi_adaptive_qcs(ar, true);
  3699. if (ret) {
  3700. ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
  3701. ret);
  3702. goto err_core_stop;
  3703. }
  3704. }
  3705. if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
  3706. param = ar->wmi.pdev_param->burst_enable;
  3707. ret = ath10k_wmi_pdev_set_param(ar, param, 0);
  3708. if (ret) {
  3709. ath10k_warn(ar, "failed to disable burst: %d\n", ret);
  3710. goto err_core_stop;
  3711. }
  3712. }
  3713. __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
  3714. /*
  3715. * By default FW set ARP frames ac to voice (6). In that case ARP
  3716. * exchange is not working properly for UAPSD enabled AP. ARP requests
  3717. * which arrives with access category 0 are processed by network stack
  3718. * and send back with access category 0, but FW changes access category
  3719. * to 6. Set ARP frames access category to best effort (0) solves
  3720. * this problem.
  3721. */
  3722. param = ar->wmi.pdev_param->arp_ac_override;
  3723. ret = ath10k_wmi_pdev_set_param(ar, param, 0);
  3724. if (ret) {
  3725. ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
  3726. ret);
  3727. goto err_core_stop;
  3728. }
  3729. if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
  3730. ar->running_fw->fw_file.fw_features)) {
  3731. ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
  3732. WMI_CCA_DETECT_LEVEL_AUTO,
  3733. WMI_CCA_DETECT_MARGIN_AUTO);
  3734. if (ret) {
  3735. ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
  3736. ret);
  3737. goto err_core_stop;
  3738. }
  3739. }
  3740. param = ar->wmi.pdev_param->ani_enable;
  3741. ret = ath10k_wmi_pdev_set_param(ar, param, 1);
  3742. if (ret) {
  3743. ath10k_warn(ar, "failed to enable ani by default: %d\n",
  3744. ret);
  3745. goto err_core_stop;
  3746. }
  3747. ar->ani_enabled = true;
  3748. if (ath10k_peer_stats_enabled(ar)) {
  3749. param = ar->wmi.pdev_param->peer_stats_update_period;
  3750. ret = ath10k_wmi_pdev_set_param(ar, param,
  3751. PEER_DEFAULT_STATS_UPDATE_PERIOD);
  3752. if (ret) {
  3753. ath10k_warn(ar,
  3754. "failed to set peer stats period : %d\n",
  3755. ret);
  3756. goto err_core_stop;
  3757. }
  3758. }
  3759. param = ar->wmi.pdev_param->enable_btcoex;
  3760. if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
  3761. test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
  3762. ar->running_fw->fw_file.fw_features)) {
  3763. ret = ath10k_wmi_pdev_set_param(ar, param, 0);
  3764. if (ret) {
  3765. ath10k_warn(ar,
  3766. "failed to set btcoex param: %d\n", ret);
  3767. goto err_core_stop;
  3768. }
  3769. clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
  3770. }
  3771. ar->num_started_vdevs = 0;
  3772. ath10k_regd_update(ar);
  3773. ath10k_spectral_start(ar);
  3774. ath10k_thermal_set_throttling(ar);
  3775. mutex_unlock(&ar->conf_mutex);
  3776. return 0;
  3777. err_core_stop:
  3778. ath10k_core_stop(ar);
  3779. err_power_down:
  3780. ath10k_hif_power_down(ar);
  3781. err_off:
  3782. ar->state = ATH10K_STATE_OFF;
  3783. err:
  3784. mutex_unlock(&ar->conf_mutex);
  3785. return ret;
  3786. }
  3787. static void ath10k_stop(struct ieee80211_hw *hw)
  3788. {
  3789. struct ath10k *ar = hw->priv;
  3790. ath10k_drain_tx(ar);
  3791. mutex_lock(&ar->conf_mutex);
  3792. if (ar->state != ATH10K_STATE_OFF) {
  3793. ath10k_halt(ar);
  3794. ar->state = ATH10K_STATE_OFF;
  3795. }
  3796. mutex_unlock(&ar->conf_mutex);
  3797. cancel_delayed_work_sync(&ar->scan.timeout);
  3798. cancel_work_sync(&ar->restart_work);
  3799. }
  3800. static int ath10k_config_ps(struct ath10k *ar)
  3801. {
  3802. struct ath10k_vif *arvif;
  3803. int ret = 0;
  3804. lockdep_assert_held(&ar->conf_mutex);
  3805. list_for_each_entry(arvif, &ar->arvifs, list) {
  3806. ret = ath10k_mac_vif_setup_ps(arvif);
  3807. if (ret) {
  3808. ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
  3809. break;
  3810. }
  3811. }
  3812. return ret;
  3813. }
  3814. static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
  3815. {
  3816. int ret;
  3817. u32 param;
  3818. lockdep_assert_held(&ar->conf_mutex);
  3819. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
  3820. param = ar->wmi.pdev_param->txpower_limit2g;
  3821. ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
  3822. if (ret) {
  3823. ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
  3824. txpower, ret);
  3825. return ret;
  3826. }
  3827. param = ar->wmi.pdev_param->txpower_limit5g;
  3828. ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
  3829. if (ret) {
  3830. ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
  3831. txpower, ret);
  3832. return ret;
  3833. }
  3834. return 0;
  3835. }
  3836. static int ath10k_mac_txpower_recalc(struct ath10k *ar)
  3837. {
  3838. struct ath10k_vif *arvif;
  3839. int ret, txpower = -1;
  3840. lockdep_assert_held(&ar->conf_mutex);
  3841. list_for_each_entry(arvif, &ar->arvifs, list) {
  3842. if (arvif->txpower <= 0)
  3843. continue;
  3844. if (txpower == -1)
  3845. txpower = arvif->txpower;
  3846. else
  3847. txpower = min(txpower, arvif->txpower);
  3848. }
  3849. if (txpower == -1)
  3850. return 0;
  3851. ret = ath10k_mac_txpower_setup(ar, txpower);
  3852. if (ret) {
  3853. ath10k_warn(ar, "failed to setup tx power %d: %d\n",
  3854. txpower, ret);
  3855. return ret;
  3856. }
  3857. return 0;
  3858. }
  3859. static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
  3860. {
  3861. struct ath10k *ar = hw->priv;
  3862. struct ieee80211_conf *conf = &hw->conf;
  3863. int ret = 0;
  3864. mutex_lock(&ar->conf_mutex);
  3865. if (changed & IEEE80211_CONF_CHANGE_PS)
  3866. ath10k_config_ps(ar);
  3867. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  3868. ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
  3869. ret = ath10k_monitor_recalc(ar);
  3870. if (ret)
  3871. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  3872. }
  3873. mutex_unlock(&ar->conf_mutex);
  3874. return ret;
  3875. }
  3876. static u32 get_nss_from_chainmask(u16 chain_mask)
  3877. {
  3878. if ((chain_mask & 0xf) == 0xf)
  3879. return 4;
  3880. else if ((chain_mask & 0x7) == 0x7)
  3881. return 3;
  3882. else if ((chain_mask & 0x3) == 0x3)
  3883. return 2;
  3884. return 1;
  3885. }
  3886. static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
  3887. {
  3888. u32 value = 0;
  3889. struct ath10k *ar = arvif->ar;
  3890. int nsts;
  3891. int sound_dim;
  3892. if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
  3893. return 0;
  3894. nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
  3895. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  3896. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
  3897. value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
  3898. sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
  3899. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  3900. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
  3901. value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
  3902. if (!value)
  3903. return 0;
  3904. if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
  3905. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  3906. if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
  3907. value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
  3908. WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
  3909. if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
  3910. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  3911. if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
  3912. value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
  3913. WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
  3914. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3915. ar->wmi.vdev_param->txbf, value);
  3916. }
  3917. /*
  3918. * TODO:
  3919. * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
  3920. * because we will send mgmt frames without CCK. This requirement
  3921. * for P2P_FIND/GO_NEG should be handled by checking CCK flag
  3922. * in the TX packet.
  3923. */
  3924. static int ath10k_add_interface(struct ieee80211_hw *hw,
  3925. struct ieee80211_vif *vif)
  3926. {
  3927. struct ath10k *ar = hw->priv;
  3928. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  3929. struct ath10k_peer *peer;
  3930. enum wmi_sta_powersave_param param;
  3931. int ret = 0;
  3932. u32 value;
  3933. int bit;
  3934. int i;
  3935. u32 vdev_param;
  3936. vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
  3937. mutex_lock(&ar->conf_mutex);
  3938. memset(arvif, 0, sizeof(*arvif));
  3939. ath10k_mac_txq_init(vif->txq);
  3940. arvif->ar = ar;
  3941. arvif->vif = vif;
  3942. INIT_LIST_HEAD(&arvif->list);
  3943. INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
  3944. INIT_DELAYED_WORK(&arvif->connection_loss_work,
  3945. ath10k_mac_vif_sta_connection_loss_work);
  3946. for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
  3947. arvif->bitrate_mask.control[i].legacy = 0xffffffff;
  3948. memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
  3949. sizeof(arvif->bitrate_mask.control[i].ht_mcs));
  3950. memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
  3951. sizeof(arvif->bitrate_mask.control[i].vht_mcs));
  3952. }
  3953. if (ar->num_peers >= ar->max_num_peers) {
  3954. ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
  3955. ret = -ENOBUFS;
  3956. goto err;
  3957. }
  3958. if (ar->free_vdev_map == 0) {
  3959. ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
  3960. ret = -EBUSY;
  3961. goto err;
  3962. }
  3963. bit = __ffs64(ar->free_vdev_map);
  3964. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
  3965. bit, ar->free_vdev_map);
  3966. arvif->vdev_id = bit;
  3967. arvif->vdev_subtype =
  3968. ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
  3969. switch (vif->type) {
  3970. case NL80211_IFTYPE_P2P_DEVICE:
  3971. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  3972. arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
  3973. (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
  3974. break;
  3975. case NL80211_IFTYPE_UNSPECIFIED:
  3976. case NL80211_IFTYPE_STATION:
  3977. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  3978. if (vif->p2p)
  3979. arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
  3980. (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
  3981. break;
  3982. case NL80211_IFTYPE_ADHOC:
  3983. arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
  3984. break;
  3985. case NL80211_IFTYPE_MESH_POINT:
  3986. if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
  3987. arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
  3988. (ar, WMI_VDEV_SUBTYPE_MESH_11S);
  3989. } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  3990. ret = -EINVAL;
  3991. ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
  3992. goto err;
  3993. }
  3994. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  3995. break;
  3996. case NL80211_IFTYPE_AP:
  3997. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  3998. if (vif->p2p)
  3999. arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
  4000. (ar, WMI_VDEV_SUBTYPE_P2P_GO);
  4001. break;
  4002. case NL80211_IFTYPE_MONITOR:
  4003. arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
  4004. break;
  4005. default:
  4006. WARN_ON(1);
  4007. break;
  4008. }
  4009. /* Using vdev_id as queue number will make it very easy to do per-vif
  4010. * tx queue locking. This shouldn't wrap due to interface combinations
  4011. * but do a modulo for correctness sake and prevent using offchannel tx
  4012. * queues for regular vif tx.
  4013. */
  4014. vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
  4015. for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
  4016. vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
  4017. /* Some firmware revisions don't wait for beacon tx completion before
  4018. * sending another SWBA event. This could lead to hardware using old
  4019. * (freed) beacon data in some cases, e.g. tx credit starvation
  4020. * combined with missed TBTT. This is very very rare.
  4021. *
  4022. * On non-IOMMU-enabled hosts this could be a possible security issue
  4023. * because hw could beacon some random data on the air. On
  4024. * IOMMU-enabled hosts DMAR faults would occur in most cases and target
  4025. * device would crash.
  4026. *
  4027. * Since there are no beacon tx completions (implicit nor explicit)
  4028. * propagated to host the only workaround for this is to allocate a
  4029. * DMA-coherent buffer for a lifetime of a vif and use it for all
  4030. * beacon tx commands. Worst case for this approach is some beacons may
  4031. * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
  4032. */
  4033. if (vif->type == NL80211_IFTYPE_ADHOC ||
  4034. vif->type == NL80211_IFTYPE_MESH_POINT ||
  4035. vif->type == NL80211_IFTYPE_AP) {
  4036. arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
  4037. IEEE80211_MAX_FRAME_LEN,
  4038. &arvif->beacon_paddr,
  4039. GFP_ATOMIC);
  4040. if (!arvif->beacon_buf) {
  4041. ret = -ENOMEM;
  4042. ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
  4043. ret);
  4044. goto err;
  4045. }
  4046. }
  4047. if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
  4048. arvif->nohwcrypt = true;
  4049. if (arvif->nohwcrypt &&
  4050. !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  4051. ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
  4052. goto err;
  4053. }
  4054. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
  4055. arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
  4056. arvif->beacon_buf ? "single-buf" : "per-skb");
  4057. ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
  4058. arvif->vdev_subtype, vif->addr);
  4059. if (ret) {
  4060. ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
  4061. arvif->vdev_id, ret);
  4062. goto err;
  4063. }
  4064. ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
  4065. spin_lock_bh(&ar->data_lock);
  4066. list_add(&arvif->list, &ar->arvifs);
  4067. spin_unlock_bh(&ar->data_lock);
  4068. /* It makes no sense to have firmware do keepalives. mac80211 already
  4069. * takes care of this with idle connection polling.
  4070. */
  4071. ret = ath10k_mac_vif_disable_keepalive(arvif);
  4072. if (ret) {
  4073. ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
  4074. arvif->vdev_id, ret);
  4075. goto err_vdev_delete;
  4076. }
  4077. arvif->def_wep_key_idx = -1;
  4078. vdev_param = ar->wmi.vdev_param->tx_encap_type;
  4079. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4080. ATH10K_HW_TXRX_NATIVE_WIFI);
  4081. /* 10.X firmware does not support this VDEV parameter. Do not warn */
  4082. if (ret && ret != -EOPNOTSUPP) {
  4083. ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
  4084. arvif->vdev_id, ret);
  4085. goto err_vdev_delete;
  4086. }
  4087. /* Configuring number of spatial stream for monitor interface is causing
  4088. * target assert in qca9888 and qca6174.
  4089. */
  4090. if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
  4091. u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
  4092. vdev_param = ar->wmi.vdev_param->nss;
  4093. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4094. nss);
  4095. if (ret) {
  4096. ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
  4097. arvif->vdev_id, ar->cfg_tx_chainmask, nss,
  4098. ret);
  4099. goto err_vdev_delete;
  4100. }
  4101. }
  4102. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  4103. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  4104. ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
  4105. vif->addr, WMI_PEER_TYPE_DEFAULT);
  4106. if (ret) {
  4107. ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
  4108. arvif->vdev_id, ret);
  4109. goto err_vdev_delete;
  4110. }
  4111. spin_lock_bh(&ar->data_lock);
  4112. peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
  4113. if (!peer) {
  4114. ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
  4115. vif->addr, arvif->vdev_id);
  4116. spin_unlock_bh(&ar->data_lock);
  4117. ret = -ENOENT;
  4118. goto err_peer_delete;
  4119. }
  4120. arvif->peer_id = find_first_bit(peer->peer_ids,
  4121. ATH10K_MAX_NUM_PEER_IDS);
  4122. spin_unlock_bh(&ar->data_lock);
  4123. } else {
  4124. arvif->peer_id = HTT_INVALID_PEERID;
  4125. }
  4126. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  4127. ret = ath10k_mac_set_kickout(arvif);
  4128. if (ret) {
  4129. ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
  4130. arvif->vdev_id, ret);
  4131. goto err_peer_delete;
  4132. }
  4133. }
  4134. if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
  4135. param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
  4136. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  4137. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  4138. param, value);
  4139. if (ret) {
  4140. ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
  4141. arvif->vdev_id, ret);
  4142. goto err_peer_delete;
  4143. }
  4144. ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
  4145. if (ret) {
  4146. ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
  4147. arvif->vdev_id, ret);
  4148. goto err_peer_delete;
  4149. }
  4150. ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
  4151. if (ret) {
  4152. ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
  4153. arvif->vdev_id, ret);
  4154. goto err_peer_delete;
  4155. }
  4156. }
  4157. ret = ath10k_mac_set_txbf_conf(arvif);
  4158. if (ret) {
  4159. ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
  4160. arvif->vdev_id, ret);
  4161. goto err_peer_delete;
  4162. }
  4163. ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
  4164. if (ret) {
  4165. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  4166. arvif->vdev_id, ret);
  4167. goto err_peer_delete;
  4168. }
  4169. arvif->txpower = vif->bss_conf.txpower;
  4170. ret = ath10k_mac_txpower_recalc(ar);
  4171. if (ret) {
  4172. ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
  4173. goto err_peer_delete;
  4174. }
  4175. if (vif->type == NL80211_IFTYPE_MONITOR) {
  4176. ar->monitor_arvif = arvif;
  4177. ret = ath10k_monitor_recalc(ar);
  4178. if (ret) {
  4179. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  4180. goto err_peer_delete;
  4181. }
  4182. }
  4183. spin_lock_bh(&ar->htt.tx_lock);
  4184. if (!ar->tx_paused)
  4185. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  4186. spin_unlock_bh(&ar->htt.tx_lock);
  4187. mutex_unlock(&ar->conf_mutex);
  4188. return 0;
  4189. err_peer_delete:
  4190. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  4191. arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
  4192. ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
  4193. err_vdev_delete:
  4194. ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  4195. ar->free_vdev_map |= 1LL << arvif->vdev_id;
  4196. spin_lock_bh(&ar->data_lock);
  4197. list_del(&arvif->list);
  4198. spin_unlock_bh(&ar->data_lock);
  4199. err:
  4200. if (arvif->beacon_buf) {
  4201. dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
  4202. arvif->beacon_buf, arvif->beacon_paddr);
  4203. arvif->beacon_buf = NULL;
  4204. }
  4205. mutex_unlock(&ar->conf_mutex);
  4206. return ret;
  4207. }
  4208. static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
  4209. {
  4210. int i;
  4211. for (i = 0; i < BITS_PER_LONG; i++)
  4212. ath10k_mac_vif_tx_unlock(arvif, i);
  4213. }
  4214. static void ath10k_remove_interface(struct ieee80211_hw *hw,
  4215. struct ieee80211_vif *vif)
  4216. {
  4217. struct ath10k *ar = hw->priv;
  4218. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4219. struct ath10k_peer *peer;
  4220. int ret;
  4221. int i;
  4222. cancel_work_sync(&arvif->ap_csa_work);
  4223. cancel_delayed_work_sync(&arvif->connection_loss_work);
  4224. mutex_lock(&ar->conf_mutex);
  4225. spin_lock_bh(&ar->data_lock);
  4226. ath10k_mac_vif_beacon_cleanup(arvif);
  4227. spin_unlock_bh(&ar->data_lock);
  4228. ret = ath10k_spectral_vif_stop(arvif);
  4229. if (ret)
  4230. ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
  4231. arvif->vdev_id, ret);
  4232. ar->free_vdev_map |= 1LL << arvif->vdev_id;
  4233. spin_lock_bh(&ar->data_lock);
  4234. list_del(&arvif->list);
  4235. spin_unlock_bh(&ar->data_lock);
  4236. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  4237. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  4238. ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
  4239. vif->addr);
  4240. if (ret)
  4241. ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
  4242. arvif->vdev_id, ret);
  4243. kfree(arvif->u.ap.noa_data);
  4244. }
  4245. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
  4246. arvif->vdev_id);
  4247. ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  4248. if (ret)
  4249. ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
  4250. arvif->vdev_id, ret);
  4251. /* Some firmware revisions don't notify host about self-peer removal
  4252. * until after associated vdev is deleted.
  4253. */
  4254. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  4255. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  4256. ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
  4257. vif->addr);
  4258. if (ret)
  4259. ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
  4260. arvif->vdev_id, ret);
  4261. spin_lock_bh(&ar->data_lock);
  4262. ar->num_peers--;
  4263. spin_unlock_bh(&ar->data_lock);
  4264. }
  4265. spin_lock_bh(&ar->data_lock);
  4266. for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
  4267. peer = ar->peer_map[i];
  4268. if (!peer)
  4269. continue;
  4270. if (peer->vif == vif) {
  4271. ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
  4272. vif->addr, arvif->vdev_id);
  4273. peer->vif = NULL;
  4274. }
  4275. }
  4276. spin_unlock_bh(&ar->data_lock);
  4277. ath10k_peer_cleanup(ar, arvif->vdev_id);
  4278. ath10k_mac_txq_unref(ar, vif->txq);
  4279. if (vif->type == NL80211_IFTYPE_MONITOR) {
  4280. ar->monitor_arvif = NULL;
  4281. ret = ath10k_monitor_recalc(ar);
  4282. if (ret)
  4283. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  4284. }
  4285. ret = ath10k_mac_txpower_recalc(ar);
  4286. if (ret)
  4287. ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
  4288. spin_lock_bh(&ar->htt.tx_lock);
  4289. ath10k_mac_vif_tx_unlock_all(arvif);
  4290. spin_unlock_bh(&ar->htt.tx_lock);
  4291. ath10k_mac_txq_unref(ar, vif->txq);
  4292. mutex_unlock(&ar->conf_mutex);
  4293. }
  4294. /*
  4295. * FIXME: Has to be verified.
  4296. */
  4297. #define SUPPORTED_FILTERS \
  4298. (FIF_ALLMULTI | \
  4299. FIF_CONTROL | \
  4300. FIF_PSPOLL | \
  4301. FIF_OTHER_BSS | \
  4302. FIF_BCN_PRBRESP_PROMISC | \
  4303. FIF_PROBE_REQ | \
  4304. FIF_FCSFAIL)
  4305. static void ath10k_configure_filter(struct ieee80211_hw *hw,
  4306. unsigned int changed_flags,
  4307. unsigned int *total_flags,
  4308. u64 multicast)
  4309. {
  4310. struct ath10k *ar = hw->priv;
  4311. int ret;
  4312. mutex_lock(&ar->conf_mutex);
  4313. changed_flags &= SUPPORTED_FILTERS;
  4314. *total_flags &= SUPPORTED_FILTERS;
  4315. ar->filter_flags = *total_flags;
  4316. ret = ath10k_monitor_recalc(ar);
  4317. if (ret)
  4318. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  4319. mutex_unlock(&ar->conf_mutex);
  4320. }
  4321. static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
  4322. struct ieee80211_vif *vif,
  4323. struct ieee80211_bss_conf *info,
  4324. u32 changed)
  4325. {
  4326. struct ath10k *ar = hw->priv;
  4327. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4328. int ret = 0;
  4329. u32 vdev_param, pdev_param, slottime, preamble;
  4330. mutex_lock(&ar->conf_mutex);
  4331. if (changed & BSS_CHANGED_IBSS)
  4332. ath10k_control_ibss(arvif, info, vif->addr);
  4333. if (changed & BSS_CHANGED_BEACON_INT) {
  4334. arvif->beacon_interval = info->beacon_int;
  4335. vdev_param = ar->wmi.vdev_param->beacon_interval;
  4336. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4337. arvif->beacon_interval);
  4338. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4339. "mac vdev %d beacon_interval %d\n",
  4340. arvif->vdev_id, arvif->beacon_interval);
  4341. if (ret)
  4342. ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
  4343. arvif->vdev_id, ret);
  4344. }
  4345. if (changed & BSS_CHANGED_BEACON) {
  4346. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4347. "vdev %d set beacon tx mode to staggered\n",
  4348. arvif->vdev_id);
  4349. pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
  4350. ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
  4351. WMI_BEACON_STAGGERED_MODE);
  4352. if (ret)
  4353. ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
  4354. arvif->vdev_id, ret);
  4355. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  4356. if (ret)
  4357. ath10k_warn(ar, "failed to update beacon template: %d\n",
  4358. ret);
  4359. if (ieee80211_vif_is_mesh(vif)) {
  4360. /* mesh doesn't use SSID but firmware needs it */
  4361. strncpy(arvif->u.ap.ssid, "mesh",
  4362. sizeof(arvif->u.ap.ssid));
  4363. arvif->u.ap.ssid_len = 4;
  4364. }
  4365. }
  4366. if (changed & BSS_CHANGED_AP_PROBE_RESP) {
  4367. ret = ath10k_mac_setup_prb_tmpl(arvif);
  4368. if (ret)
  4369. ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
  4370. arvif->vdev_id, ret);
  4371. }
  4372. if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
  4373. arvif->dtim_period = info->dtim_period;
  4374. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4375. "mac vdev %d dtim_period %d\n",
  4376. arvif->vdev_id, arvif->dtim_period);
  4377. vdev_param = ar->wmi.vdev_param->dtim_period;
  4378. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4379. arvif->dtim_period);
  4380. if (ret)
  4381. ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
  4382. arvif->vdev_id, ret);
  4383. }
  4384. if (changed & BSS_CHANGED_SSID &&
  4385. vif->type == NL80211_IFTYPE_AP) {
  4386. arvif->u.ap.ssid_len = info->ssid_len;
  4387. if (info->ssid_len)
  4388. memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
  4389. arvif->u.ap.hidden_ssid = info->hidden_ssid;
  4390. }
  4391. if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
  4392. ether_addr_copy(arvif->bssid, info->bssid);
  4393. if (changed & BSS_CHANGED_BEACON_ENABLED)
  4394. ath10k_control_beaconing(arvif, info);
  4395. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  4396. arvif->use_cts_prot = info->use_cts_prot;
  4397. ret = ath10k_recalc_rtscts_prot(arvif);
  4398. if (ret)
  4399. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  4400. arvif->vdev_id, ret);
  4401. if (ath10k_mac_can_set_cts_prot(arvif)) {
  4402. ret = ath10k_mac_set_cts_prot(arvif);
  4403. if (ret)
  4404. ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
  4405. arvif->vdev_id, ret);
  4406. }
  4407. }
  4408. if (changed & BSS_CHANGED_ERP_SLOT) {
  4409. if (info->use_short_slot)
  4410. slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
  4411. else
  4412. slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
  4413. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
  4414. arvif->vdev_id, slottime);
  4415. vdev_param = ar->wmi.vdev_param->slot_time;
  4416. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4417. slottime);
  4418. if (ret)
  4419. ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
  4420. arvif->vdev_id, ret);
  4421. }
  4422. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  4423. if (info->use_short_preamble)
  4424. preamble = WMI_VDEV_PREAMBLE_SHORT;
  4425. else
  4426. preamble = WMI_VDEV_PREAMBLE_LONG;
  4427. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4428. "mac vdev %d preamble %dn",
  4429. arvif->vdev_id, preamble);
  4430. vdev_param = ar->wmi.vdev_param->preamble;
  4431. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4432. preamble);
  4433. if (ret)
  4434. ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
  4435. arvif->vdev_id, ret);
  4436. }
  4437. if (changed & BSS_CHANGED_ASSOC) {
  4438. if (info->assoc) {
  4439. /* Workaround: Make sure monitor vdev is not running
  4440. * when associating to prevent some firmware revisions
  4441. * (e.g. 10.1 and 10.2) from crashing.
  4442. */
  4443. if (ar->monitor_started)
  4444. ath10k_monitor_stop(ar);
  4445. ath10k_bss_assoc(hw, vif, info);
  4446. ath10k_monitor_recalc(ar);
  4447. } else {
  4448. ath10k_bss_disassoc(hw, vif);
  4449. }
  4450. }
  4451. if (changed & BSS_CHANGED_TXPOWER) {
  4452. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
  4453. arvif->vdev_id, info->txpower);
  4454. arvif->txpower = info->txpower;
  4455. ret = ath10k_mac_txpower_recalc(ar);
  4456. if (ret)
  4457. ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
  4458. }
  4459. if (changed & BSS_CHANGED_PS) {
  4460. arvif->ps = vif->bss_conf.ps;
  4461. ret = ath10k_config_ps(ar);
  4462. if (ret)
  4463. ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
  4464. arvif->vdev_id, ret);
  4465. }
  4466. mutex_unlock(&ar->conf_mutex);
  4467. }
  4468. static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
  4469. {
  4470. struct ath10k *ar = hw->priv;
  4471. /* This function should never be called if setting the coverage class
  4472. * is not supported on this hardware.
  4473. */
  4474. if (!ar->hw_params.hw_ops->set_coverage_class) {
  4475. WARN_ON_ONCE(1);
  4476. return;
  4477. }
  4478. ar->hw_params.hw_ops->set_coverage_class(ar, value);
  4479. }
  4480. static int ath10k_hw_scan(struct ieee80211_hw *hw,
  4481. struct ieee80211_vif *vif,
  4482. struct ieee80211_scan_request *hw_req)
  4483. {
  4484. struct ath10k *ar = hw->priv;
  4485. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4486. struct cfg80211_scan_request *req = &hw_req->req;
  4487. struct wmi_start_scan_arg arg;
  4488. int ret = 0;
  4489. int i;
  4490. mutex_lock(&ar->conf_mutex);
  4491. spin_lock_bh(&ar->data_lock);
  4492. switch (ar->scan.state) {
  4493. case ATH10K_SCAN_IDLE:
  4494. reinit_completion(&ar->scan.started);
  4495. reinit_completion(&ar->scan.completed);
  4496. ar->scan.state = ATH10K_SCAN_STARTING;
  4497. ar->scan.is_roc = false;
  4498. ar->scan.vdev_id = arvif->vdev_id;
  4499. ret = 0;
  4500. break;
  4501. case ATH10K_SCAN_STARTING:
  4502. case ATH10K_SCAN_RUNNING:
  4503. case ATH10K_SCAN_ABORTING:
  4504. ret = -EBUSY;
  4505. break;
  4506. }
  4507. spin_unlock_bh(&ar->data_lock);
  4508. if (ret)
  4509. goto exit;
  4510. memset(&arg, 0, sizeof(arg));
  4511. ath10k_wmi_start_scan_init(ar, &arg);
  4512. arg.vdev_id = arvif->vdev_id;
  4513. arg.scan_id = ATH10K_SCAN_ID;
  4514. if (req->ie_len) {
  4515. arg.ie_len = req->ie_len;
  4516. memcpy(arg.ie, req->ie, arg.ie_len);
  4517. }
  4518. if (req->n_ssids) {
  4519. arg.n_ssids = req->n_ssids;
  4520. for (i = 0; i < arg.n_ssids; i++) {
  4521. arg.ssids[i].len = req->ssids[i].ssid_len;
  4522. arg.ssids[i].ssid = req->ssids[i].ssid;
  4523. }
  4524. } else {
  4525. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  4526. }
  4527. if (req->n_channels) {
  4528. arg.n_channels = req->n_channels;
  4529. for (i = 0; i < arg.n_channels; i++)
  4530. arg.channels[i] = req->channels[i]->center_freq;
  4531. }
  4532. ret = ath10k_start_scan(ar, &arg);
  4533. if (ret) {
  4534. ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
  4535. spin_lock_bh(&ar->data_lock);
  4536. ar->scan.state = ATH10K_SCAN_IDLE;
  4537. spin_unlock_bh(&ar->data_lock);
  4538. }
  4539. /* Add a 200ms margin to account for event/command processing */
  4540. ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
  4541. msecs_to_jiffies(arg.max_scan_time +
  4542. 200));
  4543. exit:
  4544. mutex_unlock(&ar->conf_mutex);
  4545. return ret;
  4546. }
  4547. static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
  4548. struct ieee80211_vif *vif)
  4549. {
  4550. struct ath10k *ar = hw->priv;
  4551. mutex_lock(&ar->conf_mutex);
  4552. ath10k_scan_abort(ar);
  4553. mutex_unlock(&ar->conf_mutex);
  4554. cancel_delayed_work_sync(&ar->scan.timeout);
  4555. }
  4556. static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
  4557. struct ath10k_vif *arvif,
  4558. enum set_key_cmd cmd,
  4559. struct ieee80211_key_conf *key)
  4560. {
  4561. u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
  4562. int ret;
  4563. /* 10.1 firmware branch requires default key index to be set to group
  4564. * key index after installing it. Otherwise FW/HW Txes corrupted
  4565. * frames with multi-vif APs. This is not required for main firmware
  4566. * branch (e.g. 636).
  4567. *
  4568. * This is also needed for 636 fw for IBSS-RSN to work more reliably.
  4569. *
  4570. * FIXME: It remains unknown if this is required for multi-vif STA
  4571. * interfaces on 10.1.
  4572. */
  4573. if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
  4574. arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
  4575. return;
  4576. if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
  4577. return;
  4578. if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
  4579. return;
  4580. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  4581. return;
  4582. if (cmd != SET_KEY)
  4583. return;
  4584. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4585. key->keyidx);
  4586. if (ret)
  4587. ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
  4588. arvif->vdev_id, ret);
  4589. }
  4590. static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  4591. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  4592. struct ieee80211_key_conf *key)
  4593. {
  4594. struct ath10k *ar = hw->priv;
  4595. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4596. struct ath10k_peer *peer;
  4597. const u8 *peer_addr;
  4598. bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  4599. key->cipher == WLAN_CIPHER_SUITE_WEP104;
  4600. int ret = 0;
  4601. int ret2;
  4602. u32 flags = 0;
  4603. u32 flags2;
  4604. /* this one needs to be done in software */
  4605. if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  4606. return 1;
  4607. if (arvif->nohwcrypt)
  4608. return 1;
  4609. if (key->keyidx > WMI_MAX_KEY_INDEX)
  4610. return -ENOSPC;
  4611. mutex_lock(&ar->conf_mutex);
  4612. if (sta)
  4613. peer_addr = sta->addr;
  4614. else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  4615. peer_addr = vif->bss_conf.bssid;
  4616. else
  4617. peer_addr = vif->addr;
  4618. key->hw_key_idx = key->keyidx;
  4619. if (is_wep) {
  4620. if (cmd == SET_KEY)
  4621. arvif->wep_keys[key->keyidx] = key;
  4622. else
  4623. arvif->wep_keys[key->keyidx] = NULL;
  4624. }
  4625. /* the peer should not disappear in mid-way (unless FW goes awry) since
  4626. * we already hold conf_mutex. we just make sure its there now. */
  4627. spin_lock_bh(&ar->data_lock);
  4628. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  4629. spin_unlock_bh(&ar->data_lock);
  4630. if (!peer) {
  4631. if (cmd == SET_KEY) {
  4632. ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
  4633. peer_addr);
  4634. ret = -EOPNOTSUPP;
  4635. goto exit;
  4636. } else {
  4637. /* if the peer doesn't exist there is no key to disable
  4638. * anymore */
  4639. goto exit;
  4640. }
  4641. }
  4642. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  4643. flags |= WMI_KEY_PAIRWISE;
  4644. else
  4645. flags |= WMI_KEY_GROUP;
  4646. if (is_wep) {
  4647. if (cmd == DISABLE_KEY)
  4648. ath10k_clear_vdev_key(arvif, key);
  4649. /* When WEP keys are uploaded it's possible that there are
  4650. * stations associated already (e.g. when merging) without any
  4651. * keys. Static WEP needs an explicit per-peer key upload.
  4652. */
  4653. if (vif->type == NL80211_IFTYPE_ADHOC &&
  4654. cmd == SET_KEY)
  4655. ath10k_mac_vif_update_wep_key(arvif, key);
  4656. /* 802.1x never sets the def_wep_key_idx so each set_key()
  4657. * call changes default tx key.
  4658. *
  4659. * Static WEP sets def_wep_key_idx via .set_default_unicast_key
  4660. * after first set_key().
  4661. */
  4662. if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
  4663. flags |= WMI_KEY_TX_USAGE;
  4664. }
  4665. ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
  4666. if (ret) {
  4667. WARN_ON(ret > 0);
  4668. ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
  4669. arvif->vdev_id, peer_addr, ret);
  4670. goto exit;
  4671. }
  4672. /* mac80211 sets static WEP keys as groupwise while firmware requires
  4673. * them to be installed twice as both pairwise and groupwise.
  4674. */
  4675. if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
  4676. flags2 = flags;
  4677. flags2 &= ~WMI_KEY_GROUP;
  4678. flags2 |= WMI_KEY_PAIRWISE;
  4679. ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
  4680. if (ret) {
  4681. WARN_ON(ret > 0);
  4682. ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
  4683. arvif->vdev_id, peer_addr, ret);
  4684. ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
  4685. peer_addr, flags);
  4686. if (ret2) {
  4687. WARN_ON(ret2 > 0);
  4688. ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
  4689. arvif->vdev_id, peer_addr, ret2);
  4690. }
  4691. goto exit;
  4692. }
  4693. }
  4694. ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
  4695. spin_lock_bh(&ar->data_lock);
  4696. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  4697. if (peer && cmd == SET_KEY)
  4698. peer->keys[key->keyidx] = key;
  4699. else if (peer && cmd == DISABLE_KEY)
  4700. peer->keys[key->keyidx] = NULL;
  4701. else if (peer == NULL)
  4702. /* impossible unless FW goes crazy */
  4703. ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
  4704. spin_unlock_bh(&ar->data_lock);
  4705. exit:
  4706. mutex_unlock(&ar->conf_mutex);
  4707. return ret;
  4708. }
  4709. static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
  4710. struct ieee80211_vif *vif,
  4711. int keyidx)
  4712. {
  4713. struct ath10k *ar = hw->priv;
  4714. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4715. int ret;
  4716. mutex_lock(&arvif->ar->conf_mutex);
  4717. if (arvif->ar->state != ATH10K_STATE_ON)
  4718. goto unlock;
  4719. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
  4720. arvif->vdev_id, keyidx);
  4721. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  4722. arvif->vdev_id,
  4723. arvif->ar->wmi.vdev_param->def_keyid,
  4724. keyidx);
  4725. if (ret) {
  4726. ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
  4727. arvif->vdev_id,
  4728. ret);
  4729. goto unlock;
  4730. }
  4731. arvif->def_wep_key_idx = keyidx;
  4732. unlock:
  4733. mutex_unlock(&arvif->ar->conf_mutex);
  4734. }
  4735. static void ath10k_sta_rc_update_wk(struct work_struct *wk)
  4736. {
  4737. struct ath10k *ar;
  4738. struct ath10k_vif *arvif;
  4739. struct ath10k_sta *arsta;
  4740. struct ieee80211_sta *sta;
  4741. struct cfg80211_chan_def def;
  4742. enum nl80211_band band;
  4743. const u8 *ht_mcs_mask;
  4744. const u16 *vht_mcs_mask;
  4745. u32 changed, bw, nss, smps;
  4746. int err;
  4747. arsta = container_of(wk, struct ath10k_sta, update_wk);
  4748. sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
  4749. arvif = arsta->arvif;
  4750. ar = arvif->ar;
  4751. if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
  4752. return;
  4753. band = def.chan->band;
  4754. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  4755. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  4756. spin_lock_bh(&ar->data_lock);
  4757. changed = arsta->changed;
  4758. arsta->changed = 0;
  4759. bw = arsta->bw;
  4760. nss = arsta->nss;
  4761. smps = arsta->smps;
  4762. spin_unlock_bh(&ar->data_lock);
  4763. mutex_lock(&ar->conf_mutex);
  4764. nss = max_t(u32, 1, nss);
  4765. nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
  4766. ath10k_mac_max_vht_nss(vht_mcs_mask)));
  4767. if (changed & IEEE80211_RC_BW_CHANGED) {
  4768. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
  4769. sta->addr, bw);
  4770. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4771. WMI_PEER_CHAN_WIDTH, bw);
  4772. if (err)
  4773. ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
  4774. sta->addr, bw, err);
  4775. }
  4776. if (changed & IEEE80211_RC_NSS_CHANGED) {
  4777. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
  4778. sta->addr, nss);
  4779. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4780. WMI_PEER_NSS, nss);
  4781. if (err)
  4782. ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
  4783. sta->addr, nss, err);
  4784. }
  4785. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  4786. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
  4787. sta->addr, smps);
  4788. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4789. WMI_PEER_SMPS_STATE, smps);
  4790. if (err)
  4791. ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
  4792. sta->addr, smps, err);
  4793. }
  4794. if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
  4795. changed & IEEE80211_RC_NSS_CHANGED) {
  4796. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
  4797. sta->addr);
  4798. err = ath10k_station_assoc(ar, arvif->vif, sta, true);
  4799. if (err)
  4800. ath10k_warn(ar, "failed to reassociate station: %pM\n",
  4801. sta->addr);
  4802. }
  4803. mutex_unlock(&ar->conf_mutex);
  4804. }
  4805. static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
  4806. struct ieee80211_sta *sta)
  4807. {
  4808. struct ath10k *ar = arvif->ar;
  4809. lockdep_assert_held(&ar->conf_mutex);
  4810. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
  4811. return 0;
  4812. if (ar->num_stations >= ar->max_num_stations)
  4813. return -ENOBUFS;
  4814. ar->num_stations++;
  4815. return 0;
  4816. }
  4817. static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
  4818. struct ieee80211_sta *sta)
  4819. {
  4820. struct ath10k *ar = arvif->ar;
  4821. lockdep_assert_held(&ar->conf_mutex);
  4822. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
  4823. return;
  4824. ar->num_stations--;
  4825. }
  4826. struct ath10k_mac_tdls_iter_data {
  4827. u32 num_tdls_stations;
  4828. struct ieee80211_vif *curr_vif;
  4829. };
  4830. static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
  4831. struct ieee80211_sta *sta)
  4832. {
  4833. struct ath10k_mac_tdls_iter_data *iter_data = data;
  4834. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  4835. struct ieee80211_vif *sta_vif = arsta->arvif->vif;
  4836. if (sta->tdls && sta_vif == iter_data->curr_vif)
  4837. iter_data->num_tdls_stations++;
  4838. }
  4839. static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
  4840. struct ieee80211_vif *vif)
  4841. {
  4842. struct ath10k_mac_tdls_iter_data data = {};
  4843. data.curr_vif = vif;
  4844. ieee80211_iterate_stations_atomic(hw,
  4845. ath10k_mac_tdls_vif_stations_count_iter,
  4846. &data);
  4847. return data.num_tdls_stations;
  4848. }
  4849. static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
  4850. struct ieee80211_vif *vif)
  4851. {
  4852. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4853. int *num_tdls_vifs = data;
  4854. if (vif->type != NL80211_IFTYPE_STATION)
  4855. return;
  4856. if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
  4857. (*num_tdls_vifs)++;
  4858. }
  4859. static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
  4860. {
  4861. int num_tdls_vifs = 0;
  4862. ieee80211_iterate_active_interfaces_atomic(hw,
  4863. IEEE80211_IFACE_ITER_NORMAL,
  4864. ath10k_mac_tdls_vifs_count_iter,
  4865. &num_tdls_vifs);
  4866. return num_tdls_vifs;
  4867. }
  4868. static int ath10k_sta_state(struct ieee80211_hw *hw,
  4869. struct ieee80211_vif *vif,
  4870. struct ieee80211_sta *sta,
  4871. enum ieee80211_sta_state old_state,
  4872. enum ieee80211_sta_state new_state)
  4873. {
  4874. struct ath10k *ar = hw->priv;
  4875. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  4876. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  4877. struct ath10k_peer *peer;
  4878. int ret = 0;
  4879. int i;
  4880. if (old_state == IEEE80211_STA_NOTEXIST &&
  4881. new_state == IEEE80211_STA_NONE) {
  4882. memset(arsta, 0, sizeof(*arsta));
  4883. arsta->arvif = arvif;
  4884. INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
  4885. for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
  4886. ath10k_mac_txq_init(sta->txq[i]);
  4887. }
  4888. /* cancel must be done outside the mutex to avoid deadlock */
  4889. if ((old_state == IEEE80211_STA_NONE &&
  4890. new_state == IEEE80211_STA_NOTEXIST))
  4891. cancel_work_sync(&arsta->update_wk);
  4892. mutex_lock(&ar->conf_mutex);
  4893. if (old_state == IEEE80211_STA_NOTEXIST &&
  4894. new_state == IEEE80211_STA_NONE) {
  4895. /*
  4896. * New station addition.
  4897. */
  4898. enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
  4899. u32 num_tdls_stations;
  4900. u32 num_tdls_vifs;
  4901. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4902. "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
  4903. arvif->vdev_id, sta->addr,
  4904. ar->num_stations + 1, ar->max_num_stations,
  4905. ar->num_peers + 1, ar->max_num_peers);
  4906. ret = ath10k_mac_inc_num_stations(arvif, sta);
  4907. if (ret) {
  4908. ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
  4909. ar->max_num_stations);
  4910. goto exit;
  4911. }
  4912. if (sta->tdls)
  4913. peer_type = WMI_PEER_TYPE_TDLS;
  4914. ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
  4915. sta->addr, peer_type);
  4916. if (ret) {
  4917. ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
  4918. sta->addr, arvif->vdev_id, ret);
  4919. ath10k_mac_dec_num_stations(arvif, sta);
  4920. goto exit;
  4921. }
  4922. spin_lock_bh(&ar->data_lock);
  4923. peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
  4924. if (!peer) {
  4925. ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
  4926. vif->addr, arvif->vdev_id);
  4927. spin_unlock_bh(&ar->data_lock);
  4928. ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4929. ath10k_mac_dec_num_stations(arvif, sta);
  4930. ret = -ENOENT;
  4931. goto exit;
  4932. }
  4933. arsta->peer_id = find_first_bit(peer->peer_ids,
  4934. ATH10K_MAX_NUM_PEER_IDS);
  4935. spin_unlock_bh(&ar->data_lock);
  4936. if (!sta->tdls)
  4937. goto exit;
  4938. num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
  4939. num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
  4940. if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
  4941. num_tdls_stations == 0) {
  4942. ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
  4943. arvif->vdev_id, ar->max_num_tdls_vdevs);
  4944. ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4945. ath10k_mac_dec_num_stations(arvif, sta);
  4946. ret = -ENOBUFS;
  4947. goto exit;
  4948. }
  4949. if (num_tdls_stations == 0) {
  4950. /* This is the first tdls peer in current vif */
  4951. enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
  4952. ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  4953. state);
  4954. if (ret) {
  4955. ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
  4956. arvif->vdev_id, ret);
  4957. ath10k_peer_delete(ar, arvif->vdev_id,
  4958. sta->addr);
  4959. ath10k_mac_dec_num_stations(arvif, sta);
  4960. goto exit;
  4961. }
  4962. }
  4963. ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
  4964. WMI_TDLS_PEER_STATE_PEERING);
  4965. if (ret) {
  4966. ath10k_warn(ar,
  4967. "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
  4968. sta->addr, arvif->vdev_id, ret);
  4969. ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4970. ath10k_mac_dec_num_stations(arvif, sta);
  4971. if (num_tdls_stations != 0)
  4972. goto exit;
  4973. ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  4974. WMI_TDLS_DISABLE);
  4975. }
  4976. } else if ((old_state == IEEE80211_STA_NONE &&
  4977. new_state == IEEE80211_STA_NOTEXIST)) {
  4978. /*
  4979. * Existing station deletion.
  4980. */
  4981. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4982. "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
  4983. arvif->vdev_id, sta->addr, sta);
  4984. ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4985. if (ret)
  4986. ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
  4987. sta->addr, arvif->vdev_id, ret);
  4988. ath10k_mac_dec_num_stations(arvif, sta);
  4989. spin_lock_bh(&ar->data_lock);
  4990. for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
  4991. peer = ar->peer_map[i];
  4992. if (!peer)
  4993. continue;
  4994. if (peer->sta == sta) {
  4995. ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
  4996. sta->addr, peer, i, arvif->vdev_id);
  4997. peer->sta = NULL;
  4998. /* Clean up the peer object as well since we
  4999. * must have failed to do this above.
  5000. */
  5001. list_del(&peer->list);
  5002. ar->peer_map[i] = NULL;
  5003. kfree(peer);
  5004. ar->num_peers--;
  5005. }
  5006. }
  5007. spin_unlock_bh(&ar->data_lock);
  5008. for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
  5009. ath10k_mac_txq_unref(ar, sta->txq[i]);
  5010. if (!sta->tdls)
  5011. goto exit;
  5012. if (ath10k_mac_tdls_vif_stations_count(hw, vif))
  5013. goto exit;
  5014. /* This was the last tdls peer in current vif */
  5015. ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  5016. WMI_TDLS_DISABLE);
  5017. if (ret) {
  5018. ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
  5019. arvif->vdev_id, ret);
  5020. }
  5021. } else if (old_state == IEEE80211_STA_AUTH &&
  5022. new_state == IEEE80211_STA_ASSOC &&
  5023. (vif->type == NL80211_IFTYPE_AP ||
  5024. vif->type == NL80211_IFTYPE_MESH_POINT ||
  5025. vif->type == NL80211_IFTYPE_ADHOC)) {
  5026. /*
  5027. * New association.
  5028. */
  5029. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
  5030. sta->addr);
  5031. ret = ath10k_station_assoc(ar, vif, sta, false);
  5032. if (ret)
  5033. ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
  5034. sta->addr, arvif->vdev_id, ret);
  5035. } else if (old_state == IEEE80211_STA_ASSOC &&
  5036. new_state == IEEE80211_STA_AUTHORIZED &&
  5037. sta->tdls) {
  5038. /*
  5039. * Tdls station authorized.
  5040. */
  5041. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
  5042. sta->addr);
  5043. ret = ath10k_station_assoc(ar, vif, sta, false);
  5044. if (ret) {
  5045. ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
  5046. sta->addr, arvif->vdev_id, ret);
  5047. goto exit;
  5048. }
  5049. ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
  5050. WMI_TDLS_PEER_STATE_CONNECTED);
  5051. if (ret)
  5052. ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
  5053. sta->addr, arvif->vdev_id, ret);
  5054. } else if (old_state == IEEE80211_STA_ASSOC &&
  5055. new_state == IEEE80211_STA_AUTH &&
  5056. (vif->type == NL80211_IFTYPE_AP ||
  5057. vif->type == NL80211_IFTYPE_MESH_POINT ||
  5058. vif->type == NL80211_IFTYPE_ADHOC)) {
  5059. /*
  5060. * Disassociation.
  5061. */
  5062. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
  5063. sta->addr);
  5064. ret = ath10k_station_disassoc(ar, vif, sta);
  5065. if (ret)
  5066. ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
  5067. sta->addr, arvif->vdev_id, ret);
  5068. }
  5069. exit:
  5070. mutex_unlock(&ar->conf_mutex);
  5071. return ret;
  5072. }
  5073. static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
  5074. u16 ac, bool enable)
  5075. {
  5076. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5077. struct wmi_sta_uapsd_auto_trig_arg arg = {};
  5078. u32 prio = 0, acc = 0;
  5079. u32 value = 0;
  5080. int ret = 0;
  5081. lockdep_assert_held(&ar->conf_mutex);
  5082. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  5083. return 0;
  5084. switch (ac) {
  5085. case IEEE80211_AC_VO:
  5086. value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
  5087. WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
  5088. prio = 7;
  5089. acc = 3;
  5090. break;
  5091. case IEEE80211_AC_VI:
  5092. value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
  5093. WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
  5094. prio = 5;
  5095. acc = 2;
  5096. break;
  5097. case IEEE80211_AC_BE:
  5098. value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
  5099. WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
  5100. prio = 2;
  5101. acc = 1;
  5102. break;
  5103. case IEEE80211_AC_BK:
  5104. value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
  5105. WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
  5106. prio = 0;
  5107. acc = 0;
  5108. break;
  5109. }
  5110. if (enable)
  5111. arvif->u.sta.uapsd |= value;
  5112. else
  5113. arvif->u.sta.uapsd &= ~value;
  5114. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  5115. WMI_STA_PS_PARAM_UAPSD,
  5116. arvif->u.sta.uapsd);
  5117. if (ret) {
  5118. ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
  5119. goto exit;
  5120. }
  5121. if (arvif->u.sta.uapsd)
  5122. value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
  5123. else
  5124. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  5125. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  5126. WMI_STA_PS_PARAM_RX_WAKE_POLICY,
  5127. value);
  5128. if (ret)
  5129. ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
  5130. ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
  5131. if (ret) {
  5132. ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
  5133. arvif->vdev_id, ret);
  5134. return ret;
  5135. }
  5136. ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
  5137. if (ret) {
  5138. ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
  5139. arvif->vdev_id, ret);
  5140. return ret;
  5141. }
  5142. if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
  5143. test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
  5144. /* Only userspace can make an educated decision when to send
  5145. * trigger frame. The following effectively disables u-UAPSD
  5146. * autotrigger in firmware (which is enabled by default
  5147. * provided the autotrigger service is available).
  5148. */
  5149. arg.wmm_ac = acc;
  5150. arg.user_priority = prio;
  5151. arg.service_interval = 0;
  5152. arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
  5153. arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
  5154. ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
  5155. arvif->bssid, &arg, 1);
  5156. if (ret) {
  5157. ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
  5158. ret);
  5159. return ret;
  5160. }
  5161. }
  5162. exit:
  5163. return ret;
  5164. }
  5165. static int ath10k_conf_tx(struct ieee80211_hw *hw,
  5166. struct ieee80211_vif *vif, u16 ac,
  5167. const struct ieee80211_tx_queue_params *params)
  5168. {
  5169. struct ath10k *ar = hw->priv;
  5170. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5171. struct wmi_wmm_params_arg *p = NULL;
  5172. int ret;
  5173. mutex_lock(&ar->conf_mutex);
  5174. switch (ac) {
  5175. case IEEE80211_AC_VO:
  5176. p = &arvif->wmm_params.ac_vo;
  5177. break;
  5178. case IEEE80211_AC_VI:
  5179. p = &arvif->wmm_params.ac_vi;
  5180. break;
  5181. case IEEE80211_AC_BE:
  5182. p = &arvif->wmm_params.ac_be;
  5183. break;
  5184. case IEEE80211_AC_BK:
  5185. p = &arvif->wmm_params.ac_bk;
  5186. break;
  5187. }
  5188. if (WARN_ON(!p)) {
  5189. ret = -EINVAL;
  5190. goto exit;
  5191. }
  5192. p->cwmin = params->cw_min;
  5193. p->cwmax = params->cw_max;
  5194. p->aifs = params->aifs;
  5195. /*
  5196. * The channel time duration programmed in the HW is in absolute
  5197. * microseconds, while mac80211 gives the txop in units of
  5198. * 32 microseconds.
  5199. */
  5200. p->txop = params->txop * 32;
  5201. if (ar->wmi.ops->gen_vdev_wmm_conf) {
  5202. ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
  5203. &arvif->wmm_params);
  5204. if (ret) {
  5205. ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
  5206. arvif->vdev_id, ret);
  5207. goto exit;
  5208. }
  5209. } else {
  5210. /* This won't work well with multi-interface cases but it's
  5211. * better than nothing.
  5212. */
  5213. ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
  5214. if (ret) {
  5215. ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
  5216. goto exit;
  5217. }
  5218. }
  5219. ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
  5220. if (ret)
  5221. ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
  5222. exit:
  5223. mutex_unlock(&ar->conf_mutex);
  5224. return ret;
  5225. }
  5226. #define ATH10K_ROC_TIMEOUT_HZ (2 * HZ)
  5227. static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
  5228. struct ieee80211_vif *vif,
  5229. struct ieee80211_channel *chan,
  5230. int duration,
  5231. enum ieee80211_roc_type type)
  5232. {
  5233. struct ath10k *ar = hw->priv;
  5234. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5235. struct wmi_start_scan_arg arg;
  5236. int ret = 0;
  5237. u32 scan_time_msec;
  5238. mutex_lock(&ar->conf_mutex);
  5239. spin_lock_bh(&ar->data_lock);
  5240. switch (ar->scan.state) {
  5241. case ATH10K_SCAN_IDLE:
  5242. reinit_completion(&ar->scan.started);
  5243. reinit_completion(&ar->scan.completed);
  5244. reinit_completion(&ar->scan.on_channel);
  5245. ar->scan.state = ATH10K_SCAN_STARTING;
  5246. ar->scan.is_roc = true;
  5247. ar->scan.vdev_id = arvif->vdev_id;
  5248. ar->scan.roc_freq = chan->center_freq;
  5249. ar->scan.roc_notify = true;
  5250. ret = 0;
  5251. break;
  5252. case ATH10K_SCAN_STARTING:
  5253. case ATH10K_SCAN_RUNNING:
  5254. case ATH10K_SCAN_ABORTING:
  5255. ret = -EBUSY;
  5256. break;
  5257. }
  5258. spin_unlock_bh(&ar->data_lock);
  5259. if (ret)
  5260. goto exit;
  5261. scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
  5262. memset(&arg, 0, sizeof(arg));
  5263. ath10k_wmi_start_scan_init(ar, &arg);
  5264. arg.vdev_id = arvif->vdev_id;
  5265. arg.scan_id = ATH10K_SCAN_ID;
  5266. arg.n_channels = 1;
  5267. arg.channels[0] = chan->center_freq;
  5268. arg.dwell_time_active = scan_time_msec;
  5269. arg.dwell_time_passive = scan_time_msec;
  5270. arg.max_scan_time = scan_time_msec;
  5271. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  5272. arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
  5273. arg.burst_duration_ms = duration;
  5274. ret = ath10k_start_scan(ar, &arg);
  5275. if (ret) {
  5276. ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
  5277. spin_lock_bh(&ar->data_lock);
  5278. ar->scan.state = ATH10K_SCAN_IDLE;
  5279. spin_unlock_bh(&ar->data_lock);
  5280. goto exit;
  5281. }
  5282. ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
  5283. if (ret == 0) {
  5284. ath10k_warn(ar, "failed to switch to channel for roc scan\n");
  5285. ret = ath10k_scan_stop(ar);
  5286. if (ret)
  5287. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  5288. ret = -ETIMEDOUT;
  5289. goto exit;
  5290. }
  5291. ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
  5292. msecs_to_jiffies(duration));
  5293. ret = 0;
  5294. exit:
  5295. mutex_unlock(&ar->conf_mutex);
  5296. return ret;
  5297. }
  5298. static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  5299. {
  5300. struct ath10k *ar = hw->priv;
  5301. mutex_lock(&ar->conf_mutex);
  5302. spin_lock_bh(&ar->data_lock);
  5303. ar->scan.roc_notify = false;
  5304. spin_unlock_bh(&ar->data_lock);
  5305. ath10k_scan_abort(ar);
  5306. mutex_unlock(&ar->conf_mutex);
  5307. cancel_delayed_work_sync(&ar->scan.timeout);
  5308. return 0;
  5309. }
  5310. /*
  5311. * Both RTS and Fragmentation threshold are interface-specific
  5312. * in ath10k, but device-specific in mac80211.
  5313. */
  5314. static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  5315. {
  5316. struct ath10k *ar = hw->priv;
  5317. struct ath10k_vif *arvif;
  5318. int ret = 0;
  5319. mutex_lock(&ar->conf_mutex);
  5320. list_for_each_entry(arvif, &ar->arvifs, list) {
  5321. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
  5322. arvif->vdev_id, value);
  5323. ret = ath10k_mac_set_rts(arvif, value);
  5324. if (ret) {
  5325. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  5326. arvif->vdev_id, ret);
  5327. break;
  5328. }
  5329. }
  5330. mutex_unlock(&ar->conf_mutex);
  5331. return ret;
  5332. }
  5333. static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  5334. {
  5335. /* Even though there's a WMI enum for fragmentation threshold no known
  5336. * firmware actually implements it. Moreover it is not possible to rely
  5337. * frame fragmentation to mac80211 because firmware clears the "more
  5338. * fragments" bit in frame control making it impossible for remote
  5339. * devices to reassemble frames.
  5340. *
  5341. * Hence implement a dummy callback just to say fragmentation isn't
  5342. * supported. This effectively prevents mac80211 from doing frame
  5343. * fragmentation in software.
  5344. */
  5345. return -EOPNOTSUPP;
  5346. }
  5347. static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  5348. u32 queues, bool drop)
  5349. {
  5350. struct ath10k *ar = hw->priv;
  5351. bool skip;
  5352. long time_left;
  5353. /* mac80211 doesn't care if we really xmit queued frames or not
  5354. * we'll collect those frames either way if we stop/delete vdevs */
  5355. if (drop)
  5356. return;
  5357. mutex_lock(&ar->conf_mutex);
  5358. if (ar->state == ATH10K_STATE_WEDGED)
  5359. goto skip;
  5360. time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
  5361. bool empty;
  5362. spin_lock_bh(&ar->htt.tx_lock);
  5363. empty = (ar->htt.num_pending_tx == 0);
  5364. spin_unlock_bh(&ar->htt.tx_lock);
  5365. skip = (ar->state == ATH10K_STATE_WEDGED) ||
  5366. test_bit(ATH10K_FLAG_CRASH_FLUSH,
  5367. &ar->dev_flags);
  5368. (empty || skip);
  5369. }), ATH10K_FLUSH_TIMEOUT_HZ);
  5370. if (time_left == 0 || skip)
  5371. ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
  5372. skip, ar->state, time_left);
  5373. skip:
  5374. mutex_unlock(&ar->conf_mutex);
  5375. }
  5376. /* TODO: Implement this function properly
  5377. * For now it is needed to reply to Probe Requests in IBSS mode.
  5378. * Propably we need this information from FW.
  5379. */
  5380. static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
  5381. {
  5382. return 1;
  5383. }
  5384. static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
  5385. enum ieee80211_reconfig_type reconfig_type)
  5386. {
  5387. struct ath10k *ar = hw->priv;
  5388. if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
  5389. return;
  5390. mutex_lock(&ar->conf_mutex);
  5391. /* If device failed to restart it will be in a different state, e.g.
  5392. * ATH10K_STATE_WEDGED */
  5393. if (ar->state == ATH10K_STATE_RESTARTED) {
  5394. ath10k_info(ar, "device successfully recovered\n");
  5395. ar->state = ATH10K_STATE_ON;
  5396. ieee80211_wake_queues(ar->hw);
  5397. }
  5398. mutex_unlock(&ar->conf_mutex);
  5399. }
  5400. static void
  5401. ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
  5402. struct ieee80211_channel *channel)
  5403. {
  5404. int ret;
  5405. enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ_CLEAR;
  5406. lockdep_assert_held(&ar->conf_mutex);
  5407. if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
  5408. (ar->rx_channel != channel))
  5409. return;
  5410. if (ar->scan.state != ATH10K_SCAN_IDLE) {
  5411. ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
  5412. return;
  5413. }
  5414. reinit_completion(&ar->bss_survey_done);
  5415. ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
  5416. if (ret) {
  5417. ath10k_warn(ar, "failed to send pdev bss chan info request\n");
  5418. return;
  5419. }
  5420. ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
  5421. if (!ret) {
  5422. ath10k_warn(ar, "bss channel survey timed out\n");
  5423. return;
  5424. }
  5425. }
  5426. static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
  5427. struct survey_info *survey)
  5428. {
  5429. struct ath10k *ar = hw->priv;
  5430. struct ieee80211_supported_band *sband;
  5431. struct survey_info *ar_survey = &ar->survey[idx];
  5432. int ret = 0;
  5433. mutex_lock(&ar->conf_mutex);
  5434. sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
  5435. if (sband && idx >= sband->n_channels) {
  5436. idx -= sband->n_channels;
  5437. sband = NULL;
  5438. }
  5439. if (!sband)
  5440. sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
  5441. if (!sband || idx >= sband->n_channels) {
  5442. ret = -ENOENT;
  5443. goto exit;
  5444. }
  5445. ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
  5446. spin_lock_bh(&ar->data_lock);
  5447. memcpy(survey, ar_survey, sizeof(*survey));
  5448. spin_unlock_bh(&ar->data_lock);
  5449. survey->channel = &sband->channels[idx];
  5450. if (ar->rx_channel == survey->channel)
  5451. survey->filled |= SURVEY_INFO_IN_USE;
  5452. exit:
  5453. mutex_unlock(&ar->conf_mutex);
  5454. return ret;
  5455. }
  5456. static bool
  5457. ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
  5458. enum nl80211_band band,
  5459. const struct cfg80211_bitrate_mask *mask)
  5460. {
  5461. int num_rates = 0;
  5462. int i;
  5463. num_rates += hweight32(mask->control[band].legacy);
  5464. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
  5465. num_rates += hweight8(mask->control[band].ht_mcs[i]);
  5466. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
  5467. num_rates += hweight16(mask->control[band].vht_mcs[i]);
  5468. return num_rates == 1;
  5469. }
  5470. static bool
  5471. ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
  5472. enum nl80211_band band,
  5473. const struct cfg80211_bitrate_mask *mask,
  5474. int *nss)
  5475. {
  5476. struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
  5477. u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
  5478. u8 ht_nss_mask = 0;
  5479. u8 vht_nss_mask = 0;
  5480. int i;
  5481. if (mask->control[band].legacy)
  5482. return false;
  5483. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
  5484. if (mask->control[band].ht_mcs[i] == 0)
  5485. continue;
  5486. else if (mask->control[band].ht_mcs[i] ==
  5487. sband->ht_cap.mcs.rx_mask[i])
  5488. ht_nss_mask |= BIT(i);
  5489. else
  5490. return false;
  5491. }
  5492. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
  5493. if (mask->control[band].vht_mcs[i] == 0)
  5494. continue;
  5495. else if (mask->control[band].vht_mcs[i] ==
  5496. ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
  5497. vht_nss_mask |= BIT(i);
  5498. else
  5499. return false;
  5500. }
  5501. if (ht_nss_mask != vht_nss_mask)
  5502. return false;
  5503. if (ht_nss_mask == 0)
  5504. return false;
  5505. if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
  5506. return false;
  5507. *nss = fls(ht_nss_mask);
  5508. return true;
  5509. }
  5510. static int
  5511. ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
  5512. enum nl80211_band band,
  5513. const struct cfg80211_bitrate_mask *mask,
  5514. u8 *rate, u8 *nss)
  5515. {
  5516. struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
  5517. int rate_idx;
  5518. int i;
  5519. u16 bitrate;
  5520. u8 preamble;
  5521. u8 hw_rate;
  5522. if (hweight32(mask->control[band].legacy) == 1) {
  5523. rate_idx = ffs(mask->control[band].legacy) - 1;
  5524. hw_rate = sband->bitrates[rate_idx].hw_value;
  5525. bitrate = sband->bitrates[rate_idx].bitrate;
  5526. if (ath10k_mac_bitrate_is_cck(bitrate))
  5527. preamble = WMI_RATE_PREAMBLE_CCK;
  5528. else
  5529. preamble = WMI_RATE_PREAMBLE_OFDM;
  5530. *nss = 1;
  5531. *rate = preamble << 6 |
  5532. (*nss - 1) << 4 |
  5533. hw_rate << 0;
  5534. return 0;
  5535. }
  5536. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
  5537. if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
  5538. *nss = i + 1;
  5539. *rate = WMI_RATE_PREAMBLE_HT << 6 |
  5540. (*nss - 1) << 4 |
  5541. (ffs(mask->control[band].ht_mcs[i]) - 1);
  5542. return 0;
  5543. }
  5544. }
  5545. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
  5546. if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
  5547. *nss = i + 1;
  5548. *rate = WMI_RATE_PREAMBLE_VHT << 6 |
  5549. (*nss - 1) << 4 |
  5550. (ffs(mask->control[band].vht_mcs[i]) - 1);
  5551. return 0;
  5552. }
  5553. }
  5554. return -EINVAL;
  5555. }
  5556. static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
  5557. u8 rate, u8 nss, u8 sgi, u8 ldpc)
  5558. {
  5559. struct ath10k *ar = arvif->ar;
  5560. u32 vdev_param;
  5561. int ret;
  5562. lockdep_assert_held(&ar->conf_mutex);
  5563. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
  5564. arvif->vdev_id, rate, nss, sgi);
  5565. vdev_param = ar->wmi.vdev_param->fixed_rate;
  5566. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
  5567. if (ret) {
  5568. ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
  5569. rate, ret);
  5570. return ret;
  5571. }
  5572. vdev_param = ar->wmi.vdev_param->nss;
  5573. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
  5574. if (ret) {
  5575. ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
  5576. return ret;
  5577. }
  5578. vdev_param = ar->wmi.vdev_param->sgi;
  5579. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
  5580. if (ret) {
  5581. ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
  5582. return ret;
  5583. }
  5584. vdev_param = ar->wmi.vdev_param->ldpc;
  5585. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
  5586. if (ret) {
  5587. ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
  5588. return ret;
  5589. }
  5590. return 0;
  5591. }
  5592. static bool
  5593. ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
  5594. enum nl80211_band band,
  5595. const struct cfg80211_bitrate_mask *mask)
  5596. {
  5597. int i;
  5598. u16 vht_mcs;
  5599. /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
  5600. * to express all VHT MCS rate masks. Effectively only the following
  5601. * ranges can be used: none, 0-7, 0-8 and 0-9.
  5602. */
  5603. for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
  5604. vht_mcs = mask->control[band].vht_mcs[i];
  5605. switch (vht_mcs) {
  5606. case 0:
  5607. case BIT(8) - 1:
  5608. case BIT(9) - 1:
  5609. case BIT(10) - 1:
  5610. break;
  5611. default:
  5612. ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
  5613. return false;
  5614. }
  5615. }
  5616. return true;
  5617. }
  5618. static void ath10k_mac_set_bitrate_mask_iter(void *data,
  5619. struct ieee80211_sta *sta)
  5620. {
  5621. struct ath10k_vif *arvif = data;
  5622. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  5623. struct ath10k *ar = arvif->ar;
  5624. if (arsta->arvif != arvif)
  5625. return;
  5626. spin_lock_bh(&ar->data_lock);
  5627. arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
  5628. spin_unlock_bh(&ar->data_lock);
  5629. ieee80211_queue_work(ar->hw, &arsta->update_wk);
  5630. }
  5631. static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
  5632. struct ieee80211_vif *vif,
  5633. const struct cfg80211_bitrate_mask *mask)
  5634. {
  5635. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5636. struct cfg80211_chan_def def;
  5637. struct ath10k *ar = arvif->ar;
  5638. enum nl80211_band band;
  5639. const u8 *ht_mcs_mask;
  5640. const u16 *vht_mcs_mask;
  5641. u8 rate;
  5642. u8 nss;
  5643. u8 sgi;
  5644. u8 ldpc;
  5645. int single_nss;
  5646. int ret;
  5647. if (ath10k_mac_vif_chan(vif, &def))
  5648. return -EPERM;
  5649. band = def.chan->band;
  5650. ht_mcs_mask = mask->control[band].ht_mcs;
  5651. vht_mcs_mask = mask->control[band].vht_mcs;
  5652. ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
  5653. sgi = mask->control[band].gi;
  5654. if (sgi == NL80211_TXRATE_FORCE_LGI)
  5655. return -EINVAL;
  5656. if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
  5657. ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
  5658. &rate, &nss);
  5659. if (ret) {
  5660. ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
  5661. arvif->vdev_id, ret);
  5662. return ret;
  5663. }
  5664. } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
  5665. &single_nss)) {
  5666. rate = WMI_FIXED_RATE_NONE;
  5667. nss = single_nss;
  5668. } else {
  5669. rate = WMI_FIXED_RATE_NONE;
  5670. nss = min(ar->num_rf_chains,
  5671. max(ath10k_mac_max_ht_nss(ht_mcs_mask),
  5672. ath10k_mac_max_vht_nss(vht_mcs_mask)));
  5673. if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
  5674. return -EINVAL;
  5675. mutex_lock(&ar->conf_mutex);
  5676. arvif->bitrate_mask = *mask;
  5677. ieee80211_iterate_stations_atomic(ar->hw,
  5678. ath10k_mac_set_bitrate_mask_iter,
  5679. arvif);
  5680. mutex_unlock(&ar->conf_mutex);
  5681. }
  5682. mutex_lock(&ar->conf_mutex);
  5683. ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
  5684. if (ret) {
  5685. ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
  5686. arvif->vdev_id, ret);
  5687. goto exit;
  5688. }
  5689. exit:
  5690. mutex_unlock(&ar->conf_mutex);
  5691. return ret;
  5692. }
  5693. static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
  5694. struct ieee80211_vif *vif,
  5695. struct ieee80211_sta *sta,
  5696. u32 changed)
  5697. {
  5698. struct ath10k *ar = hw->priv;
  5699. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  5700. u32 bw, smps;
  5701. spin_lock_bh(&ar->data_lock);
  5702. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5703. "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
  5704. sta->addr, changed, sta->bandwidth, sta->rx_nss,
  5705. sta->smps_mode);
  5706. if (changed & IEEE80211_RC_BW_CHANGED) {
  5707. bw = WMI_PEER_CHWIDTH_20MHZ;
  5708. switch (sta->bandwidth) {
  5709. case IEEE80211_STA_RX_BW_20:
  5710. bw = WMI_PEER_CHWIDTH_20MHZ;
  5711. break;
  5712. case IEEE80211_STA_RX_BW_40:
  5713. bw = WMI_PEER_CHWIDTH_40MHZ;
  5714. break;
  5715. case IEEE80211_STA_RX_BW_80:
  5716. bw = WMI_PEER_CHWIDTH_80MHZ;
  5717. break;
  5718. case IEEE80211_STA_RX_BW_160:
  5719. bw = WMI_PEER_CHWIDTH_160MHZ;
  5720. break;
  5721. default:
  5722. ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
  5723. sta->bandwidth, sta->addr);
  5724. bw = WMI_PEER_CHWIDTH_20MHZ;
  5725. break;
  5726. }
  5727. arsta->bw = bw;
  5728. }
  5729. if (changed & IEEE80211_RC_NSS_CHANGED)
  5730. arsta->nss = sta->rx_nss;
  5731. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  5732. smps = WMI_PEER_SMPS_PS_NONE;
  5733. switch (sta->smps_mode) {
  5734. case IEEE80211_SMPS_AUTOMATIC:
  5735. case IEEE80211_SMPS_OFF:
  5736. smps = WMI_PEER_SMPS_PS_NONE;
  5737. break;
  5738. case IEEE80211_SMPS_STATIC:
  5739. smps = WMI_PEER_SMPS_STATIC;
  5740. break;
  5741. case IEEE80211_SMPS_DYNAMIC:
  5742. smps = WMI_PEER_SMPS_DYNAMIC;
  5743. break;
  5744. case IEEE80211_SMPS_NUM_MODES:
  5745. ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
  5746. sta->smps_mode, sta->addr);
  5747. smps = WMI_PEER_SMPS_PS_NONE;
  5748. break;
  5749. }
  5750. arsta->smps = smps;
  5751. }
  5752. arsta->changed |= changed;
  5753. spin_unlock_bh(&ar->data_lock);
  5754. ieee80211_queue_work(hw, &arsta->update_wk);
  5755. }
  5756. static void ath10k_offset_tsf(struct ieee80211_hw *hw,
  5757. struct ieee80211_vif *vif, s64 tsf_offset)
  5758. {
  5759. struct ath10k *ar = hw->priv;
  5760. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5761. u32 offset, vdev_param;
  5762. int ret;
  5763. if (tsf_offset < 0) {
  5764. vdev_param = ar->wmi.vdev_param->dec_tsf;
  5765. offset = -tsf_offset;
  5766. } else {
  5767. vdev_param = ar->wmi.vdev_param->inc_tsf;
  5768. offset = tsf_offset;
  5769. }
  5770. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  5771. vdev_param, offset);
  5772. if (ret && ret != -EOPNOTSUPP)
  5773. ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
  5774. offset, vdev_param, ret);
  5775. }
  5776. static int ath10k_ampdu_action(struct ieee80211_hw *hw,
  5777. struct ieee80211_vif *vif,
  5778. struct ieee80211_ampdu_params *params)
  5779. {
  5780. struct ath10k *ar = hw->priv;
  5781. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5782. struct ieee80211_sta *sta = params->sta;
  5783. enum ieee80211_ampdu_mlme_action action = params->action;
  5784. u16 tid = params->tid;
  5785. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
  5786. arvif->vdev_id, sta->addr, tid, action);
  5787. switch (action) {
  5788. case IEEE80211_AMPDU_RX_START:
  5789. case IEEE80211_AMPDU_RX_STOP:
  5790. /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
  5791. * creation/removal. Do we need to verify this?
  5792. */
  5793. return 0;
  5794. case IEEE80211_AMPDU_TX_START:
  5795. case IEEE80211_AMPDU_TX_STOP_CONT:
  5796. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  5797. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  5798. case IEEE80211_AMPDU_TX_OPERATIONAL:
  5799. /* Firmware offloads Tx aggregation entirely so deny mac80211
  5800. * Tx aggregation requests.
  5801. */
  5802. return -EOPNOTSUPP;
  5803. }
  5804. return -EINVAL;
  5805. }
  5806. static void
  5807. ath10k_mac_update_rx_channel(struct ath10k *ar,
  5808. struct ieee80211_chanctx_conf *ctx,
  5809. struct ieee80211_vif_chanctx_switch *vifs,
  5810. int n_vifs)
  5811. {
  5812. struct cfg80211_chan_def *def = NULL;
  5813. /* Both locks are required because ar->rx_channel is modified. This
  5814. * allows readers to hold either lock.
  5815. */
  5816. lockdep_assert_held(&ar->conf_mutex);
  5817. lockdep_assert_held(&ar->data_lock);
  5818. WARN_ON(ctx && vifs);
  5819. WARN_ON(vifs && n_vifs != 1);
  5820. /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
  5821. * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
  5822. * ppdu on Rx may reduce performance on low-end systems. It should be
  5823. * possible to make tables/hashmaps to speed the lookup up (be vary of
  5824. * cpu data cache lines though regarding sizes) but to keep the initial
  5825. * implementation simple and less intrusive fallback to the slow lookup
  5826. * only for multi-channel cases. Single-channel cases will remain to
  5827. * use the old channel derival and thus performance should not be
  5828. * affected much.
  5829. */
  5830. rcu_read_lock();
  5831. if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
  5832. ieee80211_iter_chan_contexts_atomic(ar->hw,
  5833. ath10k_mac_get_any_chandef_iter,
  5834. &def);
  5835. if (vifs)
  5836. def = &vifs[0].new_ctx->def;
  5837. ar->rx_channel = def->chan;
  5838. } else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
  5839. (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
  5840. /* During driver restart due to firmware assert, since mac80211
  5841. * already has valid channel context for given radio, channel
  5842. * context iteration return num_chanctx > 0. So fix rx_channel
  5843. * when restart is in progress.
  5844. */
  5845. ar->rx_channel = ctx->def.chan;
  5846. } else {
  5847. ar->rx_channel = NULL;
  5848. }
  5849. rcu_read_unlock();
  5850. }
  5851. static void
  5852. ath10k_mac_update_vif_chan(struct ath10k *ar,
  5853. struct ieee80211_vif_chanctx_switch *vifs,
  5854. int n_vifs)
  5855. {
  5856. struct ath10k_vif *arvif;
  5857. int ret;
  5858. int i;
  5859. lockdep_assert_held(&ar->conf_mutex);
  5860. /* First stop monitor interface. Some FW versions crash if there's a
  5861. * lone monitor interface.
  5862. */
  5863. if (ar->monitor_started)
  5864. ath10k_monitor_stop(ar);
  5865. for (i = 0; i < n_vifs; i++) {
  5866. arvif = (void *)vifs[i].vif->drv_priv;
  5867. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5868. "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
  5869. arvif->vdev_id,
  5870. vifs[i].old_ctx->def.chan->center_freq,
  5871. vifs[i].new_ctx->def.chan->center_freq,
  5872. vifs[i].old_ctx->def.width,
  5873. vifs[i].new_ctx->def.width);
  5874. if (WARN_ON(!arvif->is_started))
  5875. continue;
  5876. if (WARN_ON(!arvif->is_up))
  5877. continue;
  5878. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  5879. if (ret) {
  5880. ath10k_warn(ar, "failed to down vdev %d: %d\n",
  5881. arvif->vdev_id, ret);
  5882. continue;
  5883. }
  5884. }
  5885. /* All relevant vdevs are downed and associated channel resources
  5886. * should be available for the channel switch now.
  5887. */
  5888. spin_lock_bh(&ar->data_lock);
  5889. ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
  5890. spin_unlock_bh(&ar->data_lock);
  5891. for (i = 0; i < n_vifs; i++) {
  5892. arvif = (void *)vifs[i].vif->drv_priv;
  5893. if (WARN_ON(!arvif->is_started))
  5894. continue;
  5895. if (WARN_ON(!arvif->is_up))
  5896. continue;
  5897. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  5898. if (ret)
  5899. ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
  5900. ret);
  5901. ret = ath10k_mac_setup_prb_tmpl(arvif);
  5902. if (ret)
  5903. ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
  5904. ret);
  5905. ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
  5906. if (ret) {
  5907. ath10k_warn(ar, "failed to restart vdev %d: %d\n",
  5908. arvif->vdev_id, ret);
  5909. continue;
  5910. }
  5911. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  5912. arvif->bssid);
  5913. if (ret) {
  5914. ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
  5915. arvif->vdev_id, ret);
  5916. continue;
  5917. }
  5918. }
  5919. ath10k_monitor_recalc(ar);
  5920. }
  5921. static int
  5922. ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
  5923. struct ieee80211_chanctx_conf *ctx)
  5924. {
  5925. struct ath10k *ar = hw->priv;
  5926. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5927. "mac chanctx add freq %hu width %d ptr %pK\n",
  5928. ctx->def.chan->center_freq, ctx->def.width, ctx);
  5929. mutex_lock(&ar->conf_mutex);
  5930. spin_lock_bh(&ar->data_lock);
  5931. ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
  5932. spin_unlock_bh(&ar->data_lock);
  5933. ath10k_recalc_radar_detection(ar);
  5934. ath10k_monitor_recalc(ar);
  5935. mutex_unlock(&ar->conf_mutex);
  5936. return 0;
  5937. }
  5938. static void
  5939. ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
  5940. struct ieee80211_chanctx_conf *ctx)
  5941. {
  5942. struct ath10k *ar = hw->priv;
  5943. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5944. "mac chanctx remove freq %hu width %d ptr %pK\n",
  5945. ctx->def.chan->center_freq, ctx->def.width, ctx);
  5946. mutex_lock(&ar->conf_mutex);
  5947. spin_lock_bh(&ar->data_lock);
  5948. ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
  5949. spin_unlock_bh(&ar->data_lock);
  5950. ath10k_recalc_radar_detection(ar);
  5951. ath10k_monitor_recalc(ar);
  5952. mutex_unlock(&ar->conf_mutex);
  5953. }
  5954. struct ath10k_mac_change_chanctx_arg {
  5955. struct ieee80211_chanctx_conf *ctx;
  5956. struct ieee80211_vif_chanctx_switch *vifs;
  5957. int n_vifs;
  5958. int next_vif;
  5959. };
  5960. static void
  5961. ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
  5962. struct ieee80211_vif *vif)
  5963. {
  5964. struct ath10k_mac_change_chanctx_arg *arg = data;
  5965. if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
  5966. return;
  5967. arg->n_vifs++;
  5968. }
  5969. static void
  5970. ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
  5971. struct ieee80211_vif *vif)
  5972. {
  5973. struct ath10k_mac_change_chanctx_arg *arg = data;
  5974. struct ieee80211_chanctx_conf *ctx;
  5975. ctx = rcu_access_pointer(vif->chanctx_conf);
  5976. if (ctx != arg->ctx)
  5977. return;
  5978. if (WARN_ON(arg->next_vif == arg->n_vifs))
  5979. return;
  5980. arg->vifs[arg->next_vif].vif = vif;
  5981. arg->vifs[arg->next_vif].old_ctx = ctx;
  5982. arg->vifs[arg->next_vif].new_ctx = ctx;
  5983. arg->next_vif++;
  5984. }
  5985. static void
  5986. ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
  5987. struct ieee80211_chanctx_conf *ctx,
  5988. u32 changed)
  5989. {
  5990. struct ath10k *ar = hw->priv;
  5991. struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
  5992. mutex_lock(&ar->conf_mutex);
  5993. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5994. "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
  5995. ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
  5996. /* This shouldn't really happen because channel switching should use
  5997. * switch_vif_chanctx().
  5998. */
  5999. if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
  6000. goto unlock;
  6001. if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
  6002. ieee80211_iterate_active_interfaces_atomic(
  6003. hw,
  6004. IEEE80211_IFACE_ITER_NORMAL,
  6005. ath10k_mac_change_chanctx_cnt_iter,
  6006. &arg);
  6007. if (arg.n_vifs == 0)
  6008. goto radar;
  6009. arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
  6010. GFP_KERNEL);
  6011. if (!arg.vifs)
  6012. goto radar;
  6013. ieee80211_iterate_active_interfaces_atomic(
  6014. hw,
  6015. IEEE80211_IFACE_ITER_NORMAL,
  6016. ath10k_mac_change_chanctx_fill_iter,
  6017. &arg);
  6018. ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
  6019. kfree(arg.vifs);
  6020. }
  6021. radar:
  6022. ath10k_recalc_radar_detection(ar);
  6023. /* FIXME: How to configure Rx chains properly? */
  6024. /* No other actions are actually necessary. Firmware maintains channel
  6025. * definitions per vdev internally and there's no host-side channel
  6026. * context abstraction to configure, e.g. channel width.
  6027. */
  6028. unlock:
  6029. mutex_unlock(&ar->conf_mutex);
  6030. }
  6031. static int
  6032. ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
  6033. struct ieee80211_vif *vif,
  6034. struct ieee80211_chanctx_conf *ctx)
  6035. {
  6036. struct ath10k *ar = hw->priv;
  6037. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  6038. int ret;
  6039. mutex_lock(&ar->conf_mutex);
  6040. ath10k_dbg(ar, ATH10K_DBG_MAC,
  6041. "mac chanctx assign ptr %pK vdev_id %i\n",
  6042. ctx, arvif->vdev_id);
  6043. if (WARN_ON(arvif->is_started)) {
  6044. mutex_unlock(&ar->conf_mutex);
  6045. return -EBUSY;
  6046. }
  6047. ret = ath10k_vdev_start(arvif, &ctx->def);
  6048. if (ret) {
  6049. ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
  6050. arvif->vdev_id, vif->addr,
  6051. ctx->def.chan->center_freq, ret);
  6052. goto err;
  6053. }
  6054. arvif->is_started = true;
  6055. ret = ath10k_mac_vif_setup_ps(arvif);
  6056. if (ret) {
  6057. ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
  6058. arvif->vdev_id, ret);
  6059. goto err_stop;
  6060. }
  6061. if (vif->type == NL80211_IFTYPE_MONITOR) {
  6062. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
  6063. if (ret) {
  6064. ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
  6065. arvif->vdev_id, ret);
  6066. goto err_stop;
  6067. }
  6068. arvif->is_up = true;
  6069. }
  6070. if (ath10k_mac_can_set_cts_prot(arvif)) {
  6071. ret = ath10k_mac_set_cts_prot(arvif);
  6072. if (ret)
  6073. ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
  6074. arvif->vdev_id, ret);
  6075. }
  6076. mutex_unlock(&ar->conf_mutex);
  6077. return 0;
  6078. err_stop:
  6079. ath10k_vdev_stop(arvif);
  6080. arvif->is_started = false;
  6081. ath10k_mac_vif_setup_ps(arvif);
  6082. err:
  6083. mutex_unlock(&ar->conf_mutex);
  6084. return ret;
  6085. }
  6086. static void
  6087. ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
  6088. struct ieee80211_vif *vif,
  6089. struct ieee80211_chanctx_conf *ctx)
  6090. {
  6091. struct ath10k *ar = hw->priv;
  6092. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  6093. int ret;
  6094. mutex_lock(&ar->conf_mutex);
  6095. ath10k_dbg(ar, ATH10K_DBG_MAC,
  6096. "mac chanctx unassign ptr %pK vdev_id %i\n",
  6097. ctx, arvif->vdev_id);
  6098. WARN_ON(!arvif->is_started);
  6099. if (vif->type == NL80211_IFTYPE_MONITOR) {
  6100. WARN_ON(!arvif->is_up);
  6101. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  6102. if (ret)
  6103. ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
  6104. arvif->vdev_id, ret);
  6105. arvif->is_up = false;
  6106. }
  6107. ret = ath10k_vdev_stop(arvif);
  6108. if (ret)
  6109. ath10k_warn(ar, "failed to stop vdev %i: %d\n",
  6110. arvif->vdev_id, ret);
  6111. arvif->is_started = false;
  6112. mutex_unlock(&ar->conf_mutex);
  6113. }
  6114. static int
  6115. ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
  6116. struct ieee80211_vif_chanctx_switch *vifs,
  6117. int n_vifs,
  6118. enum ieee80211_chanctx_switch_mode mode)
  6119. {
  6120. struct ath10k *ar = hw->priv;
  6121. mutex_lock(&ar->conf_mutex);
  6122. ath10k_dbg(ar, ATH10K_DBG_MAC,
  6123. "mac chanctx switch n_vifs %d mode %d\n",
  6124. n_vifs, mode);
  6125. ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
  6126. mutex_unlock(&ar->conf_mutex);
  6127. return 0;
  6128. }
  6129. static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
  6130. struct ieee80211_vif *vif,
  6131. struct ieee80211_sta *sta)
  6132. {
  6133. struct ath10k *ar;
  6134. struct ath10k_peer *peer;
  6135. ar = hw->priv;
  6136. list_for_each_entry(peer, &ar->peers, list)
  6137. if (peer->sta == sta)
  6138. peer->removed = true;
  6139. }
  6140. static const struct ieee80211_ops ath10k_ops = {
  6141. .tx = ath10k_mac_op_tx,
  6142. .wake_tx_queue = ath10k_mac_op_wake_tx_queue,
  6143. .start = ath10k_start,
  6144. .stop = ath10k_stop,
  6145. .config = ath10k_config,
  6146. .add_interface = ath10k_add_interface,
  6147. .remove_interface = ath10k_remove_interface,
  6148. .configure_filter = ath10k_configure_filter,
  6149. .bss_info_changed = ath10k_bss_info_changed,
  6150. .set_coverage_class = ath10k_mac_op_set_coverage_class,
  6151. .hw_scan = ath10k_hw_scan,
  6152. .cancel_hw_scan = ath10k_cancel_hw_scan,
  6153. .set_key = ath10k_set_key,
  6154. .set_default_unicast_key = ath10k_set_default_unicast_key,
  6155. .sta_state = ath10k_sta_state,
  6156. .conf_tx = ath10k_conf_tx,
  6157. .remain_on_channel = ath10k_remain_on_channel,
  6158. .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
  6159. .set_rts_threshold = ath10k_set_rts_threshold,
  6160. .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
  6161. .flush = ath10k_flush,
  6162. .tx_last_beacon = ath10k_tx_last_beacon,
  6163. .set_antenna = ath10k_set_antenna,
  6164. .get_antenna = ath10k_get_antenna,
  6165. .reconfig_complete = ath10k_reconfig_complete,
  6166. .get_survey = ath10k_get_survey,
  6167. .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
  6168. .sta_rc_update = ath10k_sta_rc_update,
  6169. .offset_tsf = ath10k_offset_tsf,
  6170. .ampdu_action = ath10k_ampdu_action,
  6171. .get_et_sset_count = ath10k_debug_get_et_sset_count,
  6172. .get_et_stats = ath10k_debug_get_et_stats,
  6173. .get_et_strings = ath10k_debug_get_et_strings,
  6174. .add_chanctx = ath10k_mac_op_add_chanctx,
  6175. .remove_chanctx = ath10k_mac_op_remove_chanctx,
  6176. .change_chanctx = ath10k_mac_op_change_chanctx,
  6177. .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
  6178. .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
  6179. .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
  6180. .sta_pre_rcu_remove = ath10k_mac_op_sta_pre_rcu_remove,
  6181. CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
  6182. #ifdef CONFIG_PM
  6183. .suspend = ath10k_wow_op_suspend,
  6184. .resume = ath10k_wow_op_resume,
  6185. #endif
  6186. #ifdef CONFIG_MAC80211_DEBUGFS
  6187. .sta_add_debugfs = ath10k_sta_add_debugfs,
  6188. .sta_statistics = ath10k_sta_statistics,
  6189. #endif
  6190. };
  6191. #define CHAN2G(_channel, _freq, _flags) { \
  6192. .band = NL80211_BAND_2GHZ, \
  6193. .hw_value = (_channel), \
  6194. .center_freq = (_freq), \
  6195. .flags = (_flags), \
  6196. .max_antenna_gain = 0, \
  6197. .max_power = 30, \
  6198. }
  6199. #define CHAN5G(_channel, _freq, _flags) { \
  6200. .band = NL80211_BAND_5GHZ, \
  6201. .hw_value = (_channel), \
  6202. .center_freq = (_freq), \
  6203. .flags = (_flags), \
  6204. .max_antenna_gain = 0, \
  6205. .max_power = 30, \
  6206. }
  6207. static const struct ieee80211_channel ath10k_2ghz_channels[] = {
  6208. CHAN2G(1, 2412, 0),
  6209. CHAN2G(2, 2417, 0),
  6210. CHAN2G(3, 2422, 0),
  6211. CHAN2G(4, 2427, 0),
  6212. CHAN2G(5, 2432, 0),
  6213. CHAN2G(6, 2437, 0),
  6214. CHAN2G(7, 2442, 0),
  6215. CHAN2G(8, 2447, 0),
  6216. CHAN2G(9, 2452, 0),
  6217. CHAN2G(10, 2457, 0),
  6218. CHAN2G(11, 2462, 0),
  6219. CHAN2G(12, 2467, 0),
  6220. CHAN2G(13, 2472, 0),
  6221. CHAN2G(14, 2484, 0),
  6222. };
  6223. static const struct ieee80211_channel ath10k_5ghz_channels[] = {
  6224. CHAN5G(36, 5180, 0),
  6225. CHAN5G(40, 5200, 0),
  6226. CHAN5G(44, 5220, 0),
  6227. CHAN5G(48, 5240, 0),
  6228. CHAN5G(52, 5260, 0),
  6229. CHAN5G(56, 5280, 0),
  6230. CHAN5G(60, 5300, 0),
  6231. CHAN5G(64, 5320, 0),
  6232. CHAN5G(100, 5500, 0),
  6233. CHAN5G(104, 5520, 0),
  6234. CHAN5G(108, 5540, 0),
  6235. CHAN5G(112, 5560, 0),
  6236. CHAN5G(116, 5580, 0),
  6237. CHAN5G(120, 5600, 0),
  6238. CHAN5G(124, 5620, 0),
  6239. CHAN5G(128, 5640, 0),
  6240. CHAN5G(132, 5660, 0),
  6241. CHAN5G(136, 5680, 0),
  6242. CHAN5G(140, 5700, 0),
  6243. CHAN5G(144, 5720, 0),
  6244. CHAN5G(149, 5745, 0),
  6245. CHAN5G(153, 5765, 0),
  6246. CHAN5G(157, 5785, 0),
  6247. CHAN5G(161, 5805, 0),
  6248. CHAN5G(165, 5825, 0),
  6249. CHAN5G(169, 5845, 0),
  6250. };
  6251. struct ath10k *ath10k_mac_create(size_t priv_size)
  6252. {
  6253. struct ieee80211_hw *hw;
  6254. struct ieee80211_ops *ops;
  6255. struct ath10k *ar;
  6256. ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
  6257. if (!ops)
  6258. return NULL;
  6259. hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
  6260. if (!hw) {
  6261. kfree(ops);
  6262. return NULL;
  6263. }
  6264. ar = hw->priv;
  6265. ar->hw = hw;
  6266. ar->ops = ops;
  6267. return ar;
  6268. }
  6269. void ath10k_mac_destroy(struct ath10k *ar)
  6270. {
  6271. struct ieee80211_ops *ops = ar->ops;
  6272. ieee80211_free_hw(ar->hw);
  6273. kfree(ops);
  6274. }
  6275. static const struct ieee80211_iface_limit ath10k_if_limits[] = {
  6276. {
  6277. .max = 8,
  6278. .types = BIT(NL80211_IFTYPE_STATION)
  6279. | BIT(NL80211_IFTYPE_P2P_CLIENT)
  6280. },
  6281. {
  6282. .max = 3,
  6283. .types = BIT(NL80211_IFTYPE_P2P_GO)
  6284. },
  6285. {
  6286. .max = 1,
  6287. .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
  6288. },
  6289. {
  6290. .max = 7,
  6291. .types = BIT(NL80211_IFTYPE_AP)
  6292. #ifdef CONFIG_MAC80211_MESH
  6293. | BIT(NL80211_IFTYPE_MESH_POINT)
  6294. #endif
  6295. },
  6296. };
  6297. static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
  6298. {
  6299. .max = 8,
  6300. .types = BIT(NL80211_IFTYPE_AP)
  6301. #ifdef CONFIG_MAC80211_MESH
  6302. | BIT(NL80211_IFTYPE_MESH_POINT)
  6303. #endif
  6304. },
  6305. {
  6306. .max = 1,
  6307. .types = BIT(NL80211_IFTYPE_STATION)
  6308. },
  6309. };
  6310. static const struct ieee80211_iface_combination ath10k_if_comb[] = {
  6311. {
  6312. .limits = ath10k_if_limits,
  6313. .n_limits = ARRAY_SIZE(ath10k_if_limits),
  6314. .max_interfaces = 8,
  6315. .num_different_channels = 1,
  6316. .beacon_int_infra_match = true,
  6317. },
  6318. };
  6319. static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
  6320. {
  6321. .limits = ath10k_10x_if_limits,
  6322. .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
  6323. .max_interfaces = 8,
  6324. .num_different_channels = 1,
  6325. .beacon_int_infra_match = true,
  6326. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  6327. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  6328. BIT(NL80211_CHAN_WIDTH_20) |
  6329. BIT(NL80211_CHAN_WIDTH_40) |
  6330. BIT(NL80211_CHAN_WIDTH_80),
  6331. #endif
  6332. },
  6333. };
  6334. static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
  6335. {
  6336. .max = 2,
  6337. .types = BIT(NL80211_IFTYPE_STATION),
  6338. },
  6339. {
  6340. .max = 2,
  6341. .types = BIT(NL80211_IFTYPE_AP) |
  6342. #ifdef CONFIG_MAC80211_MESH
  6343. BIT(NL80211_IFTYPE_MESH_POINT) |
  6344. #endif
  6345. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  6346. BIT(NL80211_IFTYPE_P2P_GO),
  6347. },
  6348. {
  6349. .max = 1,
  6350. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  6351. },
  6352. };
  6353. static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
  6354. {
  6355. .max = 2,
  6356. .types = BIT(NL80211_IFTYPE_STATION),
  6357. },
  6358. {
  6359. .max = 2,
  6360. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  6361. },
  6362. {
  6363. .max = 1,
  6364. .types = BIT(NL80211_IFTYPE_AP) |
  6365. #ifdef CONFIG_MAC80211_MESH
  6366. BIT(NL80211_IFTYPE_MESH_POINT) |
  6367. #endif
  6368. BIT(NL80211_IFTYPE_P2P_GO),
  6369. },
  6370. {
  6371. .max = 1,
  6372. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  6373. },
  6374. };
  6375. static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
  6376. {
  6377. .max = 1,
  6378. .types = BIT(NL80211_IFTYPE_STATION),
  6379. },
  6380. {
  6381. .max = 1,
  6382. .types = BIT(NL80211_IFTYPE_ADHOC),
  6383. },
  6384. };
  6385. /* FIXME: This is not thouroughly tested. These combinations may over- or
  6386. * underestimate hw/fw capabilities.
  6387. */
  6388. static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
  6389. {
  6390. .limits = ath10k_tlv_if_limit,
  6391. .num_different_channels = 1,
  6392. .max_interfaces = 4,
  6393. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
  6394. },
  6395. {
  6396. .limits = ath10k_tlv_if_limit_ibss,
  6397. .num_different_channels = 1,
  6398. .max_interfaces = 2,
  6399. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
  6400. },
  6401. };
  6402. static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
  6403. {
  6404. .limits = ath10k_tlv_if_limit,
  6405. .num_different_channels = 1,
  6406. .max_interfaces = 4,
  6407. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
  6408. },
  6409. {
  6410. .limits = ath10k_tlv_qcs_if_limit,
  6411. .num_different_channels = 2,
  6412. .max_interfaces = 4,
  6413. .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
  6414. },
  6415. {
  6416. .limits = ath10k_tlv_if_limit_ibss,
  6417. .num_different_channels = 1,
  6418. .max_interfaces = 2,
  6419. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
  6420. },
  6421. };
  6422. static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
  6423. {
  6424. .max = 1,
  6425. .types = BIT(NL80211_IFTYPE_STATION),
  6426. },
  6427. {
  6428. .max = 16,
  6429. .types = BIT(NL80211_IFTYPE_AP)
  6430. #ifdef CONFIG_MAC80211_MESH
  6431. | BIT(NL80211_IFTYPE_MESH_POINT)
  6432. #endif
  6433. },
  6434. };
  6435. static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
  6436. {
  6437. .limits = ath10k_10_4_if_limits,
  6438. .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
  6439. .max_interfaces = 16,
  6440. .num_different_channels = 1,
  6441. .beacon_int_infra_match = true,
  6442. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  6443. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  6444. BIT(NL80211_CHAN_WIDTH_20) |
  6445. BIT(NL80211_CHAN_WIDTH_40) |
  6446. BIT(NL80211_CHAN_WIDTH_80),
  6447. #endif
  6448. },
  6449. };
  6450. static void ath10k_get_arvif_iter(void *data, u8 *mac,
  6451. struct ieee80211_vif *vif)
  6452. {
  6453. struct ath10k_vif_iter *arvif_iter = data;
  6454. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  6455. if (arvif->vdev_id == arvif_iter->vdev_id)
  6456. arvif_iter->arvif = arvif;
  6457. }
  6458. struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
  6459. {
  6460. struct ath10k_vif_iter arvif_iter;
  6461. u32 flags;
  6462. memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
  6463. arvif_iter.vdev_id = vdev_id;
  6464. flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  6465. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  6466. flags,
  6467. ath10k_get_arvif_iter,
  6468. &arvif_iter);
  6469. if (!arvif_iter.arvif) {
  6470. ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
  6471. return NULL;
  6472. }
  6473. return arvif_iter.arvif;
  6474. }
  6475. #define WRD_METHOD "WRDD"
  6476. #define WRDD_WIFI (0x07)
  6477. static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
  6478. {
  6479. union acpi_object *mcc_pkg;
  6480. union acpi_object *domain_type;
  6481. union acpi_object *mcc_value;
  6482. u32 i;
  6483. if (wrdd->type != ACPI_TYPE_PACKAGE ||
  6484. wrdd->package.count < 2 ||
  6485. wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
  6486. wrdd->package.elements[0].integer.value != 0) {
  6487. ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
  6488. return 0;
  6489. }
  6490. for (i = 1; i < wrdd->package.count; ++i) {
  6491. mcc_pkg = &wrdd->package.elements[i];
  6492. if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
  6493. continue;
  6494. if (mcc_pkg->package.count < 2)
  6495. continue;
  6496. if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
  6497. mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
  6498. continue;
  6499. domain_type = &mcc_pkg->package.elements[0];
  6500. if (domain_type->integer.value != WRDD_WIFI)
  6501. continue;
  6502. mcc_value = &mcc_pkg->package.elements[1];
  6503. return mcc_value->integer.value;
  6504. }
  6505. return 0;
  6506. }
  6507. static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
  6508. {
  6509. struct pci_dev __maybe_unused *pdev = to_pci_dev(ar->dev);
  6510. acpi_handle root_handle;
  6511. acpi_handle handle;
  6512. struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
  6513. acpi_status status;
  6514. u32 alpha2_code;
  6515. char alpha2[3];
  6516. root_handle = ACPI_HANDLE(&pdev->dev);
  6517. if (!root_handle)
  6518. return -EOPNOTSUPP;
  6519. status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
  6520. if (ACPI_FAILURE(status)) {
  6521. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  6522. "failed to get wrd method %d\n", status);
  6523. return -EIO;
  6524. }
  6525. status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
  6526. if (ACPI_FAILURE(status)) {
  6527. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  6528. "failed to call wrdc %d\n", status);
  6529. return -EIO;
  6530. }
  6531. alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
  6532. kfree(wrdd.pointer);
  6533. if (!alpha2_code)
  6534. return -EIO;
  6535. alpha2[0] = (alpha2_code >> 8) & 0xff;
  6536. alpha2[1] = (alpha2_code >> 0) & 0xff;
  6537. alpha2[2] = '\0';
  6538. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  6539. "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
  6540. *rd = ath_regd_find_country_by_name(alpha2);
  6541. if (*rd == 0xffff)
  6542. return -EIO;
  6543. *rd |= COUNTRY_ERD_FLAG;
  6544. return 0;
  6545. }
  6546. static int ath10k_mac_init_rd(struct ath10k *ar)
  6547. {
  6548. int ret;
  6549. u16 rd;
  6550. ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
  6551. if (ret) {
  6552. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  6553. "fallback to eeprom programmed regulatory settings\n");
  6554. rd = ar->hw_eeprom_rd;
  6555. }
  6556. ar->ath_common.regulatory.current_rd = rd;
  6557. return 0;
  6558. }
  6559. int ath10k_mac_register(struct ath10k *ar)
  6560. {
  6561. static const u32 cipher_suites[] = {
  6562. WLAN_CIPHER_SUITE_WEP40,
  6563. WLAN_CIPHER_SUITE_WEP104,
  6564. WLAN_CIPHER_SUITE_TKIP,
  6565. WLAN_CIPHER_SUITE_CCMP,
  6566. WLAN_CIPHER_SUITE_AES_CMAC,
  6567. };
  6568. struct ieee80211_supported_band *band;
  6569. void *channels;
  6570. int ret;
  6571. SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
  6572. SET_IEEE80211_DEV(ar->hw, ar->dev);
  6573. BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
  6574. ARRAY_SIZE(ath10k_5ghz_channels)) !=
  6575. ATH10K_NUM_CHANS);
  6576. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  6577. channels = kmemdup(ath10k_2ghz_channels,
  6578. sizeof(ath10k_2ghz_channels),
  6579. GFP_KERNEL);
  6580. if (!channels) {
  6581. ret = -ENOMEM;
  6582. goto err_free;
  6583. }
  6584. band = &ar->mac.sbands[NL80211_BAND_2GHZ];
  6585. band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
  6586. band->channels = channels;
  6587. if (ar->hw_params.cck_rate_map_rev2) {
  6588. band->n_bitrates = ath10k_g_rates_rev2_size;
  6589. band->bitrates = ath10k_g_rates_rev2;
  6590. } else {
  6591. band->n_bitrates = ath10k_g_rates_size;
  6592. band->bitrates = ath10k_g_rates;
  6593. }
  6594. ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
  6595. }
  6596. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  6597. channels = kmemdup(ath10k_5ghz_channels,
  6598. sizeof(ath10k_5ghz_channels),
  6599. GFP_KERNEL);
  6600. if (!channels) {
  6601. ret = -ENOMEM;
  6602. goto err_free;
  6603. }
  6604. band = &ar->mac.sbands[NL80211_BAND_5GHZ];
  6605. band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
  6606. band->channels = channels;
  6607. band->n_bitrates = ath10k_a_rates_size;
  6608. band->bitrates = ath10k_a_rates;
  6609. ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
  6610. }
  6611. ath10k_mac_setup_ht_vht_cap(ar);
  6612. ar->hw->wiphy->interface_modes =
  6613. BIT(NL80211_IFTYPE_STATION) |
  6614. BIT(NL80211_IFTYPE_AP) |
  6615. BIT(NL80211_IFTYPE_MESH_POINT);
  6616. ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
  6617. ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
  6618. if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
  6619. ar->hw->wiphy->interface_modes |=
  6620. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  6621. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  6622. BIT(NL80211_IFTYPE_P2P_GO);
  6623. ieee80211_hw_set(ar->hw, SIGNAL_DBM);
  6624. ieee80211_hw_set(ar->hw, SUPPORTS_PS);
  6625. ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
  6626. ieee80211_hw_set(ar->hw, MFP_CAPABLE);
  6627. ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
  6628. ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
  6629. ieee80211_hw_set(ar->hw, AP_LINK_PS);
  6630. ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
  6631. ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
  6632. ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
  6633. ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
  6634. ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
  6635. ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
  6636. ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
  6637. ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
  6638. ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
  6639. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  6640. ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
  6641. ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
  6642. ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  6643. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
  6644. ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
  6645. if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
  6646. ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
  6647. ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
  6648. }
  6649. ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
  6650. ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
  6651. ar->hw->vif_data_size = sizeof(struct ath10k_vif);
  6652. ar->hw->sta_data_size = sizeof(struct ath10k_sta);
  6653. ar->hw->txq_data_size = sizeof(struct ath10k_txq);
  6654. ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
  6655. if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
  6656. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
  6657. /* Firmware delivers WPS/P2P Probe Requests frames to driver so
  6658. * that userspace (e.g. wpa_supplicant/hostapd) can generate
  6659. * correct Probe Responses. This is more of a hack advert..
  6660. */
  6661. ar->hw->wiphy->probe_resp_offload |=
  6662. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
  6663. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
  6664. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
  6665. }
  6666. if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
  6667. ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  6668. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  6669. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  6670. ar->hw->wiphy->max_remain_on_channel_duration = 5000;
  6671. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  6672. ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
  6673. NL80211_FEATURE_AP_SCAN;
  6674. ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
  6675. ret = ath10k_wow_init(ar);
  6676. if (ret) {
  6677. ath10k_warn(ar, "failed to init wow: %d\n", ret);
  6678. goto err_free;
  6679. }
  6680. wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
  6681. /*
  6682. * on LL hardware queues are managed entirely by the FW
  6683. * so we only advertise to mac we can do the queues thing
  6684. */
  6685. ar->hw->queues = IEEE80211_MAX_QUEUES;
  6686. /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
  6687. * something that vdev_ids can't reach so that we don't stop the queue
  6688. * accidentally.
  6689. */
  6690. ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
  6691. switch (ar->running_fw->fw_file.wmi_op_version) {
  6692. case ATH10K_FW_WMI_OP_VERSION_MAIN:
  6693. ar->hw->wiphy->iface_combinations = ath10k_if_comb;
  6694. ar->hw->wiphy->n_iface_combinations =
  6695. ARRAY_SIZE(ath10k_if_comb);
  6696. ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  6697. break;
  6698. case ATH10K_FW_WMI_OP_VERSION_TLV:
  6699. if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
  6700. ar->hw->wiphy->iface_combinations =
  6701. ath10k_tlv_qcs_if_comb;
  6702. ar->hw->wiphy->n_iface_combinations =
  6703. ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
  6704. } else {
  6705. ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
  6706. ar->hw->wiphy->n_iface_combinations =
  6707. ARRAY_SIZE(ath10k_tlv_if_comb);
  6708. }
  6709. ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  6710. break;
  6711. case ATH10K_FW_WMI_OP_VERSION_10_1:
  6712. case ATH10K_FW_WMI_OP_VERSION_10_2:
  6713. case ATH10K_FW_WMI_OP_VERSION_10_2_4:
  6714. ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
  6715. ar->hw->wiphy->n_iface_combinations =
  6716. ARRAY_SIZE(ath10k_10x_if_comb);
  6717. break;
  6718. case ATH10K_FW_WMI_OP_VERSION_10_4:
  6719. ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
  6720. ar->hw->wiphy->n_iface_combinations =
  6721. ARRAY_SIZE(ath10k_10_4_if_comb);
  6722. break;
  6723. case ATH10K_FW_WMI_OP_VERSION_UNSET:
  6724. case ATH10K_FW_WMI_OP_VERSION_MAX:
  6725. WARN_ON(1);
  6726. ret = -EINVAL;
  6727. goto err_free;
  6728. }
  6729. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  6730. ar->hw->netdev_features = NETIF_F_HW_CSUM;
  6731. if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
  6732. /* Init ath dfs pattern detector */
  6733. ar->ath_common.debug_mask = ATH_DBG_DFS;
  6734. ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
  6735. NL80211_DFS_UNSET);
  6736. if (!ar->dfs_detector)
  6737. ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
  6738. }
  6739. /* Current wake_tx_queue implementation imposes a significant
  6740. * performance penalty in some setups. The tx scheduling code needs
  6741. * more work anyway so disable the wake_tx_queue unless firmware
  6742. * supports the pull-push mechanism.
  6743. */
  6744. if (!test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL,
  6745. ar->running_fw->fw_file.fw_features))
  6746. ar->ops->wake_tx_queue = NULL;
  6747. ret = ath10k_mac_init_rd(ar);
  6748. if (ret) {
  6749. ath10k_err(ar, "failed to derive regdom: %d\n", ret);
  6750. goto err_dfs_detector_exit;
  6751. }
  6752. /* Disable set_coverage_class for chipsets that do not support it. */
  6753. if (!ar->hw_params.hw_ops->set_coverage_class)
  6754. ar->ops->set_coverage_class = NULL;
  6755. ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
  6756. ath10k_reg_notifier);
  6757. if (ret) {
  6758. ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
  6759. goto err_dfs_detector_exit;
  6760. }
  6761. ar->hw->wiphy->cipher_suites = cipher_suites;
  6762. ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  6763. ret = ieee80211_register_hw(ar->hw);
  6764. if (ret) {
  6765. ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
  6766. goto err_dfs_detector_exit;
  6767. }
  6768. if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
  6769. ret = regulatory_hint(ar->hw->wiphy,
  6770. ar->ath_common.regulatory.alpha2);
  6771. if (ret)
  6772. goto err_unregister;
  6773. }
  6774. return 0;
  6775. err_unregister:
  6776. ieee80211_unregister_hw(ar->hw);
  6777. err_dfs_detector_exit:
  6778. if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  6779. ar->dfs_detector->exit(ar->dfs_detector);
  6780. err_free:
  6781. kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
  6782. kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
  6783. SET_IEEE80211_DEV(ar->hw, NULL);
  6784. return ret;
  6785. }
  6786. void ath10k_mac_unregister(struct ath10k *ar)
  6787. {
  6788. ieee80211_unregister_hw(ar->hw);
  6789. if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  6790. ar->dfs_detector->exit(ar->dfs_detector);
  6791. kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
  6792. kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
  6793. SET_IEEE80211_DEV(ar->hw, NULL);
  6794. }