ufshcd.c 292 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Universal Flash Storage Host controller driver Core
  4. * Copyright (C) 2011-2013 Samsung India Software Operations
  5. * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  6. *
  7. * Authors:
  8. * Santosh Yaraganavi <santosh.sy@samsung.com>
  9. * Vinayak Holikatti <h.vinayak@samsung.com>
  10. */
  11. #include <linux/async.h>
  12. #include <linux/devfreq.h>
  13. #include <linux/nls.h>
  14. #include <linux/of.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/blk-pm.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/pm_opp.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/sched/clock.h>
  25. #include <linux/iopoll.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_dbg.h>
  28. #include <scsi/scsi_driver.h>
  29. #include <scsi/scsi_eh.h>
  30. #include "ufshcd-priv.h"
  31. #include <ufs/ufs_quirks.h>
  32. #include <ufs/unipro.h>
  33. #include "ufs-sysfs.h"
  34. #include "ufs-debugfs.h"
  35. #include "ufs-fault-injection.h"
  36. #include "ufs_bsg.h"
  37. #include "ufshcd-crypto.h"
  38. #include <linux/unaligned.h>
  39. #define CREATE_TRACE_POINTS
  40. #include "ufs_trace.h"
  41. #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
  42. UTP_TASK_REQ_COMPL |\
  43. UFSHCD_ERROR_MASK)
  44. #define UFSHCD_ENABLE_MCQ_INTRS (UTP_TASK_REQ_COMPL |\
  45. UFSHCD_ERROR_MASK |\
  46. MCQ_CQ_EVENT_STATUS)
  47. /* UIC command timeout, unit: ms */
  48. enum {
  49. UIC_CMD_TIMEOUT_DEFAULT = 500,
  50. UIC_CMD_TIMEOUT_MAX = 2000,
  51. };
  52. /* NOP OUT retries waiting for NOP IN response */
  53. #define NOP_OUT_RETRIES 10
  54. /* Timeout after 50 msecs if NOP OUT hangs without response */
  55. #define NOP_OUT_TIMEOUT 50 /* msecs */
  56. /* Query request retries */
  57. #define QUERY_REQ_RETRIES 3
  58. /* Query request timeout */
  59. #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
  60. /* Advanced RPMB request timeout */
  61. #define ADVANCED_RPMB_REQ_TIMEOUT 3000 /* 3 seconds */
  62. /* Task management command timeout */
  63. #define TM_CMD_TIMEOUT 100 /* msecs */
  64. /* maximum number of retries for a general UIC command */
  65. #define UFS_UIC_COMMAND_RETRIES 3
  66. /* maximum number of link-startup retries */
  67. #define DME_LINKSTARTUP_RETRIES 3
  68. /* maximum number of reset retries before giving up */
  69. #define MAX_HOST_RESET_RETRIES 5
  70. /* Maximum number of error handler retries before giving up */
  71. #define MAX_ERR_HANDLER_RETRIES 5
  72. /* Expose the flag value from utp_upiu_query.value */
  73. #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
  74. /* Interrupt aggregation default timeout, unit: 40us */
  75. #define INT_AGGR_DEF_TO 0x02
  76. /* default delay of autosuspend: 2000 ms */
  77. #define RPM_AUTOSUSPEND_DELAY_MS 2000
  78. /* Default delay of RPM device flush delayed work */
  79. #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
  80. /* Default value of wait time before gating device ref clock */
  81. #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
  82. /* Polling time to wait for fDeviceInit */
  83. #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
  84. /* Default RTC update every 10 seconds */
  85. #define UFS_RTC_UPDATE_INTERVAL_MS (10 * MSEC_PER_SEC)
  86. /* bMaxNumOfRTT is equal to two after device manufacturing */
  87. #define DEFAULT_MAX_NUM_RTT 2
  88. /* UFSHC 4.0 compliant HC support this mode. */
  89. static bool use_mcq_mode = true;
  90. static bool is_mcq_supported(struct ufs_hba *hba)
  91. {
  92. return hba->mcq_sup && use_mcq_mode;
  93. }
  94. module_param(use_mcq_mode, bool, 0644);
  95. MODULE_PARM_DESC(use_mcq_mode, "Control MCQ mode for controllers starting from UFSHCI 4.0. 1 - enable MCQ, 0 - disable MCQ. MCQ is enabled by default");
  96. static unsigned int uic_cmd_timeout = UIC_CMD_TIMEOUT_DEFAULT;
  97. static int uic_cmd_timeout_set(const char *val, const struct kernel_param *kp)
  98. {
  99. return param_set_uint_minmax(val, kp, UIC_CMD_TIMEOUT_DEFAULT,
  100. UIC_CMD_TIMEOUT_MAX);
  101. }
  102. static const struct kernel_param_ops uic_cmd_timeout_ops = {
  103. .set = uic_cmd_timeout_set,
  104. .get = param_get_uint,
  105. };
  106. module_param_cb(uic_cmd_timeout, &uic_cmd_timeout_ops, &uic_cmd_timeout, 0644);
  107. MODULE_PARM_DESC(uic_cmd_timeout,
  108. "UFS UIC command timeout in milliseconds. Defaults to 500ms. Supported values range from 500ms to 2 seconds inclusively");
  109. #define ufshcd_toggle_vreg(_dev, _vreg, _on) \
  110. ({ \
  111. int _ret; \
  112. if (_on) \
  113. _ret = ufshcd_enable_vreg(_dev, _vreg); \
  114. else \
  115. _ret = ufshcd_disable_vreg(_dev, _vreg); \
  116. _ret; \
  117. })
  118. #define ufshcd_hex_dump(prefix_str, buf, len) do { \
  119. size_t __len = (len); \
  120. print_hex_dump(KERN_ERR, prefix_str, \
  121. __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
  122. 16, 4, buf, __len, false); \
  123. } while (0)
  124. int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
  125. const char *prefix)
  126. {
  127. u32 *regs;
  128. size_t pos;
  129. if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
  130. return -EINVAL;
  131. regs = kzalloc(len, GFP_ATOMIC);
  132. if (!regs)
  133. return -ENOMEM;
  134. for (pos = 0; pos < len; pos += 4) {
  135. if (offset == 0 &&
  136. pos >= REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER &&
  137. pos <= REG_UIC_ERROR_CODE_DME)
  138. continue;
  139. regs[pos / 4] = ufshcd_readl(hba, offset + pos);
  140. }
  141. ufshcd_hex_dump(prefix, regs, len);
  142. kfree(regs);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
  146. enum {
  147. UFSHCD_MAX_CHANNEL = 0,
  148. UFSHCD_MAX_ID = 1,
  149. };
  150. static const char *const ufshcd_state_name[] = {
  151. [UFSHCD_STATE_RESET] = "reset",
  152. [UFSHCD_STATE_OPERATIONAL] = "operational",
  153. [UFSHCD_STATE_ERROR] = "error",
  154. [UFSHCD_STATE_EH_SCHEDULED_FATAL] = "eh_fatal",
  155. [UFSHCD_STATE_EH_SCHEDULED_NON_FATAL] = "eh_non_fatal",
  156. };
  157. /* UFSHCD error handling flags */
  158. enum {
  159. UFSHCD_EH_IN_PROGRESS = (1 << 0),
  160. };
  161. /* UFSHCD UIC layer error flags */
  162. enum {
  163. UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
  164. UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
  165. UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
  166. UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
  167. UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
  168. UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
  169. UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */
  170. };
  171. #define ufshcd_set_eh_in_progress(h) \
  172. ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
  173. #define ufshcd_eh_in_progress(h) \
  174. ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
  175. #define ufshcd_clear_eh_in_progress(h) \
  176. ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
  177. const struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
  178. [UFS_PM_LVL_0] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
  179. [UFS_PM_LVL_1] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
  180. [UFS_PM_LVL_2] = {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
  181. [UFS_PM_LVL_3] = {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
  182. [UFS_PM_LVL_4] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
  183. [UFS_PM_LVL_5] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
  184. /*
  185. * For DeepSleep, the link is first put in hibern8 and then off.
  186. * Leaving the link in hibern8 is not supported.
  187. */
  188. [UFS_PM_LVL_6] = {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
  189. };
  190. static inline enum ufs_dev_pwr_mode
  191. ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
  192. {
  193. return ufs_pm_lvl_states[lvl].dev_state;
  194. }
  195. static inline enum uic_link_state
  196. ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
  197. {
  198. return ufs_pm_lvl_states[lvl].link_state;
  199. }
  200. static inline enum ufs_pm_level
  201. ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
  202. enum uic_link_state link_state)
  203. {
  204. enum ufs_pm_level lvl;
  205. for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
  206. if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
  207. (ufs_pm_lvl_states[lvl].link_state == link_state))
  208. return lvl;
  209. }
  210. /* if no match found, return the level 0 */
  211. return UFS_PM_LVL_0;
  212. }
  213. static bool ufshcd_has_pending_tasks(struct ufs_hba *hba)
  214. {
  215. return hba->outstanding_tasks || hba->active_uic_cmd ||
  216. hba->uic_async_done;
  217. }
  218. static bool ufshcd_is_ufs_dev_busy(struct ufs_hba *hba)
  219. {
  220. return scsi_host_busy(hba->host) || ufshcd_has_pending_tasks(hba);
  221. }
  222. static const struct ufs_dev_quirk ufs_fixups[] = {
  223. /* UFS cards deviations table */
  224. { .wmanufacturerid = UFS_VENDOR_MICRON,
  225. .model = UFS_ANY_MODEL,
  226. .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
  227. { .wmanufacturerid = UFS_VENDOR_SAMSUNG,
  228. .model = UFS_ANY_MODEL,
  229. .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
  230. UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
  231. UFS_DEVICE_QUIRK_PA_HIBER8TIME |
  232. UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS },
  233. { .wmanufacturerid = UFS_VENDOR_SKHYNIX,
  234. .model = UFS_ANY_MODEL,
  235. .quirk = UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME },
  236. { .wmanufacturerid = UFS_VENDOR_SKHYNIX,
  237. .model = "hB8aL1" /*H28U62301AMR*/,
  238. .quirk = UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME },
  239. { .wmanufacturerid = UFS_VENDOR_TOSHIBA,
  240. .model = UFS_ANY_MODEL,
  241. .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
  242. { .wmanufacturerid = UFS_VENDOR_TOSHIBA,
  243. .model = "THGLF2G9C8KBADG",
  244. .quirk = UFS_DEVICE_QUIRK_PA_TACTIVATE },
  245. { .wmanufacturerid = UFS_VENDOR_TOSHIBA,
  246. .model = "THGLF2G9D8KBADG",
  247. .quirk = UFS_DEVICE_QUIRK_PA_TACTIVATE },
  248. {}
  249. };
  250. static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
  251. static void ufshcd_async_scan(void *data, async_cookie_t cookie);
  252. static int ufshcd_reset_and_restore(struct ufs_hba *hba);
  253. static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
  254. static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
  255. static void ufshcd_hba_exit(struct ufs_hba *hba);
  256. static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params);
  257. static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
  258. static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
  259. static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
  260. static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
  261. static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
  262. static int ufshcd_scale_clks(struct ufs_hba *hba, unsigned long freq,
  263. bool scale_up);
  264. static irqreturn_t ufshcd_intr(int irq, void *__hba);
  265. static int ufshcd_change_power_mode(struct ufs_hba *hba,
  266. struct ufs_pa_layer_attr *pwr_mode);
  267. static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
  268. static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
  269. static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
  270. struct ufs_vreg *vreg);
  271. static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba,
  272. bool enable);
  273. static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
  274. static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
  275. void ufshcd_enable_irq(struct ufs_hba *hba)
  276. {
  277. if (!hba->is_irq_enabled) {
  278. enable_irq(hba->irq);
  279. hba->is_irq_enabled = true;
  280. }
  281. }
  282. EXPORT_SYMBOL_GPL(ufshcd_enable_irq);
  283. void ufshcd_disable_irq(struct ufs_hba *hba)
  284. {
  285. if (hba->is_irq_enabled) {
  286. disable_irq(hba->irq);
  287. hba->is_irq_enabled = false;
  288. }
  289. }
  290. EXPORT_SYMBOL_GPL(ufshcd_disable_irq);
  291. static void ufshcd_configure_wb(struct ufs_hba *hba)
  292. {
  293. if (!ufshcd_is_wb_allowed(hba))
  294. return;
  295. ufshcd_wb_toggle(hba, true);
  296. ufshcd_wb_toggle_buf_flush_during_h8(hba, true);
  297. if (ufshcd_is_wb_buf_flush_allowed(hba))
  298. ufshcd_wb_toggle_buf_flush(hba, true);
  299. }
  300. static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
  301. {
  302. if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
  303. scsi_unblock_requests(hba->host);
  304. }
  305. static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
  306. {
  307. if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
  308. scsi_block_requests(hba->host);
  309. }
  310. static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
  311. enum ufs_trace_str_t str_t)
  312. {
  313. struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
  314. struct utp_upiu_header *header;
  315. if (!trace_ufshcd_upiu_enabled())
  316. return;
  317. if (str_t == UFS_CMD_SEND)
  318. header = &rq->header;
  319. else
  320. header = &hba->lrb[tag].ucd_rsp_ptr->header;
  321. trace_ufshcd_upiu(dev_name(hba->dev), str_t, header, &rq->sc.cdb,
  322. UFS_TSF_CDB);
  323. }
  324. static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba,
  325. enum ufs_trace_str_t str_t,
  326. struct utp_upiu_req *rq_rsp)
  327. {
  328. if (!trace_ufshcd_upiu_enabled())
  329. return;
  330. trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header,
  331. &rq_rsp->qr, UFS_TSF_OSF);
  332. }
  333. static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
  334. enum ufs_trace_str_t str_t)
  335. {
  336. struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[tag];
  337. if (!trace_ufshcd_upiu_enabled())
  338. return;
  339. if (str_t == UFS_TM_SEND)
  340. trace_ufshcd_upiu(dev_name(hba->dev), str_t,
  341. &descp->upiu_req.req_header,
  342. &descp->upiu_req.input_param1,
  343. UFS_TSF_TM_INPUT);
  344. else
  345. trace_ufshcd_upiu(dev_name(hba->dev), str_t,
  346. &descp->upiu_rsp.rsp_header,
  347. &descp->upiu_rsp.output_param1,
  348. UFS_TSF_TM_OUTPUT);
  349. }
  350. static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
  351. const struct uic_command *ucmd,
  352. enum ufs_trace_str_t str_t)
  353. {
  354. u32 cmd;
  355. if (!trace_ufshcd_uic_command_enabled())
  356. return;
  357. if (str_t == UFS_CMD_SEND)
  358. cmd = ucmd->command;
  359. else
  360. cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
  361. trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd,
  362. ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
  363. ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
  364. ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
  365. }
  366. static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag,
  367. enum ufs_trace_str_t str_t)
  368. {
  369. u64 lba = 0;
  370. u8 opcode = 0, group_id = 0;
  371. u32 doorbell = 0;
  372. u32 intr;
  373. int hwq_id = -1;
  374. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  375. struct scsi_cmnd *cmd = lrbp->cmd;
  376. struct request *rq = scsi_cmd_to_rq(cmd);
  377. int transfer_len = -1;
  378. if (!cmd)
  379. return;
  380. /* trace UPIU also */
  381. ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
  382. if (!trace_ufshcd_command_enabled())
  383. return;
  384. opcode = cmd->cmnd[0];
  385. if (opcode == READ_10 || opcode == WRITE_10) {
  386. /*
  387. * Currently we only fully trace read(10) and write(10) commands
  388. */
  389. transfer_len =
  390. be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
  391. lba = scsi_get_lba(cmd);
  392. if (opcode == WRITE_10)
  393. group_id = lrbp->cmd->cmnd[6];
  394. } else if (opcode == UNMAP) {
  395. /*
  396. * The number of Bytes to be unmapped beginning with the lba.
  397. */
  398. transfer_len = blk_rq_bytes(rq);
  399. lba = scsi_get_lba(cmd);
  400. }
  401. intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  402. if (hba->mcq_enabled) {
  403. struct ufs_hw_queue *hwq = ufshcd_mcq_req_to_hwq(hba, rq);
  404. hwq_id = hwq->id;
  405. } else {
  406. doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  407. }
  408. trace_ufshcd_command(cmd->device, str_t, tag, doorbell, hwq_id,
  409. transfer_len, intr, lba, opcode, group_id);
  410. }
  411. static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
  412. {
  413. struct ufs_clk_info *clki;
  414. struct list_head *head = &hba->clk_list_head;
  415. if (list_empty(head))
  416. return;
  417. list_for_each_entry(clki, head, list) {
  418. if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
  419. clki->max_freq)
  420. dev_err(hba->dev, "clk: %s, rate: %u\n",
  421. clki->name, clki->curr_freq);
  422. }
  423. }
  424. static void ufshcd_print_evt(struct ufs_hba *hba, u32 id,
  425. const char *err_name)
  426. {
  427. int i;
  428. bool found = false;
  429. const struct ufs_event_hist *e;
  430. if (id >= UFS_EVT_CNT)
  431. return;
  432. e = &hba->ufs_stats.event[id];
  433. for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) {
  434. int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH;
  435. if (e->tstamp[p] == 0)
  436. continue;
  437. dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
  438. e->val[p], div_u64(e->tstamp[p], 1000));
  439. found = true;
  440. }
  441. if (!found)
  442. dev_err(hba->dev, "No record of %s\n", err_name);
  443. else
  444. dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt);
  445. }
  446. static void ufshcd_print_evt_hist(struct ufs_hba *hba)
  447. {
  448. ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
  449. ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err");
  450. ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err");
  451. ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err");
  452. ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err");
  453. ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err");
  454. ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR,
  455. "auto_hibern8_err");
  456. ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err");
  457. ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL,
  458. "link_startup_fail");
  459. ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail");
  460. ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR,
  461. "suspend_fail");
  462. ufshcd_print_evt(hba, UFS_EVT_WL_RES_ERR, "wlun resume_fail");
  463. ufshcd_print_evt(hba, UFS_EVT_WL_SUSP_ERR,
  464. "wlun suspend_fail");
  465. ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset");
  466. ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset");
  467. ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort");
  468. ufshcd_vops_dbg_register_dump(hba);
  469. }
  470. static
  471. void ufshcd_print_tr(struct ufs_hba *hba, int tag, bool pr_prdt)
  472. {
  473. const struct ufshcd_lrb *lrbp;
  474. int prdt_length;
  475. lrbp = &hba->lrb[tag];
  476. dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
  477. tag, div_u64(lrbp->issue_time_stamp_local_clock, 1000));
  478. dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
  479. tag, div_u64(lrbp->compl_time_stamp_local_clock, 1000));
  480. dev_err(hba->dev,
  481. "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
  482. tag, (u64)lrbp->utrd_dma_addr);
  483. ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
  484. sizeof(struct utp_transfer_req_desc));
  485. dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
  486. (u64)lrbp->ucd_req_dma_addr);
  487. ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
  488. sizeof(struct utp_upiu_req));
  489. dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
  490. (u64)lrbp->ucd_rsp_dma_addr);
  491. ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
  492. sizeof(struct utp_upiu_rsp));
  493. prdt_length = le16_to_cpu(
  494. lrbp->utr_descriptor_ptr->prd_table_length);
  495. if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
  496. prdt_length /= ufshcd_sg_entry_size(hba);
  497. dev_err(hba->dev,
  498. "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
  499. tag, prdt_length,
  500. (u64)lrbp->ucd_prdt_dma_addr);
  501. if (pr_prdt)
  502. ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
  503. ufshcd_sg_entry_size(hba) * prdt_length);
  504. }
  505. static bool ufshcd_print_tr_iter(struct request *req, void *priv)
  506. {
  507. struct scsi_device *sdev = req->q->queuedata;
  508. struct Scsi_Host *shost = sdev->host;
  509. struct ufs_hba *hba = shost_priv(shost);
  510. ufshcd_print_tr(hba, req->tag, *(bool *)priv);
  511. return true;
  512. }
  513. /**
  514. * ufshcd_print_trs_all - print trs for all started requests.
  515. * @hba: per-adapter instance.
  516. * @pr_prdt: need to print prdt or not.
  517. */
  518. static void ufshcd_print_trs_all(struct ufs_hba *hba, bool pr_prdt)
  519. {
  520. blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_print_tr_iter, &pr_prdt);
  521. }
  522. static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
  523. {
  524. int tag;
  525. for_each_set_bit(tag, &bitmap, hba->nutmrs) {
  526. struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
  527. dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
  528. ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
  529. }
  530. }
  531. static void ufshcd_print_host_state(struct ufs_hba *hba)
  532. {
  533. const struct scsi_device *sdev_ufs = hba->ufs_device_wlun;
  534. dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
  535. dev_err(hba->dev, "%d outstanding reqs, tasks=0x%lx\n",
  536. scsi_host_busy(hba->host), hba->outstanding_tasks);
  537. dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
  538. hba->saved_err, hba->saved_uic_err);
  539. dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
  540. hba->curr_dev_pwr_mode, hba->uic_link_state);
  541. dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
  542. hba->pm_op_in_progress, hba->is_sys_suspended);
  543. dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
  544. hba->auto_bkops_enabled, hba->host->host_self_blocked);
  545. dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
  546. dev_err(hba->dev,
  547. "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
  548. div_u64(hba->ufs_stats.last_hibern8_exit_tstamp, 1000),
  549. hba->ufs_stats.hibern8_exit_cnt);
  550. dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
  551. div_u64(hba->ufs_stats.last_intr_ts, 1000),
  552. hba->ufs_stats.last_intr_status);
  553. dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
  554. hba->eh_flags, hba->req_abort_count);
  555. dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
  556. hba->ufs_version, hba->capabilities, hba->caps);
  557. dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
  558. hba->dev_quirks);
  559. if (sdev_ufs)
  560. dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
  561. sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
  562. ufshcd_print_clk_freqs(hba);
  563. }
  564. /**
  565. * ufshcd_print_pwr_info - print power params as saved in hba
  566. * power info
  567. * @hba: per-adapter instance
  568. */
  569. static void ufshcd_print_pwr_info(struct ufs_hba *hba)
  570. {
  571. static const char * const names[] = {
  572. "INVALID MODE",
  573. "FAST MODE",
  574. "SLOW_MODE",
  575. "INVALID MODE",
  576. "FASTAUTO_MODE",
  577. "SLOWAUTO_MODE",
  578. "INVALID MODE",
  579. };
  580. /*
  581. * Using dev_dbg to avoid messages during runtime PM to avoid
  582. * never-ending cycles of messages written back to storage by user space
  583. * causing runtime resume, causing more messages and so on.
  584. */
  585. dev_dbg(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
  586. __func__,
  587. hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
  588. hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
  589. names[hba->pwr_info.pwr_rx],
  590. names[hba->pwr_info.pwr_tx],
  591. hba->pwr_info.hs_rate);
  592. }
  593. static void ufshcd_device_reset(struct ufs_hba *hba)
  594. {
  595. int err;
  596. err = ufshcd_vops_device_reset(hba);
  597. if (!err) {
  598. ufshcd_set_ufs_dev_active(hba);
  599. if (ufshcd_is_wb_allowed(hba)) {
  600. hba->dev_info.wb_enabled = false;
  601. hba->dev_info.wb_buf_flush_enabled = false;
  602. }
  603. if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE)
  604. hba->dev_info.rtc_time_baseline = 0;
  605. }
  606. if (err != -EOPNOTSUPP)
  607. ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
  608. }
  609. void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
  610. {
  611. if (!us)
  612. return;
  613. if (us < 10)
  614. udelay(us);
  615. else
  616. usleep_range(us, us + tolerance);
  617. }
  618. EXPORT_SYMBOL_GPL(ufshcd_delay_us);
  619. /**
  620. * ufshcd_wait_for_register - wait for register value to change
  621. * @hba: per-adapter interface
  622. * @reg: mmio register offset
  623. * @mask: mask to apply to the read register value
  624. * @val: value to wait for
  625. * @interval_us: polling interval in microseconds
  626. * @timeout_ms: timeout in milliseconds
  627. *
  628. * Return: -ETIMEDOUT on error, zero on success.
  629. */
  630. static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
  631. u32 val, unsigned long interval_us,
  632. unsigned long timeout_ms)
  633. {
  634. int err = 0;
  635. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  636. /* ignore bits that we don't intend to wait on */
  637. val = val & mask;
  638. while ((ufshcd_readl(hba, reg) & mask) != val) {
  639. usleep_range(interval_us, interval_us + 50);
  640. if (time_after(jiffies, timeout)) {
  641. if ((ufshcd_readl(hba, reg) & mask) != val)
  642. err = -ETIMEDOUT;
  643. break;
  644. }
  645. }
  646. return err;
  647. }
  648. /**
  649. * ufshcd_get_intr_mask - Get the interrupt bit mask
  650. * @hba: Pointer to adapter instance
  651. *
  652. * Return: interrupt bit mask per version
  653. */
  654. static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
  655. {
  656. if (hba->ufs_version <= ufshci_version(2, 0))
  657. return INTERRUPT_MASK_ALL_VER_11;
  658. return INTERRUPT_MASK_ALL_VER_21;
  659. }
  660. /**
  661. * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
  662. * @hba: Pointer to adapter instance
  663. *
  664. * Return: UFSHCI version supported by the controller
  665. */
  666. static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
  667. {
  668. u32 ufshci_ver;
  669. if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
  670. ufshci_ver = ufshcd_vops_get_ufs_hci_version(hba);
  671. else
  672. ufshci_ver = ufshcd_readl(hba, REG_UFS_VERSION);
  673. /*
  674. * UFSHCI v1.x uses a different version scheme, in order
  675. * to allow the use of comparisons with the ufshci_version
  676. * function, we convert it to the same scheme as ufs 2.0+.
  677. */
  678. if (ufshci_ver & 0x00010000)
  679. return ufshci_version(1, ufshci_ver & 0x00000100);
  680. return ufshci_ver;
  681. }
  682. /**
  683. * ufshcd_is_device_present - Check if any device connected to
  684. * the host controller
  685. * @hba: pointer to adapter instance
  686. *
  687. * Return: true if device present, false if no device detected
  688. */
  689. static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
  690. {
  691. return ufshcd_readl(hba, REG_CONTROLLER_STATUS) & DEVICE_PRESENT;
  692. }
  693. /**
  694. * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
  695. * @lrbp: pointer to local command reference block
  696. * @cqe: pointer to the completion queue entry
  697. *
  698. * This function is used to get the OCS field from UTRD
  699. *
  700. * Return: the OCS field in the UTRD.
  701. */
  702. static enum utp_ocs ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp,
  703. struct cq_entry *cqe)
  704. {
  705. if (cqe)
  706. return le32_to_cpu(cqe->status) & MASK_OCS;
  707. return lrbp->utr_descriptor_ptr->header.ocs & MASK_OCS;
  708. }
  709. /**
  710. * ufshcd_utrl_clear() - Clear requests from the controller request list.
  711. * @hba: per adapter instance
  712. * @mask: mask with one bit set for each request to be cleared
  713. */
  714. static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 mask)
  715. {
  716. if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
  717. mask = ~mask;
  718. /*
  719. * From the UFSHCI specification: "UTP Transfer Request List CLear
  720. * Register (UTRLCLR): This field is bit significant. Each bit
  721. * corresponds to a slot in the UTP Transfer Request List, where bit 0
  722. * corresponds to request slot 0. A bit in this field is set to ‘0’
  723. * by host software to indicate to the host controller that a transfer
  724. * request slot is cleared. The host controller
  725. * shall free up any resources associated to the request slot
  726. * immediately, and shall set the associated bit in UTRLDBR to ‘0’. The
  727. * host software indicates no change to request slots by setting the
  728. * associated bits in this field to ‘1’. Bits in this field shall only
  729. * be set ‘1’ or ‘0’ by host software when UTRLRSR is set to ‘1’."
  730. */
  731. ufshcd_writel(hba, ~mask, REG_UTP_TRANSFER_REQ_LIST_CLEAR);
  732. }
  733. /**
  734. * ufshcd_utmrl_clear - Clear a bit in UTMRLCLR register
  735. * @hba: per adapter instance
  736. * @pos: position of the bit to be cleared
  737. */
  738. static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
  739. {
  740. if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
  741. ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
  742. else
  743. ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
  744. }
  745. /**
  746. * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  747. * @reg: Register value of host controller status
  748. *
  749. * Return: 0 on success; a positive value if failed.
  750. */
  751. static inline int ufshcd_get_lists_status(u32 reg)
  752. {
  753. return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
  754. }
  755. /**
  756. * ufshcd_get_uic_cmd_result - Get the UIC command result
  757. * @hba: Pointer to adapter instance
  758. *
  759. * This function gets the result of UIC command completion
  760. *
  761. * Return: 0 on success; non-zero value on error.
  762. */
  763. static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
  764. {
  765. return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
  766. MASK_UIC_COMMAND_RESULT;
  767. }
  768. /**
  769. * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
  770. * @hba: Pointer to adapter instance
  771. *
  772. * This function gets UIC command argument3
  773. *
  774. * Return: 0 on success; non-zero value on error.
  775. */
  776. static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
  777. {
  778. return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
  779. }
  780. /**
  781. * ufshcd_get_req_rsp - returns the TR response transaction type
  782. * @ucd_rsp_ptr: pointer to response UPIU
  783. *
  784. * Return: UPIU type.
  785. */
  786. static inline enum upiu_response_transaction
  787. ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
  788. {
  789. return ucd_rsp_ptr->header.transaction_code;
  790. }
  791. /**
  792. * ufshcd_is_exception_event - Check if the device raised an exception event
  793. * @ucd_rsp_ptr: pointer to response UPIU
  794. *
  795. * The function checks if the device raised an exception event indicated in
  796. * the Device Information field of response UPIU.
  797. *
  798. * Return: true if exception is raised, false otherwise.
  799. */
  800. static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
  801. {
  802. return ucd_rsp_ptr->header.device_information & 1;
  803. }
  804. /**
  805. * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
  806. * @hba: per adapter instance
  807. */
  808. static inline void
  809. ufshcd_reset_intr_aggr(struct ufs_hba *hba)
  810. {
  811. ufshcd_writel(hba, INT_AGGR_ENABLE |
  812. INT_AGGR_COUNTER_AND_TIMER_RESET,
  813. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  814. }
  815. /**
  816. * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
  817. * @hba: per adapter instance
  818. * @cnt: Interrupt aggregation counter threshold
  819. * @tmout: Interrupt aggregation timeout value
  820. */
  821. static inline void
  822. ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
  823. {
  824. ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
  825. INT_AGGR_COUNTER_THLD_VAL(cnt) |
  826. INT_AGGR_TIMEOUT_VAL(tmout),
  827. REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  828. }
  829. /**
  830. * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
  831. * @hba: per adapter instance
  832. */
  833. static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
  834. {
  835. ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
  836. }
  837. /**
  838. * ufshcd_enable_run_stop_reg - Enable run-stop registers,
  839. * When run-stop registers are set to 1, it indicates the
  840. * host controller that it can process the requests
  841. * @hba: per adapter instance
  842. */
  843. static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
  844. {
  845. ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
  846. REG_UTP_TASK_REQ_LIST_RUN_STOP);
  847. ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
  848. REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
  849. }
  850. /**
  851. * ufshcd_hba_start - Start controller initialization sequence
  852. * @hba: per adapter instance
  853. */
  854. static inline void ufshcd_hba_start(struct ufs_hba *hba)
  855. {
  856. u32 val = CONTROLLER_ENABLE;
  857. if (ufshcd_crypto_enable(hba))
  858. val |= CRYPTO_GENERAL_ENABLE;
  859. ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
  860. }
  861. /**
  862. * ufshcd_is_hba_active - Get controller state
  863. * @hba: per adapter instance
  864. *
  865. * Return: true if and only if the controller is active.
  866. */
  867. bool ufshcd_is_hba_active(struct ufs_hba *hba)
  868. {
  869. return ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE;
  870. }
  871. EXPORT_SYMBOL_GPL(ufshcd_is_hba_active);
  872. /**
  873. * ufshcd_pm_qos_init - initialize PM QoS request
  874. * @hba: per adapter instance
  875. */
  876. void ufshcd_pm_qos_init(struct ufs_hba *hba)
  877. {
  878. if (hba->pm_qos_enabled)
  879. return;
  880. cpu_latency_qos_add_request(&hba->pm_qos_req, PM_QOS_DEFAULT_VALUE);
  881. if (cpu_latency_qos_request_active(&hba->pm_qos_req))
  882. hba->pm_qos_enabled = true;
  883. }
  884. /**
  885. * ufshcd_pm_qos_exit - remove request from PM QoS
  886. * @hba: per adapter instance
  887. */
  888. void ufshcd_pm_qos_exit(struct ufs_hba *hba)
  889. {
  890. if (!hba->pm_qos_enabled)
  891. return;
  892. cpu_latency_qos_remove_request(&hba->pm_qos_req);
  893. hba->pm_qos_enabled = false;
  894. }
  895. /**
  896. * ufshcd_pm_qos_update - update PM QoS request
  897. * @hba: per adapter instance
  898. * @on: If True, vote for perf PM QoS mode otherwise power save mode
  899. */
  900. static void ufshcd_pm_qos_update(struct ufs_hba *hba, bool on)
  901. {
  902. if (!hba->pm_qos_enabled)
  903. return;
  904. cpu_latency_qos_update_request(&hba->pm_qos_req, on ? 0 : PM_QOS_DEFAULT_VALUE);
  905. }
  906. /**
  907. * ufshcd_set_clk_freq - set UFS controller clock frequencies
  908. * @hba: per adapter instance
  909. * @scale_up: If True, set max possible frequency othewise set low frequency
  910. *
  911. * Return: 0 if successful; < 0 upon failure.
  912. */
  913. static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
  914. {
  915. int ret = 0;
  916. struct ufs_clk_info *clki;
  917. struct list_head *head = &hba->clk_list_head;
  918. if (list_empty(head))
  919. goto out;
  920. list_for_each_entry(clki, head, list) {
  921. if (!IS_ERR_OR_NULL(clki->clk)) {
  922. if (scale_up && clki->max_freq) {
  923. if (clki->curr_freq == clki->max_freq)
  924. continue;
  925. ret = clk_set_rate(clki->clk, clki->max_freq);
  926. if (ret) {
  927. dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
  928. __func__, clki->name,
  929. clki->max_freq, ret);
  930. break;
  931. }
  932. trace_ufshcd_clk_scaling(dev_name(hba->dev),
  933. "scaled up", clki->name,
  934. clki->curr_freq,
  935. clki->max_freq);
  936. clki->curr_freq = clki->max_freq;
  937. } else if (!scale_up && clki->min_freq) {
  938. if (clki->curr_freq == clki->min_freq)
  939. continue;
  940. ret = clk_set_rate(clki->clk, clki->min_freq);
  941. if (ret) {
  942. dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
  943. __func__, clki->name,
  944. clki->min_freq, ret);
  945. break;
  946. }
  947. trace_ufshcd_clk_scaling(dev_name(hba->dev),
  948. "scaled down", clki->name,
  949. clki->curr_freq,
  950. clki->min_freq);
  951. clki->curr_freq = clki->min_freq;
  952. }
  953. }
  954. dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
  955. clki->name, clk_get_rate(clki->clk));
  956. }
  957. out:
  958. return ret;
  959. }
  960. int ufshcd_opp_config_clks(struct device *dev, struct opp_table *opp_table,
  961. struct dev_pm_opp *opp, void *data,
  962. bool scaling_down)
  963. {
  964. struct ufs_hba *hba = dev_get_drvdata(dev);
  965. struct list_head *head = &hba->clk_list_head;
  966. struct ufs_clk_info *clki;
  967. unsigned long freq;
  968. u8 idx = 0;
  969. int ret;
  970. list_for_each_entry(clki, head, list) {
  971. if (!IS_ERR_OR_NULL(clki->clk)) {
  972. freq = dev_pm_opp_get_freq_indexed(opp, idx++);
  973. /* Do not set rate for clocks having frequency as 0 */
  974. if (!freq)
  975. continue;
  976. ret = clk_set_rate(clki->clk, freq);
  977. if (ret) {
  978. dev_err(dev, "%s: %s clk set rate(%ldHz) failed, %d\n",
  979. __func__, clki->name, freq, ret);
  980. return ret;
  981. }
  982. trace_ufshcd_clk_scaling(dev_name(dev),
  983. (scaling_down ? "scaled down" : "scaled up"),
  984. clki->name, hba->clk_scaling.target_freq, freq);
  985. }
  986. }
  987. return 0;
  988. }
  989. EXPORT_SYMBOL_GPL(ufshcd_opp_config_clks);
  990. static int ufshcd_opp_set_rate(struct ufs_hba *hba, unsigned long freq)
  991. {
  992. struct dev_pm_opp *opp;
  993. int ret;
  994. opp = dev_pm_opp_find_freq_floor_indexed(hba->dev,
  995. &freq, 0);
  996. if (IS_ERR(opp))
  997. return PTR_ERR(opp);
  998. ret = dev_pm_opp_set_opp(hba->dev, opp);
  999. dev_pm_opp_put(opp);
  1000. return ret;
  1001. }
  1002. /**
  1003. * ufshcd_scale_clks - scale up or scale down UFS controller clocks
  1004. * @hba: per adapter instance
  1005. * @freq: frequency to scale
  1006. * @scale_up: True if scaling up and false if scaling down
  1007. *
  1008. * Return: 0 if successful; < 0 upon failure.
  1009. */
  1010. static int ufshcd_scale_clks(struct ufs_hba *hba, unsigned long freq,
  1011. bool scale_up)
  1012. {
  1013. int ret = 0;
  1014. ktime_t start = ktime_get();
  1015. ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
  1016. if (ret)
  1017. goto out;
  1018. if (hba->use_pm_opp)
  1019. ret = ufshcd_opp_set_rate(hba, freq);
  1020. else
  1021. ret = ufshcd_set_clk_freq(hba, scale_up);
  1022. if (ret)
  1023. goto out;
  1024. ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
  1025. if (ret) {
  1026. if (hba->use_pm_opp)
  1027. ufshcd_opp_set_rate(hba,
  1028. hba->devfreq->previous_freq);
  1029. else
  1030. ufshcd_set_clk_freq(hba, !scale_up);
  1031. goto out;
  1032. }
  1033. ufshcd_pm_qos_update(hba, scale_up);
  1034. out:
  1035. trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
  1036. (scale_up ? "up" : "down"),
  1037. ktime_to_us(ktime_sub(ktime_get(), start)), ret);
  1038. return ret;
  1039. }
  1040. /**
  1041. * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
  1042. * @hba: per adapter instance
  1043. * @freq: frequency to scale
  1044. * @scale_up: True if scaling up and false if scaling down
  1045. *
  1046. * Return: true if scaling is required, false otherwise.
  1047. */
  1048. static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
  1049. unsigned long freq, bool scale_up)
  1050. {
  1051. struct ufs_clk_info *clki;
  1052. struct list_head *head = &hba->clk_list_head;
  1053. if (list_empty(head))
  1054. return false;
  1055. if (hba->use_pm_opp)
  1056. return freq != hba->clk_scaling.target_freq;
  1057. list_for_each_entry(clki, head, list) {
  1058. if (!IS_ERR_OR_NULL(clki->clk)) {
  1059. if (scale_up && clki->max_freq) {
  1060. if (clki->curr_freq == clki->max_freq)
  1061. continue;
  1062. return true;
  1063. } else if (!scale_up && clki->min_freq) {
  1064. if (clki->curr_freq == clki->min_freq)
  1065. continue;
  1066. return true;
  1067. }
  1068. }
  1069. }
  1070. return false;
  1071. }
  1072. /*
  1073. * Determine the number of pending commands by counting the bits in the SCSI
  1074. * device budget maps. This approach has been selected because a bit is set in
  1075. * the budget map before scsi_host_queue_ready() checks the host_self_blocked
  1076. * flag. The host_self_blocked flag can be modified by calling
  1077. * scsi_block_requests() or scsi_unblock_requests().
  1078. */
  1079. static u32 ufshcd_pending_cmds(struct ufs_hba *hba)
  1080. {
  1081. const struct scsi_device *sdev;
  1082. u32 pending = 0;
  1083. lockdep_assert_held(hba->host->host_lock);
  1084. __shost_for_each_device(sdev, hba->host)
  1085. pending += sbitmap_weight(&sdev->budget_map);
  1086. return pending;
  1087. }
  1088. /*
  1089. * Wait until all pending SCSI commands and TMFs have finished or the timeout
  1090. * has expired.
  1091. *
  1092. * Return: 0 upon success; -EBUSY upon timeout.
  1093. */
  1094. static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
  1095. u64 wait_timeout_us)
  1096. {
  1097. unsigned long flags;
  1098. int ret = 0;
  1099. u32 tm_doorbell;
  1100. u32 tr_pending;
  1101. bool timeout = false, do_last_check = false;
  1102. ktime_t start;
  1103. ufshcd_hold(hba);
  1104. spin_lock_irqsave(hba->host->host_lock, flags);
  1105. /*
  1106. * Wait for all the outstanding tasks/transfer requests.
  1107. * Verify by checking the doorbell registers are clear.
  1108. */
  1109. start = ktime_get();
  1110. do {
  1111. if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
  1112. ret = -EBUSY;
  1113. goto out;
  1114. }
  1115. tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
  1116. tr_pending = ufshcd_pending_cmds(hba);
  1117. if (!tm_doorbell && !tr_pending) {
  1118. timeout = false;
  1119. break;
  1120. } else if (do_last_check) {
  1121. break;
  1122. }
  1123. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1124. io_schedule_timeout(msecs_to_jiffies(20));
  1125. if (ktime_to_us(ktime_sub(ktime_get(), start)) >
  1126. wait_timeout_us) {
  1127. timeout = true;
  1128. /*
  1129. * We might have scheduled out for long time so make
  1130. * sure to check if doorbells are cleared by this time
  1131. * or not.
  1132. */
  1133. do_last_check = true;
  1134. }
  1135. spin_lock_irqsave(hba->host->host_lock, flags);
  1136. } while (tm_doorbell || tr_pending);
  1137. if (timeout) {
  1138. dev_err(hba->dev,
  1139. "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
  1140. __func__, tm_doorbell, tr_pending);
  1141. ret = -EBUSY;
  1142. }
  1143. out:
  1144. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1145. ufshcd_release(hba);
  1146. return ret;
  1147. }
  1148. /**
  1149. * ufshcd_scale_gear - scale up/down UFS gear
  1150. * @hba: per adapter instance
  1151. * @scale_up: True for scaling up gear and false for scaling down
  1152. *
  1153. * Return: 0 for success; -EBUSY if scaling can't happen at this time;
  1154. * non-zero for any other errors.
  1155. */
  1156. static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
  1157. {
  1158. int ret = 0;
  1159. struct ufs_pa_layer_attr new_pwr_info;
  1160. if (scale_up) {
  1161. memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info,
  1162. sizeof(struct ufs_pa_layer_attr));
  1163. } else {
  1164. memcpy(&new_pwr_info, &hba->pwr_info,
  1165. sizeof(struct ufs_pa_layer_attr));
  1166. if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
  1167. hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) {
  1168. /* save the current power mode */
  1169. memcpy(&hba->clk_scaling.saved_pwr_info,
  1170. &hba->pwr_info,
  1171. sizeof(struct ufs_pa_layer_attr));
  1172. /* scale down gear */
  1173. new_pwr_info.gear_tx = hba->clk_scaling.min_gear;
  1174. new_pwr_info.gear_rx = hba->clk_scaling.min_gear;
  1175. }
  1176. }
  1177. /* check if the power mode needs to be changed or not? */
  1178. ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
  1179. if (ret)
  1180. dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
  1181. __func__, ret,
  1182. hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
  1183. new_pwr_info.gear_tx, new_pwr_info.gear_rx);
  1184. return ret;
  1185. }
  1186. /*
  1187. * Wait until all pending SCSI commands and TMFs have finished or the timeout
  1188. * has expired.
  1189. *
  1190. * Return: 0 upon success; -EBUSY upon timeout.
  1191. */
  1192. static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba, u64 timeout_us)
  1193. {
  1194. int ret = 0;
  1195. /*
  1196. * make sure that there are no outstanding requests when
  1197. * clock scaling is in progress
  1198. */
  1199. mutex_lock(&hba->host->scan_mutex);
  1200. blk_mq_quiesce_tagset(&hba->host->tag_set);
  1201. mutex_lock(&hba->wb_mutex);
  1202. down_write(&hba->clk_scaling_lock);
  1203. if (!hba->clk_scaling.is_allowed ||
  1204. ufshcd_wait_for_doorbell_clr(hba, timeout_us)) {
  1205. ret = -EBUSY;
  1206. up_write(&hba->clk_scaling_lock);
  1207. mutex_unlock(&hba->wb_mutex);
  1208. blk_mq_unquiesce_tagset(&hba->host->tag_set);
  1209. mutex_unlock(&hba->host->scan_mutex);
  1210. goto out;
  1211. }
  1212. /* let's not get into low power until clock scaling is completed */
  1213. ufshcd_hold(hba);
  1214. out:
  1215. return ret;
  1216. }
  1217. static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err, bool scale_up)
  1218. {
  1219. up_write(&hba->clk_scaling_lock);
  1220. /* Enable Write Booster if we have scaled up else disable it */
  1221. if (ufshcd_enable_wb_if_scaling_up(hba) && !err)
  1222. ufshcd_wb_toggle(hba, scale_up);
  1223. mutex_unlock(&hba->wb_mutex);
  1224. blk_mq_unquiesce_tagset(&hba->host->tag_set);
  1225. mutex_unlock(&hba->host->scan_mutex);
  1226. ufshcd_release(hba);
  1227. }
  1228. /**
  1229. * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
  1230. * @hba: per adapter instance
  1231. * @freq: frequency to scale
  1232. * @scale_up: True for scaling up and false for scalin down
  1233. *
  1234. * Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero
  1235. * for any other errors.
  1236. */
  1237. static int ufshcd_devfreq_scale(struct ufs_hba *hba, unsigned long freq,
  1238. bool scale_up)
  1239. {
  1240. int ret = 0;
  1241. ret = ufshcd_clock_scaling_prepare(hba, 1 * USEC_PER_SEC);
  1242. if (ret)
  1243. return ret;
  1244. /* scale down the gear before scaling down clocks */
  1245. if (!scale_up) {
  1246. ret = ufshcd_scale_gear(hba, false);
  1247. if (ret)
  1248. goto out_unprepare;
  1249. }
  1250. ret = ufshcd_scale_clks(hba, freq, scale_up);
  1251. if (ret) {
  1252. if (!scale_up)
  1253. ufshcd_scale_gear(hba, true);
  1254. goto out_unprepare;
  1255. }
  1256. /* scale up the gear after scaling up clocks */
  1257. if (scale_up) {
  1258. ret = ufshcd_scale_gear(hba, true);
  1259. if (ret) {
  1260. ufshcd_scale_clks(hba, hba->devfreq->previous_freq,
  1261. false);
  1262. goto out_unprepare;
  1263. }
  1264. }
  1265. out_unprepare:
  1266. ufshcd_clock_scaling_unprepare(hba, ret, scale_up);
  1267. return ret;
  1268. }
  1269. static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
  1270. {
  1271. struct ufs_hba *hba = container_of(work, struct ufs_hba,
  1272. clk_scaling.suspend_work);
  1273. unsigned long irq_flags;
  1274. spin_lock_irqsave(hba->host->host_lock, irq_flags);
  1275. if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
  1276. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1277. return;
  1278. }
  1279. hba->clk_scaling.is_suspended = true;
  1280. hba->clk_scaling.window_start_t = 0;
  1281. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1282. devfreq_suspend_device(hba->devfreq);
  1283. }
  1284. static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
  1285. {
  1286. struct ufs_hba *hba = container_of(work, struct ufs_hba,
  1287. clk_scaling.resume_work);
  1288. unsigned long irq_flags;
  1289. spin_lock_irqsave(hba->host->host_lock, irq_flags);
  1290. if (!hba->clk_scaling.is_suspended) {
  1291. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1292. return;
  1293. }
  1294. hba->clk_scaling.is_suspended = false;
  1295. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1296. devfreq_resume_device(hba->devfreq);
  1297. }
  1298. static int ufshcd_devfreq_target(struct device *dev,
  1299. unsigned long *freq, u32 flags)
  1300. {
  1301. int ret = 0;
  1302. struct ufs_hba *hba = dev_get_drvdata(dev);
  1303. ktime_t start;
  1304. bool scale_up = false, sched_clk_scaling_suspend_work = false;
  1305. struct list_head *clk_list = &hba->clk_list_head;
  1306. struct ufs_clk_info *clki;
  1307. unsigned long irq_flags;
  1308. if (!ufshcd_is_clkscaling_supported(hba))
  1309. return -EINVAL;
  1310. if (hba->use_pm_opp) {
  1311. struct dev_pm_opp *opp;
  1312. /* Get the recommended frequency from OPP framework */
  1313. opp = devfreq_recommended_opp(dev, freq, flags);
  1314. if (IS_ERR(opp))
  1315. return PTR_ERR(opp);
  1316. dev_pm_opp_put(opp);
  1317. } else {
  1318. /* Override with the closest supported frequency */
  1319. clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info,
  1320. list);
  1321. *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
  1322. }
  1323. spin_lock_irqsave(hba->host->host_lock, irq_flags);
  1324. if (ufshcd_eh_in_progress(hba)) {
  1325. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1326. return 0;
  1327. }
  1328. /* Skip scaling clock when clock scaling is suspended */
  1329. if (hba->clk_scaling.is_suspended) {
  1330. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1331. dev_warn(hba->dev, "clock scaling is suspended, skip");
  1332. return 0;
  1333. }
  1334. if (!hba->clk_scaling.active_reqs)
  1335. sched_clk_scaling_suspend_work = true;
  1336. if (list_empty(clk_list)) {
  1337. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1338. goto out;
  1339. }
  1340. /* Decide based on the target or rounded-off frequency and update */
  1341. if (hba->use_pm_opp)
  1342. scale_up = *freq > hba->clk_scaling.target_freq;
  1343. else
  1344. scale_up = *freq == clki->max_freq;
  1345. if (!hba->use_pm_opp && !scale_up)
  1346. *freq = clki->min_freq;
  1347. /* Update the frequency */
  1348. if (!ufshcd_is_devfreq_scaling_required(hba, *freq, scale_up)) {
  1349. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1350. ret = 0;
  1351. goto out; /* no state change required */
  1352. }
  1353. spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
  1354. start = ktime_get();
  1355. ret = ufshcd_devfreq_scale(hba, *freq, scale_up);
  1356. if (!ret)
  1357. hba->clk_scaling.target_freq = *freq;
  1358. trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
  1359. (scale_up ? "up" : "down"),
  1360. ktime_to_us(ktime_sub(ktime_get(), start)), ret);
  1361. out:
  1362. if (sched_clk_scaling_suspend_work &&
  1363. (!scale_up || hba->clk_scaling.suspend_on_no_request))
  1364. queue_work(hba->clk_scaling.workq,
  1365. &hba->clk_scaling.suspend_work);
  1366. return ret;
  1367. }
  1368. static int ufshcd_devfreq_get_dev_status(struct device *dev,
  1369. struct devfreq_dev_status *stat)
  1370. {
  1371. struct ufs_hba *hba = dev_get_drvdata(dev);
  1372. struct ufs_clk_scaling *scaling = &hba->clk_scaling;
  1373. unsigned long flags;
  1374. ktime_t curr_t;
  1375. if (!ufshcd_is_clkscaling_supported(hba))
  1376. return -EINVAL;
  1377. memset(stat, 0, sizeof(*stat));
  1378. spin_lock_irqsave(hba->host->host_lock, flags);
  1379. curr_t = ktime_get();
  1380. if (!scaling->window_start_t)
  1381. goto start_window;
  1382. /*
  1383. * If current frequency is 0, then the ondemand governor considers
  1384. * there's no initial frequency set. And it always requests to set
  1385. * to max. frequency.
  1386. */
  1387. if (hba->use_pm_opp) {
  1388. stat->current_frequency = hba->clk_scaling.target_freq;
  1389. } else {
  1390. struct list_head *clk_list = &hba->clk_list_head;
  1391. struct ufs_clk_info *clki;
  1392. clki = list_first_entry(clk_list, struct ufs_clk_info, list);
  1393. stat->current_frequency = clki->curr_freq;
  1394. }
  1395. if (scaling->is_busy_started)
  1396. scaling->tot_busy_t += ktime_us_delta(curr_t,
  1397. scaling->busy_start_t);
  1398. stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
  1399. stat->busy_time = scaling->tot_busy_t;
  1400. start_window:
  1401. scaling->window_start_t = curr_t;
  1402. scaling->tot_busy_t = 0;
  1403. if (scaling->active_reqs) {
  1404. scaling->busy_start_t = curr_t;
  1405. scaling->is_busy_started = true;
  1406. } else {
  1407. scaling->busy_start_t = 0;
  1408. scaling->is_busy_started = false;
  1409. }
  1410. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1411. return 0;
  1412. }
  1413. static int ufshcd_devfreq_init(struct ufs_hba *hba)
  1414. {
  1415. struct list_head *clk_list = &hba->clk_list_head;
  1416. struct ufs_clk_info *clki;
  1417. struct devfreq *devfreq;
  1418. int ret;
  1419. /* Skip devfreq if we don't have any clocks in the list */
  1420. if (list_empty(clk_list))
  1421. return 0;
  1422. if (!hba->use_pm_opp) {
  1423. clki = list_first_entry(clk_list, struct ufs_clk_info, list);
  1424. dev_pm_opp_add(hba->dev, clki->min_freq, 0);
  1425. dev_pm_opp_add(hba->dev, clki->max_freq, 0);
  1426. }
  1427. ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
  1428. &hba->vps->ondemand_data);
  1429. devfreq = devfreq_add_device(hba->dev,
  1430. &hba->vps->devfreq_profile,
  1431. DEVFREQ_GOV_SIMPLE_ONDEMAND,
  1432. &hba->vps->ondemand_data);
  1433. if (IS_ERR(devfreq)) {
  1434. ret = PTR_ERR(devfreq);
  1435. dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
  1436. if (!hba->use_pm_opp) {
  1437. dev_pm_opp_remove(hba->dev, clki->min_freq);
  1438. dev_pm_opp_remove(hba->dev, clki->max_freq);
  1439. }
  1440. return ret;
  1441. }
  1442. hba->devfreq = devfreq;
  1443. return 0;
  1444. }
  1445. static void ufshcd_devfreq_remove(struct ufs_hba *hba)
  1446. {
  1447. struct list_head *clk_list = &hba->clk_list_head;
  1448. if (!hba->devfreq)
  1449. return;
  1450. devfreq_remove_device(hba->devfreq);
  1451. hba->devfreq = NULL;
  1452. if (!hba->use_pm_opp) {
  1453. struct ufs_clk_info *clki;
  1454. clki = list_first_entry(clk_list, struct ufs_clk_info, list);
  1455. dev_pm_opp_remove(hba->dev, clki->min_freq);
  1456. dev_pm_opp_remove(hba->dev, clki->max_freq);
  1457. }
  1458. }
  1459. static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
  1460. {
  1461. unsigned long flags;
  1462. bool suspend = false;
  1463. cancel_work_sync(&hba->clk_scaling.suspend_work);
  1464. cancel_work_sync(&hba->clk_scaling.resume_work);
  1465. spin_lock_irqsave(hba->host->host_lock, flags);
  1466. if (!hba->clk_scaling.is_suspended) {
  1467. suspend = true;
  1468. hba->clk_scaling.is_suspended = true;
  1469. hba->clk_scaling.window_start_t = 0;
  1470. }
  1471. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1472. if (suspend)
  1473. devfreq_suspend_device(hba->devfreq);
  1474. }
  1475. static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
  1476. {
  1477. unsigned long flags;
  1478. bool resume = false;
  1479. spin_lock_irqsave(hba->host->host_lock, flags);
  1480. if (hba->clk_scaling.is_suspended) {
  1481. resume = true;
  1482. hba->clk_scaling.is_suspended = false;
  1483. }
  1484. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1485. if (resume)
  1486. devfreq_resume_device(hba->devfreq);
  1487. }
  1488. static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
  1489. struct device_attribute *attr, char *buf)
  1490. {
  1491. struct ufs_hba *hba = dev_get_drvdata(dev);
  1492. return sysfs_emit(buf, "%d\n", hba->clk_scaling.is_enabled);
  1493. }
  1494. static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
  1495. struct device_attribute *attr, const char *buf, size_t count)
  1496. {
  1497. struct ufs_hba *hba = dev_get_drvdata(dev);
  1498. u32 value;
  1499. int err = 0;
  1500. if (kstrtou32(buf, 0, &value))
  1501. return -EINVAL;
  1502. down(&hba->host_sem);
  1503. if (!ufshcd_is_user_access_allowed(hba)) {
  1504. err = -EBUSY;
  1505. goto out;
  1506. }
  1507. value = !!value;
  1508. if (value == hba->clk_scaling.is_enabled)
  1509. goto out;
  1510. ufshcd_rpm_get_sync(hba);
  1511. ufshcd_hold(hba);
  1512. hba->clk_scaling.is_enabled = value;
  1513. if (value) {
  1514. ufshcd_resume_clkscaling(hba);
  1515. } else {
  1516. ufshcd_suspend_clkscaling(hba);
  1517. err = ufshcd_devfreq_scale(hba, ULONG_MAX, true);
  1518. if (err)
  1519. dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
  1520. __func__, err);
  1521. }
  1522. ufshcd_release(hba);
  1523. ufshcd_rpm_put_sync(hba);
  1524. out:
  1525. up(&hba->host_sem);
  1526. return err ? err : count;
  1527. }
  1528. static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba)
  1529. {
  1530. hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
  1531. hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
  1532. sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
  1533. hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
  1534. hba->clk_scaling.enable_attr.attr.mode = 0644;
  1535. if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
  1536. dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
  1537. }
  1538. static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba)
  1539. {
  1540. if (hba->clk_scaling.enable_attr.attr.name)
  1541. device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
  1542. }
  1543. static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
  1544. {
  1545. if (!ufshcd_is_clkscaling_supported(hba))
  1546. return;
  1547. if (!hba->clk_scaling.min_gear)
  1548. hba->clk_scaling.min_gear = UFS_HS_G1;
  1549. INIT_WORK(&hba->clk_scaling.suspend_work,
  1550. ufshcd_clk_scaling_suspend_work);
  1551. INIT_WORK(&hba->clk_scaling.resume_work,
  1552. ufshcd_clk_scaling_resume_work);
  1553. hba->clk_scaling.workq = alloc_ordered_workqueue(
  1554. "ufs_clkscaling_%d", WQ_MEM_RECLAIM, hba->host->host_no);
  1555. hba->clk_scaling.is_initialized = true;
  1556. }
  1557. static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
  1558. {
  1559. if (!hba->clk_scaling.is_initialized)
  1560. return;
  1561. ufshcd_remove_clk_scaling_sysfs(hba);
  1562. destroy_workqueue(hba->clk_scaling.workq);
  1563. ufshcd_devfreq_remove(hba);
  1564. hba->clk_scaling.is_initialized = false;
  1565. }
  1566. static void ufshcd_ungate_work(struct work_struct *work)
  1567. {
  1568. int ret;
  1569. struct ufs_hba *hba = container_of(work, struct ufs_hba,
  1570. clk_gating.ungate_work);
  1571. cancel_delayed_work_sync(&hba->clk_gating.gate_work);
  1572. scoped_guard(spinlock_irqsave, &hba->clk_gating.lock) {
  1573. if (hba->clk_gating.state == CLKS_ON)
  1574. return;
  1575. }
  1576. ufshcd_hba_vreg_set_hpm(hba);
  1577. ufshcd_setup_clocks(hba, true);
  1578. ufshcd_enable_irq(hba);
  1579. /* Exit from hibern8 */
  1580. if (ufshcd_can_hibern8_during_gating(hba)) {
  1581. /* Prevent gating in this path */
  1582. hba->clk_gating.is_suspended = true;
  1583. if (ufshcd_is_link_hibern8(hba)) {
  1584. ret = ufshcd_uic_hibern8_exit(hba);
  1585. if (ret)
  1586. dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
  1587. __func__, ret);
  1588. else
  1589. ufshcd_set_link_active(hba);
  1590. }
  1591. hba->clk_gating.is_suspended = false;
  1592. }
  1593. }
  1594. /**
  1595. * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
  1596. * Also, exit from hibern8 mode and set the link as active.
  1597. * @hba: per adapter instance
  1598. */
  1599. void ufshcd_hold(struct ufs_hba *hba)
  1600. {
  1601. bool flush_result;
  1602. unsigned long flags;
  1603. if (!ufshcd_is_clkgating_allowed(hba) ||
  1604. !hba->clk_gating.is_initialized)
  1605. return;
  1606. spin_lock_irqsave(&hba->clk_gating.lock, flags);
  1607. hba->clk_gating.active_reqs++;
  1608. start:
  1609. switch (hba->clk_gating.state) {
  1610. case CLKS_ON:
  1611. /*
  1612. * Wait for the ungate work to complete if in progress.
  1613. * Though the clocks may be in ON state, the link could
  1614. * still be in hibner8 state if hibern8 is allowed
  1615. * during clock gating.
  1616. * Make sure we exit hibern8 state also in addition to
  1617. * clocks being ON.
  1618. */
  1619. if (ufshcd_can_hibern8_during_gating(hba) &&
  1620. ufshcd_is_link_hibern8(hba)) {
  1621. spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
  1622. flush_result = flush_work(&hba->clk_gating.ungate_work);
  1623. if (hba->clk_gating.is_suspended && !flush_result)
  1624. return;
  1625. spin_lock_irqsave(&hba->clk_gating.lock, flags);
  1626. goto start;
  1627. }
  1628. break;
  1629. case REQ_CLKS_OFF:
  1630. if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
  1631. hba->clk_gating.state = CLKS_ON;
  1632. trace_ufshcd_clk_gating(dev_name(hba->dev),
  1633. hba->clk_gating.state);
  1634. break;
  1635. }
  1636. /*
  1637. * If we are here, it means gating work is either done or
  1638. * currently running. Hence, fall through to cancel gating
  1639. * work and to enable clocks.
  1640. */
  1641. fallthrough;
  1642. case CLKS_OFF:
  1643. hba->clk_gating.state = REQ_CLKS_ON;
  1644. trace_ufshcd_clk_gating(dev_name(hba->dev),
  1645. hba->clk_gating.state);
  1646. queue_work(hba->clk_gating.clk_gating_workq,
  1647. &hba->clk_gating.ungate_work);
  1648. /*
  1649. * fall through to check if we should wait for this
  1650. * work to be done or not.
  1651. */
  1652. fallthrough;
  1653. case REQ_CLKS_ON:
  1654. spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
  1655. flush_work(&hba->clk_gating.ungate_work);
  1656. /* Make sure state is CLKS_ON before returning */
  1657. spin_lock_irqsave(&hba->clk_gating.lock, flags);
  1658. goto start;
  1659. default:
  1660. dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
  1661. __func__, hba->clk_gating.state);
  1662. break;
  1663. }
  1664. spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
  1665. }
  1666. EXPORT_SYMBOL_GPL(ufshcd_hold);
  1667. static void ufshcd_gate_work(struct work_struct *work)
  1668. {
  1669. struct ufs_hba *hba = container_of(work, struct ufs_hba,
  1670. clk_gating.gate_work.work);
  1671. int ret;
  1672. scoped_guard(spinlock_irqsave, &hba->clk_gating.lock) {
  1673. /*
  1674. * In case you are here to cancel this work the gating state
  1675. * would be marked as REQ_CLKS_ON. In this case save time by
  1676. * skipping the gating work and exit after changing the clock
  1677. * state to CLKS_ON.
  1678. */
  1679. if (hba->clk_gating.is_suspended ||
  1680. hba->clk_gating.state != REQ_CLKS_OFF) {
  1681. hba->clk_gating.state = CLKS_ON;
  1682. trace_ufshcd_clk_gating(dev_name(hba->dev),
  1683. hba->clk_gating.state);
  1684. return;
  1685. }
  1686. if (hba->clk_gating.active_reqs)
  1687. return;
  1688. }
  1689. scoped_guard(spinlock_irqsave, hba->host->host_lock) {
  1690. if (ufshcd_is_ufs_dev_busy(hba) ||
  1691. hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
  1692. return;
  1693. }
  1694. /* put the link into hibern8 mode before turning off clocks */
  1695. if (ufshcd_can_hibern8_during_gating(hba)) {
  1696. ret = ufshcd_uic_hibern8_enter(hba);
  1697. if (ret) {
  1698. hba->clk_gating.state = CLKS_ON;
  1699. dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
  1700. __func__, ret);
  1701. trace_ufshcd_clk_gating(dev_name(hba->dev),
  1702. hba->clk_gating.state);
  1703. return;
  1704. }
  1705. ufshcd_set_link_hibern8(hba);
  1706. }
  1707. ufshcd_disable_irq(hba);
  1708. ufshcd_setup_clocks(hba, false);
  1709. /* Put the host controller in low power mode if possible */
  1710. ufshcd_hba_vreg_set_lpm(hba);
  1711. /*
  1712. * In case you are here to cancel this work the gating state
  1713. * would be marked as REQ_CLKS_ON. In this case keep the state
  1714. * as REQ_CLKS_ON which would anyway imply that clocks are off
  1715. * and a request to turn them on is pending. By doing this way,
  1716. * we keep the state machine in tact and this would ultimately
  1717. * prevent from doing cancel work multiple times when there are
  1718. * new requests arriving before the current cancel work is done.
  1719. */
  1720. guard(spinlock_irqsave)(&hba->clk_gating.lock);
  1721. if (hba->clk_gating.state == REQ_CLKS_OFF) {
  1722. hba->clk_gating.state = CLKS_OFF;
  1723. trace_ufshcd_clk_gating(dev_name(hba->dev),
  1724. hba->clk_gating.state);
  1725. }
  1726. }
  1727. static void __ufshcd_release(struct ufs_hba *hba)
  1728. {
  1729. lockdep_assert_held(&hba->clk_gating.lock);
  1730. if (!ufshcd_is_clkgating_allowed(hba))
  1731. return;
  1732. hba->clk_gating.active_reqs--;
  1733. if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
  1734. !hba->clk_gating.is_initialized ||
  1735. hba->clk_gating.state == CLKS_OFF)
  1736. return;
  1737. scoped_guard(spinlock_irqsave, hba->host->host_lock) {
  1738. if (ufshcd_has_pending_tasks(hba) ||
  1739. hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
  1740. return;
  1741. }
  1742. hba->clk_gating.state = REQ_CLKS_OFF;
  1743. trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
  1744. queue_delayed_work(hba->clk_gating.clk_gating_workq,
  1745. &hba->clk_gating.gate_work,
  1746. msecs_to_jiffies(hba->clk_gating.delay_ms));
  1747. }
  1748. void ufshcd_release(struct ufs_hba *hba)
  1749. {
  1750. guard(spinlock_irqsave)(&hba->clk_gating.lock);
  1751. __ufshcd_release(hba);
  1752. }
  1753. EXPORT_SYMBOL_GPL(ufshcd_release);
  1754. static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
  1755. struct device_attribute *attr, char *buf)
  1756. {
  1757. struct ufs_hba *hba = dev_get_drvdata(dev);
  1758. return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms);
  1759. }
  1760. void ufshcd_clkgate_delay_set(struct device *dev, unsigned long value)
  1761. {
  1762. struct ufs_hba *hba = dev_get_drvdata(dev);
  1763. guard(spinlock_irqsave)(&hba->clk_gating.lock);
  1764. hba->clk_gating.delay_ms = value;
  1765. }
  1766. EXPORT_SYMBOL_GPL(ufshcd_clkgate_delay_set);
  1767. static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
  1768. struct device_attribute *attr, const char *buf, size_t count)
  1769. {
  1770. unsigned long value;
  1771. if (kstrtoul(buf, 0, &value))
  1772. return -EINVAL;
  1773. ufshcd_clkgate_delay_set(dev, value);
  1774. return count;
  1775. }
  1776. static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
  1777. struct device_attribute *attr, char *buf)
  1778. {
  1779. struct ufs_hba *hba = dev_get_drvdata(dev);
  1780. return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled);
  1781. }
  1782. static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
  1783. struct device_attribute *attr, const char *buf, size_t count)
  1784. {
  1785. struct ufs_hba *hba = dev_get_drvdata(dev);
  1786. u32 value;
  1787. if (kstrtou32(buf, 0, &value))
  1788. return -EINVAL;
  1789. value = !!value;
  1790. guard(spinlock_irqsave)(&hba->clk_gating.lock);
  1791. if (value == hba->clk_gating.is_enabled)
  1792. return count;
  1793. if (value)
  1794. __ufshcd_release(hba);
  1795. else
  1796. hba->clk_gating.active_reqs++;
  1797. hba->clk_gating.is_enabled = value;
  1798. return count;
  1799. }
  1800. static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba)
  1801. {
  1802. hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
  1803. hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
  1804. sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
  1805. hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
  1806. hba->clk_gating.delay_attr.attr.mode = 0644;
  1807. if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
  1808. dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
  1809. hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
  1810. hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
  1811. sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
  1812. hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
  1813. hba->clk_gating.enable_attr.attr.mode = 0644;
  1814. if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
  1815. dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
  1816. }
  1817. static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba)
  1818. {
  1819. if (hba->clk_gating.delay_attr.attr.name)
  1820. device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
  1821. if (hba->clk_gating.enable_attr.attr.name)
  1822. device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
  1823. }
  1824. static void ufshcd_init_clk_gating(struct ufs_hba *hba)
  1825. {
  1826. if (!ufshcd_is_clkgating_allowed(hba))
  1827. return;
  1828. hba->clk_gating.state = CLKS_ON;
  1829. hba->clk_gating.delay_ms = 150;
  1830. INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
  1831. INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
  1832. hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(
  1833. "ufs_clk_gating_%d", WQ_MEM_RECLAIM | WQ_HIGHPRI,
  1834. hba->host->host_no);
  1835. ufshcd_init_clk_gating_sysfs(hba);
  1836. hba->clk_gating.is_enabled = true;
  1837. hba->clk_gating.is_initialized = true;
  1838. }
  1839. static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
  1840. {
  1841. if (!hba->clk_gating.is_initialized)
  1842. return;
  1843. ufshcd_remove_clk_gating_sysfs(hba);
  1844. /* Ungate the clock if necessary. */
  1845. ufshcd_hold(hba);
  1846. hba->clk_gating.is_initialized = false;
  1847. ufshcd_release(hba);
  1848. destroy_workqueue(hba->clk_gating.clk_gating_workq);
  1849. }
  1850. static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
  1851. {
  1852. bool queue_resume_work = false;
  1853. ktime_t curr_t = ktime_get();
  1854. unsigned long flags;
  1855. if (!ufshcd_is_clkscaling_supported(hba))
  1856. return;
  1857. spin_lock_irqsave(hba->host->host_lock, flags);
  1858. if (!hba->clk_scaling.active_reqs++)
  1859. queue_resume_work = true;
  1860. if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) {
  1861. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1862. return;
  1863. }
  1864. if (queue_resume_work)
  1865. queue_work(hba->clk_scaling.workq,
  1866. &hba->clk_scaling.resume_work);
  1867. if (!hba->clk_scaling.window_start_t) {
  1868. hba->clk_scaling.window_start_t = curr_t;
  1869. hba->clk_scaling.tot_busy_t = 0;
  1870. hba->clk_scaling.is_busy_started = false;
  1871. }
  1872. if (!hba->clk_scaling.is_busy_started) {
  1873. hba->clk_scaling.busy_start_t = curr_t;
  1874. hba->clk_scaling.is_busy_started = true;
  1875. }
  1876. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1877. }
  1878. static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
  1879. {
  1880. struct ufs_clk_scaling *scaling = &hba->clk_scaling;
  1881. unsigned long flags;
  1882. if (!ufshcd_is_clkscaling_supported(hba))
  1883. return;
  1884. spin_lock_irqsave(hba->host->host_lock, flags);
  1885. hba->clk_scaling.active_reqs--;
  1886. if (!scaling->active_reqs && scaling->is_busy_started) {
  1887. scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
  1888. scaling->busy_start_t));
  1889. scaling->busy_start_t = 0;
  1890. scaling->is_busy_started = false;
  1891. }
  1892. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1893. }
  1894. static inline int ufshcd_monitor_opcode2dir(u8 opcode)
  1895. {
  1896. if (opcode == READ_6 || opcode == READ_10 || opcode == READ_16)
  1897. return READ;
  1898. else if (opcode == WRITE_6 || opcode == WRITE_10 || opcode == WRITE_16)
  1899. return WRITE;
  1900. else
  1901. return -EINVAL;
  1902. }
  1903. static inline bool ufshcd_should_inform_monitor(struct ufs_hba *hba,
  1904. struct ufshcd_lrb *lrbp)
  1905. {
  1906. const struct ufs_hba_monitor *m = &hba->monitor;
  1907. return (m->enabled && lrbp && lrbp->cmd &&
  1908. (!m->chunk_size || m->chunk_size == lrbp->cmd->sdb.length) &&
  1909. ktime_before(hba->monitor.enabled_ts, lrbp->issue_time_stamp));
  1910. }
  1911. static void ufshcd_start_monitor(struct ufs_hba *hba,
  1912. const struct ufshcd_lrb *lrbp)
  1913. {
  1914. int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd);
  1915. unsigned long flags;
  1916. spin_lock_irqsave(hba->host->host_lock, flags);
  1917. if (dir >= 0 && hba->monitor.nr_queued[dir]++ == 0)
  1918. hba->monitor.busy_start_ts[dir] = ktime_get();
  1919. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1920. }
  1921. static void ufshcd_update_monitor(struct ufs_hba *hba, const struct ufshcd_lrb *lrbp)
  1922. {
  1923. int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd);
  1924. unsigned long flags;
  1925. spin_lock_irqsave(hba->host->host_lock, flags);
  1926. if (dir >= 0 && hba->monitor.nr_queued[dir] > 0) {
  1927. const struct request *req = scsi_cmd_to_rq(lrbp->cmd);
  1928. struct ufs_hba_monitor *m = &hba->monitor;
  1929. ktime_t now, inc, lat;
  1930. now = lrbp->compl_time_stamp;
  1931. inc = ktime_sub(now, m->busy_start_ts[dir]);
  1932. m->total_busy[dir] = ktime_add(m->total_busy[dir], inc);
  1933. m->nr_sec_rw[dir] += blk_rq_sectors(req);
  1934. /* Update latencies */
  1935. m->nr_req[dir]++;
  1936. lat = ktime_sub(now, lrbp->issue_time_stamp);
  1937. m->lat_sum[dir] += lat;
  1938. if (m->lat_max[dir] < lat || !m->lat_max[dir])
  1939. m->lat_max[dir] = lat;
  1940. if (m->lat_min[dir] > lat || !m->lat_min[dir])
  1941. m->lat_min[dir] = lat;
  1942. m->nr_queued[dir]--;
  1943. /* Push forward the busy start of monitor */
  1944. m->busy_start_ts[dir] = now;
  1945. }
  1946. spin_unlock_irqrestore(hba->host->host_lock, flags);
  1947. }
  1948. /**
  1949. * ufshcd_send_command - Send SCSI or device management commands
  1950. * @hba: per adapter instance
  1951. * @task_tag: Task tag of the command
  1952. * @hwq: pointer to hardware queue instance
  1953. */
  1954. static inline
  1955. void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag,
  1956. struct ufs_hw_queue *hwq)
  1957. {
  1958. struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
  1959. unsigned long flags;
  1960. lrbp->issue_time_stamp = ktime_get();
  1961. lrbp->issue_time_stamp_local_clock = local_clock();
  1962. lrbp->compl_time_stamp = ktime_set(0, 0);
  1963. lrbp->compl_time_stamp_local_clock = 0;
  1964. ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND);
  1965. if (lrbp->cmd)
  1966. ufshcd_clk_scaling_start_busy(hba);
  1967. if (unlikely(ufshcd_should_inform_monitor(hba, lrbp)))
  1968. ufshcd_start_monitor(hba, lrbp);
  1969. if (hba->mcq_enabled) {
  1970. int utrd_size = sizeof(struct utp_transfer_req_desc);
  1971. struct utp_transfer_req_desc *src = lrbp->utr_descriptor_ptr;
  1972. struct utp_transfer_req_desc *dest;
  1973. spin_lock(&hwq->sq_lock);
  1974. dest = hwq->sqe_base_addr + hwq->sq_tail_slot;
  1975. memcpy(dest, src, utrd_size);
  1976. ufshcd_inc_sq_tail(hwq);
  1977. spin_unlock(&hwq->sq_lock);
  1978. } else {
  1979. spin_lock_irqsave(&hba->outstanding_lock, flags);
  1980. if (hba->vops && hba->vops->setup_xfer_req)
  1981. hba->vops->setup_xfer_req(hba, lrbp->task_tag,
  1982. !!lrbp->cmd);
  1983. __set_bit(lrbp->task_tag, &hba->outstanding_reqs);
  1984. ufshcd_writel(hba, 1 << lrbp->task_tag,
  1985. REG_UTP_TRANSFER_REQ_DOOR_BELL);
  1986. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  1987. }
  1988. }
  1989. /**
  1990. * ufshcd_copy_sense_data - Copy sense data in case of check condition
  1991. * @lrbp: pointer to local reference block
  1992. */
  1993. static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
  1994. {
  1995. u8 *const sense_buffer = lrbp->cmd->sense_buffer;
  1996. u16 resp_len;
  1997. int len;
  1998. resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header.data_segment_length);
  1999. if (sense_buffer && resp_len) {
  2000. int len_to_copy;
  2001. len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
  2002. len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
  2003. memcpy(sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
  2004. len_to_copy);
  2005. }
  2006. }
  2007. /**
  2008. * ufshcd_copy_query_response() - Copy the Query Response and the data
  2009. * descriptor
  2010. * @hba: per adapter instance
  2011. * @lrbp: pointer to local reference block
  2012. *
  2013. * Return: 0 upon success; < 0 upon failure.
  2014. */
  2015. static
  2016. int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  2017. {
  2018. struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
  2019. memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
  2020. /* Get the descriptor */
  2021. if (hba->dev_cmd.query.descriptor &&
  2022. lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
  2023. u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
  2024. GENERAL_UPIU_REQUEST_SIZE;
  2025. u16 resp_len;
  2026. u16 buf_len;
  2027. /* data segment length */
  2028. resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header
  2029. .data_segment_length);
  2030. buf_len = be16_to_cpu(
  2031. hba->dev_cmd.query.request.upiu_req.length);
  2032. if (likely(buf_len >= resp_len)) {
  2033. memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
  2034. } else {
  2035. dev_warn(hba->dev,
  2036. "%s: rsp size %d is bigger than buffer size %d",
  2037. __func__, resp_len, buf_len);
  2038. return -EINVAL;
  2039. }
  2040. }
  2041. return 0;
  2042. }
  2043. /**
  2044. * ufshcd_hba_capabilities - Read controller capabilities
  2045. * @hba: per adapter instance
  2046. *
  2047. * Return: 0 on success, negative on error.
  2048. */
  2049. static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
  2050. {
  2051. int err;
  2052. hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
  2053. /* nutrs and nutmrs are 0 based values */
  2054. hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS_SDB) + 1;
  2055. hba->nutmrs =
  2056. ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
  2057. hba->reserved_slot = hba->nutrs - 1;
  2058. hba->nortt = FIELD_GET(MASK_NUMBER_OUTSTANDING_RTT, hba->capabilities) + 1;
  2059. /* Read crypto capabilities */
  2060. err = ufshcd_hba_init_crypto_capabilities(hba);
  2061. if (err) {
  2062. dev_err(hba->dev, "crypto setup failed\n");
  2063. return err;
  2064. }
  2065. /*
  2066. * The UFSHCI 3.0 specification does not define MCQ_SUPPORT and
  2067. * LSDB_SUPPORT, but [31:29] as reserved bits with reset value 0s, which
  2068. * means we can simply read values regardless of version.
  2069. */
  2070. hba->mcq_sup = FIELD_GET(MASK_MCQ_SUPPORT, hba->capabilities);
  2071. /*
  2072. * 0h: legacy single doorbell support is available
  2073. * 1h: indicate that legacy single doorbell support has been removed
  2074. */
  2075. if (!(hba->quirks & UFSHCD_QUIRK_BROKEN_LSDBS_CAP))
  2076. hba->lsdb_sup = !FIELD_GET(MASK_LSDB_SUPPORT, hba->capabilities);
  2077. else
  2078. hba->lsdb_sup = true;
  2079. if (!hba->mcq_sup)
  2080. return 0;
  2081. hba->mcq_capabilities = ufshcd_readl(hba, REG_MCQCAP);
  2082. hba->ext_iid_sup = FIELD_GET(MASK_EXT_IID_SUPPORT,
  2083. hba->mcq_capabilities);
  2084. return 0;
  2085. }
  2086. /**
  2087. * ufshcd_ready_for_uic_cmd - Check if controller is ready
  2088. * to accept UIC commands
  2089. * @hba: per adapter instance
  2090. *
  2091. * Return: true on success, else false.
  2092. */
  2093. static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
  2094. {
  2095. u32 val;
  2096. int ret = read_poll_timeout(ufshcd_readl, val, val & UIC_COMMAND_READY,
  2097. 500, uic_cmd_timeout * 1000, false, hba,
  2098. REG_CONTROLLER_STATUS);
  2099. return ret == 0;
  2100. }
  2101. /**
  2102. * ufshcd_get_upmcrs - Get the power mode change request status
  2103. * @hba: Pointer to adapter instance
  2104. *
  2105. * This function gets the UPMCRS field of HCS register
  2106. *
  2107. * Return: value of UPMCRS field.
  2108. */
  2109. static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
  2110. {
  2111. return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
  2112. }
  2113. /**
  2114. * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer
  2115. * @hba: per adapter instance
  2116. * @uic_cmd: UIC command
  2117. */
  2118. static inline void
  2119. ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  2120. {
  2121. lockdep_assert_held(&hba->uic_cmd_mutex);
  2122. WARN_ON(hba->active_uic_cmd);
  2123. hba->active_uic_cmd = uic_cmd;
  2124. /* Write Args */
  2125. ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
  2126. ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
  2127. ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
  2128. ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND);
  2129. /* Write UIC Cmd */
  2130. ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
  2131. REG_UIC_COMMAND);
  2132. }
  2133. /**
  2134. * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command
  2135. * @hba: per adapter instance
  2136. * @uic_cmd: UIC command
  2137. *
  2138. * Return: 0 only if success.
  2139. */
  2140. static int
  2141. ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  2142. {
  2143. int ret;
  2144. unsigned long flags;
  2145. lockdep_assert_held(&hba->uic_cmd_mutex);
  2146. if (wait_for_completion_timeout(&uic_cmd->done,
  2147. msecs_to_jiffies(uic_cmd_timeout))) {
  2148. ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
  2149. } else {
  2150. ret = -ETIMEDOUT;
  2151. dev_err(hba->dev,
  2152. "uic cmd 0x%x with arg3 0x%x completion timeout\n",
  2153. uic_cmd->command, uic_cmd->argument3);
  2154. if (!uic_cmd->cmd_active) {
  2155. dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n",
  2156. __func__);
  2157. ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
  2158. }
  2159. }
  2160. spin_lock_irqsave(hba->host->host_lock, flags);
  2161. hba->active_uic_cmd = NULL;
  2162. spin_unlock_irqrestore(hba->host->host_lock, flags);
  2163. return ret;
  2164. }
  2165. /**
  2166. * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  2167. * @hba: per adapter instance
  2168. * @uic_cmd: UIC command
  2169. *
  2170. * Return: 0 only if success.
  2171. */
  2172. static int
  2173. __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  2174. {
  2175. lockdep_assert_held(&hba->uic_cmd_mutex);
  2176. if (!ufshcd_ready_for_uic_cmd(hba)) {
  2177. dev_err(hba->dev,
  2178. "Controller not ready to accept UIC commands\n");
  2179. return -EIO;
  2180. }
  2181. init_completion(&uic_cmd->done);
  2182. uic_cmd->cmd_active = 1;
  2183. ufshcd_dispatch_uic_cmd(hba, uic_cmd);
  2184. return 0;
  2185. }
  2186. /**
  2187. * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  2188. * @hba: per adapter instance
  2189. * @uic_cmd: UIC command
  2190. *
  2191. * Return: 0 only if success.
  2192. */
  2193. int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  2194. {
  2195. int ret;
  2196. if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD)
  2197. return 0;
  2198. ufshcd_hold(hba);
  2199. mutex_lock(&hba->uic_cmd_mutex);
  2200. ufshcd_add_delay_before_dme_cmd(hba);
  2201. ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
  2202. if (!ret)
  2203. ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
  2204. mutex_unlock(&hba->uic_cmd_mutex);
  2205. ufshcd_release(hba);
  2206. return ret;
  2207. }
  2208. /**
  2209. * ufshcd_sgl_to_prdt - SG list to PRTD (Physical Region Description Table, 4DW format)
  2210. * @hba: per-adapter instance
  2211. * @lrbp: pointer to local reference block
  2212. * @sg_entries: The number of sg lists actually used
  2213. * @sg_list: Pointer to SG list
  2214. */
  2215. static void ufshcd_sgl_to_prdt(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, int sg_entries,
  2216. struct scatterlist *sg_list)
  2217. {
  2218. struct ufshcd_sg_entry *prd;
  2219. struct scatterlist *sg;
  2220. int i;
  2221. if (sg_entries) {
  2222. if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
  2223. lrbp->utr_descriptor_ptr->prd_table_length =
  2224. cpu_to_le16(sg_entries * ufshcd_sg_entry_size(hba));
  2225. else
  2226. lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16(sg_entries);
  2227. prd = lrbp->ucd_prdt_ptr;
  2228. for_each_sg(sg_list, sg, sg_entries, i) {
  2229. const unsigned int len = sg_dma_len(sg);
  2230. /*
  2231. * From the UFSHCI spec: "Data Byte Count (DBC): A '0'
  2232. * based value that indicates the length, in bytes, of
  2233. * the data block. A maximum of length of 256KB may
  2234. * exist for any entry. Bits 1:0 of this field shall be
  2235. * 11b to indicate Dword granularity. A value of '3'
  2236. * indicates 4 bytes, '7' indicates 8 bytes, etc."
  2237. */
  2238. WARN_ONCE(len > SZ_256K, "len = %#x\n", len);
  2239. prd->size = cpu_to_le32(len - 1);
  2240. prd->addr = cpu_to_le64(sg->dma_address);
  2241. prd->reserved = 0;
  2242. prd = (void *)prd + ufshcd_sg_entry_size(hba);
  2243. }
  2244. } else {
  2245. lrbp->utr_descriptor_ptr->prd_table_length = 0;
  2246. }
  2247. }
  2248. /**
  2249. * ufshcd_map_sg - Map scatter-gather list to prdt
  2250. * @hba: per adapter instance
  2251. * @lrbp: pointer to local reference block
  2252. *
  2253. * Return: 0 in case of success, non-zero value in case of failure.
  2254. */
  2255. static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  2256. {
  2257. struct scsi_cmnd *cmd = lrbp->cmd;
  2258. int sg_segments = scsi_dma_map(cmd);
  2259. if (sg_segments < 0)
  2260. return sg_segments;
  2261. ufshcd_sgl_to_prdt(hba, lrbp, sg_segments, scsi_sglist(cmd));
  2262. return ufshcd_crypto_fill_prdt(hba, lrbp);
  2263. }
  2264. /**
  2265. * ufshcd_enable_intr - enable interrupts
  2266. * @hba: per adapter instance
  2267. * @intrs: interrupt bits
  2268. */
  2269. static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
  2270. {
  2271. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  2272. set |= intrs;
  2273. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  2274. }
  2275. /**
  2276. * ufshcd_disable_intr - disable interrupts
  2277. * @hba: per adapter instance
  2278. * @intrs: interrupt bits
  2279. */
  2280. static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
  2281. {
  2282. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  2283. set &= ~intrs;
  2284. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  2285. }
  2286. /**
  2287. * ufshcd_prepare_req_desc_hdr - Fill UTP Transfer request descriptor header according to request
  2288. * descriptor according to request
  2289. * @hba: per adapter instance
  2290. * @lrbp: pointer to local reference block
  2291. * @upiu_flags: flags required in the header
  2292. * @cmd_dir: requests data direction
  2293. * @ehs_length: Total EHS Length (in 32‐bytes units of all Extra Header Segments)
  2294. */
  2295. static void
  2296. ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
  2297. u8 *upiu_flags, enum dma_data_direction cmd_dir,
  2298. int ehs_length)
  2299. {
  2300. struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
  2301. struct request_desc_header *h = &req_desc->header;
  2302. enum utp_data_direction data_direction;
  2303. lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
  2304. *h = (typeof(*h)){ };
  2305. if (cmd_dir == DMA_FROM_DEVICE) {
  2306. data_direction = UTP_DEVICE_TO_HOST;
  2307. *upiu_flags = UPIU_CMD_FLAGS_READ;
  2308. } else if (cmd_dir == DMA_TO_DEVICE) {
  2309. data_direction = UTP_HOST_TO_DEVICE;
  2310. *upiu_flags = UPIU_CMD_FLAGS_WRITE;
  2311. } else {
  2312. data_direction = UTP_NO_DATA_TRANSFER;
  2313. *upiu_flags = UPIU_CMD_FLAGS_NONE;
  2314. }
  2315. h->command_type = lrbp->command_type;
  2316. h->data_direction = data_direction;
  2317. h->ehs_length = ehs_length;
  2318. if (lrbp->intr_cmd)
  2319. h->interrupt = 1;
  2320. /* Prepare crypto related dwords */
  2321. ufshcd_prepare_req_desc_hdr_crypto(lrbp, h);
  2322. /*
  2323. * assigning invalid value for command status. Controller
  2324. * updates OCS on command completion, with the command
  2325. * status
  2326. */
  2327. h->ocs = OCS_INVALID_COMMAND_STATUS;
  2328. req_desc->prd_table_length = 0;
  2329. }
  2330. /**
  2331. * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
  2332. * for scsi commands
  2333. * @lrbp: local reference block pointer
  2334. * @upiu_flags: flags
  2335. */
  2336. static
  2337. void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
  2338. {
  2339. struct scsi_cmnd *cmd = lrbp->cmd;
  2340. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  2341. unsigned short cdb_len;
  2342. ucd_req_ptr->header = (struct utp_upiu_header){
  2343. .transaction_code = UPIU_TRANSACTION_COMMAND,
  2344. .flags = upiu_flags,
  2345. .lun = lrbp->lun,
  2346. .task_tag = lrbp->task_tag,
  2347. .command_set_type = UPIU_COMMAND_SET_TYPE_SCSI,
  2348. };
  2349. WARN_ON_ONCE(ucd_req_ptr->header.task_tag != lrbp->task_tag);
  2350. ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
  2351. cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
  2352. memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
  2353. memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
  2354. memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
  2355. }
  2356. /**
  2357. * ufshcd_prepare_utp_query_req_upiu() - fill the utp_transfer_req_desc for query request
  2358. * @hba: UFS hba
  2359. * @lrbp: local reference block pointer
  2360. * @upiu_flags: flags
  2361. */
  2362. static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
  2363. struct ufshcd_lrb *lrbp, u8 upiu_flags)
  2364. {
  2365. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  2366. struct ufs_query *query = &hba->dev_cmd.query;
  2367. u16 len = be16_to_cpu(query->request.upiu_req.length);
  2368. /* Query request header */
  2369. ucd_req_ptr->header = (struct utp_upiu_header){
  2370. .transaction_code = UPIU_TRANSACTION_QUERY_REQ,
  2371. .flags = upiu_flags,
  2372. .lun = lrbp->lun,
  2373. .task_tag = lrbp->task_tag,
  2374. .query_function = query->request.query_func,
  2375. /* Data segment length only need for WRITE_DESC */
  2376. .data_segment_length =
  2377. query->request.upiu_req.opcode ==
  2378. UPIU_QUERY_OPCODE_WRITE_DESC ?
  2379. cpu_to_be16(len) :
  2380. 0,
  2381. };
  2382. /* Copy the Query Request buffer as is */
  2383. memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
  2384. QUERY_OSF_SIZE);
  2385. /* Copy the Descriptor */
  2386. if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
  2387. memcpy(ucd_req_ptr + 1, query->descriptor, len);
  2388. memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
  2389. }
  2390. static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
  2391. {
  2392. struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
  2393. memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
  2394. ucd_req_ptr->header = (struct utp_upiu_header){
  2395. .transaction_code = UPIU_TRANSACTION_NOP_OUT,
  2396. .task_tag = lrbp->task_tag,
  2397. };
  2398. memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
  2399. }
  2400. /**
  2401. * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
  2402. * for Device Management Purposes
  2403. * @hba: per adapter instance
  2404. * @lrbp: pointer to local reference block
  2405. *
  2406. * Return: 0 upon success; < 0 upon failure.
  2407. */
  2408. static int ufshcd_compose_devman_upiu(struct ufs_hba *hba,
  2409. struct ufshcd_lrb *lrbp)
  2410. {
  2411. u8 upiu_flags;
  2412. int ret = 0;
  2413. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, 0);
  2414. if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
  2415. ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
  2416. else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
  2417. ufshcd_prepare_utp_nop_upiu(lrbp);
  2418. else
  2419. ret = -EINVAL;
  2420. return ret;
  2421. }
  2422. /**
  2423. * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
  2424. * for SCSI Purposes
  2425. * @hba: per adapter instance
  2426. * @lrbp: pointer to local reference block
  2427. */
  2428. static void ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  2429. {
  2430. struct request *rq = scsi_cmd_to_rq(lrbp->cmd);
  2431. unsigned int ioprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
  2432. u8 upiu_flags;
  2433. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, lrbp->cmd->sc_data_direction, 0);
  2434. if (ioprio_class == IOPRIO_CLASS_RT)
  2435. upiu_flags |= UPIU_CMD_FLAGS_CP;
  2436. ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
  2437. }
  2438. /**
  2439. * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
  2440. * @upiu_wlun_id: UPIU W-LUN id
  2441. *
  2442. * Return: SCSI W-LUN id.
  2443. */
  2444. static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
  2445. {
  2446. return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
  2447. }
  2448. static inline bool is_device_wlun(struct scsi_device *sdev)
  2449. {
  2450. return sdev->lun ==
  2451. ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN);
  2452. }
  2453. /*
  2454. * Associate the UFS controller queue with the default and poll HCTX types.
  2455. * Initialize the mq_map[] arrays.
  2456. */
  2457. static void ufshcd_map_queues(struct Scsi_Host *shost)
  2458. {
  2459. struct ufs_hba *hba = shost_priv(shost);
  2460. int i, queue_offset = 0;
  2461. if (!is_mcq_supported(hba)) {
  2462. hba->nr_queues[HCTX_TYPE_DEFAULT] = 1;
  2463. hba->nr_queues[HCTX_TYPE_READ] = 0;
  2464. hba->nr_queues[HCTX_TYPE_POLL] = 1;
  2465. hba->nr_hw_queues = 1;
  2466. }
  2467. for (i = 0; i < shost->nr_maps; i++) {
  2468. struct blk_mq_queue_map *map = &shost->tag_set.map[i];
  2469. map->nr_queues = hba->nr_queues[i];
  2470. if (!map->nr_queues)
  2471. continue;
  2472. map->queue_offset = queue_offset;
  2473. if (i == HCTX_TYPE_POLL && !is_mcq_supported(hba))
  2474. map->queue_offset = 0;
  2475. blk_mq_map_queues(map);
  2476. queue_offset += map->nr_queues;
  2477. }
  2478. }
  2479. static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
  2480. {
  2481. struct utp_transfer_cmd_desc *cmd_descp = (void *)hba->ucdl_base_addr +
  2482. i * ufshcd_get_ucd_size(hba);
  2483. struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
  2484. dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
  2485. i * ufshcd_get_ucd_size(hba);
  2486. u16 response_offset = le16_to_cpu(utrdlp[i].response_upiu_offset);
  2487. u16 prdt_offset = le16_to_cpu(utrdlp[i].prd_table_offset);
  2488. lrb->utr_descriptor_ptr = utrdlp + i;
  2489. lrb->utrd_dma_addr = hba->utrdl_dma_addr +
  2490. i * sizeof(struct utp_transfer_req_desc);
  2491. lrb->ucd_req_ptr = (struct utp_upiu_req *)cmd_descp->command_upiu;
  2492. lrb->ucd_req_dma_addr = cmd_desc_element_addr;
  2493. lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp->response_upiu;
  2494. lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
  2495. lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp->prd_table;
  2496. lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
  2497. }
  2498. /**
  2499. * ufshcd_queuecommand - main entry point for SCSI requests
  2500. * @host: SCSI host pointer
  2501. * @cmd: command from SCSI Midlayer
  2502. *
  2503. * Return: 0 for success, non-zero in case of failure.
  2504. */
  2505. static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  2506. {
  2507. struct ufs_hba *hba = shost_priv(host);
  2508. int tag = scsi_cmd_to_rq(cmd)->tag;
  2509. struct ufshcd_lrb *lrbp;
  2510. int err = 0;
  2511. struct ufs_hw_queue *hwq = NULL;
  2512. switch (hba->ufshcd_state) {
  2513. case UFSHCD_STATE_OPERATIONAL:
  2514. break;
  2515. case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL:
  2516. /*
  2517. * SCSI error handler can call ->queuecommand() while UFS error
  2518. * handler is in progress. Error interrupts could change the
  2519. * state from UFSHCD_STATE_RESET to
  2520. * UFSHCD_STATE_EH_SCHEDULED_NON_FATAL. Prevent requests
  2521. * being issued in that case.
  2522. */
  2523. if (ufshcd_eh_in_progress(hba)) {
  2524. err = SCSI_MLQUEUE_HOST_BUSY;
  2525. goto out;
  2526. }
  2527. break;
  2528. case UFSHCD_STATE_EH_SCHEDULED_FATAL:
  2529. /*
  2530. * pm_runtime_get_sync() is used at error handling preparation
  2531. * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's
  2532. * PM ops, it can never be finished if we let SCSI layer keep
  2533. * retrying it, which gets err handler stuck forever. Neither
  2534. * can we let the scsi cmd pass through, because UFS is in bad
  2535. * state, the scsi cmd may eventually time out, which will get
  2536. * err handler blocked for too long. So, just fail the scsi cmd
  2537. * sent from PM ops, err handler can recover PM error anyways.
  2538. */
  2539. if (hba->pm_op_in_progress) {
  2540. hba->force_reset = true;
  2541. set_host_byte(cmd, DID_BAD_TARGET);
  2542. scsi_done(cmd);
  2543. goto out;
  2544. }
  2545. fallthrough;
  2546. case UFSHCD_STATE_RESET:
  2547. err = SCSI_MLQUEUE_HOST_BUSY;
  2548. goto out;
  2549. case UFSHCD_STATE_ERROR:
  2550. set_host_byte(cmd, DID_ERROR);
  2551. scsi_done(cmd);
  2552. goto out;
  2553. }
  2554. hba->req_abort_count = 0;
  2555. ufshcd_hold(hba);
  2556. lrbp = &hba->lrb[tag];
  2557. lrbp->cmd = cmd;
  2558. lrbp->task_tag = tag;
  2559. lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
  2560. lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba);
  2561. ufshcd_prepare_lrbp_crypto(scsi_cmd_to_rq(cmd), lrbp);
  2562. lrbp->req_abort_skip = false;
  2563. ufshcd_comp_scsi_upiu(hba, lrbp);
  2564. err = ufshcd_map_sg(hba, lrbp);
  2565. if (err) {
  2566. ufshcd_release(hba);
  2567. goto out;
  2568. }
  2569. if (hba->mcq_enabled)
  2570. hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd));
  2571. ufshcd_send_command(hba, tag, hwq);
  2572. out:
  2573. if (ufs_trigger_eh(hba)) {
  2574. unsigned long flags;
  2575. spin_lock_irqsave(hba->host->host_lock, flags);
  2576. ufshcd_schedule_eh_work(hba);
  2577. spin_unlock_irqrestore(hba->host->host_lock, flags);
  2578. }
  2579. return err;
  2580. }
  2581. static void ufshcd_setup_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
  2582. enum dev_cmd_type cmd_type, u8 lun, int tag)
  2583. {
  2584. lrbp->cmd = NULL;
  2585. lrbp->task_tag = tag;
  2586. lrbp->lun = lun;
  2587. lrbp->intr_cmd = true; /* No interrupt aggregation */
  2588. ufshcd_prepare_lrbp_crypto(NULL, lrbp);
  2589. hba->dev_cmd.type = cmd_type;
  2590. }
  2591. static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
  2592. struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
  2593. {
  2594. ufshcd_setup_dev_cmd(hba, lrbp, cmd_type, 0, tag);
  2595. return ufshcd_compose_devman_upiu(hba, lrbp);
  2596. }
  2597. /*
  2598. * Check with the block layer if the command is inflight
  2599. * @cmd: command to check.
  2600. *
  2601. * Return: true if command is inflight; false if not.
  2602. */
  2603. bool ufshcd_cmd_inflight(struct scsi_cmnd *cmd)
  2604. {
  2605. return cmd && blk_mq_rq_state(scsi_cmd_to_rq(cmd)) == MQ_RQ_IN_FLIGHT;
  2606. }
  2607. /*
  2608. * Clear the pending command in the controller and wait until
  2609. * the controller confirms that the command has been cleared.
  2610. * @hba: per adapter instance
  2611. * @task_tag: The tag number of the command to be cleared.
  2612. */
  2613. static int ufshcd_clear_cmd(struct ufs_hba *hba, u32 task_tag)
  2614. {
  2615. u32 mask;
  2616. unsigned long flags;
  2617. int err;
  2618. if (hba->mcq_enabled) {
  2619. /*
  2620. * MCQ mode. Clean up the MCQ resources similar to
  2621. * what the ufshcd_utrl_clear() does for SDB mode.
  2622. */
  2623. err = ufshcd_mcq_sq_cleanup(hba, task_tag);
  2624. if (err) {
  2625. dev_err(hba->dev, "%s: failed tag=%d. err=%d\n",
  2626. __func__, task_tag, err);
  2627. return err;
  2628. }
  2629. return 0;
  2630. }
  2631. mask = 1U << task_tag;
  2632. /* clear outstanding transaction before retry */
  2633. spin_lock_irqsave(hba->host->host_lock, flags);
  2634. ufshcd_utrl_clear(hba, mask);
  2635. spin_unlock_irqrestore(hba->host->host_lock, flags);
  2636. /*
  2637. * wait for h/w to clear corresponding bit in door-bell.
  2638. * max. wait is 1 sec.
  2639. */
  2640. return ufshcd_wait_for_register(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL,
  2641. mask, ~mask, 1000, 1000);
  2642. }
  2643. /**
  2644. * ufshcd_dev_cmd_completion() - handles device management command responses
  2645. * @hba: per adapter instance
  2646. * @lrbp: pointer to local reference block
  2647. *
  2648. * Return: 0 upon success; < 0 upon failure.
  2649. */
  2650. static int
  2651. ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
  2652. {
  2653. enum upiu_response_transaction resp;
  2654. int err = 0;
  2655. hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
  2656. resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
  2657. switch (resp) {
  2658. case UPIU_TRANSACTION_NOP_IN:
  2659. if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
  2660. err = -EINVAL;
  2661. dev_err(hba->dev, "%s: unexpected response %x\n",
  2662. __func__, resp);
  2663. }
  2664. break;
  2665. case UPIU_TRANSACTION_QUERY_RSP: {
  2666. u8 response = lrbp->ucd_rsp_ptr->header.response;
  2667. if (response == 0) {
  2668. err = ufshcd_copy_query_response(hba, lrbp);
  2669. } else {
  2670. err = -EINVAL;
  2671. dev_err(hba->dev, "%s: unexpected response in Query RSP: %x\n",
  2672. __func__, response);
  2673. }
  2674. break;
  2675. }
  2676. case UPIU_TRANSACTION_REJECT_UPIU:
  2677. /* TODO: handle Reject UPIU Response */
  2678. err = -EPERM;
  2679. dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
  2680. __func__);
  2681. break;
  2682. case UPIU_TRANSACTION_RESPONSE:
  2683. if (hba->dev_cmd.type != DEV_CMD_TYPE_RPMB) {
  2684. err = -EINVAL;
  2685. dev_err(hba->dev, "%s: unexpected response %x\n", __func__, resp);
  2686. }
  2687. break;
  2688. default:
  2689. err = -EINVAL;
  2690. dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
  2691. __func__, resp);
  2692. break;
  2693. }
  2694. return err;
  2695. }
  2696. static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
  2697. struct ufshcd_lrb *lrbp, int max_timeout)
  2698. {
  2699. unsigned long time_left = msecs_to_jiffies(max_timeout);
  2700. unsigned long flags;
  2701. bool pending;
  2702. int err;
  2703. retry:
  2704. time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
  2705. time_left);
  2706. if (likely(time_left)) {
  2707. /*
  2708. * The completion handler called complete() and the caller of
  2709. * this function still owns the @lrbp tag so the code below does
  2710. * not trigger any race conditions.
  2711. */
  2712. hba->dev_cmd.complete = NULL;
  2713. err = ufshcd_get_tr_ocs(lrbp, NULL);
  2714. if (!err)
  2715. err = ufshcd_dev_cmd_completion(hba, lrbp);
  2716. } else {
  2717. err = -ETIMEDOUT;
  2718. dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
  2719. __func__, lrbp->task_tag);
  2720. /* MCQ mode */
  2721. if (hba->mcq_enabled) {
  2722. /* successfully cleared the command, retry if needed */
  2723. if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0)
  2724. err = -EAGAIN;
  2725. hba->dev_cmd.complete = NULL;
  2726. return err;
  2727. }
  2728. /* SDB mode */
  2729. if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) {
  2730. /* successfully cleared the command, retry if needed */
  2731. err = -EAGAIN;
  2732. /*
  2733. * Since clearing the command succeeded we also need to
  2734. * clear the task tag bit from the outstanding_reqs
  2735. * variable.
  2736. */
  2737. spin_lock_irqsave(&hba->outstanding_lock, flags);
  2738. pending = test_bit(lrbp->task_tag,
  2739. &hba->outstanding_reqs);
  2740. if (pending) {
  2741. hba->dev_cmd.complete = NULL;
  2742. __clear_bit(lrbp->task_tag,
  2743. &hba->outstanding_reqs);
  2744. }
  2745. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  2746. if (!pending) {
  2747. /*
  2748. * The completion handler ran while we tried to
  2749. * clear the command.
  2750. */
  2751. time_left = 1;
  2752. goto retry;
  2753. }
  2754. } else {
  2755. dev_err(hba->dev, "%s: failed to clear tag %d\n",
  2756. __func__, lrbp->task_tag);
  2757. spin_lock_irqsave(&hba->outstanding_lock, flags);
  2758. pending = test_bit(lrbp->task_tag,
  2759. &hba->outstanding_reqs);
  2760. if (pending)
  2761. hba->dev_cmd.complete = NULL;
  2762. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  2763. if (!pending) {
  2764. /*
  2765. * The completion handler ran while we tried to
  2766. * clear the command.
  2767. */
  2768. time_left = 1;
  2769. goto retry;
  2770. }
  2771. }
  2772. }
  2773. return err;
  2774. }
  2775. static void ufshcd_dev_man_lock(struct ufs_hba *hba)
  2776. {
  2777. ufshcd_hold(hba);
  2778. mutex_lock(&hba->dev_cmd.lock);
  2779. down_read(&hba->clk_scaling_lock);
  2780. }
  2781. static void ufshcd_dev_man_unlock(struct ufs_hba *hba)
  2782. {
  2783. up_read(&hba->clk_scaling_lock);
  2784. mutex_unlock(&hba->dev_cmd.lock);
  2785. ufshcd_release(hba);
  2786. }
  2787. static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
  2788. const u32 tag, int timeout)
  2789. {
  2790. DECLARE_COMPLETION_ONSTACK(wait);
  2791. int err;
  2792. hba->dev_cmd.complete = &wait;
  2793. ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
  2794. ufshcd_send_command(hba, tag, hba->dev_cmd_queue);
  2795. err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
  2796. ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP,
  2797. (struct utp_upiu_req *)lrbp->ucd_rsp_ptr);
  2798. return err;
  2799. }
  2800. /**
  2801. * ufshcd_exec_dev_cmd - API for sending device management requests
  2802. * @hba: UFS hba
  2803. * @cmd_type: specifies the type (NOP, Query...)
  2804. * @timeout: timeout in milliseconds
  2805. *
  2806. * Return: 0 upon success; < 0 upon failure.
  2807. *
  2808. * NOTE: Since there is only one available tag for device management commands,
  2809. * it is expected you hold the hba->dev_cmd.lock mutex.
  2810. */
  2811. static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
  2812. enum dev_cmd_type cmd_type, int timeout)
  2813. {
  2814. const u32 tag = hba->reserved_slot;
  2815. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  2816. int err;
  2817. /* Protects use of hba->reserved_slot. */
  2818. lockdep_assert_held(&hba->dev_cmd.lock);
  2819. err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
  2820. if (unlikely(err))
  2821. return err;
  2822. return ufshcd_issue_dev_cmd(hba, lrbp, tag, timeout);
  2823. }
  2824. /**
  2825. * ufshcd_init_query() - init the query response and request parameters
  2826. * @hba: per-adapter instance
  2827. * @request: address of the request pointer to be initialized
  2828. * @response: address of the response pointer to be initialized
  2829. * @opcode: operation to perform
  2830. * @idn: flag idn to access
  2831. * @index: LU number to access
  2832. * @selector: query/flag/descriptor further identification
  2833. */
  2834. static inline void ufshcd_init_query(struct ufs_hba *hba,
  2835. struct ufs_query_req **request, struct ufs_query_res **response,
  2836. enum query_opcode opcode, u8 idn, u8 index, u8 selector)
  2837. {
  2838. *request = &hba->dev_cmd.query.request;
  2839. *response = &hba->dev_cmd.query.response;
  2840. memset(*request, 0, sizeof(struct ufs_query_req));
  2841. memset(*response, 0, sizeof(struct ufs_query_res));
  2842. (*request)->upiu_req.opcode = opcode;
  2843. (*request)->upiu_req.idn = idn;
  2844. (*request)->upiu_req.index = index;
  2845. (*request)->upiu_req.selector = selector;
  2846. }
  2847. static int ufshcd_query_flag_retry(struct ufs_hba *hba,
  2848. enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
  2849. {
  2850. int ret;
  2851. int retries;
  2852. for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
  2853. ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
  2854. if (ret)
  2855. dev_dbg(hba->dev,
  2856. "%s: failed with error %d, retries %d\n",
  2857. __func__, ret, retries);
  2858. else
  2859. break;
  2860. }
  2861. if (ret)
  2862. dev_err(hba->dev,
  2863. "%s: query flag, opcode %d, idn %d, failed with error %d after %d retries\n",
  2864. __func__, opcode, idn, ret, retries);
  2865. return ret;
  2866. }
  2867. /**
  2868. * ufshcd_query_flag() - API function for sending flag query requests
  2869. * @hba: per-adapter instance
  2870. * @opcode: flag query to perform
  2871. * @idn: flag idn to access
  2872. * @index: flag index to access
  2873. * @flag_res: the flag value after the query request completes
  2874. *
  2875. * Return: 0 for success, non-zero in case of failure.
  2876. */
  2877. int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
  2878. enum flag_idn idn, u8 index, bool *flag_res)
  2879. {
  2880. struct ufs_query_req *request = NULL;
  2881. struct ufs_query_res *response = NULL;
  2882. int err, selector = 0;
  2883. int timeout = QUERY_REQ_TIMEOUT;
  2884. BUG_ON(!hba);
  2885. ufshcd_dev_man_lock(hba);
  2886. ufshcd_init_query(hba, &request, &response, opcode, idn, index,
  2887. selector);
  2888. switch (opcode) {
  2889. case UPIU_QUERY_OPCODE_SET_FLAG:
  2890. case UPIU_QUERY_OPCODE_CLEAR_FLAG:
  2891. case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
  2892. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  2893. break;
  2894. case UPIU_QUERY_OPCODE_READ_FLAG:
  2895. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  2896. if (!flag_res) {
  2897. /* No dummy reads */
  2898. dev_err(hba->dev, "%s: Invalid argument for read request\n",
  2899. __func__);
  2900. err = -EINVAL;
  2901. goto out_unlock;
  2902. }
  2903. break;
  2904. default:
  2905. dev_err(hba->dev,
  2906. "%s: Expected query flag opcode but got = %d\n",
  2907. __func__, opcode);
  2908. err = -EINVAL;
  2909. goto out_unlock;
  2910. }
  2911. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
  2912. if (err) {
  2913. dev_err(hba->dev,
  2914. "%s: Sending flag query for idn %d failed, err = %d\n",
  2915. __func__, idn, err);
  2916. goto out_unlock;
  2917. }
  2918. if (flag_res)
  2919. *flag_res = (be32_to_cpu(response->upiu_res.value) &
  2920. MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
  2921. out_unlock:
  2922. ufshcd_dev_man_unlock(hba);
  2923. return err;
  2924. }
  2925. /**
  2926. * ufshcd_query_attr - API function for sending attribute requests
  2927. * @hba: per-adapter instance
  2928. * @opcode: attribute opcode
  2929. * @idn: attribute idn to access
  2930. * @index: index field
  2931. * @selector: selector field
  2932. * @attr_val: the attribute value after the query request completes
  2933. *
  2934. * Return: 0 for success, non-zero in case of failure.
  2935. */
  2936. int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
  2937. enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
  2938. {
  2939. struct ufs_query_req *request = NULL;
  2940. struct ufs_query_res *response = NULL;
  2941. int err;
  2942. BUG_ON(!hba);
  2943. if (!attr_val) {
  2944. dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
  2945. __func__, opcode);
  2946. return -EINVAL;
  2947. }
  2948. ufshcd_dev_man_lock(hba);
  2949. ufshcd_init_query(hba, &request, &response, opcode, idn, index,
  2950. selector);
  2951. switch (opcode) {
  2952. case UPIU_QUERY_OPCODE_WRITE_ATTR:
  2953. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  2954. request->upiu_req.value = cpu_to_be32(*attr_val);
  2955. break;
  2956. case UPIU_QUERY_OPCODE_READ_ATTR:
  2957. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  2958. break;
  2959. default:
  2960. dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
  2961. __func__, opcode);
  2962. err = -EINVAL;
  2963. goto out_unlock;
  2964. }
  2965. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  2966. if (err) {
  2967. dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
  2968. __func__, opcode, idn, index, err);
  2969. goto out_unlock;
  2970. }
  2971. *attr_val = be32_to_cpu(response->upiu_res.value);
  2972. out_unlock:
  2973. ufshcd_dev_man_unlock(hba);
  2974. return err;
  2975. }
  2976. /**
  2977. * ufshcd_query_attr_retry() - API function for sending query
  2978. * attribute with retries
  2979. * @hba: per-adapter instance
  2980. * @opcode: attribute opcode
  2981. * @idn: attribute idn to access
  2982. * @index: index field
  2983. * @selector: selector field
  2984. * @attr_val: the attribute value after the query request
  2985. * completes
  2986. *
  2987. * Return: 0 for success, non-zero in case of failure.
  2988. */
  2989. int ufshcd_query_attr_retry(struct ufs_hba *hba,
  2990. enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
  2991. u32 *attr_val)
  2992. {
  2993. int ret = 0;
  2994. u32 retries;
  2995. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  2996. ret = ufshcd_query_attr(hba, opcode, idn, index,
  2997. selector, attr_val);
  2998. if (ret)
  2999. dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
  3000. __func__, ret, retries);
  3001. else
  3002. break;
  3003. }
  3004. if (ret)
  3005. dev_err(hba->dev,
  3006. "%s: query attribute, idn %d, failed with error %d after %d retries\n",
  3007. __func__, idn, ret, QUERY_REQ_RETRIES);
  3008. return ret;
  3009. }
  3010. static int __ufshcd_query_descriptor(struct ufs_hba *hba,
  3011. enum query_opcode opcode, enum desc_idn idn, u8 index,
  3012. u8 selector, u8 *desc_buf, int *buf_len)
  3013. {
  3014. struct ufs_query_req *request = NULL;
  3015. struct ufs_query_res *response = NULL;
  3016. int err;
  3017. BUG_ON(!hba);
  3018. if (!desc_buf) {
  3019. dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
  3020. __func__, opcode);
  3021. return -EINVAL;
  3022. }
  3023. if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
  3024. dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
  3025. __func__, *buf_len);
  3026. return -EINVAL;
  3027. }
  3028. ufshcd_dev_man_lock(hba);
  3029. ufshcd_init_query(hba, &request, &response, opcode, idn, index,
  3030. selector);
  3031. hba->dev_cmd.query.descriptor = desc_buf;
  3032. request->upiu_req.length = cpu_to_be16(*buf_len);
  3033. switch (opcode) {
  3034. case UPIU_QUERY_OPCODE_WRITE_DESC:
  3035. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  3036. break;
  3037. case UPIU_QUERY_OPCODE_READ_DESC:
  3038. request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
  3039. break;
  3040. default:
  3041. dev_err(hba->dev,
  3042. "%s: Expected query descriptor opcode but got = 0x%.2x\n",
  3043. __func__, opcode);
  3044. err = -EINVAL;
  3045. goto out_unlock;
  3046. }
  3047. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  3048. if (err) {
  3049. dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
  3050. __func__, opcode, idn, index, err);
  3051. goto out_unlock;
  3052. }
  3053. *buf_len = be16_to_cpu(response->upiu_res.length);
  3054. out_unlock:
  3055. hba->dev_cmd.query.descriptor = NULL;
  3056. ufshcd_dev_man_unlock(hba);
  3057. return err;
  3058. }
  3059. /**
  3060. * ufshcd_query_descriptor_retry - API function for sending descriptor requests
  3061. * @hba: per-adapter instance
  3062. * @opcode: attribute opcode
  3063. * @idn: attribute idn to access
  3064. * @index: index field
  3065. * @selector: selector field
  3066. * @desc_buf: the buffer that contains the descriptor
  3067. * @buf_len: length parameter passed to the device
  3068. *
  3069. * The buf_len parameter will contain, on return, the length parameter
  3070. * received on the response.
  3071. *
  3072. * Return: 0 for success, non-zero in case of failure.
  3073. */
  3074. int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
  3075. enum query_opcode opcode,
  3076. enum desc_idn idn, u8 index,
  3077. u8 selector,
  3078. u8 *desc_buf, int *buf_len)
  3079. {
  3080. int err;
  3081. int retries;
  3082. for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
  3083. err = __ufshcd_query_descriptor(hba, opcode, idn, index,
  3084. selector, desc_buf, buf_len);
  3085. if (!err || err == -EINVAL)
  3086. break;
  3087. }
  3088. return err;
  3089. }
  3090. /**
  3091. * ufshcd_read_desc_param - read the specified descriptor parameter
  3092. * @hba: Pointer to adapter instance
  3093. * @desc_id: descriptor idn value
  3094. * @desc_index: descriptor index
  3095. * @param_offset: offset of the parameter to read
  3096. * @param_read_buf: pointer to buffer where parameter would be read
  3097. * @param_size: sizeof(param_read_buf)
  3098. *
  3099. * Return: 0 in case of success, non-zero otherwise.
  3100. */
  3101. int ufshcd_read_desc_param(struct ufs_hba *hba,
  3102. enum desc_idn desc_id,
  3103. int desc_index,
  3104. u8 param_offset,
  3105. u8 *param_read_buf,
  3106. u8 param_size)
  3107. {
  3108. int ret;
  3109. u8 *desc_buf;
  3110. int buff_len = QUERY_DESC_MAX_SIZE;
  3111. bool is_kmalloc = true;
  3112. /* Safety check */
  3113. if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
  3114. return -EINVAL;
  3115. /* Check whether we need temp memory */
  3116. if (param_offset != 0 || param_size < buff_len) {
  3117. desc_buf = kzalloc(buff_len, GFP_KERNEL);
  3118. if (!desc_buf)
  3119. return -ENOMEM;
  3120. } else {
  3121. desc_buf = param_read_buf;
  3122. is_kmalloc = false;
  3123. }
  3124. /* Request for full descriptor */
  3125. ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
  3126. desc_id, desc_index, 0,
  3127. desc_buf, &buff_len);
  3128. if (ret) {
  3129. dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
  3130. __func__, desc_id, desc_index, param_offset, ret);
  3131. goto out;
  3132. }
  3133. /* Update descriptor length */
  3134. buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
  3135. if (param_offset >= buff_len) {
  3136. dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n",
  3137. __func__, param_offset, desc_id, buff_len);
  3138. ret = -EINVAL;
  3139. goto out;
  3140. }
  3141. /* Sanity check */
  3142. if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
  3143. dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
  3144. __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
  3145. ret = -EINVAL;
  3146. goto out;
  3147. }
  3148. if (is_kmalloc) {
  3149. /* Make sure we don't copy more data than available */
  3150. if (param_offset >= buff_len)
  3151. ret = -EINVAL;
  3152. else
  3153. memcpy(param_read_buf, &desc_buf[param_offset],
  3154. min_t(u32, param_size, buff_len - param_offset));
  3155. }
  3156. out:
  3157. if (is_kmalloc)
  3158. kfree(desc_buf);
  3159. return ret;
  3160. }
  3161. /**
  3162. * struct uc_string_id - unicode string
  3163. *
  3164. * @len: size of this descriptor inclusive
  3165. * @type: descriptor type
  3166. * @uc: unicode string character
  3167. */
  3168. struct uc_string_id {
  3169. u8 len;
  3170. u8 type;
  3171. wchar_t uc[];
  3172. } __packed;
  3173. /* replace non-printable or non-ASCII characters with spaces */
  3174. static inline char ufshcd_remove_non_printable(u8 ch)
  3175. {
  3176. return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
  3177. }
  3178. /**
  3179. * ufshcd_read_string_desc - read string descriptor
  3180. * @hba: pointer to adapter instance
  3181. * @desc_index: descriptor index
  3182. * @buf: pointer to buffer where descriptor would be read,
  3183. * the caller should free the memory.
  3184. * @ascii: if true convert from unicode to ascii characters
  3185. * null terminated string.
  3186. *
  3187. * Return:
  3188. * * string size on success.
  3189. * * -ENOMEM: on allocation failure
  3190. * * -EINVAL: on a wrong parameter
  3191. */
  3192. int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
  3193. u8 **buf, bool ascii)
  3194. {
  3195. struct uc_string_id *uc_str;
  3196. u8 *str;
  3197. int ret;
  3198. if (!buf)
  3199. return -EINVAL;
  3200. uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
  3201. if (!uc_str)
  3202. return -ENOMEM;
  3203. ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
  3204. (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
  3205. if (ret < 0) {
  3206. dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
  3207. QUERY_REQ_RETRIES, ret);
  3208. str = NULL;
  3209. goto out;
  3210. }
  3211. if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
  3212. dev_dbg(hba->dev, "String Desc is of zero length\n");
  3213. str = NULL;
  3214. ret = 0;
  3215. goto out;
  3216. }
  3217. if (ascii) {
  3218. ssize_t ascii_len;
  3219. int i;
  3220. /* remove header and divide by 2 to move from UTF16 to UTF8 */
  3221. ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
  3222. str = kzalloc(ascii_len, GFP_KERNEL);
  3223. if (!str) {
  3224. ret = -ENOMEM;
  3225. goto out;
  3226. }
  3227. /*
  3228. * the descriptor contains string in UTF16 format
  3229. * we need to convert to utf-8 so it can be displayed
  3230. */
  3231. ret = utf16s_to_utf8s(uc_str->uc,
  3232. uc_str->len - QUERY_DESC_HDR_SIZE,
  3233. UTF16_BIG_ENDIAN, str, ascii_len - 1);
  3234. /* replace non-printable or non-ASCII characters with spaces */
  3235. for (i = 0; i < ret; i++)
  3236. str[i] = ufshcd_remove_non_printable(str[i]);
  3237. str[ret++] = '\0';
  3238. } else {
  3239. str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
  3240. if (!str) {
  3241. ret = -ENOMEM;
  3242. goto out;
  3243. }
  3244. ret = uc_str->len;
  3245. }
  3246. out:
  3247. *buf = str;
  3248. kfree(uc_str);
  3249. return ret;
  3250. }
  3251. /**
  3252. * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
  3253. * @hba: Pointer to adapter instance
  3254. * @lun: lun id
  3255. * @param_offset: offset of the parameter to read
  3256. * @param_read_buf: pointer to buffer where parameter would be read
  3257. * @param_size: sizeof(param_read_buf)
  3258. *
  3259. * Return: 0 in case of success, non-zero otherwise.
  3260. */
  3261. static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
  3262. int lun,
  3263. enum unit_desc_param param_offset,
  3264. u8 *param_read_buf,
  3265. u32 param_size)
  3266. {
  3267. /*
  3268. * Unit descriptors are only available for general purpose LUs (LUN id
  3269. * from 0 to 7) and RPMB Well known LU.
  3270. */
  3271. if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))
  3272. return -EOPNOTSUPP;
  3273. return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
  3274. param_offset, param_read_buf, param_size);
  3275. }
  3276. static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
  3277. {
  3278. int err = 0;
  3279. u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
  3280. if (hba->dev_info.wspecversion >= 0x300) {
  3281. err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  3282. QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
  3283. &gating_wait);
  3284. if (err)
  3285. dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
  3286. err, gating_wait);
  3287. if (gating_wait == 0) {
  3288. gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
  3289. dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
  3290. gating_wait);
  3291. }
  3292. hba->dev_info.clk_gating_wait_us = gating_wait;
  3293. }
  3294. return err;
  3295. }
  3296. /**
  3297. * ufshcd_memory_alloc - allocate memory for host memory space data structures
  3298. * @hba: per adapter instance
  3299. *
  3300. * 1. Allocate DMA memory for Command Descriptor array
  3301. * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
  3302. * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
  3303. * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
  3304. * (UTMRDL)
  3305. * 4. Allocate memory for local reference block(lrb).
  3306. *
  3307. * Return: 0 for success, non-zero in case of failure.
  3308. */
  3309. static int ufshcd_memory_alloc(struct ufs_hba *hba)
  3310. {
  3311. size_t utmrdl_size, utrdl_size, ucdl_size;
  3312. /* Allocate memory for UTP command descriptors */
  3313. ucdl_size = ufshcd_get_ucd_size(hba) * hba->nutrs;
  3314. hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
  3315. ucdl_size,
  3316. &hba->ucdl_dma_addr,
  3317. GFP_KERNEL);
  3318. /*
  3319. * UFSHCI requires UTP command descriptor to be 128 byte aligned.
  3320. */
  3321. if (!hba->ucdl_base_addr ||
  3322. WARN_ON(hba->ucdl_dma_addr & (128 - 1))) {
  3323. dev_err(hba->dev,
  3324. "Command Descriptor Memory allocation failed\n");
  3325. goto out;
  3326. }
  3327. /*
  3328. * Allocate memory for UTP Transfer descriptors
  3329. * UFSHCI requires 1KB alignment of UTRD
  3330. */
  3331. utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
  3332. hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
  3333. utrdl_size,
  3334. &hba->utrdl_dma_addr,
  3335. GFP_KERNEL);
  3336. if (!hba->utrdl_base_addr ||
  3337. WARN_ON(hba->utrdl_dma_addr & (SZ_1K - 1))) {
  3338. dev_err(hba->dev,
  3339. "Transfer Descriptor Memory allocation failed\n");
  3340. goto out;
  3341. }
  3342. /*
  3343. * Skip utmrdl allocation; it may have been
  3344. * allocated during first pass and not released during
  3345. * MCQ memory allocation.
  3346. * See ufshcd_release_sdb_queue() and ufshcd_config_mcq()
  3347. */
  3348. if (hba->utmrdl_base_addr)
  3349. goto skip_utmrdl;
  3350. /*
  3351. * Allocate memory for UTP Task Management descriptors
  3352. * UFSHCI requires 1KB alignment of UTMRD
  3353. */
  3354. utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
  3355. hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
  3356. utmrdl_size,
  3357. &hba->utmrdl_dma_addr,
  3358. GFP_KERNEL);
  3359. if (!hba->utmrdl_base_addr ||
  3360. WARN_ON(hba->utmrdl_dma_addr & (SZ_1K - 1))) {
  3361. dev_err(hba->dev,
  3362. "Task Management Descriptor Memory allocation failed\n");
  3363. goto out;
  3364. }
  3365. skip_utmrdl:
  3366. /* Allocate memory for local reference block */
  3367. hba->lrb = devm_kcalloc(hba->dev,
  3368. hba->nutrs, sizeof(struct ufshcd_lrb),
  3369. GFP_KERNEL);
  3370. if (!hba->lrb) {
  3371. dev_err(hba->dev, "LRB Memory allocation failed\n");
  3372. goto out;
  3373. }
  3374. return 0;
  3375. out:
  3376. return -ENOMEM;
  3377. }
  3378. /**
  3379. * ufshcd_host_memory_configure - configure local reference block with
  3380. * memory offsets
  3381. * @hba: per adapter instance
  3382. *
  3383. * Configure Host memory space
  3384. * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
  3385. * address.
  3386. * 2. Update each UTRD with Response UPIU offset, Response UPIU length
  3387. * and PRDT offset.
  3388. * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
  3389. * into local reference block.
  3390. */
  3391. static void ufshcd_host_memory_configure(struct ufs_hba *hba)
  3392. {
  3393. struct utp_transfer_req_desc *utrdlp;
  3394. dma_addr_t cmd_desc_dma_addr;
  3395. dma_addr_t cmd_desc_element_addr;
  3396. u16 response_offset;
  3397. u16 prdt_offset;
  3398. int cmd_desc_size;
  3399. int i;
  3400. utrdlp = hba->utrdl_base_addr;
  3401. response_offset =
  3402. offsetof(struct utp_transfer_cmd_desc, response_upiu);
  3403. prdt_offset =
  3404. offsetof(struct utp_transfer_cmd_desc, prd_table);
  3405. cmd_desc_size = ufshcd_get_ucd_size(hba);
  3406. cmd_desc_dma_addr = hba->ucdl_dma_addr;
  3407. for (i = 0; i < hba->nutrs; i++) {
  3408. /* Configure UTRD with command descriptor base address */
  3409. cmd_desc_element_addr =
  3410. (cmd_desc_dma_addr + (cmd_desc_size * i));
  3411. utrdlp[i].command_desc_base_addr =
  3412. cpu_to_le64(cmd_desc_element_addr);
  3413. /* Response upiu and prdt offset should be in double words */
  3414. if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
  3415. utrdlp[i].response_upiu_offset =
  3416. cpu_to_le16(response_offset);
  3417. utrdlp[i].prd_table_offset =
  3418. cpu_to_le16(prdt_offset);
  3419. utrdlp[i].response_upiu_length =
  3420. cpu_to_le16(ALIGNED_UPIU_SIZE);
  3421. } else {
  3422. utrdlp[i].response_upiu_offset =
  3423. cpu_to_le16(response_offset >> 2);
  3424. utrdlp[i].prd_table_offset =
  3425. cpu_to_le16(prdt_offset >> 2);
  3426. utrdlp[i].response_upiu_length =
  3427. cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
  3428. }
  3429. ufshcd_init_lrb(hba, &hba->lrb[i], i);
  3430. }
  3431. }
  3432. /**
  3433. * ufshcd_dme_link_startup - Notify Unipro to perform link startup
  3434. * @hba: per adapter instance
  3435. *
  3436. * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
  3437. * in order to initialize the Unipro link startup procedure.
  3438. * Once the Unipro links are up, the device connected to the controller
  3439. * is detected.
  3440. *
  3441. * Return: 0 on success, non-zero value on failure.
  3442. */
  3443. static int ufshcd_dme_link_startup(struct ufs_hba *hba)
  3444. {
  3445. struct uic_command uic_cmd = {
  3446. .command = UIC_CMD_DME_LINK_STARTUP,
  3447. };
  3448. int ret;
  3449. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  3450. if (ret)
  3451. dev_dbg(hba->dev,
  3452. "dme-link-startup: error code %d\n", ret);
  3453. return ret;
  3454. }
  3455. /**
  3456. * ufshcd_dme_reset - UIC command for DME_RESET
  3457. * @hba: per adapter instance
  3458. *
  3459. * DME_RESET command is issued in order to reset UniPro stack.
  3460. * This function now deals with cold reset.
  3461. *
  3462. * Return: 0 on success, non-zero value on failure.
  3463. */
  3464. static int ufshcd_dme_reset(struct ufs_hba *hba)
  3465. {
  3466. struct uic_command uic_cmd = {
  3467. .command = UIC_CMD_DME_RESET,
  3468. };
  3469. int ret;
  3470. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  3471. if (ret)
  3472. dev_err(hba->dev,
  3473. "dme-reset: error code %d\n", ret);
  3474. return ret;
  3475. }
  3476. int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
  3477. int agreed_gear,
  3478. int adapt_val)
  3479. {
  3480. int ret;
  3481. if (agreed_gear < UFS_HS_G4)
  3482. adapt_val = PA_NO_ADAPT;
  3483. ret = ufshcd_dme_set(hba,
  3484. UIC_ARG_MIB(PA_TXHSADAPTTYPE),
  3485. adapt_val);
  3486. return ret;
  3487. }
  3488. EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
  3489. /**
  3490. * ufshcd_dme_enable - UIC command for DME_ENABLE
  3491. * @hba: per adapter instance
  3492. *
  3493. * DME_ENABLE command is issued in order to enable UniPro stack.
  3494. *
  3495. * Return: 0 on success, non-zero value on failure.
  3496. */
  3497. static int ufshcd_dme_enable(struct ufs_hba *hba)
  3498. {
  3499. struct uic_command uic_cmd = {
  3500. .command = UIC_CMD_DME_ENABLE,
  3501. };
  3502. int ret;
  3503. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  3504. if (ret)
  3505. dev_err(hba->dev,
  3506. "dme-enable: error code %d\n", ret);
  3507. return ret;
  3508. }
  3509. static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
  3510. {
  3511. #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
  3512. unsigned long min_sleep_time_us;
  3513. if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
  3514. return;
  3515. /*
  3516. * last_dme_cmd_tstamp will be 0 only for 1st call to
  3517. * this function
  3518. */
  3519. if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
  3520. min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
  3521. } else {
  3522. unsigned long delta =
  3523. (unsigned long) ktime_to_us(
  3524. ktime_sub(ktime_get(),
  3525. hba->last_dme_cmd_tstamp));
  3526. if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
  3527. min_sleep_time_us =
  3528. MIN_DELAY_BEFORE_DME_CMDS_US - delta;
  3529. else
  3530. min_sleep_time_us = 0; /* no more delay required */
  3531. }
  3532. if (min_sleep_time_us > 0) {
  3533. /* allow sleep for extra 50us if needed */
  3534. usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
  3535. }
  3536. /* update the last_dme_cmd_tstamp */
  3537. hba->last_dme_cmd_tstamp = ktime_get();
  3538. }
  3539. /**
  3540. * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
  3541. * @hba: per adapter instance
  3542. * @attr_sel: uic command argument1
  3543. * @attr_set: attribute set type as uic command argument2
  3544. * @mib_val: setting value as uic command argument3
  3545. * @peer: indicate whether peer or local
  3546. *
  3547. * Return: 0 on success, non-zero value on failure.
  3548. */
  3549. int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
  3550. u8 attr_set, u32 mib_val, u8 peer)
  3551. {
  3552. struct uic_command uic_cmd = {
  3553. .command = peer ? UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET,
  3554. .argument1 = attr_sel,
  3555. .argument2 = UIC_ARG_ATTR_TYPE(attr_set),
  3556. .argument3 = mib_val,
  3557. };
  3558. static const char *const action[] = {
  3559. "dme-set",
  3560. "dme-peer-set"
  3561. };
  3562. const char *set = action[!!peer];
  3563. int ret;
  3564. int retries = UFS_UIC_COMMAND_RETRIES;
  3565. do {
  3566. /* for peer attributes we retry upon failure */
  3567. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  3568. if (ret)
  3569. dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
  3570. set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
  3571. } while (ret && peer && --retries);
  3572. if (ret)
  3573. dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
  3574. set, UIC_GET_ATTR_ID(attr_sel), mib_val,
  3575. UFS_UIC_COMMAND_RETRIES - retries);
  3576. return ret;
  3577. }
  3578. EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
  3579. /**
  3580. * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
  3581. * @hba: per adapter instance
  3582. * @attr_sel: uic command argument1
  3583. * @mib_val: the value of the attribute as returned by the UIC command
  3584. * @peer: indicate whether peer or local
  3585. *
  3586. * Return: 0 on success, non-zero value on failure.
  3587. */
  3588. int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
  3589. u32 *mib_val, u8 peer)
  3590. {
  3591. struct uic_command uic_cmd = {
  3592. .command = peer ? UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET,
  3593. .argument1 = attr_sel,
  3594. };
  3595. static const char *const action[] = {
  3596. "dme-get",
  3597. "dme-peer-get"
  3598. };
  3599. const char *get = action[!!peer];
  3600. int ret;
  3601. int retries = UFS_UIC_COMMAND_RETRIES;
  3602. struct ufs_pa_layer_attr orig_pwr_info;
  3603. struct ufs_pa_layer_attr temp_pwr_info;
  3604. bool pwr_mode_change = false;
  3605. if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
  3606. orig_pwr_info = hba->pwr_info;
  3607. temp_pwr_info = orig_pwr_info;
  3608. if (orig_pwr_info.pwr_tx == FAST_MODE ||
  3609. orig_pwr_info.pwr_rx == FAST_MODE) {
  3610. temp_pwr_info.pwr_tx = FASTAUTO_MODE;
  3611. temp_pwr_info.pwr_rx = FASTAUTO_MODE;
  3612. pwr_mode_change = true;
  3613. } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
  3614. orig_pwr_info.pwr_rx == SLOW_MODE) {
  3615. temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
  3616. temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
  3617. pwr_mode_change = true;
  3618. }
  3619. if (pwr_mode_change) {
  3620. ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
  3621. if (ret)
  3622. goto out;
  3623. }
  3624. }
  3625. do {
  3626. /* for peer attributes we retry upon failure */
  3627. ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
  3628. if (ret)
  3629. dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
  3630. get, UIC_GET_ATTR_ID(attr_sel), ret);
  3631. } while (ret && peer && --retries);
  3632. if (ret)
  3633. dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
  3634. get, UIC_GET_ATTR_ID(attr_sel),
  3635. UFS_UIC_COMMAND_RETRIES - retries);
  3636. if (mib_val && !ret)
  3637. *mib_val = uic_cmd.argument3;
  3638. if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
  3639. && pwr_mode_change)
  3640. ufshcd_change_power_mode(hba, &orig_pwr_info);
  3641. out:
  3642. return ret;
  3643. }
  3644. EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
  3645. /**
  3646. * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
  3647. * state) and waits for it to take effect.
  3648. *
  3649. * @hba: per adapter instance
  3650. * @cmd: UIC command to execute
  3651. *
  3652. * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
  3653. * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
  3654. * and device UniPro link and hence it's final completion would be indicated by
  3655. * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
  3656. * addition to normal UIC command completion Status (UCCS). This function only
  3657. * returns after the relevant status bits indicate the completion.
  3658. *
  3659. * Return: 0 on success, non-zero value on failure.
  3660. */
  3661. static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
  3662. {
  3663. DECLARE_COMPLETION_ONSTACK(uic_async_done);
  3664. unsigned long flags;
  3665. u8 status;
  3666. int ret;
  3667. bool reenable_intr = false;
  3668. mutex_lock(&hba->uic_cmd_mutex);
  3669. ufshcd_add_delay_before_dme_cmd(hba);
  3670. spin_lock_irqsave(hba->host->host_lock, flags);
  3671. if (ufshcd_is_link_broken(hba)) {
  3672. ret = -ENOLINK;
  3673. goto out_unlock;
  3674. }
  3675. hba->uic_async_done = &uic_async_done;
  3676. if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
  3677. ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
  3678. /*
  3679. * Make sure UIC command completion interrupt is disabled before
  3680. * issuing UIC command.
  3681. */
  3682. ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  3683. reenable_intr = true;
  3684. }
  3685. spin_unlock_irqrestore(hba->host->host_lock, flags);
  3686. ret = __ufshcd_send_uic_cmd(hba, cmd);
  3687. if (ret) {
  3688. dev_err(hba->dev,
  3689. "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
  3690. cmd->command, cmd->argument3, ret);
  3691. goto out;
  3692. }
  3693. if (!wait_for_completion_timeout(hba->uic_async_done,
  3694. msecs_to_jiffies(uic_cmd_timeout))) {
  3695. dev_err(hba->dev,
  3696. "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
  3697. cmd->command, cmd->argument3);
  3698. if (!cmd->cmd_active) {
  3699. dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n",
  3700. __func__);
  3701. goto check_upmcrs;
  3702. }
  3703. ret = -ETIMEDOUT;
  3704. goto out;
  3705. }
  3706. check_upmcrs:
  3707. status = ufshcd_get_upmcrs(hba);
  3708. if (status != PWR_LOCAL) {
  3709. dev_err(hba->dev,
  3710. "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
  3711. cmd->command, status);
  3712. ret = (status != PWR_OK) ? status : -1;
  3713. }
  3714. out:
  3715. if (ret) {
  3716. ufshcd_print_host_state(hba);
  3717. ufshcd_print_pwr_info(hba);
  3718. ufshcd_print_evt_hist(hba);
  3719. }
  3720. spin_lock_irqsave(hba->host->host_lock, flags);
  3721. hba->active_uic_cmd = NULL;
  3722. hba->uic_async_done = NULL;
  3723. if (reenable_intr)
  3724. ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
  3725. if (ret && !hba->pm_op_in_progress) {
  3726. ufshcd_set_link_broken(hba);
  3727. ufshcd_schedule_eh_work(hba);
  3728. }
  3729. out_unlock:
  3730. spin_unlock_irqrestore(hba->host->host_lock, flags);
  3731. mutex_unlock(&hba->uic_cmd_mutex);
  3732. /*
  3733. * If the h8 exit fails during the runtime resume process, it becomes
  3734. * stuck and cannot be recovered through the error handler. To fix
  3735. * this, use link recovery instead of the error handler.
  3736. */
  3737. if (ret && hba->pm_op_in_progress)
  3738. ret = ufshcd_link_recovery(hba);
  3739. return ret;
  3740. }
  3741. /**
  3742. * ufshcd_send_bsg_uic_cmd - Send UIC commands requested via BSG layer and retrieve the result
  3743. * @hba: per adapter instance
  3744. * @uic_cmd: UIC command
  3745. *
  3746. * Return: 0 only if success.
  3747. */
  3748. int ufshcd_send_bsg_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  3749. {
  3750. int ret;
  3751. if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD)
  3752. return 0;
  3753. ufshcd_hold(hba);
  3754. if (uic_cmd->argument1 == UIC_ARG_MIB(PA_PWRMODE) &&
  3755. uic_cmd->command == UIC_CMD_DME_SET) {
  3756. ret = ufshcd_uic_pwr_ctrl(hba, uic_cmd);
  3757. goto out;
  3758. }
  3759. mutex_lock(&hba->uic_cmd_mutex);
  3760. ufshcd_add_delay_before_dme_cmd(hba);
  3761. ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
  3762. if (!ret)
  3763. ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
  3764. mutex_unlock(&hba->uic_cmd_mutex);
  3765. out:
  3766. ufshcd_release(hba);
  3767. return ret;
  3768. }
  3769. /**
  3770. * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
  3771. * using DME_SET primitives.
  3772. * @hba: per adapter instance
  3773. * @mode: powr mode value
  3774. *
  3775. * Return: 0 on success, non-zero value on failure.
  3776. */
  3777. int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
  3778. {
  3779. struct uic_command uic_cmd = {
  3780. .command = UIC_CMD_DME_SET,
  3781. .argument1 = UIC_ARG_MIB(PA_PWRMODE),
  3782. .argument3 = mode,
  3783. };
  3784. int ret;
  3785. if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
  3786. ret = ufshcd_dme_set(hba,
  3787. UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
  3788. if (ret) {
  3789. dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
  3790. __func__, ret);
  3791. goto out;
  3792. }
  3793. }
  3794. ufshcd_hold(hba);
  3795. ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
  3796. ufshcd_release(hba);
  3797. out:
  3798. return ret;
  3799. }
  3800. EXPORT_SYMBOL_GPL(ufshcd_uic_change_pwr_mode);
  3801. int ufshcd_link_recovery(struct ufs_hba *hba)
  3802. {
  3803. int ret;
  3804. unsigned long flags;
  3805. spin_lock_irqsave(hba->host->host_lock, flags);
  3806. hba->ufshcd_state = UFSHCD_STATE_RESET;
  3807. ufshcd_set_eh_in_progress(hba);
  3808. spin_unlock_irqrestore(hba->host->host_lock, flags);
  3809. /* Reset the attached device */
  3810. ufshcd_device_reset(hba);
  3811. ret = ufshcd_host_reset_and_restore(hba);
  3812. spin_lock_irqsave(hba->host->host_lock, flags);
  3813. if (ret)
  3814. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  3815. ufshcd_clear_eh_in_progress(hba);
  3816. spin_unlock_irqrestore(hba->host->host_lock, flags);
  3817. if (ret)
  3818. dev_err(hba->dev, "%s: link recovery failed, err %d",
  3819. __func__, ret);
  3820. return ret;
  3821. }
  3822. EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
  3823. int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
  3824. {
  3825. struct uic_command uic_cmd = {
  3826. .command = UIC_CMD_DME_HIBER_ENTER,
  3827. };
  3828. ktime_t start = ktime_get();
  3829. int ret;
  3830. ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
  3831. ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
  3832. trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
  3833. ktime_to_us(ktime_sub(ktime_get(), start)), ret);
  3834. if (ret)
  3835. dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
  3836. __func__, ret);
  3837. else
  3838. ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
  3839. POST_CHANGE);
  3840. return ret;
  3841. }
  3842. EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_enter);
  3843. int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
  3844. {
  3845. struct uic_command uic_cmd = {
  3846. .command = UIC_CMD_DME_HIBER_EXIT,
  3847. };
  3848. int ret;
  3849. ktime_t start = ktime_get();
  3850. ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
  3851. ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
  3852. trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
  3853. ktime_to_us(ktime_sub(ktime_get(), start)), ret);
  3854. if (ret) {
  3855. dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
  3856. __func__, ret);
  3857. } else {
  3858. ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
  3859. POST_CHANGE);
  3860. hba->ufs_stats.last_hibern8_exit_tstamp = local_clock();
  3861. hba->ufs_stats.hibern8_exit_cnt++;
  3862. }
  3863. return ret;
  3864. }
  3865. EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
  3866. static void ufshcd_configure_auto_hibern8(struct ufs_hba *hba)
  3867. {
  3868. if (!ufshcd_is_auto_hibern8_supported(hba))
  3869. return;
  3870. ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
  3871. }
  3872. void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
  3873. {
  3874. const u32 cur_ahit = READ_ONCE(hba->ahit);
  3875. if (!ufshcd_is_auto_hibern8_supported(hba) || cur_ahit == ahit)
  3876. return;
  3877. WRITE_ONCE(hba->ahit, ahit);
  3878. if (!pm_runtime_suspended(&hba->ufs_device_wlun->sdev_gendev)) {
  3879. ufshcd_rpm_get_sync(hba);
  3880. ufshcd_hold(hba);
  3881. ufshcd_configure_auto_hibern8(hba);
  3882. ufshcd_release(hba);
  3883. ufshcd_rpm_put_sync(hba);
  3884. }
  3885. }
  3886. EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
  3887. /**
  3888. * ufshcd_init_pwr_info - setting the POR (power on reset)
  3889. * values in hba power info
  3890. * @hba: per-adapter instance
  3891. */
  3892. static void ufshcd_init_pwr_info(struct ufs_hba *hba)
  3893. {
  3894. hba->pwr_info.gear_rx = UFS_PWM_G1;
  3895. hba->pwr_info.gear_tx = UFS_PWM_G1;
  3896. hba->pwr_info.lane_rx = UFS_LANE_1;
  3897. hba->pwr_info.lane_tx = UFS_LANE_1;
  3898. hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
  3899. hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
  3900. hba->pwr_info.hs_rate = 0;
  3901. }
  3902. /**
  3903. * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
  3904. * @hba: per-adapter instance
  3905. *
  3906. * Return: 0 upon success; < 0 upon failure.
  3907. */
  3908. static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
  3909. {
  3910. struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
  3911. if (hba->max_pwr_info.is_valid)
  3912. return 0;
  3913. if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) {
  3914. pwr_info->pwr_tx = FASTAUTO_MODE;
  3915. pwr_info->pwr_rx = FASTAUTO_MODE;
  3916. } else {
  3917. pwr_info->pwr_tx = FAST_MODE;
  3918. pwr_info->pwr_rx = FAST_MODE;
  3919. }
  3920. pwr_info->hs_rate = PA_HS_MODE_B;
  3921. /* Get the connected lane count */
  3922. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
  3923. &pwr_info->lane_rx);
  3924. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
  3925. &pwr_info->lane_tx);
  3926. if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
  3927. dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
  3928. __func__,
  3929. pwr_info->lane_rx,
  3930. pwr_info->lane_tx);
  3931. return -EINVAL;
  3932. }
  3933. /*
  3934. * First, get the maximum gears of HS speed.
  3935. * If a zero value, it means there is no HSGEAR capability.
  3936. * Then, get the maximum gears of PWM speed.
  3937. */
  3938. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
  3939. if (!pwr_info->gear_rx) {
  3940. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
  3941. &pwr_info->gear_rx);
  3942. if (!pwr_info->gear_rx) {
  3943. dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
  3944. __func__, pwr_info->gear_rx);
  3945. return -EINVAL;
  3946. }
  3947. pwr_info->pwr_rx = SLOW_MODE;
  3948. }
  3949. ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
  3950. &pwr_info->gear_tx);
  3951. if (!pwr_info->gear_tx) {
  3952. ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
  3953. &pwr_info->gear_tx);
  3954. if (!pwr_info->gear_tx) {
  3955. dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
  3956. __func__, pwr_info->gear_tx);
  3957. return -EINVAL;
  3958. }
  3959. pwr_info->pwr_tx = SLOW_MODE;
  3960. }
  3961. hba->max_pwr_info.is_valid = true;
  3962. return 0;
  3963. }
  3964. static int ufshcd_change_power_mode(struct ufs_hba *hba,
  3965. struct ufs_pa_layer_attr *pwr_mode)
  3966. {
  3967. int ret;
  3968. /* if already configured to the requested pwr_mode */
  3969. if (!hba->force_pmc &&
  3970. pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
  3971. pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
  3972. pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
  3973. pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
  3974. pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
  3975. pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
  3976. pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
  3977. dev_dbg(hba->dev, "%s: power already configured\n", __func__);
  3978. return 0;
  3979. }
  3980. /*
  3981. * Configure attributes for power mode change with below.
  3982. * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
  3983. * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
  3984. * - PA_HSSERIES
  3985. */
  3986. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
  3987. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
  3988. pwr_mode->lane_rx);
  3989. if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
  3990. pwr_mode->pwr_rx == FAST_MODE)
  3991. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
  3992. else
  3993. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), false);
  3994. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
  3995. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
  3996. pwr_mode->lane_tx);
  3997. if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
  3998. pwr_mode->pwr_tx == FAST_MODE)
  3999. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
  4000. else
  4001. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), false);
  4002. if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
  4003. pwr_mode->pwr_tx == FASTAUTO_MODE ||
  4004. pwr_mode->pwr_rx == FAST_MODE ||
  4005. pwr_mode->pwr_tx == FAST_MODE)
  4006. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
  4007. pwr_mode->hs_rate);
  4008. if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
  4009. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
  4010. DL_FC0ProtectionTimeOutVal_Default);
  4011. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
  4012. DL_TC0ReplayTimeOutVal_Default);
  4013. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
  4014. DL_AFC0ReqTimeOutVal_Default);
  4015. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
  4016. DL_FC1ProtectionTimeOutVal_Default);
  4017. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
  4018. DL_TC1ReplayTimeOutVal_Default);
  4019. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
  4020. DL_AFC1ReqTimeOutVal_Default);
  4021. ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
  4022. DL_FC0ProtectionTimeOutVal_Default);
  4023. ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
  4024. DL_TC0ReplayTimeOutVal_Default);
  4025. ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
  4026. DL_AFC0ReqTimeOutVal_Default);
  4027. }
  4028. ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
  4029. | pwr_mode->pwr_tx);
  4030. if (ret) {
  4031. dev_err(hba->dev,
  4032. "%s: power mode change failed %d\n", __func__, ret);
  4033. } else {
  4034. memcpy(&hba->pwr_info, pwr_mode,
  4035. sizeof(struct ufs_pa_layer_attr));
  4036. }
  4037. return ret;
  4038. }
  4039. /**
  4040. * ufshcd_config_pwr_mode - configure a new power mode
  4041. * @hba: per-adapter instance
  4042. * @desired_pwr_mode: desired power configuration
  4043. *
  4044. * Return: 0 upon success; < 0 upon failure.
  4045. */
  4046. int ufshcd_config_pwr_mode(struct ufs_hba *hba,
  4047. struct ufs_pa_layer_attr *desired_pwr_mode)
  4048. {
  4049. struct ufs_pa_layer_attr final_params = { 0 };
  4050. int ret;
  4051. ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
  4052. desired_pwr_mode, &final_params);
  4053. if (ret)
  4054. memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
  4055. ret = ufshcd_change_power_mode(hba, &final_params);
  4056. if (!ret)
  4057. ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
  4058. &final_params);
  4059. return ret;
  4060. }
  4061. EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
  4062. /**
  4063. * ufshcd_complete_dev_init() - checks device readiness
  4064. * @hba: per-adapter instance
  4065. *
  4066. * Set fDeviceInit flag and poll until device toggles it.
  4067. *
  4068. * Return: 0 upon success; < 0 upon failure.
  4069. */
  4070. static int ufshcd_complete_dev_init(struct ufs_hba *hba)
  4071. {
  4072. int err;
  4073. bool flag_res = true;
  4074. ktime_t timeout;
  4075. err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
  4076. QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
  4077. if (err) {
  4078. dev_err(hba->dev,
  4079. "%s: setting fDeviceInit flag failed with error %d\n",
  4080. __func__, err);
  4081. goto out;
  4082. }
  4083. /* Poll fDeviceInit flag to be cleared */
  4084. timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT);
  4085. do {
  4086. err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
  4087. QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
  4088. if (!flag_res)
  4089. break;
  4090. usleep_range(500, 1000);
  4091. } while (ktime_before(ktime_get(), timeout));
  4092. if (err) {
  4093. dev_err(hba->dev,
  4094. "%s: reading fDeviceInit flag failed with error %d\n",
  4095. __func__, err);
  4096. } else if (flag_res) {
  4097. dev_err(hba->dev,
  4098. "%s: fDeviceInit was not cleared by the device\n",
  4099. __func__);
  4100. err = -EBUSY;
  4101. }
  4102. out:
  4103. return err;
  4104. }
  4105. /**
  4106. * ufshcd_make_hba_operational - Make UFS controller operational
  4107. * @hba: per adapter instance
  4108. *
  4109. * To bring UFS host controller to operational state,
  4110. * 1. Enable required interrupts
  4111. * 2. Configure interrupt aggregation
  4112. * 3. Program UTRL and UTMRL base address
  4113. * 4. Configure run-stop-registers
  4114. *
  4115. * Return: 0 on success, non-zero value on failure.
  4116. */
  4117. int ufshcd_make_hba_operational(struct ufs_hba *hba)
  4118. {
  4119. int err = 0;
  4120. u32 reg;
  4121. /* Enable required interrupts */
  4122. ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
  4123. /* Configure interrupt aggregation */
  4124. if (ufshcd_is_intr_aggr_allowed(hba))
  4125. ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
  4126. else
  4127. ufshcd_disable_intr_aggr(hba);
  4128. /* Configure UTRL and UTMRL base address registers */
  4129. ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
  4130. REG_UTP_TRANSFER_REQ_LIST_BASE_L);
  4131. ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
  4132. REG_UTP_TRANSFER_REQ_LIST_BASE_H);
  4133. ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
  4134. REG_UTP_TASK_REQ_LIST_BASE_L);
  4135. ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
  4136. REG_UTP_TASK_REQ_LIST_BASE_H);
  4137. /*
  4138. * UCRDY, UTMRLDY and UTRLRDY bits must be 1
  4139. */
  4140. reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
  4141. if (!(ufshcd_get_lists_status(reg))) {
  4142. ufshcd_enable_run_stop_reg(hba);
  4143. } else {
  4144. dev_err(hba->dev,
  4145. "Host controller not ready to process requests");
  4146. err = -EIO;
  4147. }
  4148. return err;
  4149. }
  4150. EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
  4151. /**
  4152. * ufshcd_hba_stop - Send controller to reset state
  4153. * @hba: per adapter instance
  4154. */
  4155. void ufshcd_hba_stop(struct ufs_hba *hba)
  4156. {
  4157. unsigned long flags;
  4158. int err;
  4159. /*
  4160. * Obtain the host lock to prevent that the controller is disabled
  4161. * while the UFS interrupt handler is active on another CPU.
  4162. */
  4163. spin_lock_irqsave(hba->host->host_lock, flags);
  4164. ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
  4165. spin_unlock_irqrestore(hba->host->host_lock, flags);
  4166. err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
  4167. CONTROLLER_ENABLE, CONTROLLER_DISABLE,
  4168. 10, 1);
  4169. if (err)
  4170. dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
  4171. }
  4172. EXPORT_SYMBOL_GPL(ufshcd_hba_stop);
  4173. /**
  4174. * ufshcd_hba_execute_hce - initialize the controller
  4175. * @hba: per adapter instance
  4176. *
  4177. * The controller resets itself and controller firmware initialization
  4178. * sequence kicks off. When controller is ready it will set
  4179. * the Host Controller Enable bit to 1.
  4180. *
  4181. * Return: 0 on success, non-zero value on failure.
  4182. */
  4183. static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
  4184. {
  4185. int retry_outer = 3;
  4186. int retry_inner;
  4187. start:
  4188. if (ufshcd_is_hba_active(hba))
  4189. /* change controller state to "reset state" */
  4190. ufshcd_hba_stop(hba);
  4191. /* UniPro link is disabled at this point */
  4192. ufshcd_set_link_off(hba);
  4193. ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
  4194. /* start controller initialization sequence */
  4195. ufshcd_hba_start(hba);
  4196. /*
  4197. * To initialize a UFS host controller HCE bit must be set to 1.
  4198. * During initialization the HCE bit value changes from 1->0->1.
  4199. * When the host controller completes initialization sequence
  4200. * it sets the value of HCE bit to 1. The same HCE bit is read back
  4201. * to check if the controller has completed initialization sequence.
  4202. * So without this delay the value HCE = 1, set in the previous
  4203. * instruction might be read back.
  4204. * This delay can be changed based on the controller.
  4205. */
  4206. ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
  4207. /* wait for the host controller to complete initialization */
  4208. retry_inner = 50;
  4209. while (!ufshcd_is_hba_active(hba)) {
  4210. if (retry_inner) {
  4211. retry_inner--;
  4212. } else {
  4213. dev_err(hba->dev,
  4214. "Controller enable failed\n");
  4215. if (retry_outer) {
  4216. retry_outer--;
  4217. goto start;
  4218. }
  4219. return -EIO;
  4220. }
  4221. usleep_range(1000, 1100);
  4222. }
  4223. /* enable UIC related interrupts */
  4224. ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
  4225. ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
  4226. return 0;
  4227. }
  4228. int ufshcd_hba_enable(struct ufs_hba *hba)
  4229. {
  4230. int ret;
  4231. if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
  4232. ufshcd_set_link_off(hba);
  4233. ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
  4234. /* enable UIC related interrupts */
  4235. ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
  4236. ret = ufshcd_dme_reset(hba);
  4237. if (ret) {
  4238. dev_err(hba->dev, "DME_RESET failed\n");
  4239. return ret;
  4240. }
  4241. ret = ufshcd_dme_enable(hba);
  4242. if (ret) {
  4243. dev_err(hba->dev, "Enabling DME failed\n");
  4244. return ret;
  4245. }
  4246. ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
  4247. } else {
  4248. ret = ufshcd_hba_execute_hce(hba);
  4249. }
  4250. return ret;
  4251. }
  4252. EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
  4253. static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
  4254. {
  4255. int tx_lanes = 0, i, err = 0;
  4256. if (!peer)
  4257. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
  4258. &tx_lanes);
  4259. else
  4260. ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
  4261. &tx_lanes);
  4262. for (i = 0; i < tx_lanes; i++) {
  4263. if (!peer)
  4264. err = ufshcd_dme_set(hba,
  4265. UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
  4266. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
  4267. 0);
  4268. else
  4269. err = ufshcd_dme_peer_set(hba,
  4270. UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
  4271. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
  4272. 0);
  4273. if (err) {
  4274. dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
  4275. __func__, peer, i, err);
  4276. break;
  4277. }
  4278. }
  4279. return err;
  4280. }
  4281. static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
  4282. {
  4283. return ufshcd_disable_tx_lcc(hba, true);
  4284. }
  4285. void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val)
  4286. {
  4287. struct ufs_event_hist *e;
  4288. if (id >= UFS_EVT_CNT)
  4289. return;
  4290. e = &hba->ufs_stats.event[id];
  4291. e->val[e->pos] = val;
  4292. e->tstamp[e->pos] = local_clock();
  4293. e->cnt += 1;
  4294. e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH;
  4295. ufshcd_vops_event_notify(hba, id, &val);
  4296. }
  4297. EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist);
  4298. /**
  4299. * ufshcd_link_startup - Initialize unipro link startup
  4300. * @hba: per adapter instance
  4301. *
  4302. * Return: 0 for success, non-zero in case of failure.
  4303. */
  4304. static int ufshcd_link_startup(struct ufs_hba *hba)
  4305. {
  4306. int ret;
  4307. int retries = DME_LINKSTARTUP_RETRIES;
  4308. bool link_startup_again = false;
  4309. /*
  4310. * If UFS device isn't active then we will have to issue link startup
  4311. * 2 times to make sure the device state move to active.
  4312. */
  4313. if (!ufshcd_is_ufs_dev_active(hba))
  4314. link_startup_again = true;
  4315. link_startup:
  4316. do {
  4317. ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
  4318. ret = ufshcd_dme_link_startup(hba);
  4319. /* check if device is detected by inter-connect layer */
  4320. if (!ret && !ufshcd_is_device_present(hba)) {
  4321. ufshcd_update_evt_hist(hba,
  4322. UFS_EVT_LINK_STARTUP_FAIL,
  4323. 0);
  4324. dev_err(hba->dev, "%s: Device not present\n", __func__);
  4325. ret = -ENXIO;
  4326. goto out;
  4327. }
  4328. /*
  4329. * DME link lost indication is only received when link is up,
  4330. * but we can't be sure if the link is up until link startup
  4331. * succeeds. So reset the local Uni-Pro and try again.
  4332. */
  4333. if (ret && retries && ufshcd_hba_enable(hba)) {
  4334. ufshcd_update_evt_hist(hba,
  4335. UFS_EVT_LINK_STARTUP_FAIL,
  4336. (u32)ret);
  4337. goto out;
  4338. }
  4339. } while (ret && retries--);
  4340. if (ret) {
  4341. /* failed to get the link up... retire */
  4342. ufshcd_update_evt_hist(hba,
  4343. UFS_EVT_LINK_STARTUP_FAIL,
  4344. (u32)ret);
  4345. goto out;
  4346. }
  4347. if (link_startup_again) {
  4348. link_startup_again = false;
  4349. retries = DME_LINKSTARTUP_RETRIES;
  4350. goto link_startup;
  4351. }
  4352. /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
  4353. ufshcd_init_pwr_info(hba);
  4354. ufshcd_print_pwr_info(hba);
  4355. if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
  4356. ret = ufshcd_disable_device_tx_lcc(hba);
  4357. if (ret)
  4358. goto out;
  4359. }
  4360. /* Include any host controller configuration via UIC commands */
  4361. ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
  4362. if (ret)
  4363. goto out;
  4364. /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
  4365. ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
  4366. ret = ufshcd_make_hba_operational(hba);
  4367. out:
  4368. if (ret) {
  4369. dev_err(hba->dev, "link startup failed %d\n", ret);
  4370. ufshcd_print_host_state(hba);
  4371. ufshcd_print_pwr_info(hba);
  4372. ufshcd_print_evt_hist(hba);
  4373. }
  4374. return ret;
  4375. }
  4376. /**
  4377. * ufshcd_verify_dev_init() - Verify device initialization
  4378. * @hba: per-adapter instance
  4379. *
  4380. * Send NOP OUT UPIU and wait for NOP IN response to check whether the
  4381. * device Transport Protocol (UTP) layer is ready after a reset.
  4382. * If the UTP layer at the device side is not initialized, it may
  4383. * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
  4384. * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
  4385. *
  4386. * Return: 0 upon success; < 0 upon failure.
  4387. */
  4388. static int ufshcd_verify_dev_init(struct ufs_hba *hba)
  4389. {
  4390. int err = 0;
  4391. int retries;
  4392. ufshcd_dev_man_lock(hba);
  4393. for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
  4394. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
  4395. hba->nop_out_timeout);
  4396. if (!err || err == -ETIMEDOUT)
  4397. break;
  4398. dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
  4399. }
  4400. ufshcd_dev_man_unlock(hba);
  4401. if (err)
  4402. dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
  4403. return err;
  4404. }
  4405. /**
  4406. * ufshcd_setup_links - associate link b/w device wlun and other luns
  4407. * @sdev: pointer to SCSI device
  4408. * @hba: pointer to ufs hba
  4409. */
  4410. static void ufshcd_setup_links(struct ufs_hba *hba, struct scsi_device *sdev)
  4411. {
  4412. struct device_link *link;
  4413. /*
  4414. * Device wlun is the supplier & rest of the luns are consumers.
  4415. * This ensures that device wlun suspends after all other luns.
  4416. */
  4417. if (hba->ufs_device_wlun) {
  4418. link = device_link_add(&sdev->sdev_gendev,
  4419. &hba->ufs_device_wlun->sdev_gendev,
  4420. DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);
  4421. if (!link) {
  4422. dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n",
  4423. dev_name(&hba->ufs_device_wlun->sdev_gendev));
  4424. return;
  4425. }
  4426. hba->luns_avail--;
  4427. /* Ignore REPORT_LUN wlun probing */
  4428. if (hba->luns_avail == 1) {
  4429. ufshcd_rpm_put(hba);
  4430. return;
  4431. }
  4432. } else {
  4433. /*
  4434. * Device wlun is probed. The assumption is that WLUNs are
  4435. * scanned before other LUNs.
  4436. */
  4437. hba->luns_avail--;
  4438. }
  4439. }
  4440. /**
  4441. * ufshcd_lu_init - Initialize the relevant parameters of the LU
  4442. * @hba: per-adapter instance
  4443. * @sdev: pointer to SCSI device
  4444. */
  4445. static void ufshcd_lu_init(struct ufs_hba *hba, struct scsi_device *sdev)
  4446. {
  4447. int len = QUERY_DESC_MAX_SIZE;
  4448. u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
  4449. u8 lun_qdepth = hba->nutrs;
  4450. u8 *desc_buf;
  4451. int ret;
  4452. desc_buf = kzalloc(len, GFP_KERNEL);
  4453. if (!desc_buf)
  4454. goto set_qdepth;
  4455. ret = ufshcd_read_unit_desc_param(hba, lun, 0, desc_buf, len);
  4456. if (ret < 0) {
  4457. if (ret == -EOPNOTSUPP)
  4458. /* If LU doesn't support unit descriptor, its queue depth is set to 1 */
  4459. lun_qdepth = 1;
  4460. kfree(desc_buf);
  4461. goto set_qdepth;
  4462. }
  4463. if (desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH]) {
  4464. /*
  4465. * In per-LU queueing architecture, bLUQueueDepth will not be 0, then we will
  4466. * use the smaller between UFSHCI CAP.NUTRS and UFS LU bLUQueueDepth
  4467. */
  4468. lun_qdepth = min_t(int, desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH], hba->nutrs);
  4469. }
  4470. /*
  4471. * According to UFS device specification, the write protection mode is only supported by
  4472. * normal LU, not supported by WLUN.
  4473. */
  4474. if (hba->dev_info.f_power_on_wp_en && lun < hba->dev_info.max_lu_supported &&
  4475. !hba->dev_info.is_lu_power_on_wp &&
  4476. desc_buf[UNIT_DESC_PARAM_LU_WR_PROTECT] == UFS_LU_POWER_ON_WP)
  4477. hba->dev_info.is_lu_power_on_wp = true;
  4478. /* In case of RPMB LU, check if advanced RPMB mode is enabled */
  4479. if (desc_buf[UNIT_DESC_PARAM_UNIT_INDEX] == UFS_UPIU_RPMB_WLUN &&
  4480. desc_buf[RPMB_UNIT_DESC_PARAM_REGION_EN] & BIT(4))
  4481. hba->dev_info.b_advanced_rpmb_en = true;
  4482. kfree(desc_buf);
  4483. set_qdepth:
  4484. /*
  4485. * For WLUNs that don't support unit descriptor, queue depth is set to 1. For LUs whose
  4486. * bLUQueueDepth == 0, the queue depth is set to a maximum value that host can queue.
  4487. */
  4488. dev_dbg(hba->dev, "Set LU %x queue depth %d\n", lun, lun_qdepth);
  4489. scsi_change_queue_depth(sdev, lun_qdepth);
  4490. }
  4491. /**
  4492. * ufshcd_slave_alloc - handle initial SCSI device configurations
  4493. * @sdev: pointer to SCSI device
  4494. *
  4495. * Return: success.
  4496. */
  4497. static int ufshcd_slave_alloc(struct scsi_device *sdev)
  4498. {
  4499. struct ufs_hba *hba;
  4500. hba = shost_priv(sdev->host);
  4501. /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
  4502. sdev->use_10_for_ms = 1;
  4503. /* DBD field should be set to 1 in mode sense(10) */
  4504. sdev->set_dbd_for_ms = 1;
  4505. /* allow SCSI layer to restart the device in case of errors */
  4506. sdev->allow_restart = 1;
  4507. /* REPORT SUPPORTED OPERATION CODES is not supported */
  4508. sdev->no_report_opcodes = 1;
  4509. /* WRITE_SAME command is not supported */
  4510. sdev->no_write_same = 1;
  4511. ufshcd_lu_init(hba, sdev);
  4512. ufshcd_setup_links(hba, sdev);
  4513. return 0;
  4514. }
  4515. /**
  4516. * ufshcd_change_queue_depth - change queue depth
  4517. * @sdev: pointer to SCSI device
  4518. * @depth: required depth to set
  4519. *
  4520. * Change queue depth and make sure the max. limits are not crossed.
  4521. *
  4522. * Return: new queue depth.
  4523. */
  4524. static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
  4525. {
  4526. return scsi_change_queue_depth(sdev, min(depth, sdev->host->can_queue));
  4527. }
  4528. /**
  4529. * ufshcd_device_configure - adjust SCSI device configurations
  4530. * @sdev: pointer to SCSI device
  4531. * @lim: queue limits
  4532. *
  4533. * Return: 0 (success).
  4534. */
  4535. static int ufshcd_device_configure(struct scsi_device *sdev,
  4536. struct queue_limits *lim)
  4537. {
  4538. struct ufs_hba *hba = shost_priv(sdev->host);
  4539. struct request_queue *q = sdev->request_queue;
  4540. lim->dma_pad_mask = PRDT_DATA_BYTE_COUNT_PAD - 1;
  4541. /*
  4542. * Block runtime-pm until all consumers are added.
  4543. * Refer ufshcd_setup_links().
  4544. */
  4545. if (is_device_wlun(sdev))
  4546. pm_runtime_get_noresume(&sdev->sdev_gendev);
  4547. else if (ufshcd_is_rpm_autosuspend_allowed(hba))
  4548. sdev->rpm_autosuspend = 1;
  4549. /*
  4550. * Do not print messages during runtime PM to avoid never-ending cycles
  4551. * of messages written back to storage by user space causing runtime
  4552. * resume, causing more messages and so on.
  4553. */
  4554. sdev->silence_suspend = 1;
  4555. ufshcd_crypto_register(hba, q);
  4556. return 0;
  4557. }
  4558. /**
  4559. * ufshcd_slave_destroy - remove SCSI device configurations
  4560. * @sdev: pointer to SCSI device
  4561. */
  4562. static void ufshcd_slave_destroy(struct scsi_device *sdev)
  4563. {
  4564. struct ufs_hba *hba;
  4565. unsigned long flags;
  4566. hba = shost_priv(sdev->host);
  4567. /* Drop the reference as it won't be needed anymore */
  4568. if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
  4569. spin_lock_irqsave(hba->host->host_lock, flags);
  4570. hba->ufs_device_wlun = NULL;
  4571. spin_unlock_irqrestore(hba->host->host_lock, flags);
  4572. } else if (hba->ufs_device_wlun) {
  4573. struct device *supplier = NULL;
  4574. /* Ensure UFS Device WLUN exists and does not disappear */
  4575. spin_lock_irqsave(hba->host->host_lock, flags);
  4576. if (hba->ufs_device_wlun) {
  4577. supplier = &hba->ufs_device_wlun->sdev_gendev;
  4578. get_device(supplier);
  4579. }
  4580. spin_unlock_irqrestore(hba->host->host_lock, flags);
  4581. if (supplier) {
  4582. /*
  4583. * If a LUN fails to probe (e.g. absent BOOT WLUN), the
  4584. * device will not have been registered but can still
  4585. * have a device link holding a reference to the device.
  4586. */
  4587. device_link_remove(&sdev->sdev_gendev, supplier);
  4588. put_device(supplier);
  4589. }
  4590. }
  4591. }
  4592. /**
  4593. * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
  4594. * @lrbp: pointer to local reference block of completed command
  4595. * @scsi_status: SCSI command status
  4596. *
  4597. * Return: value base on SCSI command status.
  4598. */
  4599. static inline int
  4600. ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
  4601. {
  4602. int result = 0;
  4603. switch (scsi_status) {
  4604. case SAM_STAT_CHECK_CONDITION:
  4605. ufshcd_copy_sense_data(lrbp);
  4606. fallthrough;
  4607. case SAM_STAT_GOOD:
  4608. result |= DID_OK << 16 | scsi_status;
  4609. break;
  4610. case SAM_STAT_TASK_SET_FULL:
  4611. case SAM_STAT_BUSY:
  4612. case SAM_STAT_TASK_ABORTED:
  4613. ufshcd_copy_sense_data(lrbp);
  4614. result |= scsi_status;
  4615. break;
  4616. default:
  4617. result |= DID_ERROR << 16;
  4618. break;
  4619. } /* end of switch */
  4620. return result;
  4621. }
  4622. /**
  4623. * ufshcd_transfer_rsp_status - Get overall status of the response
  4624. * @hba: per adapter instance
  4625. * @lrbp: pointer to local reference block of completed command
  4626. * @cqe: pointer to the completion queue entry
  4627. *
  4628. * Return: result of the command to notify SCSI midlayer.
  4629. */
  4630. static inline int
  4631. ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
  4632. struct cq_entry *cqe)
  4633. {
  4634. int result = 0;
  4635. int scsi_status;
  4636. enum utp_ocs ocs;
  4637. u8 upiu_flags;
  4638. u32 resid;
  4639. upiu_flags = lrbp->ucd_rsp_ptr->header.flags;
  4640. resid = be32_to_cpu(lrbp->ucd_rsp_ptr->sr.residual_transfer_count);
  4641. /*
  4642. * Test !overflow instead of underflow to support UFS devices that do
  4643. * not set either flag.
  4644. */
  4645. if (resid && !(upiu_flags & UPIU_RSP_FLAG_OVERFLOW))
  4646. scsi_set_resid(lrbp->cmd, resid);
  4647. /* overall command status of utrd */
  4648. ocs = ufshcd_get_tr_ocs(lrbp, cqe);
  4649. if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
  4650. if (lrbp->ucd_rsp_ptr->header.response ||
  4651. lrbp->ucd_rsp_ptr->header.status)
  4652. ocs = OCS_SUCCESS;
  4653. }
  4654. switch (ocs) {
  4655. case OCS_SUCCESS:
  4656. hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
  4657. switch (ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr)) {
  4658. case UPIU_TRANSACTION_RESPONSE:
  4659. /*
  4660. * get the result based on SCSI status response
  4661. * to notify the SCSI midlayer of the command status
  4662. */
  4663. scsi_status = lrbp->ucd_rsp_ptr->header.status;
  4664. result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
  4665. /*
  4666. * Currently we are only supporting BKOPs exception
  4667. * events hence we can ignore BKOPs exception event
  4668. * during power management callbacks. BKOPs exception
  4669. * event is not expected to be raised in runtime suspend
  4670. * callback as it allows the urgent bkops.
  4671. * During system suspend, we are anyway forcefully
  4672. * disabling the bkops and if urgent bkops is needed
  4673. * it will be enabled on system resume. Long term
  4674. * solution could be to abort the system suspend if
  4675. * UFS device needs urgent BKOPs.
  4676. */
  4677. if (!hba->pm_op_in_progress &&
  4678. !ufshcd_eh_in_progress(hba) &&
  4679. ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
  4680. /* Flushed in suspend */
  4681. schedule_work(&hba->eeh_work);
  4682. break;
  4683. case UPIU_TRANSACTION_REJECT_UPIU:
  4684. /* TODO: handle Reject UPIU Response */
  4685. result = DID_ERROR << 16;
  4686. dev_err(hba->dev,
  4687. "Reject UPIU not fully implemented\n");
  4688. break;
  4689. default:
  4690. dev_err(hba->dev,
  4691. "Unexpected request response code = %x\n",
  4692. result);
  4693. result = DID_ERROR << 16;
  4694. break;
  4695. }
  4696. break;
  4697. case OCS_ABORTED:
  4698. case OCS_INVALID_COMMAND_STATUS:
  4699. result |= DID_REQUEUE << 16;
  4700. dev_warn(hba->dev,
  4701. "OCS %s from controller for tag %d\n",
  4702. (ocs == OCS_ABORTED ? "aborted" : "invalid"),
  4703. lrbp->task_tag);
  4704. break;
  4705. case OCS_INVALID_CMD_TABLE_ATTR:
  4706. case OCS_INVALID_PRDT_ATTR:
  4707. case OCS_MISMATCH_DATA_BUF_SIZE:
  4708. case OCS_MISMATCH_RESP_UPIU_SIZE:
  4709. case OCS_PEER_COMM_FAILURE:
  4710. case OCS_FATAL_ERROR:
  4711. case OCS_DEVICE_FATAL_ERROR:
  4712. case OCS_INVALID_CRYPTO_CONFIG:
  4713. case OCS_GENERAL_CRYPTO_ERROR:
  4714. default:
  4715. result |= DID_ERROR << 16;
  4716. dev_err(hba->dev,
  4717. "OCS error from controller = %x for tag %d\n",
  4718. ocs, lrbp->task_tag);
  4719. ufshcd_print_evt_hist(hba);
  4720. ufshcd_print_host_state(hba);
  4721. break;
  4722. } /* end of switch */
  4723. if ((host_byte(result) != DID_OK) &&
  4724. (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs)
  4725. ufshcd_print_tr(hba, lrbp->task_tag, true);
  4726. return result;
  4727. }
  4728. static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
  4729. u32 intr_mask)
  4730. {
  4731. if (!ufshcd_is_auto_hibern8_supported(hba) ||
  4732. !ufshcd_is_auto_hibern8_enabled(hba))
  4733. return false;
  4734. if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
  4735. return false;
  4736. if (hba->active_uic_cmd &&
  4737. (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
  4738. hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
  4739. return false;
  4740. return true;
  4741. }
  4742. /**
  4743. * ufshcd_uic_cmd_compl - handle completion of uic command
  4744. * @hba: per adapter instance
  4745. * @intr_status: interrupt status generated by the controller
  4746. *
  4747. * Return:
  4748. * IRQ_HANDLED - If interrupt is valid
  4749. * IRQ_NONE - If invalid interrupt
  4750. */
  4751. static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
  4752. {
  4753. irqreturn_t retval = IRQ_NONE;
  4754. spin_lock(hba->host->host_lock);
  4755. if (ufshcd_is_auto_hibern8_error(hba, intr_status))
  4756. hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
  4757. if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
  4758. hba->active_uic_cmd->argument2 |=
  4759. ufshcd_get_uic_cmd_result(hba);
  4760. hba->active_uic_cmd->argument3 =
  4761. ufshcd_get_dme_attr_val(hba);
  4762. if (!hba->uic_async_done)
  4763. hba->active_uic_cmd->cmd_active = 0;
  4764. complete(&hba->active_uic_cmd->done);
  4765. retval = IRQ_HANDLED;
  4766. }
  4767. if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
  4768. hba->active_uic_cmd->cmd_active = 0;
  4769. complete(hba->uic_async_done);
  4770. retval = IRQ_HANDLED;
  4771. }
  4772. if (retval == IRQ_HANDLED)
  4773. ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
  4774. UFS_CMD_COMP);
  4775. spin_unlock(hba->host->host_lock);
  4776. return retval;
  4777. }
  4778. /* Release the resources allocated for processing a SCSI command. */
  4779. void ufshcd_release_scsi_cmd(struct ufs_hba *hba,
  4780. struct ufshcd_lrb *lrbp)
  4781. {
  4782. struct scsi_cmnd *cmd = lrbp->cmd;
  4783. scsi_dma_unmap(cmd);
  4784. ufshcd_crypto_clear_prdt(hba, lrbp);
  4785. ufshcd_release(hba);
  4786. ufshcd_clk_scaling_update_busy(hba);
  4787. }
  4788. /**
  4789. * ufshcd_compl_one_cqe - handle a completion queue entry
  4790. * @hba: per adapter instance
  4791. * @task_tag: the task tag of the request to be completed
  4792. * @cqe: pointer to the completion queue entry
  4793. */
  4794. void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag,
  4795. struct cq_entry *cqe)
  4796. {
  4797. struct ufshcd_lrb *lrbp;
  4798. struct scsi_cmnd *cmd;
  4799. enum utp_ocs ocs;
  4800. lrbp = &hba->lrb[task_tag];
  4801. lrbp->compl_time_stamp = ktime_get();
  4802. lrbp->compl_time_stamp_local_clock = local_clock();
  4803. cmd = lrbp->cmd;
  4804. if (cmd) {
  4805. if (unlikely(ufshcd_should_inform_monitor(hba, lrbp)))
  4806. ufshcd_update_monitor(hba, lrbp);
  4807. ufshcd_add_command_trace(hba, task_tag, UFS_CMD_COMP);
  4808. cmd->result = ufshcd_transfer_rsp_status(hba, lrbp, cqe);
  4809. ufshcd_release_scsi_cmd(hba, lrbp);
  4810. /* Do not touch lrbp after scsi done */
  4811. scsi_done(cmd);
  4812. } else if (hba->dev_cmd.complete) {
  4813. if (cqe) {
  4814. ocs = le32_to_cpu(cqe->status) & MASK_OCS;
  4815. lrbp->utr_descriptor_ptr->header.ocs = ocs;
  4816. }
  4817. complete(hba->dev_cmd.complete);
  4818. }
  4819. }
  4820. /**
  4821. * __ufshcd_transfer_req_compl - handle SCSI and query command completion
  4822. * @hba: per adapter instance
  4823. * @completed_reqs: bitmask that indicates which requests to complete
  4824. */
  4825. static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
  4826. unsigned long completed_reqs)
  4827. {
  4828. int tag;
  4829. for_each_set_bit(tag, &completed_reqs, hba->nutrs)
  4830. ufshcd_compl_one_cqe(hba, tag, NULL);
  4831. }
  4832. /* Any value that is not an existing queue number is fine for this constant. */
  4833. enum {
  4834. UFSHCD_POLL_FROM_INTERRUPT_CONTEXT = -1
  4835. };
  4836. static void ufshcd_clear_polled(struct ufs_hba *hba,
  4837. unsigned long *completed_reqs)
  4838. {
  4839. int tag;
  4840. for_each_set_bit(tag, completed_reqs, hba->nutrs) {
  4841. struct scsi_cmnd *cmd = hba->lrb[tag].cmd;
  4842. if (!cmd)
  4843. continue;
  4844. if (scsi_cmd_to_rq(cmd)->cmd_flags & REQ_POLLED)
  4845. __clear_bit(tag, completed_reqs);
  4846. }
  4847. }
  4848. /*
  4849. * Return: > 0 if one or more commands have been completed or 0 if no
  4850. * requests have been completed.
  4851. */
  4852. static int ufshcd_poll(struct Scsi_Host *shost, unsigned int queue_num)
  4853. {
  4854. struct ufs_hba *hba = shost_priv(shost);
  4855. unsigned long completed_reqs, flags;
  4856. u32 tr_doorbell;
  4857. struct ufs_hw_queue *hwq;
  4858. if (hba->mcq_enabled) {
  4859. hwq = &hba->uhq[queue_num];
  4860. return ufshcd_mcq_poll_cqe_lock(hba, hwq);
  4861. }
  4862. spin_lock_irqsave(&hba->outstanding_lock, flags);
  4863. tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  4864. completed_reqs = ~tr_doorbell & hba->outstanding_reqs;
  4865. WARN_ONCE(completed_reqs & ~hba->outstanding_reqs,
  4866. "completed: %#lx; outstanding: %#lx\n", completed_reqs,
  4867. hba->outstanding_reqs);
  4868. if (queue_num == UFSHCD_POLL_FROM_INTERRUPT_CONTEXT) {
  4869. /* Do not complete polled requests from interrupt context. */
  4870. ufshcd_clear_polled(hba, &completed_reqs);
  4871. }
  4872. hba->outstanding_reqs &= ~completed_reqs;
  4873. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  4874. if (completed_reqs)
  4875. __ufshcd_transfer_req_compl(hba, completed_reqs);
  4876. return completed_reqs != 0;
  4877. }
  4878. /**
  4879. * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is
  4880. * invoked from the error handler context or ufshcd_host_reset_and_restore()
  4881. * to complete the pending transfers and free the resources associated with
  4882. * the scsi command.
  4883. *
  4884. * @hba: per adapter instance
  4885. * @force_compl: This flag is set to true when invoked
  4886. * from ufshcd_host_reset_and_restore() in which case it requires special
  4887. * handling because the host controller has been reset by ufshcd_hba_stop().
  4888. */
  4889. static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba,
  4890. bool force_compl)
  4891. {
  4892. struct ufs_hw_queue *hwq;
  4893. struct ufshcd_lrb *lrbp;
  4894. struct scsi_cmnd *cmd;
  4895. unsigned long flags;
  4896. int tag;
  4897. for (tag = 0; tag < hba->nutrs; tag++) {
  4898. lrbp = &hba->lrb[tag];
  4899. cmd = lrbp->cmd;
  4900. if (!ufshcd_cmd_inflight(cmd) ||
  4901. test_bit(SCMD_STATE_COMPLETE, &cmd->state))
  4902. continue;
  4903. hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd));
  4904. if (!hwq)
  4905. continue;
  4906. if (force_compl) {
  4907. ufshcd_mcq_compl_all_cqes_lock(hba, hwq);
  4908. /*
  4909. * For those cmds of which the cqes are not present
  4910. * in the cq, complete them explicitly.
  4911. */
  4912. spin_lock_irqsave(&hwq->cq_lock, flags);
  4913. if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) {
  4914. set_host_byte(cmd, DID_REQUEUE);
  4915. ufshcd_release_scsi_cmd(hba, lrbp);
  4916. scsi_done(cmd);
  4917. }
  4918. spin_unlock_irqrestore(&hwq->cq_lock, flags);
  4919. } else {
  4920. ufshcd_mcq_poll_cqe_lock(hba, hwq);
  4921. }
  4922. }
  4923. }
  4924. /**
  4925. * ufshcd_transfer_req_compl - handle SCSI and query command completion
  4926. * @hba: per adapter instance
  4927. *
  4928. * Return:
  4929. * IRQ_HANDLED - If interrupt is valid
  4930. * IRQ_NONE - If invalid interrupt
  4931. */
  4932. static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
  4933. {
  4934. /* Resetting interrupt aggregation counters first and reading the
  4935. * DOOR_BELL afterward allows us to handle all the completed requests.
  4936. * In order to prevent other interrupts starvation the DB is read once
  4937. * after reset. The down side of this solution is the possibility of
  4938. * false interrupt if device completes another request after resetting
  4939. * aggregation and before reading the DB.
  4940. */
  4941. if (ufshcd_is_intr_aggr_allowed(hba) &&
  4942. !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
  4943. ufshcd_reset_intr_aggr(hba);
  4944. if (ufs_fail_completion(hba))
  4945. return IRQ_HANDLED;
  4946. /*
  4947. * Ignore the ufshcd_poll() return value and return IRQ_HANDLED since we
  4948. * do not want polling to trigger spurious interrupt complaints.
  4949. */
  4950. ufshcd_poll(hba->host, UFSHCD_POLL_FROM_INTERRUPT_CONTEXT);
  4951. return IRQ_HANDLED;
  4952. }
  4953. int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask)
  4954. {
  4955. return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  4956. QUERY_ATTR_IDN_EE_CONTROL, 0, 0,
  4957. &ee_ctrl_mask);
  4958. }
  4959. int ufshcd_write_ee_control(struct ufs_hba *hba)
  4960. {
  4961. int err;
  4962. mutex_lock(&hba->ee_ctrl_mutex);
  4963. err = __ufshcd_write_ee_control(hba, hba->ee_ctrl_mask);
  4964. mutex_unlock(&hba->ee_ctrl_mutex);
  4965. if (err)
  4966. dev_err(hba->dev, "%s: failed to write ee control %d\n",
  4967. __func__, err);
  4968. return err;
  4969. }
  4970. int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
  4971. const u16 *other_mask, u16 set, u16 clr)
  4972. {
  4973. u16 new_mask, ee_ctrl_mask;
  4974. int err = 0;
  4975. mutex_lock(&hba->ee_ctrl_mutex);
  4976. new_mask = (*mask & ~clr) | set;
  4977. ee_ctrl_mask = new_mask | *other_mask;
  4978. if (ee_ctrl_mask != hba->ee_ctrl_mask)
  4979. err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
  4980. /* Still need to update 'mask' even if 'ee_ctrl_mask' was unchanged */
  4981. if (!err) {
  4982. hba->ee_ctrl_mask = ee_ctrl_mask;
  4983. *mask = new_mask;
  4984. }
  4985. mutex_unlock(&hba->ee_ctrl_mutex);
  4986. return err;
  4987. }
  4988. /**
  4989. * ufshcd_disable_ee - disable exception event
  4990. * @hba: per-adapter instance
  4991. * @mask: exception event to disable
  4992. *
  4993. * Disables exception event in the device so that the EVENT_ALERT
  4994. * bit is not set.
  4995. *
  4996. * Return: zero on success, non-zero error value on failure.
  4997. */
  4998. static inline int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
  4999. {
  5000. return ufshcd_update_ee_drv_mask(hba, 0, mask);
  5001. }
  5002. /**
  5003. * ufshcd_enable_ee - enable exception event
  5004. * @hba: per-adapter instance
  5005. * @mask: exception event to enable
  5006. *
  5007. * Enable corresponding exception event in the device to allow
  5008. * device to alert host in critical scenarios.
  5009. *
  5010. * Return: zero on success, non-zero error value on failure.
  5011. */
  5012. static inline int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
  5013. {
  5014. return ufshcd_update_ee_drv_mask(hba, mask, 0);
  5015. }
  5016. /**
  5017. * ufshcd_enable_auto_bkops - Allow device managed BKOPS
  5018. * @hba: per-adapter instance
  5019. *
  5020. * Allow device to manage background operations on its own. Enabling
  5021. * this might lead to inconsistent latencies during normal data transfers
  5022. * as the device is allowed to manage its own way of handling background
  5023. * operations.
  5024. *
  5025. * Return: zero on success, non-zero on failure.
  5026. */
  5027. static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
  5028. {
  5029. int err = 0;
  5030. if (hba->auto_bkops_enabled)
  5031. goto out;
  5032. err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
  5033. QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
  5034. if (err) {
  5035. dev_err(hba->dev, "%s: failed to enable bkops %d\n",
  5036. __func__, err);
  5037. goto out;
  5038. }
  5039. hba->auto_bkops_enabled = true;
  5040. trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
  5041. /* No need of URGENT_BKOPS exception from the device */
  5042. err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
  5043. if (err)
  5044. dev_err(hba->dev, "%s: failed to disable exception event %d\n",
  5045. __func__, err);
  5046. out:
  5047. return err;
  5048. }
  5049. /**
  5050. * ufshcd_disable_auto_bkops - block device in doing background operations
  5051. * @hba: per-adapter instance
  5052. *
  5053. * Disabling background operations improves command response latency but
  5054. * has drawback of device moving into critical state where the device is
  5055. * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
  5056. * host is idle so that BKOPS are managed effectively without any negative
  5057. * impacts.
  5058. *
  5059. * Return: zero on success, non-zero on failure.
  5060. */
  5061. static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
  5062. {
  5063. int err = 0;
  5064. if (!hba->auto_bkops_enabled)
  5065. goto out;
  5066. /*
  5067. * If host assisted BKOPs is to be enabled, make sure
  5068. * urgent bkops exception is allowed.
  5069. */
  5070. err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
  5071. if (err) {
  5072. dev_err(hba->dev, "%s: failed to enable exception event %d\n",
  5073. __func__, err);
  5074. goto out;
  5075. }
  5076. err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
  5077. QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
  5078. if (err) {
  5079. dev_err(hba->dev, "%s: failed to disable bkops %d\n",
  5080. __func__, err);
  5081. ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
  5082. goto out;
  5083. }
  5084. hba->auto_bkops_enabled = false;
  5085. trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
  5086. hba->is_urgent_bkops_lvl_checked = false;
  5087. out:
  5088. return err;
  5089. }
  5090. /**
  5091. * ufshcd_force_reset_auto_bkops - force reset auto bkops state
  5092. * @hba: per adapter instance
  5093. *
  5094. * After a device reset the device may toggle the BKOPS_EN flag
  5095. * to default value. The s/w tracking variables should be updated
  5096. * as well. This function would change the auto-bkops state based on
  5097. * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
  5098. */
  5099. static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
  5100. {
  5101. if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
  5102. hba->auto_bkops_enabled = false;
  5103. hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
  5104. ufshcd_enable_auto_bkops(hba);
  5105. } else {
  5106. hba->auto_bkops_enabled = true;
  5107. hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
  5108. ufshcd_disable_auto_bkops(hba);
  5109. }
  5110. hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
  5111. hba->is_urgent_bkops_lvl_checked = false;
  5112. }
  5113. static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
  5114. {
  5115. return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5116. QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
  5117. }
  5118. /**
  5119. * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
  5120. * @hba: per-adapter instance
  5121. *
  5122. * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
  5123. * flag in the device to permit background operations if the device
  5124. * bkops_status is greater than or equal to the "hba->urgent_bkops_lvl",
  5125. * disable otherwise.
  5126. *
  5127. * Return: 0 for success, non-zero in case of failure.
  5128. *
  5129. * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
  5130. * to know whether auto bkops is enabled or disabled after this function
  5131. * returns control to it.
  5132. */
  5133. static int ufshcd_bkops_ctrl(struct ufs_hba *hba)
  5134. {
  5135. enum bkops_status status = hba->urgent_bkops_lvl;
  5136. u32 curr_status = 0;
  5137. int err;
  5138. err = ufshcd_get_bkops_status(hba, &curr_status);
  5139. if (err) {
  5140. dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
  5141. __func__, err);
  5142. goto out;
  5143. } else if (curr_status > BKOPS_STATUS_MAX) {
  5144. dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
  5145. __func__, curr_status);
  5146. err = -EINVAL;
  5147. goto out;
  5148. }
  5149. if (curr_status >= status)
  5150. err = ufshcd_enable_auto_bkops(hba);
  5151. else
  5152. err = ufshcd_disable_auto_bkops(hba);
  5153. out:
  5154. return err;
  5155. }
  5156. static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
  5157. {
  5158. return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5159. QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
  5160. }
  5161. static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
  5162. {
  5163. int err;
  5164. u32 curr_status = 0;
  5165. if (hba->is_urgent_bkops_lvl_checked)
  5166. goto enable_auto_bkops;
  5167. err = ufshcd_get_bkops_status(hba, &curr_status);
  5168. if (err) {
  5169. dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
  5170. __func__, err);
  5171. goto out;
  5172. }
  5173. /*
  5174. * We are seeing that some devices are raising the urgent bkops
  5175. * exception events even when BKOPS status doesn't indicate performace
  5176. * impacted or critical. Handle these device by determining their urgent
  5177. * bkops status at runtime.
  5178. */
  5179. if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
  5180. dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
  5181. __func__, curr_status);
  5182. /* update the current status as the urgent bkops level */
  5183. hba->urgent_bkops_lvl = curr_status;
  5184. hba->is_urgent_bkops_lvl_checked = true;
  5185. }
  5186. enable_auto_bkops:
  5187. err = ufshcd_enable_auto_bkops(hba);
  5188. out:
  5189. if (err < 0)
  5190. dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
  5191. __func__, err);
  5192. }
  5193. static void ufshcd_temp_exception_event_handler(struct ufs_hba *hba, u16 status)
  5194. {
  5195. u32 value;
  5196. if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5197. QUERY_ATTR_IDN_CASE_ROUGH_TEMP, 0, 0, &value))
  5198. return;
  5199. dev_info(hba->dev, "exception Tcase %d\n", value - 80);
  5200. ufs_hwmon_notify_event(hba, status & MASK_EE_URGENT_TEMP);
  5201. /*
  5202. * A placeholder for the platform vendors to add whatever additional
  5203. * steps required
  5204. */
  5205. }
  5206. static int __ufshcd_wb_toggle(struct ufs_hba *hba, bool set, enum flag_idn idn)
  5207. {
  5208. u8 index;
  5209. enum query_opcode opcode = set ? UPIU_QUERY_OPCODE_SET_FLAG :
  5210. UPIU_QUERY_OPCODE_CLEAR_FLAG;
  5211. index = ufshcd_wb_get_query_index(hba);
  5212. return ufshcd_query_flag_retry(hba, opcode, idn, index, NULL);
  5213. }
  5214. int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable)
  5215. {
  5216. int ret;
  5217. if (!ufshcd_is_wb_allowed(hba) ||
  5218. hba->dev_info.wb_enabled == enable)
  5219. return 0;
  5220. ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_EN);
  5221. if (ret) {
  5222. dev_err(hba->dev, "%s: Write Booster %s failed %d\n",
  5223. __func__, enable ? "enabling" : "disabling", ret);
  5224. return ret;
  5225. }
  5226. hba->dev_info.wb_enabled = enable;
  5227. dev_dbg(hba->dev, "%s: Write Booster %s\n",
  5228. __func__, enable ? "enabled" : "disabled");
  5229. return ret;
  5230. }
  5231. static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba,
  5232. bool enable)
  5233. {
  5234. int ret;
  5235. ret = __ufshcd_wb_toggle(hba, enable,
  5236. QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8);
  5237. if (ret) {
  5238. dev_err(hba->dev, "%s: WB-Buf Flush during H8 %s failed %d\n",
  5239. __func__, enable ? "enabling" : "disabling", ret);
  5240. return;
  5241. }
  5242. dev_dbg(hba->dev, "%s: WB-Buf Flush during H8 %s\n",
  5243. __func__, enable ? "enabled" : "disabled");
  5244. }
  5245. int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable)
  5246. {
  5247. int ret;
  5248. if (!ufshcd_is_wb_allowed(hba) ||
  5249. hba->dev_info.wb_buf_flush_enabled == enable)
  5250. return 0;
  5251. ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN);
  5252. if (ret) {
  5253. dev_err(hba->dev, "%s: WB-Buf Flush %s failed %d\n",
  5254. __func__, enable ? "enabling" : "disabling", ret);
  5255. return ret;
  5256. }
  5257. hba->dev_info.wb_buf_flush_enabled = enable;
  5258. dev_dbg(hba->dev, "%s: WB-Buf Flush %s\n",
  5259. __func__, enable ? "enabled" : "disabled");
  5260. return ret;
  5261. }
  5262. static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
  5263. u32 avail_buf)
  5264. {
  5265. u32 cur_buf;
  5266. int ret;
  5267. u8 index;
  5268. index = ufshcd_wb_get_query_index(hba);
  5269. ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5270. QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
  5271. index, 0, &cur_buf);
  5272. if (ret) {
  5273. dev_err(hba->dev, "%s: dCurWriteBoosterBufferSize read failed %d\n",
  5274. __func__, ret);
  5275. return false;
  5276. }
  5277. if (!cur_buf) {
  5278. dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
  5279. cur_buf);
  5280. return false;
  5281. }
  5282. /* Let it continue to flush when available buffer exceeds threshold */
  5283. return avail_buf < hba->vps->wb_flush_threshold;
  5284. }
  5285. static void ufshcd_wb_force_disable(struct ufs_hba *hba)
  5286. {
  5287. if (ufshcd_is_wb_buf_flush_allowed(hba))
  5288. ufshcd_wb_toggle_buf_flush(hba, false);
  5289. ufshcd_wb_toggle_buf_flush_during_h8(hba, false);
  5290. ufshcd_wb_toggle(hba, false);
  5291. hba->caps &= ~UFSHCD_CAP_WB_EN;
  5292. dev_info(hba->dev, "%s: WB force disabled\n", __func__);
  5293. }
  5294. static bool ufshcd_is_wb_buf_lifetime_available(struct ufs_hba *hba)
  5295. {
  5296. u32 lifetime;
  5297. int ret;
  5298. u8 index;
  5299. index = ufshcd_wb_get_query_index(hba);
  5300. ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5301. QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST,
  5302. index, 0, &lifetime);
  5303. if (ret) {
  5304. dev_err(hba->dev,
  5305. "%s: bWriteBoosterBufferLifeTimeEst read failed %d\n",
  5306. __func__, ret);
  5307. return false;
  5308. }
  5309. if (lifetime == UFS_WB_EXCEED_LIFETIME) {
  5310. dev_err(hba->dev, "%s: WB buf lifetime is exhausted 0x%02X\n",
  5311. __func__, lifetime);
  5312. return false;
  5313. }
  5314. dev_dbg(hba->dev, "%s: WB buf lifetime is 0x%02X\n",
  5315. __func__, lifetime);
  5316. return true;
  5317. }
  5318. static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
  5319. {
  5320. int ret;
  5321. u32 avail_buf;
  5322. u8 index;
  5323. if (!ufshcd_is_wb_allowed(hba))
  5324. return false;
  5325. if (!ufshcd_is_wb_buf_lifetime_available(hba)) {
  5326. ufshcd_wb_force_disable(hba);
  5327. return false;
  5328. }
  5329. /*
  5330. * The ufs device needs the vcc to be ON to flush.
  5331. * With user-space reduction enabled, it's enough to enable flush
  5332. * by checking only the available buffer. The threshold
  5333. * defined here is > 90% full.
  5334. * With user-space preserved enabled, the current-buffer
  5335. * should be checked too because the wb buffer size can reduce
  5336. * when disk tends to be full. This info is provided by current
  5337. * buffer (dCurrentWriteBoosterBufferSize). There's no point in
  5338. * keeping vcc on when current buffer is empty.
  5339. */
  5340. index = ufshcd_wb_get_query_index(hba);
  5341. ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  5342. QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
  5343. index, 0, &avail_buf);
  5344. if (ret) {
  5345. dev_warn(hba->dev, "%s: dAvailableWriteBoosterBufferSize read failed %d\n",
  5346. __func__, ret);
  5347. return false;
  5348. }
  5349. if (!hba->dev_info.b_presrv_uspc_en)
  5350. return avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10);
  5351. return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
  5352. }
  5353. static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
  5354. {
  5355. struct ufs_hba *hba = container_of(to_delayed_work(work),
  5356. struct ufs_hba,
  5357. rpm_dev_flush_recheck_work);
  5358. /*
  5359. * To prevent unnecessary VCC power drain after device finishes
  5360. * WriteBooster buffer flush or Auto BKOPs, force runtime resume
  5361. * after a certain delay to recheck the threshold by next runtime
  5362. * suspend.
  5363. */
  5364. ufshcd_rpm_get_sync(hba);
  5365. ufshcd_rpm_put_sync(hba);
  5366. }
  5367. /**
  5368. * ufshcd_exception_event_handler - handle exceptions raised by device
  5369. * @work: pointer to work data
  5370. *
  5371. * Read bExceptionEventStatus attribute from the device and handle the
  5372. * exception event accordingly.
  5373. */
  5374. static void ufshcd_exception_event_handler(struct work_struct *work)
  5375. {
  5376. struct ufs_hba *hba;
  5377. int err;
  5378. u32 status = 0;
  5379. hba = container_of(work, struct ufs_hba, eeh_work);
  5380. ufshcd_scsi_block_requests(hba);
  5381. err = ufshcd_get_ee_status(hba, &status);
  5382. if (err) {
  5383. dev_err(hba->dev, "%s: failed to get exception status %d\n",
  5384. __func__, err);
  5385. goto out;
  5386. }
  5387. trace_ufshcd_exception_event(dev_name(hba->dev), status);
  5388. if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS)
  5389. ufshcd_bkops_exception_event_handler(hba);
  5390. if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP)
  5391. ufshcd_temp_exception_event_handler(hba, status);
  5392. ufs_debugfs_exception_event(hba, status);
  5393. out:
  5394. ufshcd_scsi_unblock_requests(hba);
  5395. }
  5396. /* Complete requests that have door-bell cleared */
  5397. static void ufshcd_complete_requests(struct ufs_hba *hba, bool force_compl)
  5398. {
  5399. if (hba->mcq_enabled)
  5400. ufshcd_mcq_compl_pending_transfer(hba, force_compl);
  5401. else
  5402. ufshcd_transfer_req_compl(hba);
  5403. ufshcd_tmc_handler(hba);
  5404. }
  5405. /**
  5406. * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
  5407. * to recover from the DL NAC errors or not.
  5408. * @hba: per-adapter instance
  5409. *
  5410. * Return: true if error handling is required, false otherwise.
  5411. */
  5412. static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
  5413. {
  5414. unsigned long flags;
  5415. bool err_handling = true;
  5416. spin_lock_irqsave(hba->host->host_lock, flags);
  5417. /*
  5418. * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
  5419. * device fatal error and/or DL NAC & REPLAY timeout errors.
  5420. */
  5421. if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
  5422. goto out;
  5423. if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
  5424. ((hba->saved_err & UIC_ERROR) &&
  5425. (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
  5426. goto out;
  5427. if ((hba->saved_err & UIC_ERROR) &&
  5428. (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
  5429. int err;
  5430. /*
  5431. * wait for 50ms to see if we can get any other errors or not.
  5432. */
  5433. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5434. msleep(50);
  5435. spin_lock_irqsave(hba->host->host_lock, flags);
  5436. /*
  5437. * now check if we have got any other severe errors other than
  5438. * DL NAC error?
  5439. */
  5440. if ((hba->saved_err & INT_FATAL_ERRORS) ||
  5441. ((hba->saved_err & UIC_ERROR) &&
  5442. (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
  5443. goto out;
  5444. /*
  5445. * As DL NAC is the only error received so far, send out NOP
  5446. * command to confirm if link is still active or not.
  5447. * - If we don't get any response then do error recovery.
  5448. * - If we get response then clear the DL NAC error bit.
  5449. */
  5450. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5451. err = ufshcd_verify_dev_init(hba);
  5452. spin_lock_irqsave(hba->host->host_lock, flags);
  5453. if (err)
  5454. goto out;
  5455. /* Link seems to be alive hence ignore the DL NAC errors */
  5456. if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
  5457. hba->saved_err &= ~UIC_ERROR;
  5458. /* clear NAC error */
  5459. hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
  5460. if (!hba->saved_uic_err)
  5461. err_handling = false;
  5462. }
  5463. out:
  5464. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5465. return err_handling;
  5466. }
  5467. /* host lock must be held before calling this func */
  5468. static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba)
  5469. {
  5470. return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) ||
  5471. (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK));
  5472. }
  5473. void ufshcd_schedule_eh_work(struct ufs_hba *hba)
  5474. {
  5475. lockdep_assert_held(hba->host->host_lock);
  5476. /* handle fatal errors only when link is not in error state */
  5477. if (hba->ufshcd_state != UFSHCD_STATE_ERROR) {
  5478. if (hba->force_reset || ufshcd_is_link_broken(hba) ||
  5479. ufshcd_is_saved_err_fatal(hba))
  5480. hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL;
  5481. else
  5482. hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
  5483. queue_work(hba->eh_wq, &hba->eh_work);
  5484. }
  5485. }
  5486. static void ufshcd_force_error_recovery(struct ufs_hba *hba)
  5487. {
  5488. spin_lock_irq(hba->host->host_lock);
  5489. hba->force_reset = true;
  5490. ufshcd_schedule_eh_work(hba);
  5491. spin_unlock_irq(hba->host->host_lock);
  5492. }
  5493. static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow)
  5494. {
  5495. mutex_lock(&hba->wb_mutex);
  5496. down_write(&hba->clk_scaling_lock);
  5497. hba->clk_scaling.is_allowed = allow;
  5498. up_write(&hba->clk_scaling_lock);
  5499. mutex_unlock(&hba->wb_mutex);
  5500. }
  5501. static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend)
  5502. {
  5503. if (suspend) {
  5504. if (hba->clk_scaling.is_enabled)
  5505. ufshcd_suspend_clkscaling(hba);
  5506. ufshcd_clk_scaling_allow(hba, false);
  5507. } else {
  5508. ufshcd_clk_scaling_allow(hba, true);
  5509. if (hba->clk_scaling.is_enabled)
  5510. ufshcd_resume_clkscaling(hba);
  5511. }
  5512. }
  5513. static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
  5514. {
  5515. ufshcd_rpm_get_sync(hba);
  5516. if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev) ||
  5517. hba->is_sys_suspended) {
  5518. enum ufs_pm_op pm_op;
  5519. /*
  5520. * Don't assume anything of resume, if
  5521. * resume fails, irq and clocks can be OFF, and powers
  5522. * can be OFF or in LPM.
  5523. */
  5524. ufshcd_setup_hba_vreg(hba, true);
  5525. ufshcd_enable_irq(hba);
  5526. ufshcd_setup_vreg(hba, true);
  5527. ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
  5528. ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
  5529. ufshcd_hold(hba);
  5530. if (!ufshcd_is_clkgating_allowed(hba))
  5531. ufshcd_setup_clocks(hba, true);
  5532. pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
  5533. ufshcd_vops_resume(hba, pm_op);
  5534. } else {
  5535. ufshcd_hold(hba);
  5536. if (ufshcd_is_clkscaling_supported(hba) &&
  5537. hba->clk_scaling.is_enabled)
  5538. ufshcd_suspend_clkscaling(hba);
  5539. ufshcd_clk_scaling_allow(hba, false);
  5540. }
  5541. ufshcd_scsi_block_requests(hba);
  5542. /* Wait for ongoing ufshcd_queuecommand() calls to finish. */
  5543. blk_mq_wait_quiesce_done(&hba->host->tag_set);
  5544. cancel_work_sync(&hba->eeh_work);
  5545. }
  5546. static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
  5547. {
  5548. ufshcd_scsi_unblock_requests(hba);
  5549. ufshcd_release(hba);
  5550. if (ufshcd_is_clkscaling_supported(hba))
  5551. ufshcd_clk_scaling_suspend(hba, false);
  5552. ufshcd_rpm_put(hba);
  5553. }
  5554. static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba)
  5555. {
  5556. return (!hba->is_powered || hba->shutting_down ||
  5557. !hba->ufs_device_wlun ||
  5558. hba->ufshcd_state == UFSHCD_STATE_ERROR ||
  5559. (!(hba->saved_err || hba->saved_uic_err || hba->force_reset ||
  5560. ufshcd_is_link_broken(hba))));
  5561. }
  5562. #ifdef CONFIG_PM
  5563. static void ufshcd_recover_pm_error(struct ufs_hba *hba)
  5564. {
  5565. struct Scsi_Host *shost = hba->host;
  5566. struct scsi_device *sdev;
  5567. struct request_queue *q;
  5568. int ret;
  5569. hba->is_sys_suspended = false;
  5570. /*
  5571. * Set RPM status of wlun device to RPM_ACTIVE,
  5572. * this also clears its runtime error.
  5573. */
  5574. ret = pm_runtime_set_active(&hba->ufs_device_wlun->sdev_gendev);
  5575. /* hba device might have a runtime error otherwise */
  5576. if (ret)
  5577. ret = pm_runtime_set_active(hba->dev);
  5578. /*
  5579. * If wlun device had runtime error, we also need to resume those
  5580. * consumer scsi devices in case any of them has failed to be
  5581. * resumed due to supplier runtime resume failure. This is to unblock
  5582. * blk_queue_enter in case there are bios waiting inside it.
  5583. */
  5584. if (!ret) {
  5585. shost_for_each_device(sdev, shost) {
  5586. q = sdev->request_queue;
  5587. if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
  5588. q->rpm_status == RPM_SUSPENDING))
  5589. pm_request_resume(q->dev);
  5590. }
  5591. }
  5592. }
  5593. #else
  5594. static inline void ufshcd_recover_pm_error(struct ufs_hba *hba)
  5595. {
  5596. }
  5597. #endif
  5598. static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba)
  5599. {
  5600. struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info;
  5601. u32 mode;
  5602. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode);
  5603. if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK))
  5604. return true;
  5605. if (pwr_info->pwr_tx != (mode & PWRMODE_MASK))
  5606. return true;
  5607. return false;
  5608. }
  5609. static bool ufshcd_abort_one(struct request *rq, void *priv)
  5610. {
  5611. int *ret = priv;
  5612. u32 tag = rq->tag;
  5613. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  5614. struct scsi_device *sdev = cmd->device;
  5615. struct Scsi_Host *shost = sdev->host;
  5616. struct ufs_hba *hba = shost_priv(shost);
  5617. *ret = ufshcd_try_to_abort_task(hba, tag);
  5618. dev_err(hba->dev, "Aborting tag %d / CDB %#02x %s\n", tag,
  5619. hba->lrb[tag].cmd ? hba->lrb[tag].cmd->cmnd[0] : -1,
  5620. *ret ? "failed" : "succeeded");
  5621. return *ret == 0;
  5622. }
  5623. /**
  5624. * ufshcd_abort_all - Abort all pending commands.
  5625. * @hba: Host bus adapter pointer.
  5626. *
  5627. * Return: true if and only if the host controller needs to be reset.
  5628. */
  5629. static bool ufshcd_abort_all(struct ufs_hba *hba)
  5630. {
  5631. int tag, ret = 0;
  5632. blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_abort_one, &ret);
  5633. if (ret)
  5634. goto out;
  5635. /* Clear pending task management requests */
  5636. for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
  5637. ret = ufshcd_clear_tm_cmd(hba, tag);
  5638. if (ret)
  5639. goto out;
  5640. }
  5641. out:
  5642. /* Complete the requests that are cleared by s/w */
  5643. ufshcd_complete_requests(hba, false);
  5644. return ret != 0;
  5645. }
  5646. /**
  5647. * ufshcd_err_handler - handle UFS errors that require s/w attention
  5648. * @work: pointer to work structure
  5649. */
  5650. static void ufshcd_err_handler(struct work_struct *work)
  5651. {
  5652. int retries = MAX_ERR_HANDLER_RETRIES;
  5653. struct ufs_hba *hba;
  5654. unsigned long flags;
  5655. bool needs_restore;
  5656. bool needs_reset;
  5657. int pmc_err;
  5658. hba = container_of(work, struct ufs_hba, eh_work);
  5659. dev_info(hba->dev,
  5660. "%s started; HBA state %s; powered %d; shutting down %d; saved_err = %d; saved_uic_err = %d; force_reset = %d%s\n",
  5661. __func__, ufshcd_state_name[hba->ufshcd_state],
  5662. hba->is_powered, hba->shutting_down, hba->saved_err,
  5663. hba->saved_uic_err, hba->force_reset,
  5664. ufshcd_is_link_broken(hba) ? "; link is broken" : "");
  5665. down(&hba->host_sem);
  5666. spin_lock_irqsave(hba->host->host_lock, flags);
  5667. if (ufshcd_err_handling_should_stop(hba)) {
  5668. if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
  5669. hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
  5670. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5671. up(&hba->host_sem);
  5672. return;
  5673. }
  5674. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5675. ufshcd_err_handling_prepare(hba);
  5676. spin_lock_irqsave(hba->host->host_lock, flags);
  5677. ufshcd_set_eh_in_progress(hba);
  5678. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5679. /* Complete requests that have door-bell cleared by h/w */
  5680. ufshcd_complete_requests(hba, false);
  5681. spin_lock_irqsave(hba->host->host_lock, flags);
  5682. again:
  5683. needs_restore = false;
  5684. needs_reset = false;
  5685. if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
  5686. hba->ufshcd_state = UFSHCD_STATE_RESET;
  5687. /*
  5688. * A full reset and restore might have happened after preparation
  5689. * is finished, double check whether we should stop.
  5690. */
  5691. if (ufshcd_err_handling_should_stop(hba))
  5692. goto skip_err_handling;
  5693. if ((hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) &&
  5694. !hba->force_reset) {
  5695. bool ret;
  5696. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5697. /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
  5698. ret = ufshcd_quirk_dl_nac_errors(hba);
  5699. spin_lock_irqsave(hba->host->host_lock, flags);
  5700. if (!ret && ufshcd_err_handling_should_stop(hba))
  5701. goto skip_err_handling;
  5702. }
  5703. if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
  5704. (hba->saved_uic_err &&
  5705. (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
  5706. bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR);
  5707. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5708. ufshcd_print_host_state(hba);
  5709. ufshcd_print_pwr_info(hba);
  5710. ufshcd_print_evt_hist(hba);
  5711. ufshcd_print_tmrs(hba, hba->outstanding_tasks);
  5712. ufshcd_print_trs_all(hba, pr_prdt);
  5713. spin_lock_irqsave(hba->host->host_lock, flags);
  5714. }
  5715. /*
  5716. * if host reset is required then skip clearing the pending
  5717. * transfers forcefully because they will get cleared during
  5718. * host reset and restore
  5719. */
  5720. if (hba->force_reset || ufshcd_is_link_broken(hba) ||
  5721. ufshcd_is_saved_err_fatal(hba) ||
  5722. ((hba->saved_err & UIC_ERROR) &&
  5723. (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
  5724. UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
  5725. needs_reset = true;
  5726. goto do_reset;
  5727. }
  5728. /*
  5729. * If LINERESET was caught, UFS might have been put to PWM mode,
  5730. * check if power mode restore is needed.
  5731. */
  5732. if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
  5733. hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
  5734. if (!hba->saved_uic_err)
  5735. hba->saved_err &= ~UIC_ERROR;
  5736. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5737. if (ufshcd_is_pwr_mode_restore_needed(hba))
  5738. needs_restore = true;
  5739. spin_lock_irqsave(hba->host->host_lock, flags);
  5740. if (!hba->saved_err && !needs_restore)
  5741. goto skip_err_handling;
  5742. }
  5743. hba->silence_err_logs = true;
  5744. /* release lock as clear command might sleep */
  5745. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5746. needs_reset = ufshcd_abort_all(hba);
  5747. spin_lock_irqsave(hba->host->host_lock, flags);
  5748. hba->silence_err_logs = false;
  5749. if (needs_reset)
  5750. goto do_reset;
  5751. /*
  5752. * After all reqs and tasks are cleared from doorbell,
  5753. * now it is safe to retore power mode.
  5754. */
  5755. if (needs_restore) {
  5756. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5757. /*
  5758. * Hold the scaling lock just in case dev cmds
  5759. * are sent via bsg and/or sysfs.
  5760. */
  5761. down_write(&hba->clk_scaling_lock);
  5762. hba->force_pmc = true;
  5763. pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info));
  5764. if (pmc_err) {
  5765. needs_reset = true;
  5766. dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
  5767. __func__, pmc_err);
  5768. }
  5769. hba->force_pmc = false;
  5770. ufshcd_print_pwr_info(hba);
  5771. up_write(&hba->clk_scaling_lock);
  5772. spin_lock_irqsave(hba->host->host_lock, flags);
  5773. }
  5774. do_reset:
  5775. /* Fatal errors need reset */
  5776. if (needs_reset) {
  5777. int err;
  5778. hba->force_reset = false;
  5779. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5780. err = ufshcd_reset_and_restore(hba);
  5781. if (err)
  5782. dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
  5783. __func__, err);
  5784. else
  5785. ufshcd_recover_pm_error(hba);
  5786. spin_lock_irqsave(hba->host->host_lock, flags);
  5787. }
  5788. skip_err_handling:
  5789. if (!needs_reset) {
  5790. if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  5791. hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
  5792. if (hba->saved_err || hba->saved_uic_err)
  5793. dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
  5794. __func__, hba->saved_err, hba->saved_uic_err);
  5795. }
  5796. /* Exit in an operational state or dead */
  5797. if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
  5798. hba->ufshcd_state != UFSHCD_STATE_ERROR) {
  5799. if (--retries)
  5800. goto again;
  5801. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  5802. }
  5803. ufshcd_clear_eh_in_progress(hba);
  5804. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5805. ufshcd_err_handling_unprepare(hba);
  5806. up(&hba->host_sem);
  5807. dev_info(hba->dev, "%s finished; HBA state %s\n", __func__,
  5808. ufshcd_state_name[hba->ufshcd_state]);
  5809. }
  5810. /**
  5811. * ufshcd_update_uic_error - check and set fatal UIC error flags.
  5812. * @hba: per-adapter instance
  5813. *
  5814. * Return:
  5815. * IRQ_HANDLED - If interrupt is valid
  5816. * IRQ_NONE - If invalid interrupt
  5817. */
  5818. static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
  5819. {
  5820. u32 reg;
  5821. irqreturn_t retval = IRQ_NONE;
  5822. /* PHY layer error */
  5823. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
  5824. if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
  5825. (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
  5826. ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg);
  5827. /*
  5828. * To know whether this error is fatal or not, DB timeout
  5829. * must be checked but this error is handled separately.
  5830. */
  5831. if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
  5832. dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
  5833. __func__);
  5834. /* Got a LINERESET indication. */
  5835. if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
  5836. struct uic_command *cmd = NULL;
  5837. hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR;
  5838. if (hba->uic_async_done && hba->active_uic_cmd)
  5839. cmd = hba->active_uic_cmd;
  5840. /*
  5841. * Ignore the LINERESET during power mode change
  5842. * operation via DME_SET command.
  5843. */
  5844. if (cmd && (cmd->command == UIC_CMD_DME_SET))
  5845. hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
  5846. }
  5847. retval |= IRQ_HANDLED;
  5848. }
  5849. /* PA_INIT_ERROR is fatal and needs UIC reset */
  5850. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
  5851. if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
  5852. (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
  5853. ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
  5854. if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
  5855. hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
  5856. else if (hba->dev_quirks &
  5857. UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
  5858. if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
  5859. hba->uic_error |=
  5860. UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
  5861. else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
  5862. hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
  5863. }
  5864. retval |= IRQ_HANDLED;
  5865. }
  5866. /* UIC NL/TL/DME errors needs software retry */
  5867. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
  5868. if ((reg & UIC_NETWORK_LAYER_ERROR) &&
  5869. (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
  5870. ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg);
  5871. hba->uic_error |= UFSHCD_UIC_NL_ERROR;
  5872. retval |= IRQ_HANDLED;
  5873. }
  5874. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
  5875. if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
  5876. (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
  5877. ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg);
  5878. hba->uic_error |= UFSHCD_UIC_TL_ERROR;
  5879. retval |= IRQ_HANDLED;
  5880. }
  5881. reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
  5882. if ((reg & UIC_DME_ERROR) &&
  5883. (reg & UIC_DME_ERROR_CODE_MASK)) {
  5884. ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg);
  5885. hba->uic_error |= UFSHCD_UIC_DME_ERROR;
  5886. retval |= IRQ_HANDLED;
  5887. }
  5888. dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
  5889. __func__, hba->uic_error);
  5890. return retval;
  5891. }
  5892. /**
  5893. * ufshcd_check_errors - Check for errors that need s/w attention
  5894. * @hba: per-adapter instance
  5895. * @intr_status: interrupt status generated by the controller
  5896. *
  5897. * Return:
  5898. * IRQ_HANDLED - If interrupt is valid
  5899. * IRQ_NONE - If invalid interrupt
  5900. */
  5901. static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba, u32 intr_status)
  5902. {
  5903. bool queue_eh_work = false;
  5904. irqreturn_t retval = IRQ_NONE;
  5905. spin_lock(hba->host->host_lock);
  5906. hba->errors |= UFSHCD_ERROR_MASK & intr_status;
  5907. if (hba->errors & INT_FATAL_ERRORS) {
  5908. ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR,
  5909. hba->errors);
  5910. queue_eh_work = true;
  5911. }
  5912. if (hba->errors & UIC_ERROR) {
  5913. hba->uic_error = 0;
  5914. retval = ufshcd_update_uic_error(hba);
  5915. if (hba->uic_error)
  5916. queue_eh_work = true;
  5917. }
  5918. if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
  5919. dev_err(hba->dev,
  5920. "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
  5921. __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
  5922. "Enter" : "Exit",
  5923. hba->errors, ufshcd_get_upmcrs(hba));
  5924. ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR,
  5925. hba->errors);
  5926. ufshcd_set_link_broken(hba);
  5927. queue_eh_work = true;
  5928. }
  5929. if (queue_eh_work) {
  5930. /*
  5931. * update the transfer error masks to sticky bits, let's do this
  5932. * irrespective of current ufshcd_state.
  5933. */
  5934. hba->saved_err |= hba->errors;
  5935. hba->saved_uic_err |= hba->uic_error;
  5936. /* dump controller state before resetting */
  5937. if ((hba->saved_err &
  5938. (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
  5939. (hba->saved_uic_err &&
  5940. (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
  5941. dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
  5942. __func__, hba->saved_err,
  5943. hba->saved_uic_err);
  5944. ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE,
  5945. "host_regs: ");
  5946. ufshcd_print_pwr_info(hba);
  5947. }
  5948. ufshcd_schedule_eh_work(hba);
  5949. retval |= IRQ_HANDLED;
  5950. }
  5951. /*
  5952. * if (!queue_eh_work) -
  5953. * Other errors are either non-fatal where host recovers
  5954. * itself without s/w intervention or errors that will be
  5955. * handled by the SCSI core layer.
  5956. */
  5957. hba->errors = 0;
  5958. hba->uic_error = 0;
  5959. spin_unlock(hba->host->host_lock);
  5960. return retval;
  5961. }
  5962. /**
  5963. * ufshcd_tmc_handler - handle task management function completion
  5964. * @hba: per adapter instance
  5965. *
  5966. * Return:
  5967. * IRQ_HANDLED - If interrupt is valid
  5968. * IRQ_NONE - If invalid interrupt
  5969. */
  5970. static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
  5971. {
  5972. unsigned long flags, pending, issued;
  5973. irqreturn_t ret = IRQ_NONE;
  5974. int tag;
  5975. spin_lock_irqsave(hba->host->host_lock, flags);
  5976. pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
  5977. issued = hba->outstanding_tasks & ~pending;
  5978. for_each_set_bit(tag, &issued, hba->nutmrs) {
  5979. struct request *req = hba->tmf_rqs[tag];
  5980. struct completion *c = req->end_io_data;
  5981. complete(c);
  5982. ret = IRQ_HANDLED;
  5983. }
  5984. spin_unlock_irqrestore(hba->host->host_lock, flags);
  5985. return ret;
  5986. }
  5987. /**
  5988. * ufshcd_handle_mcq_cq_events - handle MCQ completion queue events
  5989. * @hba: per adapter instance
  5990. *
  5991. * Return: IRQ_HANDLED if interrupt is handled.
  5992. */
  5993. static irqreturn_t ufshcd_handle_mcq_cq_events(struct ufs_hba *hba)
  5994. {
  5995. struct ufs_hw_queue *hwq;
  5996. unsigned long outstanding_cqs;
  5997. unsigned int nr_queues;
  5998. int i, ret;
  5999. u32 events;
  6000. ret = ufshcd_vops_get_outstanding_cqs(hba, &outstanding_cqs);
  6001. if (ret)
  6002. outstanding_cqs = (1U << hba->nr_hw_queues) - 1;
  6003. /* Exclude the poll queues */
  6004. nr_queues = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL];
  6005. for_each_set_bit(i, &outstanding_cqs, nr_queues) {
  6006. hwq = &hba->uhq[i];
  6007. events = ufshcd_mcq_read_cqis(hba, i);
  6008. if (events)
  6009. ufshcd_mcq_write_cqis(hba, events, i);
  6010. if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS)
  6011. ufshcd_mcq_poll_cqe_lock(hba, hwq);
  6012. }
  6013. return IRQ_HANDLED;
  6014. }
  6015. /**
  6016. * ufshcd_sl_intr - Interrupt service routine
  6017. * @hba: per adapter instance
  6018. * @intr_status: contains interrupts generated by the controller
  6019. *
  6020. * Return:
  6021. * IRQ_HANDLED - If interrupt is valid
  6022. * IRQ_NONE - If invalid interrupt
  6023. */
  6024. static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
  6025. {
  6026. irqreturn_t retval = IRQ_NONE;
  6027. if (intr_status & UFSHCD_UIC_MASK)
  6028. retval |= ufshcd_uic_cmd_compl(hba, intr_status);
  6029. if (intr_status & UFSHCD_ERROR_MASK || hba->errors)
  6030. retval |= ufshcd_check_errors(hba, intr_status);
  6031. if (intr_status & UTP_TASK_REQ_COMPL)
  6032. retval |= ufshcd_tmc_handler(hba);
  6033. if (intr_status & UTP_TRANSFER_REQ_COMPL)
  6034. retval |= ufshcd_transfer_req_compl(hba);
  6035. if (intr_status & MCQ_CQ_EVENT_STATUS)
  6036. retval |= ufshcd_handle_mcq_cq_events(hba);
  6037. return retval;
  6038. }
  6039. /**
  6040. * ufshcd_intr - Main interrupt service routine
  6041. * @irq: irq number
  6042. * @__hba: pointer to adapter instance
  6043. *
  6044. * Return:
  6045. * IRQ_HANDLED - If interrupt is valid
  6046. * IRQ_NONE - If invalid interrupt
  6047. */
  6048. static irqreturn_t ufshcd_intr(int irq, void *__hba)
  6049. {
  6050. u32 intr_status, enabled_intr_status = 0;
  6051. irqreturn_t retval = IRQ_NONE;
  6052. struct ufs_hba *hba = __hba;
  6053. int retries = hba->nutrs;
  6054. intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  6055. hba->ufs_stats.last_intr_status = intr_status;
  6056. hba->ufs_stats.last_intr_ts = local_clock();
  6057. /*
  6058. * There could be max of hba->nutrs reqs in flight and in worst case
  6059. * if the reqs get finished 1 by 1 after the interrupt status is
  6060. * read, make sure we handle them by checking the interrupt status
  6061. * again in a loop until we process all of the reqs before returning.
  6062. */
  6063. while (intr_status && retries--) {
  6064. enabled_intr_status =
  6065. intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  6066. ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
  6067. if (enabled_intr_status)
  6068. retval |= ufshcd_sl_intr(hba, enabled_intr_status);
  6069. intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
  6070. }
  6071. if (enabled_intr_status && retval == IRQ_NONE &&
  6072. (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL) ||
  6073. hba->outstanding_reqs) && !ufshcd_eh_in_progress(hba)) {
  6074. dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n",
  6075. __func__,
  6076. intr_status,
  6077. hba->ufs_stats.last_intr_status,
  6078. enabled_intr_status);
  6079. ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
  6080. }
  6081. return retval;
  6082. }
  6083. static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
  6084. {
  6085. int err = 0;
  6086. u32 mask = 1 << tag;
  6087. unsigned long flags;
  6088. if (!test_bit(tag, &hba->outstanding_tasks))
  6089. goto out;
  6090. spin_lock_irqsave(hba->host->host_lock, flags);
  6091. ufshcd_utmrl_clear(hba, tag);
  6092. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6093. /* poll for max. 1 sec to clear door bell register by h/w */
  6094. err = ufshcd_wait_for_register(hba,
  6095. REG_UTP_TASK_REQ_DOOR_BELL,
  6096. mask, 0, 1000, 1000);
  6097. dev_err(hba->dev, "Clearing task management function with tag %d %s\n",
  6098. tag, err < 0 ? "failed" : "succeeded");
  6099. out:
  6100. return err;
  6101. }
  6102. static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
  6103. struct utp_task_req_desc *treq, u8 tm_function)
  6104. {
  6105. struct request_queue *q = hba->tmf_queue;
  6106. struct Scsi_Host *host = hba->host;
  6107. DECLARE_COMPLETION_ONSTACK(wait);
  6108. struct request *req;
  6109. unsigned long flags;
  6110. int task_tag, err;
  6111. /*
  6112. * blk_mq_alloc_request() is used here only to get a free tag.
  6113. */
  6114. req = blk_mq_alloc_request(q, REQ_OP_DRV_OUT, 0);
  6115. if (IS_ERR(req))
  6116. return PTR_ERR(req);
  6117. req->end_io_data = &wait;
  6118. ufshcd_hold(hba);
  6119. spin_lock_irqsave(host->host_lock, flags);
  6120. task_tag = req->tag;
  6121. hba->tmf_rqs[req->tag] = req;
  6122. treq->upiu_req.req_header.task_tag = task_tag;
  6123. memcpy(hba->utmrdl_base_addr + task_tag, treq, sizeof(*treq));
  6124. ufshcd_vops_setup_task_mgmt(hba, task_tag, tm_function);
  6125. /* send command to the controller */
  6126. __set_bit(task_tag, &hba->outstanding_tasks);
  6127. ufshcd_writel(hba, 1 << task_tag, REG_UTP_TASK_REQ_DOOR_BELL);
  6128. spin_unlock_irqrestore(host->host_lock, flags);
  6129. ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND);
  6130. /* wait until the task management command is completed */
  6131. err = wait_for_completion_io_timeout(&wait,
  6132. msecs_to_jiffies(TM_CMD_TIMEOUT));
  6133. if (!err) {
  6134. ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
  6135. dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
  6136. __func__, tm_function);
  6137. if (ufshcd_clear_tm_cmd(hba, task_tag))
  6138. dev_WARN(hba->dev, "%s: unable to clear tm cmd (slot %d) after timeout\n",
  6139. __func__, task_tag);
  6140. err = -ETIMEDOUT;
  6141. } else {
  6142. err = 0;
  6143. memcpy(treq, hba->utmrdl_base_addr + task_tag, sizeof(*treq));
  6144. ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP);
  6145. }
  6146. spin_lock_irqsave(hba->host->host_lock, flags);
  6147. hba->tmf_rqs[req->tag] = NULL;
  6148. __clear_bit(task_tag, &hba->outstanding_tasks);
  6149. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6150. ufshcd_release(hba);
  6151. blk_mq_free_request(req);
  6152. return err;
  6153. }
  6154. /**
  6155. * ufshcd_issue_tm_cmd - issues task management commands to controller
  6156. * @hba: per adapter instance
  6157. * @lun_id: LUN ID to which TM command is sent
  6158. * @task_id: task ID to which the TM command is applicable
  6159. * @tm_function: task management function opcode
  6160. * @tm_response: task management service response return value
  6161. *
  6162. * Return: non-zero value on error, zero on success.
  6163. */
  6164. static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
  6165. u8 tm_function, u8 *tm_response)
  6166. {
  6167. struct utp_task_req_desc treq = { };
  6168. enum utp_ocs ocs_value;
  6169. int err;
  6170. /* Configure task request descriptor */
  6171. treq.header.interrupt = 1;
  6172. treq.header.ocs = OCS_INVALID_COMMAND_STATUS;
  6173. /* Configure task request UPIU */
  6174. treq.upiu_req.req_header.transaction_code = UPIU_TRANSACTION_TASK_REQ;
  6175. treq.upiu_req.req_header.lun = lun_id;
  6176. treq.upiu_req.req_header.tm_function = tm_function;
  6177. /*
  6178. * The host shall provide the same value for LUN field in the basic
  6179. * header and for Input Parameter.
  6180. */
  6181. treq.upiu_req.input_param1 = cpu_to_be32(lun_id);
  6182. treq.upiu_req.input_param2 = cpu_to_be32(task_id);
  6183. err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
  6184. if (err == -ETIMEDOUT)
  6185. return err;
  6186. ocs_value = treq.header.ocs & MASK_OCS;
  6187. if (ocs_value != OCS_SUCCESS)
  6188. dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
  6189. __func__, ocs_value);
  6190. else if (tm_response)
  6191. *tm_response = be32_to_cpu(treq.upiu_rsp.output_param1) &
  6192. MASK_TM_SERVICE_RESP;
  6193. return err;
  6194. }
  6195. /**
  6196. * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
  6197. * @hba: per-adapter instance
  6198. * @req_upiu: upiu request
  6199. * @rsp_upiu: upiu reply
  6200. * @desc_buff: pointer to descriptor buffer, NULL if NA
  6201. * @buff_len: descriptor size, 0 if NA
  6202. * @cmd_type: specifies the type (NOP, Query...)
  6203. * @desc_op: descriptor operation
  6204. *
  6205. * Those type of requests uses UTP Transfer Request Descriptor - utrd.
  6206. * Therefore, it "rides" the device management infrastructure: uses its tag and
  6207. * tasks work queues.
  6208. *
  6209. * Since there is only one available tag for device management commands,
  6210. * the caller is expected to hold the hba->dev_cmd.lock mutex.
  6211. *
  6212. * Return: 0 upon success; < 0 upon failure.
  6213. */
  6214. static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
  6215. struct utp_upiu_req *req_upiu,
  6216. struct utp_upiu_req *rsp_upiu,
  6217. u8 *desc_buff, int *buff_len,
  6218. enum dev_cmd_type cmd_type,
  6219. enum query_opcode desc_op)
  6220. {
  6221. const u32 tag = hba->reserved_slot;
  6222. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  6223. int err = 0;
  6224. u8 upiu_flags;
  6225. /* Protects use of hba->reserved_slot. */
  6226. lockdep_assert_held(&hba->dev_cmd.lock);
  6227. ufshcd_setup_dev_cmd(hba, lrbp, cmd_type, 0, tag);
  6228. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, 0);
  6229. /* update the task tag in the request upiu */
  6230. req_upiu->header.task_tag = tag;
  6231. /* just copy the upiu request as it is */
  6232. memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
  6233. if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
  6234. /* The Data Segment Area is optional depending upon the query
  6235. * function value. for WRITE DESCRIPTOR, the data segment
  6236. * follows right after the tsf.
  6237. */
  6238. memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
  6239. *buff_len = 0;
  6240. }
  6241. memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
  6242. /*
  6243. * ignore the returning value here - ufshcd_check_query_response is
  6244. * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
  6245. * read the response directly ignoring all errors.
  6246. */
  6247. ufshcd_issue_dev_cmd(hba, lrbp, tag, QUERY_REQ_TIMEOUT);
  6248. /* just copy the upiu response as it is */
  6249. memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
  6250. if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
  6251. u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
  6252. u16 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header
  6253. .data_segment_length);
  6254. if (*buff_len >= resp_len) {
  6255. memcpy(desc_buff, descp, resp_len);
  6256. *buff_len = resp_len;
  6257. } else {
  6258. dev_warn(hba->dev,
  6259. "%s: rsp size %d is bigger than buffer size %d",
  6260. __func__, resp_len, *buff_len);
  6261. *buff_len = 0;
  6262. err = -EINVAL;
  6263. }
  6264. }
  6265. return err;
  6266. }
  6267. /**
  6268. * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
  6269. * @hba: per-adapter instance
  6270. * @req_upiu: upiu request
  6271. * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
  6272. * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
  6273. * @desc_buff: pointer to descriptor buffer, NULL if NA
  6274. * @buff_len: descriptor size, 0 if NA
  6275. * @desc_op: descriptor operation
  6276. *
  6277. * Supports UTP Transfer requests (nop and query), and UTP Task
  6278. * Management requests.
  6279. * It is up to the caller to fill the upiu conent properly, as it will
  6280. * be copied without any further input validations.
  6281. *
  6282. * Return: 0 upon success; < 0 upon failure.
  6283. */
  6284. int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
  6285. struct utp_upiu_req *req_upiu,
  6286. struct utp_upiu_req *rsp_upiu,
  6287. enum upiu_request_transaction msgcode,
  6288. u8 *desc_buff, int *buff_len,
  6289. enum query_opcode desc_op)
  6290. {
  6291. int err;
  6292. enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
  6293. struct utp_task_req_desc treq = { };
  6294. enum utp_ocs ocs_value;
  6295. u8 tm_f = req_upiu->header.tm_function;
  6296. switch (msgcode) {
  6297. case UPIU_TRANSACTION_NOP_OUT:
  6298. cmd_type = DEV_CMD_TYPE_NOP;
  6299. fallthrough;
  6300. case UPIU_TRANSACTION_QUERY_REQ:
  6301. ufshcd_dev_man_lock(hba);
  6302. err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
  6303. desc_buff, buff_len,
  6304. cmd_type, desc_op);
  6305. ufshcd_dev_man_unlock(hba);
  6306. break;
  6307. case UPIU_TRANSACTION_TASK_REQ:
  6308. treq.header.interrupt = 1;
  6309. treq.header.ocs = OCS_INVALID_COMMAND_STATUS;
  6310. memcpy(&treq.upiu_req, req_upiu, sizeof(*req_upiu));
  6311. err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
  6312. if (err == -ETIMEDOUT)
  6313. break;
  6314. ocs_value = treq.header.ocs & MASK_OCS;
  6315. if (ocs_value != OCS_SUCCESS) {
  6316. dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
  6317. ocs_value);
  6318. break;
  6319. }
  6320. memcpy(rsp_upiu, &treq.upiu_rsp, sizeof(*rsp_upiu));
  6321. break;
  6322. default:
  6323. err = -EINVAL;
  6324. break;
  6325. }
  6326. return err;
  6327. }
  6328. /**
  6329. * ufshcd_advanced_rpmb_req_handler - handle advanced RPMB request
  6330. * @hba: per adapter instance
  6331. * @req_upiu: upiu request
  6332. * @rsp_upiu: upiu reply
  6333. * @req_ehs: EHS field which contains Advanced RPMB Request Message
  6334. * @rsp_ehs: EHS field which returns Advanced RPMB Response Message
  6335. * @sg_cnt: The number of sg lists actually used
  6336. * @sg_list: Pointer to SG list when DATA IN/OUT UPIU is required in ARPMB operation
  6337. * @dir: DMA direction
  6338. *
  6339. * Return: zero on success, non-zero on failure.
  6340. */
  6341. int ufshcd_advanced_rpmb_req_handler(struct ufs_hba *hba, struct utp_upiu_req *req_upiu,
  6342. struct utp_upiu_req *rsp_upiu, struct ufs_ehs *req_ehs,
  6343. struct ufs_ehs *rsp_ehs, int sg_cnt, struct scatterlist *sg_list,
  6344. enum dma_data_direction dir)
  6345. {
  6346. const u32 tag = hba->reserved_slot;
  6347. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  6348. int err = 0;
  6349. int result;
  6350. u8 upiu_flags;
  6351. u8 *ehs_data;
  6352. u16 ehs_len;
  6353. int ehs = (hba->capabilities & MASK_EHSLUTRD_SUPPORTED) ? 2 : 0;
  6354. /* Protects use of hba->reserved_slot. */
  6355. ufshcd_dev_man_lock(hba);
  6356. ufshcd_setup_dev_cmd(hba, lrbp, DEV_CMD_TYPE_RPMB, UFS_UPIU_RPMB_WLUN, tag);
  6357. ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, ehs);
  6358. /* update the task tag */
  6359. req_upiu->header.task_tag = tag;
  6360. /* copy the UPIU(contains CDB) request as it is */
  6361. memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
  6362. /* Copy EHS, starting with byte32, immediately after the CDB package */
  6363. memcpy(lrbp->ucd_req_ptr + 1, req_ehs, sizeof(*req_ehs));
  6364. if (dir != DMA_NONE && sg_list)
  6365. ufshcd_sgl_to_prdt(hba, lrbp, sg_cnt, sg_list);
  6366. memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
  6367. err = ufshcd_issue_dev_cmd(hba, lrbp, tag, ADVANCED_RPMB_REQ_TIMEOUT);
  6368. if (!err) {
  6369. /* Just copy the upiu response as it is */
  6370. memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
  6371. /* Get the response UPIU result */
  6372. result = (lrbp->ucd_rsp_ptr->header.response << 8) |
  6373. lrbp->ucd_rsp_ptr->header.status;
  6374. ehs_len = lrbp->ucd_rsp_ptr->header.ehs_length;
  6375. /*
  6376. * Since the bLength in EHS indicates the total size of the EHS Header and EHS Data
  6377. * in 32 Byte units, the value of the bLength Request/Response for Advanced RPMB
  6378. * Message is 02h
  6379. */
  6380. if (ehs_len == 2 && rsp_ehs) {
  6381. /*
  6382. * ucd_rsp_ptr points to a buffer with a length of 512 bytes
  6383. * (ALIGNED_UPIU_SIZE = 512), and the EHS data just starts from byte32
  6384. */
  6385. ehs_data = (u8 *)lrbp->ucd_rsp_ptr + EHS_OFFSET_IN_RESPONSE;
  6386. memcpy(rsp_ehs, ehs_data, ehs_len * 32);
  6387. }
  6388. }
  6389. ufshcd_dev_man_unlock(hba);
  6390. return err ? : result;
  6391. }
  6392. /**
  6393. * ufshcd_eh_device_reset_handler() - Reset a single logical unit.
  6394. * @cmd: SCSI command pointer
  6395. *
  6396. * Return: SUCCESS or FAILED.
  6397. */
  6398. static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
  6399. {
  6400. unsigned long flags, pending_reqs = 0, not_cleared = 0;
  6401. struct Scsi_Host *host;
  6402. struct ufs_hba *hba;
  6403. struct ufs_hw_queue *hwq;
  6404. struct ufshcd_lrb *lrbp;
  6405. u32 pos, not_cleared_mask = 0;
  6406. int err;
  6407. u8 resp = 0xF, lun;
  6408. host = cmd->device->host;
  6409. hba = shost_priv(host);
  6410. lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
  6411. err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp);
  6412. if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
  6413. if (!err)
  6414. err = resp;
  6415. goto out;
  6416. }
  6417. if (hba->mcq_enabled) {
  6418. for (pos = 0; pos < hba->nutrs; pos++) {
  6419. lrbp = &hba->lrb[pos];
  6420. if (ufshcd_cmd_inflight(lrbp->cmd) &&
  6421. lrbp->lun == lun) {
  6422. ufshcd_clear_cmd(hba, pos);
  6423. hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd));
  6424. ufshcd_mcq_poll_cqe_lock(hba, hwq);
  6425. }
  6426. }
  6427. err = 0;
  6428. goto out;
  6429. }
  6430. /* clear the commands that were pending for corresponding LUN */
  6431. spin_lock_irqsave(&hba->outstanding_lock, flags);
  6432. for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs)
  6433. if (hba->lrb[pos].lun == lun)
  6434. __set_bit(pos, &pending_reqs);
  6435. hba->outstanding_reqs &= ~pending_reqs;
  6436. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  6437. for_each_set_bit(pos, &pending_reqs, hba->nutrs) {
  6438. if (ufshcd_clear_cmd(hba, pos) < 0) {
  6439. spin_lock_irqsave(&hba->outstanding_lock, flags);
  6440. not_cleared = 1U << pos &
  6441. ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  6442. hba->outstanding_reqs |= not_cleared;
  6443. not_cleared_mask |= not_cleared;
  6444. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  6445. dev_err(hba->dev, "%s: failed to clear request %d\n",
  6446. __func__, pos);
  6447. }
  6448. }
  6449. __ufshcd_transfer_req_compl(hba, pending_reqs & ~not_cleared_mask);
  6450. out:
  6451. hba->req_abort_count = 0;
  6452. ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err);
  6453. if (!err) {
  6454. err = SUCCESS;
  6455. } else {
  6456. dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
  6457. err = FAILED;
  6458. }
  6459. return err;
  6460. }
  6461. static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
  6462. {
  6463. struct ufshcd_lrb *lrbp;
  6464. int tag;
  6465. for_each_set_bit(tag, &bitmap, hba->nutrs) {
  6466. lrbp = &hba->lrb[tag];
  6467. lrbp->req_abort_skip = true;
  6468. }
  6469. }
  6470. /**
  6471. * ufshcd_try_to_abort_task - abort a specific task
  6472. * @hba: Pointer to adapter instance
  6473. * @tag: Task tag/index to be aborted
  6474. *
  6475. * Abort the pending command in device by sending UFS_ABORT_TASK task management
  6476. * command, and in host controller by clearing the door-bell register. There can
  6477. * be race between controller sending the command to the device while abort is
  6478. * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
  6479. * really issued and then try to abort it.
  6480. *
  6481. * Return: zero on success, non-zero on failure.
  6482. */
  6483. int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
  6484. {
  6485. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  6486. int err = 0;
  6487. int poll_cnt;
  6488. u8 resp = 0xF;
  6489. u32 reg;
  6490. for (poll_cnt = 100; poll_cnt; poll_cnt--) {
  6491. err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
  6492. UFS_QUERY_TASK, &resp);
  6493. if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
  6494. /* cmd pending in the device */
  6495. dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
  6496. __func__, tag);
  6497. break;
  6498. } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
  6499. /*
  6500. * cmd not pending in the device, check if it is
  6501. * in transition.
  6502. */
  6503. dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
  6504. __func__, tag);
  6505. if (hba->mcq_enabled) {
  6506. /* MCQ mode */
  6507. if (ufshcd_cmd_inflight(lrbp->cmd)) {
  6508. /* sleep for max. 200us same delay as in SDB mode */
  6509. usleep_range(100, 200);
  6510. continue;
  6511. }
  6512. /* command completed already */
  6513. dev_err(hba->dev, "%s: cmd at tag=%d is cleared.\n",
  6514. __func__, tag);
  6515. goto out;
  6516. }
  6517. /* Single Doorbell Mode */
  6518. reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  6519. if (reg & (1 << tag)) {
  6520. /* sleep for max. 200us to stabilize */
  6521. usleep_range(100, 200);
  6522. continue;
  6523. }
  6524. /* command completed already */
  6525. dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
  6526. __func__, tag);
  6527. goto out;
  6528. } else {
  6529. dev_err(hba->dev,
  6530. "%s: no response from device. tag = %d, err %d\n",
  6531. __func__, tag, err);
  6532. if (!err)
  6533. err = resp; /* service response error */
  6534. goto out;
  6535. }
  6536. }
  6537. if (!poll_cnt) {
  6538. err = -EBUSY;
  6539. goto out;
  6540. }
  6541. err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
  6542. UFS_ABORT_TASK, &resp);
  6543. if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
  6544. if (!err) {
  6545. err = resp; /* service response error */
  6546. dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
  6547. __func__, tag, err);
  6548. }
  6549. goto out;
  6550. }
  6551. err = ufshcd_clear_cmd(hba, tag);
  6552. if (err)
  6553. dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
  6554. __func__, tag, err);
  6555. out:
  6556. return err;
  6557. }
  6558. /**
  6559. * ufshcd_abort - scsi host template eh_abort_handler callback
  6560. * @cmd: SCSI command pointer
  6561. *
  6562. * Return: SUCCESS or FAILED.
  6563. */
  6564. static int ufshcd_abort(struct scsi_cmnd *cmd)
  6565. {
  6566. struct Scsi_Host *host = cmd->device->host;
  6567. struct ufs_hba *hba = shost_priv(host);
  6568. int tag = scsi_cmd_to_rq(cmd)->tag;
  6569. struct ufshcd_lrb *lrbp = &hba->lrb[tag];
  6570. unsigned long flags;
  6571. int err = FAILED;
  6572. bool outstanding;
  6573. u32 reg;
  6574. ufshcd_hold(hba);
  6575. if (!hba->mcq_enabled) {
  6576. reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
  6577. if (!test_bit(tag, &hba->outstanding_reqs)) {
  6578. /* If command is already aborted/completed, return FAILED. */
  6579. dev_err(hba->dev,
  6580. "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
  6581. __func__, tag, hba->outstanding_reqs, reg);
  6582. goto release;
  6583. }
  6584. }
  6585. /* Print Transfer Request of aborted task */
  6586. dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
  6587. /*
  6588. * Print detailed info about aborted request.
  6589. * As more than one request might get aborted at the same time,
  6590. * print full information only for the first aborted request in order
  6591. * to reduce repeated printouts. For other aborted requests only print
  6592. * basic details.
  6593. */
  6594. scsi_print_command(cmd);
  6595. if (!hba->req_abort_count) {
  6596. ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
  6597. ufshcd_print_evt_hist(hba);
  6598. ufshcd_print_host_state(hba);
  6599. ufshcd_print_pwr_info(hba);
  6600. ufshcd_print_tr(hba, tag, true);
  6601. } else {
  6602. ufshcd_print_tr(hba, tag, false);
  6603. }
  6604. hba->req_abort_count++;
  6605. if (!hba->mcq_enabled && !(reg & (1 << tag))) {
  6606. /* only execute this code in single doorbell mode */
  6607. dev_err(hba->dev,
  6608. "%s: cmd was completed, but without a notifying intr, tag = %d",
  6609. __func__, tag);
  6610. __ufshcd_transfer_req_compl(hba, 1UL << tag);
  6611. goto release;
  6612. }
  6613. /*
  6614. * Task abort to the device W-LUN is illegal. When this command
  6615. * will fail, due to spec violation, scsi err handling next step
  6616. * will be to send LU reset which, again, is a spec violation.
  6617. * To avoid these unnecessary/illegal steps, first we clean up
  6618. * the lrb taken by this cmd and re-set it in outstanding_reqs,
  6619. * then queue the eh_work and bail.
  6620. */
  6621. if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
  6622. ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
  6623. spin_lock_irqsave(host->host_lock, flags);
  6624. hba->force_reset = true;
  6625. ufshcd_schedule_eh_work(hba);
  6626. spin_unlock_irqrestore(host->host_lock, flags);
  6627. goto release;
  6628. }
  6629. if (hba->mcq_enabled) {
  6630. /* MCQ mode. Branch off to handle abort for mcq mode */
  6631. err = ufshcd_mcq_abort(cmd);
  6632. goto release;
  6633. }
  6634. /* Skip task abort in case previous aborts failed and report failure */
  6635. if (lrbp->req_abort_skip) {
  6636. dev_err(hba->dev, "%s: skipping abort\n", __func__);
  6637. ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
  6638. goto release;
  6639. }
  6640. err = ufshcd_try_to_abort_task(hba, tag);
  6641. if (err) {
  6642. dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
  6643. ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
  6644. err = FAILED;
  6645. goto release;
  6646. }
  6647. /*
  6648. * Clear the corresponding bit from outstanding_reqs since the command
  6649. * has been aborted successfully.
  6650. */
  6651. spin_lock_irqsave(&hba->outstanding_lock, flags);
  6652. outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs);
  6653. spin_unlock_irqrestore(&hba->outstanding_lock, flags);
  6654. if (outstanding)
  6655. ufshcd_release_scsi_cmd(hba, lrbp);
  6656. err = SUCCESS;
  6657. release:
  6658. /* Matches the ufshcd_hold() call at the start of this function. */
  6659. ufshcd_release(hba);
  6660. return err;
  6661. }
  6662. /**
  6663. * ufshcd_host_reset_and_restore - reset and restore host controller
  6664. * @hba: per-adapter instance
  6665. *
  6666. * Note that host controller reset may issue DME_RESET to
  6667. * local and remote (device) Uni-Pro stack and the attributes
  6668. * are reset to default state.
  6669. *
  6670. * Return: zero on success, non-zero on failure.
  6671. */
  6672. static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
  6673. {
  6674. int err;
  6675. /*
  6676. * Stop the host controller and complete the requests
  6677. * cleared by h/w
  6678. */
  6679. ufshcd_hba_stop(hba);
  6680. hba->silence_err_logs = true;
  6681. ufshcd_complete_requests(hba, true);
  6682. hba->silence_err_logs = false;
  6683. /* scale up clocks to max frequency before full reinitialization */
  6684. if (ufshcd_is_clkscaling_supported(hba))
  6685. ufshcd_scale_clks(hba, ULONG_MAX, true);
  6686. err = ufshcd_hba_enable(hba);
  6687. /* Establish the link again and restore the device */
  6688. if (!err)
  6689. err = ufshcd_probe_hba(hba, false);
  6690. if (err)
  6691. dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
  6692. ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err);
  6693. return err;
  6694. }
  6695. /**
  6696. * ufshcd_reset_and_restore - reset and re-initialize host/device
  6697. * @hba: per-adapter instance
  6698. *
  6699. * Reset and recover device, host and re-establish link. This
  6700. * is helpful to recover the communication in fatal error conditions.
  6701. *
  6702. * Return: zero on success, non-zero on failure.
  6703. */
  6704. static int ufshcd_reset_and_restore(struct ufs_hba *hba)
  6705. {
  6706. u32 saved_err = 0;
  6707. u32 saved_uic_err = 0;
  6708. int err = 0;
  6709. unsigned long flags;
  6710. int retries = MAX_HOST_RESET_RETRIES;
  6711. spin_lock_irqsave(hba->host->host_lock, flags);
  6712. do {
  6713. /*
  6714. * This is a fresh start, cache and clear saved error first,
  6715. * in case new error generated during reset and restore.
  6716. */
  6717. saved_err |= hba->saved_err;
  6718. saved_uic_err |= hba->saved_uic_err;
  6719. hba->saved_err = 0;
  6720. hba->saved_uic_err = 0;
  6721. hba->force_reset = false;
  6722. hba->ufshcd_state = UFSHCD_STATE_RESET;
  6723. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6724. /* Reset the attached device */
  6725. ufshcd_device_reset(hba);
  6726. err = ufshcd_host_reset_and_restore(hba);
  6727. spin_lock_irqsave(hba->host->host_lock, flags);
  6728. if (err)
  6729. continue;
  6730. /* Do not exit unless operational or dead */
  6731. if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
  6732. hba->ufshcd_state != UFSHCD_STATE_ERROR &&
  6733. hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL)
  6734. err = -EAGAIN;
  6735. } while (err && --retries);
  6736. /*
  6737. * Inform scsi mid-layer that we did reset and allow to handle
  6738. * Unit Attention properly.
  6739. */
  6740. scsi_report_bus_reset(hba->host, 0);
  6741. if (err) {
  6742. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  6743. hba->saved_err |= saved_err;
  6744. hba->saved_uic_err |= saved_uic_err;
  6745. }
  6746. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6747. return err;
  6748. }
  6749. /**
  6750. * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
  6751. * @cmd: SCSI command pointer
  6752. *
  6753. * Return: SUCCESS or FAILED.
  6754. */
  6755. static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
  6756. {
  6757. int err = SUCCESS;
  6758. unsigned long flags;
  6759. struct ufs_hba *hba;
  6760. hba = shost_priv(cmd->device->host);
  6761. /*
  6762. * If runtime PM sent SSU and got a timeout, scsi_error_handler is
  6763. * stuck in this function waiting for flush_work(&hba->eh_work). And
  6764. * ufshcd_err_handler(eh_work) is stuck waiting for runtime PM. Do
  6765. * ufshcd_link_recovery instead of eh_work to prevent deadlock.
  6766. */
  6767. if (hba->pm_op_in_progress) {
  6768. if (ufshcd_link_recovery(hba))
  6769. err = FAILED;
  6770. return err;
  6771. }
  6772. spin_lock_irqsave(hba->host->host_lock, flags);
  6773. hba->force_reset = true;
  6774. ufshcd_schedule_eh_work(hba);
  6775. dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
  6776. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6777. flush_work(&hba->eh_work);
  6778. spin_lock_irqsave(hba->host->host_lock, flags);
  6779. if (hba->ufshcd_state == UFSHCD_STATE_ERROR)
  6780. err = FAILED;
  6781. spin_unlock_irqrestore(hba->host->host_lock, flags);
  6782. return err;
  6783. }
  6784. /**
  6785. * ufshcd_get_max_icc_level - calculate the ICC level
  6786. * @sup_curr_uA: max. current supported by the regulator
  6787. * @start_scan: row at the desc table to start scan from
  6788. * @buff: power descriptor buffer
  6789. *
  6790. * Return: calculated max ICC level for specific regulator.
  6791. */
  6792. static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan,
  6793. const char *buff)
  6794. {
  6795. int i;
  6796. int curr_uA;
  6797. u16 data;
  6798. u16 unit;
  6799. for (i = start_scan; i >= 0; i--) {
  6800. data = get_unaligned_be16(&buff[2 * i]);
  6801. unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
  6802. ATTR_ICC_LVL_UNIT_OFFSET;
  6803. curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
  6804. switch (unit) {
  6805. case UFSHCD_NANO_AMP:
  6806. curr_uA = curr_uA / 1000;
  6807. break;
  6808. case UFSHCD_MILI_AMP:
  6809. curr_uA = curr_uA * 1000;
  6810. break;
  6811. case UFSHCD_AMP:
  6812. curr_uA = curr_uA * 1000 * 1000;
  6813. break;
  6814. case UFSHCD_MICRO_AMP:
  6815. default:
  6816. break;
  6817. }
  6818. if (sup_curr_uA >= curr_uA)
  6819. break;
  6820. }
  6821. if (i < 0) {
  6822. i = 0;
  6823. pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
  6824. }
  6825. return (u32)i;
  6826. }
  6827. /**
  6828. * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level
  6829. * In case regulators are not initialized we'll return 0
  6830. * @hba: per-adapter instance
  6831. * @desc_buf: power descriptor buffer to extract ICC levels from.
  6832. *
  6833. * Return: calculated ICC level.
  6834. */
  6835. static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
  6836. const u8 *desc_buf)
  6837. {
  6838. u32 icc_level = 0;
  6839. if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
  6840. !hba->vreg_info.vccq2) {
  6841. /*
  6842. * Using dev_dbg to avoid messages during runtime PM to avoid
  6843. * never-ending cycles of messages written back to storage by
  6844. * user space causing runtime resume, causing more messages and
  6845. * so on.
  6846. */
  6847. dev_dbg(hba->dev,
  6848. "%s: Regulator capability was not set, actvIccLevel=%d",
  6849. __func__, icc_level);
  6850. goto out;
  6851. }
  6852. if (hba->vreg_info.vcc->max_uA)
  6853. icc_level = ufshcd_get_max_icc_level(
  6854. hba->vreg_info.vcc->max_uA,
  6855. POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
  6856. &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
  6857. if (hba->vreg_info.vccq->max_uA)
  6858. icc_level = ufshcd_get_max_icc_level(
  6859. hba->vreg_info.vccq->max_uA,
  6860. icc_level,
  6861. &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
  6862. if (hba->vreg_info.vccq2->max_uA)
  6863. icc_level = ufshcd_get_max_icc_level(
  6864. hba->vreg_info.vccq2->max_uA,
  6865. icc_level,
  6866. &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
  6867. out:
  6868. return icc_level;
  6869. }
  6870. static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
  6871. {
  6872. int ret;
  6873. u8 *desc_buf;
  6874. u32 icc_level;
  6875. desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
  6876. if (!desc_buf)
  6877. return;
  6878. ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
  6879. desc_buf, QUERY_DESC_MAX_SIZE);
  6880. if (ret) {
  6881. dev_err(hba->dev,
  6882. "%s: Failed reading power descriptor ret = %d",
  6883. __func__, ret);
  6884. goto out;
  6885. }
  6886. icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf);
  6887. dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
  6888. ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  6889. QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
  6890. if (ret)
  6891. dev_err(hba->dev,
  6892. "%s: Failed configuring bActiveICCLevel = %d ret = %d",
  6893. __func__, icc_level, ret);
  6894. out:
  6895. kfree(desc_buf);
  6896. }
  6897. static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
  6898. {
  6899. struct Scsi_Host *shost = sdev->host;
  6900. scsi_autopm_get_device(sdev);
  6901. blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
  6902. if (sdev->rpm_autosuspend)
  6903. pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
  6904. shost->rpm_autosuspend_delay);
  6905. scsi_autopm_put_device(sdev);
  6906. }
  6907. /**
  6908. * ufshcd_scsi_add_wlus - Adds required W-LUs
  6909. * @hba: per-adapter instance
  6910. *
  6911. * UFS device specification requires the UFS devices to support 4 well known
  6912. * logical units:
  6913. * "REPORT_LUNS" (address: 01h)
  6914. * "UFS Device" (address: 50h)
  6915. * "RPMB" (address: 44h)
  6916. * "BOOT" (address: 30h)
  6917. * UFS device's power management needs to be controlled by "POWER CONDITION"
  6918. * field of SSU (START STOP UNIT) command. But this "power condition" field
  6919. * will take effect only when its sent to "UFS device" well known logical unit
  6920. * hence we require the scsi_device instance to represent this logical unit in
  6921. * order for the UFS host driver to send the SSU command for power management.
  6922. *
  6923. * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
  6924. * Block) LU so user space process can control this LU. User space may also
  6925. * want to have access to BOOT LU.
  6926. *
  6927. * This function adds scsi device instances for each of all well known LUs
  6928. * (except "REPORT LUNS" LU).
  6929. *
  6930. * Return: zero on success (all required W-LUs are added successfully),
  6931. * non-zero error value on failure (if failed to add any of the required W-LU).
  6932. */
  6933. static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
  6934. {
  6935. int ret = 0;
  6936. struct scsi_device *sdev_boot, *sdev_rpmb;
  6937. hba->ufs_device_wlun = __scsi_add_device(hba->host, 0, 0,
  6938. ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
  6939. if (IS_ERR(hba->ufs_device_wlun)) {
  6940. ret = PTR_ERR(hba->ufs_device_wlun);
  6941. hba->ufs_device_wlun = NULL;
  6942. goto out;
  6943. }
  6944. scsi_device_put(hba->ufs_device_wlun);
  6945. sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
  6946. ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
  6947. if (IS_ERR(sdev_rpmb)) {
  6948. ret = PTR_ERR(sdev_rpmb);
  6949. goto remove_ufs_device_wlun;
  6950. }
  6951. ufshcd_blk_pm_runtime_init(sdev_rpmb);
  6952. scsi_device_put(sdev_rpmb);
  6953. sdev_boot = __scsi_add_device(hba->host, 0, 0,
  6954. ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
  6955. if (IS_ERR(sdev_boot)) {
  6956. dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
  6957. } else {
  6958. ufshcd_blk_pm_runtime_init(sdev_boot);
  6959. scsi_device_put(sdev_boot);
  6960. }
  6961. goto out;
  6962. remove_ufs_device_wlun:
  6963. scsi_remove_device(hba->ufs_device_wlun);
  6964. out:
  6965. return ret;
  6966. }
  6967. static void ufshcd_wb_probe(struct ufs_hba *hba, const u8 *desc_buf)
  6968. {
  6969. struct ufs_dev_info *dev_info = &hba->dev_info;
  6970. u8 lun;
  6971. u32 d_lu_wb_buf_alloc;
  6972. u32 ext_ufs_feature;
  6973. if (!ufshcd_is_wb_allowed(hba))
  6974. return;
  6975. /*
  6976. * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
  6977. * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
  6978. * enabled
  6979. */
  6980. if (!(dev_info->wspecversion >= 0x310 ||
  6981. dev_info->wspecversion == 0x220 ||
  6982. (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
  6983. goto wb_disabled;
  6984. ext_ufs_feature = get_unaligned_be32(desc_buf +
  6985. DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
  6986. if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP))
  6987. goto wb_disabled;
  6988. /*
  6989. * WB may be supported but not configured while provisioning. The spec
  6990. * says, in dedicated wb buffer mode, a max of 1 lun would have wb
  6991. * buffer configured.
  6992. */
  6993. dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
  6994. dev_info->b_presrv_uspc_en =
  6995. desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
  6996. if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) {
  6997. if (!get_unaligned_be32(desc_buf +
  6998. DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS))
  6999. goto wb_disabled;
  7000. } else {
  7001. for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
  7002. d_lu_wb_buf_alloc = 0;
  7003. ufshcd_read_unit_desc_param(hba,
  7004. lun,
  7005. UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
  7006. (u8 *)&d_lu_wb_buf_alloc,
  7007. sizeof(d_lu_wb_buf_alloc));
  7008. if (d_lu_wb_buf_alloc) {
  7009. dev_info->wb_dedicated_lu = lun;
  7010. break;
  7011. }
  7012. }
  7013. if (!d_lu_wb_buf_alloc)
  7014. goto wb_disabled;
  7015. }
  7016. if (!ufshcd_is_wb_buf_lifetime_available(hba))
  7017. goto wb_disabled;
  7018. return;
  7019. wb_disabled:
  7020. hba->caps &= ~UFSHCD_CAP_WB_EN;
  7021. }
  7022. static void ufshcd_temp_notif_probe(struct ufs_hba *hba, const u8 *desc_buf)
  7023. {
  7024. struct ufs_dev_info *dev_info = &hba->dev_info;
  7025. u32 ext_ufs_feature;
  7026. u8 mask = 0;
  7027. if (!(hba->caps & UFSHCD_CAP_TEMP_NOTIF) || dev_info->wspecversion < 0x300)
  7028. return;
  7029. ext_ufs_feature = get_unaligned_be32(desc_buf + DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
  7030. if (ext_ufs_feature & UFS_DEV_LOW_TEMP_NOTIF)
  7031. mask |= MASK_EE_TOO_LOW_TEMP;
  7032. if (ext_ufs_feature & UFS_DEV_HIGH_TEMP_NOTIF)
  7033. mask |= MASK_EE_TOO_HIGH_TEMP;
  7034. if (mask) {
  7035. ufshcd_enable_ee(hba, mask);
  7036. ufs_hwmon_probe(hba, mask);
  7037. }
  7038. }
  7039. static void ufshcd_ext_iid_probe(struct ufs_hba *hba, u8 *desc_buf)
  7040. {
  7041. struct ufs_dev_info *dev_info = &hba->dev_info;
  7042. u32 ext_ufs_feature;
  7043. u32 ext_iid_en = 0;
  7044. int err;
  7045. /* Only UFS-4.0 and above may support EXT_IID */
  7046. if (dev_info->wspecversion < 0x400)
  7047. goto out;
  7048. ext_ufs_feature = get_unaligned_be32(desc_buf +
  7049. DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
  7050. if (!(ext_ufs_feature & UFS_DEV_EXT_IID_SUP))
  7051. goto out;
  7052. err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  7053. QUERY_ATTR_IDN_EXT_IID_EN, 0, 0, &ext_iid_en);
  7054. if (err)
  7055. dev_err(hba->dev, "failed reading bEXTIIDEn. err = %d\n", err);
  7056. out:
  7057. dev_info->b_ext_iid_en = ext_iid_en;
  7058. }
  7059. static void ufshcd_set_rtt(struct ufs_hba *hba)
  7060. {
  7061. struct ufs_dev_info *dev_info = &hba->dev_info;
  7062. u32 rtt = 0;
  7063. u32 dev_rtt = 0;
  7064. int host_rtt_cap = hba->vops && hba->vops->max_num_rtt ?
  7065. hba->vops->max_num_rtt : hba->nortt;
  7066. /* RTT override makes sense only for UFS-4.0 and above */
  7067. if (dev_info->wspecversion < 0x400)
  7068. return;
  7069. if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  7070. QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &dev_rtt)) {
  7071. dev_err(hba->dev, "failed reading bMaxNumOfRTT\n");
  7072. return;
  7073. }
  7074. /* do not override if it was already written */
  7075. if (dev_rtt != DEFAULT_MAX_NUM_RTT)
  7076. return;
  7077. rtt = min_t(int, dev_info->rtt_cap, host_rtt_cap);
  7078. if (rtt == dev_rtt)
  7079. return;
  7080. if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  7081. QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt))
  7082. dev_err(hba->dev, "failed writing bMaxNumOfRTT\n");
  7083. }
  7084. void ufshcd_fixup_dev_quirks(struct ufs_hba *hba,
  7085. const struct ufs_dev_quirk *fixups)
  7086. {
  7087. const struct ufs_dev_quirk *f;
  7088. struct ufs_dev_info *dev_info = &hba->dev_info;
  7089. if (!fixups)
  7090. return;
  7091. for (f = fixups; f->quirk; f++) {
  7092. if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
  7093. f->wmanufacturerid == UFS_ANY_VENDOR) &&
  7094. ((dev_info->model &&
  7095. STR_PRFX_EQUAL(f->model, dev_info->model)) ||
  7096. !strcmp(f->model, UFS_ANY_MODEL)))
  7097. hba->dev_quirks |= f->quirk;
  7098. }
  7099. }
  7100. EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
  7101. static void ufs_fixup_device_setup(struct ufs_hba *hba)
  7102. {
  7103. /* fix by general quirk table */
  7104. ufshcd_fixup_dev_quirks(hba, ufs_fixups);
  7105. /* allow vendors to fix quirks */
  7106. ufshcd_vops_fixup_dev_quirks(hba);
  7107. }
  7108. static void ufshcd_update_rtc(struct ufs_hba *hba)
  7109. {
  7110. struct timespec64 ts64;
  7111. int err;
  7112. u32 val;
  7113. ktime_get_real_ts64(&ts64);
  7114. if (ts64.tv_sec < hba->dev_info.rtc_time_baseline) {
  7115. dev_warn_once(hba->dev, "%s: Current time precedes previous setting!\n", __func__);
  7116. return;
  7117. }
  7118. /*
  7119. * The Absolute RTC mode has a 136-year limit, spanning from 2010 to 2146. If a time beyond
  7120. * 2146 is required, it is recommended to choose the relative RTC mode.
  7121. */
  7122. val = ts64.tv_sec - hba->dev_info.rtc_time_baseline;
  7123. /* Skip update RTC if RPM state is not RPM_ACTIVE */
  7124. if (ufshcd_rpm_get_if_active(hba) <= 0)
  7125. return;
  7126. err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, QUERY_ATTR_IDN_SECONDS_PASSED,
  7127. 0, 0, &val);
  7128. ufshcd_rpm_put(hba);
  7129. if (err)
  7130. dev_err(hba->dev, "%s: Failed to update rtc %d\n", __func__, err);
  7131. else if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE)
  7132. hba->dev_info.rtc_time_baseline = ts64.tv_sec;
  7133. }
  7134. static void ufshcd_rtc_work(struct work_struct *work)
  7135. {
  7136. struct ufs_hba *hba;
  7137. hba = container_of(to_delayed_work(work), struct ufs_hba, ufs_rtc_update_work);
  7138. /* Update RTC only when there are no requests in progress and UFSHCI is operational */
  7139. if (!ufshcd_is_ufs_dev_busy(hba) &&
  7140. hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL &&
  7141. !hba->clk_gating.active_reqs)
  7142. ufshcd_update_rtc(hba);
  7143. if (ufshcd_is_ufs_dev_active(hba) && hba->dev_info.rtc_update_period)
  7144. schedule_delayed_work(&hba->ufs_rtc_update_work,
  7145. msecs_to_jiffies(hba->dev_info.rtc_update_period));
  7146. }
  7147. static void ufs_init_rtc(struct ufs_hba *hba, u8 *desc_buf)
  7148. {
  7149. u16 periodic_rtc_update = get_unaligned_be16(&desc_buf[DEVICE_DESC_PARAM_FRQ_RTC]);
  7150. struct ufs_dev_info *dev_info = &hba->dev_info;
  7151. if (periodic_rtc_update & UFS_RTC_TIME_BASELINE) {
  7152. dev_info->rtc_type = UFS_RTC_ABSOLUTE;
  7153. /*
  7154. * The concept of measuring time in Linux as the number of seconds elapsed since
  7155. * 00:00:00 UTC on January 1, 1970, and UFS ABS RTC is elapsed from January 1st
  7156. * 2010 00:00, here we need to adjust ABS baseline.
  7157. */
  7158. dev_info->rtc_time_baseline = mktime64(2010, 1, 1, 0, 0, 0) -
  7159. mktime64(1970, 1, 1, 0, 0, 0);
  7160. } else {
  7161. dev_info->rtc_type = UFS_RTC_RELATIVE;
  7162. dev_info->rtc_time_baseline = 0;
  7163. }
  7164. /*
  7165. * We ignore TIME_PERIOD defined in wPeriodicRTCUpdate because Spec does not clearly state
  7166. * how to calculate the specific update period for each time unit. And we disable periodic
  7167. * RTC update work, let user configure by sysfs node according to specific circumstance.
  7168. */
  7169. dev_info->rtc_update_period = 0;
  7170. }
  7171. static int ufs_get_device_desc(struct ufs_hba *hba)
  7172. {
  7173. int err;
  7174. u8 model_index;
  7175. u8 *desc_buf;
  7176. struct ufs_dev_info *dev_info = &hba->dev_info;
  7177. desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
  7178. if (!desc_buf) {
  7179. err = -ENOMEM;
  7180. goto out;
  7181. }
  7182. err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
  7183. QUERY_DESC_MAX_SIZE);
  7184. if (err) {
  7185. dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
  7186. __func__, err);
  7187. goto out;
  7188. }
  7189. /*
  7190. * getting vendor (manufacturerID) and Bank Index in big endian
  7191. * format
  7192. */
  7193. dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
  7194. desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
  7195. /* getting Specification Version in big endian format */
  7196. dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
  7197. desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
  7198. dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH];
  7199. dev_info->rtt_cap = desc_buf[DEVICE_DESC_PARAM_RTT_CAP];
  7200. model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
  7201. err = ufshcd_read_string_desc(hba, model_index,
  7202. &dev_info->model, SD_ASCII_STD);
  7203. if (err < 0) {
  7204. dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
  7205. __func__, err);
  7206. goto out;
  7207. }
  7208. hba->luns_avail = desc_buf[DEVICE_DESC_PARAM_NUM_LU] +
  7209. desc_buf[DEVICE_DESC_PARAM_NUM_WLU];
  7210. ufs_fixup_device_setup(hba);
  7211. ufshcd_wb_probe(hba, desc_buf);
  7212. ufshcd_temp_notif_probe(hba, desc_buf);
  7213. ufs_init_rtc(hba, desc_buf);
  7214. if (hba->ext_iid_sup)
  7215. ufshcd_ext_iid_probe(hba, desc_buf);
  7216. /*
  7217. * ufshcd_read_string_desc returns size of the string
  7218. * reset the error value
  7219. */
  7220. err = 0;
  7221. out:
  7222. kfree(desc_buf);
  7223. return err;
  7224. }
  7225. static void ufs_put_device_desc(struct ufs_hba *hba)
  7226. {
  7227. struct ufs_dev_info *dev_info = &hba->dev_info;
  7228. kfree(dev_info->model);
  7229. dev_info->model = NULL;
  7230. }
  7231. /**
  7232. * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
  7233. * less than device PA_TACTIVATE time.
  7234. * @hba: per-adapter instance
  7235. *
  7236. * Some UFS devices require host PA_TACTIVATE to be lower than device
  7237. * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
  7238. * for such devices.
  7239. *
  7240. * Return: zero on success, non-zero error value on failure.
  7241. */
  7242. static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
  7243. {
  7244. int ret = 0;
  7245. u32 granularity, peer_granularity;
  7246. u32 pa_tactivate, peer_pa_tactivate;
  7247. u32 pa_tactivate_us, peer_pa_tactivate_us;
  7248. static const u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
  7249. ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
  7250. &granularity);
  7251. if (ret)
  7252. goto out;
  7253. ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
  7254. &peer_granularity);
  7255. if (ret)
  7256. goto out;
  7257. if ((granularity < PA_GRANULARITY_MIN_VAL) ||
  7258. (granularity > PA_GRANULARITY_MAX_VAL)) {
  7259. dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
  7260. __func__, granularity);
  7261. return -EINVAL;
  7262. }
  7263. if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
  7264. (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
  7265. dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
  7266. __func__, peer_granularity);
  7267. return -EINVAL;
  7268. }
  7269. ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
  7270. if (ret)
  7271. goto out;
  7272. ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
  7273. &peer_pa_tactivate);
  7274. if (ret)
  7275. goto out;
  7276. pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
  7277. peer_pa_tactivate_us = peer_pa_tactivate *
  7278. gran_to_us_table[peer_granularity - 1];
  7279. if (pa_tactivate_us >= peer_pa_tactivate_us) {
  7280. u32 new_peer_pa_tactivate;
  7281. new_peer_pa_tactivate = pa_tactivate_us /
  7282. gran_to_us_table[peer_granularity - 1];
  7283. new_peer_pa_tactivate++;
  7284. ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
  7285. new_peer_pa_tactivate);
  7286. }
  7287. out:
  7288. return ret;
  7289. }
  7290. /**
  7291. * ufshcd_quirk_override_pa_h8time - Ensures proper adjustment of PA_HIBERN8TIME.
  7292. * @hba: per-adapter instance
  7293. *
  7294. * Some UFS devices require specific adjustments to the PA_HIBERN8TIME parameter
  7295. * to ensure proper hibernation timing. This function retrieves the current
  7296. * PA_HIBERN8TIME value and increments it by 100us.
  7297. */
  7298. static void ufshcd_quirk_override_pa_h8time(struct ufs_hba *hba)
  7299. {
  7300. u32 pa_h8time;
  7301. int ret;
  7302. ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_HIBERN8TIME), &pa_h8time);
  7303. if (ret) {
  7304. dev_err(hba->dev, "Failed to get PA_HIBERN8TIME: %d\n", ret);
  7305. return;
  7306. }
  7307. /* Increment by 1 to increase hibernation time by 100 µs */
  7308. ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), pa_h8time + 1);
  7309. if (ret)
  7310. dev_err(hba->dev, "Failed updating PA_HIBERN8TIME: %d\n", ret);
  7311. }
  7312. static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
  7313. {
  7314. ufshcd_vops_apply_dev_quirks(hba);
  7315. if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
  7316. /* set 1ms timeout for PA_TACTIVATE */
  7317. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
  7318. if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
  7319. ufshcd_quirk_tune_host_pa_tactivate(hba);
  7320. if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_HIBER8TIME)
  7321. ufshcd_quirk_override_pa_h8time(hba);
  7322. }
  7323. static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
  7324. {
  7325. hba->ufs_stats.hibern8_exit_cnt = 0;
  7326. hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
  7327. hba->req_abort_count = 0;
  7328. }
  7329. static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
  7330. {
  7331. int err;
  7332. u8 *desc_buf;
  7333. desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
  7334. if (!desc_buf) {
  7335. err = -ENOMEM;
  7336. goto out;
  7337. }
  7338. err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
  7339. desc_buf, QUERY_DESC_MAX_SIZE);
  7340. if (err) {
  7341. dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
  7342. __func__, err);
  7343. goto out;
  7344. }
  7345. if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
  7346. hba->dev_info.max_lu_supported = 32;
  7347. else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
  7348. hba->dev_info.max_lu_supported = 8;
  7349. out:
  7350. kfree(desc_buf);
  7351. return err;
  7352. }
  7353. struct ufs_ref_clk {
  7354. unsigned long freq_hz;
  7355. enum ufs_ref_clk_freq val;
  7356. };
  7357. static const struct ufs_ref_clk ufs_ref_clk_freqs[] = {
  7358. {19200000, REF_CLK_FREQ_19_2_MHZ},
  7359. {26000000, REF_CLK_FREQ_26_MHZ},
  7360. {38400000, REF_CLK_FREQ_38_4_MHZ},
  7361. {52000000, REF_CLK_FREQ_52_MHZ},
  7362. {0, REF_CLK_FREQ_INVAL},
  7363. };
  7364. static enum ufs_ref_clk_freq
  7365. ufs_get_bref_clk_from_hz(unsigned long freq)
  7366. {
  7367. int i;
  7368. for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
  7369. if (ufs_ref_clk_freqs[i].freq_hz == freq)
  7370. return ufs_ref_clk_freqs[i].val;
  7371. return REF_CLK_FREQ_INVAL;
  7372. }
  7373. void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
  7374. {
  7375. unsigned long freq;
  7376. freq = clk_get_rate(refclk);
  7377. hba->dev_ref_clk_freq =
  7378. ufs_get_bref_clk_from_hz(freq);
  7379. if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
  7380. dev_err(hba->dev,
  7381. "invalid ref_clk setting = %ld\n", freq);
  7382. }
  7383. static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
  7384. {
  7385. int err;
  7386. u32 ref_clk;
  7387. u32 freq = hba->dev_ref_clk_freq;
  7388. err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
  7389. QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
  7390. if (err) {
  7391. dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
  7392. err);
  7393. goto out;
  7394. }
  7395. if (ref_clk == freq)
  7396. goto out; /* nothing to update */
  7397. err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
  7398. QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
  7399. if (err) {
  7400. dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
  7401. ufs_ref_clk_freqs[freq].freq_hz);
  7402. goto out;
  7403. }
  7404. dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
  7405. ufs_ref_clk_freqs[freq].freq_hz);
  7406. out:
  7407. return err;
  7408. }
  7409. static int ufshcd_device_params_init(struct ufs_hba *hba)
  7410. {
  7411. bool flag;
  7412. int ret;
  7413. /* Init UFS geometry descriptor related parameters */
  7414. ret = ufshcd_device_geo_params_init(hba);
  7415. if (ret)
  7416. goto out;
  7417. /* Check and apply UFS device quirks */
  7418. ret = ufs_get_device_desc(hba);
  7419. if (ret) {
  7420. dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
  7421. __func__, ret);
  7422. goto out;
  7423. }
  7424. ufshcd_set_rtt(hba);
  7425. ufshcd_get_ref_clk_gating_wait(hba);
  7426. if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
  7427. QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
  7428. hba->dev_info.f_power_on_wp_en = flag;
  7429. /* Probe maximum power mode co-supported by both UFS host and device */
  7430. if (ufshcd_get_max_pwr_mode(hba))
  7431. dev_err(hba->dev,
  7432. "%s: Failed getting max supported power mode\n",
  7433. __func__);
  7434. out:
  7435. return ret;
  7436. }
  7437. static void ufshcd_set_timestamp_attr(struct ufs_hba *hba)
  7438. {
  7439. int err;
  7440. struct ufs_query_req *request = NULL;
  7441. struct ufs_query_res *response = NULL;
  7442. struct ufs_dev_info *dev_info = &hba->dev_info;
  7443. struct utp_upiu_query_v4_0 *upiu_data;
  7444. if (dev_info->wspecversion < 0x400)
  7445. return;
  7446. ufshcd_dev_man_lock(hba);
  7447. ufshcd_init_query(hba, &request, &response,
  7448. UPIU_QUERY_OPCODE_WRITE_ATTR,
  7449. QUERY_ATTR_IDN_TIMESTAMP, 0, 0);
  7450. request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
  7451. upiu_data = (struct utp_upiu_query_v4_0 *)&request->upiu_req;
  7452. put_unaligned_be64(ktime_get_real_ns(), &upiu_data->osf3);
  7453. err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
  7454. if (err)
  7455. dev_err(hba->dev, "%s: failed to set timestamp %d\n",
  7456. __func__, err);
  7457. ufshcd_dev_man_unlock(hba);
  7458. }
  7459. /**
  7460. * ufshcd_add_lus - probe and add UFS logical units
  7461. * @hba: per-adapter instance
  7462. *
  7463. * Return: 0 upon success; < 0 upon failure.
  7464. */
  7465. static int ufshcd_add_lus(struct ufs_hba *hba)
  7466. {
  7467. int ret;
  7468. /* Add required well known logical units to scsi mid layer */
  7469. ret = ufshcd_scsi_add_wlus(hba);
  7470. if (ret)
  7471. goto out;
  7472. /* Initialize devfreq after UFS device is detected */
  7473. if (ufshcd_is_clkscaling_supported(hba)) {
  7474. memcpy(&hba->clk_scaling.saved_pwr_info,
  7475. &hba->pwr_info,
  7476. sizeof(struct ufs_pa_layer_attr));
  7477. hba->clk_scaling.is_allowed = true;
  7478. ret = ufshcd_devfreq_init(hba);
  7479. if (ret)
  7480. goto out;
  7481. hba->clk_scaling.is_enabled = true;
  7482. ufshcd_init_clk_scaling_sysfs(hba);
  7483. }
  7484. /*
  7485. * The RTC update code accesses the hba->ufs_device_wlun->sdev_gendev
  7486. * pointer and hence must only be started after the WLUN pointer has
  7487. * been initialized by ufshcd_scsi_add_wlus().
  7488. */
  7489. schedule_delayed_work(&hba->ufs_rtc_update_work,
  7490. msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
  7491. ufs_bsg_probe(hba);
  7492. scsi_scan_host(hba->host);
  7493. out:
  7494. return ret;
  7495. }
  7496. /* SDB - Single Doorbell */
  7497. static void ufshcd_release_sdb_queue(struct ufs_hba *hba, int nutrs)
  7498. {
  7499. size_t ucdl_size, utrdl_size;
  7500. ucdl_size = ufshcd_get_ucd_size(hba) * nutrs;
  7501. dmam_free_coherent(hba->dev, ucdl_size, hba->ucdl_base_addr,
  7502. hba->ucdl_dma_addr);
  7503. utrdl_size = sizeof(struct utp_transfer_req_desc) * nutrs;
  7504. dmam_free_coherent(hba->dev, utrdl_size, hba->utrdl_base_addr,
  7505. hba->utrdl_dma_addr);
  7506. devm_kfree(hba->dev, hba->lrb);
  7507. }
  7508. static int ufshcd_alloc_mcq(struct ufs_hba *hba)
  7509. {
  7510. int ret;
  7511. int old_nutrs = hba->nutrs;
  7512. ret = ufshcd_mcq_decide_queue_depth(hba);
  7513. if (ret < 0)
  7514. return ret;
  7515. hba->nutrs = ret;
  7516. ret = ufshcd_mcq_init(hba);
  7517. if (ret)
  7518. goto err;
  7519. /*
  7520. * Previously allocated memory for nutrs may not be enough in MCQ mode.
  7521. * Number of supported tags in MCQ mode may be larger than SDB mode.
  7522. */
  7523. if (hba->nutrs != old_nutrs) {
  7524. ufshcd_release_sdb_queue(hba, old_nutrs);
  7525. ret = ufshcd_memory_alloc(hba);
  7526. if (ret)
  7527. goto err;
  7528. ufshcd_host_memory_configure(hba);
  7529. }
  7530. ret = ufshcd_mcq_memory_alloc(hba);
  7531. if (ret)
  7532. goto err;
  7533. hba->host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED;
  7534. hba->reserved_slot = hba->nutrs - UFSHCD_NUM_RESERVED;
  7535. return 0;
  7536. err:
  7537. hba->nutrs = old_nutrs;
  7538. return ret;
  7539. }
  7540. static void ufshcd_config_mcq(struct ufs_hba *hba)
  7541. {
  7542. int ret;
  7543. u32 intrs;
  7544. ret = ufshcd_mcq_vops_config_esi(hba);
  7545. dev_info(hba->dev, "ESI %sconfigured\n", ret ? "is not " : "");
  7546. intrs = UFSHCD_ENABLE_MCQ_INTRS;
  7547. if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_INTR)
  7548. intrs &= ~MCQ_CQ_EVENT_STATUS;
  7549. ufshcd_enable_intr(hba, intrs);
  7550. ufshcd_mcq_make_queues_operational(hba);
  7551. ufshcd_mcq_config_mac(hba, hba->nutrs);
  7552. dev_info(hba->dev, "MCQ configured, nr_queues=%d, io_queues=%d, read_queue=%d, poll_queues=%d, queue_depth=%d\n",
  7553. hba->nr_hw_queues, hba->nr_queues[HCTX_TYPE_DEFAULT],
  7554. hba->nr_queues[HCTX_TYPE_READ], hba->nr_queues[HCTX_TYPE_POLL],
  7555. hba->nutrs);
  7556. }
  7557. static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
  7558. {
  7559. int ret;
  7560. struct Scsi_Host *host = hba->host;
  7561. hba->ufshcd_state = UFSHCD_STATE_RESET;
  7562. ret = ufshcd_link_startup(hba);
  7563. if (ret)
  7564. return ret;
  7565. if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION)
  7566. return ret;
  7567. /* Debug counters initialization */
  7568. ufshcd_clear_dbg_ufs_stats(hba);
  7569. /* UniPro link is active now */
  7570. ufshcd_set_link_active(hba);
  7571. /* Reconfigure MCQ upon reset */
  7572. if (hba->mcq_enabled && !init_dev_params) {
  7573. ufshcd_config_mcq(hba);
  7574. ufshcd_mcq_enable(hba);
  7575. }
  7576. /* Verify device initialization by sending NOP OUT UPIU */
  7577. ret = ufshcd_verify_dev_init(hba);
  7578. if (ret)
  7579. return ret;
  7580. /* Initiate UFS initialization, and waiting until completion */
  7581. ret = ufshcd_complete_dev_init(hba);
  7582. if (ret)
  7583. return ret;
  7584. /*
  7585. * Initialize UFS device parameters used by driver, these
  7586. * parameters are associated with UFS descriptors.
  7587. */
  7588. if (init_dev_params) {
  7589. ret = ufshcd_device_params_init(hba);
  7590. if (ret)
  7591. return ret;
  7592. if (is_mcq_supported(hba) && !hba->scsi_host_added) {
  7593. ufshcd_mcq_enable(hba);
  7594. ret = ufshcd_alloc_mcq(hba);
  7595. if (!ret) {
  7596. ufshcd_config_mcq(hba);
  7597. } else {
  7598. /* Continue with SDB mode */
  7599. ufshcd_mcq_disable(hba);
  7600. use_mcq_mode = false;
  7601. dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
  7602. ret);
  7603. }
  7604. ret = scsi_add_host(host, hba->dev);
  7605. if (ret) {
  7606. dev_err(hba->dev, "scsi_add_host failed\n");
  7607. return ret;
  7608. }
  7609. hba->scsi_host_added = true;
  7610. } else if (is_mcq_supported(hba)) {
  7611. /* UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is set */
  7612. ufshcd_config_mcq(hba);
  7613. ufshcd_mcq_enable(hba);
  7614. }
  7615. }
  7616. ufshcd_tune_unipro_params(hba);
  7617. /* UFS device is also active now */
  7618. ufshcd_set_ufs_dev_active(hba);
  7619. ufshcd_force_reset_auto_bkops(hba);
  7620. ufshcd_set_timestamp_attr(hba);
  7621. /* Gear up to HS gear if supported */
  7622. if (hba->max_pwr_info.is_valid) {
  7623. /*
  7624. * Set the right value to bRefClkFreq before attempting to
  7625. * switch to HS gears.
  7626. */
  7627. if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
  7628. ufshcd_set_dev_ref_clk(hba);
  7629. ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
  7630. if (ret) {
  7631. dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
  7632. __func__, ret);
  7633. return ret;
  7634. }
  7635. }
  7636. return 0;
  7637. }
  7638. /**
  7639. * ufshcd_probe_hba - probe hba to detect device and initialize it
  7640. * @hba: per-adapter instance
  7641. * @init_dev_params: whether or not to call ufshcd_device_params_init().
  7642. *
  7643. * Execute link-startup and verify device initialization
  7644. *
  7645. * Return: 0 upon success; < 0 upon failure.
  7646. */
  7647. static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  7648. {
  7649. ktime_t start = ktime_get();
  7650. unsigned long flags;
  7651. int ret;
  7652. ret = ufshcd_device_init(hba, init_dev_params);
  7653. if (ret)
  7654. goto out;
  7655. if (!hba->pm_op_in_progress &&
  7656. (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) {
  7657. /* Reset the device and controller before doing reinit */
  7658. ufshcd_device_reset(hba);
  7659. ufs_put_device_desc(hba);
  7660. ufshcd_hba_stop(hba);
  7661. ret = ufshcd_hba_enable(hba);
  7662. if (ret) {
  7663. dev_err(hba->dev, "Host controller enable failed\n");
  7664. ufshcd_print_evt_hist(hba);
  7665. ufshcd_print_host_state(hba);
  7666. goto out;
  7667. }
  7668. /* Reinit the device */
  7669. ret = ufshcd_device_init(hba, init_dev_params);
  7670. if (ret)
  7671. goto out;
  7672. }
  7673. ufshcd_print_pwr_info(hba);
  7674. /*
  7675. * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
  7676. * and for removable UFS card as well, hence always set the parameter.
  7677. * Note: Error handler may issue the device reset hence resetting
  7678. * bActiveICCLevel as well so it is always safe to set this here.
  7679. */
  7680. ufshcd_set_active_icc_lvl(hba);
  7681. /* Enable UFS Write Booster if supported */
  7682. ufshcd_configure_wb(hba);
  7683. if (hba->ee_usr_mask)
  7684. ufshcd_write_ee_control(hba);
  7685. ufshcd_configure_auto_hibern8(hba);
  7686. out:
  7687. spin_lock_irqsave(hba->host->host_lock, flags);
  7688. if (ret)
  7689. hba->ufshcd_state = UFSHCD_STATE_ERROR;
  7690. else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
  7691. hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
  7692. spin_unlock_irqrestore(hba->host->host_lock, flags);
  7693. trace_ufshcd_init(dev_name(hba->dev), ret,
  7694. ktime_to_us(ktime_sub(ktime_get(), start)),
  7695. hba->curr_dev_pwr_mode, hba->uic_link_state);
  7696. return ret;
  7697. }
  7698. /**
  7699. * ufshcd_async_scan - asynchronous execution for probing hba
  7700. * @data: data pointer to pass to this function
  7701. * @cookie: cookie data
  7702. */
  7703. static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  7704. {
  7705. struct ufs_hba *hba = (struct ufs_hba *)data;
  7706. int ret;
  7707. down(&hba->host_sem);
  7708. /* Initialize hba, detect and initialize UFS device */
  7709. ret = ufshcd_probe_hba(hba, true);
  7710. up(&hba->host_sem);
  7711. if (ret)
  7712. goto out;
  7713. /* Probe and add UFS logical units */
  7714. ret = ufshcd_add_lus(hba);
  7715. out:
  7716. pm_runtime_put_sync(hba->dev);
  7717. if (ret)
  7718. dev_err(hba->dev, "%s failed: %d\n", __func__, ret);
  7719. }
  7720. static enum scsi_timeout_action ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
  7721. {
  7722. struct ufs_hba *hba = shost_priv(scmd->device->host);
  7723. if (!hba->system_suspending) {
  7724. /* Activate the error handler in the SCSI core. */
  7725. return SCSI_EH_NOT_HANDLED;
  7726. }
  7727. /*
  7728. * If we get here we know that no TMFs are outstanding and also that
  7729. * the only pending command is a START STOP UNIT command. Handle the
  7730. * timeout of that command directly to prevent a deadlock between
  7731. * ufshcd_set_dev_pwr_mode() and ufshcd_err_handler().
  7732. */
  7733. ufshcd_link_recovery(hba);
  7734. dev_info(hba->dev, "%s() finished; outstanding_tasks = %#lx.\n",
  7735. __func__, hba->outstanding_tasks);
  7736. return scsi_host_busy(hba->host) ? SCSI_EH_RESET_TIMER : SCSI_EH_DONE;
  7737. }
  7738. static const struct attribute_group *ufshcd_driver_groups[] = {
  7739. &ufs_sysfs_unit_descriptor_group,
  7740. &ufs_sysfs_lun_attributes_group,
  7741. NULL,
  7742. };
  7743. static struct ufs_hba_variant_params ufs_hba_vps = {
  7744. .hba_enable_delay_us = 1000,
  7745. .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
  7746. .devfreq_profile.polling_ms = 100,
  7747. .devfreq_profile.target = ufshcd_devfreq_target,
  7748. .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
  7749. .ondemand_data.upthreshold = 70,
  7750. .ondemand_data.downdifferential = 5,
  7751. };
  7752. static const struct scsi_host_template ufshcd_driver_template = {
  7753. .module = THIS_MODULE,
  7754. .name = UFSHCD,
  7755. .proc_name = UFSHCD,
  7756. .map_queues = ufshcd_map_queues,
  7757. .queuecommand = ufshcd_queuecommand,
  7758. .mq_poll = ufshcd_poll,
  7759. .slave_alloc = ufshcd_slave_alloc,
  7760. .device_configure = ufshcd_device_configure,
  7761. .slave_destroy = ufshcd_slave_destroy,
  7762. .change_queue_depth = ufshcd_change_queue_depth,
  7763. .eh_abort_handler = ufshcd_abort,
  7764. .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
  7765. .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
  7766. .eh_timed_out = ufshcd_eh_timed_out,
  7767. .this_id = -1,
  7768. .sg_tablesize = SG_ALL,
  7769. .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
  7770. .max_sectors = SZ_1M / SECTOR_SIZE,
  7771. .max_host_blocked = 1,
  7772. .track_queue_depth = 1,
  7773. .skip_settle_delay = 1,
  7774. .sdev_groups = ufshcd_driver_groups,
  7775. };
  7776. static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
  7777. int ua)
  7778. {
  7779. int ret;
  7780. if (!vreg)
  7781. return 0;
  7782. /*
  7783. * "set_load" operation shall be required on those regulators
  7784. * which specifically configured current limitation. Otherwise
  7785. * zero max_uA may cause unexpected behavior when regulator is
  7786. * enabled or set as high power mode.
  7787. */
  7788. if (!vreg->max_uA)
  7789. return 0;
  7790. ret = regulator_set_load(vreg->reg, ua);
  7791. if (ret < 0) {
  7792. dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
  7793. __func__, vreg->name, ua, ret);
  7794. }
  7795. return ret;
  7796. }
  7797. static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
  7798. struct ufs_vreg *vreg)
  7799. {
  7800. return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
  7801. }
  7802. static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
  7803. struct ufs_vreg *vreg)
  7804. {
  7805. if (!vreg)
  7806. return 0;
  7807. return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
  7808. }
  7809. static int ufshcd_config_vreg(struct device *dev,
  7810. struct ufs_vreg *vreg, bool on)
  7811. {
  7812. if (regulator_count_voltages(vreg->reg) <= 0)
  7813. return 0;
  7814. return ufshcd_config_vreg_load(dev, vreg, on ? vreg->max_uA : 0);
  7815. }
  7816. static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
  7817. {
  7818. int ret = 0;
  7819. if (!vreg || vreg->enabled)
  7820. goto out;
  7821. ret = ufshcd_config_vreg(dev, vreg, true);
  7822. if (!ret)
  7823. ret = regulator_enable(vreg->reg);
  7824. if (!ret)
  7825. vreg->enabled = true;
  7826. else
  7827. dev_err(dev, "%s: %s enable failed, err=%d\n",
  7828. __func__, vreg->name, ret);
  7829. out:
  7830. return ret;
  7831. }
  7832. static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
  7833. {
  7834. int ret = 0;
  7835. if (!vreg || !vreg->enabled || vreg->always_on)
  7836. goto out;
  7837. ret = regulator_disable(vreg->reg);
  7838. if (!ret) {
  7839. /* ignore errors on applying disable config */
  7840. ufshcd_config_vreg(dev, vreg, false);
  7841. vreg->enabled = false;
  7842. } else {
  7843. dev_err(dev, "%s: %s disable failed, err=%d\n",
  7844. __func__, vreg->name, ret);
  7845. }
  7846. out:
  7847. return ret;
  7848. }
  7849. static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
  7850. {
  7851. int ret = 0;
  7852. struct device *dev = hba->dev;
  7853. struct ufs_vreg_info *info = &hba->vreg_info;
  7854. ret = ufshcd_toggle_vreg(dev, info->vcc, on);
  7855. if (ret)
  7856. goto out;
  7857. ret = ufshcd_toggle_vreg(dev, info->vccq, on);
  7858. if (ret)
  7859. goto out;
  7860. ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
  7861. out:
  7862. if (ret) {
  7863. ufshcd_toggle_vreg(dev, info->vccq2, false);
  7864. ufshcd_toggle_vreg(dev, info->vccq, false);
  7865. ufshcd_toggle_vreg(dev, info->vcc, false);
  7866. }
  7867. return ret;
  7868. }
  7869. static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
  7870. {
  7871. struct ufs_vreg_info *info = &hba->vreg_info;
  7872. return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
  7873. }
  7874. int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
  7875. {
  7876. int ret = 0;
  7877. if (!vreg)
  7878. goto out;
  7879. vreg->reg = devm_regulator_get(dev, vreg->name);
  7880. if (IS_ERR(vreg->reg)) {
  7881. ret = PTR_ERR(vreg->reg);
  7882. dev_err(dev, "%s: %s get failed, err=%d\n",
  7883. __func__, vreg->name, ret);
  7884. }
  7885. out:
  7886. return ret;
  7887. }
  7888. EXPORT_SYMBOL_GPL(ufshcd_get_vreg);
  7889. static int ufshcd_init_vreg(struct ufs_hba *hba)
  7890. {
  7891. int ret = 0;
  7892. struct device *dev = hba->dev;
  7893. struct ufs_vreg_info *info = &hba->vreg_info;
  7894. ret = ufshcd_get_vreg(dev, info->vcc);
  7895. if (ret)
  7896. goto out;
  7897. ret = ufshcd_get_vreg(dev, info->vccq);
  7898. if (!ret)
  7899. ret = ufshcd_get_vreg(dev, info->vccq2);
  7900. out:
  7901. return ret;
  7902. }
  7903. static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
  7904. {
  7905. struct ufs_vreg_info *info = &hba->vreg_info;
  7906. return ufshcd_get_vreg(hba->dev, info->vdd_hba);
  7907. }
  7908. static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
  7909. {
  7910. int ret = 0;
  7911. struct ufs_clk_info *clki;
  7912. struct list_head *head = &hba->clk_list_head;
  7913. ktime_t start = ktime_get();
  7914. bool clk_state_changed = false;
  7915. if (list_empty(head))
  7916. goto out;
  7917. ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
  7918. if (ret)
  7919. return ret;
  7920. list_for_each_entry(clki, head, list) {
  7921. if (!IS_ERR_OR_NULL(clki->clk)) {
  7922. /*
  7923. * Don't disable clocks which are needed
  7924. * to keep the link active.
  7925. */
  7926. if (ufshcd_is_link_active(hba) &&
  7927. clki->keep_link_active)
  7928. continue;
  7929. clk_state_changed = on ^ clki->enabled;
  7930. if (on && !clki->enabled) {
  7931. ret = clk_prepare_enable(clki->clk);
  7932. if (ret) {
  7933. dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
  7934. __func__, clki->name, ret);
  7935. goto out;
  7936. }
  7937. } else if (!on && clki->enabled) {
  7938. clk_disable_unprepare(clki->clk);
  7939. }
  7940. clki->enabled = on;
  7941. dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
  7942. clki->name, on ? "en" : "dis");
  7943. }
  7944. }
  7945. ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
  7946. if (ret)
  7947. return ret;
  7948. if (!ufshcd_is_clkscaling_supported(hba))
  7949. ufshcd_pm_qos_update(hba, on);
  7950. out:
  7951. if (ret) {
  7952. list_for_each_entry(clki, head, list) {
  7953. if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
  7954. clk_disable_unprepare(clki->clk);
  7955. }
  7956. } else if (!ret && on && hba->clk_gating.is_initialized) {
  7957. scoped_guard(spinlock_irqsave, &hba->clk_gating.lock)
  7958. hba->clk_gating.state = CLKS_ON;
  7959. trace_ufshcd_clk_gating(dev_name(hba->dev),
  7960. hba->clk_gating.state);
  7961. }
  7962. if (clk_state_changed)
  7963. trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
  7964. (on ? "on" : "off"),
  7965. ktime_to_us(ktime_sub(ktime_get(), start)), ret);
  7966. return ret;
  7967. }
  7968. static enum ufs_ref_clk_freq ufshcd_parse_ref_clk_property(struct ufs_hba *hba)
  7969. {
  7970. u32 freq;
  7971. int ret = device_property_read_u32(hba->dev, "ref-clk-freq", &freq);
  7972. if (ret) {
  7973. dev_dbg(hba->dev, "Cannot query 'ref-clk-freq' property = %d", ret);
  7974. return REF_CLK_FREQ_INVAL;
  7975. }
  7976. return ufs_get_bref_clk_from_hz(freq);
  7977. }
  7978. static int ufshcd_init_clocks(struct ufs_hba *hba)
  7979. {
  7980. int ret = 0;
  7981. struct ufs_clk_info *clki;
  7982. struct device *dev = hba->dev;
  7983. struct list_head *head = &hba->clk_list_head;
  7984. if (list_empty(head))
  7985. goto out;
  7986. list_for_each_entry(clki, head, list) {
  7987. if (!clki->name)
  7988. continue;
  7989. clki->clk = devm_clk_get(dev, clki->name);
  7990. if (IS_ERR(clki->clk)) {
  7991. ret = PTR_ERR(clki->clk);
  7992. dev_err(dev, "%s: %s clk get failed, %d\n",
  7993. __func__, clki->name, ret);
  7994. goto out;
  7995. }
  7996. /*
  7997. * Parse device ref clk freq as per device tree "ref_clk".
  7998. * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
  7999. * in ufshcd_alloc_host().
  8000. */
  8001. if (!strcmp(clki->name, "ref_clk"))
  8002. ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
  8003. if (clki->max_freq) {
  8004. ret = clk_set_rate(clki->clk, clki->max_freq);
  8005. if (ret) {
  8006. dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
  8007. __func__, clki->name,
  8008. clki->max_freq, ret);
  8009. goto out;
  8010. }
  8011. clki->curr_freq = clki->max_freq;
  8012. }
  8013. dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
  8014. clki->name, clk_get_rate(clki->clk));
  8015. }
  8016. /* Set Max. frequency for all clocks */
  8017. if (hba->use_pm_opp) {
  8018. ret = ufshcd_opp_set_rate(hba, ULONG_MAX);
  8019. if (ret) {
  8020. dev_err(hba->dev, "%s: failed to set OPP: %d", __func__,
  8021. ret);
  8022. goto out;
  8023. }
  8024. }
  8025. out:
  8026. return ret;
  8027. }
  8028. static int ufshcd_variant_hba_init(struct ufs_hba *hba)
  8029. {
  8030. int err = 0;
  8031. if (!hba->vops)
  8032. goto out;
  8033. err = ufshcd_vops_init(hba);
  8034. if (err)
  8035. dev_err_probe(hba->dev, err,
  8036. "%s: variant %s init failed with err %d\n",
  8037. __func__, ufshcd_get_var_name(hba), err);
  8038. out:
  8039. return err;
  8040. }
  8041. static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
  8042. {
  8043. if (!hba->vops)
  8044. return;
  8045. ufshcd_vops_exit(hba);
  8046. }
  8047. static int ufshcd_hba_init(struct ufs_hba *hba)
  8048. {
  8049. int err;
  8050. /*
  8051. * Handle host controller power separately from the UFS device power
  8052. * rails as it will help controlling the UFS host controller power
  8053. * collapse easily which is different than UFS device power collapse.
  8054. * Also, enable the host controller power before we go ahead with rest
  8055. * of the initialization here.
  8056. */
  8057. err = ufshcd_init_hba_vreg(hba);
  8058. if (err)
  8059. goto out;
  8060. err = ufshcd_setup_hba_vreg(hba, true);
  8061. if (err)
  8062. goto out;
  8063. err = ufshcd_init_clocks(hba);
  8064. if (err)
  8065. goto out_disable_hba_vreg;
  8066. if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
  8067. hba->dev_ref_clk_freq = ufshcd_parse_ref_clk_property(hba);
  8068. err = ufshcd_setup_clocks(hba, true);
  8069. if (err)
  8070. goto out_disable_hba_vreg;
  8071. err = ufshcd_init_vreg(hba);
  8072. if (err)
  8073. goto out_disable_clks;
  8074. err = ufshcd_setup_vreg(hba, true);
  8075. if (err)
  8076. goto out_disable_clks;
  8077. err = ufshcd_variant_hba_init(hba);
  8078. if (err)
  8079. goto out_disable_vreg;
  8080. ufs_debugfs_hba_init(hba);
  8081. ufs_fault_inject_hba_init(hba);
  8082. hba->is_powered = true;
  8083. goto out;
  8084. out_disable_vreg:
  8085. ufshcd_setup_vreg(hba, false);
  8086. out_disable_clks:
  8087. ufshcd_setup_clocks(hba, false);
  8088. out_disable_hba_vreg:
  8089. ufshcd_setup_hba_vreg(hba, false);
  8090. out:
  8091. return err;
  8092. }
  8093. static void ufshcd_hba_exit(struct ufs_hba *hba)
  8094. {
  8095. if (hba->is_powered) {
  8096. ufshcd_pm_qos_exit(hba);
  8097. ufshcd_exit_clk_scaling(hba);
  8098. ufshcd_exit_clk_gating(hba);
  8099. if (hba->eh_wq)
  8100. destroy_workqueue(hba->eh_wq);
  8101. ufs_debugfs_hba_exit(hba);
  8102. ufshcd_variant_hba_exit(hba);
  8103. ufshcd_setup_vreg(hba, false);
  8104. ufshcd_setup_clocks(hba, false);
  8105. ufshcd_setup_hba_vreg(hba, false);
  8106. hba->is_powered = false;
  8107. ufs_put_device_desc(hba);
  8108. }
  8109. }
  8110. static int ufshcd_execute_start_stop(struct scsi_device *sdev,
  8111. enum ufs_dev_pwr_mode pwr_mode,
  8112. struct scsi_sense_hdr *sshdr)
  8113. {
  8114. const unsigned char cdb[6] = { START_STOP, 0, 0, 0, pwr_mode << 4, 0 };
  8115. struct scsi_failure failure_defs[] = {
  8116. {
  8117. .allowed = 2,
  8118. .result = SCMD_FAILURE_RESULT_ANY,
  8119. },
  8120. };
  8121. struct scsi_failures failures = {
  8122. .failure_definitions = failure_defs,
  8123. };
  8124. const struct scsi_exec_args args = {
  8125. .failures = &failures,
  8126. .sshdr = sshdr,
  8127. .req_flags = BLK_MQ_REQ_PM,
  8128. .scmd_flags = SCMD_FAIL_IF_RECOVERING,
  8129. };
  8130. return scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, /*buffer=*/NULL,
  8131. /*bufflen=*/0, /*timeout=*/10 * HZ, /*retries=*/0,
  8132. &args);
  8133. }
  8134. /**
  8135. * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
  8136. * power mode
  8137. * @hba: per adapter instance
  8138. * @pwr_mode: device power mode to set
  8139. *
  8140. * Return: 0 if requested power mode is set successfully;
  8141. * < 0 if failed to set the requested power mode.
  8142. */
  8143. static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
  8144. enum ufs_dev_pwr_mode pwr_mode)
  8145. {
  8146. struct scsi_sense_hdr sshdr;
  8147. struct scsi_device *sdp;
  8148. unsigned long flags;
  8149. int ret;
  8150. spin_lock_irqsave(hba->host->host_lock, flags);
  8151. sdp = hba->ufs_device_wlun;
  8152. if (sdp && scsi_device_online(sdp))
  8153. ret = scsi_device_get(sdp);
  8154. else
  8155. ret = -ENODEV;
  8156. spin_unlock_irqrestore(hba->host->host_lock, flags);
  8157. if (ret)
  8158. return ret;
  8159. /*
  8160. * If scsi commands fail, the scsi mid-layer schedules scsi error-
  8161. * handling, which would wait for host to be resumed. Since we know
  8162. * we are functional while we are here, skip host resume in error
  8163. * handling context.
  8164. */
  8165. hba->host->eh_noresume = 1;
  8166. /*
  8167. * Current function would be generally called from the power management
  8168. * callbacks hence set the RQF_PM flag so that it doesn't resume the
  8169. * already suspended childs.
  8170. */
  8171. ret = ufshcd_execute_start_stop(sdp, pwr_mode, &sshdr);
  8172. if (ret) {
  8173. sdev_printk(KERN_WARNING, sdp,
  8174. "START_STOP failed for power mode: %d, result %x\n",
  8175. pwr_mode, ret);
  8176. if (ret > 0) {
  8177. if (scsi_sense_valid(&sshdr))
  8178. scsi_print_sense_hdr(sdp, NULL, &sshdr);
  8179. ret = -EIO;
  8180. }
  8181. } else {
  8182. hba->curr_dev_pwr_mode = pwr_mode;
  8183. }
  8184. scsi_device_put(sdp);
  8185. hba->host->eh_noresume = 0;
  8186. return ret;
  8187. }
  8188. static int ufshcd_link_state_transition(struct ufs_hba *hba,
  8189. enum uic_link_state req_link_state,
  8190. bool check_for_bkops)
  8191. {
  8192. int ret = 0;
  8193. if (req_link_state == hba->uic_link_state)
  8194. return 0;
  8195. if (req_link_state == UIC_LINK_HIBERN8_STATE) {
  8196. ret = ufshcd_uic_hibern8_enter(hba);
  8197. if (!ret) {
  8198. ufshcd_set_link_hibern8(hba);
  8199. } else {
  8200. dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
  8201. __func__, ret);
  8202. goto out;
  8203. }
  8204. }
  8205. /*
  8206. * If autobkops is enabled, link can't be turned off because
  8207. * turning off the link would also turn off the device, except in the
  8208. * case of DeepSleep where the device is expected to remain powered.
  8209. */
  8210. else if ((req_link_state == UIC_LINK_OFF_STATE) &&
  8211. (!check_for_bkops || !hba->auto_bkops_enabled)) {
  8212. /*
  8213. * Let's make sure that link is in low power mode, we are doing
  8214. * this currently by putting the link in Hibern8. Otherway to
  8215. * put the link in low power mode is to send the DME end point
  8216. * to device and then send the DME reset command to local
  8217. * unipro. But putting the link in hibern8 is much faster.
  8218. *
  8219. * Note also that putting the link in Hibern8 is a requirement
  8220. * for entering DeepSleep.
  8221. */
  8222. ret = ufshcd_uic_hibern8_enter(hba);
  8223. if (ret) {
  8224. dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
  8225. __func__, ret);
  8226. goto out;
  8227. }
  8228. /*
  8229. * Change controller state to "reset state" which
  8230. * should also put the link in off/reset state
  8231. */
  8232. ufshcd_hba_stop(hba);
  8233. /*
  8234. * TODO: Check if we need any delay to make sure that
  8235. * controller is reset
  8236. */
  8237. ufshcd_set_link_off(hba);
  8238. }
  8239. out:
  8240. return ret;
  8241. }
  8242. static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
  8243. {
  8244. bool vcc_off = false;
  8245. /*
  8246. * It seems some UFS devices may keep drawing more than sleep current
  8247. * (atleast for 500us) from UFS rails (especially from VCCQ rail).
  8248. * To avoid this situation, add 2ms delay before putting these UFS
  8249. * rails in LPM mode.
  8250. */
  8251. if (!ufshcd_is_link_active(hba) &&
  8252. hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
  8253. usleep_range(2000, 2100);
  8254. /*
  8255. * If UFS device is either in UFS_Sleep turn off VCC rail to save some
  8256. * power.
  8257. *
  8258. * If UFS device and link is in OFF state, all power supplies (VCC,
  8259. * VCCQ, VCCQ2) can be turned off if power on write protect is not
  8260. * required. If UFS link is inactive (Hibern8 or OFF state) and device
  8261. * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
  8262. *
  8263. * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
  8264. * in low power state which would save some power.
  8265. *
  8266. * If Write Booster is enabled and the device needs to flush the WB
  8267. * buffer OR if bkops status is urgent for WB, keep Vcc on.
  8268. */
  8269. if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
  8270. !hba->dev_info.is_lu_power_on_wp) {
  8271. ufshcd_setup_vreg(hba, false);
  8272. vcc_off = true;
  8273. } else if (!ufshcd_is_ufs_dev_active(hba)) {
  8274. ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
  8275. vcc_off = true;
  8276. if (ufshcd_is_link_hibern8(hba) || ufshcd_is_link_off(hba)) {
  8277. ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
  8278. ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
  8279. }
  8280. }
  8281. /*
  8282. * Some UFS devices require delay after VCC power rail is turned-off.
  8283. */
  8284. if (vcc_off && hba->vreg_info.vcc &&
  8285. hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
  8286. usleep_range(5000, 5100);
  8287. }
  8288. #ifdef CONFIG_PM
  8289. static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
  8290. {
  8291. int ret = 0;
  8292. if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
  8293. !hba->dev_info.is_lu_power_on_wp) {
  8294. ret = ufshcd_setup_vreg(hba, true);
  8295. } else if (!ufshcd_is_ufs_dev_active(hba)) {
  8296. if (!ufshcd_is_link_active(hba)) {
  8297. ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
  8298. if (ret)
  8299. goto vcc_disable;
  8300. ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
  8301. if (ret)
  8302. goto vccq_lpm;
  8303. }
  8304. ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
  8305. }
  8306. goto out;
  8307. vccq_lpm:
  8308. ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
  8309. vcc_disable:
  8310. ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
  8311. out:
  8312. return ret;
  8313. }
  8314. #endif /* CONFIG_PM */
  8315. static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
  8316. {
  8317. if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
  8318. ufshcd_setup_hba_vreg(hba, false);
  8319. }
  8320. static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
  8321. {
  8322. if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
  8323. ufshcd_setup_hba_vreg(hba, true);
  8324. }
  8325. static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  8326. {
  8327. int ret = 0;
  8328. bool check_for_bkops;
  8329. enum ufs_pm_level pm_lvl;
  8330. enum ufs_dev_pwr_mode req_dev_pwr_mode;
  8331. enum uic_link_state req_link_state;
  8332. hba->pm_op_in_progress = true;
  8333. if (pm_op != UFS_SHUTDOWN_PM) {
  8334. pm_lvl = pm_op == UFS_RUNTIME_PM ?
  8335. hba->rpm_lvl : hba->spm_lvl;
  8336. req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
  8337. req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
  8338. } else {
  8339. req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
  8340. req_link_state = UIC_LINK_OFF_STATE;
  8341. }
  8342. /*
  8343. * If we can't transition into any of the low power modes
  8344. * just gate the clocks.
  8345. */
  8346. ufshcd_hold(hba);
  8347. hba->clk_gating.is_suspended = true;
  8348. if (ufshcd_is_clkscaling_supported(hba))
  8349. ufshcd_clk_scaling_suspend(hba, true);
  8350. if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
  8351. req_link_state == UIC_LINK_ACTIVE_STATE) {
  8352. goto vops_suspend;
  8353. }
  8354. if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
  8355. (req_link_state == hba->uic_link_state))
  8356. goto enable_scaling;
  8357. /* UFS device & link must be active before we enter in this function */
  8358. if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
  8359. /* Wait err handler finish or trigger err recovery */
  8360. if (!ufshcd_eh_in_progress(hba))
  8361. ufshcd_force_error_recovery(hba);
  8362. ret = -EBUSY;
  8363. goto enable_scaling;
  8364. }
  8365. if (pm_op == UFS_RUNTIME_PM) {
  8366. if (ufshcd_can_autobkops_during_suspend(hba)) {
  8367. /*
  8368. * The device is idle with no requests in the queue,
  8369. * allow background operations if bkops status shows
  8370. * that performance might be impacted.
  8371. */
  8372. ret = ufshcd_bkops_ctrl(hba);
  8373. if (ret) {
  8374. /*
  8375. * If return err in suspend flow, IO will hang.
  8376. * Trigger error handler and break suspend for
  8377. * error recovery.
  8378. */
  8379. ufshcd_force_error_recovery(hba);
  8380. ret = -EBUSY;
  8381. goto enable_scaling;
  8382. }
  8383. } else {
  8384. /* make sure that auto bkops is disabled */
  8385. ufshcd_disable_auto_bkops(hba);
  8386. }
  8387. /*
  8388. * If device needs to do BKOP or WB buffer flush during
  8389. * Hibern8, keep device power mode as "active power mode"
  8390. * and VCC supply.
  8391. */
  8392. hba->dev_info.b_rpm_dev_flush_capable =
  8393. hba->auto_bkops_enabled ||
  8394. (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
  8395. ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
  8396. ufshcd_is_auto_hibern8_enabled(hba))) &&
  8397. ufshcd_wb_need_flush(hba));
  8398. }
  8399. flush_work(&hba->eeh_work);
  8400. ret = ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE);
  8401. if (ret)
  8402. goto enable_scaling;
  8403. if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
  8404. if (pm_op != UFS_RUNTIME_PM)
  8405. /* ensure that bkops is disabled */
  8406. ufshcd_disable_auto_bkops(hba);
  8407. if (!hba->dev_info.b_rpm_dev_flush_capable) {
  8408. ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
  8409. if (ret && pm_op != UFS_SHUTDOWN_PM) {
  8410. /*
  8411. * If return err in suspend flow, IO will hang.
  8412. * Trigger error handler and break suspend for
  8413. * error recovery.
  8414. */
  8415. ufshcd_force_error_recovery(hba);
  8416. ret = -EBUSY;
  8417. }
  8418. if (ret)
  8419. goto enable_scaling;
  8420. }
  8421. }
  8422. /*
  8423. * In the case of DeepSleep, the device is expected to remain powered
  8424. * with the link off, so do not check for bkops.
  8425. */
  8426. check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
  8427. ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops);
  8428. if (ret && pm_op != UFS_SHUTDOWN_PM) {
  8429. /*
  8430. * If return err in suspend flow, IO will hang.
  8431. * Trigger error handler and break suspend for
  8432. * error recovery.
  8433. */
  8434. ufshcd_force_error_recovery(hba);
  8435. ret = -EBUSY;
  8436. }
  8437. if (ret)
  8438. goto set_dev_active;
  8439. vops_suspend:
  8440. /*
  8441. * Call vendor specific suspend callback. As these callbacks may access
  8442. * vendor specific host controller register space call them before the
  8443. * host clocks are ON.
  8444. */
  8445. ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
  8446. if (ret)
  8447. goto set_link_active;
  8448. cancel_delayed_work_sync(&hba->ufs_rtc_update_work);
  8449. goto out;
  8450. set_link_active:
  8451. /*
  8452. * Device hardware reset is required to exit DeepSleep. Also, for
  8453. * DeepSleep, the link is off so host reset and restore will be done
  8454. * further below.
  8455. */
  8456. if (ufshcd_is_ufs_dev_deepsleep(hba)) {
  8457. ufshcd_device_reset(hba);
  8458. WARN_ON(!ufshcd_is_link_off(hba));
  8459. }
  8460. if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
  8461. ufshcd_set_link_active(hba);
  8462. else if (ufshcd_is_link_off(hba))
  8463. ufshcd_host_reset_and_restore(hba);
  8464. set_dev_active:
  8465. /* Can also get here needing to exit DeepSleep */
  8466. if (ufshcd_is_ufs_dev_deepsleep(hba)) {
  8467. ufshcd_device_reset(hba);
  8468. ufshcd_host_reset_and_restore(hba);
  8469. }
  8470. if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
  8471. ufshcd_disable_auto_bkops(hba);
  8472. enable_scaling:
  8473. if (ufshcd_is_clkscaling_supported(hba))
  8474. ufshcd_clk_scaling_suspend(hba, false);
  8475. hba->dev_info.b_rpm_dev_flush_capable = false;
  8476. out:
  8477. if (hba->dev_info.b_rpm_dev_flush_capable) {
  8478. schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
  8479. msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
  8480. }
  8481. if (ret) {
  8482. ufshcd_update_evt_hist(hba, UFS_EVT_WL_SUSP_ERR, (u32)ret);
  8483. hba->clk_gating.is_suspended = false;
  8484. ufshcd_release(hba);
  8485. }
  8486. hba->pm_op_in_progress = false;
  8487. return ret;
  8488. }
  8489. #ifdef CONFIG_PM
  8490. static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  8491. {
  8492. int ret;
  8493. enum uic_link_state old_link_state = hba->uic_link_state;
  8494. hba->pm_op_in_progress = true;
  8495. /*
  8496. * Call vendor specific resume callback. As these callbacks may access
  8497. * vendor specific host controller register space call them when the
  8498. * host clocks are ON.
  8499. */
  8500. ret = ufshcd_vops_resume(hba, pm_op);
  8501. if (ret)
  8502. goto out;
  8503. /* For DeepSleep, the only supported option is to have the link off */
  8504. WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
  8505. if (ufshcd_is_link_hibern8(hba)) {
  8506. ret = ufshcd_uic_hibern8_exit(hba);
  8507. if (!ret) {
  8508. ufshcd_set_link_active(hba);
  8509. } else {
  8510. dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
  8511. __func__, ret);
  8512. goto vendor_suspend;
  8513. }
  8514. } else if (ufshcd_is_link_off(hba)) {
  8515. /*
  8516. * A full initialization of the host and the device is
  8517. * required since the link was put to off during suspend.
  8518. * Note, in the case of DeepSleep, the device will exit
  8519. * DeepSleep due to device reset.
  8520. */
  8521. ret = ufshcd_reset_and_restore(hba);
  8522. /*
  8523. * ufshcd_reset_and_restore() should have already
  8524. * set the link state as active
  8525. */
  8526. if (ret || !ufshcd_is_link_active(hba))
  8527. goto vendor_suspend;
  8528. }
  8529. if (!ufshcd_is_ufs_dev_active(hba)) {
  8530. ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
  8531. if (ret)
  8532. goto set_old_link_state;
  8533. ufshcd_set_timestamp_attr(hba);
  8534. schedule_delayed_work(&hba->ufs_rtc_update_work,
  8535. msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
  8536. }
  8537. if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
  8538. ufshcd_enable_auto_bkops(hba);
  8539. else
  8540. /*
  8541. * If BKOPs operations are urgently needed at this moment then
  8542. * keep auto-bkops enabled or else disable it.
  8543. */
  8544. ufshcd_bkops_ctrl(hba);
  8545. if (hba->ee_usr_mask)
  8546. ufshcd_write_ee_control(hba);
  8547. if (ufshcd_is_clkscaling_supported(hba))
  8548. ufshcd_clk_scaling_suspend(hba, false);
  8549. if (hba->dev_info.b_rpm_dev_flush_capable) {
  8550. hba->dev_info.b_rpm_dev_flush_capable = false;
  8551. cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
  8552. }
  8553. ufshcd_configure_auto_hibern8(hba);
  8554. goto out;
  8555. set_old_link_state:
  8556. ufshcd_link_state_transition(hba, old_link_state, 0);
  8557. vendor_suspend:
  8558. ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE);
  8559. ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
  8560. out:
  8561. if (ret)
  8562. ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret);
  8563. hba->clk_gating.is_suspended = false;
  8564. ufshcd_release(hba);
  8565. hba->pm_op_in_progress = false;
  8566. return ret;
  8567. }
  8568. static int ufshcd_wl_runtime_suspend(struct device *dev)
  8569. {
  8570. struct scsi_device *sdev = to_scsi_device(dev);
  8571. struct ufs_hba *hba;
  8572. int ret;
  8573. ktime_t start = ktime_get();
  8574. hba = shost_priv(sdev->host);
  8575. ret = __ufshcd_wl_suspend(hba, UFS_RUNTIME_PM);
  8576. if (ret)
  8577. dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
  8578. trace_ufshcd_wl_runtime_suspend(dev_name(dev), ret,
  8579. ktime_to_us(ktime_sub(ktime_get(), start)),
  8580. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8581. return ret;
  8582. }
  8583. static int ufshcd_wl_runtime_resume(struct device *dev)
  8584. {
  8585. struct scsi_device *sdev = to_scsi_device(dev);
  8586. struct ufs_hba *hba;
  8587. int ret = 0;
  8588. ktime_t start = ktime_get();
  8589. hba = shost_priv(sdev->host);
  8590. ret = __ufshcd_wl_resume(hba, UFS_RUNTIME_PM);
  8591. if (ret)
  8592. dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
  8593. trace_ufshcd_wl_runtime_resume(dev_name(dev), ret,
  8594. ktime_to_us(ktime_sub(ktime_get(), start)),
  8595. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8596. return ret;
  8597. }
  8598. #endif
  8599. #ifdef CONFIG_PM_SLEEP
  8600. static int ufshcd_wl_suspend(struct device *dev)
  8601. {
  8602. struct scsi_device *sdev = to_scsi_device(dev);
  8603. struct ufs_hba *hba;
  8604. int ret = 0;
  8605. ktime_t start = ktime_get();
  8606. hba = shost_priv(sdev->host);
  8607. down(&hba->host_sem);
  8608. hba->system_suspending = true;
  8609. if (pm_runtime_suspended(dev))
  8610. goto out;
  8611. ret = __ufshcd_wl_suspend(hba, UFS_SYSTEM_PM);
  8612. if (ret) {
  8613. dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
  8614. up(&hba->host_sem);
  8615. }
  8616. out:
  8617. if (!ret)
  8618. hba->is_sys_suspended = true;
  8619. trace_ufshcd_wl_suspend(dev_name(dev), ret,
  8620. ktime_to_us(ktime_sub(ktime_get(), start)),
  8621. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8622. return ret;
  8623. }
  8624. static int ufshcd_wl_resume(struct device *dev)
  8625. {
  8626. struct scsi_device *sdev = to_scsi_device(dev);
  8627. struct ufs_hba *hba;
  8628. int ret = 0;
  8629. ktime_t start = ktime_get();
  8630. hba = shost_priv(sdev->host);
  8631. if (pm_runtime_suspended(dev))
  8632. goto out;
  8633. ret = __ufshcd_wl_resume(hba, UFS_SYSTEM_PM);
  8634. if (ret)
  8635. dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
  8636. out:
  8637. trace_ufshcd_wl_resume(dev_name(dev), ret,
  8638. ktime_to_us(ktime_sub(ktime_get(), start)),
  8639. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8640. if (!ret)
  8641. hba->is_sys_suspended = false;
  8642. hba->system_suspending = false;
  8643. up(&hba->host_sem);
  8644. return ret;
  8645. }
  8646. #endif
  8647. /**
  8648. * ufshcd_suspend - helper function for suspend operations
  8649. * @hba: per adapter instance
  8650. *
  8651. * This function will put disable irqs, turn off clocks
  8652. * and set vreg and hba-vreg in lpm mode.
  8653. *
  8654. * Return: 0 upon success; < 0 upon failure.
  8655. */
  8656. static int ufshcd_suspend(struct ufs_hba *hba)
  8657. {
  8658. int ret;
  8659. if (!hba->is_powered)
  8660. return 0;
  8661. /*
  8662. * Disable the host irq as host controller as there won't be any
  8663. * host controller transaction expected till resume.
  8664. */
  8665. ufshcd_disable_irq(hba);
  8666. ret = ufshcd_setup_clocks(hba, false);
  8667. if (ret) {
  8668. ufshcd_enable_irq(hba);
  8669. return ret;
  8670. }
  8671. if (ufshcd_is_clkgating_allowed(hba)) {
  8672. hba->clk_gating.state = CLKS_OFF;
  8673. trace_ufshcd_clk_gating(dev_name(hba->dev),
  8674. hba->clk_gating.state);
  8675. }
  8676. ufshcd_vreg_set_lpm(hba);
  8677. /* Put the host controller in low power mode if possible */
  8678. ufshcd_hba_vreg_set_lpm(hba);
  8679. ufshcd_pm_qos_update(hba, false);
  8680. return ret;
  8681. }
  8682. #ifdef CONFIG_PM
  8683. /**
  8684. * ufshcd_resume - helper function for resume operations
  8685. * @hba: per adapter instance
  8686. *
  8687. * This function basically turns on the regulators, clocks and
  8688. * irqs of the hba.
  8689. *
  8690. * Return: 0 for success and non-zero for failure.
  8691. */
  8692. static int ufshcd_resume(struct ufs_hba *hba)
  8693. {
  8694. int ret;
  8695. if (!hba->is_powered)
  8696. return 0;
  8697. ufshcd_hba_vreg_set_hpm(hba);
  8698. ret = ufshcd_vreg_set_hpm(hba);
  8699. if (ret)
  8700. goto out;
  8701. /* Make sure clocks are enabled before accessing controller */
  8702. ret = ufshcd_setup_clocks(hba, true);
  8703. if (ret)
  8704. goto disable_vreg;
  8705. /* enable the host irq as host controller would be active soon */
  8706. ufshcd_enable_irq(hba);
  8707. goto out;
  8708. disable_vreg:
  8709. ufshcd_vreg_set_lpm(hba);
  8710. out:
  8711. if (ret)
  8712. ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret);
  8713. return ret;
  8714. }
  8715. #endif /* CONFIG_PM */
  8716. #ifdef CONFIG_PM_SLEEP
  8717. /**
  8718. * ufshcd_system_suspend - system suspend callback
  8719. * @dev: Device associated with the UFS controller.
  8720. *
  8721. * Executed before putting the system into a sleep state in which the contents
  8722. * of main memory are preserved.
  8723. *
  8724. * Return: 0 for success and non-zero for failure.
  8725. */
  8726. int ufshcd_system_suspend(struct device *dev)
  8727. {
  8728. struct ufs_hba *hba = dev_get_drvdata(dev);
  8729. int ret = 0;
  8730. ktime_t start = ktime_get();
  8731. if (pm_runtime_suspended(hba->dev))
  8732. goto out;
  8733. ret = ufshcd_suspend(hba);
  8734. out:
  8735. trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
  8736. ktime_to_us(ktime_sub(ktime_get(), start)),
  8737. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8738. return ret;
  8739. }
  8740. EXPORT_SYMBOL(ufshcd_system_suspend);
  8741. /**
  8742. * ufshcd_system_resume - system resume callback
  8743. * @dev: Device associated with the UFS controller.
  8744. *
  8745. * Executed after waking the system up from a sleep state in which the contents
  8746. * of main memory were preserved.
  8747. *
  8748. * Return: 0 for success and non-zero for failure.
  8749. */
  8750. int ufshcd_system_resume(struct device *dev)
  8751. {
  8752. struct ufs_hba *hba = dev_get_drvdata(dev);
  8753. ktime_t start = ktime_get();
  8754. int ret = 0;
  8755. if (pm_runtime_suspended(hba->dev))
  8756. goto out;
  8757. ret = ufshcd_resume(hba);
  8758. out:
  8759. trace_ufshcd_system_resume(dev_name(hba->dev), ret,
  8760. ktime_to_us(ktime_sub(ktime_get(), start)),
  8761. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8762. return ret;
  8763. }
  8764. EXPORT_SYMBOL(ufshcd_system_resume);
  8765. #endif /* CONFIG_PM_SLEEP */
  8766. #ifdef CONFIG_PM
  8767. /**
  8768. * ufshcd_runtime_suspend - runtime suspend callback
  8769. * @dev: Device associated with the UFS controller.
  8770. *
  8771. * Check the description of ufshcd_suspend() function for more details.
  8772. *
  8773. * Return: 0 for success and non-zero for failure.
  8774. */
  8775. int ufshcd_runtime_suspend(struct device *dev)
  8776. {
  8777. struct ufs_hba *hba = dev_get_drvdata(dev);
  8778. int ret;
  8779. ktime_t start = ktime_get();
  8780. ret = ufshcd_suspend(hba);
  8781. trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
  8782. ktime_to_us(ktime_sub(ktime_get(), start)),
  8783. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8784. return ret;
  8785. }
  8786. EXPORT_SYMBOL(ufshcd_runtime_suspend);
  8787. /**
  8788. * ufshcd_runtime_resume - runtime resume routine
  8789. * @dev: Device associated with the UFS controller.
  8790. *
  8791. * This function basically brings controller
  8792. * to active state. Following operations are done in this function:
  8793. *
  8794. * 1. Turn on all the controller related clocks
  8795. * 2. Turn ON VCC rail
  8796. *
  8797. * Return: 0 upon success; < 0 upon failure.
  8798. */
  8799. int ufshcd_runtime_resume(struct device *dev)
  8800. {
  8801. struct ufs_hba *hba = dev_get_drvdata(dev);
  8802. int ret;
  8803. ktime_t start = ktime_get();
  8804. ret = ufshcd_resume(hba);
  8805. trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
  8806. ktime_to_us(ktime_sub(ktime_get(), start)),
  8807. hba->curr_dev_pwr_mode, hba->uic_link_state);
  8808. return ret;
  8809. }
  8810. EXPORT_SYMBOL(ufshcd_runtime_resume);
  8811. #endif /* CONFIG_PM */
  8812. static void ufshcd_wl_shutdown(struct device *dev)
  8813. {
  8814. struct scsi_device *sdev = to_scsi_device(dev);
  8815. struct ufs_hba *hba = shost_priv(sdev->host);
  8816. down(&hba->host_sem);
  8817. hba->shutting_down = true;
  8818. up(&hba->host_sem);
  8819. /* Turn on everything while shutting down */
  8820. ufshcd_rpm_get_sync(hba);
  8821. scsi_device_quiesce(sdev);
  8822. shost_for_each_device(sdev, hba->host) {
  8823. if (sdev == hba->ufs_device_wlun)
  8824. continue;
  8825. mutex_lock(&sdev->state_mutex);
  8826. scsi_device_set_state(sdev, SDEV_OFFLINE);
  8827. mutex_unlock(&sdev->state_mutex);
  8828. }
  8829. __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM);
  8830. /*
  8831. * Next, turn off the UFS controller and the UFS regulators. Disable
  8832. * clocks.
  8833. */
  8834. if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
  8835. ufshcd_suspend(hba);
  8836. hba->is_powered = false;
  8837. }
  8838. /**
  8839. * ufshcd_remove - de-allocate SCSI host and host memory space
  8840. * data structure memory
  8841. * @hba: per adapter instance
  8842. */
  8843. void ufshcd_remove(struct ufs_hba *hba)
  8844. {
  8845. if (hba->ufs_device_wlun)
  8846. ufshcd_rpm_get_sync(hba);
  8847. ufs_hwmon_remove(hba);
  8848. ufs_bsg_remove(hba);
  8849. ufs_sysfs_remove_nodes(hba->dev);
  8850. cancel_delayed_work_sync(&hba->ufs_rtc_update_work);
  8851. blk_mq_destroy_queue(hba->tmf_queue);
  8852. blk_put_queue(hba->tmf_queue);
  8853. blk_mq_free_tag_set(&hba->tmf_tag_set);
  8854. if (hba->scsi_host_added)
  8855. scsi_remove_host(hba->host);
  8856. /* disable interrupts */
  8857. ufshcd_disable_intr(hba, hba->intr_mask);
  8858. ufshcd_hba_stop(hba);
  8859. ufshcd_hba_exit(hba);
  8860. }
  8861. EXPORT_SYMBOL_GPL(ufshcd_remove);
  8862. #ifdef CONFIG_PM_SLEEP
  8863. int ufshcd_system_freeze(struct device *dev)
  8864. {
  8865. return ufshcd_system_suspend(dev);
  8866. }
  8867. EXPORT_SYMBOL_GPL(ufshcd_system_freeze);
  8868. int ufshcd_system_restore(struct device *dev)
  8869. {
  8870. struct ufs_hba *hba = dev_get_drvdata(dev);
  8871. int ret;
  8872. ret = ufshcd_system_resume(dev);
  8873. if (ret)
  8874. return ret;
  8875. /* Configure UTRL and UTMRL base address registers */
  8876. ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
  8877. REG_UTP_TRANSFER_REQ_LIST_BASE_L);
  8878. ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
  8879. REG_UTP_TRANSFER_REQ_LIST_BASE_H);
  8880. ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
  8881. REG_UTP_TASK_REQ_LIST_BASE_L);
  8882. ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
  8883. REG_UTP_TASK_REQ_LIST_BASE_H);
  8884. /*
  8885. * Make sure that UTRL and UTMRL base address registers
  8886. * are updated with the latest queue addresses. Only after
  8887. * updating these addresses, we can queue the new commands.
  8888. */
  8889. ufshcd_readl(hba, REG_UTP_TASK_REQ_LIST_BASE_H);
  8890. return 0;
  8891. }
  8892. EXPORT_SYMBOL_GPL(ufshcd_system_restore);
  8893. int ufshcd_system_thaw(struct device *dev)
  8894. {
  8895. return ufshcd_system_resume(dev);
  8896. }
  8897. EXPORT_SYMBOL_GPL(ufshcd_system_thaw);
  8898. #endif /* CONFIG_PM_SLEEP */
  8899. /**
  8900. * ufshcd_set_dma_mask - Set dma mask based on the controller
  8901. * addressing capability
  8902. * @hba: per adapter instance
  8903. *
  8904. * Return: 0 for success, non-zero for failure.
  8905. */
  8906. static int ufshcd_set_dma_mask(struct ufs_hba *hba)
  8907. {
  8908. if (hba->vops && hba->vops->set_dma_mask)
  8909. return hba->vops->set_dma_mask(hba);
  8910. if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
  8911. if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
  8912. return 0;
  8913. }
  8914. return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
  8915. }
  8916. /**
  8917. * ufshcd_devres_release - devres cleanup handler, invoked during release of
  8918. * hba->dev
  8919. * @host: pointer to SCSI host
  8920. */
  8921. static void ufshcd_devres_release(void *host)
  8922. {
  8923. scsi_host_put(host);
  8924. }
  8925. /**
  8926. * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
  8927. * @dev: pointer to device handle
  8928. * @hba_handle: driver private handle
  8929. *
  8930. * Return: 0 on success, non-zero value on failure.
  8931. *
  8932. * NOTE: There is no corresponding ufshcd_dealloc_host() because this function
  8933. * keeps track of its allocations using devres and deallocates everything on
  8934. * device removal automatically.
  8935. */
  8936. int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
  8937. {
  8938. struct Scsi_Host *host;
  8939. struct ufs_hba *hba;
  8940. int err = 0;
  8941. if (!dev) {
  8942. dev_err(dev,
  8943. "Invalid memory reference for dev is NULL\n");
  8944. err = -ENODEV;
  8945. goto out_error;
  8946. }
  8947. host = scsi_host_alloc(&ufshcd_driver_template,
  8948. sizeof(struct ufs_hba));
  8949. if (!host) {
  8950. dev_err(dev, "scsi_host_alloc failed\n");
  8951. err = -ENOMEM;
  8952. goto out_error;
  8953. }
  8954. err = devm_add_action_or_reset(dev, ufshcd_devres_release,
  8955. host);
  8956. if (err)
  8957. return dev_err_probe(dev, err,
  8958. "failed to add ufshcd dealloc action\n");
  8959. host->nr_maps = HCTX_TYPE_POLL + 1;
  8960. hba = shost_priv(host);
  8961. hba->host = host;
  8962. hba->dev = dev;
  8963. hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
  8964. hba->nop_out_timeout = NOP_OUT_TIMEOUT;
  8965. ufshcd_set_sg_entry_size(hba, sizeof(struct ufshcd_sg_entry));
  8966. INIT_LIST_HEAD(&hba->clk_list_head);
  8967. spin_lock_init(&hba->outstanding_lock);
  8968. *hba_handle = hba;
  8969. out_error:
  8970. return err;
  8971. }
  8972. EXPORT_SYMBOL(ufshcd_alloc_host);
  8973. /* This function exists because blk_mq_alloc_tag_set() requires this. */
  8974. static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
  8975. const struct blk_mq_queue_data *qd)
  8976. {
  8977. WARN_ON_ONCE(true);
  8978. return BLK_STS_NOTSUPP;
  8979. }
  8980. static const struct blk_mq_ops ufshcd_tmf_ops = {
  8981. .queue_rq = ufshcd_queue_tmf,
  8982. };
  8983. /**
  8984. * ufshcd_init - Driver initialization routine
  8985. * @hba: per-adapter instance
  8986. * @mmio_base: base register address
  8987. * @irq: Interrupt line of device
  8988. *
  8989. * Return: 0 on success, non-zero value on failure.
  8990. */
  8991. int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
  8992. {
  8993. int err;
  8994. struct Scsi_Host *host = hba->host;
  8995. struct device *dev = hba->dev;
  8996. /*
  8997. * dev_set_drvdata() must be called before any callbacks are registered
  8998. * that use dev_get_drvdata() (frequency scaling, clock scaling, hwmon,
  8999. * sysfs).
  9000. */
  9001. dev_set_drvdata(dev, hba);
  9002. if (!mmio_base) {
  9003. dev_err(hba->dev,
  9004. "Invalid memory reference for mmio_base is NULL\n");
  9005. err = -ENODEV;
  9006. goto out_error;
  9007. }
  9008. hba->mmio_base = mmio_base;
  9009. hba->irq = irq;
  9010. hba->vps = &ufs_hba_vps;
  9011. /*
  9012. * Initialize clk_gating.lock early since it is being used in
  9013. * ufshcd_setup_clocks()
  9014. */
  9015. spin_lock_init(&hba->clk_gating.lock);
  9016. /*
  9017. * Set the default power management level for runtime and system PM.
  9018. * Host controller drivers can override them in their
  9019. * 'ufs_hba_variant_ops::init' callback.
  9020. *
  9021. * Default power saving mode is to keep UFS link in Hibern8 state
  9022. * and UFS device in sleep state.
  9023. */
  9024. hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
  9025. UFS_SLEEP_PWR_MODE,
  9026. UIC_LINK_HIBERN8_STATE);
  9027. hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
  9028. UFS_SLEEP_PWR_MODE,
  9029. UIC_LINK_HIBERN8_STATE);
  9030. err = ufshcd_hba_init(hba);
  9031. if (err)
  9032. goto out_error;
  9033. /* Read capabilities registers */
  9034. err = ufshcd_hba_capabilities(hba);
  9035. if (err)
  9036. goto out_disable;
  9037. /* Get UFS version supported by the controller */
  9038. hba->ufs_version = ufshcd_get_ufs_version(hba);
  9039. /* Get Interrupt bit mask per version */
  9040. hba->intr_mask = ufshcd_get_intr_mask(hba);
  9041. err = ufshcd_set_dma_mask(hba);
  9042. if (err) {
  9043. dev_err(hba->dev, "set dma mask failed\n");
  9044. goto out_disable;
  9045. }
  9046. /* Allocate memory for host memory space */
  9047. err = ufshcd_memory_alloc(hba);
  9048. if (err) {
  9049. dev_err(hba->dev, "Memory allocation failed\n");
  9050. goto out_disable;
  9051. }
  9052. /* Configure LRB */
  9053. ufshcd_host_memory_configure(hba);
  9054. host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED;
  9055. host->cmd_per_lun = hba->nutrs - UFSHCD_NUM_RESERVED;
  9056. host->max_id = UFSHCD_MAX_ID;
  9057. host->max_lun = UFS_MAX_LUNS;
  9058. host->max_channel = UFSHCD_MAX_CHANNEL;
  9059. host->unique_id = host->host_no;
  9060. host->max_cmd_len = UFS_CDB_SIZE;
  9061. host->queuecommand_may_block = !!(hba->caps & UFSHCD_CAP_CLK_GATING);
  9062. /* Use default RPM delay if host not set */
  9063. if (host->rpm_autosuspend_delay == 0)
  9064. host->rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS;
  9065. hba->max_pwr_info.is_valid = false;
  9066. /* Initialize work queues */
  9067. hba->eh_wq = alloc_ordered_workqueue("ufs_eh_wq_%d", WQ_MEM_RECLAIM,
  9068. hba->host->host_no);
  9069. if (!hba->eh_wq) {
  9070. dev_err(hba->dev, "%s: failed to create eh workqueue\n",
  9071. __func__);
  9072. err = -ENOMEM;
  9073. goto out_disable;
  9074. }
  9075. INIT_WORK(&hba->eh_work, ufshcd_err_handler);
  9076. INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
  9077. sema_init(&hba->host_sem, 1);
  9078. /* Initialize UIC command mutex */
  9079. mutex_init(&hba->uic_cmd_mutex);
  9080. /* Initialize mutex for device management commands */
  9081. mutex_init(&hba->dev_cmd.lock);
  9082. /* Initialize mutex for exception event control */
  9083. mutex_init(&hba->ee_ctrl_mutex);
  9084. mutex_init(&hba->wb_mutex);
  9085. init_rwsem(&hba->clk_scaling_lock);
  9086. ufshcd_init_clk_gating(hba);
  9087. ufshcd_init_clk_scaling(hba);
  9088. /*
  9089. * In order to avoid any spurious interrupt immediately after
  9090. * registering UFS controller interrupt handler, clear any pending UFS
  9091. * interrupt status and disable all the UFS interrupts.
  9092. */
  9093. ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
  9094. REG_INTERRUPT_STATUS);
  9095. ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
  9096. /*
  9097. * Make sure that UFS interrupts are disabled and any pending interrupt
  9098. * status is cleared before registering UFS interrupt handler.
  9099. */
  9100. ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  9101. /* IRQ registration */
  9102. err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
  9103. if (err) {
  9104. dev_err(hba->dev, "request irq failed\n");
  9105. goto out_disable;
  9106. } else {
  9107. hba->is_irq_enabled = true;
  9108. }
  9109. if (!is_mcq_supported(hba)) {
  9110. if (!hba->lsdb_sup) {
  9111. dev_err(hba->dev, "%s: failed to initialize (legacy doorbell mode not supported)\n",
  9112. __func__);
  9113. err = -EINVAL;
  9114. goto out_disable;
  9115. }
  9116. err = scsi_add_host(host, hba->dev);
  9117. if (err) {
  9118. dev_err(hba->dev, "scsi_add_host failed\n");
  9119. goto out_disable;
  9120. }
  9121. hba->scsi_host_added = true;
  9122. }
  9123. hba->tmf_tag_set = (struct blk_mq_tag_set) {
  9124. .nr_hw_queues = 1,
  9125. .queue_depth = hba->nutmrs,
  9126. .ops = &ufshcd_tmf_ops,
  9127. .flags = BLK_MQ_F_NO_SCHED,
  9128. };
  9129. err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
  9130. if (err < 0)
  9131. goto out_remove_scsi_host;
  9132. hba->tmf_queue = blk_mq_alloc_queue(&hba->tmf_tag_set, NULL, NULL);
  9133. if (IS_ERR(hba->tmf_queue)) {
  9134. err = PTR_ERR(hba->tmf_queue);
  9135. goto free_tmf_tag_set;
  9136. }
  9137. hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs,
  9138. sizeof(*hba->tmf_rqs), GFP_KERNEL);
  9139. if (!hba->tmf_rqs) {
  9140. err = -ENOMEM;
  9141. goto free_tmf_queue;
  9142. }
  9143. /* Reset the attached device */
  9144. ufshcd_device_reset(hba);
  9145. ufshcd_init_crypto(hba);
  9146. /* Host controller enable */
  9147. err = ufshcd_hba_enable(hba);
  9148. if (err) {
  9149. dev_err(hba->dev, "Host controller enable failed\n");
  9150. ufshcd_print_evt_hist(hba);
  9151. ufshcd_print_host_state(hba);
  9152. goto free_tmf_queue;
  9153. }
  9154. INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, ufshcd_rpm_dev_flush_recheck_work);
  9155. INIT_DELAYED_WORK(&hba->ufs_rtc_update_work, ufshcd_rtc_work);
  9156. /* Set the default auto-hiberate idle timer value to 150 ms */
  9157. if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
  9158. hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
  9159. FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
  9160. }
  9161. /* Hold auto suspend until async scan completes */
  9162. pm_runtime_get_sync(dev);
  9163. atomic_set(&hba->scsi_block_reqs_cnt, 0);
  9164. /*
  9165. * We are assuming that device wasn't put in sleep/power-down
  9166. * state exclusively during the boot stage before kernel.
  9167. * This assumption helps avoid doing link startup twice during
  9168. * ufshcd_probe_hba().
  9169. */
  9170. ufshcd_set_ufs_dev_active(hba);
  9171. async_schedule(ufshcd_async_scan, hba);
  9172. ufs_sysfs_add_nodes(hba->dev);
  9173. device_enable_async_suspend(dev);
  9174. ufshcd_pm_qos_init(hba);
  9175. return 0;
  9176. free_tmf_queue:
  9177. blk_mq_destroy_queue(hba->tmf_queue);
  9178. blk_put_queue(hba->tmf_queue);
  9179. free_tmf_tag_set:
  9180. blk_mq_free_tag_set(&hba->tmf_tag_set);
  9181. out_remove_scsi_host:
  9182. if (hba->scsi_host_added)
  9183. scsi_remove_host(hba->host);
  9184. out_disable:
  9185. hba->is_irq_enabled = false;
  9186. ufshcd_hba_exit(hba);
  9187. out_error:
  9188. return err;
  9189. }
  9190. EXPORT_SYMBOL_GPL(ufshcd_init);
  9191. void ufshcd_resume_complete(struct device *dev)
  9192. {
  9193. struct ufs_hba *hba = dev_get_drvdata(dev);
  9194. if (hba->complete_put) {
  9195. ufshcd_rpm_put(hba);
  9196. hba->complete_put = false;
  9197. }
  9198. }
  9199. EXPORT_SYMBOL_GPL(ufshcd_resume_complete);
  9200. static bool ufshcd_rpm_ok_for_spm(struct ufs_hba *hba)
  9201. {
  9202. struct device *dev = &hba->ufs_device_wlun->sdev_gendev;
  9203. enum ufs_dev_pwr_mode dev_pwr_mode;
  9204. enum uic_link_state link_state;
  9205. unsigned long flags;
  9206. bool res;
  9207. spin_lock_irqsave(&dev->power.lock, flags);
  9208. dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl);
  9209. link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl);
  9210. res = pm_runtime_suspended(dev) &&
  9211. hba->curr_dev_pwr_mode == dev_pwr_mode &&
  9212. hba->uic_link_state == link_state &&
  9213. !hba->dev_info.b_rpm_dev_flush_capable;
  9214. spin_unlock_irqrestore(&dev->power.lock, flags);
  9215. return res;
  9216. }
  9217. int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm)
  9218. {
  9219. struct ufs_hba *hba = dev_get_drvdata(dev);
  9220. int ret;
  9221. /*
  9222. * SCSI assumes that runtime-pm and system-pm for scsi drivers
  9223. * are same. And it doesn't wake up the device for system-suspend
  9224. * if it's runtime suspended. But ufs doesn't follow that.
  9225. * Refer ufshcd_resume_complete()
  9226. */
  9227. if (hba->ufs_device_wlun) {
  9228. /* Prevent runtime suspend */
  9229. ufshcd_rpm_get_noresume(hba);
  9230. /*
  9231. * Check if already runtime suspended in same state as system
  9232. * suspend would be.
  9233. */
  9234. if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) {
  9235. /* RPM state is not ok for SPM, so runtime resume */
  9236. ret = ufshcd_rpm_resume(hba);
  9237. if (ret < 0 && ret != -EACCES) {
  9238. ufshcd_rpm_put(hba);
  9239. return ret;
  9240. }
  9241. }
  9242. hba->complete_put = true;
  9243. }
  9244. return 0;
  9245. }
  9246. EXPORT_SYMBOL_GPL(__ufshcd_suspend_prepare);
  9247. int ufshcd_suspend_prepare(struct device *dev)
  9248. {
  9249. return __ufshcd_suspend_prepare(dev, true);
  9250. }
  9251. EXPORT_SYMBOL_GPL(ufshcd_suspend_prepare);
  9252. #ifdef CONFIG_PM_SLEEP
  9253. static int ufshcd_wl_poweroff(struct device *dev)
  9254. {
  9255. struct scsi_device *sdev = to_scsi_device(dev);
  9256. struct ufs_hba *hba = shost_priv(sdev->host);
  9257. __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM);
  9258. return 0;
  9259. }
  9260. #endif
  9261. static int ufshcd_wl_probe(struct device *dev)
  9262. {
  9263. struct scsi_device *sdev = to_scsi_device(dev);
  9264. if (!is_device_wlun(sdev))
  9265. return -ENODEV;
  9266. blk_pm_runtime_init(sdev->request_queue, dev);
  9267. pm_runtime_set_autosuspend_delay(dev, 0);
  9268. pm_runtime_allow(dev);
  9269. return 0;
  9270. }
  9271. static int ufshcd_wl_remove(struct device *dev)
  9272. {
  9273. pm_runtime_forbid(dev);
  9274. return 0;
  9275. }
  9276. static const struct dev_pm_ops ufshcd_wl_pm_ops = {
  9277. #ifdef CONFIG_PM_SLEEP
  9278. .suspend = ufshcd_wl_suspend,
  9279. .resume = ufshcd_wl_resume,
  9280. .freeze = ufshcd_wl_suspend,
  9281. .thaw = ufshcd_wl_resume,
  9282. .poweroff = ufshcd_wl_poweroff,
  9283. .restore = ufshcd_wl_resume,
  9284. #endif
  9285. SET_RUNTIME_PM_OPS(ufshcd_wl_runtime_suspend, ufshcd_wl_runtime_resume, NULL)
  9286. };
  9287. static void ufshcd_check_header_layout(void)
  9288. {
  9289. /*
  9290. * gcc compilers before version 10 cannot do constant-folding for
  9291. * sub-byte bitfields. Hence skip the layout checks for gcc 9 and
  9292. * before.
  9293. */
  9294. if (IS_ENABLED(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 100000)
  9295. return;
  9296. BUILD_BUG_ON(((u8 *)&(struct request_desc_header){
  9297. .cci = 3})[0] != 3);
  9298. BUILD_BUG_ON(((u8 *)&(struct request_desc_header){
  9299. .ehs_length = 2})[1] != 2);
  9300. BUILD_BUG_ON(((u8 *)&(struct request_desc_header){
  9301. .enable_crypto = 1})[2]
  9302. != 0x80);
  9303. BUILD_BUG_ON((((u8 *)&(struct request_desc_header){
  9304. .command_type = 5,
  9305. .data_direction = 3,
  9306. .interrupt = 1,
  9307. })[3]) != ((5 << 4) | (3 << 1) | 1));
  9308. BUILD_BUG_ON(((__le32 *)&(struct request_desc_header){
  9309. .dunl = cpu_to_le32(0xdeadbeef)})[1] !=
  9310. cpu_to_le32(0xdeadbeef));
  9311. BUILD_BUG_ON(((u8 *)&(struct request_desc_header){
  9312. .ocs = 4})[8] != 4);
  9313. BUILD_BUG_ON(((u8 *)&(struct request_desc_header){
  9314. .cds = 5})[9] != 5);
  9315. BUILD_BUG_ON(((__le32 *)&(struct request_desc_header){
  9316. .dunu = cpu_to_le32(0xbadcafe)})[3] !=
  9317. cpu_to_le32(0xbadcafe));
  9318. BUILD_BUG_ON(((u8 *)&(struct utp_upiu_header){
  9319. .iid = 0xf })[4] != 0xf0);
  9320. BUILD_BUG_ON(((u8 *)&(struct utp_upiu_header){
  9321. .command_set_type = 0xf })[4] != 0xf);
  9322. }
  9323. /*
  9324. * ufs_dev_wlun_template - describes ufs device wlun
  9325. * ufs-device wlun - used to send pm commands
  9326. * All luns are consumers of ufs-device wlun.
  9327. *
  9328. * Currently, no sd driver is present for wluns.
  9329. * Hence the no specific pm operations are performed.
  9330. * With ufs design, SSU should be sent to ufs-device wlun.
  9331. * Hence register a scsi driver for ufs wluns only.
  9332. */
  9333. static struct scsi_driver ufs_dev_wlun_template = {
  9334. .gendrv = {
  9335. .name = "ufs_device_wlun",
  9336. .probe = ufshcd_wl_probe,
  9337. .remove = ufshcd_wl_remove,
  9338. .pm = &ufshcd_wl_pm_ops,
  9339. .shutdown = ufshcd_wl_shutdown,
  9340. },
  9341. };
  9342. static int __init ufshcd_core_init(void)
  9343. {
  9344. int ret;
  9345. ufshcd_check_header_layout();
  9346. ufs_debugfs_init();
  9347. ret = scsi_register_driver(&ufs_dev_wlun_template.gendrv);
  9348. if (ret)
  9349. ufs_debugfs_exit();
  9350. return ret;
  9351. }
  9352. static void __exit ufshcd_core_exit(void)
  9353. {
  9354. ufs_debugfs_exit();
  9355. scsi_unregister_driver(&ufs_dev_wlun_template.gendrv);
  9356. }
  9357. module_init(ufshcd_core_init);
  9358. module_exit(ufshcd_core_exit);
  9359. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  9360. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  9361. MODULE_DESCRIPTION("Generic UFS host controller driver Core");
  9362. MODULE_SOFTDEP("pre: governor_simpleondemand");
  9363. MODULE_LICENSE("GPL");