12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242 |
- /*
- * QEMU NVM Express Controller
- *
- * Copyright (c) 2012, Intel Corporation
- *
- * Written by Keith Busch <keith.busch@intel.com>
- *
- * This code is licensed under the GNU GPL v2 or later.
- */
- /**
- * Reference Specs: http://www.nvmexpress.org, 1.4, 1.3, 1.2, 1.1, 1.0e
- *
- * https://nvmexpress.org/developers/nvme-specification/
- *
- *
- * Notes on coding style
- * ---------------------
- * While QEMU coding style prefers lowercase hexadecimals in constants, the
- * NVMe subsystem use this format from the NVMe specifications in the comments
- * (i.e. 'h' suffix instead of '0x' prefix).
- *
- * Usage
- * -----
- * See docs/system/nvme.rst for extensive documentation.
- *
- * Add options:
- * -drive file=<file>,if=none,id=<drive_id>
- * -device nvme-subsys,id=<subsys_id>,nqn=<nqn_id>
- * -device nvme,serial=<serial>,id=<bus_name>, \
- * cmb_size_mb=<cmb_size_mb[optional]>, \
- * [pmrdev=<mem_backend_file_id>,] \
- * max_ioqpairs=<N[optional]>, \
- * aerl=<N[optional]>,aer_max_queued=<N[optional]>, \
- * mdts=<N[optional]>,vsl=<N[optional]>, \
- * zoned.zasl=<N[optional]>, \
- * zoned.auto_transition=<on|off[optional]>, \
- * sriov_max_vfs=<N[optional]> \
- * sriov_vq_flexible=<N[optional]> \
- * sriov_vi_flexible=<N[optional]> \
- * sriov_max_vi_per_vf=<N[optional]> \
- * sriov_max_vq_per_vf=<N[optional]> \
- * atomic.dn=<on|off[optional]>, \
- * atomic.awun<N[optional]>, \
- * atomic.awupf<N[optional]>, \
- * subsys=<subsys_id>
- * -device nvme-ns,drive=<drive_id>,bus=<bus_name>,nsid=<nsid>,\
- * zoned=<true|false[optional]>, \
- * subsys=<subsys_id>,shared=<true|false[optional]>, \
- * detached=<true|false[optional]>, \
- * zoned.zone_size=<N[optional]>, \
- * zoned.zone_capacity=<N[optional]>, \
- * zoned.descr_ext_size=<N[optional]>, \
- * zoned.max_active=<N[optional]>, \
- * zoned.max_open=<N[optional]>, \
- * zoned.cross_read=<true|false[optional]>
- *
- * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
- * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. By default, the
- * device will use the "v1.4 CMB scheme" - use the `legacy-cmb` parameter to
- * always enable the CMBLOC and CMBSZ registers (v1.3 behavior).
- *
- * Enabling pmr emulation can be achieved by pointing to memory-backend-file.
- * For example:
- * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \
- * size=<size> .... -device nvme,...,pmrdev=<mem_id>
- *
- * The PMR will use BAR 4/5 exclusively.
- *
- * To place controller(s) and namespace(s) to a subsystem, then provide
- * nvme-subsys device as above.
- *
- * nvme subsystem device parameters
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * - `nqn`
- * This parameter provides the `<nqn_id>` part of the string
- * `nqn.2019-08.org.qemu:<nqn_id>` which will be reported in the SUBNQN field
- * of subsystem controllers. Note that `<nqn_id>` should be unique per
- * subsystem, but this is not enforced by QEMU. If not specified, it will
- * default to the value of the `id` parameter (`<subsys_id>`).
- *
- * nvme device parameters
- * ~~~~~~~~~~~~~~~~~~~~~~
- * - `subsys`
- * Specifying this parameter attaches the controller to the subsystem and
- * the SUBNQN field in the controller will report the NQN of the subsystem
- * device. This also enables multi controller capability represented in
- * Identify Controller data structure in CMIC (Controller Multi-path I/O and
- * Namespace Sharing Capabilities).
- *
- * - `aerl`
- * The Asynchronous Event Request Limit (AERL). Indicates the maximum number
- * of concurrently outstanding Asynchronous Event Request commands support
- * by the controller. This is a 0's based value.
- *
- * - `aer_max_queued`
- * This is the maximum number of events that the device will enqueue for
- * completion when there are no outstanding AERs. When the maximum number of
- * enqueued events are reached, subsequent events will be dropped.
- *
- * - `mdts`
- * Indicates the maximum data transfer size for a command that transfers data
- * between host-accessible memory and the controller. The value is specified
- * as a power of two (2^n) and is in units of the minimum memory page size
- * (CAP.MPSMIN). The default value is 7 (i.e. 512 KiB).
- *
- * - `vsl`
- * Indicates the maximum data size limit for the Verify command. Like `mdts`,
- * this value is specified as a power of two (2^n) and is in units of the
- * minimum memory page size (CAP.MPSMIN). The default value is 7 (i.e. 512
- * KiB).
- *
- * - `zoned.zasl`
- * Indicates the maximum data transfer size for the Zone Append command. Like
- * `mdts`, the value is specified as a power of two (2^n) and is in units of
- * the minimum memory page size (CAP.MPSMIN). The default value is 0 (i.e.
- * defaulting to the value of `mdts`).
- *
- * - `zoned.auto_transition`
- * Indicates if zones in zone state implicitly opened can be automatically
- * transitioned to zone state closed for resource management purposes.
- * Defaults to 'on'.
- *
- * - `sriov_max_vfs`
- * Indicates the maximum number of PCIe virtual functions supported
- * by the controller. The default value is 0. Specifying a non-zero value
- * enables reporting of both SR-IOV and ARI capabilities by the NVMe device.
- * Virtual function controllers will not report SR-IOV capability.
- *
- * NOTE: Single Root I/O Virtualization support is experimental.
- * All the related parameters may be subject to change.
- *
- * - `sriov_vq_flexible`
- * Indicates the total number of flexible queue resources assignable to all
- * the secondary controllers. Implicitly sets the number of primary
- * controller's private resources to `(max_ioqpairs - sriov_vq_flexible)`.
- *
- * - `sriov_vi_flexible`
- * Indicates the total number of flexible interrupt resources assignable to
- * all the secondary controllers. Implicitly sets the number of primary
- * controller's private resources to `(msix_qsize - sriov_vi_flexible)`.
- *
- * - `sriov_max_vi_per_vf`
- * Indicates the maximum number of virtual interrupt resources assignable
- * to a secondary controller. The default 0 resolves to
- * `(sriov_vi_flexible / sriov_max_vfs)`.
- *
- * - `sriov_max_vq_per_vf`
- * Indicates the maximum number of virtual queue resources assignable to
- * a secondary controller. The default 0 resolves to
- * `(sriov_vq_flexible / sriov_max_vfs)`.
- *
- * nvme namespace device parameters
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * - `shared`
- * When the parent nvme device (as defined explicitly by the 'bus' parameter
- * or implicitly by the most recently defined NvmeBus) is linked to an
- * nvme-subsys device, the namespace will be attached to all controllers in
- * the subsystem. If set to 'off' (the default), the namespace will remain a
- * private namespace and may only be attached to a single controller at a
- * time.
- *
- * - `detached`
- * This parameter is only valid together with the `subsys` parameter. If left
- * at the default value (`false/off`), the namespace will be attached to all
- * controllers in the NVMe subsystem at boot-up. If set to `true/on`, the
- * namespace will be available in the subsystem but not attached to any
- * controllers.
- *
- * Setting `zoned` to true selects Zoned Command Set at the namespace.
- * In this case, the following namespace properties are available to configure
- * zoned operation:
- * zoned.zone_size=<zone size in bytes, default: 128MiB>
- * The number may be followed by K, M, G as in kilo-, mega- or giga-.
- *
- * zoned.zone_capacity=<zone capacity in bytes, default: zone size>
- * The value 0 (default) forces zone capacity to be the same as zone
- * size. The value of this property may not exceed zone size.
- *
- * zoned.descr_ext_size=<zone descriptor extension size, default 0>
- * This value needs to be specified in 64B units. If it is zero,
- * namespace(s) will not support zone descriptor extensions.
- *
- * zoned.max_active=<Maximum Active Resources (zones), default: 0>
- * The default value means there is no limit to the number of
- * concurrently active zones.
- *
- * zoned.max_open=<Maximum Open Resources (zones), default: 0>
- * The default value means there is no limit to the number of
- * concurrently open zones.
- *
- * zoned.cross_read=<enable RAZB, default: false>
- * Setting this property to true enables Read Across Zone Boundaries.
- */
- #include "qemu/osdep.h"
- #include "qemu/cutils.h"
- #include "qemu/error-report.h"
- #include "qemu/log.h"
- #include "qemu/units.h"
- #include "qemu/range.h"
- #include "qapi/error.h"
- #include "qapi/visitor.h"
- #include "system/system.h"
- #include "system/block-backend.h"
- #include "system/hostmem.h"
- #include "hw/pci/msix.h"
- #include "hw/pci/pcie_sriov.h"
- #include "system/spdm-socket.h"
- #include "migration/vmstate.h"
- #include "nvme.h"
- #include "dif.h"
- #include "trace.h"
- #define NVME_MAX_IOQPAIRS 0xffff
- #define NVME_DB_SIZE 4
- #define NVME_SPEC_VER 0x00010400
- #define NVME_CMB_BIR 2
- #define NVME_PMR_BIR 4
- #define NVME_TEMPERATURE 0x143
- #define NVME_TEMPERATURE_WARNING 0x157
- #define NVME_TEMPERATURE_CRITICAL 0x175
- #define NVME_NUM_FW_SLOTS 1
- #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
- #define NVME_VF_RES_GRANULARITY 1
- #define NVME_VF_OFFSET 0x1
- #define NVME_VF_STRIDE 1
- #define NVME_GUEST_ERR(trace, fmt, ...) \
- do { \
- (trace_##trace)(__VA_ARGS__); \
- qemu_log_mask(LOG_GUEST_ERROR, #trace \
- " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
- } while (0)
- static const bool nvme_feature_support[NVME_FID_MAX] = {
- [NVME_ARBITRATION] = true,
- [NVME_POWER_MANAGEMENT] = true,
- [NVME_TEMPERATURE_THRESHOLD] = true,
- [NVME_ERROR_RECOVERY] = true,
- [NVME_VOLATILE_WRITE_CACHE] = true,
- [NVME_NUMBER_OF_QUEUES] = true,
- [NVME_INTERRUPT_COALESCING] = true,
- [NVME_INTERRUPT_VECTOR_CONF] = true,
- [NVME_WRITE_ATOMICITY] = true,
- [NVME_ASYNCHRONOUS_EVENT_CONF] = true,
- [NVME_TIMESTAMP] = true,
- [NVME_HOST_BEHAVIOR_SUPPORT] = true,
- [NVME_COMMAND_SET_PROFILE] = true,
- [NVME_FDP_MODE] = true,
- [NVME_FDP_EVENTS] = true,
- };
- static const uint32_t nvme_feature_cap[NVME_FID_MAX] = {
- [NVME_TEMPERATURE_THRESHOLD] = NVME_FEAT_CAP_CHANGE,
- [NVME_ERROR_RECOVERY] = NVME_FEAT_CAP_CHANGE | NVME_FEAT_CAP_NS,
- [NVME_VOLATILE_WRITE_CACHE] = NVME_FEAT_CAP_CHANGE,
- [NVME_NUMBER_OF_QUEUES] = NVME_FEAT_CAP_CHANGE,
- [NVME_WRITE_ATOMICITY] = NVME_FEAT_CAP_CHANGE,
- [NVME_ASYNCHRONOUS_EVENT_CONF] = NVME_FEAT_CAP_CHANGE,
- [NVME_TIMESTAMP] = NVME_FEAT_CAP_CHANGE,
- [NVME_HOST_BEHAVIOR_SUPPORT] = NVME_FEAT_CAP_CHANGE,
- [NVME_COMMAND_SET_PROFILE] = NVME_FEAT_CAP_CHANGE,
- [NVME_FDP_MODE] = NVME_FEAT_CAP_CHANGE,
- [NVME_FDP_EVENTS] = NVME_FEAT_CAP_CHANGE | NVME_FEAT_CAP_NS,
- };
- static const uint32_t nvme_cse_acs_default[256] = {
- [NVME_ADM_CMD_DELETE_SQ] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_CREATE_SQ] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_GET_LOG_PAGE] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_DELETE_CQ] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_CREATE_CQ] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_IDENTIFY] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_ABORT] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_SET_FEATURES] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_GET_FEATURES] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_ASYNC_EV_REQ] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_NS_ATTACHMENT] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_NIC |
- NVME_CMD_EFF_CCC,
- [NVME_ADM_CMD_FORMAT_NVM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_ADM_CMD_DIRECTIVE_RECV] = NVME_CMD_EFF_CSUPP,
- [NVME_ADM_CMD_DIRECTIVE_SEND] = NVME_CMD_EFF_CSUPP,
- };
- static const uint32_t nvme_cse_iocs_nvm_default[256] = {
- [NVME_CMD_FLUSH] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_WRITE_ZEROES] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_WRITE] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_READ] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_DSM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_VERIFY] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_COPY] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_COMPARE] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_IO_MGMT_RECV] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_IO_MGMT_SEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- };
- static const uint32_t nvme_cse_iocs_zoned_default[256] = {
- [NVME_CMD_FLUSH] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_WRITE_ZEROES] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_WRITE] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_READ] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_DSM] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_VERIFY] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_COPY] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_COMPARE] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_IO_MGMT_RECV] = NVME_CMD_EFF_CSUPP,
- [NVME_CMD_IO_MGMT_SEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_ZONE_APPEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_ZONE_MGMT_SEND] = NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_LBCC,
- [NVME_CMD_ZONE_MGMT_RECV] = NVME_CMD_EFF_CSUPP,
- };
- static void nvme_process_sq(void *opaque);
- static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst);
- static inline uint64_t nvme_get_timestamp(const NvmeCtrl *n);
- static uint16_t nvme_sqid(NvmeRequest *req)
- {
- return le16_to_cpu(req->sq->sqid);
- }
- static inline uint16_t nvme_make_pid(NvmeNamespace *ns, uint16_t rg,
- uint16_t ph)
- {
- uint16_t rgif = ns->endgrp->fdp.rgif;
- if (!rgif) {
- return ph;
- }
- return (rg << (16 - rgif)) | ph;
- }
- static inline bool nvme_ph_valid(NvmeNamespace *ns, uint16_t ph)
- {
- return ph < ns->fdp.nphs;
- }
- static inline bool nvme_rg_valid(NvmeEnduranceGroup *endgrp, uint16_t rg)
- {
- return rg < endgrp->fdp.nrg;
- }
- static inline uint16_t nvme_pid2ph(NvmeNamespace *ns, uint16_t pid)
- {
- uint16_t rgif = ns->endgrp->fdp.rgif;
- if (!rgif) {
- return pid;
- }
- return pid & ((1 << (15 - rgif)) - 1);
- }
- static inline uint16_t nvme_pid2rg(NvmeNamespace *ns, uint16_t pid)
- {
- uint16_t rgif = ns->endgrp->fdp.rgif;
- if (!rgif) {
- return 0;
- }
- return pid >> (16 - rgif);
- }
- static inline bool nvme_parse_pid(NvmeNamespace *ns, uint16_t pid,
- uint16_t *ph, uint16_t *rg)
- {
- *rg = nvme_pid2rg(ns, pid);
- *ph = nvme_pid2ph(ns, pid);
- return nvme_ph_valid(ns, *ph) && nvme_rg_valid(ns->endgrp, *rg);
- }
- static void nvme_assign_zone_state(NvmeNamespace *ns, NvmeZone *zone,
- NvmeZoneState state)
- {
- if (QTAILQ_IN_USE(zone, entry)) {
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- QTAILQ_REMOVE(&ns->exp_open_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_CLOSED:
- QTAILQ_REMOVE(&ns->closed_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_FULL:
- QTAILQ_REMOVE(&ns->full_zones, zone, entry);
- default:
- ;
- }
- }
- nvme_set_zone_state(zone, state);
- switch (state) {
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- QTAILQ_INSERT_TAIL(&ns->exp_open_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- QTAILQ_INSERT_TAIL(&ns->imp_open_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_CLOSED:
- QTAILQ_INSERT_TAIL(&ns->closed_zones, zone, entry);
- break;
- case NVME_ZONE_STATE_FULL:
- QTAILQ_INSERT_TAIL(&ns->full_zones, zone, entry);
- case NVME_ZONE_STATE_READ_ONLY:
- break;
- default:
- zone->d.za = 0;
- }
- }
- static uint16_t nvme_zns_check_resources(NvmeNamespace *ns, uint32_t act,
- uint32_t opn, uint32_t zrwa)
- {
- if (ns->params.max_active_zones != 0 &&
- ns->nr_active_zones + act > ns->params.max_active_zones) {
- trace_pci_nvme_err_insuff_active_res(ns->params.max_active_zones);
- return NVME_ZONE_TOO_MANY_ACTIVE | NVME_DNR;
- }
- if (ns->params.max_open_zones != 0 &&
- ns->nr_open_zones + opn > ns->params.max_open_zones) {
- trace_pci_nvme_err_insuff_open_res(ns->params.max_open_zones);
- return NVME_ZONE_TOO_MANY_OPEN | NVME_DNR;
- }
- if (zrwa > ns->zns.numzrwa) {
- return NVME_NOZRWA | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- /*
- * Check if we can open a zone without exceeding open/active limits.
- * AOR stands for "Active and Open Resources" (see TP 4053 section 2.5).
- */
- static uint16_t nvme_aor_check(NvmeNamespace *ns, uint32_t act, uint32_t opn)
- {
- return nvme_zns_check_resources(ns, act, opn, 0);
- }
- static NvmeFdpEvent *nvme_fdp_alloc_event(NvmeCtrl *n, NvmeFdpEventBuffer *ebuf)
- {
- NvmeFdpEvent *ret = NULL;
- bool is_full = ebuf->next == ebuf->start && ebuf->nelems;
- ret = &ebuf->events[ebuf->next++];
- if (unlikely(ebuf->next == NVME_FDP_MAX_EVENTS)) {
- ebuf->next = 0;
- }
- if (is_full) {
- ebuf->start = ebuf->next;
- } else {
- ebuf->nelems++;
- }
- memset(ret, 0, sizeof(NvmeFdpEvent));
- ret->timestamp = nvme_get_timestamp(n);
- return ret;
- }
- static inline int log_event(NvmeRuHandle *ruh, uint8_t event_type)
- {
- return (ruh->event_filter >> nvme_fdp_evf_shifts[event_type]) & 0x1;
- }
- static bool nvme_update_ruh(NvmeCtrl *n, NvmeNamespace *ns, uint16_t pid)
- {
- NvmeEnduranceGroup *endgrp = ns->endgrp;
- NvmeRuHandle *ruh;
- NvmeReclaimUnit *ru;
- NvmeFdpEvent *e = NULL;
- uint16_t ph, rg, ruhid;
- if (!nvme_parse_pid(ns, pid, &ph, &rg)) {
- return false;
- }
- ruhid = ns->fdp.phs[ph];
- ruh = &endgrp->fdp.ruhs[ruhid];
- ru = &ruh->rus[rg];
- if (ru->ruamw) {
- if (log_event(ruh, FDP_EVT_RU_NOT_FULLY_WRITTEN)) {
- e = nvme_fdp_alloc_event(n, &endgrp->fdp.host_events);
- e->type = FDP_EVT_RU_NOT_FULLY_WRITTEN;
- e->flags = FDPEF_PIV | FDPEF_NSIDV | FDPEF_LV;
- e->pid = cpu_to_le16(pid);
- e->nsid = cpu_to_le32(ns->params.nsid);
- e->rgid = cpu_to_le16(rg);
- e->ruhid = cpu_to_le16(ruhid);
- }
- /* log (eventual) GC overhead of prematurely swapping the RU */
- nvme_fdp_stat_inc(&endgrp->fdp.mbmw, nvme_l2b(ns, ru->ruamw));
- }
- ru->ruamw = ruh->ruamw;
- return true;
- }
- static bool nvme_addr_is_cmb(NvmeCtrl *n, hwaddr addr)
- {
- hwaddr hi, lo;
- if (!n->cmb.cmse) {
- return false;
- }
- lo = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba;
- hi = lo + int128_get64(n->cmb.mem.size);
- return addr >= lo && addr < hi;
- }
- static inline void *nvme_addr_to_cmb(NvmeCtrl *n, hwaddr addr)
- {
- hwaddr base = n->params.legacy_cmb ? n->cmb.mem.addr : n->cmb.cba;
- return &n->cmb.buf[addr - base];
- }
- static bool nvme_addr_is_pmr(NvmeCtrl *n, hwaddr addr)
- {
- hwaddr hi;
- if (!n->pmr.cmse) {
- return false;
- }
- hi = n->pmr.cba + int128_get64(n->pmr.dev->mr.size);
- return addr >= n->pmr.cba && addr < hi;
- }
- static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr)
- {
- return memory_region_get_ram_ptr(&n->pmr.dev->mr) + (addr - n->pmr.cba);
- }
- static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr)
- {
- hwaddr hi, lo;
- /*
- * The purpose of this check is to guard against invalid "local" access to
- * the iomem (i.e. controller registers). Thus, we check against the range
- * covered by the 'bar0' MemoryRegion since that is currently composed of
- * two subregions (the NVMe "MBAR" and the MSI-X table/pba). Note, however,
- * that if the device model is ever changed to allow the CMB to be located
- * in BAR0 as well, then this must be changed.
- */
- lo = n->bar0.addr;
- hi = lo + int128_get64(n->bar0.size);
- return addr >= lo && addr < hi;
- }
- static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
- {
- hwaddr hi = addr + size - 1;
- if (hi < addr) {
- return 1;
- }
- if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) {
- memcpy(buf, nvme_addr_to_cmb(n, addr), size);
- return 0;
- }
- if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) {
- memcpy(buf, nvme_addr_to_pmr(n, addr), size);
- return 0;
- }
- return pci_dma_read(PCI_DEVICE(n), addr, buf, size);
- }
- static int nvme_addr_write(NvmeCtrl *n, hwaddr addr, const void *buf, int size)
- {
- hwaddr hi = addr + size - 1;
- if (hi < addr) {
- return 1;
- }
- if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) {
- memcpy(nvme_addr_to_cmb(n, addr), buf, size);
- return 0;
- }
- if (nvme_addr_is_pmr(n, addr) && nvme_addr_is_pmr(n, hi)) {
- memcpy(nvme_addr_to_pmr(n, addr), buf, size);
- return 0;
- }
- return pci_dma_write(PCI_DEVICE(n), addr, buf, size);
- }
- static bool nvme_nsid_valid(NvmeCtrl *n, uint32_t nsid)
- {
- return nsid &&
- (nsid == NVME_NSID_BROADCAST || nsid <= NVME_MAX_NAMESPACES);
- }
- static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid)
- {
- return sqid < n->conf_ioqpairs + 1 && n->sq[sqid] != NULL ? 0 : -1;
- }
- static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid)
- {
- return cqid < n->conf_ioqpairs + 1 && n->cq[cqid] != NULL ? 0 : -1;
- }
- static void nvme_inc_cq_tail(NvmeCQueue *cq)
- {
- cq->tail++;
- if (cq->tail >= cq->size) {
- cq->tail = 0;
- cq->phase = !cq->phase;
- }
- }
- static void nvme_inc_sq_head(NvmeSQueue *sq)
- {
- sq->head = (sq->head + 1) % sq->size;
- }
- static uint8_t nvme_cq_full(NvmeCQueue *cq)
- {
- return (cq->tail + 1) % cq->size == cq->head;
- }
- static uint8_t nvme_sq_empty(NvmeSQueue *sq)
- {
- return sq->head == sq->tail;
- }
- static void nvme_irq_check(NvmeCtrl *n)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- uint32_t intms = ldl_le_p(&n->bar.intms);
- if (msix_enabled(pci)) {
- return;
- }
- /* vfs does not implement intx */
- if (pci_is_vf(pci)) {
- return;
- }
- if (~intms & n->irq_status) {
- pci_irq_assert(pci);
- } else {
- pci_irq_deassert(pci);
- }
- }
- static void nvme_irq_assert(NvmeCtrl *n, NvmeCQueue *cq)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- if (cq->irq_enabled) {
- if (msix_enabled(pci)) {
- trace_pci_nvme_irq_msix(cq->vector);
- msix_notify(pci, cq->vector);
- } else {
- trace_pci_nvme_irq_pin();
- assert(cq->vector < 32);
- n->irq_status |= 1 << cq->vector;
- nvme_irq_check(n);
- }
- } else {
- trace_pci_nvme_irq_masked();
- }
- }
- static void nvme_irq_deassert(NvmeCtrl *n, NvmeCQueue *cq)
- {
- if (cq->irq_enabled) {
- if (msix_enabled(PCI_DEVICE(n))) {
- return;
- } else {
- assert(cq->vector < 32);
- if (!n->cq_pending) {
- n->irq_status &= ~(1 << cq->vector);
- }
- nvme_irq_check(n);
- }
- }
- }
- static void nvme_req_clear(NvmeRequest *req)
- {
- req->ns = NULL;
- req->opaque = NULL;
- req->aiocb = NULL;
- memset(&req->cqe, 0x0, sizeof(req->cqe));
- req->status = NVME_SUCCESS;
- }
- static inline void nvme_sg_init(NvmeCtrl *n, NvmeSg *sg, bool dma)
- {
- if (dma) {
- pci_dma_sglist_init(&sg->qsg, PCI_DEVICE(n), 0);
- sg->flags = NVME_SG_DMA;
- } else {
- qemu_iovec_init(&sg->iov, 0);
- }
- sg->flags |= NVME_SG_ALLOC;
- }
- static inline void nvme_sg_unmap(NvmeSg *sg)
- {
- if (!(sg->flags & NVME_SG_ALLOC)) {
- return;
- }
- if (sg->flags & NVME_SG_DMA) {
- qemu_sglist_destroy(&sg->qsg);
- } else {
- qemu_iovec_destroy(&sg->iov);
- }
- memset(sg, 0x0, sizeof(*sg));
- }
- /*
- * When metadata is transferred as extended LBAs, the DPTR mapped into `sg`
- * holds both data and metadata. This function splits the data and metadata
- * into two separate QSG/IOVs.
- */
- static void nvme_sg_split(NvmeSg *sg, NvmeNamespace *ns, NvmeSg *data,
- NvmeSg *mdata)
- {
- NvmeSg *dst = data;
- uint32_t trans_len, count = ns->lbasz;
- uint64_t offset = 0;
- bool dma = sg->flags & NVME_SG_DMA;
- size_t sge_len;
- size_t sg_len = dma ? sg->qsg.size : sg->iov.size;
- int sg_idx = 0;
- assert(sg->flags & NVME_SG_ALLOC);
- while (sg_len) {
- sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len;
- trans_len = MIN(sg_len, count);
- trans_len = MIN(trans_len, sge_len - offset);
- if (dst) {
- if (dma) {
- qemu_sglist_add(&dst->qsg, sg->qsg.sg[sg_idx].base + offset,
- trans_len);
- } else {
- qemu_iovec_add(&dst->iov,
- sg->iov.iov[sg_idx].iov_base + offset,
- trans_len);
- }
- }
- sg_len -= trans_len;
- count -= trans_len;
- offset += trans_len;
- if (count == 0) {
- dst = (dst == data) ? mdata : data;
- count = (dst == data) ? ns->lbasz : ns->lbaf.ms;
- }
- if (sge_len == offset) {
- offset = 0;
- sg_idx++;
- }
- }
- }
- static uint16_t nvme_map_addr_cmb(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr,
- size_t len)
- {
- if (!len) {
- return NVME_SUCCESS;
- }
- trace_pci_nvme_map_addr_cmb(addr, len);
- if (!nvme_addr_is_cmb(n, addr) || !nvme_addr_is_cmb(n, addr + len - 1)) {
- return NVME_DATA_TRAS_ERROR;
- }
- qemu_iovec_add(iov, nvme_addr_to_cmb(n, addr), len);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_map_addr_pmr(NvmeCtrl *n, QEMUIOVector *iov, hwaddr addr,
- size_t len)
- {
- if (!len) {
- return NVME_SUCCESS;
- }
- if (!nvme_addr_is_pmr(n, addr) || !nvme_addr_is_pmr(n, addr + len - 1)) {
- return NVME_DATA_TRAS_ERROR;
- }
- qemu_iovec_add(iov, nvme_addr_to_pmr(n, addr), len);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_map_addr(NvmeCtrl *n, NvmeSg *sg, hwaddr addr, size_t len)
- {
- bool cmb = false, pmr = false;
- if (!len) {
- return NVME_SUCCESS;
- }
- trace_pci_nvme_map_addr(addr, len);
- if (nvme_addr_is_iomem(n, addr)) {
- return NVME_DATA_TRAS_ERROR;
- }
- if (nvme_addr_is_cmb(n, addr)) {
- cmb = true;
- } else if (nvme_addr_is_pmr(n, addr)) {
- pmr = true;
- }
- if (cmb || pmr) {
- if (sg->flags & NVME_SG_DMA) {
- return NVME_INVALID_USE_OF_CMB | NVME_DNR;
- }
- if (sg->iov.niov + 1 > IOV_MAX) {
- goto max_mappings_exceeded;
- }
- if (cmb) {
- return nvme_map_addr_cmb(n, &sg->iov, addr, len);
- } else {
- return nvme_map_addr_pmr(n, &sg->iov, addr, len);
- }
- }
- if (!(sg->flags & NVME_SG_DMA)) {
- return NVME_INVALID_USE_OF_CMB | NVME_DNR;
- }
- if (sg->qsg.nsg + 1 > IOV_MAX) {
- goto max_mappings_exceeded;
- }
- qemu_sglist_add(&sg->qsg, addr, len);
- return NVME_SUCCESS;
- max_mappings_exceeded:
- NVME_GUEST_ERR(pci_nvme_ub_too_many_mappings,
- "number of mappings exceed 1024");
- return NVME_INTERNAL_DEV_ERROR | NVME_DNR;
- }
- static inline bool nvme_addr_is_dma(NvmeCtrl *n, hwaddr addr)
- {
- return !(nvme_addr_is_cmb(n, addr) || nvme_addr_is_pmr(n, addr));
- }
- static uint16_t nvme_map_prp(NvmeCtrl *n, NvmeSg *sg, uint64_t prp1,
- uint64_t prp2, uint32_t len)
- {
- hwaddr trans_len = n->page_size - (prp1 % n->page_size);
- trans_len = MIN(len, trans_len);
- int num_prps = (len >> n->page_bits) + 1;
- uint16_t status;
- int ret;
- trace_pci_nvme_map_prp(trans_len, len, prp1, prp2, num_prps);
- nvme_sg_init(n, sg, nvme_addr_is_dma(n, prp1));
- status = nvme_map_addr(n, sg, prp1, trans_len);
- if (status) {
- goto unmap;
- }
- len -= trans_len;
- if (len) {
- if (len > n->page_size) {
- g_autofree uint64_t *prp_list = g_new(uint64_t, n->max_prp_ents);
- uint32_t nents, prp_trans;
- int i = 0;
- /*
- * The first PRP list entry, pointed to by PRP2 may contain offset.
- * Hence, we need to calculate the number of entries in based on
- * that offset.
- */
- nents = (n->page_size - (prp2 & (n->page_size - 1))) >> 3;
- prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
- ret = nvme_addr_read(n, prp2, (void *)prp_list, prp_trans);
- if (ret) {
- trace_pci_nvme_err_addr_read(prp2);
- status = NVME_DATA_TRAS_ERROR;
- goto unmap;
- }
- while (len != 0) {
- uint64_t prp_ent = le64_to_cpu(prp_list[i]);
- if (i == nents - 1 && len > n->page_size) {
- if (unlikely(prp_ent & (n->page_size - 1))) {
- trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
- status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
- goto unmap;
- }
- i = 0;
- nents = (len + n->page_size - 1) >> n->page_bits;
- nents = MIN(nents, n->max_prp_ents);
- prp_trans = nents * sizeof(uint64_t);
- ret = nvme_addr_read(n, prp_ent, (void *)prp_list,
- prp_trans);
- if (ret) {
- trace_pci_nvme_err_addr_read(prp_ent);
- status = NVME_DATA_TRAS_ERROR;
- goto unmap;
- }
- prp_ent = le64_to_cpu(prp_list[i]);
- }
- if (unlikely(prp_ent & (n->page_size - 1))) {
- trace_pci_nvme_err_invalid_prplist_ent(prp_ent);
- status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
- goto unmap;
- }
- trans_len = MIN(len, n->page_size);
- status = nvme_map_addr(n, sg, prp_ent, trans_len);
- if (status) {
- goto unmap;
- }
- len -= trans_len;
- i++;
- }
- } else {
- if (unlikely(prp2 & (n->page_size - 1))) {
- trace_pci_nvme_err_invalid_prp2_align(prp2);
- status = NVME_INVALID_PRP_OFFSET | NVME_DNR;
- goto unmap;
- }
- status = nvme_map_addr(n, sg, prp2, len);
- if (status) {
- goto unmap;
- }
- }
- }
- return NVME_SUCCESS;
- unmap:
- nvme_sg_unmap(sg);
- return status;
- }
- /*
- * Map 'nsgld' data descriptors from 'segment'. The function will subtract the
- * number of bytes mapped in len.
- */
- static uint16_t nvme_map_sgl_data(NvmeCtrl *n, NvmeSg *sg,
- NvmeSglDescriptor *segment, uint64_t nsgld,
- size_t *len, NvmeCmd *cmd)
- {
- dma_addr_t addr, trans_len;
- uint32_t dlen;
- uint16_t status;
- for (int i = 0; i < nsgld; i++) {
- uint8_t type = NVME_SGL_TYPE(segment[i].type);
- switch (type) {
- case NVME_SGL_DESCR_TYPE_DATA_BLOCK:
- break;
- case NVME_SGL_DESCR_TYPE_SEGMENT:
- case NVME_SGL_DESCR_TYPE_LAST_SEGMENT:
- return NVME_INVALID_NUM_SGL_DESCRS | NVME_DNR;
- default:
- return NVME_SGL_DESCR_TYPE_INVALID | NVME_DNR;
- }
- dlen = le32_to_cpu(segment[i].len);
- if (!dlen) {
- continue;
- }
- if (*len == 0) {
- /*
- * All data has been mapped, but the SGL contains additional
- * segments and/or descriptors. The controller might accept
- * ignoring the rest of the SGL.
- */
- uint32_t sgls = le32_to_cpu(n->id_ctrl.sgls);
- if (sgls & NVME_CTRL_SGLS_EXCESS_LENGTH) {
- break;
- }
- trace_pci_nvme_err_invalid_sgl_excess_length(dlen);
- return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
- }
- trans_len = MIN(*len, dlen);
- addr = le64_to_cpu(segment[i].addr);
- if (UINT64_MAX - addr < dlen) {
- return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
- }
- status = nvme_map_addr(n, sg, addr, trans_len);
- if (status) {
- return status;
- }
- *len -= trans_len;
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_map_sgl(NvmeCtrl *n, NvmeSg *sg, NvmeSglDescriptor sgl,
- size_t len, NvmeCmd *cmd)
- {
- /*
- * Read the segment in chunks of 256 descriptors (one 4k page) to avoid
- * dynamically allocating a potentially huge SGL. The spec allows the SGL
- * to be larger (as in number of bytes required to describe the SGL
- * descriptors and segment chain) than the command transfer size, so it is
- * not bounded by MDTS.
- */
- #define SEG_CHUNK_SIZE 256
- NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld;
- uint64_t nsgld;
- uint32_t seg_len;
- uint16_t status;
- hwaddr addr;
- int ret;
- sgld = &sgl;
- addr = le64_to_cpu(sgl.addr);
- trace_pci_nvme_map_sgl(NVME_SGL_TYPE(sgl.type), len);
- nvme_sg_init(n, sg, nvme_addr_is_dma(n, addr));
- /*
- * If the entire transfer can be described with a single data block it can
- * be mapped directly.
- */
- if (NVME_SGL_TYPE(sgl.type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) {
- status = nvme_map_sgl_data(n, sg, sgld, 1, &len, cmd);
- if (status) {
- goto unmap;
- }
- goto out;
- }
- for (;;) {
- switch (NVME_SGL_TYPE(sgld->type)) {
- case NVME_SGL_DESCR_TYPE_SEGMENT:
- case NVME_SGL_DESCR_TYPE_LAST_SEGMENT:
- break;
- default:
- return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
- }
- seg_len = le32_to_cpu(sgld->len);
- /* check the length of the (Last) Segment descriptor */
- if (!seg_len || seg_len & 0xf) {
- return NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
- }
- if (UINT64_MAX - addr < seg_len) {
- return NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
- }
- nsgld = seg_len / sizeof(NvmeSglDescriptor);
- while (nsgld > SEG_CHUNK_SIZE) {
- if (nvme_addr_read(n, addr, segment, sizeof(segment))) {
- trace_pci_nvme_err_addr_read(addr);
- status = NVME_DATA_TRAS_ERROR;
- goto unmap;
- }
- status = nvme_map_sgl_data(n, sg, segment, SEG_CHUNK_SIZE,
- &len, cmd);
- if (status) {
- goto unmap;
- }
- nsgld -= SEG_CHUNK_SIZE;
- addr += SEG_CHUNK_SIZE * sizeof(NvmeSglDescriptor);
- }
- ret = nvme_addr_read(n, addr, segment, nsgld *
- sizeof(NvmeSglDescriptor));
- if (ret) {
- trace_pci_nvme_err_addr_read(addr);
- status = NVME_DATA_TRAS_ERROR;
- goto unmap;
- }
- last_sgld = &segment[nsgld - 1];
- /*
- * If the segment ends with a Data Block, then we are done.
- */
- if (NVME_SGL_TYPE(last_sgld->type) == NVME_SGL_DESCR_TYPE_DATA_BLOCK) {
- status = nvme_map_sgl_data(n, sg, segment, nsgld, &len, cmd);
- if (status) {
- goto unmap;
- }
- goto out;
- }
- /*
- * If the last descriptor was not a Data Block, then the current
- * segment must not be a Last Segment.
- */
- if (NVME_SGL_TYPE(sgld->type) == NVME_SGL_DESCR_TYPE_LAST_SEGMENT) {
- status = NVME_INVALID_SGL_SEG_DESCR | NVME_DNR;
- goto unmap;
- }
- sgld = last_sgld;
- addr = le64_to_cpu(sgld->addr);
- /*
- * Do not map the last descriptor; it will be a Segment or Last Segment
- * descriptor and is handled by the next iteration.
- */
- status = nvme_map_sgl_data(n, sg, segment, nsgld - 1, &len, cmd);
- if (status) {
- goto unmap;
- }
- }
- out:
- /* if there is any residual left in len, the SGL was too short */
- if (len) {
- status = NVME_DATA_SGL_LEN_INVALID | NVME_DNR;
- goto unmap;
- }
- return NVME_SUCCESS;
- unmap:
- nvme_sg_unmap(sg);
- return status;
- }
- uint16_t nvme_map_dptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
- NvmeCmd *cmd)
- {
- uint64_t prp1, prp2;
- switch (NVME_CMD_FLAGS_PSDT(cmd->flags)) {
- case NVME_PSDT_PRP:
- prp1 = le64_to_cpu(cmd->dptr.prp1);
- prp2 = le64_to_cpu(cmd->dptr.prp2);
- return nvme_map_prp(n, sg, prp1, prp2, len);
- case NVME_PSDT_SGL_MPTR_CONTIGUOUS:
- case NVME_PSDT_SGL_MPTR_SGL:
- return nvme_map_sgl(n, sg, cmd->dptr.sgl, len, cmd);
- default:
- return NVME_INVALID_FIELD;
- }
- }
- static uint16_t nvme_map_mptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
- NvmeCmd *cmd)
- {
- int psdt = NVME_CMD_FLAGS_PSDT(cmd->flags);
- hwaddr mptr = le64_to_cpu(cmd->mptr);
- uint16_t status;
- if (psdt == NVME_PSDT_SGL_MPTR_SGL) {
- NvmeSglDescriptor sgl;
- if (nvme_addr_read(n, mptr, &sgl, sizeof(sgl))) {
- return NVME_DATA_TRAS_ERROR;
- }
- status = nvme_map_sgl(n, sg, sgl, len, cmd);
- if (status && (status & 0x7ff) == NVME_DATA_SGL_LEN_INVALID) {
- status = NVME_MD_SGL_LEN_INVALID | NVME_DNR;
- }
- return status;
- }
- nvme_sg_init(n, sg, nvme_addr_is_dma(n, mptr));
- status = nvme_map_addr(n, sg, mptr, len);
- if (status) {
- nvme_sg_unmap(sg);
- }
- return status;
- }
- static uint16_t nvme_map_data(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps);
- bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT);
- size_t len = nvme_l2b(ns, nlb);
- uint16_t status;
- if (nvme_ns_ext(ns) &&
- !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) {
- NvmeSg sg;
- len += nvme_m2b(ns, nlb);
- status = nvme_map_dptr(n, &sg, len, &req->cmd);
- if (status) {
- return status;
- }
- nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA);
- nvme_sg_split(&sg, ns, &req->sg, NULL);
- nvme_sg_unmap(&sg);
- return NVME_SUCCESS;
- }
- return nvme_map_dptr(n, &req->sg, len, &req->cmd);
- }
- static uint16_t nvme_map_mdata(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- size_t len = nvme_m2b(ns, nlb);
- uint16_t status;
- if (nvme_ns_ext(ns)) {
- NvmeSg sg;
- len += nvme_l2b(ns, nlb);
- status = nvme_map_dptr(n, &sg, len, &req->cmd);
- if (status) {
- return status;
- }
- nvme_sg_init(n, &req->sg, sg.flags & NVME_SG_DMA);
- nvme_sg_split(&sg, ns, NULL, &req->sg);
- nvme_sg_unmap(&sg);
- return NVME_SUCCESS;
- }
- return nvme_map_mptr(n, &req->sg, len, &req->cmd);
- }
- static uint16_t nvme_tx_interleaved(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr,
- uint32_t len, uint32_t bytes,
- int32_t skip_bytes, int64_t offset,
- NvmeTxDirection dir)
- {
- hwaddr addr;
- uint32_t trans_len, count = bytes;
- bool dma = sg->flags & NVME_SG_DMA;
- int64_t sge_len;
- int sg_idx = 0;
- int ret;
- assert(sg->flags & NVME_SG_ALLOC);
- while (len) {
- sge_len = dma ? sg->qsg.sg[sg_idx].len : sg->iov.iov[sg_idx].iov_len;
- if (sge_len - offset < 0) {
- offset -= sge_len;
- sg_idx++;
- continue;
- }
- if (sge_len == offset) {
- offset = 0;
- sg_idx++;
- continue;
- }
- trans_len = MIN(len, count);
- trans_len = MIN(trans_len, sge_len - offset);
- if (dma) {
- addr = sg->qsg.sg[sg_idx].base + offset;
- } else {
- addr = (hwaddr)(uintptr_t)sg->iov.iov[sg_idx].iov_base + offset;
- }
- if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
- ret = nvme_addr_read(n, addr, ptr, trans_len);
- } else {
- ret = nvme_addr_write(n, addr, ptr, trans_len);
- }
- if (ret) {
- return NVME_DATA_TRAS_ERROR;
- }
- ptr += trans_len;
- len -= trans_len;
- count -= trans_len;
- offset += trans_len;
- if (count == 0) {
- count = bytes;
- offset += skip_bytes;
- }
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len,
- NvmeTxDirection dir)
- {
- assert(sg->flags & NVME_SG_ALLOC);
- if (sg->flags & NVME_SG_DMA) {
- const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
- dma_addr_t residual;
- if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
- dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
- } else {
- dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
- }
- if (unlikely(residual)) {
- trace_pci_nvme_err_invalid_dma();
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- } else {
- size_t bytes;
- if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
- bytes = qemu_iovec_to_buf(&sg->iov, 0, ptr, len);
- } else {
- bytes = qemu_iovec_from_buf(&sg->iov, 0, ptr, len);
- }
- if (unlikely(bytes != len)) {
- trace_pci_nvme_err_invalid_dma();
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- return NVME_SUCCESS;
- }
- static inline uint16_t nvme_c2h(NvmeCtrl *n, void *ptr, uint32_t len,
- NvmeRequest *req)
- {
- uint16_t status;
- status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
- if (status) {
- return status;
- }
- return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_FROM_DEVICE);
- }
- static inline uint16_t nvme_h2c(NvmeCtrl *n, void *ptr, uint32_t len,
- NvmeRequest *req)
- {
- uint16_t status;
- status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
- if (status) {
- return status;
- }
- return nvme_tx(n, &req->sg, ptr, len, NVME_TX_DIRECTION_TO_DEVICE);
- }
- uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len,
- NvmeTxDirection dir, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- bool pi = !!NVME_ID_NS_DPS_TYPE(ns->id_ns.dps);
- bool pract = !!(le16_to_cpu(rw->control) & NVME_RW_PRINFO_PRACT);
- if (nvme_ns_ext(ns) &&
- !(pi && pract && ns->lbaf.ms == nvme_pi_tuple_size(ns))) {
- return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbasz,
- ns->lbaf.ms, 0, dir);
- }
- return nvme_tx(n, &req->sg, ptr, len, dir);
- }
- uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len,
- NvmeTxDirection dir, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- uint16_t status;
- if (nvme_ns_ext(ns)) {
- return nvme_tx_interleaved(n, &req->sg, ptr, len, ns->lbaf.ms,
- ns->lbasz, ns->lbasz, dir);
- }
- nvme_sg_unmap(&req->sg);
- status = nvme_map_mptr(n, &req->sg, len, &req->cmd);
- if (status) {
- return status;
- }
- return nvme_tx(n, &req->sg, ptr, len, dir);
- }
- static inline void nvme_blk_read(BlockBackend *blk, int64_t offset,
- uint32_t align, BlockCompletionFunc *cb,
- NvmeRequest *req)
- {
- assert(req->sg.flags & NVME_SG_ALLOC);
- if (req->sg.flags & NVME_SG_DMA) {
- req->aiocb = dma_blk_read(blk, &req->sg.qsg, offset, align, cb, req);
- } else {
- req->aiocb = blk_aio_preadv(blk, offset, &req->sg.iov, 0, cb, req);
- }
- }
- static inline void nvme_blk_write(BlockBackend *blk, int64_t offset,
- uint32_t align, BlockCompletionFunc *cb,
- NvmeRequest *req)
- {
- assert(req->sg.flags & NVME_SG_ALLOC);
- if (req->sg.flags & NVME_SG_DMA) {
- req->aiocb = dma_blk_write(blk, &req->sg.qsg, offset, align, cb, req);
- } else {
- req->aiocb = blk_aio_pwritev(blk, offset, &req->sg.iov, 0, cb, req);
- }
- }
- static void nvme_update_cq_eventidx(const NvmeCQueue *cq)
- {
- trace_pci_nvme_update_cq_eventidx(cq->cqid, cq->head);
- stl_le_pci_dma(PCI_DEVICE(cq->ctrl), cq->ei_addr, cq->head,
- MEMTXATTRS_UNSPECIFIED);
- }
- static void nvme_update_cq_head(NvmeCQueue *cq)
- {
- ldl_le_pci_dma(PCI_DEVICE(cq->ctrl), cq->db_addr, &cq->head,
- MEMTXATTRS_UNSPECIFIED);
- trace_pci_nvme_update_cq_head(cq->cqid, cq->head);
- }
- static void nvme_post_cqes(void *opaque)
- {
- NvmeCQueue *cq = opaque;
- NvmeCtrl *n = cq->ctrl;
- NvmeRequest *req, *next;
- bool pending = cq->head != cq->tail;
- int ret;
- QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
- NvmeSQueue *sq;
- hwaddr addr;
- if (n->dbbuf_enabled) {
- nvme_update_cq_eventidx(cq);
- nvme_update_cq_head(cq);
- }
- if (nvme_cq_full(cq)) {
- break;
- }
- sq = req->sq;
- req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase);
- req->cqe.sq_id = cpu_to_le16(sq->sqid);
- req->cqe.sq_head = cpu_to_le16(sq->head);
- addr = cq->dma_addr + (cq->tail << NVME_CQES);
- ret = pci_dma_write(PCI_DEVICE(n), addr, (void *)&req->cqe,
- sizeof(req->cqe));
- if (ret) {
- trace_pci_nvme_err_addr_write(addr);
- trace_pci_nvme_err_cfs();
- stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
- break;
- }
- QTAILQ_REMOVE(&cq->req_list, req, entry);
- nvme_inc_cq_tail(cq);
- nvme_sg_unmap(&req->sg);
- if (QTAILQ_EMPTY(&sq->req_list) && !nvme_sq_empty(sq)) {
- qemu_bh_schedule(sq->bh);
- }
- QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
- }
- if (cq->tail != cq->head) {
- if (cq->irq_enabled && !pending) {
- n->cq_pending++;
- }
- nvme_irq_assert(n, cq);
- }
- }
- static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
- {
- assert(cq->cqid == req->sq->cqid);
- trace_pci_nvme_enqueue_req_completion(nvme_cid(req), cq->cqid,
- le32_to_cpu(req->cqe.result),
- le32_to_cpu(req->cqe.dw1),
- req->status);
- if (req->status) {
- trace_pci_nvme_err_req_status(nvme_cid(req), nvme_nsid(req->ns),
- req->status, req->cmd.opcode);
- }
- QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
- QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
- qemu_bh_schedule(cq->bh);
- }
- static void nvme_process_aers(void *opaque)
- {
- NvmeCtrl *n = opaque;
- NvmeAsyncEvent *event, *next;
- trace_pci_nvme_process_aers(n->aer_queued);
- QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) {
- NvmeRequest *req;
- NvmeAerResult *result;
- /* can't post cqe if there is nothing to complete */
- if (!n->outstanding_aers) {
- trace_pci_nvme_no_outstanding_aers();
- break;
- }
- /* ignore if masked (cqe posted, but event not cleared) */
- if (n->aer_mask & (1 << event->result.event_type)) {
- trace_pci_nvme_aer_masked(event->result.event_type, n->aer_mask);
- continue;
- }
- QTAILQ_REMOVE(&n->aer_queue, event, entry);
- n->aer_queued--;
- n->aer_mask |= 1 << event->result.event_type;
- n->outstanding_aers--;
- req = n->aer_reqs[n->outstanding_aers];
- result = (NvmeAerResult *) &req->cqe.result;
- result->event_type = event->result.event_type;
- result->event_info = event->result.event_info;
- result->log_page = event->result.log_page;
- g_free(event);
- trace_pci_nvme_aer_post_cqe(result->event_type, result->event_info,
- result->log_page);
- nvme_enqueue_req_completion(&n->admin_cq, req);
- }
- }
- static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,
- uint8_t event_info, uint8_t log_page)
- {
- NvmeAsyncEvent *event;
- trace_pci_nvme_enqueue_event(event_type, event_info, log_page);
- if (n->aer_queued == n->params.aer_max_queued) {
- trace_pci_nvme_enqueue_event_noqueue(n->aer_queued);
- return;
- }
- event = g_new(NvmeAsyncEvent, 1);
- event->result = (NvmeAerResult) {
- .event_type = event_type,
- .event_info = event_info,
- .log_page = log_page,
- };
- QTAILQ_INSERT_TAIL(&n->aer_queue, event, entry);
- n->aer_queued++;
- nvme_process_aers(n);
- }
- static void nvme_smart_event(NvmeCtrl *n, uint8_t event)
- {
- uint8_t aer_info;
- /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
- if (!(NVME_AEC_SMART(n->features.async_config) & event)) {
- return;
- }
- switch (event) {
- case NVME_SMART_SPARE:
- aer_info = NVME_AER_INFO_SMART_SPARE_THRESH;
- break;
- case NVME_SMART_TEMPERATURE:
- aer_info = NVME_AER_INFO_SMART_TEMP_THRESH;
- break;
- case NVME_SMART_RELIABILITY:
- case NVME_SMART_MEDIA_READ_ONLY:
- case NVME_SMART_FAILED_VOLATILE_MEDIA:
- case NVME_SMART_PMR_UNRELIABLE:
- aer_info = NVME_AER_INFO_SMART_RELIABILITY;
- break;
- default:
- return;
- }
- nvme_enqueue_event(n, NVME_AER_TYPE_SMART, aer_info, NVME_LOG_SMART_INFO);
- }
- static void nvme_clear_events(NvmeCtrl *n, uint8_t event_type)
- {
- NvmeAsyncEvent *event, *next;
- n->aer_mask &= ~(1 << event_type);
- QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) {
- if (event->result.event_type == event_type) {
- QTAILQ_REMOVE(&n->aer_queue, event, entry);
- n->aer_queued--;
- g_free(event);
- }
- }
- }
- static inline uint16_t nvme_check_mdts(NvmeCtrl *n, size_t len)
- {
- uint8_t mdts = n->params.mdts;
- if (mdts && len > n->page_size << mdts) {
- trace_pci_nvme_err_mdts(len);
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- static inline uint16_t nvme_check_bounds(NvmeNamespace *ns, uint64_t slba,
- uint32_t nlb)
- {
- uint64_t nsze = le64_to_cpu(ns->id_ns.nsze);
- if (unlikely(UINT64_MAX - slba < nlb || slba + nlb > nsze)) {
- trace_pci_nvme_err_invalid_lba_range(slba, nlb, nsze);
- return NVME_LBA_RANGE | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- static int nvme_block_status_all(NvmeNamespace *ns, uint64_t slba,
- uint32_t nlb, int flags)
- {
- BlockDriverState *bs = blk_bs(ns->blkconf.blk);
- int64_t pnum = 0, bytes = nvme_l2b(ns, nlb);
- int64_t offset = nvme_l2b(ns, slba);
- int ret;
- /*
- * `pnum` holds the number of bytes after offset that shares the same
- * allocation status as the byte at offset. If `pnum` is different from
- * `bytes`, we should check the allocation status of the next range and
- * continue this until all bytes have been checked.
- */
- do {
- bytes -= pnum;
- ret = bdrv_block_status(bs, offset, bytes, &pnum, NULL, NULL);
- if (ret < 0) {
- return ret;
- }
- trace_pci_nvme_block_status(offset, bytes, pnum, ret,
- !!(ret & BDRV_BLOCK_ZERO));
- if (!(ret & flags)) {
- return 1;
- }
- offset += pnum;
- } while (pnum != bytes);
- return 0;
- }
- static uint16_t nvme_check_dulbe(NvmeNamespace *ns, uint64_t slba,
- uint32_t nlb)
- {
- int ret;
- Error *err = NULL;
- ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_DATA);
- if (ret) {
- if (ret < 0) {
- error_setg_errno(&err, -ret, "unable to get block status");
- error_report_err(err);
- return NVME_INTERNAL_DEV_ERROR;
- }
- return NVME_DULB;
- }
- return NVME_SUCCESS;
- }
- static inline uint32_t nvme_zone_idx(NvmeNamespace *ns, uint64_t slba)
- {
- return ns->zone_size_log2 > 0 ? slba >> ns->zone_size_log2 :
- slba / ns->zone_size;
- }
- static inline NvmeZone *nvme_get_zone_by_slba(NvmeNamespace *ns, uint64_t slba)
- {
- uint32_t zone_idx = nvme_zone_idx(ns, slba);
- if (zone_idx >= ns->num_zones) {
- return NULL;
- }
- return &ns->zone_array[zone_idx];
- }
- static uint16_t nvme_check_zone_state_for_write(NvmeZone *zone)
- {
- uint64_t zslba = zone->d.zslba;
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EMPTY:
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- case NVME_ZONE_STATE_CLOSED:
- return NVME_SUCCESS;
- case NVME_ZONE_STATE_FULL:
- trace_pci_nvme_err_zone_is_full(zslba);
- return NVME_ZONE_FULL;
- case NVME_ZONE_STATE_OFFLINE:
- trace_pci_nvme_err_zone_is_offline(zslba);
- return NVME_ZONE_OFFLINE;
- case NVME_ZONE_STATE_READ_ONLY:
- trace_pci_nvme_err_zone_is_read_only(zslba);
- return NVME_ZONE_READ_ONLY;
- default:
- g_assert_not_reached();
- }
- return NVME_INTERNAL_DEV_ERROR;
- }
- static uint16_t nvme_check_zone_write(NvmeNamespace *ns, NvmeZone *zone,
- uint64_t slba, uint32_t nlb)
- {
- uint64_t zcap = nvme_zone_wr_boundary(zone);
- uint16_t status;
- status = nvme_check_zone_state_for_write(zone);
- if (status) {
- return status;
- }
- if (zone->d.za & NVME_ZA_ZRWA_VALID) {
- uint64_t ezrwa = zone->w_ptr + 2 * ns->zns.zrwas;
- if (slba < zone->w_ptr || slba + nlb > ezrwa) {
- trace_pci_nvme_err_zone_invalid_write(slba, zone->w_ptr);
- return NVME_ZONE_INVALID_WRITE;
- }
- } else {
- if (unlikely(slba != zone->w_ptr)) {
- trace_pci_nvme_err_write_not_at_wp(slba, zone->d.zslba,
- zone->w_ptr);
- return NVME_ZONE_INVALID_WRITE;
- }
- }
- if (unlikely((slba + nlb) > zcap)) {
- trace_pci_nvme_err_zone_boundary(slba, nlb, zcap);
- return NVME_ZONE_BOUNDARY_ERROR;
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_check_zone_state_for_read(NvmeZone *zone)
- {
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EMPTY:
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- case NVME_ZONE_STATE_FULL:
- case NVME_ZONE_STATE_CLOSED:
- case NVME_ZONE_STATE_READ_ONLY:
- return NVME_SUCCESS;
- case NVME_ZONE_STATE_OFFLINE:
- trace_pci_nvme_err_zone_is_offline(zone->d.zslba);
- return NVME_ZONE_OFFLINE;
- default:
- g_assert_not_reached();
- }
- return NVME_INTERNAL_DEV_ERROR;
- }
- static uint16_t nvme_check_zone_read(NvmeNamespace *ns, uint64_t slba,
- uint32_t nlb)
- {
- NvmeZone *zone;
- uint64_t bndry, end;
- uint16_t status;
- zone = nvme_get_zone_by_slba(ns, slba);
- assert(zone);
- bndry = nvme_zone_rd_boundary(ns, zone);
- end = slba + nlb;
- status = nvme_check_zone_state_for_read(zone);
- if (status) {
- ;
- } else if (unlikely(end > bndry)) {
- if (!ns->params.cross_zone_read) {
- status = NVME_ZONE_BOUNDARY_ERROR;
- } else {
- /*
- * Read across zone boundary - check that all subsequent
- * zones that are being read have an appropriate state.
- */
- do {
- zone++;
- status = nvme_check_zone_state_for_read(zone);
- if (status) {
- break;
- }
- } while (end > nvme_zone_rd_boundary(ns, zone));
- }
- }
- return status;
- }
- static uint16_t nvme_zrm_finish(NvmeNamespace *ns, NvmeZone *zone)
- {
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_FULL:
- return NVME_SUCCESS;
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- nvme_aor_dec_open(ns);
- /* fallthrough */
- case NVME_ZONE_STATE_CLOSED:
- nvme_aor_dec_active(ns);
- if (zone->d.za & NVME_ZA_ZRWA_VALID) {
- zone->d.za &= ~NVME_ZA_ZRWA_VALID;
- if (ns->params.numzrwa) {
- ns->zns.numzrwa++;
- }
- }
- /* fallthrough */
- case NVME_ZONE_STATE_EMPTY:
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_FULL);
- return NVME_SUCCESS;
- default:
- return NVME_ZONE_INVAL_TRANSITION;
- }
- }
- static uint16_t nvme_zrm_close(NvmeNamespace *ns, NvmeZone *zone)
- {
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- nvme_aor_dec_open(ns);
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_CLOSED);
- /* fall through */
- case NVME_ZONE_STATE_CLOSED:
- return NVME_SUCCESS;
- default:
- return NVME_ZONE_INVAL_TRANSITION;
- }
- }
- static uint16_t nvme_zrm_reset(NvmeNamespace *ns, NvmeZone *zone)
- {
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- nvme_aor_dec_open(ns);
- /* fallthrough */
- case NVME_ZONE_STATE_CLOSED:
- nvme_aor_dec_active(ns);
- if (zone->d.za & NVME_ZA_ZRWA_VALID) {
- if (ns->params.numzrwa) {
- ns->zns.numzrwa++;
- }
- }
- /* fallthrough */
- case NVME_ZONE_STATE_FULL:
- zone->w_ptr = zone->d.zslba;
- zone->d.wp = zone->w_ptr;
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EMPTY);
- /* fallthrough */
- case NVME_ZONE_STATE_EMPTY:
- return NVME_SUCCESS;
- default:
- return NVME_ZONE_INVAL_TRANSITION;
- }
- }
- static void nvme_zrm_auto_transition_zone(NvmeNamespace *ns)
- {
- NvmeZone *zone;
- if (ns->params.max_open_zones &&
- ns->nr_open_zones == ns->params.max_open_zones) {
- zone = QTAILQ_FIRST(&ns->imp_open_zones);
- if (zone) {
- /*
- * Automatically close this implicitly open zone.
- */
- QTAILQ_REMOVE(&ns->imp_open_zones, zone, entry);
- nvme_zrm_close(ns, zone);
- }
- }
- }
- enum {
- NVME_ZRM_AUTO = 1 << 0,
- NVME_ZRM_ZRWA = 1 << 1,
- };
- static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
- NvmeZone *zone, int flags)
- {
- int act = 0;
- uint16_t status;
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EMPTY:
- act = 1;
- /* fallthrough */
- case NVME_ZONE_STATE_CLOSED:
- if (n->params.auto_transition_zones) {
- nvme_zrm_auto_transition_zone(ns);
- }
- status = nvme_zns_check_resources(ns, act, 1,
- (flags & NVME_ZRM_ZRWA) ? 1 : 0);
- if (status) {
- return status;
- }
- if (act) {
- nvme_aor_inc_active(ns);
- }
- nvme_aor_inc_open(ns);
- if (flags & NVME_ZRM_AUTO) {
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_IMPLICITLY_OPEN);
- return NVME_SUCCESS;
- }
- /* fallthrough */
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- if (flags & NVME_ZRM_AUTO) {
- return NVME_SUCCESS;
- }
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_EXPLICITLY_OPEN);
- /* fallthrough */
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- if (flags & NVME_ZRM_ZRWA) {
- ns->zns.numzrwa--;
- zone->d.za |= NVME_ZA_ZRWA_VALID;
- }
- return NVME_SUCCESS;
- default:
- return NVME_ZONE_INVAL_TRANSITION;
- }
- }
- static inline uint16_t nvme_zrm_auto(NvmeCtrl *n, NvmeNamespace *ns,
- NvmeZone *zone)
- {
- return nvme_zrm_open_flags(n, ns, zone, NVME_ZRM_AUTO);
- }
- static void nvme_advance_zone_wp(NvmeNamespace *ns, NvmeZone *zone,
- uint32_t nlb)
- {
- zone->d.wp += nlb;
- if (zone->d.wp == nvme_zone_wr_boundary(zone)) {
- nvme_zrm_finish(ns, zone);
- }
- }
- static void nvme_zoned_zrwa_implicit_flush(NvmeNamespace *ns, NvmeZone *zone,
- uint32_t nlbc)
- {
- uint16_t nzrwafgs = DIV_ROUND_UP(nlbc, ns->zns.zrwafg);
- nlbc = nzrwafgs * ns->zns.zrwafg;
- trace_pci_nvme_zoned_zrwa_implicit_flush(zone->d.zslba, nlbc);
- zone->w_ptr += nlbc;
- nvme_advance_zone_wp(ns, zone, nlbc);
- }
- static void nvme_finalize_zoned_write(NvmeNamespace *ns, NvmeRequest *req)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- NvmeZone *zone;
- uint64_t slba;
- uint32_t nlb;
- slba = le64_to_cpu(rw->slba);
- nlb = le16_to_cpu(rw->nlb) + 1;
- zone = nvme_get_zone_by_slba(ns, slba);
- assert(zone);
- if (zone->d.za & NVME_ZA_ZRWA_VALID) {
- uint64_t ezrwa = zone->w_ptr + ns->zns.zrwas - 1;
- uint64_t elba = slba + nlb - 1;
- if (elba > ezrwa) {
- nvme_zoned_zrwa_implicit_flush(ns, zone, elba - ezrwa);
- }
- return;
- }
- nvme_advance_zone_wp(ns, zone, nlb);
- }
- static inline bool nvme_is_write(NvmeRequest *req)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- return rw->opcode == NVME_CMD_WRITE ||
- rw->opcode == NVME_CMD_ZONE_APPEND ||
- rw->opcode == NVME_CMD_WRITE_ZEROES;
- }
- static void nvme_misc_cb(void *opaque, int ret)
- {
- NvmeRequest *req = opaque;
- uint16_t cid = nvme_cid(req);
- trace_pci_nvme_misc_cb(cid);
- if (ret) {
- if (!req->status) {
- req->status = NVME_INTERNAL_DEV_ERROR;
- }
- trace_pci_nvme_err_aio(cid, strerror(-ret), req->status);
- }
- nvme_enqueue_req_completion(nvme_cq(req), req);
- }
- void nvme_rw_complete_cb(void *opaque, int ret)
- {
- NvmeRequest *req = opaque;
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- BlockAcctCookie *acct = &req->acct;
- BlockAcctStats *stats = blk_get_stats(blk);
- trace_pci_nvme_rw_complete_cb(nvme_cid(req), blk_name(blk));
- if (ret) {
- Error *err = NULL;
- block_acct_failed(stats, acct);
- switch (req->cmd.opcode) {
- case NVME_CMD_READ:
- req->status = NVME_UNRECOVERED_READ;
- break;
- case NVME_CMD_WRITE:
- case NVME_CMD_WRITE_ZEROES:
- case NVME_CMD_ZONE_APPEND:
- req->status = NVME_WRITE_FAULT;
- break;
- default:
- req->status = NVME_INTERNAL_DEV_ERROR;
- break;
- }
- trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), req->status);
- error_setg_errno(&err, -ret, "aio failed");
- error_report_err(err);
- } else {
- block_acct_done(stats, acct);
- }
- if (ns->params.zoned && nvme_is_write(req)) {
- nvme_finalize_zoned_write(ns, req);
- }
- nvme_enqueue_req_completion(nvme_cq(req), req);
- }
- static void nvme_rw_cb(void *opaque, int ret)
- {
- NvmeRequest *req = opaque;
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk));
- if (ret) {
- goto out;
- }
- if (ns->lbaf.ms) {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
- uint64_t offset = nvme_moff(ns, slba);
- if (req->cmd.opcode == NVME_CMD_WRITE_ZEROES) {
- size_t mlen = nvme_m2b(ns, nlb);
- req->aiocb = blk_aio_pwrite_zeroes(blk, offset, mlen,
- BDRV_REQ_MAY_UNMAP,
- nvme_rw_complete_cb, req);
- return;
- }
- if (nvme_ns_ext(ns) || req->cmd.mptr) {
- uint16_t status;
- nvme_sg_unmap(&req->sg);
- status = nvme_map_mdata(nvme_ctrl(req), nlb, req);
- if (status) {
- ret = -EFAULT;
- goto out;
- }
- if (req->cmd.opcode == NVME_CMD_READ) {
- return nvme_blk_read(blk, offset, 1, nvme_rw_complete_cb, req);
- }
- return nvme_blk_write(blk, offset, 1, nvme_rw_complete_cb, req);
- }
- }
- out:
- nvme_rw_complete_cb(req, ret);
- }
- static void nvme_verify_cb(void *opaque, int ret)
- {
- NvmeBounceContext *ctx = opaque;
- NvmeRequest *req = ctx->req;
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- BlockAcctCookie *acct = &req->acct;
- BlockAcctStats *stats = blk_get_stats(blk);
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
- uint16_t apptag = le16_to_cpu(rw->apptag);
- uint16_t appmask = le16_to_cpu(rw->appmask);
- uint64_t reftag = le32_to_cpu(rw->reftag);
- uint64_t cdw3 = le32_to_cpu(rw->cdw3);
- uint16_t status;
- reftag |= cdw3 << 32;
- trace_pci_nvme_verify_cb(nvme_cid(req), prinfo, apptag, appmask, reftag);
- if (ret) {
- block_acct_failed(stats, acct);
- req->status = NVME_UNRECOVERED_READ;
- trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), req->status);
- goto out;
- }
- block_acct_done(stats, acct);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- status = nvme_dif_mangle_mdata(ns, ctx->mdata.bounce,
- ctx->mdata.iov.size, slba);
- if (status) {
- req->status = status;
- goto out;
- }
- req->status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size,
- ctx->mdata.bounce, ctx->mdata.iov.size,
- prinfo, slba, apptag, appmask, &reftag);
- }
- out:
- qemu_iovec_destroy(&ctx->data.iov);
- g_free(ctx->data.bounce);
- qemu_iovec_destroy(&ctx->mdata.iov);
- g_free(ctx->mdata.bounce);
- g_free(ctx);
- nvme_enqueue_req_completion(nvme_cq(req), req);
- }
- static void nvme_verify_mdata_in_cb(void *opaque, int ret)
- {
- NvmeBounceContext *ctx = opaque;
- NvmeRequest *req = ctx->req;
- NvmeNamespace *ns = req->ns;
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
- size_t mlen = nvme_m2b(ns, nlb);
- uint64_t offset = nvme_moff(ns, slba);
- BlockBackend *blk = ns->blkconf.blk;
- trace_pci_nvme_verify_mdata_in_cb(nvme_cid(req), blk_name(blk));
- if (ret) {
- goto out;
- }
- ctx->mdata.bounce = g_malloc(mlen);
- qemu_iovec_reset(&ctx->mdata.iov);
- qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen);
- req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0,
- nvme_verify_cb, ctx);
- return;
- out:
- nvme_verify_cb(ctx, ret);
- }
- struct nvme_compare_ctx {
- struct {
- QEMUIOVector iov;
- uint8_t *bounce;
- } data;
- struct {
- QEMUIOVector iov;
- uint8_t *bounce;
- } mdata;
- };
- static void nvme_compare_mdata_cb(void *opaque, int ret)
- {
- NvmeRequest *req = opaque;
- NvmeNamespace *ns = req->ns;
- NvmeCtrl *n = nvme_ctrl(req);
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
- uint16_t apptag = le16_to_cpu(rw->apptag);
- uint16_t appmask = le16_to_cpu(rw->appmask);
- uint64_t reftag = le32_to_cpu(rw->reftag);
- uint64_t cdw3 = le32_to_cpu(rw->cdw3);
- struct nvme_compare_ctx *ctx = req->opaque;
- g_autofree uint8_t *buf = NULL;
- BlockBackend *blk = ns->blkconf.blk;
- BlockAcctCookie *acct = &req->acct;
- BlockAcctStats *stats = blk_get_stats(blk);
- uint16_t status = NVME_SUCCESS;
- reftag |= cdw3 << 32;
- trace_pci_nvme_compare_mdata_cb(nvme_cid(req));
- if (ret) {
- block_acct_failed(stats, acct);
- req->status = NVME_UNRECOVERED_READ;
- trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), req->status);
- goto out;
- }
- buf = g_malloc(ctx->mdata.iov.size);
- status = nvme_bounce_mdata(n, buf, ctx->mdata.iov.size,
- NVME_TX_DIRECTION_TO_DEVICE, req);
- if (status) {
- req->status = status;
- goto out;
- }
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- uint64_t slba = le64_to_cpu(rw->slba);
- uint8_t *bufp;
- uint8_t *mbufp = ctx->mdata.bounce;
- uint8_t *end = mbufp + ctx->mdata.iov.size;
- int16_t pil = 0;
- status = nvme_dif_check(ns, ctx->data.bounce, ctx->data.iov.size,
- ctx->mdata.bounce, ctx->mdata.iov.size, prinfo,
- slba, apptag, appmask, &reftag);
- if (status) {
- req->status = status;
- goto out;
- }
- /*
- * When formatted with protection information, do not compare the DIF
- * tuple.
- */
- if (!(ns->id_ns.dps & NVME_ID_NS_DPS_FIRST_EIGHT)) {
- pil = ns->lbaf.ms - nvme_pi_tuple_size(ns);
- }
- for (bufp = buf; mbufp < end; bufp += ns->lbaf.ms, mbufp += ns->lbaf.ms) {
- if (memcmp(bufp + pil, mbufp + pil, ns->lbaf.ms - pil)) {
- req->status = NVME_CMP_FAILURE | NVME_DNR;
- goto out;
- }
- }
- goto out;
- }
- if (memcmp(buf, ctx->mdata.bounce, ctx->mdata.iov.size)) {
- req->status = NVME_CMP_FAILURE | NVME_DNR;
- goto out;
- }
- block_acct_done(stats, acct);
- out:
- qemu_iovec_destroy(&ctx->data.iov);
- g_free(ctx->data.bounce);
- qemu_iovec_destroy(&ctx->mdata.iov);
- g_free(ctx->mdata.bounce);
- g_free(ctx);
- nvme_enqueue_req_completion(nvme_cq(req), req);
- }
- static void nvme_compare_data_cb(void *opaque, int ret)
- {
- NvmeRequest *req = opaque;
- NvmeCtrl *n = nvme_ctrl(req);
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- BlockAcctCookie *acct = &req->acct;
- BlockAcctStats *stats = blk_get_stats(blk);
- struct nvme_compare_ctx *ctx = req->opaque;
- g_autofree uint8_t *buf = NULL;
- uint16_t status;
- trace_pci_nvme_compare_data_cb(nvme_cid(req));
- if (ret) {
- block_acct_failed(stats, acct);
- req->status = NVME_UNRECOVERED_READ;
- trace_pci_nvme_err_aio(nvme_cid(req), strerror(-ret), req->status);
- goto out;
- }
- buf = g_malloc(ctx->data.iov.size);
- status = nvme_bounce_data(n, buf, ctx->data.iov.size,
- NVME_TX_DIRECTION_TO_DEVICE, req);
- if (status) {
- req->status = status;
- goto out;
- }
- if (memcmp(buf, ctx->data.bounce, ctx->data.iov.size)) {
- req->status = NVME_CMP_FAILURE | NVME_DNR;
- goto out;
- }
- if (ns->lbaf.ms) {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
- size_t mlen = nvme_m2b(ns, nlb);
- uint64_t offset = nvme_moff(ns, slba);
- ctx->mdata.bounce = g_malloc(mlen);
- qemu_iovec_init(&ctx->mdata.iov, 1);
- qemu_iovec_add(&ctx->mdata.iov, ctx->mdata.bounce, mlen);
- req->aiocb = blk_aio_preadv(blk, offset, &ctx->mdata.iov, 0,
- nvme_compare_mdata_cb, req);
- return;
- }
- block_acct_done(stats, acct);
- out:
- qemu_iovec_destroy(&ctx->data.iov);
- g_free(ctx->data.bounce);
- g_free(ctx);
- nvme_enqueue_req_completion(nvme_cq(req), req);
- }
- typedef struct NvmeDSMAIOCB {
- BlockAIOCB common;
- BlockAIOCB *aiocb;
- NvmeRequest *req;
- int ret;
- NvmeDsmRange *range;
- unsigned int nr;
- unsigned int idx;
- } NvmeDSMAIOCB;
- static void nvme_dsm_cancel(BlockAIOCB *aiocb)
- {
- NvmeDSMAIOCB *iocb = container_of(aiocb, NvmeDSMAIOCB, common);
- /* break nvme_dsm_cb loop */
- iocb->idx = iocb->nr;
- iocb->ret = -ECANCELED;
- if (iocb->aiocb) {
- blk_aio_cancel_async(iocb->aiocb);
- iocb->aiocb = NULL;
- } else {
- /*
- * We only reach this if nvme_dsm_cancel() has already been called or
- * the command ran to completion.
- */
- assert(iocb->idx == iocb->nr);
- }
- }
- static const AIOCBInfo nvme_dsm_aiocb_info = {
- .aiocb_size = sizeof(NvmeDSMAIOCB),
- .cancel_async = nvme_dsm_cancel,
- };
- static void nvme_dsm_cb(void *opaque, int ret);
- static void nvme_dsm_md_cb(void *opaque, int ret)
- {
- NvmeDSMAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *ns = req->ns;
- NvmeDsmRange *range;
- uint64_t slba;
- uint32_t nlb;
- if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
- goto done;
- }
- range = &iocb->range[iocb->idx - 1];
- slba = le64_to_cpu(range->slba);
- nlb = le32_to_cpu(range->nlb);
- /*
- * Check that all block were discarded (zeroed); otherwise we do not zero
- * the metadata.
- */
- ret = nvme_block_status_all(ns, slba, nlb, BDRV_BLOCK_ZERO);
- if (ret) {
- if (ret < 0) {
- goto done;
- }
- nvme_dsm_cb(iocb, 0);
- return;
- }
- iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, nvme_moff(ns, slba),
- nvme_m2b(ns, nlb), BDRV_REQ_MAY_UNMAP,
- nvme_dsm_cb, iocb);
- return;
- done:
- nvme_dsm_cb(iocb, ret);
- }
- static void nvme_dsm_cb(void *opaque, int ret)
- {
- NvmeDSMAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeCtrl *n = nvme_ctrl(req);
- NvmeNamespace *ns = req->ns;
- NvmeDsmRange *range;
- uint64_t slba;
- uint32_t nlb;
- if (iocb->ret < 0) {
- goto done;
- } else if (ret < 0) {
- iocb->ret = ret;
- goto done;
- }
- next:
- if (iocb->idx == iocb->nr) {
- goto done;
- }
- range = &iocb->range[iocb->idx++];
- slba = le64_to_cpu(range->slba);
- nlb = le32_to_cpu(range->nlb);
- trace_pci_nvme_dsm_deallocate(slba, nlb);
- if (nlb > n->dmrsl) {
- trace_pci_nvme_dsm_single_range_limit_exceeded(nlb, n->dmrsl);
- goto next;
- }
- if (nvme_check_bounds(ns, slba, nlb)) {
- trace_pci_nvme_err_invalid_lba_range(slba, nlb,
- ns->id_ns.nsze);
- goto next;
- }
- iocb->aiocb = blk_aio_pdiscard(ns->blkconf.blk, nvme_l2b(ns, slba),
- nvme_l2b(ns, nlb),
- nvme_dsm_md_cb, iocb);
- return;
- done:
- iocb->aiocb = NULL;
- iocb->common.cb(iocb->common.opaque, iocb->ret);
- g_free(iocb->range);
- qemu_aio_unref(iocb);
- }
- static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- NvmeDsmCmd *dsm = (NvmeDsmCmd *) &req->cmd;
- uint32_t attr = le32_to_cpu(dsm->attributes);
- uint32_t nr = (le32_to_cpu(dsm->nr) & 0xff) + 1;
- uint16_t status = NVME_SUCCESS;
- trace_pci_nvme_dsm(nr, attr);
- if (attr & NVME_DSMGMT_AD) {
- NvmeDSMAIOCB *iocb = blk_aio_get(&nvme_dsm_aiocb_info, ns->blkconf.blk,
- nvme_misc_cb, req);
- iocb->req = req;
- iocb->ret = 0;
- iocb->range = g_new(NvmeDsmRange, nr);
- iocb->nr = nr;
- iocb->idx = 0;
- status = nvme_h2c(n, (uint8_t *)iocb->range, sizeof(NvmeDsmRange) * nr,
- req);
- if (status) {
- g_free(iocb->range);
- qemu_aio_unref(iocb);
- return status;
- }
- req->aiocb = &iocb->common;
- nvme_dsm_cb(iocb, 0);
- return NVME_NO_COMPLETE;
- }
- return status;
- }
- static uint16_t nvme_verify(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
- size_t len = nvme_l2b(ns, nlb);
- size_t data_len = len;
- int64_t offset = nvme_l2b(ns, slba);
- uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
- uint32_t reftag = le32_to_cpu(rw->reftag);
- NvmeBounceContext *ctx = NULL;
- uint16_t status;
- trace_pci_nvme_verify(nvme_cid(req), nvme_nsid(ns), slba, nlb);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- status = nvme_check_prinfo(ns, prinfo, slba, reftag);
- if (status) {
- return status;
- }
- if (prinfo & NVME_PRINFO_PRACT) {
- return NVME_INVALID_PROT_INFO | NVME_DNR;
- }
- }
- if (nvme_ns_ext(ns) && !(NVME_ID_CTRL_CTRATT_MEM(n->id_ctrl.ctratt))) {
- data_len += nvme_m2b(ns, nlb);
- }
- if (data_len > (n->page_size << n->params.vsl)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- status = nvme_check_bounds(ns, slba, nlb);
- if (status) {
- return status;
- }
- if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
- status = nvme_check_dulbe(ns, slba, nlb);
- if (status) {
- return status;
- }
- }
- ctx = g_new0(NvmeBounceContext, 1);
- ctx->req = req;
- ctx->data.bounce = g_malloc(len);
- qemu_iovec_init(&ctx->data.iov, 1);
- qemu_iovec_add(&ctx->data.iov, ctx->data.bounce, len);
- block_acct_start(blk_get_stats(blk), &req->acct, ctx->data.iov.size,
- BLOCK_ACCT_READ);
- req->aiocb = blk_aio_preadv(ns->blkconf.blk, offset, &ctx->data.iov, 0,
- nvme_verify_mdata_in_cb, ctx);
- return NVME_NO_COMPLETE;
- }
- typedef struct NvmeCopyAIOCB {
- BlockAIOCB common;
- BlockAIOCB *aiocb;
- NvmeRequest *req;
- NvmeCtrl *n;
- int ret;
- void *ranges;
- unsigned int format;
- int nr;
- int idx;
- uint8_t *bounce;
- QEMUIOVector iov;
- struct {
- BlockAcctCookie read;
- BlockAcctCookie write;
- } acct;
- uint64_t reftag;
- uint64_t slba;
- NvmeZone *zone;
- NvmeNamespace *sns;
- uint32_t tcl;
- } NvmeCopyAIOCB;
- static void nvme_copy_cancel(BlockAIOCB *aiocb)
- {
- NvmeCopyAIOCB *iocb = container_of(aiocb, NvmeCopyAIOCB, common);
- iocb->ret = -ECANCELED;
- if (iocb->aiocb) {
- blk_aio_cancel_async(iocb->aiocb);
- iocb->aiocb = NULL;
- }
- }
- static const AIOCBInfo nvme_copy_aiocb_info = {
- .aiocb_size = sizeof(NvmeCopyAIOCB),
- .cancel_async = nvme_copy_cancel,
- };
- static void nvme_copy_done(NvmeCopyAIOCB *iocb)
- {
- NvmeRequest *req = iocb->req;
- NvmeNamespace *ns = req->ns;
- BlockAcctStats *stats = blk_get_stats(ns->blkconf.blk);
- if (iocb->idx != iocb->nr) {
- req->cqe.result = cpu_to_le32(iocb->idx);
- }
- qemu_iovec_destroy(&iocb->iov);
- g_free(iocb->bounce);
- if (iocb->ret < 0) {
- block_acct_failed(stats, &iocb->acct.read);
- block_acct_failed(stats, &iocb->acct.write);
- } else {
- block_acct_done(stats, &iocb->acct.read);
- block_acct_done(stats, &iocb->acct.write);
- }
- iocb->common.cb(iocb->common.opaque, iocb->ret);
- qemu_aio_unref(iocb);
- }
- static void nvme_do_copy(NvmeCopyAIOCB *iocb);
- static void nvme_copy_source_range_parse_format0_2(void *ranges,
- int idx, uint64_t *slba,
- uint32_t *nlb,
- uint32_t *snsid,
- uint16_t *apptag,
- uint16_t *appmask,
- uint64_t *reftag)
- {
- NvmeCopySourceRangeFormat0_2 *_ranges = ranges;
- if (snsid) {
- *snsid = le32_to_cpu(_ranges[idx].sparams);
- }
- if (slba) {
- *slba = le64_to_cpu(_ranges[idx].slba);
- }
- if (nlb) {
- *nlb = le16_to_cpu(_ranges[idx].nlb) + 1;
- }
- if (apptag) {
- *apptag = le16_to_cpu(_ranges[idx].apptag);
- }
- if (appmask) {
- *appmask = le16_to_cpu(_ranges[idx].appmask);
- }
- if (reftag) {
- *reftag = le32_to_cpu(_ranges[idx].reftag);
- }
- }
- static void nvme_copy_source_range_parse_format1_3(void *ranges, int idx,
- uint64_t *slba,
- uint32_t *nlb,
- uint32_t *snsid,
- uint16_t *apptag,
- uint16_t *appmask,
- uint64_t *reftag)
- {
- NvmeCopySourceRangeFormat1_3 *_ranges = ranges;
- if (snsid) {
- *snsid = le32_to_cpu(_ranges[idx].sparams);
- }
- if (slba) {
- *slba = le64_to_cpu(_ranges[idx].slba);
- }
- if (nlb) {
- *nlb = le16_to_cpu(_ranges[idx].nlb) + 1;
- }
- if (apptag) {
- *apptag = le16_to_cpu(_ranges[idx].apptag);
- }
- if (appmask) {
- *appmask = le16_to_cpu(_ranges[idx].appmask);
- }
- if (reftag) {
- *reftag = 0;
- *reftag |= (uint64_t)_ranges[idx].sr[4] << 40;
- *reftag |= (uint64_t)_ranges[idx].sr[5] << 32;
- *reftag |= (uint64_t)_ranges[idx].sr[6] << 24;
- *reftag |= (uint64_t)_ranges[idx].sr[7] << 16;
- *reftag |= (uint64_t)_ranges[idx].sr[8] << 8;
- *reftag |= (uint64_t)_ranges[idx].sr[9];
- }
- }
- static void nvme_copy_source_range_parse(void *ranges, int idx, uint8_t format,
- uint64_t *slba, uint32_t *nlb,
- uint32_t *snsid, uint16_t *apptag,
- uint16_t *appmask, uint64_t *reftag)
- {
- switch (format) {
- case NVME_COPY_FORMAT_0:
- case NVME_COPY_FORMAT_2:
- nvme_copy_source_range_parse_format0_2(ranges, idx, slba, nlb, snsid,
- apptag, appmask, reftag);
- break;
- case NVME_COPY_FORMAT_1:
- case NVME_COPY_FORMAT_3:
- nvme_copy_source_range_parse_format1_3(ranges, idx, slba, nlb, snsid,
- apptag, appmask, reftag);
- break;
- default:
- abort();
- }
- }
- static inline uint16_t nvme_check_copy_mcl(NvmeNamespace *ns,
- NvmeCopyAIOCB *iocb, uint16_t nr)
- {
- uint32_t copy_len = 0;
- for (int idx = 0; idx < nr; idx++) {
- uint32_t nlb;
- nvme_copy_source_range_parse(iocb->ranges, idx, iocb->format, NULL,
- &nlb, NULL, NULL, NULL, NULL);
- copy_len += nlb;
- }
- iocb->tcl = copy_len;
- if (copy_len > ns->id_ns.mcl) {
- return NVME_CMD_SIZE_LIMIT | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- static void nvme_copy_out_completed_cb(void *opaque, int ret)
- {
- NvmeCopyAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *dns = req->ns;
- uint32_t nlb;
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL,
- &nlb, NULL, NULL, NULL, NULL);
- if (ret < 0) {
- iocb->ret = ret;
- req->status = NVME_WRITE_FAULT;
- goto out;
- } else if (iocb->ret < 0) {
- goto out;
- }
- if (dns->params.zoned) {
- nvme_advance_zone_wp(dns, iocb->zone, nlb);
- }
- iocb->idx++;
- iocb->slba += nlb;
- out:
- nvme_do_copy(iocb);
- }
- static void nvme_copy_out_cb(void *opaque, int ret)
- {
- NvmeCopyAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *dns = req->ns;
- uint32_t nlb;
- size_t mlen;
- uint8_t *mbounce;
- if (ret < 0 || iocb->ret < 0 || !dns->lbaf.ms) {
- goto out;
- }
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, NULL,
- &nlb, NULL, NULL, NULL, NULL);
- mlen = nvme_m2b(dns, nlb);
- mbounce = iocb->bounce + nvme_l2b(dns, nlb);
- qemu_iovec_reset(&iocb->iov);
- qemu_iovec_add(&iocb->iov, mbounce, mlen);
- iocb->aiocb = blk_aio_pwritev(dns->blkconf.blk, nvme_moff(dns, iocb->slba),
- &iocb->iov, 0, nvme_copy_out_completed_cb,
- iocb);
- return;
- out:
- nvme_copy_out_completed_cb(iocb, ret);
- }
- static void nvme_copy_in_completed_cb(void *opaque, int ret)
- {
- NvmeCopyAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *sns = iocb->sns;
- NvmeNamespace *dns = req->ns;
- NvmeCopyCmd *copy = NULL;
- uint8_t *mbounce = NULL;
- uint32_t nlb;
- uint64_t slba;
- uint16_t apptag, appmask;
- uint64_t reftag;
- size_t len, mlen;
- uint16_t status;
- if (ret < 0) {
- iocb->ret = ret;
- req->status = NVME_UNRECOVERED_READ;
- goto out;
- } else if (iocb->ret < 0) {
- goto out;
- }
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba,
- &nlb, NULL, &apptag, &appmask, &reftag);
- trace_pci_nvme_copy_out(iocb->slba, nlb);
- len = nvme_l2b(sns, nlb);
- if (NVME_ID_NS_DPS_TYPE(sns->id_ns.dps)) {
- copy = (NvmeCopyCmd *)&req->cmd;
- uint16_t prinfor = ((copy->control[0] >> 4) & 0xf);
- mlen = nvme_m2b(sns, nlb);
- mbounce = iocb->bounce + nvme_l2b(sns, nlb);
- status = nvme_dif_mangle_mdata(sns, mbounce, mlen, slba);
- if (status) {
- goto invalid;
- }
- status = nvme_dif_check(sns, iocb->bounce, len, mbounce, mlen, prinfor,
- slba, apptag, appmask, &reftag);
- if (status) {
- goto invalid;
- }
- }
- if (NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
- copy = (NvmeCopyCmd *)&req->cmd;
- uint16_t prinfow = ((copy->control[2] >> 2) & 0xf);
- mlen = nvme_m2b(dns, nlb);
- mbounce = iocb->bounce + nvme_l2b(dns, nlb);
- apptag = le16_to_cpu(copy->apptag);
- appmask = le16_to_cpu(copy->appmask);
- if (prinfow & NVME_PRINFO_PRACT) {
- status = nvme_check_prinfo(dns, prinfow, iocb->slba, iocb->reftag);
- if (status) {
- goto invalid;
- }
- nvme_dif_pract_generate_dif(dns, iocb->bounce, len, mbounce, mlen,
- apptag, &iocb->reftag);
- } else {
- status = nvme_dif_check(dns, iocb->bounce, len, mbounce, mlen,
- prinfow, iocb->slba, apptag, appmask,
- &iocb->reftag);
- if (status) {
- goto invalid;
- }
- }
- }
- status = nvme_check_bounds(dns, iocb->slba, nlb);
- if (status) {
- goto invalid;
- }
- if (dns->params.zoned) {
- status = nvme_check_zone_write(dns, iocb->zone, iocb->slba, nlb);
- if (status) {
- goto invalid;
- }
- if (!(iocb->zone->d.za & NVME_ZA_ZRWA_VALID)) {
- iocb->zone->w_ptr += nlb;
- }
- }
- qemu_iovec_reset(&iocb->iov);
- qemu_iovec_add(&iocb->iov, iocb->bounce, len);
- block_acct_start(blk_get_stats(dns->blkconf.blk), &iocb->acct.write, 0,
- BLOCK_ACCT_WRITE);
- iocb->aiocb = blk_aio_pwritev(dns->blkconf.blk, nvme_l2b(dns, iocb->slba),
- &iocb->iov, 0, nvme_copy_out_cb, iocb);
- return;
- invalid:
- req->status = status;
- iocb->ret = -1;
- out:
- nvme_do_copy(iocb);
- }
- static void nvme_copy_in_cb(void *opaque, int ret)
- {
- NvmeCopyAIOCB *iocb = opaque;
- NvmeNamespace *sns = iocb->sns;
- uint64_t slba;
- uint32_t nlb;
- if (ret < 0 || iocb->ret < 0 || !sns->lbaf.ms) {
- goto out;
- }
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format, &slba,
- &nlb, NULL, NULL, NULL, NULL);
- qemu_iovec_reset(&iocb->iov);
- qemu_iovec_add(&iocb->iov, iocb->bounce + nvme_l2b(sns, nlb),
- nvme_m2b(sns, nlb));
- iocb->aiocb = blk_aio_preadv(sns->blkconf.blk, nvme_moff(sns, slba),
- &iocb->iov, 0, nvme_copy_in_completed_cb,
- iocb);
- return;
- out:
- nvme_copy_in_completed_cb(iocb, ret);
- }
- static inline bool nvme_csi_supports_copy(uint8_t csi)
- {
- return csi == NVME_CSI_NVM || csi == NVME_CSI_ZONED;
- }
- static inline bool nvme_copy_ns_format_match(NvmeNamespace *sns,
- NvmeNamespace *dns)
- {
- return sns->lbaf.ds == dns->lbaf.ds && sns->lbaf.ms == dns->lbaf.ms;
- }
- static bool nvme_copy_matching_ns_format(NvmeNamespace *sns, NvmeNamespace *dns,
- bool pi_enable)
- {
- if (!nvme_csi_supports_copy(sns->csi) ||
- !nvme_csi_supports_copy(dns->csi)) {
- return false;
- }
- if (!pi_enable && !nvme_copy_ns_format_match(sns, dns)) {
- return false;
- }
- if (pi_enable && (!nvme_copy_ns_format_match(sns, dns) ||
- sns->id_ns.dps != dns->id_ns.dps)) {
- return false;
- }
- return true;
- }
- static inline bool nvme_copy_corresp_pi_match(NvmeNamespace *sns,
- NvmeNamespace *dns)
- {
- return sns->lbaf.ms == 0 &&
- ((dns->lbaf.ms == 8 && dns->pif == 0) ||
- (dns->lbaf.ms == 16 && dns->pif == 1));
- }
- static bool nvme_copy_corresp_pi_format(NvmeNamespace *sns, NvmeNamespace *dns,
- bool sns_pi_en)
- {
- if (!nvme_csi_supports_copy(sns->csi) ||
- !nvme_csi_supports_copy(dns->csi)) {
- return false;
- }
- if (!sns_pi_en && !nvme_copy_corresp_pi_match(sns, dns)) {
- return false;
- }
- if (sns_pi_en && !nvme_copy_corresp_pi_match(dns, sns)) {
- return false;
- }
- return true;
- }
- static void nvme_do_copy(NvmeCopyAIOCB *iocb)
- {
- NvmeRequest *req = iocb->req;
- NvmeNamespace *sns;
- NvmeNamespace *dns = req->ns;
- NvmeCopyCmd *copy = (NvmeCopyCmd *)&req->cmd;
- uint16_t prinfor = ((copy->control[0] >> 4) & 0xf);
- uint16_t prinfow = ((copy->control[2] >> 2) & 0xf);
- uint64_t slba;
- uint32_t nlb;
- size_t len;
- uint16_t status;
- uint32_t dnsid = le32_to_cpu(req->cmd.nsid);
- uint32_t snsid = dnsid;
- if (iocb->ret < 0) {
- goto done;
- }
- if (iocb->idx == iocb->nr) {
- goto done;
- }
- if (iocb->format == 2 || iocb->format == 3) {
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format,
- &slba, &nlb, &snsid, NULL, NULL, NULL);
- if (snsid != dnsid) {
- if (snsid == NVME_NSID_BROADCAST ||
- !nvme_nsid_valid(iocb->n, snsid)) {
- status = NVME_INVALID_NSID | NVME_DNR;
- goto invalid;
- }
- iocb->sns = nvme_ns(iocb->n, snsid);
- if (unlikely(!iocb->sns)) {
- status = NVME_INVALID_FIELD | NVME_DNR;
- goto invalid;
- }
- } else {
- if (((slba + nlb) > iocb->slba) &&
- ((slba + nlb) < (iocb->slba + iocb->tcl))) {
- status = NVME_CMD_OVERLAP_IO_RANGE | NVME_DNR;
- goto invalid;
- }
- }
- } else {
- nvme_copy_source_range_parse(iocb->ranges, iocb->idx, iocb->format,
- &slba, &nlb, NULL, NULL, NULL, NULL);
- }
- sns = iocb->sns;
- if ((snsid == dnsid) && NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
- ((prinfor & NVME_PRINFO_PRACT) != (prinfow & NVME_PRINFO_PRACT))) {
- status = NVME_INVALID_FIELD | NVME_DNR;
- goto invalid;
- } else if (snsid != dnsid) {
- if (!NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
- !NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
- if (!nvme_copy_matching_ns_format(sns, dns, false)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- }
- }
- if (NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
- NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
- if ((prinfor & NVME_PRINFO_PRACT) !=
- (prinfow & NVME_PRINFO_PRACT)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- } else {
- if (!nvme_copy_matching_ns_format(sns, dns, true)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- }
- }
- }
- if (!NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
- NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
- if (!(prinfow & NVME_PRINFO_PRACT)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- } else {
- if (!nvme_copy_corresp_pi_format(sns, dns, false)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- }
- }
- }
- if (NVME_ID_NS_DPS_TYPE(sns->id_ns.dps) &&
- !NVME_ID_NS_DPS_TYPE(dns->id_ns.dps)) {
- if (!(prinfor & NVME_PRINFO_PRACT)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- } else {
- if (!nvme_copy_corresp_pi_format(sns, dns, true)) {
- status = NVME_CMD_INCOMP_NS_OR_FMT | NVME_DNR;
- goto invalid;
- }
- }
- }
- }
- len = nvme_l2b(sns, nlb);
- trace_pci_nvme_copy_source_range(slba, nlb);
- if (nlb > le16_to_cpu(sns->id_ns.mssrl)) {
- status = NVME_CMD_SIZE_LIMIT | NVME_DNR;
- goto invalid;
- }
- status = nvme_check_bounds(sns, slba, nlb);
- if (status) {
- goto invalid;
- }
- if (NVME_ERR_REC_DULBE(sns->features.err_rec)) {
- status = nvme_check_dulbe(sns, slba, nlb);
- if (status) {
- goto invalid;
- }
- }
- if (sns->params.zoned) {
- status = nvme_check_zone_read(sns, slba, nlb);
- if (status) {
- goto invalid;
- }
- }
- g_free(iocb->bounce);
- iocb->bounce = g_malloc_n(le16_to_cpu(sns->id_ns.mssrl),
- sns->lbasz + sns->lbaf.ms);
- qemu_iovec_reset(&iocb->iov);
- qemu_iovec_add(&iocb->iov, iocb->bounce, len);
- block_acct_start(blk_get_stats(sns->blkconf.blk), &iocb->acct.read, 0,
- BLOCK_ACCT_READ);
- iocb->aiocb = blk_aio_preadv(sns->blkconf.blk, nvme_l2b(sns, slba),
- &iocb->iov, 0, nvme_copy_in_cb, iocb);
- return;
- invalid:
- req->status = status;
- iocb->ret = -1;
- done:
- nvme_copy_done(iocb);
- }
- static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- NvmeCopyCmd *copy = (NvmeCopyCmd *)&req->cmd;
- NvmeCopyAIOCB *iocb = blk_aio_get(&nvme_copy_aiocb_info, ns->blkconf.blk,
- nvme_misc_cb, req);
- uint16_t nr = copy->nr + 1;
- uint8_t format = copy->control[0] & 0xf;
- size_t len = sizeof(NvmeCopySourceRangeFormat0_2);
- uint16_t status;
- trace_pci_nvme_copy(nvme_cid(req), nvme_nsid(ns), nr, format);
- iocb->ranges = NULL;
- iocb->zone = NULL;
- if (!(n->id_ctrl.ocfs & (1 << format)) ||
- ((format == 2 || format == 3) &&
- !(n->features.hbs.cdfe & (1 << format)))) {
- trace_pci_nvme_err_copy_invalid_format(format);
- status = NVME_INVALID_FIELD | NVME_DNR;
- goto invalid;
- }
- if (nr > ns->id_ns.msrc + 1) {
- status = NVME_CMD_SIZE_LIMIT | NVME_DNR;
- goto invalid;
- }
- if ((ns->pif == 0x0 && (format != 0x0 && format != 0x2)) ||
- (ns->pif != 0x0 && (format != 0x1 && format != 0x3))) {
- status = NVME_INVALID_FORMAT | NVME_DNR;
- goto invalid;
- }
- if (ns->pif) {
- len = sizeof(NvmeCopySourceRangeFormat1_3);
- }
- iocb->format = format;
- iocb->ranges = g_malloc_n(nr, len);
- status = nvme_h2c(n, (uint8_t *)iocb->ranges, len * nr, req);
- if (status) {
- goto invalid;
- }
- iocb->slba = le64_to_cpu(copy->sdlba);
- if (ns->params.zoned) {
- iocb->zone = nvme_get_zone_by_slba(ns, iocb->slba);
- if (!iocb->zone) {
- status = NVME_LBA_RANGE | NVME_DNR;
- goto invalid;
- }
- status = nvme_zrm_auto(n, ns, iocb->zone);
- if (status) {
- goto invalid;
- }
- }
- status = nvme_check_copy_mcl(ns, iocb, nr);
- if (status) {
- goto invalid;
- }
- iocb->req = req;
- iocb->ret = 0;
- iocb->nr = nr;
- iocb->idx = 0;
- iocb->reftag = le32_to_cpu(copy->reftag);
- iocb->reftag |= (uint64_t)le32_to_cpu(copy->cdw3) << 32;
- qemu_iovec_init(&iocb->iov, 1);
- req->aiocb = &iocb->common;
- iocb->sns = req->ns;
- iocb->n = n;
- iocb->bounce = NULL;
- nvme_do_copy(iocb);
- return NVME_NO_COMPLETE;
- invalid:
- g_free(iocb->ranges);
- qemu_aio_unref(iocb);
- return status;
- }
- static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- NvmeNamespace *ns = req->ns;
- BlockBackend *blk = ns->blkconf.blk;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
- uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
- size_t data_len = nvme_l2b(ns, nlb);
- size_t len = data_len;
- int64_t offset = nvme_l2b(ns, slba);
- struct nvme_compare_ctx *ctx = NULL;
- uint16_t status;
- trace_pci_nvme_compare(nvme_cid(req), nvme_nsid(ns), slba, nlb);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps) && (prinfo & NVME_PRINFO_PRACT)) {
- return NVME_INVALID_PROT_INFO | NVME_DNR;
- }
- if (nvme_ns_ext(ns)) {
- len += nvme_m2b(ns, nlb);
- }
- if (NVME_ID_CTRL_CTRATT_MEM(n->id_ctrl.ctratt)) {
- status = nvme_check_mdts(n, data_len);
- } else {
- status = nvme_check_mdts(n, len);
- }
- if (status) {
- return status;
- }
- status = nvme_check_bounds(ns, slba, nlb);
- if (status) {
- return status;
- }
- if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
- status = nvme_check_dulbe(ns, slba, nlb);
- if (status) {
- return status;
- }
- }
- status = nvme_map_dptr(n, &req->sg, len, &req->cmd);
- if (status) {
- return status;
- }
- ctx = g_new(struct nvme_compare_ctx, 1);
- ctx->data.bounce = g_malloc(data_len);
- req->opaque = ctx;
- qemu_iovec_init(&ctx->data.iov, 1);
- qemu_iovec_add(&ctx->data.iov, ctx->data.bounce, data_len);
- block_acct_start(blk_get_stats(blk), &req->acct, data_len,
- BLOCK_ACCT_READ);
- req->aiocb = blk_aio_preadv(blk, offset, &ctx->data.iov, 0,
- nvme_compare_data_cb, req);
- return NVME_NO_COMPLETE;
- }
- typedef struct NvmeFlushAIOCB {
- BlockAIOCB common;
- BlockAIOCB *aiocb;
- NvmeRequest *req;
- int ret;
- NvmeNamespace *ns;
- uint32_t nsid;
- bool broadcast;
- } NvmeFlushAIOCB;
- static void nvme_flush_cancel(BlockAIOCB *acb)
- {
- NvmeFlushAIOCB *iocb = container_of(acb, NvmeFlushAIOCB, common);
- iocb->ret = -ECANCELED;
- if (iocb->aiocb) {
- blk_aio_cancel_async(iocb->aiocb);
- iocb->aiocb = NULL;
- }
- }
- static const AIOCBInfo nvme_flush_aiocb_info = {
- .aiocb_size = sizeof(NvmeFlushAIOCB),
- .cancel_async = nvme_flush_cancel,
- };
- static void nvme_do_flush(NvmeFlushAIOCB *iocb);
- static void nvme_flush_ns_cb(void *opaque, int ret)
- {
- NvmeFlushAIOCB *iocb = opaque;
- NvmeNamespace *ns = iocb->ns;
- if (ret < 0) {
- iocb->ret = ret;
- iocb->req->status = NVME_WRITE_FAULT;
- goto out;
- } else if (iocb->ret < 0) {
- goto out;
- }
- if (ns) {
- trace_pci_nvme_flush_ns(iocb->nsid);
- iocb->ns = NULL;
- iocb->aiocb = blk_aio_flush(ns->blkconf.blk, nvme_flush_ns_cb, iocb);
- return;
- }
- out:
- nvme_do_flush(iocb);
- }
- static void nvme_do_flush(NvmeFlushAIOCB *iocb)
- {
- NvmeRequest *req = iocb->req;
- NvmeCtrl *n = nvme_ctrl(req);
- int i;
- if (iocb->ret < 0) {
- goto done;
- }
- if (iocb->broadcast) {
- for (i = iocb->nsid + 1; i <= NVME_MAX_NAMESPACES; i++) {
- iocb->ns = nvme_ns(n, i);
- if (iocb->ns) {
- iocb->nsid = i;
- break;
- }
- }
- }
- if (!iocb->ns) {
- goto done;
- }
- nvme_flush_ns_cb(iocb, 0);
- return;
- done:
- iocb->common.cb(iocb->common.opaque, iocb->ret);
- qemu_aio_unref(iocb);
- }
- static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeFlushAIOCB *iocb;
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- uint16_t status;
- iocb = qemu_aio_get(&nvme_flush_aiocb_info, NULL, nvme_misc_cb, req);
- iocb->req = req;
- iocb->ret = 0;
- iocb->ns = NULL;
- iocb->nsid = 0;
- iocb->broadcast = (nsid == NVME_NSID_BROADCAST);
- if (!iocb->broadcast) {
- if (!nvme_nsid_valid(n, nsid)) {
- status = NVME_INVALID_NSID | NVME_DNR;
- goto out;
- }
- iocb->ns = nvme_ns(n, nsid);
- if (!iocb->ns) {
- status = NVME_INVALID_FIELD | NVME_DNR;
- goto out;
- }
- iocb->nsid = nsid;
- }
- req->aiocb = &iocb->common;
- nvme_do_flush(iocb);
- return NVME_NO_COMPLETE;
- out:
- qemu_aio_unref(iocb);
- return status;
- }
- static uint16_t nvme_read(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- NvmeNamespace *ns = req->ns;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
- uint8_t prinfo = NVME_RW_PRINFO(le16_to_cpu(rw->control));
- uint64_t data_size = nvme_l2b(ns, nlb);
- uint64_t mapped_size = data_size;
- uint64_t data_offset;
- BlockBackend *blk = ns->blkconf.blk;
- uint16_t status;
- if (nvme_ns_ext(ns) && !(NVME_ID_CTRL_CTRATT_MEM(n->id_ctrl.ctratt))) {
- mapped_size += nvme_m2b(ns, nlb);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- bool pract = prinfo & NVME_PRINFO_PRACT;
- if (pract && ns->lbaf.ms == nvme_pi_tuple_size(ns)) {
- mapped_size = data_size;
- }
- }
- }
- trace_pci_nvme_read(nvme_cid(req), nvme_nsid(ns), nlb, mapped_size, slba);
- status = nvme_check_mdts(n, mapped_size);
- if (status) {
- goto invalid;
- }
- status = nvme_check_bounds(ns, slba, nlb);
- if (status) {
- goto invalid;
- }
- if (ns->params.zoned) {
- status = nvme_check_zone_read(ns, slba, nlb);
- if (status) {
- trace_pci_nvme_err_zone_read_not_ok(slba, nlb, status);
- goto invalid;
- }
- }
- if (NVME_ERR_REC_DULBE(ns->features.err_rec)) {
- status = nvme_check_dulbe(ns, slba, nlb);
- if (status) {
- goto invalid;
- }
- }
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- return nvme_dif_rw(n, req);
- }
- status = nvme_map_data(n, nlb, req);
- if (status) {
- goto invalid;
- }
- data_offset = nvme_l2b(ns, slba);
- block_acct_start(blk_get_stats(blk), &req->acct, data_size,
- BLOCK_ACCT_READ);
- nvme_blk_read(blk, data_offset, BDRV_SECTOR_SIZE, nvme_rw_cb, req);
- return NVME_NO_COMPLETE;
- invalid:
- block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
- return status | NVME_DNR;
- }
- static void nvme_do_write_fdp(NvmeCtrl *n, NvmeRequest *req, uint64_t slba,
- uint32_t nlb)
- {
- NvmeNamespace *ns = req->ns;
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- uint64_t data_size = nvme_l2b(ns, nlb);
- uint32_t dw12 = le32_to_cpu(req->cmd.cdw12);
- uint8_t dtype = (dw12 >> 20) & 0xf;
- uint16_t pid = le16_to_cpu(rw->dspec);
- uint16_t ph, rg, ruhid;
- NvmeReclaimUnit *ru;
- if (dtype != NVME_DIRECTIVE_DATA_PLACEMENT ||
- !nvme_parse_pid(ns, pid, &ph, &rg)) {
- ph = 0;
- rg = 0;
- }
- ruhid = ns->fdp.phs[ph];
- ru = &ns->endgrp->fdp.ruhs[ruhid].rus[rg];
- nvme_fdp_stat_inc(&ns->endgrp->fdp.hbmw, data_size);
- nvme_fdp_stat_inc(&ns->endgrp->fdp.mbmw, data_size);
- while (nlb) {
- if (nlb < ru->ruamw) {
- ru->ruamw -= nlb;
- break;
- }
- nlb -= ru->ruamw;
- nvme_update_ruh(n, ns, pid);
- }
- }
- static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
- bool wrz)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
- NvmeNamespace *ns = req->ns;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb) + 1;
- uint16_t ctrl = le16_to_cpu(rw->control);
- uint8_t prinfo = NVME_RW_PRINFO(ctrl);
- uint64_t data_size = nvme_l2b(ns, nlb);
- uint64_t mapped_size = data_size;
- uint64_t data_offset;
- NvmeZone *zone;
- NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
- BlockBackend *blk = ns->blkconf.blk;
- uint16_t status;
- if (nvme_ns_ext(ns) && !(NVME_ID_CTRL_CTRATT_MEM(n->id_ctrl.ctratt))) {
- mapped_size += nvme_m2b(ns, nlb);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- bool pract = prinfo & NVME_PRINFO_PRACT;
- if (pract && ns->lbaf.ms == nvme_pi_tuple_size(ns)) {
- mapped_size -= nvme_m2b(ns, nlb);
- }
- }
- }
- trace_pci_nvme_write(nvme_cid(req), nvme_io_opc_str(rw->opcode),
- nvme_nsid(ns), nlb, mapped_size, slba);
- if (!wrz) {
- status = nvme_check_mdts(n, mapped_size);
- if (status) {
- goto invalid;
- }
- }
- status = nvme_check_bounds(ns, slba, nlb);
- if (status) {
- goto invalid;
- }
- if (ns->params.zoned) {
- zone = nvme_get_zone_by_slba(ns, slba);
- assert(zone);
- if (append) {
- bool piremap = !!(ctrl & NVME_RW_PIREMAP);
- if (unlikely(zone->d.za & NVME_ZA_ZRWA_VALID)) {
- return NVME_INVALID_ZONE_OP | NVME_DNR;
- }
- if (unlikely(slba != zone->d.zslba)) {
- trace_pci_nvme_err_append_not_at_start(slba, zone->d.zslba);
- status = NVME_INVALID_FIELD;
- goto invalid;
- }
- if (n->params.zasl &&
- data_size > (uint64_t)n->page_size << n->params.zasl) {
- trace_pci_nvme_err_zasl(data_size);
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- slba = zone->w_ptr;
- rw->slba = cpu_to_le64(slba);
- res->slba = cpu_to_le64(slba);
- switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- case NVME_ID_NS_DPS_TYPE_1:
- if (!piremap) {
- return NVME_INVALID_PROT_INFO | NVME_DNR;
- }
- /* fallthrough */
- case NVME_ID_NS_DPS_TYPE_2:
- if (piremap) {
- uint32_t reftag = le32_to_cpu(rw->reftag);
- rw->reftag = cpu_to_le32(reftag + (slba - zone->d.zslba));
- }
- break;
- case NVME_ID_NS_DPS_TYPE_3:
- if (piremap) {
- return NVME_INVALID_PROT_INFO | NVME_DNR;
- }
- break;
- }
- }
- status = nvme_check_zone_write(ns, zone, slba, nlb);
- if (status) {
- goto invalid;
- }
- status = nvme_zrm_auto(n, ns, zone);
- if (status) {
- goto invalid;
- }
- if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
- zone->w_ptr += nlb;
- }
- } else if (ns->endgrp && ns->endgrp->fdp.enabled) {
- nvme_do_write_fdp(n, req, slba, nlb);
- }
- data_offset = nvme_l2b(ns, slba);
- if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- return nvme_dif_rw(n, req);
- }
- if (!wrz) {
- status = nvme_map_data(n, nlb, req);
- if (status) {
- goto invalid;
- }
- block_acct_start(blk_get_stats(blk), &req->acct, data_size,
- BLOCK_ACCT_WRITE);
- nvme_blk_write(blk, data_offset, BDRV_SECTOR_SIZE, nvme_rw_cb, req);
- } else {
- req->aiocb = blk_aio_pwrite_zeroes(blk, data_offset, data_size,
- BDRV_REQ_MAY_UNMAP, nvme_rw_cb,
- req);
- }
- return NVME_NO_COMPLETE;
- invalid:
- block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
- return status | NVME_DNR;
- }
- static inline uint16_t nvme_write(NvmeCtrl *n, NvmeRequest *req)
- {
- return nvme_do_write(n, req, false, false);
- }
- static inline uint16_t nvme_write_zeroes(NvmeCtrl *n, NvmeRequest *req)
- {
- return nvme_do_write(n, req, false, true);
- }
- static inline uint16_t nvme_zone_append(NvmeCtrl *n, NvmeRequest *req)
- {
- return nvme_do_write(n, req, true, false);
- }
- static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace *ns, NvmeCmd *c,
- uint64_t *slba, uint32_t *zone_idx)
- {
- uint32_t dw10 = le32_to_cpu(c->cdw10);
- uint32_t dw11 = le32_to_cpu(c->cdw11);
- if (!ns->params.zoned) {
- trace_pci_nvme_err_invalid_opc(c->opcode);
- return NVME_INVALID_OPCODE | NVME_DNR;
- }
- *slba = ((uint64_t)dw11) << 32 | dw10;
- if (unlikely(*slba >= ns->id_ns.nsze)) {
- trace_pci_nvme_err_invalid_lba_range(*slba, 0, ns->id_ns.nsze);
- *slba = 0;
- return NVME_LBA_RANGE | NVME_DNR;
- }
- *zone_idx = nvme_zone_idx(ns, *slba);
- assert(*zone_idx < ns->num_zones);
- return NVME_SUCCESS;
- }
- typedef uint16_t (*op_handler_t)(NvmeNamespace *, NvmeZone *, NvmeZoneState,
- NvmeRequest *);
- enum NvmeZoneProcessingMask {
- NVME_PROC_CURRENT_ZONE = 0,
- NVME_PROC_OPENED_ZONES = 1 << 0,
- NVME_PROC_CLOSED_ZONES = 1 << 1,
- NVME_PROC_READ_ONLY_ZONES = 1 << 2,
- NVME_PROC_FULL_ZONES = 1 << 3,
- };
- static uint16_t nvme_open_zone(NvmeNamespace *ns, NvmeZone *zone,
- NvmeZoneState state, NvmeRequest *req)
- {
- NvmeZoneSendCmd *cmd = (NvmeZoneSendCmd *)&req->cmd;
- int flags = 0;
- if (cmd->zsflags & NVME_ZSFLAG_ZRWA_ALLOC) {
- uint16_t ozcs = le16_to_cpu(ns->id_ns_zoned->ozcs);
- if (!(ozcs & NVME_ID_NS_ZONED_OZCS_ZRWASUP)) {
- return NVME_INVALID_ZONE_OP | NVME_DNR;
- }
- if (zone->w_ptr % ns->zns.zrwafg) {
- return NVME_NOZRWA | NVME_DNR;
- }
- flags = NVME_ZRM_ZRWA;
- }
- return nvme_zrm_open_flags(nvme_ctrl(req), ns, zone, flags);
- }
- static uint16_t nvme_close_zone(NvmeNamespace *ns, NvmeZone *zone,
- NvmeZoneState state, NvmeRequest *req)
- {
- return nvme_zrm_close(ns, zone);
- }
- static uint16_t nvme_finish_zone(NvmeNamespace *ns, NvmeZone *zone,
- NvmeZoneState state, NvmeRequest *req)
- {
- return nvme_zrm_finish(ns, zone);
- }
- static uint16_t nvme_offline_zone(NvmeNamespace *ns, NvmeZone *zone,
- NvmeZoneState state, NvmeRequest *req)
- {
- switch (state) {
- case NVME_ZONE_STATE_READ_ONLY:
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_OFFLINE);
- /* fall through */
- case NVME_ZONE_STATE_OFFLINE:
- return NVME_SUCCESS;
- default:
- return NVME_ZONE_INVAL_TRANSITION;
- }
- }
- static uint16_t nvme_set_zd_ext(NvmeNamespace *ns, NvmeZone *zone)
- {
- uint16_t status;
- uint8_t state = nvme_get_zone_state(zone);
- if (state == NVME_ZONE_STATE_EMPTY) {
- status = nvme_aor_check(ns, 1, 0);
- if (status) {
- return status;
- }
- nvme_aor_inc_active(ns);
- zone->d.za |= NVME_ZA_ZD_EXT_VALID;
- nvme_assign_zone_state(ns, zone, NVME_ZONE_STATE_CLOSED);
- return NVME_SUCCESS;
- }
- return NVME_ZONE_INVAL_TRANSITION;
- }
- static uint16_t nvme_bulk_proc_zone(NvmeNamespace *ns, NvmeZone *zone,
- enum NvmeZoneProcessingMask proc_mask,
- op_handler_t op_hndlr, NvmeRequest *req)
- {
- uint16_t status = NVME_SUCCESS;
- NvmeZoneState zs = nvme_get_zone_state(zone);
- bool proc_zone;
- switch (zs) {
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- proc_zone = proc_mask & NVME_PROC_OPENED_ZONES;
- break;
- case NVME_ZONE_STATE_CLOSED:
- proc_zone = proc_mask & NVME_PROC_CLOSED_ZONES;
- break;
- case NVME_ZONE_STATE_READ_ONLY:
- proc_zone = proc_mask & NVME_PROC_READ_ONLY_ZONES;
- break;
- case NVME_ZONE_STATE_FULL:
- proc_zone = proc_mask & NVME_PROC_FULL_ZONES;
- break;
- default:
- proc_zone = false;
- }
- if (proc_zone) {
- status = op_hndlr(ns, zone, zs, req);
- }
- return status;
- }
- static uint16_t nvme_do_zone_op(NvmeNamespace *ns, NvmeZone *zone,
- enum NvmeZoneProcessingMask proc_mask,
- op_handler_t op_hndlr, NvmeRequest *req)
- {
- NvmeZone *next;
- uint16_t status = NVME_SUCCESS;
- int i;
- if (!proc_mask) {
- status = op_hndlr(ns, zone, nvme_get_zone_state(zone), req);
- } else {
- if (proc_mask & NVME_PROC_CLOSED_ZONES) {
- QTAILQ_FOREACH_SAFE(zone, &ns->closed_zones, entry, next) {
- status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
- req);
- if (status && status != NVME_NO_COMPLETE) {
- goto out;
- }
- }
- }
- if (proc_mask & NVME_PROC_OPENED_ZONES) {
- QTAILQ_FOREACH_SAFE(zone, &ns->imp_open_zones, entry, next) {
- status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
- req);
- if (status && status != NVME_NO_COMPLETE) {
- goto out;
- }
- }
- QTAILQ_FOREACH_SAFE(zone, &ns->exp_open_zones, entry, next) {
- status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
- req);
- if (status && status != NVME_NO_COMPLETE) {
- goto out;
- }
- }
- }
- if (proc_mask & NVME_PROC_FULL_ZONES) {
- QTAILQ_FOREACH_SAFE(zone, &ns->full_zones, entry, next) {
- status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
- req);
- if (status && status != NVME_NO_COMPLETE) {
- goto out;
- }
- }
- }
- if (proc_mask & NVME_PROC_READ_ONLY_ZONES) {
- for (i = 0; i < ns->num_zones; i++, zone++) {
- status = nvme_bulk_proc_zone(ns, zone, proc_mask, op_hndlr,
- req);
- if (status && status != NVME_NO_COMPLETE) {
- goto out;
- }
- }
- }
- }
- out:
- return status;
- }
- typedef struct NvmeZoneResetAIOCB {
- BlockAIOCB common;
- BlockAIOCB *aiocb;
- NvmeRequest *req;
- int ret;
- bool all;
- int idx;
- NvmeZone *zone;
- } NvmeZoneResetAIOCB;
- static void nvme_zone_reset_cancel(BlockAIOCB *aiocb)
- {
- NvmeZoneResetAIOCB *iocb = container_of(aiocb, NvmeZoneResetAIOCB, common);
- NvmeRequest *req = iocb->req;
- NvmeNamespace *ns = req->ns;
- iocb->idx = ns->num_zones;
- iocb->ret = -ECANCELED;
- if (iocb->aiocb) {
- blk_aio_cancel_async(iocb->aiocb);
- iocb->aiocb = NULL;
- }
- }
- static const AIOCBInfo nvme_zone_reset_aiocb_info = {
- .aiocb_size = sizeof(NvmeZoneResetAIOCB),
- .cancel_async = nvme_zone_reset_cancel,
- };
- static void nvme_zone_reset_cb(void *opaque, int ret);
- static void nvme_zone_reset_epilogue_cb(void *opaque, int ret)
- {
- NvmeZoneResetAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *ns = req->ns;
- int64_t moff;
- int count;
- if (ret < 0 || iocb->ret < 0 || !ns->lbaf.ms) {
- goto out;
- }
- moff = nvme_moff(ns, iocb->zone->d.zslba);
- count = nvme_m2b(ns, ns->zone_size);
- iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, moff, count,
- BDRV_REQ_MAY_UNMAP,
- nvme_zone_reset_cb, iocb);
- return;
- out:
- nvme_zone_reset_cb(iocb, ret);
- }
- static void nvme_zone_reset_cb(void *opaque, int ret)
- {
- NvmeZoneResetAIOCB *iocb = opaque;
- NvmeRequest *req = iocb->req;
- NvmeNamespace *ns = req->ns;
- if (iocb->ret < 0) {
- goto done;
- } else if (ret < 0) {
- iocb->ret = ret;
- goto done;
- }
- if (iocb->zone) {
- nvme_zrm_reset(ns, iocb->zone);
- if (!iocb->all) {
- goto done;
- }
- }
- while (iocb->idx < ns->num_zones) {
- NvmeZone *zone = &ns->zone_array[iocb->idx++];
- switch (nvme_get_zone_state(zone)) {
- case NVME_ZONE_STATE_EMPTY:
- if (!iocb->all) {
- goto done;
- }
- continue;
- case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- case NVME_ZONE_STATE_IMPLICITLY_OPEN:
- case NVME_ZONE_STATE_CLOSED:
- case NVME_ZONE_STATE_FULL:
- iocb->zone = zone;
- break;
- default:
- continue;
- }
- trace_pci_nvme_zns_zone_reset(zone->d.zslba);
- iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk,
- nvme_l2b(ns, zone->d.zslba),
- nvme_l2b(ns, ns->zone_size),
- BDRV_REQ_MAY_UNMAP,
- nvme_zone_reset_epilogue_cb,
- iocb);
- return;
- }
- done:
- iocb->aiocb = NULL;
- iocb->common.cb(iocb->common.opaque, iocb->ret);
- qemu_aio_unref(iocb);
- }
- static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
- uint64_t elba, NvmeRequest *req)
- {
- NvmeNamespace *ns = req->ns;
- uint16_t ozcs = le16_to_cpu(ns->id_ns_zoned->ozcs);
- uint64_t wp = zone->d.wp;
- uint32_t nlb = elba - wp + 1;
- uint16_t status;
- if (!(ozcs & NVME_ID_NS_ZONED_OZCS_ZRWASUP)) {
- return NVME_INVALID_ZONE_OP | NVME_DNR;
- }
- if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (elba < wp || elba > wp + ns->zns.zrwas) {
- return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
- }
- if (nlb % ns->zns.zrwafg) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- status = nvme_zrm_auto(n, ns, zone);
- if (status) {
- return status;
- }
- zone->w_ptr += nlb;
- nvme_advance_zone_wp(ns, zone, nlb);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeZoneSendCmd *cmd = (NvmeZoneSendCmd *)&req->cmd;
- NvmeNamespace *ns = req->ns;
- NvmeZone *zone;
- NvmeZoneResetAIOCB *iocb;
- uint8_t *zd_ext;
- uint64_t slba = 0;
- uint32_t zone_idx = 0;
- uint16_t status;
- uint8_t action = cmd->zsa;
- bool all;
- enum NvmeZoneProcessingMask proc_mask = NVME_PROC_CURRENT_ZONE;
- all = cmd->zsflags & NVME_ZSFLAG_SELECT_ALL;
- req->status = NVME_SUCCESS;
- if (!all) {
- status = nvme_get_mgmt_zone_slba_idx(ns, &req->cmd, &slba, &zone_idx);
- if (status) {
- return status;
- }
- }
- zone = &ns->zone_array[zone_idx];
- if (slba != zone->d.zslba && action != NVME_ZONE_ACTION_ZRWA_FLUSH) {
- trace_pci_nvme_err_unaligned_zone_cmd(action, slba, zone->d.zslba);
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- switch (action) {
- case NVME_ZONE_ACTION_OPEN:
- if (all) {
- proc_mask = NVME_PROC_CLOSED_ZONES;
- }
- trace_pci_nvme_open_zone(slba, zone_idx, all);
- status = nvme_do_zone_op(ns, zone, proc_mask, nvme_open_zone, req);
- break;
- case NVME_ZONE_ACTION_CLOSE:
- if (all) {
- proc_mask = NVME_PROC_OPENED_ZONES;
- }
- trace_pci_nvme_close_zone(slba, zone_idx, all);
- status = nvme_do_zone_op(ns, zone, proc_mask, nvme_close_zone, req);
- break;
- case NVME_ZONE_ACTION_FINISH:
- if (all) {
- proc_mask = NVME_PROC_OPENED_ZONES | NVME_PROC_CLOSED_ZONES;
- }
- trace_pci_nvme_finish_zone(slba, zone_idx, all);
- status = nvme_do_zone_op(ns, zone, proc_mask, nvme_finish_zone, req);
- break;
- case NVME_ZONE_ACTION_RESET:
- trace_pci_nvme_reset_zone(slba, zone_idx, all);
- iocb = blk_aio_get(&nvme_zone_reset_aiocb_info, ns->blkconf.blk,
- nvme_misc_cb, req);
- iocb->req = req;
- iocb->ret = 0;
- iocb->all = all;
- iocb->idx = zone_idx;
- iocb->zone = NULL;
- req->aiocb = &iocb->common;
- nvme_zone_reset_cb(iocb, 0);
- return NVME_NO_COMPLETE;
- case NVME_ZONE_ACTION_OFFLINE:
- if (all) {
- proc_mask = NVME_PROC_READ_ONLY_ZONES;
- }
- trace_pci_nvme_offline_zone(slba, zone_idx, all);
- status = nvme_do_zone_op(ns, zone, proc_mask, nvme_offline_zone, req);
- break;
- case NVME_ZONE_ACTION_SET_ZD_EXT:
- trace_pci_nvme_set_descriptor_extension(slba, zone_idx);
- if (all || !ns->params.zd_extension_size) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- zd_ext = nvme_get_zd_extension(ns, zone_idx);
- status = nvme_h2c(n, zd_ext, ns->params.zd_extension_size, req);
- if (status) {
- trace_pci_nvme_err_zd_extension_map_error(zone_idx);
- return status;
- }
- status = nvme_set_zd_ext(ns, zone);
- if (status == NVME_SUCCESS) {
- trace_pci_nvme_zd_extension_set(zone_idx);
- return status;
- }
- break;
- case NVME_ZONE_ACTION_ZRWA_FLUSH:
- if (all) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- return nvme_zone_mgmt_send_zrwa_flush(n, zone, slba, req);
- default:
- trace_pci_nvme_err_invalid_mgmt_action(action);
- status = NVME_INVALID_FIELD;
- }
- if (status == NVME_ZONE_INVAL_TRANSITION) {
- trace_pci_nvme_err_invalid_zone_state_transition(action, slba,
- zone->d.za);
- }
- if (status) {
- status |= NVME_DNR;
- }
- return status;
- }
- static bool nvme_zone_matches_filter(uint32_t zafs, NvmeZone *zl)
- {
- NvmeZoneState zs = nvme_get_zone_state(zl);
- switch (zafs) {
- case NVME_ZONE_REPORT_ALL:
- return true;
- case NVME_ZONE_REPORT_EMPTY:
- return zs == NVME_ZONE_STATE_EMPTY;
- case NVME_ZONE_REPORT_IMPLICITLY_OPEN:
- return zs == NVME_ZONE_STATE_IMPLICITLY_OPEN;
- case NVME_ZONE_REPORT_EXPLICITLY_OPEN:
- return zs == NVME_ZONE_STATE_EXPLICITLY_OPEN;
- case NVME_ZONE_REPORT_CLOSED:
- return zs == NVME_ZONE_STATE_CLOSED;
- case NVME_ZONE_REPORT_FULL:
- return zs == NVME_ZONE_STATE_FULL;
- case NVME_ZONE_REPORT_READ_ONLY:
- return zs == NVME_ZONE_STATE_READ_ONLY;
- case NVME_ZONE_REPORT_OFFLINE:
- return zs == NVME_ZONE_STATE_OFFLINE;
- default:
- return false;
- }
- }
- static uint16_t nvme_zone_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- NvmeNamespace *ns = req->ns;
- /* cdw12 is zero-based number of dwords to return. Convert to bytes */
- uint32_t data_size = (le32_to_cpu(cmd->cdw12) + 1) << 2;
- uint32_t dw13 = le32_to_cpu(cmd->cdw13);
- uint32_t zone_idx, zra, zrasf, partial;
- uint64_t max_zones, nr_zones = 0;
- uint16_t status;
- uint64_t slba;
- NvmeZoneDescr *z;
- NvmeZone *zone;
- NvmeZoneReportHeader *header;
- void *buf, *buf_p;
- size_t zone_entry_sz;
- int i;
- req->status = NVME_SUCCESS;
- status = nvme_get_mgmt_zone_slba_idx(ns, cmd, &slba, &zone_idx);
- if (status) {
- return status;
- }
- zra = dw13 & 0xff;
- if (zra != NVME_ZONE_REPORT && zra != NVME_ZONE_REPORT_EXTENDED) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (zra == NVME_ZONE_REPORT_EXTENDED && !ns->params.zd_extension_size) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- zrasf = (dw13 >> 8) & 0xff;
- if (zrasf > NVME_ZONE_REPORT_OFFLINE) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (data_size < sizeof(NvmeZoneReportHeader)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- status = nvme_check_mdts(n, data_size);
- if (status) {
- return status;
- }
- partial = (dw13 >> 16) & 0x01;
- zone_entry_sz = sizeof(NvmeZoneDescr);
- if (zra == NVME_ZONE_REPORT_EXTENDED) {
- zone_entry_sz += ns->params.zd_extension_size;
- }
- max_zones = (data_size - sizeof(NvmeZoneReportHeader)) / zone_entry_sz;
- buf = g_malloc0(data_size);
- zone = &ns->zone_array[zone_idx];
- for (i = zone_idx; i < ns->num_zones; i++) {
- if (partial && nr_zones >= max_zones) {
- break;
- }
- if (nvme_zone_matches_filter(zrasf, zone++)) {
- nr_zones++;
- }
- }
- header = buf;
- header->nr_zones = cpu_to_le64(nr_zones);
- buf_p = buf + sizeof(NvmeZoneReportHeader);
- for (; zone_idx < ns->num_zones && max_zones > 0; zone_idx++) {
- zone = &ns->zone_array[zone_idx];
- if (nvme_zone_matches_filter(zrasf, zone)) {
- z = buf_p;
- buf_p += sizeof(NvmeZoneDescr);
- z->zt = zone->d.zt;
- z->zs = zone->d.zs;
- z->zcap = cpu_to_le64(zone->d.zcap);
- z->zslba = cpu_to_le64(zone->d.zslba);
- z->za = zone->d.za;
- if (nvme_wp_is_valid(zone)) {
- z->wp = cpu_to_le64(zone->d.wp);
- } else {
- z->wp = cpu_to_le64(~0ULL);
- }
- if (zra == NVME_ZONE_REPORT_EXTENDED) {
- if (zone->d.za & NVME_ZA_ZD_EXT_VALID) {
- memcpy(buf_p, nvme_get_zd_extension(ns, zone_idx),
- ns->params.zd_extension_size);
- }
- buf_p += ns->params.zd_extension_size;
- }
- max_zones--;
- }
- }
- status = nvme_c2h(n, (uint8_t *)buf, data_size, req);
- g_free(buf);
- return status;
- }
- static uint16_t nvme_io_mgmt_recv_ruhs(NvmeCtrl *n, NvmeRequest *req,
- size_t len)
- {
- NvmeNamespace *ns = req->ns;
- NvmeEnduranceGroup *endgrp;
- NvmeRuhStatus *hdr;
- NvmeRuhStatusDescr *ruhsd;
- unsigned int nruhsd;
- uint16_t rg, ph, *ruhid;
- size_t trans_len;
- g_autofree uint8_t *buf = NULL;
- if (!n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (ns->params.nsid == 0 || ns->params.nsid == 0xffffffff) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- if (!n->subsys->endgrp.fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- endgrp = ns->endgrp;
- nruhsd = ns->fdp.nphs * endgrp->fdp.nrg;
- trans_len = sizeof(NvmeRuhStatus) + nruhsd * sizeof(NvmeRuhStatusDescr);
- buf = g_malloc0(trans_len);
- trans_len = MIN(trans_len, len);
- hdr = (NvmeRuhStatus *)buf;
- ruhsd = (NvmeRuhStatusDescr *)(buf + sizeof(NvmeRuhStatus));
- hdr->nruhsd = cpu_to_le16(nruhsd);
- ruhid = ns->fdp.phs;
- for (ph = 0; ph < ns->fdp.nphs; ph++, ruhid++) {
- NvmeRuHandle *ruh = &endgrp->fdp.ruhs[*ruhid];
- for (rg = 0; rg < endgrp->fdp.nrg; rg++, ruhsd++) {
- uint16_t pid = nvme_make_pid(ns, rg, ph);
- ruhsd->pid = cpu_to_le16(pid);
- ruhsd->ruhid = *ruhid;
- ruhsd->earutr = 0;
- ruhsd->ruamw = cpu_to_le64(ruh->rus[rg].ruamw);
- }
- }
- return nvme_c2h(n, buf, trans_len, req);
- }
- static uint16_t nvme_io_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
- uint32_t numd = le32_to_cpu(cmd->cdw11);
- uint8_t mo = (cdw10 & 0xff);
- size_t len = (numd + 1) << 2;
- switch (mo) {
- case NVME_IOMR_MO_NOP:
- return 0;
- case NVME_IOMR_MO_RUH_STATUS:
- return nvme_io_mgmt_recv_ruhs(n, req, len);
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- };
- }
- static uint16_t nvme_io_mgmt_send_ruh_update(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- NvmeNamespace *ns = req->ns;
- uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
- uint16_t ret = NVME_SUCCESS;
- uint32_t npid = (cdw10 >> 16) + 1;
- unsigned int i = 0;
- g_autofree uint16_t *pids = NULL;
- uint32_t maxnpid;
- if (!ns->endgrp || !ns->endgrp->fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- maxnpid = n->subsys->endgrp.fdp.nrg * n->subsys->endgrp.fdp.nruh;
- if (unlikely(npid >= MIN(NVME_FDP_MAXPIDS, maxnpid))) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- pids = g_new(uint16_t, npid);
- ret = nvme_h2c(n, pids, npid * sizeof(uint16_t), req);
- if (ret) {
- return ret;
- }
- for (; i < npid; i++) {
- if (!nvme_update_ruh(n, ns, pids[i])) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- return ret;
- }
- static uint16_t nvme_io_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t cdw10 = le32_to_cpu(cmd->cdw10);
- uint8_t mo = (cdw10 & 0xff);
- switch (mo) {
- case NVME_IOMS_MO_NOP:
- return 0;
- case NVME_IOMS_MO_RUH_UPDATE:
- return nvme_io_mgmt_send_ruh_update(n, req);
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- };
- }
- static uint16_t __nvme_io_cmd_nvm(NvmeCtrl *n, NvmeRequest *req)
- {
- switch (req->cmd.opcode) {
- case NVME_CMD_WRITE:
- return nvme_write(n, req);
- case NVME_CMD_READ:
- return nvme_read(n, req);
- case NVME_CMD_COMPARE:
- return nvme_compare(n, req);
- case NVME_CMD_WRITE_ZEROES:
- return nvme_write_zeroes(n, req);
- case NVME_CMD_DSM:
- return nvme_dsm(n, req);
- case NVME_CMD_VERIFY:
- return nvme_verify(n, req);
- case NVME_CMD_COPY:
- return nvme_copy(n, req);
- case NVME_CMD_IO_MGMT_RECV:
- return nvme_io_mgmt_recv(n, req);
- case NVME_CMD_IO_MGMT_SEND:
- return nvme_io_mgmt_send(n, req);
- }
- g_assert_not_reached();
- }
- static uint16_t nvme_io_cmd_nvm(NvmeCtrl *n, NvmeRequest *req)
- {
- if (!(n->cse.iocs.nvm[req->cmd.opcode] & NVME_CMD_EFF_CSUPP)) {
- trace_pci_nvme_err_invalid_opc(req->cmd.opcode);
- return NVME_INVALID_OPCODE | NVME_DNR;
- }
- return __nvme_io_cmd_nvm(n, req);
- }
- static uint16_t nvme_io_cmd_zoned(NvmeCtrl *n, NvmeRequest *req)
- {
- if (!(n->cse.iocs.zoned[req->cmd.opcode] & NVME_CMD_EFF_CSUPP)) {
- trace_pci_nvme_err_invalid_opc(req->cmd.opcode);
- return NVME_INVALID_OPCODE | NVME_DNR;
- }
- switch (req->cmd.opcode) {
- case NVME_CMD_ZONE_APPEND:
- return nvme_zone_append(n, req);
- case NVME_CMD_ZONE_MGMT_SEND:
- return nvme_zone_mgmt_send(n, req);
- case NVME_CMD_ZONE_MGMT_RECV:
- return nvme_zone_mgmt_recv(n, req);
- }
- return __nvme_io_cmd_nvm(n, req);
- }
- static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns;
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- trace_pci_nvme_io_cmd(nvme_cid(req), nsid, nvme_sqid(req),
- req->cmd.opcode, nvme_io_opc_str(req->cmd.opcode));
- /*
- * In the base NVM command set, Flush may apply to all namespaces
- * (indicated by NSID being set to FFFFFFFFh). But if that feature is used
- * along with TP 4056 (Namespace Types), it may be pretty screwed up.
- *
- * If NSID is indeed set to FFFFFFFFh, we simply cannot associate the
- * opcode with a specific command since we cannot determine a unique I/O
- * command set. Opcode 0h could have any other meaning than something
- * equivalent to flushing and say it DOES have completely different
- * semantics in some other command set - does an NSID of FFFFFFFFh then
- * mean "for all namespaces, apply whatever command set specific command
- * that uses the 0h opcode?" Or does it mean "for all namespaces, apply
- * whatever command that uses the 0h opcode if, and only if, it allows NSID
- * to be FFFFFFFFh"?
- *
- * Anyway (and luckily), for now, we do not care about this since the
- * device only supports namespace types that includes the NVM Flush command
- * (NVM and Zoned), so always do an NVM Flush.
- */
- if (req->cmd.opcode == NVME_CMD_FLUSH) {
- return nvme_flush(n, req);
- }
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (ns->status) {
- return ns->status;
- }
- if (NVME_CMD_FLAGS_FUSE(req->cmd.flags)) {
- return NVME_INVALID_FIELD;
- }
- req->ns = ns;
- switch (ns->csi) {
- case NVME_CSI_NVM:
- return nvme_io_cmd_nvm(n, req);
- case NVME_CSI_ZONED:
- return nvme_io_cmd_zoned(n, req);
- }
- g_assert_not_reached();
- }
- static void nvme_cq_notifier(EventNotifier *e)
- {
- NvmeCQueue *cq = container_of(e, NvmeCQueue, notifier);
- NvmeCtrl *n = cq->ctrl;
- if (!event_notifier_test_and_clear(e)) {
- return;
- }
- nvme_update_cq_head(cq);
- if (cq->tail == cq->head) {
- if (cq->irq_enabled) {
- n->cq_pending--;
- }
- nvme_irq_deassert(n, cq);
- }
- qemu_bh_schedule(cq->bh);
- }
- static int nvme_init_cq_ioeventfd(NvmeCQueue *cq)
- {
- NvmeCtrl *n = cq->ctrl;
- uint16_t offset = (cq->cqid << 3) + (1 << 2);
- int ret;
- ret = event_notifier_init(&cq->notifier, 0);
- if (ret < 0) {
- return ret;
- }
- event_notifier_set_handler(&cq->notifier, nvme_cq_notifier);
- memory_region_add_eventfd(&n->iomem,
- 0x1000 + offset, 4, false, 0, &cq->notifier);
- return 0;
- }
- static void nvme_sq_notifier(EventNotifier *e)
- {
- NvmeSQueue *sq = container_of(e, NvmeSQueue, notifier);
- if (!event_notifier_test_and_clear(e)) {
- return;
- }
- nvme_process_sq(sq);
- }
- static int nvme_init_sq_ioeventfd(NvmeSQueue *sq)
- {
- NvmeCtrl *n = sq->ctrl;
- uint16_t offset = sq->sqid << 3;
- int ret;
- ret = event_notifier_init(&sq->notifier, 0);
- if (ret < 0) {
- return ret;
- }
- event_notifier_set_handler(&sq->notifier, nvme_sq_notifier);
- memory_region_add_eventfd(&n->iomem,
- 0x1000 + offset, 4, false, 0, &sq->notifier);
- return 0;
- }
- static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
- {
- uint16_t offset = sq->sqid << 3;
- n->sq[sq->sqid] = NULL;
- qemu_bh_delete(sq->bh);
- if (sq->ioeventfd_enabled) {
- memory_region_del_eventfd(&n->iomem,
- 0x1000 + offset, 4, false, 0, &sq->notifier);
- event_notifier_set_handler(&sq->notifier, NULL);
- event_notifier_cleanup(&sq->notifier);
- }
- g_free(sq->io_req);
- if (sq->sqid) {
- g_free(sq);
- }
- }
- static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
- NvmeRequest *r, *next;
- NvmeSQueue *sq;
- NvmeCQueue *cq;
- uint16_t qid = le16_to_cpu(c->qid);
- if (unlikely(!qid || nvme_check_sqid(n, qid))) {
- trace_pci_nvme_err_invalid_del_sq(qid);
- return NVME_INVALID_QID | NVME_DNR;
- }
- trace_pci_nvme_del_sq(qid);
- sq = n->sq[qid];
- while (!QTAILQ_EMPTY(&sq->out_req_list)) {
- r = QTAILQ_FIRST(&sq->out_req_list);
- assert(r->aiocb);
- r->status = NVME_CMD_ABORT_SQ_DEL;
- blk_aio_cancel(r->aiocb);
- }
- assert(QTAILQ_EMPTY(&sq->out_req_list));
- if (!nvme_check_cqid(n, sq->cqid)) {
- cq = n->cq[sq->cqid];
- QTAILQ_REMOVE(&cq->sq_list, sq, entry);
- nvme_post_cqes(cq);
- QTAILQ_FOREACH_SAFE(r, &cq->req_list, entry, next) {
- if (r->sq == sq) {
- QTAILQ_REMOVE(&cq->req_list, r, entry);
- QTAILQ_INSERT_TAIL(&sq->req_list, r, entry);
- }
- }
- }
- nvme_free_sq(sq, n);
- return NVME_SUCCESS;
- }
- static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
- uint16_t sqid, uint16_t cqid, uint16_t size)
- {
- int i;
- NvmeCQueue *cq;
- sq->ctrl = n;
- sq->dma_addr = dma_addr;
- sq->sqid = sqid;
- sq->size = size;
- sq->cqid = cqid;
- sq->head = sq->tail = 0;
- sq->io_req = g_new0(NvmeRequest, sq->size);
- QTAILQ_INIT(&sq->req_list);
- QTAILQ_INIT(&sq->out_req_list);
- for (i = 0; i < sq->size; i++) {
- sq->io_req[i].sq = sq;
- QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
- }
- sq->bh = qemu_bh_new_guarded(nvme_process_sq, sq,
- &DEVICE(sq->ctrl)->mem_reentrancy_guard);
- if (n->dbbuf_enabled) {
- sq->db_addr = n->dbbuf_dbs + (sqid << 3);
- sq->ei_addr = n->dbbuf_eis + (sqid << 3);
- if (n->params.ioeventfd && sq->sqid != 0) {
- if (!nvme_init_sq_ioeventfd(sq)) {
- sq->ioeventfd_enabled = true;
- }
- }
- }
- assert(n->cq[cqid]);
- cq = n->cq[cqid];
- QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
- n->sq[sqid] = sq;
- }
- static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeSQueue *sq;
- NvmeCreateSq *c = (NvmeCreateSq *)&req->cmd;
- uint16_t cqid = le16_to_cpu(c->cqid);
- uint16_t sqid = le16_to_cpu(c->sqid);
- uint16_t qsize = le16_to_cpu(c->qsize);
- uint16_t qflags = le16_to_cpu(c->sq_flags);
- uint64_t prp1 = le64_to_cpu(c->prp1);
- trace_pci_nvme_create_sq(prp1, sqid, cqid, qsize, qflags);
- if (unlikely(!cqid || nvme_check_cqid(n, cqid))) {
- trace_pci_nvme_err_invalid_create_sq_cqid(cqid);
- return NVME_INVALID_CQID | NVME_DNR;
- }
- if (unlikely(!sqid || sqid > n->conf_ioqpairs || n->sq[sqid] != NULL)) {
- trace_pci_nvme_err_invalid_create_sq_sqid(sqid);
- return NVME_INVALID_QID | NVME_DNR;
- }
- if (unlikely(!qsize || qsize > NVME_CAP_MQES(ldq_le_p(&n->bar.cap)))) {
- trace_pci_nvme_err_invalid_create_sq_size(qsize);
- return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
- }
- if (unlikely(prp1 & (n->page_size - 1))) {
- trace_pci_nvme_err_invalid_create_sq_addr(prp1);
- return NVME_INVALID_PRP_OFFSET | NVME_DNR;
- }
- if (unlikely(!(NVME_SQ_FLAGS_PC(qflags)))) {
- trace_pci_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags));
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- sq = g_malloc0(sizeof(*sq));
- nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
- return NVME_SUCCESS;
- }
- struct nvme_stats {
- uint64_t units_read;
- uint64_t units_written;
- uint64_t read_commands;
- uint64_t write_commands;
- };
- static void nvme_set_blk_stats(NvmeNamespace *ns, struct nvme_stats *stats)
- {
- BlockAcctStats *s = blk_get_stats(ns->blkconf.blk);
- stats->units_read += s->nr_bytes[BLOCK_ACCT_READ];
- stats->units_written += s->nr_bytes[BLOCK_ACCT_WRITE];
- stats->read_commands += s->nr_ops[BLOCK_ACCT_READ];
- stats->write_commands += s->nr_ops[BLOCK_ACCT_WRITE];
- }
- static uint16_t nvme_ocp_extended_smart_info(NvmeCtrl *n, uint8_t rae,
- uint32_t buf_len, uint64_t off,
- NvmeRequest *req)
- {
- NvmeNamespace *ns = NULL;
- NvmeSmartLogExtended smart_l = { 0 };
- struct nvme_stats stats = { 0 };
- uint32_t trans_len;
- if (off >= sizeof(smart_l)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- /* accumulate all stats from all namespaces */
- for (int i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (ns) {
- nvme_set_blk_stats(ns, &stats);
- }
- }
- smart_l.physical_media_units_written[0] = cpu_to_le64(stats.units_written);
- smart_l.physical_media_units_read[0] = cpu_to_le64(stats.units_read);
- smart_l.log_page_version = 0x0005;
- static const uint8_t guid[16] = {
- 0xC5, 0xAF, 0x10, 0x28, 0xEA, 0xBF, 0xF2, 0xA4,
- 0x9C, 0x4F, 0x6F, 0x7C, 0xC9, 0x14, 0xD5, 0xAF
- };
- memcpy(smart_l.log_page_guid, guid, sizeof(smart_l.log_page_guid));
- if (!rae) {
- nvme_clear_events(n, NVME_AER_TYPE_SMART);
- }
- trans_len = MIN(sizeof(smart_l) - off, buf_len);
- return nvme_c2h(n, (uint8_t *) &smart_l + off, trans_len, req);
- }
- static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- struct nvme_stats stats = { 0 };
- NvmeSmartLog smart = { 0 };
- uint32_t trans_len;
- NvmeNamespace *ns;
- time_t current_ms;
- uint64_t u_read, u_written;
- if (off >= sizeof(smart)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (nsid != 0xffffffff) {
- ns = nvme_ns(n, nsid);
- if (!ns) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- nvme_set_blk_stats(ns, &stats);
- } else {
- int i;
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- nvme_set_blk_stats(ns, &stats);
- }
- }
- trans_len = MIN(sizeof(smart) - off, buf_len);
- smart.critical_warning = n->smart_critical_warning;
- u_read = DIV_ROUND_UP(stats.units_read >> BDRV_SECTOR_BITS, 1000);
- u_written = DIV_ROUND_UP(stats.units_written >> BDRV_SECTOR_BITS, 1000);
- smart.data_units_read[0] = cpu_to_le64(u_read);
- smart.data_units_written[0] = cpu_to_le64(u_written);
- smart.host_read_commands[0] = cpu_to_le64(stats.read_commands);
- smart.host_write_commands[0] = cpu_to_le64(stats.write_commands);
- smart.temperature = cpu_to_le16(n->temperature);
- if ((n->temperature >= n->features.temp_thresh_hi) ||
- (n->temperature <= n->features.temp_thresh_low)) {
- smart.critical_warning |= NVME_SMART_TEMPERATURE;
- }
- current_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
- smart.power_on_hours[0] =
- cpu_to_le64((((current_ms - n->starttime_ms) / 1000) / 60) / 60);
- if (!rae) {
- nvme_clear_events(n, NVME_AER_TYPE_SMART);
- }
- return nvme_c2h(n, (uint8_t *) &smart + off, trans_len, req);
- }
- static uint16_t nvme_endgrp_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
- uint16_t endgrpid = (dw11 >> 16) & 0xffff;
- struct nvme_stats stats = {};
- NvmeEndGrpLog info = {};
- int i;
- if (!n->subsys || endgrpid != 0x1) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (off >= sizeof(info)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- NvmeNamespace *ns = nvme_subsys_ns(n->subsys, i);
- if (!ns) {
- continue;
- }
- nvme_set_blk_stats(ns, &stats);
- }
- info.data_units_read[0] =
- cpu_to_le64(DIV_ROUND_UP(stats.units_read / 1000000000, 1000000000));
- info.data_units_written[0] =
- cpu_to_le64(DIV_ROUND_UP(stats.units_written / 1000000000, 1000000000));
- info.media_units_written[0] =
- cpu_to_le64(DIV_ROUND_UP(stats.units_written / 1000000000, 1000000000));
- info.host_read_commands[0] = cpu_to_le64(stats.read_commands);
- info.host_write_commands[0] = cpu_to_le64(stats.write_commands);
- buf_len = MIN(sizeof(info) - off, buf_len);
- return nvme_c2h(n, (uint8_t *)&info + off, buf_len, req);
- }
- static uint16_t nvme_fw_log_info(NvmeCtrl *n, uint32_t buf_len, uint64_t off,
- NvmeRequest *req)
- {
- uint32_t trans_len;
- NvmeFwSlotInfoLog fw_log = {
- .afi = 0x1,
- };
- if (off >= sizeof(fw_log)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- strpadcpy((char *)&fw_log.frs1, sizeof(fw_log.frs1), "1.0", ' ');
- trans_len = MIN(sizeof(fw_log) - off, buf_len);
- return nvme_c2h(n, (uint8_t *) &fw_log + off, trans_len, req);
- }
- static uint16_t nvme_error_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- uint32_t trans_len;
- NvmeErrorLog errlog;
- if (off >= sizeof(errlog)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (!rae) {
- nvme_clear_events(n, NVME_AER_TYPE_ERROR);
- }
- memset(&errlog, 0x0, sizeof(errlog));
- trans_len = MIN(sizeof(errlog) - off, buf_len);
- return nvme_c2h(n, (uint8_t *)&errlog, trans_len, req);
- }
- static uint16_t nvme_changed_nslist(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- uint32_t nslist[1024];
- uint32_t trans_len;
- int i = 0;
- uint32_t nsid;
- if (off >= sizeof(nslist)) {
- trace_pci_nvme_err_invalid_log_page_offset(off, sizeof(nslist));
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- memset(nslist, 0x0, sizeof(nslist));
- trans_len = MIN(sizeof(nslist) - off, buf_len);
- while ((nsid = find_first_bit(n->changed_nsids, NVME_CHANGED_NSID_SIZE)) !=
- NVME_CHANGED_NSID_SIZE) {
- /*
- * If more than 1024 namespaces, the first entry in the log page should
- * be set to FFFFFFFFh and the others to 0 as spec.
- */
- if (i == ARRAY_SIZE(nslist)) {
- memset(nslist, 0x0, sizeof(nslist));
- nslist[0] = 0xffffffff;
- break;
- }
- nslist[i++] = nsid;
- clear_bit(nsid, n->changed_nsids);
- }
- /*
- * Remove all the remaining list entries in case returns directly due to
- * more than 1024 namespaces.
- */
- if (nslist[0] == 0xffffffff) {
- bitmap_zero(n->changed_nsids, NVME_CHANGED_NSID_SIZE);
- }
- if (!rae) {
- nvme_clear_events(n, NVME_AER_TYPE_NOTICE);
- }
- return nvme_c2h(n, ((uint8_t *)nslist) + off, trans_len, req);
- }
- static uint16_t nvme_cmd_effects(NvmeCtrl *n, uint8_t csi, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- NvmeEffectsLog log = {};
- const uint32_t *iocs = NULL;
- uint32_t trans_len;
- if (off >= sizeof(log)) {
- trace_pci_nvme_err_invalid_log_page_offset(off, sizeof(log));
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- switch (NVME_CC_CSS(ldl_le_p(&n->bar.cc))) {
- case NVME_CC_CSS_NVM:
- iocs = n->cse.iocs.nvm;
- break;
- case NVME_CC_CSS_ALL:
- switch (csi) {
- case NVME_CSI_NVM:
- iocs = n->cse.iocs.nvm;
- break;
- case NVME_CSI_ZONED:
- iocs = n->cse.iocs.zoned;
- break;
- }
- break;
- }
- memcpy(log.acs, n->cse.acs, sizeof(log.acs));
- if (iocs) {
- memcpy(log.iocs, iocs, sizeof(log.iocs));
- }
- trans_len = MIN(sizeof(log) - off, buf_len);
- return nvme_c2h(n, ((uint8_t *)&log) + off, trans_len, req);
- }
- static uint16_t nvme_vendor_specific_log(NvmeCtrl *n, uint8_t rae,
- uint32_t buf_len, uint64_t off,
- NvmeRequest *req, uint8_t lid)
- {
- switch (lid) {
- case NVME_OCP_EXTENDED_SMART_INFO:
- if (n->params.ocp) {
- return nvme_ocp_extended_smart_info(n, rae, buf_len, off, req);
- }
- break;
- /* add a case for each additional vendor specific log id */
- }
- trace_pci_nvme_err_invalid_log_page(nvme_cid(req), lid);
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- static size_t sizeof_fdp_conf_descr(size_t nruh, size_t vss)
- {
- size_t entry_siz = sizeof(NvmeFdpDescrHdr) + nruh * sizeof(NvmeRuhDescr)
- + vss;
- return ROUND_UP(entry_siz, 8);
- }
- static uint16_t nvme_fdp_confs(NvmeCtrl *n, uint32_t endgrpid, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- uint32_t log_size, trans_len;
- g_autofree uint8_t *buf = NULL;
- NvmeFdpDescrHdr *hdr;
- NvmeRuhDescr *ruhd;
- NvmeEnduranceGroup *endgrp;
- NvmeFdpConfsHdr *log;
- size_t nruh, fdp_descr_size;
- int i;
- if (endgrpid != 1 || !n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- endgrp = &n->subsys->endgrp;
- if (endgrp->fdp.enabled) {
- nruh = endgrp->fdp.nruh;
- } else {
- nruh = 1;
- }
- fdp_descr_size = sizeof_fdp_conf_descr(nruh, FDPVSS);
- log_size = sizeof(NvmeFdpConfsHdr) + fdp_descr_size;
- if (off >= log_size) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- trans_len = MIN(log_size - off, buf_len);
- buf = g_malloc0(log_size);
- log = (NvmeFdpConfsHdr *)buf;
- hdr = (NvmeFdpDescrHdr *)(log + 1);
- ruhd = (NvmeRuhDescr *)(buf + sizeof(*log) + sizeof(*hdr));
- log->num_confs = cpu_to_le16(0);
- log->size = cpu_to_le32(log_size);
- hdr->descr_size = cpu_to_le16(fdp_descr_size);
- if (endgrp->fdp.enabled) {
- hdr->fdpa = FIELD_DP8(hdr->fdpa, FDPA, VALID, 1);
- hdr->fdpa = FIELD_DP8(hdr->fdpa, FDPA, RGIF, endgrp->fdp.rgif);
- hdr->nrg = cpu_to_le16(endgrp->fdp.nrg);
- hdr->nruh = cpu_to_le16(endgrp->fdp.nruh);
- hdr->maxpids = cpu_to_le16(NVME_FDP_MAXPIDS - 1);
- hdr->nnss = cpu_to_le32(NVME_MAX_NAMESPACES);
- hdr->runs = cpu_to_le64(endgrp->fdp.runs);
- for (i = 0; i < nruh; i++) {
- ruhd->ruht = NVME_RUHT_INITIALLY_ISOLATED;
- ruhd++;
- }
- } else {
- /* 1 bit for RUH in PIF -> 2 RUHs max. */
- hdr->nrg = cpu_to_le16(1);
- hdr->nruh = cpu_to_le16(1);
- hdr->maxpids = cpu_to_le16(NVME_FDP_MAXPIDS - 1);
- hdr->nnss = cpu_to_le32(1);
- hdr->runs = cpu_to_le64(96 * MiB);
- ruhd->ruht = NVME_RUHT_INITIALLY_ISOLATED;
- }
- return nvme_c2h(n, (uint8_t *)buf + off, trans_len, req);
- }
- static uint16_t nvme_fdp_ruh_usage(NvmeCtrl *n, uint32_t endgrpid,
- uint32_t dw10, uint32_t dw12,
- uint32_t buf_len, uint64_t off,
- NvmeRequest *req)
- {
- NvmeRuHandle *ruh;
- NvmeRuhuLog *hdr;
- NvmeRuhuDescr *ruhud;
- NvmeEnduranceGroup *endgrp;
- g_autofree uint8_t *buf = NULL;
- uint32_t log_size, trans_len;
- uint16_t i;
- if (endgrpid != 1 || !n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- endgrp = &n->subsys->endgrp;
- if (!endgrp->fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- log_size = sizeof(NvmeRuhuLog) + endgrp->fdp.nruh * sizeof(NvmeRuhuDescr);
- if (off >= log_size) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- trans_len = MIN(log_size - off, buf_len);
- buf = g_malloc0(log_size);
- hdr = (NvmeRuhuLog *)buf;
- ruhud = (NvmeRuhuDescr *)(hdr + 1);
- ruh = endgrp->fdp.ruhs;
- hdr->nruh = cpu_to_le16(endgrp->fdp.nruh);
- for (i = 0; i < endgrp->fdp.nruh; i++, ruhud++, ruh++) {
- ruhud->ruha = ruh->ruha;
- }
- return nvme_c2h(n, (uint8_t *)buf + off, trans_len, req);
- }
- static uint16_t nvme_fdp_stats(NvmeCtrl *n, uint32_t endgrpid, uint32_t buf_len,
- uint64_t off, NvmeRequest *req)
- {
- NvmeEnduranceGroup *endgrp;
- NvmeFdpStatsLog log = {};
- uint32_t trans_len;
- if (off >= sizeof(NvmeFdpStatsLog)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (endgrpid != 1 || !n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (!n->subsys->endgrp.fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- endgrp = &n->subsys->endgrp;
- trans_len = MIN(sizeof(log) - off, buf_len);
- /* spec value is 128 bit, we only use 64 bit */
- log.hbmw[0] = cpu_to_le64(endgrp->fdp.hbmw);
- log.mbmw[0] = cpu_to_le64(endgrp->fdp.mbmw);
- log.mbe[0] = cpu_to_le64(endgrp->fdp.mbe);
- return nvme_c2h(n, (uint8_t *)&log + off, trans_len, req);
- }
- static uint16_t nvme_fdp_events(NvmeCtrl *n, uint32_t endgrpid,
- uint32_t buf_len, uint64_t off,
- NvmeRequest *req)
- {
- NvmeEnduranceGroup *endgrp;
- NvmeCmd *cmd = &req->cmd;
- bool host_events = (cmd->cdw10 >> 8) & 0x1;
- uint32_t log_size, trans_len;
- NvmeFdpEventBuffer *ebuf;
- g_autofree NvmeFdpEventsLog *elog = NULL;
- NvmeFdpEvent *event;
- if (endgrpid != 1 || !n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- endgrp = &n->subsys->endgrp;
- if (!endgrp->fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- if (host_events) {
- ebuf = &endgrp->fdp.host_events;
- } else {
- ebuf = &endgrp->fdp.ctrl_events;
- }
- log_size = sizeof(NvmeFdpEventsLog) + ebuf->nelems * sizeof(NvmeFdpEvent);
- if (off >= log_size) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- trans_len = MIN(log_size - off, buf_len);
- elog = g_malloc0(log_size);
- elog->num_events = cpu_to_le32(ebuf->nelems);
- event = (NvmeFdpEvent *)(elog + 1);
- if (ebuf->nelems && ebuf->start == ebuf->next) {
- unsigned int nelems = (NVME_FDP_MAX_EVENTS - ebuf->start);
- /* wrap over, copy [start;NVME_FDP_MAX_EVENTS[ and [0; next[ */
- memcpy(event, &ebuf->events[ebuf->start],
- sizeof(NvmeFdpEvent) * nelems);
- memcpy(event + nelems, ebuf->events,
- sizeof(NvmeFdpEvent) * ebuf->next);
- } else if (ebuf->start < ebuf->next) {
- memcpy(event, &ebuf->events[ebuf->start],
- sizeof(NvmeFdpEvent) * (ebuf->next - ebuf->start));
- }
- return nvme_c2h(n, (uint8_t *)elog + off, trans_len, req);
- }
- static uint16_t nvme_get_log(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t dw10 = le32_to_cpu(cmd->cdw10);
- uint32_t dw11 = le32_to_cpu(cmd->cdw11);
- uint32_t dw12 = le32_to_cpu(cmd->cdw12);
- uint32_t dw13 = le32_to_cpu(cmd->cdw13);
- uint8_t lid = dw10 & 0xff;
- uint8_t lsp = (dw10 >> 8) & 0xf;
- uint8_t rae = (dw10 >> 15) & 0x1;
- uint8_t csi = le32_to_cpu(cmd->cdw14) >> 24;
- uint32_t numdl, numdu, lspi;
- uint64_t off, lpol, lpou;
- size_t len;
- uint16_t status;
- numdl = (dw10 >> 16);
- numdu = (dw11 & 0xffff);
- lspi = (dw11 >> 16);
- lpol = dw12;
- lpou = dw13;
- len = (((numdu << 16) | numdl) + 1) << 2;
- off = (lpou << 32ULL) | lpol;
- if (off & 0x3) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- trace_pci_nvme_get_log(nvme_cid(req), lid, lsp, rae, len, off);
- status = nvme_check_mdts(n, len);
- if (status) {
- return status;
- }
- switch (lid) {
- case NVME_LOG_ERROR_INFO:
- return nvme_error_info(n, rae, len, off, req);
- case NVME_LOG_SMART_INFO:
- return nvme_smart_info(n, rae, len, off, req);
- case NVME_LOG_FW_SLOT_INFO:
- return nvme_fw_log_info(n, len, off, req);
- case NVME_LOG_VENDOR_START...NVME_LOG_VENDOR_END:
- return nvme_vendor_specific_log(n, rae, len, off, req, lid);
- case NVME_LOG_CHANGED_NSLIST:
- return nvme_changed_nslist(n, rae, len, off, req);
- case NVME_LOG_CMD_EFFECTS:
- return nvme_cmd_effects(n, csi, len, off, req);
- case NVME_LOG_ENDGRP:
- return nvme_endgrp_info(n, rae, len, off, req);
- case NVME_LOG_FDP_CONFS:
- return nvme_fdp_confs(n, lspi, len, off, req);
- case NVME_LOG_FDP_RUH_USAGE:
- return nvme_fdp_ruh_usage(n, lspi, dw10, dw12, len, off, req);
- case NVME_LOG_FDP_STATS:
- return nvme_fdp_stats(n, lspi, len, off, req);
- case NVME_LOG_FDP_EVENTS:
- return nvme_fdp_events(n, lspi, len, off, req);
- default:
- trace_pci_nvme_err_invalid_log_page(nvme_cid(req), lid);
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- uint16_t offset = (cq->cqid << 3) + (1 << 2);
- n->cq[cq->cqid] = NULL;
- qemu_bh_delete(cq->bh);
- if (cq->ioeventfd_enabled) {
- memory_region_del_eventfd(&n->iomem,
- 0x1000 + offset, 4, false, 0, &cq->notifier);
- event_notifier_set_handler(&cq->notifier, NULL);
- event_notifier_cleanup(&cq->notifier);
- }
- if (msix_enabled(pci) && cq->irq_enabled) {
- msix_vector_unuse(pci, cq->vector);
- }
- if (cq->cqid) {
- g_free(cq);
- }
- }
- static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeDeleteQ *c = (NvmeDeleteQ *)&req->cmd;
- NvmeCQueue *cq;
- uint16_t qid = le16_to_cpu(c->qid);
- if (unlikely(!qid || nvme_check_cqid(n, qid))) {
- trace_pci_nvme_err_invalid_del_cq_cqid(qid);
- return NVME_INVALID_CQID | NVME_DNR;
- }
- cq = n->cq[qid];
- if (unlikely(!QTAILQ_EMPTY(&cq->sq_list))) {
- trace_pci_nvme_err_invalid_del_cq_notempty(qid);
- return NVME_INVALID_QUEUE_DEL;
- }
- if (cq->irq_enabled && cq->tail != cq->head) {
- n->cq_pending--;
- }
- nvme_irq_deassert(n, cq);
- trace_pci_nvme_del_cq(qid);
- nvme_free_cq(cq, n);
- return NVME_SUCCESS;
- }
- static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
- uint16_t cqid, uint16_t vector, uint16_t size,
- uint16_t irq_enabled)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- if (msix_enabled(pci) && irq_enabled) {
- msix_vector_use(pci, vector);
- }
- cq->ctrl = n;
- cq->cqid = cqid;
- cq->size = size;
- cq->dma_addr = dma_addr;
- cq->phase = 1;
- cq->irq_enabled = irq_enabled;
- cq->vector = vector;
- cq->head = cq->tail = 0;
- QTAILQ_INIT(&cq->req_list);
- QTAILQ_INIT(&cq->sq_list);
- if (n->dbbuf_enabled) {
- cq->db_addr = n->dbbuf_dbs + (cqid << 3) + (1 << 2);
- cq->ei_addr = n->dbbuf_eis + (cqid << 3) + (1 << 2);
- if (n->params.ioeventfd && cqid != 0) {
- if (!nvme_init_cq_ioeventfd(cq)) {
- cq->ioeventfd_enabled = true;
- }
- }
- }
- n->cq[cqid] = cq;
- cq->bh = qemu_bh_new_guarded(nvme_post_cqes, cq,
- &DEVICE(cq->ctrl)->mem_reentrancy_guard);
- }
- static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCQueue *cq;
- NvmeCreateCq *c = (NvmeCreateCq *)&req->cmd;
- uint16_t cqid = le16_to_cpu(c->cqid);
- uint16_t vector = le16_to_cpu(c->irq_vector);
- uint16_t qsize = le16_to_cpu(c->qsize);
- uint16_t qflags = le16_to_cpu(c->cq_flags);
- uint64_t prp1 = le64_to_cpu(c->prp1);
- uint32_t cc = ldq_le_p(&n->bar.cc);
- uint8_t iocqes = NVME_CC_IOCQES(cc);
- uint8_t iosqes = NVME_CC_IOSQES(cc);
- trace_pci_nvme_create_cq(prp1, cqid, vector, qsize, qflags,
- NVME_CQ_FLAGS_IEN(qflags) != 0);
- if (iosqes != NVME_SQES || iocqes != NVME_CQES) {
- trace_pci_nvme_err_invalid_create_cq_entry_size(iosqes, iocqes);
- return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
- }
- if (unlikely(!cqid || cqid > n->conf_ioqpairs || n->cq[cqid] != NULL)) {
- trace_pci_nvme_err_invalid_create_cq_cqid(cqid);
- return NVME_INVALID_QID | NVME_DNR;
- }
- if (unlikely(!qsize || qsize > NVME_CAP_MQES(ldq_le_p(&n->bar.cap)))) {
- trace_pci_nvme_err_invalid_create_cq_size(qsize);
- return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
- }
- if (unlikely(prp1 & (n->page_size - 1))) {
- trace_pci_nvme_err_invalid_create_cq_addr(prp1);
- return NVME_INVALID_PRP_OFFSET | NVME_DNR;
- }
- if (unlikely(!msix_enabled(PCI_DEVICE(n)) && vector)) {
- trace_pci_nvme_err_invalid_create_cq_vector(vector);
- return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
- }
- if (unlikely(vector >= n->conf_msix_qsize)) {
- trace_pci_nvme_err_invalid_create_cq_vector(vector);
- return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
- }
- if (unlikely(!(NVME_CQ_FLAGS_PC(qflags)))) {
- trace_pci_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags));
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- cq = g_malloc0(sizeof(*cq));
- nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
- NVME_CQ_FLAGS_IEN(qflags));
- /*
- * It is only required to set qs_created when creating a completion queue;
- * creating a submission queue without a matching completion queue will
- * fail.
- */
- n->qs_created = true;
- return NVME_SUCCESS;
- }
- static uint16_t nvme_rpt_empty_id_struct(NvmeCtrl *n, NvmeRequest *req)
- {
- uint8_t id[NVME_IDENTIFY_DATA_SIZE] = {};
- return nvme_c2h(n, id, sizeof(id), req);
- }
- static uint16_t nvme_identify_ctrl(NvmeCtrl *n, NvmeRequest *req)
- {
- trace_pci_nvme_identify_ctrl();
- return nvme_c2h(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl), req);
- }
- static uint16_t nvme_identify_ctrl_csi(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint8_t id[NVME_IDENTIFY_DATA_SIZE] = {};
- NvmeIdCtrlNvm *id_nvm = (NvmeIdCtrlNvm *)&id;
- trace_pci_nvme_identify_ctrl_csi(c->csi);
- switch (c->csi) {
- case NVME_CSI_NVM:
- id_nvm->vsl = n->params.vsl;
- id_nvm->dmrl = NVME_ID_CTRL_NVM_DMRL_MAX;
- id_nvm->dmrsl = cpu_to_le32(n->dmrsl);
- id_nvm->dmsl = NVME_ID_CTRL_NVM_DMRL_MAX * n->dmrsl;
- break;
- case NVME_CSI_ZONED:
- ((NvmeIdCtrlZoned *)&id)->zasl = n->params.zasl;
- break;
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- return nvme_c2h(n, id, sizeof(id), req);
- }
- static uint16_t nvme_identify_ns(NvmeCtrl *n, NvmeRequest *req, bool active)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t nsid = le32_to_cpu(c->nsid);
- trace_pci_nvme_identify_ns(nsid);
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- if (!active) {
- ns = nvme_subsys_ns(n->subsys, nsid);
- if (!ns) {
- return nvme_rpt_empty_id_struct(n, req);
- }
- } else {
- return nvme_rpt_empty_id_struct(n, req);
- }
- }
- if (active || ns->csi == NVME_CSI_NVM) {
- return nvme_c2h(n, (uint8_t *)&ns->id_ns, sizeof(NvmeIdNs), req);
- }
- return NVME_INVALID_IOCS | NVME_DNR;
- }
- static uint16_t nvme_identify_ctrl_list(NvmeCtrl *n, NvmeRequest *req,
- bool attached)
- {
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t nsid = le32_to_cpu(c->nsid);
- uint16_t min_id = le16_to_cpu(c->ctrlid);
- uint16_t list[NVME_CONTROLLER_LIST_SIZE] = {};
- uint16_t *ids = &list[1];
- NvmeNamespace *ns;
- NvmeCtrl *ctrl;
- int cntlid, nr_ids = 0;
- trace_pci_nvme_identify_ctrl_list(c->cns, min_id);
- if (!n->subsys) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (attached) {
- if (nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ns = nvme_subsys_ns(n->subsys, nsid);
- if (!ns) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- for (cntlid = min_id; cntlid < ARRAY_SIZE(n->subsys->ctrls); cntlid++) {
- ctrl = nvme_subsys_ctrl(n->subsys, cntlid);
- if (!ctrl) {
- continue;
- }
- if (attached && !nvme_ns(ctrl, nsid)) {
- continue;
- }
- ids[nr_ids++] = cntlid;
- }
- list[0] = nr_ids;
- return nvme_c2h(n, (uint8_t *)list, sizeof(list), req);
- }
- static uint16_t nvme_identify_pri_ctrl_cap(NvmeCtrl *n, NvmeRequest *req)
- {
- trace_pci_nvme_identify_pri_ctrl_cap(le16_to_cpu(n->pri_ctrl_cap.cntlid));
- return nvme_c2h(n, (uint8_t *)&n->pri_ctrl_cap,
- sizeof(NvmePriCtrlCap), req);
- }
- static uint16_t nvme_identify_sec_ctrl_list(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint16_t pri_ctrl_id = le16_to_cpu(n->pri_ctrl_cap.cntlid);
- uint16_t min_id = le16_to_cpu(c->ctrlid);
- uint8_t num_sec_ctrl = n->nr_sec_ctrls;
- NvmeSecCtrlList list = {0};
- uint8_t i;
- for (i = 0; i < num_sec_ctrl; i++) {
- if (n->sec_ctrl_list[i].scid >= min_id) {
- list.numcntl = MIN(num_sec_ctrl - i, 127);
- memcpy(&list.sec, n->sec_ctrl_list + i,
- list.numcntl * sizeof(NvmeSecCtrlEntry));
- break;
- }
- }
- trace_pci_nvme_identify_sec_ctrl_list(pri_ctrl_id, list.numcntl);
- return nvme_c2h(n, (uint8_t *)&list, sizeof(list), req);
- }
- static uint16_t nvme_identify_ns_ind(NvmeCtrl *n, NvmeRequest *req, bool alloc)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t nsid = le32_to_cpu(c->nsid);
- trace_pci_nvme_identify_ns_ind(nsid);
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- if (alloc) {
- ns = nvme_subsys_ns(n->subsys, nsid);
- if (!ns) {
- return nvme_rpt_empty_id_struct(n, req);
- }
- } else {
- return nvme_rpt_empty_id_struct(n, req);
- }
- }
- return nvme_c2h(n, (uint8_t *)&ns->id_ns_ind, sizeof(NvmeIdNsInd), req);
- }
- static uint16_t nvme_identify_ns_csi(NvmeCtrl *n, NvmeRequest *req,
- bool active)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t nsid = le32_to_cpu(c->nsid);
- trace_pci_nvme_identify_ns_csi(nsid, c->csi);
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- if (!active) {
- ns = nvme_subsys_ns(n->subsys, nsid);
- if (!ns) {
- return nvme_rpt_empty_id_struct(n, req);
- }
- } else {
- return nvme_rpt_empty_id_struct(n, req);
- }
- }
- if (c->csi == NVME_CSI_NVM) {
- return nvme_c2h(n, (uint8_t *)&ns->id_ns_nvm, sizeof(NvmeIdNsNvm),
- req);
- } else if (c->csi == NVME_CSI_ZONED && ns->csi == NVME_CSI_ZONED) {
- return nvme_c2h(n, (uint8_t *)ns->id_ns_zoned, sizeof(NvmeIdNsZoned),
- req);
- }
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- static uint16_t nvme_identify_nslist(NvmeCtrl *n, NvmeRequest *req,
- bool active)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t min_nsid = le32_to_cpu(c->nsid);
- uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
- static const int data_len = sizeof(list);
- uint32_t *list_ptr = (uint32_t *)list;
- int i, j = 0;
- trace_pci_nvme_identify_nslist(min_nsid);
- /*
- * Both FFFFFFFFh (NVME_NSID_BROADCAST) and FFFFFFFFEh are invalid values
- * since the Active Namespace ID List should return namespaces with ids
- * *higher* than the NSID specified in the command. This is also specified
- * in the spec (NVM Express v1.3d, Section 5.15.4).
- */
- if (min_nsid >= NVME_NSID_BROADCAST - 1) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- if (!active) {
- ns = nvme_subsys_ns(n->subsys, i);
- if (!ns) {
- continue;
- }
- } else {
- continue;
- }
- }
- if (ns->params.nsid <= min_nsid) {
- continue;
- }
- list_ptr[j++] = cpu_to_le32(ns->params.nsid);
- if (j == data_len / sizeof(uint32_t)) {
- break;
- }
- }
- return nvme_c2h(n, list, data_len, req);
- }
- static uint16_t nvme_identify_nslist_csi(NvmeCtrl *n, NvmeRequest *req,
- bool active)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t min_nsid = le32_to_cpu(c->nsid);
- uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
- static const int data_len = sizeof(list);
- uint32_t *list_ptr = (uint32_t *)list;
- int i, j = 0;
- trace_pci_nvme_identify_nslist_csi(min_nsid, c->csi);
- /*
- * Same as in nvme_identify_nslist(), FFFFFFFFh/FFFFFFFFEh are invalid.
- */
- if (min_nsid >= NVME_NSID_BROADCAST - 1) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- if (c->csi != NVME_CSI_NVM && c->csi != NVME_CSI_ZONED) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- if (!active) {
- ns = nvme_subsys_ns(n->subsys, i);
- if (!ns) {
- continue;
- }
- } else {
- continue;
- }
- }
- if (ns->params.nsid <= min_nsid || c->csi != ns->csi) {
- continue;
- }
- list_ptr[j++] = cpu_to_le32(ns->params.nsid);
- if (j == data_len / sizeof(uint32_t)) {
- break;
- }
- }
- return nvme_c2h(n, list, data_len, req);
- }
- static uint16_t nvme_endurance_group_list(NvmeCtrl *n, NvmeRequest *req)
- {
- uint16_t list[NVME_CONTROLLER_LIST_SIZE] = {};
- uint16_t *nr_ids = &list[0];
- uint16_t *ids = &list[1];
- uint16_t endgid = le32_to_cpu(req->cmd.cdw11) & 0xffff;
- /*
- * The current nvme-subsys only supports Endurance Group #1.
- */
- if (!endgid) {
- *nr_ids = 1;
- ids[0] = 1;
- } else {
- *nr_ids = 0;
- }
- return nvme_c2h(n, list, sizeof(list), req);
- }
- static uint16_t nvme_identify_ns_descr_list(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns;
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- uint32_t nsid = le32_to_cpu(c->nsid);
- uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
- uint8_t *pos = list;
- struct {
- NvmeIdNsDescr hdr;
- uint8_t v[NVME_NIDL_UUID];
- } QEMU_PACKED uuid = {};
- struct {
- NvmeIdNsDescr hdr;
- uint8_t v[NVME_NIDL_NGUID];
- } QEMU_PACKED nguid = {};
- struct {
- NvmeIdNsDescr hdr;
- uint64_t v;
- } QEMU_PACKED eui64 = {};
- struct {
- NvmeIdNsDescr hdr;
- uint8_t v;
- } QEMU_PACKED csi = {};
- trace_pci_nvme_identify_ns_descr_list(nsid);
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (!qemu_uuid_is_null(&ns->params.uuid)) {
- uuid.hdr.nidt = NVME_NIDT_UUID;
- uuid.hdr.nidl = NVME_NIDL_UUID;
- memcpy(uuid.v, ns->params.uuid.data, NVME_NIDL_UUID);
- memcpy(pos, &uuid, sizeof(uuid));
- pos += sizeof(uuid);
- }
- if (!nvme_nguid_is_null(&ns->params.nguid)) {
- nguid.hdr.nidt = NVME_NIDT_NGUID;
- nguid.hdr.nidl = NVME_NIDL_NGUID;
- memcpy(nguid.v, ns->params.nguid.data, NVME_NIDL_NGUID);
- memcpy(pos, &nguid, sizeof(nguid));
- pos += sizeof(nguid);
- }
- if (ns->params.eui64) {
- eui64.hdr.nidt = NVME_NIDT_EUI64;
- eui64.hdr.nidl = NVME_NIDL_EUI64;
- eui64.v = cpu_to_be64(ns->params.eui64);
- memcpy(pos, &eui64, sizeof(eui64));
- pos += sizeof(eui64);
- }
- csi.hdr.nidt = NVME_NIDT_CSI;
- csi.hdr.nidl = NVME_NIDL_CSI;
- csi.v = ns->csi;
- memcpy(pos, &csi, sizeof(csi));
- pos += sizeof(csi);
- return nvme_c2h(n, list, sizeof(list), req);
- }
- static uint16_t nvme_identify_cmd_set(NvmeCtrl *n, NvmeRequest *req)
- {
- uint8_t list[NVME_IDENTIFY_DATA_SIZE] = {};
- static const int data_len = sizeof(list);
- trace_pci_nvme_identify_cmd_set();
- NVME_SET_CSI(*list, NVME_CSI_NVM);
- NVME_SET_CSI(*list, NVME_CSI_ZONED);
- return nvme_c2h(n, list, data_len, req);
- }
- static uint16_t nvme_identify(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeIdentify *c = (NvmeIdentify *)&req->cmd;
- trace_pci_nvme_identify(nvme_cid(req), c->cns, le16_to_cpu(c->ctrlid),
- c->csi);
- switch (c->cns) {
- case NVME_ID_CNS_NS:
- return nvme_identify_ns(n, req, true);
- case NVME_ID_CNS_NS_PRESENT:
- return nvme_identify_ns(n, req, false);
- case NVME_ID_CNS_NS_ATTACHED_CTRL_LIST:
- return nvme_identify_ctrl_list(n, req, true);
- case NVME_ID_CNS_CTRL_LIST:
- return nvme_identify_ctrl_list(n, req, false);
- case NVME_ID_CNS_PRIMARY_CTRL_CAP:
- return nvme_identify_pri_ctrl_cap(n, req);
- case NVME_ID_CNS_SECONDARY_CTRL_LIST:
- return nvme_identify_sec_ctrl_list(n, req);
- case NVME_ID_CNS_CS_NS:
- return nvme_identify_ns_csi(n, req, true);
- case NVME_ID_CNS_CS_IND_NS:
- return nvme_identify_ns_ind(n, req, false);
- case NVME_ID_CNS_CS_IND_NS_ALLOCATED:
- return nvme_identify_ns_ind(n, req, true);
- case NVME_ID_CNS_CS_NS_PRESENT:
- return nvme_identify_ns_csi(n, req, false);
- case NVME_ID_CNS_CTRL:
- return nvme_identify_ctrl(n, req);
- case NVME_ID_CNS_CS_CTRL:
- return nvme_identify_ctrl_csi(n, req);
- case NVME_ID_CNS_NS_ACTIVE_LIST:
- return nvme_identify_nslist(n, req, true);
- case NVME_ID_CNS_NS_PRESENT_LIST:
- return nvme_identify_nslist(n, req, false);
- case NVME_ID_CNS_CS_NS_ACTIVE_LIST:
- return nvme_identify_nslist_csi(n, req, true);
- case NVME_ID_CNS_ENDURANCE_GROUP_LIST:
- return nvme_endurance_group_list(n, req);
- case NVME_ID_CNS_CS_NS_PRESENT_LIST:
- return nvme_identify_nslist_csi(n, req, false);
- case NVME_ID_CNS_NS_DESCR_LIST:
- return nvme_identify_ns_descr_list(n, req);
- case NVME_ID_CNS_IO_COMMAND_SET:
- return nvme_identify_cmd_set(n, req);
- default:
- trace_pci_nvme_err_invalid_identify_cns(le32_to_cpu(c->cns));
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- static uint16_t nvme_abort(NvmeCtrl *n, NvmeRequest *req)
- {
- uint16_t sqid = le32_to_cpu(req->cmd.cdw10) & 0xffff;
- uint16_t cid = (le32_to_cpu(req->cmd.cdw10) >> 16) & 0xffff;
- NvmeSQueue *sq = n->sq[sqid];
- NvmeRequest *r, *next;
- int i;
- req->cqe.result = 1;
- if (nvme_check_sqid(n, sqid)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (sqid == 0) {
- for (i = 0; i < n->outstanding_aers; i++) {
- NvmeRequest *re = n->aer_reqs[i];
- if (re->cqe.cid == cid) {
- memmove(n->aer_reqs + i, n->aer_reqs + i + 1,
- (n->outstanding_aers - i - 1) * sizeof(NvmeRequest *));
- n->outstanding_aers--;
- re->status = NVME_CMD_ABORT_REQ;
- req->cqe.result = 0;
- nvme_enqueue_req_completion(&n->admin_cq, re);
- return NVME_SUCCESS;
- }
- }
- }
- QTAILQ_FOREACH_SAFE(r, &sq->out_req_list, entry, next) {
- if (r->cqe.cid == cid) {
- if (r->aiocb) {
- r->status = NVME_CMD_ABORT_REQ;
- blk_aio_cancel_async(r->aiocb);
- }
- break;
- }
- }
- return NVME_SUCCESS;
- }
- static inline void nvme_set_timestamp(NvmeCtrl *n, uint64_t ts)
- {
- trace_pci_nvme_setfeat_timestamp(ts);
- n->host_timestamp = le64_to_cpu(ts);
- n->timestamp_set_qemu_clock_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
- }
- static inline uint64_t nvme_get_timestamp(const NvmeCtrl *n)
- {
- uint64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
- uint64_t elapsed_time = current_time - n->timestamp_set_qemu_clock_ms;
- union nvme_timestamp {
- struct {
- uint64_t timestamp:48;
- uint64_t sync:1;
- uint64_t origin:3;
- uint64_t rsvd1:12;
- };
- uint64_t all;
- };
- union nvme_timestamp ts;
- ts.all = 0;
- ts.timestamp = n->host_timestamp + elapsed_time;
- /* If the host timestamp is non-zero, set the timestamp origin */
- ts.origin = n->host_timestamp ? 0x01 : 0x00;
- trace_pci_nvme_getfeat_timestamp(ts.all);
- return cpu_to_le64(ts.all);
- }
- static uint16_t nvme_get_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
- {
- uint64_t timestamp = nvme_get_timestamp(n);
- return nvme_c2h(n, (uint8_t *)×tamp, sizeof(timestamp), req);
- }
- static int nvme_get_feature_fdp(NvmeCtrl *n, uint32_t endgrpid,
- uint32_t *result)
- {
- *result = 0;
- if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- *result = FIELD_DP16(0, FEAT_FDP, FDPE, 1);
- *result = FIELD_DP16(*result, FEAT_FDP, CONF_NDX, 0);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_get_feature_fdp_events(NvmeCtrl *n, NvmeNamespace *ns,
- NvmeRequest *req, uint32_t *result)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t cdw11 = le32_to_cpu(cmd->cdw11);
- uint16_t ph = cdw11 & 0xffff;
- uint8_t noet = (cdw11 >> 16) & 0xff;
- uint16_t ruhid, ret;
- uint32_t nentries = 0;
- uint8_t s_events_ndx = 0;
- size_t s_events_siz = sizeof(NvmeFdpEventDescr) * noet;
- g_autofree NvmeFdpEventDescr *s_events = g_malloc0(s_events_siz);
- NvmeRuHandle *ruh;
- NvmeFdpEventDescr *s_event;
- if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- if (!nvme_ph_valid(ns, ph)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ruhid = ns->fdp.phs[ph];
- ruh = &n->subsys->endgrp.fdp.ruhs[ruhid];
- assert(ruh);
- if (unlikely(noet == 0)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- for (uint8_t event_type = 0; event_type < FDP_EVT_MAX; event_type++) {
- uint8_t shift = nvme_fdp_evf_shifts[event_type];
- if (!shift && event_type) {
- /*
- * only first entry (event_type == 0) has a shift value of 0
- * other entries are simply unpopulated.
- */
- continue;
- }
- nentries++;
- s_event = &s_events[s_events_ndx];
- s_event->evt = event_type;
- s_event->evta = (ruh->event_filter >> shift) & 0x1;
- /* break if all `noet` entries are filled */
- if ((++s_events_ndx) == noet) {
- break;
- }
- }
- ret = nvme_c2h(n, s_events, s_events_siz, req);
- if (ret) {
- return ret;
- }
- *result = nentries;
- return NVME_SUCCESS;
- }
- static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t dw10 = le32_to_cpu(cmd->cdw10);
- uint32_t dw11 = le32_to_cpu(cmd->cdw11);
- uint32_t nsid = le32_to_cpu(cmd->nsid);
- uint32_t result = 0;
- uint8_t fid = NVME_GETSETFEAT_FID(dw10);
- NvmeGetFeatureSelect sel = NVME_GETFEAT_SELECT(dw10);
- uint16_t iv;
- NvmeNamespace *ns;
- int i;
- uint16_t endgrpid = 0, ret = NVME_SUCCESS;
- static const uint32_t nvme_feature_default[NVME_FID_MAX] = {
- [NVME_ARBITRATION] = NVME_ARB_AB_NOLIMIT,
- };
- trace_pci_nvme_getfeat(nvme_cid(req), nsid, fid, sel, dw11);
- if (!nvme_feature_support[fid]) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
- if (!nvme_nsid_valid(n, nsid) || nsid == NVME_NSID_BROADCAST) {
- /*
- * The Reservation Notification Mask and Reservation Persistence
- * features require a status code of Invalid Field in Command when
- * NSID is FFFFFFFFh. Since the device does not support those
- * features we can always return Invalid Namespace or Format as we
- * should do for all other features.
- */
- return NVME_INVALID_NSID | NVME_DNR;
- }
- if (!nvme_ns(n, nsid)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- switch (sel) {
- case NVME_GETFEAT_SELECT_CURRENT:
- break;
- case NVME_GETFEAT_SELECT_SAVED:
- /* no features are saveable by the controller; fallthrough */
- case NVME_GETFEAT_SELECT_DEFAULT:
- goto defaults;
- case NVME_GETFEAT_SELECT_CAP:
- result = nvme_feature_cap[fid];
- goto out;
- }
- switch (fid) {
- case NVME_TEMPERATURE_THRESHOLD:
- result = 0;
- /*
- * The controller only implements the Composite Temperature sensor, so
- * return 0 for all other sensors.
- */
- if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
- goto out;
- }
- switch (NVME_TEMP_THSEL(dw11)) {
- case NVME_TEMP_THSEL_OVER:
- result = n->features.temp_thresh_hi;
- goto out;
- case NVME_TEMP_THSEL_UNDER:
- result = n->features.temp_thresh_low;
- goto out;
- }
- return NVME_INVALID_FIELD | NVME_DNR;
- case NVME_ERROR_RECOVERY:
- if (!nvme_nsid_valid(n, nsid)) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- result = ns->features.err_rec;
- goto out;
- case NVME_VOLATILE_WRITE_CACHE:
- result = 0;
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- result = blk_enable_write_cache(ns->blkconf.blk);
- if (result) {
- break;
- }
- }
- trace_pci_nvme_getfeat_vwcache(result ? "enabled" : "disabled");
- goto out;
- case NVME_ASYNCHRONOUS_EVENT_CONF:
- result = n->features.async_config;
- goto out;
- case NVME_TIMESTAMP:
- return nvme_get_feature_timestamp(n, req);
- case NVME_HOST_BEHAVIOR_SUPPORT:
- return nvme_c2h(n, (uint8_t *)&n->features.hbs,
- sizeof(n->features.hbs), req);
- case NVME_FDP_MODE:
- endgrpid = dw11 & 0xff;
- if (endgrpid != 0x1) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ret = nvme_get_feature_fdp(n, endgrpid, &result);
- if (ret) {
- return ret;
- }
- goto out;
- case NVME_FDP_EVENTS:
- if (!nvme_nsid_valid(n, nsid)) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ret = nvme_get_feature_fdp_events(n, ns, req, &result);
- if (ret) {
- return ret;
- }
- goto out;
- default:
- break;
- }
- defaults:
- switch (fid) {
- case NVME_TEMPERATURE_THRESHOLD:
- result = 0;
- if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
- break;
- }
- if (NVME_TEMP_THSEL(dw11) == NVME_TEMP_THSEL_OVER) {
- result = NVME_TEMPERATURE_WARNING;
- }
- break;
- case NVME_NUMBER_OF_QUEUES:
- result = (n->conf_ioqpairs - 1) | ((n->conf_ioqpairs - 1) << 16);
- trace_pci_nvme_getfeat_numq(result);
- break;
- case NVME_INTERRUPT_VECTOR_CONF:
- iv = dw11 & 0xffff;
- if (iv >= n->conf_ioqpairs + 1) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- result = iv;
- if (iv == n->admin_cq.vector) {
- result |= NVME_INTVC_NOCOALESCING;
- }
- break;
- case NVME_FDP_MODE:
- endgrpid = dw11 & 0xff;
- if (endgrpid != 0x1) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ret = nvme_get_feature_fdp(n, endgrpid, &result);
- if (ret) {
- return ret;
- }
- break;
- case NVME_WRITE_ATOMICITY:
- result = n->dn;
- break;
- default:
- result = nvme_feature_default[fid];
- break;
- }
- out:
- req->cqe.result = cpu_to_le32(result);
- return ret;
- }
- static uint16_t nvme_set_feature_timestamp(NvmeCtrl *n, NvmeRequest *req)
- {
- uint16_t ret;
- uint64_t timestamp;
- ret = nvme_h2c(n, (uint8_t *)×tamp, sizeof(timestamp), req);
- if (ret) {
- return ret;
- }
- nvme_set_timestamp(n, timestamp);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_set_feature_fdp_events(NvmeCtrl *n, NvmeNamespace *ns,
- NvmeRequest *req)
- {
- NvmeCmd *cmd = &req->cmd;
- uint32_t cdw11 = le32_to_cpu(cmd->cdw11);
- uint16_t ph = cdw11 & 0xffff;
- uint8_t noet = (cdw11 >> 16) & 0xff;
- uint16_t ret, ruhid;
- uint8_t enable = le32_to_cpu(cmd->cdw12) & 0x1;
- uint8_t event_mask = 0;
- unsigned int i;
- g_autofree uint8_t *events = g_malloc0(noet);
- NvmeRuHandle *ruh = NULL;
- assert(ns);
- if (!n->subsys || !n->subsys->endgrp.fdp.enabled) {
- return NVME_FDP_DISABLED | NVME_DNR;
- }
- if (!nvme_ph_valid(ns, ph)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ruhid = ns->fdp.phs[ph];
- ruh = &n->subsys->endgrp.fdp.ruhs[ruhid];
- ret = nvme_h2c(n, events, noet, req);
- if (ret) {
- return ret;
- }
- for (i = 0; i < noet; i++) {
- event_mask |= (1 << nvme_fdp_evf_shifts[events[i]]);
- }
- if (enable) {
- ruh->event_filter |= event_mask;
- } else {
- ruh->event_filter = ruh->event_filter & ~event_mask;
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns = NULL;
- NvmeCmd *cmd = &req->cmd;
- uint32_t dw10 = le32_to_cpu(cmd->cdw10);
- uint32_t dw11 = le32_to_cpu(cmd->cdw11);
- uint32_t nsid = le32_to_cpu(cmd->nsid);
- uint8_t fid = NVME_GETSETFEAT_FID(dw10);
- uint8_t save = NVME_SETFEAT_SAVE(dw10);
- uint16_t status;
- int i;
- NvmeIdCtrl *id = &n->id_ctrl;
- NvmeAtomic *atomic = &n->atomic;
- trace_pci_nvme_setfeat(nvme_cid(req), nsid, fid, save, dw11);
- if (save && !(nvme_feature_cap[fid] & NVME_FEAT_CAP_SAVE)) {
- return NVME_FID_NOT_SAVEABLE | NVME_DNR;
- }
- if (!nvme_feature_support[fid]) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (nvme_feature_cap[fid] & NVME_FEAT_CAP_NS) {
- if (nsid != NVME_NSID_BROADCAST) {
- if (!nvme_nsid_valid(n, nsid)) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (unlikely(!ns)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- } else if (nsid && nsid != NVME_NSID_BROADCAST) {
- if (!nvme_nsid_valid(n, nsid)) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- return NVME_FEAT_NOT_NS_SPEC | NVME_DNR;
- }
- if (!(nvme_feature_cap[fid] & NVME_FEAT_CAP_CHANGE)) {
- return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
- }
- switch (fid) {
- case NVME_TEMPERATURE_THRESHOLD:
- if (NVME_TEMP_TMPSEL(dw11) != NVME_TEMP_TMPSEL_COMPOSITE) {
- break;
- }
- switch (NVME_TEMP_THSEL(dw11)) {
- case NVME_TEMP_THSEL_OVER:
- n->features.temp_thresh_hi = NVME_TEMP_TMPTH(dw11);
- break;
- case NVME_TEMP_THSEL_UNDER:
- n->features.temp_thresh_low = NVME_TEMP_TMPTH(dw11);
- break;
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if ((n->temperature >= n->features.temp_thresh_hi) ||
- (n->temperature <= n->features.temp_thresh_low)) {
- nvme_smart_event(n, NVME_SMART_TEMPERATURE);
- }
- break;
- case NVME_ERROR_RECOVERY:
- if (nsid == NVME_NSID_BROADCAST) {
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- if (NVME_ID_NS_NSFEAT_DULBE(ns->id_ns.nsfeat)) {
- ns->features.err_rec = dw11;
- }
- }
- break;
- }
- assert(ns);
- if (NVME_ID_NS_NSFEAT_DULBE(ns->id_ns.nsfeat)) {
- ns->features.err_rec = dw11;
- }
- break;
- case NVME_VOLATILE_WRITE_CACHE:
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- if (!(dw11 & 0x1) && blk_enable_write_cache(ns->blkconf.blk)) {
- blk_flush(ns->blkconf.blk);
- }
- blk_set_enable_write_cache(ns->blkconf.blk, dw11 & 1);
- }
- break;
- case NVME_NUMBER_OF_QUEUES:
- if (n->qs_created) {
- return NVME_CMD_SEQ_ERROR | NVME_DNR;
- }
- /*
- * NVMe v1.3, Section 5.21.1.7: FFFFh is not an allowed value for NCQR
- * and NSQR.
- */
- if ((dw11 & 0xffff) == 0xffff || ((dw11 >> 16) & 0xffff) == 0xffff) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- trace_pci_nvme_setfeat_numq((dw11 & 0xffff) + 1,
- ((dw11 >> 16) & 0xffff) + 1,
- n->conf_ioqpairs,
- n->conf_ioqpairs);
- req->cqe.result = cpu_to_le32((n->conf_ioqpairs - 1) |
- ((n->conf_ioqpairs - 1) << 16));
- break;
- case NVME_ASYNCHRONOUS_EVENT_CONF:
- n->features.async_config = dw11;
- break;
- case NVME_TIMESTAMP:
- return nvme_set_feature_timestamp(n, req);
- case NVME_HOST_BEHAVIOR_SUPPORT:
- status = nvme_h2c(n, (uint8_t *)&n->features.hbs,
- sizeof(n->features.hbs), req);
- if (status) {
- return status;
- }
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- ns->id_ns.nlbaf = ns->nlbaf - 1;
- if (!n->features.hbs.lbafee) {
- ns->id_ns.nlbaf = MIN(ns->id_ns.nlbaf, 15);
- }
- }
- return status;
- case NVME_COMMAND_SET_PROFILE:
- if (dw11 & 0x1ff) {
- trace_pci_nvme_err_invalid_iocsci(dw11 & 0x1ff);
- return NVME_IOCS_COMBINATION_REJECTED | NVME_DNR;
- }
- break;
- case NVME_FDP_MODE:
- /* spec: abort with cmd seq err if there's one or more NS' in endgrp */
- return NVME_CMD_SEQ_ERROR | NVME_DNR;
- case NVME_FDP_EVENTS:
- return nvme_set_feature_fdp_events(n, ns, req);
- case NVME_WRITE_ATOMICITY:
- n->dn = 0x1 & dw11;
- if (n->dn) {
- atomic->atomic_max_write_size = le16_to_cpu(id->awupf) + 1;
- } else {
- atomic->atomic_max_write_size = le16_to_cpu(id->awun) + 1;
- }
- if (atomic->atomic_max_write_size == 1) {
- atomic->atomic_writes = 0;
- } else {
- atomic->atomic_writes = 1;
- }
- break;
- default:
- return NVME_FEAT_NOT_CHANGEABLE | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_aer(NvmeCtrl *n, NvmeRequest *req)
- {
- trace_pci_nvme_aer(nvme_cid(req));
- if (n->outstanding_aers > n->params.aerl) {
- trace_pci_nvme_aer_aerl_exceeded();
- return NVME_AER_LIMIT_EXCEEDED;
- }
- n->aer_reqs[n->outstanding_aers] = req;
- n->outstanding_aers++;
- if (!QTAILQ_EMPTY(&n->aer_queue)) {
- nvme_process_aers(n);
- }
- return NVME_NO_COMPLETE;
- }
- static void nvme_update_dsm_limits(NvmeCtrl *n, NvmeNamespace *ns)
- {
- if (ns) {
- n->dmrsl =
- MIN_NON_ZERO(n->dmrsl, BDRV_REQUEST_MAX_BYTES / nvme_l2b(ns, 1));
- return;
- }
- for (uint32_t nsid = 1; nsid <= NVME_MAX_NAMESPACES; nsid++) {
- ns = nvme_ns(n, nsid);
- if (!ns) {
- continue;
- }
- n->dmrsl =
- MIN_NON_ZERO(n->dmrsl, BDRV_REQUEST_MAX_BYTES / nvme_l2b(ns, 1));
- }
- }
- static bool nvme_csi_supported(NvmeCtrl *n, uint8_t csi)
- {
- uint32_t cc;
- switch (csi) {
- case NVME_CSI_NVM:
- return true;
- case NVME_CSI_ZONED:
- cc = ldl_le_p(&n->bar.cc);
- return NVME_CC_CSS(cc) == NVME_CC_CSS_ALL;
- }
- g_assert_not_reached();
- }
- static void nvme_detach_ns(NvmeCtrl *n, NvmeNamespace *ns)
- {
- assert(ns->attached > 0);
- n->namespaces[ns->params.nsid] = NULL;
- ns->attached--;
- }
- static uint16_t nvme_ns_attachment(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns;
- NvmeCtrl *ctrl;
- uint16_t list[NVME_CONTROLLER_LIST_SIZE] = {};
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
- uint8_t sel = dw10 & 0xf;
- uint16_t *nr_ids = &list[0];
- uint16_t *ids = &list[1];
- uint16_t ret;
- int i;
- trace_pci_nvme_ns_attachment(nvme_cid(req), dw10 & 0xf);
- if (!nvme_nsid_valid(n, nsid)) {
- return NVME_INVALID_NSID | NVME_DNR;
- }
- ns = nvme_subsys_ns(n->subsys, nsid);
- if (!ns) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ret = nvme_h2c(n, (uint8_t *)list, 4096, req);
- if (ret) {
- return ret;
- }
- if (!*nr_ids) {
- return NVME_NS_CTRL_LIST_INVALID | NVME_DNR;
- }
- *nr_ids = MIN(*nr_ids, NVME_CONTROLLER_LIST_SIZE - 1);
- for (i = 0; i < *nr_ids; i++) {
- ctrl = nvme_subsys_ctrl(n->subsys, ids[i]);
- if (!ctrl) {
- return NVME_NS_CTRL_LIST_INVALID | NVME_DNR;
- }
- switch (sel) {
- case NVME_NS_ATTACHMENT_ATTACH:
- if (nvme_ns(n, nsid)) {
- return NVME_NS_ALREADY_ATTACHED | NVME_DNR;
- }
- if (ns->attached && !ns->params.shared) {
- return NVME_NS_PRIVATE | NVME_DNR;
- }
- if (!nvme_csi_supported(n, ns->csi)) {
- return NVME_IOCS_NOT_SUPPORTED | NVME_DNR;
- }
- nvme_attach_ns(ctrl, ns);
- nvme_update_dsm_limits(ctrl, ns);
- break;
- case NVME_NS_ATTACHMENT_DETACH:
- nvme_detach_ns(ctrl, ns);
- nvme_update_dsm_limits(ctrl, NULL);
- break;
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- /*
- * Add namespace id to the changed namespace id list for event clearing
- * via Get Log Page command.
- */
- if (!test_and_set_bit(nsid, ctrl->changed_nsids)) {
- nvme_enqueue_event(ctrl, NVME_AER_TYPE_NOTICE,
- NVME_AER_INFO_NOTICE_NS_ATTR_CHANGED,
- NVME_LOG_CHANGED_NSLIST);
- }
- }
- return NVME_SUCCESS;
- }
- typedef struct NvmeFormatAIOCB {
- BlockAIOCB common;
- BlockAIOCB *aiocb;
- NvmeRequest *req;
- int ret;
- NvmeNamespace *ns;
- uint32_t nsid;
- bool broadcast;
- int64_t offset;
- uint8_t lbaf;
- uint8_t mset;
- uint8_t pi;
- uint8_t pil;
- } NvmeFormatAIOCB;
- static void nvme_format_cancel(BlockAIOCB *aiocb)
- {
- NvmeFormatAIOCB *iocb = container_of(aiocb, NvmeFormatAIOCB, common);
- iocb->ret = -ECANCELED;
- if (iocb->aiocb) {
- blk_aio_cancel_async(iocb->aiocb);
- iocb->aiocb = NULL;
- }
- }
- static const AIOCBInfo nvme_format_aiocb_info = {
- .aiocb_size = sizeof(NvmeFormatAIOCB),
- .cancel_async = nvme_format_cancel,
- };
- static void nvme_format_set(NvmeNamespace *ns, uint8_t lbaf, uint8_t mset,
- uint8_t pi, uint8_t pil)
- {
- uint8_t lbafl = lbaf & 0xf;
- uint8_t lbafu = lbaf >> 4;
- trace_pci_nvme_format_set(ns->params.nsid, lbaf, mset, pi, pil);
- ns->id_ns.dps = (pil << 3) | pi;
- ns->id_ns.flbas = (lbafu << 5) | (mset << 4) | lbafl;
- nvme_ns_init_format(ns);
- }
- static void nvme_do_format(NvmeFormatAIOCB *iocb);
- static void nvme_format_ns_cb(void *opaque, int ret)
- {
- NvmeFormatAIOCB *iocb = opaque;
- NvmeNamespace *ns = iocb->ns;
- int bytes;
- if (iocb->ret < 0) {
- goto done;
- } else if (ret < 0) {
- iocb->ret = ret;
- goto done;
- }
- assert(ns);
- if (iocb->offset < ns->size) {
- bytes = MIN(BDRV_REQUEST_MAX_BYTES, ns->size - iocb->offset);
- iocb->aiocb = blk_aio_pwrite_zeroes(ns->blkconf.blk, iocb->offset,
- bytes, BDRV_REQ_MAY_UNMAP,
- nvme_format_ns_cb, iocb);
- iocb->offset += bytes;
- return;
- }
- nvme_format_set(ns, iocb->lbaf, iocb->mset, iocb->pi, iocb->pil);
- ns->status = 0x0;
- iocb->ns = NULL;
- iocb->offset = 0;
- done:
- nvme_do_format(iocb);
- }
- static uint16_t nvme_format_check(NvmeNamespace *ns, uint8_t lbaf, uint8_t pi)
- {
- if (ns->params.zoned) {
- return NVME_INVALID_FORMAT | NVME_DNR;
- }
- if (lbaf > ns->id_ns.nlbaf) {
- return NVME_INVALID_FORMAT | NVME_DNR;
- }
- if (pi && (ns->id_ns.lbaf[lbaf].ms < nvme_pi_tuple_size(ns))) {
- return NVME_INVALID_FORMAT | NVME_DNR;
- }
- if (pi && pi > NVME_ID_NS_DPS_TYPE_3) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- return NVME_SUCCESS;
- }
- static void nvme_do_format(NvmeFormatAIOCB *iocb)
- {
- NvmeRequest *req = iocb->req;
- NvmeCtrl *n = nvme_ctrl(req);
- uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
- uint8_t lbaf = dw10 & 0xf;
- uint8_t pi = (dw10 >> 5) & 0x7;
- uint16_t status;
- int i;
- if (iocb->ret < 0) {
- goto done;
- }
- if (iocb->broadcast) {
- for (i = iocb->nsid + 1; i <= NVME_MAX_NAMESPACES; i++) {
- iocb->ns = nvme_ns(n, i);
- if (iocb->ns) {
- iocb->nsid = i;
- break;
- }
- }
- }
- if (!iocb->ns) {
- goto done;
- }
- status = nvme_format_check(iocb->ns, lbaf, pi);
- if (status) {
- req->status = status;
- goto done;
- }
- iocb->ns->status = NVME_FORMAT_IN_PROGRESS;
- nvme_format_ns_cb(iocb, 0);
- return;
- done:
- iocb->common.cb(iocb->common.opaque, iocb->ret);
- qemu_aio_unref(iocb);
- }
- static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeFormatAIOCB *iocb;
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
- uint8_t lbaf = dw10 & 0xf;
- uint8_t mset = (dw10 >> 4) & 0x1;
- uint8_t pi = (dw10 >> 5) & 0x7;
- uint8_t pil = (dw10 >> 8) & 0x1;
- uint8_t lbafu = (dw10 >> 12) & 0x3;
- uint16_t status;
- iocb = qemu_aio_get(&nvme_format_aiocb_info, NULL, nvme_misc_cb, req);
- iocb->req = req;
- iocb->ret = 0;
- iocb->ns = NULL;
- iocb->nsid = 0;
- iocb->lbaf = lbaf;
- iocb->mset = mset;
- iocb->pi = pi;
- iocb->pil = pil;
- iocb->broadcast = (nsid == NVME_NSID_BROADCAST);
- iocb->offset = 0;
- if (n->features.hbs.lbafee) {
- iocb->lbaf |= lbafu << 4;
- }
- if (!iocb->broadcast) {
- if (!nvme_nsid_valid(n, nsid)) {
- status = NVME_INVALID_NSID | NVME_DNR;
- goto out;
- }
- iocb->ns = nvme_ns(n, nsid);
- if (!iocb->ns) {
- status = NVME_INVALID_FIELD | NVME_DNR;
- goto out;
- }
- }
- req->aiocb = &iocb->common;
- nvme_do_format(iocb);
- return NVME_NO_COMPLETE;
- out:
- qemu_aio_unref(iocb);
- return status;
- }
- static void nvme_get_virt_res_num(NvmeCtrl *n, uint8_t rt, int *num_total,
- int *num_prim, int *num_sec)
- {
- *num_total = le32_to_cpu(rt ?
- n->pri_ctrl_cap.vifrt : n->pri_ctrl_cap.vqfrt);
- *num_prim = le16_to_cpu(rt ?
- n->pri_ctrl_cap.virfap : n->pri_ctrl_cap.vqrfap);
- *num_sec = le16_to_cpu(rt ? n->pri_ctrl_cap.virfa : n->pri_ctrl_cap.vqrfa);
- }
- static uint16_t nvme_assign_virt_res_to_prim(NvmeCtrl *n, NvmeRequest *req,
- uint16_t cntlid, uint8_t rt,
- int nr)
- {
- int num_total, num_prim, num_sec;
- if (cntlid != n->cntlid) {
- return NVME_INVALID_CTRL_ID | NVME_DNR;
- }
- nvme_get_virt_res_num(n, rt, &num_total, &num_prim, &num_sec);
- if (nr > num_total) {
- return NVME_INVALID_NUM_RESOURCES | NVME_DNR;
- }
- if (nr > num_total - num_sec) {
- return NVME_INVALID_RESOURCE_ID | NVME_DNR;
- }
- if (rt) {
- n->next_pri_ctrl_cap.virfap = cpu_to_le16(nr);
- } else {
- n->next_pri_ctrl_cap.vqrfap = cpu_to_le16(nr);
- }
- req->cqe.result = cpu_to_le32(nr);
- return req->status;
- }
- static void nvme_update_virt_res(NvmeCtrl *n, NvmeSecCtrlEntry *sctrl,
- uint8_t rt, int nr)
- {
- int prev_nr, prev_total;
- if (rt) {
- prev_nr = le16_to_cpu(sctrl->nvi);
- prev_total = le32_to_cpu(n->pri_ctrl_cap.virfa);
- sctrl->nvi = cpu_to_le16(nr);
- n->pri_ctrl_cap.virfa = cpu_to_le32(prev_total + nr - prev_nr);
- } else {
- prev_nr = le16_to_cpu(sctrl->nvq);
- prev_total = le32_to_cpu(n->pri_ctrl_cap.vqrfa);
- sctrl->nvq = cpu_to_le16(nr);
- n->pri_ctrl_cap.vqrfa = cpu_to_le32(prev_total + nr - prev_nr);
- }
- }
- static uint16_t nvme_assign_virt_res_to_sec(NvmeCtrl *n, NvmeRequest *req,
- uint16_t cntlid, uint8_t rt, int nr)
- {
- int num_total, num_prim, num_sec, num_free, diff, limit;
- NvmeSecCtrlEntry *sctrl;
- sctrl = nvme_sctrl_for_cntlid(n, cntlid);
- if (!sctrl) {
- return NVME_INVALID_CTRL_ID | NVME_DNR;
- }
- if (sctrl->scs) {
- return NVME_INVALID_SEC_CTRL_STATE | NVME_DNR;
- }
- limit = le16_to_cpu(rt ? n->pri_ctrl_cap.vifrsm : n->pri_ctrl_cap.vqfrsm);
- if (nr > limit) {
- return NVME_INVALID_NUM_RESOURCES | NVME_DNR;
- }
- nvme_get_virt_res_num(n, rt, &num_total, &num_prim, &num_sec);
- num_free = num_total - num_prim - num_sec;
- diff = nr - le16_to_cpu(rt ? sctrl->nvi : sctrl->nvq);
- if (diff > num_free) {
- return NVME_INVALID_RESOURCE_ID | NVME_DNR;
- }
- nvme_update_virt_res(n, sctrl, rt, nr);
- req->cqe.result = cpu_to_le32(nr);
- return req->status;
- }
- static uint16_t nvme_virt_set_state(NvmeCtrl *n, uint16_t cntlid, bool online)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- NvmeCtrl *sn = NULL;
- NvmeSecCtrlEntry *sctrl;
- int vf_index;
- sctrl = nvme_sctrl_for_cntlid(n, cntlid);
- if (!sctrl) {
- return NVME_INVALID_CTRL_ID | NVME_DNR;
- }
- if (!pci_is_vf(pci)) {
- vf_index = le16_to_cpu(sctrl->vfn) - 1;
- sn = NVME(pcie_sriov_get_vf_at_index(pci, vf_index));
- }
- if (online) {
- if (!sctrl->nvi || (le16_to_cpu(sctrl->nvq) < 2) || !sn) {
- return NVME_INVALID_SEC_CTRL_STATE | NVME_DNR;
- }
- if (!sctrl->scs) {
- sctrl->scs = 0x1;
- nvme_ctrl_reset(sn, NVME_RESET_FUNCTION);
- }
- } else {
- nvme_update_virt_res(n, sctrl, NVME_VIRT_RES_INTERRUPT, 0);
- nvme_update_virt_res(n, sctrl, NVME_VIRT_RES_QUEUE, 0);
- if (sctrl->scs) {
- sctrl->scs = 0x0;
- if (sn) {
- nvme_ctrl_reset(sn, NVME_RESET_FUNCTION);
- }
- }
- }
- return NVME_SUCCESS;
- }
- static uint16_t nvme_virt_mngmt(NvmeCtrl *n, NvmeRequest *req)
- {
- uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
- uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
- uint8_t act = dw10 & 0xf;
- uint8_t rt = (dw10 >> 8) & 0x7;
- uint16_t cntlid = (dw10 >> 16) & 0xffff;
- int nr = dw11 & 0xffff;
- trace_pci_nvme_virt_mngmt(nvme_cid(req), act, cntlid, rt ? "VI" : "VQ", nr);
- if (rt != NVME_VIRT_RES_QUEUE && rt != NVME_VIRT_RES_INTERRUPT) {
- return NVME_INVALID_RESOURCE_ID | NVME_DNR;
- }
- switch (act) {
- case NVME_VIRT_MNGMT_ACTION_SEC_ASSIGN:
- return nvme_assign_virt_res_to_sec(n, req, cntlid, rt, nr);
- case NVME_VIRT_MNGMT_ACTION_PRM_ALLOC:
- return nvme_assign_virt_res_to_prim(n, req, cntlid, rt, nr);
- case NVME_VIRT_MNGMT_ACTION_SEC_ONLINE:
- return nvme_virt_set_state(n, cntlid, true);
- case NVME_VIRT_MNGMT_ACTION_SEC_OFFLINE:
- return nvme_virt_set_state(n, cntlid, false);
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- }
- static uint16_t nvme_dbbuf_config(NvmeCtrl *n, const NvmeRequest *req)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- uint64_t dbs_addr = le64_to_cpu(req->cmd.dptr.prp1);
- uint64_t eis_addr = le64_to_cpu(req->cmd.dptr.prp2);
- int i;
- /* Address should be page aligned */
- if (dbs_addr & (n->page_size - 1) || eis_addr & (n->page_size - 1)) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- /* Save shadow buffer base addr for use during queue creation */
- n->dbbuf_dbs = dbs_addr;
- n->dbbuf_eis = eis_addr;
- n->dbbuf_enabled = true;
- for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
- NvmeSQueue *sq = n->sq[i];
- NvmeCQueue *cq = n->cq[i];
- if (sq) {
- /*
- * CAP.DSTRD is 0, so offset of ith sq db_addr is (i<<3)
- * nvme_process_db() uses this hard-coded way to calculate
- * doorbell offsets. Be consistent with that here.
- */
- sq->db_addr = dbs_addr + (i << 3);
- sq->ei_addr = eis_addr + (i << 3);
- stl_le_pci_dma(pci, sq->db_addr, sq->tail, MEMTXATTRS_UNSPECIFIED);
- if (n->params.ioeventfd && sq->sqid != 0) {
- if (!nvme_init_sq_ioeventfd(sq)) {
- sq->ioeventfd_enabled = true;
- }
- }
- }
- if (cq) {
- /* CAP.DSTRD is 0, so offset of ith cq db_addr is (i<<3)+(1<<2) */
- cq->db_addr = dbs_addr + (i << 3) + (1 << 2);
- cq->ei_addr = eis_addr + (i << 3) + (1 << 2);
- stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
- if (n->params.ioeventfd && cq->cqid != 0) {
- if (!nvme_init_cq_ioeventfd(cq)) {
- cq->ioeventfd_enabled = true;
- }
- }
- }
- }
- trace_pci_nvme_dbbuf_config(dbs_addr, eis_addr);
- return NVME_SUCCESS;
- }
- static uint16_t nvme_directive_send(NvmeCtrl *n, NvmeRequest *req)
- {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- static uint16_t nvme_directive_receive(NvmeCtrl *n, NvmeRequest *req)
- {
- NvmeNamespace *ns;
- uint32_t dw10 = le32_to_cpu(req->cmd.cdw10);
- uint32_t dw11 = le32_to_cpu(req->cmd.cdw11);
- uint32_t nsid = le32_to_cpu(req->cmd.nsid);
- uint8_t doper, dtype;
- uint32_t numd, trans_len;
- NvmeDirectiveIdentify id = {
- .supported = 1 << NVME_DIRECTIVE_IDENTIFY,
- .enabled = 1 << NVME_DIRECTIVE_IDENTIFY,
- };
- numd = dw10 + 1;
- doper = dw11 & 0xff;
- dtype = (dw11 >> 8) & 0xff;
- trans_len = MIN(sizeof(NvmeDirectiveIdentify), numd << 2);
- if (nsid == NVME_NSID_BROADCAST || dtype != NVME_DIRECTIVE_IDENTIFY ||
- doper != NVME_DIRECTIVE_RETURN_PARAMS) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- ns = nvme_ns(n, nsid);
- if (!ns) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- switch (dtype) {
- case NVME_DIRECTIVE_IDENTIFY:
- switch (doper) {
- case NVME_DIRECTIVE_RETURN_PARAMS:
- if (ns->endgrp && ns->endgrp->fdp.enabled) {
- id.supported |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
- id.enabled |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
- id.persistent |= 1 << NVME_DIRECTIVE_DATA_PLACEMENT;
- }
- return nvme_c2h(n, (uint8_t *)&id, trans_len, req);
- default:
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- default:
- return NVME_INVALID_FIELD;
- }
- }
- static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeRequest *req)
- {
- trace_pci_nvme_admin_cmd(nvme_cid(req), nvme_sqid(req), req->cmd.opcode,
- nvme_adm_opc_str(req->cmd.opcode));
- if (!(n->cse.acs[req->cmd.opcode] & NVME_CMD_EFF_CSUPP)) {
- trace_pci_nvme_err_invalid_admin_opc(req->cmd.opcode);
- return NVME_INVALID_OPCODE | NVME_DNR;
- }
- /* SGLs shall not be used for Admin commands in NVMe over PCIe */
- if (NVME_CMD_FLAGS_PSDT(req->cmd.flags) != NVME_PSDT_PRP) {
- return NVME_INVALID_FIELD | NVME_DNR;
- }
- if (NVME_CMD_FLAGS_FUSE(req->cmd.flags)) {
- return NVME_INVALID_FIELD;
- }
- switch (req->cmd.opcode) {
- case NVME_ADM_CMD_DELETE_SQ:
- return nvme_del_sq(n, req);
- case NVME_ADM_CMD_CREATE_SQ:
- return nvme_create_sq(n, req);
- case NVME_ADM_CMD_GET_LOG_PAGE:
- return nvme_get_log(n, req);
- case NVME_ADM_CMD_DELETE_CQ:
- return nvme_del_cq(n, req);
- case NVME_ADM_CMD_CREATE_CQ:
- return nvme_create_cq(n, req);
- case NVME_ADM_CMD_IDENTIFY:
- return nvme_identify(n, req);
- case NVME_ADM_CMD_ABORT:
- return nvme_abort(n, req);
- case NVME_ADM_CMD_SET_FEATURES:
- return nvme_set_feature(n, req);
- case NVME_ADM_CMD_GET_FEATURES:
- return nvme_get_feature(n, req);
- case NVME_ADM_CMD_ASYNC_EV_REQ:
- return nvme_aer(n, req);
- case NVME_ADM_CMD_NS_ATTACHMENT:
- return nvme_ns_attachment(n, req);
- case NVME_ADM_CMD_VIRT_MNGMT:
- return nvme_virt_mngmt(n, req);
- case NVME_ADM_CMD_DBBUF_CONFIG:
- return nvme_dbbuf_config(n, req);
- case NVME_ADM_CMD_FORMAT_NVM:
- return nvme_format(n, req);
- case NVME_ADM_CMD_DIRECTIVE_SEND:
- return nvme_directive_send(n, req);
- case NVME_ADM_CMD_DIRECTIVE_RECV:
- return nvme_directive_receive(n, req);
- default:
- g_assert_not_reached();
- }
- return NVME_INVALID_OPCODE | NVME_DNR;
- }
- static void nvme_update_sq_eventidx(const NvmeSQueue *sq)
- {
- trace_pci_nvme_update_sq_eventidx(sq->sqid, sq->tail);
- stl_le_pci_dma(PCI_DEVICE(sq->ctrl), sq->ei_addr, sq->tail,
- MEMTXATTRS_UNSPECIFIED);
- }
- static void nvme_update_sq_tail(NvmeSQueue *sq)
- {
- ldl_le_pci_dma(PCI_DEVICE(sq->ctrl), sq->db_addr, &sq->tail,
- MEMTXATTRS_UNSPECIFIED);
- trace_pci_nvme_update_sq_tail(sq->sqid, sq->tail);
- }
- #define NVME_ATOMIC_NO_START 0
- #define NVME_ATOMIC_START_ATOMIC 1
- #define NVME_ATOMIC_START_NONATOMIC 2
- static int nvme_atomic_write_check(NvmeCtrl *n, NvmeCmd *cmd,
- NvmeAtomic *atomic)
- {
- NvmeRwCmd *rw = (NvmeRwCmd *)cmd;
- uint64_t slba = le64_to_cpu(rw->slba);
- uint32_t nlb = (uint32_t)le16_to_cpu(rw->nlb);
- uint64_t elba = slba + nlb;
- bool cmd_atomic_wr = true;
- int i;
- if ((cmd->opcode == NVME_CMD_READ) || ((cmd->opcode == NVME_CMD_WRITE) &&
- ((rw->nlb + 1) > atomic->atomic_max_write_size))) {
- cmd_atomic_wr = false;
- }
- /*
- * Walk the queues to see if there are any atomic conflicts.
- */
- for (i = 1; i < n->params.max_ioqpairs + 1; i++) {
- NvmeSQueue *sq;
- NvmeRequest *req;
- NvmeRwCmd *req_rw;
- uint64_t req_slba;
- uint32_t req_nlb;
- uint64_t req_elba;
- sq = n->sq[i];
- if (!sq) {
- continue;
- }
- /*
- * Walk all the requests on a given queue.
- */
- QTAILQ_FOREACH(req, &sq->out_req_list, entry) {
- req_rw = (NvmeRwCmd *)&req->cmd;
- if (((req_rw->opcode == NVME_CMD_WRITE) ||
- (req_rw->opcode == NVME_CMD_READ)) &&
- (cmd->nsid == req->ns->params.nsid)) {
- req_slba = le64_to_cpu(req_rw->slba);
- req_nlb = (uint32_t)le16_to_cpu(req_rw->nlb);
- req_elba = req_slba + req_nlb;
- if (cmd_atomic_wr) {
- if ((elba >= req_slba) && (slba <= req_elba)) {
- return NVME_ATOMIC_NO_START;
- }
- } else {
- if (req->atomic_write && ((elba >= req_slba) &&
- (slba <= req_elba))) {
- return NVME_ATOMIC_NO_START;
- }
- }
- }
- }
- }
- if (cmd_atomic_wr) {
- return NVME_ATOMIC_START_ATOMIC;
- }
- return NVME_ATOMIC_START_NONATOMIC;
- }
- static NvmeAtomic *nvme_get_atomic(NvmeCtrl *n, NvmeCmd *cmd)
- {
- if (n->atomic.atomic_writes) {
- return &n->atomic;
- }
- return NULL;
- }
- static void nvme_process_sq(void *opaque)
- {
- NvmeSQueue *sq = opaque;
- NvmeCtrl *n = sq->ctrl;
- NvmeCQueue *cq = n->cq[sq->cqid];
- uint16_t status;
- hwaddr addr;
- NvmeCmd cmd;
- NvmeRequest *req;
- if (n->dbbuf_enabled) {
- nvme_update_sq_tail(sq);
- }
- while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
- NvmeAtomic *atomic;
- bool cmd_is_atomic;
- addr = sq->dma_addr + (sq->head << NVME_SQES);
- if (nvme_addr_read(n, addr, (void *)&cmd, sizeof(cmd))) {
- trace_pci_nvme_err_addr_read(addr);
- trace_pci_nvme_err_cfs();
- stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
- break;
- }
- atomic = nvme_get_atomic(n, &cmd);
- cmd_is_atomic = false;
- if (sq->sqid && atomic) {
- int ret;
- ret = nvme_atomic_write_check(n, &cmd, atomic);
- switch (ret) {
- case NVME_ATOMIC_NO_START:
- qemu_bh_schedule(sq->bh);
- return;
- case NVME_ATOMIC_START_ATOMIC:
- cmd_is_atomic = true;
- break;
- case NVME_ATOMIC_START_NONATOMIC:
- default:
- break;
- }
- }
- nvme_inc_sq_head(sq);
- req = QTAILQ_FIRST(&sq->req_list);
- QTAILQ_REMOVE(&sq->req_list, req, entry);
- QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
- nvme_req_clear(req);
- req->cqe.cid = cmd.cid;
- memcpy(&req->cmd, &cmd, sizeof(NvmeCmd));
- if (sq->sqid && atomic) {
- req->atomic_write = cmd_is_atomic;
- }
- status = sq->sqid ? nvme_io_cmd(n, req) :
- nvme_admin_cmd(n, req);
- if (status != NVME_NO_COMPLETE) {
- req->status = status;
- nvme_enqueue_req_completion(cq, req);
- }
- if (n->dbbuf_enabled) {
- nvme_update_sq_eventidx(sq);
- nvme_update_sq_tail(sq);
- }
- }
- }
- static void nvme_update_msixcap_ts(PCIDevice *pci_dev, uint32_t table_size)
- {
- uint8_t *config;
- if (!msix_present(pci_dev)) {
- return;
- }
- assert(table_size > 0 && table_size <= pci_dev->msix_entries_nr);
- config = pci_dev->config + pci_dev->msix_cap;
- pci_set_word_by_mask(config + PCI_MSIX_FLAGS, PCI_MSIX_FLAGS_QSIZE,
- table_size - 1);
- }
- static void nvme_activate_virt_res(NvmeCtrl *n)
- {
- PCIDevice *pci_dev = PCI_DEVICE(n);
- NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
- NvmeSecCtrlEntry *sctrl;
- /* -1 to account for the admin queue */
- if (pci_is_vf(pci_dev)) {
- sctrl = nvme_sctrl(n);
- cap->vqprt = sctrl->nvq;
- cap->viprt = sctrl->nvi;
- n->conf_ioqpairs = sctrl->nvq ? le16_to_cpu(sctrl->nvq) - 1 : 0;
- n->conf_msix_qsize = sctrl->nvi ? le16_to_cpu(sctrl->nvi) : 1;
- } else {
- cap->vqrfap = n->next_pri_ctrl_cap.vqrfap;
- cap->virfap = n->next_pri_ctrl_cap.virfap;
- n->conf_ioqpairs = le16_to_cpu(cap->vqprt) +
- le16_to_cpu(cap->vqrfap) - 1;
- n->conf_msix_qsize = le16_to_cpu(cap->viprt) +
- le16_to_cpu(cap->virfap);
- }
- }
- static void nvme_ctrl_reset(NvmeCtrl *n, NvmeResetType rst)
- {
- PCIDevice *pci_dev = PCI_DEVICE(n);
- NvmeSecCtrlEntry *sctrl;
- NvmeNamespace *ns;
- int i;
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- nvme_ns_drain(ns);
- }
- for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
- if (n->sq[i] != NULL) {
- nvme_free_sq(n->sq[i], n);
- }
- }
- for (i = 0; i < n->params.max_ioqpairs + 1; i++) {
- if (n->cq[i] != NULL) {
- nvme_free_cq(n->cq[i], n);
- }
- }
- while (!QTAILQ_EMPTY(&n->aer_queue)) {
- NvmeAsyncEvent *event = QTAILQ_FIRST(&n->aer_queue);
- QTAILQ_REMOVE(&n->aer_queue, event, entry);
- g_free(event);
- }
- if (n->params.sriov_max_vfs) {
- if (!pci_is_vf(pci_dev)) {
- for (i = 0; i < n->nr_sec_ctrls; i++) {
- sctrl = &n->sec_ctrl_list[i];
- nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
- }
- }
- if (rst != NVME_RESET_CONTROLLER) {
- nvme_activate_virt_res(n);
- }
- }
- n->aer_queued = 0;
- n->aer_mask = 0;
- n->outstanding_aers = 0;
- n->qs_created = false;
- n->dn = n->params.atomic_dn; /* Set Disable Normal */
- nvme_update_msixcap_ts(pci_dev, n->conf_msix_qsize);
- if (pci_is_vf(pci_dev)) {
- sctrl = nvme_sctrl(n);
- stl_le_p(&n->bar.csts, sctrl->scs ? 0 : NVME_CSTS_FAILED);
- } else {
- stl_le_p(&n->bar.csts, 0);
- }
- stl_le_p(&n->bar.intms, 0);
- stl_le_p(&n->bar.intmc, 0);
- stl_le_p(&n->bar.cc, 0);
- n->dbbuf_dbs = 0;
- n->dbbuf_eis = 0;
- n->dbbuf_enabled = false;
- }
- static void nvme_ctrl_shutdown(NvmeCtrl *n)
- {
- NvmeNamespace *ns;
- int i;
- if (n->pmr.dev) {
- memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
- }
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (!ns) {
- continue;
- }
- nvme_ns_shutdown(ns);
- }
- }
- static int nvme_start_ctrl(NvmeCtrl *n)
- {
- uint64_t cap = ldq_le_p(&n->bar.cap);
- uint32_t cc = ldl_le_p(&n->bar.cc);
- uint32_t aqa = ldl_le_p(&n->bar.aqa);
- uint64_t asq = ldq_le_p(&n->bar.asq);
- uint64_t acq = ldq_le_p(&n->bar.acq);
- uint32_t page_bits = NVME_CC_MPS(cc) + 12;
- uint32_t page_size = 1 << page_bits;
- NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
- if (pci_is_vf(PCI_DEVICE(n)) && !sctrl->scs) {
- trace_pci_nvme_err_startfail_virt_state(le16_to_cpu(sctrl->nvi),
- le16_to_cpu(sctrl->nvq));
- return -1;
- }
- if (unlikely(n->cq[0])) {
- trace_pci_nvme_err_startfail_cq();
- return -1;
- }
- if (unlikely(n->sq[0])) {
- trace_pci_nvme_err_startfail_sq();
- return -1;
- }
- if (unlikely(asq & (page_size - 1))) {
- trace_pci_nvme_err_startfail_asq_misaligned(asq);
- return -1;
- }
- if (unlikely(acq & (page_size - 1))) {
- trace_pci_nvme_err_startfail_acq_misaligned(acq);
- return -1;
- }
- if (unlikely(!(NVME_CAP_CSS(cap) & (1 << NVME_CC_CSS(cc))))) {
- trace_pci_nvme_err_startfail_css(NVME_CC_CSS(cc));
- return -1;
- }
- if (unlikely(NVME_CC_MPS(cc) < NVME_CAP_MPSMIN(cap))) {
- trace_pci_nvme_err_startfail_page_too_small(
- NVME_CC_MPS(cc),
- NVME_CAP_MPSMIN(cap));
- return -1;
- }
- if (unlikely(NVME_CC_MPS(cc) >
- NVME_CAP_MPSMAX(cap))) {
- trace_pci_nvme_err_startfail_page_too_large(
- NVME_CC_MPS(cc),
- NVME_CAP_MPSMAX(cap));
- return -1;
- }
- if (unlikely(!NVME_AQA_ASQS(aqa))) {
- trace_pci_nvme_err_startfail_asqent_sz_zero();
- return -1;
- }
- if (unlikely(!NVME_AQA_ACQS(aqa))) {
- trace_pci_nvme_err_startfail_acqent_sz_zero();
- return -1;
- }
- n->page_bits = page_bits;
- n->page_size = page_size;
- n->max_prp_ents = n->page_size / sizeof(uint64_t);
- nvme_init_cq(&n->admin_cq, n, acq, 0, 0, NVME_AQA_ACQS(aqa) + 1, 1);
- nvme_init_sq(&n->admin_sq, n, asq, 0, 0, NVME_AQA_ASQS(aqa) + 1);
- nvme_set_timestamp(n, 0ULL);
- /* verify that the command sets of attached namespaces are supported */
- for (int i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- NvmeNamespace *ns = nvme_subsys_ns(n->subsys, i);
- if (!ns || (!ns->params.shared && ns->ctrl != n)) {
- continue;
- }
- if (nvme_csi_supported(n, ns->csi) && !ns->params.detached) {
- if (!ns->attached || ns->params.shared) {
- nvme_attach_ns(n, ns);
- }
- }
- }
- nvme_update_dsm_limits(n, NULL);
- return 0;
- }
- static void nvme_cmb_enable_regs(NvmeCtrl *n)
- {
- uint32_t cmbloc = ldl_le_p(&n->bar.cmbloc);
- uint32_t cmbsz = ldl_le_p(&n->bar.cmbsz);
- NVME_CMBLOC_SET_CDPCILS(cmbloc, 1);
- NVME_CMBLOC_SET_CDPMLS(cmbloc, 1);
- NVME_CMBLOC_SET_BIR(cmbloc, NVME_CMB_BIR);
- stl_le_p(&n->bar.cmbloc, cmbloc);
- NVME_CMBSZ_SET_SQS(cmbsz, 1);
- NVME_CMBSZ_SET_CQS(cmbsz, 0);
- NVME_CMBSZ_SET_LISTS(cmbsz, 1);
- NVME_CMBSZ_SET_RDS(cmbsz, 1);
- NVME_CMBSZ_SET_WDS(cmbsz, 1);
- NVME_CMBSZ_SET_SZU(cmbsz, 2); /* MBs */
- NVME_CMBSZ_SET_SZ(cmbsz, n->params.cmb_size_mb);
- stl_le_p(&n->bar.cmbsz, cmbsz);
- }
- static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
- unsigned size)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- uint64_t cap = ldq_le_p(&n->bar.cap);
- uint32_t cc = ldl_le_p(&n->bar.cc);
- uint32_t intms = ldl_le_p(&n->bar.intms);
- uint32_t csts = ldl_le_p(&n->bar.csts);
- uint32_t pmrsts = ldl_le_p(&n->bar.pmrsts);
- if (unlikely(offset & (sizeof(uint32_t) - 1))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_misaligned32,
- "MMIO write not 32-bit aligned,"
- " offset=0x%"PRIx64"", offset);
- /* should be ignored, fall through for now */
- }
- if (unlikely(size < sizeof(uint32_t))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_toosmall,
- "MMIO write smaller than 32-bits,"
- " offset=0x%"PRIx64", size=%u",
- offset, size);
- /* should be ignored, fall through for now */
- }
- switch (offset) {
- case NVME_REG_INTMS:
- if (unlikely(msix_enabled(pci))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
- "undefined access to interrupt mask set"
- " when MSI-X is enabled");
- /* should be ignored, fall through for now */
- }
- intms |= data;
- stl_le_p(&n->bar.intms, intms);
- n->bar.intmc = n->bar.intms;
- trace_pci_nvme_mmio_intm_set(data & 0xffffffff, intms);
- nvme_irq_check(n);
- break;
- case NVME_REG_INTMC:
- if (unlikely(msix_enabled(pci))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_intmask_with_msix,
- "undefined access to interrupt mask clr"
- " when MSI-X is enabled");
- /* should be ignored, fall through for now */
- }
- intms &= ~data;
- stl_le_p(&n->bar.intms, intms);
- n->bar.intmc = n->bar.intms;
- trace_pci_nvme_mmio_intm_clr(data & 0xffffffff, intms);
- nvme_irq_check(n);
- break;
- case NVME_REG_CC:
- stl_le_p(&n->bar.cc, data);
- trace_pci_nvme_mmio_cfg(data & 0xffffffff);
- if (NVME_CC_SHN(data) && !(NVME_CC_SHN(cc))) {
- trace_pci_nvme_mmio_shutdown_set();
- nvme_ctrl_shutdown(n);
- csts &= ~(CSTS_SHST_MASK << CSTS_SHST_SHIFT);
- csts |= NVME_CSTS_SHST_COMPLETE;
- } else if (!NVME_CC_SHN(data) && NVME_CC_SHN(cc)) {
- trace_pci_nvme_mmio_shutdown_cleared();
- csts &= ~(CSTS_SHST_MASK << CSTS_SHST_SHIFT);
- }
- if (NVME_CC_EN(data) && !NVME_CC_EN(cc)) {
- if (unlikely(nvme_start_ctrl(n))) {
- trace_pci_nvme_err_startfail();
- csts = NVME_CSTS_FAILED;
- } else {
- trace_pci_nvme_mmio_start_success();
- csts = NVME_CSTS_READY;
- }
- } else if (!NVME_CC_EN(data) && NVME_CC_EN(cc)) {
- trace_pci_nvme_mmio_stopped();
- nvme_ctrl_reset(n, NVME_RESET_CONTROLLER);
- break;
- }
- stl_le_p(&n->bar.csts, csts);
- break;
- case NVME_REG_CSTS:
- if (data & (1 << 4)) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ssreset_w1c_unsupported,
- "attempted to W1C CSTS.NSSRO"
- " but CAP.NSSRS is zero (not supported)");
- } else if (data != 0) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_ro_csts,
- "attempted to set a read only bit"
- " of controller status");
- }
- break;
- case NVME_REG_NSSR:
- if (data == 0x4e564d65) {
- trace_pci_nvme_ub_mmiowr_ssreset_unsupported();
- } else {
- /* The spec says that writes of other values have no effect */
- return;
- }
- break;
- case NVME_REG_AQA:
- stl_le_p(&n->bar.aqa, data);
- trace_pci_nvme_mmio_aqattr(data & 0xffffffff);
- break;
- case NVME_REG_ASQ:
- stn_le_p(&n->bar.asq, size, data);
- trace_pci_nvme_mmio_asqaddr(data);
- break;
- case NVME_REG_ASQ + 4:
- stl_le_p((uint8_t *)&n->bar.asq + 4, data);
- trace_pci_nvme_mmio_asqaddr_hi(data, ldq_le_p(&n->bar.asq));
- break;
- case NVME_REG_ACQ:
- trace_pci_nvme_mmio_acqaddr(data);
- stn_le_p(&n->bar.acq, size, data);
- break;
- case NVME_REG_ACQ + 4:
- stl_le_p((uint8_t *)&n->bar.acq + 4, data);
- trace_pci_nvme_mmio_acqaddr_hi(data, ldq_le_p(&n->bar.acq));
- break;
- case NVME_REG_CMBLOC:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbloc_reserved,
- "invalid write to reserved CMBLOC"
- " when CMBSZ is zero, ignored");
- return;
- case NVME_REG_CMBSZ:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_cmbsz_readonly,
- "invalid write to read only CMBSZ, ignored");
- return;
- case NVME_REG_CMBMSC:
- if (!NVME_CAP_CMBS(cap)) {
- return;
- }
- stn_le_p(&n->bar.cmbmsc, size, data);
- n->cmb.cmse = false;
- if (NVME_CMBMSC_CRE(data)) {
- nvme_cmb_enable_regs(n);
- if (NVME_CMBMSC_CMSE(data)) {
- uint64_t cmbmsc = ldq_le_p(&n->bar.cmbmsc);
- hwaddr cba = NVME_CMBMSC_CBA(cmbmsc) << CMBMSC_CBA_SHIFT;
- if (cba + int128_get64(n->cmb.mem.size) < cba) {
- uint32_t cmbsts = ldl_le_p(&n->bar.cmbsts);
- NVME_CMBSTS_SET_CBAI(cmbsts, 1);
- stl_le_p(&n->bar.cmbsts, cmbsts);
- return;
- }
- n->cmb.cba = cba;
- n->cmb.cmse = true;
- }
- } else {
- n->bar.cmbsz = 0;
- n->bar.cmbloc = 0;
- }
- return;
- case NVME_REG_CMBMSC + 4:
- stl_le_p((uint8_t *)&n->bar.cmbmsc + 4, data);
- return;
- case NVME_REG_PMRCAP:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrcap_readonly,
- "invalid write to PMRCAP register, ignored");
- return;
- case NVME_REG_PMRCTL:
- if (!NVME_CAP_PMRS(cap)) {
- return;
- }
- stl_le_p(&n->bar.pmrctl, data);
- if (NVME_PMRCTL_EN(data)) {
- memory_region_set_enabled(&n->pmr.dev->mr, true);
- pmrsts = 0;
- } else {
- memory_region_set_enabled(&n->pmr.dev->mr, false);
- NVME_PMRSTS_SET_NRDY(pmrsts, 1);
- n->pmr.cmse = false;
- }
- stl_le_p(&n->bar.pmrsts, pmrsts);
- return;
- case NVME_REG_PMRSTS:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrsts_readonly,
- "invalid write to PMRSTS register, ignored");
- return;
- case NVME_REG_PMREBS:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrebs_readonly,
- "invalid write to PMREBS register, ignored");
- return;
- case NVME_REG_PMRSWTP:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_pmrswtp_readonly,
- "invalid write to PMRSWTP register, ignored");
- return;
- case NVME_REG_PMRMSCL:
- if (!NVME_CAP_PMRS(cap)) {
- return;
- }
- stl_le_p(&n->bar.pmrmscl, data);
- n->pmr.cmse = false;
- if (NVME_PMRMSCL_CMSE(data)) {
- uint64_t pmrmscu = ldl_le_p(&n->bar.pmrmscu);
- hwaddr cba = pmrmscu << 32 |
- (NVME_PMRMSCL_CBA(data) << PMRMSCL_CBA_SHIFT);
- if (cba + int128_get64(n->pmr.dev->mr.size) < cba) {
- NVME_PMRSTS_SET_CBAI(pmrsts, 1);
- stl_le_p(&n->bar.pmrsts, pmrsts);
- return;
- }
- n->pmr.cmse = true;
- n->pmr.cba = cba;
- }
- return;
- case NVME_REG_PMRMSCU:
- if (!NVME_CAP_PMRS(cap)) {
- return;
- }
- stl_le_p(&n->bar.pmrmscu, data);
- return;
- default:
- NVME_GUEST_ERR(pci_nvme_ub_mmiowr_invalid,
- "invalid MMIO write,"
- " offset=0x%"PRIx64", data=%"PRIx64"",
- offset, data);
- break;
- }
- }
- static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
- {
- NvmeCtrl *n = (NvmeCtrl *)opaque;
- uint8_t *ptr = (uint8_t *)&n->bar;
- trace_pci_nvme_mmio_read(addr, size);
- if (unlikely(addr & (sizeof(uint32_t) - 1))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiord_misaligned32,
- "MMIO read not 32-bit aligned,"
- " offset=0x%"PRIx64"", addr);
- /* should RAZ, fall through for now */
- } else if (unlikely(size < sizeof(uint32_t))) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiord_toosmall,
- "MMIO read smaller than 32-bits,"
- " offset=0x%"PRIx64"", addr);
- /* should RAZ, fall through for now */
- }
- if (addr > sizeof(n->bar) - size) {
- NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs,
- "MMIO read beyond last register,"
- " offset=0x%"PRIx64", returning 0", addr);
- return 0;
- }
- if (pci_is_vf(PCI_DEVICE(n)) && !nvme_sctrl(n)->scs &&
- addr != NVME_REG_CSTS) {
- trace_pci_nvme_err_ignored_mmio_vf_offline(addr, size);
- return 0;
- }
- /*
- * When PMRWBM bit 1 is set then read from
- * from PMRSTS should ensure prior writes
- * made it to persistent media
- */
- if (addr == NVME_REG_PMRSTS &&
- (NVME_PMRCAP_PMRWBM(ldl_le_p(&n->bar.pmrcap)) & 0x02)) {
- memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
- }
- return ldn_le_p(ptr + addr, size);
- }
- static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
- {
- PCIDevice *pci = PCI_DEVICE(n);
- uint32_t qid;
- if (unlikely(addr & ((1 << 2) - 1))) {
- NVME_GUEST_ERR(pci_nvme_ub_db_wr_misaligned,
- "doorbell write not 32-bit aligned,"
- " offset=0x%"PRIx64", ignoring", addr);
- return;
- }
- if (((addr - 0x1000) >> 2) & 1) {
- /* Completion queue doorbell write */
- uint16_t new_head = val & 0xffff;
- NvmeCQueue *cq;
- qid = (addr - (0x1000 + (1 << 2))) >> 3;
- if (unlikely(nvme_check_cqid(n, qid))) {
- NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cq,
- "completion queue doorbell write"
- " for nonexistent queue,"
- " sqid=%"PRIu32", ignoring", qid);
- /*
- * NVM Express v1.3d, Section 4.1 state: "If host software writes
- * an invalid value to the Submission Queue Tail Doorbell or
- * Completion Queue Head Doorbell register and an Asynchronous Event
- * Request command is outstanding, then an asynchronous event is
- * posted to the Admin Completion Queue with a status code of
- * Invalid Doorbell Write Value."
- *
- * Also note that the spec includes the "Invalid Doorbell Register"
- * status code, but nowhere does it specify when to use it.
- * However, it seems reasonable to use it here in a similar
- * fashion.
- */
- if (n->outstanding_aers) {
- nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
- NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
- NVME_LOG_ERROR_INFO);
- }
- return;
- }
- cq = n->cq[qid];
- if (unlikely(new_head >= cq->size)) {
- NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_cqhead,
- "completion queue doorbell write value"
- " beyond queue size, sqid=%"PRIu32","
- " new_head=%"PRIu16", ignoring",
- qid, new_head);
- if (n->outstanding_aers) {
- nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
- NVME_AER_INFO_ERR_INVALID_DB_VALUE,
- NVME_LOG_ERROR_INFO);
- }
- return;
- }
- trace_pci_nvme_mmio_doorbell_cq(cq->cqid, new_head);
- /* scheduled deferred cqe posting if queue was previously full */
- if (nvme_cq_full(cq)) {
- qemu_bh_schedule(cq->bh);
- }
- cq->head = new_head;
- if (!qid && n->dbbuf_enabled) {
- stl_le_pci_dma(pci, cq->db_addr, cq->head, MEMTXATTRS_UNSPECIFIED);
- }
- if (cq->tail == cq->head) {
- if (cq->irq_enabled) {
- n->cq_pending--;
- }
- nvme_irq_deassert(n, cq);
- }
- } else {
- /* Submission queue doorbell write */
- uint16_t new_tail = val & 0xffff;
- NvmeSQueue *sq;
- qid = (addr - 0x1000) >> 3;
- if (unlikely(nvme_check_sqid(n, qid))) {
- NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sq,
- "submission queue doorbell write"
- " for nonexistent queue,"
- " sqid=%"PRIu32", ignoring", qid);
- if (n->outstanding_aers) {
- nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
- NVME_AER_INFO_ERR_INVALID_DB_REGISTER,
- NVME_LOG_ERROR_INFO);
- }
- return;
- }
- sq = n->sq[qid];
- if (unlikely(new_tail >= sq->size)) {
- NVME_GUEST_ERR(pci_nvme_ub_db_wr_invalid_sqtail,
- "submission queue doorbell write value"
- " beyond queue size, sqid=%"PRIu32","
- " new_tail=%"PRIu16", ignoring",
- qid, new_tail);
- if (n->outstanding_aers) {
- nvme_enqueue_event(n, NVME_AER_TYPE_ERROR,
- NVME_AER_INFO_ERR_INVALID_DB_VALUE,
- NVME_LOG_ERROR_INFO);
- }
- return;
- }
- trace_pci_nvme_mmio_doorbell_sq(sq->sqid, new_tail);
- sq->tail = new_tail;
- if (!qid && n->dbbuf_enabled) {
- /*
- * The spec states "the host shall also update the controller's
- * corresponding doorbell property to match the value of that entry
- * in the Shadow Doorbell buffer."
- *
- * Since this context is currently a VM trap, we can safely enforce
- * the requirement from the device side in case the host is
- * misbehaving.
- *
- * Note, we shouldn't have to do this, but various drivers
- * including ones that run on Linux, are not updating Admin Queues,
- * so we can't trust reading it for an appropriate sq tail.
- */
- stl_le_pci_dma(pci, sq->db_addr, sq->tail, MEMTXATTRS_UNSPECIFIED);
- }
- qemu_bh_schedule(sq->bh);
- }
- }
- static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
- unsigned size)
- {
- NvmeCtrl *n = (NvmeCtrl *)opaque;
- trace_pci_nvme_mmio_write(addr, data, size);
- if (pci_is_vf(PCI_DEVICE(n)) && !nvme_sctrl(n)->scs &&
- addr != NVME_REG_CSTS) {
- trace_pci_nvme_err_ignored_mmio_vf_offline(addr, size);
- return;
- }
- if (addr < sizeof(n->bar)) {
- nvme_write_bar(n, addr, data, size);
- } else {
- nvme_process_db(n, addr, data);
- }
- }
- static const MemoryRegionOps nvme_mmio_ops = {
- .read = nvme_mmio_read,
- .write = nvme_mmio_write,
- .endianness = DEVICE_LITTLE_ENDIAN,
- .impl = {
- .min_access_size = 2,
- .max_access_size = 8,
- },
- };
- static void nvme_cmb_write(void *opaque, hwaddr addr, uint64_t data,
- unsigned size)
- {
- NvmeCtrl *n = (NvmeCtrl *)opaque;
- stn_le_p(&n->cmb.buf[addr], size, data);
- }
- static uint64_t nvme_cmb_read(void *opaque, hwaddr addr, unsigned size)
- {
- NvmeCtrl *n = (NvmeCtrl *)opaque;
- return ldn_le_p(&n->cmb.buf[addr], size);
- }
- static const MemoryRegionOps nvme_cmb_ops = {
- .read = nvme_cmb_read,
- .write = nvme_cmb_write,
- .endianness = DEVICE_LITTLE_ENDIAN,
- .impl = {
- .min_access_size = 1,
- .max_access_size = 8,
- },
- };
- static bool nvme_check_params(NvmeCtrl *n, Error **errp)
- {
- NvmeParams *params = &n->params;
- if (params->num_queues) {
- warn_report("num_queues is deprecated; please use max_ioqpairs "
- "instead");
- params->max_ioqpairs = params->num_queues - 1;
- }
- if (n->namespace.blkconf.blk && n->subsys) {
- error_setg(errp, "subsystem support is unavailable with legacy "
- "namespace ('drive' property)");
- return false;
- }
- if (params->max_ioqpairs < 1 ||
- params->max_ioqpairs > NVME_MAX_IOQPAIRS) {
- error_setg(errp, "max_ioqpairs must be between 1 and %d",
- NVME_MAX_IOQPAIRS);
- return false;
- }
- if (params->msix_qsize < 1 ||
- params->msix_qsize > PCI_MSIX_FLAGS_QSIZE + 1) {
- error_setg(errp, "msix_qsize must be between 1 and %d",
- PCI_MSIX_FLAGS_QSIZE + 1);
- return false;
- }
- if (!params->serial) {
- error_setg(errp, "serial property not set");
- return false;
- }
- if (params->mqes < 1) {
- error_setg(errp, "mqes property cannot be less than 1");
- return false;
- }
- if (n->pmr.dev) {
- if (params->msix_exclusive_bar) {
- error_setg(errp, "not enough BARs available to enable PMR");
- return false;
- }
- if (host_memory_backend_is_mapped(n->pmr.dev)) {
- error_setg(errp, "can't use already busy memdev: %s",
- object_get_canonical_path_component(OBJECT(n->pmr.dev)));
- return false;
- }
- if (!is_power_of_2(n->pmr.dev->size)) {
- error_setg(errp, "pmr backend size needs to be power of 2 in size");
- return false;
- }
- host_memory_backend_set_mapped(n->pmr.dev, true);
- }
- if (n->params.zasl > n->params.mdts) {
- error_setg(errp, "zoned.zasl (Zone Append Size Limit) must be less "
- "than or equal to mdts (Maximum Data Transfer Size)");
- return false;
- }
- if (!n->params.vsl) {
- error_setg(errp, "vsl must be non-zero");
- return false;
- }
- if (params->sriov_max_vfs) {
- if (!n->subsys) {
- error_setg(errp, "subsystem is required for the use of SR-IOV");
- return false;
- }
- if (params->cmb_size_mb) {
- error_setg(errp, "CMB is not supported with SR-IOV");
- return false;
- }
- if (n->pmr.dev) {
- error_setg(errp, "PMR is not supported with SR-IOV");
- return false;
- }
- if (!params->sriov_vq_flexible || !params->sriov_vi_flexible) {
- error_setg(errp, "both sriov_vq_flexible and sriov_vi_flexible"
- " must be set for the use of SR-IOV");
- return false;
- }
- if (params->sriov_vq_flexible < params->sriov_max_vfs * 2) {
- error_setg(errp, "sriov_vq_flexible must be greater than or equal"
- " to %d (sriov_max_vfs * 2)", params->sriov_max_vfs * 2);
- return false;
- }
- if (params->max_ioqpairs < params->sriov_vq_flexible + 2) {
- error_setg(errp, "(max_ioqpairs - sriov_vq_flexible) must be"
- " greater than or equal to 2");
- return false;
- }
- if (params->sriov_vi_flexible < params->sriov_max_vfs) {
- error_setg(errp, "sriov_vi_flexible must be greater than or equal"
- " to %d (sriov_max_vfs)", params->sriov_max_vfs);
- return false;
- }
- if (params->msix_qsize < params->sriov_vi_flexible + 1) {
- error_setg(errp, "(msix_qsize - sriov_vi_flexible) must be"
- " greater than or equal to 1");
- return false;
- }
- if (params->sriov_max_vi_per_vf &&
- (params->sriov_max_vi_per_vf - 1) % NVME_VF_RES_GRANULARITY) {
- error_setg(errp, "sriov_max_vi_per_vf must meet:"
- " (sriov_max_vi_per_vf - 1) %% %d == 0 and"
- " sriov_max_vi_per_vf >= 1", NVME_VF_RES_GRANULARITY);
- return false;
- }
- if (params->sriov_max_vq_per_vf &&
- (params->sriov_max_vq_per_vf < 2 ||
- (params->sriov_max_vq_per_vf - 1) % NVME_VF_RES_GRANULARITY)) {
- error_setg(errp, "sriov_max_vq_per_vf must meet:"
- " (sriov_max_vq_per_vf - 1) %% %d == 0 and"
- " sriov_max_vq_per_vf >= 2", NVME_VF_RES_GRANULARITY);
- return false;
- }
- }
- return true;
- }
- static void nvme_init_state(NvmeCtrl *n)
- {
- NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
- NvmeSecCtrlEntry *list = n->sec_ctrl_list;
- NvmeSecCtrlEntry *sctrl;
- PCIDevice *pci = PCI_DEVICE(n);
- NvmeAtomic *atomic = &n->atomic;
- NvmeIdCtrl *id = &n->id_ctrl;
- uint8_t max_vfs;
- int i;
- if (pci_is_vf(pci)) {
- sctrl = nvme_sctrl(n);
- max_vfs = 0;
- n->conf_ioqpairs = sctrl->nvq ? le16_to_cpu(sctrl->nvq) - 1 : 0;
- n->conf_msix_qsize = sctrl->nvi ? le16_to_cpu(sctrl->nvi) : 1;
- } else {
- max_vfs = n->params.sriov_max_vfs;
- n->conf_ioqpairs = n->params.max_ioqpairs;
- n->conf_msix_qsize = n->params.msix_qsize;
- }
- n->sq = g_new0(NvmeSQueue *, n->params.max_ioqpairs + 1);
- n->cq = g_new0(NvmeCQueue *, n->params.max_ioqpairs + 1);
- n->temperature = NVME_TEMPERATURE;
- n->features.temp_thresh_hi = NVME_TEMPERATURE_WARNING;
- n->starttime_ms = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
- n->aer_reqs = g_new0(NvmeRequest *, n->params.aerl + 1);
- QTAILQ_INIT(&n->aer_queue);
- n->nr_sec_ctrls = max_vfs;
- for (i = 0; i < max_vfs; i++) {
- sctrl = &list[i];
- sctrl->pcid = cpu_to_le16(n->cntlid);
- sctrl->vfn = cpu_to_le16(i + 1);
- }
- cap->cntlid = cpu_to_le16(n->cntlid);
- cap->crt = NVME_CRT_VQ | NVME_CRT_VI;
- if (pci_is_vf(pci)) {
- cap->vqprt = cpu_to_le16(1 + n->conf_ioqpairs);
- } else {
- cap->vqprt = cpu_to_le16(1 + n->params.max_ioqpairs -
- n->params.sriov_vq_flexible);
- cap->vqfrt = cpu_to_le32(n->params.sriov_vq_flexible);
- cap->vqrfap = cap->vqfrt;
- cap->vqgran = cpu_to_le16(NVME_VF_RES_GRANULARITY);
- cap->vqfrsm = n->params.sriov_max_vq_per_vf ?
- cpu_to_le16(n->params.sriov_max_vq_per_vf) :
- cap->vqfrt / MAX(max_vfs, 1);
- }
- if (pci_is_vf(pci)) {
- cap->viprt = cpu_to_le16(n->conf_msix_qsize);
- } else {
- cap->viprt = cpu_to_le16(n->params.msix_qsize -
- n->params.sriov_vi_flexible);
- cap->vifrt = cpu_to_le32(n->params.sriov_vi_flexible);
- cap->virfap = cap->vifrt;
- cap->vigran = cpu_to_le16(NVME_VF_RES_GRANULARITY);
- cap->vifrsm = n->params.sriov_max_vi_per_vf ?
- cpu_to_le16(n->params.sriov_max_vi_per_vf) :
- cap->vifrt / MAX(max_vfs, 1);
- }
- /* Atomic Write */
- id->awun = cpu_to_le16(n->params.atomic_awun);
- id->awupf = cpu_to_le16(n->params.atomic_awupf);
- n->dn = n->params.atomic_dn;
- if (id->awun || id->awupf) {
- if (id->awupf > id->awun) {
- id->awupf = 0;
- }
- if (n->dn) {
- atomic->atomic_max_write_size = id->awupf + 1;
- } else {
- atomic->atomic_max_write_size = id->awun + 1;
- }
- if (atomic->atomic_max_write_size == 1) {
- atomic->atomic_writes = 0;
- } else {
- atomic->atomic_writes = 1;
- }
- }
- }
- static void nvme_init_cmb(NvmeCtrl *n, PCIDevice *pci_dev)
- {
- uint64_t cmb_size = n->params.cmb_size_mb * MiB;
- uint64_t cap = ldq_le_p(&n->bar.cap);
- n->cmb.buf = g_malloc0(cmb_size);
- memory_region_init_io(&n->cmb.mem, OBJECT(n), &nvme_cmb_ops, n,
- "nvme-cmb", cmb_size);
- pci_register_bar(pci_dev, NVME_CMB_BIR,
- PCI_BASE_ADDRESS_SPACE_MEMORY |
- PCI_BASE_ADDRESS_MEM_TYPE_64 |
- PCI_BASE_ADDRESS_MEM_PREFETCH, &n->cmb.mem);
- NVME_CAP_SET_CMBS(cap, 1);
- stq_le_p(&n->bar.cap, cap);
- if (n->params.legacy_cmb) {
- nvme_cmb_enable_regs(n);
- n->cmb.cmse = true;
- }
- }
- static void nvme_init_pmr(NvmeCtrl *n, PCIDevice *pci_dev)
- {
- uint32_t pmrcap = ldl_le_p(&n->bar.pmrcap);
- NVME_PMRCAP_SET_RDS(pmrcap, 1);
- NVME_PMRCAP_SET_WDS(pmrcap, 1);
- NVME_PMRCAP_SET_BIR(pmrcap, NVME_PMR_BIR);
- /* Turn on bit 1 support */
- NVME_PMRCAP_SET_PMRWBM(pmrcap, 0x02);
- NVME_PMRCAP_SET_CMSS(pmrcap, 1);
- stl_le_p(&n->bar.pmrcap, pmrcap);
- pci_register_bar(pci_dev, NVME_PMR_BIR,
- PCI_BASE_ADDRESS_SPACE_MEMORY |
- PCI_BASE_ADDRESS_MEM_TYPE_64 |
- PCI_BASE_ADDRESS_MEM_PREFETCH, &n->pmr.dev->mr);
- memory_region_set_enabled(&n->pmr.dev->mr, false);
- }
- static uint64_t nvme_mbar_size(unsigned total_queues, unsigned total_irqs,
- unsigned *msix_table_offset,
- unsigned *msix_pba_offset)
- {
- uint64_t bar_size, msix_table_size;
- bar_size = sizeof(NvmeBar) + 2 * total_queues * NVME_DB_SIZE;
- if (total_irqs == 0) {
- goto out;
- }
- bar_size = QEMU_ALIGN_UP(bar_size, 4 * KiB);
- if (msix_table_offset) {
- *msix_table_offset = bar_size;
- }
- msix_table_size = PCI_MSIX_ENTRY_SIZE * total_irqs;
- bar_size += msix_table_size;
- bar_size = QEMU_ALIGN_UP(bar_size, 4 * KiB);
- if (msix_pba_offset) {
- *msix_pba_offset = bar_size;
- }
- bar_size += QEMU_ALIGN_UP(total_irqs, 64) / 8;
- out:
- return pow2ceil(bar_size);
- }
- static bool nvme_init_sriov(NvmeCtrl *n, PCIDevice *pci_dev, uint16_t offset,
- Error **errp)
- {
- uint16_t vf_dev_id = n->params.use_intel_id ?
- PCI_DEVICE_ID_INTEL_NVME : PCI_DEVICE_ID_REDHAT_NVME;
- NvmePriCtrlCap *cap = &n->pri_ctrl_cap;
- uint64_t bar_size = nvme_mbar_size(le16_to_cpu(cap->vqfrsm),
- le16_to_cpu(cap->vifrsm),
- NULL, NULL);
- if (!pcie_sriov_pf_init(pci_dev, offset, "nvme", vf_dev_id,
- n->params.sriov_max_vfs, n->params.sriov_max_vfs,
- NVME_VF_OFFSET, NVME_VF_STRIDE, errp)) {
- return false;
- }
- pcie_sriov_pf_init_vf_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
- PCI_BASE_ADDRESS_MEM_TYPE_64, bar_size);
- return true;
- }
- static int nvme_add_pm_capability(PCIDevice *pci_dev, uint8_t offset)
- {
- Error *err = NULL;
- int ret;
- ret = pci_pm_init(pci_dev, offset, &err);
- if (err) {
- error_report_err(err);
- return ret;
- }
- pci_set_word(pci_dev->config + offset + PCI_PM_PMC,
- PCI_PM_CAP_VER_1_2);
- pci_set_word(pci_dev->config + offset + PCI_PM_CTRL,
- PCI_PM_CTRL_NO_SOFT_RESET);
- pci_set_word(pci_dev->wmask + offset + PCI_PM_CTRL,
- PCI_PM_CTRL_STATE_MASK);
- return 0;
- }
- static bool pcie_doe_spdm_rsp(DOECap *doe_cap)
- {
- void *req = pcie_doe_get_write_mbox_ptr(doe_cap);
- uint32_t req_len = pcie_doe_get_obj_len(req) * 4;
- void *rsp = doe_cap->read_mbox;
- uint32_t rsp_len = SPDM_SOCKET_MAX_MESSAGE_BUFFER_SIZE;
- uint32_t recvd = spdm_socket_rsp(doe_cap->spdm_socket,
- SPDM_SOCKET_TRANSPORT_TYPE_PCI_DOE,
- req, req_len, rsp, rsp_len);
- doe_cap->read_mbox_len += DIV_ROUND_UP(recvd, 4);
- return recvd != 0;
- }
- static DOEProtocol doe_spdm_prot[] = {
- { PCI_VENDOR_ID_PCI_SIG, PCI_SIG_DOE_CMA, pcie_doe_spdm_rsp },
- { PCI_VENDOR_ID_PCI_SIG, PCI_SIG_DOE_SECURED_CMA, pcie_doe_spdm_rsp },
- { }
- };
- static bool nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)
- {
- ERRP_GUARD();
- uint8_t *pci_conf = pci_dev->config;
- uint64_t bar_size;
- unsigned msix_table_offset = 0, msix_pba_offset = 0;
- unsigned nr_vectors;
- int ret;
- pci_conf[PCI_INTERRUPT_PIN] = pci_is_vf(pci_dev) ? 0 : 1;
- pci_config_set_prog_interface(pci_conf, 0x2);
- if (n->params.use_intel_id) {
- pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
- pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_NVME);
- } else {
- pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REDHAT);
- pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REDHAT_NVME);
- }
- pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_EXPRESS);
- nvme_add_pm_capability(pci_dev, 0x60);
- pcie_endpoint_cap_init(pci_dev, 0x80);
- pcie_cap_flr_init(pci_dev);
- if (n->params.sriov_max_vfs) {
- pcie_ari_init(pci_dev, 0x100);
- }
- if (n->params.msix_exclusive_bar && !pci_is_vf(pci_dev)) {
- bar_size = nvme_mbar_size(n->params.max_ioqpairs + 1, 0, NULL, NULL);
- memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n, "nvme",
- bar_size);
- pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
- PCI_BASE_ADDRESS_MEM_TYPE_64, &n->iomem);
- ret = msix_init_exclusive_bar(pci_dev, n->params.msix_qsize, 4, errp);
- } else {
- assert(n->params.msix_qsize >= 1);
- /* add one to max_ioqpairs to account for the admin queue pair */
- if (!pci_is_vf(pci_dev)) {
- nr_vectors = n->params.msix_qsize;
- bar_size = nvme_mbar_size(n->params.max_ioqpairs + 1,
- nr_vectors, &msix_table_offset,
- &msix_pba_offset);
- } else {
- NvmeCtrl *pn = NVME(pcie_sriov_get_pf(pci_dev));
- NvmePriCtrlCap *cap = &pn->pri_ctrl_cap;
- nr_vectors = le16_to_cpu(cap->vifrsm);
- bar_size = nvme_mbar_size(le16_to_cpu(cap->vqfrsm), nr_vectors,
- &msix_table_offset, &msix_pba_offset);
- }
- memory_region_init(&n->bar0, OBJECT(n), "nvme-bar0", bar_size);
- memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n, "nvme",
- msix_table_offset);
- memory_region_add_subregion(&n->bar0, 0, &n->iomem);
- if (pci_is_vf(pci_dev)) {
- pcie_sriov_vf_register_bar(pci_dev, 0, &n->bar0);
- } else {
- pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY |
- PCI_BASE_ADDRESS_MEM_TYPE_64, &n->bar0);
- }
- ret = msix_init(pci_dev, nr_vectors,
- &n->bar0, 0, msix_table_offset,
- &n->bar0, 0, msix_pba_offset, 0, errp);
- }
- if (ret == -ENOTSUP) {
- /* report that msix is not supported, but do not error out */
- warn_report_err(*errp);
- *errp = NULL;
- } else if (ret < 0) {
- /* propagate error to caller */
- return false;
- }
- if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs &&
- !nvme_init_sriov(n, pci_dev, 0x120, errp)) {
- return false;
- }
- nvme_update_msixcap_ts(pci_dev, n->conf_msix_qsize);
- pcie_cap_deverr_init(pci_dev);
- /* DOE Initialisation */
- if (pci_dev->spdm_port) {
- uint16_t doe_offset = n->params.sriov_max_vfs ?
- PCI_CONFIG_SPACE_SIZE + PCI_ARI_SIZEOF
- : PCI_CONFIG_SPACE_SIZE;
- pcie_doe_init(pci_dev, &pci_dev->doe_spdm, doe_offset,
- doe_spdm_prot, true, 0);
- pci_dev->doe_spdm.spdm_socket = spdm_socket_connect(pci_dev->spdm_port,
- errp);
- if (pci_dev->doe_spdm.spdm_socket < 0) {
- return false;
- }
- }
- if (n->params.cmb_size_mb) {
- nvme_init_cmb(n, pci_dev);
- }
- if (n->pmr.dev) {
- nvme_init_pmr(n, pci_dev);
- }
- return true;
- }
- static void nvme_init_subnqn(NvmeCtrl *n)
- {
- NvmeSubsystem *subsys = n->subsys;
- NvmeIdCtrl *id = &n->id_ctrl;
- if (!subsys) {
- snprintf((char *)id->subnqn, sizeof(id->subnqn),
- "nqn.2019-08.org.qemu:%s", n->params.serial);
- } else {
- pstrcpy((char *)id->subnqn, sizeof(id->subnqn), (char*)subsys->subnqn);
- }
- }
- static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
- {
- NvmeIdCtrl *id = &n->id_ctrl;
- uint8_t *pci_conf = pci_dev->config;
- uint64_t cap = ldq_le_p(&n->bar.cap);
- NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
- uint32_t ctratt;
- uint16_t oacs;
- memcpy(n->cse.acs, nvme_cse_acs_default, sizeof(n->cse.acs));
- memcpy(n->cse.iocs.nvm, nvme_cse_iocs_nvm_default, sizeof(n->cse.iocs.nvm));
- memcpy(n->cse.iocs.zoned, nvme_cse_iocs_zoned_default,
- sizeof(n->cse.iocs.zoned));
- id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
- id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
- strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
- strpadcpy((char *)id->fr, sizeof(id->fr), QEMU_VERSION, ' ');
- strpadcpy((char *)id->sn, sizeof(id->sn), n->params.serial, ' ');
- id->cntlid = cpu_to_le16(n->cntlid);
- id->oaes = cpu_to_le32(NVME_OAES_NS_ATTR);
- ctratt = NVME_CTRATT_ELBAS;
- if (n->params.ctratt.mem) {
- ctratt |= NVME_CTRATT_MEM;
- }
- id->rab = 6;
- if (n->params.use_intel_id) {
- id->ieee[0] = 0xb3;
- id->ieee[1] = 0x02;
- id->ieee[2] = 0x00;
- } else {
- id->ieee[0] = 0x00;
- id->ieee[1] = 0x54;
- id->ieee[2] = 0x52;
- }
- id->mdts = n->params.mdts;
- id->ver = cpu_to_le32(NVME_SPEC_VER);
- oacs = NVME_OACS_NMS | NVME_OACS_FORMAT | NVME_OACS_DIRECTIVES;
- if (n->params.dbcs) {
- oacs |= NVME_OACS_DBCS;
- n->cse.acs[NVME_ADM_CMD_DBBUF_CONFIG] = NVME_CMD_EFF_CSUPP;
- }
- if (n->params.sriov_max_vfs) {
- oacs |= NVME_OACS_VMS;
- n->cse.acs[NVME_ADM_CMD_VIRT_MNGMT] = NVME_CMD_EFF_CSUPP;
- }
- id->oacs = cpu_to_le16(oacs);
- id->cntrltype = 0x1;
- /*
- * Because the controller always completes the Abort command immediately,
- * there can never be more than one concurrently executing Abort command,
- * so this value is never used for anything. Note that there can easily be
- * many Abort commands in the queues, but they are not considered
- * "executing" until processed by nvme_abort.
- *
- * The specification recommends a value of 3 for Abort Command Limit (four
- * concurrently outstanding Abort commands), so lets use that though it is
- * inconsequential.
- */
- id->acl = 3;
- id->aerl = n->params.aerl;
- id->frmw = (NVME_NUM_FW_SLOTS << 1) | NVME_FRMW_SLOT1_RO;
- id->lpa = NVME_LPA_NS_SMART | NVME_LPA_CSE | NVME_LPA_EXTENDED;
- /* recommended default value (~70 C) */
- id->wctemp = cpu_to_le16(NVME_TEMPERATURE_WARNING);
- id->cctemp = cpu_to_le16(NVME_TEMPERATURE_CRITICAL);
- id->sqes = (NVME_SQES << 4) | NVME_SQES;
- id->cqes = (NVME_CQES << 4) | NVME_CQES;
- id->nn = cpu_to_le32(NVME_MAX_NAMESPACES);
- id->oncs = cpu_to_le16(NVME_ONCS_WRITE_ZEROES | NVME_ONCS_TIMESTAMP |
- NVME_ONCS_FEATURES | NVME_ONCS_DSM |
- NVME_ONCS_COMPARE | NVME_ONCS_COPY |
- NVME_ONCS_NVMCSA | NVME_ONCS_NVMAFC);
- /*
- * NOTE: If this device ever supports a command set that does NOT use 0x0
- * as a Flush-equivalent operation, support for the broadcast NSID in Flush
- * should probably be removed.
- *
- * See comment in nvme_io_cmd.
- */
- id->vwc = NVME_VWC_NSID_BROADCAST_SUPPORT | NVME_VWC_PRESENT;
- id->ocfs = cpu_to_le16(NVME_OCFS_COPY_FORMAT_0 | NVME_OCFS_COPY_FORMAT_1 |
- NVME_OCFS_COPY_FORMAT_2 | NVME_OCFS_COPY_FORMAT_3);
- id->sgls = cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN |
- NVME_CTRL_SGLS_MPTR_SGL);
- nvme_init_subnqn(n);
- id->psd[0].mp = cpu_to_le16(0x9c4);
- id->psd[0].enlat = cpu_to_le32(0x10);
- id->psd[0].exlat = cpu_to_le32(0x4);
- id->cmic |= NVME_CMIC_MULTI_CTRL;
- ctratt |= NVME_CTRATT_ENDGRPS;
- id->endgidmax = cpu_to_le16(0x1);
- if (n->subsys->endgrp.fdp.enabled) {
- ctratt |= NVME_CTRATT_FDPS;
- }
- id->ctratt = cpu_to_le32(ctratt);
- NVME_CAP_SET_MQES(cap, n->params.mqes);
- NVME_CAP_SET_CQR(cap, 1);
- NVME_CAP_SET_TO(cap, 0xf);
- NVME_CAP_SET_CSS(cap, NVME_CAP_CSS_NCSS);
- NVME_CAP_SET_CSS(cap, NVME_CAP_CSS_IOCSS);
- NVME_CAP_SET_MPSMAX(cap, 4);
- NVME_CAP_SET_CMBS(cap, n->params.cmb_size_mb ? 1 : 0);
- NVME_CAP_SET_PMRS(cap, n->pmr.dev ? 1 : 0);
- stq_le_p(&n->bar.cap, cap);
- stl_le_p(&n->bar.vs, NVME_SPEC_VER);
- n->bar.intmc = n->bar.intms = 0;
- if (pci_is_vf(pci_dev) && !sctrl->scs) {
- stl_le_p(&n->bar.csts, NVME_CSTS_FAILED);
- }
- }
- static int nvme_init_subsys(NvmeCtrl *n, Error **errp)
- {
- int cntlid;
- if (!n->subsys) {
- DeviceState *dev = qdev_new(TYPE_NVME_SUBSYS);
- qdev_prop_set_string(dev, "nqn", n->params.serial);
- if (!qdev_realize(dev, NULL, errp)) {
- return -1;
- }
- n->subsys = NVME_SUBSYS(dev);
- }
- cntlid = nvme_subsys_register_ctrl(n, errp);
- if (cntlid < 0) {
- return -1;
- }
- n->cntlid = cntlid;
- return 0;
- }
- void nvme_attach_ns(NvmeCtrl *n, NvmeNamespace *ns)
- {
- uint32_t nsid = ns->params.nsid;
- assert(nsid && nsid <= NVME_MAX_NAMESPACES);
- n->namespaces[nsid] = ns;
- ns->attached++;
- }
- static void nvme_realize(PCIDevice *pci_dev, Error **errp)
- {
- NvmeCtrl *n = NVME(pci_dev);
- DeviceState *dev = DEVICE(pci_dev);
- NvmeNamespace *ns;
- NvmeCtrl *pn = NVME(pcie_sriov_get_pf(pci_dev));
- if (pci_is_vf(pci_dev)) {
- /*
- * VFs derive settings from the parent. PF's lifespan exceeds
- * that of VF's.
- */
- memcpy(&n->params, &pn->params, sizeof(NvmeParams));
- /*
- * Set PF's serial value to a new string memory to prevent 'serial'
- * property object release of PF when a VF is removed from the system.
- */
- n->params.serial = g_strdup(pn->params.serial);
- n->subsys = pn->subsys;
- /*
- * Assigning this link (strong link) causes an `object_unref` later in
- * `object_release_link_property`. Increment the refcount to balance
- * this out.
- */
- object_ref(OBJECT(pn->subsys));
- }
- if (!nvme_check_params(n, errp)) {
- return;
- }
- qbus_init(&n->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);
- if (nvme_init_subsys(n, errp)) {
- return;
- }
- nvme_init_state(n);
- if (!nvme_init_pci(n, pci_dev, errp)) {
- return;
- }
- nvme_init_ctrl(n, pci_dev);
- /* setup a namespace if the controller drive property was given */
- if (n->namespace.blkconf.blk) {
- ns = &n->namespace;
- ns->params.nsid = 1;
- ns->ctrl = n;
- if (nvme_ns_setup(ns, errp)) {
- return;
- }
- n->subsys->namespaces[ns->params.nsid] = ns;
- }
- }
- static void nvme_exit(PCIDevice *pci_dev)
- {
- NvmeCtrl *n = NVME(pci_dev);
- NvmeNamespace *ns;
- int i;
- nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
- for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
- ns = nvme_ns(n, i);
- if (ns) {
- ns->attached--;
- }
- }
- nvme_subsys_unregister_ctrl(n->subsys, n);
- g_free(n->cq);
- g_free(n->sq);
- g_free(n->aer_reqs);
- if (n->params.cmb_size_mb) {
- g_free(n->cmb.buf);
- }
- if (pci_dev->doe_spdm.spdm_socket > 0) {
- spdm_socket_close(pci_dev->doe_spdm.spdm_socket,
- SPDM_SOCKET_TRANSPORT_TYPE_PCI_DOE);
- }
- if (n->pmr.dev) {
- host_memory_backend_set_mapped(n->pmr.dev, false);
- }
- if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
- pcie_sriov_pf_exit(pci_dev);
- }
- if (n->params.msix_exclusive_bar && !pci_is_vf(pci_dev)) {
- msix_uninit_exclusive_bar(pci_dev);
- } else {
- msix_uninit(pci_dev, &n->bar0, &n->bar0);
- }
- memory_region_del_subregion(&n->bar0, &n->iomem);
- }
- static const Property nvme_props[] = {
- DEFINE_BLOCK_PROPERTIES(NvmeCtrl, namespace.blkconf),
- DEFINE_PROP_LINK("pmrdev", NvmeCtrl, pmr.dev, TYPE_MEMORY_BACKEND,
- HostMemoryBackend *),
- DEFINE_PROP_LINK("subsys", NvmeCtrl, subsys, TYPE_NVME_SUBSYS,
- NvmeSubsystem *),
- DEFINE_PROP_STRING("serial", NvmeCtrl, params.serial),
- DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl, params.cmb_size_mb, 0),
- DEFINE_PROP_UINT32("num_queues", NvmeCtrl, params.num_queues, 0),
- DEFINE_PROP_UINT32("max_ioqpairs", NvmeCtrl, params.max_ioqpairs, 64),
- DEFINE_PROP_UINT16("msix_qsize", NvmeCtrl, params.msix_qsize, 65),
- DEFINE_PROP_UINT8("aerl", NvmeCtrl, params.aerl, 3),
- DEFINE_PROP_UINT32("aer_max_queued", NvmeCtrl, params.aer_max_queued, 64),
- DEFINE_PROP_UINT8("mdts", NvmeCtrl, params.mdts, 7),
- DEFINE_PROP_UINT8("vsl", NvmeCtrl, params.vsl, 7),
- DEFINE_PROP_BOOL("use-intel-id", NvmeCtrl, params.use_intel_id, false),
- DEFINE_PROP_BOOL("legacy-cmb", NvmeCtrl, params.legacy_cmb, false),
- DEFINE_PROP_BOOL("ioeventfd", NvmeCtrl, params.ioeventfd, false),
- DEFINE_PROP_BOOL("dbcs", NvmeCtrl, params.dbcs, true),
- DEFINE_PROP_UINT8("zoned.zasl", NvmeCtrl, params.zasl, 0),
- DEFINE_PROP_BOOL("zoned.auto_transition", NvmeCtrl,
- params.auto_transition_zones, true),
- DEFINE_PROP_UINT16("sriov_max_vfs", NvmeCtrl, params.sriov_max_vfs, 0),
- DEFINE_PROP_UINT16("sriov_vq_flexible", NvmeCtrl,
- params.sriov_vq_flexible, 0),
- DEFINE_PROP_UINT16("sriov_vi_flexible", NvmeCtrl,
- params.sriov_vi_flexible, 0),
- DEFINE_PROP_UINT32("sriov_max_vi_per_vf", NvmeCtrl,
- params.sriov_max_vi_per_vf, 0),
- DEFINE_PROP_UINT32("sriov_max_vq_per_vf", NvmeCtrl,
- params.sriov_max_vq_per_vf, 0),
- DEFINE_PROP_BOOL("msix-exclusive-bar", NvmeCtrl, params.msix_exclusive_bar,
- false),
- DEFINE_PROP_UINT16("mqes", NvmeCtrl, params.mqes, 0x7ff),
- DEFINE_PROP_UINT16("spdm_port", PCIDevice, spdm_port, 0),
- DEFINE_PROP_BOOL("ctratt.mem", NvmeCtrl, params.ctratt.mem, false),
- DEFINE_PROP_BOOL("atomic.dn", NvmeCtrl, params.atomic_dn, 0),
- DEFINE_PROP_UINT16("atomic.awun", NvmeCtrl, params.atomic_awun, 0),
- DEFINE_PROP_UINT16("atomic.awupf", NvmeCtrl, params.atomic_awupf, 0),
- DEFINE_PROP_BOOL("ocp", NvmeCtrl, params.ocp, false),
- };
- static void nvme_get_smart_warning(Object *obj, Visitor *v, const char *name,
- void *opaque, Error **errp)
- {
- NvmeCtrl *n = NVME(obj);
- uint8_t value = n->smart_critical_warning;
- visit_type_uint8(v, name, &value, errp);
- }
- static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name,
- void *opaque, Error **errp)
- {
- NvmeCtrl *n = NVME(obj);
- uint8_t value, old_value, cap = 0, index, event;
- if (!visit_type_uint8(v, name, &value, errp)) {
- return;
- }
- cap = NVME_SMART_SPARE | NVME_SMART_TEMPERATURE | NVME_SMART_RELIABILITY
- | NVME_SMART_MEDIA_READ_ONLY | NVME_SMART_FAILED_VOLATILE_MEDIA;
- if (NVME_CAP_PMRS(ldq_le_p(&n->bar.cap))) {
- cap |= NVME_SMART_PMR_UNRELIABLE;
- }
- if ((value & cap) != value) {
- error_setg(errp, "unsupported smart critical warning bits: 0x%x",
- value & ~cap);
- return;
- }
- old_value = n->smart_critical_warning;
- n->smart_critical_warning = value;
- /* only inject new bits of smart critical warning */
- for (index = 0; index < NVME_SMART_WARN_MAX; index++) {
- event = 1 << index;
- if (value & ~old_value & event)
- nvme_smart_event(n, event);
- }
- }
- static void nvme_pci_reset(DeviceState *qdev)
- {
- PCIDevice *pci_dev = PCI_DEVICE(qdev);
- NvmeCtrl *n = NVME(pci_dev);
- trace_pci_nvme_pci_reset();
- nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
- }
- static void nvme_sriov_post_write_config(PCIDevice *dev, uint16_t old_num_vfs)
- {
- NvmeCtrl *n = NVME(dev);
- NvmeSecCtrlEntry *sctrl;
- int i;
- for (i = pcie_sriov_num_vfs(dev); i < old_num_vfs; i++) {
- sctrl = &n->sec_ctrl_list[i];
- nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
- }
- }
- static void nvme_pci_write_config(PCIDevice *dev, uint32_t address,
- uint32_t val, int len)
- {
- uint16_t old_num_vfs = pcie_sriov_num_vfs(dev);
- if (pcie_find_capability(dev, PCI_EXT_CAP_ID_DOE)) {
- pcie_doe_write_config(&dev->doe_spdm, address, val, len);
- }
- pci_default_write_config(dev, address, val, len);
- pcie_cap_flr_write_config(dev, address, val, len);
- nvme_sriov_post_write_config(dev, old_num_vfs);
- }
- static uint32_t nvme_pci_read_config(PCIDevice *dev, uint32_t address, int len)
- {
- uint32_t val;
- if (dev->spdm_port && pcie_find_capability(dev, PCI_EXT_CAP_ID_DOE)) {
- if (pcie_doe_read_config(&dev->doe_spdm, address, len, &val)) {
- return val;
- }
- }
- return pci_default_read_config(dev, address, len);
- }
- static const VMStateDescription nvme_vmstate = {
- .name = "nvme",
- .unmigratable = 1,
- };
- static void nvme_class_init(ObjectClass *oc, void *data)
- {
- DeviceClass *dc = DEVICE_CLASS(oc);
- PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
- pc->realize = nvme_realize;
- pc->config_write = nvme_pci_write_config;
- pc->config_read = nvme_pci_read_config;
- pc->exit = nvme_exit;
- pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
- pc->revision = 2;
- set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
- dc->desc = "Non-Volatile Memory Express";
- device_class_set_props(dc, nvme_props);
- dc->vmsd = &nvme_vmstate;
- device_class_set_legacy_reset(dc, nvme_pci_reset);
- }
- static void nvme_instance_init(Object *obj)
- {
- NvmeCtrl *n = NVME(obj);
- device_add_bootindex_property(obj, &n->namespace.blkconf.bootindex,
- "bootindex", "/namespace@1,0",
- DEVICE(obj));
- object_property_add(obj, "smart_critical_warning", "uint8",
- nvme_get_smart_warning,
- nvme_set_smart_warning, NULL, NULL);
- }
- static const TypeInfo nvme_info = {
- .name = TYPE_NVME,
- .parent = TYPE_PCI_DEVICE,
- .instance_size = sizeof(NvmeCtrl),
- .instance_init = nvme_instance_init,
- .class_init = nvme_class_init,
- .interfaces = (InterfaceInfo[]) {
- { INTERFACE_PCIE_DEVICE },
- { }
- },
- };
- static const TypeInfo nvme_bus_info = {
- .name = TYPE_NVME_BUS,
- .parent = TYPE_BUS,
- .instance_size = sizeof(NvmeBus),
- };
- static void nvme_register_types(void)
- {
- type_register_static(&nvme_info);
- type_register_static(&nvme_bus_info);
- }
- type_init(nvme_register_types)
|