0003-surface-sam.patch 341 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619
  1. From 25788573eb6b46e5a525013489433a3a7a6ffffd Mon Sep 17 00:00:00 2001
  2. From: qzed <qzed@users.noreply.github.com>
  3. Date: Mon, 26 Aug 2019 01:11:08 +0200
  4. Subject: [PATCH 3/6] surface-sam
  5. ---
  6. drivers/acpi/acpica/dsopcode.c | 2 +-
  7. drivers/acpi/acpica/exfield.c | 12 +-
  8. drivers/platform/x86/Kconfig | 1 +
  9. drivers/platform/x86/Makefile | 1 +
  10. drivers/platform/x86/surface_sam/Kconfig | 176 +
  11. drivers/platform/x86/surface_sam/Makefile | 16 +
  12. .../x86/surface_sam/surface_sam_debugfs.c | 270 +
  13. .../x86/surface_sam/surface_sam_dtx.c | 582 ++
  14. .../x86/surface_sam/surface_sam_hps.c | 1287 ++++
  15. .../x86/surface_sam/surface_sam_san.c | 930 +++
  16. .../x86/surface_sam/surface_sam_san.h | 30 +
  17. .../x86/surface_sam/surface_sam_sid.c | 283 +
  18. .../x86/surface_sam/surface_sam_sid_gpelid.c | 232 +
  19. .../surface_sam/surface_sam_sid_perfmode.c | 214 +
  20. .../x86/surface_sam/surface_sam_sid_power.c | 1054 ++++
  21. .../x86/surface_sam/surface_sam_sid_power.h | 16 +
  22. .../x86/surface_sam/surface_sam_sid_vhf.c | 429 ++
  23. .../x86/surface_sam/surface_sam_sid_vhf.h | 14 +
  24. .../x86/surface_sam/surface_sam_ssh.c | 5190 +++++++++++++++++
  25. .../x86/surface_sam/surface_sam_ssh.h | 717 +++
  26. .../x86/surface_sam/surface_sam_ssh_trace.h | 532 ++
  27. .../x86/surface_sam/surface_sam_vhf.c | 266 +
  28. drivers/tty/serdev/core.c | 111 +-
  29. 23 files changed, 12349 insertions(+), 16 deletions(-)
  30. create mode 100644 drivers/platform/x86/surface_sam/Kconfig
  31. create mode 100644 drivers/platform/x86/surface_sam/Makefile
  32. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_debugfs.c
  33. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_dtx.c
  34. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_hps.c
  35. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_san.c
  36. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_san.h
  37. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid.c
  38. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
  39. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
  40. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_power.c
  41. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_power.h
  42. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
  43. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
  44. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh.c
  45. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh.h
  46. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
  47. create mode 100644 drivers/platform/x86/surface_sam/surface_sam_vhf.c
  48. diff --git a/drivers/acpi/acpica/dsopcode.c b/drivers/acpi/acpica/dsopcode.c
  49. index 10f32b62608ee..7b2a4987f0507 100644
  50. --- a/drivers/acpi/acpica/dsopcode.c
  51. +++ b/drivers/acpi/acpica/dsopcode.c
  52. @@ -123,7 +123,7 @@ acpi_ds_init_buffer_field(u16 aml_opcode,
  53. /* Offset is in bits, count is in bits */
  54. - field_flags = AML_FIELD_ACCESS_BYTE;
  55. + field_flags = AML_FIELD_ACCESS_BUFFER;
  56. bit_offset = offset;
  57. bit_count = (u32) length_desc->integer.value;
  58. diff --git a/drivers/acpi/acpica/exfield.c b/drivers/acpi/acpica/exfield.c
  59. index d3d2dbfba680c..0b7f617a6e9b1 100644
  60. --- a/drivers/acpi/acpica/exfield.c
  61. +++ b/drivers/acpi/acpica/exfield.c
  62. @@ -109,6 +109,7 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  63. union acpi_operand_object *buffer_desc;
  64. void *buffer;
  65. u32 buffer_length;
  66. + u8 field_flags;
  67. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  68. @@ -157,11 +158,16 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  69. * Note: Field.length is in bits.
  70. */
  71. buffer_length =
  72. - (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  73. + (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  74. + field_flags = obj_desc->common_field.field_flags;
  75. - if (buffer_length > acpi_gbl_integer_byte_width) {
  76. + if (buffer_length > acpi_gbl_integer_byte_width ||
  77. + (field_flags & AML_FIELD_ACCESS_TYPE_MASK) == AML_FIELD_ACCESS_BUFFER) {
  78. - /* Field is too large for an Integer, create a Buffer instead */
  79. + /*
  80. + * Field is either too large for an Integer, or a actually of type
  81. + * buffer, so create a Buffer.
  82. + */
  83. buffer_desc = acpi_ut_create_buffer_object(buffer_length);
  84. if (!buffer_desc) {
  85. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
  86. index 348c795019fa4..d25067a838a33 100644
  87. --- a/drivers/platform/x86/Kconfig
  88. +++ b/drivers/platform/x86/Kconfig
  89. @@ -1342,6 +1342,7 @@ config PCENGINES_APU2
  90. will be called pcengines-apuv2.
  91. source "drivers/platform/x86/intel_speed_select_if/Kconfig"
  92. +source "drivers/platform/x86/surface_sam/Kconfig"
  93. endif # X86_PLATFORM_DEVICES
  94. diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
  95. index 6dd955ad9bf18..19b56f2181eb9 100644
  96. --- a/drivers/platform/x86/Makefile
  97. +++ b/drivers/platform/x86/Makefile
  98. @@ -101,3 +101,4 @@ obj-$(CONFIG_I2C_MULTI_INSTANTIATE) += i2c-multi-instantiate.o
  99. obj-$(CONFIG_INTEL_ATOMISP2_PM) += intel_atomisp2_pm.o
  100. obj-$(CONFIG_PCENGINES_APU2) += pcengines-apuv2.o
  101. obj-$(CONFIG_INTEL_SPEED_SELECT_INTERFACE) += intel_speed_select_if/
  102. +obj-$(CONFIG_SURFACE_SAM) += surface_sam/
  103. diff --git a/drivers/platform/x86/surface_sam/Kconfig b/drivers/platform/x86/surface_sam/Kconfig
  104. new file mode 100644
  105. index 0000000000000..b5bb55248a5d5
  106. --- /dev/null
  107. +++ b/drivers/platform/x86/surface_sam/Kconfig
  108. @@ -0,0 +1,176 @@
  109. +menuconfig SURFACE_SAM
  110. + depends on ACPI
  111. + tristate "Microsoft Surface/System Aggregator Module and Platform Drivers"
  112. + help
  113. + Drivers for the Surface/System Aggregator Module (SAM) of Microsoft
  114. + Surface devices.
  115. +
  116. + SAM is an embedded controller that provides access to various
  117. + functionalities on these devices, including battery status, keyboard
  118. + events (on the Laptops) and many more.
  119. +
  120. + Say M/Y here if you have a Microsoft Surface device with a SAM device
  121. + (i.e. 5th generation or later).
  122. +
  123. +config SURFACE_SAM_SSH
  124. + tristate "Surface Serial Hub Driver"
  125. + depends on SURFACE_SAM
  126. + depends on SERIAL_DEV_CTRL_TTYPORT
  127. + select CRC_CCITT
  128. + default m
  129. + help
  130. + Surface Serial Hub driver for 5th generation (or later) Microsoft
  131. + Surface devices.
  132. +
  133. + This is the base driver for the embedded serial controller found on
  134. + 5th generation (and later) Microsoft Surface devices (e.g. Book 2,
  135. + Laptop, Laptop 2, Pro 2017, Pro 6, ...). This driver itself only
  136. + provides access to the embedded controller (SAM) and subsequent
  137. + drivers are required for the respective functionalities.
  138. +
  139. + If you have a 5th generation (or later) Microsoft Surface device, say
  140. + Y or M here.
  141. +
  142. +config SURFACE_SAM_SSH_ERROR_INJECTION
  143. + bool "Surface Serial Hub Error Injection Capabilities"
  144. + depends on SURFACE_SAM_SSH
  145. + depends on FUNCTION_ERROR_INJECTION
  146. + default n
  147. + help
  148. + Enable error injection capabilities for the Surface Serial Hub.
  149. + This is used to debug the driver, specifically the communication
  150. + interface. It is not required for normal use.
  151. +
  152. + If you are not sure, say N here.
  153. +
  154. +config SURFACE_SAM_DEBUGFS
  155. + tristate "Surface Serial Hub Debug Device"
  156. + depends on SURFACE_SAM_SSH
  157. + depends on DEBUG_FS
  158. + default n
  159. + help
  160. + Debug device for direct communication with the embedded controller
  161. + found on 5th generation (and later) Microsoft Surface devices (e.g.
  162. + Book 2, Laptop, Laptop 2, Pro 2017, Pro 6, ...) via debugfs.
  163. +
  164. + If you are not sure, say N here.
  165. +
  166. +config SURFACE_SAM_SAN
  167. + tristate "Surface ACPI Notify Driver"
  168. + depends on SURFACE_SAM_SSH
  169. + default m
  170. + help
  171. + Surface ACPI Notify driver for 5th generation (or later) Microsoft
  172. + Surface devices.
  173. +
  174. + This driver enables basic ACPI events and requests, such as battery
  175. + status requests/events, thermal events, lid status, and possibly more,
  176. + which would otherwise not work on these devices.
  177. +
  178. + If you are not sure, say M here.
  179. +
  180. +config SURFACE_SAM_VHF
  181. + tristate "Surface Virtual HID Framework Driver"
  182. + depends on SURFACE_SAM_SSH
  183. + depends on HID
  184. + default m
  185. + help
  186. + Surface Virtual HID Framework driver for 5th generation (or later)
  187. + Microsoft Surface devices.
  188. +
  189. + This driver provides support for the Microsoft Virtual HID framework,
  190. + which is required for keyboard support on the Surface Laptop 1 and 2.
  191. +
  192. + If you are not sure, say M here.
  193. +
  194. +config SURFACE_SAM_DTX
  195. + tristate "Surface Detachment System (DTX) Driver"
  196. + depends on SURFACE_SAM_SSH
  197. + depends on INPUT
  198. + default m
  199. + help
  200. + Surface Detachment System (DTX) driver for the Microsoft Surface Book
  201. + 2. This driver provides support for proper detachment handling in
  202. + user-space, status-events relating to the base and support for
  203. + the safe-guard keeping the base attached when the discrete GPU
  204. + contained in it is running via the special /dev/surface-dtx device.
  205. +
  206. + Also provides a standard input device to provide SW_TABLET_MODE events
  207. + upon device mode change.
  208. +
  209. + If you are not sure, say M here.
  210. +
  211. +config SURFACE_SAM_HPS
  212. + tristate "Surface dGPU Hot-Plug System (dGPU-HPS) Driver"
  213. + depends on SURFACE_SAM_SSH
  214. + depends on SURFACE_SAM_SAN
  215. + depends on GPIO_SYSFS
  216. + default m
  217. + help
  218. + Driver to properly handle hot-plugging and explicit power-on/power-off
  219. + of the discrete GPU (dGPU) on the Surface Book 2 and 3.
  220. +
  221. + If you are not sure, say M here.
  222. +
  223. +config SURFACE_SAM_SID
  224. + tristate "Surface Platform Integration Driver"
  225. + depends on SURFACE_SAM_SSH
  226. + default m
  227. + help
  228. + Surface Platform Integration Driver for the Microsoft Surface Devices.
  229. + This driver loads various model-specific sub-drivers, including
  230. + battery and keyboard support on 7th generation Surface devices, proper
  231. + lid setup to enable device wakeup when the lid is opened on multiple
  232. + models, as well as performance mode setting support on the Surface
  233. + Book 2.
  234. +
  235. + If you are not sure, say M here.
  236. +
  237. +config SURFACE_SAM_SID_GPELID
  238. + tristate "Surface Lid Wakeup Driver"
  239. + depends on SURFACE_SAM_SID
  240. + default m
  241. + help
  242. + Driver to set up device wake-up via lid on Intel-based Microsoft
  243. + Surface devices. These devices do not wake up from sleep as their GPE
  244. + interrupt is not configured automatically. This driver solves that
  245. + problem.
  246. +
  247. + If you are not sure, say M here.
  248. +
  249. +config SURFACE_SAM_SID_PERFMODE
  250. + tristate "Surface Performance Mode Driver"
  251. + depends on SURFACE_SAM_SID
  252. + depends on SYSFS
  253. + default m
  254. + help
  255. + This driver provides support for setting performance-modes on Surface
  256. + devices via the perf_mode sysfs attribute. Currently only supports the
  257. + Surface Book 2. Performance-modes directly influence the fan-profile
  258. + of the device, allowing to choose between higher performance or
  259. + quieter operation.
  260. +
  261. + If you are not sure, say M here.
  262. +
  263. +config SURFACE_SAM_SID_VHF
  264. + tristate "Surface SAM HID Driver"
  265. + depends on SURFACE_SAM_SID
  266. + depends on HID
  267. + default m
  268. + help
  269. + This driver provides support for HID devices connected via the Surface
  270. + SAM embedded controller. It provides support for keyboard and touchpad
  271. + on the Surface Laptop 3 models.
  272. +
  273. + If you are not sure, say M here.
  274. +
  275. +config SURFACE_SAM_SID_POWER
  276. + tristate "Surface SAM Battery/AC Driver"
  277. + depends on SURFACE_SAM_SID
  278. + select POWER_SUPPLY
  279. + default m
  280. + help
  281. + This driver provides support for the battery and AC on 7th generation
  282. + Surface devices.
  283. +
  284. + If you are not sure, say M here.
  285. diff --git a/drivers/platform/x86/surface_sam/Makefile b/drivers/platform/x86/surface_sam/Makefile
  286. new file mode 100644
  287. index 0000000000000..89bced46ebcdd
  288. --- /dev/null
  289. +++ b/drivers/platform/x86/surface_sam/Makefile
  290. @@ -0,0 +1,16 @@
  291. +# SPDX-License-Identifier: GPL-2.0-or-later
  292. +
  293. +# For include/trace/define_trace.h to include surface_sam_ssh_trace.h
  294. +CFLAGS_surface_sam_ssh.o = -I$(src)
  295. +
  296. +obj-$(CONFIG_SURFACE_SAM_SSH) += surface_sam_ssh.o
  297. +obj-$(CONFIG_SURFACE_SAM_SAN) += surface_sam_san.o
  298. +obj-$(CONFIG_SURFACE_SAM_DTX) += surface_sam_dtx.o
  299. +obj-$(CONFIG_SURFACE_SAM_HPS) += surface_sam_hps.o
  300. +obj-$(CONFIG_SURFACE_SAM_VHF) += surface_sam_vhf.o
  301. +obj-$(CONFIG_SURFACE_SAM_SID) += surface_sam_sid.o
  302. +obj-$(CONFIG_SURFACE_SAM_SID_GPELID) += surface_sam_sid_gpelid.o
  303. +obj-$(CONFIG_SURFACE_SAM_SID_PERFMODE) += surface_sam_sid_perfmode.o
  304. +obj-$(CONFIG_SURFACE_SAM_SID_POWER) += surface_sam_sid_power.o
  305. +obj-$(CONFIG_SURFACE_SAM_SID_VHF) += surface_sam_sid_vhf.o
  306. +obj-$(CONFIG_SURFACE_SAM_DEBUGFS) += surface_sam_debugfs.o
  307. diff --git a/drivers/platform/x86/surface_sam/surface_sam_debugfs.c b/drivers/platform/x86/surface_sam/surface_sam_debugfs.c
  308. new file mode 100644
  309. index 0000000000000..13e93404775c5
  310. --- /dev/null
  311. +++ b/drivers/platform/x86/surface_sam/surface_sam_debugfs.c
  312. @@ -0,0 +1,270 @@
  313. +// SPDX-License-Identifier: GPL-2.0-or-later
  314. +
  315. +#include <linux/debugfs.h>
  316. +#include <linux/fs.h>
  317. +#include <linux/kernel.h>
  318. +#include <linux/module.h>
  319. +#include <linux/platform_device.h>
  320. +#include <linux/slab.h>
  321. +#include <linux/uaccess.h>
  322. +
  323. +#include "surface_sam_ssh.h"
  324. +
  325. +#define SSAM_DBGDEV_NAME "surface_sam_dbgdev"
  326. +#define SSAM_DBGDEV_VERS 0x0100
  327. +
  328. +
  329. +struct ssam_dbgdev_request {
  330. + __u8 target_category;
  331. + __u8 command_id;
  332. + __u8 instance_id;
  333. + __u8 channel;
  334. + __u16 flags;
  335. + __s16 status;
  336. +
  337. + struct {
  338. + __u8 __pad[6];
  339. + __u16 length;
  340. + const __u8 __user *data;
  341. + } payload;
  342. +
  343. + struct {
  344. + __u8 __pad[6];
  345. + __u16 length;
  346. + __u8 __user *data;
  347. + } response;
  348. +};
  349. +
  350. +#define SSAM_DBGDEV_IOCTL_GETVERSION _IOR(0xA5, 0, __u32)
  351. +#define SSAM_DBGDEV_IOCTL_REQUEST _IOWR(0xA5, 1, struct ssam_dbgdev_request)
  352. +
  353. +
  354. +struct ssam_dbgdev {
  355. + struct ssam_controller *ctrl;
  356. + struct dentry *dentry_dir;
  357. + struct dentry *dentry_dev;
  358. +};
  359. +
  360. +
  361. +static int ssam_dbgdev_open(struct inode *inode, struct file *filp)
  362. +{
  363. + filp->private_data = inode->i_private;
  364. + return nonseekable_open(inode, filp);
  365. +}
  366. +
  367. +static long ssam_dbgdev_request(struct file *file, unsigned long arg)
  368. +{
  369. + struct ssam_dbgdev *ddev = file->private_data;
  370. + struct ssam_dbgdev_request __user *r;
  371. + struct ssam_dbgdev_request rqst;
  372. + struct ssam_request spec;
  373. + struct ssam_response rsp;
  374. + u8 *pldbuf = NULL;
  375. + u8 *rspbuf = NULL;
  376. + int status = 0, ret = 0, tmp;
  377. +
  378. + r = (struct ssam_dbgdev_request __user *)arg;
  379. + ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
  380. + if (ret)
  381. + goto out;
  382. +
  383. + // setup basic request fields
  384. + spec.target_category = rqst.target_category;
  385. + spec.command_id = rqst.command_id;
  386. + spec.instance_id = rqst.instance_id;
  387. + spec.channel = rqst.channel;
  388. + spec.flags = rqst.flags;
  389. + spec.length = rqst.payload.length;
  390. +
  391. + rsp.capacity = rqst.response.length;
  392. + rsp.length = 0;
  393. +
  394. + // get request payload from user-space
  395. + if (spec.length) {
  396. + if (!rqst.payload.data) {
  397. + ret = -EINVAL;
  398. + goto out;
  399. + }
  400. +
  401. + pldbuf = kzalloc(spec.length, GFP_KERNEL);
  402. + if (!pldbuf) {
  403. + status = -ENOMEM;
  404. + ret = -EFAULT;
  405. + goto out;
  406. + }
  407. +
  408. + if (copy_from_user(pldbuf, rqst.payload.data, spec.length)) {
  409. + ret = -EFAULT;
  410. + goto out;
  411. + }
  412. + }
  413. + spec.payload = pldbuf;
  414. +
  415. + // allocate response buffer
  416. + if (rsp.capacity) {
  417. + if (!rqst.response.data) {
  418. + ret = -EINVAL;
  419. + goto out;
  420. + }
  421. +
  422. + rspbuf = kzalloc(rsp.capacity, GFP_KERNEL);
  423. + if (!rspbuf) {
  424. + status = -ENOMEM;
  425. + ret = -EFAULT;
  426. + goto out;
  427. + }
  428. + }
  429. + rsp.pointer = rspbuf;
  430. +
  431. + // perform request
  432. + status = ssam_request_sync(ddev->ctrl, &spec, &rsp);
  433. + if (status)
  434. + goto out;
  435. +
  436. + // copy response to user-space
  437. + if (rsp.length) {
  438. + if (copy_to_user(rqst.response.data, rsp.pointer, rsp.length)) {
  439. + ret = -EFAULT;
  440. + goto out;
  441. + }
  442. + }
  443. +
  444. +out:
  445. + // always try to set response-length and status
  446. + tmp = put_user(rsp.length, &r->response.length);
  447. + if (!ret)
  448. + ret = tmp;
  449. +
  450. + tmp = put_user(status, &r->status);
  451. + if (!ret)
  452. + ret = tmp;
  453. +
  454. + // cleanup
  455. + if (pldbuf)
  456. + kfree(pldbuf);
  457. +
  458. + if (rspbuf)
  459. + kfree(rspbuf);
  460. +
  461. + return ret;
  462. +}
  463. +
  464. +static long ssam_dbgdev_getversion(struct file *file, unsigned long arg)
  465. +{
  466. + put_user(SSAM_DBGDEV_VERS, (u32 __user *)arg);
  467. + return 0;
  468. +}
  469. +
  470. +static long ssam_dbgdev_ioctl(struct file *file, unsigned int cmd,
  471. + unsigned long arg)
  472. +{
  473. + switch (cmd) {
  474. + case SSAM_DBGDEV_IOCTL_GETVERSION:
  475. + return ssam_dbgdev_getversion(file, arg);
  476. +
  477. + case SSAM_DBGDEV_IOCTL_REQUEST:
  478. + return ssam_dbgdev_request(file, arg);
  479. +
  480. + default:
  481. + return -EINVAL;
  482. + }
  483. +}
  484. +
  485. +const struct file_operations ssam_dbgdev_fops = {
  486. + .owner = THIS_MODULE,
  487. + .open = ssam_dbgdev_open,
  488. + .unlocked_ioctl = ssam_dbgdev_ioctl,
  489. + .compat_ioctl = ssam_dbgdev_ioctl,
  490. + .llseek = noop_llseek,
  491. +};
  492. +
  493. +static int ssam_dbgdev_probe(struct platform_device *pdev)
  494. +{
  495. + struct ssam_dbgdev *ddev;
  496. + struct ssam_controller *ctrl;
  497. + int status;
  498. +
  499. + status = ssam_client_bind(&pdev->dev, &ctrl);
  500. + if (status)
  501. + return status == -ENXIO ? -EPROBE_DEFER : status;
  502. +
  503. + ddev = devm_kzalloc(&pdev->dev, sizeof(struct ssam_dbgdev), GFP_KERNEL);
  504. + if (!ddev)
  505. + return -ENOMEM;
  506. +
  507. + ddev->ctrl = ctrl;
  508. +
  509. + ddev->dentry_dir = debugfs_create_dir("surface_sam", NULL);
  510. + if (IS_ERR(ddev->dentry_dir))
  511. + return PTR_ERR(ddev->dentry_dir);
  512. +
  513. + ddev->dentry_dev = debugfs_create_file("controller", 0600,
  514. + ddev->dentry_dir, ddev,
  515. + &ssam_dbgdev_fops);
  516. + if (IS_ERR(ddev->dentry_dev)) {
  517. + debugfs_remove(ddev->dentry_dir);
  518. + return PTR_ERR(ddev->dentry_dev);
  519. + }
  520. +
  521. + platform_set_drvdata(pdev, ddev);
  522. + return 0;
  523. +}
  524. +
  525. +static int ssam_dbgdev_remove(struct platform_device *pdev)
  526. +{
  527. + struct ssam_dbgdev *ddev = platform_get_drvdata(pdev);
  528. +
  529. + debugfs_remove(ddev->dentry_dev);
  530. + debugfs_remove(ddev->dentry_dir);
  531. +
  532. + platform_set_drvdata(pdev, NULL);
  533. + return 0;
  534. +}
  535. +
  536. +static void ssam_dbgdev_release(struct device *dev)
  537. +{
  538. + // nothing to do
  539. +}
  540. +
  541. +
  542. +static struct platform_device ssam_dbgdev_device = {
  543. + .name = SSAM_DBGDEV_NAME,
  544. + .id = PLATFORM_DEVID_NONE,
  545. + .dev.release = ssam_dbgdev_release,
  546. +};
  547. +
  548. +static struct platform_driver ssam_dbgdev_driver = {
  549. + .probe = ssam_dbgdev_probe,
  550. + .remove = ssam_dbgdev_remove,
  551. + .driver = {
  552. + .name = SSAM_DBGDEV_NAME,
  553. + },
  554. +};
  555. +
  556. +static int __init surface_sam_debugfs_init(void)
  557. +{
  558. + int status;
  559. +
  560. + status = platform_device_register(&ssam_dbgdev_device);
  561. + if (status)
  562. + return status;
  563. +
  564. + status = platform_driver_register(&ssam_dbgdev_driver);
  565. + if (status)
  566. + platform_device_unregister(&ssam_dbgdev_device);
  567. +
  568. + return status;
  569. +}
  570. +
  571. +static void __exit surface_sam_debugfs_exit(void)
  572. +{
  573. + platform_driver_unregister(&ssam_dbgdev_driver);
  574. + platform_device_unregister(&ssam_dbgdev_device);
  575. +}
  576. +
  577. +module_init(surface_sam_debugfs_init);
  578. +module_exit(surface_sam_debugfs_exit);
  579. +
  580. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  581. +MODULE_DESCRIPTION("DebugFS entries for Surface Aggregator Module");
  582. +MODULE_LICENSE("GPL");
  583. diff --git a/drivers/platform/x86/surface_sam/surface_sam_dtx.c b/drivers/platform/x86/surface_sam/surface_sam_dtx.c
  584. new file mode 100644
  585. index 0000000000000..9c844bb0f7739
  586. --- /dev/null
  587. +++ b/drivers/platform/x86/surface_sam/surface_sam_dtx.c
  588. @@ -0,0 +1,582 @@
  589. +// SPDX-License-Identifier: GPL-2.0-or-later
  590. +/*
  591. + * Detachment system (DTX) driver for Microsoft Surface Book 2.
  592. + */
  593. +
  594. +#include <linux/acpi.h>
  595. +#include <linux/delay.h>
  596. +#include <linux/fs.h>
  597. +#include <linux/input.h>
  598. +#include <linux/ioctl.h>
  599. +#include <linux/kernel.h>
  600. +#include <linux/miscdevice.h>
  601. +#include <linux/module.h>
  602. +#include <linux/poll.h>
  603. +#include <linux/rculist.h>
  604. +#include <linux/slab.h>
  605. +#include <linux/spinlock.h>
  606. +#include <linux/platform_device.h>
  607. +
  608. +#include "surface_sam_ssh.h"
  609. +
  610. +
  611. +#define USB_VENDOR_ID_MICROSOFT 0x045e
  612. +#define USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION 0x0922
  613. +
  614. +// name copied from MS device manager
  615. +#define DTX_INPUT_NAME "Microsoft Surface Base 2 Integration Device"
  616. +
  617. +
  618. +#define DTX_CMD_LATCH_LOCK _IO(0x11, 0x01)
  619. +#define DTX_CMD_LATCH_UNLOCK _IO(0x11, 0x02)
  620. +#define DTX_CMD_LATCH_REQUEST _IO(0x11, 0x03)
  621. +#define DTX_CMD_LATCH_OPEN _IO(0x11, 0x04)
  622. +#define DTX_CMD_GET_OPMODE _IOR(0x11, 0x05, int)
  623. +
  624. +#define SAM_EVENT_DTX_CID_CONNECTION 0x0c
  625. +#define SAM_EVENT_DTX_CID_BUTTON 0x0e
  626. +#define SAM_EVENT_DTX_CID_ERROR 0x0f
  627. +#define SAM_EVENT_DTX_CID_LATCH_STATUS 0x11
  628. +
  629. +#define DTX_OPMODE_TABLET 0x00
  630. +#define DTX_OPMODE_LAPTOP 0x01
  631. +#define DTX_OPMODE_STUDIO 0x02
  632. +
  633. +#define DTX_LATCH_CLOSED 0x00
  634. +#define DTX_LATCH_OPENED 0x01
  635. +
  636. +
  637. +// Warning: This must always be a power of 2!
  638. +#define DTX_CLIENT_BUF_SIZE 16
  639. +
  640. +#define DTX_CONNECT_OPMODE_DELAY 1000
  641. +
  642. +#define DTX_ERR KERN_ERR "surface_sam_dtx: "
  643. +#define DTX_WARN KERN_WARNING "surface_sam_dtx: "
  644. +
  645. +
  646. +struct surface_dtx_event {
  647. + u8 type;
  648. + u8 code;
  649. + u8 arg0;
  650. + u8 arg1;
  651. +} __packed;
  652. +
  653. +struct surface_dtx_dev {
  654. + struct ssam_controller *ctrl;
  655. +
  656. + struct ssam_event_notifier notif;
  657. + struct delayed_work opmode_work;
  658. + wait_queue_head_t waitq;
  659. + struct miscdevice mdev;
  660. + spinlock_t client_lock;
  661. + struct list_head client_list;
  662. + struct mutex mutex;
  663. + bool active;
  664. + spinlock_t input_lock;
  665. + struct input_dev *input_dev;
  666. +};
  667. +
  668. +struct surface_dtx_client {
  669. + struct list_head node;
  670. + struct surface_dtx_dev *ddev;
  671. + struct fasync_struct *fasync;
  672. + spinlock_t buffer_lock;
  673. + unsigned int buffer_head;
  674. + unsigned int buffer_tail;
  675. + struct surface_dtx_event buffer[DTX_CLIENT_BUF_SIZE];
  676. +};
  677. +
  678. +
  679. +static struct surface_dtx_dev surface_dtx_dev;
  680. +
  681. +
  682. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
  683. + .target_category = SSAM_SSH_TC_BAS,
  684. + .command_id = 0x06,
  685. + .instance_id = 0x00,
  686. + .channel = 0x01,
  687. +});
  688. +
  689. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
  690. + .target_category = SSAM_SSH_TC_BAS,
  691. + .command_id = 0x07,
  692. + .instance_id = 0x00,
  693. + .channel = 0x01,
  694. +});
  695. +
  696. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
  697. + .target_category = SSAM_SSH_TC_BAS,
  698. + .command_id = 0x08,
  699. + .instance_id = 0x00,
  700. + .channel = 0x01,
  701. +});
  702. +
  703. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_open, {
  704. + .target_category = SSAM_SSH_TC_BAS,
  705. + .command_id = 0x09,
  706. + .instance_id = 0x00,
  707. + .channel = 0x01,
  708. +});
  709. +
  710. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
  711. + .target_category = SSAM_SSH_TC_BAS,
  712. + .command_id = 0x0d,
  713. + .instance_id = 0x00,
  714. + .channel = 0x01,
  715. +});
  716. +
  717. +
  718. +static int dtx_bas_get_opmode(struct ssam_controller *ctrl, int __user *buf)
  719. +{
  720. + u8 opmode;
  721. + int status;
  722. +
  723. + status = ssam_bas_query_opmode(ctrl, &opmode);
  724. + if (status < 0)
  725. + return status;
  726. +
  727. + if (put_user(opmode, buf))
  728. + return -EACCES;
  729. +
  730. + return 0;
  731. +}
  732. +
  733. +
  734. +static int surface_dtx_open(struct inode *inode, struct file *file)
  735. +{
  736. + struct surface_dtx_dev *ddev = container_of(file->private_data, struct surface_dtx_dev, mdev);
  737. + struct surface_dtx_client *client;
  738. +
  739. + // initialize client
  740. + client = kzalloc(sizeof(struct surface_dtx_client), GFP_KERNEL);
  741. + if (!client)
  742. + return -ENOMEM;
  743. +
  744. + spin_lock_init(&client->buffer_lock);
  745. + client->buffer_head = 0;
  746. + client->buffer_tail = 0;
  747. + client->ddev = ddev;
  748. +
  749. + // attach client
  750. + spin_lock(&ddev->client_lock);
  751. + list_add_tail_rcu(&client->node, &ddev->client_list);
  752. + spin_unlock(&ddev->client_lock);
  753. +
  754. + file->private_data = client;
  755. + nonseekable_open(inode, file);
  756. +
  757. + return 0;
  758. +}
  759. +
  760. +static int surface_dtx_release(struct inode *inode, struct file *file)
  761. +{
  762. + struct surface_dtx_client *client = file->private_data;
  763. +
  764. + // detach client
  765. + spin_lock(&client->ddev->client_lock);
  766. + list_del_rcu(&client->node);
  767. + spin_unlock(&client->ddev->client_lock);
  768. + synchronize_rcu();
  769. +
  770. + kfree(client);
  771. + file->private_data = NULL;
  772. +
  773. + return 0;
  774. +}
  775. +
  776. +static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
  777. +{
  778. + struct surface_dtx_client *client = file->private_data;
  779. + struct surface_dtx_dev *ddev = client->ddev;
  780. + struct surface_dtx_event event;
  781. + size_t read = 0;
  782. + int status = 0;
  783. +
  784. + if (count != 0 && count < sizeof(struct surface_dtx_event))
  785. + return -EINVAL;
  786. +
  787. + if (!ddev->active)
  788. + return -ENODEV;
  789. +
  790. + // check availability
  791. + if (client->buffer_head == client->buffer_tail) {
  792. + if (file->f_flags & O_NONBLOCK)
  793. + return -EAGAIN;
  794. +
  795. + status = wait_event_interruptible(ddev->waitq,
  796. + client->buffer_head != client->buffer_tail ||
  797. + !ddev->active);
  798. + if (status)
  799. + return status;
  800. +
  801. + if (!ddev->active)
  802. + return -ENODEV;
  803. + }
  804. +
  805. + // copy events one by one
  806. + while (read + sizeof(struct surface_dtx_event) <= count) {
  807. + spin_lock_irq(&client->buffer_lock);
  808. +
  809. + if (client->buffer_head == client->buffer_tail) {
  810. + spin_unlock_irq(&client->buffer_lock);
  811. + break;
  812. + }
  813. +
  814. + // get one event
  815. + event = client->buffer[client->buffer_tail];
  816. + client->buffer_tail = (client->buffer_tail + 1) & (DTX_CLIENT_BUF_SIZE - 1);
  817. + spin_unlock_irq(&client->buffer_lock);
  818. +
  819. + // copy to userspace
  820. + if (copy_to_user(buf, &event, sizeof(struct surface_dtx_event)))
  821. + return -EFAULT;
  822. +
  823. + read += sizeof(struct surface_dtx_event);
  824. + }
  825. +
  826. + return read;
  827. +}
  828. +
  829. +static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
  830. +{
  831. + struct surface_dtx_client *client = file->private_data;
  832. + int mask;
  833. +
  834. + poll_wait(file, &client->ddev->waitq, pt);
  835. +
  836. + if (client->ddev->active)
  837. + mask = EPOLLOUT | EPOLLWRNORM;
  838. + else
  839. + mask = EPOLLHUP | EPOLLERR;
  840. +
  841. + if (client->buffer_head != client->buffer_tail)
  842. + mask |= EPOLLIN | EPOLLRDNORM;
  843. +
  844. + return mask;
  845. +}
  846. +
  847. +static int surface_dtx_fasync(int fd, struct file *file, int on)
  848. +{
  849. + struct surface_dtx_client *client = file->private_data;
  850. +
  851. + return fasync_helper(fd, file, on, &client->fasync);
  852. +}
  853. +
  854. +static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  855. +{
  856. + struct surface_dtx_client *client = file->private_data;
  857. + struct surface_dtx_dev *ddev = client->ddev;
  858. + int status;
  859. +
  860. + status = mutex_lock_interruptible(&ddev->mutex);
  861. + if (status)
  862. + return status;
  863. +
  864. + if (!ddev->active) {
  865. + mutex_unlock(&ddev->mutex);
  866. + return -ENODEV;
  867. + }
  868. +
  869. + switch (cmd) {
  870. + case DTX_CMD_LATCH_LOCK:
  871. + status = ssam_bas_latch_lock(ddev->ctrl);
  872. + break;
  873. +
  874. + case DTX_CMD_LATCH_UNLOCK:
  875. + status = ssam_bas_latch_unlock(ddev->ctrl);
  876. + break;
  877. +
  878. + case DTX_CMD_LATCH_REQUEST:
  879. + status = ssam_bas_latch_request(ddev->ctrl);
  880. + break;
  881. +
  882. + case DTX_CMD_LATCH_OPEN:
  883. + status = ssam_bas_latch_open(ddev->ctrl);
  884. + break;
  885. +
  886. + case DTX_CMD_GET_OPMODE:
  887. + status = dtx_bas_get_opmode(ddev->ctrl, (int __user *)arg);
  888. + break;
  889. +
  890. + default:
  891. + status = -EINVAL;
  892. + break;
  893. + }
  894. +
  895. + mutex_unlock(&ddev->mutex);
  896. + return status;
  897. +}
  898. +
  899. +static const struct file_operations surface_dtx_fops = {
  900. + .owner = THIS_MODULE,
  901. + .open = surface_dtx_open,
  902. + .release = surface_dtx_release,
  903. + .read = surface_dtx_read,
  904. + .poll = surface_dtx_poll,
  905. + .fasync = surface_dtx_fasync,
  906. + .unlocked_ioctl = surface_dtx_ioctl,
  907. + .llseek = no_llseek,
  908. +};
  909. +
  910. +static struct surface_dtx_dev surface_dtx_dev = {
  911. + .mdev = {
  912. + .minor = MISC_DYNAMIC_MINOR,
  913. + .name = "surface_dtx",
  914. + .fops = &surface_dtx_fops,
  915. + },
  916. + .client_lock = __SPIN_LOCK_UNLOCKED(),
  917. + .input_lock = __SPIN_LOCK_UNLOCKED(),
  918. + .mutex = __MUTEX_INITIALIZER(surface_dtx_dev.mutex),
  919. + .active = false,
  920. +};
  921. +
  922. +
  923. +static void surface_dtx_push_event(struct surface_dtx_dev *ddev, struct surface_dtx_event *event)
  924. +{
  925. + struct surface_dtx_client *client;
  926. +
  927. + rcu_read_lock();
  928. + list_for_each_entry_rcu(client, &ddev->client_list, node) {
  929. + spin_lock(&client->buffer_lock);
  930. +
  931. + client->buffer[client->buffer_head++] = *event;
  932. + client->buffer_head &= DTX_CLIENT_BUF_SIZE - 1;
  933. +
  934. + if (unlikely(client->buffer_head == client->buffer_tail)) {
  935. + printk(DTX_WARN "event buffer overrun\n");
  936. + client->buffer_tail = (client->buffer_tail + 1) & (DTX_CLIENT_BUF_SIZE - 1);
  937. + }
  938. +
  939. + spin_unlock(&client->buffer_lock);
  940. +
  941. + kill_fasync(&client->fasync, SIGIO, POLL_IN);
  942. + }
  943. + rcu_read_unlock();
  944. +
  945. + wake_up_interruptible(&ddev->waitq);
  946. +}
  947. +
  948. +
  949. +static void surface_dtx_update_opmpde(struct surface_dtx_dev *ddev)
  950. +{
  951. + struct surface_dtx_event event;
  952. + u8 opmode;
  953. + int status;
  954. +
  955. + // get operation mode
  956. + status = ssam_bas_query_opmode(ddev->ctrl, &opmode);
  957. + if (status < 0) {
  958. + printk(DTX_ERR "EC request failed with error %d\n", status);
  959. + return;
  960. + }
  961. +
  962. + // send DTX event
  963. + event.type = 0x11;
  964. + event.code = 0x0D;
  965. + event.arg0 = opmode;
  966. + event.arg1 = 0x00;
  967. +
  968. + surface_dtx_push_event(ddev, &event);
  969. +
  970. + // send SW_TABLET_MODE event
  971. + spin_lock(&ddev->input_lock);
  972. + input_report_switch(ddev->input_dev, SW_TABLET_MODE, opmode != DTX_OPMODE_LAPTOP);
  973. + input_sync(ddev->input_dev);
  974. + spin_unlock(&ddev->input_lock);
  975. +}
  976. +
  977. +static void surface_dtx_opmode_workfn(struct work_struct *work)
  978. +{
  979. + struct surface_dtx_dev *ddev = container_of(work, struct surface_dtx_dev, opmode_work.work);
  980. +
  981. + surface_dtx_update_opmpde(ddev);
  982. +}
  983. +
  984. +static u32 surface_dtx_notification(struct ssam_notifier_block *nb, const struct ssam_event *in_event)
  985. +{
  986. + struct surface_dtx_dev *ddev = container_of(nb, struct surface_dtx_dev, notif.base);
  987. + struct surface_dtx_event event;
  988. + unsigned long delay;
  989. +
  990. + switch (in_event->command_id) {
  991. + case SAM_EVENT_DTX_CID_CONNECTION:
  992. + case SAM_EVENT_DTX_CID_BUTTON:
  993. + case SAM_EVENT_DTX_CID_ERROR:
  994. + case SAM_EVENT_DTX_CID_LATCH_STATUS:
  995. + if (in_event->length > 2) {
  996. + printk(DTX_ERR "unexpected payload size (cid: %x, len: %u)\n",
  997. + in_event->command_id, in_event->length);
  998. + return SSAM_NOTIF_HANDLED;
  999. + }
  1000. +
  1001. + event.type = in_event->target_category;
  1002. + event.code = in_event->command_id;
  1003. + event.arg0 = in_event->length >= 1 ? in_event->data[0] : 0x00;
  1004. + event.arg1 = in_event->length >= 2 ? in_event->data[1] : 0x00;
  1005. + surface_dtx_push_event(ddev, &event);
  1006. + break;
  1007. +
  1008. + default:
  1009. + return 0;
  1010. + }
  1011. +
  1012. + // update device mode
  1013. + if (in_event->command_id == SAM_EVENT_DTX_CID_CONNECTION) {
  1014. + delay = event.arg0 ? DTX_CONNECT_OPMODE_DELAY : 0;
  1015. + schedule_delayed_work(&ddev->opmode_work, delay);
  1016. + }
  1017. +
  1018. + return SSAM_NOTIF_HANDLED;
  1019. +}
  1020. +
  1021. +
  1022. +static struct input_dev *surface_dtx_register_inputdev(
  1023. + struct platform_device *pdev, struct ssam_controller *ctrl)
  1024. +{
  1025. + struct input_dev *input_dev;
  1026. + u8 opmode;
  1027. + int status;
  1028. +
  1029. + input_dev = input_allocate_device();
  1030. + if (!input_dev)
  1031. + return ERR_PTR(-ENOMEM);
  1032. +
  1033. + input_dev->name = DTX_INPUT_NAME;
  1034. + input_dev->dev.parent = &pdev->dev;
  1035. + input_dev->id.bustype = BUS_VIRTUAL;
  1036. + input_dev->id.vendor = USB_VENDOR_ID_MICROSOFT;
  1037. + input_dev->id.product = USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION;
  1038. +
  1039. + input_set_capability(input_dev, EV_SW, SW_TABLET_MODE);
  1040. +
  1041. + status = ssam_bas_query_opmode(ctrl, &opmode);
  1042. + if (status < 0) {
  1043. + input_free_device(input_dev);
  1044. + return ERR_PTR(status);
  1045. + }
  1046. +
  1047. + input_report_switch(input_dev, SW_TABLET_MODE, opmode != DTX_OPMODE_LAPTOP);
  1048. +
  1049. + status = input_register_device(input_dev);
  1050. + if (status) {
  1051. + input_unregister_device(input_dev);
  1052. + return ERR_PTR(status);
  1053. + }
  1054. +
  1055. + return input_dev;
  1056. +}
  1057. +
  1058. +
  1059. +static int surface_sam_dtx_probe(struct platform_device *pdev)
  1060. +{
  1061. + struct surface_dtx_dev *ddev = &surface_dtx_dev;
  1062. + struct ssam_controller *ctrl;
  1063. + struct input_dev *input_dev;
  1064. + int status;
  1065. +
  1066. + // link to ec
  1067. + status = ssam_client_bind(&pdev->dev, &ctrl);
  1068. + if (status)
  1069. + return status == -ENXIO ? -EPROBE_DEFER : status;
  1070. +
  1071. + input_dev = surface_dtx_register_inputdev(pdev, ctrl);
  1072. + if (IS_ERR(input_dev))
  1073. + return PTR_ERR(input_dev);
  1074. +
  1075. + // initialize device
  1076. + mutex_lock(&ddev->mutex);
  1077. + if (ddev->active) {
  1078. + mutex_unlock(&ddev->mutex);
  1079. + status = -ENODEV;
  1080. + goto err_register;
  1081. + }
  1082. +
  1083. + ddev->ctrl = ctrl;
  1084. + INIT_DELAYED_WORK(&ddev->opmode_work, surface_dtx_opmode_workfn);
  1085. + INIT_LIST_HEAD(&ddev->client_list);
  1086. + init_waitqueue_head(&ddev->waitq);
  1087. + ddev->active = true;
  1088. + ddev->input_dev = input_dev;
  1089. + mutex_unlock(&ddev->mutex);
  1090. +
  1091. + status = misc_register(&ddev->mdev);
  1092. + if (status)
  1093. + goto err_register;
  1094. +
  1095. + // set up events
  1096. + ddev->notif.base.priority = 1;
  1097. + ddev->notif.base.fn = surface_dtx_notification;
  1098. + ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  1099. + ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
  1100. + ddev->notif.event.id.instance = 0;
  1101. + ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
  1102. +
  1103. + status = ssam_notifier_register(ctrl, &ddev->notif);
  1104. + if (status)
  1105. + goto err_events_setup;
  1106. +
  1107. + return 0;
  1108. +
  1109. +err_events_setup:
  1110. + misc_deregister(&ddev->mdev);
  1111. +err_register:
  1112. + input_unregister_device(ddev->input_dev);
  1113. + return status;
  1114. +}
  1115. +
  1116. +static int surface_sam_dtx_remove(struct platform_device *pdev)
  1117. +{
  1118. + struct surface_dtx_dev *ddev = &surface_dtx_dev;
  1119. + struct surface_dtx_client *client;
  1120. +
  1121. + mutex_lock(&ddev->mutex);
  1122. + if (!ddev->active) {
  1123. + mutex_unlock(&ddev->mutex);
  1124. + return 0;
  1125. + }
  1126. +
  1127. + // mark as inactive
  1128. + ddev->active = false;
  1129. + mutex_unlock(&ddev->mutex);
  1130. +
  1131. + // After this call we're guaranteed that no more input events will arive
  1132. + ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
  1133. +
  1134. + // wake up clients
  1135. + spin_lock(&ddev->client_lock);
  1136. + list_for_each_entry(client, &ddev->client_list, node) {
  1137. + kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  1138. + }
  1139. + spin_unlock(&ddev->client_lock);
  1140. +
  1141. + wake_up_interruptible(&ddev->waitq);
  1142. +
  1143. + // unregister user-space devices
  1144. + input_unregister_device(ddev->input_dev);
  1145. + misc_deregister(&ddev->mdev);
  1146. +
  1147. + return 0;
  1148. +}
  1149. +
  1150. +
  1151. +static const struct acpi_device_id surface_sam_dtx_match[] = {
  1152. + { "MSHW0133", 0 },
  1153. + { },
  1154. +};
  1155. +MODULE_DEVICE_TABLE(acpi, surface_sam_dtx_match);
  1156. +
  1157. +static struct platform_driver surface_sam_dtx = {
  1158. + .probe = surface_sam_dtx_probe,
  1159. + .remove = surface_sam_dtx_remove,
  1160. + .driver = {
  1161. + .name = "surface_sam_dtx",
  1162. + .acpi_match_table = surface_sam_dtx_match,
  1163. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1164. + },
  1165. +};
  1166. +module_platform_driver(surface_sam_dtx);
  1167. +
  1168. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  1169. +MODULE_DESCRIPTION("Surface Detachment System (DTX) Driver for 5th Generation Surface Devices");
  1170. +MODULE_LICENSE("GPL");
  1171. diff --git a/drivers/platform/x86/surface_sam/surface_sam_hps.c b/drivers/platform/x86/surface_sam/surface_sam_hps.c
  1172. new file mode 100644
  1173. index 0000000000000..b11f9fa8095fb
  1174. --- /dev/null
  1175. +++ b/drivers/platform/x86/surface_sam/surface_sam_hps.c
  1176. @@ -0,0 +1,1287 @@
  1177. +// SPDX-License-Identifier: GPL-2.0-or-later
  1178. +/*
  1179. + * Surface dGPU hot-plug system driver.
  1180. + * Supports explicit setting of the dGPU power-state on the Surface Book 2 and
  1181. + * properly handles hot-plugging by detaching the base.
  1182. + */
  1183. +
  1184. +#include <linux/acpi.h>
  1185. +#include <linux/delay.h>
  1186. +#include <linux/gpio.h>
  1187. +#include <linux/kernel.h>
  1188. +#include <linux/module.h>
  1189. +#include <linux/mutex.h>
  1190. +#include <linux/pci.h>
  1191. +#include <linux/platform_device.h>
  1192. +#include <linux/sysfs.h>
  1193. +
  1194. +#include "surface_sam_ssh.h"
  1195. +#include "surface_sam_san.h"
  1196. +
  1197. +
  1198. +// TODO: vgaswitcheroo integration
  1199. +
  1200. +
  1201. +static void dbg_dump_drvsta(struct platform_device *pdev, const char *prefix);
  1202. +
  1203. +
  1204. +#define SHPS_DSM_REVISION 1
  1205. +#define SHPS_DSM_GPU_ADDRS 0x02
  1206. +#define SHPS_DSM_GPU_POWER 0x05
  1207. +static const guid_t SHPS_DSM_UUID =
  1208. + GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd,
  1209. + 0x32, 0x0e, 0x10, 0x36, 0x0a);
  1210. +
  1211. +
  1212. +#define SAM_DGPU_TC 0x13
  1213. +#define SAM_DGPU_CID_POWERON 0x02
  1214. +#define ACPI_SGCP_NOTIFY_POWER_ON 0x81
  1215. +
  1216. +#define SHPS_DSM_GPU_ADDRS_RP "RP5_PCIE"
  1217. +#define SHPS_DSM_GPU_ADDRS_DGPU "DGPU_PCIE"
  1218. +#define SHPS_PCI_GPU_ADDR_RP "\\_SB.PCI0.RP13._ADR"
  1219. +
  1220. +static const struct acpi_gpio_params gpio_base_presence_int = { 0, 0, false };
  1221. +static const struct acpi_gpio_params gpio_base_presence = { 1, 0, false };
  1222. +static const struct acpi_gpio_params gpio_dgpu_power_int = { 2, 0, false };
  1223. +static const struct acpi_gpio_params gpio_dgpu_power = { 3, 0, false };
  1224. +static const struct acpi_gpio_params gpio_dgpu_presence_int = { 4, 0, false };
  1225. +static const struct acpi_gpio_params gpio_dgpu_presence = { 5, 0, false };
  1226. +
  1227. +static const struct acpi_gpio_mapping shps_acpi_gpios[] = {
  1228. + { "base_presence-int-gpio", &gpio_base_presence_int, 1 },
  1229. + { "base_presence-gpio", &gpio_base_presence, 1 },
  1230. + { "dgpu_power-int-gpio", &gpio_dgpu_power_int, 1 },
  1231. + { "dgpu_power-gpio", &gpio_dgpu_power, 1 },
  1232. + { "dgpu_presence-int-gpio", &gpio_dgpu_presence_int, 1 },
  1233. + { "dgpu_presence-gpio", &gpio_dgpu_presence, 1 },
  1234. + { },
  1235. +};
  1236. +
  1237. +
  1238. +enum shps_dgpu_power {
  1239. + SHPS_DGPU_POWER_OFF = 0,
  1240. + SHPS_DGPU_POWER_ON = 1,
  1241. + SHPS_DGPU_POWER_UNKNOWN = 2,
  1242. +};
  1243. +
  1244. +static const char *shps_dgpu_power_str(enum shps_dgpu_power power)
  1245. +{
  1246. + if (power == SHPS_DGPU_POWER_OFF)
  1247. + return "off";
  1248. + else if (power == SHPS_DGPU_POWER_ON)
  1249. + return "on";
  1250. + else if (power == SHPS_DGPU_POWER_UNKNOWN)
  1251. + return "unknown";
  1252. + else
  1253. + return "<invalid>";
  1254. +}
  1255. +
  1256. +enum shps_notification_method {
  1257. + SHPS_NOTIFICATION_METHOD_SAN = 1,
  1258. + SHPS_NOTIFICATION_METHOD_SGCP = 2
  1259. +};
  1260. +
  1261. +struct shps_hardware_traits {
  1262. + enum shps_notification_method notification_method;
  1263. + const char *dgpu_rp_pci_address;
  1264. +};
  1265. +
  1266. +struct shps_driver_data {
  1267. + struct ssam_controller *ctrl;
  1268. +
  1269. + struct mutex lock;
  1270. + struct pci_dev *dgpu_root_port;
  1271. + struct pci_saved_state *dgpu_root_port_state;
  1272. + struct gpio_desc *gpio_dgpu_power;
  1273. + struct gpio_desc *gpio_dgpu_presence;
  1274. + struct gpio_desc *gpio_base_presence;
  1275. + unsigned int irq_dgpu_presence;
  1276. + unsigned int irq_base_presence;
  1277. + unsigned long state;
  1278. + acpi_handle sgpc_handle;
  1279. + struct shps_hardware_traits hardware_traits;
  1280. +};
  1281. +
  1282. +struct shps_hardware_probe {
  1283. + const char *hardware_id;
  1284. + int generation;
  1285. + struct shps_hardware_traits *hardware_traits;
  1286. +};
  1287. +
  1288. +static struct shps_hardware_traits shps_gen1_hwtraits = {
  1289. + .notification_method = SHPS_NOTIFICATION_METHOD_SAN
  1290. +};
  1291. +
  1292. +static struct shps_hardware_traits shps_gen2_hwtraits = {
  1293. + .notification_method = SHPS_NOTIFICATION_METHOD_SGCP,
  1294. + .dgpu_rp_pci_address = SHPS_PCI_GPU_ADDR_RP
  1295. +};
  1296. +
  1297. +static const struct shps_hardware_probe shps_hardware_probe_match[] = {
  1298. + /* Surface Book 3 */
  1299. + { "MSHW0117", 2, &shps_gen2_hwtraits },
  1300. +
  1301. + /* Surface Book 2 (default, must be last entry) */
  1302. + { NULL, 1, &shps_gen1_hwtraits }
  1303. +};
  1304. +
  1305. +#define SHPS_STATE_BIT_PWRTGT 0 /* desired power state: 1 for on, 0 for off */
  1306. +#define SHPS_STATE_BIT_RPPWRON_SYNC 1 /* synchronous/requested power-up in progress */
  1307. +#define SHPS_STATE_BIT_WAKE_ENABLED 2 /* wakeup via base-presence GPIO enabled */
  1308. +
  1309. +
  1310. +#define SHPS_DGPU_PARAM_PERM 0644
  1311. +
  1312. +enum shps_dgpu_power_mp {
  1313. + SHPS_DGPU_MP_POWER_OFF = SHPS_DGPU_POWER_OFF,
  1314. + SHPS_DGPU_MP_POWER_ON = SHPS_DGPU_POWER_ON,
  1315. + SHPS_DGPU_MP_POWER_ASIS = -1,
  1316. +
  1317. + __SHPS_DGPU_MP_POWER_START = -1,
  1318. + __SHPS_DGPU_MP_POWER_END = 1,
  1319. +};
  1320. +
  1321. +static int param_dgpu_power_set(const char *val, const struct kernel_param *kp)
  1322. +{
  1323. + int power = SHPS_DGPU_MP_POWER_OFF;
  1324. + int status;
  1325. +
  1326. + status = kstrtoint(val, 0, &power);
  1327. + if (status)
  1328. + return status;
  1329. +
  1330. + if (power < __SHPS_DGPU_MP_POWER_START || power > __SHPS_DGPU_MP_POWER_END)
  1331. + return -EINVAL;
  1332. +
  1333. + return param_set_int(val, kp);
  1334. +}
  1335. +
  1336. +static const struct kernel_param_ops param_dgpu_power_ops = {
  1337. + .set = param_dgpu_power_set,
  1338. + .get = param_get_int,
  1339. +};
  1340. +
  1341. +static int param_dgpu_power_init = SHPS_DGPU_MP_POWER_OFF;
  1342. +static int param_dgpu_power_exit = SHPS_DGPU_MP_POWER_ON;
  1343. +static int param_dgpu_power_susp = SHPS_DGPU_MP_POWER_ASIS;
  1344. +static bool param_dtx_latch = true;
  1345. +
  1346. +module_param_cb(dgpu_power_init, &param_dgpu_power_ops, &param_dgpu_power_init, SHPS_DGPU_PARAM_PERM);
  1347. +module_param_cb(dgpu_power_exit, &param_dgpu_power_ops, &param_dgpu_power_exit, SHPS_DGPU_PARAM_PERM);
  1348. +module_param_cb(dgpu_power_susp, &param_dgpu_power_ops, &param_dgpu_power_susp, SHPS_DGPU_PARAM_PERM);
  1349. +module_param_named(dtx_latch, param_dtx_latch, bool, SHPS_DGPU_PARAM_PERM);
  1350. +
  1351. +MODULE_PARM_DESC(dgpu_power_init, "dGPU power state to be set on init (0: off / 1: on / 2: as-is, default: off)");
  1352. +MODULE_PARM_DESC(dgpu_power_exit, "dGPU power state to be set on exit (0: off / 1: on / 2: as-is, default: on)");
  1353. +MODULE_PARM_DESC(dgpu_power_susp, "dGPU power state to be set on exit (0: off / 1: on / 2: as-is, default: as-is)");
  1354. +MODULE_PARM_DESC(dtx_latch, "lock/unlock DTX base latch in accordance to power-state (Y/n)");
  1355. +
  1356. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
  1357. + .target_category = SSAM_SSH_TC_BAS,
  1358. + .command_id = 0x06,
  1359. + .instance_id = 0x00,
  1360. + .channel = 0x01,
  1361. +});
  1362. +
  1363. +static SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
  1364. + .target_category = SSAM_SSH_TC_BAS,
  1365. + .command_id = 0x07,
  1366. + .instance_id = 0x00,
  1367. + .channel = 0x01,
  1368. +});
  1369. +
  1370. +static int shps_dgpu_dsm_get_pci_addr_from_adr(struct platform_device *pdev, const char *entry) {
  1371. + acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  1372. + int status;
  1373. + struct acpi_object_list input;
  1374. + union acpi_object input_args[0];
  1375. + u64 device_addr;
  1376. + u8 bus, dev, fun;
  1377. +
  1378. + input.count = 0;
  1379. + input.pointer = input_args;
  1380. +
  1381. +
  1382. + status = acpi_evaluate_integer(handle, (acpi_string)entry, &input, &device_addr);
  1383. + if (status) {
  1384. + return -ENODEV;
  1385. + }
  1386. +
  1387. + bus = 0;
  1388. + dev = (device_addr & 0xFF0000) >> 16;
  1389. + fun = device_addr & 0xFF;
  1390. +
  1391. + dev_info(&pdev->dev, "found pci device at bus = %d, dev = %x, fun = %x\n",
  1392. + (u32)bus, (u32)dev, (u32)fun);
  1393. +
  1394. + return bus << 8 | PCI_DEVFN(dev, fun);
  1395. +}
  1396. +
  1397. +static int shps_dgpu_dsm_get_pci_addr_from_dsm(struct platform_device *pdev, const char *entry)
  1398. +{
  1399. + acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  1400. + union acpi_object *result;
  1401. + union acpi_object *e0;
  1402. + union acpi_object *e1;
  1403. + union acpi_object *e2;
  1404. + u64 device_addr = 0;
  1405. + u8 bus, dev, fun;
  1406. + int i;
  1407. +
  1408. +
  1409. + result = acpi_evaluate_dsm_typed(handle, &SHPS_DSM_UUID, SHPS_DSM_REVISION,
  1410. + SHPS_DSM_GPU_ADDRS, NULL, ACPI_TYPE_PACKAGE);
  1411. +
  1412. + if (IS_ERR_OR_NULL(result))
  1413. + return result ? PTR_ERR(result) : -EIO;
  1414. +
  1415. + // three entries per device: name, address, <integer>
  1416. + for (i = 0; i + 2 < result->package.count; i += 3) {
  1417. + e0 = &result->package.elements[i];
  1418. + e1 = &result->package.elements[i + 1];
  1419. + e2 = &result->package.elements[i + 2];
  1420. +
  1421. + if (e0->type != ACPI_TYPE_STRING) {
  1422. + ACPI_FREE(result);
  1423. + return -EIO;
  1424. + }
  1425. +
  1426. + if (e1->type != ACPI_TYPE_INTEGER) {
  1427. + ACPI_FREE(result);
  1428. + return -EIO;
  1429. + }
  1430. +
  1431. + if (e2->type != ACPI_TYPE_INTEGER) {
  1432. + ACPI_FREE(result);
  1433. + return -EIO;
  1434. + }
  1435. +
  1436. + if (strncmp(e0->string.pointer, entry, 64) == 0)
  1437. + device_addr = e1->integer.value;
  1438. + }
  1439. +
  1440. + ACPI_FREE(result);
  1441. + if (device_addr == 0)
  1442. + return -ENODEV;
  1443. +
  1444. +
  1445. + // convert address
  1446. + bus = (device_addr & 0x0FF00000) >> 20;
  1447. + dev = (device_addr & 0x000F8000) >> 15;
  1448. + fun = (device_addr & 0x00007000) >> 12;
  1449. +
  1450. + return bus << 8 | PCI_DEVFN(dev, fun);
  1451. +}
  1452. +
  1453. +static struct pci_dev *shps_dgpu_dsm_get_pci_dev(struct platform_device *pdev)
  1454. +{
  1455. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1456. + struct pci_dev *dev;
  1457. + int addr;
  1458. +
  1459. +
  1460. + if (drvdata->hardware_traits.dgpu_rp_pci_address) {
  1461. + addr = shps_dgpu_dsm_get_pci_addr_from_adr(pdev, drvdata->hardware_traits.dgpu_rp_pci_address);
  1462. + } else {
  1463. + addr = shps_dgpu_dsm_get_pci_addr_from_dsm(pdev, SHPS_DSM_GPU_ADDRS_RP);
  1464. + }
  1465. +
  1466. + if (addr < 0)
  1467. + return ERR_PTR(addr);
  1468. +
  1469. + dev = pci_get_domain_bus_and_slot(0, (addr & 0xFF00) >> 8, addr & 0xFF);
  1470. + return dev ? dev : ERR_PTR(-ENODEV);
  1471. +}
  1472. +
  1473. +
  1474. +static int shps_dgpu_dsm_get_power_unlocked(struct platform_device *pdev)
  1475. +{
  1476. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1477. + struct gpio_desc *gpio = drvdata->gpio_dgpu_power;
  1478. + int status;
  1479. +
  1480. + status = gpiod_get_value_cansleep(gpio);
  1481. + if (status < 0)
  1482. + return status;
  1483. +
  1484. + return status == 0 ? SHPS_DGPU_POWER_OFF : SHPS_DGPU_POWER_ON;
  1485. +}
  1486. +
  1487. +static int shps_dgpu_dsm_get_power(struct platform_device *pdev)
  1488. +{
  1489. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1490. + int status;
  1491. +
  1492. + mutex_lock(&drvdata->lock);
  1493. + status = shps_dgpu_dsm_get_power_unlocked(pdev);
  1494. + mutex_unlock(&drvdata->lock);
  1495. +
  1496. + return status;
  1497. +}
  1498. +
  1499. +static int __shps_dgpu_dsm_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
  1500. +{
  1501. + acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  1502. + union acpi_object *result;
  1503. + union acpi_object param;
  1504. +
  1505. + dev_info(&pdev->dev, "setting dGPU direct power to \'%s\'\n", shps_dgpu_power_str(power));
  1506. +
  1507. + param.type = ACPI_TYPE_INTEGER;
  1508. + param.integer.value = power == SHPS_DGPU_POWER_ON;
  1509. +
  1510. + result = acpi_evaluate_dsm_typed(handle, &SHPS_DSM_UUID, SHPS_DSM_REVISION,
  1511. + SHPS_DSM_GPU_POWER, &param, ACPI_TYPE_BUFFER);
  1512. +
  1513. + if (IS_ERR_OR_NULL(result))
  1514. + return result ? PTR_ERR(result) : -EIO;
  1515. +
  1516. + // check for the expected result
  1517. + if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
  1518. + ACPI_FREE(result);
  1519. + return -EIO;
  1520. + }
  1521. +
  1522. + ACPI_FREE(result);
  1523. + return 0;
  1524. +}
  1525. +
  1526. +static int shps_dgpu_dsm_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
  1527. +{
  1528. + int status;
  1529. +
  1530. + if (power != SHPS_DGPU_POWER_ON && power != SHPS_DGPU_POWER_OFF)
  1531. + return -EINVAL;
  1532. +
  1533. + status = shps_dgpu_dsm_get_power_unlocked(pdev);
  1534. + if (status < 0)
  1535. + return status;
  1536. + if (status == power)
  1537. + return 0;
  1538. +
  1539. + return __shps_dgpu_dsm_set_power_unlocked(pdev, power);
  1540. +}
  1541. +
  1542. +static int shps_dgpu_dsm_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
  1543. +{
  1544. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1545. + int status;
  1546. +
  1547. + mutex_lock(&drvdata->lock);
  1548. + status = shps_dgpu_dsm_set_power_unlocked(pdev, power);
  1549. + mutex_unlock(&drvdata->lock);
  1550. +
  1551. + return status;
  1552. +}
  1553. +
  1554. +
  1555. +static bool shps_rp_link_up(struct pci_dev *rp)
  1556. +{
  1557. + u16 lnksta = 0, sltsta = 0;
  1558. +
  1559. + pcie_capability_read_word(rp, PCI_EXP_LNKSTA, &lnksta);
  1560. + pcie_capability_read_word(rp, PCI_EXP_SLTSTA, &sltsta);
  1561. +
  1562. + return (lnksta & PCI_EXP_LNKSTA_DLLLA) || (sltsta & PCI_EXP_SLTSTA_PDS);
  1563. +}
  1564. +
  1565. +
  1566. +static int shps_dgpu_rp_get_power_unlocked(struct platform_device *pdev)
  1567. +{
  1568. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1569. + struct pci_dev *rp = drvdata->dgpu_root_port;
  1570. +
  1571. + if (rp->current_state == PCI_D3hot || rp->current_state == PCI_D3cold)
  1572. + return SHPS_DGPU_POWER_OFF;
  1573. + else if (rp->current_state == PCI_UNKNOWN || rp->current_state == PCI_POWER_ERROR)
  1574. + return SHPS_DGPU_POWER_UNKNOWN;
  1575. + else
  1576. + return SHPS_DGPU_POWER_ON;
  1577. +}
  1578. +
  1579. +static int shps_dgpu_rp_get_power(struct platform_device *pdev)
  1580. +{
  1581. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1582. + int status;
  1583. +
  1584. + mutex_lock(&drvdata->lock);
  1585. + status = shps_dgpu_rp_get_power_unlocked(pdev);
  1586. + mutex_unlock(&drvdata->lock);
  1587. +
  1588. + return status;
  1589. +}
  1590. +
  1591. +static int __shps_dgpu_rp_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
  1592. +{
  1593. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1594. + struct pci_dev *rp = drvdata->dgpu_root_port;
  1595. + int status, i;
  1596. +
  1597. + dev_info(&pdev->dev, "setting dGPU power state to \'%s\'\n", shps_dgpu_power_str(power));
  1598. +
  1599. + dbg_dump_drvsta(pdev, "__shps_dgpu_rp_set_power_unlocked.1");
  1600. + if (power == SHPS_DGPU_POWER_ON) {
  1601. + set_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state);
  1602. + pci_set_power_state(rp, PCI_D0);
  1603. +
  1604. + if (drvdata->dgpu_root_port_state)
  1605. + pci_load_and_free_saved_state(rp, &drvdata->dgpu_root_port_state);
  1606. +
  1607. + pci_restore_state(rp);
  1608. +
  1609. + if (!pci_is_enabled(rp))
  1610. + pci_enable_device(rp);
  1611. +
  1612. + pci_set_master(rp);
  1613. + clear_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state);
  1614. +
  1615. + set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1616. + } else {
  1617. + if (!drvdata->dgpu_root_port_state) {
  1618. + pci_save_state(rp);
  1619. + drvdata->dgpu_root_port_state = pci_store_saved_state(rp);
  1620. + }
  1621. +
  1622. + /*
  1623. + * To properly update the hot-plug system we need to "remove" the dGPU
  1624. + * before disabling it and sending it to D3cold. Following this, we
  1625. + * need to wait for the link and slot status to actually change.
  1626. + */
  1627. + status = shps_dgpu_dsm_set_power_unlocked(pdev, SHPS_DGPU_POWER_OFF);
  1628. + if (status)
  1629. + return status;
  1630. +
  1631. + for (i = 0; i < 20 && shps_rp_link_up(rp); i++)
  1632. + msleep(50);
  1633. +
  1634. + if (shps_rp_link_up(rp))
  1635. + dev_err(&pdev->dev, "dGPU removal via DSM timed out\n");
  1636. +
  1637. + pci_clear_master(rp);
  1638. +
  1639. + if (pci_is_enabled(rp))
  1640. + pci_disable_device(rp);
  1641. +
  1642. + pci_set_power_state(rp, PCI_D3cold);
  1643. +
  1644. + clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1645. + }
  1646. + dbg_dump_drvsta(pdev, "__shps_dgpu_rp_set_power_unlocked.2");
  1647. +
  1648. + return 0;
  1649. +}
  1650. +
  1651. +static int shps_dgpu_rp_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
  1652. +{
  1653. + int status;
  1654. +
  1655. + if (power != SHPS_DGPU_POWER_ON && power != SHPS_DGPU_POWER_OFF)
  1656. + return -EINVAL;
  1657. +
  1658. + status = shps_dgpu_rp_get_power_unlocked(pdev);
  1659. + if (status < 0)
  1660. + return status;
  1661. + if (status == power)
  1662. + return 0;
  1663. +
  1664. + return __shps_dgpu_rp_set_power_unlocked(pdev, power);
  1665. +}
  1666. +
  1667. +static int shps_dgpu_rp_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
  1668. +{
  1669. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1670. + int status;
  1671. +
  1672. + mutex_lock(&drvdata->lock);
  1673. + status = shps_dgpu_rp_set_power_unlocked(pdev, power);
  1674. + mutex_unlock(&drvdata->lock);
  1675. +
  1676. + return status;
  1677. +}
  1678. +
  1679. +
  1680. +static int shps_dgpu_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
  1681. +{
  1682. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1683. + int status;
  1684. +
  1685. + if (!param_dtx_latch)
  1686. + return shps_dgpu_rp_set_power(pdev, power);
  1687. +
  1688. + if (power == SHPS_DGPU_POWER_ON) {
  1689. + status = ssam_bas_latch_lock(drvdata->ctrl);
  1690. + if (status)
  1691. + return status;
  1692. +
  1693. + status = shps_dgpu_rp_set_power(pdev, power);
  1694. + if (status)
  1695. + ssam_bas_latch_unlock(drvdata->ctrl);
  1696. +
  1697. + } else {
  1698. + status = shps_dgpu_rp_set_power(pdev, power);
  1699. + if (status)
  1700. + return status;
  1701. +
  1702. + status = ssam_bas_latch_unlock(drvdata->ctrl);
  1703. + }
  1704. +
  1705. + return status;
  1706. +}
  1707. +
  1708. +
  1709. +static int shps_dgpu_is_present(struct platform_device *pdev)
  1710. +{
  1711. + struct shps_driver_data *drvdata;
  1712. +
  1713. + drvdata = platform_get_drvdata(pdev);
  1714. + return gpiod_get_value_cansleep(drvdata->gpio_dgpu_presence);
  1715. +}
  1716. +
  1717. +
  1718. +static ssize_t dgpu_power_show(struct device *dev, struct device_attribute *attr, char *data)
  1719. +{
  1720. + struct platform_device *pdev = to_platform_device(dev);
  1721. + int power = shps_dgpu_rp_get_power(pdev);
  1722. +
  1723. + if (power < 0)
  1724. + return power;
  1725. +
  1726. + return sprintf(data, "%s\n", shps_dgpu_power_str(power));
  1727. +}
  1728. +
  1729. +static ssize_t dgpu_power_store(struct device *dev, struct device_attribute *attr,
  1730. + const char *data, size_t count)
  1731. +{
  1732. + struct platform_device *pdev = to_platform_device(dev);
  1733. + enum shps_dgpu_power power;
  1734. + bool b = false;
  1735. + int status;
  1736. +
  1737. + status = kstrtobool(data, &b);
  1738. + if (status)
  1739. + return status;
  1740. +
  1741. + status = shps_dgpu_is_present(pdev);
  1742. + if (status <= 0)
  1743. + return status < 0 ? status : -EPERM;
  1744. +
  1745. + power = b ? SHPS_DGPU_POWER_ON : SHPS_DGPU_POWER_OFF;
  1746. + status = shps_dgpu_set_power(pdev, power);
  1747. +
  1748. + return status < 0 ? status : count;
  1749. +}
  1750. +
  1751. +static ssize_t dgpu_power_dsm_show(struct device *dev, struct device_attribute *attr, char *data)
  1752. +{
  1753. + struct platform_device *pdev = to_platform_device(dev);
  1754. + int power = shps_dgpu_dsm_get_power(pdev);
  1755. +
  1756. + if (power < 0)
  1757. + return power;
  1758. +
  1759. + return sprintf(data, "%s\n", shps_dgpu_power_str(power));
  1760. +}
  1761. +
  1762. +static ssize_t dgpu_power_dsm_store(struct device *dev, struct device_attribute *attr,
  1763. + const char *data, size_t count)
  1764. +{
  1765. + struct platform_device *pdev = to_platform_device(dev);
  1766. + enum shps_dgpu_power power;
  1767. + bool b = false;
  1768. + int status;
  1769. +
  1770. + status = kstrtobool(data, &b);
  1771. + if (status)
  1772. + return status;
  1773. +
  1774. + status = shps_dgpu_is_present(pdev);
  1775. + if (status <= 0)
  1776. + return status < 0 ? status : -EPERM;
  1777. +
  1778. + power = b ? SHPS_DGPU_POWER_ON : SHPS_DGPU_POWER_OFF;
  1779. + status = shps_dgpu_dsm_set_power(pdev, power);
  1780. +
  1781. + return status < 0 ? status : count;
  1782. +}
  1783. +
  1784. +static DEVICE_ATTR_RW(dgpu_power);
  1785. +static DEVICE_ATTR_RW(dgpu_power_dsm);
  1786. +
  1787. +static struct attribute *shps_power_attrs[] = {
  1788. + &dev_attr_dgpu_power.attr,
  1789. + &dev_attr_dgpu_power_dsm.attr,
  1790. + NULL,
  1791. +};
  1792. +ATTRIBUTE_GROUPS(shps_power);
  1793. +
  1794. +
  1795. +static void dbg_dump_power_states(struct platform_device *pdev, const char *prefix)
  1796. +{
  1797. + enum shps_dgpu_power power_dsm;
  1798. + enum shps_dgpu_power power_rp;
  1799. + int status;
  1800. +
  1801. + status = shps_dgpu_rp_get_power_unlocked(pdev);
  1802. + if (status < 0)
  1803. + dev_err(&pdev->dev, "%s: failed to get root-port power state: %d\n", prefix, status);
  1804. + power_rp = status;
  1805. +
  1806. + status = shps_dgpu_rp_get_power_unlocked(pdev);
  1807. + if (status < 0)
  1808. + dev_err(&pdev->dev, "%s: failed to get direct power state: %d\n", prefix, status);
  1809. + power_dsm = status;
  1810. +
  1811. + dev_dbg(&pdev->dev, "%s: root-port power state: %d\n", prefix, power_rp);
  1812. + dev_dbg(&pdev->dev, "%s: direct power state: %d\n", prefix, power_dsm);
  1813. +}
  1814. +
  1815. +static void dbg_dump_pciesta(struct platform_device *pdev, const char *prefix)
  1816. +{
  1817. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1818. + struct pci_dev *rp = drvdata->dgpu_root_port;
  1819. + u16 lnksta, lnksta2, sltsta, sltsta2;
  1820. +
  1821. + pcie_capability_read_word(rp, PCI_EXP_LNKSTA, &lnksta);
  1822. + pcie_capability_read_word(rp, PCI_EXP_LNKSTA2, &lnksta2);
  1823. + pcie_capability_read_word(rp, PCI_EXP_SLTSTA, &sltsta);
  1824. + pcie_capability_read_word(rp, PCI_EXP_SLTSTA2, &sltsta2);
  1825. +
  1826. + dev_dbg(&pdev->dev, "%s: LNKSTA: 0x%04x\n", prefix, lnksta);
  1827. + dev_dbg(&pdev->dev, "%s: LNKSTA2: 0x%04x\n", prefix, lnksta2);
  1828. + dev_dbg(&pdev->dev, "%s: SLTSTA: 0x%04x\n", prefix, sltsta);
  1829. + dev_dbg(&pdev->dev, "%s: SLTSTA2: 0x%04x\n", prefix, sltsta2);
  1830. +}
  1831. +
  1832. +static void dbg_dump_drvsta(struct platform_device *pdev, const char *prefix)
  1833. +{
  1834. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1835. + struct pci_dev *rp = drvdata->dgpu_root_port;
  1836. +
  1837. + dev_dbg(&pdev->dev, "%s: RP power: %d\n", prefix, rp->current_state);
  1838. + dev_dbg(&pdev->dev, "%s: RP state saved: %d\n", prefix, rp->state_saved);
  1839. + dev_dbg(&pdev->dev, "%s: RP state stored: %d\n", prefix, !!drvdata->dgpu_root_port_state);
  1840. + dev_dbg(&pdev->dev, "%s: RP enabled: %d\n", prefix, atomic_read(&rp->enable_cnt));
  1841. + dev_dbg(&pdev->dev, "%s: RP mastered: %d\n", prefix, rp->is_busmaster);
  1842. +}
  1843. +
  1844. +static int shps_pm_prepare(struct device *dev)
  1845. +{
  1846. + struct platform_device *pdev = to_platform_device(dev);
  1847. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1848. + bool pwrtgt;
  1849. + int status = 0;
  1850. +
  1851. + dbg_dump_power_states(pdev, "shps_pm_prepare");
  1852. +
  1853. + if (param_dgpu_power_susp != SHPS_DGPU_MP_POWER_ASIS) {
  1854. + pwrtgt = test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1855. +
  1856. + status = shps_dgpu_set_power(pdev, param_dgpu_power_susp);
  1857. + if (status) {
  1858. + dev_err(&pdev->dev, "failed to power %s dGPU: %d\n",
  1859. + param_dgpu_power_susp == SHPS_DGPU_MP_POWER_OFF ? "off" : "on",
  1860. + status);
  1861. + return status;
  1862. + }
  1863. +
  1864. + if (pwrtgt)
  1865. + set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1866. + else
  1867. + clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1868. + }
  1869. +
  1870. + return 0;
  1871. +}
  1872. +
  1873. +static void shps_pm_complete(struct device *dev)
  1874. +{
  1875. + struct platform_device *pdev = to_platform_device(dev);
  1876. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1877. + int status;
  1878. +
  1879. + dbg_dump_power_states(pdev, "shps_pm_complete");
  1880. + dbg_dump_pciesta(pdev, "shps_pm_complete");
  1881. + dbg_dump_drvsta(pdev, "shps_pm_complete.1");
  1882. +
  1883. + // update power target, dGPU may have been detached while suspended
  1884. + status = shps_dgpu_is_present(pdev);
  1885. + if (status < 0) {
  1886. + dev_err(&pdev->dev, "failed to get dGPU presence: %d\n", status);
  1887. + return;
  1888. + } else if (status == 0) {
  1889. + clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  1890. + }
  1891. +
  1892. + /*
  1893. + * During resume, the PCIe core will power on the root-port, which in turn
  1894. + * will power on the dGPU. Most of the state synchronization is already
  1895. + * handled via the SAN RQSG handler, so it is in a fully consistent
  1896. + * on-state here. If requested, turn it off here.
  1897. + *
  1898. + * As there seem to be some synchronization issues turning off the dGPU
  1899. + * directly after the power-on SAN RQSG notification during the resume
  1900. + * process, let's do this here.
  1901. + *
  1902. + * TODO/FIXME:
  1903. + * This does not combat unhandled power-ons when the device is not fully
  1904. + * resumed, i.e. re-suspended before shps_pm_complete is called. Those
  1905. + * should normally not be an issue, but the dGPU does get hot even though
  1906. + * it is suspended, so ideally we want to keep it off.
  1907. + */
  1908. + if (!test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state)) {
  1909. + status = shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_OFF);
  1910. + if (status)
  1911. + dev_err(&pdev->dev, "failed to power-off dGPU: %d\n", status);
  1912. + }
  1913. +
  1914. + dbg_dump_drvsta(pdev, "shps_pm_complete.2");
  1915. +}
  1916. +
  1917. +static int shps_pm_suspend(struct device *dev)
  1918. +{
  1919. + struct platform_device *pdev = to_platform_device(dev);
  1920. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1921. + int status;
  1922. +
  1923. + if (device_may_wakeup(dev)) {
  1924. + status = enable_irq_wake(drvdata->irq_base_presence);
  1925. + if (status)
  1926. + return status;
  1927. +
  1928. + set_bit(SHPS_STATE_BIT_WAKE_ENABLED, &drvdata->state);
  1929. + }
  1930. +
  1931. + return 0;
  1932. +}
  1933. +
  1934. +static int shps_pm_resume(struct device *dev)
  1935. +{
  1936. + struct platform_device *pdev = to_platform_device(dev);
  1937. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1938. + int status = 0;
  1939. +
  1940. + if (test_and_clear_bit(SHPS_STATE_BIT_WAKE_ENABLED, &drvdata->state))
  1941. + status = disable_irq_wake(drvdata->irq_base_presence);
  1942. +
  1943. + return status;
  1944. +}
  1945. +
  1946. +static void shps_shutdown(struct platform_device *pdev)
  1947. +{
  1948. + int status;
  1949. +
  1950. + /*
  1951. + * Turn on dGPU before shutting down. This allows the core drivers to
  1952. + * properly shut down the device. If we don't do this, the pcieport driver
  1953. + * will complain that the device has already been disabled.
  1954. + */
  1955. + status = shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_ON);
  1956. + if (status)
  1957. + dev_err(&pdev->dev, "failed to turn on dGPU: %d\n", status);
  1958. +}
  1959. +
  1960. +static int shps_dgpu_detached(struct platform_device *pdev)
  1961. +{
  1962. + dbg_dump_power_states(pdev, "shps_dgpu_detached");
  1963. + return shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_OFF);
  1964. +}
  1965. +
  1966. +static int shps_dgpu_attached(struct platform_device *pdev)
  1967. +{
  1968. + dbg_dump_power_states(pdev, "shps_dgpu_attached");
  1969. + return 0;
  1970. +}
  1971. +
  1972. +static int shps_dgpu_powered_on(struct platform_device *pdev)
  1973. +{
  1974. + /*
  1975. + * This function gets called directly after a power-state transition of
  1976. + * the dGPU root port out of D3cold state, indicating a power-on of the
  1977. + * dGPU. Specifically, this function is called from the RQSG handler of
  1978. + * SAN, invoked by the ACPI _ON method of the dGPU root port. This means
  1979. + * that this function is run inside `pci_set_power_state(rp, ...)`
  1980. + * syncrhonously and thus returns before the `pci_set_power_state` call
  1981. + * does.
  1982. + *
  1983. + * `pci_set_power_state` may either be called by us or when the PCI
  1984. + * subsystem decides to power up the root port (e.g. during resume). Thus
  1985. + * we should use this function to ensure that the dGPU and root port
  1986. + * states are consistent when an unexpected power-up is encountered.
  1987. + */
  1988. +
  1989. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  1990. + struct pci_dev *rp = drvdata->dgpu_root_port;
  1991. + int status;
  1992. +
  1993. + dbg_dump_drvsta(pdev, "shps_dgpu_powered_on.1");
  1994. +
  1995. + // if we caused the root port to power-on, return
  1996. + if (test_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state))
  1997. + return 0;
  1998. +
  1999. + // if dGPU is not present, force power-target to off and return
  2000. + status = shps_dgpu_is_present(pdev);
  2001. + if (status == 0)
  2002. + clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  2003. + if (status <= 0)
  2004. + return status;
  2005. +
  2006. + mutex_lock(&drvdata->lock);
  2007. +
  2008. + dbg_dump_power_states(pdev, "shps_dgpu_powered_on.1");
  2009. + dbg_dump_pciesta(pdev, "shps_dgpu_powered_on.1");
  2010. + if (drvdata->dgpu_root_port_state)
  2011. + pci_load_and_free_saved_state(rp, &drvdata->dgpu_root_port_state);
  2012. + pci_restore_state(rp);
  2013. + if (!pci_is_enabled(rp))
  2014. + pci_enable_device(rp);
  2015. + pci_set_master(rp);
  2016. + dbg_dump_drvsta(pdev, "shps_dgpu_powered_on.2");
  2017. + dbg_dump_power_states(pdev, "shps_dgpu_powered_on.2");
  2018. + dbg_dump_pciesta(pdev, "shps_dgpu_powered_on.2");
  2019. +
  2020. + mutex_unlock(&drvdata->lock);
  2021. +
  2022. + if (!test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state)) {
  2023. + dev_warn(&pdev->dev, "unexpected dGPU power-on detected\n");
  2024. + // TODO: schedule state re-check and update
  2025. + }
  2026. +
  2027. + return 0;
  2028. +}
  2029. +
  2030. +static int shps_dgpu_handle_rqsg(struct surface_sam_san_rqsg *rqsg, void *data)
  2031. +{
  2032. + struct platform_device *pdev = data;
  2033. +
  2034. + if (rqsg->tc == SAM_DGPU_TC && rqsg->cid == SAM_DGPU_CID_POWERON)
  2035. + return shps_dgpu_powered_on(pdev);
  2036. +
  2037. + dev_warn(&pdev->dev, "unimplemented dGPU request: RQSG(0x%02x, 0x%02x, 0x%02x)\n",
  2038. + rqsg->tc, rqsg->cid, rqsg->iid);
  2039. + return 0;
  2040. +}
  2041. +
  2042. +static irqreturn_t shps_dgpu_presence_irq(int irq, void *data)
  2043. +{
  2044. + struct platform_device *pdev = data;
  2045. + bool dgpu_present;
  2046. + int status;
  2047. +
  2048. + status = shps_dgpu_is_present(pdev);
  2049. + if (status < 0) {
  2050. + dev_err(&pdev->dev, "failed to check physical dGPU presence: %d\n", status);
  2051. + return IRQ_HANDLED;
  2052. + }
  2053. +
  2054. + dgpu_present = status != 0;
  2055. + dev_info(&pdev->dev, "dGPU physically %s\n", dgpu_present ? "attached" : "detached");
  2056. +
  2057. + if (dgpu_present)
  2058. + status = shps_dgpu_attached(pdev);
  2059. + else
  2060. + status = shps_dgpu_detached(pdev);
  2061. +
  2062. + if (status)
  2063. + dev_err(&pdev->dev, "error handling dGPU interrupt: %d\n", status);
  2064. +
  2065. + return IRQ_HANDLED;
  2066. +}
  2067. +
  2068. +static irqreturn_t shps_base_presence_irq(int irq, void *data)
  2069. +{
  2070. + return IRQ_HANDLED; // nothing to do, just wake
  2071. +}
  2072. +
  2073. +
  2074. +static int shps_gpios_setup(struct platform_device *pdev)
  2075. +{
  2076. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2077. + struct gpio_desc *gpio_dgpu_power;
  2078. + struct gpio_desc *gpio_dgpu_presence;
  2079. + struct gpio_desc *gpio_base_presence;
  2080. + int status;
  2081. +
  2082. + // get GPIOs
  2083. + gpio_dgpu_power = devm_gpiod_get(&pdev->dev, "dgpu_power", GPIOD_IN);
  2084. + if (IS_ERR(gpio_dgpu_power)) {
  2085. + status = PTR_ERR(gpio_dgpu_power);
  2086. + goto err_out;
  2087. + }
  2088. +
  2089. + gpio_dgpu_presence = devm_gpiod_get(&pdev->dev, "dgpu_presence", GPIOD_IN);
  2090. + if (IS_ERR(gpio_dgpu_presence)) {
  2091. + status = PTR_ERR(gpio_dgpu_presence);
  2092. + goto err_out;
  2093. + }
  2094. +
  2095. + gpio_base_presence = devm_gpiod_get(&pdev->dev, "base_presence", GPIOD_IN);
  2096. + if (IS_ERR(gpio_base_presence)) {
  2097. + status = PTR_ERR(gpio_base_presence);
  2098. + goto err_out;
  2099. + }
  2100. +
  2101. + // export GPIOs
  2102. + status = gpiod_export(gpio_dgpu_power, false);
  2103. + if (status)
  2104. + goto err_out;
  2105. +
  2106. + status = gpiod_export(gpio_dgpu_presence, false);
  2107. + if (status)
  2108. + goto err_export_dgpu_presence;
  2109. +
  2110. + status = gpiod_export(gpio_base_presence, false);
  2111. + if (status)
  2112. + goto err_export_base_presence;
  2113. +
  2114. + // create sysfs links
  2115. + status = gpiod_export_link(&pdev->dev, "gpio-dgpu_power", gpio_dgpu_power);
  2116. + if (status)
  2117. + goto err_link_dgpu_power;
  2118. +
  2119. + status = gpiod_export_link(&pdev->dev, "gpio-dgpu_presence", gpio_dgpu_presence);
  2120. + if (status)
  2121. + goto err_link_dgpu_presence;
  2122. +
  2123. + status = gpiod_export_link(&pdev->dev, "gpio-base_presence", gpio_base_presence);
  2124. + if (status)
  2125. + goto err_link_base_presence;
  2126. +
  2127. + drvdata->gpio_dgpu_power = gpio_dgpu_power;
  2128. + drvdata->gpio_dgpu_presence = gpio_dgpu_presence;
  2129. + drvdata->gpio_base_presence = gpio_base_presence;
  2130. + return 0;
  2131. +
  2132. +err_link_base_presence:
  2133. + sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_presence");
  2134. +err_link_dgpu_presence:
  2135. + sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_power");
  2136. +err_link_dgpu_power:
  2137. + gpiod_unexport(gpio_base_presence);
  2138. +err_export_base_presence:
  2139. + gpiod_unexport(gpio_dgpu_presence);
  2140. +err_export_dgpu_presence:
  2141. + gpiod_unexport(gpio_dgpu_power);
  2142. +err_out:
  2143. + return status;
  2144. +}
  2145. +
  2146. +static void shps_gpios_remove(struct platform_device *pdev)
  2147. +{
  2148. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2149. +
  2150. + sysfs_remove_link(&pdev->dev.kobj, "gpio-base_presence");
  2151. + sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_presence");
  2152. + sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_power");
  2153. + gpiod_unexport(drvdata->gpio_base_presence);
  2154. + gpiod_unexport(drvdata->gpio_dgpu_presence);
  2155. + gpiod_unexport(drvdata->gpio_dgpu_power);
  2156. +}
  2157. +
  2158. +static int shps_gpios_setup_irq(struct platform_device *pdev)
  2159. +{
  2160. + const int irqf_dgpu = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  2161. + const int irqf_base = IRQF_SHARED;
  2162. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2163. + int status;
  2164. +
  2165. + status = gpiod_to_irq(drvdata->gpio_base_presence);
  2166. + if (status < 0)
  2167. + return status;
  2168. + drvdata->irq_base_presence = status;
  2169. +
  2170. + status = gpiod_to_irq(drvdata->gpio_dgpu_presence);
  2171. + if (status < 0)
  2172. + return status;
  2173. + drvdata->irq_dgpu_presence = status;
  2174. +
  2175. + status = request_irq(drvdata->irq_base_presence,
  2176. + shps_base_presence_irq, irqf_base,
  2177. + "shps_base_presence_irq", pdev);
  2178. + if (status) {
  2179. + dev_err(&pdev->dev, "base irq failed: %d\n", status);
  2180. + return status;
  2181. + }
  2182. +
  2183. + status = request_threaded_irq(drvdata->irq_dgpu_presence,
  2184. + NULL, shps_dgpu_presence_irq, irqf_dgpu,
  2185. + "shps_dgpu_presence_irq", pdev);
  2186. + if (status) {
  2187. + free_irq(drvdata->irq_base_presence, pdev);
  2188. + return status;
  2189. + }
  2190. +
  2191. + return 0;
  2192. +}
  2193. +
  2194. +static void shps_gpios_remove_irq(struct platform_device *pdev)
  2195. +{
  2196. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2197. +
  2198. + free_irq(drvdata->irq_base_presence, pdev);
  2199. + free_irq(drvdata->irq_dgpu_presence, pdev);
  2200. +}
  2201. +
  2202. +static void shps_sgcp_notify(acpi_handle device, u32 value, void *context) {
  2203. + struct platform_device *pdev = context;
  2204. + switch (value) {
  2205. + case ACPI_SGCP_NOTIFY_POWER_ON:
  2206. + shps_dgpu_powered_on(pdev);
  2207. + }
  2208. +}
  2209. +
  2210. +static int shps_start_sgcp_notification(struct platform_device *pdev, acpi_handle *sgpc_handle) {
  2211. + acpi_handle handle;
  2212. + int status;
  2213. +
  2214. + status = acpi_get_handle(NULL, "\\_SB.SGPC", &handle);
  2215. + if (status) {
  2216. + dev_err(&pdev->dev, "error in get_handle %d\n", status);
  2217. + return status;
  2218. + }
  2219. +
  2220. + status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY, shps_sgcp_notify, pdev);
  2221. + if (status) {
  2222. + dev_err(&pdev->dev, "error in install notify %d\n", status);
  2223. + *sgpc_handle = NULL;
  2224. + return status;
  2225. + }
  2226. +
  2227. + *sgpc_handle = handle;
  2228. + return 0;
  2229. +}
  2230. +
  2231. +static void shps_remove_sgcp_notification(struct platform_device *pdev) {
  2232. + int status;
  2233. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2234. +
  2235. + if (drvdata->sgpc_handle) {
  2236. + status = acpi_remove_notify_handler(drvdata->sgpc_handle, ACPI_DEVICE_NOTIFY, shps_sgcp_notify);
  2237. + if (status) {
  2238. + dev_err(&pdev->dev, "failed to remove notify handler: %d\n", status);
  2239. + }
  2240. + }
  2241. +}
  2242. +
  2243. +static struct shps_hardware_traits shps_detect_hardware_traits(struct platform_device *pdev) {
  2244. + const struct shps_hardware_probe *p;
  2245. +
  2246. + for (p = shps_hardware_probe_match; p->hardware_id; ++p) {
  2247. + if (acpi_dev_present(p->hardware_id, NULL, -1)) {
  2248. + break;
  2249. + }
  2250. + }
  2251. +
  2252. + dev_info(&pdev->dev,
  2253. + "shps_detect_hardware_traits found device %s, generation %d\n",
  2254. + p->hardware_id ? p->hardware_id : "SAN (default)",
  2255. + p->generation);
  2256. +
  2257. + return *p->hardware_traits;
  2258. +}
  2259. +
  2260. +static int shps_probe(struct platform_device *pdev)
  2261. +{
  2262. + struct acpi_device *shps_dev = ACPI_COMPANION(&pdev->dev);
  2263. + struct shps_driver_data *drvdata;
  2264. + struct ssam_controller *ctrl;
  2265. + struct device_link *link;
  2266. + int power, status;
  2267. + struct shps_hardware_traits detected_traits;
  2268. +
  2269. + if (gpiod_count(&pdev->dev, NULL) < 0) {
  2270. + dev_err(&pdev->dev, "gpiod_count returned < 0\n");
  2271. + return -ENODEV;
  2272. + }
  2273. +
  2274. + // link to SSH
  2275. + status = ssam_client_bind(&pdev->dev, &ctrl);
  2276. + if (status) {
  2277. + return status == -ENXIO ? -EPROBE_DEFER : status;
  2278. + }
  2279. +
  2280. + // detect what kind of hardware we're running
  2281. + detected_traits = shps_detect_hardware_traits(pdev);
  2282. +
  2283. + if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
  2284. + // link to SAN
  2285. + status = surface_sam_san_consumer_register(&pdev->dev, 0);
  2286. + if (status) {
  2287. + dev_err(&pdev->dev, "failed to register with san consumer: %d\n", status);
  2288. + return status == -ENXIO ? -EPROBE_DEFER : status;
  2289. + }
  2290. + }
  2291. +
  2292. + status = acpi_dev_add_driver_gpios(shps_dev, shps_acpi_gpios);
  2293. + if (status) {
  2294. + dev_err(&pdev->dev, "failed to add gpios: %d\n", status);
  2295. + return status;
  2296. + }
  2297. +
  2298. + drvdata = kzalloc(sizeof(struct shps_driver_data), GFP_KERNEL);
  2299. + if (!drvdata) {
  2300. + status = -ENOMEM;
  2301. + goto err_drvdata;
  2302. + }
  2303. + mutex_init(&drvdata->lock);
  2304. + platform_set_drvdata(pdev, drvdata);
  2305. +
  2306. + drvdata->ctrl = ctrl;
  2307. + drvdata->hardware_traits = detected_traits;
  2308. +
  2309. + drvdata->dgpu_root_port = shps_dgpu_dsm_get_pci_dev(pdev);
  2310. + if (IS_ERR(drvdata->dgpu_root_port)) {
  2311. + status = PTR_ERR(drvdata->dgpu_root_port);
  2312. + dev_err(&pdev->dev, "failed to get pci dev: %d\n", status);
  2313. + goto err_rp_lookup;
  2314. + }
  2315. +
  2316. + status = shps_gpios_setup(pdev);
  2317. + if (status) {
  2318. + dev_err(&pdev->dev, "unable to set up gpios, %d\n", status);
  2319. + goto err_gpio;
  2320. + }
  2321. +
  2322. + status = shps_gpios_setup_irq(pdev);
  2323. + if (status) {
  2324. + dev_err(&pdev->dev, "unable to set up irqs %d\n", status);
  2325. + goto err_gpio_irqs;
  2326. + }
  2327. +
  2328. + status = device_add_groups(&pdev->dev, shps_power_groups);
  2329. + if (status)
  2330. + goto err_devattr;
  2331. +
  2332. + link = device_link_add(&pdev->dev, &drvdata->dgpu_root_port->dev,
  2333. + DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER);
  2334. + if (!link)
  2335. + goto err_devlink;
  2336. +
  2337. + if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
  2338. + status = surface_sam_san_set_rqsg_handler(shps_dgpu_handle_rqsg, pdev);
  2339. + if (status) {
  2340. + dev_err(&pdev->dev, "unable to set SAN notification handler (%d)\n", status);
  2341. + goto err_devlink;
  2342. + }
  2343. + } else if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
  2344. + status = shps_start_sgcp_notification(pdev, &drvdata->sgpc_handle);
  2345. + if (status) {
  2346. + dev_err(&pdev->dev, "unable to install SGCP notification handler (%d)\n", status);
  2347. + goto err_devlink;
  2348. + }
  2349. + }
  2350. +
  2351. + // if dGPU is not present turn-off root-port, else obey module param
  2352. + status = shps_dgpu_is_present(pdev);
  2353. + if (status < 0)
  2354. + goto err_post_notification;
  2355. +
  2356. + power = status == 0 ? SHPS_DGPU_POWER_OFF : param_dgpu_power_init;
  2357. + if (power != SHPS_DGPU_MP_POWER_ASIS) {
  2358. + status = shps_dgpu_set_power(pdev, power);
  2359. + if (status)
  2360. + goto err_post_notification;
  2361. + }
  2362. +
  2363. + // initialize power target
  2364. + status = shps_dgpu_rp_get_power(pdev);
  2365. + if (status < 0)
  2366. + goto err_pwrtgt;
  2367. +
  2368. + if (status)
  2369. + set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  2370. + else
  2371. + clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
  2372. +
  2373. + device_init_wakeup(&pdev->dev, true);
  2374. + return 0;
  2375. +
  2376. +err_pwrtgt:
  2377. + if (param_dgpu_power_exit != SHPS_DGPU_MP_POWER_ASIS) {
  2378. + status = shps_dgpu_set_power(pdev, param_dgpu_power_exit);
  2379. + if (status)
  2380. + dev_err(&pdev->dev, "failed to set dGPU power state: %d\n", status);
  2381. + }
  2382. +err_post_notification:
  2383. + if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
  2384. + shps_remove_sgcp_notification(pdev);
  2385. + } else if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
  2386. + surface_sam_san_set_rqsg_handler(NULL, NULL);
  2387. + }
  2388. +err_devlink:
  2389. + device_remove_groups(&pdev->dev, shps_power_groups);
  2390. +err_devattr:
  2391. + shps_gpios_remove_irq(pdev);
  2392. +err_gpio_irqs:
  2393. + shps_gpios_remove(pdev);
  2394. +err_gpio:
  2395. + pci_dev_put(drvdata->dgpu_root_port);
  2396. +err_rp_lookup:
  2397. + platform_set_drvdata(pdev, NULL);
  2398. + kfree(drvdata);
  2399. +err_drvdata:
  2400. + acpi_dev_remove_driver_gpios(shps_dev);
  2401. + return status;
  2402. +}
  2403. +
  2404. +static int shps_remove(struct platform_device *pdev)
  2405. +{
  2406. + struct acpi_device *shps_dev = ACPI_COMPANION(&pdev->dev);
  2407. + struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
  2408. + int status;
  2409. +
  2410. + if (param_dgpu_power_exit != SHPS_DGPU_MP_POWER_ASIS) {
  2411. + status = shps_dgpu_set_power(pdev, param_dgpu_power_exit);
  2412. + if (status)
  2413. + dev_err(&pdev->dev, "failed to set dGPU power state: %d\n", status);
  2414. + }
  2415. +
  2416. + device_set_wakeup_capable(&pdev->dev, false);
  2417. +
  2418. + if (drvdata->hardware_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
  2419. + shps_remove_sgcp_notification(pdev);
  2420. + } else if (drvdata->hardware_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
  2421. + surface_sam_san_set_rqsg_handler(NULL, NULL);
  2422. + }
  2423. + device_remove_groups(&pdev->dev, shps_power_groups);
  2424. + shps_gpios_remove_irq(pdev);
  2425. + shps_gpios_remove(pdev);
  2426. + pci_dev_put(drvdata->dgpu_root_port);
  2427. + platform_set_drvdata(pdev, NULL);
  2428. + kfree(drvdata);
  2429. +
  2430. + acpi_dev_remove_driver_gpios(shps_dev);
  2431. + return 0;
  2432. +}
  2433. +
  2434. +
  2435. +static const struct dev_pm_ops shps_pm_ops = {
  2436. + .prepare = shps_pm_prepare,
  2437. + .complete = shps_pm_complete,
  2438. + .suspend = shps_pm_suspend,
  2439. + .resume = shps_pm_resume,
  2440. +};
  2441. +
  2442. +static const struct acpi_device_id shps_acpi_match[] = {
  2443. + { "MSHW0153", 0 },
  2444. + { },
  2445. +};
  2446. +MODULE_DEVICE_TABLE(acpi, shps_acpi_match);
  2447. +
  2448. +static struct platform_driver surface_sam_hps = {
  2449. + .probe = shps_probe,
  2450. + .remove = shps_remove,
  2451. + .shutdown = shps_shutdown,
  2452. + .driver = {
  2453. + .name = "surface_dgpu_hps",
  2454. + .acpi_match_table = shps_acpi_match,
  2455. + .pm = &shps_pm_ops,
  2456. + },
  2457. +};
  2458. +
  2459. +module_platform_driver(surface_sam_hps);
  2460. +
  2461. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  2462. +MODULE_DESCRIPTION("Surface Hot-Plug System (HPS) and dGPU power-state Driver for Surface Book 2");
  2463. +MODULE_LICENSE("GPL");
  2464. diff --git a/drivers/platform/x86/surface_sam/surface_sam_san.c b/drivers/platform/x86/surface_sam/surface_sam_san.c
  2465. new file mode 100644
  2466. index 0000000000000..eab4e178a8450
  2467. --- /dev/null
  2468. +++ b/drivers/platform/x86/surface_sam/surface_sam_san.c
  2469. @@ -0,0 +1,930 @@
  2470. +// SPDX-License-Identifier: GPL-2.0-or-later
  2471. +/*
  2472. + * Surface ACPI Notify (SAN) and ACPI integration driver for SAM.
  2473. + * Translates communication from ACPI to SSH and back.
  2474. + */
  2475. +
  2476. +#include <asm/unaligned.h>
  2477. +#include <linux/acpi.h>
  2478. +#include <linux/delay.h>
  2479. +#include <linux/jiffies.h>
  2480. +#include <linux/kernel.h>
  2481. +#include <linux/platform_device.h>
  2482. +
  2483. +#include "surface_sam_ssh.h"
  2484. +#include "surface_sam_san.h"
  2485. +
  2486. +
  2487. +#define SAN_RQST_RETRY 5
  2488. +
  2489. +#define SAN_DSM_REVISION 0
  2490. +#define SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT 0x09
  2491. +
  2492. +static const guid_t SAN_DSM_UUID =
  2493. + GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
  2494. + 0x48, 0x7c, 0x91, 0xab, 0x3c);
  2495. +
  2496. +#define SAM_EVENT_DELAY_PWR_ADAPTER msecs_to_jiffies(5000)
  2497. +#define SAM_EVENT_DELAY_PWR_BST msecs_to_jiffies(2500)
  2498. +
  2499. +#define SAM_EVENT_PWR_CID_BIX 0x15
  2500. +#define SAM_EVENT_PWR_CID_BST 0x16
  2501. +#define SAM_EVENT_PWR_CID_ADAPTER 0x17
  2502. +#define SAM_EVENT_PWR_CID_DPTF 0x4f
  2503. +
  2504. +#define SAM_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT 0x0b
  2505. +
  2506. +
  2507. +struct san_acpi_consumer {
  2508. + char *path;
  2509. + bool required;
  2510. + u32 flags;
  2511. +};
  2512. +
  2513. +struct san_handler_data {
  2514. + struct acpi_connection_info info; // must be first
  2515. +};
  2516. +
  2517. +struct san_consumer_link {
  2518. + const struct san_acpi_consumer *properties;
  2519. + struct device_link *link;
  2520. +};
  2521. +
  2522. +struct san_consumers {
  2523. + u32 num;
  2524. + struct san_consumer_link *links;
  2525. +};
  2526. +
  2527. +struct san_data {
  2528. + struct device *dev;
  2529. + struct ssam_controller *ctrl;
  2530. +
  2531. + struct san_handler_data context;
  2532. + struct san_consumers consumers;
  2533. +
  2534. + struct ssam_event_notifier nf_bat;
  2535. + struct ssam_event_notifier nf_tmp;
  2536. +};
  2537. +
  2538. +#define to_san_data(ptr, member) \
  2539. + container_of(ptr, struct san_data, member)
  2540. +
  2541. +struct san_event_work {
  2542. + struct delayed_work work;
  2543. + struct device *dev;
  2544. + struct ssam_event event; // must be last
  2545. +};
  2546. +
  2547. +struct gsb_data_in {
  2548. + u8 cv;
  2549. +} __packed;
  2550. +
  2551. +struct gsb_data_rqsx {
  2552. + u8 cv; // command value (should be 0x01 or 0x03)
  2553. + u8 tc; // target controller
  2554. + u8 tid; // transport channnel ID
  2555. + u8 iid; // target sub-controller (e.g. primary vs. secondary battery)
  2556. + u8 snc; // expect-response-flag
  2557. + u8 cid; // command ID
  2558. + u16 cdl; // payload length
  2559. + u8 pld[0]; // payload
  2560. +} __packed;
  2561. +
  2562. +struct gsb_data_etwl {
  2563. + u8 cv; // command value (should be 0x02)
  2564. + u8 etw3; // ?
  2565. + u8 etw4; // ?
  2566. + u8 msg[0]; // error message (ASCIIZ)
  2567. +} __packed;
  2568. +
  2569. +struct gsb_data_out {
  2570. + u8 status; // _SSH communication status
  2571. + u8 len; // _SSH payload length
  2572. + u8 pld[0]; // _SSH payload
  2573. +} __packed;
  2574. +
  2575. +union gsb_buffer_data {
  2576. + struct gsb_data_in in; // common input
  2577. + struct gsb_data_rqsx rqsx; // RQSX input
  2578. + struct gsb_data_etwl etwl; // ETWL input
  2579. + struct gsb_data_out out; // output
  2580. +};
  2581. +
  2582. +struct gsb_buffer {
  2583. + u8 status; // GSB AttribRawProcess status
  2584. + u8 len; // GSB AttribRawProcess length
  2585. + union gsb_buffer_data data;
  2586. +} __packed;
  2587. +
  2588. +#define SAN_GSB_MAX_RQSX_PAYLOAD (U8_MAX - 2 - sizeof(struct gsb_data_rqsx))
  2589. +#define SAN_GSB_MAX_RESPONSE (U8_MAX - 2 - sizeof(struct gsb_data_out))
  2590. +
  2591. +#define san_request_sync_onstack(ctrl, rqst, rsp) \
  2592. + ssam_request_sync_onstack(ctrl, rqst, rsp, SAN_GSB_MAX_RQSX_PAYLOAD)
  2593. +
  2594. +
  2595. +enum san_pwr_event {
  2596. + SAN_PWR_EVENT_BAT1_STAT = 0x03,
  2597. + SAN_PWR_EVENT_BAT1_INFO = 0x04,
  2598. + SAN_PWR_EVENT_ADP1_STAT = 0x05,
  2599. + SAN_PWR_EVENT_ADP1_INFO = 0x06,
  2600. + SAN_PWR_EVENT_BAT2_STAT = 0x07,
  2601. + SAN_PWR_EVENT_BAT2_INFO = 0x08,
  2602. + SAN_PWR_EVENT_DPTF = 0x0A,
  2603. +};
  2604. +
  2605. +
  2606. +static int sam_san_default_rqsg_handler(struct surface_sam_san_rqsg *rqsg, void *data);
  2607. +
  2608. +struct sam_san_rqsg_if {
  2609. + struct mutex lock;
  2610. + struct device *san_dev;
  2611. + surface_sam_san_rqsg_handler_fn handler;
  2612. + void *handler_data;
  2613. +};
  2614. +
  2615. +static struct sam_san_rqsg_if rqsg_if = {
  2616. + .lock = __MUTEX_INITIALIZER(rqsg_if.lock),
  2617. + .san_dev = NULL,
  2618. + .handler = sam_san_default_rqsg_handler,
  2619. + .handler_data = NULL,
  2620. +};
  2621. +
  2622. +int surface_sam_san_consumer_register(struct device *consumer, u32 flags)
  2623. +{
  2624. + const u32 valid = DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE;
  2625. + int status;
  2626. +
  2627. + if ((flags | valid) != valid)
  2628. + return -EINVAL;
  2629. +
  2630. + flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
  2631. +
  2632. + mutex_lock(&rqsg_if.lock);
  2633. + if (rqsg_if.san_dev)
  2634. + status = device_link_add(consumer, rqsg_if.san_dev, flags) ? 0 : -EINVAL;
  2635. + else
  2636. + status = -ENXIO;
  2637. + mutex_unlock(&rqsg_if.lock);
  2638. + return status;
  2639. +}
  2640. +EXPORT_SYMBOL_GPL(surface_sam_san_consumer_register);
  2641. +
  2642. +int surface_sam_san_set_rqsg_handler(surface_sam_san_rqsg_handler_fn fn, void *data)
  2643. +{
  2644. + int status = -EBUSY;
  2645. +
  2646. + mutex_lock(&rqsg_if.lock);
  2647. +
  2648. + if (rqsg_if.handler == sam_san_default_rqsg_handler || !fn) {
  2649. + rqsg_if.handler = fn ? fn : sam_san_default_rqsg_handler;
  2650. + rqsg_if.handler_data = fn ? data : NULL;
  2651. + status = 0;
  2652. + }
  2653. +
  2654. + mutex_unlock(&rqsg_if.lock);
  2655. + return status;
  2656. +}
  2657. +EXPORT_SYMBOL_GPL(surface_sam_san_set_rqsg_handler);
  2658. +
  2659. +int san_call_rqsg_handler(struct surface_sam_san_rqsg *rqsg)
  2660. +{
  2661. + int status;
  2662. +
  2663. + mutex_lock(&rqsg_if.lock);
  2664. + status = rqsg_if.handler(rqsg, rqsg_if.handler_data);
  2665. + mutex_unlock(&rqsg_if.lock);
  2666. +
  2667. + return status;
  2668. +}
  2669. +
  2670. +static int sam_san_default_rqsg_handler(struct surface_sam_san_rqsg *rqsg, void *data)
  2671. +{
  2672. + struct device *dev = rqsg_if.san_dev;
  2673. +
  2674. + dev_warn(dev, "unhandled request: RQSG(0x%02x, 0x%02x, 0x%02x)\n",
  2675. + rqsg->tc, rqsg->cid, rqsg->iid);
  2676. +
  2677. + return 0;
  2678. +}
  2679. +
  2680. +
  2681. +static bool san_acpi_can_notify(struct device *dev, u64 func)
  2682. +{
  2683. + acpi_handle san = ACPI_HANDLE(dev);
  2684. + return acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, 1 << func);
  2685. +}
  2686. +
  2687. +static int san_acpi_notify_power_event(struct device *dev, enum san_pwr_event event)
  2688. +{
  2689. + acpi_handle san = ACPI_HANDLE(dev);
  2690. + union acpi_object *obj;
  2691. +
  2692. + if (!san_acpi_can_notify(dev, event))
  2693. + return 0;
  2694. +
  2695. + dev_dbg(dev, "notify power event 0x%02x\n", event);
  2696. + obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
  2697. + event, NULL, ACPI_TYPE_BUFFER);
  2698. +
  2699. + if (IS_ERR_OR_NULL(obj))
  2700. + return obj ? PTR_ERR(obj) : -ENXIO;
  2701. +
  2702. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  2703. + dev_err(dev, "got unexpected result from _DSM\n");
  2704. + return -EFAULT;
  2705. + }
  2706. +
  2707. + ACPI_FREE(obj);
  2708. + return 0;
  2709. +}
  2710. +
  2711. +static int san_acpi_notify_sensor_trip_point(struct device *dev, u8 iid)
  2712. +{
  2713. + acpi_handle san = ACPI_HANDLE(dev);
  2714. + union acpi_object *obj;
  2715. + union acpi_object param;
  2716. +
  2717. + if (!san_acpi_can_notify(dev, SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT))
  2718. + return 0;
  2719. +
  2720. + param.type = ACPI_TYPE_INTEGER;
  2721. + param.integer.value = iid;
  2722. +
  2723. + obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
  2724. + SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT,
  2725. + &param, ACPI_TYPE_BUFFER);
  2726. +
  2727. + if (IS_ERR_OR_NULL(obj))
  2728. + return obj ? PTR_ERR(obj) : -ENXIO;
  2729. +
  2730. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  2731. + dev_err(dev, "got unexpected result from _DSM\n");
  2732. + return -EFAULT;
  2733. + }
  2734. +
  2735. + ACPI_FREE(obj);
  2736. + return 0;
  2737. +}
  2738. +
  2739. +
  2740. +static inline int san_evt_power_adapter(struct device *dev, const struct ssam_event *event)
  2741. +{
  2742. + int status;
  2743. +
  2744. + status = san_acpi_notify_power_event(dev, SAN_PWR_EVENT_ADP1_STAT);
  2745. + if (status)
  2746. + return status;
  2747. +
  2748. + /*
  2749. + * Enusre that the battery states get updated correctly.
  2750. + * When the battery is fully charged and an adapter is plugged in, it
  2751. + * sometimes is not updated correctly, instead showing it as charging.
  2752. + * Explicitly trigger battery updates to fix this.
  2753. + */
  2754. +
  2755. + status = san_acpi_notify_power_event(dev, SAN_PWR_EVENT_BAT1_STAT);
  2756. + if (status)
  2757. + return status;
  2758. +
  2759. + return san_acpi_notify_power_event(dev, SAN_PWR_EVENT_BAT2_STAT);
  2760. +}
  2761. +
  2762. +static inline int san_evt_power_bix(struct device *dev, const struct ssam_event *event)
  2763. +{
  2764. + enum san_pwr_event evcode;
  2765. +
  2766. + if (event->instance_id == 0x02)
  2767. + evcode = SAN_PWR_EVENT_BAT2_INFO;
  2768. + else
  2769. + evcode = SAN_PWR_EVENT_BAT1_INFO;
  2770. +
  2771. + return san_acpi_notify_power_event(dev, evcode);
  2772. +}
  2773. +
  2774. +static inline int san_evt_power_bst(struct device *dev, const struct ssam_event *event)
  2775. +{
  2776. + enum san_pwr_event evcode;
  2777. +
  2778. + if (event->instance_id == 0x02)
  2779. + evcode = SAN_PWR_EVENT_BAT2_STAT;
  2780. + else
  2781. + evcode = SAN_PWR_EVENT_BAT1_STAT;
  2782. +
  2783. + return san_acpi_notify_power_event(dev, evcode);
  2784. +}
  2785. +
  2786. +static inline int san_evt_power_dptf(struct device *dev, const struct ssam_event *event)
  2787. +{
  2788. + union acpi_object payload;
  2789. + acpi_handle san = ACPI_HANDLE(dev);
  2790. + union acpi_object *obj;
  2791. +
  2792. + if (!san_acpi_can_notify(dev, SAN_PWR_EVENT_DPTF))
  2793. + return 0;
  2794. +
  2795. + /*
  2796. + * The Surface ACPI expects a buffer and not a package. It specifically
  2797. + * checks for ObjectType (Arg3) == 0x03. This will cause a warning in
  2798. + * acpica/nsarguments.c, but this can safely be ignored.
  2799. + */
  2800. + payload.type = ACPI_TYPE_BUFFER;
  2801. + payload.buffer.length = event->length;
  2802. + payload.buffer.pointer = (u8 *)&event->data[0];
  2803. +
  2804. + dev_dbg(dev, "notify power event 0x%02x\n", event->command_id);
  2805. + obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
  2806. + SAN_PWR_EVENT_DPTF, &payload,
  2807. + ACPI_TYPE_BUFFER);
  2808. +
  2809. + if (IS_ERR_OR_NULL(obj))
  2810. + return obj ? PTR_ERR(obj) : -ENXIO;
  2811. +
  2812. + if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  2813. + dev_err(dev, "got unexpected result from _DSM\n");
  2814. + return -EFAULT;
  2815. + }
  2816. +
  2817. + ACPI_FREE(obj);
  2818. + return 0;
  2819. +}
  2820. +
  2821. +static unsigned long san_evt_power_delay(u8 cid)
  2822. +{
  2823. + switch (cid) {
  2824. + case SAM_EVENT_PWR_CID_ADAPTER:
  2825. + /*
  2826. + * Wait for battery state to update before signalling adapter change.
  2827. + */
  2828. + return SAM_EVENT_DELAY_PWR_ADAPTER;
  2829. +
  2830. + case SAM_EVENT_PWR_CID_BST:
  2831. + /*
  2832. + * Ensure we do not miss anything important due to caching.
  2833. + */
  2834. + return SAM_EVENT_DELAY_PWR_BST;
  2835. +
  2836. + case SAM_EVENT_PWR_CID_BIX:
  2837. + case SAM_EVENT_PWR_CID_DPTF:
  2838. + default:
  2839. + return 0;
  2840. + }
  2841. +}
  2842. +
  2843. +static bool san_evt_power(const struct ssam_event *event, struct device *dev)
  2844. +{
  2845. + int status;
  2846. +
  2847. + switch (event->command_id) {
  2848. + case SAM_EVENT_PWR_CID_BIX:
  2849. + status = san_evt_power_bix(dev, event);
  2850. + break;
  2851. +
  2852. + case SAM_EVENT_PWR_CID_BST:
  2853. + status = san_evt_power_bst(dev, event);
  2854. + break;
  2855. +
  2856. + case SAM_EVENT_PWR_CID_ADAPTER:
  2857. + status = san_evt_power_adapter(dev, event);
  2858. + break;
  2859. +
  2860. + case SAM_EVENT_PWR_CID_DPTF:
  2861. + status = san_evt_power_dptf(dev, event);
  2862. + break;
  2863. +
  2864. + default:
  2865. + return false;
  2866. + }
  2867. +
  2868. + if (status)
  2869. + dev_err(dev, "error handling power event (cid = %x)\n",
  2870. + event->command_id);
  2871. +
  2872. + return true;
  2873. +}
  2874. +
  2875. +static void san_evt_power_workfn(struct work_struct *work)
  2876. +{
  2877. + struct san_event_work *ev = container_of(work, struct san_event_work, work.work);
  2878. +
  2879. + san_evt_power(&ev->event, ev->dev);
  2880. + kfree(ev);
  2881. +}
  2882. +
  2883. +
  2884. +static u32 san_evt_power_nb(struct ssam_notifier_block *nb, const struct ssam_event *event)
  2885. +{
  2886. + struct san_data *d = to_san_data(nb, nf_bat.base);
  2887. + struct san_event_work *work;
  2888. + unsigned long delay = san_evt_power_delay(event->command_id);
  2889. +
  2890. + if (delay == 0) {
  2891. + if (san_evt_power(event, d->dev))
  2892. + return SSAM_NOTIF_HANDLED;
  2893. + else
  2894. + return 0;
  2895. + }
  2896. +
  2897. + work = kzalloc(sizeof(struct san_event_work) + event->length, GFP_KERNEL);
  2898. + if (!work)
  2899. + return ssam_notifier_from_errno(-ENOMEM);
  2900. +
  2901. + INIT_DELAYED_WORK(&work->work, san_evt_power_workfn);
  2902. + work->dev = d->dev;
  2903. +
  2904. + memcpy(&work->event, event, sizeof(struct ssam_event) + event->length);
  2905. +
  2906. + schedule_delayed_work(&work->work, delay);
  2907. + return SSAM_NOTIF_HANDLED;
  2908. +}
  2909. +
  2910. +
  2911. +static inline int san_evt_thermal_notify(struct device *dev, const struct ssam_event *event)
  2912. +{
  2913. + return san_acpi_notify_sensor_trip_point(dev, event->instance_id);
  2914. +}
  2915. +
  2916. +static bool san_evt_thermal(const struct ssam_event *event, struct device *dev)
  2917. +{
  2918. + int status;
  2919. +
  2920. + switch (event->command_id) {
  2921. + case SAM_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT:
  2922. + status = san_evt_thermal_notify(dev, event);
  2923. + break;
  2924. +
  2925. + default:
  2926. + return false;
  2927. + }
  2928. +
  2929. + if (status) {
  2930. + dev_err(dev, "error handling thermal event (cid = %x)\n",
  2931. + event->command_id);
  2932. + }
  2933. +
  2934. + return true;
  2935. +}
  2936. +
  2937. +static u32 san_evt_thermal_nb(struct ssam_notifier_block *nb, const struct ssam_event *event)
  2938. +{
  2939. + if (san_evt_thermal(event, to_san_data(nb, nf_tmp.base)->dev))
  2940. + return SSAM_NOTIF_HANDLED;
  2941. + else
  2942. + return 0;
  2943. +}
  2944. +
  2945. +
  2946. +static struct gsb_data_rqsx
  2947. +*san_validate_rqsx(struct device *dev, const char *type, struct gsb_buffer *buffer)
  2948. +{
  2949. + struct gsb_data_rqsx *rqsx = &buffer->data.rqsx;
  2950. +
  2951. + if (buffer->len < 8) {
  2952. + dev_err(dev, "invalid %s package (len = %d)\n",
  2953. + type, buffer->len);
  2954. + return NULL;
  2955. + }
  2956. +
  2957. + if (get_unaligned(&rqsx->cdl) != buffer->len - sizeof(struct gsb_data_rqsx)) {
  2958. + dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
  2959. + type, buffer->len, get_unaligned(&rqsx->cdl));
  2960. + return NULL;
  2961. + }
  2962. +
  2963. + if (get_unaligned(&rqsx->cdl) > SAN_GSB_MAX_RQSX_PAYLOAD) {
  2964. + dev_err(dev, "payload for %s package too large (cdl = %d)\n",
  2965. + type, get_unaligned(&rqsx->cdl));
  2966. + return NULL;
  2967. + }
  2968. +
  2969. + if (rqsx->tid != 0x01) {
  2970. + dev_warn(dev, "unsupported %s package (tid = 0x%02x)\n",
  2971. + type, rqsx->tid);
  2972. + return NULL;
  2973. + }
  2974. +
  2975. + return rqsx;
  2976. +}
  2977. +
  2978. +static acpi_status san_etwl(struct san_data *d, struct gsb_buffer *buffer)
  2979. +{
  2980. + struct gsb_data_etwl *etwl = &buffer->data.etwl;
  2981. +
  2982. + if (buffer->len < 3) {
  2983. + dev_err(d->dev, "invalid ETWL package (len = %d)\n", buffer->len);
  2984. + return AE_OK;
  2985. + }
  2986. +
  2987. + dev_err(d->dev, "ETWL(0x%02x, 0x%02x): %.*s\n",
  2988. + etwl->etw3, etwl->etw4,
  2989. + buffer->len - 3, (char *)etwl->msg);
  2990. +
  2991. + // indicate success
  2992. + buffer->status = 0x00;
  2993. + buffer->len = 0x00;
  2994. +
  2995. + return AE_OK;
  2996. +}
  2997. +
  2998. +static void gsb_response_error(struct gsb_buffer *gsb, int status)
  2999. +{
  3000. + gsb->status = 0x00;
  3001. + gsb->len = 0x02;
  3002. + gsb->data.out.status = (u8)(-status);
  3003. + gsb->data.out.len = 0x00;
  3004. +}
  3005. +
  3006. +static void gsb_response_success(struct gsb_buffer *gsb, u8 *ptr, size_t len)
  3007. +{
  3008. + gsb->status = 0x00;
  3009. + gsb->len = len + 2;
  3010. + gsb->data.out.status = 0x00;
  3011. + gsb->data.out.len = len;
  3012. +
  3013. + if (len)
  3014. + memcpy(&gsb->data.out.pld[0], ptr, len);
  3015. +}
  3016. +
  3017. +static acpi_status san_rqst_fixup_suspended(struct ssam_request *rqst,
  3018. + struct gsb_buffer *gsb)
  3019. +{
  3020. + if (rqst->target_category == 0x11 && rqst->command_id == 0x0D) {
  3021. + /* Base state quirk:
  3022. + * The base state may be queried from ACPI when the EC is still
  3023. + * suspended. In this case it will return '-EPERM'. This query
  3024. + * will only be triggered from the ACPI lid GPE interrupt, thus
  3025. + * we are either in laptop or studio mode (base status 0x01 or
  3026. + * 0x02). Furthermore, we will only get here if the device (and
  3027. + * EC) have been suspended.
  3028. + *
  3029. + * We now assume that the device is in laptop mode (0x01). This
  3030. + * has the drawback that it will wake the device when unfolding
  3031. + * it in studio mode, but it also allows us to avoid actively
  3032. + * waiting for the EC to wake up, which may incur a notable
  3033. + * delay.
  3034. + */
  3035. +
  3036. + u8 base_state = 1;
  3037. + gsb_response_success(gsb, &base_state, 1);
  3038. + return AE_OK;
  3039. + }
  3040. +
  3041. + gsb_response_error(gsb, -ENXIO);
  3042. + return AE_OK;
  3043. +}
  3044. +
  3045. +static acpi_status san_rqst(struct san_data *d, struct gsb_buffer *buffer)
  3046. +{
  3047. + u8 rspbuf[SAN_GSB_MAX_RESPONSE];
  3048. + struct gsb_data_rqsx *gsb_rqst;
  3049. + struct ssam_request rqst;
  3050. + struct ssam_response rsp;
  3051. + int status = 0;
  3052. + int try;
  3053. +
  3054. + gsb_rqst = san_validate_rqsx(d->dev, "RQST", buffer);
  3055. + if (!gsb_rqst)
  3056. + return AE_OK;
  3057. +
  3058. + rqst.target_category = gsb_rqst->tc;
  3059. + rqst.command_id = gsb_rqst->cid;
  3060. + rqst.instance_id = gsb_rqst->iid;
  3061. + rqst.channel = gsb_rqst->tid;
  3062. + rqst.flags = gsb_rqst->snc ? SSAM_REQUEST_HAS_RESPONSE : 0;
  3063. + rqst.length = get_unaligned(&gsb_rqst->cdl);
  3064. + rqst.payload = &gsb_rqst->pld[0];
  3065. +
  3066. + rsp.capacity = ARRAY_SIZE(rspbuf);
  3067. + rsp.length = 0;
  3068. + rsp.pointer = &rspbuf[0];
  3069. +
  3070. + // handle suspended device
  3071. + if (d->dev->power.is_suspended) {
  3072. + dev_warn(d->dev, "rqst: device is suspended, not executing\n");
  3073. + return san_rqst_fixup_suspended(&rqst, buffer);
  3074. + }
  3075. +
  3076. + for (try = 0; try < SAN_RQST_RETRY; try++) {
  3077. + if (try)
  3078. + dev_warn(d->dev, "rqst: IO error, trying again\n");
  3079. +
  3080. + status = san_request_sync_onstack(d->ctrl, &rqst, &rsp);
  3081. + if (status != -ETIMEDOUT && status != -EREMOTEIO)
  3082. + break;
  3083. + }
  3084. +
  3085. + if (!status) {
  3086. + gsb_response_success(buffer, rsp.pointer, rsp.length);
  3087. + } else {
  3088. + dev_err(d->dev, "rqst: failed with error %d\n", status);
  3089. + gsb_response_error(buffer, status);
  3090. + }
  3091. +
  3092. + return AE_OK;
  3093. +}
  3094. +
  3095. +static acpi_status san_rqsg(struct san_data *d, struct gsb_buffer *buffer)
  3096. +{
  3097. + struct gsb_data_rqsx *gsb_rqsg;
  3098. + struct surface_sam_san_rqsg rqsg;
  3099. + int status;
  3100. +
  3101. + gsb_rqsg = san_validate_rqsx(d->dev, "RQSG", buffer);
  3102. + if (!gsb_rqsg)
  3103. + return AE_OK;
  3104. +
  3105. + rqsg.tc = gsb_rqsg->tc;
  3106. + rqsg.cid = gsb_rqsg->cid;
  3107. + rqsg.iid = gsb_rqsg->iid;
  3108. + rqsg.cdl = get_unaligned(&gsb_rqsg->cdl);
  3109. + rqsg.pld = &gsb_rqsg->pld[0];
  3110. +
  3111. + status = san_call_rqsg_handler(&rqsg);
  3112. + if (!status) {
  3113. + gsb_response_success(buffer, NULL, 0);
  3114. + } else {
  3115. + dev_err(d->dev, "rqsg: failed with error %d\n", status);
  3116. + gsb_response_error(buffer, status);
  3117. + }
  3118. +
  3119. + return AE_OK;
  3120. +}
  3121. +
  3122. +
  3123. +static acpi_status
  3124. +san_opreg_handler(u32 function, acpi_physical_address command,
  3125. + u32 bits, u64 *value64,
  3126. + void *opreg_context, void *region_context)
  3127. +{
  3128. + struct san_data *d = to_san_data(opreg_context, context);
  3129. + struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
  3130. + int accessor_type = (0xFFFF0000 & function) >> 16;
  3131. +
  3132. + if (command != 0) {
  3133. + dev_warn(d->dev, "unsupported command: 0x%02llx\n", command);
  3134. + return AE_OK;
  3135. + }
  3136. +
  3137. + if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  3138. + dev_err(d->dev, "invalid access type: 0x%02x\n", accessor_type);
  3139. + return AE_OK;
  3140. + }
  3141. +
  3142. + // buffer must have at least contain the command-value
  3143. + if (buffer->len == 0) {
  3144. + dev_err(d->dev, "request-package too small\n");
  3145. + return AE_OK;
  3146. + }
  3147. +
  3148. + switch (buffer->data.in.cv) {
  3149. + case 0x01: return san_rqst(d, buffer);
  3150. + case 0x02: return san_etwl(d, buffer);
  3151. + case 0x03: return san_rqsg(d, buffer);
  3152. + }
  3153. +
  3154. + dev_warn(d->dev, "unsupported SAN0 request (cv: 0x%02x)\n", buffer->data.in.cv);
  3155. + return AE_OK;
  3156. +}
  3157. +
  3158. +static int san_events_register(struct platform_device *pdev)
  3159. +{
  3160. + struct san_data *d = platform_get_drvdata(pdev);
  3161. + int status;
  3162. +
  3163. + d->nf_bat.base.priority = 1;
  3164. + d->nf_bat.base.fn = san_evt_power_nb;
  3165. + d->nf_bat.event.reg = SSAM_EVENT_REGISTRY_SAM;
  3166. + d->nf_bat.event.id.target_category = SSAM_SSH_TC_BAT;
  3167. + d->nf_bat.event.id.instance = 0;
  3168. + d->nf_bat.event.flags = SSAM_EVENT_SEQUENCED;
  3169. +
  3170. + d->nf_tmp.base.priority = 1;
  3171. + d->nf_tmp.base.fn = san_evt_thermal_nb;
  3172. + d->nf_tmp.event.reg = SSAM_EVENT_REGISTRY_SAM;
  3173. + d->nf_tmp.event.id.target_category = SSAM_SSH_TC_TMP;
  3174. + d->nf_tmp.event.id.instance = 0;
  3175. + d->nf_tmp.event.flags = SSAM_EVENT_SEQUENCED;
  3176. +
  3177. + status = ssam_notifier_register(d->ctrl, &d->nf_bat);
  3178. + if (status)
  3179. + return status;
  3180. +
  3181. + status = ssam_notifier_register(d->ctrl, &d->nf_tmp);
  3182. + if (status)
  3183. + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  3184. +
  3185. + return status;
  3186. +}
  3187. +
  3188. +static void san_events_unregister(struct platform_device *pdev)
  3189. +{
  3190. + struct san_data *d = platform_get_drvdata(pdev);
  3191. +
  3192. + ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  3193. + ssam_notifier_unregister(d->ctrl, &d->nf_tmp);
  3194. +}
  3195. +
  3196. +
  3197. +static int san_consumers_link(struct platform_device *pdev,
  3198. + const struct san_acpi_consumer *cons,
  3199. + struct san_consumers *out)
  3200. +{
  3201. + const struct san_acpi_consumer *con;
  3202. + struct san_consumer_link *links, *link;
  3203. + struct acpi_device *adev;
  3204. + acpi_handle handle;
  3205. + u32 max_links = 0;
  3206. + int status;
  3207. +
  3208. + if (!cons)
  3209. + return 0;
  3210. +
  3211. + // count links
  3212. + for (con = cons; con->path; ++con)
  3213. + max_links += 1;
  3214. +
  3215. + // allocate
  3216. + links = kcalloc(max_links, sizeof(struct san_consumer_link), GFP_KERNEL);
  3217. + link = &links[0];
  3218. +
  3219. + if (!links)
  3220. + return -ENOMEM;
  3221. +
  3222. + // create links
  3223. + for (con = cons; con->path; ++con) {
  3224. + status = acpi_get_handle(NULL, con->path, &handle);
  3225. + if (status) {
  3226. + if (con->required || status != AE_NOT_FOUND) {
  3227. + status = -ENXIO;
  3228. + goto cleanup;
  3229. + } else {
  3230. + continue;
  3231. + }
  3232. + }
  3233. +
  3234. + status = acpi_bus_get_device(handle, &adev);
  3235. + if (status)
  3236. + goto cleanup;
  3237. +
  3238. + link->link = device_link_add(&adev->dev, &pdev->dev, con->flags);
  3239. + if (!(link->link)) {
  3240. + status = -EFAULT;
  3241. + goto cleanup;
  3242. + }
  3243. + link->properties = con;
  3244. +
  3245. + link += 1;
  3246. + }
  3247. +
  3248. + out->num = link - links;
  3249. + out->links = links;
  3250. +
  3251. + return 0;
  3252. +
  3253. +cleanup:
  3254. + for (link = link - 1; link >= links; --link) {
  3255. + if (link->properties->flags & DL_FLAG_STATELESS)
  3256. + device_link_del(link->link);
  3257. + }
  3258. +
  3259. + return status;
  3260. +}
  3261. +
  3262. +static void san_consumers_unlink(struct san_consumers *consumers)
  3263. +{
  3264. + u32 i;
  3265. +
  3266. + if (!consumers)
  3267. + return;
  3268. +
  3269. + for (i = 0; i < consumers->num; ++i) {
  3270. + if (consumers->links[i].properties->flags & DL_FLAG_STATELESS)
  3271. + device_link_del(consumers->links[i].link);
  3272. + }
  3273. +
  3274. + kfree(consumers->links);
  3275. +
  3276. + consumers->num = 0;
  3277. + consumers->links = NULL;
  3278. +}
  3279. +
  3280. +static int surface_sam_san_probe(struct platform_device *pdev)
  3281. +{
  3282. + const struct san_acpi_consumer *cons;
  3283. + acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
  3284. + struct ssam_controller *ctrl;
  3285. + struct san_data *data;
  3286. + int status;
  3287. +
  3288. + status = ssam_client_bind(&pdev->dev, &ctrl);
  3289. + if (status)
  3290. + return status == -ENXIO ? -EPROBE_DEFER : status;
  3291. +
  3292. + data = kzalloc(sizeof(struct san_data), GFP_KERNEL);
  3293. + if (!data)
  3294. + return -ENOMEM;
  3295. +
  3296. + data->dev = &pdev->dev;
  3297. + data->ctrl = ctrl;
  3298. +
  3299. + cons = acpi_device_get_match_data(&pdev->dev);
  3300. + status = san_consumers_link(pdev, cons, &data->consumers);
  3301. + if (status)
  3302. + goto err_consumers;
  3303. +
  3304. + platform_set_drvdata(pdev, data);
  3305. +
  3306. + status = acpi_install_address_space_handler(san,
  3307. + ACPI_ADR_SPACE_GSBUS,
  3308. + &san_opreg_handler,
  3309. + NULL, &data->context);
  3310. +
  3311. + if (ACPI_FAILURE(status)) {
  3312. + status = -ENODEV;
  3313. + goto err_install_handler;
  3314. + }
  3315. +
  3316. + status = san_events_register(pdev);
  3317. + if (status)
  3318. + goto err_enable_events;
  3319. +
  3320. + mutex_lock(&rqsg_if.lock);
  3321. + if (!rqsg_if.san_dev)
  3322. + rqsg_if.san_dev = &pdev->dev;
  3323. + else
  3324. + status = -EBUSY;
  3325. + mutex_unlock(&rqsg_if.lock);
  3326. +
  3327. + if (status)
  3328. + goto err_install_dev;
  3329. +
  3330. + acpi_walk_dep_device_list(san);
  3331. + return 0;
  3332. +
  3333. +err_install_dev:
  3334. + san_events_unregister(pdev);
  3335. +err_enable_events:
  3336. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &san_opreg_handler);
  3337. +err_install_handler:
  3338. + platform_set_drvdata(san, NULL);
  3339. + san_consumers_unlink(&data->consumers);
  3340. +err_consumers:
  3341. + kfree(data);
  3342. + return status;
  3343. +}
  3344. +
  3345. +static int surface_sam_san_remove(struct platform_device *pdev)
  3346. +{
  3347. + struct san_data *data = platform_get_drvdata(pdev);
  3348. + acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
  3349. + acpi_status status = AE_OK;
  3350. +
  3351. + mutex_lock(&rqsg_if.lock);
  3352. + rqsg_if.san_dev = NULL;
  3353. + mutex_unlock(&rqsg_if.lock);
  3354. +
  3355. + acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &san_opreg_handler);
  3356. + san_events_unregister(pdev);
  3357. +
  3358. + /*
  3359. + * We have unregistered our event sources. Now we need to ensure that
  3360. + * all delayed works they may have spawned are run to completion.
  3361. + */
  3362. + flush_scheduled_work();
  3363. +
  3364. + san_consumers_unlink(&data->consumers);
  3365. + kfree(data);
  3366. +
  3367. + platform_set_drvdata(pdev, NULL);
  3368. + return status;
  3369. +}
  3370. +
  3371. +
  3372. +static const struct san_acpi_consumer san_mshw0091_consumers[] = {
  3373. + { "\\_SB.SRTC", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  3374. + { "\\ADP1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  3375. + { "\\_SB.BAT1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  3376. + { "\\_SB.BAT2", false, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
  3377. + { },
  3378. +};
  3379. +
  3380. +static const struct acpi_device_id surface_sam_san_match[] = {
  3381. + { "MSHW0091", (unsigned long) san_mshw0091_consumers },
  3382. + { },
  3383. +};
  3384. +MODULE_DEVICE_TABLE(acpi, surface_sam_san_match);
  3385. +
  3386. +static struct platform_driver surface_sam_san = {
  3387. + .probe = surface_sam_san_probe,
  3388. + .remove = surface_sam_san_remove,
  3389. + .driver = {
  3390. + .name = "surface_sam_san",
  3391. + .acpi_match_table = surface_sam_san_match,
  3392. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3393. + },
  3394. +};
  3395. +module_platform_driver(surface_sam_san);
  3396. +
  3397. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3398. +MODULE_DESCRIPTION("Surface ACPI Notify Driver for 5th Generation Surface Devices");
  3399. +MODULE_LICENSE("GPL");
  3400. diff --git a/drivers/platform/x86/surface_sam/surface_sam_san.h b/drivers/platform/x86/surface_sam/surface_sam_san.h
  3401. new file mode 100644
  3402. index 0000000000000..3408dde964b3c
  3403. --- /dev/null
  3404. +++ b/drivers/platform/x86/surface_sam/surface_sam_san.h
  3405. @@ -0,0 +1,30 @@
  3406. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  3407. +/*
  3408. + * Interface for Surface ACPI/Notify (SAN).
  3409. + *
  3410. + * The SAN is the main interface between the Surface Serial Hub (SSH) and the
  3411. + * Surface/System Aggregator Module (SAM). It allows requests to be translated
  3412. + * from ACPI to SSH/SAM. It also interfaces with the discrete GPU hot-plug
  3413. + * driver.
  3414. + */
  3415. +
  3416. +#ifndef _SURFACE_SAM_SAN_H
  3417. +#define _SURFACE_SAM_SAN_H
  3418. +
  3419. +#include <linux/types.h>
  3420. +
  3421. +
  3422. +struct surface_sam_san_rqsg {
  3423. + u8 tc; // target category
  3424. + u8 cid; // command ID
  3425. + u8 iid; // instance ID
  3426. + u16 cdl; // command data length (length of payload)
  3427. + u8 *pld; // pointer to payload of length cdl
  3428. +};
  3429. +
  3430. +typedef int (*surface_sam_san_rqsg_handler_fn)(struct surface_sam_san_rqsg *rqsg, void *data);
  3431. +
  3432. +int surface_sam_san_consumer_register(struct device *consumer, u32 flags);
  3433. +int surface_sam_san_set_rqsg_handler(surface_sam_san_rqsg_handler_fn fn, void *data);
  3434. +
  3435. +#endif /* _SURFACE_SAM_SAN_H */
  3436. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid.c b/drivers/platform/x86/surface_sam/surface_sam_sid.c
  3437. new file mode 100644
  3438. index 0000000000000..bcf9a569ee719
  3439. --- /dev/null
  3440. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid.c
  3441. @@ -0,0 +1,283 @@
  3442. +// SPDX-License-Identifier: GPL-2.0-or-later
  3443. +/*
  3444. + * Surface Integration Driver.
  3445. + * MFD driver to provide device/model dependent functionality.
  3446. + */
  3447. +
  3448. +#include <linux/acpi.h>
  3449. +#include <linux/kernel.h>
  3450. +#include <linux/module.h>
  3451. +#include <linux/platform_device.h>
  3452. +#include <linux/mfd/core.h>
  3453. +
  3454. +#include "surface_sam_sid_power.h"
  3455. +#include "surface_sam_sid_vhf.h"
  3456. +
  3457. +
  3458. +static const struct ssam_battery_properties ssam_battery_props_bat1 = {
  3459. + .registry = SSAM_EVENT_REGISTRY_SAM,
  3460. + .num = 0,
  3461. + .channel = 1,
  3462. + .instance = 1,
  3463. +};
  3464. +
  3465. +static const struct ssam_battery_properties ssam_battery_props_bat2_sb3 = {
  3466. + .registry = SSAM_EVENT_REGISTRY_KIP,
  3467. + .num = 1,
  3468. + .channel = 2,
  3469. + .instance = 1,
  3470. +};
  3471. +
  3472. +
  3473. +static const struct ssam_hid_properties ssam_hid_props_keyboard = {
  3474. + .registry = SSAM_EVENT_REGISTRY_REG,
  3475. + .instance = 1,
  3476. +};
  3477. +
  3478. +static const struct ssam_hid_properties ssam_hid_props_touchpad = {
  3479. + .registry = SSAM_EVENT_REGISTRY_REG,
  3480. + .instance = 3,
  3481. +};
  3482. +
  3483. +static const struct ssam_hid_properties ssam_hid_props_iid5 = {
  3484. + .registry = SSAM_EVENT_REGISTRY_REG,
  3485. + .instance = 5,
  3486. +};
  3487. +
  3488. +static const struct ssam_hid_properties ssam_hid_props_iid6 = {
  3489. + .registry = SSAM_EVENT_REGISTRY_REG,
  3490. + .instance = 6,
  3491. +};
  3492. +
  3493. +
  3494. +static const struct mfd_cell sid_devs_sp4[] = {
  3495. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3496. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3497. + { },
  3498. +};
  3499. +
  3500. +static const struct mfd_cell sid_devs_sp6[] = {
  3501. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3502. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3503. + { },
  3504. +};
  3505. +
  3506. +static const struct mfd_cell sid_devs_sp7[] = {
  3507. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3508. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3509. + { .name = "surface_sam_sid_ac", .id = -1 },
  3510. + {
  3511. + .name = "surface_sam_sid_battery",
  3512. + .id = -1,
  3513. + .platform_data = (void *)&ssam_battery_props_bat1,
  3514. + .pdata_size = sizeof(struct ssam_battery_properties),
  3515. + },
  3516. + { },
  3517. +};
  3518. +
  3519. +static const struct mfd_cell sid_devs_sb1[] = {
  3520. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3521. + { },
  3522. +};
  3523. +
  3524. +static const struct mfd_cell sid_devs_sb2[] = {
  3525. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3526. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3527. + { },
  3528. +};
  3529. +
  3530. +static const struct mfd_cell sid_devs_sb3[] = {
  3531. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3532. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3533. + { .name = "surface_sam_sid_ac", .id = -1 },
  3534. + {
  3535. + .name = "surface_sam_sid_battery",
  3536. + .id = 1,
  3537. + .platform_data = (void *)&ssam_battery_props_bat1,
  3538. + .pdata_size = sizeof(struct ssam_battery_properties),
  3539. + },
  3540. + {
  3541. + .name = "surface_sam_sid_battery",
  3542. + .id = 2,
  3543. + .platform_data = (void *)&ssam_battery_props_bat2_sb3,
  3544. + .pdata_size = sizeof(struct ssam_battery_properties),
  3545. + },
  3546. + {
  3547. + .name = "surface_sam_sid_vhf",
  3548. + .id = 1,
  3549. + .platform_data = (void *)&ssam_hid_props_keyboard,
  3550. + .pdata_size = sizeof(struct ssam_hid_properties),
  3551. + },
  3552. + {
  3553. + .name = "surface_sam_sid_vhf",
  3554. + .id = 3,
  3555. + .platform_data = (void *)&ssam_hid_props_touchpad,
  3556. + .pdata_size = sizeof(struct ssam_hid_properties),
  3557. + },
  3558. + {
  3559. + .name = "surface_sam_sid_vhf",
  3560. + .id = 5,
  3561. + .platform_data = (void *)&ssam_hid_props_iid5,
  3562. + .pdata_size = sizeof(struct ssam_hid_properties),
  3563. + },
  3564. + {
  3565. + .name = "surface_sam_sid_vhf",
  3566. + .id = 6,
  3567. + .platform_data = (void *)&ssam_hid_props_iid6,
  3568. + .pdata_size = sizeof(struct ssam_hid_properties),
  3569. + },
  3570. + { },
  3571. +};
  3572. +
  3573. +static const struct mfd_cell sid_devs_sl1[] = {
  3574. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3575. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3576. + { },
  3577. +};
  3578. +
  3579. +static const struct mfd_cell sid_devs_sl2[] = {
  3580. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3581. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3582. + { },
  3583. +};
  3584. +
  3585. +static const struct mfd_cell sid_devs_sl3_13[] = {
  3586. + { .name = "surface_sam_sid_gpelid", .id = -1 },
  3587. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3588. + { .name = "surface_sam_sid_ac", .id = -1 },
  3589. + {
  3590. + .name = "surface_sam_sid_battery",
  3591. + .id = -1,
  3592. + .platform_data = (void *)&ssam_battery_props_bat1,
  3593. + .pdata_size = sizeof(struct ssam_battery_properties),
  3594. + },
  3595. + {
  3596. + .name = "surface_sam_sid_vhf",
  3597. + .id = 1,
  3598. + .platform_data = (void *)&ssam_hid_props_keyboard,
  3599. + .pdata_size = sizeof(struct ssam_hid_properties),
  3600. + },
  3601. + {
  3602. + .name = "surface_sam_sid_vhf",
  3603. + .id = 3,
  3604. + .platform_data = (void *)&ssam_hid_props_touchpad,
  3605. + .pdata_size = sizeof(struct ssam_hid_properties),
  3606. + },
  3607. + {
  3608. + .name = "surface_sam_sid_vhf",
  3609. + .id = 5,
  3610. + .platform_data = (void *)&ssam_hid_props_iid5,
  3611. + .pdata_size = sizeof(struct ssam_hid_properties),
  3612. + },
  3613. + { },
  3614. +};
  3615. +
  3616. +static const struct mfd_cell sid_devs_sl3_15[] = {
  3617. + { .name = "surface_sam_sid_perfmode", .id = -1 },
  3618. + { .name = "surface_sam_sid_ac", .id = -1 },
  3619. + {
  3620. + .name = "surface_sam_sid_battery",
  3621. + .id = -1,
  3622. + .platform_data = (void *)&ssam_battery_props_bat1,
  3623. + .pdata_size = sizeof(struct ssam_battery_properties),
  3624. + },
  3625. + {
  3626. + .name = "surface_sam_sid_vhf",
  3627. + .id = 1,
  3628. + .platform_data = (void *)&ssam_hid_props_keyboard,
  3629. + .pdata_size = sizeof(struct ssam_hid_properties),
  3630. + },
  3631. + {
  3632. + .name = "surface_sam_sid_vhf",
  3633. + .id = 3,
  3634. + .platform_data = (void *)&ssam_hid_props_touchpad,
  3635. + .pdata_size = sizeof(struct ssam_hid_properties),
  3636. + },
  3637. + {
  3638. + .name = "surface_sam_sid_vhf",
  3639. + .id = 5,
  3640. + .platform_data = (void *)&ssam_hid_props_iid5,
  3641. + .pdata_size = sizeof(struct ssam_hid_properties),
  3642. + },
  3643. + { },
  3644. +};
  3645. +
  3646. +static const struct acpi_device_id surface_sam_sid_match[] = {
  3647. + /* Surface Pro 4, 5, and 6 */
  3648. + { "MSHW0081", (unsigned long)sid_devs_sp4 },
  3649. +
  3650. + /* Surface Pro 6 (OMBR >= 0x10) */
  3651. + { "MSHW0111", (unsigned long)sid_devs_sp6 },
  3652. +
  3653. + /* Surface Pro 7 */
  3654. + { "MSHW0116", (unsigned long)sid_devs_sp7 },
  3655. +
  3656. + /* Surface Book 1 */
  3657. + { "MSHW0080", (unsigned long)sid_devs_sb1 },
  3658. +
  3659. + /* Surface Book 2 */
  3660. + { "MSHW0107", (unsigned long)sid_devs_sb2 },
  3661. +
  3662. + /* Surface Book 3 */
  3663. + { "MSHW0117", (unsigned long)sid_devs_sb3 },
  3664. +
  3665. + /* Surface Laptop 1 */
  3666. + { "MSHW0086", (unsigned long)sid_devs_sl1 },
  3667. +
  3668. + /* Surface Laptop 2 */
  3669. + { "MSHW0112", (unsigned long)sid_devs_sl2 },
  3670. +
  3671. + /* Surface Laptop 3 (13") */
  3672. + { "MSHW0114", (unsigned long)sid_devs_sl3_13 },
  3673. +
  3674. + /* Surface Laptop 3 (15") */
  3675. + { "MSHW0110", (unsigned long)sid_devs_sl3_15 },
  3676. +
  3677. + { },
  3678. +};
  3679. +MODULE_DEVICE_TABLE(acpi, surface_sam_sid_match);
  3680. +
  3681. +
  3682. +static int surface_sam_sid_probe(struct platform_device *pdev)
  3683. +{
  3684. + const struct acpi_device_id *match;
  3685. + const struct mfd_cell *cells, *p;
  3686. +
  3687. + match = acpi_match_device(surface_sam_sid_match, &pdev->dev);
  3688. + if (!match)
  3689. + return -ENODEV;
  3690. +
  3691. + cells = (struct mfd_cell *)match->driver_data;
  3692. + if (!cells)
  3693. + return -ENODEV;
  3694. +
  3695. + for (p = cells; p->name; ++p) {
  3696. + /* just count */
  3697. + }
  3698. +
  3699. + if (p == cells)
  3700. + return -ENODEV;
  3701. +
  3702. + return mfd_add_devices(&pdev->dev, 0, cells, p - cells, NULL, 0, NULL);
  3703. +}
  3704. +
  3705. +static int surface_sam_sid_remove(struct platform_device *pdev)
  3706. +{
  3707. + mfd_remove_devices(&pdev->dev);
  3708. + return 0;
  3709. +}
  3710. +
  3711. +static struct platform_driver surface_sam_sid = {
  3712. + .probe = surface_sam_sid_probe,
  3713. + .remove = surface_sam_sid_remove,
  3714. + .driver = {
  3715. + .name = "surface_sam_sid",
  3716. + .acpi_match_table = surface_sam_sid_match,
  3717. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3718. + },
  3719. +};
  3720. +module_platform_driver(surface_sam_sid);
  3721. +
  3722. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3723. +MODULE_DESCRIPTION("Surface Integration Driver for 5th Generation Surface Devices");
  3724. +MODULE_LICENSE("GPL");
  3725. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c b/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
  3726. new file mode 100644
  3727. index 0000000000000..f0cee43c859b4
  3728. --- /dev/null
  3729. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
  3730. @@ -0,0 +1,232 @@
  3731. +// SPDX-License-Identifier: GPL-2.0-or-later
  3732. +/*
  3733. + * Surface Lid driver to enable wakeup from suspend via the lid.
  3734. + */
  3735. +
  3736. +#include <linux/acpi.h>
  3737. +#include <linux/dmi.h>
  3738. +#include <linux/kernel.h>
  3739. +#include <linux/module.h>
  3740. +#include <linux/platform_device.h>
  3741. +
  3742. +
  3743. +struct sid_lid_device {
  3744. + const char *acpi_path;
  3745. + const u32 gpe_number;
  3746. +};
  3747. +
  3748. +
  3749. +static const struct sid_lid_device lid_device_l17 = {
  3750. + .acpi_path = "\\_SB.LID0",
  3751. + .gpe_number = 0x17,
  3752. +};
  3753. +
  3754. +static const struct sid_lid_device lid_device_l4D = {
  3755. + .acpi_path = "\\_SB.LID0",
  3756. + .gpe_number = 0x4D,
  3757. +};
  3758. +
  3759. +static const struct sid_lid_device lid_device_l4F = {
  3760. + .acpi_path = "\\_SB.LID0",
  3761. + .gpe_number = 0x4F,
  3762. +};
  3763. +
  3764. +static const struct sid_lid_device lid_device_l57 = {
  3765. + .acpi_path = "\\_SB.LID0",
  3766. + .gpe_number = 0x57,
  3767. +};
  3768. +
  3769. +
  3770. +static const struct dmi_system_id dmi_lid_device_table[] = {
  3771. + {
  3772. + .ident = "Surface Pro 4",
  3773. + .matches = {
  3774. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3775. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
  3776. + },
  3777. + .driver_data = (void *)&lid_device_l17,
  3778. + },
  3779. + {
  3780. + .ident = "Surface Pro 5",
  3781. + .matches = {
  3782. + /* match for SKU here due to generic product name "Surface Pro" */
  3783. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3784. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
  3785. + },
  3786. + .driver_data = (void *)&lid_device_l4F,
  3787. + },
  3788. + {
  3789. + .ident = "Surface Pro 5 (LTE)",
  3790. + .matches = {
  3791. + /* match for SKU here due to generic product name "Surface Pro" */
  3792. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3793. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
  3794. + },
  3795. + .driver_data = (void *)&lid_device_l4F,
  3796. + },
  3797. + {
  3798. + .ident = "Surface Pro 6",
  3799. + .matches = {
  3800. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3801. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
  3802. + },
  3803. + .driver_data = (void *)&lid_device_l4F,
  3804. + },
  3805. + {
  3806. + .ident = "Surface Pro 7",
  3807. + .matches = {
  3808. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3809. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
  3810. + },
  3811. + .driver_data = (void *)&lid_device_l4D,
  3812. + },
  3813. + {
  3814. + .ident = "Surface Book 1",
  3815. + .matches = {
  3816. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3817. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
  3818. + },
  3819. + .driver_data = (void *)&lid_device_l17,
  3820. + },
  3821. + {
  3822. + .ident = "Surface Book 2",
  3823. + .matches = {
  3824. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3825. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
  3826. + },
  3827. + .driver_data = (void *)&lid_device_l17,
  3828. + },
  3829. + {
  3830. + .ident = "Surface Book 3",
  3831. + .matches = {
  3832. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3833. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
  3834. + },
  3835. + .driver_data = (void *)&lid_device_l4D,
  3836. + },
  3837. + {
  3838. + .ident = "Surface Laptop 1",
  3839. + .matches = {
  3840. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3841. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
  3842. + },
  3843. + .driver_data = (void *)&lid_device_l57,
  3844. + },
  3845. + {
  3846. + .ident = "Surface Laptop 2",
  3847. + .matches = {
  3848. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3849. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
  3850. + },
  3851. + .driver_data = (void *)&lid_device_l57,
  3852. + },
  3853. + {
  3854. + .ident = "Surface Laptop 3 (13\")",
  3855. + .matches = {
  3856. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  3857. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
  3858. + },
  3859. + .driver_data = (void *)&lid_device_l4D,
  3860. + },
  3861. + { }
  3862. +};
  3863. +
  3864. +
  3865. +static int sid_lid_enable_wakeup(const struct sid_lid_device *dev, bool enable)
  3866. +{
  3867. + int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
  3868. + int status;
  3869. +
  3870. + status = acpi_set_gpe_wake_mask(NULL, dev->gpe_number, action);
  3871. + if (status)
  3872. + return -EFAULT;
  3873. +
  3874. + return 0;
  3875. +}
  3876. +
  3877. +
  3878. +static int surface_sam_sid_gpelid_suspend(struct device *dev)
  3879. +{
  3880. + const struct sid_lid_device *ldev;
  3881. +
  3882. + ldev = dev_get_drvdata(dev);
  3883. + return sid_lid_enable_wakeup(ldev, true);
  3884. +}
  3885. +
  3886. +static int surface_sam_sid_gpelid_resume(struct device *dev)
  3887. +{
  3888. + const struct sid_lid_device *ldev;
  3889. +
  3890. + ldev = dev_get_drvdata(dev);
  3891. + return sid_lid_enable_wakeup(ldev, false);
  3892. +}
  3893. +
  3894. +static SIMPLE_DEV_PM_OPS(surface_sam_sid_gpelid_pm,
  3895. + surface_sam_sid_gpelid_suspend,
  3896. + surface_sam_sid_gpelid_resume);
  3897. +
  3898. +
  3899. +static int surface_sam_sid_gpelid_probe(struct platform_device *pdev)
  3900. +{
  3901. + const struct dmi_system_id *match;
  3902. + struct sid_lid_device *dev;
  3903. + acpi_handle lid_handle;
  3904. + int status;
  3905. +
  3906. + match = dmi_first_match(dmi_lid_device_table);
  3907. + if (!match)
  3908. + return -ENODEV;
  3909. +
  3910. + dev = match->driver_data;
  3911. + if (!dev)
  3912. + return -ENODEV;
  3913. +
  3914. + status = acpi_get_handle(NULL, (acpi_string)dev->acpi_path, &lid_handle);
  3915. + if (status)
  3916. + return -EFAULT;
  3917. +
  3918. + status = acpi_setup_gpe_for_wake(lid_handle, NULL, dev->gpe_number);
  3919. + if (status)
  3920. + return -EFAULT;
  3921. +
  3922. + status = acpi_enable_gpe(NULL, dev->gpe_number);
  3923. + if (status)
  3924. + return -EFAULT;
  3925. +
  3926. + status = sid_lid_enable_wakeup(dev, false);
  3927. + if (status) {
  3928. + acpi_disable_gpe(NULL, dev->gpe_number);
  3929. + return status;
  3930. + }
  3931. +
  3932. + platform_set_drvdata(pdev, dev);
  3933. + return 0;
  3934. +}
  3935. +
  3936. +static int surface_sam_sid_gpelid_remove(struct platform_device *pdev)
  3937. +{
  3938. + struct sid_lid_device *dev = platform_get_drvdata(pdev);
  3939. +
  3940. + /* restore default behavior without this module */
  3941. + sid_lid_enable_wakeup(dev, false);
  3942. + acpi_disable_gpe(NULL, dev->gpe_number);
  3943. +
  3944. + platform_set_drvdata(pdev, NULL);
  3945. + return 0;
  3946. +}
  3947. +
  3948. +static struct platform_driver surface_sam_sid_gpelid = {
  3949. + .probe = surface_sam_sid_gpelid_probe,
  3950. + .remove = surface_sam_sid_gpelid_remove,
  3951. + .driver = {
  3952. + .name = "surface_sam_sid_gpelid",
  3953. + .pm = &surface_sam_sid_gpelid_pm,
  3954. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3955. + },
  3956. +};
  3957. +module_platform_driver(surface_sam_sid_gpelid);
  3958. +
  3959. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  3960. +MODULE_DESCRIPTION("Surface Lid Driver for 5th Generation Surface Devices");
  3961. +MODULE_LICENSE("GPL");
  3962. +MODULE_ALIAS("platform:surface_sam_sid_gpelid");
  3963. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c b/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
  3964. new file mode 100644
  3965. index 0000000000000..e0b1e42c2087f
  3966. --- /dev/null
  3967. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
  3968. @@ -0,0 +1,214 @@
  3969. +// SPDX-License-Identifier: GPL-2.0-or-later
  3970. +/*
  3971. + * Surface Performance Mode Driver.
  3972. + * Allows to change cooling capabilities based on user preference.
  3973. + */
  3974. +
  3975. +#include <asm/unaligned.h>
  3976. +#include <linux/kernel.h>
  3977. +#include <linux/module.h>
  3978. +#include <linux/platform_device.h>
  3979. +
  3980. +#include "surface_sam_ssh.h"
  3981. +
  3982. +
  3983. +#define SID_PARAM_PERM 0644
  3984. +
  3985. +enum sam_perf_mode {
  3986. + SAM_PERF_MODE_NORMAL = 1,
  3987. + SAM_PERF_MODE_BATTERY = 2,
  3988. + SAM_PERF_MODE_PERF1 = 3,
  3989. + SAM_PERF_MODE_PERF2 = 4,
  3990. +
  3991. + __SAM_PERF_MODE__START = 1,
  3992. + __SAM_PERF_MODE__END = 4,
  3993. +};
  3994. +
  3995. +enum sid_param_perf_mode {
  3996. + SID_PARAM_PERF_MODE_AS_IS = 0,
  3997. + SID_PARAM_PERF_MODE_NORMAL = SAM_PERF_MODE_NORMAL,
  3998. + SID_PARAM_PERF_MODE_BATTERY = SAM_PERF_MODE_BATTERY,
  3999. + SID_PARAM_PERF_MODE_PERF1 = SAM_PERF_MODE_PERF1,
  4000. + SID_PARAM_PERF_MODE_PERF2 = SAM_PERF_MODE_PERF2,
  4001. +
  4002. + __SID_PARAM_PERF_MODE__START = 0,
  4003. + __SID_PARAM_PERF_MODE__END = 4,
  4004. +};
  4005. +
  4006. +struct spm_data {
  4007. + struct ssam_controller *ctrl;
  4008. +};
  4009. +
  4010. +
  4011. +struct ssam_perf_info {
  4012. + __le32 mode;
  4013. + __le16 unknown1;
  4014. + __le16 unknown2;
  4015. +} __packed;
  4016. +
  4017. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_tmp_perf_mode_get, struct ssam_perf_info, {
  4018. + .target_category = SSAM_SSH_TC_TMP,
  4019. + .command_id = 0x02,
  4020. + .instance_id = 0x00,
  4021. + .channel = 0x01,
  4022. +});
  4023. +
  4024. +static SSAM_DEFINE_SYNC_REQUEST_W(__ssam_tmp_perf_mode_set, __le32, {
  4025. + .target_category = SSAM_SSH_TC_TMP,
  4026. + .command_id = 0x03,
  4027. + .instance_id = 0x00,
  4028. + .channel = 0x01,
  4029. +});
  4030. +
  4031. +static int ssam_tmp_perf_mode_set(struct ssam_controller *ctrl, u32 mode)
  4032. +{
  4033. + __le32 mode_le = cpu_to_le32(mode);
  4034. +
  4035. + if (mode < __SAM_PERF_MODE__START || mode > __SAM_PERF_MODE__END)
  4036. + return -EINVAL;
  4037. +
  4038. + return __ssam_tmp_perf_mode_set(ctrl, &mode_le);
  4039. +}
  4040. +
  4041. +
  4042. +static int param_perf_mode_set(const char *val, const struct kernel_param *kp)
  4043. +{
  4044. + int perf_mode;
  4045. + int status;
  4046. +
  4047. + status = kstrtoint(val, 0, &perf_mode);
  4048. + if (status)
  4049. + return status;
  4050. +
  4051. + if (perf_mode < __SID_PARAM_PERF_MODE__START || perf_mode > __SID_PARAM_PERF_MODE__END)
  4052. + return -EINVAL;
  4053. +
  4054. + return param_set_int(val, kp);
  4055. +}
  4056. +
  4057. +static const struct kernel_param_ops param_perf_mode_ops = {
  4058. + .set = param_perf_mode_set,
  4059. + .get = param_get_int,
  4060. +};
  4061. +
  4062. +static int param_perf_mode_init = SID_PARAM_PERF_MODE_AS_IS;
  4063. +static int param_perf_mode_exit = SID_PARAM_PERF_MODE_AS_IS;
  4064. +
  4065. +module_param_cb(perf_mode_init, &param_perf_mode_ops, &param_perf_mode_init, SID_PARAM_PERM);
  4066. +module_param_cb(perf_mode_exit, &param_perf_mode_ops, &param_perf_mode_exit, SID_PARAM_PERM);
  4067. +
  4068. +MODULE_PARM_DESC(perf_mode_init, "Performance-mode to be set on module initialization");
  4069. +MODULE_PARM_DESC(perf_mode_exit, "Performance-mode to be set on module exit");
  4070. +
  4071. +
  4072. +static ssize_t perf_mode_show(struct device *dev, struct device_attribute *attr, char *data)
  4073. +{
  4074. + struct spm_data *d = dev_get_drvdata(dev);
  4075. + struct ssam_perf_info info;
  4076. + int status;
  4077. +
  4078. + status = ssam_tmp_perf_mode_get(d->ctrl, &info);
  4079. + if (status) {
  4080. + dev_err(dev, "failed to get current performance mode: %d\n", status);
  4081. + return -EIO;
  4082. + }
  4083. +
  4084. + return sprintf(data, "%d\n", le32_to_cpu(info.mode));
  4085. +}
  4086. +
  4087. +static ssize_t perf_mode_store(struct device *dev, struct device_attribute *attr,
  4088. + const char *data, size_t count)
  4089. +{
  4090. + struct spm_data *d = dev_get_drvdata(dev);
  4091. + int perf_mode;
  4092. + int status;
  4093. +
  4094. + status = kstrtoint(data, 0, &perf_mode);
  4095. + if (status)
  4096. + return status;
  4097. +
  4098. + status = ssam_tmp_perf_mode_set(d->ctrl, perf_mode);
  4099. + if (status)
  4100. + return status;
  4101. +
  4102. + // TODO: Should we notify ACPI here?
  4103. + //
  4104. + // There is a _DSM call described as
  4105. + // WSID._DSM: Notify DPTF on Slider State change
  4106. + // which calls
  4107. + // ODV3 = ToInteger (Arg3)
  4108. + // Notify(IETM, 0x88)
  4109. + // IETM is an INT3400 Intel Dynamic Power Performance Management
  4110. + // device, part of the DPTF framework. From the corresponding
  4111. + // kernel driver, it looks like event 0x88 is being ignored. Also
  4112. + // it is currently unknown what the consequecnes of setting ODV3
  4113. + // are.
  4114. +
  4115. + return count;
  4116. +}
  4117. +
  4118. +static const DEVICE_ATTR_RW(perf_mode);
  4119. +
  4120. +
  4121. +static int surface_sam_sid_perfmode_probe(struct platform_device *pdev)
  4122. +{
  4123. + struct ssam_controller *ctrl;
  4124. + struct spm_data *data;
  4125. + int status;
  4126. +
  4127. + // link to ec
  4128. + status = ssam_client_bind(&pdev->dev, &ctrl);
  4129. + if (status)
  4130. + return status == -ENXIO ? -EPROBE_DEFER : status;
  4131. +
  4132. + data = devm_kzalloc(&pdev->dev, sizeof(struct spm_data), GFP_KERNEL);
  4133. + if (!data)
  4134. + return -ENOMEM;
  4135. +
  4136. + data->ctrl = ctrl;
  4137. + platform_set_drvdata(pdev, data);
  4138. +
  4139. + // set initial perf_mode
  4140. + if (param_perf_mode_init != SID_PARAM_PERF_MODE_AS_IS) {
  4141. + status = ssam_tmp_perf_mode_set(ctrl, param_perf_mode_init);
  4142. + if (status)
  4143. + return status;
  4144. + }
  4145. +
  4146. + // register perf_mode attribute
  4147. + status = sysfs_create_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
  4148. + if (status)
  4149. + goto err_sysfs;
  4150. +
  4151. + return 0;
  4152. +
  4153. +err_sysfs:
  4154. + ssam_tmp_perf_mode_set(ctrl, param_perf_mode_exit);
  4155. + return status;
  4156. +}
  4157. +
  4158. +static int surface_sam_sid_perfmode_remove(struct platform_device *pdev)
  4159. +{
  4160. + struct spm_data *data = platform_get_drvdata(pdev);
  4161. +
  4162. + sysfs_remove_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
  4163. + ssam_tmp_perf_mode_set(data->ctrl, param_perf_mode_exit);
  4164. +
  4165. + platform_set_drvdata(pdev, NULL);
  4166. + return 0;
  4167. +}
  4168. +
  4169. +static struct platform_driver surface_sam_sid_perfmode = {
  4170. + .probe = surface_sam_sid_perfmode_probe,
  4171. + .remove = surface_sam_sid_perfmode_remove,
  4172. + .driver = {
  4173. + .name = "surface_sam_sid_perfmode",
  4174. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  4175. + },
  4176. +};
  4177. +module_platform_driver(surface_sam_sid_perfmode);
  4178. +
  4179. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  4180. +MODULE_DESCRIPTION("Surface Performance Mode Driver for 5th Generation Surface Devices");
  4181. +MODULE_LICENSE("GPL");
  4182. +MODULE_ALIAS("platform:surface_sam_sid_perfmode");
  4183. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_power.c b/drivers/platform/x86/surface_sam/surface_sam_sid_power.c
  4184. new file mode 100644
  4185. index 0000000000000..64a3d46a128cc
  4186. --- /dev/null
  4187. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_power.c
  4188. @@ -0,0 +1,1054 @@
  4189. +// SPDX-License-Identifier: GPL-2.0-or-later
  4190. +/*
  4191. + * Surface SID Battery/AC Driver.
  4192. + * Provides support for the battery and AC on 7th generation Surface devices.
  4193. + */
  4194. +
  4195. +#include <asm/unaligned.h>
  4196. +#include <linux/kernel.h>
  4197. +#include <linux/delay.h>
  4198. +#include <linux/jiffies.h>
  4199. +#include <linux/module.h>
  4200. +#include <linux/platform_device.h>
  4201. +#include <linux/power_supply.h>
  4202. +#include <linux/workqueue.h>
  4203. +
  4204. +#include "surface_sam_ssh.h"
  4205. +#include "surface_sam_sid_power.h"
  4206. +
  4207. +#define SPWR_WARN KERN_WARNING KBUILD_MODNAME ": "
  4208. +#define SPWR_DEBUG KERN_DEBUG KBUILD_MODNAME ": "
  4209. +
  4210. +
  4211. +// TODO: check BIX/BST for unknown/unsupported 0xffffffff entries
  4212. +// TODO: DPTF (/SAN notifications)?
  4213. +// TODO: other properties?
  4214. +
  4215. +
  4216. +static unsigned int cache_time = 1000;
  4217. +module_param(cache_time, uint, 0644);
  4218. +MODULE_PARM_DESC(cache_time, "battery state chaching time in milliseconds [default: 1000]");
  4219. +
  4220. +#define SPWR_AC_BAT_UPDATE_DELAY msecs_to_jiffies(5000)
  4221. +
  4222. +
  4223. +/*
  4224. + * SAM Interface.
  4225. + */
  4226. +
  4227. +#define SAM_EVENT_PWR_CID_BIX 0x15
  4228. +#define SAM_EVENT_PWR_CID_BST 0x16
  4229. +#define SAM_EVENT_PWR_CID_ADAPTER 0x17
  4230. +
  4231. +#define SAM_BATTERY_STA_OK 0x0f
  4232. +#define SAM_BATTERY_STA_PRESENT 0x10
  4233. +
  4234. +#define SAM_BATTERY_STATE_DISCHARGING 0x01
  4235. +#define SAM_BATTERY_STATE_CHARGING 0x02
  4236. +#define SAM_BATTERY_STATE_CRITICAL 0x04
  4237. +
  4238. +#define SAM_BATTERY_POWER_UNIT_MA 1
  4239. +
  4240. +
  4241. +/* Equivalent to data returned in ACPI _BIX method */
  4242. +struct spwr_bix {
  4243. + u8 revision;
  4244. + __le32 power_unit;
  4245. + __le32 design_cap;
  4246. + __le32 last_full_charge_cap;
  4247. + __le32 technology;
  4248. + __le32 design_voltage;
  4249. + __le32 design_cap_warn;
  4250. + __le32 design_cap_low;
  4251. + __le32 cycle_count;
  4252. + __le32 measurement_accuracy;
  4253. + __le32 max_sampling_time;
  4254. + __le32 min_sampling_time;
  4255. + __le32 max_avg_interval;
  4256. + __le32 min_avg_interval;
  4257. + __le32 bat_cap_granularity_1;
  4258. + __le32 bat_cap_granularity_2;
  4259. + u8 model[21];
  4260. + u8 serial[11];
  4261. + u8 type[5];
  4262. + u8 oem_info[21];
  4263. +} __packed;
  4264. +
  4265. +/* Equivalent to data returned in ACPI _BST method */
  4266. +struct spwr_bst {
  4267. + __le32 state;
  4268. + __le32 present_rate;
  4269. + __le32 remaining_cap;
  4270. + __le32 present_voltage;
  4271. +} __packed;
  4272. +
  4273. +/* DPTF event payload */
  4274. +struct spwr_event_dptf {
  4275. + __le32 pmax;
  4276. + __le32 _1; /* currently unknown */
  4277. + __le32 _2; /* currently unknown */
  4278. +} __packed;
  4279. +
  4280. +
  4281. +/* Get battery status (_STA) */
  4282. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_sta, __le32, {
  4283. + .target_category = SSAM_SSH_TC_BAT,
  4284. + .command_id = 0x01,
  4285. +});
  4286. +
  4287. +/* Get battery static information (_BIX) */
  4288. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_bix, struct spwr_bix, {
  4289. + .target_category = SSAM_SSH_TC_BAT,
  4290. + .command_id = 0x02,
  4291. +});
  4292. +
  4293. +/* Get battery dynamic information (_BST) */
  4294. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_bst, struct spwr_bst, {
  4295. + .target_category = SSAM_SSH_TC_BAT,
  4296. + .command_id = 0x03,
  4297. +});
  4298. +
  4299. +/* Set battery trip point (_BTP) */
  4300. +static SSAM_DEFINE_SYNC_REQUEST_MD_W(ssam_bat_set_btp, __le32, {
  4301. + .target_category = SSAM_SSH_TC_BAT,
  4302. + .command_id = 0x04,
  4303. +});
  4304. +
  4305. +/* Get platform power soruce for battery (DPTF PSRC) */
  4306. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_psrc, __le32, {
  4307. + .target_category = SSAM_SSH_TC_BAT,
  4308. + .command_id = 0x0d,
  4309. +});
  4310. +
  4311. +/* Get maximum platform power for battery (DPTF PMAX) */
  4312. +__always_unused
  4313. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_pmax, __le32, {
  4314. + .target_category = SSAM_SSH_TC_BAT,
  4315. + .command_id = 0x0b,
  4316. +});
  4317. +
  4318. +/* Get adapter rating (DPTF ARTG) */
  4319. +__always_unused
  4320. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_artg, __le32, {
  4321. + .target_category = SSAM_SSH_TC_BAT,
  4322. + .command_id = 0x0f,
  4323. +});
  4324. +
  4325. +/* Unknown (DPTF PSOC) */
  4326. +__always_unused
  4327. +static SSAM_DEFINE_SYNC_REQUEST_MD_R(ssam_bat_get_psoc, __le32, {
  4328. + .target_category = SSAM_SSH_TC_BAT,
  4329. + .command_id = 0x0c,
  4330. +});
  4331. +
  4332. +/* Unknown (DPTF CHGI/ INT3403 SPPC) */
  4333. +__always_unused
  4334. +static SSAM_DEFINE_SYNC_REQUEST_MD_W(ssam_bat_set_chgi, __le32, {
  4335. + .target_category = SSAM_SSH_TC_BAT,
  4336. + .command_id = 0x0e,
  4337. +});
  4338. +
  4339. +
  4340. +/*
  4341. + * Common Power-Subsystem Interface.
  4342. + */
  4343. +
  4344. +struct spwr_battery_device {
  4345. + struct platform_device *pdev;
  4346. + struct ssam_controller *ctrl;
  4347. + const struct ssam_battery_properties *p;
  4348. +
  4349. + char name[32];
  4350. + struct power_supply *psy;
  4351. + struct power_supply_desc psy_desc;
  4352. +
  4353. + struct delayed_work update_work;
  4354. +
  4355. + struct ssam_event_notifier notif;
  4356. +
  4357. + struct mutex lock;
  4358. + unsigned long timestamp;
  4359. +
  4360. + __le32 sta;
  4361. + struct spwr_bix bix;
  4362. + struct spwr_bst bst;
  4363. + u32 alarm;
  4364. +};
  4365. +
  4366. +struct spwr_ac_device {
  4367. + struct platform_device *pdev;
  4368. + struct ssam_controller *ctrl;
  4369. +
  4370. + char name[32];
  4371. + struct power_supply *psy;
  4372. + struct power_supply_desc psy_desc;
  4373. +
  4374. + struct ssam_event_notifier notif;
  4375. +
  4376. + struct mutex lock;
  4377. +
  4378. + __le32 state;
  4379. +};
  4380. +
  4381. +static enum power_supply_property spwr_ac_props[] = {
  4382. + POWER_SUPPLY_PROP_ONLINE,
  4383. +};
  4384. +
  4385. +static enum power_supply_property spwr_battery_props_chg[] = {
  4386. + POWER_SUPPLY_PROP_STATUS,
  4387. + POWER_SUPPLY_PROP_PRESENT,
  4388. + POWER_SUPPLY_PROP_TECHNOLOGY,
  4389. + POWER_SUPPLY_PROP_CYCLE_COUNT,
  4390. + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  4391. + POWER_SUPPLY_PROP_VOLTAGE_NOW,
  4392. + POWER_SUPPLY_PROP_CURRENT_NOW,
  4393. + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  4394. + POWER_SUPPLY_PROP_CHARGE_FULL,
  4395. + POWER_SUPPLY_PROP_CHARGE_NOW,
  4396. + POWER_SUPPLY_PROP_CAPACITY,
  4397. + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  4398. + POWER_SUPPLY_PROP_MODEL_NAME,
  4399. + POWER_SUPPLY_PROP_MANUFACTURER,
  4400. + POWER_SUPPLY_PROP_SERIAL_NUMBER,
  4401. +};
  4402. +
  4403. +static enum power_supply_property spwr_battery_props_eng[] = {
  4404. + POWER_SUPPLY_PROP_STATUS,
  4405. + POWER_SUPPLY_PROP_PRESENT,
  4406. + POWER_SUPPLY_PROP_TECHNOLOGY,
  4407. + POWER_SUPPLY_PROP_CYCLE_COUNT,
  4408. + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  4409. + POWER_SUPPLY_PROP_VOLTAGE_NOW,
  4410. + POWER_SUPPLY_PROP_POWER_NOW,
  4411. + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  4412. + POWER_SUPPLY_PROP_ENERGY_FULL,
  4413. + POWER_SUPPLY_PROP_ENERGY_NOW,
  4414. + POWER_SUPPLY_PROP_CAPACITY,
  4415. + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  4416. + POWER_SUPPLY_PROP_MODEL_NAME,
  4417. + POWER_SUPPLY_PROP_MANUFACTURER,
  4418. + POWER_SUPPLY_PROP_SERIAL_NUMBER,
  4419. +};
  4420. +
  4421. +
  4422. +static int spwr_battery_register(struct spwr_battery_device *bat,
  4423. + struct platform_device *pdev,
  4424. + struct ssam_controller *ctrl,
  4425. + const struct ssam_battery_properties *p);
  4426. +
  4427. +static void spwr_battery_unregister(struct spwr_battery_device *bat);
  4428. +
  4429. +
  4430. +static inline bool spwr_battery_present(struct spwr_battery_device *bat)
  4431. +{
  4432. + return le32_to_cpu(bat->sta) & SAM_BATTERY_STA_PRESENT;
  4433. +}
  4434. +
  4435. +
  4436. +static inline int spwr_battery_load_sta(struct spwr_battery_device *bat)
  4437. +{
  4438. + return ssam_bat_get_sta(bat->ctrl, bat->p->channel, bat->p->instance,
  4439. + &bat->sta);
  4440. +}
  4441. +
  4442. +static inline int spwr_battery_load_bix(struct spwr_battery_device *bat)
  4443. +{
  4444. + if (!spwr_battery_present(bat))
  4445. + return 0;
  4446. +
  4447. + return ssam_bat_get_bix(bat->ctrl, bat->p->channel, bat->p->instance,
  4448. + &bat->bix);
  4449. +}
  4450. +
  4451. +static inline int spwr_battery_load_bst(struct spwr_battery_device *bat)
  4452. +{
  4453. + if (!spwr_battery_present(bat))
  4454. + return 0;
  4455. +
  4456. + return ssam_bat_get_bst(bat->ctrl, bat->p->channel, bat->p->instance,
  4457. + &bat->bst);
  4458. +}
  4459. +
  4460. +
  4461. +static inline int spwr_battery_set_alarm_unlocked(struct spwr_battery_device *bat, u32 value)
  4462. +{
  4463. + __le32 alarm = cpu_to_le32(value);
  4464. +
  4465. + bat->alarm = value;
  4466. + return ssam_bat_set_btp(bat->ctrl, bat->p->channel, bat->p->instance,
  4467. + &alarm);
  4468. +}
  4469. +
  4470. +static inline int spwr_battery_set_alarm(struct spwr_battery_device *bat, u32 value)
  4471. +{
  4472. + int status;
  4473. +
  4474. + mutex_lock(&bat->lock);
  4475. + status = spwr_battery_set_alarm_unlocked(bat, value);
  4476. + mutex_unlock(&bat->lock);
  4477. +
  4478. + return status;
  4479. +}
  4480. +
  4481. +static inline int spwr_battery_update_bst_unlocked(struct spwr_battery_device *bat, bool cached)
  4482. +{
  4483. + unsigned long cache_deadline = bat->timestamp + msecs_to_jiffies(cache_time);
  4484. + int status;
  4485. +
  4486. + if (cached && bat->timestamp && time_is_after_jiffies(cache_deadline))
  4487. + return 0;
  4488. +
  4489. + status = spwr_battery_load_sta(bat);
  4490. + if (status)
  4491. + return status;
  4492. +
  4493. + status = spwr_battery_load_bst(bat);
  4494. + if (status)
  4495. + return status;
  4496. +
  4497. + bat->timestamp = jiffies;
  4498. + return 0;
  4499. +}
  4500. +
  4501. +static int spwr_battery_update_bst(struct spwr_battery_device *bat, bool cached)
  4502. +{
  4503. + int status;
  4504. +
  4505. + mutex_lock(&bat->lock);
  4506. + status = spwr_battery_update_bst_unlocked(bat, cached);
  4507. + mutex_unlock(&bat->lock);
  4508. +
  4509. + return status;
  4510. +}
  4511. +
  4512. +static inline int spwr_battery_update_bix_unlocked(struct spwr_battery_device *bat)
  4513. +{
  4514. + int status;
  4515. +
  4516. + status = spwr_battery_load_sta(bat);
  4517. + if (status)
  4518. + return status;
  4519. +
  4520. + status = spwr_battery_load_bix(bat);
  4521. + if (status)
  4522. + return status;
  4523. +
  4524. + status = spwr_battery_load_bst(bat);
  4525. + if (status)
  4526. + return status;
  4527. +
  4528. + bat->timestamp = jiffies;
  4529. + return 0;
  4530. +}
  4531. +
  4532. +static int spwr_battery_update_bix(struct spwr_battery_device *bat)
  4533. +{
  4534. + int status;
  4535. +
  4536. + mutex_lock(&bat->lock);
  4537. + status = spwr_battery_update_bix_unlocked(bat);
  4538. + mutex_unlock(&bat->lock);
  4539. +
  4540. + return status;
  4541. +}
  4542. +
  4543. +static inline int spwr_ac_update_unlocked(struct spwr_ac_device *ac)
  4544. +{
  4545. + return ssam_bat_get_psrc(ac->ctrl, 0x01, 0x01, &ac->state);
  4546. +}
  4547. +
  4548. +static int spwr_ac_update(struct spwr_ac_device *ac)
  4549. +{
  4550. + int status;
  4551. +
  4552. + mutex_lock(&ac->lock);
  4553. + status = spwr_ac_update_unlocked(ac);
  4554. + mutex_unlock(&ac->lock);
  4555. +
  4556. + return status;
  4557. +}
  4558. +
  4559. +
  4560. +static int spwr_battery_recheck(struct spwr_battery_device *bat)
  4561. +{
  4562. + bool present = spwr_battery_present(bat);
  4563. + u32 unit = get_unaligned_le32(&bat->bix.power_unit);
  4564. + int status;
  4565. +
  4566. + status = spwr_battery_update_bix(bat);
  4567. + if (status)
  4568. + return status;
  4569. +
  4570. + // if battery has been attached, (re-)initialize alarm
  4571. + if (!present && spwr_battery_present(bat)) {
  4572. + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
  4573. + status = spwr_battery_set_alarm(bat, cap_warn);
  4574. + if (status)
  4575. + return status;
  4576. + }
  4577. +
  4578. + // if the unit has changed, re-add the battery
  4579. + if (unit != get_unaligned_le32(&bat->bix.power_unit)) {
  4580. + spwr_battery_unregister(bat);
  4581. + status = spwr_battery_register(bat, bat->pdev, bat->ctrl, bat->p);
  4582. + }
  4583. +
  4584. + return status;
  4585. +}
  4586. +
  4587. +
  4588. +static inline int spwr_notify_bix(struct spwr_battery_device *bat)
  4589. +{
  4590. + int status;
  4591. +
  4592. + status = spwr_battery_recheck(bat);
  4593. + if (!status)
  4594. + power_supply_changed(bat->psy);
  4595. +
  4596. + return status;
  4597. +}
  4598. +
  4599. +static inline int spwr_notify_bst(struct spwr_battery_device *bat)
  4600. +{
  4601. + int status;
  4602. +
  4603. + status = spwr_battery_update_bst(bat, false);
  4604. + if (!status)
  4605. + power_supply_changed(bat->psy);
  4606. +
  4607. + return status;
  4608. +}
  4609. +
  4610. +static inline int spwr_notify_adapter_bat(struct spwr_battery_device *bat)
  4611. +{
  4612. + u32 last_full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  4613. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  4614. +
  4615. + /*
  4616. + * Handle battery update quirk:
  4617. + * When the battery is fully charged and the adapter is plugged in or
  4618. + * removed, the EC does not send a separate event for the state
  4619. + * (charging/discharging) change. Furthermore it may take some time until
  4620. + * the state is updated on the battery. Schedule an update to solve this.
  4621. + */
  4622. +
  4623. + if (remaining_cap >= last_full_cap)
  4624. + schedule_delayed_work(&bat->update_work, SPWR_AC_BAT_UPDATE_DELAY);
  4625. +
  4626. + return 0;
  4627. +}
  4628. +
  4629. +static inline int spwr_notify_adapter_ac(struct spwr_ac_device *ac)
  4630. +{
  4631. + int status;
  4632. +
  4633. + status = spwr_ac_update(ac);
  4634. + if (!status)
  4635. + power_supply_changed(ac->psy);
  4636. +
  4637. + return status;
  4638. +}
  4639. +
  4640. +static u32 spwr_notify_bat(struct ssam_notifier_block *nb, const struct ssam_event *event)
  4641. +{
  4642. + struct spwr_battery_device *bat = container_of(nb, struct spwr_battery_device, notif.base);
  4643. + int status;
  4644. +
  4645. + dev_dbg(&bat->pdev->dev, "power event (cid = 0x%02x, iid = %d, chn = %d)\n",
  4646. + event->command_id, event->instance_id, event->channel);
  4647. +
  4648. + // handled here, needs to be handled for all channels/instances
  4649. + if (event->command_id == SAM_EVENT_PWR_CID_ADAPTER) {
  4650. + status = spwr_notify_adapter_bat(bat);
  4651. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  4652. + }
  4653. +
  4654. + // check for the correct channel and instance ID
  4655. + if (event->channel != bat->p->channel)
  4656. + return 0;
  4657. +
  4658. + if (event->instance_id != bat->p->instance)
  4659. + return 0;
  4660. +
  4661. + switch (event->command_id) {
  4662. + case SAM_EVENT_PWR_CID_BIX:
  4663. + status = spwr_notify_bix(bat);
  4664. + break;
  4665. +
  4666. + case SAM_EVENT_PWR_CID_BST:
  4667. + status = spwr_notify_bst(bat);
  4668. + break;
  4669. +
  4670. + default:
  4671. + return 0;
  4672. + }
  4673. +
  4674. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  4675. +}
  4676. +
  4677. +static u32 spwr_notify_ac(struct ssam_notifier_block *nb, const struct ssam_event *event)
  4678. +{
  4679. + struct spwr_ac_device *ac = container_of(nb, struct spwr_ac_device, notif.base);
  4680. + int status;
  4681. +
  4682. + dev_dbg(&ac->pdev->dev, "power event (cid = 0x%02x, iid = %d, chn = %d)\n",
  4683. + event->command_id, event->instance_id, event->channel);
  4684. +
  4685. + // AC has IID = 0
  4686. + if (event->instance_id != 0)
  4687. + return 0;
  4688. +
  4689. + switch (event->command_id) {
  4690. + case SAM_EVENT_PWR_CID_ADAPTER:
  4691. + status = spwr_notify_adapter_ac(ac);
  4692. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  4693. +
  4694. + default:
  4695. + return 0;
  4696. + }
  4697. +}
  4698. +
  4699. +static void spwr_battery_update_bst_workfn(struct work_struct *work)
  4700. +{
  4701. + struct delayed_work *dwork = to_delayed_work(work);
  4702. + struct spwr_battery_device *bat = container_of(dwork, struct spwr_battery_device, update_work);
  4703. + int status;
  4704. +
  4705. + status = spwr_battery_update_bst(bat, false);
  4706. + if (!status)
  4707. + power_supply_changed(bat->psy);
  4708. +
  4709. + if (status)
  4710. + dev_err(&bat->pdev->dev, "failed to update battery state: %d\n", status);
  4711. +}
  4712. +
  4713. +
  4714. +static inline int spwr_battery_prop_status(struct spwr_battery_device *bat)
  4715. +{
  4716. + u32 state = get_unaligned_le32(&bat->bst.state);
  4717. + u32 last_full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  4718. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  4719. + u32 present_rate = get_unaligned_le32(&bat->bst.present_rate);
  4720. +
  4721. + if (state & SAM_BATTERY_STATE_DISCHARGING)
  4722. + return POWER_SUPPLY_STATUS_DISCHARGING;
  4723. +
  4724. + if (state & SAM_BATTERY_STATE_CHARGING)
  4725. + return POWER_SUPPLY_STATUS_CHARGING;
  4726. +
  4727. + if (last_full_cap == remaining_cap)
  4728. + return POWER_SUPPLY_STATUS_FULL;
  4729. +
  4730. + if (present_rate == 0)
  4731. + return POWER_SUPPLY_STATUS_NOT_CHARGING;
  4732. +
  4733. + return POWER_SUPPLY_STATUS_UNKNOWN;
  4734. +}
  4735. +
  4736. +static inline int spwr_battery_prop_technology(struct spwr_battery_device *bat)
  4737. +{
  4738. + if (!strcasecmp("NiCd", bat->bix.type))
  4739. + return POWER_SUPPLY_TECHNOLOGY_NiCd;
  4740. +
  4741. + if (!strcasecmp("NiMH", bat->bix.type))
  4742. + return POWER_SUPPLY_TECHNOLOGY_NiMH;
  4743. +
  4744. + if (!strcasecmp("LION", bat->bix.type))
  4745. + return POWER_SUPPLY_TECHNOLOGY_LION;
  4746. +
  4747. + if (!strncasecmp("LI-ION", bat->bix.type, 6))
  4748. + return POWER_SUPPLY_TECHNOLOGY_LION;
  4749. +
  4750. + if (!strcasecmp("LiP", bat->bix.type))
  4751. + return POWER_SUPPLY_TECHNOLOGY_LIPO;
  4752. +
  4753. + return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  4754. +}
  4755. +
  4756. +static inline int spwr_battery_prop_capacity(struct spwr_battery_device *bat)
  4757. +{
  4758. + u32 last_full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  4759. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  4760. +
  4761. + if (remaining_cap && last_full_cap)
  4762. + return remaining_cap * 100 / last_full_cap;
  4763. + else
  4764. + return 0;
  4765. +}
  4766. +
  4767. +static inline int spwr_battery_prop_capacity_level(struct spwr_battery_device *bat)
  4768. +{
  4769. + u32 state = get_unaligned_le32(&bat->bst.state);
  4770. + u32 last_full_cap = get_unaligned_le32(&bat->bix.last_full_charge_cap);
  4771. + u32 remaining_cap = get_unaligned_le32(&bat->bst.remaining_cap);
  4772. +
  4773. + if (state & SAM_BATTERY_STATE_CRITICAL)
  4774. + return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  4775. +
  4776. + if (remaining_cap >= last_full_cap)
  4777. + return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  4778. +
  4779. + if (remaining_cap <= bat->alarm)
  4780. + return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  4781. +
  4782. + return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  4783. +}
  4784. +
  4785. +static int spwr_ac_get_property(struct power_supply *psy,
  4786. + enum power_supply_property psp,
  4787. + union power_supply_propval *val)
  4788. +{
  4789. + struct spwr_ac_device *ac = power_supply_get_drvdata(psy);
  4790. + int status;
  4791. +
  4792. + mutex_lock(&ac->lock);
  4793. +
  4794. + status = spwr_ac_update_unlocked(ac);
  4795. + if (status)
  4796. + goto out;
  4797. +
  4798. + switch (psp) {
  4799. + case POWER_SUPPLY_PROP_ONLINE:
  4800. + val->intval = le32_to_cpu(ac->state) == 1;
  4801. + break;
  4802. +
  4803. + default:
  4804. + status = -EINVAL;
  4805. + goto out;
  4806. + }
  4807. +
  4808. +out:
  4809. + mutex_unlock(&ac->lock);
  4810. + return status;
  4811. +}
  4812. +
  4813. +static int spwr_battery_get_property(struct power_supply *psy,
  4814. + enum power_supply_property psp,
  4815. + union power_supply_propval *val)
  4816. +{
  4817. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  4818. + int status;
  4819. +
  4820. + mutex_lock(&bat->lock);
  4821. +
  4822. + status = spwr_battery_update_bst_unlocked(bat, true);
  4823. + if (status)
  4824. + goto out;
  4825. +
  4826. + // abort if battery is not present
  4827. + if (!spwr_battery_present(bat) && psp != POWER_SUPPLY_PROP_PRESENT) {
  4828. + status = -ENODEV;
  4829. + goto out;
  4830. + }
  4831. +
  4832. + switch (psp) {
  4833. + case POWER_SUPPLY_PROP_STATUS:
  4834. + val->intval = spwr_battery_prop_status(bat);
  4835. + break;
  4836. +
  4837. + case POWER_SUPPLY_PROP_PRESENT:
  4838. + val->intval = spwr_battery_present(bat);
  4839. + break;
  4840. +
  4841. + case POWER_SUPPLY_PROP_TECHNOLOGY:
  4842. + val->intval = spwr_battery_prop_technology(bat);
  4843. + break;
  4844. +
  4845. + case POWER_SUPPLY_PROP_CYCLE_COUNT:
  4846. + val->intval = get_unaligned_le32(&bat->bix.cycle_count);
  4847. + break;
  4848. +
  4849. + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  4850. + val->intval = get_unaligned_le32(&bat->bix.design_voltage)
  4851. + * 1000;
  4852. + break;
  4853. +
  4854. + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  4855. + val->intval = get_unaligned_le32(&bat->bst.present_voltage)
  4856. + * 1000;
  4857. + break;
  4858. +
  4859. + case POWER_SUPPLY_PROP_CURRENT_NOW:
  4860. + case POWER_SUPPLY_PROP_POWER_NOW:
  4861. + val->intval = get_unaligned_le32(&bat->bst.present_rate) * 1000;
  4862. + break;
  4863. +
  4864. + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  4865. + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  4866. + val->intval = get_unaligned_le32(&bat->bix.design_cap) * 1000;
  4867. + break;
  4868. +
  4869. + case POWER_SUPPLY_PROP_CHARGE_FULL:
  4870. + case POWER_SUPPLY_PROP_ENERGY_FULL:
  4871. + val->intval = get_unaligned_le32(&bat->bix.last_full_charge_cap)
  4872. + * 1000;
  4873. + break;
  4874. +
  4875. + case POWER_SUPPLY_PROP_CHARGE_NOW:
  4876. + case POWER_SUPPLY_PROP_ENERGY_NOW:
  4877. + val->intval = get_unaligned_le32(&bat->bst.remaining_cap)
  4878. + * 1000;
  4879. + break;
  4880. +
  4881. + case POWER_SUPPLY_PROP_CAPACITY:
  4882. + val->intval = spwr_battery_prop_capacity(bat);
  4883. + break;
  4884. +
  4885. + case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  4886. + val->intval = spwr_battery_prop_capacity_level(bat);
  4887. + break;
  4888. +
  4889. + case POWER_SUPPLY_PROP_MODEL_NAME:
  4890. + val->strval = bat->bix.model;
  4891. + break;
  4892. +
  4893. + case POWER_SUPPLY_PROP_MANUFACTURER:
  4894. + val->strval = bat->bix.oem_info;
  4895. + break;
  4896. +
  4897. + case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  4898. + val->strval = bat->bix.serial;
  4899. + break;
  4900. +
  4901. + default:
  4902. + status = -EINVAL;
  4903. + goto out;
  4904. + }
  4905. +
  4906. +out:
  4907. + mutex_unlock(&bat->lock);
  4908. + return status;
  4909. +}
  4910. +
  4911. +
  4912. +static ssize_t spwr_battery_alarm_show(struct device *dev,
  4913. + struct device_attribute *attr,
  4914. + char *buf)
  4915. +{
  4916. + struct power_supply *psy = dev_get_drvdata(dev);
  4917. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  4918. +
  4919. + return sprintf(buf, "%d\n", bat->alarm * 1000);
  4920. +}
  4921. +
  4922. +static ssize_t spwr_battery_alarm_store(struct device *dev,
  4923. + struct device_attribute *attr,
  4924. + const char *buf, size_t count)
  4925. +{
  4926. + struct power_supply *psy = dev_get_drvdata(dev);
  4927. + struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
  4928. + unsigned long value;
  4929. + int status;
  4930. +
  4931. + status = kstrtoul(buf, 0, &value);
  4932. + if (status)
  4933. + return status;
  4934. +
  4935. + if (!spwr_battery_present(bat))
  4936. + return -ENODEV;
  4937. +
  4938. + status = spwr_battery_set_alarm(bat, value / 1000);
  4939. + if (status)
  4940. + return status;
  4941. +
  4942. + return count;
  4943. +}
  4944. +
  4945. +static const struct device_attribute alarm_attr = {
  4946. + .attr = {.name = "alarm", .mode = 0644},
  4947. + .show = spwr_battery_alarm_show,
  4948. + .store = spwr_battery_alarm_store,
  4949. +};
  4950. +
  4951. +
  4952. +static int spwr_ac_register(struct spwr_ac_device *ac,
  4953. + struct platform_device *pdev,
  4954. + struct ssam_controller *ctrl)
  4955. +{
  4956. + struct power_supply_config psy_cfg = {};
  4957. + __le32 sta;
  4958. + int status;
  4959. +
  4960. + // make sure the device is there and functioning properly
  4961. + status = ssam_bat_get_sta(ctrl, 0x01, 0x01, &sta);
  4962. + if (status)
  4963. + return status;
  4964. +
  4965. + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
  4966. + return -ENODEV;
  4967. +
  4968. + psy_cfg.drv_data = ac;
  4969. +
  4970. + ac->pdev = pdev;
  4971. + ac->ctrl = ctrl;
  4972. + mutex_init(&ac->lock);
  4973. +
  4974. + snprintf(ac->name, ARRAY_SIZE(ac->name), "ADP0");
  4975. +
  4976. + ac->psy_desc.name = ac->name;
  4977. + ac->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
  4978. + ac->psy_desc.properties = spwr_ac_props;
  4979. + ac->psy_desc.num_properties = ARRAY_SIZE(spwr_ac_props);
  4980. + ac->psy_desc.get_property = spwr_ac_get_property;
  4981. +
  4982. + ac->psy = power_supply_register(&ac->pdev->dev, &ac->psy_desc, &psy_cfg);
  4983. + if (IS_ERR(ac->psy)) {
  4984. + status = PTR_ERR(ac->psy);
  4985. + goto err_psy;
  4986. + }
  4987. +
  4988. + ac->notif.base.priority = 1;
  4989. + ac->notif.base.fn = spwr_notify_ac;
  4990. + ac->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  4991. + ac->notif.event.id.target_category = SSAM_SSH_TC_BAT;
  4992. + ac->notif.event.id.instance = 0;
  4993. + ac->notif.event.flags = SSAM_EVENT_SEQUENCED;
  4994. +
  4995. + status = ssam_notifier_register(ctrl, &ac->notif);
  4996. + if (status)
  4997. + goto err_notif;
  4998. +
  4999. + return 0;
  5000. +
  5001. +err_notif:
  5002. + power_supply_unregister(ac->psy);
  5003. +err_psy:
  5004. + mutex_destroy(&ac->lock);
  5005. + return status;
  5006. +}
  5007. +
  5008. +static int spwr_ac_unregister(struct spwr_ac_device *ac)
  5009. +{
  5010. + ssam_notifier_unregister(ac->ctrl, &ac->notif);
  5011. + power_supply_unregister(ac->psy);
  5012. + mutex_destroy(&ac->lock);
  5013. + return 0;
  5014. +}
  5015. +
  5016. +static int spwr_battery_register(struct spwr_battery_device *bat,
  5017. + struct platform_device *pdev,
  5018. + struct ssam_controller *ctrl,
  5019. + const struct ssam_battery_properties *p)
  5020. +{
  5021. + struct power_supply_config psy_cfg = {};
  5022. + __le32 sta;
  5023. + int status;
  5024. +
  5025. + bat->pdev = pdev;
  5026. + bat->ctrl = ctrl;
  5027. + bat->p = p;
  5028. +
  5029. + // make sure the device is there and functioning properly
  5030. + status = ssam_bat_get_sta(ctrl, bat->p->channel, bat->p->instance, &sta);
  5031. + if (status)
  5032. + return status;
  5033. +
  5034. + if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
  5035. + return -ENODEV;
  5036. +
  5037. + status = spwr_battery_update_bix_unlocked(bat);
  5038. + if (status)
  5039. + return status;
  5040. +
  5041. + if (spwr_battery_present(bat)) {
  5042. + u32 cap_warn = get_unaligned_le32(&bat->bix.design_cap_warn);
  5043. + status = spwr_battery_set_alarm_unlocked(bat, cap_warn);
  5044. + if (status)
  5045. + return status;
  5046. + }
  5047. +
  5048. + snprintf(bat->name, ARRAY_SIZE(bat->name), "BAT%d", bat->p->num);
  5049. + bat->psy_desc.name = bat->name;
  5050. + bat->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  5051. +
  5052. + if (get_unaligned_le32(&bat->bix.power_unit) == SAM_BATTERY_POWER_UNIT_MA) {
  5053. + bat->psy_desc.properties = spwr_battery_props_chg;
  5054. + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_chg);
  5055. + } else {
  5056. + bat->psy_desc.properties = spwr_battery_props_eng;
  5057. + bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_eng);
  5058. + }
  5059. +
  5060. + bat->psy_desc.get_property = spwr_battery_get_property;
  5061. +
  5062. + mutex_init(&bat->lock);
  5063. + psy_cfg.drv_data = bat;
  5064. +
  5065. + INIT_DELAYED_WORK(&bat->update_work, spwr_battery_update_bst_workfn);
  5066. +
  5067. + bat->psy = power_supply_register(&bat->pdev->dev, &bat->psy_desc, &psy_cfg);
  5068. + if (IS_ERR(bat->psy)) {
  5069. + status = PTR_ERR(bat->psy);
  5070. + goto err_psy;
  5071. + }
  5072. +
  5073. + bat->notif.base.priority = 1;
  5074. + bat->notif.base.fn = spwr_notify_bat;
  5075. + bat->notif.event.reg = p->registry;
  5076. + bat->notif.event.id.target_category = SSAM_SSH_TC_BAT;
  5077. + bat->notif.event.id.instance = 0;
  5078. + bat->notif.event.flags = SSAM_EVENT_SEQUENCED;
  5079. +
  5080. + status = ssam_notifier_register(ctrl, &bat->notif);
  5081. + if (status)
  5082. + goto err_notif;
  5083. +
  5084. + status = device_create_file(&bat->psy->dev, &alarm_attr);
  5085. + if (status)
  5086. + goto err_file;
  5087. +
  5088. + return 0;
  5089. +
  5090. +err_file:
  5091. + ssam_notifier_unregister(ctrl, &bat->notif);
  5092. +err_notif:
  5093. + power_supply_unregister(bat->psy);
  5094. +err_psy:
  5095. + mutex_destroy(&bat->lock);
  5096. + return status;
  5097. +}
  5098. +
  5099. +static void spwr_battery_unregister(struct spwr_battery_device *bat)
  5100. +{
  5101. + ssam_notifier_unregister(bat->ctrl, &bat->notif);
  5102. + cancel_delayed_work_sync(&bat->update_work);
  5103. + device_remove_file(&bat->psy->dev, &alarm_attr);
  5104. + power_supply_unregister(bat->psy);
  5105. + mutex_destroy(&bat->lock);
  5106. +}
  5107. +
  5108. +
  5109. +/*
  5110. + * Battery Driver.
  5111. + */
  5112. +
  5113. +#ifdef CONFIG_PM_SLEEP
  5114. +static int surface_sam_sid_battery_resume(struct device *dev)
  5115. +{
  5116. + struct spwr_battery_device *bat;
  5117. +
  5118. + bat = dev_get_drvdata(dev);
  5119. + return spwr_battery_recheck(bat);
  5120. +}
  5121. +#else
  5122. +#define surface_sam_sid_battery_resume NULL
  5123. +#endif
  5124. +
  5125. +SIMPLE_DEV_PM_OPS(surface_sam_sid_battery_pm, NULL, surface_sam_sid_battery_resume);
  5126. +
  5127. +static int surface_sam_sid_battery_probe(struct platform_device *pdev)
  5128. +{
  5129. + struct spwr_battery_device *bat;
  5130. + struct ssam_controller *ctrl;
  5131. + int status;
  5132. +
  5133. + // link to ec
  5134. + status = ssam_client_bind(&pdev->dev, &ctrl);
  5135. + if (status)
  5136. + return status == -ENXIO ? -EPROBE_DEFER : status;
  5137. +
  5138. + bat = devm_kzalloc(&pdev->dev, sizeof(struct spwr_battery_device), GFP_KERNEL);
  5139. + if (!bat)
  5140. + return -ENOMEM;
  5141. +
  5142. + platform_set_drvdata(pdev, bat);
  5143. + return spwr_battery_register(bat, pdev, ctrl, pdev->dev.platform_data);
  5144. +}
  5145. +
  5146. +static int surface_sam_sid_battery_remove(struct platform_device *pdev)
  5147. +{
  5148. + struct spwr_battery_device *bat;
  5149. +
  5150. + bat = platform_get_drvdata(pdev);
  5151. + spwr_battery_unregister(bat);
  5152. +
  5153. + return 0;
  5154. +}
  5155. +
  5156. +static struct platform_driver surface_sam_sid_battery = {
  5157. + .probe = surface_sam_sid_battery_probe,
  5158. + .remove = surface_sam_sid_battery_remove,
  5159. + .driver = {
  5160. + .name = "surface_sam_sid_battery",
  5161. + .pm = &surface_sam_sid_battery_pm,
  5162. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  5163. + },
  5164. +};
  5165. +
  5166. +
  5167. +/*
  5168. + * AC Driver.
  5169. + */
  5170. +
  5171. +static int surface_sam_sid_ac_probe(struct platform_device *pdev)
  5172. +{
  5173. + struct spwr_ac_device *ac;
  5174. + struct ssam_controller *ctrl;
  5175. + int status;
  5176. +
  5177. + // link to ec
  5178. + status = ssam_client_bind(&pdev->dev, &ctrl);
  5179. + if (status)
  5180. + return status == -ENXIO ? -EPROBE_DEFER : status;
  5181. +
  5182. + ac = devm_kzalloc(&pdev->dev, sizeof(struct spwr_ac_device), GFP_KERNEL);
  5183. + if (!ac)
  5184. + return -ENOMEM;
  5185. +
  5186. + status = spwr_ac_register(ac, pdev, ctrl);
  5187. + if (status)
  5188. + return status;
  5189. +
  5190. + platform_set_drvdata(pdev, ac);
  5191. + return 0;
  5192. +}
  5193. +
  5194. +static int surface_sam_sid_ac_remove(struct platform_device *pdev)
  5195. +{
  5196. + struct spwr_ac_device *ac;
  5197. +
  5198. + ac = platform_get_drvdata(pdev);
  5199. + return spwr_ac_unregister(ac);
  5200. +}
  5201. +
  5202. +static struct platform_driver surface_sam_sid_ac = {
  5203. + .probe = surface_sam_sid_ac_probe,
  5204. + .remove = surface_sam_sid_ac_remove,
  5205. + .driver = {
  5206. + .name = "surface_sam_sid_ac",
  5207. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  5208. + },
  5209. +};
  5210. +
  5211. +
  5212. +static int __init surface_sam_sid_power_init(void)
  5213. +{
  5214. + int status;
  5215. +
  5216. + status = platform_driver_register(&surface_sam_sid_battery);
  5217. + if (status)
  5218. + return status;
  5219. +
  5220. + status = platform_driver_register(&surface_sam_sid_ac);
  5221. + if (status) {
  5222. + platform_driver_unregister(&surface_sam_sid_battery);
  5223. + return status;
  5224. + }
  5225. +
  5226. + return 0;
  5227. +}
  5228. +
  5229. +static void __exit surface_sam_sid_power_exit(void)
  5230. +{
  5231. + platform_driver_unregister(&surface_sam_sid_battery);
  5232. + platform_driver_unregister(&surface_sam_sid_ac);
  5233. +}
  5234. +
  5235. +module_init(surface_sam_sid_power_init);
  5236. +module_exit(surface_sam_sid_power_exit);
  5237. +
  5238. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  5239. +MODULE_DESCRIPTION("Surface Battery/AC Driver for 7th Generation Surface Devices");
  5240. +MODULE_LICENSE("GPL");
  5241. +MODULE_ALIAS("platform:surface_sam_sid_ac");
  5242. +MODULE_ALIAS("platform:surface_sam_sid_battery");
  5243. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_power.h b/drivers/platform/x86/surface_sam/surface_sam_sid_power.h
  5244. new file mode 100644
  5245. index 0000000000000..d8d9509b7d122
  5246. --- /dev/null
  5247. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_power.h
  5248. @@ -0,0 +1,16 @@
  5249. +
  5250. +#ifndef _SURFACE_SAM_SID_POWER_H
  5251. +#define _SURFACE_SAM_SID_POWER_H
  5252. +
  5253. +#include <linux/types.h>
  5254. +#include "surface_sam_ssh.h"
  5255. +
  5256. +
  5257. +struct ssam_battery_properties {
  5258. + struct ssam_event_registry registry;
  5259. + u8 num;
  5260. + u8 channel;
  5261. + u8 instance;
  5262. +};
  5263. +
  5264. +#endif /* _SURFACE_SAM_SID_POWER_H */
  5265. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
  5266. new file mode 100644
  5267. index 0000000000000..a6059d6796619
  5268. --- /dev/null
  5269. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
  5270. @@ -0,0 +1,429 @@
  5271. +// SPDX-License-Identifier: GPL-2.0-or-later
  5272. +/*
  5273. + * Microsofs Surface HID (VHF) driver for HID input events via SAM.
  5274. + * Used for keyboard input events on the 7th generation Surface Laptops.
  5275. + */
  5276. +
  5277. +#include <linux/acpi.h>
  5278. +#include <linux/hid.h>
  5279. +#include <linux/input.h>
  5280. +#include <linux/platform_device.h>
  5281. +#include <linux/types.h>
  5282. +
  5283. +#include "surface_sam_ssh.h"
  5284. +#include "surface_sam_sid_vhf.h"
  5285. +
  5286. +#define SID_VHF_INPUT_NAME "Microsoft Surface HID"
  5287. +
  5288. +#define SAM_EVENT_SID_VHF_TC 0x15
  5289. +
  5290. +#define VHF_HID_STARTED 0
  5291. +
  5292. +struct sid_vhf {
  5293. + struct platform_device *dev;
  5294. + struct ssam_controller *ctrl;
  5295. + const struct ssam_hid_properties *p;
  5296. +
  5297. + struct ssam_event_notifier notif;
  5298. +
  5299. + struct hid_device *hid;
  5300. + unsigned long state;
  5301. +};
  5302. +
  5303. +
  5304. +static int sid_vhf_hid_start(struct hid_device *hid)
  5305. +{
  5306. + hid_dbg(hid, "%s\n", __func__);
  5307. + return 0;
  5308. +}
  5309. +
  5310. +static void sid_vhf_hid_stop(struct hid_device *hid)
  5311. +{
  5312. + hid_dbg(hid, "%s\n", __func__);
  5313. +}
  5314. +
  5315. +static int sid_vhf_hid_open(struct hid_device *hid)
  5316. +{
  5317. + struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
  5318. +
  5319. + hid_dbg(hid, "%s\n", __func__);
  5320. +
  5321. + set_bit(VHF_HID_STARTED, &vhf->state);
  5322. + return 0;
  5323. +}
  5324. +
  5325. +static void sid_vhf_hid_close(struct hid_device *hid)
  5326. +{
  5327. +
  5328. + struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
  5329. +
  5330. + hid_dbg(hid, "%s\n", __func__);
  5331. +
  5332. + clear_bit(VHF_HID_STARTED, &vhf->state);
  5333. +}
  5334. +
  5335. +struct surface_sam_sid_vhf_meta_rqst {
  5336. + u8 id;
  5337. + u32 offset;
  5338. + u32 length; // buffer limit on send, length of data received on receive
  5339. + u8 end; // 0x01 if end was reached
  5340. +} __packed;
  5341. +
  5342. +struct vhf_device_metadata_info {
  5343. + u8 len;
  5344. + u8 _2;
  5345. + u8 _3;
  5346. + u8 _4;
  5347. + u8 _5;
  5348. + u8 _6;
  5349. + u8 _7;
  5350. + u16 hid_len; // hid descriptor length
  5351. +} __packed;
  5352. +
  5353. +struct vhf_device_metadata {
  5354. + u32 len;
  5355. + u16 vendor_id;
  5356. + u16 product_id;
  5357. + u8 _1[24];
  5358. +} __packed;
  5359. +
  5360. +union vhf_buffer_data {
  5361. + struct vhf_device_metadata_info info;
  5362. + u8 pld[0x76];
  5363. + struct vhf_device_metadata meta;
  5364. +};
  5365. +
  5366. +struct surface_sam_sid_vhf_meta_resp {
  5367. + struct surface_sam_sid_vhf_meta_rqst rqst;
  5368. + union vhf_buffer_data data;
  5369. +} __packed;
  5370. +
  5371. +
  5372. +static int vhf_get_metadata(struct ssam_controller *ctrl, u8 iid,
  5373. + struct vhf_device_metadata *meta)
  5374. +{
  5375. + struct surface_sam_sid_vhf_meta_resp data = {};
  5376. + struct ssam_request rqst;
  5377. + struct ssam_response rsp;
  5378. + int status;
  5379. +
  5380. + data.rqst.id = 2;
  5381. + data.rqst.offset = 0;
  5382. + data.rqst.length = 0x76;
  5383. + data.rqst.end = 0;
  5384. +
  5385. + rqst.target_category = SSAM_SSH_TC_HID;;
  5386. + rqst.command_id = 0x04;
  5387. + rqst.instance_id = iid;
  5388. + rqst.channel = 0x02;
  5389. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  5390. + rqst.length = sizeof(struct surface_sam_sid_vhf_meta_rqst);
  5391. + rqst.payload = (u8 *)&data.rqst;
  5392. +
  5393. + rsp.capacity = sizeof(struct surface_sam_sid_vhf_meta_resp);
  5394. + rsp.length = 0;
  5395. + rsp.pointer = (u8 *)&data;
  5396. +
  5397. + status = ssam_request_sync(ctrl, &rqst, &rsp);
  5398. + if (status)
  5399. + return status;
  5400. +
  5401. + *meta = data.data.meta;
  5402. +
  5403. + return 0;
  5404. +}
  5405. +
  5406. +static int vhf_get_hid_descriptor(struct hid_device *hid, u8 iid, u8 **desc, int *size)
  5407. +{
  5408. + struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
  5409. + struct surface_sam_sid_vhf_meta_resp data = {};
  5410. + struct ssam_request rqst;
  5411. + struct ssam_response rsp;
  5412. + int status, len;
  5413. + u8 *buf;
  5414. +
  5415. + data.rqst.id = 0;
  5416. + data.rqst.offset = 0;
  5417. + data.rqst.length = 0x76;
  5418. + data.rqst.end = 0;
  5419. +
  5420. + rqst.target_category = SSAM_SSH_TC_HID;
  5421. + rqst.command_id = 0x04;
  5422. + rqst.instance_id = iid;
  5423. + rqst.channel = 0x02;
  5424. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  5425. + rqst.length = sizeof(struct surface_sam_sid_vhf_meta_rqst);
  5426. + rqst.payload = (u8 *)&data.rqst;
  5427. +
  5428. + rsp.capacity = sizeof(struct surface_sam_sid_vhf_meta_resp);
  5429. + rsp.length = 0;
  5430. + rsp.pointer = (u8 *)&data;
  5431. +
  5432. + // first fetch 00 to get the total length
  5433. + status = ssam_request_sync(vhf->ctrl, &rqst, &rsp);
  5434. + if (status)
  5435. + return status;
  5436. +
  5437. + len = data.data.info.hid_len;
  5438. +
  5439. + // allocate a buffer for the descriptor
  5440. + buf = kzalloc(len, GFP_KERNEL);
  5441. +
  5442. + // then, iterate and write into buffer, copying out bytes
  5443. + data.rqst.id = 1;
  5444. + data.rqst.offset = 0;
  5445. + data.rqst.length = 0x76;
  5446. + data.rqst.end = 0;
  5447. +
  5448. + while (!data.rqst.end && data.rqst.offset < len) {
  5449. + status = ssam_request_sync(vhf->ctrl, &rqst, &rsp);
  5450. + if (status) {
  5451. + kfree(buf);
  5452. + return status;
  5453. + }
  5454. + memcpy(buf + data.rqst.offset, data.data.pld, data.rqst.length);
  5455. +
  5456. + data.rqst.offset += data.rqst.length;
  5457. + }
  5458. +
  5459. + *desc = buf;
  5460. + *size = len;
  5461. +
  5462. + return 0;
  5463. +}
  5464. +
  5465. +static int sid_vhf_hid_parse(struct hid_device *hid)
  5466. +{
  5467. + struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
  5468. + int ret = 0, size;
  5469. + u8 *buf;
  5470. +
  5471. + ret = vhf_get_hid_descriptor(hid, vhf->p->instance, &buf, &size);
  5472. + if (ret != 0) {
  5473. + hid_err(hid, "Failed to read HID descriptor from device: %d\n", ret);
  5474. + return -EIO;
  5475. + }
  5476. + hid_dbg(hid, "HID descriptor of device:");
  5477. + print_hex_dump_debug("descriptor:", DUMP_PREFIX_OFFSET, 16, 1, buf, size, false);
  5478. +
  5479. + ret = hid_parse_report(hid, buf, size);
  5480. + kfree(buf);
  5481. + return ret;
  5482. +
  5483. +}
  5484. +
  5485. +static int sid_vhf_hid_raw_request(struct hid_device *hid, unsigned char
  5486. + reportnum, u8 *buf, size_t len, unsigned char rtype, int
  5487. + reqtype)
  5488. +{
  5489. + struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
  5490. + struct ssam_request rqst;
  5491. + struct ssam_response rsp;
  5492. + int status;
  5493. + u8 cid;
  5494. +
  5495. + hid_dbg(hid, "%s: reportnum=%#04x rtype=%i reqtype=%i\n", __func__, reportnum, rtype, reqtype);
  5496. + print_hex_dump_debug("report:", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
  5497. +
  5498. + // Byte 0 is the report number. Report data starts at byte 1.
  5499. + buf[0] = reportnum;
  5500. +
  5501. + switch (rtype) {
  5502. + case HID_OUTPUT_REPORT:
  5503. + cid = 0x01;
  5504. + break;
  5505. + case HID_FEATURE_REPORT:
  5506. + switch (reqtype) {
  5507. + case HID_REQ_GET_REPORT:
  5508. + // The EC doesn't respond to GET FEATURE for these touchpad reports
  5509. + // we immediately discard to avoid waiting for a timeout.
  5510. + if (reportnum == 6 || reportnum == 7 || reportnum == 8 || reportnum == 9 || reportnum == 0x0b) {
  5511. + hid_dbg(hid, "%s: skipping get feature report for 0x%02x\n", __func__, reportnum);
  5512. + return 0;
  5513. + }
  5514. +
  5515. + cid = 0x02;
  5516. + break;
  5517. + case HID_REQ_SET_REPORT:
  5518. + cid = 0x03;
  5519. + break;
  5520. + default:
  5521. + hid_err(hid, "%s: unknown req type 0x%02x\n", __func__, rtype);
  5522. + return -EIO;
  5523. + }
  5524. + break;
  5525. + default:
  5526. + hid_err(hid, "%s: unknown report type 0x%02x\n", __func__, reportnum);
  5527. + return -EIO;
  5528. + }
  5529. +
  5530. + rqst.target_category = SSAM_SSH_TC_HID;
  5531. + rqst.channel = 0x02;
  5532. + rqst.instance_id = vhf->p->instance;
  5533. + rqst.command_id = cid;
  5534. + rqst.flags = reqtype == HID_REQ_GET_REPORT ? SSAM_REQUEST_HAS_RESPONSE : 0;
  5535. + rqst.length = reqtype == HID_REQ_GET_REPORT ? 1 : len;
  5536. + rqst.payload = buf;
  5537. +
  5538. + rsp.capacity = len;
  5539. + rsp.length = 0;
  5540. + rsp.pointer = buf;
  5541. +
  5542. + hid_dbg(hid, "%s: sending to cid=%#04x snc=%#04x\n", __func__, cid, HID_REQ_GET_REPORT == reqtype);
  5543. +
  5544. + status = ssam_request_sync(vhf->ctrl, &rqst, &rsp);
  5545. + hid_dbg(hid, "%s: status %i\n", __func__, status);
  5546. +
  5547. + if (status)
  5548. + return status;
  5549. +
  5550. + if (rsp.length > 0)
  5551. + print_hex_dump_debug("response:", DUMP_PREFIX_OFFSET, 16, 1, rsp.pointer, rsp.length, false);
  5552. +
  5553. + return rsp.length;
  5554. +}
  5555. +
  5556. +static struct hid_ll_driver sid_vhf_hid_ll_driver = {
  5557. + .start = sid_vhf_hid_start,
  5558. + .stop = sid_vhf_hid_stop,
  5559. + .open = sid_vhf_hid_open,
  5560. + .close = sid_vhf_hid_close,
  5561. + .parse = sid_vhf_hid_parse,
  5562. + .raw_request = sid_vhf_hid_raw_request,
  5563. +};
  5564. +
  5565. +
  5566. +static struct hid_device *sid_vhf_create_hid_device(struct platform_device *pdev, struct vhf_device_metadata *meta)
  5567. +{
  5568. + struct hid_device *hid;
  5569. +
  5570. + hid = hid_allocate_device();
  5571. + if (IS_ERR(hid))
  5572. + return hid;
  5573. +
  5574. + hid->dev.parent = &pdev->dev;
  5575. +
  5576. + hid->bus = BUS_VIRTUAL;
  5577. + hid->vendor = meta->vendor_id;
  5578. + hid->product = meta->product_id;
  5579. +
  5580. + hid->ll_driver = &sid_vhf_hid_ll_driver;
  5581. +
  5582. + sprintf(hid->name, "%s", SID_VHF_INPUT_NAME);
  5583. +
  5584. + return hid;
  5585. +}
  5586. +
  5587. +static u32 sid_vhf_event_handler(struct ssam_notifier_block *nb, const struct ssam_event *event)
  5588. +{
  5589. + struct sid_vhf *vhf = container_of(nb, struct sid_vhf, notif.base);
  5590. + int status;
  5591. +
  5592. + if (event->target_category != SSAM_SSH_TC_HID)
  5593. + return 0;
  5594. +
  5595. + if (event->channel != 0x02)
  5596. + return 0;
  5597. +
  5598. + if (event->instance_id != vhf->p->instance)
  5599. + return 0;
  5600. +
  5601. + if (event->command_id != 0x00 && event->command_id != 0x03 && event->command_id != 0x04)
  5602. + return 0;
  5603. +
  5604. + // skip if HID hasn't started yet
  5605. + if (!test_bit(VHF_HID_STARTED, &vhf->state))
  5606. + return SSAM_NOTIF_HANDLED;
  5607. +
  5608. + status = hid_input_report(vhf->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
  5609. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  5610. +}
  5611. +
  5612. +static int surface_sam_sid_vhf_probe(struct platform_device *pdev)
  5613. +{
  5614. + const struct ssam_hid_properties *p = pdev->dev.platform_data;
  5615. + struct ssam_controller *ctrl;
  5616. + struct sid_vhf *vhf;
  5617. + struct vhf_device_metadata meta = {};
  5618. + struct hid_device *hid;
  5619. + int status;
  5620. +
  5621. + // add device link to EC
  5622. + status = ssam_client_bind(&pdev->dev, &ctrl);
  5623. + if (status)
  5624. + return status == -ENXIO ? -EPROBE_DEFER : status;
  5625. +
  5626. + vhf = kzalloc(sizeof(struct sid_vhf), GFP_KERNEL);
  5627. + if (!vhf)
  5628. + return -ENOMEM;
  5629. +
  5630. + status = vhf_get_metadata(ctrl, p->instance, &meta);
  5631. + if (status)
  5632. + goto err_create_hid;
  5633. +
  5634. + hid = sid_vhf_create_hid_device(pdev, &meta);
  5635. + if (IS_ERR(hid)) {
  5636. + status = PTR_ERR(hid);
  5637. + goto err_create_hid;
  5638. + }
  5639. +
  5640. + vhf->dev = pdev;
  5641. + vhf->ctrl = ctrl;
  5642. + vhf->p = pdev->dev.platform_data;
  5643. + vhf->hid = hid;
  5644. +
  5645. + vhf->notif.base.priority = 1;
  5646. + vhf->notif.base.fn = sid_vhf_event_handler;
  5647. + vhf->notif.event.reg = p->registry;
  5648. + vhf->notif.event.id.target_category = SSAM_SSH_TC_HID;
  5649. + vhf->notif.event.id.instance = p->instance;
  5650. + vhf->notif.event.flags = 0;
  5651. +
  5652. + platform_set_drvdata(pdev, vhf);
  5653. +
  5654. + status = ssam_notifier_register(ctrl, &vhf->notif);
  5655. + if (status)
  5656. + goto err_notif;
  5657. +
  5658. + status = hid_add_device(hid);
  5659. + if (status)
  5660. + goto err_add_hid;
  5661. +
  5662. + return 0;
  5663. +
  5664. +err_add_hid:
  5665. + ssam_notifier_unregister(ctrl, &vhf->notif);
  5666. +err_notif:
  5667. + hid_destroy_device(hid);
  5668. + platform_set_drvdata(pdev, NULL);
  5669. +err_create_hid:
  5670. + kfree(vhf);
  5671. + return status;
  5672. +}
  5673. +
  5674. +static int surface_sam_sid_vhf_remove(struct platform_device *pdev)
  5675. +{
  5676. + struct sid_vhf *vhf = platform_get_drvdata(pdev);
  5677. +
  5678. + ssam_notifier_unregister(vhf->ctrl, &vhf->notif);
  5679. + hid_destroy_device(vhf->hid);
  5680. + kfree(vhf);
  5681. +
  5682. + platform_set_drvdata(pdev, NULL);
  5683. + return 0;
  5684. +}
  5685. +
  5686. +static struct platform_driver surface_sam_sid_vhf = {
  5687. + .probe = surface_sam_sid_vhf_probe,
  5688. + .remove = surface_sam_sid_vhf_remove,
  5689. + .driver = {
  5690. + .name = "surface_sam_sid_vhf",
  5691. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  5692. + },
  5693. +};
  5694. +module_platform_driver(surface_sam_sid_vhf);
  5695. +
  5696. +MODULE_AUTHOR("Blaž Hrastnik <blaz@mxxn.io>");
  5697. +MODULE_DESCRIPTION("Driver for HID devices connected via Surface SAM");
  5698. +MODULE_LICENSE("GPL");
  5699. +MODULE_ALIAS("platform:surface_sam_sid_vhf");
  5700. diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
  5701. new file mode 100644
  5702. index 0000000000000..d956de5cf877a
  5703. --- /dev/null
  5704. +++ b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
  5705. @@ -0,0 +1,14 @@
  5706. +
  5707. +#ifndef _SURFACE_SAM_SID_VHF_H
  5708. +#define _SURFACE_SAM_SID_VHF_H
  5709. +
  5710. +#include <linux/types.h>
  5711. +#include "surface_sam_ssh.h"
  5712. +
  5713. +
  5714. +struct ssam_hid_properties {
  5715. + struct ssam_event_registry registry;
  5716. + u8 instance;
  5717. +};
  5718. +
  5719. +#endif /* _SURFACE_SAM_SID_VHF_H */
  5720. diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh.c b/drivers/platform/x86/surface_sam/surface_sam_ssh.c
  5721. new file mode 100644
  5722. index 0000000000000..4144fe0b3d192
  5723. --- /dev/null
  5724. +++ b/drivers/platform/x86/surface_sam/surface_sam_ssh.c
  5725. @@ -0,0 +1,5190 @@
  5726. +// SPDX-License-Identifier: GPL-2.0-or-later
  5727. +/*
  5728. + * Surface Serial Hub (SSH) driver for communication with the Surface/System
  5729. + * Aggregator Module.
  5730. + */
  5731. +
  5732. +#include <asm/unaligned.h>
  5733. +#include <linux/acpi.h>
  5734. +#include <linux/atomic.h>
  5735. +#include <linux/completion.h>
  5736. +#include <linux/crc-ccitt.h>
  5737. +#include <linux/dmaengine.h>
  5738. +#include <linux/gpio/consumer.h>
  5739. +#include <linux/interrupt.h>
  5740. +#include <linux/jiffies.h>
  5741. +#include <linux/kernel.h>
  5742. +#include <linux/kfifo.h>
  5743. +#include <linux/kref.h>
  5744. +#include <linux/kthread.h>
  5745. +#include <linux/ktime.h>
  5746. +#include <linux/list.h>
  5747. +#include <linux/mutex.h>
  5748. +#include <linux/pm.h>
  5749. +#include <linux/refcount.h>
  5750. +#include <linux/serdev.h>
  5751. +#include <linux/spinlock.h>
  5752. +#include <linux/sysfs.h>
  5753. +#include <linux/workqueue.h>
  5754. +
  5755. +#include "surface_sam_ssh.h"
  5756. +
  5757. +#define CREATE_TRACE_POINTS
  5758. +#include "surface_sam_ssh_trace.h"
  5759. +
  5760. +
  5761. +/* -- Error injection helpers. ---------------------------------------------- */
  5762. +
  5763. +#ifdef CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION
  5764. +#define noinline_if_inject noinline
  5765. +#else /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
  5766. +#define noinline_if_inject inline
  5767. +#endif /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
  5768. +
  5769. +
  5770. +/* -- SSH protocol utility functions and definitions. ----------------------- */
  5771. +
  5772. +/*
  5773. + * The number of reserved event IDs, used for registering an SSH event
  5774. + * handler. Valid event IDs are numbers below or equal to this value, with
  5775. + * exception of zero, which is not an event ID. Thus, this is also the
  5776. + * absolute maximum number of event handlers that can be registered.
  5777. + */
  5778. +#define SSH_NUM_EVENTS 34
  5779. +
  5780. +/*
  5781. + * The number of communication channels used in the protocol.
  5782. + */
  5783. +#define SSH_NUM_CHANNELS 2
  5784. +
  5785. +
  5786. +static inline u16 ssh_crc(const u8 *buf, size_t len)
  5787. +{
  5788. + return crc_ccitt_false(0xffff, buf, len);
  5789. +}
  5790. +
  5791. +static inline u16 ssh_rqid_next_valid(u16 rqid)
  5792. +{
  5793. + return rqid > 0 ? rqid + 1u : rqid + SSH_NUM_EVENTS + 1u;
  5794. +}
  5795. +
  5796. +static inline u16 ssh_event_to_rqid(u16 event)
  5797. +{
  5798. + return event + 1u;
  5799. +}
  5800. +
  5801. +static inline u16 ssh_rqid_to_event(u16 rqid)
  5802. +{
  5803. + return rqid - 1u;
  5804. +}
  5805. +
  5806. +static inline bool ssh_rqid_is_event(u16 rqid)
  5807. +{
  5808. + return ssh_rqid_to_event(rqid) < SSH_NUM_EVENTS;
  5809. +}
  5810. +
  5811. +static inline int ssh_tc_to_rqid(u8 tc)
  5812. +{
  5813. + return tc;
  5814. +}
  5815. +
  5816. +static inline int ssh_tc_to_event(u8 tc)
  5817. +{
  5818. + return ssh_rqid_to_event(ssh_tc_to_rqid(tc));
  5819. +}
  5820. +
  5821. +static inline u8 ssh_channel_to_index(u8 channel)
  5822. +{
  5823. + return channel - 1u;
  5824. +}
  5825. +
  5826. +static inline bool ssh_channel_is_valid(u8 channel)
  5827. +{
  5828. + return ssh_channel_to_index(channel) < SSH_NUM_CHANNELS;
  5829. +}
  5830. +
  5831. +
  5832. +/* -- Safe counters. -------------------------------------------------------- */
  5833. +
  5834. +struct ssh_seq_counter {
  5835. + u8 value;
  5836. +};
  5837. +
  5838. +struct ssh_rqid_counter {
  5839. + u16 value;
  5840. +};
  5841. +
  5842. +static inline void ssh_seq_reset(struct ssh_seq_counter *c)
  5843. +{
  5844. + WRITE_ONCE(c->value, 0);
  5845. +}
  5846. +
  5847. +static inline u8 ssh_seq_next(struct ssh_seq_counter *c)
  5848. +{
  5849. + u8 old = READ_ONCE(c->value);
  5850. + u8 new = old + 1;
  5851. + u8 ret;
  5852. +
  5853. + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
  5854. + old = ret;
  5855. + new = old + 1;
  5856. + }
  5857. +
  5858. + return old;
  5859. +}
  5860. +
  5861. +static inline void ssh_rqid_reset(struct ssh_rqid_counter *c)
  5862. +{
  5863. + WRITE_ONCE(c->value, 0);
  5864. +}
  5865. +
  5866. +static inline u16 ssh_rqid_next(struct ssh_rqid_counter *c)
  5867. +{
  5868. + u16 old = READ_ONCE(c->value);
  5869. + u16 new = ssh_rqid_next_valid(old);
  5870. + u16 ret;
  5871. +
  5872. + while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
  5873. + old = ret;
  5874. + new = ssh_rqid_next_valid(old);
  5875. + }
  5876. +
  5877. + return old;
  5878. +}
  5879. +
  5880. +
  5881. +/* -- Builder functions for SAM-over-SSH messages. -------------------------- */
  5882. +
  5883. +struct msgbuf {
  5884. + u8 *begin;
  5885. + u8 *end;
  5886. + u8 *ptr;
  5887. +};
  5888. +
  5889. +static inline void msgb_init(struct msgbuf *msgb, u8 *ptr, size_t cap)
  5890. +{
  5891. + msgb->begin = ptr;
  5892. + msgb->end = ptr + cap;
  5893. + msgb->ptr = ptr;
  5894. +}
  5895. +
  5896. +static inline int msgb_alloc(struct msgbuf *msgb, size_t cap, gfp_t flags)
  5897. +{
  5898. + u8 *buf;
  5899. +
  5900. + buf = kzalloc(cap, flags);
  5901. + if (!buf)
  5902. + return -ENOMEM;
  5903. +
  5904. + msgb_init(msgb, buf, cap);
  5905. + return 0;
  5906. +}
  5907. +
  5908. +static inline void msgb_free(struct msgbuf *msgb)
  5909. +{
  5910. + kfree(msgb->begin);
  5911. + msgb->begin = NULL;
  5912. + msgb->end = NULL;
  5913. + msgb->ptr = NULL;
  5914. +}
  5915. +
  5916. +static inline void msgb_reset(struct msgbuf *msgb)
  5917. +{
  5918. + msgb->ptr = msgb->begin;
  5919. +}
  5920. +
  5921. +static inline size_t msgb_bytes_used(const struct msgbuf *msgb)
  5922. +{
  5923. + return msgb->ptr - msgb->begin;
  5924. +}
  5925. +
  5926. +static inline void msgb_push_u16(struct msgbuf *msgb, u16 value)
  5927. +{
  5928. + if (WARN_ON(msgb->ptr + sizeof(u16) > msgb->end))
  5929. + return;
  5930. +
  5931. + put_unaligned_le16(value, msgb->ptr);
  5932. + msgb->ptr += sizeof(u16);
  5933. +}
  5934. +
  5935. +static inline void msgb_push_syn(struct msgbuf *msgb)
  5936. +{
  5937. + msgb_push_u16(msgb, SSH_MSG_SYN);
  5938. +}
  5939. +
  5940. +static inline void msgb_push_buf(struct msgbuf *msgb, const u8 *buf, size_t len)
  5941. +{
  5942. + msgb->ptr = memcpy(msgb->ptr, buf, len) + len;
  5943. +}
  5944. +
  5945. +static inline void msgb_push_crc(struct msgbuf *msgb, const u8 *buf, size_t len)
  5946. +{
  5947. + msgb_push_u16(msgb, ssh_crc(buf, len));
  5948. +}
  5949. +
  5950. +static inline void msgb_push_frame(struct msgbuf *msgb, u8 ty, u16 len, u8 seq)
  5951. +{
  5952. + struct ssh_frame *frame = (struct ssh_frame *)msgb->ptr;
  5953. + const u8 *const begin = msgb->ptr;
  5954. +
  5955. + if (WARN_ON(msgb->ptr + sizeof(*frame) > msgb->end))
  5956. + return;
  5957. +
  5958. + frame->type = ty;
  5959. + put_unaligned_le16(len, &frame->len);
  5960. + frame->seq = seq;
  5961. +
  5962. + msgb->ptr += sizeof(*frame);
  5963. + msgb_push_crc(msgb, begin, msgb->ptr - begin);
  5964. +}
  5965. +
  5966. +static inline void msgb_push_ack(struct msgbuf *msgb, u8 seq)
  5967. +{
  5968. + // SYN
  5969. + msgb_push_syn(msgb);
  5970. +
  5971. + // ACK-type frame + CRC
  5972. + msgb_push_frame(msgb, SSH_FRAME_TYPE_ACK, 0x00, seq);
  5973. +
  5974. + // payload CRC (ACK-type frames do not have a payload)
  5975. + msgb_push_crc(msgb, msgb->ptr, 0);
  5976. +}
  5977. +
  5978. +static inline void msgb_push_nak(struct msgbuf *msgb)
  5979. +{
  5980. + // SYN
  5981. + msgb_push_syn(msgb);
  5982. +
  5983. + // NAK-type frame + CRC
  5984. + msgb_push_frame(msgb, SSH_FRAME_TYPE_NAK, 0x00, 0x00);
  5985. +
  5986. + // payload CRC (ACK-type frames do not have a payload)
  5987. + msgb_push_crc(msgb, msgb->ptr, 0);
  5988. +}
  5989. +
  5990. +static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq, u16 rqid,
  5991. + const struct ssam_request *rqst)
  5992. +{
  5993. + struct ssh_command *cmd;
  5994. + const u8 *cmd_begin;
  5995. + const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
  5996. +
  5997. + // SYN
  5998. + msgb_push_syn(msgb);
  5999. +
  6000. + // command frame + crc
  6001. + msgb_push_frame(msgb, type, sizeof(*cmd) + rqst->length, seq);
  6002. +
  6003. + // frame payload: command struct + payload
  6004. + if (WARN_ON(msgb->ptr + sizeof(*cmd) > msgb->end))
  6005. + return;
  6006. +
  6007. + cmd_begin = msgb->ptr;
  6008. + cmd = (struct ssh_command *)msgb->ptr;
  6009. +
  6010. + cmd->type = SSH_PLD_TYPE_CMD;
  6011. + cmd->tc = rqst->target_category;
  6012. + cmd->chn_out = rqst->channel;
  6013. + cmd->chn_in = 0x00;
  6014. + cmd->iid = rqst->instance_id;
  6015. + put_unaligned_le16(rqid, &cmd->rqid);
  6016. + cmd->cid = rqst->command_id;
  6017. +
  6018. + msgb->ptr += sizeof(*cmd);
  6019. +
  6020. + // command payload
  6021. + msgb_push_buf(msgb, rqst->payload, rqst->length);
  6022. +
  6023. + // crc for command struct + payload
  6024. + msgb_push_crc(msgb, cmd_begin, msgb->ptr - cmd_begin);
  6025. +}
  6026. +
  6027. +
  6028. +/* -- Parser functions and utilities for SAM-over-SSH messages. ------------- */
  6029. +
  6030. +struct sshp_buf {
  6031. + u8 *ptr;
  6032. + size_t len;
  6033. + size_t cap;
  6034. +};
  6035. +
  6036. +
  6037. +static inline bool sshp_validate_crc(const struct ssam_span *src, const u8 *crc)
  6038. +{
  6039. + u16 actual = ssh_crc(src->ptr, src->len);
  6040. + u16 expected = get_unaligned_le16(crc);
  6041. +
  6042. + return actual == expected;
  6043. +}
  6044. +
  6045. +static bool sshp_find_syn(const struct ssam_span *src, struct ssam_span *rem)
  6046. +{
  6047. + size_t i;
  6048. +
  6049. + for (i = 0; i < src->len - 1; i++) {
  6050. + if (likely(get_unaligned_le16(src->ptr + i) == SSH_MSG_SYN)) {
  6051. + rem->ptr = src->ptr + i;
  6052. + rem->len = src->len - i;
  6053. + return true;
  6054. + }
  6055. + }
  6056. +
  6057. + if (unlikely(src->ptr[src->len - 1] == (SSH_MSG_SYN & 0xff))) {
  6058. + rem->ptr = src->ptr + src->len - 1;
  6059. + rem->len = 1;
  6060. + return false;
  6061. + } else {
  6062. + rem->ptr = src->ptr + src->len;
  6063. + rem->len = 0;
  6064. + return false;
  6065. + }
  6066. +}
  6067. +
  6068. +static bool sshp_starts_with_syn(const struct ssam_span *src)
  6069. +{
  6070. + return src->len >= 2 && get_unaligned_le16(src->ptr) == SSH_MSG_SYN;
  6071. +}
  6072. +
  6073. +static int sshp_parse_frame(const struct device *dev,
  6074. + const struct ssam_span *source,
  6075. + struct ssh_frame **frame,
  6076. + struct ssam_span *payload,
  6077. + size_t maxlen)
  6078. +{
  6079. + struct ssam_span sf;
  6080. + struct ssam_span sp;
  6081. +
  6082. + // initialize output
  6083. + *frame = NULL;
  6084. + payload->ptr = NULL;
  6085. + payload->len = 0;
  6086. +
  6087. + if (!sshp_starts_with_syn(source)) {
  6088. + dev_warn(dev, "rx: parser: invalid start of frame\n");
  6089. + return -ENOMSG;
  6090. + }
  6091. +
  6092. + // check for minumum packet length
  6093. + if (unlikely(source->len < SSH_MESSAGE_LENGTH(0))) {
  6094. + dev_dbg(dev, "rx: parser: not enough data for frame\n");
  6095. + return 0;
  6096. + }
  6097. +
  6098. + // pin down frame
  6099. + sf.ptr = source->ptr + sizeof(u16);
  6100. + sf.len = sizeof(struct ssh_frame);
  6101. +
  6102. + // validate frame CRC
  6103. + if (unlikely(!sshp_validate_crc(&sf, sf.ptr + sf.len))) {
  6104. + dev_warn(dev, "rx: parser: invalid frame CRC\n");
  6105. + return -EBADMSG;
  6106. + }
  6107. +
  6108. + // ensure packet does not exceed maximum length
  6109. + if (unlikely(((struct ssh_frame *)sf.ptr)->len > maxlen)) {
  6110. + dev_warn(dev, "rx: parser: frame too large: %u bytes\n",
  6111. + ((struct ssh_frame *)sf.ptr)->len);
  6112. + return -EMSGSIZE;
  6113. + }
  6114. +
  6115. + // pin down payload
  6116. + sp.ptr = sf.ptr + sf.len + sizeof(u16);
  6117. + sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
  6118. +
  6119. + // check for frame + payload length
  6120. + if (source->len < SSH_MESSAGE_LENGTH(sp.len)) {
  6121. + dev_dbg(dev, "rx: parser: not enough data for payload\n");
  6122. + return 0;
  6123. + }
  6124. +
  6125. + // validate payload crc
  6126. + if (unlikely(!sshp_validate_crc(&sp, sp.ptr + sp.len))) {
  6127. + dev_warn(dev, "rx: parser: invalid payload CRC\n");
  6128. + return -EBADMSG;
  6129. + }
  6130. +
  6131. + *frame = (struct ssh_frame *)sf.ptr;
  6132. + *payload = sp;
  6133. +
  6134. + dev_dbg(dev, "rx: parser: valid frame found (type: 0x%02x, len: %u)\n",
  6135. + (*frame)->type, (*frame)->len);
  6136. +
  6137. + return 0;
  6138. +}
  6139. +
  6140. +static int sshp_parse_command(const struct device *dev,
  6141. + const struct ssam_span *source,
  6142. + struct ssh_command **command,
  6143. + struct ssam_span *command_data)
  6144. +{
  6145. + // check for minimum length
  6146. + if (unlikely(source->len < sizeof(struct ssh_command))) {
  6147. + *command = NULL;
  6148. + command_data->ptr = NULL;
  6149. + command_data->len = 0;
  6150. +
  6151. + dev_err(dev, "rx: parser: command payload is too short\n");
  6152. + return -ENOMSG;
  6153. + }
  6154. +
  6155. + *command = (struct ssh_command *)source->ptr;
  6156. + command_data->ptr = source->ptr + sizeof(struct ssh_command);
  6157. + command_data->len = source->len - sizeof(struct ssh_command);
  6158. +
  6159. + dev_dbg(dev, "rx: parser: valid command found (tc: 0x%02x,"
  6160. + " cid: 0x%02x)\n", (*command)->tc, (*command)->cid);
  6161. +
  6162. + return 0;
  6163. +}
  6164. +
  6165. +
  6166. +static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
  6167. +{
  6168. + buf->ptr = ptr;
  6169. + buf->len = 0;
  6170. + buf->cap = cap;
  6171. +}
  6172. +
  6173. +static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
  6174. +{
  6175. + u8 *ptr;
  6176. +
  6177. + ptr = kzalloc(cap, flags);
  6178. + if (!ptr)
  6179. + return -ENOMEM;
  6180. +
  6181. + sshp_buf_init(buf, ptr, cap);
  6182. + return 0;
  6183. +
  6184. +}
  6185. +
  6186. +static inline void sshp_buf_free(struct sshp_buf *buf)
  6187. +{
  6188. + kfree(buf->ptr);
  6189. + buf->ptr = NULL;
  6190. + buf->len = 0;
  6191. + buf->cap = 0;
  6192. +}
  6193. +
  6194. +static inline void sshp_buf_reset(struct sshp_buf *buf)
  6195. +{
  6196. + buf->len = 0;
  6197. +}
  6198. +
  6199. +static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
  6200. +{
  6201. + memmove(buf->ptr, buf->ptr + n, buf->len - n);
  6202. + buf->len -= n;
  6203. +}
  6204. +
  6205. +static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
  6206. + struct kfifo *fifo)
  6207. +{
  6208. + size_t n;
  6209. +
  6210. + n = kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
  6211. + buf->len += n;
  6212. +
  6213. + return n;
  6214. +}
  6215. +
  6216. +static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
  6217. + struct ssam_span *span)
  6218. +{
  6219. + span->ptr = buf->ptr + offset;
  6220. + span->len = buf->len - offset;
  6221. +}
  6222. +
  6223. +
  6224. +/* -- Packet transport layer (ptl). ----------------------------------------- */
  6225. +/*
  6226. + * To simplify reasoning about the code below, we define a few concepts. The
  6227. + * system below is similar to a state-machine for packets, however, there are
  6228. + * too many states to explicitly write them down. To (somewhat) manage the
  6229. + * states and packets we rely on flags, reference counting, and some simple
  6230. + * concepts. State transitions are triggered by actions.
  6231. + *
  6232. + * >> Actions <<
  6233. + *
  6234. + * - submit
  6235. + * - transmission start (process next item in queue)
  6236. + * - transmission finished (guaranteed to never be parallel to transmission
  6237. + * start)
  6238. + * - ACK received
  6239. + * - NAK received (this is equivalent to issuing re-submit for all pending
  6240. + * packets)
  6241. + * - timeout (this is equivalent to re-issuing a submit or canceling)
  6242. + * - cancel (non-pending and pending)
  6243. + *
  6244. + * >> Data Structures, Packet Ownership, General Overview <<
  6245. + *
  6246. + * The code below employs two main data structures: The packet queue, containing
  6247. + * all packets scheduled for transmission, and the set of pending packets,
  6248. + * containing all packets awaiting an ACK.
  6249. + *
  6250. + * Shared ownership of a packet is controlled via reference counting. Inside the
  6251. + * transmission system are a total of five packet owners:
  6252. + *
  6253. + * - the packet queue,
  6254. + * - the pending set,
  6255. + * - the transmitter thread,
  6256. + * - the receiver thread (via ACKing), and
  6257. + * - the timeout work item.
  6258. + *
  6259. + * Normal operation is as follows: The initial reference of the packet is
  6260. + * obtained by submitting the packet and queueing it. The receiver thread
  6261. + * takes packets from the queue. By doing this, it does not increment the
  6262. + * refcount but takes over the reference (removing it from the queue).
  6263. + * If the packet is sequenced (i.e. needs to be ACKed by the client), the
  6264. + * transmitter thread sets-up the timeout and adds the packet to the pending set
  6265. + * before starting to transmit it. As the timeout is handled by a reaper task,
  6266. + * no additional reference for it is needed. After the transmit is done, the
  6267. + * reference hold by the transmitter thread is dropped. If the packet is
  6268. + * unsequenced (i.e. does not need an ACK), the packet is completed by the
  6269. + * transmitter thread before dropping that reference.
  6270. + *
  6271. + * On receial of an ACK, the receiver thread removes and obtains the refernce to
  6272. + * the packet from the pending set. On succes, the receiver thread will then
  6273. + * complete the packet and drop its reference.
  6274. + *
  6275. + * On error, the completion callback is immediately run by on thread on which
  6276. + * the error was detected.
  6277. + *
  6278. + * To ensure that a packet eventually leaves the system it is marked as "locked"
  6279. + * directly before it is going to be completed or when it is canceled. Marking a
  6280. + * packet as "locked" has the effect that passing and creating new references
  6281. + * of the packet will be blocked. This means that the packet cannot be added
  6282. + * to the queue, the pending set, and the timeout, or be picked up by the
  6283. + * transmitter thread or receiver thread. To remove a packet from the system it
  6284. + * has to be marked as locked and subsequently all references from the data
  6285. + * structures (queue, pending) have to be removed. References held by threads
  6286. + * will eventually be dropped automatically as their execution progresses.
  6287. + *
  6288. + * Note that the packet completion callback is, in case of success and for a
  6289. + * sequenced packet, guaranteed to run on the receiver thread, thus providing a
  6290. + * way to reliably identify responses to the packet. The packet completion
  6291. + * callback is only run once and it does not indicate that the packet has fully
  6292. + * left the system. In case of re-submission (and with somewhat unlikely
  6293. + * timing), it may be possible that the packet is being re-transmitted while the
  6294. + * completion callback runs. Completion will occur both on success and internal
  6295. + * error, as well as when the packet is canceled.
  6296. + *
  6297. + * >> Flags <<
  6298. + *
  6299. + * Flags are used to indicate the state and progression of a packet. Some flags
  6300. + * have stricter guarantees than other:
  6301. + *
  6302. + * - locked
  6303. + * Indicates if the packet is locked. If the packet is locked, passing and/or
  6304. + * creating additional references to the packet is forbidden. The packet thus
  6305. + * may not be queued, dequeued, or removed or added to the pending set. Note
  6306. + * that the packet state flags may still change (e.g. it may be marked as
  6307. + * ACKed, transmitted, ...).
  6308. + *
  6309. + * - completed
  6310. + * Indicates if the packet completion has been run or is about to be run. This
  6311. + * flag is used to ensure that the packet completion callback is only run
  6312. + * once.
  6313. + *
  6314. + * - queued
  6315. + * Indicates if a packet is present in the submission queue or not. This flag
  6316. + * must only be modified with the queue lock held, and must be coherent
  6317. + * presence of the packet in the queue.
  6318. + *
  6319. + * - pending
  6320. + * Indicates if a packet is present in the set of pending packets or not.
  6321. + * This flag must only be modified with the pending lock held, and must be
  6322. + * coherent presence of the packet in the pending set.
  6323. + *
  6324. + * - transmitting
  6325. + * Indicates if the packet is currently transmitting. In case of
  6326. + * re-transmissions, it is only safe to wait on the "transmitted" completion
  6327. + * after this flag has been set. The completion will be set both in success
  6328. + * and error case.
  6329. + *
  6330. + * - transmitted
  6331. + * Indicates if the packet has been transmitted. This flag is not cleared by
  6332. + * the system, thus it indicates the first transmission only.
  6333. + *
  6334. + * - acked
  6335. + * Indicates if the packet has been acknowledged by the client. There are no
  6336. + * other guarantees given. For example, the packet may still be canceled
  6337. + * and/or the completion may be triggered an error even though this bit is
  6338. + * set. Rely on the status provided by completion instead.
  6339. + *
  6340. + * - canceled
  6341. + * Indicates if the packet has been canceled from the outside. There are no
  6342. + * other guarantees given. Specifically, the packet may be completed by
  6343. + * another part of the system before the cancellation attempts to complete it.
  6344. + *
  6345. + * >> General Notes <<
  6346. + *
  6347. + * To avoid deadlocks, if both queue and pending locks are required, the pending
  6348. + * lock must be acquired before the queue lock.
  6349. + */
  6350. +
  6351. +/**
  6352. + * Maximum number transmission attempts per sequenced packet in case of
  6353. + * time-outs. Must be smaller than 16.
  6354. + */
  6355. +#define SSH_PTL_MAX_PACKET_TRIES 3
  6356. +
  6357. +/**
  6358. + * Timeout as ktime_t delta for ACKs. If we have not received an ACK in this
  6359. + * time-frame after starting transmission, the packet will be re-submitted.
  6360. + */
  6361. +#define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
  6362. +
  6363. +/**
  6364. + * Maximum time resolution for timeouts. Currently set to max(2 jiffies, 50ms).
  6365. + * Should be larger than one jiffy to avoid direct re-scheduling of reaper
  6366. + * work_struct.
  6367. + */
  6368. +#define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
  6369. +
  6370. +/**
  6371. + * Maximum number of sequenced packets concurrently waiting for an ACK.
  6372. + * Packets marked as blocking will not be transmitted while this limit is
  6373. + * reached.
  6374. + */
  6375. +#define SSH_PTL_MAX_PENDING 1
  6376. +
  6377. +#define SSH_PTL_RX_BUF_LEN 4096
  6378. +
  6379. +#define SSH_PTL_RX_FIFO_LEN 4096
  6380. +
  6381. +
  6382. +enum ssh_ptl_state_flags {
  6383. + SSH_PTL_SF_SHUTDOWN_BIT,
  6384. +};
  6385. +
  6386. +struct ssh_ptl_ops {
  6387. + void (*data_received)(struct ssh_ptl *p, const struct ssam_span *data);
  6388. +};
  6389. +
  6390. +struct ssh_ptl {
  6391. + struct serdev_device *serdev;
  6392. + unsigned long state;
  6393. +
  6394. + struct {
  6395. + spinlock_t lock;
  6396. + struct list_head head;
  6397. + } queue;
  6398. +
  6399. + struct {
  6400. + spinlock_t lock;
  6401. + struct list_head head;
  6402. + atomic_t count;
  6403. + } pending;
  6404. +
  6405. + struct {
  6406. + bool thread_signal;
  6407. + struct task_struct *thread;
  6408. + struct wait_queue_head thread_wq;
  6409. + struct wait_queue_head packet_wq;
  6410. + struct ssh_packet *packet;
  6411. + size_t offset;
  6412. + } tx;
  6413. +
  6414. + struct {
  6415. + struct task_struct *thread;
  6416. + struct wait_queue_head wq;
  6417. + struct kfifo fifo;
  6418. + struct sshp_buf buf;
  6419. +
  6420. + struct {
  6421. + u16 seqs[8];
  6422. + u16 offset;
  6423. + } blocked;
  6424. + } rx;
  6425. +
  6426. + struct {
  6427. + ktime_t timeout;
  6428. + ktime_t expires;
  6429. + struct delayed_work reaper;
  6430. + } rtx_timeout;
  6431. +
  6432. + struct ssh_ptl_ops ops;
  6433. +};
  6434. +
  6435. +
  6436. +#define __ssam_prcond(func, p, fmt, ...) \
  6437. + do { \
  6438. + if ((p)) \
  6439. + func((p), fmt, ##__VA_ARGS__); \
  6440. + } while (0);
  6441. +
  6442. +#define ptl_dbg(p, fmt, ...) dev_dbg(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  6443. +#define ptl_info(p, fmt, ...) dev_info(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  6444. +#define ptl_warn(p, fmt, ...) dev_warn(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  6445. +#define ptl_err(p, fmt, ...) dev_err(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
  6446. +#define ptl_dbg_cond(p, fmt, ...) __ssam_prcond(ptl_dbg, p, fmt, ##__VA_ARGS__)
  6447. +
  6448. +#define to_ssh_packet(ptr, member) \
  6449. + container_of(ptr, struct ssh_packet, member)
  6450. +
  6451. +#define to_ssh_ptl(ptr, member) \
  6452. + container_of(ptr, struct ssh_ptl, member)
  6453. +
  6454. +
  6455. +#ifdef CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION
  6456. +
  6457. +/**
  6458. + * ssh_ptl_should_drop_ack_packet - error injection hook to drop ACK packets
  6459. + *
  6460. + * Useful to test detection and handling of automated re-transmits by the EC.
  6461. + * Specifically of packets that the EC consideres not-ACKed but the driver
  6462. + * already consideres ACKed (due to dropped ACK). In this case, the EC
  6463. + * re-transmits the packet-to-be-ACKed and the driver should detect it as
  6464. + * duplicate/already handled. Note that the driver should still send an ACK
  6465. + * for the re-transmitted packet.
  6466. + */
  6467. +static noinline bool ssh_ptl_should_drop_ack_packet(void)
  6468. +{
  6469. + return false;
  6470. +}
  6471. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
  6472. +
  6473. +/**
  6474. + * ssh_ptl_should_drop_nak_packet - error injection hook to drop NAK packets
  6475. + *
  6476. + * Useful to test/force automated (timeout-based) re-transmit by the EC.
  6477. + * Specifically, packets that have not reached the driver completely/with valid
  6478. + * checksums. Only useful in combination with receival of (injected) bad data.
  6479. + */
  6480. +static noinline bool ssh_ptl_should_drop_nak_packet(void)
  6481. +{
  6482. + return false;
  6483. +}
  6484. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
  6485. +
  6486. +/**
  6487. + * ssh_ptl_should_drop_dsq_packet - error injection hook to drop sequenced data
  6488. + * packet
  6489. + *
  6490. + * Useful to test re-transmit timeout of the driver. If the data packet has not
  6491. + * been ACKed after a certain time, the driver should re-transmit the packet up
  6492. + * to limited number of times defined in SSH_PTL_MAX_PACKET_TRIES.
  6493. + */
  6494. +static noinline bool ssh_ptl_should_drop_dsq_packet(void)
  6495. +{
  6496. + return false;
  6497. +}
  6498. +ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
  6499. +
  6500. +/**
  6501. + * ssh_ptl_should_fail_write - error injection hook to make serdev_device_write
  6502. + * fail
  6503. + *
  6504. + * Hook to simulate errors in serdev_device_write when transmitting packets.
  6505. + */
  6506. +static noinline int ssh_ptl_should_fail_write(void)
  6507. +{
  6508. + return 0;
  6509. +}
  6510. +ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
  6511. +
  6512. +/**
  6513. + * ssh_ptl_should_corrupt_tx_data - error injection hook to simualte invalid
  6514. + * data being sent to the EC
  6515. + *
  6516. + * Hook to simulate corrupt/invalid data being sent from host (driver) to EC.
  6517. + * Causes the packet data to be actively corrupted by overwriting it with
  6518. + * pre-defined values, such that it becomes invalid, causing the EC to respond
  6519. + * with a NAK packet. Useful to test handling of NAK packets received by the
  6520. + * driver.
  6521. + */
  6522. +static noinline bool ssh_ptl_should_corrupt_tx_data(void)
  6523. +{
  6524. + return false;
  6525. +}
  6526. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
  6527. +
  6528. +/**
  6529. + * ssh_ptl_should_corrupt_rx_syn - error injection hook to simulate invalid
  6530. + * data being sent by the EC
  6531. + *
  6532. + * Hook to simulate invalid SYN bytes, i.e. an invalid start of messages and
  6533. + * test handling thereof in the driver.
  6534. + */
  6535. +static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
  6536. +{
  6537. + return false;
  6538. +}
  6539. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
  6540. +
  6541. +/**
  6542. + * ssh_ptl_should_corrupt_rx_data - error injection hook to simulate invalid
  6543. + * data being sent by the EC
  6544. + *
  6545. + * Hook to simulate invalid data/checksum of the message frame and test handling
  6546. + * thereof in the driver.
  6547. + */
  6548. +static noinline bool ssh_ptl_should_corrupt_rx_data(void)
  6549. +{
  6550. + return false;
  6551. +}
  6552. +ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
  6553. +
  6554. +
  6555. +static inline bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
  6556. +{
  6557. + if (likely(!ssh_ptl_should_drop_ack_packet()))
  6558. + return false;
  6559. +
  6560. + trace_ssam_ei_tx_drop_ack_packet(packet);
  6561. + ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
  6562. + packet);
  6563. +
  6564. + return true;
  6565. +}
  6566. +
  6567. +static inline bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
  6568. +{
  6569. + if (likely(!ssh_ptl_should_drop_nak_packet()))
  6570. + return false;
  6571. +
  6572. + trace_ssam_ei_tx_drop_nak_packet(packet);
  6573. + ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
  6574. + packet);
  6575. +
  6576. + return true;
  6577. +}
  6578. +
  6579. +static inline bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
  6580. +{
  6581. + if (likely(!ssh_ptl_should_drop_dsq_packet()))
  6582. + return false;
  6583. +
  6584. + trace_ssam_ei_tx_drop_dsq_packet(packet);
  6585. + ptl_info(packet->ptl,
  6586. + "packet error injection: dropping sequenced data packet %p\n",
  6587. + packet);
  6588. +
  6589. + return true;
  6590. +}
  6591. +
  6592. +static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
  6593. +{
  6594. + // ignore packets that don't carry any data (i.e. flush)
  6595. + if (!packet->data.ptr || !packet->data.len)
  6596. + return false;
  6597. +
  6598. + switch (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)]) {
  6599. + case SSH_FRAME_TYPE_ACK:
  6600. + return __ssh_ptl_should_drop_ack_packet(packet);
  6601. +
  6602. + case SSH_FRAME_TYPE_NAK:
  6603. + return __ssh_ptl_should_drop_nak_packet(packet);
  6604. +
  6605. + case SSH_FRAME_TYPE_DATA_SEQ:
  6606. + return __ssh_ptl_should_drop_dsq_packet(packet);
  6607. +
  6608. + default:
  6609. + return false;
  6610. + }
  6611. +}
  6612. +
  6613. +static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
  6614. + struct ssh_packet *packet,
  6615. + const unsigned char *buf,
  6616. + size_t count)
  6617. +{
  6618. + int status;
  6619. +
  6620. + status = ssh_ptl_should_fail_write();
  6621. + if (unlikely(status)) {
  6622. + trace_ssam_ei_tx_fail_write(packet, status);
  6623. + ptl_info(packet->ptl,
  6624. + "packet error injection: simulating transmit error %d, packet %p\n",
  6625. + status, packet);
  6626. +
  6627. + return status;
  6628. + }
  6629. +
  6630. + return serdev_device_write_buf(ptl->serdev, buf, count);
  6631. +}
  6632. +
  6633. +static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
  6634. +{
  6635. + // ignore packets that don't carry any data (i.e. flush)
  6636. + if (!packet->data.ptr || !packet->data.len)
  6637. + return;
  6638. +
  6639. + // only allow sequenced data packets to be modified
  6640. + if (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
  6641. + return;
  6642. +
  6643. + if (likely(!ssh_ptl_should_corrupt_tx_data()))
  6644. + return;
  6645. +
  6646. + trace_ssam_ei_tx_corrupt_data(packet);
  6647. + ptl_info(packet->ptl,
  6648. + "packet error injection: simulating invalid transmit data on packet %p\n",
  6649. + packet);
  6650. +
  6651. + /*
  6652. + * NB: The value 0xb3 has been chosen more or less randomly so that it
  6653. + * doesn't have any (major) overlap with the SYN bytes (aa 55) and is
  6654. + * non-trivial (i.e. non-zero, non-0xff).
  6655. + */
  6656. + memset(packet->data.ptr, 0xb3, packet->data.len);
  6657. +}
  6658. +
  6659. +static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
  6660. + struct ssam_span *data)
  6661. +{
  6662. + struct ssam_span frame;
  6663. +
  6664. + // check if there actually is something to corrupt
  6665. + if (!sshp_find_syn(data, &frame))
  6666. + return;
  6667. +
  6668. + if (likely(!ssh_ptl_should_corrupt_rx_syn()))
  6669. + return;
  6670. +
  6671. + trace_ssam_ei_rx_corrupt_syn("data_length", data->len);
  6672. +
  6673. + data->ptr[1] = 0xb3; // set second byte of SYN to "random" value
  6674. +}
  6675. +
  6676. +static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
  6677. + struct ssam_span *frame)
  6678. +{
  6679. + size_t payload_len, message_len;
  6680. + struct ssh_frame *sshf;
  6681. +
  6682. + // ignore incomplete messages, will get handled once it's complete
  6683. + if (frame->len < SSH_MESSAGE_LENGTH(0))
  6684. + return;
  6685. +
  6686. + // ignore incomplete messages, part 2
  6687. + payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
  6688. + message_len = SSH_MESSAGE_LENGTH(payload_len);
  6689. + if (frame->len < message_len)
  6690. + return;
  6691. +
  6692. + if (likely(!ssh_ptl_should_corrupt_rx_data()))
  6693. + return;
  6694. +
  6695. + sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
  6696. + trace_ssam_ei_rx_corrupt_data(sshf);
  6697. +
  6698. + /*
  6699. + * Flip bits in first byte of payload checksum. This is basically
  6700. + * equivalent to a payload/frame data error without us having to worry
  6701. + * about (the, arguably pretty small, probability of) accidental
  6702. + * checksum collisions.
  6703. + */
  6704. + frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
  6705. +}
  6706. +
  6707. +#else /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
  6708. +
  6709. +static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
  6710. +{
  6711. + return false;
  6712. +}
  6713. +
  6714. +static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
  6715. + struct ssh_packet *packet,
  6716. + const unsigned char *buf,
  6717. + size_t count)
  6718. +{
  6719. + return serdev_device_write_buf(ptl->serdev, buf, count);
  6720. +}
  6721. +
  6722. +static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
  6723. +{
  6724. +}
  6725. +
  6726. +static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
  6727. + struct ssam_span *data)
  6728. +{
  6729. +}
  6730. +
  6731. +static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
  6732. + struct ssam_span *frame)
  6733. +{
  6734. +}
  6735. +
  6736. +#endif /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
  6737. +
  6738. +
  6739. +static void __ssh_ptl_packet_release(struct kref *kref)
  6740. +{
  6741. + struct ssh_packet *p = to_ssh_packet(kref, refcnt);
  6742. +
  6743. + trace_ssam_packet_release(p);
  6744. +
  6745. + ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
  6746. + p->ops->release(p);
  6747. +}
  6748. +
  6749. +void ssh_packet_get(struct ssh_packet *packet)
  6750. +{
  6751. + kref_get(&packet->refcnt);
  6752. +}
  6753. +EXPORT_SYMBOL_GPL(ssh_packet_get);
  6754. +
  6755. +void ssh_packet_put(struct ssh_packet *packet)
  6756. +{
  6757. + kref_put(&packet->refcnt, __ssh_ptl_packet_release);
  6758. +}
  6759. +EXPORT_SYMBOL_GPL(ssh_packet_put);
  6760. +
  6761. +static inline u8 ssh_packet_get_seq(struct ssh_packet *packet)
  6762. +{
  6763. + return packet->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
  6764. +}
  6765. +
  6766. +
  6767. +struct ssh_packet_args {
  6768. + unsigned long type;
  6769. + u8 priority;
  6770. + const struct ssh_packet_ops *ops;
  6771. +};
  6772. +
  6773. +static void ssh_packet_init(struct ssh_packet *packet,
  6774. + const struct ssh_packet_args *args)
  6775. +{
  6776. + kref_init(&packet->refcnt);
  6777. +
  6778. + packet->ptl = NULL;
  6779. + INIT_LIST_HEAD(&packet->queue_node);
  6780. + INIT_LIST_HEAD(&packet->pending_node);
  6781. +
  6782. + packet->state = args->type & SSH_PACKET_FLAGS_TY_MASK;
  6783. + packet->priority = args->priority;
  6784. + packet->timestamp = KTIME_MAX;
  6785. +
  6786. + packet->data.ptr = NULL;
  6787. + packet->data.len = 0;
  6788. +
  6789. + packet->ops = args->ops;
  6790. +}
  6791. +
  6792. +
  6793. +static int ptl_alloc_ctrl_packet(struct ssh_ptl *ptl,
  6794. + struct ssh_packet **packet,
  6795. + struct ssam_span *buffer, gfp_t flags)
  6796. +{
  6797. + // TODO: cache packets
  6798. + // - Potential problem with kmem_cache: Minimum alloc size of that is
  6799. + // PAGE_SIZE (???), which is somewhat overkill here.
  6800. + // - Note: Mempool always tries to allocate with alloc callback first.
  6801. + // Buffered objects are only chosen on allocation failure.
  6802. + //
  6803. + // => either kmem_cache or custom, try kmem_cache first and check via
  6804. + // /proc/slabinfo
  6805. + //
  6806. + // Note: kmem_cache_create needs unique name
  6807. +
  6808. + *packet = kzalloc(sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL, flags);
  6809. + if (!*packet)
  6810. + return -ENOMEM;
  6811. +
  6812. + buffer->ptr = (u8 *)(*packet + 1);
  6813. + buffer->len = SSH_MSG_LEN_CTRL;
  6814. +
  6815. + return 0;
  6816. +}
  6817. +
  6818. +static void ptl_free_ctrl_packet(struct ssh_packet *p)
  6819. +{
  6820. + // TODO: cache packets
  6821. +
  6822. + kfree(p);
  6823. +}
  6824. +
  6825. +static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
  6826. + .complete = NULL,
  6827. + .release = ptl_free_ctrl_packet,
  6828. +};
  6829. +
  6830. +
  6831. +static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
  6832. + ktime_t expires)
  6833. +{
  6834. + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
  6835. + ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
  6836. + ktime_t old;
  6837. +
  6838. + // re-adjust / schedule reaper if it is above resolution delta
  6839. + old = READ_ONCE(ptl->rtx_timeout.expires);
  6840. + while (ktime_before(aexp, old))
  6841. + old = cmpxchg64(&ptl->rtx_timeout.expires, old, expires);
  6842. +
  6843. + // if we updated the reaper expiration, modify work timeout
  6844. + if (old == expires)
  6845. + mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
  6846. +}
  6847. +
  6848. +static void ssh_ptl_timeout_start(struct ssh_packet *packet)
  6849. +{
  6850. + struct ssh_ptl *ptl = packet->ptl;
  6851. + ktime_t timestamp = ktime_get_coarse_boottime();
  6852. + ktime_t timeout = ptl->rtx_timeout.timeout;
  6853. +
  6854. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
  6855. + return;
  6856. +
  6857. + WRITE_ONCE(packet->timestamp, timestamp);
  6858. + smp_mb__after_atomic();
  6859. +
  6860. + ssh_ptl_timeout_reaper_mod(packet->ptl, timestamp, timestamp + timeout);
  6861. +}
  6862. +
  6863. +
  6864. +static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
  6865. +{
  6866. + struct list_head *head;
  6867. + u8 priority = READ_ONCE(p->priority);
  6868. +
  6869. + /*
  6870. + * We generally assume that there are less control (ACK/NAK) packets and
  6871. + * re-submitted data packets as there are normal data packets (at least
  6872. + * in situations in which many packets are queued; if there aren't many
  6873. + * packets queued the decision on how to iterate should be basically
  6874. + * irrellevant; the number of control/data packets is more or less
  6875. + * limited via the maximum number of pending packets). Thus, when
  6876. + * inserting a control or re-submitted data packet, (determined by their
  6877. + * priority), we search from front to back. Normal data packets are,
  6878. + * usually queued directly at the tail of the queue, so for those search
  6879. + * from back to front.
  6880. + */
  6881. +
  6882. + if (priority > SSH_PACKET_PRIORITY_DATA) {
  6883. + list_for_each(head, &p->ptl->queue.head) {
  6884. + p = list_entry(head, struct ssh_packet, queue_node);
  6885. +
  6886. + if (READ_ONCE(p->priority) < priority)
  6887. + break;
  6888. + }
  6889. + } else {
  6890. + list_for_each_prev(head, &p->ptl->queue.head) {
  6891. + p = list_entry(head, struct ssh_packet, queue_node);
  6892. +
  6893. + if (READ_ONCE(p->priority) >= priority) {
  6894. + head = head->next;
  6895. + break;
  6896. + }
  6897. + }
  6898. + }
  6899. +
  6900. +
  6901. + return head;
  6902. +}
  6903. +
  6904. +static int ssh_ptl_queue_push(struct ssh_packet *packet)
  6905. +{
  6906. + struct ssh_ptl *ptl = packet->ptl;
  6907. + struct list_head *head;
  6908. +
  6909. + spin_lock(&ptl->queue.lock);
  6910. +
  6911. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state)) {
  6912. + spin_unlock(&ptl->queue.lock);
  6913. + return -ESHUTDOWN;
  6914. + }
  6915. +
  6916. + // avoid further transitions when cancelling/completing
  6917. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state)) {
  6918. + spin_unlock(&ptl->queue.lock);
  6919. + return -EINVAL;
  6920. + }
  6921. +
  6922. + // if this packet has already been queued, do not add it
  6923. + if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
  6924. + spin_unlock(&ptl->queue.lock);
  6925. + return -EALREADY;
  6926. + }
  6927. +
  6928. + head = __ssh_ptl_queue_find_entrypoint(packet);
  6929. +
  6930. + ssh_packet_get(packet);
  6931. + list_add_tail(&packet->queue_node, &ptl->queue.head);
  6932. +
  6933. + spin_unlock(&ptl->queue.lock);
  6934. + return 0;
  6935. +}
  6936. +
  6937. +static void ssh_ptl_queue_remove(struct ssh_packet *packet)
  6938. +{
  6939. + struct ssh_ptl *ptl = packet->ptl;
  6940. + bool remove;
  6941. +
  6942. + spin_lock(&ptl->queue.lock);
  6943. +
  6944. + remove = test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state);
  6945. + if (remove)
  6946. + list_del(&packet->queue_node);
  6947. +
  6948. + spin_unlock(&ptl->queue.lock);
  6949. +
  6950. + if (remove)
  6951. + ssh_packet_put(packet);
  6952. +}
  6953. +
  6954. +
  6955. +static void ssh_ptl_pending_push(struct ssh_packet *packet)
  6956. +{
  6957. + struct ssh_ptl *ptl = packet->ptl;
  6958. +
  6959. + spin_lock(&ptl->pending.lock);
  6960. +
  6961. + // if we are cancelling/completing this packet, do not add it
  6962. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state)) {
  6963. + spin_unlock(&ptl->pending.lock);
  6964. + return;
  6965. + }
  6966. +
  6967. + // in case it is already pending (e.g. re-submission), do not add it
  6968. + if (test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
  6969. + spin_unlock(&ptl->pending.lock);
  6970. + return;
  6971. + }
  6972. +
  6973. + atomic_inc(&ptl->pending.count);
  6974. + ssh_packet_get(packet);
  6975. + list_add_tail(&packet->pending_node, &ptl->pending.head);
  6976. +
  6977. + spin_unlock(&ptl->pending.lock);
  6978. +}
  6979. +
  6980. +static void ssh_ptl_pending_remove(struct ssh_packet *packet)
  6981. +{
  6982. + struct ssh_ptl *ptl = packet->ptl;
  6983. + bool remove;
  6984. +
  6985. + spin_lock(&ptl->pending.lock);
  6986. +
  6987. + remove = test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state);
  6988. + if (remove) {
  6989. + list_del(&packet->pending_node);
  6990. + atomic_dec(&ptl->pending.count);
  6991. + }
  6992. +
  6993. + spin_unlock(&ptl->pending.lock);
  6994. +
  6995. + if (remove)
  6996. + ssh_packet_put(packet);
  6997. +}
  6998. +
  6999. +
  7000. +static void __ssh_ptl_complete(struct ssh_packet *p, int status)
  7001. +{
  7002. + struct ssh_ptl *ptl = READ_ONCE(p->ptl);
  7003. +
  7004. + trace_ssam_packet_complete(p, status);
  7005. +
  7006. + ptl_dbg_cond(ptl, "ptl: completing packet %p\n", p);
  7007. + if (status && status != -ECANCELED)
  7008. + ptl_dbg_cond(ptl, "ptl: packet error: %d\n", status);
  7009. +
  7010. + if (p->ops->complete)
  7011. + p->ops->complete(p, status);
  7012. +}
  7013. +
  7014. +static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
  7015. +{
  7016. + /*
  7017. + * A call to this function should in general be preceeded by
  7018. + * set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->flags) to avoid re-adding the
  7019. + * packet to the structures it's going to be removed from.
  7020. + *
  7021. + * The set_bit call does not need explicit memory barriers as the
  7022. + * implicit barrier of the test_and_set_bit call below ensure that the
  7023. + * flag is visible before we actually attempt to remove the packet.
  7024. + */
  7025. +
  7026. + if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  7027. + return;
  7028. +
  7029. + ssh_ptl_queue_remove(p);
  7030. + ssh_ptl_pending_remove(p);
  7031. +
  7032. + __ssh_ptl_complete(p, status);
  7033. +}
  7034. +
  7035. +
  7036. +static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
  7037. +{
  7038. + struct ssh_ptl *ptl = packet->ptl;
  7039. +
  7040. + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &packet->state))
  7041. + return !atomic_read(&ptl->pending.count);
  7042. +
  7043. + // we can alwas process non-blocking packets
  7044. + if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &packet->state))
  7045. + return true;
  7046. +
  7047. + // if we are already waiting for this packet, send it again
  7048. + if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
  7049. + return true;
  7050. +
  7051. + // otherwise: check if we have the capacity to send
  7052. + return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
  7053. +}
  7054. +
  7055. +static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
  7056. +{
  7057. + struct ssh_packet *packet = ERR_PTR(-ENOENT);
  7058. + struct ssh_packet *p, *n;
  7059. +
  7060. + spin_lock(&ptl->queue.lock);
  7061. + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
  7062. + /*
  7063. + * If we are cancelling or completing this packet, ignore it.
  7064. + * It's going to be removed from this queue shortly.
  7065. + */
  7066. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  7067. + continue;
  7068. +
  7069. + /*
  7070. + * Packets should be ordered non-blocking/to-be-resent first.
  7071. + * If we cannot process this packet, assume that we can't
  7072. + * process any following packet either and abort.
  7073. + */
  7074. + if (!ssh_ptl_tx_can_process(p)) {
  7075. + packet = ERR_PTR(-EBUSY);
  7076. + break;
  7077. + }
  7078. +
  7079. + /*
  7080. + * We are allowed to change the state now. Remove it from the
  7081. + * queue and mark it as being transmitted. Note that we cannot
  7082. + * add it to the set of pending packets yet, as queue locks must
  7083. + * always be acquired before packet locks (otherwise we might
  7084. + * run into a deadlock).
  7085. + */
  7086. +
  7087. + list_del(&p->queue_node);
  7088. +
  7089. + /*
  7090. + * Ensure that the "queued" bit gets cleared after setting the
  7091. + * "transmitting" bit to guaranteee non-zero flags.
  7092. + */
  7093. + set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
  7094. + smp_mb__before_atomic();
  7095. + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
  7096. +
  7097. + packet = p;
  7098. + break;
  7099. + }
  7100. + spin_unlock(&ptl->queue.lock);
  7101. +
  7102. + return packet;
  7103. +}
  7104. +
  7105. +static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
  7106. +{
  7107. + struct ssh_packet *p;
  7108. +
  7109. + p = ssh_ptl_tx_pop(ptl);
  7110. + if (IS_ERR(p))
  7111. + return p;
  7112. +
  7113. + if (test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state)) {
  7114. + ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
  7115. + ssh_ptl_pending_push(p);
  7116. + ssh_ptl_timeout_start(p);
  7117. + } else {
  7118. + ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
  7119. + }
  7120. +
  7121. + /*
  7122. + * Update number of tries. This directly influences the priority in case
  7123. + * the packet is re-submitted (e.g. via timeout/NAK). Note that this is
  7124. + * the only place where we update the priority in-flight. As this runs
  7125. + * only on the tx-thread, this read-modify-write procedure is safe.
  7126. + */
  7127. + WRITE_ONCE(p->priority, READ_ONCE(p->priority) + 1);
  7128. +
  7129. + return p;
  7130. +}
  7131. +
  7132. +static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
  7133. +{
  7134. + struct ssh_ptl *ptl = packet->ptl;
  7135. +
  7136. + ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
  7137. +
  7138. + /*
  7139. + * Transition to state to "transmitted". Ensure that the flags never get
  7140. + * zero with barrier.
  7141. + */
  7142. + set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
  7143. + smp_mb__before_atomic();
  7144. + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
  7145. +
  7146. + // if the packet is unsequenced, we're done: lock and complete
  7147. + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &packet->state)) {
  7148. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
  7149. + ssh_ptl_remove_and_complete(packet, 0);
  7150. + }
  7151. +
  7152. + /*
  7153. + * Notify that a packet transmission has finished. In general we're only
  7154. + * waiting for one packet (if any), so wake_up_all should be fine.
  7155. + */
  7156. + wake_up_all(&ptl->tx.packet_wq);
  7157. +}
  7158. +
  7159. +static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
  7160. +{
  7161. + /*
  7162. + * Transmission failure: Lock the packet and try to complete it. Ensure
  7163. + * that the flags never get zero with barrier.
  7164. + */
  7165. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
  7166. + smp_mb__before_atomic();
  7167. + clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
  7168. +
  7169. + ptl_err(packet->ptl, "ptl: transmission error: %d\n", status);
  7170. + ptl_dbg(packet->ptl, "ptl: failed to transmit packet: %p\n", packet);
  7171. +
  7172. + ssh_ptl_remove_and_complete(packet, status);
  7173. +
  7174. + /*
  7175. + * Notify that a packet transmission has finished. In general we're only
  7176. + * waiting for one packet (if any), so wake_up_all should be fine.
  7177. + */
  7178. + wake_up_all(&packet->ptl->tx.packet_wq);
  7179. +}
  7180. +
  7181. +static void ssh_ptl_tx_threadfn_wait(struct ssh_ptl *ptl)
  7182. +{
  7183. + wait_event_interruptible(ptl->tx.thread_wq,
  7184. + READ_ONCE(ptl->tx.thread_signal) || kthread_should_stop());
  7185. + WRITE_ONCE(ptl->tx.thread_signal, false);
  7186. +}
  7187. +
  7188. +static int ssh_ptl_tx_threadfn(void *data)
  7189. +{
  7190. + struct ssh_ptl *ptl = data;
  7191. +
  7192. + while (!kthread_should_stop()) {
  7193. + unsigned char *buf;
  7194. + bool drop = false;
  7195. + size_t len = 0;
  7196. + int status = 0;
  7197. +
  7198. + // if we don't have a packet, get the next and add it to pending
  7199. + if (IS_ERR_OR_NULL(ptl->tx.packet)) {
  7200. + ptl->tx.packet = ssh_ptl_tx_next(ptl);
  7201. + ptl->tx.offset = 0;
  7202. +
  7203. + // if no packet is available, we are done
  7204. + if (IS_ERR(ptl->tx.packet)) {
  7205. + ssh_ptl_tx_threadfn_wait(ptl);
  7206. + continue;
  7207. + }
  7208. + }
  7209. +
  7210. + // error injection: drop packet to simulate transmission problem
  7211. + if (ptl->tx.offset == 0)
  7212. + drop = ssh_ptl_should_drop_packet(ptl->tx.packet);
  7213. +
  7214. + // error injection: simulate invalid packet data
  7215. + if (ptl->tx.offset == 0 && !drop)
  7216. + ssh_ptl_tx_inject_invalid_data(ptl->tx.packet);
  7217. +
  7218. + // flush-packets don't have any data
  7219. + if (likely(ptl->tx.packet->data.ptr && !drop)) {
  7220. + buf = ptl->tx.packet->data.ptr + ptl->tx.offset;
  7221. + len = ptl->tx.packet->data.len - ptl->tx.offset;
  7222. +
  7223. + ptl_dbg(ptl, "tx: sending data (length: %zu)\n", len);
  7224. + print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
  7225. + buf, len, false);
  7226. +
  7227. + status = ssh_ptl_write_buf(ptl, ptl->tx.packet, buf, len);
  7228. + }
  7229. +
  7230. + if (status < 0) {
  7231. + // complete packet with error
  7232. + ssh_ptl_tx_compl_error(ptl->tx.packet, status);
  7233. + ssh_packet_put(ptl->tx.packet);
  7234. + ptl->tx.packet = NULL;
  7235. +
  7236. + } else if (status == len) {
  7237. + // complete packet and/or mark as transmitted
  7238. + ssh_ptl_tx_compl_success(ptl->tx.packet);
  7239. + ssh_packet_put(ptl->tx.packet);
  7240. + ptl->tx.packet = NULL;
  7241. +
  7242. + } else { // need more buffer space
  7243. + ptl->tx.offset += status;
  7244. + ssh_ptl_tx_threadfn_wait(ptl);
  7245. + }
  7246. + }
  7247. +
  7248. + // cancel active packet before we actually stop
  7249. + if (!IS_ERR_OR_NULL(ptl->tx.packet)) {
  7250. + ssh_ptl_tx_compl_error(ptl->tx.packet, -ESHUTDOWN);
  7251. + ssh_packet_put(ptl->tx.packet);
  7252. + ptl->tx.packet = NULL;
  7253. + }
  7254. +
  7255. + return 0;
  7256. +}
  7257. +
  7258. +static inline void ssh_ptl_tx_wakeup(struct ssh_ptl *ptl, bool force)
  7259. +{
  7260. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  7261. + return;
  7262. +
  7263. + if (force || atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING) {
  7264. + WRITE_ONCE(ptl->tx.thread_signal, true);
  7265. + smp_mb__after_atomic();
  7266. + wake_up(&ptl->tx.thread_wq);
  7267. + }
  7268. +}
  7269. +
  7270. +static int ssh_ptl_tx_start(struct ssh_ptl *ptl)
  7271. +{
  7272. + ptl->tx.thread = kthread_run(ssh_ptl_tx_threadfn, ptl, "surface-sh-tx");
  7273. + if (IS_ERR(ptl->tx.thread))
  7274. + return PTR_ERR(ptl->tx.thread);
  7275. +
  7276. + return 0;
  7277. +}
  7278. +
  7279. +static int ssh_ptl_tx_stop(struct ssh_ptl *ptl)
  7280. +{
  7281. + int status = 0;
  7282. +
  7283. + if (ptl->tx.thread) {
  7284. + status = kthread_stop(ptl->tx.thread);
  7285. + ptl->tx.thread = NULL;
  7286. + }
  7287. +
  7288. + return status;
  7289. +}
  7290. +
  7291. +
  7292. +static struct ssh_packet *ssh_ptl_ack_pop(struct ssh_ptl *ptl, u8 seq_id)
  7293. +{
  7294. + struct ssh_packet *packet = ERR_PTR(-ENOENT);
  7295. + struct ssh_packet *p, *n;
  7296. +
  7297. + spin_lock(&ptl->pending.lock);
  7298. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  7299. + /*
  7300. + * We generally expect packets to be in order, so first packet
  7301. + * to be added to pending is first to be sent, is first to be
  7302. + * ACKed.
  7303. + */
  7304. + if (unlikely(ssh_packet_get_seq(p) != seq_id))
  7305. + continue;
  7306. +
  7307. + /*
  7308. + * In case we receive an ACK while handling a transmission error
  7309. + * completion. The packet will be removed shortly.
  7310. + */
  7311. + if (unlikely(test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
  7312. + packet = ERR_PTR(-EPERM);
  7313. + break;
  7314. + }
  7315. +
  7316. + /*
  7317. + * Mark packet as ACKed and remove it from pending. Ensure that
  7318. + * the flags never get zero with barrier.
  7319. + */
  7320. + set_bit(SSH_PACKET_SF_ACKED_BIT, &p->state);
  7321. + smp_mb__before_atomic();
  7322. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  7323. +
  7324. + atomic_dec(&ptl->pending.count);
  7325. + list_del(&p->pending_node);
  7326. + packet = p;
  7327. +
  7328. + break;
  7329. + }
  7330. + spin_unlock(&ptl->pending.lock);
  7331. +
  7332. + return packet;
  7333. +}
  7334. +
  7335. +static void ssh_ptl_wait_until_transmitted(struct ssh_packet *packet)
  7336. +{
  7337. + wait_event(packet->ptl->tx.packet_wq,
  7338. + test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state)
  7339. + || test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state));
  7340. +}
  7341. +
  7342. +static void ssh_ptl_acknowledge(struct ssh_ptl *ptl, u8 seq)
  7343. +{
  7344. + struct ssh_packet *p;
  7345. + int status = 0;
  7346. +
  7347. + p = ssh_ptl_ack_pop(ptl, seq);
  7348. + if (IS_ERR(p)) {
  7349. + if (PTR_ERR(p) == -ENOENT) {
  7350. + /*
  7351. + * The packet has not been found in the set of pending
  7352. + * packets.
  7353. + */
  7354. + ptl_warn(ptl, "ptl: received ACK for non-pending"
  7355. + " packet\n");
  7356. + } else {
  7357. + /*
  7358. + * The packet is pending, but we are not allowed to take
  7359. + * it because it has been locked.
  7360. + */
  7361. + }
  7362. + return;
  7363. + }
  7364. +
  7365. + ptl_dbg(ptl, "ptl: received ACK for packet %p\n", p);
  7366. +
  7367. + /*
  7368. + * It is possible that the packet has been transmitted, but the state
  7369. + * has not been updated from "transmitting" to "transmitted" yet.
  7370. + * In that case, we need to wait for this transition to occur in order
  7371. + * to determine between success or failure.
  7372. + */
  7373. + if (test_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state))
  7374. + ssh_ptl_wait_until_transmitted(p);
  7375. +
  7376. + /*
  7377. + * The packet will already be locked in case of a transmission error or
  7378. + * cancellation. Let the transmitter or cancellation issuer complete the
  7379. + * packet.
  7380. + */
  7381. + if (unlikely(test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
  7382. + ssh_packet_put(p);
  7383. + return;
  7384. + }
  7385. +
  7386. + if (unlikely(!test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &p->state))) {
  7387. + ptl_err(ptl, "ptl: received ACK before packet had been fully"
  7388. + " transmitted\n");
  7389. + status = -EREMOTEIO;
  7390. + }
  7391. +
  7392. + ssh_ptl_remove_and_complete(p, status);
  7393. + ssh_packet_put(p);
  7394. +
  7395. + ssh_ptl_tx_wakeup(ptl, false);
  7396. +}
  7397. +
  7398. +
  7399. +static int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
  7400. +{
  7401. + struct ssh_ptl *ptl_old;
  7402. + int status;
  7403. +
  7404. + trace_ssam_packet_submit(p);
  7405. +
  7406. + // validate packet fields
  7407. + if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
  7408. + if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
  7409. + return -EINVAL;
  7410. + } else if (!p->data.ptr) {
  7411. + return -EINVAL;
  7412. + }
  7413. +
  7414. + /*
  7415. + * The ptl reference only gets set on or before the first submission.
  7416. + * After the first submission, it has to be read-only.
  7417. + */
  7418. + ptl_old = READ_ONCE(p->ptl);
  7419. + if (ptl_old == NULL)
  7420. + WRITE_ONCE(p->ptl, ptl);
  7421. + else if (ptl_old != ptl)
  7422. + return -EALREADY;
  7423. +
  7424. + status = ssh_ptl_queue_push(p);
  7425. + if (status)
  7426. + return status;
  7427. +
  7428. + ssh_ptl_tx_wakeup(ptl, !test_bit(SSH_PACKET_TY_BLOCKING_BIT, &p->state));
  7429. + return 0;
  7430. +}
  7431. +
  7432. +static void __ssh_ptl_resubmit(struct ssh_packet *packet)
  7433. +{
  7434. + struct list_head *head;
  7435. +
  7436. + trace_ssam_packet_resubmit(packet);
  7437. +
  7438. + spin_lock(&packet->ptl->queue.lock);
  7439. +
  7440. + // if this packet has already been queued, do not add it
  7441. + if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
  7442. + spin_unlock(&packet->ptl->queue.lock);
  7443. + return;
  7444. + }
  7445. +
  7446. + // find first node with lower priority
  7447. + head = __ssh_ptl_queue_find_entrypoint(packet);
  7448. +
  7449. + WRITE_ONCE(packet->timestamp, KTIME_MAX);
  7450. + smp_mb__after_atomic();
  7451. +
  7452. + // add packet
  7453. + ssh_packet_get(packet);
  7454. + list_add_tail(&packet->queue_node, head);
  7455. +
  7456. + spin_unlock(&packet->ptl->queue.lock);
  7457. +}
  7458. +
  7459. +static void ssh_ptl_resubmit_pending(struct ssh_ptl *ptl)
  7460. +{
  7461. + struct ssh_packet *p;
  7462. + bool resub = false;
  7463. + u8 try;
  7464. +
  7465. + /*
  7466. + * Note: We deliberately do not remove/attempt to cancel and complete
  7467. + * packets that are out of tires in this function. The packet will be
  7468. + * eventually canceled and completed by the timeout. Removing the packet
  7469. + * here could lead to overly eager cancelation if the packet has not
  7470. + * been re-transmitted yet but the tries-counter already updated (i.e
  7471. + * ssh_ptl_tx_next removed the packet from the queue and updated the
  7472. + * counter, but re-transmission for the last try has not actually
  7473. + * started yet).
  7474. + */
  7475. +
  7476. + spin_lock(&ptl->pending.lock);
  7477. +
  7478. + // re-queue all pending packets
  7479. + list_for_each_entry(p, &ptl->pending.head, pending_node) {
  7480. + // avoid further transitions if locked
  7481. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  7482. + continue;
  7483. +
  7484. + // do not re-schedule if packet is out of tries
  7485. + try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
  7486. + if (try >= SSH_PTL_MAX_PACKET_TRIES)
  7487. + continue;
  7488. +
  7489. + resub = true;
  7490. + __ssh_ptl_resubmit(p);
  7491. + }
  7492. +
  7493. + spin_unlock(&ptl->pending.lock);
  7494. +
  7495. + ssh_ptl_tx_wakeup(ptl, resub);
  7496. +}
  7497. +
  7498. +static void ssh_ptl_cancel(struct ssh_packet *p)
  7499. +{
  7500. + if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
  7501. + return;
  7502. +
  7503. + trace_ssam_packet_cancel(p);
  7504. +
  7505. + /*
  7506. + * Lock packet and commit with memory barrier. If this packet has
  7507. + * already been locked, it's going to be removed and completed by
  7508. + * another party, which should have precedence.
  7509. + */
  7510. + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  7511. + return;
  7512. +
  7513. + /*
  7514. + * By marking the packet as locked and employing the implicit memory
  7515. + * barrier of test_and_set_bit, we have guaranteed that, at this point,
  7516. + * the packet cannot be added to the queue any more.
  7517. + *
  7518. + * In case the packet has never been submitted, packet->ptl is NULL. If
  7519. + * the packet is currently being submitted, packet->ptl may be NULL or
  7520. + * non-NULL. Due marking the packet as locked above and committing with
  7521. + * the memory barrier, we have guaranteed that, if packet->ptl is NULL,
  7522. + * the packet will never be added to the queue. If packet->ptl is
  7523. + * non-NULL, we don't have any guarantees.
  7524. + */
  7525. +
  7526. + if (READ_ONCE(p->ptl)) {
  7527. + ssh_ptl_remove_and_complete(p, -ECANCELED);
  7528. + ssh_ptl_tx_wakeup(p->ptl, false);
  7529. + } else if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
  7530. + __ssh_ptl_complete(p, -ECANCELED);
  7531. + }
  7532. +}
  7533. +
  7534. +
  7535. +static ktime_t ssh_packet_get_expiration(struct ssh_packet *p, ktime_t timeout)
  7536. +{
  7537. + ktime_t timestamp = READ_ONCE(p->timestamp);
  7538. +
  7539. + if (timestamp != KTIME_MAX)
  7540. + return ktime_add(timestamp, timeout);
  7541. + else
  7542. + return KTIME_MAX;
  7543. +}
  7544. +
  7545. +static void ssh_ptl_timeout_reap(struct work_struct *work)
  7546. +{
  7547. + struct ssh_ptl *ptl = to_ssh_ptl(work, rtx_timeout.reaper.work);
  7548. + struct ssh_packet *p, *n;
  7549. + LIST_HEAD(claimed);
  7550. + ktime_t now = ktime_get_coarse_boottime();
  7551. + ktime_t timeout = ptl->rtx_timeout.timeout;
  7552. + ktime_t next = KTIME_MAX;
  7553. + bool resub = false;
  7554. +
  7555. + trace_ssam_ptl_timeout_reap("pending", atomic_read(&ptl->pending.count));
  7556. +
  7557. + /*
  7558. + * Mark reaper as "not pending". This is done before checking any
  7559. + * packets to avoid lost-update type problems.
  7560. + */
  7561. + WRITE_ONCE(ptl->rtx_timeout.expires, KTIME_MAX);
  7562. + smp_mb__after_atomic();
  7563. +
  7564. + spin_lock(&ptl->pending.lock);
  7565. +
  7566. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  7567. + ktime_t expires = ssh_packet_get_expiration(p, timeout);
  7568. + u8 try;
  7569. +
  7570. + /*
  7571. + * Check if the timeout hasn't expired yet. Find out next
  7572. + * expiration date to be handled after this run.
  7573. + */
  7574. + if (ktime_after(expires, now)) {
  7575. + next = ktime_before(expires, next) ? expires : next;
  7576. + continue;
  7577. + }
  7578. +
  7579. + // avoid further transitions if locked
  7580. + if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  7581. + continue;
  7582. +
  7583. + trace_ssam_packet_timeout(p);
  7584. +
  7585. + // check if we still have some tries left
  7586. + try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
  7587. + if (likely(try < SSH_PTL_MAX_PACKET_TRIES)) {
  7588. + resub = true;
  7589. + __ssh_ptl_resubmit(p);
  7590. + continue;
  7591. + }
  7592. +
  7593. + // no more tries left: cancel the packet
  7594. +
  7595. + // if someone else has locked the packet already, don't use it
  7596. + if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
  7597. + continue;
  7598. +
  7599. + /*
  7600. + * We have now marked the packet as locked. Thus it cannot be
  7601. + * added to the pending list again after we've removed it here.
  7602. + * We can therefore re-use the pending_node of this packet
  7603. + * temporarily.
  7604. + */
  7605. +
  7606. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  7607. +
  7608. + atomic_dec(&ptl->pending.count);
  7609. + list_del(&p->pending_node);
  7610. +
  7611. + list_add_tail(&p->pending_node, &claimed);
  7612. + }
  7613. +
  7614. + spin_unlock(&ptl->pending.lock);
  7615. +
  7616. + // cancel and complete the packet
  7617. + list_for_each_entry_safe(p, n, &claimed, pending_node) {
  7618. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
  7619. + ssh_ptl_queue_remove(p);
  7620. + __ssh_ptl_complete(p, -ETIMEDOUT);
  7621. + }
  7622. +
  7623. + // drop the reference we've obtained by removing it from pending
  7624. + list_del(&p->pending_node);
  7625. + ssh_packet_put(p);
  7626. + }
  7627. +
  7628. + // ensure that reaper doesn't run again immediately
  7629. + next = max(next, ktime_add(now, SSH_PTL_PACKET_TIMEOUT_RESOLUTION));
  7630. + if (next != KTIME_MAX)
  7631. + ssh_ptl_timeout_reaper_mod(ptl, now, next);
  7632. +
  7633. + // force-wakeup to properly handle re-transmits if we've re-submitted
  7634. + ssh_ptl_tx_wakeup(ptl, resub);
  7635. +}
  7636. +
  7637. +
  7638. +static bool ssh_ptl_rx_retransmit_check(struct ssh_ptl *ptl, u8 seq)
  7639. +{
  7640. + int i;
  7641. +
  7642. + // check if SEQ has been seen recently (i.e. packet was re-transmitted)
  7643. + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++) {
  7644. + if (likely(ptl->rx.blocked.seqs[i] != seq))
  7645. + continue;
  7646. +
  7647. + ptl_dbg(ptl, "ptl: ignoring repeated data packet\n");
  7648. + return true;
  7649. + }
  7650. +
  7651. + // update list of blocked seuence IDs
  7652. + ptl->rx.blocked.seqs[ptl->rx.blocked.offset] = seq;
  7653. + ptl->rx.blocked.offset = (ptl->rx.blocked.offset + 1)
  7654. + % ARRAY_SIZE(ptl->rx.blocked.seqs);
  7655. +
  7656. + return false;
  7657. +}
  7658. +
  7659. +static void ssh_ptl_rx_dataframe(struct ssh_ptl *ptl,
  7660. + const struct ssh_frame *frame,
  7661. + const struct ssam_span *payload)
  7662. +{
  7663. + if (ssh_ptl_rx_retransmit_check(ptl, frame->seq))
  7664. + return;
  7665. +
  7666. + ptl->ops.data_received(ptl, payload);
  7667. +}
  7668. +
  7669. +static void ssh_ptl_send_ack(struct ssh_ptl *ptl, u8 seq)
  7670. +{
  7671. + struct ssh_packet_args args;
  7672. + struct ssh_packet *packet;
  7673. + struct ssam_span buf;
  7674. + struct msgbuf msgb;
  7675. + int status;
  7676. +
  7677. + status = ptl_alloc_ctrl_packet(ptl, &packet, &buf, GFP_KERNEL);
  7678. + if (status) {
  7679. + ptl_err(ptl, "ptl: failed to allocate ACK packet\n");
  7680. + return;
  7681. + }
  7682. +
  7683. + args.type = 0;
  7684. + args.priority = SSH_PACKET_PRIORITY(ACK, 0);
  7685. + args.ops = &ssh_ptl_ctrl_packet_ops;
  7686. + ssh_packet_init(packet, &args);
  7687. +
  7688. + msgb_init(&msgb, buf.ptr, buf.len);
  7689. + msgb_push_ack(&msgb, seq);
  7690. + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
  7691. +
  7692. + ssh_ptl_submit(ptl, packet);
  7693. + ssh_packet_put(packet);
  7694. +}
  7695. +
  7696. +static void ssh_ptl_send_nak(struct ssh_ptl *ptl)
  7697. +{
  7698. + struct ssh_packet_args args;
  7699. + struct ssh_packet *packet;
  7700. + struct ssam_span buf;
  7701. + struct msgbuf msgb;
  7702. + int status;
  7703. +
  7704. + status = ptl_alloc_ctrl_packet(ptl, &packet, &buf, GFP_KERNEL);
  7705. + if (status) {
  7706. + ptl_err(ptl, "ptl: failed to allocate NAK packet\n");
  7707. + return;
  7708. + }
  7709. +
  7710. + args.type = 0;
  7711. + args.priority = SSH_PACKET_PRIORITY(NAK, 0);
  7712. + args.ops = &ssh_ptl_ctrl_packet_ops;
  7713. + ssh_packet_init(packet, &args);
  7714. +
  7715. + msgb_init(&msgb, buf.ptr, buf.len);
  7716. + msgb_push_nak(&msgb);
  7717. + ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
  7718. +
  7719. + ssh_ptl_submit(ptl, packet);
  7720. + ssh_packet_put(packet);
  7721. +}
  7722. +
  7723. +static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
  7724. +{
  7725. + struct ssh_frame *frame;
  7726. + struct ssam_span payload;
  7727. + struct ssam_span aligned;
  7728. + bool syn_found;
  7729. + int status;
  7730. +
  7731. + // error injection: modify data to simulate corrupt SYN bytes
  7732. + ssh_ptl_rx_inject_invalid_syn(ptl, source);
  7733. +
  7734. + // find SYN
  7735. + syn_found = sshp_find_syn(source, &aligned);
  7736. +
  7737. + if (unlikely(aligned.ptr - source->ptr) > 0) {
  7738. + ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
  7739. +
  7740. + /*
  7741. + * Notes:
  7742. + * - This might send multiple NAKs in case the communication
  7743. + * starts with an invalid SYN and is broken down into multiple
  7744. + * pieces. This should generally be handled fine, we just
  7745. + * might receive duplicate data in this case, which is
  7746. + * detected when handling data frames.
  7747. + * - This path will also be executed on invalid CRCs: When an
  7748. + * invalid CRC is encountered, the code below will skip data
  7749. + * until direclty after the SYN. This causes the search for
  7750. + * the next SYN, which is generally not placed directly after
  7751. + * the last one.
  7752. + */
  7753. + ssh_ptl_send_nak(ptl);
  7754. + }
  7755. +
  7756. + if (unlikely(!syn_found))
  7757. + return aligned.ptr - source->ptr;
  7758. +
  7759. + // error injection: modify data to simulate corruption
  7760. + ssh_ptl_rx_inject_invalid_data(ptl, &aligned);
  7761. +
  7762. + // parse and validate frame
  7763. + status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
  7764. + SSH_PTL_RX_BUF_LEN);
  7765. + if (status) // invalid frame: skip to next syn
  7766. + return aligned.ptr - source->ptr + sizeof(u16);
  7767. + if (!frame) // not enough data
  7768. + return aligned.ptr - source->ptr;
  7769. +
  7770. + trace_ssam_rx_frame_received(frame);
  7771. +
  7772. + switch (frame->type) {
  7773. + case SSH_FRAME_TYPE_ACK:
  7774. + ssh_ptl_acknowledge(ptl, frame->seq);
  7775. + break;
  7776. +
  7777. + case SSH_FRAME_TYPE_NAK:
  7778. + ssh_ptl_resubmit_pending(ptl);
  7779. + break;
  7780. +
  7781. + case SSH_FRAME_TYPE_DATA_SEQ:
  7782. + ssh_ptl_send_ack(ptl, frame->seq);
  7783. + /* fallthrough */
  7784. +
  7785. + case SSH_FRAME_TYPE_DATA_NSQ:
  7786. + ssh_ptl_rx_dataframe(ptl, frame, &payload);
  7787. + break;
  7788. +
  7789. + default:
  7790. + ptl_warn(ptl, "ptl: received frame with unknown type 0x%02x\n",
  7791. + frame->type);
  7792. + break;
  7793. + }
  7794. +
  7795. + return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(frame->len);
  7796. +}
  7797. +
  7798. +static int ssh_ptl_rx_threadfn(void *data)
  7799. +{
  7800. + struct ssh_ptl *ptl = data;
  7801. +
  7802. + while (true) {
  7803. + struct ssam_span span;
  7804. + size_t offs = 0;
  7805. + size_t n;
  7806. +
  7807. + wait_event_interruptible(ptl->rx.wq,
  7808. + !kfifo_is_empty(&ptl->rx.fifo)
  7809. + || kthread_should_stop());
  7810. + if (kthread_should_stop())
  7811. + break;
  7812. +
  7813. + // copy from fifo to evaluation buffer
  7814. + n = sshp_buf_read_from_fifo(&ptl->rx.buf, &ptl->rx.fifo);
  7815. +
  7816. + ptl_dbg(ptl, "rx: received data (size: %zu)\n", n);
  7817. + print_hex_dump_debug("rx: ", DUMP_PREFIX_OFFSET, 16, 1,
  7818. + ptl->rx.buf.ptr + ptl->rx.buf.len - n,
  7819. + n, false);
  7820. +
  7821. + // parse until we need more bytes or buffer is empty
  7822. + while (offs < ptl->rx.buf.len) {
  7823. + sshp_buf_span_from(&ptl->rx.buf, offs, &span);
  7824. + n = ssh_ptl_rx_eval(ptl, &span);
  7825. + if (n == 0)
  7826. + break; // need more bytes
  7827. +
  7828. + offs += n;
  7829. + }
  7830. +
  7831. + // throw away the evaluated parts
  7832. + sshp_buf_drop(&ptl->rx.buf, offs);
  7833. + }
  7834. +
  7835. + return 0;
  7836. +}
  7837. +
  7838. +static inline void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
  7839. +{
  7840. + wake_up(&ptl->rx.wq);
  7841. +}
  7842. +
  7843. +static int ssh_ptl_rx_start(struct ssh_ptl *ptl)
  7844. +{
  7845. + if (ptl->rx.thread)
  7846. + return 0;
  7847. +
  7848. + ptl->rx.thread = kthread_run(ssh_ptl_rx_threadfn, ptl, "surface-sh-rx");
  7849. + if (IS_ERR(ptl->rx.thread))
  7850. + return PTR_ERR(ptl->rx.thread);
  7851. +
  7852. + return 0;
  7853. +}
  7854. +
  7855. +static int ssh_ptl_rx_stop(struct ssh_ptl *ptl)
  7856. +{
  7857. + int status = 0;
  7858. +
  7859. + if (ptl->rx.thread) {
  7860. + status = kthread_stop(ptl->rx.thread);
  7861. + ptl->rx.thread = NULL;
  7862. + }
  7863. +
  7864. + return status;
  7865. +}
  7866. +
  7867. +static int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
  7868. +{
  7869. + int used;
  7870. +
  7871. + if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
  7872. + return -ESHUTDOWN;
  7873. +
  7874. + used = kfifo_in(&ptl->rx.fifo, buf, n);
  7875. + if (used)
  7876. + ssh_ptl_rx_wakeup(ptl);
  7877. +
  7878. + return used;
  7879. +}
  7880. +
  7881. +
  7882. +struct ssh_flush_packet {
  7883. + struct ssh_packet base;
  7884. + struct completion completion;
  7885. + int status;
  7886. +};
  7887. +
  7888. +static void ssh_ptl_flush_complete(struct ssh_packet *p, int status)
  7889. +{
  7890. + struct ssh_flush_packet *packet;
  7891. +
  7892. + packet = container_of(p, struct ssh_flush_packet, base);
  7893. + packet->status = status;
  7894. +}
  7895. +
  7896. +static void ssh_ptl_flush_release(struct ssh_packet *p)
  7897. +{
  7898. + struct ssh_flush_packet *packet;
  7899. +
  7900. + packet = container_of(p, struct ssh_flush_packet, base);
  7901. + complete_all(&packet->completion);
  7902. +}
  7903. +
  7904. +static const struct ssh_packet_ops ssh_flush_packet_ops = {
  7905. + .complete = ssh_ptl_flush_complete,
  7906. + .release = ssh_ptl_flush_release,
  7907. +};
  7908. +
  7909. +/**
  7910. + * ssh_ptl_shutdown - shut down the packet transmission layer
  7911. + * @ptl: packet transmission layer
  7912. + *
  7913. + * Shuts down the packet transmission layer, removing and canceling all queued
  7914. + * and pending packets. Packets canceled by this operation will be completed
  7915. + * with -ESHUTDOWN as status.
  7916. + *
  7917. + * As a result of this function, the transmission layer will be marked as shut
  7918. + * down. Submission of packets after the transmission layer has been shut down
  7919. + * will fail with -ESHUTDOWN.
  7920. + */
  7921. +static void ssh_ptl_shutdown(struct ssh_ptl *ptl)
  7922. +{
  7923. + LIST_HEAD(complete_q);
  7924. + LIST_HEAD(complete_p);
  7925. + struct ssh_packet *p, *n;
  7926. + int status;
  7927. +
  7928. + // ensure that no new packets (including ACK/NAK) can be submitted
  7929. + set_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state);
  7930. + smp_mb__after_atomic();
  7931. +
  7932. + status = ssh_ptl_rx_stop(ptl);
  7933. + if (status)
  7934. + ptl_err(ptl, "ptl: failed to stop receiver thread\n");
  7935. +
  7936. + status = ssh_ptl_tx_stop(ptl);
  7937. + if (status)
  7938. + ptl_err(ptl, "ptl: failed to stop transmitter thread\n");
  7939. +
  7940. + cancel_delayed_work_sync(&ptl->rtx_timeout.reaper);
  7941. +
  7942. + /*
  7943. + * At this point, all threads have been stopped. This means that the
  7944. + * only references to packets from inside the system are in the queue
  7945. + * and pending set.
  7946. + *
  7947. + * Note: We still need locks here because someone could still be
  7948. + * cancelling packets.
  7949. + *
  7950. + * Note 2: We can re-use queue_node (or pending_node) if we mark the
  7951. + * packet as locked an then remove it from the queue (or pending set
  7952. + * respecitvely). Marking the packet as locked avoids re-queueing
  7953. + * (which should already be prevented by having stopped the treads...)
  7954. + * and not setting QUEUED_BIT (or PENDING_BIT) prevents removal from a
  7955. + * new list via other threads (e.g. canellation).
  7956. + *
  7957. + * Note 3: There may be overlap between complete_p and complete_q.
  7958. + * This is handled via test_and_set_bit on the "completed" flag
  7959. + * (also handles cancelation).
  7960. + */
  7961. +
  7962. + // mark queued packets as locked and move them to complete_q
  7963. + spin_lock(&ptl->queue.lock);
  7964. + list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
  7965. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
  7966. + smp_mb__before_atomic();
  7967. + clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
  7968. +
  7969. + list_del(&p->queue_node);
  7970. + list_add_tail(&p->queue_node, &complete_q);
  7971. + }
  7972. + spin_unlock(&ptl->queue.lock);
  7973. +
  7974. + // mark pending packets as locked and move them to complete_p
  7975. + spin_lock(&ptl->pending.lock);
  7976. + list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
  7977. + set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
  7978. + smp_mb__before_atomic();
  7979. + clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
  7980. +
  7981. + list_del(&p->pending_node);
  7982. + list_add_tail(&p->pending_node, &complete_q);
  7983. + }
  7984. + atomic_set(&ptl->pending.count, 0);
  7985. + spin_unlock(&ptl->pending.lock);
  7986. +
  7987. + // complete and drop packets on complete_q
  7988. + list_for_each_entry(p, &complete_q, queue_node) {
  7989. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  7990. + __ssh_ptl_complete(p, -ESHUTDOWN);
  7991. +
  7992. + ssh_packet_put(p);
  7993. + }
  7994. +
  7995. + // complete and drop packets on complete_p
  7996. + list_for_each_entry(p, &complete_p, pending_node) {
  7997. + if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  7998. + __ssh_ptl_complete(p, -ESHUTDOWN);
  7999. +
  8000. + ssh_packet_put(p);
  8001. + }
  8002. +
  8003. + /*
  8004. + * At this point we have guaranteed that the system doesn't reference
  8005. + * any packets any more.
  8006. + */
  8007. +}
  8008. +
  8009. +static inline struct device *ssh_ptl_get_device(struct ssh_ptl *ptl)
  8010. +{
  8011. + return &ptl->serdev->dev;
  8012. +}
  8013. +
  8014. +static int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
  8015. + struct ssh_ptl_ops *ops)
  8016. +{
  8017. + int i, status;
  8018. +
  8019. + ptl->serdev = serdev;
  8020. + ptl->state = 0;
  8021. +
  8022. + spin_lock_init(&ptl->queue.lock);
  8023. + INIT_LIST_HEAD(&ptl->queue.head);
  8024. +
  8025. + spin_lock_init(&ptl->pending.lock);
  8026. + INIT_LIST_HEAD(&ptl->pending.head);
  8027. + atomic_set_release(&ptl->pending.count, 0);
  8028. +
  8029. + ptl->tx.thread = NULL;
  8030. + ptl->tx.thread_signal = false;
  8031. + ptl->tx.packet = NULL;
  8032. + ptl->tx.offset = 0;
  8033. + init_waitqueue_head(&ptl->tx.thread_wq);
  8034. + init_waitqueue_head(&ptl->tx.packet_wq);
  8035. +
  8036. + ptl->rx.thread = NULL;
  8037. + init_waitqueue_head(&ptl->rx.wq);
  8038. +
  8039. + ptl->rtx_timeout.timeout = SSH_PTL_PACKET_TIMEOUT;
  8040. + ptl->rtx_timeout.expires = KTIME_MAX;
  8041. + INIT_DELAYED_WORK(&ptl->rtx_timeout.reaper, ssh_ptl_timeout_reap);
  8042. +
  8043. + ptl->ops = *ops;
  8044. +
  8045. + // initialize list of recent/blocked SEQs with invalid sequence IDs
  8046. + for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++)
  8047. + ptl->rx.blocked.seqs[i] = 0xFFFF;
  8048. + ptl->rx.blocked.offset = 0;
  8049. +
  8050. + status = kfifo_alloc(&ptl->rx.fifo, SSH_PTL_RX_FIFO_LEN, GFP_KERNEL);
  8051. + if (status)
  8052. + return status;
  8053. +
  8054. + status = sshp_buf_alloc(&ptl->rx.buf, SSH_PTL_RX_BUF_LEN, GFP_KERNEL);
  8055. + if (status)
  8056. + kfifo_free(&ptl->rx.fifo);
  8057. +
  8058. + return status;
  8059. +}
  8060. +
  8061. +static void ssh_ptl_destroy(struct ssh_ptl *ptl)
  8062. +{
  8063. + kfifo_free(&ptl->rx.fifo);
  8064. + sshp_buf_free(&ptl->rx.buf);
  8065. +}
  8066. +
  8067. +
  8068. +/* -- Request transport layer (rtl). ---------------------------------------- */
  8069. +
  8070. +#define SSH_RTL_REQUEST_TIMEOUT ms_to_ktime(3000)
  8071. +#define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
  8072. +
  8073. +#define SSH_RTL_MAX_PENDING 3
  8074. +
  8075. +
  8076. +enum ssh_rtl_state_flags {
  8077. + SSH_RTL_SF_SHUTDOWN_BIT,
  8078. +};
  8079. +
  8080. +struct ssh_rtl_ops {
  8081. + void (*handle_event)(struct ssh_rtl *rtl, const struct ssh_command *cmd,
  8082. + const struct ssam_span *data);
  8083. +};
  8084. +
  8085. +struct ssh_rtl {
  8086. + struct ssh_ptl ptl;
  8087. + unsigned long state;
  8088. +
  8089. + struct {
  8090. + spinlock_t lock;
  8091. + struct list_head head;
  8092. + } queue;
  8093. +
  8094. + struct {
  8095. + spinlock_t lock;
  8096. + struct list_head head;
  8097. + atomic_t count;
  8098. + } pending;
  8099. +
  8100. + struct {
  8101. + struct work_struct work;
  8102. + } tx;
  8103. +
  8104. + struct {
  8105. + ktime_t timeout;
  8106. + ktime_t expires;
  8107. + struct delayed_work reaper;
  8108. + } rtx_timeout;
  8109. +
  8110. + struct ssh_rtl_ops ops;
  8111. +};
  8112. +
  8113. +
  8114. +#define rtl_dbg(r, fmt, ...) ptl_dbg(&(r)->ptl, fmt, ##__VA_ARGS__)
  8115. +#define rtl_info(p, fmt, ...) ptl_info(&(p)->ptl, fmt, ##__VA_ARGS__)
  8116. +#define rtl_warn(r, fmt, ...) ptl_warn(&(r)->ptl, fmt, ##__VA_ARGS__)
  8117. +#define rtl_err(r, fmt, ...) ptl_err(&(r)->ptl, fmt, ##__VA_ARGS__)
  8118. +#define rtl_dbg_cond(r, fmt, ...) __ssam_prcond(rtl_dbg, r, fmt, ##__VA_ARGS__)
  8119. +
  8120. +#define to_ssh_rtl(ptr, member) \
  8121. + container_of(ptr, struct ssh_rtl, member)
  8122. +
  8123. +#define to_ssh_request(ptr, member) \
  8124. + container_of(ptr, struct ssh_request, member)
  8125. +
  8126. +static inline struct ssh_rtl *ssh_request_rtl(struct ssh_request *rqst)
  8127. +{
  8128. + struct ssh_ptl *ptl = READ_ONCE(rqst->packet.ptl);
  8129. + return likely(ptl) ? to_ssh_rtl(ptl, ptl) : NULL;
  8130. +}
  8131. +
  8132. +
  8133. +/**
  8134. + * ssh_rtl_should_drop_response - error injection hook to drop request responses
  8135. + *
  8136. + * Useful to cause request transmission timeouts in the driver by dropping the
  8137. + * response to a request.
  8138. + */
  8139. +static noinline_if_inject bool ssh_rtl_should_drop_response(void)
  8140. +{
  8141. + return false;
  8142. +}
  8143. +ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
  8144. +
  8145. +
  8146. +static inline u16 ssh_request_get_rqid(struct ssh_request *rqst)
  8147. +{
  8148. + return get_unaligned_le16(rqst->packet.data.ptr
  8149. + + SSH_MSGOFFSET_COMMAND(rqid));
  8150. +}
  8151. +
  8152. +static inline u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
  8153. +{
  8154. + if (!rqst->packet.data.ptr)
  8155. + return -1;
  8156. +
  8157. + return ssh_request_get_rqid(rqst);
  8158. +}
  8159. +
  8160. +
  8161. +static void ssh_rtl_queue_remove(struct ssh_request *rqst)
  8162. +{
  8163. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8164. + bool remove;
  8165. +
  8166. + spin_lock(&rtl->queue.lock);
  8167. +
  8168. + remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
  8169. + if (remove)
  8170. + list_del(&rqst->node);
  8171. +
  8172. + spin_unlock(&rtl->queue.lock);
  8173. +
  8174. + if (remove)
  8175. + ssh_request_put(rqst);
  8176. +}
  8177. +
  8178. +static void ssh_rtl_pending_remove(struct ssh_request *rqst)
  8179. +{
  8180. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8181. + bool remove;
  8182. +
  8183. + spin_lock(&rtl->pending.lock);
  8184. +
  8185. + remove = test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state);
  8186. + if (remove) {
  8187. + atomic_dec(&rtl->pending.count);
  8188. + list_del(&rqst->node);
  8189. + }
  8190. +
  8191. + spin_unlock(&rtl->pending.lock);
  8192. +
  8193. + if (remove)
  8194. + ssh_request_put(rqst);
  8195. +}
  8196. +
  8197. +
  8198. +static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
  8199. +{
  8200. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8201. +
  8202. + trace_ssam_request_complete(rqst, status);
  8203. +
  8204. + // rtl/ptl may not be set if we're cancelling before submitting
  8205. + rtl_dbg_cond(rtl, "rtl: completing request (rqid: 0x%04x,"
  8206. + " status: %d)\n", ssh_request_get_rqid_safe(rqst), status);
  8207. +
  8208. + if (status && status != -ECANCELED)
  8209. + rtl_dbg_cond(rtl, "rtl: request error: %d\n", status);
  8210. +
  8211. + rqst->ops->complete(rqst, NULL, NULL, status);
  8212. +}
  8213. +
  8214. +static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
  8215. + const struct ssh_command *cmd,
  8216. + const struct ssam_span *data)
  8217. +{
  8218. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8219. +
  8220. + trace_ssam_request_complete(rqst, 0);
  8221. +
  8222. + rtl_dbg(rtl, "rtl: completing request with response"
  8223. + " (rqid: 0x%04x)\n", ssh_request_get_rqid(rqst));
  8224. +
  8225. + rqst->ops->complete(rqst, cmd, data, 0);
  8226. +}
  8227. +
  8228. +
  8229. +static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
  8230. +{
  8231. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8232. +
  8233. + if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
  8234. + return !atomic_read(&rtl->pending.count);
  8235. +
  8236. + return atomic_read(&rtl->pending.count) < SSH_RTL_MAX_PENDING;
  8237. +}
  8238. +
  8239. +static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
  8240. +{
  8241. + struct ssh_request *rqst = ERR_PTR(-ENOENT);
  8242. + struct ssh_request *p, *n;
  8243. +
  8244. + spin_lock(&rtl->queue.lock);
  8245. +
  8246. + // find first non-locked request and remove it
  8247. + list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
  8248. + if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
  8249. + continue;
  8250. +
  8251. + if (!ssh_rtl_tx_can_process(p)) {
  8252. + rqst = ERR_PTR(-EBUSY);
  8253. + break;
  8254. + }
  8255. +
  8256. + /*
  8257. + * Remove from queue and mark as transmitting. Ensure that the
  8258. + * state does not get zero via memory barrier.
  8259. + */
  8260. + set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
  8261. + smp_mb__before_atomic();
  8262. + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
  8263. +
  8264. + list_del(&p->node);
  8265. +
  8266. + rqst = p;
  8267. + break;
  8268. + }
  8269. +
  8270. + spin_unlock(&rtl->queue.lock);
  8271. + return rqst;
  8272. +}
  8273. +
  8274. +static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
  8275. +{
  8276. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8277. +
  8278. + spin_lock(&rtl->pending.lock);
  8279. +
  8280. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
  8281. + spin_unlock(&rtl->pending.lock);
  8282. + return -EINVAL;
  8283. + }
  8284. +
  8285. + if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
  8286. + spin_unlock(&rtl->pending.lock);
  8287. + return -EALREADY;
  8288. + }
  8289. +
  8290. + atomic_inc(&rtl->pending.count);
  8291. + ssh_request_get(rqst);
  8292. + list_add_tail(&rqst->node, &rtl->pending.head);
  8293. +
  8294. + spin_unlock(&rtl->pending.lock);
  8295. + return 0;
  8296. +}
  8297. +
  8298. +static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
  8299. +{
  8300. + struct ssh_request *rqst;
  8301. + int status;
  8302. +
  8303. + // get and prepare next request for transmit
  8304. + rqst = ssh_rtl_tx_next(rtl);
  8305. + if (IS_ERR(rqst))
  8306. + return PTR_ERR(rqst);
  8307. +
  8308. + // add to/mark as pending
  8309. + status = ssh_rtl_tx_pending_push(rqst);
  8310. + if (status) {
  8311. + ssh_request_put(rqst);
  8312. + return -EAGAIN;
  8313. + }
  8314. +
  8315. + // submit packet
  8316. + status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
  8317. + if (status == -ESHUTDOWN) {
  8318. + /*
  8319. + * Packet has been refused due to the packet layer shutting
  8320. + * down. Complete it here.
  8321. + */
  8322. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
  8323. + smp_mb__after_atomic();
  8324. +
  8325. + ssh_rtl_pending_remove(rqst);
  8326. + ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
  8327. +
  8328. + ssh_request_put(rqst);
  8329. + return -ESHUTDOWN;
  8330. +
  8331. + } else if (status) {
  8332. + /*
  8333. + * If submitting the packet failed and the packet layer isn't
  8334. + * shutting down, the packet has either been submmitted/queued
  8335. + * before (-EALREADY, which cannot happen as we have guaranteed
  8336. + * that requests cannot be re-submitted), or the packet was
  8337. + * marked as locked (-EINVAL). To mark the packet locked at this
  8338. + * stage, the request, and thus the packets itself, had to have
  8339. + * been canceled. Simply drop the reference. Cancellation itself
  8340. + * will remove it from the set of pending requests.
  8341. + */
  8342. +
  8343. + WARN_ON(status != -EINVAL);
  8344. +
  8345. + ssh_request_put(rqst);
  8346. + return -EAGAIN;
  8347. + }
  8348. +
  8349. + ssh_request_put(rqst);
  8350. + return 0;
  8351. +}
  8352. +
  8353. +static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
  8354. +{
  8355. + bool empty;
  8356. +
  8357. + spin_lock(&rtl->queue.lock);
  8358. + empty = list_empty(&rtl->queue.head);
  8359. + spin_unlock(&rtl->queue.lock);
  8360. +
  8361. + return empty;
  8362. +}
  8363. +
  8364. +static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
  8365. +{
  8366. + if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
  8367. + return false;
  8368. +
  8369. + if (ssh_rtl_queue_empty(rtl))
  8370. + return false;
  8371. +
  8372. + return schedule_work(&rtl->tx.work);
  8373. +}
  8374. +
  8375. +static void ssh_rtl_tx_work_fn(struct work_struct *work)
  8376. +{
  8377. + struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
  8378. + int i, status;
  8379. +
  8380. + /*
  8381. + * Try to be nice and not block the workqueue: Run a maximum of 10
  8382. + * tries, then re-submit if necessary. This should not be neccesary,
  8383. + * for normal execution, but guarantee it anyway.
  8384. + */
  8385. + for (i = 0; i < 10; i++) {
  8386. + status = ssh_rtl_tx_try_process_one(rtl);
  8387. + if (status == -ENOENT || status == -EBUSY)
  8388. + return; // no more requests to process
  8389. +
  8390. + if (status == -ESHUTDOWN) {
  8391. + /*
  8392. + * Packet system shutting down. No new packets can be
  8393. + * transmitted. Return silently, the party initiating
  8394. + * the shutdown should handle the rest.
  8395. + */
  8396. + return;
  8397. + }
  8398. +
  8399. + WARN_ON(status != 0 && status != -EAGAIN);
  8400. + }
  8401. +
  8402. + // out of tries, reschedule
  8403. + ssh_rtl_tx_schedule(rtl);
  8404. +}
  8405. +
  8406. +
  8407. +static int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
  8408. +{
  8409. + trace_ssam_request_submit(rqst);
  8410. +
  8411. + /*
  8412. + * Ensure that requests expecting a response are sequenced. If this
  8413. + * invariant ever changes, see the comment in ssh_rtl_complete on what
  8414. + * is required to be changed in the code.
  8415. + */
  8416. + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
  8417. + if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &rqst->packet.state))
  8418. + return -EINVAL;
  8419. +
  8420. + // try to set ptl and check if this request has already been submitted
  8421. + if (cmpxchg(&rqst->packet.ptl, NULL, &rtl->ptl) != NULL)
  8422. + return -EALREADY;
  8423. +
  8424. + spin_lock(&rtl->queue.lock);
  8425. +
  8426. + if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
  8427. + spin_unlock(&rtl->queue.lock);
  8428. + return -ESHUTDOWN;
  8429. + }
  8430. +
  8431. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
  8432. + spin_unlock(&rtl->queue.lock);
  8433. + return -EINVAL;
  8434. + }
  8435. +
  8436. + ssh_request_get(rqst);
  8437. + set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
  8438. + list_add_tail(&rqst->node, &rtl->queue.head);
  8439. +
  8440. + spin_unlock(&rtl->queue.lock);
  8441. +
  8442. + ssh_rtl_tx_schedule(rtl);
  8443. + return 0;
  8444. +}
  8445. +
  8446. +
  8447. +static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
  8448. + ktime_t expires)
  8449. +{
  8450. + unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
  8451. + ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
  8452. + ktime_t old;
  8453. +
  8454. + // re-adjust / schedule reaper if it is above resolution delta
  8455. + old = READ_ONCE(rtl->rtx_timeout.expires);
  8456. + while (ktime_before(aexp, old))
  8457. + old = cmpxchg64(&rtl->rtx_timeout.expires, old, expires);
  8458. +
  8459. + // if we updated the reaper expiration, modify work timeout
  8460. + if (old == expires)
  8461. + mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
  8462. +}
  8463. +
  8464. +static void ssh_rtl_timeout_start(struct ssh_request *rqst)
  8465. +{
  8466. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  8467. + ktime_t timestamp = ktime_get_coarse_boottime();
  8468. + ktime_t timeout = rtl->rtx_timeout.timeout;
  8469. +
  8470. + if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
  8471. + return;
  8472. +
  8473. + WRITE_ONCE(rqst->timestamp, timestamp);
  8474. + smp_mb__after_atomic();
  8475. +
  8476. + ssh_rtl_timeout_reaper_mod(rtl, timestamp, timestamp + timeout);
  8477. +}
  8478. +
  8479. +
  8480. +static void ssh_rtl_complete(struct ssh_rtl *rtl,
  8481. + const struct ssh_command *command,
  8482. + const struct ssam_span *command_data)
  8483. +{
  8484. + struct ssh_request *r = NULL;
  8485. + struct ssh_request *p, *n;
  8486. + u16 rqid = get_unaligned_le16(&command->rqid);
  8487. +
  8488. + trace_ssam_rx_response_received(command, command_data->len);
  8489. +
  8490. + /*
  8491. + * Get request from pending based on request ID and mark it as response
  8492. + * received and locked.
  8493. + */
  8494. + spin_lock(&rtl->pending.lock);
  8495. + list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
  8496. + // we generally expect requests to be processed in order
  8497. + if (unlikely(ssh_request_get_rqid(p) != rqid))
  8498. + continue;
  8499. +
  8500. + // simulate response timeout
  8501. + if (ssh_rtl_should_drop_response()) {
  8502. + spin_unlock(&rtl->pending.lock);
  8503. +
  8504. + trace_ssam_ei_rx_drop_response(p);
  8505. + rtl_info(rtl, "request error injection: "
  8506. + "dropping response for request %p\n",
  8507. + &p->packet);
  8508. + return;
  8509. + }
  8510. +
  8511. + /*
  8512. + * Mark as "response received" and "locked" as we're going to
  8513. + * complete it. Ensure that the state doesn't get zero by
  8514. + * employing a memory barrier.
  8515. + */
  8516. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
  8517. + set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
  8518. + smp_mb__before_atomic();
  8519. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
  8520. +
  8521. + atomic_dec(&rtl->pending.count);
  8522. + list_del(&p->node);
  8523. +
  8524. + r = p;
  8525. + break;
  8526. + }
  8527. + spin_unlock(&rtl->pending.lock);
  8528. +
  8529. + if (!r) {
  8530. + rtl_warn(rtl, "rtl: dropping unexpected command message"
  8531. + " (rqid = 0x%04x)\n", rqid);
  8532. + return;
  8533. + }
  8534. +
  8535. + // if the request hasn't been completed yet, we will do this now
  8536. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
  8537. + ssh_request_put(r);
  8538. + ssh_rtl_tx_schedule(rtl);
  8539. + return;
  8540. + }
  8541. +
  8542. + /*
  8543. + * Make sure the request has been transmitted. In case of a sequenced
  8544. + * request, we are guaranteed that the completion callback will run on
  8545. + * the receiver thread directly when the ACK for the packet has been
  8546. + * received. Similarly, this function is guaranteed to run on the
  8547. + * receiver thread. Thus we are guaranteed that if the packet has been
  8548. + * successfully transmitted and received an ACK, the transmitted flag
  8549. + * has been set and is visible here.
  8550. + *
  8551. + * We are currently not handling unsequenced packets here, as those
  8552. + * should never expect a response as ensured in ssh_rtl_submit. If this
  8553. + * ever changes, one would have to test for
  8554. + *
  8555. + * (r->state & (transmitting | transmitted))
  8556. + *
  8557. + * on unsequenced packets to determine if they could have been
  8558. + * transmitted. There are no synchronization guarantees as in the
  8559. + * sequenced case, since, in this case, the callback function will not
  8560. + * run on the same thread. Thus an exact determination is impossible.
  8561. + */
  8562. + if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
  8563. + rtl_err(rtl, "rtl: received response before ACK for request"
  8564. + " (rqid = 0x%04x)\n", rqid);
  8565. +
  8566. + /*
  8567. + * NB: Timeout has already been canceled, request already been
  8568. + * removed from pending and marked as locked and completed. As
  8569. + * we receive a "false" response, the packet might still be
  8570. + * queued though.
  8571. + */
  8572. + ssh_rtl_queue_remove(r);
  8573. +
  8574. + ssh_rtl_complete_with_status(r, -EREMOTEIO);
  8575. + ssh_request_put(r);
  8576. +
  8577. + ssh_rtl_tx_schedule(rtl);
  8578. + return;
  8579. + }
  8580. +
  8581. + /*
  8582. + * NB: Timeout has already been canceled, request already been
  8583. + * removed from pending and marked as locked and completed. The request
  8584. + * can also not be queued any more, as it has been marked as
  8585. + * transmitting and later transmitted. Thus no need to remove it from
  8586. + * anywhere.
  8587. + */
  8588. +
  8589. + ssh_rtl_complete_with_rsp(r, command, command_data);
  8590. + ssh_request_put(r);
  8591. +
  8592. + ssh_rtl_tx_schedule(rtl);
  8593. +}
  8594. +
  8595. +
  8596. +static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
  8597. +{
  8598. + struct ssh_rtl *rtl;
  8599. + unsigned long state, fixed;
  8600. + bool remove;
  8601. +
  8602. + /*
  8603. + * Handle unsubmitted request: Try to mark the packet as locked,
  8604. + * expecting the state to be zero (i.e. unsubmitted). Note that, if
  8605. + * setting the state worked, we might still be adding the packet to the
  8606. + * queue in a currently executing submit call. In that case, however,
  8607. + * ptl reference must have been set previously, as locked is checked
  8608. + * after setting ptl. Thus only if we successfully lock this request and
  8609. + * ptl is NULL, we have successfully removed the request.
  8610. + * Otherwise we need to try and grab it from the queue.
  8611. + *
  8612. + * Note that if the CMPXCHG fails, we are guaranteed that ptl has
  8613. + * been set and is non-NULL, as states can only be nonzero after this
  8614. + * has been set. Also note that we need to fetch the static (type) flags
  8615. + * to ensure that they don't cause the cmpxchg to fail.
  8616. + */
  8617. + fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
  8618. + state = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
  8619. + if (!state && !READ_ONCE(r->packet.ptl)) {
  8620. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8621. + return true;
  8622. +
  8623. + ssh_rtl_complete_with_status(r, -ECANCELED);
  8624. + return true;
  8625. + }
  8626. +
  8627. + rtl = ssh_request_rtl(r);
  8628. + spin_lock(&rtl->queue.lock);
  8629. +
  8630. + /*
  8631. + * Note: 1) Requests cannot be re-submitted. 2) If a request is queued,
  8632. + * it cannot be "transmitting"/"pending" yet. Thus, if we successfully
  8633. + * remove the the request here, we have removed all its occurences in
  8634. + * the system.
  8635. + */
  8636. +
  8637. + remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
  8638. + if (!remove) {
  8639. + spin_unlock(&rtl->queue.lock);
  8640. + return false;
  8641. + }
  8642. +
  8643. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  8644. + list_del(&r->node);
  8645. +
  8646. + spin_unlock(&rtl->queue.lock);
  8647. +
  8648. + ssh_request_put(r); // drop reference obtained from queue
  8649. +
  8650. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8651. + return true;
  8652. +
  8653. + ssh_rtl_complete_with_status(r, -ECANCELED);
  8654. + return true;
  8655. +}
  8656. +
  8657. +static bool ssh_rtl_cancel_pending(struct ssh_request *r)
  8658. +{
  8659. + // if the packet is already locked, it's going to be removed shortly
  8660. + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
  8661. + return true;
  8662. +
  8663. + /*
  8664. + * Now that we have locked the packet, we have guaranteed that it can't
  8665. + * be added to the system any more. If rtl is zero, the locked
  8666. + * check in ssh_rtl_submit has not been run and any submission,
  8667. + * currently in progress or called later, won't add the packet. Thus we
  8668. + * can directly complete it.
  8669. + */
  8670. + if (!ssh_request_rtl(r)) {
  8671. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8672. + return true;
  8673. +
  8674. + ssh_rtl_complete_with_status(r, -ECANCELED);
  8675. + return true;
  8676. + }
  8677. +
  8678. + /*
  8679. + * Try to cancel the packet. If the packet has not been completed yet,
  8680. + * this will subsequently (and synchronously) call the completion
  8681. + * callback of the packet, which will complete the request.
  8682. + */
  8683. + ssh_ptl_cancel(&r->packet);
  8684. +
  8685. + /*
  8686. + * If the packet has been completed with success, i.e. has not been
  8687. + * canceled by the above call, the request may not have been completed
  8688. + * yet (may be waiting for a response). Check if we need to do this
  8689. + * here.
  8690. + */
  8691. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8692. + return true;
  8693. +
  8694. + ssh_rtl_queue_remove(r);
  8695. + ssh_rtl_pending_remove(r);
  8696. + ssh_rtl_complete_with_status(r, -ECANCELED);
  8697. +
  8698. + return true;
  8699. +}
  8700. +
  8701. +static bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
  8702. +{
  8703. + struct ssh_rtl *rtl;
  8704. + bool canceled;
  8705. +
  8706. + if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
  8707. + return true;
  8708. +
  8709. + trace_ssam_request_cancel(rqst);
  8710. +
  8711. + if (pending)
  8712. + canceled = ssh_rtl_cancel_pending(rqst);
  8713. + else
  8714. + canceled = ssh_rtl_cancel_nonpending(rqst);
  8715. +
  8716. + // note: rtl may be NULL if request has not been submitted yet
  8717. + rtl = ssh_request_rtl(rqst);
  8718. + if (canceled && rtl)
  8719. + ssh_rtl_tx_schedule(rtl);
  8720. +
  8721. + return canceled;
  8722. +}
  8723. +
  8724. +
  8725. +static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
  8726. +{
  8727. + struct ssh_request *r = to_ssh_request(p, packet);
  8728. +
  8729. + if (unlikely(status)) {
  8730. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  8731. +
  8732. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8733. + return;
  8734. +
  8735. + /*
  8736. + * The packet may get cancelled even though it has not been
  8737. + * submitted yet. The request may still be queued. Check the
  8738. + * queue and remove it if necessary. As the timeout would have
  8739. + * been started in this function on success, there's no need to
  8740. + * cancel it here.
  8741. + */
  8742. + ssh_rtl_queue_remove(r);
  8743. + ssh_rtl_pending_remove(r);
  8744. + ssh_rtl_complete_with_status(r, status);
  8745. +
  8746. + ssh_rtl_tx_schedule(ssh_request_rtl(r));
  8747. + return;
  8748. + }
  8749. +
  8750. + /*
  8751. + * Mark as transmitted, ensure that state doesn't get zero by inserting
  8752. + * a memory barrier.
  8753. + */
  8754. + set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
  8755. + smp_mb__before_atomic();
  8756. + clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
  8757. +
  8758. + // if we expect a response, we just need to start the timeout
  8759. + if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
  8760. + ssh_rtl_timeout_start(r);
  8761. + return;
  8762. + }
  8763. +
  8764. + /*
  8765. + * If we don't expect a response, lock, remove, and complete the
  8766. + * request. Note that, at this point, the request is guaranteed to have
  8767. + * left the queue and no timeout has been started. Thus we only need to
  8768. + * remove it from pending. If the request has already been completed (it
  8769. + * may have been canceled) return.
  8770. + */
  8771. +
  8772. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  8773. + if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8774. + return;
  8775. +
  8776. + ssh_rtl_pending_remove(r);
  8777. + ssh_rtl_complete_with_status(r, 0);
  8778. +
  8779. + ssh_rtl_tx_schedule(ssh_request_rtl(r));
  8780. +}
  8781. +
  8782. +
  8783. +static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeo)
  8784. +{
  8785. + ktime_t timestamp = READ_ONCE(r->timestamp);
  8786. +
  8787. + if (timestamp != KTIME_MAX)
  8788. + return ktime_add(timestamp, timeo);
  8789. + else
  8790. + return KTIME_MAX;
  8791. +}
  8792. +
  8793. +static void ssh_rtl_timeout_reap(struct work_struct *work)
  8794. +{
  8795. + struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
  8796. + struct ssh_request *r, *n;
  8797. + LIST_HEAD(claimed);
  8798. + ktime_t now = ktime_get_coarse_boottime();
  8799. + ktime_t timeout = rtl->rtx_timeout.timeout;
  8800. + ktime_t next = KTIME_MAX;
  8801. +
  8802. + trace_ssam_rtl_timeout_reap("pending", atomic_read(&rtl->pending.count));
  8803. +
  8804. + /*
  8805. + * Mark reaper as "not pending". This is done before checking any
  8806. + * requests to avoid lost-update type problems.
  8807. + */
  8808. + WRITE_ONCE(rtl->rtx_timeout.expires, KTIME_MAX);
  8809. + smp_mb__after_atomic();
  8810. +
  8811. + spin_lock(&rtl->pending.lock);
  8812. + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
  8813. + ktime_t expires = ssh_request_get_expiration(r, timeout);
  8814. +
  8815. + /*
  8816. + * Check if the timeout hasn't expired yet. Find out next
  8817. + * expiration date to be handled after this run.
  8818. + */
  8819. + if (ktime_after(expires, now)) {
  8820. + next = ktime_before(expires, next) ? expires : next;
  8821. + continue;
  8822. + }
  8823. +
  8824. + // avoid further transitions if locked
  8825. + if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
  8826. + continue;
  8827. +
  8828. + /*
  8829. + * We have now marked the packet as locked. Thus it cannot be
  8830. + * added to the pending or queued lists again after we've
  8831. + * removed it here. We can therefore re-use the node of this
  8832. + * packet temporarily.
  8833. + */
  8834. +
  8835. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
  8836. +
  8837. + atomic_dec(&rtl->pending.count);
  8838. + list_del(&r->node);
  8839. +
  8840. + list_add_tail(&r->node, &claimed);
  8841. + }
  8842. + spin_unlock(&rtl->pending.lock);
  8843. +
  8844. + // cancel and complete the request
  8845. + list_for_each_entry_safe(r, n, &claimed, node) {
  8846. + trace_ssam_request_timeout(r);
  8847. +
  8848. + /*
  8849. + * At this point we've removed the packet from pending. This
  8850. + * means that we've obtained the last (only) reference of the
  8851. + * system to it. Thus we can just complete it.
  8852. + */
  8853. + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  8854. + ssh_rtl_complete_with_status(r, -ETIMEDOUT);
  8855. +
  8856. + // drop the reference we've obtained by removing it from pending
  8857. + list_del(&r->node);
  8858. + ssh_request_put(r);
  8859. + }
  8860. +
  8861. + // ensure that reaper doesn't run again immediately
  8862. + next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
  8863. + if (next != KTIME_MAX)
  8864. + ssh_rtl_timeout_reaper_mod(rtl, now, next);
  8865. +
  8866. + ssh_rtl_tx_schedule(rtl);
  8867. +}
  8868. +
  8869. +
  8870. +static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
  8871. + const struct ssam_span *data)
  8872. +{
  8873. + trace_ssam_rx_event_received(cmd, data->len);
  8874. +
  8875. + rtl_dbg(rtl, "rtl: handling event (rqid: 0x%04x)\n",
  8876. + get_unaligned_le16(&cmd->rqid));
  8877. +
  8878. + rtl->ops.handle_event(rtl, cmd, data);
  8879. +}
  8880. +
  8881. +static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct ssam_span *data)
  8882. +{
  8883. + struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
  8884. + struct device *dev = &p->serdev->dev;
  8885. + struct ssh_command *command;
  8886. + struct ssam_span command_data;
  8887. +
  8888. + if (sshp_parse_command(dev, data, &command, &command_data))
  8889. + return;
  8890. +
  8891. + if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
  8892. + ssh_rtl_rx_event(rtl, command, &command_data);
  8893. + else
  8894. + ssh_rtl_complete(rtl, command, &command_data);
  8895. +}
  8896. +
  8897. +static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct ssam_span *data)
  8898. +{
  8899. + switch (data->ptr[0]) {
  8900. + case SSH_PLD_TYPE_CMD:
  8901. + ssh_rtl_rx_command(p, data);
  8902. + break;
  8903. +
  8904. + default:
  8905. + ptl_err(p, "rtl: rx: unknown frame payload type"
  8906. + " (type: 0x%02x)\n", data->ptr[0]);
  8907. + break;
  8908. + }
  8909. +}
  8910. +
  8911. +
  8912. +static inline struct device *ssh_rtl_get_device(struct ssh_rtl *rtl)
  8913. +{
  8914. + return ssh_ptl_get_device(&rtl->ptl);
  8915. +}
  8916. +
  8917. +static inline bool ssh_rtl_tx_flush(struct ssh_rtl *rtl)
  8918. +{
  8919. + return flush_work(&rtl->tx.work);
  8920. +}
  8921. +
  8922. +static inline int ssh_rtl_tx_start(struct ssh_rtl *rtl)
  8923. +{
  8924. + int status;
  8925. + bool sched;
  8926. +
  8927. + status = ssh_ptl_tx_start(&rtl->ptl);
  8928. + if (status)
  8929. + return status;
  8930. +
  8931. + /*
  8932. + * If the packet layer has been shut down and restarted without shutting
  8933. + * down the request layer, there may still be requests queued and not
  8934. + * handled.
  8935. + */
  8936. + spin_lock(&rtl->queue.lock);
  8937. + sched = !list_empty(&rtl->queue.head);
  8938. + spin_unlock(&rtl->queue.lock);
  8939. +
  8940. + if (sched)
  8941. + ssh_rtl_tx_schedule(rtl);
  8942. +
  8943. + return 0;
  8944. +}
  8945. +
  8946. +static inline int ssh_rtl_rx_start(struct ssh_rtl *rtl)
  8947. +{
  8948. + return ssh_ptl_rx_start(&rtl->ptl);
  8949. +}
  8950. +
  8951. +static int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
  8952. + const struct ssh_rtl_ops *ops)
  8953. +{
  8954. + struct ssh_ptl_ops ptl_ops;
  8955. + int status;
  8956. +
  8957. + ptl_ops.data_received = ssh_rtl_rx_data;
  8958. +
  8959. + status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
  8960. + if (status)
  8961. + return status;
  8962. +
  8963. + spin_lock_init(&rtl->queue.lock);
  8964. + INIT_LIST_HEAD(&rtl->queue.head);
  8965. +
  8966. + spin_lock_init(&rtl->pending.lock);
  8967. + INIT_LIST_HEAD(&rtl->pending.head);
  8968. + atomic_set_release(&rtl->pending.count, 0);
  8969. +
  8970. + INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
  8971. +
  8972. + rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
  8973. + rtl->rtx_timeout.expires = KTIME_MAX;
  8974. + INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
  8975. +
  8976. + rtl->ops = *ops;
  8977. +
  8978. + return 0;
  8979. +}
  8980. +
  8981. +static void ssh_rtl_destroy(struct ssh_rtl *rtl)
  8982. +{
  8983. + ssh_ptl_destroy(&rtl->ptl);
  8984. +}
  8985. +
  8986. +
  8987. +static void ssh_rtl_packet_release(struct ssh_packet *p)
  8988. +{
  8989. + struct ssh_request *rqst = to_ssh_request(p, packet);
  8990. + rqst->ops->release(rqst);
  8991. +}
  8992. +
  8993. +static const struct ssh_packet_ops ssh_rtl_packet_ops = {
  8994. + .complete = ssh_rtl_packet_callback,
  8995. + .release = ssh_rtl_packet_release,
  8996. +};
  8997. +
  8998. +static void ssh_request_init(struct ssh_request *rqst,
  8999. + enum ssam_request_flags flags,
  9000. + const struct ssh_request_ops *ops)
  9001. +{
  9002. + struct ssh_packet_args packet_args;
  9003. +
  9004. + packet_args.type = BIT(SSH_PACKET_TY_BLOCKING_BIT);
  9005. + if (!(flags & SSAM_REQUEST_UNSEQUENCED))
  9006. + packet_args.type |= BIT(SSH_PACKET_TY_SEQUENCED_BIT);
  9007. +
  9008. + packet_args.priority = SSH_PACKET_PRIORITY(DATA, 0);
  9009. + packet_args.ops = &ssh_rtl_packet_ops;
  9010. +
  9011. + ssh_packet_init(&rqst->packet, &packet_args);
  9012. + INIT_LIST_HEAD(&rqst->node);
  9013. +
  9014. + rqst->state = 0;
  9015. + if (flags & SSAM_REQUEST_HAS_RESPONSE)
  9016. + rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
  9017. +
  9018. + rqst->timestamp = KTIME_MAX;
  9019. + rqst->ops = ops;
  9020. +}
  9021. +
  9022. +
  9023. +struct ssh_flush_request {
  9024. + struct ssh_request base;
  9025. + struct completion completion;
  9026. + int status;
  9027. +};
  9028. +
  9029. +static void ssh_rtl_flush_request_complete(struct ssh_request *r,
  9030. + const struct ssh_command *cmd,
  9031. + const struct ssam_span *data,
  9032. + int status)
  9033. +{
  9034. + struct ssh_flush_request *rqst;
  9035. +
  9036. + rqst = container_of(r, struct ssh_flush_request, base);
  9037. + rqst->status = status;
  9038. +}
  9039. +
  9040. +static void ssh_rtl_flush_request_release(struct ssh_request *r)
  9041. +{
  9042. + struct ssh_flush_request *rqst;
  9043. +
  9044. + rqst = container_of(r, struct ssh_flush_request, base);
  9045. + complete_all(&rqst->completion);
  9046. +}
  9047. +
  9048. +static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
  9049. + .complete = ssh_rtl_flush_request_complete,
  9050. + .release = ssh_rtl_flush_request_release,
  9051. +};
  9052. +
  9053. +/**
  9054. + * ssh_rtl_flush - flush the request transmission layer
  9055. + * @rtl: request transmission layer
  9056. + * @timeout: timeout for the flush operation in jiffies
  9057. + *
  9058. + * Queue a special flush request and wait for its completion. This request
  9059. + * will be completed after all other currently queued and pending requests
  9060. + * have been completed. Instead of a normal data packet, this request submits
  9061. + * a special flush packet, meaning that upon completion, also the underlying
  9062. + * packet transmission layer has been flushed.
  9063. + *
  9064. + * Flushing the request layer gurarantees that all previously submitted
  9065. + * requests have been fully completed before this call returns. Additinally,
  9066. + * flushing blocks execution of all later submitted requests until the flush
  9067. + * has been completed.
  9068. + *
  9069. + * If the caller ensures that no new requests are submitted after a call to
  9070. + * this function, the request transmission layer is guaranteed to have no
  9071. + * remaining requests when this call returns. The same guarantee does not hold
  9072. + * for the packet layer, on which control packets may still be queued after
  9073. + * this call. See the documentation of ssh_ptl_flush for more details on
  9074. + * packet layer flushing.
  9075. + *
  9076. + * Return: Zero on success, -ETIMEDOUT if the flush timed out and has been
  9077. + * canceled as a result of the timeout, or -ESHUTDOWN if the packet and/or
  9078. + * request transmission layer has been shut down before this call. May also
  9079. + * return -EINTR if the underlying packet transmission has been interrupted.
  9080. + */
  9081. +static int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
  9082. +{
  9083. + const unsigned init_flags = SSAM_REQUEST_UNSEQUENCED;
  9084. + struct ssh_flush_request rqst;
  9085. + int status;
  9086. +
  9087. + ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
  9088. + rqst.base.packet.state |= BIT(SSH_PACKET_TY_FLUSH_BIT);
  9089. + rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
  9090. + rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
  9091. +
  9092. + init_completion(&rqst.completion);
  9093. +
  9094. + status = ssh_rtl_submit(rtl, &rqst.base);
  9095. + if (status)
  9096. + return status;
  9097. +
  9098. + ssh_request_put(&rqst.base);
  9099. +
  9100. + if (wait_for_completion_timeout(&rqst.completion, timeout))
  9101. + return 0;
  9102. +
  9103. + ssh_rtl_cancel(&rqst.base, true);
  9104. + wait_for_completion(&rqst.completion);
  9105. +
  9106. + WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED
  9107. + && rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
  9108. +
  9109. + return rqst.status == -ECANCELED ? -ETIMEDOUT : status;
  9110. +}
  9111. +
  9112. +
  9113. +static void ssh_rtl_shutdown(struct ssh_rtl *rtl)
  9114. +{
  9115. + struct ssh_request *r, *n;
  9116. + LIST_HEAD(claimed);
  9117. + int pending;
  9118. +
  9119. + set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
  9120. + smp_mb__after_atomic();
  9121. +
  9122. + // remove requests from queue
  9123. + spin_lock(&rtl->queue.lock);
  9124. + list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
  9125. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  9126. + smp_mb__before_atomic();
  9127. + clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
  9128. +
  9129. + list_del(&r->node);
  9130. + list_add_tail(&r->node, &claimed);
  9131. + }
  9132. + spin_unlock(&rtl->queue.lock);
  9133. +
  9134. + /*
  9135. + * We have now guaranteed that the queue is empty and no more new
  9136. + * requests can be submitted (i.e. it will stay empty). This means that
  9137. + * calling ssh_rtl_tx_schedule will not schedule tx.work any more. So we
  9138. + * can simply call cancel_work_sync on tx.work here and when that
  9139. + * returns, we've locked it down. This also means that after this call,
  9140. + * we don't submit any more packets to the underlying packet layer, so
  9141. + * we can also shut that down.
  9142. + */
  9143. +
  9144. + cancel_work_sync(&rtl->tx.work);
  9145. + ssh_ptl_shutdown(&rtl->ptl);
  9146. + cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
  9147. +
  9148. + /*
  9149. + * Shutting down the packet layer should also have caneled all requests.
  9150. + * Thus the pending set should be empty. Attempt to handle this
  9151. + * gracefully anyways, even though this should be dead code.
  9152. + */
  9153. +
  9154. + pending = atomic_read(&rtl->pending.count);
  9155. + if (WARN_ON(pending)) {
  9156. + spin_lock(&rtl->pending.lock);
  9157. + list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
  9158. + set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
  9159. + smp_mb__before_atomic();
  9160. + clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
  9161. +
  9162. + list_del(&r->node);
  9163. + list_add_tail(&r->node, &claimed);
  9164. + }
  9165. + spin_unlock(&rtl->pending.lock);
  9166. + }
  9167. +
  9168. + // finally cancel and complete requests
  9169. + list_for_each_entry_safe(r, n, &claimed, node) {
  9170. + // test_and_set because we still might compete with cancellation
  9171. + if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
  9172. + ssh_rtl_complete_with_status(r, -ESHUTDOWN);
  9173. +
  9174. + // drop the reference we've obtained by removing it from list
  9175. + list_del(&r->node);
  9176. + ssh_request_put(r);
  9177. + }
  9178. +}
  9179. +
  9180. +
  9181. +/* -- Event notifier/callbacks. --------------------------------------------- */
  9182. +/*
  9183. + * The notifier system is based on linux/notifier.h, specifically the SRCU
  9184. + * implementation. The difference to that is, that some bits of the notifier
  9185. + * call return value can be tracked accross multiple calls. This is done so that
  9186. + * handling of events can be tracked and a warning can be issued in case an
  9187. + * event goes unhandled. The idea of that waring is that it should help discover
  9188. + * and identify new/currently unimplemented features.
  9189. + */
  9190. +
  9191. +struct ssam_nf_head {
  9192. + struct srcu_struct srcu;
  9193. + struct ssam_notifier_block __rcu *head;
  9194. +};
  9195. +
  9196. +
  9197. +int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
  9198. +{
  9199. + struct ssam_notifier_block *nb, *next_nb;
  9200. + int ret = 0, idx;
  9201. +
  9202. + idx = srcu_read_lock(&nh->srcu);
  9203. +
  9204. + nb = rcu_dereference_raw(nh->head);
  9205. + while (nb) {
  9206. + next_nb = rcu_dereference_raw(nb->next);
  9207. +
  9208. + ret = (ret & SSAM_NOTIF_STATE_MASK) | nb->fn(nb, event);
  9209. + if (ret & SSAM_NOTIF_STOP)
  9210. + break;
  9211. +
  9212. + nb = next_nb;
  9213. + }
  9214. +
  9215. + srcu_read_unlock(&nh->srcu, idx);
  9216. + return ret;
  9217. +}
  9218. +
  9219. +/*
  9220. + * Note: This function must be synchronized by the caller with respect to other
  9221. + * insert and/or remove calls.
  9222. + */
  9223. +int __ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
  9224. +{
  9225. + struct ssam_notifier_block **link = &nh->head;
  9226. +
  9227. + while ((*link) != NULL) {
  9228. + if (unlikely((*link) == nb)) {
  9229. + WARN(1, "double register detected");
  9230. + return -EINVAL;
  9231. + }
  9232. +
  9233. + if (nb->priority > (*link)->priority)
  9234. + break;
  9235. +
  9236. + link = &((*link)->next);
  9237. + }
  9238. +
  9239. + nb->next = *link;
  9240. + rcu_assign_pointer(*link, nb);
  9241. +
  9242. + return 0;
  9243. +}
  9244. +
  9245. +/*
  9246. + * Note: This function must be synchronized by the caller with respect to other
  9247. + * insert and/or remove calls. On success, the caller _must_ ensure SRCU
  9248. + * synchronization by calling `synchronize_srcu(&nh->srcu)` after leaving the
  9249. + * critical section, to ensure that the removed notifier block is not in use any
  9250. + * more.
  9251. + */
  9252. +int __ssam_nfblk_remove(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
  9253. +{
  9254. + struct ssam_notifier_block **link = &nh->head;
  9255. +
  9256. + while ((*link) != NULL) {
  9257. + if ((*link) == nb) {
  9258. + rcu_assign_pointer(*link, nb->next);
  9259. + return 0;
  9260. + }
  9261. +
  9262. + link = &((*link)->next);
  9263. + }
  9264. +
  9265. + return -ENOENT;
  9266. +}
  9267. +
  9268. +static int ssam_nf_head_init(struct ssam_nf_head *nh)
  9269. +{
  9270. + int status;
  9271. +
  9272. + status = init_srcu_struct(&nh->srcu);
  9273. + if (status)
  9274. + return status;
  9275. +
  9276. + nh->head = NULL;
  9277. + return 0;
  9278. +}
  9279. +
  9280. +static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
  9281. +{
  9282. + cleanup_srcu_struct(&nh->srcu);
  9283. +}
  9284. +
  9285. +
  9286. +/* -- Event/notification registry. ------------------------------------------ */
  9287. +
  9288. +struct ssam_nf_refcount_key {
  9289. + struct ssam_event_registry reg;
  9290. + struct ssam_event_id id;
  9291. +};
  9292. +
  9293. +struct ssam_nf_refcount_entry {
  9294. + struct rb_node node;
  9295. + struct ssam_nf_refcount_key key;
  9296. + int refcount;
  9297. +};
  9298. +
  9299. +struct ssam_nf {
  9300. + struct mutex lock;
  9301. + struct rb_root refcount;
  9302. + struct ssam_nf_head head[SSH_NUM_EVENTS];
  9303. +};
  9304. +
  9305. +
  9306. +static int ssam_nf_refcount_inc(struct ssam_nf *nf,
  9307. + struct ssam_event_registry reg,
  9308. + struct ssam_event_id id)
  9309. +{
  9310. + struct ssam_nf_refcount_entry *entry;
  9311. + struct ssam_nf_refcount_key key;
  9312. + struct rb_node **link = &nf->refcount.rb_node;
  9313. + struct rb_node *parent = NULL;
  9314. + int cmp;
  9315. +
  9316. + key.reg = reg;
  9317. + key.id = id;
  9318. +
  9319. + while (*link) {
  9320. + entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
  9321. + parent = *link;
  9322. +
  9323. + cmp = memcmp(&key, &entry->key, sizeof(key));
  9324. + if (cmp < 0) {
  9325. + link = &(*link)->rb_left;
  9326. + } else if (cmp > 0) {
  9327. + link = &(*link)->rb_right;
  9328. + } else if (entry->refcount < INT_MAX) {
  9329. + return ++entry->refcount;
  9330. + } else {
  9331. + return -ENOSPC;
  9332. + }
  9333. + }
  9334. +
  9335. + entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  9336. + if (!entry)
  9337. + return -ENOMEM;
  9338. +
  9339. + entry->key = key;
  9340. + entry->refcount = 1;
  9341. +
  9342. + rb_link_node(&entry->node, parent, link);
  9343. + rb_insert_color(&entry->node, &nf->refcount);
  9344. +
  9345. + return entry->refcount;
  9346. +}
  9347. +
  9348. +static int ssam_nf_refcount_dec(struct ssam_nf *nf,
  9349. + struct ssam_event_registry reg,
  9350. + struct ssam_event_id id)
  9351. +{
  9352. + struct ssam_nf_refcount_entry *entry;
  9353. + struct ssam_nf_refcount_key key;
  9354. + struct rb_node *node = nf->refcount.rb_node;
  9355. + int cmp, rc;
  9356. +
  9357. + key.reg = reg;
  9358. + key.id = id;
  9359. +
  9360. + while (node) {
  9361. + entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
  9362. +
  9363. + cmp = memcmp(&key, &entry->key, sizeof(key));
  9364. + if (cmp < 0) {
  9365. + node = node->rb_left;
  9366. + } else if (cmp > 0) {
  9367. + node = node->rb_right;
  9368. + } else {
  9369. + rc = --entry->refcount;
  9370. +
  9371. + if (rc == 0) {
  9372. + rb_erase(&entry->node, &nf->refcount);
  9373. + kfree(entry);
  9374. + }
  9375. +
  9376. + return rc;
  9377. + }
  9378. + }
  9379. +
  9380. + return -ENOENT;
  9381. +}
  9382. +
  9383. +static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
  9384. + struct ssam_event *event)
  9385. +{
  9386. + struct ssam_nf_head *nf_head;
  9387. + int status, nf_ret;
  9388. +
  9389. + if (!ssh_rqid_is_event(rqid)) {
  9390. + dev_warn(dev, "event: unsupported rqid: 0x%04x\n", rqid);
  9391. + return;
  9392. + }
  9393. +
  9394. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  9395. + nf_ret = ssam_nfblk_call_chain(nf_head, event);
  9396. + status = ssam_notifier_to_errno(nf_ret);
  9397. +
  9398. + if (status < 0) {
  9399. + dev_err(dev, "event: error handling event: %d "
  9400. + "(tc: 0x%02x, cid: 0x%02x, iid: 0x%02x, chn: 0x%02x)\n",
  9401. + status, event->target_category, event->command_id,
  9402. + event->instance_id, event->channel);
  9403. + }
  9404. +
  9405. + if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
  9406. + dev_warn(dev, "event: unhandled event (rqid: 0x%02x, "
  9407. + "tc: 0x%02x, cid: 0x%02x, iid: 0x%02x, chn: 0x%02x)\n",
  9408. + rqid, event->target_category, event->command_id,
  9409. + event->instance_id, event->channel);
  9410. + }
  9411. +}
  9412. +
  9413. +static int ssam_nf_init(struct ssam_nf *nf)
  9414. +{
  9415. + int i, status;
  9416. +
  9417. + for (i = 0; i < SSH_NUM_EVENTS; i++) {
  9418. + status = ssam_nf_head_init(&nf->head[i]);
  9419. + if (status)
  9420. + break;
  9421. + }
  9422. +
  9423. + if (status) {
  9424. + for (i = i - 1; i >= 0; i--)
  9425. + ssam_nf_head_destroy(&nf->head[i]);
  9426. +
  9427. + return status;
  9428. + }
  9429. +
  9430. + mutex_init(&nf->lock);
  9431. + return 0;
  9432. +}
  9433. +
  9434. +static void ssam_nf_destroy(struct ssam_nf *nf)
  9435. +{
  9436. + int i;
  9437. +
  9438. + for (i = 0; i < SSH_NUM_EVENTS; i++)
  9439. + ssam_nf_head_destroy(&nf->head[i]);
  9440. +
  9441. + mutex_destroy(&nf->lock);
  9442. +}
  9443. +
  9444. +
  9445. +/* -- Event/async request completion system. -------------------------------- */
  9446. +
  9447. +#define SSAM_CPLT_WQ_NAME "ssam_cpltq"
  9448. +
  9449. +
  9450. +struct ssam_cplt;
  9451. +
  9452. +struct ssam_event_item {
  9453. + struct list_head node;
  9454. + u16 rqid;
  9455. + struct ssam_event event; // must be last
  9456. +};
  9457. +
  9458. +struct ssam_event_queue {
  9459. + struct ssam_cplt *cplt;
  9460. +
  9461. + spinlock_t lock;
  9462. + struct list_head head;
  9463. + struct work_struct work;
  9464. +};
  9465. +
  9466. +struct ssam_event_channel {
  9467. + struct ssam_event_queue queue[SSH_NUM_EVENTS];
  9468. +};
  9469. +
  9470. +struct ssam_cplt {
  9471. + struct device *dev;
  9472. + struct workqueue_struct *wq;
  9473. +
  9474. + struct {
  9475. + struct ssam_event_channel channel[SSH_NUM_CHANNELS];
  9476. + struct ssam_nf notif;
  9477. + } event;
  9478. +};
  9479. +
  9480. +
  9481. +static void ssam_event_queue_push(struct ssam_event_queue *q,
  9482. + struct ssam_event_item *item)
  9483. +{
  9484. + spin_lock(&q->lock);
  9485. + list_add_tail(&item->node, &q->head);
  9486. + spin_unlock(&q->lock);
  9487. +}
  9488. +
  9489. +static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
  9490. +{
  9491. + struct ssam_event_item *item;
  9492. +
  9493. + spin_lock(&q->lock);
  9494. + item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
  9495. + if (item)
  9496. + list_del(&item->node);
  9497. + spin_unlock(&q->lock);
  9498. +
  9499. + return item;
  9500. +}
  9501. +
  9502. +static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
  9503. +{
  9504. + bool empty;
  9505. +
  9506. + spin_lock(&q->lock);
  9507. + empty = list_empty(&q->head);
  9508. + spin_unlock(&q->lock);
  9509. +
  9510. + return empty;
  9511. +}
  9512. +
  9513. +static struct ssam_event_queue *ssam_cplt_get_event_queue(
  9514. + struct ssam_cplt *cplt, u8 channel, u16 rqid)
  9515. +{
  9516. + u16 event = ssh_rqid_to_event(rqid);
  9517. + u16 chidx = ssh_channel_to_index(channel);
  9518. +
  9519. + if (!ssh_rqid_is_event(rqid)) {
  9520. + dev_err(cplt->dev, "event: unsupported rqid: 0x%04x\n", rqid);
  9521. + return NULL;
  9522. + }
  9523. +
  9524. + if (!ssh_channel_is_valid(channel)) {
  9525. + dev_warn(cplt->dev, "event: unsupported channel: %u\n",
  9526. + channel);
  9527. + chidx = 0;
  9528. + }
  9529. +
  9530. + return &cplt->event.channel[chidx].queue[event];
  9531. +}
  9532. +
  9533. +static inline bool ssam_cplt_submit(struct ssam_cplt *cplt,
  9534. + struct work_struct *work)
  9535. +{
  9536. + return queue_work(cplt->wq, work);
  9537. +}
  9538. +
  9539. +static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
  9540. + struct ssam_event_item *item)
  9541. +{
  9542. + struct ssam_event_queue *evq;
  9543. +
  9544. + evq = ssam_cplt_get_event_queue(cplt, item->event.channel, item->rqid);
  9545. + if (!evq)
  9546. + return -EINVAL;
  9547. +
  9548. + ssam_event_queue_push(evq, item);
  9549. + ssam_cplt_submit(cplt, &evq->work);
  9550. + return 0;
  9551. +}
  9552. +
  9553. +static void ssam_cplt_flush(struct ssam_cplt *cplt)
  9554. +{
  9555. + flush_workqueue(cplt->wq);
  9556. +}
  9557. +
  9558. +static void ssam_event_queue_work_fn(struct work_struct *work)
  9559. +{
  9560. + struct ssam_event_queue *queue;
  9561. + struct ssam_event_item *item;
  9562. + struct ssam_nf *nf;
  9563. + struct device *dev;
  9564. + int i;
  9565. +
  9566. + queue = container_of(work, struct ssam_event_queue, work);
  9567. + nf = &queue->cplt->event.notif;
  9568. + dev = queue->cplt->dev;
  9569. +
  9570. + for (i = 0; i < 10; i++) {
  9571. + item = ssam_event_queue_pop(queue);
  9572. + if (item == NULL)
  9573. + return;
  9574. +
  9575. + ssam_nf_call(nf, dev, item->rqid, &item->event);
  9576. + kfree(item);
  9577. + }
  9578. +
  9579. + if (!ssam_event_queue_is_empty(queue))
  9580. + ssam_cplt_submit(queue->cplt, &queue->work);
  9581. +}
  9582. +
  9583. +static void ssam_event_queue_init(struct ssam_cplt *cplt,
  9584. + struct ssam_event_queue *evq)
  9585. +{
  9586. + evq->cplt = cplt;
  9587. + spin_lock_init(&evq->lock);
  9588. + INIT_LIST_HEAD(&evq->head);
  9589. + INIT_WORK(&evq->work, ssam_event_queue_work_fn);
  9590. +}
  9591. +
  9592. +static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
  9593. +{
  9594. + struct ssam_event_channel *channel;
  9595. + int status, c, i;
  9596. +
  9597. + cplt->dev = dev;
  9598. +
  9599. + cplt->wq = create_workqueue(SSAM_CPLT_WQ_NAME);
  9600. + if (!cplt->wq)
  9601. + return -ENOMEM;
  9602. +
  9603. + for (c = 0; c < ARRAY_SIZE(cplt->event.channel); c++) {
  9604. + channel = &cplt->event.channel[c];
  9605. +
  9606. + for (i = 0; i < ARRAY_SIZE(channel->queue); i++)
  9607. + ssam_event_queue_init(cplt, &channel->queue[i]);
  9608. + }
  9609. +
  9610. + status = ssam_nf_init(&cplt->event.notif);
  9611. + if (status)
  9612. + destroy_workqueue(cplt->wq);
  9613. +
  9614. + return status;
  9615. +}
  9616. +
  9617. +static void ssam_cplt_destroy(struct ssam_cplt *cplt)
  9618. +{
  9619. + destroy_workqueue(cplt->wq);
  9620. + ssam_nf_destroy(&cplt->event.notif);
  9621. +}
  9622. +
  9623. +
  9624. +/* -- Main SSAM device structures. ------------------------------------------ */
  9625. +
  9626. +enum ssam_controller_state {
  9627. + SSAM_CONTROLLER_UNINITIALIZED,
  9628. + SSAM_CONTROLLER_INITIALIZED,
  9629. + SSAM_CONTROLLER_STARTED,
  9630. + SSAM_CONTROLLER_STOPPED,
  9631. + SSAM_CONTROLLER_SUSPENDED,
  9632. +};
  9633. +
  9634. +struct ssam_device_caps {
  9635. + u32 notif_display:1;
  9636. + u32 notif_d0exit:1;
  9637. +};
  9638. +
  9639. +struct ssam_controller {
  9640. + enum ssam_controller_state state;
  9641. +
  9642. + struct ssh_rtl rtl;
  9643. + struct ssam_cplt cplt;
  9644. +
  9645. + struct {
  9646. + struct ssh_seq_counter seq;
  9647. + struct ssh_rqid_counter rqid;
  9648. + } counter;
  9649. +
  9650. + struct {
  9651. + int num;
  9652. + bool wakeup_enabled;
  9653. + } irq;
  9654. +
  9655. + struct ssam_device_caps caps;
  9656. +};
  9657. +
  9658. +
  9659. +#define ssam_dbg(ctrl, fmt, ...) rtl_dbg(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  9660. +#define ssam_info(ctrl, fmt, ...) rtl_info(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  9661. +#define ssam_warn(ctrl, fmt, ...) rtl_warn(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  9662. +#define ssam_err(ctrl, fmt, ...) rtl_err(&(ctrl)->rtl, fmt, ##__VA_ARGS__)
  9663. +
  9664. +#define to_ssam_controller(ptr, member) \
  9665. + container_of(ptr, struct ssam_controller, member)
  9666. +
  9667. +struct device *ssam_controller_device(struct ssam_controller *c)
  9668. +{
  9669. + return (c && c->rtl.ptl.serdev) ? &c->rtl.ptl.serdev->dev : NULL;
  9670. +}
  9671. +EXPORT_SYMBOL_GPL(ssam_controller_device);
  9672. +
  9673. +
  9674. +static void ssam_handle_event(struct ssh_rtl *rtl,
  9675. + const struct ssh_command *cmd,
  9676. + const struct ssam_span *data)
  9677. +{
  9678. + struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
  9679. + struct ssam_event_item *item;
  9680. +
  9681. + item = kzalloc(sizeof(struct ssam_event_item) + data->len, GFP_KERNEL);
  9682. + if (!item)
  9683. + return;
  9684. +
  9685. + item->rqid = get_unaligned_le16(&cmd->rqid);
  9686. + item->event.target_category = cmd->tc;
  9687. + item->event.command_id = cmd->cid;
  9688. + item->event.instance_id = cmd->iid;
  9689. + item->event.channel = cmd->chn_in;
  9690. + item->event.length = data->len;
  9691. + memcpy(&item->event.data[0], data->ptr, data->len);
  9692. +
  9693. + ssam_cplt_submit_event(&ctrl->cplt, item);
  9694. +}
  9695. +
  9696. +static const struct ssh_rtl_ops ssam_rtl_ops = {
  9697. + .handle_event = ssam_handle_event,
  9698. +};
  9699. +
  9700. +
  9701. +#define SSAM_SSH_DSM_REVISION 0
  9702. +#define SSAM_SSH_DSM_NOTIF_D0 8
  9703. +static const guid_t SSAM_SSH_DSM_UUID = GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
  9704. + 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
  9705. +
  9706. +static int ssam_device_caps_load_from_acpi(acpi_handle handle,
  9707. + struct ssam_device_caps *caps)
  9708. +{
  9709. + union acpi_object *obj;
  9710. + u64 funcs = 0;
  9711. + int i;
  9712. +
  9713. + // set defaults
  9714. + caps->notif_display = true;
  9715. + caps->notif_d0exit = false;
  9716. +
  9717. + if (!acpi_has_method(handle, "_DSM"))
  9718. + return 0;
  9719. +
  9720. + // get function availability bitfield
  9721. + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_UUID, 0, 0, NULL,
  9722. + ACPI_TYPE_BUFFER);
  9723. + if (!obj)
  9724. + return -EFAULT;
  9725. +
  9726. + for (i = 0; i < obj->buffer.length && i < 8; i++)
  9727. + funcs |= (((u64)obj->buffer.pointer[i]) << (i * 8));
  9728. +
  9729. + ACPI_FREE(obj);
  9730. +
  9731. + // D0 exit/entry notification
  9732. + if (funcs & BIT(SSAM_SSH_DSM_NOTIF_D0)) {
  9733. + obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_UUID,
  9734. + SSAM_SSH_DSM_REVISION, SSAM_SSH_DSM_NOTIF_D0,
  9735. + NULL, ACPI_TYPE_INTEGER);
  9736. + if (!obj)
  9737. + return -EFAULT;
  9738. +
  9739. + caps->notif_d0exit = !!obj->integer.value;
  9740. + ACPI_FREE(obj);
  9741. + }
  9742. +
  9743. + return 0;
  9744. +}
  9745. +
  9746. +static int ssam_controller_init(struct ssam_controller *ctrl,
  9747. + struct serdev_device *serdev)
  9748. +{
  9749. + acpi_handle handle = ACPI_HANDLE(&serdev->dev);
  9750. + int status;
  9751. +
  9752. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_UNINITIALIZED) {
  9753. + dev_err(&serdev->dev, "embedded controller already initialized\n");
  9754. + return -EBUSY;
  9755. + }
  9756. +
  9757. + status = ssam_device_caps_load_from_acpi(handle, &ctrl->caps);
  9758. + if (status)
  9759. + return status;
  9760. +
  9761. + dev_dbg(&serdev->dev, "device capabilities:\n");
  9762. + dev_dbg(&serdev->dev, " notif_display: %u\n", ctrl->caps.notif_display);
  9763. + dev_dbg(&serdev->dev, " notif_d0exit: %u\n", ctrl->caps.notif_d0exit);
  9764. +
  9765. + ssh_seq_reset(&ctrl->counter.seq);
  9766. + ssh_rqid_reset(&ctrl->counter.rqid);
  9767. +
  9768. + // initialize event/request completion system
  9769. + status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
  9770. + if (status)
  9771. + return status;
  9772. +
  9773. + // initialize request and packet transmission layers
  9774. + status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
  9775. + if (status) {
  9776. + ssam_cplt_flush(&ctrl->cplt);
  9777. + ssam_cplt_destroy(&ctrl->cplt);
  9778. + return status;
  9779. + }
  9780. +
  9781. + // update state
  9782. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_INITIALIZED);
  9783. + return 0;
  9784. +}
  9785. +
  9786. +static int ssam_controller_start(struct ssam_controller *ctrl)
  9787. +{
  9788. + int status;
  9789. +
  9790. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_INITIALIZED)
  9791. + return -EINVAL;
  9792. +
  9793. + status = ssh_rtl_tx_start(&ctrl->rtl);
  9794. + if (status)
  9795. + return status;
  9796. +
  9797. + status = ssh_rtl_rx_start(&ctrl->rtl);
  9798. + if (status) {
  9799. + ssh_rtl_tx_flush(&ctrl->rtl);
  9800. + return status;
  9801. + }
  9802. +
  9803. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_STARTED);
  9804. + return 0;
  9805. +}
  9806. +
  9807. +static void ssam_controller_shutdown(struct ssam_controller *ctrl)
  9808. +{
  9809. + enum ssam_controller_state s = smp_load_acquire(&ctrl->state);
  9810. + int status;
  9811. +
  9812. + if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
  9813. + return;
  9814. +
  9815. + // try to flush pending events and requests while everything still works
  9816. + status = ssh_rtl_flush(&ctrl->rtl, msecs_to_jiffies(5000));
  9817. + if (status) {
  9818. + ssam_err(ctrl, "failed to flush request transmission layer: %d\n",
  9819. + status);
  9820. + }
  9821. +
  9822. + // flush out all currently completing requests and events
  9823. + ssam_cplt_flush(&ctrl->cplt);
  9824. +
  9825. + // cancel rem. requests, ensure no new ones can be queued, stop threads
  9826. + ssh_rtl_tx_flush(&ctrl->rtl);
  9827. + ssh_rtl_shutdown(&ctrl->rtl);
  9828. +
  9829. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_STOPPED);
  9830. +}
  9831. +
  9832. +static void ssam_controller_destroy(struct ssam_controller *ctrl)
  9833. +{
  9834. + if (smp_load_acquire(&ctrl->state) == SSAM_CONTROLLER_UNINITIALIZED)
  9835. + return;
  9836. +
  9837. + /*
  9838. + * Ensure _all_ events are completed. New ones could still have been
  9839. + * received after the previous flush in ssam_controller_shutdown, before
  9840. + * the request transport layer has been shut down. At this point we can
  9841. + * be sure that no new requests will be queued for completion after this
  9842. + * call.
  9843. + */
  9844. + ssam_cplt_flush(&ctrl->cplt);
  9845. +
  9846. + // actually free resources
  9847. + ssam_cplt_destroy(&ctrl->cplt);
  9848. + ssh_rtl_destroy(&ctrl->rtl);
  9849. +
  9850. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
  9851. +}
  9852. +
  9853. +static inline int ssam_controller_suspend(struct ssam_controller *ctrl)
  9854. +{
  9855. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_STARTED)
  9856. + return -EINVAL;
  9857. +
  9858. + ssam_dbg(ctrl, "pm: suspending controller\n");
  9859. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_SUSPENDED);
  9860. + return 0;
  9861. +}
  9862. +
  9863. +static inline int ssam_controller_resume(struct ssam_controller *ctrl)
  9864. +{
  9865. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_SUSPENDED)
  9866. + return -EINVAL;
  9867. +
  9868. + ssam_dbg(ctrl, "pm: resuming controller\n");
  9869. + smp_store_release(&ctrl->state, SSAM_CONTROLLER_STARTED);
  9870. + return 0;
  9871. +}
  9872. +
  9873. +
  9874. +static inline
  9875. +int ssam_controller_receive_buf(struct ssam_controller *ctrl,
  9876. + const unsigned char *buf, size_t n)
  9877. +{
  9878. + return ssh_ptl_rx_rcvbuf(&ctrl->rtl.ptl, buf, n);
  9879. +}
  9880. +
  9881. +static inline void ssam_controller_write_wakeup(struct ssam_controller *ctrl)
  9882. +{
  9883. + ssh_ptl_tx_wakeup(&ctrl->rtl.ptl, true);
  9884. +}
  9885. +
  9886. +
  9887. +/* -- Top-level request interface ------------------------------------------- */
  9888. +
  9889. +ssize_t ssam_request_write_data(struct ssam_span *buf,
  9890. + struct ssam_controller *ctrl,
  9891. + struct ssam_request *spec)
  9892. +{
  9893. + struct msgbuf msgb;
  9894. + u16 rqid;
  9895. + u8 seq;
  9896. +
  9897. + if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
  9898. + return -EINVAL;
  9899. +
  9900. + msgb_init(&msgb, buf->ptr, buf->len);
  9901. + seq = ssh_seq_next(&ctrl->counter.seq);
  9902. + rqid = ssh_rqid_next(&ctrl->counter.rqid);
  9903. + msgb_push_cmd(&msgb, seq, rqid, spec);
  9904. +
  9905. + return msgb_bytes_used(&msgb);
  9906. +}
  9907. +EXPORT_SYMBOL_GPL(ssam_request_write_data);
  9908. +
  9909. +
  9910. +static void ssam_request_sync_complete(struct ssh_request *rqst,
  9911. + const struct ssh_command *cmd,
  9912. + const struct ssam_span *data, int status)
  9913. +{
  9914. + struct ssh_rtl *rtl = ssh_request_rtl(rqst);
  9915. + struct ssam_request_sync *r;
  9916. +
  9917. + r = container_of(rqst, struct ssam_request_sync, base);
  9918. + r->status = status;
  9919. +
  9920. + if (r->resp)
  9921. + r->resp->length = 0;
  9922. +
  9923. + if (status) {
  9924. + rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
  9925. + return;
  9926. + }
  9927. +
  9928. + if (!data) // handle requests without a response
  9929. + return;
  9930. +
  9931. + if (!r->resp || !r->resp->pointer) {
  9932. + if (data->len) {
  9933. + rtl_warn(rtl, "rsp: no response buffer provided, "
  9934. + "dropping data\n");
  9935. + }
  9936. + return;
  9937. + }
  9938. +
  9939. + if (data->len > r->resp->capacity) {
  9940. + rtl_err(rtl, "rsp: response buffer too small, "
  9941. + "capacity: %zu bytes, got: %zu bytes\n",
  9942. + r->resp->capacity, data->len);
  9943. + r->status = -ENOSPC;
  9944. + return;
  9945. + }
  9946. +
  9947. + r->resp->length = data->len;
  9948. + memcpy(r->resp->pointer, data->ptr, data->len);
  9949. +}
  9950. +
  9951. +static void ssam_request_sync_release(struct ssh_request *rqst)
  9952. +{
  9953. + complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
  9954. +}
  9955. +
  9956. +static const struct ssh_request_ops ssam_request_sync_ops = {
  9957. + .release = ssam_request_sync_release,
  9958. + .complete = ssam_request_sync_complete,
  9959. +};
  9960. +
  9961. +
  9962. +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
  9963. + struct ssam_request_sync **rqst,
  9964. + struct ssam_span *buffer)
  9965. +{
  9966. + size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
  9967. +
  9968. + *rqst = kzalloc(sizeof(struct ssam_request_sync) + msglen, flags);
  9969. + if (!*rqst)
  9970. + return -ENOMEM;
  9971. +
  9972. + buffer->ptr = (u8 *)(*rqst + 1);
  9973. + buffer->len = msglen;
  9974. +
  9975. + return 0;
  9976. +}
  9977. +EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
  9978. +
  9979. +void ssam_request_sync_init(struct ssam_request_sync *rqst,
  9980. + enum ssam_request_flags flags)
  9981. +{
  9982. + ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
  9983. + init_completion(&rqst->comp);
  9984. + rqst->resp = NULL;
  9985. + rqst->status = 0;
  9986. +}
  9987. +EXPORT_SYMBOL_GPL(ssam_request_sync_init);
  9988. +
  9989. +int ssam_request_sync_submit(struct ssam_controller *ctrl,
  9990. + struct ssam_request_sync *rqst)
  9991. +{
  9992. + enum ssam_controller_state state = smp_load_acquire(&ctrl->state);
  9993. + int status;
  9994. +
  9995. + if (state == SSAM_CONTROLLER_SUSPENDED) {
  9996. + ssam_warn(ctrl, "rqst: embedded controller is suspended\n");
  9997. + ssh_request_put(&rqst->base);
  9998. + return -EPERM;
  9999. + }
  10000. +
  10001. + if (state != SSAM_CONTROLLER_STARTED) {
  10002. + ssam_warn(ctrl, "rqst: embedded controller is uninitialized\n");
  10003. + ssh_request_put(&rqst->base);
  10004. + return -ENXIO;
  10005. + }
  10006. +
  10007. + status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
  10008. + ssh_request_put(&rqst->base);
  10009. +
  10010. + return status;
  10011. +}
  10012. +EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
  10013. +
  10014. +int ssam_request_sync(struct ssam_controller *ctrl, struct ssam_request *spec,
  10015. + struct ssam_response *rsp)
  10016. +{
  10017. + struct ssam_request_sync *rqst;
  10018. + struct ssam_span buf;
  10019. + size_t len;
  10020. + int status;
  10021. +
  10022. + // prevent overflow, allows us to skip checks later on
  10023. + if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE) {
  10024. + ssam_err(ctrl, "rqst: request payload too large\n");
  10025. + return -EINVAL;
  10026. + }
  10027. +
  10028. + status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
  10029. + if (status)
  10030. + return status;
  10031. +
  10032. + ssam_request_sync_init(rqst, spec->flags);
  10033. + ssam_request_sync_set_resp(rqst, rsp);
  10034. +
  10035. + len = ssam_request_write_data(&buf, ctrl, spec);
  10036. + ssam_request_sync_set_data(rqst, buf.ptr, len);
  10037. +
  10038. + status = ssam_request_sync_submit(ctrl, rqst);
  10039. + if (!status)
  10040. + status = ssam_request_sync_wait(rqst);
  10041. +
  10042. + kfree(rqst);
  10043. + return status;
  10044. +}
  10045. +EXPORT_SYMBOL_GPL(ssam_request_sync);
  10046. +
  10047. +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
  10048. + struct ssam_request *spec,
  10049. + struct ssam_response *rsp,
  10050. + struct ssam_span *buf)
  10051. +{
  10052. + struct ssam_request_sync rqst;
  10053. + size_t len;
  10054. + int status;
  10055. +
  10056. + // prevent overflow, allows us to skip checks later on
  10057. + if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE) {
  10058. + ssam_err(ctrl, "rqst: request payload too large\n");
  10059. + return -EINVAL;
  10060. + }
  10061. +
  10062. + ssam_request_sync_init(&rqst, spec->flags);
  10063. + ssam_request_sync_set_resp(&rqst, rsp);
  10064. +
  10065. + len = ssam_request_write_data(buf, ctrl, spec);
  10066. + ssam_request_sync_set_data(&rqst, buf->ptr, len);
  10067. +
  10068. + status = ssam_request_sync_submit(ctrl, &rqst);
  10069. + if (!status)
  10070. + status = ssam_request_sync_wait(&rqst);
  10071. +
  10072. + return status;
  10073. +}
  10074. +EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
  10075. +
  10076. +
  10077. +/* -- Internal SAM requests. ------------------------------------------------ */
  10078. +
  10079. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
  10080. + .target_category = SSAM_SSH_TC_SAM,
  10081. + .command_id = 0x13,
  10082. + .instance_id = 0x00,
  10083. + .channel = 0x01,
  10084. +});
  10085. +
  10086. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
  10087. + .target_category = SSAM_SSH_TC_SAM,
  10088. + .command_id = 0x15,
  10089. + .instance_id = 0x00,
  10090. + .channel = 0x01,
  10091. +});
  10092. +
  10093. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
  10094. + .target_category = SSAM_SSH_TC_SAM,
  10095. + .command_id = 0x16,
  10096. + .instance_id = 0x00,
  10097. + .channel = 0x01,
  10098. +});
  10099. +
  10100. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
  10101. + .target_category = SSAM_SSH_TC_SAM,
  10102. + .command_id = 0x33,
  10103. + .instance_id = 0x00,
  10104. + .channel = 0x01,
  10105. +});
  10106. +
  10107. +static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
  10108. + .target_category = SSAM_SSH_TC_SAM,
  10109. + .command_id = 0x34,
  10110. + .instance_id = 0x00,
  10111. + .channel = 0x01,
  10112. +});
  10113. +
  10114. +static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
  10115. + struct ssam_event_registry reg,
  10116. + struct ssam_event_id id, u8 flags)
  10117. +{
  10118. + struct ssh_notification_params params;
  10119. + struct ssam_request rqst;
  10120. + struct ssam_response result;
  10121. + int status;
  10122. +
  10123. + u16 rqid = ssh_tc_to_rqid(id.target_category);
  10124. + u8 buf[1] = { 0x00 };
  10125. +
  10126. + // only allow RQIDs that lie within event spectrum
  10127. + if (!ssh_rqid_is_event(rqid))
  10128. + return -EINVAL;
  10129. +
  10130. + params.target_category = id.target_category;
  10131. + params.instance_id = id.instance;
  10132. + params.flags = flags;
  10133. + put_unaligned_le16(rqid, &params.request_id);
  10134. +
  10135. + rqst.target_category = reg.target_category;
  10136. + rqst.command_id = reg.cid_enable;
  10137. + rqst.instance_id = 0x00;
  10138. + rqst.channel = reg.channel;
  10139. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  10140. + rqst.length = sizeof(params);
  10141. + rqst.payload = (u8 *)&params;
  10142. +
  10143. + result.capacity = ARRAY_SIZE(buf);
  10144. + result.length = 0;
  10145. + result.pointer = buf;
  10146. +
  10147. + status = ssam_request_sync_onstack(ctrl, &rqst, &result, sizeof(params));
  10148. + if (status) {
  10149. + ssam_err(ctrl, "failed to enable event source "
  10150. + "(tc: 0x%02x, iid: 0x%02x, reg: 0x%02x)\n",
  10151. + id.target_category, id.instance, reg.target_category);
  10152. + }
  10153. +
  10154. + if (buf[0] != 0x00) {
  10155. + ssam_warn(ctrl, "unexpected result while enabling event source: "
  10156. + "0x%02x (tc: 0x%02x, iid: 0x%02x, reg: 0x%02x)\n",
  10157. + buf[0], id.target_category, id.instance,
  10158. + reg.target_category);
  10159. + }
  10160. +
  10161. + return status;
  10162. +
  10163. +}
  10164. +
  10165. +static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
  10166. + struct ssam_event_registry reg,
  10167. + struct ssam_event_id id, u8 flags)
  10168. +{
  10169. + struct ssh_notification_params params;
  10170. + struct ssam_request rqst;
  10171. + struct ssam_response result;
  10172. + int status;
  10173. +
  10174. + u16 rqid = ssh_tc_to_rqid(id.target_category);
  10175. + u8 buf[1] = { 0x00 };
  10176. +
  10177. + // only allow RQIDs that lie within event spectrum
  10178. + if (!ssh_rqid_is_event(rqid))
  10179. + return -EINVAL;
  10180. +
  10181. + params.target_category = id.target_category;
  10182. + params.instance_id = id.instance;
  10183. + params.flags = flags;
  10184. + put_unaligned_le16(rqid, &params.request_id);
  10185. +
  10186. + rqst.target_category = reg.target_category;
  10187. + rqst.command_id = reg.cid_disable;
  10188. + rqst.instance_id = 0x00;
  10189. + rqst.channel = reg.channel;
  10190. + rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  10191. + rqst.length = sizeof(params);
  10192. + rqst.payload = (u8 *)&params;
  10193. +
  10194. + result.capacity = ARRAY_SIZE(buf);
  10195. + result.length = 0;
  10196. + result.pointer = buf;
  10197. +
  10198. + status = ssam_request_sync_onstack(ctrl, &rqst, &result, sizeof(params));
  10199. + if (status) {
  10200. + ssam_err(ctrl, "failed to disable event source "
  10201. + "(tc: 0x%02x, iid: 0x%02x, reg: 0x%02x)\n",
  10202. + id.target_category, id.instance, reg.target_category);
  10203. + }
  10204. +
  10205. + if (buf[0] != 0x00) {
  10206. + ssam_warn(ctrl, "unexpected result while disabling event source: "
  10207. + "0x%02x (tc: 0x%02x, iid: 0x%02x, reg: 0x%02x)\n",
  10208. + buf[0], id.target_category, id.instance,
  10209. + reg.target_category);
  10210. + }
  10211. +
  10212. + return status;
  10213. +}
  10214. +
  10215. +
  10216. +/* -- Wrappers for internal SAM requests. ----------------------------------- */
  10217. +
  10218. +static int ssam_log_firmware_version(struct ssam_controller *ctrl)
  10219. +{
  10220. + __le32 __version;
  10221. + u32 version, a, b, c;
  10222. + int status;
  10223. +
  10224. + status = ssam_ssh_get_firmware_version(ctrl, &__version);
  10225. + if (status)
  10226. + return status;
  10227. +
  10228. + version = le32_to_cpu(__version);
  10229. + a = (version >> 24) & 0xff;
  10230. + b = ((version >> 8) & 0xffff);
  10231. + c = version & 0xff;
  10232. +
  10233. + ssam_info(ctrl, "SAM controller version: %u.%u.%u\n", a, b, c);
  10234. + return 0;
  10235. +}
  10236. +
  10237. +static int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
  10238. +{
  10239. + int status;
  10240. + u8 response;
  10241. +
  10242. + if (!ctrl->caps.notif_display)
  10243. + return 0;
  10244. +
  10245. + ssam_dbg(ctrl, "pm: notifying display off\n");
  10246. +
  10247. + status = ssam_ssh_notif_display_off(ctrl, &response);
  10248. + if (status)
  10249. + return status;
  10250. +
  10251. + if (response != 0) {
  10252. + ssam_err(ctrl, "unexpected response from display-off notification: "
  10253. + "0x%02x\n", response);
  10254. + return -EIO;
  10255. + }
  10256. +
  10257. + return 0;
  10258. +}
  10259. +
  10260. +static int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
  10261. +{
  10262. + int status;
  10263. + u8 response;
  10264. +
  10265. + if (!ctrl->caps.notif_display)
  10266. + return 0;
  10267. +
  10268. + ssam_dbg(ctrl, "pm: notifying display on\n");
  10269. +
  10270. + status = ssam_ssh_notif_display_on(ctrl, &response);
  10271. + if (status)
  10272. + return status;
  10273. +
  10274. + if (response != 0) {
  10275. + ssam_err(ctrl, "unexpected response from display-on notification: "
  10276. + "0x%02x\n", response);
  10277. + return -EIO;
  10278. + }
  10279. +
  10280. + return 0;
  10281. +}
  10282. +
  10283. +static int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
  10284. +{
  10285. + int status;
  10286. + u8 response;
  10287. +
  10288. + if (!ctrl->caps.notif_d0exit)
  10289. + return 0;
  10290. +
  10291. + ssam_dbg(ctrl, "pm: notifying D0 exit\n");
  10292. +
  10293. + status = ssam_ssh_notif_d0_exit(ctrl, &response);
  10294. + if (status)
  10295. + return status;
  10296. +
  10297. + if (response != 0) {
  10298. + ssam_err(ctrl, "unexpected response from D0-exit notification: "
  10299. + "0x%02x\n", response);
  10300. + return -EIO;
  10301. + }
  10302. +
  10303. + return 0;
  10304. +}
  10305. +
  10306. +static int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
  10307. +{
  10308. + int status;
  10309. + u8 response;
  10310. +
  10311. + if (!ctrl->caps.notif_d0exit)
  10312. + return 0;
  10313. +
  10314. + ssam_dbg(ctrl, "pm: notifying D0 entry\n");
  10315. +
  10316. + status = ssam_ssh_notif_d0_entry(ctrl, &response);
  10317. + if (status)
  10318. + return status;
  10319. +
  10320. + if (response != 0) {
  10321. + ssam_err(ctrl, "unexpected response from D0-entry notification: "
  10322. + "0x%02x\n", response);
  10323. + return -EIO;
  10324. + }
  10325. +
  10326. + return 0;
  10327. +}
  10328. +
  10329. +
  10330. +/* -- Top-level event registry interface. ----------------------------------- */
  10331. +
  10332. +int ssam_notifier_register(struct ssam_controller *ctrl,
  10333. + struct ssam_event_notifier *n)
  10334. +{
  10335. + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
  10336. + struct ssam_nf_head *nf_head;
  10337. + struct ssam_nf *nf;
  10338. + int rc, status;
  10339. +
  10340. + if (!ssh_rqid_is_event(rqid))
  10341. + return -EINVAL;
  10342. +
  10343. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_STARTED)
  10344. + return -ENXIO;
  10345. +
  10346. + nf = &ctrl->cplt.event.notif;
  10347. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  10348. +
  10349. + mutex_lock(&nf->lock);
  10350. +
  10351. + rc = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
  10352. + if (rc < 0) {
  10353. + mutex_unlock(&nf->lock);
  10354. + return rc;
  10355. + }
  10356. +
  10357. + ssam_dbg(ctrl, "enabling event (reg: 0x%02x, tc: 0x%02x, iid: 0x%02x, "
  10358. + "rc: %d)\n", n->event.reg.target_category,
  10359. + n->event.id.target_category, n->event.id.instance, rc);
  10360. +
  10361. + status = __ssam_nfblk_insert(nf_head, &n->base);
  10362. + if (status) {
  10363. + ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
  10364. + mutex_unlock(&nf->lock);
  10365. + return status;
  10366. + }
  10367. +
  10368. + if (rc == 1) {
  10369. + status = ssam_ssh_event_enable(ctrl, n->event.reg, n->event.id,
  10370. + n->event.flags);
  10371. + if (status) {
  10372. + __ssam_nfblk_remove(nf_head, &n->base);
  10373. + ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
  10374. + mutex_unlock(&nf->lock);
  10375. + synchronize_srcu(&nf_head->srcu);
  10376. + return status;
  10377. + }
  10378. + }
  10379. +
  10380. + mutex_unlock(&nf->lock);
  10381. + return 0;
  10382. +
  10383. +}
  10384. +EXPORT_SYMBOL_GPL(ssam_notifier_register);
  10385. +
  10386. +int ssam_notifier_unregister(struct ssam_controller *ctrl,
  10387. + struct ssam_event_notifier *n)
  10388. +{
  10389. + u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
  10390. + struct ssam_nf_head *nf_head;
  10391. + struct ssam_nf *nf;
  10392. + int rc, status = 0;
  10393. +
  10394. + if (!ssh_rqid_is_event(rqid))
  10395. + return -EINVAL;
  10396. +
  10397. + if (smp_load_acquire(&ctrl->state) != SSAM_CONTROLLER_STARTED)
  10398. + return -ENXIO;
  10399. +
  10400. + nf = &ctrl->cplt.event.notif;
  10401. + nf_head = &nf->head[ssh_rqid_to_event(rqid)];
  10402. +
  10403. + mutex_lock(&nf->lock);
  10404. +
  10405. + rc = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
  10406. + if (rc < 0) {
  10407. + mutex_unlock(&nf->lock);
  10408. + return rc;
  10409. + }
  10410. +
  10411. + ssam_dbg(ctrl, "disabling event (reg: 0x%02x, tc: 0x%02x, iid: 0x%02x, "
  10412. + "rc: %d)\n", n->event.reg.target_category,
  10413. + n->event.id.target_category, n->event.id.instance, rc);
  10414. +
  10415. + if (rc == 0) {
  10416. + status = ssam_ssh_event_disable(ctrl, n->event.reg, n->event.id,
  10417. + n->event.flags);
  10418. + }
  10419. +
  10420. + __ssam_nfblk_remove(nf_head, &n->base);
  10421. + mutex_unlock(&nf->lock);
  10422. + synchronize_srcu(&nf_head->srcu);
  10423. +
  10424. + return status;
  10425. +}
  10426. +EXPORT_SYMBOL_GPL(ssam_notifier_unregister);
  10427. +
  10428. +
  10429. +/* -- Wakeup IRQ. ----------------------------------------------------------- */
  10430. +
  10431. +static const struct acpi_gpio_params gpio_ssam_wakeup_int = { 0, 0, false };
  10432. +static const struct acpi_gpio_params gpio_ssam_wakeup = { 1, 0, false };
  10433. +
  10434. +static const struct acpi_gpio_mapping ssam_acpi_gpios[] = {
  10435. + { "ssam_wakeup-int-gpio", &gpio_ssam_wakeup_int, 1 },
  10436. + { "ssam_wakeup-gpio", &gpio_ssam_wakeup, 1 },
  10437. + { },
  10438. +};
  10439. +
  10440. +static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
  10441. +{
  10442. + struct ssam_controller *ctrl = dev_id;
  10443. +
  10444. + ssam_dbg(ctrl, "pm: wake irq triggered\n");
  10445. +
  10446. + // Note: Proper wakeup detection is currently unimplemented.
  10447. + // When the EC is in display-off or any other non-D0 state, it
  10448. + // does not send events/notifications to the host. Instead it
  10449. + // signals that there are events available via the wakeup IRQ.
  10450. + // This driver is responsible for calling back to the EC to
  10451. + // release these events one-by-one.
  10452. + //
  10453. + // This IRQ should not cause a full system resume by its own.
  10454. + // Instead, events should be handled by their respective subsystem
  10455. + // drivers, which in turn should signal whether a full system
  10456. + // resume should be performed.
  10457. + //
  10458. + // TODO: Send GPIO callback command repeatedly to EC until callback
  10459. + // returns 0x00. Return flag of callback is "has more events".
  10460. + // Each time the command is sent, one event is "released". Once
  10461. + // all events have been released (return = 0x00), the GPIO is
  10462. + // re-armed. Detect wakeup events during this process, go back to
  10463. + // sleep if no wakeup event has been received.
  10464. +
  10465. + return IRQ_HANDLED;
  10466. +}
  10467. +
  10468. +static int ssam_irq_setup(struct ssam_controller *ctrl)
  10469. +{
  10470. + const int irqf = IRQF_SHARED | IRQF_ONESHOT;
  10471. + struct device *dev = ssam_controller_device(ctrl);
  10472. + struct gpio_desc *gpiod;
  10473. + int irq;
  10474. + int status;
  10475. +
  10476. + gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
  10477. + if (IS_ERR(gpiod))
  10478. + return PTR_ERR(gpiod);
  10479. +
  10480. + irq = gpiod_to_irq(gpiod);
  10481. + gpiod_put(gpiod);
  10482. +
  10483. + if (irq < 0)
  10484. + return irq;
  10485. +
  10486. + status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
  10487. + "surface_sam_wakeup", ctrl);
  10488. + if (status)
  10489. + return status;
  10490. +
  10491. + ctrl->irq.num = irq;
  10492. + return 0;
  10493. +}
  10494. +
  10495. +static void ssam_irq_free(struct ssam_controller *ctrl)
  10496. +{
  10497. + free_irq(ctrl->irq.num, ctrl);
  10498. + ctrl->irq.num = -1;
  10499. +}
  10500. +
  10501. +
  10502. +/* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
  10503. +
  10504. +static int ssam_receive_buf(struct serdev_device *dev, const unsigned char *buf,
  10505. + size_t n)
  10506. +{
  10507. + struct ssam_controller *ctrl = serdev_device_get_drvdata(dev);
  10508. + return ssam_controller_receive_buf(ctrl, buf, n);
  10509. +}
  10510. +
  10511. +static void ssam_write_wakeup(struct serdev_device *dev)
  10512. +{
  10513. + struct ssam_controller *ctrl = serdev_device_get_drvdata(dev);
  10514. + ssam_controller_write_wakeup(ctrl);
  10515. +}
  10516. +
  10517. +static const struct serdev_device_ops ssam_serdev_ops = {
  10518. + .receive_buf = ssam_receive_buf,
  10519. + .write_wakeup = ssam_write_wakeup,
  10520. +};
  10521. +
  10522. +
  10523. +/* -- ACPI based device setup. ---------------------------------------------- */
  10524. +
  10525. +static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc,
  10526. + void *ctx)
  10527. +{
  10528. + struct serdev_device *serdev = ctx;
  10529. + struct acpi_resource_common_serialbus *serial;
  10530. + struct acpi_resource_uart_serialbus *uart;
  10531. + bool flow_control;
  10532. + int status = 0;
  10533. +
  10534. + if (rsc->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  10535. + return AE_OK;
  10536. +
  10537. + serial = &rsc->data.common_serial_bus;
  10538. + if (serial->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  10539. + return AE_OK;
  10540. +
  10541. + uart = &rsc->data.uart_serial_bus;
  10542. +
  10543. + // set up serdev device
  10544. + serdev_device_set_baudrate(serdev, uart->default_baud_rate);
  10545. +
  10546. + // serdev currently only supports RTSCTS flow control
  10547. + if (uart->flow_control & (~((u8) ACPI_UART_FLOW_CONTROL_HW))) {
  10548. + dev_warn(&serdev->dev, "setup: unsupported flow control"
  10549. + " (value: 0x%02x)\n", uart->flow_control);
  10550. + }
  10551. +
  10552. + // set RTSCTS flow control
  10553. + flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;
  10554. + serdev_device_set_flow_control(serdev, flow_control);
  10555. +
  10556. + // serdev currently only supports EVEN/ODD parity
  10557. + switch (uart->parity) {
  10558. + case ACPI_UART_PARITY_NONE:
  10559. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
  10560. + break;
  10561. + case ACPI_UART_PARITY_EVEN:
  10562. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);
  10563. + break;
  10564. + case ACPI_UART_PARITY_ODD:
  10565. + status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);
  10566. + break;
  10567. + default:
  10568. + dev_warn(&serdev->dev, "setup: unsupported parity"
  10569. + " (value: 0x%02x)\n", uart->parity);
  10570. + break;
  10571. + }
  10572. +
  10573. + if (status) {
  10574. + dev_err(&serdev->dev, "setup: failed to set parity"
  10575. + " (value: 0x%02x)\n", uart->parity);
  10576. + return status;
  10577. + }
  10578. +
  10579. + return AE_CTRL_TERMINATE; // we've found the resource and are done
  10580. +}
  10581. +
  10582. +static acpi_status ssam_serdev_setup_via_acpi(acpi_handle handle,
  10583. + struct serdev_device *serdev)
  10584. +{
  10585. + return acpi_walk_resources(handle, METHOD_NAME__CRS,
  10586. + ssam_serdev_setup_via_acpi_crs, serdev);
  10587. +}
  10588. +
  10589. +
  10590. +/* -- Power management. ----------------------------------------------------- */
  10591. +
  10592. +static void surface_sam_ssh_shutdown(struct device *dev)
  10593. +{
  10594. + struct ssam_controller *c = dev_get_drvdata(dev);
  10595. + int status;
  10596. +
  10597. + /*
  10598. + * Try to signal display-off and D0-exit, ignore any errors.
  10599. + *
  10600. + * Note: It has not been established yet if this is actually
  10601. + * necessary/useful for shutdown.
  10602. + */
  10603. +
  10604. + status = ssam_ctrl_notif_display_off(c);
  10605. + if (status)
  10606. + ssam_err(c, "pm: display-off notification failed: %d\n", status);
  10607. +
  10608. + status = ssam_ctrl_notif_d0_exit(c);
  10609. + if (status)
  10610. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  10611. +}
  10612. +
  10613. +static int surface_sam_ssh_suspend(struct device *dev)
  10614. +{
  10615. + struct ssam_controller *c = dev_get_drvdata(dev);
  10616. + int status;
  10617. +
  10618. + /*
  10619. + * Try to signal display-off and D0-exit, enable IRQ wakeup if
  10620. + * specified. Abort on error.
  10621. + *
  10622. + * Note: Signalling display-off/display-on should normally be done from
  10623. + * some sort of display state notifier. As that is not available, signal
  10624. + * it here.
  10625. + */
  10626. +
  10627. + status = ssam_ctrl_notif_display_off(c);
  10628. + if (status) {
  10629. + ssam_err(c, "pm: display-off notification failed: %d\n", status);
  10630. + return status;
  10631. + }
  10632. +
  10633. + status = ssam_ctrl_notif_d0_exit(c);
  10634. + if (status) {
  10635. + ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
  10636. + goto err_notif;
  10637. + }
  10638. +
  10639. + if (device_may_wakeup(dev)) {
  10640. + status = enable_irq_wake(c->irq.num);
  10641. + if (status) {
  10642. + ssam_err(c, "failed to disable wake IRQ: %d\n", status);
  10643. + goto err_irq;
  10644. + }
  10645. +
  10646. + c->irq.wakeup_enabled = true;
  10647. + } else {
  10648. + c->irq.wakeup_enabled = false;
  10649. + }
  10650. +
  10651. + WARN_ON(ssam_controller_suspend(c));
  10652. + return 0;
  10653. +
  10654. +err_irq:
  10655. + ssam_ctrl_notif_d0_entry(c);
  10656. +err_notif:
  10657. + ssam_ctrl_notif_display_on(c);
  10658. + return status;
  10659. +}
  10660. +
  10661. +static int surface_sam_ssh_resume(struct device *dev)
  10662. +{
  10663. + struct ssam_controller *c = dev_get_drvdata(dev);
  10664. + int status;
  10665. +
  10666. + WARN_ON(ssam_controller_resume(c));
  10667. +
  10668. + /*
  10669. + * Try to disable IRQ wakeup (if specified), signal display-on and
  10670. + * D0-entry. In case of errors, log them and try to restore normal
  10671. + * operation state as far as possible.
  10672. + *
  10673. + * Note: Signalling display-off/display-on should normally be done from
  10674. + * some sort of display state notifier. As that is not available, signal
  10675. + * it here.
  10676. + */
  10677. +
  10678. + if (c->irq.wakeup_enabled) {
  10679. + status = disable_irq_wake(c->irq.num);
  10680. + if (status)
  10681. + ssam_err(c, "failed to disable wake IRQ: %d\n", status);
  10682. +
  10683. + c->irq.wakeup_enabled = false;
  10684. + }
  10685. +
  10686. + status = ssam_ctrl_notif_d0_entry(c);
  10687. + if (status)
  10688. + ssam_err(c, "pm: display-on notification failed: %d\n", status);
  10689. +
  10690. + status = ssam_ctrl_notif_display_on(c);
  10691. + if (status)
  10692. + ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
  10693. +
  10694. + return 0;
  10695. +}
  10696. +
  10697. +static SIMPLE_DEV_PM_OPS(surface_sam_ssh_pm_ops, surface_sam_ssh_suspend,
  10698. + surface_sam_ssh_resume);
  10699. +
  10700. +
  10701. +/* -- Device/driver setup. -------------------------------------------------- */
  10702. +
  10703. +static struct ssam_controller ssam_controller = {
  10704. + .state = SSAM_CONTROLLER_UNINITIALIZED,
  10705. +};
  10706. +static DEFINE_MUTEX(ssam_controller_lock);
  10707. +
  10708. +static int __ssam_client_link(struct ssam_controller *c, struct device *client)
  10709. +{
  10710. + const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  10711. + struct device_link *link;
  10712. + struct device *ctrldev;
  10713. +
  10714. + if (smp_load_acquire(&c->state) != SSAM_CONTROLLER_STARTED)
  10715. + return -ENXIO;
  10716. +
  10717. + if ((ctrldev = ssam_controller_device(c)) == NULL)
  10718. + return -ENXIO;
  10719. +
  10720. + if ((link = device_link_add(client, ctrldev, flags)) == NULL)
  10721. + return -ENOMEM;
  10722. +
  10723. + /*
  10724. + * Return -ENXIO if supplier driver is on its way to be removed. In this
  10725. + * case, the controller won't be around for much longer and the device
  10726. + * link is not going to save us any more, as unbinding is already in
  10727. + * progress.
  10728. + */
  10729. + if (link->status == DL_STATE_SUPPLIER_UNBIND)
  10730. + return -ENXIO;
  10731. +
  10732. + return 0;
  10733. +}
  10734. +
  10735. +int ssam_client_bind(struct device *client, struct ssam_controller **ctrl)
  10736. +{
  10737. + struct ssam_controller *c = &ssam_controller;
  10738. + int status;
  10739. +
  10740. + mutex_lock(&ssam_controller_lock);
  10741. + status = __ssam_client_link(c, client);
  10742. + mutex_unlock(&ssam_controller_lock);
  10743. +
  10744. + *ctrl = status == 0 ? c : NULL;
  10745. + return status;
  10746. +}
  10747. +EXPORT_SYMBOL_GPL(ssam_client_bind);
  10748. +
  10749. +
  10750. +static int surface_sam_ssh_probe(struct serdev_device *serdev)
  10751. +{
  10752. + struct ssam_controller *ctrl = &ssam_controller;
  10753. + acpi_handle *ssh = ACPI_HANDLE(&serdev->dev);
  10754. + int status;
  10755. +
  10756. + if (gpiod_count(&serdev->dev, NULL) < 0)
  10757. + return -ENODEV;
  10758. +
  10759. + status = devm_acpi_dev_add_driver_gpios(&serdev->dev, ssam_acpi_gpios);
  10760. + if (status)
  10761. + return status;
  10762. +
  10763. + // set up EC
  10764. + mutex_lock(&ssam_controller_lock);
  10765. +
  10766. + // initialize controller
  10767. + status = ssam_controller_init(ctrl, serdev);
  10768. + if (status)
  10769. + goto err_ctrl_init;
  10770. +
  10771. + // set up serdev device
  10772. + serdev_device_set_drvdata(serdev, ctrl);
  10773. + serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
  10774. + status = serdev_device_open(serdev);
  10775. + if (status)
  10776. + goto err_devopen;
  10777. +
  10778. + status = ssam_serdev_setup_via_acpi(ssh, serdev);
  10779. + if (ACPI_FAILURE(status))
  10780. + goto err_devinit;
  10781. +
  10782. + // start controller
  10783. + status = ssam_controller_start(ctrl);
  10784. + if (status)
  10785. + goto err_devinit;
  10786. +
  10787. + // initial SAM requests: log version, notify default/init power states
  10788. + status = ssam_log_firmware_version(ctrl);
  10789. + if (status)
  10790. + goto err_initrq;
  10791. +
  10792. + status = ssam_ctrl_notif_d0_entry(ctrl);
  10793. + if (status)
  10794. + goto err_initrq;
  10795. +
  10796. + status = ssam_ctrl_notif_display_on(ctrl);
  10797. + if (status)
  10798. + goto err_initrq;
  10799. +
  10800. + // setup IRQ
  10801. + status = ssam_irq_setup(ctrl);
  10802. + if (status)
  10803. + goto err_initrq;
  10804. +
  10805. + mutex_unlock(&ssam_controller_lock);
  10806. +
  10807. + /*
  10808. + * TODO: The EC can wake up the system via the associated GPIO interrupt
  10809. + * in multiple situations. One of which is the remaining battery
  10810. + * capacity falling below a certain threshold. Normally, we should
  10811. + * use the device_init_wakeup function, however, the EC also seems
  10812. + * to have other reasons for waking up the system and it seems
  10813. + * that Windows has additional checks whether the system should be
  10814. + * resumed. In short, this causes some spurious unwanted wake-ups.
  10815. + * For now let's thus default power/wakeup to false.
  10816. + */
  10817. + device_set_wakeup_capable(&serdev->dev, true);
  10818. + acpi_walk_dep_device_list(ssh);
  10819. +
  10820. + return 0;
  10821. +
  10822. +err_initrq:
  10823. + ssam_controller_shutdown(ctrl);
  10824. +err_devinit:
  10825. + serdev_device_close(serdev);
  10826. +err_devopen:
  10827. + ssam_controller_destroy(ctrl);
  10828. +err_ctrl_init:
  10829. + serdev_device_set_drvdata(serdev, NULL);
  10830. + mutex_unlock(&ssam_controller_lock);
  10831. + return status;
  10832. +}
  10833. +
  10834. +static void surface_sam_ssh_remove(struct serdev_device *serdev)
  10835. +{
  10836. + struct ssam_controller *ctrl = serdev_device_get_drvdata(serdev);
  10837. + int status;
  10838. +
  10839. + mutex_lock(&ssam_controller_lock);
  10840. + ssam_irq_free(ctrl);
  10841. +
  10842. + // suspend EC and disable events
  10843. + status = ssam_ctrl_notif_display_off(ctrl);
  10844. + if (status) {
  10845. + dev_err(&serdev->dev, "display-off notification failed: %d\n",
  10846. + status);
  10847. + }
  10848. +
  10849. + status = ssam_ctrl_notif_d0_exit(ctrl);
  10850. + if (status) {
  10851. + dev_err(&serdev->dev, "D0-exit notification failed: %d\n",
  10852. + status);
  10853. + }
  10854. +
  10855. + ssam_controller_shutdown(ctrl);
  10856. +
  10857. + // shut down actual transport
  10858. + serdev_device_wait_until_sent(serdev, 0);
  10859. + serdev_device_close(serdev);
  10860. +
  10861. + ssam_controller_destroy(ctrl);
  10862. +
  10863. + device_set_wakeup_capable(&serdev->dev, false);
  10864. + serdev_device_set_drvdata(serdev, NULL);
  10865. + mutex_unlock(&ssam_controller_lock);
  10866. +}
  10867. +
  10868. +
  10869. +static const struct acpi_device_id surface_sam_ssh_match[] = {
  10870. + { "MSHW0084", 0 },
  10871. + { },
  10872. +};
  10873. +MODULE_DEVICE_TABLE(acpi, surface_sam_ssh_match);
  10874. +
  10875. +static struct serdev_device_driver surface_sam_ssh = {
  10876. + .probe = surface_sam_ssh_probe,
  10877. + .remove = surface_sam_ssh_remove,
  10878. + .driver = {
  10879. + .name = "surface_sam_ssh",
  10880. + .acpi_match_table = surface_sam_ssh_match,
  10881. + .pm = &surface_sam_ssh_pm_ops,
  10882. + .shutdown = surface_sam_ssh_shutdown,
  10883. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  10884. + },
  10885. +};
  10886. +
  10887. +
  10888. +/* -- Module setup. --------------------------------------------------------- */
  10889. +
  10890. +static int __init surface_sam_ssh_init(void)
  10891. +{
  10892. + return serdev_device_driver_register(&surface_sam_ssh);
  10893. +}
  10894. +
  10895. +static void __exit surface_sam_ssh_exit(void)
  10896. +{
  10897. + serdev_device_driver_unregister(&surface_sam_ssh);
  10898. +}
  10899. +
  10900. +/*
  10901. + * Ensure that the driver is loaded late due to some issues with the UART
  10902. + * communication. Specifically, we want to ensure that DMA is ready and being
  10903. + * used. Not using DMA can result in spurious communication failures,
  10904. + * especially during boot, which among other things will result in wrong
  10905. + * battery information (via ACPI _BIX) being displayed. Using a late init_call
  10906. + * instead of the normal module_init gives the DMA subsystem time to
  10907. + * initialize and via that results in a more stable communication, avoiding
  10908. + * such failures.
  10909. + */
  10910. +late_initcall(surface_sam_ssh_init);
  10911. +module_exit(surface_sam_ssh_exit);
  10912. +
  10913. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  10914. +MODULE_DESCRIPTION("Surface Serial Hub Driver for 5th Generation Surface Devices");
  10915. +MODULE_LICENSE("GPL");
  10916. diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh.h b/drivers/platform/x86/surface_sam/surface_sam_ssh.h
  10917. new file mode 100644
  10918. index 0000000000000..ba57adb2a3c9d
  10919. --- /dev/null
  10920. +++ b/drivers/platform/x86/surface_sam/surface_sam_ssh.h
  10921. @@ -0,0 +1,717 @@
  10922. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  10923. +/*
  10924. + * Interface for Surface Serial Hub (SSH).
  10925. + *
  10926. + * The SSH is the main communication hub for communication between host and
  10927. + * the Surface/System Aggregator Module (SAM) on newer Microsoft Surface
  10928. + * devices (Book 2, Pro 5, Laptops, ...). Also referred to as SAM-over-SSH.
  10929. + * Older devices (Book 1, Pro 4) use SAM-over-HID (via I2C).
  10930. + */
  10931. +
  10932. +#ifndef _SURFACE_SAM_SSH_H
  10933. +#define _SURFACE_SAM_SSH_H
  10934. +
  10935. +#include <linux/types.h>
  10936. +#include <linux/device.h>
  10937. +
  10938. +
  10939. +/* -- Data structures for SAM-over-SSH communication. ----------------------- */
  10940. +
  10941. +/**
  10942. + * enum ssh_frame_type - Frame types for SSH frames.
  10943. + * @SSH_FRAME_TYPE_DATA_SEQ: Indicates a data frame, followed by a payload with
  10944. + * the length specified in the ssh_frame.len field. This
  10945. + * frame is sequenced, meaning that an ACK is required.
  10946. + * @SSH_FRAME_TYPE_DATA_NSQ: Same as SSH_FRAME_TYPE_DATA_SEQ, but unsequenced,
  10947. + * meaning that the message does not have to be ACKed.
  10948. + * @SSH_FRAME_TYPE_ACK: Indicates an ACK message.
  10949. + * @SSH_FRAME_TYPE_NAK: Indicates an error response for previously sent
  10950. + * frame. In general, this means that the frame and/or
  10951. + * payload is malformed, e.g. a CRC is wrong. For command-
  10952. + * type payloads, this can also mean that the command is
  10953. + * invalid.
  10954. + */
  10955. +enum ssh_frame_type {
  10956. + SSH_FRAME_TYPE_DATA_SEQ = 0x80,
  10957. + SSH_FRAME_TYPE_DATA_NSQ = 0x00,
  10958. + SSH_FRAME_TYPE_ACK = 0x40,
  10959. + SSH_FRAME_TYPE_NAK = 0x04,
  10960. +};
  10961. +
  10962. +/**
  10963. + * struct ssh_frame - SSH communication frame.
  10964. + * @type: The type of the frame. See &enum ssh_frame_type.
  10965. + * @len: The length of the frame payload directly following the CRC for this
  10966. + * frame. Does not include the final CRC for that payload.
  10967. + * @seq: The sequence number for this message/exchange.
  10968. + */
  10969. +struct ssh_frame {
  10970. + u8 type;
  10971. + __le16 len;
  10972. + u8 seq;
  10973. +} __packed;
  10974. +
  10975. +static_assert(sizeof(struct ssh_frame) == 4);
  10976. +
  10977. +/*
  10978. + * Maximum SSH frame payload length in bytes. This is the physical maximum
  10979. + * length of the protocol. Implementations may set a more constrained limit.
  10980. + */
  10981. +#define SSH_FRAME_MAX_PAYLOAD_SIZE U16_MAX
  10982. +
  10983. +/**
  10984. + * enum ssh_payload_type - Type indicator for the SSH payload.
  10985. + * @SSH_PLD_TYPE_CMD: The payload is a command structure with optional command
  10986. + * payload.
  10987. + */
  10988. +enum ssh_payload_type {
  10989. + SSH_PLD_TYPE_CMD = 0x80,
  10990. +};
  10991. +
  10992. +/**
  10993. + * struct ssh_command - Payload of a command-type frame.
  10994. + * @type: The type of the payload. See &enum ssh_payload_type. Should be
  10995. + * SSH_PLD_TYPE_CMD for this struct.
  10996. + * @tc: Command target category.
  10997. + * @chn_out: Output channel. Should be zero if this an incoming (EC to host)
  10998. + * message.
  10999. + * @chn_in: Input channel. Should be zero if this is an outgoing (hos to EC)
  11000. + * message.
  11001. + * @iid: Instance ID.
  11002. + * @rqid: Request ID. Used to match requests with responses and differentiate
  11003. + * between responses and events.
  11004. + * @cid: Command ID.
  11005. + */
  11006. +struct ssh_command {
  11007. + u8 type;
  11008. + u8 tc;
  11009. + u8 chn_out;
  11010. + u8 chn_in;
  11011. + u8 iid;
  11012. + __le16 rqid;
  11013. + u8 cid;
  11014. +} __packed;
  11015. +
  11016. +static_assert(sizeof(struct ssh_command) == 8);
  11017. +
  11018. +/*
  11019. + * Maximum SSH command payload length in bytes. This is the physical maximum
  11020. + * length of the protocol. Implementations may set a more constrained limit.
  11021. + */
  11022. +#define SSH_COMMAND_MAX_PAYLOAD_SIZE \
  11023. + (SSH_FRAME_MAX_PAYLOAD_SIZE - sizeof(struct ssh_command))
  11024. +
  11025. +/**
  11026. + * struct ssh_notification_params - Command payload to enable/disable SSH
  11027. + * notifications.
  11028. + * @target_category: The target category for which notifications should be
  11029. + * enabled/disabled.
  11030. + * @flags: Flags determining how notifications are being sent.
  11031. + * @request_id: The request ID that is used to send these notifications.
  11032. + * @instance_id: The specific instance in the given target category for
  11033. + * which notifications should be enabled.
  11034. + */
  11035. +struct ssh_notification_params {
  11036. + u8 target_category;
  11037. + u8 flags;
  11038. + __le16 request_id;
  11039. + u8 instance_id;
  11040. +} __packed;
  11041. +
  11042. +static_assert(sizeof(struct ssh_notification_params) == 5);
  11043. +
  11044. +/**
  11045. + * SSH message syncrhonization (SYN) bytes.
  11046. + */
  11047. +#define SSH_MSG_SYN ((u16)0x55aa)
  11048. +
  11049. +/**
  11050. + * Base-length of a SSH message. This is the minimum number of bytes required
  11051. + * to form a message. The actual message length is SSH_MSG_LEN_BASE plus the
  11052. + * length of the frame payload.
  11053. + */
  11054. +#define SSH_MSG_LEN_BASE (sizeof(struct ssh_frame) + 3ull * sizeof(u16))
  11055. +
  11056. +/**
  11057. + * Length of a SSH control message.
  11058. + */
  11059. +#define SSH_MSG_LEN_CTRL SSH_MSG_LEN_BASE
  11060. +
  11061. +/**
  11062. + * Length of a SSH message with payload of specified size.
  11063. + */
  11064. +#define SSH_MESSAGE_LENGTH(payload_size) (SSH_MSG_LEN_BASE + payload_size)
  11065. +
  11066. +/**
  11067. + * Length of a SSH command message with command payload of specified size.
  11068. + */
  11069. +#define SSH_COMMAND_MESSAGE_LENGTH(payload_size) \
  11070. + SSH_MESSAGE_LENGTH(sizeof(struct ssh_command) + payload_size)
  11071. +
  11072. +/**
  11073. + * Offset of the specified struct ssh_frame field in the raw SSH message data.
  11074. + */
  11075. +#define SSH_MSGOFFSET_FRAME(field) \
  11076. + (sizeof(u16) + offsetof(struct ssh_frame, field))
  11077. +
  11078. +/**
  11079. + * Offset of the specified struct ssh_command field in the raw SSH message data.
  11080. + */
  11081. +#define SSH_MSGOFFSET_COMMAND(field) \
  11082. + (2ull * sizeof(u16) + sizeof(struct ssh_frame) \
  11083. + + offsetof(struct ssh_command, field))
  11084. +
  11085. +/**
  11086. + * struct ssam_span - reference to a buffer region
  11087. + * @ptr: pointer to the buffer region
  11088. + * @len: length of the buffer region
  11089. + *
  11090. + * A reference to a (non-owned) buffer segment, consisting of pointer and
  11091. + * length. Use of this struct indicates non-owned data, i.e. data of which the
  11092. + * life-time is managed (i.e. it is allocated/freed) via another pointer.
  11093. + */
  11094. +struct ssam_span {
  11095. + u8 *ptr;
  11096. + size_t len;
  11097. +};
  11098. +
  11099. +
  11100. +/* -- Packet transport layer (ptl). ----------------------------------------- */
  11101. +
  11102. +enum ssh_packet_priority {
  11103. + SSH_PACKET_PRIORITY_FLUSH = 0,
  11104. + SSH_PACKET_PRIORITY_DATA = 0,
  11105. + SSH_PACKET_PRIORITY_NAK = 1 << 4,
  11106. + SSH_PACKET_PRIORITY_ACK = 2 << 4,
  11107. +};
  11108. +
  11109. +#define SSH_PACKET_PRIORITY(base, try) \
  11110. + ((SSH_PACKET_PRIORITY_##base) | ((try) & 0x0f))
  11111. +
  11112. +#define ssh_packet_priority_get_try(p) ((p) & 0x0f)
  11113. +
  11114. +
  11115. +enum ssh_packet_flags {
  11116. + SSH_PACKET_SF_LOCKED_BIT,
  11117. + SSH_PACKET_SF_QUEUED_BIT,
  11118. + SSH_PACKET_SF_PENDING_BIT,
  11119. + SSH_PACKET_SF_TRANSMITTING_BIT,
  11120. + SSH_PACKET_SF_TRANSMITTED_BIT,
  11121. + SSH_PACKET_SF_ACKED_BIT,
  11122. + SSH_PACKET_SF_CANCELED_BIT,
  11123. + SSH_PACKET_SF_COMPLETED_BIT,
  11124. +
  11125. + SSH_PACKET_TY_FLUSH_BIT,
  11126. + SSH_PACKET_TY_SEQUENCED_BIT,
  11127. + SSH_PACKET_TY_BLOCKING_BIT,
  11128. +
  11129. + SSH_PACKET_FLAGS_SF_MASK =
  11130. + BIT(SSH_PACKET_SF_LOCKED_BIT)
  11131. + | BIT(SSH_PACKET_SF_QUEUED_BIT)
  11132. + | BIT(SSH_PACKET_SF_PENDING_BIT)
  11133. + | BIT(SSH_PACKET_SF_TRANSMITTING_BIT)
  11134. + | BIT(SSH_PACKET_SF_TRANSMITTED_BIT)
  11135. + | BIT(SSH_PACKET_SF_ACKED_BIT)
  11136. + | BIT(SSH_PACKET_SF_CANCELED_BIT)
  11137. + | BIT(SSH_PACKET_SF_COMPLETED_BIT),
  11138. +
  11139. + SSH_PACKET_FLAGS_TY_MASK =
  11140. + BIT(SSH_PACKET_TY_FLUSH_BIT)
  11141. + | BIT(SSH_PACKET_TY_SEQUENCED_BIT)
  11142. + | BIT(SSH_PACKET_TY_BLOCKING_BIT),
  11143. +};
  11144. +
  11145. +
  11146. +struct ssh_ptl;
  11147. +struct ssh_packet;
  11148. +
  11149. +struct ssh_packet_ops {
  11150. + void (*release)(struct ssh_packet *p);
  11151. + void (*complete)(struct ssh_packet *p, int status);
  11152. +};
  11153. +
  11154. +struct ssh_packet {
  11155. + struct ssh_ptl *ptl;
  11156. + struct kref refcnt;
  11157. +
  11158. + u8 priority;
  11159. +
  11160. + struct {
  11161. + size_t len;
  11162. + u8 *ptr;
  11163. + } data;
  11164. +
  11165. + unsigned long state;
  11166. + ktime_t timestamp;
  11167. +
  11168. + struct list_head queue_node;
  11169. + struct list_head pending_node;
  11170. +
  11171. + const struct ssh_packet_ops *ops;
  11172. +};
  11173. +
  11174. +
  11175. +void ssh_packet_get(struct ssh_packet *p);
  11176. +void ssh_packet_put(struct ssh_packet *p);
  11177. +
  11178. +static inline void ssh_packet_set_data(struct ssh_packet *p, u8 *ptr, size_t len)
  11179. +{
  11180. + p->data.ptr = ptr;
  11181. + p->data.len = len;
  11182. +}
  11183. +
  11184. +
  11185. +/* -- Request transport layer (rtl). ---------------------------------------- */
  11186. +
  11187. +enum ssh_request_flags {
  11188. + SSH_REQUEST_SF_LOCKED_BIT,
  11189. + SSH_REQUEST_SF_QUEUED_BIT,
  11190. + SSH_REQUEST_SF_PENDING_BIT,
  11191. + SSH_REQUEST_SF_TRANSMITTING_BIT,
  11192. + SSH_REQUEST_SF_TRANSMITTED_BIT,
  11193. + SSH_REQUEST_SF_RSPRCVD_BIT,
  11194. + SSH_REQUEST_SF_CANCELED_BIT,
  11195. + SSH_REQUEST_SF_COMPLETED_BIT,
  11196. +
  11197. + SSH_REQUEST_TY_FLUSH_BIT,
  11198. + SSH_REQUEST_TY_HAS_RESPONSE_BIT,
  11199. +
  11200. + SSH_REQUEST_FLAGS_SF_MASK =
  11201. + BIT(SSH_REQUEST_SF_LOCKED_BIT)
  11202. + | BIT(SSH_REQUEST_SF_QUEUED_BIT)
  11203. + | BIT(SSH_REQUEST_SF_PENDING_BIT)
  11204. + | BIT(SSH_REQUEST_SF_TRANSMITTING_BIT)
  11205. + | BIT(SSH_REQUEST_SF_TRANSMITTED_BIT)
  11206. + | BIT(SSH_REQUEST_SF_RSPRCVD_BIT)
  11207. + | BIT(SSH_REQUEST_SF_CANCELED_BIT)
  11208. + | BIT(SSH_REQUEST_SF_COMPLETED_BIT),
  11209. +
  11210. + SSH_REQUEST_FLAGS_TY_MASK =
  11211. + BIT(SSH_REQUEST_TY_FLUSH_BIT)
  11212. + | BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT),
  11213. +};
  11214. +
  11215. +
  11216. +struct ssh_rtl;
  11217. +struct ssh_request;
  11218. +
  11219. +struct ssh_request_ops {
  11220. + void (*release)(struct ssh_request *rqst);
  11221. + void (*complete)(struct ssh_request *rqst,
  11222. + const struct ssh_command *cmd,
  11223. + const struct ssam_span *data, int status);
  11224. +};
  11225. +
  11226. +struct ssh_request {
  11227. + struct ssh_packet packet;
  11228. + struct list_head node;
  11229. +
  11230. + unsigned long state;
  11231. + ktime_t timestamp;
  11232. +
  11233. + const struct ssh_request_ops *ops;
  11234. +};
  11235. +
  11236. +
  11237. +static inline void ssh_request_get(struct ssh_request *r)
  11238. +{
  11239. + ssh_packet_get(&r->packet);
  11240. +}
  11241. +
  11242. +static inline void ssh_request_put(struct ssh_request *r)
  11243. +{
  11244. + ssh_packet_put(&r->packet);
  11245. +}
  11246. +
  11247. +static inline void ssh_request_set_data(struct ssh_request *r, u8 *ptr, size_t len)
  11248. +{
  11249. + ssh_packet_set_data(&r->packet, ptr, len);
  11250. +}
  11251. +
  11252. +
  11253. +/* -- Main data types and definitions --------------------------------------- */
  11254. +
  11255. +enum ssam_ssh_tc {
  11256. + SSAM_SSH_TC_SAM = 0x01, // generic system functionality, real-time clock
  11257. + SSAM_SSH_TC_BAT = 0x02, // battery/power subsystem
  11258. + SSAM_SSH_TC_TMP = 0x03, // thermal subsystem
  11259. + SSAM_SSH_TC_PMC = 0x04,
  11260. + SSAM_SSH_TC_FAN = 0x05,
  11261. + SSAM_SSH_TC_PoM = 0x06,
  11262. + SSAM_SSH_TC_DBG = 0x07,
  11263. + SSAM_SSH_TC_KBD = 0x08, // legacy keyboard (Laptop 1/2)
  11264. + SSAM_SSH_TC_FWU = 0x09,
  11265. + SSAM_SSH_TC_UNI = 0x0a,
  11266. + SSAM_SSH_TC_LPC = 0x0b,
  11267. + SSAM_SSH_TC_TCL = 0x0c,
  11268. + SSAM_SSH_TC_SFL = 0x0d,
  11269. + SSAM_SSH_TC_KIP = 0x0e,
  11270. + SSAM_SSH_TC_EXT = 0x0f,
  11271. + SSAM_SSH_TC_BLD = 0x10,
  11272. + SSAM_SSH_TC_BAS = 0x11, // detachment system (Surface Book 2/3)
  11273. + SSAM_SSH_TC_SEN = 0x12,
  11274. + SSAM_SSH_TC_SRQ = 0x13,
  11275. + SSAM_SSH_TC_MCU = 0x14,
  11276. + SSAM_SSH_TC_HID = 0x15, // generic HID input subsystem
  11277. + SSAM_SSH_TC_TCH = 0x16,
  11278. + SSAM_SSH_TC_BKL = 0x17,
  11279. + SSAM_SSH_TC_TAM = 0x18,
  11280. + SSAM_SSH_TC_ACC = 0x19,
  11281. + SSAM_SSH_TC_UFI = 0x1a,
  11282. + SSAM_SSH_TC_USC = 0x1b,
  11283. + SSAM_SSH_TC_PEN = 0x1c,
  11284. + SSAM_SSH_TC_VID = 0x1d,
  11285. + SSAM_SSH_TC_AUD = 0x1e,
  11286. + SSAM_SSH_TC_SMC = 0x1f,
  11287. + SSAM_SSH_TC_KPD = 0x20,
  11288. + SSAM_SSH_TC_REG = 0x21,
  11289. +};
  11290. +
  11291. +struct ssam_controller;
  11292. +
  11293. +/**
  11294. + * struct ssam_event_flags - Flags for enabling/disabling SAM-over-SSH events
  11295. + * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
  11296. + */
  11297. +enum ssam_event_flags {
  11298. + SSAM_EVENT_SEQUENCED = BIT(0),
  11299. +};
  11300. +
  11301. +struct ssam_event {
  11302. + u8 target_category;
  11303. + u8 command_id;
  11304. + u8 instance_id;
  11305. + u8 channel;
  11306. + u16 length;
  11307. + u8 data[0];
  11308. +};
  11309. +
  11310. +enum ssam_request_flags {
  11311. + SSAM_REQUEST_HAS_RESPONSE = BIT(0),
  11312. + SSAM_REQUEST_UNSEQUENCED = BIT(1),
  11313. +};
  11314. +
  11315. +struct ssam_request {
  11316. + u8 target_category;
  11317. + u8 command_id;
  11318. + u8 instance_id;
  11319. + u8 channel;
  11320. + u16 flags;
  11321. + u16 length;
  11322. + const u8 *payload;
  11323. +};
  11324. +
  11325. +struct ssam_response {
  11326. + size_t capacity;
  11327. + size_t length;
  11328. + u8 *pointer;
  11329. +};
  11330. +
  11331. +
  11332. +int ssam_client_bind(struct device *client, struct ssam_controller **ctrl);
  11333. +
  11334. +struct device *ssam_controller_device(struct ssam_controller *c);
  11335. +
  11336. +ssize_t ssam_request_write_data(struct ssam_span *buf,
  11337. + struct ssam_controller *ctrl,
  11338. + struct ssam_request *spec);
  11339. +
  11340. +
  11341. +/* -- Synchronous request interface. ---------------------------------------- */
  11342. +
  11343. +struct ssam_request_sync {
  11344. + struct ssh_request base;
  11345. + struct completion comp;
  11346. + struct ssam_response *resp;
  11347. + int status;
  11348. +};
  11349. +
  11350. +int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
  11351. + struct ssam_request_sync **rqst,
  11352. + struct ssam_span *buffer);
  11353. +
  11354. +void ssam_request_sync_init(struct ssam_request_sync *rqst,
  11355. + enum ssam_request_flags flags);
  11356. +
  11357. +static inline void ssam_request_sync_set_data(struct ssam_request_sync *rqst,
  11358. + u8 *ptr, size_t len)
  11359. +{
  11360. + ssh_request_set_data(&rqst->base, ptr, len);
  11361. +}
  11362. +
  11363. +static inline void ssam_request_sync_set_resp(struct ssam_request_sync *rqst,
  11364. + struct ssam_response *resp)
  11365. +{
  11366. + rqst->resp = resp;
  11367. +}
  11368. +
  11369. +int ssam_request_sync_submit(struct ssam_controller *ctrl,
  11370. + struct ssam_request_sync *rqst);
  11371. +
  11372. +static inline int ssam_request_sync_wait(struct ssam_request_sync *rqst)
  11373. +{
  11374. + wait_for_completion(&rqst->comp);
  11375. + return rqst->status;
  11376. +}
  11377. +
  11378. +int ssam_request_sync(struct ssam_controller *ctrl, struct ssam_request *spec,
  11379. + struct ssam_response *rsp);
  11380. +
  11381. +int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
  11382. + struct ssam_request *spec,
  11383. + struct ssam_response *rsp,
  11384. + struct ssam_span *buf);
  11385. +
  11386. +
  11387. +#define ssam_request_sync_onstack(ctrl, rqst, rsp, payload_len) \
  11388. + ({ \
  11389. + u8 __data[SSH_COMMAND_MESSAGE_LENGTH(payload_len)]; \
  11390. + struct ssam_span __buf = { &__data[0], ARRAY_SIZE(__data) }; \
  11391. + int __status; \
  11392. + \
  11393. + /* ensure input does not overflow buffer */ \
  11394. + if ((rqst)->length <= payload_len) { \
  11395. + __status = ssam_request_sync_with_buffer( \
  11396. + ctrl, rqst, rsp, &__buf); \
  11397. + } else { \
  11398. + __status = -EINVAL; \
  11399. + } \
  11400. + \
  11401. + __status; \
  11402. + })
  11403. +
  11404. +
  11405. +struct ssam_request_spec {
  11406. + u8 target_category;
  11407. + u8 command_id;
  11408. + u8 instance_id;
  11409. + u8 channel;
  11410. + u8 flags;
  11411. +};
  11412. +
  11413. +struct ssam_request_spec_md {
  11414. + u8 target_category;
  11415. + u8 command_id;
  11416. + u8 flags;
  11417. +};
  11418. +
  11419. +#define SSAM_DEFINE_SYNC_REQUEST_N(name, spec...) \
  11420. + int name(struct ssam_controller *ctrl) \
  11421. + { \
  11422. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  11423. + struct ssam_request rqst; \
  11424. + \
  11425. + rqst.target_category = s.target_category; \
  11426. + rqst.command_id = s.command_id; \
  11427. + rqst.instance_id = s.instance_id; \
  11428. + rqst.channel = s.channel; \
  11429. + rqst.flags = s.flags; \
  11430. + rqst.length = 0; \
  11431. + rqst.payload = NULL; \
  11432. + \
  11433. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, 0); \
  11434. + }
  11435. +
  11436. +#define SSAM_DEFINE_SYNC_REQUEST_W(name, wtype, spec...) \
  11437. + int name(struct ssam_controller *ctrl, const wtype *in) \
  11438. + { \
  11439. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  11440. + struct ssam_request rqst; \
  11441. + \
  11442. + rqst.target_category = s.target_category; \
  11443. + rqst.command_id = s.command_id; \
  11444. + rqst.instance_id = s.instance_id; \
  11445. + rqst.channel = s.channel; \
  11446. + rqst.flags = s.flags; \
  11447. + rqst.length = sizeof(wtype); \
  11448. + rqst.payload = (u8 *)in; \
  11449. + \
  11450. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  11451. + sizeof(wtype)); \
  11452. + }
  11453. +
  11454. +#define SSAM_DEFINE_SYNC_REQUEST_R(name, rtype, spec...) \
  11455. + int name(struct ssam_controller *ctrl, rtype *out) \
  11456. + { \
  11457. + struct ssam_request_spec s = (struct ssam_request_spec)spec; \
  11458. + struct ssam_request rqst; \
  11459. + struct ssam_response rsp; \
  11460. + int status; \
  11461. + \
  11462. + rqst.target_category = s.target_category; \
  11463. + rqst.command_id = s.command_id; \
  11464. + rqst.instance_id = s.instance_id; \
  11465. + rqst.channel = s.channel; \
  11466. + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  11467. + rqst.length = 0; \
  11468. + rqst.payload = NULL; \
  11469. + \
  11470. + rsp.capacity = sizeof(rtype); \
  11471. + rsp.length = 0; \
  11472. + rsp.pointer = (u8 *)out; \
  11473. + \
  11474. + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  11475. + if (status) \
  11476. + return status; \
  11477. + \
  11478. + if (rsp.length != sizeof(rtype)) { \
  11479. + struct device *dev = ssam_controller_device(ctrl); \
  11480. + dev_err(dev, "rqst: invalid response length, expected %zu, got %zu" \
  11481. + " (tc: 0x%02x, cid: 0x%02x)", sizeof(rtype), \
  11482. + rsp.length, rqst.target_category, \
  11483. + rqst.command_id); \
  11484. + return -EIO; \
  11485. + } \
  11486. + \
  11487. + return 0; \
  11488. + }
  11489. +
  11490. +#define SSAM_DEFINE_SYNC_REQUEST_MD_W(name, wtype, spec...) \
  11491. + int name(struct ssam_controller *ctrl, u8 chn, u8 iid, const wtype *in) \
  11492. + { \
  11493. + struct ssam_request_spec_md s \
  11494. + = (struct ssam_request_spec_md)spec; \
  11495. + struct ssam_request rqst; \
  11496. + \
  11497. + rqst.target_category = s.target_category; \
  11498. + rqst.command_id = s.command_id; \
  11499. + rqst.instance_id = iid; \
  11500. + rqst.channel = chn; \
  11501. + rqst.flags = s.flags; \
  11502. + rqst.length = sizeof(wtype); \
  11503. + rqst.payload = (u8 *)in; \
  11504. + \
  11505. + return ssam_request_sync_onstack(ctrl, &rqst, NULL, \
  11506. + sizeof(wtype)); \
  11507. + }
  11508. +
  11509. +#define SSAM_DEFINE_SYNC_REQUEST_MD_R(name, rtype, spec...) \
  11510. + int name(struct ssam_controller *ctrl, u8 chn, u8 iid, rtype *out) \
  11511. + { \
  11512. + struct ssam_request_spec_md s \
  11513. + = (struct ssam_request_spec_md)spec; \
  11514. + struct ssam_request rqst; \
  11515. + struct ssam_response rsp; \
  11516. + int status; \
  11517. + \
  11518. + rqst.target_category = s.target_category; \
  11519. + rqst.command_id = s.command_id; \
  11520. + rqst.instance_id = iid; \
  11521. + rqst.channel = chn; \
  11522. + rqst.flags = s.flags | SSAM_REQUEST_HAS_RESPONSE; \
  11523. + rqst.length = 0; \
  11524. + rqst.payload = NULL; \
  11525. + \
  11526. + rsp.capacity = sizeof(rtype); \
  11527. + rsp.length = 0; \
  11528. + rsp.pointer = (u8 *)out; \
  11529. + \
  11530. + status = ssam_request_sync_onstack(ctrl, &rqst, &rsp, 0); \
  11531. + if (status) \
  11532. + return status; \
  11533. + \
  11534. + if (rsp.length != sizeof(rtype)) { \
  11535. + struct device *dev = ssam_controller_device(ctrl); \
  11536. + dev_err(dev, "rqst: invalid response length, expected %zu, got %zu" \
  11537. + " (tc: 0x%02x, cid: 0x%02x)", sizeof(rtype), \
  11538. + rsp.length, rqst.target_category, \
  11539. + rqst.command_id); \
  11540. + return -EIO; \
  11541. + } \
  11542. + \
  11543. + return 0; \
  11544. + }
  11545. +
  11546. +
  11547. +/* -- Event notifier/callbacks. --------------------------------------------- */
  11548. +
  11549. +#define SSAM_NOTIF_STATE_SHIFT 2
  11550. +#define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
  11551. +
  11552. +#define SSAM_NOTIF_HANDLED BIT(0)
  11553. +#define SSAM_NOTIF_STOP BIT(1)
  11554. +
  11555. +
  11556. +struct ssam_notifier_block;
  11557. +
  11558. +typedef u32 (*ssam_notifier_fn_t)(struct ssam_notifier_block *nb,
  11559. + const struct ssam_event *event);
  11560. +
  11561. +struct ssam_notifier_block {
  11562. + struct ssam_notifier_block __rcu *next;
  11563. + ssam_notifier_fn_t fn;
  11564. + int priority;
  11565. +};
  11566. +
  11567. +
  11568. +static inline u32 ssam_notifier_from_errno(int err)
  11569. +{
  11570. + if (WARN_ON(err > 0) || err == 0)
  11571. + return 0;
  11572. + else
  11573. + return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
  11574. +}
  11575. +
  11576. +static inline int ssam_notifier_to_errno(u32 ret)
  11577. +{
  11578. + return -(ret >> SSAM_NOTIF_STATE_SHIFT);
  11579. +}
  11580. +
  11581. +
  11582. +/* -- Event/notification registry. ------------------------------------------ */
  11583. +
  11584. +struct ssam_event_registry {
  11585. + u8 target_category;
  11586. + u8 channel;
  11587. + u8 cid_enable;
  11588. + u8 cid_disable;
  11589. +};
  11590. +
  11591. +struct ssam_event_id {
  11592. + u8 target_category;
  11593. + u8 instance;
  11594. +};
  11595. +
  11596. +
  11597. +#define SSAM_EVENT_REGISTRY(tc, chn, cid_en, cid_dis) \
  11598. + ((struct ssam_event_registry) { \
  11599. + .target_category = (tc), \
  11600. + .channel = (chn), \
  11601. + .cid_enable = (cid_en), \
  11602. + .cid_disable = (cid_dis), \
  11603. + })
  11604. +
  11605. +#define SSAM_EVENT_ID(tc, iid) \
  11606. + ((struct ssam_event_id) { \
  11607. + .target_category = tc, \
  11608. + .instance = iid, \
  11609. + })
  11610. +
  11611. +
  11612. +#define SSAM_EVENT_REGISTRY_SAM \
  11613. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
  11614. +
  11615. +#define SSAM_EVENT_REGISTRY_KIP \
  11616. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
  11617. +
  11618. +#define SSAM_EVENT_REGISTRY_REG \
  11619. + SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, 0x02, 0x01, 0x02)
  11620. +
  11621. +
  11622. +struct ssam_event_notifier {
  11623. + struct ssam_notifier_block base;
  11624. +
  11625. + struct {
  11626. + struct ssam_event_registry reg;
  11627. + struct ssam_event_id id;
  11628. + u8 flags;
  11629. + } event;
  11630. +};
  11631. +
  11632. +int ssam_notifier_register(struct ssam_controller *ctrl,
  11633. + struct ssam_event_notifier *n);
  11634. +
  11635. +int ssam_notifier_unregister(struct ssam_controller *ctrl,
  11636. + struct ssam_event_notifier *n);
  11637. +
  11638. +#endif /* _SURFACE_SAM_SSH_H */
  11639. diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h b/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
  11640. new file mode 100644
  11641. index 0000000000000..4755183fa423b
  11642. --- /dev/null
  11643. +++ b/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
  11644. @@ -0,0 +1,532 @@
  11645. +#undef TRACE_SYSTEM
  11646. +#define TRACE_SYSTEM surface_sam_ssh
  11647. +
  11648. +#if !defined(_SURFACE_SAM_SSH_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
  11649. +#define _SURFACE_SAM_SSH_TRACE_H
  11650. +
  11651. +#include <linux/tracepoint.h>
  11652. +
  11653. +#include "surface_sam_ssh.h"
  11654. +
  11655. +
  11656. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_SEQ);
  11657. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_NSQ);
  11658. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_ACK);
  11659. +TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_NAK);
  11660. +
  11661. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_LOCKED_BIT);
  11662. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_QUEUED_BIT);
  11663. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_PENDING_BIT);
  11664. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTING_BIT);
  11665. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTED_BIT);
  11666. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_ACKED_BIT);
  11667. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_CANCELED_BIT);
  11668. +TRACE_DEFINE_ENUM(SSH_PACKET_SF_COMPLETED_BIT);
  11669. +
  11670. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH_BIT);
  11671. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED_BIT);
  11672. +TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING_BIT);
  11673. +
  11674. +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_SF_MASK);
  11675. +TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_TY_MASK);
  11676. +
  11677. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_LOCKED_BIT);
  11678. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_QUEUED_BIT);
  11679. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_PENDING_BIT);
  11680. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTING_BIT);
  11681. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTED_BIT);
  11682. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_RSPRCVD_BIT);
  11683. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_CANCELED_BIT);
  11684. +TRACE_DEFINE_ENUM(SSH_REQUEST_SF_COMPLETED_BIT);
  11685. +
  11686. +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_FLUSH_BIT);
  11687. +TRACE_DEFINE_ENUM(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
  11688. +
  11689. +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_SF_MASK);
  11690. +TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_TY_MASK);
  11691. +
  11692. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SAM);
  11693. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAT);
  11694. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TMP);
  11695. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PMC);
  11696. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FAN);
  11697. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PoM);
  11698. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_DBG);
  11699. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KBD);
  11700. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_FWU);
  11701. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UNI);
  11702. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_LPC);
  11703. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCL);
  11704. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SFL);
  11705. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KIP);
  11706. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_EXT);
  11707. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BLD);
  11708. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAS);
  11709. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SEN);
  11710. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SRQ);
  11711. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_MCU);
  11712. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_HID);
  11713. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCH);
  11714. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_BKL);
  11715. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_TAM);
  11716. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_ACC);
  11717. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_UFI);
  11718. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_USC);
  11719. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_PEN);
  11720. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_VID);
  11721. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_AUD);
  11722. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_SMC);
  11723. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_KPD);
  11724. +TRACE_DEFINE_ENUM(SSAM_SSH_TC_REG);
  11725. +
  11726. +
  11727. +#define SSAM_PTR_UID_LEN 9
  11728. +#define SSAM_U8_FIELD_NOT_APPLICABLE ((u16)-1)
  11729. +#define SSAM_SEQ_NOT_APPLICABLE ((u16)-1)
  11730. +#define SSAM_RQID_NOT_APPLICABLE ((u32)-1)
  11731. +#define SSAM_SSH_TC_NOT_APPLICABLE 0
  11732. +
  11733. +
  11734. +#ifndef _SURFACE_SAM_SSH_TRACE_HELPERS
  11735. +#define _SURFACE_SAM_SSH_TRACE_HELPERS
  11736. +
  11737. +static inline void ssam_trace_ptr_uid(const void *ptr, char* uid_str)
  11738. +{
  11739. + char buf[2 * sizeof(void*) + 1];
  11740. +
  11741. + snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
  11742. + memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
  11743. + SSAM_PTR_UID_LEN);
  11744. +}
  11745. +
  11746. +static inline u16 ssam_trace_get_packet_seq(const struct ssh_packet *p)
  11747. +{
  11748. + if (!p->data.ptr || p->data.len < SSH_MESSAGE_LENGTH(0))
  11749. + return SSAM_SEQ_NOT_APPLICABLE;
  11750. +
  11751. + return p->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
  11752. +}
  11753. +
  11754. +static inline u32 ssam_trace_get_request_id(const struct ssh_packet *p)
  11755. +{
  11756. + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
  11757. + return SSAM_RQID_NOT_APPLICABLE;
  11758. +
  11759. + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(rqid)]);
  11760. +}
  11761. +
  11762. +static inline u32 ssam_trace_get_request_tc(const struct ssh_packet *p)
  11763. +{
  11764. + if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
  11765. + return SSAM_SSH_TC_NOT_APPLICABLE;
  11766. +
  11767. + return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(tc)]);
  11768. +}
  11769. +
  11770. +#endif /* _SURFACE_SAM_SSH_TRACE_HELPERS */
  11771. +
  11772. +#define ssam_trace_get_command_field_u8(packet, field) \
  11773. + ((!packet || packet->data.len < SSH_COMMAND_MESSAGE_LENGTH(0)) \
  11774. + ? 0 : p->data.ptr[SSH_MSGOFFSET_COMMAND(field)])
  11775. +
  11776. +#define ssam_show_generic_u8_field(value) \
  11777. + __print_symbolic(value, \
  11778. + { SSAM_U8_FIELD_NOT_APPLICABLE, "N/A" } \
  11779. + )
  11780. +
  11781. +
  11782. +#define ssam_show_frame_type(ty) \
  11783. + __print_symbolic(ty, \
  11784. + { SSH_FRAME_TYPE_DATA_SEQ, "DSEQ" }, \
  11785. + { SSH_FRAME_TYPE_DATA_NSQ, "DNSQ" }, \
  11786. + { SSH_FRAME_TYPE_ACK, "ACK" }, \
  11787. + { SSH_FRAME_TYPE_NAK, "NAK" } \
  11788. + )
  11789. +
  11790. +#define ssam_show_packet_type(type) \
  11791. + __print_flags(flags & SSH_PACKET_FLAGS_TY_MASK, "", \
  11792. + { BIT(SSH_PACKET_TY_FLUSH_BIT), "F" }, \
  11793. + { BIT(SSH_PACKET_TY_SEQUENCED_BIT), "S" }, \
  11794. + { BIT(SSH_PACKET_TY_BLOCKING_BIT), "B" } \
  11795. + )
  11796. +
  11797. +#define ssam_show_packet_state(state) \
  11798. + __print_flags(flags & SSH_PACKET_FLAGS_SF_MASK, "", \
  11799. + { BIT(SSH_PACKET_SF_LOCKED_BIT), "L" }, \
  11800. + { BIT(SSH_PACKET_SF_QUEUED_BIT), "Q" }, \
  11801. + { BIT(SSH_PACKET_SF_PENDING_BIT), "P" }, \
  11802. + { BIT(SSH_PACKET_SF_TRANSMITTING_BIT), "S" }, \
  11803. + { BIT(SSH_PACKET_SF_TRANSMITTED_BIT), "T" }, \
  11804. + { BIT(SSH_PACKET_SF_ACKED_BIT), "A" }, \
  11805. + { BIT(SSH_PACKET_SF_CANCELED_BIT), "C" }, \
  11806. + { BIT(SSH_PACKET_SF_COMPLETED_BIT), "F" } \
  11807. + )
  11808. +
  11809. +#define ssam_show_packet_seq(seq) \
  11810. + __print_symbolic(seq, \
  11811. + { SSAM_SEQ_NOT_APPLICABLE, "N/A" } \
  11812. + )
  11813. +
  11814. +
  11815. +#define ssam_show_request_type(flags) \
  11816. + __print_flags(flags & SSH_REQUEST_FLAGS_TY_MASK, "", \
  11817. + { BIT(SSH_REQUEST_TY_FLUSH_BIT), "F" }, \
  11818. + { BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT), "R" } \
  11819. + )
  11820. +
  11821. +#define ssam_show_request_state(flags) \
  11822. + __print_flags(flags & SSH_REQUEST_FLAGS_SF_MASK, "", \
  11823. + { BIT(SSH_REQUEST_SF_LOCKED_BIT), "L" }, \
  11824. + { BIT(SSH_REQUEST_SF_QUEUED_BIT), "Q" }, \
  11825. + { BIT(SSH_REQUEST_SF_PENDING_BIT), "P" }, \
  11826. + { BIT(SSH_REQUEST_SF_TRANSMITTING_BIT), "S" }, \
  11827. + { BIT(SSH_REQUEST_SF_TRANSMITTED_BIT), "T" }, \
  11828. + { BIT(SSH_REQUEST_SF_RSPRCVD_BIT), "A" }, \
  11829. + { BIT(SSH_REQUEST_SF_CANCELED_BIT), "C" }, \
  11830. + { BIT(SSH_REQUEST_SF_COMPLETED_BIT), "F" } \
  11831. + )
  11832. +
  11833. +#define ssam_show_request_id(rqid) \
  11834. + __print_symbolic(rqid, \
  11835. + { SSAM_RQID_NOT_APPLICABLE, "N/A" } \
  11836. + )
  11837. +
  11838. +#define ssam_show_ssh_tc(rqid) \
  11839. + __print_symbolic(rqid, \
  11840. + { SSAM_SSH_TC_NOT_APPLICABLE, "N/A" }, \
  11841. + { SSAM_SSH_TC_SAM, "SAM" }, \
  11842. + { SSAM_SSH_TC_BAT, "BAT" }, \
  11843. + { SSAM_SSH_TC_TMP, "TMP" }, \
  11844. + { SSAM_SSH_TC_PMC, "PMC" }, \
  11845. + { SSAM_SSH_TC_FAN, "FAN" }, \
  11846. + { SSAM_SSH_TC_PoM, "PoM" }, \
  11847. + { SSAM_SSH_TC_DBG, "DBG" }, \
  11848. + { SSAM_SSH_TC_KBD, "KBD" }, \
  11849. + { SSAM_SSH_TC_FWU, "FWU" }, \
  11850. + { SSAM_SSH_TC_UNI, "UNI" }, \
  11851. + { SSAM_SSH_TC_LPC, "LPC" }, \
  11852. + { SSAM_SSH_TC_TCL, "TCL" }, \
  11853. + { SSAM_SSH_TC_SFL, "SFL" }, \
  11854. + { SSAM_SSH_TC_KIP, "KIP" }, \
  11855. + { SSAM_SSH_TC_EXT, "EXT" }, \
  11856. + { SSAM_SSH_TC_BLD, "BLD" }, \
  11857. + { SSAM_SSH_TC_BAS, "BAS" }, \
  11858. + { SSAM_SSH_TC_SEN, "SEN" }, \
  11859. + { SSAM_SSH_TC_SRQ, "SRQ" }, \
  11860. + { SSAM_SSH_TC_MCU, "MCU" }, \
  11861. + { SSAM_SSH_TC_HID, "HID" }, \
  11862. + { SSAM_SSH_TC_TCH, "TCH" }, \
  11863. + { SSAM_SSH_TC_BKL, "BKL" }, \
  11864. + { SSAM_SSH_TC_TAM, "TAM" }, \
  11865. + { SSAM_SSH_TC_ACC, "ACC" }, \
  11866. + { SSAM_SSH_TC_UFI, "UFI" }, \
  11867. + { SSAM_SSH_TC_USC, "USC" }, \
  11868. + { SSAM_SSH_TC_PEN, "PEN" }, \
  11869. + { SSAM_SSH_TC_VID, "VID" }, \
  11870. + { SSAM_SSH_TC_AUD, "AUD" }, \
  11871. + { SSAM_SSH_TC_SMC, "SMC" }, \
  11872. + { SSAM_SSH_TC_KPD, "KPD" }, \
  11873. + { SSAM_SSH_TC_REG, "REG" } \
  11874. + )
  11875. +
  11876. +
  11877. +DECLARE_EVENT_CLASS(ssam_frame_class,
  11878. + TP_PROTO(const struct ssh_frame *frame),
  11879. +
  11880. + TP_ARGS(frame),
  11881. +
  11882. + TP_STRUCT__entry(
  11883. + __field(u8, type)
  11884. + __field(u8, seq)
  11885. + __field(u16, len)
  11886. + ),
  11887. +
  11888. + TP_fast_assign(
  11889. + __entry->type = frame->type;
  11890. + __entry->seq = frame->seq;
  11891. + __entry->len = get_unaligned_le16(&frame->len);
  11892. + ),
  11893. +
  11894. + TP_printk("ty=%s, seq=0x%02x, len=%u",
  11895. + ssam_show_frame_type(__entry->type),
  11896. + __entry->seq,
  11897. + __entry->len
  11898. + )
  11899. +);
  11900. +
  11901. +#define DEFINE_SSAM_FRAME_EVENT(name) \
  11902. + DEFINE_EVENT(ssam_frame_class, ssam_##name, \
  11903. + TP_PROTO(const struct ssh_frame *frame), \
  11904. + TP_ARGS(frame) \
  11905. + )
  11906. +
  11907. +
  11908. +DECLARE_EVENT_CLASS(ssam_command_class,
  11909. + TP_PROTO(const struct ssh_command *cmd, u16 len),
  11910. +
  11911. + TP_ARGS(cmd, len),
  11912. +
  11913. + TP_STRUCT__entry(
  11914. + __field(u16, rqid)
  11915. + __field(u16, len)
  11916. + __field(u8, tc)
  11917. + __field(u8, cid)
  11918. + __field(u8, iid)
  11919. + ),
  11920. +
  11921. + TP_fast_assign(
  11922. + __entry->rqid = get_unaligned_le16(&cmd->rqid);
  11923. + __entry->tc = cmd->tc;
  11924. + __entry->cid = cmd->cid;
  11925. + __entry->iid = cmd->iid;
  11926. + __entry->len = len;
  11927. + ),
  11928. +
  11929. + TP_printk("rqid=0x%04x, tc=%s, cid=0x%02x, iid=0x%02x, len=%u",
  11930. + __entry->rqid,
  11931. + ssam_show_ssh_tc(__entry->tc),
  11932. + __entry->cid,
  11933. + __entry->iid,
  11934. + __entry->len
  11935. + )
  11936. +);
  11937. +
  11938. +#define DEFINE_SSAM_COMMAND_EVENT(name) \
  11939. + DEFINE_EVENT(ssam_command_class, ssam_##name, \
  11940. + TP_PROTO(const struct ssh_command *cmd, u16 len), \
  11941. + TP_ARGS(cmd, len) \
  11942. + )
  11943. +
  11944. +
  11945. +DECLARE_EVENT_CLASS(ssam_packet_class,
  11946. + TP_PROTO(const struct ssh_packet *packet),
  11947. +
  11948. + TP_ARGS(packet),
  11949. +
  11950. + TP_STRUCT__entry(
  11951. + __array(char, uid, SSAM_PTR_UID_LEN)
  11952. + __field(u8, priority)
  11953. + __field(u16, length)
  11954. + __field(unsigned long, state)
  11955. + __field(u16, seq)
  11956. + ),
  11957. +
  11958. + TP_fast_assign(
  11959. + ssam_trace_ptr_uid(packet, __entry->uid);
  11960. + __entry->priority = READ_ONCE(packet->priority);
  11961. + __entry->length = packet->data.len;
  11962. + __entry->state = READ_ONCE(packet->state);
  11963. + __entry->seq = ssam_trace_get_packet_seq(packet);
  11964. + ),
  11965. +
  11966. + TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s",
  11967. + __entry->uid,
  11968. + ssam_show_packet_seq(__entry->seq),
  11969. + ssam_show_packet_type(__entry->state),
  11970. + __entry->priority,
  11971. + __entry->length,
  11972. + ssam_show_packet_state(__entry->state)
  11973. + )
  11974. +);
  11975. +
  11976. +#define DEFINE_SSAM_PACKET_EVENT(name) \
  11977. + DEFINE_EVENT(ssam_packet_class, ssam_##name, \
  11978. + TP_PROTO(const struct ssh_packet *packet), \
  11979. + TP_ARGS(packet) \
  11980. + )
  11981. +
  11982. +
  11983. +DECLARE_EVENT_CLASS(ssam_packet_status_class,
  11984. + TP_PROTO(const struct ssh_packet *packet, int status),
  11985. +
  11986. + TP_ARGS(packet, status),
  11987. +
  11988. + TP_STRUCT__entry(
  11989. + __array(char, uid, SSAM_PTR_UID_LEN)
  11990. + __field(u8, priority)
  11991. + __field(u16, length)
  11992. + __field(unsigned long, state)
  11993. + __field(u16, seq)
  11994. + __field(int, status)
  11995. + ),
  11996. +
  11997. + TP_fast_assign(
  11998. + ssam_trace_ptr_uid(packet, __entry->uid);
  11999. + __entry->priority = READ_ONCE(packet->priority);
  12000. + __entry->length = packet->data.len;
  12001. + __entry->state = READ_ONCE(packet->state);
  12002. + __entry->seq = ssam_trace_get_packet_seq(packet);
  12003. + __entry->status = status;
  12004. + ),
  12005. +
  12006. + TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s, status=%d",
  12007. + __entry->uid,
  12008. + ssam_show_packet_seq(__entry->seq),
  12009. + ssam_show_packet_type(__entry->state),
  12010. + __entry->priority,
  12011. + __entry->length,
  12012. + ssam_show_packet_state(__entry->state),
  12013. + __entry->status
  12014. + )
  12015. +);
  12016. +
  12017. +#define DEFINE_SSAM_PACKET_STATUS_EVENT(name) \
  12018. + DEFINE_EVENT(ssam_packet_status_class, ssam_##name, \
  12019. + TP_PROTO(const struct ssh_packet *packet, int status), \
  12020. + TP_ARGS(packet, status) \
  12021. + )
  12022. +
  12023. +
  12024. +DECLARE_EVENT_CLASS(ssam_request_class,
  12025. + TP_PROTO(const struct ssh_request *request),
  12026. +
  12027. + TP_ARGS(request),
  12028. +
  12029. + TP_STRUCT__entry(
  12030. + __array(char, uid, SSAM_PTR_UID_LEN)
  12031. + __field(unsigned long, state)
  12032. + __field(u32, rqid)
  12033. + __field(u8, tc)
  12034. + __field(u16, cid)
  12035. + __field(u16, iid)
  12036. + ),
  12037. +
  12038. + TP_fast_assign(
  12039. + const struct ssh_packet *p = &request->packet;
  12040. +
  12041. + // use packet for UID so we can match requests to packets
  12042. + ssam_trace_ptr_uid(p, __entry->uid);
  12043. + __entry->state = READ_ONCE(request->state);
  12044. + __entry->rqid = ssam_trace_get_request_id(p);
  12045. + __entry->tc = ssam_trace_get_request_tc(p);
  12046. + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
  12047. + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
  12048. + ),
  12049. +
  12050. + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s",
  12051. + __entry->uid,
  12052. + ssam_show_request_id(__entry->rqid),
  12053. + ssam_show_request_type(__entry->state),
  12054. + ssam_show_request_state(__entry->state),
  12055. + ssam_show_ssh_tc(__entry->tc),
  12056. + ssam_show_generic_u8_field(__entry->cid),
  12057. + ssam_show_generic_u8_field(__entry->iid)
  12058. + )
  12059. +);
  12060. +
  12061. +#define DEFINE_SSAM_REQUEST_EVENT(name) \
  12062. + DEFINE_EVENT(ssam_request_class, ssam_##name, \
  12063. + TP_PROTO(const struct ssh_request *request), \
  12064. + TP_ARGS(request) \
  12065. + )
  12066. +
  12067. +
  12068. +DECLARE_EVENT_CLASS(ssam_request_status_class,
  12069. + TP_PROTO(const struct ssh_request *request, int status),
  12070. +
  12071. + TP_ARGS(request, status),
  12072. +
  12073. + TP_STRUCT__entry(
  12074. + __array(char, uid, SSAM_PTR_UID_LEN)
  12075. + __field(unsigned long, state)
  12076. + __field(u32, rqid)
  12077. + __field(u8, tc)
  12078. + __field(u16, cid)
  12079. + __field(u16, iid)
  12080. + __field(int, status)
  12081. + ),
  12082. +
  12083. + TP_fast_assign(
  12084. + const struct ssh_packet *p = &request->packet;
  12085. +
  12086. + // use packet for UID so we can match requests to packets
  12087. + ssam_trace_ptr_uid(p, __entry->uid);
  12088. + __entry->state = READ_ONCE(request->state);
  12089. + __entry->rqid = ssam_trace_get_request_id(p);
  12090. + __entry->tc = ssam_trace_get_request_tc(p);
  12091. + __entry->cid = ssam_trace_get_command_field_u8(p, cid);
  12092. + __entry->iid = ssam_trace_get_command_field_u8(p, iid);
  12093. + __entry->status = status;
  12094. + ),
  12095. +
  12096. + TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s, status=%d",
  12097. + __entry->uid,
  12098. + ssam_show_request_id(__entry->rqid),
  12099. + ssam_show_request_type(__entry->state),
  12100. + ssam_show_request_state(__entry->state),
  12101. + ssam_show_ssh_tc(__entry->tc),
  12102. + ssam_show_generic_u8_field(__entry->cid),
  12103. + ssam_show_generic_u8_field(__entry->iid),
  12104. + __entry->status
  12105. + )
  12106. +);
  12107. +
  12108. +#define DEFINE_SSAM_REQUEST_STATUS_EVENT(name) \
  12109. + DEFINE_EVENT(ssam_request_status_class, ssam_##name, \
  12110. + TP_PROTO(const struct ssh_request *request, int status),\
  12111. + TP_ARGS(request, status) \
  12112. + )
  12113. +
  12114. +
  12115. +DECLARE_EVENT_CLASS(ssam_generic_uint_class,
  12116. + TP_PROTO(const char* property, unsigned int value),
  12117. +
  12118. + TP_ARGS(property, value),
  12119. +
  12120. + TP_STRUCT__entry(
  12121. + __string(property, property)
  12122. + __field(unsigned int, value)
  12123. + ),
  12124. +
  12125. + TP_fast_assign(
  12126. + __assign_str(property, property);
  12127. + __entry->value = value;
  12128. + ),
  12129. +
  12130. + TP_printk("%s=%u", __get_str(property), __entry->value)
  12131. +);
  12132. +
  12133. +#define DEFINE_SSAM_GENERIC_UINT_EVENT(name) \
  12134. + DEFINE_EVENT(ssam_generic_uint_class, ssam_##name, \
  12135. + TP_PROTO(const char* property, unsigned int value), \
  12136. + TP_ARGS(property, value) \
  12137. + )
  12138. +
  12139. +
  12140. +DEFINE_SSAM_FRAME_EVENT(rx_frame_received);
  12141. +DEFINE_SSAM_COMMAND_EVENT(rx_response_received);
  12142. +DEFINE_SSAM_COMMAND_EVENT(rx_event_received);
  12143. +
  12144. +DEFINE_SSAM_PACKET_EVENT(packet_release);
  12145. +DEFINE_SSAM_PACKET_EVENT(packet_submit);
  12146. +DEFINE_SSAM_PACKET_EVENT(packet_resubmit);
  12147. +DEFINE_SSAM_PACKET_EVENT(packet_timeout);
  12148. +DEFINE_SSAM_PACKET_EVENT(packet_cancel);
  12149. +DEFINE_SSAM_PACKET_STATUS_EVENT(packet_complete);
  12150. +DEFINE_SSAM_GENERIC_UINT_EVENT(ptl_timeout_reap);
  12151. +
  12152. +DEFINE_SSAM_REQUEST_EVENT(request_submit);
  12153. +DEFINE_SSAM_REQUEST_EVENT(request_timeout);
  12154. +DEFINE_SSAM_REQUEST_EVENT(request_cancel);
  12155. +DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
  12156. +DEFINE_SSAM_GENERIC_UINT_EVENT(rtl_timeout_reap);
  12157. +
  12158. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_ack_packet);
  12159. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_nak_packet);
  12160. +DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_dsq_packet);
  12161. +DEFINE_SSAM_PACKET_STATUS_EVENT(ei_tx_fail_write);
  12162. +DEFINE_SSAM_PACKET_EVENT(ei_tx_corrupt_data);
  12163. +DEFINE_SSAM_GENERIC_UINT_EVENT(ei_rx_corrupt_syn);
  12164. +DEFINE_SSAM_FRAME_EVENT(ei_rx_corrupt_data);
  12165. +DEFINE_SSAM_REQUEST_EVENT(ei_rx_drop_response);
  12166. +
  12167. +#endif /* _SURFACE_SAM_SSH_TRACE_H */
  12168. +
  12169. +/* This part must be outside protection */
  12170. +#undef TRACE_INCLUDE_PATH
  12171. +#undef TRACE_INCLUDE_FILE
  12172. +
  12173. +#define TRACE_INCLUDE_PATH .
  12174. +#define TRACE_INCLUDE_FILE surface_sam_ssh_trace
  12175. +
  12176. +#include <trace/define_trace.h>
  12177. diff --git a/drivers/platform/x86/surface_sam/surface_sam_vhf.c b/drivers/platform/x86/surface_sam/surface_sam_vhf.c
  12178. new file mode 100644
  12179. index 0000000000000..8455f952c2724
  12180. --- /dev/null
  12181. +++ b/drivers/platform/x86/surface_sam/surface_sam_vhf.c
  12182. @@ -0,0 +1,266 @@
  12183. +// SPDX-License-Identifier: GPL-2.0-or-later
  12184. +/*
  12185. + * Virtual HID Framework (VHF) driver for input events via SAM.
  12186. + * Used for keyboard input events on the Surface Laptops.
  12187. + */
  12188. +
  12189. +#include <linux/acpi.h>
  12190. +#include <linux/hid.h>
  12191. +#include <linux/input.h>
  12192. +#include <linux/platform_device.h>
  12193. +#include <linux/types.h>
  12194. +
  12195. +#include "surface_sam_ssh.h"
  12196. +
  12197. +
  12198. +#define USB_VENDOR_ID_MICROSOFT 0x045e
  12199. +#define USB_DEVICE_ID_MS_VHF 0xf001
  12200. +
  12201. +#define VHF_INPUT_NAME "Microsoft Virtual HID Framework Device"
  12202. +
  12203. +
  12204. +struct vhf_drvdata {
  12205. + struct platform_device *dev;
  12206. + struct ssam_controller *ctrl;
  12207. +
  12208. + struct ssam_event_notifier notif;
  12209. +
  12210. + struct hid_device *hid;
  12211. +};
  12212. +
  12213. +
  12214. +/*
  12215. + * These report descriptors have been extracted from a Surface Book 2.
  12216. + * They seems to be similar enough to be usable on the Surface Laptop.
  12217. + */
  12218. +static const u8 vhf_hid_desc[] = {
  12219. + // keyboard descriptor (event command ID 0x03)
  12220. + 0x05, 0x01, /* Usage Page (Desktop), */
  12221. + 0x09, 0x06, /* Usage (Keyboard), */
  12222. + 0xA1, 0x01, /* Collection (Application), */
  12223. + 0x85, 0x01, /* Report ID (1), */
  12224. + 0x15, 0x00, /* Logical Minimum (0), */
  12225. + 0x25, 0x01, /* Logical Maximum (1), */
  12226. + 0x75, 0x01, /* Report Size (1), */
  12227. + 0x95, 0x08, /* Report Count (8), */
  12228. + 0x05, 0x07, /* Usage Page (Keyboard), */
  12229. + 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
  12230. + 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
  12231. + 0x81, 0x02, /* Input (Variable), */
  12232. + 0x75, 0x08, /* Report Size (8), */
  12233. + 0x95, 0x0A, /* Report Count (10), */
  12234. + 0x19, 0x00, /* Usage Minimum (None), */
  12235. + 0x29, 0x91, /* Usage Maximum (KB LANG2), */
  12236. + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  12237. + 0x81, 0x00, /* Input, */
  12238. + 0x05, 0x0C, /* Usage Page (Consumer), */
  12239. + 0x0A, 0xC0, 0x02, /* Usage (02C0h), */
  12240. + 0xA1, 0x02, /* Collection (Logical), */
  12241. + 0x1A, 0xC1, 0x02, /* Usage Minimum (02C1h), */
  12242. + 0x2A, 0xC6, 0x02, /* Usage Maximum (02C6h), */
  12243. + 0x95, 0x06, /* Report Count (6), */
  12244. + 0xB1, 0x03, /* Feature (Constant, Variable), */
  12245. + 0xC0, /* End Collection, */
  12246. + 0x05, 0x08, /* Usage Page (LED), */
  12247. + 0x19, 0x01, /* Usage Minimum (01h), */
  12248. + 0x29, 0x03, /* Usage Maximum (03h), */
  12249. + 0x75, 0x01, /* Report Size (1), */
  12250. + 0x95, 0x03, /* Report Count (3), */
  12251. + 0x25, 0x01, /* Logical Maximum (1), */
  12252. + 0x91, 0x02, /* Output (Variable), */
  12253. + 0x95, 0x05, /* Report Count (5), */
  12254. + 0x91, 0x01, /* Output (Constant), */
  12255. + 0xC0, /* End Collection, */
  12256. +
  12257. + // media key descriptor (event command ID 0x04)
  12258. + 0x05, 0x0C, /* Usage Page (Consumer), */
  12259. + 0x09, 0x01, /* Usage (Consumer Control), */
  12260. + 0xA1, 0x01, /* Collection (Application), */
  12261. + 0x85, 0x03, /* Report ID (3), */
  12262. + 0x75, 0x10, /* Report Size (16), */
  12263. + 0x15, 0x00, /* Logical Minimum (0), */
  12264. + 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
  12265. + 0x19, 0x00, /* Usage Minimum (00h), */
  12266. + 0x2A, 0xFF, 0x03, /* Usage Maximum (03FFh), */
  12267. + 0x81, 0x00, /* Input, */
  12268. + 0xC0, /* End Collection, */
  12269. +};
  12270. +
  12271. +
  12272. +static int vhf_hid_start(struct hid_device *hid)
  12273. +{
  12274. + hid_dbg(hid, "%s\n", __func__);
  12275. + return 0;
  12276. +}
  12277. +
  12278. +static void vhf_hid_stop(struct hid_device *hid)
  12279. +{
  12280. + hid_dbg(hid, "%s\n", __func__);
  12281. +}
  12282. +
  12283. +static int vhf_hid_open(struct hid_device *hid)
  12284. +{
  12285. + hid_dbg(hid, "%s\n", __func__);
  12286. + return 0;
  12287. +}
  12288. +
  12289. +static void vhf_hid_close(struct hid_device *hid)
  12290. +{
  12291. + hid_dbg(hid, "%s\n", __func__);
  12292. +}
  12293. +
  12294. +static int vhf_hid_parse(struct hid_device *hid)
  12295. +{
  12296. + return hid_parse_report(hid, (u8 *)vhf_hid_desc, ARRAY_SIZE(vhf_hid_desc));
  12297. +}
  12298. +
  12299. +static int vhf_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  12300. + u8 *buf, size_t len, unsigned char rtype,
  12301. + int reqtype)
  12302. +{
  12303. + hid_dbg(hid, "%s\n", __func__);
  12304. + return 0;
  12305. +}
  12306. +
  12307. +static int vhf_hid_output_report(struct hid_device *hid, u8 *buf, size_t len)
  12308. +{
  12309. + hid_dbg(hid, "%s\n", __func__);
  12310. + print_hex_dump_debug("report:", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
  12311. +
  12312. + return len;
  12313. +}
  12314. +
  12315. +static struct hid_ll_driver vhf_hid_ll_driver = {
  12316. + .start = vhf_hid_start,
  12317. + .stop = vhf_hid_stop,
  12318. + .open = vhf_hid_open,
  12319. + .close = vhf_hid_close,
  12320. + .parse = vhf_hid_parse,
  12321. + .raw_request = vhf_hid_raw_request,
  12322. + .output_report = vhf_hid_output_report,
  12323. +};
  12324. +
  12325. +
  12326. +static struct hid_device *vhf_create_hid_device(struct platform_device *pdev)
  12327. +{
  12328. + struct hid_device *hid;
  12329. +
  12330. + hid = hid_allocate_device();
  12331. + if (IS_ERR(hid))
  12332. + return hid;
  12333. +
  12334. + hid->dev.parent = &pdev->dev;
  12335. +
  12336. + hid->bus = BUS_VIRTUAL;
  12337. + hid->vendor = USB_VENDOR_ID_MICROSOFT;
  12338. + hid->product = USB_DEVICE_ID_MS_VHF;
  12339. +
  12340. + hid->ll_driver = &vhf_hid_ll_driver;
  12341. +
  12342. + sprintf(hid->name, "%s", VHF_INPUT_NAME);
  12343. +
  12344. + return hid;
  12345. +}
  12346. +
  12347. +static u32 vhf_event_handler(struct ssam_notifier_block *nb, const struct ssam_event *event)
  12348. +{
  12349. + struct vhf_drvdata *drvdata = container_of(nb, struct vhf_drvdata, notif.base);
  12350. + int status;
  12351. +
  12352. + if (event->target_category != 0x08)
  12353. + return 0;
  12354. +
  12355. + if (event->command_id == 0x03 || event->command_id == 0x04) {
  12356. + status = hid_input_report(drvdata->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 1);
  12357. + return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  12358. + }
  12359. +
  12360. + return 0;
  12361. +}
  12362. +
  12363. +static int surface_sam_vhf_probe(struct platform_device *pdev)
  12364. +{
  12365. + struct ssam_controller *ctrl;
  12366. + struct vhf_drvdata *drvdata;
  12367. + struct hid_device *hid;
  12368. + int status;
  12369. +
  12370. + // add device link to EC
  12371. + status = ssam_client_bind(&pdev->dev, &ctrl);
  12372. + if (status)
  12373. + return status == -ENXIO ? -EPROBE_DEFER : status;
  12374. +
  12375. + drvdata = kzalloc(sizeof(struct vhf_drvdata), GFP_KERNEL);
  12376. + if (!drvdata)
  12377. + return -ENOMEM;
  12378. +
  12379. + hid = vhf_create_hid_device(pdev);
  12380. + if (IS_ERR(hid)) {
  12381. + status = PTR_ERR(hid);
  12382. + goto err_probe_hid;
  12383. + }
  12384. +
  12385. + status = hid_add_device(hid);
  12386. + if (status)
  12387. + goto err_add_hid;
  12388. +
  12389. + drvdata->dev = pdev;
  12390. + drvdata->ctrl = ctrl;
  12391. + drvdata->hid = hid;
  12392. +
  12393. + drvdata->notif.base.priority = 1;
  12394. + drvdata->notif.base.fn = vhf_event_handler;
  12395. + drvdata->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
  12396. + drvdata->notif.event.id.target_category = SSAM_SSH_TC_KBD;
  12397. + drvdata->notif.event.id.instance = 0;
  12398. + drvdata->notif.event.flags = 0;
  12399. +
  12400. + platform_set_drvdata(pdev, drvdata);
  12401. +
  12402. + status = ssam_notifier_register(ctrl, &drvdata->notif);
  12403. + if (status)
  12404. + goto err_add_hid;
  12405. +
  12406. + return 0;
  12407. +
  12408. +err_add_hid:
  12409. + hid_destroy_device(hid);
  12410. + platform_set_drvdata(pdev, NULL);
  12411. +err_probe_hid:
  12412. + kfree(drvdata);
  12413. + return status;
  12414. +}
  12415. +
  12416. +static int surface_sam_vhf_remove(struct platform_device *pdev)
  12417. +{
  12418. + struct vhf_drvdata *drvdata = platform_get_drvdata(pdev);
  12419. +
  12420. + ssam_notifier_unregister(drvdata->ctrl, &drvdata->notif);
  12421. + hid_destroy_device(drvdata->hid);
  12422. + kfree(drvdata);
  12423. +
  12424. + platform_set_drvdata(pdev, NULL);
  12425. + return 0;
  12426. +}
  12427. +
  12428. +
  12429. +static const struct acpi_device_id surface_sam_vhf_match[] = {
  12430. + { "MSHW0096" },
  12431. + { },
  12432. +};
  12433. +MODULE_DEVICE_TABLE(acpi, surface_sam_vhf_match);
  12434. +
  12435. +static struct platform_driver surface_sam_vhf = {
  12436. + .probe = surface_sam_vhf_probe,
  12437. + .remove = surface_sam_vhf_remove,
  12438. + .driver = {
  12439. + .name = "surface_sam_vhf",
  12440. + .acpi_match_table = surface_sam_vhf_match,
  12441. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  12442. + },
  12443. +};
  12444. +module_platform_driver(surface_sam_vhf);
  12445. +
  12446. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  12447. +MODULE_DESCRIPTION("Virtual HID Framework Driver for 5th Generation Surface Devices");
  12448. +MODULE_LICENSE("GPL");
  12449. diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
  12450. index a9719858c950b..ce5309d002805 100644
  12451. --- a/drivers/tty/serdev/core.c
  12452. +++ b/drivers/tty/serdev/core.c
  12453. @@ -552,16 +552,97 @@ static int of_serdev_register_devices(struct serdev_controller *ctrl)
  12454. }
  12455. #ifdef CONFIG_ACPI
  12456. +
  12457. +#define SERDEV_ACPI_MAX_SCAN_DEPTH 32
  12458. +
  12459. +struct acpi_serdev_lookup {
  12460. + acpi_handle device_handle;
  12461. + acpi_handle controller_handle;
  12462. + int n;
  12463. + int index;
  12464. +};
  12465. +
  12466. +static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
  12467. +{
  12468. + struct acpi_serdev_lookup *lookup = data;
  12469. + struct acpi_resource_uart_serialbus *sb;
  12470. + acpi_status status;
  12471. +
  12472. + if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  12473. + return 1;
  12474. +
  12475. + if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
  12476. + return 1;
  12477. +
  12478. + if (lookup->index != -1 && lookup->n++ != lookup->index)
  12479. + return 1;
  12480. +
  12481. + sb = &ares->data.uart_serial_bus;
  12482. +
  12483. + status = acpi_get_handle(lookup->device_handle,
  12484. + sb->resource_source.string_ptr,
  12485. + &lookup->controller_handle);
  12486. + if (ACPI_FAILURE(status))
  12487. + return 1;
  12488. +
  12489. + /*
  12490. + * NOTE: Ideally, we would also want to retreive other properties here,
  12491. + * once setting them before opening the device is supported by serdev.
  12492. + */
  12493. +
  12494. + return 1;
  12495. +}
  12496. +
  12497. +static int acpi_serdev_do_lookup(struct acpi_device *adev,
  12498. + struct acpi_serdev_lookup *lookup)
  12499. +{
  12500. + struct list_head resource_list;
  12501. + int ret;
  12502. +
  12503. + lookup->device_handle = acpi_device_handle(adev);
  12504. + lookup->controller_handle = NULL;
  12505. + lookup->n = 0;
  12506. +
  12507. + INIT_LIST_HEAD(&resource_list);
  12508. + ret = acpi_dev_get_resources(adev, &resource_list,
  12509. + acpi_serdev_parse_resource, lookup);
  12510. + acpi_dev_free_resource_list(&resource_list);
  12511. +
  12512. + if (ret < 0)
  12513. + return -EINVAL;
  12514. +
  12515. + return 0;
  12516. +}
  12517. +
  12518. +static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
  12519. + struct acpi_device *adev)
  12520. +{
  12521. + struct acpi_serdev_lookup lookup;
  12522. + int ret;
  12523. +
  12524. + if (acpi_bus_get_status(adev) || !adev->status.present)
  12525. + return -EINVAL;
  12526. +
  12527. + /* Look for UARTSerialBusV2 resource */
  12528. + lookup.index = -1; // we only care for the last device
  12529. +
  12530. + ret = acpi_serdev_do_lookup(adev, &lookup);
  12531. + if (ret)
  12532. + return ret;
  12533. +
  12534. + /* Make sure controller and ResourceSource handle match */
  12535. + if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
  12536. + return -ENODEV;
  12537. +
  12538. + return 0;
  12539. +}
  12540. +
  12541. static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
  12542. - struct acpi_device *adev)
  12543. + struct acpi_device *adev)
  12544. {
  12545. - struct serdev_device *serdev = NULL;
  12546. + struct serdev_device *serdev;
  12547. int err;
  12548. - if (acpi_bus_get_status(adev) || !adev->status.present ||
  12549. - acpi_device_enumerated(adev))
  12550. - return AE_OK;
  12551. -
  12552. serdev = serdev_device_alloc(ctrl);
  12553. if (!serdev) {
  12554. dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
  12555. @@ -589,7 +670,7 @@ static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
  12556. };
  12557. static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
  12558. - void *data, void **return_value)
  12559. + void *data, void **return_value)
  12560. {
  12561. struct serdev_controller *ctrl = data;
  12562. struct acpi_device *adev;
  12563. @@ -597,26 +678,32 @@ static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
  12564. if (acpi_bus_get_device(handle, &adev))
  12565. return AE_OK;
  12566. + if (acpi_device_enumerated(adev))
  12567. + return AE_OK;
  12568. +
  12569. /* Skip if black listed */
  12570. if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
  12571. return AE_OK;
  12572. + if (acpi_serdev_check_resources(ctrl, adev))
  12573. + return AE_OK;
  12574. +
  12575. return acpi_serdev_register_device(ctrl, adev);
  12576. }
  12577. +
  12578. static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
  12579. {
  12580. acpi_status status;
  12581. - acpi_handle handle;
  12582. - handle = ACPI_HANDLE(ctrl->dev.parent);
  12583. - if (!handle)
  12584. + if (!has_acpi_companion(ctrl->dev.parent))
  12585. return -ENODEV;
  12586. - status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  12587. + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  12588. + SERDEV_ACPI_MAX_SCAN_DEPTH,
  12589. acpi_serdev_add_device, NULL, ctrl, NULL);
  12590. if (ACPI_FAILURE(status))
  12591. - dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
  12592. + dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
  12593. if (!ctrl->serdev)
  12594. return -ENODEV;
  12595. --
  12596. 2.28.0