mac.c 217 KB

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