mac.c 213 KB

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