mac.c 209 KB

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