scsi_debug.c 230 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  4. * Copyright (C) 1992 Eric Youngdale
  5. * Simulate a host adapter with 2 disks attached. Do a lot of checking
  6. * to make sure that we are not getting blocks mixed up, and PANIC if
  7. * anything out of the ordinary is seen.
  8. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. *
  10. * Copyright (C) 2001 - 2021 Douglas Gilbert
  11. *
  12. * For documentation see http://sg.danny.cz/sg/scsi_debug.html
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  15. #include <linux/module.h>
  16. #include <linux/align.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/string.h>
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/crc-t10dif.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/atomic.h>
  34. #include <linux/hrtimer.h>
  35. #include <linux/uuid.h>
  36. #include <linux/t10-pi.h>
  37. #include <linux/msdos_partition.h>
  38. #include <linux/random.h>
  39. #include <linux/xarray.h>
  40. #include <linux/prefetch.h>
  41. #include <net/checksum.h>
  42. #include <asm/unaligned.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/scsi_device.h>
  46. #include <scsi/scsi_host.h>
  47. #include <scsi/scsicam.h>
  48. #include <scsi/scsi_eh.h>
  49. #include <scsi/scsi_tcq.h>
  50. #include <scsi/scsi_dbg.h>
  51. #include "sd.h"
  52. #include "scsi_logging.h"
  53. /* make sure inq_product_rev string corresponds to this version */
  54. #define SDEBUG_VERSION "0191" /* format to fit INQUIRY revision field */
  55. static const char *sdebug_version_date = "20210520";
  56. #define MY_NAME "scsi_debug"
  57. /* Additional Sense Code (ASC) */
  58. #define NO_ADDITIONAL_SENSE 0x0
  59. #define LOGICAL_UNIT_NOT_READY 0x4
  60. #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
  61. #define UNRECOVERED_READ_ERR 0x11
  62. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  63. #define INVALID_OPCODE 0x20
  64. #define LBA_OUT_OF_RANGE 0x21
  65. #define INVALID_FIELD_IN_CDB 0x24
  66. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  67. #define WRITE_PROTECTED 0x27
  68. #define UA_RESET_ASC 0x29
  69. #define UA_CHANGED_ASC 0x2a
  70. #define TARGET_CHANGED_ASC 0x3f
  71. #define LUNS_CHANGED_ASCQ 0x0e
  72. #define INSUFF_RES_ASC 0x55
  73. #define INSUFF_RES_ASCQ 0x3
  74. #define POWER_ON_RESET_ASCQ 0x0
  75. #define POWER_ON_OCCURRED_ASCQ 0x1
  76. #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
  77. #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
  78. #define CAPACITY_CHANGED_ASCQ 0x9
  79. #define SAVING_PARAMS_UNSUP 0x39
  80. #define TRANSPORT_PROBLEM 0x4b
  81. #define THRESHOLD_EXCEEDED 0x5d
  82. #define LOW_POWER_COND_ON 0x5e
  83. #define MISCOMPARE_VERIFY_ASC 0x1d
  84. #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
  85. #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
  86. #define WRITE_ERROR_ASC 0xc
  87. #define UNALIGNED_WRITE_ASCQ 0x4
  88. #define WRITE_BOUNDARY_ASCQ 0x5
  89. #define READ_INVDATA_ASCQ 0x6
  90. #define READ_BOUNDARY_ASCQ 0x7
  91. #define ATTEMPT_ACCESS_GAP 0x9
  92. #define INSUFF_ZONE_ASCQ 0xe
  93. /* Additional Sense Code Qualifier (ASCQ) */
  94. #define ACK_NAK_TO 0x3
  95. /* Default values for driver parameters */
  96. #define DEF_NUM_HOST 1
  97. #define DEF_NUM_TGTS 1
  98. #define DEF_MAX_LUNS 1
  99. /* With these defaults, this driver will make 1 host with 1 target
  100. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  101. */
  102. #define DEF_ATO 1
  103. #define DEF_CDB_LEN 10
  104. #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */
  105. #define DEF_DEV_SIZE_PRE_INIT 0
  106. #define DEF_DEV_SIZE_MB 8
  107. #define DEF_ZBC_DEV_SIZE_MB 128
  108. #define DEF_DIF 0
  109. #define DEF_DIX 0
  110. #define DEF_PER_HOST_STORE false
  111. #define DEF_D_SENSE 0
  112. #define DEF_EVERY_NTH 0
  113. #define DEF_FAKE_RW 0
  114. #define DEF_GUARD 0
  115. #define DEF_HOST_LOCK 0
  116. #define DEF_LBPU 0
  117. #define DEF_LBPWS 0
  118. #define DEF_LBPWS10 0
  119. #define DEF_LBPRZ 1
  120. #define DEF_LOWEST_ALIGNED 0
  121. #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
  122. #define DEF_NO_LUN_0 0
  123. #define DEF_NUM_PARTS 0
  124. #define DEF_OPTS 0
  125. #define DEF_OPT_BLKS 1024
  126. #define DEF_PHYSBLK_EXP 0
  127. #define DEF_OPT_XFERLEN_EXP 0
  128. #define DEF_PTYPE TYPE_DISK
  129. #define DEF_RANDOM false
  130. #define DEF_REMOVABLE false
  131. #define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
  132. #define DEF_SECTOR_SIZE 512
  133. #define DEF_UNMAP_ALIGNMENT 0
  134. #define DEF_UNMAP_GRANULARITY 1
  135. #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
  136. #define DEF_UNMAP_MAX_DESC 256
  137. #define DEF_VIRTUAL_GB 0
  138. #define DEF_VPD_USE_HOSTNO 1
  139. #define DEF_WRITESAME_LENGTH 0xFFFF
  140. #define DEF_STRICT 0
  141. #define DEF_STATISTICS false
  142. #define DEF_SUBMIT_QUEUES 1
  143. #define DEF_TUR_MS_TO_READY 0
  144. #define DEF_UUID_CTL 0
  145. #define JDELAY_OVERRIDDEN -9999
  146. /* Default parameters for ZBC drives */
  147. #define DEF_ZBC_ZONE_SIZE_MB 128
  148. #define DEF_ZBC_MAX_OPEN_ZONES 8
  149. #define DEF_ZBC_NR_CONV_ZONES 1
  150. #define SDEBUG_LUN_0_VAL 0
  151. /* bit mask values for sdebug_opts */
  152. #define SDEBUG_OPT_NOISE 1
  153. #define SDEBUG_OPT_MEDIUM_ERR 2
  154. #define SDEBUG_OPT_TIMEOUT 4
  155. #define SDEBUG_OPT_RECOVERED_ERR 8
  156. #define SDEBUG_OPT_TRANSPORT_ERR 16
  157. #define SDEBUG_OPT_DIF_ERR 32
  158. #define SDEBUG_OPT_DIX_ERR 64
  159. #define SDEBUG_OPT_MAC_TIMEOUT 128
  160. #define SDEBUG_OPT_SHORT_TRANSFER 0x100
  161. #define SDEBUG_OPT_Q_NOISE 0x200
  162. #define SDEBUG_OPT_ALL_TSF 0x400 /* ignore */
  163. #define SDEBUG_OPT_RARE_TSF 0x800
  164. #define SDEBUG_OPT_N_WCE 0x1000
  165. #define SDEBUG_OPT_RESET_NOISE 0x2000
  166. #define SDEBUG_OPT_NO_CDB_NOISE 0x4000
  167. #define SDEBUG_OPT_HOST_BUSY 0x8000
  168. #define SDEBUG_OPT_CMD_ABORT 0x10000
  169. #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
  170. SDEBUG_OPT_RESET_NOISE)
  171. #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
  172. SDEBUG_OPT_TRANSPORT_ERR | \
  173. SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
  174. SDEBUG_OPT_SHORT_TRANSFER | \
  175. SDEBUG_OPT_HOST_BUSY | \
  176. SDEBUG_OPT_CMD_ABORT)
  177. #define SDEBUG_OPT_RECOV_DIF_DIX (SDEBUG_OPT_RECOVERED_ERR | \
  178. SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR)
  179. /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
  180. * priority order. In the subset implemented here lower numbers have higher
  181. * priority. The UA numbers should be a sequence starting from 0 with
  182. * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
  183. #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
  184. #define SDEBUG_UA_POOCCUR 1 /* Power on occurred */
  185. #define SDEBUG_UA_BUS_RESET 2
  186. #define SDEBUG_UA_MODE_CHANGED 3
  187. #define SDEBUG_UA_CAPACITY_CHANGED 4
  188. #define SDEBUG_UA_LUNS_CHANGED 5
  189. #define SDEBUG_UA_MICROCODE_CHANGED 6 /* simulate firmware change */
  190. #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 7
  191. #define SDEBUG_NUM_UAS 8
  192. /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  193. * sector on read commands: */
  194. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  195. #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
  196. /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
  197. * (for response) per submit queue at one time. Can be reduced by max_queue
  198. * option. Command responses are not queued when jdelay=0 and ndelay=0. The
  199. * per-device DEF_CMD_PER_LUN can be changed via sysfs:
  200. * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
  201. * but cannot exceed SDEBUG_CANQUEUE .
  202. */
  203. #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */
  204. #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
  205. #define DEF_CMD_PER_LUN SDEBUG_CANQUEUE
  206. /* UA - Unit Attention; SA - Service Action; SSU - Start Stop Unit */
  207. #define F_D_IN 1 /* Data-in command (e.g. READ) */
  208. #define F_D_OUT 2 /* Data-out command (e.g. WRITE) */
  209. #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
  210. #define F_D_UNKN 8
  211. #define F_RL_WLUN_OK 0x10 /* allowed with REPORT LUNS W-LUN */
  212. #define F_SKIP_UA 0x20 /* bypass UAs (e.g. INQUIRY command) */
  213. #define F_DELAY_OVERR 0x40 /* for commands like INQUIRY */
  214. #define F_SA_LOW 0x80 /* SA is in cdb byte 1, bits 4 to 0 */
  215. #define F_SA_HIGH 0x100 /* SA is in cdb bytes 8 and 9 */
  216. #define F_INV_OP 0x200 /* invalid opcode (not supported) */
  217. #define F_FAKE_RW 0x400 /* bypass resp_*() when fake_rw set */
  218. #define F_M_ACCESS 0x800 /* media access, reacts to SSU state */
  219. #define F_SSU_DELAY 0x1000 /* SSU command delay (long-ish) */
  220. #define F_SYNC_DELAY 0x2000 /* SYNCHRONIZE CACHE delay */
  221. /* Useful combinations of the above flags */
  222. #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
  223. #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
  224. #define FF_SA (F_SA_HIGH | F_SA_LOW)
  225. #define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY)
  226. #define SDEBUG_MAX_PARTS 4
  227. #define SDEBUG_MAX_CMD_LEN 32
  228. #define SDEB_XA_NOT_IN_USE XA_MARK_1
  229. /* Zone types (zbcr05 table 25) */
  230. enum sdebug_z_type {
  231. ZBC_ZTYPE_CNV = 0x1,
  232. ZBC_ZTYPE_SWR = 0x2,
  233. ZBC_ZTYPE_SWP = 0x3,
  234. /* ZBC_ZTYPE_SOBR = 0x4, */
  235. ZBC_ZTYPE_GAP = 0x5,
  236. };
  237. /* enumeration names taken from table 26, zbcr05 */
  238. enum sdebug_z_cond {
  239. ZBC_NOT_WRITE_POINTER = 0x0,
  240. ZC1_EMPTY = 0x1,
  241. ZC2_IMPLICIT_OPEN = 0x2,
  242. ZC3_EXPLICIT_OPEN = 0x3,
  243. ZC4_CLOSED = 0x4,
  244. ZC6_READ_ONLY = 0xd,
  245. ZC5_FULL = 0xe,
  246. ZC7_OFFLINE = 0xf,
  247. };
  248. struct sdeb_zone_state { /* ZBC: per zone state */
  249. enum sdebug_z_type z_type;
  250. enum sdebug_z_cond z_cond;
  251. bool z_non_seq_resource;
  252. unsigned int z_size;
  253. sector_t z_start;
  254. sector_t z_wp;
  255. };
  256. struct sdebug_dev_info {
  257. struct list_head dev_list;
  258. unsigned int channel;
  259. unsigned int target;
  260. u64 lun;
  261. uuid_t lu_name;
  262. struct sdebug_host_info *sdbg_host;
  263. unsigned long uas_bm[1];
  264. atomic_t num_in_q;
  265. atomic_t stopped; /* 1: by SSU, 2: device start */
  266. bool used;
  267. /* For ZBC devices */
  268. enum blk_zoned_model zmodel;
  269. unsigned int zcap;
  270. unsigned int zsize;
  271. unsigned int zsize_shift;
  272. unsigned int nr_zones;
  273. unsigned int nr_conv_zones;
  274. unsigned int nr_seq_zones;
  275. unsigned int nr_imp_open;
  276. unsigned int nr_exp_open;
  277. unsigned int nr_closed;
  278. unsigned int max_open;
  279. ktime_t create_ts; /* time since bootup that this device was created */
  280. struct sdeb_zone_state *zstate;
  281. };
  282. struct sdebug_host_info {
  283. struct list_head host_list;
  284. int si_idx; /* sdeb_store_info (per host) xarray index */
  285. struct Scsi_Host *shost;
  286. struct device dev;
  287. struct list_head dev_info_list;
  288. };
  289. /* There is an xarray of pointers to this struct's objects, one per host */
  290. struct sdeb_store_info {
  291. rwlock_t macc_lck; /* for atomic media access on this store */
  292. u8 *storep; /* user data storage (ram) */
  293. struct t10_pi_tuple *dif_storep; /* protection info */
  294. void *map_storep; /* provisioning map */
  295. };
  296. #define to_sdebug_host(d) \
  297. container_of(d, struct sdebug_host_info, dev)
  298. enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
  299. SDEB_DEFER_WQ = 2, SDEB_DEFER_POLL = 3};
  300. struct sdebug_defer {
  301. struct hrtimer hrt;
  302. struct execute_work ew;
  303. ktime_t cmpl_ts;/* time since boot to complete this cmd */
  304. int sqa_idx; /* index of sdebug_queue array */
  305. int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */
  306. int hc_idx; /* hostwide tag index */
  307. int issuing_cpu;
  308. bool init_hrt;
  309. bool init_wq;
  310. bool init_poll;
  311. bool aborted; /* true when blk_abort_request() already called */
  312. enum sdeb_defer_type defer_t;
  313. };
  314. struct sdebug_queued_cmd {
  315. /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
  316. * instance indicates this slot is in use.
  317. */
  318. struct sdebug_defer *sd_dp;
  319. struct scsi_cmnd *a_cmnd;
  320. };
  321. struct sdebug_queue {
  322. struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
  323. unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
  324. spinlock_t qc_lock;
  325. atomic_t blocked; /* to temporarily stop more being queued */
  326. };
  327. static atomic_t sdebug_cmnd_count; /* number of incoming commands */
  328. static atomic_t sdebug_completions; /* count of deferred completions */
  329. static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */
  330. static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */
  331. static atomic_t sdeb_inject_pending;
  332. static atomic_t sdeb_mq_poll_count; /* bumped when mq_poll returns > 0 */
  333. struct opcode_info_t {
  334. u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */
  335. /* for terminating element */
  336. u8 opcode; /* if num_attached > 0, preferred */
  337. u16 sa; /* service action */
  338. u32 flags; /* OR-ed set of SDEB_F_* */
  339. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  340. const struct opcode_info_t *arrp; /* num_attached elements or NULL */
  341. u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */
  342. /* 1 to min(cdb_len, 15); ignore cdb[15...] */
  343. };
  344. /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
  345. enum sdeb_opcode_index {
  346. SDEB_I_INVALID_OPCODE = 0,
  347. SDEB_I_INQUIRY = 1,
  348. SDEB_I_REPORT_LUNS = 2,
  349. SDEB_I_REQUEST_SENSE = 3,
  350. SDEB_I_TEST_UNIT_READY = 4,
  351. SDEB_I_MODE_SENSE = 5, /* 6, 10 */
  352. SDEB_I_MODE_SELECT = 6, /* 6, 10 */
  353. SDEB_I_LOG_SENSE = 7,
  354. SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
  355. SDEB_I_READ = 9, /* 6, 10, 12, 16 */
  356. SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
  357. SDEB_I_START_STOP = 11,
  358. SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */
  359. SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */
  360. SDEB_I_MAINT_IN = 14,
  361. SDEB_I_MAINT_OUT = 15,
  362. SDEB_I_VERIFY = 16, /* VERIFY(10), VERIFY(16) */
  363. SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */
  364. SDEB_I_RESERVE = 18, /* 6, 10 */
  365. SDEB_I_RELEASE = 19, /* 6, 10 */
  366. SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
  367. SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
  368. SDEB_I_ATA_PT = 22, /* 12, 16 */
  369. SDEB_I_SEND_DIAG = 23,
  370. SDEB_I_UNMAP = 24,
  371. SDEB_I_WRITE_BUFFER = 25,
  372. SDEB_I_WRITE_SAME = 26, /* 10, 16 */
  373. SDEB_I_SYNC_CACHE = 27, /* 10, 16 */
  374. SDEB_I_COMP_WRITE = 28,
  375. SDEB_I_PRE_FETCH = 29, /* 10, 16 */
  376. SDEB_I_ZONE_OUT = 30, /* 0x94+SA; includes no data xfer */
  377. SDEB_I_ZONE_IN = 31, /* 0x95+SA; all have data-in */
  378. SDEB_I_LAST_ELEM_P1 = 32, /* keep this last (previous + 1) */
  379. };
  380. static const unsigned char opcode_ind_arr[256] = {
  381. /* 0x0; 0x0->0x1f: 6 byte cdbs */
  382. SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
  383. 0, 0, 0, 0,
  384. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
  385. 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  386. SDEB_I_RELEASE,
  387. 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
  388. SDEB_I_ALLOW_REMOVAL, 0,
  389. /* 0x20; 0x20->0x3f: 10 byte cdbs */
  390. 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
  391. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
  392. 0, 0, 0, 0, SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, 0,
  393. 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
  394. /* 0x40; 0x40->0x5f: 10 byte cdbs */
  395. 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
  396. 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
  397. 0, 0, 0, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  398. SDEB_I_RELEASE,
  399. 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
  400. /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
  401. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  402. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  403. 0, SDEB_I_VARIABLE_LEN,
  404. /* 0x80; 0x80->0x9f: 16 byte cdbs */
  405. 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
  406. SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0,
  407. 0, 0, 0, SDEB_I_VERIFY,
  408. SDEB_I_PRE_FETCH, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME,
  409. SDEB_I_ZONE_OUT, SDEB_I_ZONE_IN, 0, 0,
  410. 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
  411. /* 0xa0; 0xa0->0xbf: 12 byte cdbs */
  412. SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
  413. SDEB_I_MAINT_OUT, 0, 0, 0,
  414. SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
  415. 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
  416. 0, 0, 0, 0, 0, 0, 0, 0,
  417. 0, 0, 0, 0, 0, 0, 0, 0,
  418. /* 0xc0; 0xc0->0xff: vendor specific */
  419. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  420. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  421. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  422. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  423. };
  424. /*
  425. * The following "response" functions return the SCSI mid-level's 4 byte
  426. * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
  427. * command completion, they can mask their return value with
  428. * SDEG_RES_IMMED_MASK .
  429. */
  430. #define SDEG_RES_IMMED_MASK 0x40000000
  431. static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
  432. static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
  433. static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
  434. static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  435. static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
  436. static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  437. static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
  438. static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  439. static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  440. static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
  441. static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
  442. static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
  443. static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
  444. static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
  445. static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
  446. static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
  447. static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
  448. static int resp_verify(struct scsi_cmnd *, struct sdebug_dev_info *);
  449. static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
  450. static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
  451. static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
  452. static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
  453. static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
  454. static int resp_pre_fetch(struct scsi_cmnd *, struct sdebug_dev_info *);
  455. static int resp_report_zones(struct scsi_cmnd *, struct sdebug_dev_info *);
  456. static int resp_open_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
  457. static int resp_close_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
  458. static int resp_finish_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
  459. static int resp_rwp_zone(struct scsi_cmnd *, struct sdebug_dev_info *);
  460. static int sdebug_do_add_host(bool mk_new_store);
  461. static int sdebug_add_host_helper(int per_host_idx);
  462. static void sdebug_do_remove_host(bool the_end);
  463. static int sdebug_add_store(void);
  464. static void sdebug_erase_store(int idx, struct sdeb_store_info *sip);
  465. static void sdebug_erase_all_stores(bool apart_from_first);
  466. /*
  467. * The following are overflow arrays for cdbs that "hit" the same index in
  468. * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
  469. * should be placed in opcode_info_arr[], the others should be placed here.
  470. */
  471. static const struct opcode_info_t msense_iarr[] = {
  472. {0, 0x1a, 0, F_D_IN, NULL, NULL,
  473. {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  474. };
  475. static const struct opcode_info_t mselect_iarr[] = {
  476. {0, 0x15, 0, F_D_OUT, NULL, NULL,
  477. {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  478. };
  479. static const struct opcode_info_t read_iarr[] = {
  480. {0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
  481. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  482. 0, 0, 0, 0} },
  483. {0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */
  484. {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  485. {0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
  486. {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
  487. 0xc7, 0, 0, 0, 0} },
  488. };
  489. static const struct opcode_info_t write_iarr[] = {
  490. {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */
  491. NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
  492. 0, 0, 0, 0, 0, 0} },
  493. {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */
  494. NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
  495. 0, 0, 0} },
  496. {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */
  497. NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  498. 0xbf, 0xc7, 0, 0, 0, 0} },
  499. };
  500. static const struct opcode_info_t verify_iarr[] = {
  501. {0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify,/* VERIFY(10) */
  502. NULL, {10, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xc7,
  503. 0, 0, 0, 0, 0, 0} },
  504. };
  505. static const struct opcode_info_t sa_in_16_iarr[] = {
  506. {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
  507. {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  508. 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */
  509. };
  510. static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */
  511. {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
  512. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
  513. 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
  514. {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  515. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
  516. 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */
  517. };
  518. static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */
  519. {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
  520. {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
  521. 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
  522. {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
  523. {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  524. 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
  525. };
  526. static const struct opcode_info_t write_same_iarr[] = {
  527. {0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
  528. {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  529. 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */
  530. };
  531. static const struct opcode_info_t reserve_iarr[] = {
  532. {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
  533. {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  534. };
  535. static const struct opcode_info_t release_iarr[] = {
  536. {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
  537. {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  538. };
  539. static const struct opcode_info_t sync_cache_iarr[] = {
  540. {0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
  541. {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  542. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */
  543. };
  544. static const struct opcode_info_t pre_fetch_iarr[] = {
  545. {0, 0x90, 0, F_SYNC_DELAY | FF_MEDIA_IO, resp_pre_fetch, NULL,
  546. {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  547. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* PRE-FETCH (16) */
  548. };
  549. static const struct opcode_info_t zone_out_iarr[] = { /* ZONE OUT(16) */
  550. {0, 0x94, 0x1, F_SA_LOW | F_M_ACCESS, resp_close_zone, NULL,
  551. {16, 0x1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  552. 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* CLOSE ZONE */
  553. {0, 0x94, 0x2, F_SA_LOW | F_M_ACCESS, resp_finish_zone, NULL,
  554. {16, 0x2, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  555. 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* FINISH ZONE */
  556. {0, 0x94, 0x4, F_SA_LOW | F_M_ACCESS, resp_rwp_zone, NULL,
  557. {16, 0x4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  558. 0xff, 0, 0, 0xff, 0xff, 0x1, 0xc7} }, /* RESET WRITE POINTER */
  559. };
  560. static const struct opcode_info_t zone_in_iarr[] = { /* ZONE IN(16) */
  561. {0, 0x95, 0x6, F_SA_LOW | F_D_IN | F_M_ACCESS, NULL, NULL,
  562. {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  563. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* REPORT ZONES */
  564. };
  565. /* This array is accessed via SDEB_I_* values. Make sure all are mapped,
  566. * plus the terminating elements for logic that scans this table such as
  567. * REPORT SUPPORTED OPERATION CODES. */
  568. static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEM_P1 + 1] = {
  569. /* 0 */
  570. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */
  571. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  572. {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
  573. {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  574. {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
  575. {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  576. 0, 0} }, /* REPORT LUNS */
  577. {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
  578. {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  579. {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
  580. {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  581. /* 5 */
  582. {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */
  583. resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0,
  584. 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  585. {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */
  586. resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff,
  587. 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  588. {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */
  589. {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
  590. 0, 0, 0} },
  591. {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */
  592. {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
  593. 0, 0} },
  594. {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */
  595. resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff,
  596. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  597. /* 10 */
  598. {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO,
  599. resp_write_dt0, write_iarr, /* WRITE(16) */
  600. {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  601. 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  602. {0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
  603. {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  604. {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN,
  605. resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
  606. {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  607. 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
  608. {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  609. NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
  610. 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */
  611. {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN,
  612. resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */
  613. maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
  614. 0xff, 0, 0xc7, 0, 0, 0, 0} },
  615. /* 15 */
  616. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
  617. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  618. {ARRAY_SIZE(verify_iarr), 0x8f, 0,
  619. F_D_OUT_MAYBE | FF_MEDIA_IO, resp_verify, /* VERIFY(16) */
  620. verify_iarr, {16, 0xf6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  621. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} },
  622. {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
  623. resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */
  624. {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
  625. 0xff, 0xff} },
  626. {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT,
  627. NULL, reserve_iarr, /* RESERVE(10) <no response function> */
  628. {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  629. 0} },
  630. {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT,
  631. NULL, release_iarr, /* RELEASE(10) <no response function> */
  632. {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  633. 0} },
  634. /* 20 */
  635. {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
  636. {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  637. {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
  638. {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  639. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
  640. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  641. {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
  642. {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  643. {0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
  644. {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  645. /* 25 */
  646. {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
  647. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
  648. 0, 0, 0, 0} }, /* WRITE_BUFFER */
  649. {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO,
  650. resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */
  651. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
  652. 0, 0, 0, 0, 0} },
  653. {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS,
  654. resp_sync_cache, sync_cache_iarr,
  655. {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  656. 0, 0, 0, 0} }, /* SYNC_CACHE (10) */
  657. {0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
  658. {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
  659. 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */
  660. {ARRAY_SIZE(pre_fetch_iarr), 0x34, 0, F_SYNC_DELAY | FF_MEDIA_IO,
  661. resp_pre_fetch, pre_fetch_iarr,
  662. {10, 0x2, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  663. 0, 0, 0, 0} }, /* PRE-FETCH (10) */
  664. /* 30 */
  665. {ARRAY_SIZE(zone_out_iarr), 0x94, 0x3, F_SA_LOW | F_M_ACCESS,
  666. resp_open_zone, zone_out_iarr, /* ZONE_OUT(16), OPEN ZONE) */
  667. {16, 0x3 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  668. 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x1, 0xc7} },
  669. {ARRAY_SIZE(zone_in_iarr), 0x95, 0x0, F_SA_LOW | F_M_ACCESS,
  670. resp_report_zones, zone_in_iarr, /* ZONE_IN(16), REPORT ZONES) */
  671. {16, 0x0 /* SA */, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  672. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc7} },
  673. /* sentinel */
  674. {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
  675. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  676. };
  677. static int sdebug_num_hosts;
  678. static int sdebug_add_host = DEF_NUM_HOST; /* in sysfs this is relative */
  679. static int sdebug_ato = DEF_ATO;
  680. static int sdebug_cdb_len = DEF_CDB_LEN;
  681. static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */
  682. static int sdebug_dev_size_mb = DEF_DEV_SIZE_PRE_INIT;
  683. static int sdebug_dif = DEF_DIF;
  684. static int sdebug_dix = DEF_DIX;
  685. static int sdebug_dsense = DEF_D_SENSE;
  686. static int sdebug_every_nth = DEF_EVERY_NTH;
  687. static int sdebug_fake_rw = DEF_FAKE_RW;
  688. static unsigned int sdebug_guard = DEF_GUARD;
  689. static int sdebug_host_max_queue; /* per host */
  690. static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
  691. static int sdebug_max_luns = DEF_MAX_LUNS;
  692. static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */
  693. static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
  694. static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
  695. static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
  696. static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */
  697. static int sdebug_no_lun_0 = DEF_NO_LUN_0;
  698. static int sdebug_no_uld;
  699. static int sdebug_num_parts = DEF_NUM_PARTS;
  700. static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  701. static int sdebug_opt_blks = DEF_OPT_BLKS;
  702. static int sdebug_opts = DEF_OPTS;
  703. static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
  704. static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
  705. static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
  706. static int sdebug_scsi_level = DEF_SCSI_LEVEL;
  707. static int sdebug_sector_size = DEF_SECTOR_SIZE;
  708. static int sdeb_tur_ms_to_ready = DEF_TUR_MS_TO_READY;
  709. static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
  710. static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  711. static unsigned int sdebug_lbpu = DEF_LBPU;
  712. static unsigned int sdebug_lbpws = DEF_LBPWS;
  713. static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
  714. static unsigned int sdebug_lbprz = DEF_LBPRZ;
  715. static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
  716. static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
  717. static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
  718. static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
  719. static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
  720. static int sdebug_uuid_ctl = DEF_UUID_CTL;
  721. static bool sdebug_random = DEF_RANDOM;
  722. static bool sdebug_per_host_store = DEF_PER_HOST_STORE;
  723. static bool sdebug_removable = DEF_REMOVABLE;
  724. static bool sdebug_clustering;
  725. static bool sdebug_host_lock = DEF_HOST_LOCK;
  726. static bool sdebug_strict = DEF_STRICT;
  727. static bool sdebug_any_injecting_opt;
  728. static bool sdebug_no_rwlock;
  729. static bool sdebug_verbose;
  730. static bool have_dif_prot;
  731. static bool write_since_sync;
  732. static bool sdebug_statistics = DEF_STATISTICS;
  733. static bool sdebug_wp;
  734. /* Following enum: 0: no zbc, def; 1: host aware; 2: host managed */
  735. static enum blk_zoned_model sdeb_zbc_model = BLK_ZONED_NONE;
  736. static char *sdeb_zbc_model_s;
  737. enum sam_lun_addr_method {SAM_LUN_AM_PERIPHERAL = 0x0,
  738. SAM_LUN_AM_FLAT = 0x1,
  739. SAM_LUN_AM_LOGICAL_UNIT = 0x2,
  740. SAM_LUN_AM_EXTENDED = 0x3};
  741. static enum sam_lun_addr_method sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
  742. static int sdebug_lun_am_i = (int)SAM_LUN_AM_PERIPHERAL;
  743. static unsigned int sdebug_store_sectors;
  744. static sector_t sdebug_capacity; /* in sectors */
  745. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  746. may still need them */
  747. static int sdebug_heads; /* heads per disk */
  748. static int sdebug_cylinders_per; /* cylinders per surface */
  749. static int sdebug_sectors_per; /* sectors per cylinder */
  750. static LIST_HEAD(sdebug_host_list);
  751. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  752. static struct xarray per_store_arr;
  753. static struct xarray *per_store_ap = &per_store_arr;
  754. static int sdeb_first_idx = -1; /* invalid index ==> none created */
  755. static int sdeb_most_recent_idx = -1;
  756. static DEFINE_RWLOCK(sdeb_fake_rw_lck); /* need a RW lock when fake_rw=1 */
  757. static unsigned long map_size;
  758. static int num_aborts;
  759. static int num_dev_resets;
  760. static int num_target_resets;
  761. static int num_bus_resets;
  762. static int num_host_resets;
  763. static int dix_writes;
  764. static int dix_reads;
  765. static int dif_errors;
  766. /* ZBC global data */
  767. static bool sdeb_zbc_in_use; /* true for host-aware and host-managed disks */
  768. static int sdeb_zbc_zone_cap_mb;
  769. static int sdeb_zbc_zone_size_mb;
  770. static int sdeb_zbc_max_open = DEF_ZBC_MAX_OPEN_ZONES;
  771. static int sdeb_zbc_nr_conv = DEF_ZBC_NR_CONV_ZONES;
  772. static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */
  773. static int poll_queues; /* iouring iopoll interface.*/
  774. static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */
  775. static DEFINE_RWLOCK(atomic_rw);
  776. static DEFINE_RWLOCK(atomic_rw2);
  777. static rwlock_t *ramdisk_lck_a[2];
  778. static char sdebug_proc_name[] = MY_NAME;
  779. static const char *my_name = MY_NAME;
  780. static struct bus_type pseudo_lld_bus;
  781. static struct device_driver sdebug_driverfs_driver = {
  782. .name = sdebug_proc_name,
  783. .bus = &pseudo_lld_bus,
  784. };
  785. static const int check_condition_result =
  786. SAM_STAT_CHECK_CONDITION;
  787. static const int illegal_condition_result =
  788. (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
  789. static const int device_qfull_result =
  790. (DID_ABORT << 16) | SAM_STAT_TASK_SET_FULL;
  791. static const int condition_met_result = SAM_STAT_CONDITION_MET;
  792. /* Only do the extra work involved in logical block provisioning if one or
  793. * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
  794. * real reads and writes (i.e. not skipping them for speed).
  795. */
  796. static inline bool scsi_debug_lbp(void)
  797. {
  798. return 0 == sdebug_fake_rw &&
  799. (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
  800. }
  801. static void *lba2fake_store(struct sdeb_store_info *sip,
  802. unsigned long long lba)
  803. {
  804. struct sdeb_store_info *lsip = sip;
  805. lba = do_div(lba, sdebug_store_sectors);
  806. if (!sip || !sip->storep) {
  807. WARN_ON_ONCE(true);
  808. lsip = xa_load(per_store_ap, 0); /* should never be NULL */
  809. }
  810. return lsip->storep + lba * sdebug_sector_size;
  811. }
  812. static struct t10_pi_tuple *dif_store(struct sdeb_store_info *sip,
  813. sector_t sector)
  814. {
  815. sector = sector_div(sector, sdebug_store_sectors);
  816. return sip->dif_storep + sector;
  817. }
  818. static void sdebug_max_tgts_luns(void)
  819. {
  820. struct sdebug_host_info *sdbg_host;
  821. struct Scsi_Host *hpnt;
  822. spin_lock(&sdebug_host_list_lock);
  823. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  824. hpnt = sdbg_host->shost;
  825. if ((hpnt->this_id >= 0) &&
  826. (sdebug_num_tgts > hpnt->this_id))
  827. hpnt->max_id = sdebug_num_tgts + 1;
  828. else
  829. hpnt->max_id = sdebug_num_tgts;
  830. /* sdebug_max_luns; */
  831. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  832. }
  833. spin_unlock(&sdebug_host_list_lock);
  834. }
  835. enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
  836. /* Set in_bit to -1 to indicate no bit position of invalid field */
  837. static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
  838. enum sdeb_cmd_data c_d,
  839. int in_byte, int in_bit)
  840. {
  841. unsigned char *sbuff;
  842. u8 sks[4];
  843. int sl, asc;
  844. sbuff = scp->sense_buffer;
  845. if (!sbuff) {
  846. sdev_printk(KERN_ERR, scp->device,
  847. "%s: sense_buffer is NULL\n", __func__);
  848. return;
  849. }
  850. asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
  851. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  852. scsi_build_sense(scp, sdebug_dsense, ILLEGAL_REQUEST, asc, 0);
  853. memset(sks, 0, sizeof(sks));
  854. sks[0] = 0x80;
  855. if (c_d)
  856. sks[0] |= 0x40;
  857. if (in_bit >= 0) {
  858. sks[0] |= 0x8;
  859. sks[0] |= 0x7 & in_bit;
  860. }
  861. put_unaligned_be16(in_byte, sks + 1);
  862. if (sdebug_dsense) {
  863. sl = sbuff[7] + 8;
  864. sbuff[7] = sl;
  865. sbuff[sl] = 0x2;
  866. sbuff[sl + 1] = 0x6;
  867. memcpy(sbuff + sl + 4, sks, 3);
  868. } else
  869. memcpy(sbuff + 15, sks, 3);
  870. if (sdebug_verbose)
  871. sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
  872. "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
  873. my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
  874. }
  875. static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
  876. {
  877. if (!scp->sense_buffer) {
  878. sdev_printk(KERN_ERR, scp->device,
  879. "%s: sense_buffer is NULL\n", __func__);
  880. return;
  881. }
  882. memset(scp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  883. scsi_build_sense(scp, sdebug_dsense, key, asc, asq);
  884. if (sdebug_verbose)
  885. sdev_printk(KERN_INFO, scp->device,
  886. "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
  887. my_name, key, asc, asq);
  888. }
  889. static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
  890. {
  891. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  892. }
  893. static int scsi_debug_ioctl(struct scsi_device *dev, unsigned int cmd,
  894. void __user *arg)
  895. {
  896. if (sdebug_verbose) {
  897. if (0x1261 == cmd)
  898. sdev_printk(KERN_INFO, dev,
  899. "%s: BLKFLSBUF [0x1261]\n", __func__);
  900. else if (0x5331 == cmd)
  901. sdev_printk(KERN_INFO, dev,
  902. "%s: CDROM_GET_CAPABILITY [0x5331]\n",
  903. __func__);
  904. else
  905. sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
  906. __func__, cmd);
  907. }
  908. return -EINVAL;
  909. /* return -ENOTTY; // correct return but upsets fdisk */
  910. }
  911. static void config_cdb_len(struct scsi_device *sdev)
  912. {
  913. switch (sdebug_cdb_len) {
  914. case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
  915. sdev->use_10_for_rw = false;
  916. sdev->use_16_for_rw = false;
  917. sdev->use_10_for_ms = false;
  918. break;
  919. case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
  920. sdev->use_10_for_rw = true;
  921. sdev->use_16_for_rw = false;
  922. sdev->use_10_for_ms = false;
  923. break;
  924. case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
  925. sdev->use_10_for_rw = true;
  926. sdev->use_16_for_rw = false;
  927. sdev->use_10_for_ms = true;
  928. break;
  929. case 16:
  930. sdev->use_10_for_rw = false;
  931. sdev->use_16_for_rw = true;
  932. sdev->use_10_for_ms = true;
  933. break;
  934. case 32: /* No knobs to suggest this so same as 16 for now */
  935. sdev->use_10_for_rw = false;
  936. sdev->use_16_for_rw = true;
  937. sdev->use_10_for_ms = true;
  938. break;
  939. default:
  940. pr_warn("unexpected cdb_len=%d, force to 10\n",
  941. sdebug_cdb_len);
  942. sdev->use_10_for_rw = true;
  943. sdev->use_16_for_rw = false;
  944. sdev->use_10_for_ms = false;
  945. sdebug_cdb_len = 10;
  946. break;
  947. }
  948. }
  949. static void all_config_cdb_len(void)
  950. {
  951. struct sdebug_host_info *sdbg_host;
  952. struct Scsi_Host *shost;
  953. struct scsi_device *sdev;
  954. spin_lock(&sdebug_host_list_lock);
  955. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  956. shost = sdbg_host->shost;
  957. shost_for_each_device(sdev, shost) {
  958. config_cdb_len(sdev);
  959. }
  960. }
  961. spin_unlock(&sdebug_host_list_lock);
  962. }
  963. static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
  964. {
  965. struct sdebug_host_info *sdhp;
  966. struct sdebug_dev_info *dp;
  967. spin_lock(&sdebug_host_list_lock);
  968. list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
  969. list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
  970. if ((devip->sdbg_host == dp->sdbg_host) &&
  971. (devip->target == dp->target))
  972. clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
  973. }
  974. }
  975. spin_unlock(&sdebug_host_list_lock);
  976. }
  977. static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  978. {
  979. int k;
  980. k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
  981. if (k != SDEBUG_NUM_UAS) {
  982. const char *cp = NULL;
  983. switch (k) {
  984. case SDEBUG_UA_POR:
  985. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  986. POWER_ON_RESET_ASCQ);
  987. if (sdebug_verbose)
  988. cp = "power on reset";
  989. break;
  990. case SDEBUG_UA_POOCCUR:
  991. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  992. POWER_ON_OCCURRED_ASCQ);
  993. if (sdebug_verbose)
  994. cp = "power on occurred";
  995. break;
  996. case SDEBUG_UA_BUS_RESET:
  997. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  998. BUS_RESET_ASCQ);
  999. if (sdebug_verbose)
  1000. cp = "bus reset";
  1001. break;
  1002. case SDEBUG_UA_MODE_CHANGED:
  1003. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  1004. MODE_CHANGED_ASCQ);
  1005. if (sdebug_verbose)
  1006. cp = "mode parameters changed";
  1007. break;
  1008. case SDEBUG_UA_CAPACITY_CHANGED:
  1009. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  1010. CAPACITY_CHANGED_ASCQ);
  1011. if (sdebug_verbose)
  1012. cp = "capacity data changed";
  1013. break;
  1014. case SDEBUG_UA_MICROCODE_CHANGED:
  1015. mk_sense_buffer(scp, UNIT_ATTENTION,
  1016. TARGET_CHANGED_ASC,
  1017. MICROCODE_CHANGED_ASCQ);
  1018. if (sdebug_verbose)
  1019. cp = "microcode has been changed";
  1020. break;
  1021. case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
  1022. mk_sense_buffer(scp, UNIT_ATTENTION,
  1023. TARGET_CHANGED_ASC,
  1024. MICROCODE_CHANGED_WO_RESET_ASCQ);
  1025. if (sdebug_verbose)
  1026. cp = "microcode has been changed without reset";
  1027. break;
  1028. case SDEBUG_UA_LUNS_CHANGED:
  1029. /*
  1030. * SPC-3 behavior is to report a UNIT ATTENTION with
  1031. * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
  1032. * on the target, until a REPORT LUNS command is
  1033. * received. SPC-4 behavior is to report it only once.
  1034. * NOTE: sdebug_scsi_level does not use the same
  1035. * values as struct scsi_device->scsi_level.
  1036. */
  1037. if (sdebug_scsi_level >= 6) /* SPC-4 and above */
  1038. clear_luns_changed_on_target(devip);
  1039. mk_sense_buffer(scp, UNIT_ATTENTION,
  1040. TARGET_CHANGED_ASC,
  1041. LUNS_CHANGED_ASCQ);
  1042. if (sdebug_verbose)
  1043. cp = "reported luns data has changed";
  1044. break;
  1045. default:
  1046. pr_warn("unexpected unit attention code=%d\n", k);
  1047. if (sdebug_verbose)
  1048. cp = "unknown";
  1049. break;
  1050. }
  1051. clear_bit(k, devip->uas_bm);
  1052. if (sdebug_verbose)
  1053. sdev_printk(KERN_INFO, scp->device,
  1054. "%s reports: Unit attention: %s\n",
  1055. my_name, cp);
  1056. return check_condition_result;
  1057. }
  1058. return 0;
  1059. }
  1060. /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
  1061. static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  1062. int arr_len)
  1063. {
  1064. int act_len;
  1065. struct scsi_data_buffer *sdb = &scp->sdb;
  1066. if (!sdb->length)
  1067. return 0;
  1068. if (scp->sc_data_direction != DMA_FROM_DEVICE)
  1069. return DID_ERROR << 16;
  1070. act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
  1071. arr, arr_len);
  1072. scsi_set_resid(scp, scsi_bufflen(scp) - act_len);
  1073. return 0;
  1074. }
  1075. /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
  1076. * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
  1077. * calls, not required to write in ascending offset order. Assumes resid
  1078. * set to scsi_bufflen() prior to any calls.
  1079. */
  1080. static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
  1081. int arr_len, unsigned int off_dst)
  1082. {
  1083. unsigned int act_len, n;
  1084. struct scsi_data_buffer *sdb = &scp->sdb;
  1085. off_t skip = off_dst;
  1086. if (sdb->length <= off_dst)
  1087. return 0;
  1088. if (scp->sc_data_direction != DMA_FROM_DEVICE)
  1089. return DID_ERROR << 16;
  1090. act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
  1091. arr, arr_len, skip);
  1092. pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
  1093. __func__, off_dst, scsi_bufflen(scp), act_len,
  1094. scsi_get_resid(scp));
  1095. n = scsi_bufflen(scp) - (off_dst + act_len);
  1096. scsi_set_resid(scp, min_t(u32, scsi_get_resid(scp), n));
  1097. return 0;
  1098. }
  1099. /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
  1100. * 'arr' or -1 if error.
  1101. */
  1102. static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  1103. int arr_len)
  1104. {
  1105. if (!scsi_bufflen(scp))
  1106. return 0;
  1107. if (scp->sc_data_direction != DMA_TO_DEVICE)
  1108. return -1;
  1109. return scsi_sg_copy_to_buffer(scp, arr, arr_len);
  1110. }
  1111. static char sdebug_inq_vendor_id[9] = "Linux ";
  1112. static char sdebug_inq_product_id[17] = "scsi_debug ";
  1113. static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
  1114. /* Use some locally assigned NAAs for SAS addresses. */
  1115. static const u64 naa3_comp_a = 0x3222222000000000ULL;
  1116. static const u64 naa3_comp_b = 0x3333333000000000ULL;
  1117. static const u64 naa3_comp_c = 0x3111111000000000ULL;
  1118. /* Device identification VPD page. Returns number of bytes placed in arr */
  1119. static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
  1120. int target_dev_id, int dev_id_num,
  1121. const char *dev_id_str, int dev_id_str_len,
  1122. const uuid_t *lu_name)
  1123. {
  1124. int num, port_a;
  1125. char b[32];
  1126. port_a = target_dev_id + 1;
  1127. /* T10 vendor identifier field format (faked) */
  1128. arr[0] = 0x2; /* ASCII */
  1129. arr[1] = 0x1;
  1130. arr[2] = 0x0;
  1131. memcpy(&arr[4], sdebug_inq_vendor_id, 8);
  1132. memcpy(&arr[12], sdebug_inq_product_id, 16);
  1133. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  1134. num = 8 + 16 + dev_id_str_len;
  1135. arr[3] = num;
  1136. num += 4;
  1137. if (dev_id_num >= 0) {
  1138. if (sdebug_uuid_ctl) {
  1139. /* Locally assigned UUID */
  1140. arr[num++] = 0x1; /* binary (not necessarily sas) */
  1141. arr[num++] = 0xa; /* PIV=0, lu, naa */
  1142. arr[num++] = 0x0;
  1143. arr[num++] = 0x12;
  1144. arr[num++] = 0x10; /* uuid type=1, locally assigned */
  1145. arr[num++] = 0x0;
  1146. memcpy(arr + num, lu_name, 16);
  1147. num += 16;
  1148. } else {
  1149. /* NAA-3, Logical unit identifier (binary) */
  1150. arr[num++] = 0x1; /* binary (not necessarily sas) */
  1151. arr[num++] = 0x3; /* PIV=0, lu, naa */
  1152. arr[num++] = 0x0;
  1153. arr[num++] = 0x8;
  1154. put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
  1155. num += 8;
  1156. }
  1157. /* Target relative port number */
  1158. arr[num++] = 0x61; /* proto=sas, binary */
  1159. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  1160. arr[num++] = 0x0; /* reserved */
  1161. arr[num++] = 0x4; /* length */
  1162. arr[num++] = 0x0; /* reserved */
  1163. arr[num++] = 0x0; /* reserved */
  1164. arr[num++] = 0x0;
  1165. arr[num++] = 0x1; /* relative port A */
  1166. }
  1167. /* NAA-3, Target port identifier */
  1168. arr[num++] = 0x61; /* proto=sas, binary */
  1169. arr[num++] = 0x93; /* piv=1, target port, naa */
  1170. arr[num++] = 0x0;
  1171. arr[num++] = 0x8;
  1172. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1173. num += 8;
  1174. /* NAA-3, Target port group identifier */
  1175. arr[num++] = 0x61; /* proto=sas, binary */
  1176. arr[num++] = 0x95; /* piv=1, target port group id */
  1177. arr[num++] = 0x0;
  1178. arr[num++] = 0x4;
  1179. arr[num++] = 0;
  1180. arr[num++] = 0;
  1181. put_unaligned_be16(port_group_id, arr + num);
  1182. num += 2;
  1183. /* NAA-3, Target device identifier */
  1184. arr[num++] = 0x61; /* proto=sas, binary */
  1185. arr[num++] = 0xa3; /* piv=1, target device, naa */
  1186. arr[num++] = 0x0;
  1187. arr[num++] = 0x8;
  1188. put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
  1189. num += 8;
  1190. /* SCSI name string: Target device identifier */
  1191. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  1192. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  1193. arr[num++] = 0x0;
  1194. arr[num++] = 24;
  1195. memcpy(arr + num, "naa.32222220", 12);
  1196. num += 12;
  1197. snprintf(b, sizeof(b), "%08X", target_dev_id);
  1198. memcpy(arr + num, b, 8);
  1199. num += 8;
  1200. memset(arr + num, 0, 4);
  1201. num += 4;
  1202. return num;
  1203. }
  1204. static unsigned char vpd84_data[] = {
  1205. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  1206. 0x22,0x22,0x22,0x0,0xbb,0x1,
  1207. 0x22,0x22,0x22,0x0,0xbb,0x2,
  1208. };
  1209. /* Software interface identification VPD page */
  1210. static int inquiry_vpd_84(unsigned char *arr)
  1211. {
  1212. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  1213. return sizeof(vpd84_data);
  1214. }
  1215. /* Management network addresses VPD page */
  1216. static int inquiry_vpd_85(unsigned char *arr)
  1217. {
  1218. int num = 0;
  1219. const char *na1 = "https://www.kernel.org/config";
  1220. const char *na2 = "http://www.kernel.org/log";
  1221. int plen, olen;
  1222. arr[num++] = 0x1; /* lu, storage config */
  1223. arr[num++] = 0x0; /* reserved */
  1224. arr[num++] = 0x0;
  1225. olen = strlen(na1);
  1226. plen = olen + 1;
  1227. if (plen % 4)
  1228. plen = ((plen / 4) + 1) * 4;
  1229. arr[num++] = plen; /* length, null termianted, padded */
  1230. memcpy(arr + num, na1, olen);
  1231. memset(arr + num + olen, 0, plen - olen);
  1232. num += plen;
  1233. arr[num++] = 0x4; /* lu, logging */
  1234. arr[num++] = 0x0; /* reserved */
  1235. arr[num++] = 0x0;
  1236. olen = strlen(na2);
  1237. plen = olen + 1;
  1238. if (plen % 4)
  1239. plen = ((plen / 4) + 1) * 4;
  1240. arr[num++] = plen; /* length, null terminated, padded */
  1241. memcpy(arr + num, na2, olen);
  1242. memset(arr + num + olen, 0, plen - olen);
  1243. num += plen;
  1244. return num;
  1245. }
  1246. /* SCSI ports VPD page */
  1247. static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
  1248. {
  1249. int num = 0;
  1250. int port_a, port_b;
  1251. port_a = target_dev_id + 1;
  1252. port_b = port_a + 1;
  1253. arr[num++] = 0x0; /* reserved */
  1254. arr[num++] = 0x0; /* reserved */
  1255. arr[num++] = 0x0;
  1256. arr[num++] = 0x1; /* relative port 1 (primary) */
  1257. memset(arr + num, 0, 6);
  1258. num += 6;
  1259. arr[num++] = 0x0;
  1260. arr[num++] = 12; /* length tp descriptor */
  1261. /* naa-5 target port identifier (A) */
  1262. arr[num++] = 0x61; /* proto=sas, binary */
  1263. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1264. arr[num++] = 0x0; /* reserved */
  1265. arr[num++] = 0x8; /* length */
  1266. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1267. num += 8;
  1268. arr[num++] = 0x0; /* reserved */
  1269. arr[num++] = 0x0; /* reserved */
  1270. arr[num++] = 0x0;
  1271. arr[num++] = 0x2; /* relative port 2 (secondary) */
  1272. memset(arr + num, 0, 6);
  1273. num += 6;
  1274. arr[num++] = 0x0;
  1275. arr[num++] = 12; /* length tp descriptor */
  1276. /* naa-5 target port identifier (B) */
  1277. arr[num++] = 0x61; /* proto=sas, binary */
  1278. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1279. arr[num++] = 0x0; /* reserved */
  1280. arr[num++] = 0x8; /* length */
  1281. put_unaligned_be64(naa3_comp_a + port_b, arr + num);
  1282. num += 8;
  1283. return num;
  1284. }
  1285. static unsigned char vpd89_data[] = {
  1286. /* from 4th byte */ 0,0,0,0,
  1287. 'l','i','n','u','x',' ',' ',' ',
  1288. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  1289. '1','2','3','4',
  1290. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  1291. 0xec,0,0,0,
  1292. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  1293. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  1294. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  1295. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  1296. 0x53,0x41,
  1297. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1298. 0x20,0x20,
  1299. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1300. 0x10,0x80,
  1301. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  1302. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  1303. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  1304. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  1305. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  1306. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  1307. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  1308. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1309. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1310. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1311. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  1312. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  1313. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  1314. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  1315. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1316. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1317. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1318. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1319. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1320. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1321. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1322. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1323. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1324. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1325. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1326. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  1327. };
  1328. /* ATA Information VPD page */
  1329. static int inquiry_vpd_89(unsigned char *arr)
  1330. {
  1331. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  1332. return sizeof(vpd89_data);
  1333. }
  1334. static unsigned char vpdb0_data[] = {
  1335. /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
  1336. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1337. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1338. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1339. };
  1340. /* Block limits VPD page (SBC-3) */
  1341. static int inquiry_vpd_b0(unsigned char *arr)
  1342. {
  1343. unsigned int gran;
  1344. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  1345. /* Optimal transfer length granularity */
  1346. if (sdebug_opt_xferlen_exp != 0 &&
  1347. sdebug_physblk_exp < sdebug_opt_xferlen_exp)
  1348. gran = 1 << sdebug_opt_xferlen_exp;
  1349. else
  1350. gran = 1 << sdebug_physblk_exp;
  1351. put_unaligned_be16(gran, arr + 2);
  1352. /* Maximum Transfer Length */
  1353. if (sdebug_store_sectors > 0x400)
  1354. put_unaligned_be32(sdebug_store_sectors, arr + 4);
  1355. /* Optimal Transfer Length */
  1356. put_unaligned_be32(sdebug_opt_blks, &arr[8]);
  1357. if (sdebug_lbpu) {
  1358. /* Maximum Unmap LBA Count */
  1359. put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
  1360. /* Maximum Unmap Block Descriptor Count */
  1361. put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
  1362. }
  1363. /* Unmap Granularity Alignment */
  1364. if (sdebug_unmap_alignment) {
  1365. put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
  1366. arr[28] |= 0x80; /* UGAVALID */
  1367. }
  1368. /* Optimal Unmap Granularity */
  1369. put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
  1370. /* Maximum WRITE SAME Length */
  1371. put_unaligned_be64(sdebug_write_same_length, &arr[32]);
  1372. return 0x3c; /* Mandatory page length for Logical Block Provisioning */
  1373. return sizeof(vpdb0_data);
  1374. }
  1375. /* Block device characteristics VPD page (SBC-3) */
  1376. static int inquiry_vpd_b1(struct sdebug_dev_info *devip, unsigned char *arr)
  1377. {
  1378. memset(arr, 0, 0x3c);
  1379. arr[0] = 0;
  1380. arr[1] = 1; /* non rotating medium (e.g. solid state) */
  1381. arr[2] = 0;
  1382. arr[3] = 5; /* less than 1.8" */
  1383. if (devip->zmodel == BLK_ZONED_HA)
  1384. arr[4] = 1 << 4; /* zoned field = 01b */
  1385. return 0x3c;
  1386. }
  1387. /* Logical block provisioning VPD page (SBC-4) */
  1388. static int inquiry_vpd_b2(unsigned char *arr)
  1389. {
  1390. memset(arr, 0, 0x4);
  1391. arr[0] = 0; /* threshold exponent */
  1392. if (sdebug_lbpu)
  1393. arr[1] = 1 << 7;
  1394. if (sdebug_lbpws)
  1395. arr[1] |= 1 << 6;
  1396. if (sdebug_lbpws10)
  1397. arr[1] |= 1 << 5;
  1398. if (sdebug_lbprz && scsi_debug_lbp())
  1399. arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */
  1400. /* anc_sup=0; dp=0 (no provisioning group descriptor) */
  1401. /* minimum_percentage=0; provisioning_type=0 (unknown) */
  1402. /* threshold_percentage=0 */
  1403. return 0x4;
  1404. }
  1405. /* Zoned block device characteristics VPD page (ZBC mandatory) */
  1406. static int inquiry_vpd_b6(struct sdebug_dev_info *devip, unsigned char *arr)
  1407. {
  1408. memset(arr, 0, 0x3c);
  1409. arr[0] = 0x1; /* set URSWRZ (unrestricted read in seq. wr req zone) */
  1410. /*
  1411. * Set Optimal number of open sequential write preferred zones and
  1412. * Optimal number of non-sequentially written sequential write
  1413. * preferred zones fields to 'not reported' (0xffffffff). Leave other
  1414. * fields set to zero, apart from Max. number of open swrz_s field.
  1415. */
  1416. put_unaligned_be32(0xffffffff, &arr[4]);
  1417. put_unaligned_be32(0xffffffff, &arr[8]);
  1418. if (sdeb_zbc_model == BLK_ZONED_HM && devip->max_open)
  1419. put_unaligned_be32(devip->max_open, &arr[12]);
  1420. else
  1421. put_unaligned_be32(0xffffffff, &arr[12]);
  1422. if (devip->zcap < devip->zsize) {
  1423. arr[19] = ZBC_CONSTANT_ZONE_START_OFFSET;
  1424. put_unaligned_be64(devip->zsize, &arr[20]);
  1425. } else {
  1426. arr[19] = 0;
  1427. }
  1428. return 0x3c;
  1429. }
  1430. #define SDEBUG_LONG_INQ_SZ 96
  1431. #define SDEBUG_MAX_INQ_ARR_SZ 584
  1432. static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  1433. {
  1434. unsigned char pq_pdt;
  1435. unsigned char *arr;
  1436. unsigned char *cmd = scp->cmnd;
  1437. u32 alloc_len, n;
  1438. int ret;
  1439. bool have_wlun, is_disk, is_zbc, is_disk_zbc;
  1440. alloc_len = get_unaligned_be16(cmd + 3);
  1441. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  1442. if (! arr)
  1443. return DID_REQUEUE << 16;
  1444. is_disk = (sdebug_ptype == TYPE_DISK);
  1445. is_zbc = (devip->zmodel != BLK_ZONED_NONE);
  1446. is_disk_zbc = (is_disk || is_zbc);
  1447. have_wlun = scsi_is_wlun(scp->device->lun);
  1448. if (have_wlun)
  1449. pq_pdt = TYPE_WLUN; /* present, wlun */
  1450. else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
  1451. pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */
  1452. else
  1453. pq_pdt = (sdebug_ptype & 0x1f);
  1454. arr[0] = pq_pdt;
  1455. if (0x2 & cmd[1]) { /* CMDDT bit set */
  1456. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
  1457. kfree(arr);
  1458. return check_condition_result;
  1459. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  1460. int lu_id_num, port_group_id, target_dev_id;
  1461. u32 len;
  1462. char lu_id_str[6];
  1463. int host_no = devip->sdbg_host->shost->host_no;
  1464. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  1465. (devip->channel & 0x7f);
  1466. if (sdebug_vpd_use_hostno == 0)
  1467. host_no = 0;
  1468. lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
  1469. (devip->target * 1000) + devip->lun);
  1470. target_dev_id = ((host_no + 1) * 2000) +
  1471. (devip->target * 1000) - 3;
  1472. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  1473. if (0 == cmd[2]) { /* supported vital product data pages */
  1474. arr[1] = cmd[2]; /*sanity */
  1475. n = 4;
  1476. arr[n++] = 0x0; /* this page */
  1477. arr[n++] = 0x80; /* unit serial number */
  1478. arr[n++] = 0x83; /* device identification */
  1479. arr[n++] = 0x84; /* software interface ident. */
  1480. arr[n++] = 0x85; /* management network addresses */
  1481. arr[n++] = 0x86; /* extended inquiry */
  1482. arr[n++] = 0x87; /* mode page policy */
  1483. arr[n++] = 0x88; /* SCSI ports */
  1484. if (is_disk_zbc) { /* SBC or ZBC */
  1485. arr[n++] = 0x89; /* ATA information */
  1486. arr[n++] = 0xb0; /* Block limits */
  1487. arr[n++] = 0xb1; /* Block characteristics */
  1488. if (is_disk)
  1489. arr[n++] = 0xb2; /* LB Provisioning */
  1490. if (is_zbc)
  1491. arr[n++] = 0xb6; /* ZB dev. char. */
  1492. }
  1493. arr[3] = n - 4; /* number of supported VPD pages */
  1494. } else if (0x80 == cmd[2]) { /* unit serial number */
  1495. arr[1] = cmd[2]; /*sanity */
  1496. arr[3] = len;
  1497. memcpy(&arr[4], lu_id_str, len);
  1498. } else if (0x83 == cmd[2]) { /* device identification */
  1499. arr[1] = cmd[2]; /*sanity */
  1500. arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
  1501. target_dev_id, lu_id_num,
  1502. lu_id_str, len,
  1503. &devip->lu_name);
  1504. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  1505. arr[1] = cmd[2]; /*sanity */
  1506. arr[3] = inquiry_vpd_84(&arr[4]);
  1507. } else if (0x85 == cmd[2]) { /* Management network addresses */
  1508. arr[1] = cmd[2]; /*sanity */
  1509. arr[3] = inquiry_vpd_85(&arr[4]);
  1510. } else if (0x86 == cmd[2]) { /* extended inquiry */
  1511. arr[1] = cmd[2]; /*sanity */
  1512. arr[3] = 0x3c; /* number of following entries */
  1513. if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
  1514. arr[4] = 0x4; /* SPT: GRD_CHK:1 */
  1515. else if (have_dif_prot)
  1516. arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
  1517. else
  1518. arr[4] = 0x0; /* no protection stuff */
  1519. arr[5] = 0x7; /* head of q, ordered + simple q's */
  1520. } else if (0x87 == cmd[2]) { /* mode page policy */
  1521. arr[1] = cmd[2]; /*sanity */
  1522. arr[3] = 0x8; /* number of following entries */
  1523. arr[4] = 0x2; /* disconnect-reconnect mp */
  1524. arr[6] = 0x80; /* mlus, shared */
  1525. arr[8] = 0x18; /* protocol specific lu */
  1526. arr[10] = 0x82; /* mlus, per initiator port */
  1527. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  1528. arr[1] = cmd[2]; /*sanity */
  1529. arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
  1530. } else if (is_disk_zbc && 0x89 == cmd[2]) { /* ATA info */
  1531. arr[1] = cmd[2]; /*sanity */
  1532. n = inquiry_vpd_89(&arr[4]);
  1533. put_unaligned_be16(n, arr + 2);
  1534. } else if (is_disk_zbc && 0xb0 == cmd[2]) { /* Block limits */
  1535. arr[1] = cmd[2]; /*sanity */
  1536. arr[3] = inquiry_vpd_b0(&arr[4]);
  1537. } else if (is_disk_zbc && 0xb1 == cmd[2]) { /* Block char. */
  1538. arr[1] = cmd[2]; /*sanity */
  1539. arr[3] = inquiry_vpd_b1(devip, &arr[4]);
  1540. } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
  1541. arr[1] = cmd[2]; /*sanity */
  1542. arr[3] = inquiry_vpd_b2(&arr[4]);
  1543. } else if (is_zbc && cmd[2] == 0xb6) { /* ZB dev. charact. */
  1544. arr[1] = cmd[2]; /*sanity */
  1545. arr[3] = inquiry_vpd_b6(devip, &arr[4]);
  1546. } else {
  1547. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  1548. kfree(arr);
  1549. return check_condition_result;
  1550. }
  1551. len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
  1552. ret = fill_from_dev_buffer(scp, arr,
  1553. min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
  1554. kfree(arr);
  1555. return ret;
  1556. }
  1557. /* drops through here for a standard inquiry */
  1558. arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */
  1559. arr[2] = sdebug_scsi_level;
  1560. arr[3] = 2; /* response_data_format==2 */
  1561. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  1562. arr[5] = (int)have_dif_prot; /* PROTECT bit */
  1563. if (sdebug_vpd_use_hostno == 0)
  1564. arr[5] |= 0x10; /* claim: implicit TPGS */
  1565. arr[6] = 0x10; /* claim: MultiP */
  1566. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  1567. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  1568. memcpy(&arr[8], sdebug_inq_vendor_id, 8);
  1569. memcpy(&arr[16], sdebug_inq_product_id, 16);
  1570. memcpy(&arr[32], sdebug_inq_product_rev, 4);
  1571. /* Use Vendor Specific area to place driver date in ASCII hex */
  1572. memcpy(&arr[36], sdebug_version_date, 8);
  1573. /* version descriptors (2 bytes each) follow */
  1574. put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */
  1575. put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */
  1576. n = 62;
  1577. if (is_disk) { /* SBC-4 no version claimed */
  1578. put_unaligned_be16(0x600, arr + n);
  1579. n += 2;
  1580. } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */
  1581. put_unaligned_be16(0x525, arr + n);
  1582. n += 2;
  1583. } else if (is_zbc) { /* ZBC BSR INCITS 536 revision 05 */
  1584. put_unaligned_be16(0x624, arr + n);
  1585. n += 2;
  1586. }
  1587. put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */
  1588. ret = fill_from_dev_buffer(scp, arr,
  1589. min_t(u32, alloc_len, SDEBUG_LONG_INQ_SZ));
  1590. kfree(arr);
  1591. return ret;
  1592. }
  1593. /* See resp_iec_m_pg() for how this data is manipulated */
  1594. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1595. 0, 0, 0x0, 0x0};
  1596. static int resp_requests(struct scsi_cmnd *scp,
  1597. struct sdebug_dev_info *devip)
  1598. {
  1599. unsigned char *cmd = scp->cmnd;
  1600. unsigned char arr[SCSI_SENSE_BUFFERSIZE]; /* assume >= 18 bytes */
  1601. bool dsense = !!(cmd[1] & 1);
  1602. u32 alloc_len = cmd[4];
  1603. u32 len = 18;
  1604. int stopped_state = atomic_read(&devip->stopped);
  1605. memset(arr, 0, sizeof(arr));
  1606. if (stopped_state > 0) { /* some "pollable" data [spc6r02: 5.12.2] */
  1607. if (dsense) {
  1608. arr[0] = 0x72;
  1609. arr[1] = NOT_READY;
  1610. arr[2] = LOGICAL_UNIT_NOT_READY;
  1611. arr[3] = (stopped_state == 2) ? 0x1 : 0x2;
  1612. len = 8;
  1613. } else {
  1614. arr[0] = 0x70;
  1615. arr[2] = NOT_READY; /* NO_SENSE in sense_key */
  1616. arr[7] = 0xa; /* 18 byte sense buffer */
  1617. arr[12] = LOGICAL_UNIT_NOT_READY;
  1618. arr[13] = (stopped_state == 2) ? 0x1 : 0x2;
  1619. }
  1620. } else if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  1621. /* Information exceptions control mode page: TEST=1, MRIE=6 */
  1622. if (dsense) {
  1623. arr[0] = 0x72;
  1624. arr[1] = 0x0; /* NO_SENSE in sense_key */
  1625. arr[2] = THRESHOLD_EXCEEDED;
  1626. arr[3] = 0xff; /* Failure prediction(false) */
  1627. len = 8;
  1628. } else {
  1629. arr[0] = 0x70;
  1630. arr[2] = 0x0; /* NO_SENSE in sense_key */
  1631. arr[7] = 0xa; /* 18 byte sense buffer */
  1632. arr[12] = THRESHOLD_EXCEEDED;
  1633. arr[13] = 0xff; /* Failure prediction(false) */
  1634. }
  1635. } else { /* nothing to report */
  1636. if (dsense) {
  1637. len = 8;
  1638. memset(arr, 0, len);
  1639. arr[0] = 0x72;
  1640. } else {
  1641. memset(arr, 0, len);
  1642. arr[0] = 0x70;
  1643. arr[7] = 0xa;
  1644. }
  1645. }
  1646. return fill_from_dev_buffer(scp, arr, min_t(u32, len, alloc_len));
  1647. }
  1648. static int resp_start_stop(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  1649. {
  1650. unsigned char *cmd = scp->cmnd;
  1651. int power_cond, want_stop, stopped_state;
  1652. bool changing;
  1653. power_cond = (cmd[4] & 0xf0) >> 4;
  1654. if (power_cond) {
  1655. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
  1656. return check_condition_result;
  1657. }
  1658. want_stop = !(cmd[4] & 1);
  1659. stopped_state = atomic_read(&devip->stopped);
  1660. if (stopped_state == 2) {
  1661. ktime_t now_ts = ktime_get_boottime();
  1662. if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
  1663. u64 diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
  1664. if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
  1665. /* tur_ms_to_ready timer extinguished */
  1666. atomic_set(&devip->stopped, 0);
  1667. stopped_state = 0;
  1668. }
  1669. }
  1670. if (stopped_state == 2) {
  1671. if (want_stop) {
  1672. stopped_state = 1; /* dummy up success */
  1673. } else { /* Disallow tur_ms_to_ready delay to be overridden */
  1674. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 0 /* START bit */);
  1675. return check_condition_result;
  1676. }
  1677. }
  1678. }
  1679. changing = (stopped_state != want_stop);
  1680. if (changing)
  1681. atomic_xchg(&devip->stopped, want_stop);
  1682. if (!changing || (cmd[1] & 0x1)) /* state unchanged or IMMED bit set in cdb */
  1683. return SDEG_RES_IMMED_MASK;
  1684. else
  1685. return 0;
  1686. }
  1687. static sector_t get_sdebug_capacity(void)
  1688. {
  1689. static const unsigned int gibibyte = 1073741824;
  1690. if (sdebug_virtual_gb > 0)
  1691. return (sector_t)sdebug_virtual_gb *
  1692. (gibibyte / sdebug_sector_size);
  1693. else
  1694. return sdebug_store_sectors;
  1695. }
  1696. #define SDEBUG_READCAP_ARR_SZ 8
  1697. static int resp_readcap(struct scsi_cmnd *scp,
  1698. struct sdebug_dev_info *devip)
  1699. {
  1700. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1701. unsigned int capac;
  1702. /* following just in case virtual_gb changed */
  1703. sdebug_capacity = get_sdebug_capacity();
  1704. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1705. if (sdebug_capacity < 0xffffffff) {
  1706. capac = (unsigned int)sdebug_capacity - 1;
  1707. put_unaligned_be32(capac, arr + 0);
  1708. } else
  1709. put_unaligned_be32(0xffffffff, arr + 0);
  1710. put_unaligned_be16(sdebug_sector_size, arr + 6);
  1711. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1712. }
  1713. #define SDEBUG_READCAP16_ARR_SZ 32
  1714. static int resp_readcap16(struct scsi_cmnd *scp,
  1715. struct sdebug_dev_info *devip)
  1716. {
  1717. unsigned char *cmd = scp->cmnd;
  1718. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1719. u32 alloc_len;
  1720. alloc_len = get_unaligned_be32(cmd + 10);
  1721. /* following just in case virtual_gb changed */
  1722. sdebug_capacity = get_sdebug_capacity();
  1723. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1724. put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
  1725. put_unaligned_be32(sdebug_sector_size, arr + 8);
  1726. arr[13] = sdebug_physblk_exp & 0xf;
  1727. arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
  1728. if (scsi_debug_lbp()) {
  1729. arr[14] |= 0x80; /* LBPME */
  1730. /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
  1731. * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
  1732. * in the wider field maps to 0 in this field.
  1733. */
  1734. if (sdebug_lbprz & 1) /* precisely what the draft requires */
  1735. arr[14] |= 0x40;
  1736. }
  1737. /*
  1738. * Since the scsi_debug READ CAPACITY implementation always reports the
  1739. * total disk capacity, set RC BASIS = 1 for host-managed ZBC devices.
  1740. */
  1741. if (devip->zmodel == BLK_ZONED_HM)
  1742. arr[12] |= 1 << 4;
  1743. arr[15] = sdebug_lowest_aligned & 0xff;
  1744. if (have_dif_prot) {
  1745. arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
  1746. arr[12] |= 1; /* PROT_EN */
  1747. }
  1748. return fill_from_dev_buffer(scp, arr,
  1749. min_t(u32, alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1750. }
  1751. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1752. static int resp_report_tgtpgs(struct scsi_cmnd *scp,
  1753. struct sdebug_dev_info *devip)
  1754. {
  1755. unsigned char *cmd = scp->cmnd;
  1756. unsigned char *arr;
  1757. int host_no = devip->sdbg_host->shost->host_no;
  1758. int port_group_a, port_group_b, port_a, port_b;
  1759. u32 alen, n, rlen;
  1760. int ret;
  1761. alen = get_unaligned_be32(cmd + 6);
  1762. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  1763. if (! arr)
  1764. return DID_REQUEUE << 16;
  1765. /*
  1766. * EVPD page 0x88 states we have two ports, one
  1767. * real and a fake port with no device connected.
  1768. * So we create two port groups with one port each
  1769. * and set the group with port B to unavailable.
  1770. */
  1771. port_a = 0x1; /* relative port A */
  1772. port_b = 0x2; /* relative port B */
  1773. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1774. (devip->channel & 0x7f);
  1775. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1776. (devip->channel & 0x7f) + 0x80;
  1777. /*
  1778. * The asymmetric access state is cycled according to the host_id.
  1779. */
  1780. n = 4;
  1781. if (sdebug_vpd_use_hostno == 0) {
  1782. arr[n++] = host_no % 3; /* Asymm access state */
  1783. arr[n++] = 0x0F; /* claim: all states are supported */
  1784. } else {
  1785. arr[n++] = 0x0; /* Active/Optimized path */
  1786. arr[n++] = 0x01; /* only support active/optimized paths */
  1787. }
  1788. put_unaligned_be16(port_group_a, arr + n);
  1789. n += 2;
  1790. arr[n++] = 0; /* Reserved */
  1791. arr[n++] = 0; /* Status code */
  1792. arr[n++] = 0; /* Vendor unique */
  1793. arr[n++] = 0x1; /* One port per group */
  1794. arr[n++] = 0; /* Reserved */
  1795. arr[n++] = 0; /* Reserved */
  1796. put_unaligned_be16(port_a, arr + n);
  1797. n += 2;
  1798. arr[n++] = 3; /* Port unavailable */
  1799. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1800. put_unaligned_be16(port_group_b, arr + n);
  1801. n += 2;
  1802. arr[n++] = 0; /* Reserved */
  1803. arr[n++] = 0; /* Status code */
  1804. arr[n++] = 0; /* Vendor unique */
  1805. arr[n++] = 0x1; /* One port per group */
  1806. arr[n++] = 0; /* Reserved */
  1807. arr[n++] = 0; /* Reserved */
  1808. put_unaligned_be16(port_b, arr + n);
  1809. n += 2;
  1810. rlen = n - 4;
  1811. put_unaligned_be32(rlen, arr + 0);
  1812. /*
  1813. * Return the smallest value of either
  1814. * - The allocated length
  1815. * - The constructed command length
  1816. * - The maximum array size
  1817. */
  1818. rlen = min(alen, n);
  1819. ret = fill_from_dev_buffer(scp, arr,
  1820. min_t(u32, rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1821. kfree(arr);
  1822. return ret;
  1823. }
  1824. static int resp_rsup_opcodes(struct scsi_cmnd *scp,
  1825. struct sdebug_dev_info *devip)
  1826. {
  1827. bool rctd;
  1828. u8 reporting_opts, req_opcode, sdeb_i, supp;
  1829. u16 req_sa, u;
  1830. u32 alloc_len, a_len;
  1831. int k, offset, len, errsts, count, bump, na;
  1832. const struct opcode_info_t *oip;
  1833. const struct opcode_info_t *r_oip;
  1834. u8 *arr;
  1835. u8 *cmd = scp->cmnd;
  1836. rctd = !!(cmd[2] & 0x80);
  1837. reporting_opts = cmd[2] & 0x7;
  1838. req_opcode = cmd[3];
  1839. req_sa = get_unaligned_be16(cmd + 4);
  1840. alloc_len = get_unaligned_be32(cmd + 6);
  1841. if (alloc_len < 4 || alloc_len > 0xffff) {
  1842. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1843. return check_condition_result;
  1844. }
  1845. if (alloc_len > 8192)
  1846. a_len = 8192;
  1847. else
  1848. a_len = alloc_len;
  1849. arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
  1850. if (NULL == arr) {
  1851. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  1852. INSUFF_RES_ASCQ);
  1853. return check_condition_result;
  1854. }
  1855. switch (reporting_opts) {
  1856. case 0: /* all commands */
  1857. /* count number of commands */
  1858. for (count = 0, oip = opcode_info_arr;
  1859. oip->num_attached != 0xff; ++oip) {
  1860. if (F_INV_OP & oip->flags)
  1861. continue;
  1862. count += (oip->num_attached + 1);
  1863. }
  1864. bump = rctd ? 20 : 8;
  1865. put_unaligned_be32(count * bump, arr);
  1866. for (offset = 4, oip = opcode_info_arr;
  1867. oip->num_attached != 0xff && offset < a_len; ++oip) {
  1868. if (F_INV_OP & oip->flags)
  1869. continue;
  1870. na = oip->num_attached;
  1871. arr[offset] = oip->opcode;
  1872. put_unaligned_be16(oip->sa, arr + offset + 2);
  1873. if (rctd)
  1874. arr[offset + 5] |= 0x2;
  1875. if (FF_SA & oip->flags)
  1876. arr[offset + 5] |= 0x1;
  1877. put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
  1878. if (rctd)
  1879. put_unaligned_be16(0xa, arr + offset + 8);
  1880. r_oip = oip;
  1881. for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
  1882. if (F_INV_OP & oip->flags)
  1883. continue;
  1884. offset += bump;
  1885. arr[offset] = oip->opcode;
  1886. put_unaligned_be16(oip->sa, arr + offset + 2);
  1887. if (rctd)
  1888. arr[offset + 5] |= 0x2;
  1889. if (FF_SA & oip->flags)
  1890. arr[offset + 5] |= 0x1;
  1891. put_unaligned_be16(oip->len_mask[0],
  1892. arr + offset + 6);
  1893. if (rctd)
  1894. put_unaligned_be16(0xa,
  1895. arr + offset + 8);
  1896. }
  1897. oip = r_oip;
  1898. offset += bump;
  1899. }
  1900. break;
  1901. case 1: /* one command: opcode only */
  1902. case 2: /* one command: opcode plus service action */
  1903. case 3: /* one command: if sa==0 then opcode only else opcode+sa */
  1904. sdeb_i = opcode_ind_arr[req_opcode];
  1905. oip = &opcode_info_arr[sdeb_i];
  1906. if (F_INV_OP & oip->flags) {
  1907. supp = 1;
  1908. offset = 4;
  1909. } else {
  1910. if (1 == reporting_opts) {
  1911. if (FF_SA & oip->flags) {
  1912. mk_sense_invalid_fld(scp, SDEB_IN_CDB,
  1913. 2, 2);
  1914. kfree(arr);
  1915. return check_condition_result;
  1916. }
  1917. req_sa = 0;
  1918. } else if (2 == reporting_opts &&
  1919. 0 == (FF_SA & oip->flags)) {
  1920. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
  1921. kfree(arr); /* point at requested sa */
  1922. return check_condition_result;
  1923. }
  1924. if (0 == (FF_SA & oip->flags) &&
  1925. req_opcode == oip->opcode)
  1926. supp = 3;
  1927. else if (0 == (FF_SA & oip->flags)) {
  1928. na = oip->num_attached;
  1929. for (k = 0, oip = oip->arrp; k < na;
  1930. ++k, ++oip) {
  1931. if (req_opcode == oip->opcode)
  1932. break;
  1933. }
  1934. supp = (k >= na) ? 1 : 3;
  1935. } else if (req_sa != oip->sa) {
  1936. na = oip->num_attached;
  1937. for (k = 0, oip = oip->arrp; k < na;
  1938. ++k, ++oip) {
  1939. if (req_sa == oip->sa)
  1940. break;
  1941. }
  1942. supp = (k >= na) ? 1 : 3;
  1943. } else
  1944. supp = 3;
  1945. if (3 == supp) {
  1946. u = oip->len_mask[0];
  1947. put_unaligned_be16(u, arr + 2);
  1948. arr[4] = oip->opcode;
  1949. for (k = 1; k < u; ++k)
  1950. arr[4 + k] = (k < 16) ?
  1951. oip->len_mask[k] : 0xff;
  1952. offset = 4 + u;
  1953. } else
  1954. offset = 4;
  1955. }
  1956. arr[1] = (rctd ? 0x80 : 0) | supp;
  1957. if (rctd) {
  1958. put_unaligned_be16(0xa, arr + offset);
  1959. offset += 12;
  1960. }
  1961. break;
  1962. default:
  1963. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
  1964. kfree(arr);
  1965. return check_condition_result;
  1966. }
  1967. offset = (offset < a_len) ? offset : a_len;
  1968. len = (offset < alloc_len) ? offset : alloc_len;
  1969. errsts = fill_from_dev_buffer(scp, arr, len);
  1970. kfree(arr);
  1971. return errsts;
  1972. }
  1973. static int resp_rsup_tmfs(struct scsi_cmnd *scp,
  1974. struct sdebug_dev_info *devip)
  1975. {
  1976. bool repd;
  1977. u32 alloc_len, len;
  1978. u8 arr[16];
  1979. u8 *cmd = scp->cmnd;
  1980. memset(arr, 0, sizeof(arr));
  1981. repd = !!(cmd[2] & 0x80);
  1982. alloc_len = get_unaligned_be32(cmd + 6);
  1983. if (alloc_len < 4) {
  1984. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1985. return check_condition_result;
  1986. }
  1987. arr[0] = 0xc8; /* ATS | ATSS | LURS */
  1988. arr[1] = 0x1; /* ITNRS */
  1989. if (repd) {
  1990. arr[3] = 0xc;
  1991. len = 16;
  1992. } else
  1993. len = 4;
  1994. len = (len < alloc_len) ? len : alloc_len;
  1995. return fill_from_dev_buffer(scp, arr, len);
  1996. }
  1997. /* <<Following mode page info copied from ST318451LW>> */
  1998. static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
  1999. { /* Read-Write Error Recovery page for mode_sense */
  2000. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  2001. 5, 0, 0xff, 0xff};
  2002. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  2003. if (1 == pcontrol)
  2004. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  2005. return sizeof(err_recov_pg);
  2006. }
  2007. static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
  2008. { /* Disconnect-Reconnect page for mode_sense */
  2009. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  2010. 0, 0, 0, 0, 0, 0, 0, 0};
  2011. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  2012. if (1 == pcontrol)
  2013. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  2014. return sizeof(disconnect_pg);
  2015. }
  2016. static int resp_format_pg(unsigned char *p, int pcontrol, int target)
  2017. { /* Format device page for mode_sense */
  2018. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  2019. 0, 0, 0, 0, 0, 0, 0, 0,
  2020. 0, 0, 0, 0, 0x40, 0, 0, 0};
  2021. memcpy(p, format_pg, sizeof(format_pg));
  2022. put_unaligned_be16(sdebug_sectors_per, p + 10);
  2023. put_unaligned_be16(sdebug_sector_size, p + 12);
  2024. if (sdebug_removable)
  2025. p[20] |= 0x20; /* should agree with INQUIRY */
  2026. if (1 == pcontrol)
  2027. memset(p + 2, 0, sizeof(format_pg) - 2);
  2028. return sizeof(format_pg);
  2029. }
  2030. static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  2031. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
  2032. 0, 0, 0, 0};
  2033. static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
  2034. { /* Caching page for mode_sense */
  2035. unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
  2036. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  2037. unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  2038. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  2039. if (SDEBUG_OPT_N_WCE & sdebug_opts)
  2040. caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
  2041. memcpy(p, caching_pg, sizeof(caching_pg));
  2042. if (1 == pcontrol)
  2043. memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
  2044. else if (2 == pcontrol)
  2045. memcpy(p, d_caching_pg, sizeof(d_caching_pg));
  2046. return sizeof(caching_pg);
  2047. }
  2048. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  2049. 0, 0, 0x2, 0x4b};
  2050. static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
  2051. { /* Control mode page for mode_sense */
  2052. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  2053. 0, 0, 0, 0};
  2054. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  2055. 0, 0, 0x2, 0x4b};
  2056. if (sdebug_dsense)
  2057. ctrl_m_pg[2] |= 0x4;
  2058. else
  2059. ctrl_m_pg[2] &= ~0x4;
  2060. if (sdebug_ato)
  2061. ctrl_m_pg[5] |= 0x80; /* ATO=1 */
  2062. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  2063. if (1 == pcontrol)
  2064. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  2065. else if (2 == pcontrol)
  2066. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  2067. return sizeof(ctrl_m_pg);
  2068. }
  2069. static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
  2070. { /* Informational Exceptions control mode page for mode_sense */
  2071. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  2072. 0, 0, 0x0, 0x0};
  2073. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  2074. 0, 0, 0x0, 0x0};
  2075. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  2076. if (1 == pcontrol)
  2077. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  2078. else if (2 == pcontrol)
  2079. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  2080. return sizeof(iec_m_pg);
  2081. }
  2082. static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
  2083. { /* SAS SSP mode page - short format for mode_sense */
  2084. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  2085. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  2086. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  2087. if (1 == pcontrol)
  2088. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  2089. return sizeof(sas_sf_m_pg);
  2090. }
  2091. static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
  2092. int target_dev_id)
  2093. { /* SAS phy control and discover mode page for mode_sense */
  2094. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  2095. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  2096. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  2097. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  2098. 0x2, 0, 0, 0, 0, 0, 0, 0,
  2099. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  2100. 0, 0, 0, 0, 0, 0, 0, 0,
  2101. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  2102. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  2103. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  2104. 0x3, 0, 0, 0, 0, 0, 0, 0,
  2105. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  2106. 0, 0, 0, 0, 0, 0, 0, 0,
  2107. };
  2108. int port_a, port_b;
  2109. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
  2110. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
  2111. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
  2112. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
  2113. port_a = target_dev_id + 1;
  2114. port_b = port_a + 1;
  2115. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  2116. put_unaligned_be32(port_a, p + 20);
  2117. put_unaligned_be32(port_b, p + 48 + 20);
  2118. if (1 == pcontrol)
  2119. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  2120. return sizeof(sas_pcd_m_pg);
  2121. }
  2122. static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
  2123. { /* SAS SSP shared protocol specific port mode subpage */
  2124. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  2125. 0, 0, 0, 0, 0, 0, 0, 0,
  2126. };
  2127. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  2128. if (1 == pcontrol)
  2129. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  2130. return sizeof(sas_sha_m_pg);
  2131. }
  2132. #define SDEBUG_MAX_MSENSE_SZ 256
  2133. static int resp_mode_sense(struct scsi_cmnd *scp,
  2134. struct sdebug_dev_info *devip)
  2135. {
  2136. int pcontrol, pcode, subpcode, bd_len;
  2137. unsigned char dev_spec;
  2138. u32 alloc_len, offset, len;
  2139. int target_dev_id;
  2140. int target = scp->device->id;
  2141. unsigned char *ap;
  2142. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  2143. unsigned char *cmd = scp->cmnd;
  2144. bool dbd, llbaa, msense_6, is_disk, is_zbc, bad_pcode;
  2145. dbd = !!(cmd[1] & 0x8); /* disable block descriptors */
  2146. pcontrol = (cmd[2] & 0xc0) >> 6;
  2147. pcode = cmd[2] & 0x3f;
  2148. subpcode = cmd[3];
  2149. msense_6 = (MODE_SENSE == cmd[0]);
  2150. llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
  2151. is_disk = (sdebug_ptype == TYPE_DISK);
  2152. is_zbc = (devip->zmodel != BLK_ZONED_NONE);
  2153. if ((is_disk || is_zbc) && !dbd)
  2154. bd_len = llbaa ? 16 : 8;
  2155. else
  2156. bd_len = 0;
  2157. alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  2158. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  2159. if (0x3 == pcontrol) { /* Saving values not supported */
  2160. mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
  2161. return check_condition_result;
  2162. }
  2163. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  2164. (devip->target * 1000) - 3;
  2165. /* for disks+zbc set DPOFUA bit and clear write protect (WP) bit */
  2166. if (is_disk || is_zbc) {
  2167. dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */
  2168. if (sdebug_wp)
  2169. dev_spec |= 0x80;
  2170. } else
  2171. dev_spec = 0x0;
  2172. if (msense_6) {
  2173. arr[2] = dev_spec;
  2174. arr[3] = bd_len;
  2175. offset = 4;
  2176. } else {
  2177. arr[3] = dev_spec;
  2178. if (16 == bd_len)
  2179. arr[4] = 0x1; /* set LONGLBA bit */
  2180. arr[7] = bd_len; /* assume 255 or less */
  2181. offset = 8;
  2182. }
  2183. ap = arr + offset;
  2184. if ((bd_len > 0) && (!sdebug_capacity))
  2185. sdebug_capacity = get_sdebug_capacity();
  2186. if (8 == bd_len) {
  2187. if (sdebug_capacity > 0xfffffffe)
  2188. put_unaligned_be32(0xffffffff, ap + 0);
  2189. else
  2190. put_unaligned_be32(sdebug_capacity, ap + 0);
  2191. put_unaligned_be16(sdebug_sector_size, ap + 6);
  2192. offset += bd_len;
  2193. ap = arr + offset;
  2194. } else if (16 == bd_len) {
  2195. put_unaligned_be64((u64)sdebug_capacity, ap + 0);
  2196. put_unaligned_be32(sdebug_sector_size, ap + 12);
  2197. offset += bd_len;
  2198. ap = arr + offset;
  2199. }
  2200. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  2201. /* TODO: Control Extension page */
  2202. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2203. return check_condition_result;
  2204. }
  2205. bad_pcode = false;
  2206. switch (pcode) {
  2207. case 0x1: /* Read-Write error recovery page, direct access */
  2208. len = resp_err_recov_pg(ap, pcontrol, target);
  2209. offset += len;
  2210. break;
  2211. case 0x2: /* Disconnect-Reconnect page, all devices */
  2212. len = resp_disconnect_pg(ap, pcontrol, target);
  2213. offset += len;
  2214. break;
  2215. case 0x3: /* Format device page, direct access */
  2216. if (is_disk) {
  2217. len = resp_format_pg(ap, pcontrol, target);
  2218. offset += len;
  2219. } else
  2220. bad_pcode = true;
  2221. break;
  2222. case 0x8: /* Caching page, direct access */
  2223. if (is_disk || is_zbc) {
  2224. len = resp_caching_pg(ap, pcontrol, target);
  2225. offset += len;
  2226. } else
  2227. bad_pcode = true;
  2228. break;
  2229. case 0xa: /* Control Mode page, all devices */
  2230. len = resp_ctrl_m_pg(ap, pcontrol, target);
  2231. offset += len;
  2232. break;
  2233. case 0x19: /* if spc==1 then sas phy, control+discover */
  2234. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  2235. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2236. return check_condition_result;
  2237. }
  2238. len = 0;
  2239. if ((0x0 == subpcode) || (0xff == subpcode))
  2240. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2241. if ((0x1 == subpcode) || (0xff == subpcode))
  2242. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  2243. target_dev_id);
  2244. if ((0x2 == subpcode) || (0xff == subpcode))
  2245. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2246. offset += len;
  2247. break;
  2248. case 0x1c: /* Informational Exceptions Mode page, all devices */
  2249. len = resp_iec_m_pg(ap, pcontrol, target);
  2250. offset += len;
  2251. break;
  2252. case 0x3f: /* Read all Mode pages */
  2253. if ((0 == subpcode) || (0xff == subpcode)) {
  2254. len = resp_err_recov_pg(ap, pcontrol, target);
  2255. len += resp_disconnect_pg(ap + len, pcontrol, target);
  2256. if (is_disk) {
  2257. len += resp_format_pg(ap + len, pcontrol,
  2258. target);
  2259. len += resp_caching_pg(ap + len, pcontrol,
  2260. target);
  2261. } else if (is_zbc) {
  2262. len += resp_caching_pg(ap + len, pcontrol,
  2263. target);
  2264. }
  2265. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  2266. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2267. if (0xff == subpcode) {
  2268. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  2269. target, target_dev_id);
  2270. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2271. }
  2272. len += resp_iec_m_pg(ap + len, pcontrol, target);
  2273. offset += len;
  2274. } else {
  2275. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2276. return check_condition_result;
  2277. }
  2278. break;
  2279. default:
  2280. bad_pcode = true;
  2281. break;
  2282. }
  2283. if (bad_pcode) {
  2284. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2285. return check_condition_result;
  2286. }
  2287. if (msense_6)
  2288. arr[0] = offset - 1;
  2289. else
  2290. put_unaligned_be16((offset - 2), arr + 0);
  2291. return fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, offset));
  2292. }
  2293. #define SDEBUG_MAX_MSELECT_SZ 512
  2294. static int resp_mode_select(struct scsi_cmnd *scp,
  2295. struct sdebug_dev_info *devip)
  2296. {
  2297. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  2298. int param_len, res, mpage;
  2299. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  2300. unsigned char *cmd = scp->cmnd;
  2301. int mselect6 = (MODE_SELECT == cmd[0]);
  2302. memset(arr, 0, sizeof(arr));
  2303. pf = cmd[1] & 0x10;
  2304. sp = cmd[1] & 0x1;
  2305. param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  2306. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  2307. mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
  2308. return check_condition_result;
  2309. }
  2310. res = fetch_to_dev_buffer(scp, arr, param_len);
  2311. if (-1 == res)
  2312. return DID_ERROR << 16;
  2313. else if (sdebug_verbose && (res < param_len))
  2314. sdev_printk(KERN_INFO, scp->device,
  2315. "%s: cdb indicated=%d, IO sent=%d bytes\n",
  2316. __func__, param_len, res);
  2317. md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
  2318. bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
  2319. off = bd_len + (mselect6 ? 4 : 8);
  2320. if (md_len > 2 || off >= res) {
  2321. mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
  2322. return check_condition_result;
  2323. }
  2324. mpage = arr[off] & 0x3f;
  2325. ps = !!(arr[off] & 0x80);
  2326. if (ps) {
  2327. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
  2328. return check_condition_result;
  2329. }
  2330. spf = !!(arr[off] & 0x40);
  2331. pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
  2332. (arr[off + 1] + 2);
  2333. if ((pg_len + off) > param_len) {
  2334. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2335. PARAMETER_LIST_LENGTH_ERR, 0);
  2336. return check_condition_result;
  2337. }
  2338. switch (mpage) {
  2339. case 0x8: /* Caching Mode page */
  2340. if (caching_pg[1] == arr[off + 1]) {
  2341. memcpy(caching_pg + 2, arr + off + 2,
  2342. sizeof(caching_pg) - 2);
  2343. goto set_mode_changed_ua;
  2344. }
  2345. break;
  2346. case 0xa: /* Control Mode page */
  2347. if (ctrl_m_pg[1] == arr[off + 1]) {
  2348. memcpy(ctrl_m_pg + 2, arr + off + 2,
  2349. sizeof(ctrl_m_pg) - 2);
  2350. if (ctrl_m_pg[4] & 0x8)
  2351. sdebug_wp = true;
  2352. else
  2353. sdebug_wp = false;
  2354. sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
  2355. goto set_mode_changed_ua;
  2356. }
  2357. break;
  2358. case 0x1c: /* Informational Exceptions Mode page */
  2359. if (iec_m_pg[1] == arr[off + 1]) {
  2360. memcpy(iec_m_pg + 2, arr + off + 2,
  2361. sizeof(iec_m_pg) - 2);
  2362. goto set_mode_changed_ua;
  2363. }
  2364. break;
  2365. default:
  2366. break;
  2367. }
  2368. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
  2369. return check_condition_result;
  2370. set_mode_changed_ua:
  2371. set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
  2372. return 0;
  2373. }
  2374. static int resp_temp_l_pg(unsigned char *arr)
  2375. {
  2376. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  2377. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  2378. };
  2379. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  2380. return sizeof(temp_l_pg);
  2381. }
  2382. static int resp_ie_l_pg(unsigned char *arr)
  2383. {
  2384. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  2385. };
  2386. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  2387. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  2388. arr[4] = THRESHOLD_EXCEEDED;
  2389. arr[5] = 0xff;
  2390. }
  2391. return sizeof(ie_l_pg);
  2392. }
  2393. static int resp_env_rep_l_spg(unsigned char *arr)
  2394. {
  2395. unsigned char env_rep_l_spg[] = {0x0, 0x0, 0x23, 0x8,
  2396. 0x0, 40, 72, 0xff, 45, 18, 0, 0,
  2397. 0x1, 0x0, 0x23, 0x8,
  2398. 0x0, 55, 72, 35, 55, 45, 0, 0,
  2399. };
  2400. memcpy(arr, env_rep_l_spg, sizeof(env_rep_l_spg));
  2401. return sizeof(env_rep_l_spg);
  2402. }
  2403. #define SDEBUG_MAX_LSENSE_SZ 512
  2404. static int resp_log_sense(struct scsi_cmnd *scp,
  2405. struct sdebug_dev_info *devip)
  2406. {
  2407. int ppc, sp, pcode, subpcode;
  2408. u32 alloc_len, len, n;
  2409. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  2410. unsigned char *cmd = scp->cmnd;
  2411. memset(arr, 0, sizeof(arr));
  2412. ppc = cmd[1] & 0x2;
  2413. sp = cmd[1] & 0x1;
  2414. if (ppc || sp) {
  2415. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
  2416. return check_condition_result;
  2417. }
  2418. pcode = cmd[2] & 0x3f;
  2419. subpcode = cmd[3] & 0xff;
  2420. alloc_len = get_unaligned_be16(cmd + 7);
  2421. arr[0] = pcode;
  2422. if (0 == subpcode) {
  2423. switch (pcode) {
  2424. case 0x0: /* Supported log pages log page */
  2425. n = 4;
  2426. arr[n++] = 0x0; /* this page */
  2427. arr[n++] = 0xd; /* Temperature */
  2428. arr[n++] = 0x2f; /* Informational exceptions */
  2429. arr[3] = n - 4;
  2430. break;
  2431. case 0xd: /* Temperature log page */
  2432. arr[3] = resp_temp_l_pg(arr + 4);
  2433. break;
  2434. case 0x2f: /* Informational exceptions log page */
  2435. arr[3] = resp_ie_l_pg(arr + 4);
  2436. break;
  2437. default:
  2438. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2439. return check_condition_result;
  2440. }
  2441. } else if (0xff == subpcode) {
  2442. arr[0] |= 0x40;
  2443. arr[1] = subpcode;
  2444. switch (pcode) {
  2445. case 0x0: /* Supported log pages and subpages log page */
  2446. n = 4;
  2447. arr[n++] = 0x0;
  2448. arr[n++] = 0x0; /* 0,0 page */
  2449. arr[n++] = 0x0;
  2450. arr[n++] = 0xff; /* this page */
  2451. arr[n++] = 0xd;
  2452. arr[n++] = 0x0; /* Temperature */
  2453. arr[n++] = 0xd;
  2454. arr[n++] = 0x1; /* Environment reporting */
  2455. arr[n++] = 0xd;
  2456. arr[n++] = 0xff; /* all 0xd subpages */
  2457. arr[n++] = 0x2f;
  2458. arr[n++] = 0x0; /* Informational exceptions */
  2459. arr[n++] = 0x2f;
  2460. arr[n++] = 0xff; /* all 0x2f subpages */
  2461. arr[3] = n - 4;
  2462. break;
  2463. case 0xd: /* Temperature subpages */
  2464. n = 4;
  2465. arr[n++] = 0xd;
  2466. arr[n++] = 0x0; /* Temperature */
  2467. arr[n++] = 0xd;
  2468. arr[n++] = 0x1; /* Environment reporting */
  2469. arr[n++] = 0xd;
  2470. arr[n++] = 0xff; /* these subpages */
  2471. arr[3] = n - 4;
  2472. break;
  2473. case 0x2f: /* Informational exceptions subpages */
  2474. n = 4;
  2475. arr[n++] = 0x2f;
  2476. arr[n++] = 0x0; /* Informational exceptions */
  2477. arr[n++] = 0x2f;
  2478. arr[n++] = 0xff; /* these subpages */
  2479. arr[3] = n - 4;
  2480. break;
  2481. default:
  2482. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2483. return check_condition_result;
  2484. }
  2485. } else if (subpcode > 0) {
  2486. arr[0] |= 0x40;
  2487. arr[1] = subpcode;
  2488. if (pcode == 0xd && subpcode == 1)
  2489. arr[3] = resp_env_rep_l_spg(arr + 4);
  2490. else {
  2491. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2492. return check_condition_result;
  2493. }
  2494. } else {
  2495. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2496. return check_condition_result;
  2497. }
  2498. len = min_t(u32, get_unaligned_be16(arr + 2) + 4, alloc_len);
  2499. return fill_from_dev_buffer(scp, arr,
  2500. min_t(u32, len, SDEBUG_MAX_INQ_ARR_SZ));
  2501. }
  2502. static inline bool sdebug_dev_is_zoned(struct sdebug_dev_info *devip)
  2503. {
  2504. return devip->nr_zones != 0;
  2505. }
  2506. static struct sdeb_zone_state *zbc_zone(struct sdebug_dev_info *devip,
  2507. unsigned long long lba)
  2508. {
  2509. u32 zno = lba >> devip->zsize_shift;
  2510. struct sdeb_zone_state *zsp;
  2511. if (devip->zcap == devip->zsize || zno < devip->nr_conv_zones)
  2512. return &devip->zstate[zno];
  2513. /*
  2514. * If the zone capacity is less than the zone size, adjust for gap
  2515. * zones.
  2516. */
  2517. zno = 2 * zno - devip->nr_conv_zones;
  2518. WARN_ONCE(zno >= devip->nr_zones, "%u > %u\n", zno, devip->nr_zones);
  2519. zsp = &devip->zstate[zno];
  2520. if (lba >= zsp->z_start + zsp->z_size)
  2521. zsp++;
  2522. WARN_ON_ONCE(lba >= zsp->z_start + zsp->z_size);
  2523. return zsp;
  2524. }
  2525. static inline bool zbc_zone_is_conv(struct sdeb_zone_state *zsp)
  2526. {
  2527. return zsp->z_type == ZBC_ZTYPE_CNV;
  2528. }
  2529. static inline bool zbc_zone_is_gap(struct sdeb_zone_state *zsp)
  2530. {
  2531. return zsp->z_type == ZBC_ZTYPE_GAP;
  2532. }
  2533. static inline bool zbc_zone_is_seq(struct sdeb_zone_state *zsp)
  2534. {
  2535. return !zbc_zone_is_conv(zsp) && !zbc_zone_is_gap(zsp);
  2536. }
  2537. static void zbc_close_zone(struct sdebug_dev_info *devip,
  2538. struct sdeb_zone_state *zsp)
  2539. {
  2540. enum sdebug_z_cond zc;
  2541. if (!zbc_zone_is_seq(zsp))
  2542. return;
  2543. zc = zsp->z_cond;
  2544. if (!(zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN))
  2545. return;
  2546. if (zc == ZC2_IMPLICIT_OPEN)
  2547. devip->nr_imp_open--;
  2548. else
  2549. devip->nr_exp_open--;
  2550. if (zsp->z_wp == zsp->z_start) {
  2551. zsp->z_cond = ZC1_EMPTY;
  2552. } else {
  2553. zsp->z_cond = ZC4_CLOSED;
  2554. devip->nr_closed++;
  2555. }
  2556. }
  2557. static void zbc_close_imp_open_zone(struct sdebug_dev_info *devip)
  2558. {
  2559. struct sdeb_zone_state *zsp = &devip->zstate[0];
  2560. unsigned int i;
  2561. for (i = 0; i < devip->nr_zones; i++, zsp++) {
  2562. if (zsp->z_cond == ZC2_IMPLICIT_OPEN) {
  2563. zbc_close_zone(devip, zsp);
  2564. return;
  2565. }
  2566. }
  2567. }
  2568. static void zbc_open_zone(struct sdebug_dev_info *devip,
  2569. struct sdeb_zone_state *zsp, bool explicit)
  2570. {
  2571. enum sdebug_z_cond zc;
  2572. if (!zbc_zone_is_seq(zsp))
  2573. return;
  2574. zc = zsp->z_cond;
  2575. if ((explicit && zc == ZC3_EXPLICIT_OPEN) ||
  2576. (!explicit && zc == ZC2_IMPLICIT_OPEN))
  2577. return;
  2578. /* Close an implicit open zone if necessary */
  2579. if (explicit && zsp->z_cond == ZC2_IMPLICIT_OPEN)
  2580. zbc_close_zone(devip, zsp);
  2581. else if (devip->max_open &&
  2582. devip->nr_imp_open + devip->nr_exp_open >= devip->max_open)
  2583. zbc_close_imp_open_zone(devip);
  2584. if (zsp->z_cond == ZC4_CLOSED)
  2585. devip->nr_closed--;
  2586. if (explicit) {
  2587. zsp->z_cond = ZC3_EXPLICIT_OPEN;
  2588. devip->nr_exp_open++;
  2589. } else {
  2590. zsp->z_cond = ZC2_IMPLICIT_OPEN;
  2591. devip->nr_imp_open++;
  2592. }
  2593. }
  2594. static inline void zbc_set_zone_full(struct sdebug_dev_info *devip,
  2595. struct sdeb_zone_state *zsp)
  2596. {
  2597. switch (zsp->z_cond) {
  2598. case ZC2_IMPLICIT_OPEN:
  2599. devip->nr_imp_open--;
  2600. break;
  2601. case ZC3_EXPLICIT_OPEN:
  2602. devip->nr_exp_open--;
  2603. break;
  2604. default:
  2605. WARN_ONCE(true, "Invalid zone %llu condition %x\n",
  2606. zsp->z_start, zsp->z_cond);
  2607. break;
  2608. }
  2609. zsp->z_cond = ZC5_FULL;
  2610. }
  2611. static void zbc_inc_wp(struct sdebug_dev_info *devip,
  2612. unsigned long long lba, unsigned int num)
  2613. {
  2614. struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
  2615. unsigned long long n, end, zend = zsp->z_start + zsp->z_size;
  2616. if (!zbc_zone_is_seq(zsp))
  2617. return;
  2618. if (zsp->z_type == ZBC_ZTYPE_SWR) {
  2619. zsp->z_wp += num;
  2620. if (zsp->z_wp >= zend)
  2621. zbc_set_zone_full(devip, zsp);
  2622. return;
  2623. }
  2624. while (num) {
  2625. if (lba != zsp->z_wp)
  2626. zsp->z_non_seq_resource = true;
  2627. end = lba + num;
  2628. if (end >= zend) {
  2629. n = zend - lba;
  2630. zsp->z_wp = zend;
  2631. } else if (end > zsp->z_wp) {
  2632. n = num;
  2633. zsp->z_wp = end;
  2634. } else {
  2635. n = num;
  2636. }
  2637. if (zsp->z_wp >= zend)
  2638. zbc_set_zone_full(devip, zsp);
  2639. num -= n;
  2640. lba += n;
  2641. if (num) {
  2642. zsp++;
  2643. zend = zsp->z_start + zsp->z_size;
  2644. }
  2645. }
  2646. }
  2647. static int check_zbc_access_params(struct scsi_cmnd *scp,
  2648. unsigned long long lba, unsigned int num, bool write)
  2649. {
  2650. struct scsi_device *sdp = scp->device;
  2651. struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
  2652. struct sdeb_zone_state *zsp = zbc_zone(devip, lba);
  2653. struct sdeb_zone_state *zsp_end = zbc_zone(devip, lba + num - 1);
  2654. if (!write) {
  2655. if (devip->zmodel == BLK_ZONED_HA)
  2656. return 0;
  2657. /* For host-managed, reads cannot cross zone types boundaries */
  2658. if (zsp->z_type != zsp_end->z_type) {
  2659. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2660. LBA_OUT_OF_RANGE,
  2661. READ_INVDATA_ASCQ);
  2662. return check_condition_result;
  2663. }
  2664. return 0;
  2665. }
  2666. /* Writing into a gap zone is not allowed */
  2667. if (zbc_zone_is_gap(zsp)) {
  2668. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE,
  2669. ATTEMPT_ACCESS_GAP);
  2670. return check_condition_result;
  2671. }
  2672. /* No restrictions for writes within conventional zones */
  2673. if (zbc_zone_is_conv(zsp)) {
  2674. if (!zbc_zone_is_conv(zsp_end)) {
  2675. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2676. LBA_OUT_OF_RANGE,
  2677. WRITE_BOUNDARY_ASCQ);
  2678. return check_condition_result;
  2679. }
  2680. return 0;
  2681. }
  2682. if (zsp->z_type == ZBC_ZTYPE_SWR) {
  2683. /* Writes cannot cross sequential zone boundaries */
  2684. if (zsp_end != zsp) {
  2685. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2686. LBA_OUT_OF_RANGE,
  2687. WRITE_BOUNDARY_ASCQ);
  2688. return check_condition_result;
  2689. }
  2690. /* Cannot write full zones */
  2691. if (zsp->z_cond == ZC5_FULL) {
  2692. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2693. INVALID_FIELD_IN_CDB, 0);
  2694. return check_condition_result;
  2695. }
  2696. /* Writes must be aligned to the zone WP */
  2697. if (lba != zsp->z_wp) {
  2698. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2699. LBA_OUT_OF_RANGE,
  2700. UNALIGNED_WRITE_ASCQ);
  2701. return check_condition_result;
  2702. }
  2703. }
  2704. /* Handle implicit open of closed and empty zones */
  2705. if (zsp->z_cond == ZC1_EMPTY || zsp->z_cond == ZC4_CLOSED) {
  2706. if (devip->max_open &&
  2707. devip->nr_exp_open >= devip->max_open) {
  2708. mk_sense_buffer(scp, DATA_PROTECT,
  2709. INSUFF_RES_ASC,
  2710. INSUFF_ZONE_ASCQ);
  2711. return check_condition_result;
  2712. }
  2713. zbc_open_zone(devip, zsp, false);
  2714. }
  2715. return 0;
  2716. }
  2717. static inline int check_device_access_params
  2718. (struct scsi_cmnd *scp, unsigned long long lba,
  2719. unsigned int num, bool write)
  2720. {
  2721. struct scsi_device *sdp = scp->device;
  2722. struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
  2723. if (lba + num > sdebug_capacity) {
  2724. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2725. return check_condition_result;
  2726. }
  2727. /* transfer length excessive (tie in to block limits VPD page) */
  2728. if (num > sdebug_store_sectors) {
  2729. /* needs work to find which cdb byte 'num' comes from */
  2730. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2731. return check_condition_result;
  2732. }
  2733. if (write && unlikely(sdebug_wp)) {
  2734. mk_sense_buffer(scp, DATA_PROTECT, WRITE_PROTECTED, 0x2);
  2735. return check_condition_result;
  2736. }
  2737. if (sdebug_dev_is_zoned(devip))
  2738. return check_zbc_access_params(scp, lba, num, write);
  2739. return 0;
  2740. }
  2741. /*
  2742. * Note: if BUG_ON() fires it usually indicates a problem with the parser
  2743. * tables. Perhaps a missing F_FAKE_RW or FF_MEDIA_IO flag. Response functions
  2744. * that access any of the "stores" in struct sdeb_store_info should call this
  2745. * function with bug_if_fake_rw set to true.
  2746. */
  2747. static inline struct sdeb_store_info *devip2sip(struct sdebug_dev_info *devip,
  2748. bool bug_if_fake_rw)
  2749. {
  2750. if (sdebug_fake_rw) {
  2751. BUG_ON(bug_if_fake_rw); /* See note above */
  2752. return NULL;
  2753. }
  2754. return xa_load(per_store_ap, devip->sdbg_host->si_idx);
  2755. }
  2756. /* Returns number of bytes copied or -1 if error. */
  2757. static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
  2758. u32 sg_skip, u64 lba, u32 num, bool do_write)
  2759. {
  2760. int ret;
  2761. u64 block, rest = 0;
  2762. enum dma_data_direction dir;
  2763. struct scsi_data_buffer *sdb = &scp->sdb;
  2764. u8 *fsp;
  2765. if (do_write) {
  2766. dir = DMA_TO_DEVICE;
  2767. write_since_sync = true;
  2768. } else {
  2769. dir = DMA_FROM_DEVICE;
  2770. }
  2771. if (!sdb->length || !sip)
  2772. return 0;
  2773. if (scp->sc_data_direction != dir)
  2774. return -1;
  2775. fsp = sip->storep;
  2776. block = do_div(lba, sdebug_store_sectors);
  2777. if (block + num > sdebug_store_sectors)
  2778. rest = block + num - sdebug_store_sectors;
  2779. ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2780. fsp + (block * sdebug_sector_size),
  2781. (num - rest) * sdebug_sector_size, sg_skip, do_write);
  2782. if (ret != (num - rest) * sdebug_sector_size)
  2783. return ret;
  2784. if (rest) {
  2785. ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2786. fsp, rest * sdebug_sector_size,
  2787. sg_skip + ((num - rest) * sdebug_sector_size),
  2788. do_write);
  2789. }
  2790. return ret;
  2791. }
  2792. /* Returns number of bytes copied or -1 if error. */
  2793. static int do_dout_fetch(struct scsi_cmnd *scp, u32 num, u8 *doutp)
  2794. {
  2795. struct scsi_data_buffer *sdb = &scp->sdb;
  2796. if (!sdb->length)
  2797. return 0;
  2798. if (scp->sc_data_direction != DMA_TO_DEVICE)
  2799. return -1;
  2800. return sg_copy_buffer(sdb->table.sgl, sdb->table.nents, doutp,
  2801. num * sdebug_sector_size, 0, true);
  2802. }
  2803. /* If sip->storep+lba compares equal to arr(num), then copy top half of
  2804. * arr into sip->storep+lba and return true. If comparison fails then
  2805. * return false. */
  2806. static bool comp_write_worker(struct sdeb_store_info *sip, u64 lba, u32 num,
  2807. const u8 *arr, bool compare_only)
  2808. {
  2809. bool res;
  2810. u64 block, rest = 0;
  2811. u32 store_blks = sdebug_store_sectors;
  2812. u32 lb_size = sdebug_sector_size;
  2813. u8 *fsp = sip->storep;
  2814. block = do_div(lba, store_blks);
  2815. if (block + num > store_blks)
  2816. rest = block + num - store_blks;
  2817. res = !memcmp(fsp + (block * lb_size), arr, (num - rest) * lb_size);
  2818. if (!res)
  2819. return res;
  2820. if (rest)
  2821. res = memcmp(fsp, arr + ((num - rest) * lb_size),
  2822. rest * lb_size);
  2823. if (!res)
  2824. return res;
  2825. if (compare_only)
  2826. return true;
  2827. arr += num * lb_size;
  2828. memcpy(fsp + (block * lb_size), arr, (num - rest) * lb_size);
  2829. if (rest)
  2830. memcpy(fsp, arr + ((num - rest) * lb_size), rest * lb_size);
  2831. return res;
  2832. }
  2833. static __be16 dif_compute_csum(const void *buf, int len)
  2834. {
  2835. __be16 csum;
  2836. if (sdebug_guard)
  2837. csum = (__force __be16)ip_compute_csum(buf, len);
  2838. else
  2839. csum = cpu_to_be16(crc_t10dif(buf, len));
  2840. return csum;
  2841. }
  2842. static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
  2843. sector_t sector, u32 ei_lba)
  2844. {
  2845. __be16 csum = dif_compute_csum(data, sdebug_sector_size);
  2846. if (sdt->guard_tag != csum) {
  2847. pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
  2848. (unsigned long)sector,
  2849. be16_to_cpu(sdt->guard_tag),
  2850. be16_to_cpu(csum));
  2851. return 0x01;
  2852. }
  2853. if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
  2854. be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  2855. pr_err("REF check failed on sector %lu\n",
  2856. (unsigned long)sector);
  2857. return 0x03;
  2858. }
  2859. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2860. be32_to_cpu(sdt->ref_tag) != ei_lba) {
  2861. pr_err("REF check failed on sector %lu\n",
  2862. (unsigned long)sector);
  2863. return 0x03;
  2864. }
  2865. return 0;
  2866. }
  2867. static void dif_copy_prot(struct scsi_cmnd *scp, sector_t sector,
  2868. unsigned int sectors, bool read)
  2869. {
  2870. size_t resid;
  2871. void *paddr;
  2872. struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
  2873. scp->device->hostdata, true);
  2874. struct t10_pi_tuple *dif_storep = sip->dif_storep;
  2875. const void *dif_store_end = dif_storep + sdebug_store_sectors;
  2876. struct sg_mapping_iter miter;
  2877. /* Bytes of protection data to copy into sgl */
  2878. resid = sectors * sizeof(*dif_storep);
  2879. sg_miter_start(&miter, scsi_prot_sglist(scp),
  2880. scsi_prot_sg_count(scp), SG_MITER_ATOMIC |
  2881. (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
  2882. while (sg_miter_next(&miter) && resid > 0) {
  2883. size_t len = min_t(size_t, miter.length, resid);
  2884. void *start = dif_store(sip, sector);
  2885. size_t rest = 0;
  2886. if (dif_store_end < start + len)
  2887. rest = start + len - dif_store_end;
  2888. paddr = miter.addr;
  2889. if (read)
  2890. memcpy(paddr, start, len - rest);
  2891. else
  2892. memcpy(start, paddr, len - rest);
  2893. if (rest) {
  2894. if (read)
  2895. memcpy(paddr + len - rest, dif_storep, rest);
  2896. else
  2897. memcpy(dif_storep, paddr + len - rest, rest);
  2898. }
  2899. sector += len / sizeof(*dif_storep);
  2900. resid -= len;
  2901. }
  2902. sg_miter_stop(&miter);
  2903. }
  2904. static int prot_verify_read(struct scsi_cmnd *scp, sector_t start_sec,
  2905. unsigned int sectors, u32 ei_lba)
  2906. {
  2907. int ret = 0;
  2908. unsigned int i;
  2909. sector_t sector;
  2910. struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
  2911. scp->device->hostdata, true);
  2912. struct t10_pi_tuple *sdt;
  2913. for (i = 0; i < sectors; i++, ei_lba++) {
  2914. sector = start_sec + i;
  2915. sdt = dif_store(sip, sector);
  2916. if (sdt->app_tag == cpu_to_be16(0xffff))
  2917. continue;
  2918. /*
  2919. * Because scsi_debug acts as both initiator and
  2920. * target we proceed to verify the PI even if
  2921. * RDPROTECT=3. This is done so the "initiator" knows
  2922. * which type of error to return. Otherwise we would
  2923. * have to iterate over the PI twice.
  2924. */
  2925. if (scp->cmnd[1] >> 5) { /* RDPROTECT */
  2926. ret = dif_verify(sdt, lba2fake_store(sip, sector),
  2927. sector, ei_lba);
  2928. if (ret) {
  2929. dif_errors++;
  2930. break;
  2931. }
  2932. }
  2933. }
  2934. dif_copy_prot(scp, start_sec, sectors, true);
  2935. dix_reads++;
  2936. return ret;
  2937. }
  2938. static inline void
  2939. sdeb_read_lock(struct sdeb_store_info *sip)
  2940. {
  2941. if (sdebug_no_rwlock) {
  2942. if (sip)
  2943. __acquire(&sip->macc_lck);
  2944. else
  2945. __acquire(&sdeb_fake_rw_lck);
  2946. } else {
  2947. if (sip)
  2948. read_lock(&sip->macc_lck);
  2949. else
  2950. read_lock(&sdeb_fake_rw_lck);
  2951. }
  2952. }
  2953. static inline void
  2954. sdeb_read_unlock(struct sdeb_store_info *sip)
  2955. {
  2956. if (sdebug_no_rwlock) {
  2957. if (sip)
  2958. __release(&sip->macc_lck);
  2959. else
  2960. __release(&sdeb_fake_rw_lck);
  2961. } else {
  2962. if (sip)
  2963. read_unlock(&sip->macc_lck);
  2964. else
  2965. read_unlock(&sdeb_fake_rw_lck);
  2966. }
  2967. }
  2968. static inline void
  2969. sdeb_write_lock(struct sdeb_store_info *sip)
  2970. {
  2971. if (sdebug_no_rwlock) {
  2972. if (sip)
  2973. __acquire(&sip->macc_lck);
  2974. else
  2975. __acquire(&sdeb_fake_rw_lck);
  2976. } else {
  2977. if (sip)
  2978. write_lock(&sip->macc_lck);
  2979. else
  2980. write_lock(&sdeb_fake_rw_lck);
  2981. }
  2982. }
  2983. static inline void
  2984. sdeb_write_unlock(struct sdeb_store_info *sip)
  2985. {
  2986. if (sdebug_no_rwlock) {
  2987. if (sip)
  2988. __release(&sip->macc_lck);
  2989. else
  2990. __release(&sdeb_fake_rw_lck);
  2991. } else {
  2992. if (sip)
  2993. write_unlock(&sip->macc_lck);
  2994. else
  2995. write_unlock(&sdeb_fake_rw_lck);
  2996. }
  2997. }
  2998. static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  2999. {
  3000. bool check_prot;
  3001. u32 num;
  3002. u32 ei_lba;
  3003. int ret;
  3004. u64 lba;
  3005. struct sdeb_store_info *sip = devip2sip(devip, true);
  3006. u8 *cmd = scp->cmnd;
  3007. switch (cmd[0]) {
  3008. case READ_16:
  3009. ei_lba = 0;
  3010. lba = get_unaligned_be64(cmd + 2);
  3011. num = get_unaligned_be32(cmd + 10);
  3012. check_prot = true;
  3013. break;
  3014. case READ_10:
  3015. ei_lba = 0;
  3016. lba = get_unaligned_be32(cmd + 2);
  3017. num = get_unaligned_be16(cmd + 7);
  3018. check_prot = true;
  3019. break;
  3020. case READ_6:
  3021. ei_lba = 0;
  3022. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  3023. (u32)(cmd[1] & 0x1f) << 16;
  3024. num = (0 == cmd[4]) ? 256 : cmd[4];
  3025. check_prot = true;
  3026. break;
  3027. case READ_12:
  3028. ei_lba = 0;
  3029. lba = get_unaligned_be32(cmd + 2);
  3030. num = get_unaligned_be32(cmd + 6);
  3031. check_prot = true;
  3032. break;
  3033. case XDWRITEREAD_10:
  3034. ei_lba = 0;
  3035. lba = get_unaligned_be32(cmd + 2);
  3036. num = get_unaligned_be16(cmd + 7);
  3037. check_prot = false;
  3038. break;
  3039. default: /* assume READ(32) */
  3040. lba = get_unaligned_be64(cmd + 12);
  3041. ei_lba = get_unaligned_be32(cmd + 20);
  3042. num = get_unaligned_be32(cmd + 28);
  3043. check_prot = false;
  3044. break;
  3045. }
  3046. if (unlikely(have_dif_prot && check_prot)) {
  3047. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3048. (cmd[1] & 0xe0)) {
  3049. mk_sense_invalid_opcode(scp);
  3050. return check_condition_result;
  3051. }
  3052. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3053. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3054. (cmd[1] & 0xe0) == 0)
  3055. sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
  3056. "to DIF device\n");
  3057. }
  3058. if (unlikely((sdebug_opts & SDEBUG_OPT_SHORT_TRANSFER) &&
  3059. atomic_read(&sdeb_inject_pending))) {
  3060. num /= 2;
  3061. atomic_set(&sdeb_inject_pending, 0);
  3062. }
  3063. ret = check_device_access_params(scp, lba, num, false);
  3064. if (ret)
  3065. return ret;
  3066. if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
  3067. (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
  3068. ((lba + num) > sdebug_medium_error_start))) {
  3069. /* claim unrecoverable read error */
  3070. mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
  3071. /* set info field and valid bit for fixed descriptor */
  3072. if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
  3073. scp->sense_buffer[0] |= 0x80; /* Valid bit */
  3074. ret = (lba < OPT_MEDIUM_ERR_ADDR)
  3075. ? OPT_MEDIUM_ERR_ADDR : (int)lba;
  3076. put_unaligned_be32(ret, scp->sense_buffer + 3);
  3077. }
  3078. scsi_set_resid(scp, scsi_bufflen(scp));
  3079. return check_condition_result;
  3080. }
  3081. sdeb_read_lock(sip);
  3082. /* DIX + T10 DIF */
  3083. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  3084. switch (prot_verify_read(scp, lba, num, ei_lba)) {
  3085. case 1: /* Guard tag error */
  3086. if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */
  3087. sdeb_read_unlock(sip);
  3088. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  3089. return check_condition_result;
  3090. } else if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) {
  3091. sdeb_read_unlock(sip);
  3092. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  3093. return illegal_condition_result;
  3094. }
  3095. break;
  3096. case 3: /* Reference tag error */
  3097. if (cmd[1] >> 5 != 3) { /* RDPROTECT != 3 */
  3098. sdeb_read_unlock(sip);
  3099. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 3);
  3100. return check_condition_result;
  3101. } else if (scp->prot_flags & SCSI_PROT_REF_CHECK) {
  3102. sdeb_read_unlock(sip);
  3103. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 3);
  3104. return illegal_condition_result;
  3105. }
  3106. break;
  3107. }
  3108. }
  3109. ret = do_device_access(sip, scp, 0, lba, num, false);
  3110. sdeb_read_unlock(sip);
  3111. if (unlikely(ret == -1))
  3112. return DID_ERROR << 16;
  3113. scsi_set_resid(scp, scsi_bufflen(scp) - ret);
  3114. if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
  3115. atomic_read(&sdeb_inject_pending))) {
  3116. if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
  3117. mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
  3118. atomic_set(&sdeb_inject_pending, 0);
  3119. return check_condition_result;
  3120. } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
  3121. /* Logical block guard check failed */
  3122. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  3123. atomic_set(&sdeb_inject_pending, 0);
  3124. return illegal_condition_result;
  3125. } else if (SDEBUG_OPT_DIX_ERR & sdebug_opts) {
  3126. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  3127. atomic_set(&sdeb_inject_pending, 0);
  3128. return illegal_condition_result;
  3129. }
  3130. }
  3131. return 0;
  3132. }
  3133. static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
  3134. unsigned int sectors, u32 ei_lba)
  3135. {
  3136. int ret;
  3137. struct t10_pi_tuple *sdt;
  3138. void *daddr;
  3139. sector_t sector = start_sec;
  3140. int ppage_offset;
  3141. int dpage_offset;
  3142. struct sg_mapping_iter diter;
  3143. struct sg_mapping_iter piter;
  3144. BUG_ON(scsi_sg_count(SCpnt) == 0);
  3145. BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
  3146. sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
  3147. scsi_prot_sg_count(SCpnt),
  3148. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  3149. sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  3150. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  3151. /* For each protection page */
  3152. while (sg_miter_next(&piter)) {
  3153. dpage_offset = 0;
  3154. if (WARN_ON(!sg_miter_next(&diter))) {
  3155. ret = 0x01;
  3156. goto out;
  3157. }
  3158. for (ppage_offset = 0; ppage_offset < piter.length;
  3159. ppage_offset += sizeof(struct t10_pi_tuple)) {
  3160. /* If we're at the end of the current
  3161. * data page advance to the next one
  3162. */
  3163. if (dpage_offset >= diter.length) {
  3164. if (WARN_ON(!sg_miter_next(&diter))) {
  3165. ret = 0x01;
  3166. goto out;
  3167. }
  3168. dpage_offset = 0;
  3169. }
  3170. sdt = piter.addr + ppage_offset;
  3171. daddr = diter.addr + dpage_offset;
  3172. if (SCpnt->cmnd[1] >> 5 != 3) { /* WRPROTECT */
  3173. ret = dif_verify(sdt, daddr, sector, ei_lba);
  3174. if (ret)
  3175. goto out;
  3176. }
  3177. sector++;
  3178. ei_lba++;
  3179. dpage_offset += sdebug_sector_size;
  3180. }
  3181. diter.consumed = dpage_offset;
  3182. sg_miter_stop(&diter);
  3183. }
  3184. sg_miter_stop(&piter);
  3185. dif_copy_prot(SCpnt, start_sec, sectors, false);
  3186. dix_writes++;
  3187. return 0;
  3188. out:
  3189. dif_errors++;
  3190. sg_miter_stop(&diter);
  3191. sg_miter_stop(&piter);
  3192. return ret;
  3193. }
  3194. static unsigned long lba_to_map_index(sector_t lba)
  3195. {
  3196. if (sdebug_unmap_alignment)
  3197. lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
  3198. sector_div(lba, sdebug_unmap_granularity);
  3199. return lba;
  3200. }
  3201. static sector_t map_index_to_lba(unsigned long index)
  3202. {
  3203. sector_t lba = index * sdebug_unmap_granularity;
  3204. if (sdebug_unmap_alignment)
  3205. lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
  3206. return lba;
  3207. }
  3208. static unsigned int map_state(struct sdeb_store_info *sip, sector_t lba,
  3209. unsigned int *num)
  3210. {
  3211. sector_t end;
  3212. unsigned int mapped;
  3213. unsigned long index;
  3214. unsigned long next;
  3215. index = lba_to_map_index(lba);
  3216. mapped = test_bit(index, sip->map_storep);
  3217. if (mapped)
  3218. next = find_next_zero_bit(sip->map_storep, map_size, index);
  3219. else
  3220. next = find_next_bit(sip->map_storep, map_size, index);
  3221. end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
  3222. *num = end - lba;
  3223. return mapped;
  3224. }
  3225. static void map_region(struct sdeb_store_info *sip, sector_t lba,
  3226. unsigned int len)
  3227. {
  3228. sector_t end = lba + len;
  3229. while (lba < end) {
  3230. unsigned long index = lba_to_map_index(lba);
  3231. if (index < map_size)
  3232. set_bit(index, sip->map_storep);
  3233. lba = map_index_to_lba(index + 1);
  3234. }
  3235. }
  3236. static void unmap_region(struct sdeb_store_info *sip, sector_t lba,
  3237. unsigned int len)
  3238. {
  3239. sector_t end = lba + len;
  3240. u8 *fsp = sip->storep;
  3241. while (lba < end) {
  3242. unsigned long index = lba_to_map_index(lba);
  3243. if (lba == map_index_to_lba(index) &&
  3244. lba + sdebug_unmap_granularity <= end &&
  3245. index < map_size) {
  3246. clear_bit(index, sip->map_storep);
  3247. if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */
  3248. memset(fsp + lba * sdebug_sector_size,
  3249. (sdebug_lbprz & 1) ? 0 : 0xff,
  3250. sdebug_sector_size *
  3251. sdebug_unmap_granularity);
  3252. }
  3253. if (sip->dif_storep) {
  3254. memset(sip->dif_storep + lba, 0xff,
  3255. sizeof(*sip->dif_storep) *
  3256. sdebug_unmap_granularity);
  3257. }
  3258. }
  3259. lba = map_index_to_lba(index + 1);
  3260. }
  3261. }
  3262. static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  3263. {
  3264. bool check_prot;
  3265. u32 num;
  3266. u32 ei_lba;
  3267. int ret;
  3268. u64 lba;
  3269. struct sdeb_store_info *sip = devip2sip(devip, true);
  3270. u8 *cmd = scp->cmnd;
  3271. switch (cmd[0]) {
  3272. case WRITE_16:
  3273. ei_lba = 0;
  3274. lba = get_unaligned_be64(cmd + 2);
  3275. num = get_unaligned_be32(cmd + 10);
  3276. check_prot = true;
  3277. break;
  3278. case WRITE_10:
  3279. ei_lba = 0;
  3280. lba = get_unaligned_be32(cmd + 2);
  3281. num = get_unaligned_be16(cmd + 7);
  3282. check_prot = true;
  3283. break;
  3284. case WRITE_6:
  3285. ei_lba = 0;
  3286. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  3287. (u32)(cmd[1] & 0x1f) << 16;
  3288. num = (0 == cmd[4]) ? 256 : cmd[4];
  3289. check_prot = true;
  3290. break;
  3291. case WRITE_12:
  3292. ei_lba = 0;
  3293. lba = get_unaligned_be32(cmd + 2);
  3294. num = get_unaligned_be32(cmd + 6);
  3295. check_prot = true;
  3296. break;
  3297. case 0x53: /* XDWRITEREAD(10) */
  3298. ei_lba = 0;
  3299. lba = get_unaligned_be32(cmd + 2);
  3300. num = get_unaligned_be16(cmd + 7);
  3301. check_prot = false;
  3302. break;
  3303. default: /* assume WRITE(32) */
  3304. lba = get_unaligned_be64(cmd + 12);
  3305. ei_lba = get_unaligned_be32(cmd + 20);
  3306. num = get_unaligned_be32(cmd + 28);
  3307. check_prot = false;
  3308. break;
  3309. }
  3310. if (unlikely(have_dif_prot && check_prot)) {
  3311. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3312. (cmd[1] & 0xe0)) {
  3313. mk_sense_invalid_opcode(scp);
  3314. return check_condition_result;
  3315. }
  3316. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3317. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3318. (cmd[1] & 0xe0) == 0)
  3319. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  3320. "to DIF device\n");
  3321. }
  3322. sdeb_write_lock(sip);
  3323. ret = check_device_access_params(scp, lba, num, true);
  3324. if (ret) {
  3325. sdeb_write_unlock(sip);
  3326. return ret;
  3327. }
  3328. /* DIX + T10 DIF */
  3329. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  3330. switch (prot_verify_write(scp, lba, num, ei_lba)) {
  3331. case 1: /* Guard tag error */
  3332. if (scp->prot_flags & SCSI_PROT_GUARD_CHECK) {
  3333. sdeb_write_unlock(sip);
  3334. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  3335. return illegal_condition_result;
  3336. } else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */
  3337. sdeb_write_unlock(sip);
  3338. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  3339. return check_condition_result;
  3340. }
  3341. break;
  3342. case 3: /* Reference tag error */
  3343. if (scp->prot_flags & SCSI_PROT_REF_CHECK) {
  3344. sdeb_write_unlock(sip);
  3345. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 3);
  3346. return illegal_condition_result;
  3347. } else if (scp->cmnd[1] >> 5 != 3) { /* WRPROTECT != 3 */
  3348. sdeb_write_unlock(sip);
  3349. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 3);
  3350. return check_condition_result;
  3351. }
  3352. break;
  3353. }
  3354. }
  3355. ret = do_device_access(sip, scp, 0, lba, num, true);
  3356. if (unlikely(scsi_debug_lbp()))
  3357. map_region(sip, lba, num);
  3358. /* If ZBC zone then bump its write pointer */
  3359. if (sdebug_dev_is_zoned(devip))
  3360. zbc_inc_wp(devip, lba, num);
  3361. sdeb_write_unlock(sip);
  3362. if (unlikely(-1 == ret))
  3363. return DID_ERROR << 16;
  3364. else if (unlikely(sdebug_verbose &&
  3365. (ret < (num * sdebug_sector_size))))
  3366. sdev_printk(KERN_INFO, scp->device,
  3367. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  3368. my_name, num * sdebug_sector_size, ret);
  3369. if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
  3370. atomic_read(&sdeb_inject_pending))) {
  3371. if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
  3372. mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
  3373. atomic_set(&sdeb_inject_pending, 0);
  3374. return check_condition_result;
  3375. } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
  3376. /* Logical block guard check failed */
  3377. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  3378. atomic_set(&sdeb_inject_pending, 0);
  3379. return illegal_condition_result;
  3380. } else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
  3381. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  3382. atomic_set(&sdeb_inject_pending, 0);
  3383. return illegal_condition_result;
  3384. }
  3385. }
  3386. return 0;
  3387. }
  3388. /*
  3389. * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
  3390. * No READ GATHERED yet (requires bidi or long cdb holding gather list).
  3391. */
  3392. static int resp_write_scat(struct scsi_cmnd *scp,
  3393. struct sdebug_dev_info *devip)
  3394. {
  3395. u8 *cmd = scp->cmnd;
  3396. u8 *lrdp = NULL;
  3397. u8 *up;
  3398. struct sdeb_store_info *sip = devip2sip(devip, true);
  3399. u8 wrprotect;
  3400. u16 lbdof, num_lrd, k;
  3401. u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
  3402. u32 lb_size = sdebug_sector_size;
  3403. u32 ei_lba;
  3404. u64 lba;
  3405. int ret, res;
  3406. bool is_16;
  3407. static const u32 lrd_size = 32; /* + parameter list header size */
  3408. if (cmd[0] == VARIABLE_LENGTH_CMD) {
  3409. is_16 = false;
  3410. wrprotect = (cmd[10] >> 5) & 0x7;
  3411. lbdof = get_unaligned_be16(cmd + 12);
  3412. num_lrd = get_unaligned_be16(cmd + 16);
  3413. bt_len = get_unaligned_be32(cmd + 28);
  3414. } else { /* that leaves WRITE SCATTERED(16) */
  3415. is_16 = true;
  3416. wrprotect = (cmd[2] >> 5) & 0x7;
  3417. lbdof = get_unaligned_be16(cmd + 4);
  3418. num_lrd = get_unaligned_be16(cmd + 8);
  3419. bt_len = get_unaligned_be32(cmd + 10);
  3420. if (unlikely(have_dif_prot)) {
  3421. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3422. wrprotect) {
  3423. mk_sense_invalid_opcode(scp);
  3424. return illegal_condition_result;
  3425. }
  3426. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3427. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3428. wrprotect == 0)
  3429. sdev_printk(KERN_ERR, scp->device,
  3430. "Unprotected WR to DIF device\n");
  3431. }
  3432. }
  3433. if ((num_lrd == 0) || (bt_len == 0))
  3434. return 0; /* T10 says these do-nothings are not errors */
  3435. if (lbdof == 0) {
  3436. if (sdebug_verbose)
  3437. sdev_printk(KERN_INFO, scp->device,
  3438. "%s: %s: LB Data Offset field bad\n",
  3439. my_name, __func__);
  3440. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  3441. return illegal_condition_result;
  3442. }
  3443. lbdof_blen = lbdof * lb_size;
  3444. if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
  3445. if (sdebug_verbose)
  3446. sdev_printk(KERN_INFO, scp->device,
  3447. "%s: %s: LBA range descriptors don't fit\n",
  3448. my_name, __func__);
  3449. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  3450. return illegal_condition_result;
  3451. }
  3452. lrdp = kzalloc(lbdof_blen, GFP_ATOMIC | __GFP_NOWARN);
  3453. if (lrdp == NULL)
  3454. return SCSI_MLQUEUE_HOST_BUSY;
  3455. if (sdebug_verbose)
  3456. sdev_printk(KERN_INFO, scp->device,
  3457. "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n",
  3458. my_name, __func__, lbdof_blen);
  3459. res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
  3460. if (res == -1) {
  3461. ret = DID_ERROR << 16;
  3462. goto err_out;
  3463. }
  3464. sdeb_write_lock(sip);
  3465. sg_off = lbdof_blen;
  3466. /* Spec says Buffer xfer Length field in number of LBs in dout */
  3467. cum_lb = 0;
  3468. for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
  3469. lba = get_unaligned_be64(up + 0);
  3470. num = get_unaligned_be32(up + 8);
  3471. if (sdebug_verbose)
  3472. sdev_printk(KERN_INFO, scp->device,
  3473. "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n",
  3474. my_name, __func__, k, lba, num, sg_off);
  3475. if (num == 0)
  3476. continue;
  3477. ret = check_device_access_params(scp, lba, num, true);
  3478. if (ret)
  3479. goto err_out_unlock;
  3480. num_by = num * lb_size;
  3481. ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
  3482. if ((cum_lb + num) > bt_len) {
  3483. if (sdebug_verbose)
  3484. sdev_printk(KERN_INFO, scp->device,
  3485. "%s: %s: sum of blocks > data provided\n",
  3486. my_name, __func__);
  3487. mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC,
  3488. 0);
  3489. ret = illegal_condition_result;
  3490. goto err_out_unlock;
  3491. }
  3492. /* DIX + T10 DIF */
  3493. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  3494. int prot_ret = prot_verify_write(scp, lba, num,
  3495. ei_lba);
  3496. if (prot_ret) {
  3497. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10,
  3498. prot_ret);
  3499. ret = illegal_condition_result;
  3500. goto err_out_unlock;
  3501. }
  3502. }
  3503. ret = do_device_access(sip, scp, sg_off, lba, num, true);
  3504. /* If ZBC zone then bump its write pointer */
  3505. if (sdebug_dev_is_zoned(devip))
  3506. zbc_inc_wp(devip, lba, num);
  3507. if (unlikely(scsi_debug_lbp()))
  3508. map_region(sip, lba, num);
  3509. if (unlikely(-1 == ret)) {
  3510. ret = DID_ERROR << 16;
  3511. goto err_out_unlock;
  3512. } else if (unlikely(sdebug_verbose && (ret < num_by)))
  3513. sdev_printk(KERN_INFO, scp->device,
  3514. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  3515. my_name, num_by, ret);
  3516. if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) &&
  3517. atomic_read(&sdeb_inject_pending))) {
  3518. if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) {
  3519. mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0);
  3520. atomic_set(&sdeb_inject_pending, 0);
  3521. ret = check_condition_result;
  3522. goto err_out_unlock;
  3523. } else if (sdebug_opts & SDEBUG_OPT_DIF_ERR) {
  3524. /* Logical block guard check failed */
  3525. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  3526. atomic_set(&sdeb_inject_pending, 0);
  3527. ret = illegal_condition_result;
  3528. goto err_out_unlock;
  3529. } else if (sdebug_opts & SDEBUG_OPT_DIX_ERR) {
  3530. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  3531. atomic_set(&sdeb_inject_pending, 0);
  3532. ret = illegal_condition_result;
  3533. goto err_out_unlock;
  3534. }
  3535. }
  3536. sg_off += num_by;
  3537. cum_lb += num;
  3538. }
  3539. ret = 0;
  3540. err_out_unlock:
  3541. sdeb_write_unlock(sip);
  3542. err_out:
  3543. kfree(lrdp);
  3544. return ret;
  3545. }
  3546. static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
  3547. u32 ei_lba, bool unmap, bool ndob)
  3548. {
  3549. struct scsi_device *sdp = scp->device;
  3550. struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata;
  3551. unsigned long long i;
  3552. u64 block, lbaa;
  3553. u32 lb_size = sdebug_sector_size;
  3554. int ret;
  3555. struct sdeb_store_info *sip = devip2sip((struct sdebug_dev_info *)
  3556. scp->device->hostdata, true);
  3557. u8 *fs1p;
  3558. u8 *fsp;
  3559. sdeb_write_lock(sip);
  3560. ret = check_device_access_params(scp, lba, num, true);
  3561. if (ret) {
  3562. sdeb_write_unlock(sip);
  3563. return ret;
  3564. }
  3565. if (unmap && scsi_debug_lbp()) {
  3566. unmap_region(sip, lba, num);
  3567. goto out;
  3568. }
  3569. lbaa = lba;
  3570. block = do_div(lbaa, sdebug_store_sectors);
  3571. /* if ndob then zero 1 logical block, else fetch 1 logical block */
  3572. fsp = sip->storep;
  3573. fs1p = fsp + (block * lb_size);
  3574. if (ndob) {
  3575. memset(fs1p, 0, lb_size);
  3576. ret = 0;
  3577. } else
  3578. ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
  3579. if (-1 == ret) {
  3580. sdeb_write_unlock(sip);
  3581. return DID_ERROR << 16;
  3582. } else if (sdebug_verbose && !ndob && (ret < lb_size))
  3583. sdev_printk(KERN_INFO, scp->device,
  3584. "%s: %s: lb size=%u, IO sent=%d bytes\n",
  3585. my_name, "write same", lb_size, ret);
  3586. /* Copy first sector to remaining blocks */
  3587. for (i = 1 ; i < num ; i++) {
  3588. lbaa = lba + i;
  3589. block = do_div(lbaa, sdebug_store_sectors);
  3590. memmove(fsp + (block * lb_size), fs1p, lb_size);
  3591. }
  3592. if (scsi_debug_lbp())
  3593. map_region(sip, lba, num);
  3594. /* If ZBC zone then bump its write pointer */
  3595. if (sdebug_dev_is_zoned(devip))
  3596. zbc_inc_wp(devip, lba, num);
  3597. out:
  3598. sdeb_write_unlock(sip);
  3599. return 0;
  3600. }
  3601. static int resp_write_same_10(struct scsi_cmnd *scp,
  3602. struct sdebug_dev_info *devip)
  3603. {
  3604. u8 *cmd = scp->cmnd;
  3605. u32 lba;
  3606. u16 num;
  3607. u32 ei_lba = 0;
  3608. bool unmap = false;
  3609. if (cmd[1] & 0x8) {
  3610. if (sdebug_lbpws10 == 0) {
  3611. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3612. return check_condition_result;
  3613. } else
  3614. unmap = true;
  3615. }
  3616. lba = get_unaligned_be32(cmd + 2);
  3617. num = get_unaligned_be16(cmd + 7);
  3618. if (num > sdebug_write_same_length) {
  3619. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3620. return check_condition_result;
  3621. }
  3622. return resp_write_same(scp, lba, num, ei_lba, unmap, false);
  3623. }
  3624. static int resp_write_same_16(struct scsi_cmnd *scp,
  3625. struct sdebug_dev_info *devip)
  3626. {
  3627. u8 *cmd = scp->cmnd;
  3628. u64 lba;
  3629. u32 num;
  3630. u32 ei_lba = 0;
  3631. bool unmap = false;
  3632. bool ndob = false;
  3633. if (cmd[1] & 0x8) { /* UNMAP */
  3634. if (sdebug_lbpws == 0) {
  3635. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3636. return check_condition_result;
  3637. } else
  3638. unmap = true;
  3639. }
  3640. if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
  3641. ndob = true;
  3642. lba = get_unaligned_be64(cmd + 2);
  3643. num = get_unaligned_be32(cmd + 10);
  3644. if (num > sdebug_write_same_length) {
  3645. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
  3646. return check_condition_result;
  3647. }
  3648. return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
  3649. }
  3650. /* Note the mode field is in the same position as the (lower) service action
  3651. * field. For the Report supported operation codes command, SPC-4 suggests
  3652. * each mode of this command should be reported separately; for future. */
  3653. static int resp_write_buffer(struct scsi_cmnd *scp,
  3654. struct sdebug_dev_info *devip)
  3655. {
  3656. u8 *cmd = scp->cmnd;
  3657. struct scsi_device *sdp = scp->device;
  3658. struct sdebug_dev_info *dp;
  3659. u8 mode;
  3660. mode = cmd[1] & 0x1f;
  3661. switch (mode) {
  3662. case 0x4: /* download microcode (MC) and activate (ACT) */
  3663. /* set UAs on this device only */
  3664. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3665. set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
  3666. break;
  3667. case 0x5: /* download MC, save and ACT */
  3668. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
  3669. break;
  3670. case 0x6: /* download MC with offsets and ACT */
  3671. /* set UAs on most devices (LUs) in this target */
  3672. list_for_each_entry(dp,
  3673. &devip->sdbg_host->dev_info_list,
  3674. dev_list)
  3675. if (dp->target == sdp->id) {
  3676. set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
  3677. if (devip != dp)
  3678. set_bit(SDEBUG_UA_MICROCODE_CHANGED,
  3679. dp->uas_bm);
  3680. }
  3681. break;
  3682. case 0x7: /* download MC with offsets, save, and ACT */
  3683. /* set UA on all devices (LUs) in this target */
  3684. list_for_each_entry(dp,
  3685. &devip->sdbg_host->dev_info_list,
  3686. dev_list)
  3687. if (dp->target == sdp->id)
  3688. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
  3689. dp->uas_bm);
  3690. break;
  3691. default:
  3692. /* do nothing for this command for other mode values */
  3693. break;
  3694. }
  3695. return 0;
  3696. }
  3697. static int resp_comp_write(struct scsi_cmnd *scp,
  3698. struct sdebug_dev_info *devip)
  3699. {
  3700. u8 *cmd = scp->cmnd;
  3701. u8 *arr;
  3702. struct sdeb_store_info *sip = devip2sip(devip, true);
  3703. u64 lba;
  3704. u32 dnum;
  3705. u32 lb_size = sdebug_sector_size;
  3706. u8 num;
  3707. int ret;
  3708. int retval = 0;
  3709. lba = get_unaligned_be64(cmd + 2);
  3710. num = cmd[13]; /* 1 to a maximum of 255 logical blocks */
  3711. if (0 == num)
  3712. return 0; /* degenerate case, not an error */
  3713. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3714. (cmd[1] & 0xe0)) {
  3715. mk_sense_invalid_opcode(scp);
  3716. return check_condition_result;
  3717. }
  3718. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3719. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3720. (cmd[1] & 0xe0) == 0)
  3721. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  3722. "to DIF device\n");
  3723. ret = check_device_access_params(scp, lba, num, false);
  3724. if (ret)
  3725. return ret;
  3726. dnum = 2 * num;
  3727. arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
  3728. if (NULL == arr) {
  3729. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3730. INSUFF_RES_ASCQ);
  3731. return check_condition_result;
  3732. }
  3733. sdeb_write_lock(sip);
  3734. ret = do_dout_fetch(scp, dnum, arr);
  3735. if (ret == -1) {
  3736. retval = DID_ERROR << 16;
  3737. goto cleanup;
  3738. } else if (sdebug_verbose && (ret < (dnum * lb_size)))
  3739. sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
  3740. "indicated=%u, IO sent=%d bytes\n", my_name,
  3741. dnum * lb_size, ret);
  3742. if (!comp_write_worker(sip, lba, num, arr, false)) {
  3743. mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
  3744. retval = check_condition_result;
  3745. goto cleanup;
  3746. }
  3747. if (scsi_debug_lbp())
  3748. map_region(sip, lba, num);
  3749. cleanup:
  3750. sdeb_write_unlock(sip);
  3751. kfree(arr);
  3752. return retval;
  3753. }
  3754. struct unmap_block_desc {
  3755. __be64 lba;
  3756. __be32 blocks;
  3757. __be32 __reserved;
  3758. };
  3759. static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  3760. {
  3761. unsigned char *buf;
  3762. struct unmap_block_desc *desc;
  3763. struct sdeb_store_info *sip = devip2sip(devip, true);
  3764. unsigned int i, payload_len, descriptors;
  3765. int ret;
  3766. if (!scsi_debug_lbp())
  3767. return 0; /* fib and say its done */
  3768. payload_len = get_unaligned_be16(scp->cmnd + 7);
  3769. BUG_ON(scsi_bufflen(scp) != payload_len);
  3770. descriptors = (payload_len - 8) / 16;
  3771. if (descriptors > sdebug_unmap_max_desc) {
  3772. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3773. return check_condition_result;
  3774. }
  3775. buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
  3776. if (!buf) {
  3777. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3778. INSUFF_RES_ASCQ);
  3779. return check_condition_result;
  3780. }
  3781. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  3782. BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
  3783. BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
  3784. desc = (void *)&buf[8];
  3785. sdeb_write_lock(sip);
  3786. for (i = 0 ; i < descriptors ; i++) {
  3787. unsigned long long lba = get_unaligned_be64(&desc[i].lba);
  3788. unsigned int num = get_unaligned_be32(&desc[i].blocks);
  3789. ret = check_device_access_params(scp, lba, num, true);
  3790. if (ret)
  3791. goto out;
  3792. unmap_region(sip, lba, num);
  3793. }
  3794. ret = 0;
  3795. out:
  3796. sdeb_write_unlock(sip);
  3797. kfree(buf);
  3798. return ret;
  3799. }
  3800. #define SDEBUG_GET_LBA_STATUS_LEN 32
  3801. static int resp_get_lba_status(struct scsi_cmnd *scp,
  3802. struct sdebug_dev_info *devip)
  3803. {
  3804. u8 *cmd = scp->cmnd;
  3805. u64 lba;
  3806. u32 alloc_len, mapped, num;
  3807. int ret;
  3808. u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
  3809. lba = get_unaligned_be64(cmd + 2);
  3810. alloc_len = get_unaligned_be32(cmd + 10);
  3811. if (alloc_len < 24)
  3812. return 0;
  3813. ret = check_device_access_params(scp, lba, 1, false);
  3814. if (ret)
  3815. return ret;
  3816. if (scsi_debug_lbp()) {
  3817. struct sdeb_store_info *sip = devip2sip(devip, true);
  3818. mapped = map_state(sip, lba, &num);
  3819. } else {
  3820. mapped = 1;
  3821. /* following just in case virtual_gb changed */
  3822. sdebug_capacity = get_sdebug_capacity();
  3823. if (sdebug_capacity - lba <= 0xffffffff)
  3824. num = sdebug_capacity - lba;
  3825. else
  3826. num = 0xffffffff;
  3827. }
  3828. memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
  3829. put_unaligned_be32(20, arr); /* Parameter Data Length */
  3830. put_unaligned_be64(lba, arr + 8); /* LBA */
  3831. put_unaligned_be32(num, arr + 16); /* Number of blocks */
  3832. arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
  3833. return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
  3834. }
  3835. static int resp_sync_cache(struct scsi_cmnd *scp,
  3836. struct sdebug_dev_info *devip)
  3837. {
  3838. int res = 0;
  3839. u64 lba;
  3840. u32 num_blocks;
  3841. u8 *cmd = scp->cmnd;
  3842. if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */
  3843. lba = get_unaligned_be32(cmd + 2);
  3844. num_blocks = get_unaligned_be16(cmd + 7);
  3845. } else { /* SYNCHRONIZE_CACHE(16) */
  3846. lba = get_unaligned_be64(cmd + 2);
  3847. num_blocks = get_unaligned_be32(cmd + 10);
  3848. }
  3849. if (lba + num_blocks > sdebug_capacity) {
  3850. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3851. return check_condition_result;
  3852. }
  3853. if (!write_since_sync || (cmd[1] & 0x2))
  3854. res = SDEG_RES_IMMED_MASK;
  3855. else /* delay if write_since_sync and IMMED clear */
  3856. write_since_sync = false;
  3857. return res;
  3858. }
  3859. /*
  3860. * Assuming the LBA+num_blocks is not out-of-range, this function will return
  3861. * CONDITION MET if the specified blocks will/have fitted in the cache, and
  3862. * a GOOD status otherwise. Model a disk with a big cache and yield
  3863. * CONDITION MET. Actually tries to bring range in main memory into the
  3864. * cache associated with the CPU(s).
  3865. */
  3866. static int resp_pre_fetch(struct scsi_cmnd *scp,
  3867. struct sdebug_dev_info *devip)
  3868. {
  3869. int res = 0;
  3870. u64 lba;
  3871. u64 block, rest = 0;
  3872. u32 nblks;
  3873. u8 *cmd = scp->cmnd;
  3874. struct sdeb_store_info *sip = devip2sip(devip, true);
  3875. u8 *fsp = sip->storep;
  3876. if (cmd[0] == PRE_FETCH) { /* 10 byte cdb */
  3877. lba = get_unaligned_be32(cmd + 2);
  3878. nblks = get_unaligned_be16(cmd + 7);
  3879. } else { /* PRE-FETCH(16) */
  3880. lba = get_unaligned_be64(cmd + 2);
  3881. nblks = get_unaligned_be32(cmd + 10);
  3882. }
  3883. if (lba + nblks > sdebug_capacity) {
  3884. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3885. return check_condition_result;
  3886. }
  3887. if (!fsp)
  3888. goto fini;
  3889. /* PRE-FETCH spec says nothing about LBP or PI so skip them */
  3890. block = do_div(lba, sdebug_store_sectors);
  3891. if (block + nblks > sdebug_store_sectors)
  3892. rest = block + nblks - sdebug_store_sectors;
  3893. /* Try to bring the PRE-FETCH range into CPU's cache */
  3894. sdeb_read_lock(sip);
  3895. prefetch_range(fsp + (sdebug_sector_size * block),
  3896. (nblks - rest) * sdebug_sector_size);
  3897. if (rest)
  3898. prefetch_range(fsp, rest * sdebug_sector_size);
  3899. sdeb_read_unlock(sip);
  3900. fini:
  3901. if (cmd[1] & 0x2)
  3902. res = SDEG_RES_IMMED_MASK;
  3903. return res | condition_met_result;
  3904. }
  3905. #define RL_BUCKET_ELEMS 8
  3906. /* Even though each pseudo target has a REPORT LUNS "well known logical unit"
  3907. * (W-LUN), the normal Linux scanning logic does not associate it with a
  3908. * device (e.g. /dev/sg7). The following magic will make that association:
  3909. * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
  3910. * where <n> is a host number. If there are multiple targets in a host then
  3911. * the above will associate a W-LUN to each target. To only get a W-LUN
  3912. * for target 2, then use "echo '- 2 49409' > scan" .
  3913. */
  3914. static int resp_report_luns(struct scsi_cmnd *scp,
  3915. struct sdebug_dev_info *devip)
  3916. {
  3917. unsigned char *cmd = scp->cmnd;
  3918. unsigned int alloc_len;
  3919. unsigned char select_report;
  3920. u64 lun;
  3921. struct scsi_lun *lun_p;
  3922. u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
  3923. unsigned int lun_cnt; /* normal LUN count (max: 256) */
  3924. unsigned int wlun_cnt; /* report luns W-LUN count */
  3925. unsigned int tlun_cnt; /* total LUN count */
  3926. unsigned int rlen; /* response length (in bytes) */
  3927. int k, j, n, res;
  3928. unsigned int off_rsp = 0;
  3929. const int sz_lun = sizeof(struct scsi_lun);
  3930. clear_luns_changed_on_target(devip);
  3931. select_report = cmd[2];
  3932. alloc_len = get_unaligned_be32(cmd + 6);
  3933. if (alloc_len < 4) {
  3934. pr_err("alloc len too small %d\n", alloc_len);
  3935. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  3936. return check_condition_result;
  3937. }
  3938. switch (select_report) {
  3939. case 0: /* all LUNs apart from W-LUNs */
  3940. lun_cnt = sdebug_max_luns;
  3941. wlun_cnt = 0;
  3942. break;
  3943. case 1: /* only W-LUNs */
  3944. lun_cnt = 0;
  3945. wlun_cnt = 1;
  3946. break;
  3947. case 2: /* all LUNs */
  3948. lun_cnt = sdebug_max_luns;
  3949. wlun_cnt = 1;
  3950. break;
  3951. case 0x10: /* only administrative LUs */
  3952. case 0x11: /* see SPC-5 */
  3953. case 0x12: /* only subsiduary LUs owned by referenced LU */
  3954. default:
  3955. pr_debug("select report invalid %d\n", select_report);
  3956. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  3957. return check_condition_result;
  3958. }
  3959. if (sdebug_no_lun_0 && (lun_cnt > 0))
  3960. --lun_cnt;
  3961. tlun_cnt = lun_cnt + wlun_cnt;
  3962. rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */
  3963. scsi_set_resid(scp, scsi_bufflen(scp));
  3964. pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
  3965. select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
  3966. /* loops rely on sizeof response header same as sizeof lun (both 8) */
  3967. lun = sdebug_no_lun_0 ? 1 : 0;
  3968. for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
  3969. memset(arr, 0, sizeof(arr));
  3970. lun_p = (struct scsi_lun *)&arr[0];
  3971. if (k == 0) {
  3972. put_unaligned_be32(rlen, &arr[0]);
  3973. ++lun_p;
  3974. j = 1;
  3975. }
  3976. for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
  3977. if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
  3978. break;
  3979. int_to_scsilun(lun++, lun_p);
  3980. if (lun > 1 && sdebug_lun_am == SAM_LUN_AM_FLAT)
  3981. lun_p->scsi_lun[0] |= 0x40;
  3982. }
  3983. if (j < RL_BUCKET_ELEMS)
  3984. break;
  3985. n = j * sz_lun;
  3986. res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
  3987. if (res)
  3988. return res;
  3989. off_rsp += n;
  3990. }
  3991. if (wlun_cnt) {
  3992. int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
  3993. ++j;
  3994. }
  3995. if (j > 0)
  3996. res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
  3997. return res;
  3998. }
  3999. static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  4000. {
  4001. bool is_bytchk3 = false;
  4002. u8 bytchk;
  4003. int ret, j;
  4004. u32 vnum, a_num, off;
  4005. const u32 lb_size = sdebug_sector_size;
  4006. u64 lba;
  4007. u8 *arr;
  4008. u8 *cmd = scp->cmnd;
  4009. struct sdeb_store_info *sip = devip2sip(devip, true);
  4010. bytchk = (cmd[1] >> 1) & 0x3;
  4011. if (bytchk == 0) {
  4012. return 0; /* always claim internal verify okay */
  4013. } else if (bytchk == 2) {
  4014. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
  4015. return check_condition_result;
  4016. } else if (bytchk == 3) {
  4017. is_bytchk3 = true; /* 1 block sent, compared repeatedly */
  4018. }
  4019. switch (cmd[0]) {
  4020. case VERIFY_16:
  4021. lba = get_unaligned_be64(cmd + 2);
  4022. vnum = get_unaligned_be32(cmd + 10);
  4023. break;
  4024. case VERIFY: /* is VERIFY(10) */
  4025. lba = get_unaligned_be32(cmd + 2);
  4026. vnum = get_unaligned_be16(cmd + 7);
  4027. break;
  4028. default:
  4029. mk_sense_invalid_opcode(scp);
  4030. return check_condition_result;
  4031. }
  4032. if (vnum == 0)
  4033. return 0; /* not an error */
  4034. a_num = is_bytchk3 ? 1 : vnum;
  4035. /* Treat following check like one for read (i.e. no write) access */
  4036. ret = check_device_access_params(scp, lba, a_num, false);
  4037. if (ret)
  4038. return ret;
  4039. arr = kcalloc(lb_size, vnum, GFP_ATOMIC | __GFP_NOWARN);
  4040. if (!arr) {
  4041. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  4042. INSUFF_RES_ASCQ);
  4043. return check_condition_result;
  4044. }
  4045. /* Not changing store, so only need read access */
  4046. sdeb_read_lock(sip);
  4047. ret = do_dout_fetch(scp, a_num, arr);
  4048. if (ret == -1) {
  4049. ret = DID_ERROR << 16;
  4050. goto cleanup;
  4051. } else if (sdebug_verbose && (ret < (a_num * lb_size))) {
  4052. sdev_printk(KERN_INFO, scp->device,
  4053. "%s: %s: cdb indicated=%u, IO sent=%d bytes\n",
  4054. my_name, __func__, a_num * lb_size, ret);
  4055. }
  4056. if (is_bytchk3) {
  4057. for (j = 1, off = lb_size; j < vnum; ++j, off += lb_size)
  4058. memcpy(arr + off, arr, lb_size);
  4059. }
  4060. ret = 0;
  4061. if (!comp_write_worker(sip, lba, vnum, arr, true)) {
  4062. mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
  4063. ret = check_condition_result;
  4064. goto cleanup;
  4065. }
  4066. cleanup:
  4067. sdeb_read_unlock(sip);
  4068. kfree(arr);
  4069. return ret;
  4070. }
  4071. #define RZONES_DESC_HD 64
  4072. /* Report zones depending on start LBA and reporting options */
  4073. static int resp_report_zones(struct scsi_cmnd *scp,
  4074. struct sdebug_dev_info *devip)
  4075. {
  4076. unsigned int rep_max_zones, nrz = 0;
  4077. int ret = 0;
  4078. u32 alloc_len, rep_opts, rep_len;
  4079. bool partial;
  4080. u64 lba, zs_lba;
  4081. u8 *arr = NULL, *desc;
  4082. u8 *cmd = scp->cmnd;
  4083. struct sdeb_zone_state *zsp = NULL;
  4084. struct sdeb_store_info *sip = devip2sip(devip, false);
  4085. if (!sdebug_dev_is_zoned(devip)) {
  4086. mk_sense_invalid_opcode(scp);
  4087. return check_condition_result;
  4088. }
  4089. zs_lba = get_unaligned_be64(cmd + 2);
  4090. alloc_len = get_unaligned_be32(cmd + 10);
  4091. if (alloc_len == 0)
  4092. return 0; /* not an error */
  4093. rep_opts = cmd[14] & 0x3f;
  4094. partial = cmd[14] & 0x80;
  4095. if (zs_lba >= sdebug_capacity) {
  4096. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  4097. return check_condition_result;
  4098. }
  4099. rep_max_zones = (alloc_len - 64) >> ilog2(RZONES_DESC_HD);
  4100. arr = kzalloc(alloc_len, GFP_ATOMIC | __GFP_NOWARN);
  4101. if (!arr) {
  4102. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  4103. INSUFF_RES_ASCQ);
  4104. return check_condition_result;
  4105. }
  4106. sdeb_read_lock(sip);
  4107. desc = arr + 64;
  4108. for (lba = zs_lba; lba < sdebug_capacity;
  4109. lba = zsp->z_start + zsp->z_size) {
  4110. if (WARN_ONCE(zbc_zone(devip, lba) == zsp, "lba = %llu\n", lba))
  4111. break;
  4112. zsp = zbc_zone(devip, lba);
  4113. switch (rep_opts) {
  4114. case 0x00:
  4115. /* All zones */
  4116. break;
  4117. case 0x01:
  4118. /* Empty zones */
  4119. if (zsp->z_cond != ZC1_EMPTY)
  4120. continue;
  4121. break;
  4122. case 0x02:
  4123. /* Implicit open zones */
  4124. if (zsp->z_cond != ZC2_IMPLICIT_OPEN)
  4125. continue;
  4126. break;
  4127. case 0x03:
  4128. /* Explicit open zones */
  4129. if (zsp->z_cond != ZC3_EXPLICIT_OPEN)
  4130. continue;
  4131. break;
  4132. case 0x04:
  4133. /* Closed zones */
  4134. if (zsp->z_cond != ZC4_CLOSED)
  4135. continue;
  4136. break;
  4137. case 0x05:
  4138. /* Full zones */
  4139. if (zsp->z_cond != ZC5_FULL)
  4140. continue;
  4141. break;
  4142. case 0x06:
  4143. case 0x07:
  4144. case 0x10:
  4145. /*
  4146. * Read-only, offline, reset WP recommended are
  4147. * not emulated: no zones to report;
  4148. */
  4149. continue;
  4150. case 0x11:
  4151. /* non-seq-resource set */
  4152. if (!zsp->z_non_seq_resource)
  4153. continue;
  4154. break;
  4155. case 0x3e:
  4156. /* All zones except gap zones. */
  4157. if (zbc_zone_is_gap(zsp))
  4158. continue;
  4159. break;
  4160. case 0x3f:
  4161. /* Not write pointer (conventional) zones */
  4162. if (zbc_zone_is_seq(zsp))
  4163. continue;
  4164. break;
  4165. default:
  4166. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  4167. INVALID_FIELD_IN_CDB, 0);
  4168. ret = check_condition_result;
  4169. goto fini;
  4170. }
  4171. if (nrz < rep_max_zones) {
  4172. /* Fill zone descriptor */
  4173. desc[0] = zsp->z_type;
  4174. desc[1] = zsp->z_cond << 4;
  4175. if (zsp->z_non_seq_resource)
  4176. desc[1] |= 1 << 1;
  4177. put_unaligned_be64((u64)zsp->z_size, desc + 8);
  4178. put_unaligned_be64((u64)zsp->z_start, desc + 16);
  4179. put_unaligned_be64((u64)zsp->z_wp, desc + 24);
  4180. desc += 64;
  4181. }
  4182. if (partial && nrz >= rep_max_zones)
  4183. break;
  4184. nrz++;
  4185. }
  4186. /* Report header */
  4187. /* Zone list length. */
  4188. put_unaligned_be32(nrz * RZONES_DESC_HD, arr + 0);
  4189. /* Maximum LBA */
  4190. put_unaligned_be64(sdebug_capacity - 1, arr + 8);
  4191. /* Zone starting LBA granularity. */
  4192. if (devip->zcap < devip->zsize)
  4193. put_unaligned_be64(devip->zsize, arr + 16);
  4194. rep_len = (unsigned long)desc - (unsigned long)arr;
  4195. ret = fill_from_dev_buffer(scp, arr, min_t(u32, alloc_len, rep_len));
  4196. fini:
  4197. sdeb_read_unlock(sip);
  4198. kfree(arr);
  4199. return ret;
  4200. }
  4201. /* Logic transplanted from tcmu-runner, file_zbc.c */
  4202. static void zbc_open_all(struct sdebug_dev_info *devip)
  4203. {
  4204. struct sdeb_zone_state *zsp = &devip->zstate[0];
  4205. unsigned int i;
  4206. for (i = 0; i < devip->nr_zones; i++, zsp++) {
  4207. if (zsp->z_cond == ZC4_CLOSED)
  4208. zbc_open_zone(devip, &devip->zstate[i], true);
  4209. }
  4210. }
  4211. static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  4212. {
  4213. int res = 0;
  4214. u64 z_id;
  4215. enum sdebug_z_cond zc;
  4216. u8 *cmd = scp->cmnd;
  4217. struct sdeb_zone_state *zsp;
  4218. bool all = cmd[14] & 0x01;
  4219. struct sdeb_store_info *sip = devip2sip(devip, false);
  4220. if (!sdebug_dev_is_zoned(devip)) {
  4221. mk_sense_invalid_opcode(scp);
  4222. return check_condition_result;
  4223. }
  4224. sdeb_write_lock(sip);
  4225. if (all) {
  4226. /* Check if all closed zones can be open */
  4227. if (devip->max_open &&
  4228. devip->nr_exp_open + devip->nr_closed > devip->max_open) {
  4229. mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
  4230. INSUFF_ZONE_ASCQ);
  4231. res = check_condition_result;
  4232. goto fini;
  4233. }
  4234. /* Open all closed zones */
  4235. zbc_open_all(devip);
  4236. goto fini;
  4237. }
  4238. /* Open the specified zone */
  4239. z_id = get_unaligned_be64(cmd + 2);
  4240. if (z_id >= sdebug_capacity) {
  4241. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  4242. res = check_condition_result;
  4243. goto fini;
  4244. }
  4245. zsp = zbc_zone(devip, z_id);
  4246. if (z_id != zsp->z_start) {
  4247. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4248. res = check_condition_result;
  4249. goto fini;
  4250. }
  4251. if (zbc_zone_is_conv(zsp)) {
  4252. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4253. res = check_condition_result;
  4254. goto fini;
  4255. }
  4256. zc = zsp->z_cond;
  4257. if (zc == ZC3_EXPLICIT_OPEN || zc == ZC5_FULL)
  4258. goto fini;
  4259. if (devip->max_open && devip->nr_exp_open >= devip->max_open) {
  4260. mk_sense_buffer(scp, DATA_PROTECT, INSUFF_RES_ASC,
  4261. INSUFF_ZONE_ASCQ);
  4262. res = check_condition_result;
  4263. goto fini;
  4264. }
  4265. zbc_open_zone(devip, zsp, true);
  4266. fini:
  4267. sdeb_write_unlock(sip);
  4268. return res;
  4269. }
  4270. static void zbc_close_all(struct sdebug_dev_info *devip)
  4271. {
  4272. unsigned int i;
  4273. for (i = 0; i < devip->nr_zones; i++)
  4274. zbc_close_zone(devip, &devip->zstate[i]);
  4275. }
  4276. static int resp_close_zone(struct scsi_cmnd *scp,
  4277. struct sdebug_dev_info *devip)
  4278. {
  4279. int res = 0;
  4280. u64 z_id;
  4281. u8 *cmd = scp->cmnd;
  4282. struct sdeb_zone_state *zsp;
  4283. bool all = cmd[14] & 0x01;
  4284. struct sdeb_store_info *sip = devip2sip(devip, false);
  4285. if (!sdebug_dev_is_zoned(devip)) {
  4286. mk_sense_invalid_opcode(scp);
  4287. return check_condition_result;
  4288. }
  4289. sdeb_write_lock(sip);
  4290. if (all) {
  4291. zbc_close_all(devip);
  4292. goto fini;
  4293. }
  4294. /* Close specified zone */
  4295. z_id = get_unaligned_be64(cmd + 2);
  4296. if (z_id >= sdebug_capacity) {
  4297. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  4298. res = check_condition_result;
  4299. goto fini;
  4300. }
  4301. zsp = zbc_zone(devip, z_id);
  4302. if (z_id != zsp->z_start) {
  4303. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4304. res = check_condition_result;
  4305. goto fini;
  4306. }
  4307. if (zbc_zone_is_conv(zsp)) {
  4308. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4309. res = check_condition_result;
  4310. goto fini;
  4311. }
  4312. zbc_close_zone(devip, zsp);
  4313. fini:
  4314. sdeb_write_unlock(sip);
  4315. return res;
  4316. }
  4317. static void zbc_finish_zone(struct sdebug_dev_info *devip,
  4318. struct sdeb_zone_state *zsp, bool empty)
  4319. {
  4320. enum sdebug_z_cond zc = zsp->z_cond;
  4321. if (zc == ZC4_CLOSED || zc == ZC2_IMPLICIT_OPEN ||
  4322. zc == ZC3_EXPLICIT_OPEN || (empty && zc == ZC1_EMPTY)) {
  4323. if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
  4324. zbc_close_zone(devip, zsp);
  4325. if (zsp->z_cond == ZC4_CLOSED)
  4326. devip->nr_closed--;
  4327. zsp->z_wp = zsp->z_start + zsp->z_size;
  4328. zsp->z_cond = ZC5_FULL;
  4329. }
  4330. }
  4331. static void zbc_finish_all(struct sdebug_dev_info *devip)
  4332. {
  4333. unsigned int i;
  4334. for (i = 0; i < devip->nr_zones; i++)
  4335. zbc_finish_zone(devip, &devip->zstate[i], false);
  4336. }
  4337. static int resp_finish_zone(struct scsi_cmnd *scp,
  4338. struct sdebug_dev_info *devip)
  4339. {
  4340. struct sdeb_zone_state *zsp;
  4341. int res = 0;
  4342. u64 z_id;
  4343. u8 *cmd = scp->cmnd;
  4344. bool all = cmd[14] & 0x01;
  4345. struct sdeb_store_info *sip = devip2sip(devip, false);
  4346. if (!sdebug_dev_is_zoned(devip)) {
  4347. mk_sense_invalid_opcode(scp);
  4348. return check_condition_result;
  4349. }
  4350. sdeb_write_lock(sip);
  4351. if (all) {
  4352. zbc_finish_all(devip);
  4353. goto fini;
  4354. }
  4355. /* Finish the specified zone */
  4356. z_id = get_unaligned_be64(cmd + 2);
  4357. if (z_id >= sdebug_capacity) {
  4358. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  4359. res = check_condition_result;
  4360. goto fini;
  4361. }
  4362. zsp = zbc_zone(devip, z_id);
  4363. if (z_id != zsp->z_start) {
  4364. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4365. res = check_condition_result;
  4366. goto fini;
  4367. }
  4368. if (zbc_zone_is_conv(zsp)) {
  4369. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4370. res = check_condition_result;
  4371. goto fini;
  4372. }
  4373. zbc_finish_zone(devip, zsp, true);
  4374. fini:
  4375. sdeb_write_unlock(sip);
  4376. return res;
  4377. }
  4378. static void zbc_rwp_zone(struct sdebug_dev_info *devip,
  4379. struct sdeb_zone_state *zsp)
  4380. {
  4381. enum sdebug_z_cond zc;
  4382. struct sdeb_store_info *sip = devip2sip(devip, false);
  4383. if (!zbc_zone_is_seq(zsp))
  4384. return;
  4385. zc = zsp->z_cond;
  4386. if (zc == ZC2_IMPLICIT_OPEN || zc == ZC3_EXPLICIT_OPEN)
  4387. zbc_close_zone(devip, zsp);
  4388. if (zsp->z_cond == ZC4_CLOSED)
  4389. devip->nr_closed--;
  4390. if (zsp->z_wp > zsp->z_start)
  4391. memset(sip->storep + zsp->z_start * sdebug_sector_size, 0,
  4392. (zsp->z_wp - zsp->z_start) * sdebug_sector_size);
  4393. zsp->z_non_seq_resource = false;
  4394. zsp->z_wp = zsp->z_start;
  4395. zsp->z_cond = ZC1_EMPTY;
  4396. }
  4397. static void zbc_rwp_all(struct sdebug_dev_info *devip)
  4398. {
  4399. unsigned int i;
  4400. for (i = 0; i < devip->nr_zones; i++)
  4401. zbc_rwp_zone(devip, &devip->zstate[i]);
  4402. }
  4403. static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  4404. {
  4405. struct sdeb_zone_state *zsp;
  4406. int res = 0;
  4407. u64 z_id;
  4408. u8 *cmd = scp->cmnd;
  4409. bool all = cmd[14] & 0x01;
  4410. struct sdeb_store_info *sip = devip2sip(devip, false);
  4411. if (!sdebug_dev_is_zoned(devip)) {
  4412. mk_sense_invalid_opcode(scp);
  4413. return check_condition_result;
  4414. }
  4415. sdeb_write_lock(sip);
  4416. if (all) {
  4417. zbc_rwp_all(devip);
  4418. goto fini;
  4419. }
  4420. z_id = get_unaligned_be64(cmd + 2);
  4421. if (z_id >= sdebug_capacity) {
  4422. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  4423. res = check_condition_result;
  4424. goto fini;
  4425. }
  4426. zsp = zbc_zone(devip, z_id);
  4427. if (z_id != zsp->z_start) {
  4428. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4429. res = check_condition_result;
  4430. goto fini;
  4431. }
  4432. if (zbc_zone_is_conv(zsp)) {
  4433. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  4434. res = check_condition_result;
  4435. goto fini;
  4436. }
  4437. zbc_rwp_zone(devip, zsp);
  4438. fini:
  4439. sdeb_write_unlock(sip);
  4440. return res;
  4441. }
  4442. static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
  4443. {
  4444. u16 hwq;
  4445. u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
  4446. hwq = blk_mq_unique_tag_to_hwq(tag);
  4447. pr_debug("tag=%#x, hwq=%d\n", tag, hwq);
  4448. if (WARN_ON_ONCE(hwq >= submit_queues))
  4449. hwq = 0;
  4450. return sdebug_q_arr + hwq;
  4451. }
  4452. static u32 get_tag(struct scsi_cmnd *cmnd)
  4453. {
  4454. return blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
  4455. }
  4456. /* Queued (deferred) command completions converge here. */
  4457. static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
  4458. {
  4459. bool aborted = sd_dp->aborted;
  4460. int qc_idx;
  4461. int retiring = 0;
  4462. unsigned long iflags;
  4463. struct sdebug_queue *sqp;
  4464. struct sdebug_queued_cmd *sqcp;
  4465. struct scsi_cmnd *scp;
  4466. struct sdebug_dev_info *devip;
  4467. if (unlikely(aborted))
  4468. sd_dp->aborted = false;
  4469. qc_idx = sd_dp->qc_idx;
  4470. sqp = sdebug_q_arr + sd_dp->sqa_idx;
  4471. if (sdebug_statistics) {
  4472. atomic_inc(&sdebug_completions);
  4473. if (raw_smp_processor_id() != sd_dp->issuing_cpu)
  4474. atomic_inc(&sdebug_miss_cpus);
  4475. }
  4476. if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
  4477. pr_err("wild qc_idx=%d\n", qc_idx);
  4478. return;
  4479. }
  4480. spin_lock_irqsave(&sqp->qc_lock, iflags);
  4481. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_NONE);
  4482. sqcp = &sqp->qc_arr[qc_idx];
  4483. scp = sqcp->a_cmnd;
  4484. if (unlikely(scp == NULL)) {
  4485. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4486. pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d, hc_idx=%d\n",
  4487. sd_dp->sqa_idx, qc_idx, sd_dp->hc_idx);
  4488. return;
  4489. }
  4490. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  4491. if (likely(devip))
  4492. atomic_dec(&devip->num_in_q);
  4493. else
  4494. pr_err("devip=NULL\n");
  4495. if (unlikely(atomic_read(&retired_max_queue) > 0))
  4496. retiring = 1;
  4497. sqcp->a_cmnd = NULL;
  4498. if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
  4499. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4500. pr_err("Unexpected completion\n");
  4501. return;
  4502. }
  4503. if (unlikely(retiring)) { /* user has reduced max_queue */
  4504. int k, retval;
  4505. retval = atomic_read(&retired_max_queue);
  4506. if (qc_idx >= retval) {
  4507. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4508. pr_err("index %d too large\n", retval);
  4509. return;
  4510. }
  4511. k = find_last_bit(sqp->in_use_bm, retval);
  4512. if ((k < sdebug_max_queue) || (k == retval))
  4513. atomic_set(&retired_max_queue, 0);
  4514. else
  4515. atomic_set(&retired_max_queue, k + 1);
  4516. }
  4517. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4518. if (unlikely(aborted)) {
  4519. if (sdebug_verbose)
  4520. pr_info("bypassing scsi_done() due to aborted cmd\n");
  4521. return;
  4522. }
  4523. scsi_done(scp); /* callback to mid level */
  4524. }
  4525. /* When high resolution timer goes off this function is called. */
  4526. static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
  4527. {
  4528. struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
  4529. hrt);
  4530. sdebug_q_cmd_complete(sd_dp);
  4531. return HRTIMER_NORESTART;
  4532. }
  4533. /* When work queue schedules work, it calls this function. */
  4534. static void sdebug_q_cmd_wq_complete(struct work_struct *work)
  4535. {
  4536. struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
  4537. ew.work);
  4538. sdebug_q_cmd_complete(sd_dp);
  4539. }
  4540. static bool got_shared_uuid;
  4541. static uuid_t shared_uuid;
  4542. static int sdebug_device_create_zones(struct sdebug_dev_info *devip)
  4543. {
  4544. struct sdeb_zone_state *zsp;
  4545. sector_t capacity = get_sdebug_capacity();
  4546. sector_t conv_capacity;
  4547. sector_t zstart = 0;
  4548. unsigned int i;
  4549. /*
  4550. * Set the zone size: if sdeb_zbc_zone_size_mb is not set, figure out
  4551. * a zone size allowing for at least 4 zones on the device. Otherwise,
  4552. * use the specified zone size checking that at least 2 zones can be
  4553. * created for the device.
  4554. */
  4555. if (!sdeb_zbc_zone_size_mb) {
  4556. devip->zsize = (DEF_ZBC_ZONE_SIZE_MB * SZ_1M)
  4557. >> ilog2(sdebug_sector_size);
  4558. while (capacity < devip->zsize << 2 && devip->zsize >= 2)
  4559. devip->zsize >>= 1;
  4560. if (devip->zsize < 2) {
  4561. pr_err("Device capacity too small\n");
  4562. return -EINVAL;
  4563. }
  4564. } else {
  4565. if (!is_power_of_2(sdeb_zbc_zone_size_mb)) {
  4566. pr_err("Zone size is not a power of 2\n");
  4567. return -EINVAL;
  4568. }
  4569. devip->zsize = (sdeb_zbc_zone_size_mb * SZ_1M)
  4570. >> ilog2(sdebug_sector_size);
  4571. if (devip->zsize >= capacity) {
  4572. pr_err("Zone size too large for device capacity\n");
  4573. return -EINVAL;
  4574. }
  4575. }
  4576. devip->zsize_shift = ilog2(devip->zsize);
  4577. devip->nr_zones = (capacity + devip->zsize - 1) >> devip->zsize_shift;
  4578. if (sdeb_zbc_zone_cap_mb == 0) {
  4579. devip->zcap = devip->zsize;
  4580. } else {
  4581. devip->zcap = (sdeb_zbc_zone_cap_mb * SZ_1M) >>
  4582. ilog2(sdebug_sector_size);
  4583. if (devip->zcap > devip->zsize) {
  4584. pr_err("Zone capacity too large\n");
  4585. return -EINVAL;
  4586. }
  4587. }
  4588. conv_capacity = (sector_t)sdeb_zbc_nr_conv << devip->zsize_shift;
  4589. if (conv_capacity >= capacity) {
  4590. pr_err("Number of conventional zones too large\n");
  4591. return -EINVAL;
  4592. }
  4593. devip->nr_conv_zones = sdeb_zbc_nr_conv;
  4594. devip->nr_seq_zones = ALIGN(capacity - conv_capacity, devip->zsize) >>
  4595. devip->zsize_shift;
  4596. devip->nr_zones = devip->nr_conv_zones + devip->nr_seq_zones;
  4597. /* Add gap zones if zone capacity is smaller than the zone size */
  4598. if (devip->zcap < devip->zsize)
  4599. devip->nr_zones += devip->nr_seq_zones;
  4600. if (devip->zmodel == BLK_ZONED_HM) {
  4601. /* zbc_max_open_zones can be 0, meaning "not reported" */
  4602. if (sdeb_zbc_max_open >= devip->nr_zones - 1)
  4603. devip->max_open = (devip->nr_zones - 1) / 2;
  4604. else
  4605. devip->max_open = sdeb_zbc_max_open;
  4606. }
  4607. devip->zstate = kcalloc(devip->nr_zones,
  4608. sizeof(struct sdeb_zone_state), GFP_KERNEL);
  4609. if (!devip->zstate)
  4610. return -ENOMEM;
  4611. for (i = 0; i < devip->nr_zones; i++) {
  4612. zsp = &devip->zstate[i];
  4613. zsp->z_start = zstart;
  4614. if (i < devip->nr_conv_zones) {
  4615. zsp->z_type = ZBC_ZTYPE_CNV;
  4616. zsp->z_cond = ZBC_NOT_WRITE_POINTER;
  4617. zsp->z_wp = (sector_t)-1;
  4618. zsp->z_size =
  4619. min_t(u64, devip->zsize, capacity - zstart);
  4620. } else if ((zstart & (devip->zsize - 1)) == 0) {
  4621. if (devip->zmodel == BLK_ZONED_HM)
  4622. zsp->z_type = ZBC_ZTYPE_SWR;
  4623. else
  4624. zsp->z_type = ZBC_ZTYPE_SWP;
  4625. zsp->z_cond = ZC1_EMPTY;
  4626. zsp->z_wp = zsp->z_start;
  4627. zsp->z_size =
  4628. min_t(u64, devip->zcap, capacity - zstart);
  4629. } else {
  4630. zsp->z_type = ZBC_ZTYPE_GAP;
  4631. zsp->z_cond = ZBC_NOT_WRITE_POINTER;
  4632. zsp->z_wp = (sector_t)-1;
  4633. zsp->z_size = min_t(u64, devip->zsize - devip->zcap,
  4634. capacity - zstart);
  4635. }
  4636. WARN_ON_ONCE((int)zsp->z_size <= 0);
  4637. zstart += zsp->z_size;
  4638. }
  4639. return 0;
  4640. }
  4641. static struct sdebug_dev_info *sdebug_device_create(
  4642. struct sdebug_host_info *sdbg_host, gfp_t flags)
  4643. {
  4644. struct sdebug_dev_info *devip;
  4645. devip = kzalloc(sizeof(*devip), flags);
  4646. if (devip) {
  4647. if (sdebug_uuid_ctl == 1)
  4648. uuid_gen(&devip->lu_name);
  4649. else if (sdebug_uuid_ctl == 2) {
  4650. if (got_shared_uuid)
  4651. devip->lu_name = shared_uuid;
  4652. else {
  4653. uuid_gen(&shared_uuid);
  4654. got_shared_uuid = true;
  4655. devip->lu_name = shared_uuid;
  4656. }
  4657. }
  4658. devip->sdbg_host = sdbg_host;
  4659. if (sdeb_zbc_in_use) {
  4660. devip->zmodel = sdeb_zbc_model;
  4661. if (sdebug_device_create_zones(devip)) {
  4662. kfree(devip);
  4663. return NULL;
  4664. }
  4665. } else {
  4666. devip->zmodel = BLK_ZONED_NONE;
  4667. }
  4668. devip->sdbg_host = sdbg_host;
  4669. devip->create_ts = ktime_get_boottime();
  4670. atomic_set(&devip->stopped, (sdeb_tur_ms_to_ready > 0 ? 2 : 0));
  4671. list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
  4672. }
  4673. return devip;
  4674. }
  4675. static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
  4676. {
  4677. struct sdebug_host_info *sdbg_host;
  4678. struct sdebug_dev_info *open_devip = NULL;
  4679. struct sdebug_dev_info *devip;
  4680. sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
  4681. if (!sdbg_host) {
  4682. pr_err("Host info NULL\n");
  4683. return NULL;
  4684. }
  4685. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  4686. if ((devip->used) && (devip->channel == sdev->channel) &&
  4687. (devip->target == sdev->id) &&
  4688. (devip->lun == sdev->lun))
  4689. return devip;
  4690. else {
  4691. if ((!devip->used) && (!open_devip))
  4692. open_devip = devip;
  4693. }
  4694. }
  4695. if (!open_devip) { /* try and make a new one */
  4696. open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
  4697. if (!open_devip) {
  4698. pr_err("out of memory at line %d\n", __LINE__);
  4699. return NULL;
  4700. }
  4701. }
  4702. open_devip->channel = sdev->channel;
  4703. open_devip->target = sdev->id;
  4704. open_devip->lun = sdev->lun;
  4705. open_devip->sdbg_host = sdbg_host;
  4706. atomic_set(&open_devip->num_in_q, 0);
  4707. set_bit(SDEBUG_UA_POOCCUR, open_devip->uas_bm);
  4708. open_devip->used = true;
  4709. return open_devip;
  4710. }
  4711. static int scsi_debug_slave_alloc(struct scsi_device *sdp)
  4712. {
  4713. if (sdebug_verbose)
  4714. pr_info("slave_alloc <%u %u %u %llu>\n",
  4715. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  4716. return 0;
  4717. }
  4718. static int scsi_debug_slave_configure(struct scsi_device *sdp)
  4719. {
  4720. struct sdebug_dev_info *devip =
  4721. (struct sdebug_dev_info *)sdp->hostdata;
  4722. if (sdebug_verbose)
  4723. pr_info("slave_configure <%u %u %u %llu>\n",
  4724. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  4725. if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
  4726. sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
  4727. if (devip == NULL) {
  4728. devip = find_build_dev_info(sdp);
  4729. if (devip == NULL)
  4730. return 1; /* no resources, will be marked offline */
  4731. }
  4732. sdp->hostdata = devip;
  4733. if (sdebug_no_uld)
  4734. sdp->no_uld_attach = 1;
  4735. config_cdb_len(sdp);
  4736. return 0;
  4737. }
  4738. static void scsi_debug_slave_destroy(struct scsi_device *sdp)
  4739. {
  4740. struct sdebug_dev_info *devip =
  4741. (struct sdebug_dev_info *)sdp->hostdata;
  4742. if (sdebug_verbose)
  4743. pr_info("slave_destroy <%u %u %u %llu>\n",
  4744. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  4745. if (devip) {
  4746. /* make this slot available for re-use */
  4747. devip->used = false;
  4748. sdp->hostdata = NULL;
  4749. }
  4750. }
  4751. static void stop_qc_helper(struct sdebug_defer *sd_dp,
  4752. enum sdeb_defer_type defer_t)
  4753. {
  4754. if (!sd_dp)
  4755. return;
  4756. if (defer_t == SDEB_DEFER_HRT)
  4757. hrtimer_cancel(&sd_dp->hrt);
  4758. else if (defer_t == SDEB_DEFER_WQ)
  4759. cancel_work_sync(&sd_dp->ew.work);
  4760. }
  4761. /* If @cmnd found deletes its timer or work queue and returns true; else
  4762. returns false */
  4763. static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
  4764. {
  4765. unsigned long iflags;
  4766. int j, k, qmax, r_qmax;
  4767. enum sdeb_defer_type l_defer_t;
  4768. struct sdebug_queue *sqp;
  4769. struct sdebug_queued_cmd *sqcp;
  4770. struct sdebug_dev_info *devip;
  4771. struct sdebug_defer *sd_dp;
  4772. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  4773. spin_lock_irqsave(&sqp->qc_lock, iflags);
  4774. qmax = sdebug_max_queue;
  4775. r_qmax = atomic_read(&retired_max_queue);
  4776. if (r_qmax > qmax)
  4777. qmax = r_qmax;
  4778. for (k = 0; k < qmax; ++k) {
  4779. if (test_bit(k, sqp->in_use_bm)) {
  4780. sqcp = &sqp->qc_arr[k];
  4781. if (cmnd != sqcp->a_cmnd)
  4782. continue;
  4783. /* found */
  4784. devip = (struct sdebug_dev_info *)
  4785. cmnd->device->hostdata;
  4786. if (devip)
  4787. atomic_dec(&devip->num_in_q);
  4788. sqcp->a_cmnd = NULL;
  4789. sd_dp = sqcp->sd_dp;
  4790. if (sd_dp) {
  4791. l_defer_t = READ_ONCE(sd_dp->defer_t);
  4792. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_NONE);
  4793. } else
  4794. l_defer_t = SDEB_DEFER_NONE;
  4795. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4796. stop_qc_helper(sd_dp, l_defer_t);
  4797. clear_bit(k, sqp->in_use_bm);
  4798. return true;
  4799. }
  4800. }
  4801. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4802. }
  4803. return false;
  4804. }
  4805. /* Deletes (stops) timers or work queues of all queued commands */
  4806. static void stop_all_queued(void)
  4807. {
  4808. unsigned long iflags;
  4809. int j, k;
  4810. enum sdeb_defer_type l_defer_t;
  4811. struct sdebug_queue *sqp;
  4812. struct sdebug_queued_cmd *sqcp;
  4813. struct sdebug_dev_info *devip;
  4814. struct sdebug_defer *sd_dp;
  4815. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  4816. spin_lock_irqsave(&sqp->qc_lock, iflags);
  4817. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  4818. if (test_bit(k, sqp->in_use_bm)) {
  4819. sqcp = &sqp->qc_arr[k];
  4820. if (sqcp->a_cmnd == NULL)
  4821. continue;
  4822. devip = (struct sdebug_dev_info *)
  4823. sqcp->a_cmnd->device->hostdata;
  4824. if (devip)
  4825. atomic_dec(&devip->num_in_q);
  4826. sqcp->a_cmnd = NULL;
  4827. sd_dp = sqcp->sd_dp;
  4828. if (sd_dp) {
  4829. l_defer_t = READ_ONCE(sd_dp->defer_t);
  4830. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_NONE);
  4831. } else
  4832. l_defer_t = SDEB_DEFER_NONE;
  4833. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4834. stop_qc_helper(sd_dp, l_defer_t);
  4835. clear_bit(k, sqp->in_use_bm);
  4836. spin_lock_irqsave(&sqp->qc_lock, iflags);
  4837. }
  4838. }
  4839. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4840. }
  4841. }
  4842. /* Free queued command memory on heap */
  4843. static void free_all_queued(void)
  4844. {
  4845. int j, k;
  4846. struct sdebug_queue *sqp;
  4847. struct sdebug_queued_cmd *sqcp;
  4848. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  4849. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  4850. sqcp = &sqp->qc_arr[k];
  4851. kfree(sqcp->sd_dp);
  4852. sqcp->sd_dp = NULL;
  4853. }
  4854. }
  4855. }
  4856. static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
  4857. {
  4858. bool ok;
  4859. ++num_aborts;
  4860. if (SCpnt) {
  4861. ok = stop_queued_cmnd(SCpnt);
  4862. if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  4863. sdev_printk(KERN_INFO, SCpnt->device,
  4864. "%s: command%s found\n", __func__,
  4865. ok ? "" : " not");
  4866. }
  4867. return SUCCESS;
  4868. }
  4869. static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
  4870. {
  4871. ++num_dev_resets;
  4872. if (SCpnt && SCpnt->device) {
  4873. struct scsi_device *sdp = SCpnt->device;
  4874. struct sdebug_dev_info *devip =
  4875. (struct sdebug_dev_info *)sdp->hostdata;
  4876. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  4877. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  4878. if (devip)
  4879. set_bit(SDEBUG_UA_POR, devip->uas_bm);
  4880. }
  4881. return SUCCESS;
  4882. }
  4883. static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
  4884. {
  4885. struct sdebug_host_info *sdbg_host;
  4886. struct sdebug_dev_info *devip;
  4887. struct scsi_device *sdp;
  4888. struct Scsi_Host *hp;
  4889. int k = 0;
  4890. ++num_target_resets;
  4891. if (!SCpnt)
  4892. goto lie;
  4893. sdp = SCpnt->device;
  4894. if (!sdp)
  4895. goto lie;
  4896. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  4897. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  4898. hp = sdp->host;
  4899. if (!hp)
  4900. goto lie;
  4901. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  4902. if (sdbg_host) {
  4903. list_for_each_entry(devip,
  4904. &sdbg_host->dev_info_list,
  4905. dev_list)
  4906. if (devip->target == sdp->id) {
  4907. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  4908. ++k;
  4909. }
  4910. }
  4911. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  4912. sdev_printk(KERN_INFO, sdp,
  4913. "%s: %d device(s) found in target\n", __func__, k);
  4914. lie:
  4915. return SUCCESS;
  4916. }
  4917. static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
  4918. {
  4919. struct sdebug_host_info *sdbg_host;
  4920. struct sdebug_dev_info *devip;
  4921. struct scsi_device *sdp;
  4922. struct Scsi_Host *hp;
  4923. int k = 0;
  4924. ++num_bus_resets;
  4925. if (!(SCpnt && SCpnt->device))
  4926. goto lie;
  4927. sdp = SCpnt->device;
  4928. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  4929. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  4930. hp = sdp->host;
  4931. if (hp) {
  4932. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  4933. if (sdbg_host) {
  4934. list_for_each_entry(devip,
  4935. &sdbg_host->dev_info_list,
  4936. dev_list) {
  4937. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  4938. ++k;
  4939. }
  4940. }
  4941. }
  4942. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  4943. sdev_printk(KERN_INFO, sdp,
  4944. "%s: %d device(s) found in host\n", __func__, k);
  4945. lie:
  4946. return SUCCESS;
  4947. }
  4948. static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
  4949. {
  4950. struct sdebug_host_info *sdbg_host;
  4951. struct sdebug_dev_info *devip;
  4952. int k = 0;
  4953. ++num_host_resets;
  4954. if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  4955. sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
  4956. spin_lock(&sdebug_host_list_lock);
  4957. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  4958. list_for_each_entry(devip, &sdbg_host->dev_info_list,
  4959. dev_list) {
  4960. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  4961. ++k;
  4962. }
  4963. }
  4964. spin_unlock(&sdebug_host_list_lock);
  4965. stop_all_queued();
  4966. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  4967. sdev_printk(KERN_INFO, SCpnt->device,
  4968. "%s: %d device(s) found\n", __func__, k);
  4969. return SUCCESS;
  4970. }
  4971. static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size)
  4972. {
  4973. struct msdos_partition *pp;
  4974. int starts[SDEBUG_MAX_PARTS + 2], max_part_secs;
  4975. int sectors_per_part, num_sectors, k;
  4976. int heads_by_sects, start_sec, end_sec;
  4977. /* assume partition table already zeroed */
  4978. if ((sdebug_num_parts < 1) || (store_size < 1048576))
  4979. return;
  4980. if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
  4981. sdebug_num_parts = SDEBUG_MAX_PARTS;
  4982. pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
  4983. }
  4984. num_sectors = (int)get_sdebug_capacity();
  4985. sectors_per_part = (num_sectors - sdebug_sectors_per)
  4986. / sdebug_num_parts;
  4987. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  4988. starts[0] = sdebug_sectors_per;
  4989. max_part_secs = sectors_per_part;
  4990. for (k = 1; k < sdebug_num_parts; ++k) {
  4991. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  4992. * heads_by_sects;
  4993. if (starts[k] - starts[k - 1] < max_part_secs)
  4994. max_part_secs = starts[k] - starts[k - 1];
  4995. }
  4996. starts[sdebug_num_parts] = num_sectors;
  4997. starts[sdebug_num_parts + 1] = 0;
  4998. ramp[510] = 0x55; /* magic partition markings */
  4999. ramp[511] = 0xAA;
  5000. pp = (struct msdos_partition *)(ramp + 0x1be);
  5001. for (k = 0; starts[k + 1]; ++k, ++pp) {
  5002. start_sec = starts[k];
  5003. end_sec = starts[k] + max_part_secs - 1;
  5004. pp->boot_ind = 0;
  5005. pp->cyl = start_sec / heads_by_sects;
  5006. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  5007. / sdebug_sectors_per;
  5008. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  5009. pp->end_cyl = end_sec / heads_by_sects;
  5010. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  5011. / sdebug_sectors_per;
  5012. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  5013. pp->start_sect = cpu_to_le32(start_sec);
  5014. pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
  5015. pp->sys_ind = 0x83; /* plain Linux partition */
  5016. }
  5017. }
  5018. static void block_unblock_all_queues(bool block)
  5019. {
  5020. int j;
  5021. struct sdebug_queue *sqp;
  5022. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
  5023. atomic_set(&sqp->blocked, (int)block);
  5024. }
  5025. /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
  5026. * commands will be processed normally before triggers occur.
  5027. */
  5028. static void tweak_cmnd_count(void)
  5029. {
  5030. int count, modulo;
  5031. modulo = abs(sdebug_every_nth);
  5032. if (modulo < 2)
  5033. return;
  5034. block_unblock_all_queues(true);
  5035. count = atomic_read(&sdebug_cmnd_count);
  5036. atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
  5037. block_unblock_all_queues(false);
  5038. }
  5039. static void clear_queue_stats(void)
  5040. {
  5041. atomic_set(&sdebug_cmnd_count, 0);
  5042. atomic_set(&sdebug_completions, 0);
  5043. atomic_set(&sdebug_miss_cpus, 0);
  5044. atomic_set(&sdebug_a_tsf, 0);
  5045. }
  5046. static bool inject_on_this_cmd(void)
  5047. {
  5048. if (sdebug_every_nth == 0)
  5049. return false;
  5050. return (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
  5051. }
  5052. #define INCLUSIVE_TIMING_MAX_NS 1000000 /* 1 millisecond */
  5053. /* Complete the processing of the thread that queued a SCSI command to this
  5054. * driver. It either completes the command by calling cmnd_done() or
  5055. * schedules a hr timer or work queue then returns 0. Returns
  5056. * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
  5057. */
  5058. static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
  5059. int scsi_result,
  5060. int (*pfp)(struct scsi_cmnd *,
  5061. struct sdebug_dev_info *),
  5062. int delta_jiff, int ndelay)
  5063. {
  5064. bool new_sd_dp;
  5065. bool inject = false;
  5066. bool polled = scsi_cmd_to_rq(cmnd)->cmd_flags & REQ_POLLED;
  5067. int k, num_in_q, qdepth;
  5068. unsigned long iflags;
  5069. u64 ns_from_boot = 0;
  5070. struct sdebug_queue *sqp;
  5071. struct sdebug_queued_cmd *sqcp;
  5072. struct scsi_device *sdp;
  5073. struct sdebug_defer *sd_dp;
  5074. if (unlikely(devip == NULL)) {
  5075. if (scsi_result == 0)
  5076. scsi_result = DID_NO_CONNECT << 16;
  5077. goto respond_in_thread;
  5078. }
  5079. sdp = cmnd->device;
  5080. if (delta_jiff == 0)
  5081. goto respond_in_thread;
  5082. sqp = get_queue(cmnd);
  5083. spin_lock_irqsave(&sqp->qc_lock, iflags);
  5084. if (unlikely(atomic_read(&sqp->blocked))) {
  5085. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5086. return SCSI_MLQUEUE_HOST_BUSY;
  5087. }
  5088. num_in_q = atomic_read(&devip->num_in_q);
  5089. qdepth = cmnd->device->queue_depth;
  5090. if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
  5091. if (scsi_result) {
  5092. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5093. goto respond_in_thread;
  5094. } else
  5095. scsi_result = device_qfull_result;
  5096. } else if (unlikely(sdebug_every_nth &&
  5097. (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
  5098. (scsi_result == 0))) {
  5099. if ((num_in_q == (qdepth - 1)) &&
  5100. (atomic_inc_return(&sdebug_a_tsf) >=
  5101. abs(sdebug_every_nth))) {
  5102. atomic_set(&sdebug_a_tsf, 0);
  5103. inject = true;
  5104. scsi_result = device_qfull_result;
  5105. }
  5106. }
  5107. k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
  5108. if (unlikely(k >= sdebug_max_queue)) {
  5109. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5110. if (scsi_result)
  5111. goto respond_in_thread;
  5112. scsi_result = device_qfull_result;
  5113. if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
  5114. sdev_printk(KERN_INFO, sdp, "%s: max_queue=%d exceeded: TASK SET FULL\n",
  5115. __func__, sdebug_max_queue);
  5116. goto respond_in_thread;
  5117. }
  5118. set_bit(k, sqp->in_use_bm);
  5119. atomic_inc(&devip->num_in_q);
  5120. sqcp = &sqp->qc_arr[k];
  5121. sqcp->a_cmnd = cmnd;
  5122. cmnd->host_scribble = (unsigned char *)sqcp;
  5123. sd_dp = sqcp->sd_dp;
  5124. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5125. if (!sd_dp) {
  5126. sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
  5127. if (!sd_dp) {
  5128. atomic_dec(&devip->num_in_q);
  5129. clear_bit(k, sqp->in_use_bm);
  5130. return SCSI_MLQUEUE_HOST_BUSY;
  5131. }
  5132. new_sd_dp = true;
  5133. } else {
  5134. new_sd_dp = false;
  5135. }
  5136. /* Set the hostwide tag */
  5137. if (sdebug_host_max_queue)
  5138. sd_dp->hc_idx = get_tag(cmnd);
  5139. if (polled)
  5140. ns_from_boot = ktime_get_boottime_ns();
  5141. /* one of the resp_*() response functions is called here */
  5142. cmnd->result = pfp ? pfp(cmnd, devip) : 0;
  5143. if (cmnd->result & SDEG_RES_IMMED_MASK) {
  5144. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  5145. delta_jiff = ndelay = 0;
  5146. }
  5147. if (cmnd->result == 0 && scsi_result != 0)
  5148. cmnd->result = scsi_result;
  5149. if (cmnd->result == 0 && unlikely(sdebug_opts & SDEBUG_OPT_TRANSPORT_ERR)) {
  5150. if (atomic_read(&sdeb_inject_pending)) {
  5151. mk_sense_buffer(cmnd, ABORTED_COMMAND, TRANSPORT_PROBLEM, ACK_NAK_TO);
  5152. atomic_set(&sdeb_inject_pending, 0);
  5153. cmnd->result = check_condition_result;
  5154. }
  5155. }
  5156. if (unlikely(sdebug_verbose && cmnd->result))
  5157. sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
  5158. __func__, cmnd->result);
  5159. if (delta_jiff > 0 || ndelay > 0) {
  5160. ktime_t kt;
  5161. if (delta_jiff > 0) {
  5162. u64 ns = jiffies_to_nsecs(delta_jiff);
  5163. if (sdebug_random && ns < U32_MAX) {
  5164. ns = prandom_u32_max((u32)ns);
  5165. } else if (sdebug_random) {
  5166. ns >>= 12; /* scale to 4 usec precision */
  5167. if (ns < U32_MAX) /* over 4 hours max */
  5168. ns = prandom_u32_max((u32)ns);
  5169. ns <<= 12;
  5170. }
  5171. kt = ns_to_ktime(ns);
  5172. } else { /* ndelay has a 4.2 second max */
  5173. kt = sdebug_random ? prandom_u32_max((u32)ndelay) :
  5174. (u32)ndelay;
  5175. if (ndelay < INCLUSIVE_TIMING_MAX_NS) {
  5176. u64 d = ktime_get_boottime_ns() - ns_from_boot;
  5177. if (kt <= d) { /* elapsed duration >= kt */
  5178. spin_lock_irqsave(&sqp->qc_lock, iflags);
  5179. sqcp->a_cmnd = NULL;
  5180. atomic_dec(&devip->num_in_q);
  5181. clear_bit(k, sqp->in_use_bm);
  5182. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5183. if (new_sd_dp)
  5184. kfree(sd_dp);
  5185. /* call scsi_done() from this thread */
  5186. scsi_done(cmnd);
  5187. return 0;
  5188. }
  5189. /* otherwise reduce kt by elapsed time */
  5190. kt -= d;
  5191. }
  5192. }
  5193. if (polled) {
  5194. sd_dp->cmpl_ts = ktime_add(ns_to_ktime(ns_from_boot), kt);
  5195. spin_lock_irqsave(&sqp->qc_lock, iflags);
  5196. if (!sd_dp->init_poll) {
  5197. sd_dp->init_poll = true;
  5198. sqcp->sd_dp = sd_dp;
  5199. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  5200. sd_dp->qc_idx = k;
  5201. }
  5202. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_POLL);
  5203. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5204. } else {
  5205. if (!sd_dp->init_hrt) {
  5206. sd_dp->init_hrt = true;
  5207. sqcp->sd_dp = sd_dp;
  5208. hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
  5209. HRTIMER_MODE_REL_PINNED);
  5210. sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
  5211. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  5212. sd_dp->qc_idx = k;
  5213. }
  5214. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_HRT);
  5215. /* schedule the invocation of scsi_done() for a later time */
  5216. hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
  5217. }
  5218. if (sdebug_statistics)
  5219. sd_dp->issuing_cpu = raw_smp_processor_id();
  5220. } else { /* jdelay < 0, use work queue */
  5221. if (unlikely((sdebug_opts & SDEBUG_OPT_CMD_ABORT) &&
  5222. atomic_read(&sdeb_inject_pending)))
  5223. sd_dp->aborted = true;
  5224. if (polled) {
  5225. sd_dp->cmpl_ts = ns_to_ktime(ns_from_boot);
  5226. spin_lock_irqsave(&sqp->qc_lock, iflags);
  5227. if (!sd_dp->init_poll) {
  5228. sd_dp->init_poll = true;
  5229. sqcp->sd_dp = sd_dp;
  5230. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  5231. sd_dp->qc_idx = k;
  5232. }
  5233. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_POLL);
  5234. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  5235. } else {
  5236. if (!sd_dp->init_wq) {
  5237. sd_dp->init_wq = true;
  5238. sqcp->sd_dp = sd_dp;
  5239. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  5240. sd_dp->qc_idx = k;
  5241. INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
  5242. }
  5243. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_WQ);
  5244. schedule_work(&sd_dp->ew.work);
  5245. }
  5246. if (sdebug_statistics)
  5247. sd_dp->issuing_cpu = raw_smp_processor_id();
  5248. if (unlikely(sd_dp->aborted)) {
  5249. sdev_printk(KERN_INFO, sdp, "abort request tag %d\n",
  5250. scsi_cmd_to_rq(cmnd)->tag);
  5251. blk_abort_request(scsi_cmd_to_rq(cmnd));
  5252. atomic_set(&sdeb_inject_pending, 0);
  5253. sd_dp->aborted = false;
  5254. }
  5255. }
  5256. if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) && scsi_result == device_qfull_result))
  5257. sdev_printk(KERN_INFO, sdp, "%s: num_in_q=%d +1, %s%s\n", __func__,
  5258. num_in_q, (inject ? "<inject> " : ""), "status: TASK SET FULL");
  5259. return 0;
  5260. respond_in_thread: /* call back to mid-layer using invocation thread */
  5261. cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
  5262. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  5263. if (cmnd->result == 0 && scsi_result != 0)
  5264. cmnd->result = scsi_result;
  5265. scsi_done(cmnd);
  5266. return 0;
  5267. }
  5268. /* Note: The following macros create attribute files in the
  5269. /sys/module/scsi_debug/parameters directory. Unfortunately this
  5270. driver is unaware of a change and cannot trigger auxiliary actions
  5271. as it can when the corresponding attribute in the
  5272. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  5273. */
  5274. module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
  5275. module_param_named(ato, sdebug_ato, int, S_IRUGO);
  5276. module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
  5277. module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
  5278. module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
  5279. module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
  5280. module_param_named(dif, sdebug_dif, int, S_IRUGO);
  5281. module_param_named(dix, sdebug_dix, int, S_IRUGO);
  5282. module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
  5283. module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
  5284. module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
  5285. module_param_named(guard, sdebug_guard, uint, S_IRUGO);
  5286. module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
  5287. module_param_named(host_max_queue, sdebug_host_max_queue, int, S_IRUGO);
  5288. module_param_string(inq_product, sdebug_inq_product_id,
  5289. sizeof(sdebug_inq_product_id), S_IRUGO | S_IWUSR);
  5290. module_param_string(inq_rev, sdebug_inq_product_rev,
  5291. sizeof(sdebug_inq_product_rev), S_IRUGO | S_IWUSR);
  5292. module_param_string(inq_vendor, sdebug_inq_vendor_id,
  5293. sizeof(sdebug_inq_vendor_id), S_IRUGO | S_IWUSR);
  5294. module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
  5295. module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
  5296. module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
  5297. module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
  5298. module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
  5299. module_param_named(lun_format, sdebug_lun_am_i, int, S_IRUGO | S_IWUSR);
  5300. module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
  5301. module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
  5302. module_param_named(medium_error_count, sdebug_medium_error_count, int,
  5303. S_IRUGO | S_IWUSR);
  5304. module_param_named(medium_error_start, sdebug_medium_error_start, int,
  5305. S_IRUGO | S_IWUSR);
  5306. module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
  5307. module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
  5308. module_param_named(no_rwlock, sdebug_no_rwlock, bool, S_IRUGO | S_IWUSR);
  5309. module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
  5310. module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
  5311. module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
  5312. module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
  5313. module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
  5314. module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
  5315. module_param_named(per_host_store, sdebug_per_host_store, bool,
  5316. S_IRUGO | S_IWUSR);
  5317. module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
  5318. module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
  5319. module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR);
  5320. module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
  5321. module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
  5322. module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
  5323. module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
  5324. module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
  5325. module_param_named(submit_queues, submit_queues, int, S_IRUGO);
  5326. module_param_named(poll_queues, poll_queues, int, S_IRUGO);
  5327. module_param_named(tur_ms_to_ready, sdeb_tur_ms_to_ready, int, S_IRUGO);
  5328. module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
  5329. module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
  5330. module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
  5331. module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
  5332. module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
  5333. module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
  5334. module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
  5335. S_IRUGO | S_IWUSR);
  5336. module_param_named(wp, sdebug_wp, bool, S_IRUGO | S_IWUSR);
  5337. module_param_named(write_same_length, sdebug_write_same_length, int,
  5338. S_IRUGO | S_IWUSR);
  5339. module_param_named(zbc, sdeb_zbc_model_s, charp, S_IRUGO);
  5340. module_param_named(zone_cap_mb, sdeb_zbc_zone_cap_mb, int, S_IRUGO);
  5341. module_param_named(zone_max_open, sdeb_zbc_max_open, int, S_IRUGO);
  5342. module_param_named(zone_nr_conv, sdeb_zbc_nr_conv, int, S_IRUGO);
  5343. module_param_named(zone_size_mb, sdeb_zbc_zone_size_mb, int, S_IRUGO);
  5344. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  5345. MODULE_DESCRIPTION("SCSI debug adapter driver");
  5346. MODULE_LICENSE("GPL");
  5347. MODULE_VERSION(SDEBUG_VERSION);
  5348. MODULE_PARM_DESC(add_host, "add n hosts, in sysfs if negative remove host(s) (def=1)");
  5349. MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
  5350. MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
  5351. MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
  5352. MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
  5353. MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
  5354. MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
  5355. MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
  5356. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  5357. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  5358. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  5359. MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
  5360. MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
  5361. MODULE_PARM_DESC(host_max_queue,
  5362. "host max # of queued cmds (0 to max(def) [max_queue fixed equal for !0])");
  5363. MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
  5364. MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
  5365. SDEBUG_VERSION "\")");
  5366. MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
  5367. MODULE_PARM_DESC(lbprz,
  5368. "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
  5369. MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
  5370. MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
  5371. MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
  5372. MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
  5373. MODULE_PARM_DESC(lun_format, "LUN format: 0->peripheral (def); 1 --> flat address method");
  5374. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  5375. MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
  5376. MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
  5377. MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
  5378. MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
  5379. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  5380. MODULE_PARM_DESC(no_rwlock, "don't protect user data reads+writes (def=0)");
  5381. MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
  5382. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  5383. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  5384. MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
  5385. MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
  5386. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  5387. MODULE_PARM_DESC(per_host_store, "If set, next positive add_host will get new store (def=0)");
  5388. MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
  5389. MODULE_PARM_DESC(poll_queues, "support for iouring iopoll queues (1 to max(submit_queues - 1))");
  5390. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  5391. MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns");
  5392. MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
  5393. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
  5394. MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
  5395. MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
  5396. MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
  5397. MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
  5398. MODULE_PARM_DESC(tur_ms_to_ready, "TEST UNIT READY millisecs before initial good status (def=0)");
  5399. MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
  5400. MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
  5401. MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
  5402. MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
  5403. MODULE_PARM_DESC(uuid_ctl,
  5404. "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
  5405. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
  5406. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  5407. MODULE_PARM_DESC(wp, "Write Protect (def=0)");
  5408. MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
  5409. MODULE_PARM_DESC(zbc, "'none' [0]; 'aware' [1]; 'managed' [2] (def=0). Can have 'host-' prefix");
  5410. MODULE_PARM_DESC(zone_cap_mb, "Zone capacity in MiB (def=zone size)");
  5411. MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones; [0] for no limit (def=auto)");
  5412. MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones (def=1)");
  5413. MODULE_PARM_DESC(zone_size_mb, "Zone size in MiB (def=auto)");
  5414. #define SDEBUG_INFO_LEN 256
  5415. static char sdebug_info[SDEBUG_INFO_LEN];
  5416. static const char *scsi_debug_info(struct Scsi_Host *shp)
  5417. {
  5418. int k;
  5419. k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
  5420. my_name, SDEBUG_VERSION, sdebug_version_date);
  5421. if (k >= (SDEBUG_INFO_LEN - 1))
  5422. return sdebug_info;
  5423. scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
  5424. " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
  5425. sdebug_dev_size_mb, sdebug_opts, submit_queues,
  5426. "statistics", (int)sdebug_statistics);
  5427. return sdebug_info;
  5428. }
  5429. /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
  5430. static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
  5431. int length)
  5432. {
  5433. char arr[16];
  5434. int opts;
  5435. int minLen = length > 15 ? 15 : length;
  5436. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  5437. return -EACCES;
  5438. memcpy(arr, buffer, minLen);
  5439. arr[minLen] = '\0';
  5440. if (1 != sscanf(arr, "%d", &opts))
  5441. return -EINVAL;
  5442. sdebug_opts = opts;
  5443. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  5444. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  5445. if (sdebug_every_nth != 0)
  5446. tweak_cmnd_count();
  5447. return length;
  5448. }
  5449. /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
  5450. * same for each scsi_debug host (if more than one). Some of the counters
  5451. * output are not atomics so might be inaccurate in a busy system. */
  5452. static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
  5453. {
  5454. int f, j, l;
  5455. struct sdebug_queue *sqp;
  5456. struct sdebug_host_info *sdhp;
  5457. seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
  5458. SDEBUG_VERSION, sdebug_version_date);
  5459. seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
  5460. sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
  5461. sdebug_opts, sdebug_every_nth);
  5462. seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
  5463. sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
  5464. sdebug_sector_size, "bytes");
  5465. seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
  5466. sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  5467. num_aborts);
  5468. seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
  5469. num_dev_resets, num_target_resets, num_bus_resets,
  5470. num_host_resets);
  5471. seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
  5472. dix_reads, dix_writes, dif_errors);
  5473. seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
  5474. sdebug_statistics);
  5475. seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d, mq_polls=%d\n",
  5476. atomic_read(&sdebug_cmnd_count),
  5477. atomic_read(&sdebug_completions),
  5478. "miss_cpus", atomic_read(&sdebug_miss_cpus),
  5479. atomic_read(&sdebug_a_tsf),
  5480. atomic_read(&sdeb_mq_poll_count));
  5481. seq_printf(m, "submit_queues=%d\n", submit_queues);
  5482. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  5483. seq_printf(m, " queue %d:\n", j);
  5484. f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
  5485. if (f != sdebug_max_queue) {
  5486. l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
  5487. seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n",
  5488. "first,last bits", f, l);
  5489. }
  5490. }
  5491. seq_printf(m, "this host_no=%d\n", host->host_no);
  5492. if (!xa_empty(per_store_ap)) {
  5493. bool niu;
  5494. int idx;
  5495. unsigned long l_idx;
  5496. struct sdeb_store_info *sip;
  5497. seq_puts(m, "\nhost list:\n");
  5498. j = 0;
  5499. list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
  5500. idx = sdhp->si_idx;
  5501. seq_printf(m, " %d: host_no=%d, si_idx=%d\n", j,
  5502. sdhp->shost->host_no, idx);
  5503. ++j;
  5504. }
  5505. seq_printf(m, "\nper_store array [most_recent_idx=%d]:\n",
  5506. sdeb_most_recent_idx);
  5507. j = 0;
  5508. xa_for_each(per_store_ap, l_idx, sip) {
  5509. niu = xa_get_mark(per_store_ap, l_idx,
  5510. SDEB_XA_NOT_IN_USE);
  5511. idx = (int)l_idx;
  5512. seq_printf(m, " %d: idx=%d%s\n", j, idx,
  5513. (niu ? " not_in_use" : ""));
  5514. ++j;
  5515. }
  5516. }
  5517. return 0;
  5518. }
  5519. static ssize_t delay_show(struct device_driver *ddp, char *buf)
  5520. {
  5521. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
  5522. }
  5523. /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
  5524. * of delay is jiffies.
  5525. */
  5526. static ssize_t delay_store(struct device_driver *ddp, const char *buf,
  5527. size_t count)
  5528. {
  5529. int jdelay, res;
  5530. if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
  5531. res = count;
  5532. if (sdebug_jdelay != jdelay) {
  5533. int j, k;
  5534. struct sdebug_queue *sqp;
  5535. block_unblock_all_queues(true);
  5536. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  5537. ++j, ++sqp) {
  5538. k = find_first_bit(sqp->in_use_bm,
  5539. sdebug_max_queue);
  5540. if (k != sdebug_max_queue) {
  5541. res = -EBUSY; /* queued commands */
  5542. break;
  5543. }
  5544. }
  5545. if (res > 0) {
  5546. sdebug_jdelay = jdelay;
  5547. sdebug_ndelay = 0;
  5548. }
  5549. block_unblock_all_queues(false);
  5550. }
  5551. return res;
  5552. }
  5553. return -EINVAL;
  5554. }
  5555. static DRIVER_ATTR_RW(delay);
  5556. static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
  5557. {
  5558. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
  5559. }
  5560. /* Returns -EBUSY if ndelay is being changed and commands are queued */
  5561. /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
  5562. static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
  5563. size_t count)
  5564. {
  5565. int ndelay, res;
  5566. if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
  5567. (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
  5568. res = count;
  5569. if (sdebug_ndelay != ndelay) {
  5570. int j, k;
  5571. struct sdebug_queue *sqp;
  5572. block_unblock_all_queues(true);
  5573. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  5574. ++j, ++sqp) {
  5575. k = find_first_bit(sqp->in_use_bm,
  5576. sdebug_max_queue);
  5577. if (k != sdebug_max_queue) {
  5578. res = -EBUSY; /* queued commands */
  5579. break;
  5580. }
  5581. }
  5582. if (res > 0) {
  5583. sdebug_ndelay = ndelay;
  5584. sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN
  5585. : DEF_JDELAY;
  5586. }
  5587. block_unblock_all_queues(false);
  5588. }
  5589. return res;
  5590. }
  5591. return -EINVAL;
  5592. }
  5593. static DRIVER_ATTR_RW(ndelay);
  5594. static ssize_t opts_show(struct device_driver *ddp, char *buf)
  5595. {
  5596. return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
  5597. }
  5598. static ssize_t opts_store(struct device_driver *ddp, const char *buf,
  5599. size_t count)
  5600. {
  5601. int opts;
  5602. char work[20];
  5603. if (sscanf(buf, "%10s", work) == 1) {
  5604. if (strncasecmp(work, "0x", 2) == 0) {
  5605. if (kstrtoint(work + 2, 16, &opts) == 0)
  5606. goto opts_done;
  5607. } else {
  5608. if (kstrtoint(work, 10, &opts) == 0)
  5609. goto opts_done;
  5610. }
  5611. }
  5612. return -EINVAL;
  5613. opts_done:
  5614. sdebug_opts = opts;
  5615. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  5616. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  5617. tweak_cmnd_count();
  5618. return count;
  5619. }
  5620. static DRIVER_ATTR_RW(opts);
  5621. static ssize_t ptype_show(struct device_driver *ddp, char *buf)
  5622. {
  5623. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
  5624. }
  5625. static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
  5626. size_t count)
  5627. {
  5628. int n;
  5629. /* Cannot change from or to TYPE_ZBC with sysfs */
  5630. if (sdebug_ptype == TYPE_ZBC)
  5631. return -EINVAL;
  5632. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5633. if (n == TYPE_ZBC)
  5634. return -EINVAL;
  5635. sdebug_ptype = n;
  5636. return count;
  5637. }
  5638. return -EINVAL;
  5639. }
  5640. static DRIVER_ATTR_RW(ptype);
  5641. static ssize_t dsense_show(struct device_driver *ddp, char *buf)
  5642. {
  5643. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
  5644. }
  5645. static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
  5646. size_t count)
  5647. {
  5648. int n;
  5649. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5650. sdebug_dsense = n;
  5651. return count;
  5652. }
  5653. return -EINVAL;
  5654. }
  5655. static DRIVER_ATTR_RW(dsense);
  5656. static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
  5657. {
  5658. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
  5659. }
  5660. static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
  5661. size_t count)
  5662. {
  5663. int n, idx;
  5664. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5665. bool want_store = (n == 0);
  5666. struct sdebug_host_info *sdhp;
  5667. n = (n > 0);
  5668. sdebug_fake_rw = (sdebug_fake_rw > 0);
  5669. if (sdebug_fake_rw == n)
  5670. return count; /* not transitioning so do nothing */
  5671. if (want_store) { /* 1 --> 0 transition, set up store */
  5672. if (sdeb_first_idx < 0) {
  5673. idx = sdebug_add_store();
  5674. if (idx < 0)
  5675. return idx;
  5676. } else {
  5677. idx = sdeb_first_idx;
  5678. xa_clear_mark(per_store_ap, idx,
  5679. SDEB_XA_NOT_IN_USE);
  5680. }
  5681. /* make all hosts use same store */
  5682. list_for_each_entry(sdhp, &sdebug_host_list,
  5683. host_list) {
  5684. if (sdhp->si_idx != idx) {
  5685. xa_set_mark(per_store_ap, sdhp->si_idx,
  5686. SDEB_XA_NOT_IN_USE);
  5687. sdhp->si_idx = idx;
  5688. }
  5689. }
  5690. sdeb_most_recent_idx = idx;
  5691. } else { /* 0 --> 1 transition is trigger for shrink */
  5692. sdebug_erase_all_stores(true /* apart from first */);
  5693. }
  5694. sdebug_fake_rw = n;
  5695. return count;
  5696. }
  5697. return -EINVAL;
  5698. }
  5699. static DRIVER_ATTR_RW(fake_rw);
  5700. static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
  5701. {
  5702. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
  5703. }
  5704. static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
  5705. size_t count)
  5706. {
  5707. int n;
  5708. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5709. sdebug_no_lun_0 = n;
  5710. return count;
  5711. }
  5712. return -EINVAL;
  5713. }
  5714. static DRIVER_ATTR_RW(no_lun_0);
  5715. static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
  5716. {
  5717. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
  5718. }
  5719. static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
  5720. size_t count)
  5721. {
  5722. int n;
  5723. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5724. sdebug_num_tgts = n;
  5725. sdebug_max_tgts_luns();
  5726. return count;
  5727. }
  5728. return -EINVAL;
  5729. }
  5730. static DRIVER_ATTR_RW(num_tgts);
  5731. static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
  5732. {
  5733. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
  5734. }
  5735. static DRIVER_ATTR_RO(dev_size_mb);
  5736. static ssize_t per_host_store_show(struct device_driver *ddp, char *buf)
  5737. {
  5738. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_per_host_store);
  5739. }
  5740. static ssize_t per_host_store_store(struct device_driver *ddp, const char *buf,
  5741. size_t count)
  5742. {
  5743. bool v;
  5744. if (kstrtobool(buf, &v))
  5745. return -EINVAL;
  5746. sdebug_per_host_store = v;
  5747. return count;
  5748. }
  5749. static DRIVER_ATTR_RW(per_host_store);
  5750. static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
  5751. {
  5752. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
  5753. }
  5754. static DRIVER_ATTR_RO(num_parts);
  5755. static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
  5756. {
  5757. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
  5758. }
  5759. static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
  5760. size_t count)
  5761. {
  5762. int nth;
  5763. char work[20];
  5764. if (sscanf(buf, "%10s", work) == 1) {
  5765. if (strncasecmp(work, "0x", 2) == 0) {
  5766. if (kstrtoint(work + 2, 16, &nth) == 0)
  5767. goto every_nth_done;
  5768. } else {
  5769. if (kstrtoint(work, 10, &nth) == 0)
  5770. goto every_nth_done;
  5771. }
  5772. }
  5773. return -EINVAL;
  5774. every_nth_done:
  5775. sdebug_every_nth = nth;
  5776. if (nth && !sdebug_statistics) {
  5777. pr_info("every_nth needs statistics=1, set it\n");
  5778. sdebug_statistics = true;
  5779. }
  5780. tweak_cmnd_count();
  5781. return count;
  5782. }
  5783. static DRIVER_ATTR_RW(every_nth);
  5784. static ssize_t lun_format_show(struct device_driver *ddp, char *buf)
  5785. {
  5786. return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_lun_am);
  5787. }
  5788. static ssize_t lun_format_store(struct device_driver *ddp, const char *buf,
  5789. size_t count)
  5790. {
  5791. int n;
  5792. bool changed;
  5793. if (kstrtoint(buf, 0, &n))
  5794. return -EINVAL;
  5795. if (n >= 0) {
  5796. if (n > (int)SAM_LUN_AM_FLAT) {
  5797. pr_warn("only LUN address methods 0 and 1 are supported\n");
  5798. return -EINVAL;
  5799. }
  5800. changed = ((int)sdebug_lun_am != n);
  5801. sdebug_lun_am = n;
  5802. if (changed && sdebug_scsi_level >= 5) { /* >= SPC-3 */
  5803. struct sdebug_host_info *sdhp;
  5804. struct sdebug_dev_info *dp;
  5805. spin_lock(&sdebug_host_list_lock);
  5806. list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
  5807. list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
  5808. set_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
  5809. }
  5810. }
  5811. spin_unlock(&sdebug_host_list_lock);
  5812. }
  5813. return count;
  5814. }
  5815. return -EINVAL;
  5816. }
  5817. static DRIVER_ATTR_RW(lun_format);
  5818. static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
  5819. {
  5820. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
  5821. }
  5822. static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
  5823. size_t count)
  5824. {
  5825. int n;
  5826. bool changed;
  5827. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5828. if (n > 256) {
  5829. pr_warn("max_luns can be no more than 256\n");
  5830. return -EINVAL;
  5831. }
  5832. changed = (sdebug_max_luns != n);
  5833. sdebug_max_luns = n;
  5834. sdebug_max_tgts_luns();
  5835. if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */
  5836. struct sdebug_host_info *sdhp;
  5837. struct sdebug_dev_info *dp;
  5838. spin_lock(&sdebug_host_list_lock);
  5839. list_for_each_entry(sdhp, &sdebug_host_list,
  5840. host_list) {
  5841. list_for_each_entry(dp, &sdhp->dev_info_list,
  5842. dev_list) {
  5843. set_bit(SDEBUG_UA_LUNS_CHANGED,
  5844. dp->uas_bm);
  5845. }
  5846. }
  5847. spin_unlock(&sdebug_host_list_lock);
  5848. }
  5849. return count;
  5850. }
  5851. return -EINVAL;
  5852. }
  5853. static DRIVER_ATTR_RW(max_luns);
  5854. static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
  5855. {
  5856. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
  5857. }
  5858. /* N.B. max_queue can be changed while there are queued commands. In flight
  5859. * commands beyond the new max_queue will be completed. */
  5860. static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
  5861. size_t count)
  5862. {
  5863. int j, n, k, a;
  5864. struct sdebug_queue *sqp;
  5865. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
  5866. (n <= SDEBUG_CANQUEUE) &&
  5867. (sdebug_host_max_queue == 0)) {
  5868. block_unblock_all_queues(true);
  5869. k = 0;
  5870. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  5871. ++j, ++sqp) {
  5872. a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
  5873. if (a > k)
  5874. k = a;
  5875. }
  5876. sdebug_max_queue = n;
  5877. if (k == SDEBUG_CANQUEUE)
  5878. atomic_set(&retired_max_queue, 0);
  5879. else if (k >= n)
  5880. atomic_set(&retired_max_queue, k + 1);
  5881. else
  5882. atomic_set(&retired_max_queue, 0);
  5883. block_unblock_all_queues(false);
  5884. return count;
  5885. }
  5886. return -EINVAL;
  5887. }
  5888. static DRIVER_ATTR_RW(max_queue);
  5889. static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf)
  5890. {
  5891. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_host_max_queue);
  5892. }
  5893. static ssize_t no_rwlock_show(struct device_driver *ddp, char *buf)
  5894. {
  5895. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_rwlock);
  5896. }
  5897. static ssize_t no_rwlock_store(struct device_driver *ddp, const char *buf, size_t count)
  5898. {
  5899. bool v;
  5900. if (kstrtobool(buf, &v))
  5901. return -EINVAL;
  5902. sdebug_no_rwlock = v;
  5903. return count;
  5904. }
  5905. static DRIVER_ATTR_RW(no_rwlock);
  5906. /*
  5907. * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap
  5908. * in range [0, sdebug_host_max_queue), we can't change it.
  5909. */
  5910. static DRIVER_ATTR_RO(host_max_queue);
  5911. static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
  5912. {
  5913. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
  5914. }
  5915. static DRIVER_ATTR_RO(no_uld);
  5916. static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
  5917. {
  5918. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
  5919. }
  5920. static DRIVER_ATTR_RO(scsi_level);
  5921. static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
  5922. {
  5923. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
  5924. }
  5925. static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
  5926. size_t count)
  5927. {
  5928. int n;
  5929. bool changed;
  5930. /* Ignore capacity change for ZBC drives for now */
  5931. if (sdeb_zbc_in_use)
  5932. return -ENOTSUPP;
  5933. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  5934. changed = (sdebug_virtual_gb != n);
  5935. sdebug_virtual_gb = n;
  5936. sdebug_capacity = get_sdebug_capacity();
  5937. if (changed) {
  5938. struct sdebug_host_info *sdhp;
  5939. struct sdebug_dev_info *dp;
  5940. spin_lock(&sdebug_host_list_lock);
  5941. list_for_each_entry(sdhp, &sdebug_host_list,
  5942. host_list) {
  5943. list_for_each_entry(dp, &sdhp->dev_info_list,
  5944. dev_list) {
  5945. set_bit(SDEBUG_UA_CAPACITY_CHANGED,
  5946. dp->uas_bm);
  5947. }
  5948. }
  5949. spin_unlock(&sdebug_host_list_lock);
  5950. }
  5951. return count;
  5952. }
  5953. return -EINVAL;
  5954. }
  5955. static DRIVER_ATTR_RW(virtual_gb);
  5956. static ssize_t add_host_show(struct device_driver *ddp, char *buf)
  5957. {
  5958. /* absolute number of hosts currently active is what is shown */
  5959. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_hosts);
  5960. }
  5961. static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
  5962. size_t count)
  5963. {
  5964. bool found;
  5965. unsigned long idx;
  5966. struct sdeb_store_info *sip;
  5967. bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
  5968. int delta_hosts;
  5969. if (sscanf(buf, "%d", &delta_hosts) != 1)
  5970. return -EINVAL;
  5971. if (delta_hosts > 0) {
  5972. do {
  5973. found = false;
  5974. if (want_phs) {
  5975. xa_for_each_marked(per_store_ap, idx, sip,
  5976. SDEB_XA_NOT_IN_USE) {
  5977. sdeb_most_recent_idx = (int)idx;
  5978. found = true;
  5979. break;
  5980. }
  5981. if (found) /* re-use case */
  5982. sdebug_add_host_helper((int)idx);
  5983. else
  5984. sdebug_do_add_host(true);
  5985. } else {
  5986. sdebug_do_add_host(false);
  5987. }
  5988. } while (--delta_hosts);
  5989. } else if (delta_hosts < 0) {
  5990. do {
  5991. sdebug_do_remove_host(false);
  5992. } while (++delta_hosts);
  5993. }
  5994. return count;
  5995. }
  5996. static DRIVER_ATTR_RW(add_host);
  5997. static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
  5998. {
  5999. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
  6000. }
  6001. static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
  6002. size_t count)
  6003. {
  6004. int n;
  6005. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  6006. sdebug_vpd_use_hostno = n;
  6007. return count;
  6008. }
  6009. return -EINVAL;
  6010. }
  6011. static DRIVER_ATTR_RW(vpd_use_hostno);
  6012. static ssize_t statistics_show(struct device_driver *ddp, char *buf)
  6013. {
  6014. return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
  6015. }
  6016. static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
  6017. size_t count)
  6018. {
  6019. int n;
  6020. if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
  6021. if (n > 0)
  6022. sdebug_statistics = true;
  6023. else {
  6024. clear_queue_stats();
  6025. sdebug_statistics = false;
  6026. }
  6027. return count;
  6028. }
  6029. return -EINVAL;
  6030. }
  6031. static DRIVER_ATTR_RW(statistics);
  6032. static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
  6033. {
  6034. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
  6035. }
  6036. static DRIVER_ATTR_RO(sector_size);
  6037. static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
  6038. {
  6039. return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
  6040. }
  6041. static DRIVER_ATTR_RO(submit_queues);
  6042. static ssize_t dix_show(struct device_driver *ddp, char *buf)
  6043. {
  6044. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
  6045. }
  6046. static DRIVER_ATTR_RO(dix);
  6047. static ssize_t dif_show(struct device_driver *ddp, char *buf)
  6048. {
  6049. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
  6050. }
  6051. static DRIVER_ATTR_RO(dif);
  6052. static ssize_t guard_show(struct device_driver *ddp, char *buf)
  6053. {
  6054. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
  6055. }
  6056. static DRIVER_ATTR_RO(guard);
  6057. static ssize_t ato_show(struct device_driver *ddp, char *buf)
  6058. {
  6059. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
  6060. }
  6061. static DRIVER_ATTR_RO(ato);
  6062. static ssize_t map_show(struct device_driver *ddp, char *buf)
  6063. {
  6064. ssize_t count = 0;
  6065. if (!scsi_debug_lbp())
  6066. return scnprintf(buf, PAGE_SIZE, "0-%u\n",
  6067. sdebug_store_sectors);
  6068. if (sdebug_fake_rw == 0 && !xa_empty(per_store_ap)) {
  6069. struct sdeb_store_info *sip = xa_load(per_store_ap, 0);
  6070. if (sip)
  6071. count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  6072. (int)map_size, sip->map_storep);
  6073. }
  6074. buf[count++] = '\n';
  6075. buf[count] = '\0';
  6076. return count;
  6077. }
  6078. static DRIVER_ATTR_RO(map);
  6079. static ssize_t random_show(struct device_driver *ddp, char *buf)
  6080. {
  6081. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_random);
  6082. }
  6083. static ssize_t random_store(struct device_driver *ddp, const char *buf,
  6084. size_t count)
  6085. {
  6086. bool v;
  6087. if (kstrtobool(buf, &v))
  6088. return -EINVAL;
  6089. sdebug_random = v;
  6090. return count;
  6091. }
  6092. static DRIVER_ATTR_RW(random);
  6093. static ssize_t removable_show(struct device_driver *ddp, char *buf)
  6094. {
  6095. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
  6096. }
  6097. static ssize_t removable_store(struct device_driver *ddp, const char *buf,
  6098. size_t count)
  6099. {
  6100. int n;
  6101. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  6102. sdebug_removable = (n > 0);
  6103. return count;
  6104. }
  6105. return -EINVAL;
  6106. }
  6107. static DRIVER_ATTR_RW(removable);
  6108. static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
  6109. {
  6110. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
  6111. }
  6112. /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
  6113. static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
  6114. size_t count)
  6115. {
  6116. int n;
  6117. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  6118. sdebug_host_lock = (n > 0);
  6119. return count;
  6120. }
  6121. return -EINVAL;
  6122. }
  6123. static DRIVER_ATTR_RW(host_lock);
  6124. static ssize_t strict_show(struct device_driver *ddp, char *buf)
  6125. {
  6126. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
  6127. }
  6128. static ssize_t strict_store(struct device_driver *ddp, const char *buf,
  6129. size_t count)
  6130. {
  6131. int n;
  6132. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  6133. sdebug_strict = (n > 0);
  6134. return count;
  6135. }
  6136. return -EINVAL;
  6137. }
  6138. static DRIVER_ATTR_RW(strict);
  6139. static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
  6140. {
  6141. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
  6142. }
  6143. static DRIVER_ATTR_RO(uuid_ctl);
  6144. static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
  6145. {
  6146. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
  6147. }
  6148. static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
  6149. size_t count)
  6150. {
  6151. int ret, n;
  6152. ret = kstrtoint(buf, 0, &n);
  6153. if (ret)
  6154. return ret;
  6155. sdebug_cdb_len = n;
  6156. all_config_cdb_len();
  6157. return count;
  6158. }
  6159. static DRIVER_ATTR_RW(cdb_len);
  6160. static const char * const zbc_model_strs_a[] = {
  6161. [BLK_ZONED_NONE] = "none",
  6162. [BLK_ZONED_HA] = "host-aware",
  6163. [BLK_ZONED_HM] = "host-managed",
  6164. };
  6165. static const char * const zbc_model_strs_b[] = {
  6166. [BLK_ZONED_NONE] = "no",
  6167. [BLK_ZONED_HA] = "aware",
  6168. [BLK_ZONED_HM] = "managed",
  6169. };
  6170. static const char * const zbc_model_strs_c[] = {
  6171. [BLK_ZONED_NONE] = "0",
  6172. [BLK_ZONED_HA] = "1",
  6173. [BLK_ZONED_HM] = "2",
  6174. };
  6175. static int sdeb_zbc_model_str(const char *cp)
  6176. {
  6177. int res = sysfs_match_string(zbc_model_strs_a, cp);
  6178. if (res < 0) {
  6179. res = sysfs_match_string(zbc_model_strs_b, cp);
  6180. if (res < 0) {
  6181. res = sysfs_match_string(zbc_model_strs_c, cp);
  6182. if (res < 0)
  6183. return -EINVAL;
  6184. }
  6185. }
  6186. return res;
  6187. }
  6188. static ssize_t zbc_show(struct device_driver *ddp, char *buf)
  6189. {
  6190. return scnprintf(buf, PAGE_SIZE, "%s\n",
  6191. zbc_model_strs_a[sdeb_zbc_model]);
  6192. }
  6193. static DRIVER_ATTR_RO(zbc);
  6194. static ssize_t tur_ms_to_ready_show(struct device_driver *ddp, char *buf)
  6195. {
  6196. return scnprintf(buf, PAGE_SIZE, "%d\n", sdeb_tur_ms_to_ready);
  6197. }
  6198. static DRIVER_ATTR_RO(tur_ms_to_ready);
  6199. /* Note: The following array creates attribute files in the
  6200. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  6201. files (over those found in the /sys/module/scsi_debug/parameters
  6202. directory) is that auxiliary actions can be triggered when an attribute
  6203. is changed. For example see: add_host_store() above.
  6204. */
  6205. static struct attribute *sdebug_drv_attrs[] = {
  6206. &driver_attr_delay.attr,
  6207. &driver_attr_opts.attr,
  6208. &driver_attr_ptype.attr,
  6209. &driver_attr_dsense.attr,
  6210. &driver_attr_fake_rw.attr,
  6211. &driver_attr_host_max_queue.attr,
  6212. &driver_attr_no_lun_0.attr,
  6213. &driver_attr_num_tgts.attr,
  6214. &driver_attr_dev_size_mb.attr,
  6215. &driver_attr_num_parts.attr,
  6216. &driver_attr_every_nth.attr,
  6217. &driver_attr_lun_format.attr,
  6218. &driver_attr_max_luns.attr,
  6219. &driver_attr_max_queue.attr,
  6220. &driver_attr_no_rwlock.attr,
  6221. &driver_attr_no_uld.attr,
  6222. &driver_attr_scsi_level.attr,
  6223. &driver_attr_virtual_gb.attr,
  6224. &driver_attr_add_host.attr,
  6225. &driver_attr_per_host_store.attr,
  6226. &driver_attr_vpd_use_hostno.attr,
  6227. &driver_attr_sector_size.attr,
  6228. &driver_attr_statistics.attr,
  6229. &driver_attr_submit_queues.attr,
  6230. &driver_attr_dix.attr,
  6231. &driver_attr_dif.attr,
  6232. &driver_attr_guard.attr,
  6233. &driver_attr_ato.attr,
  6234. &driver_attr_map.attr,
  6235. &driver_attr_random.attr,
  6236. &driver_attr_removable.attr,
  6237. &driver_attr_host_lock.attr,
  6238. &driver_attr_ndelay.attr,
  6239. &driver_attr_strict.attr,
  6240. &driver_attr_uuid_ctl.attr,
  6241. &driver_attr_cdb_len.attr,
  6242. &driver_attr_tur_ms_to_ready.attr,
  6243. &driver_attr_zbc.attr,
  6244. NULL,
  6245. };
  6246. ATTRIBUTE_GROUPS(sdebug_drv);
  6247. static struct device *pseudo_primary;
  6248. static int __init scsi_debug_init(void)
  6249. {
  6250. bool want_store = (sdebug_fake_rw == 0);
  6251. unsigned long sz;
  6252. int k, ret, hosts_to_add;
  6253. int idx = -1;
  6254. ramdisk_lck_a[0] = &atomic_rw;
  6255. ramdisk_lck_a[1] = &atomic_rw2;
  6256. atomic_set(&retired_max_queue, 0);
  6257. if (sdebug_ndelay >= 1000 * 1000 * 1000) {
  6258. pr_warn("ndelay must be less than 1 second, ignored\n");
  6259. sdebug_ndelay = 0;
  6260. } else if (sdebug_ndelay > 0)
  6261. sdebug_jdelay = JDELAY_OVERRIDDEN;
  6262. switch (sdebug_sector_size) {
  6263. case 512:
  6264. case 1024:
  6265. case 2048:
  6266. case 4096:
  6267. break;
  6268. default:
  6269. pr_err("invalid sector_size %d\n", sdebug_sector_size);
  6270. return -EINVAL;
  6271. }
  6272. switch (sdebug_dif) {
  6273. case T10_PI_TYPE0_PROTECTION:
  6274. break;
  6275. case T10_PI_TYPE1_PROTECTION:
  6276. case T10_PI_TYPE2_PROTECTION:
  6277. case T10_PI_TYPE3_PROTECTION:
  6278. have_dif_prot = true;
  6279. break;
  6280. default:
  6281. pr_err("dif must be 0, 1, 2 or 3\n");
  6282. return -EINVAL;
  6283. }
  6284. if (sdebug_num_tgts < 0) {
  6285. pr_err("num_tgts must be >= 0\n");
  6286. return -EINVAL;
  6287. }
  6288. if (sdebug_guard > 1) {
  6289. pr_err("guard must be 0 or 1\n");
  6290. return -EINVAL;
  6291. }
  6292. if (sdebug_ato > 1) {
  6293. pr_err("ato must be 0 or 1\n");
  6294. return -EINVAL;
  6295. }
  6296. if (sdebug_physblk_exp > 15) {
  6297. pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
  6298. return -EINVAL;
  6299. }
  6300. sdebug_lun_am = sdebug_lun_am_i;
  6301. if (sdebug_lun_am > SAM_LUN_AM_FLAT) {
  6302. pr_warn("Invalid LUN format %u, using default\n", (int)sdebug_lun_am);
  6303. sdebug_lun_am = SAM_LUN_AM_PERIPHERAL;
  6304. }
  6305. if (sdebug_max_luns > 256) {
  6306. if (sdebug_max_luns > 16384) {
  6307. pr_warn("max_luns can be no more than 16384, use default\n");
  6308. sdebug_max_luns = DEF_MAX_LUNS;
  6309. }
  6310. sdebug_lun_am = SAM_LUN_AM_FLAT;
  6311. }
  6312. if (sdebug_lowest_aligned > 0x3fff) {
  6313. pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
  6314. return -EINVAL;
  6315. }
  6316. if (submit_queues < 1) {
  6317. pr_err("submit_queues must be 1 or more\n");
  6318. return -EINVAL;
  6319. }
  6320. if ((sdebug_max_queue > SDEBUG_CANQUEUE) || (sdebug_max_queue < 1)) {
  6321. pr_err("max_queue must be in range [1, %d]\n", SDEBUG_CANQUEUE);
  6322. return -EINVAL;
  6323. }
  6324. if ((sdebug_host_max_queue > SDEBUG_CANQUEUE) ||
  6325. (sdebug_host_max_queue < 0)) {
  6326. pr_err("host_max_queue must be in range [0 %d]\n",
  6327. SDEBUG_CANQUEUE);
  6328. return -EINVAL;
  6329. }
  6330. if (sdebug_host_max_queue &&
  6331. (sdebug_max_queue != sdebug_host_max_queue)) {
  6332. sdebug_max_queue = sdebug_host_max_queue;
  6333. pr_warn("fixing max submit queue depth to host max queue depth, %d\n",
  6334. sdebug_max_queue);
  6335. }
  6336. sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
  6337. GFP_KERNEL);
  6338. if (sdebug_q_arr == NULL)
  6339. return -ENOMEM;
  6340. for (k = 0; k < submit_queues; ++k)
  6341. spin_lock_init(&sdebug_q_arr[k].qc_lock);
  6342. /*
  6343. * check for host managed zoned block device specified with
  6344. * ptype=0x14 or zbc=XXX.
  6345. */
  6346. if (sdebug_ptype == TYPE_ZBC) {
  6347. sdeb_zbc_model = BLK_ZONED_HM;
  6348. } else if (sdeb_zbc_model_s && *sdeb_zbc_model_s) {
  6349. k = sdeb_zbc_model_str(sdeb_zbc_model_s);
  6350. if (k < 0) {
  6351. ret = k;
  6352. goto free_q_arr;
  6353. }
  6354. sdeb_zbc_model = k;
  6355. switch (sdeb_zbc_model) {
  6356. case BLK_ZONED_NONE:
  6357. case BLK_ZONED_HA:
  6358. sdebug_ptype = TYPE_DISK;
  6359. break;
  6360. case BLK_ZONED_HM:
  6361. sdebug_ptype = TYPE_ZBC;
  6362. break;
  6363. default:
  6364. pr_err("Invalid ZBC model\n");
  6365. ret = -EINVAL;
  6366. goto free_q_arr;
  6367. }
  6368. }
  6369. if (sdeb_zbc_model != BLK_ZONED_NONE) {
  6370. sdeb_zbc_in_use = true;
  6371. if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
  6372. sdebug_dev_size_mb = DEF_ZBC_DEV_SIZE_MB;
  6373. }
  6374. if (sdebug_dev_size_mb == DEF_DEV_SIZE_PRE_INIT)
  6375. sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
  6376. if (sdebug_dev_size_mb < 1)
  6377. sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  6378. sz = (unsigned long)sdebug_dev_size_mb * 1048576;
  6379. sdebug_store_sectors = sz / sdebug_sector_size;
  6380. sdebug_capacity = get_sdebug_capacity();
  6381. /* play around with geometry, don't waste too much on track 0 */
  6382. sdebug_heads = 8;
  6383. sdebug_sectors_per = 32;
  6384. if (sdebug_dev_size_mb >= 256)
  6385. sdebug_heads = 64;
  6386. else if (sdebug_dev_size_mb >= 16)
  6387. sdebug_heads = 32;
  6388. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  6389. (sdebug_sectors_per * sdebug_heads);
  6390. if (sdebug_cylinders_per >= 1024) {
  6391. /* other LLDs do this; implies >= 1GB ram disk ... */
  6392. sdebug_heads = 255;
  6393. sdebug_sectors_per = 63;
  6394. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  6395. (sdebug_sectors_per * sdebug_heads);
  6396. }
  6397. if (scsi_debug_lbp()) {
  6398. sdebug_unmap_max_blocks =
  6399. clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
  6400. sdebug_unmap_max_desc =
  6401. clamp(sdebug_unmap_max_desc, 0U, 256U);
  6402. sdebug_unmap_granularity =
  6403. clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
  6404. if (sdebug_unmap_alignment &&
  6405. sdebug_unmap_granularity <=
  6406. sdebug_unmap_alignment) {
  6407. pr_err("ERR: unmap_granularity <= unmap_alignment\n");
  6408. ret = -EINVAL;
  6409. goto free_q_arr;
  6410. }
  6411. }
  6412. xa_init_flags(per_store_ap, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
  6413. if (want_store) {
  6414. idx = sdebug_add_store();
  6415. if (idx < 0) {
  6416. ret = idx;
  6417. goto free_q_arr;
  6418. }
  6419. }
  6420. pseudo_primary = root_device_register("pseudo_0");
  6421. if (IS_ERR(pseudo_primary)) {
  6422. pr_warn("root_device_register() error\n");
  6423. ret = PTR_ERR(pseudo_primary);
  6424. goto free_vm;
  6425. }
  6426. ret = bus_register(&pseudo_lld_bus);
  6427. if (ret < 0) {
  6428. pr_warn("bus_register error: %d\n", ret);
  6429. goto dev_unreg;
  6430. }
  6431. ret = driver_register(&sdebug_driverfs_driver);
  6432. if (ret < 0) {
  6433. pr_warn("driver_register error: %d\n", ret);
  6434. goto bus_unreg;
  6435. }
  6436. hosts_to_add = sdebug_add_host;
  6437. sdebug_add_host = 0;
  6438. for (k = 0; k < hosts_to_add; k++) {
  6439. if (want_store && k == 0) {
  6440. ret = sdebug_add_host_helper(idx);
  6441. if (ret < 0) {
  6442. pr_err("add_host_helper k=%d, error=%d\n",
  6443. k, -ret);
  6444. break;
  6445. }
  6446. } else {
  6447. ret = sdebug_do_add_host(want_store &&
  6448. sdebug_per_host_store);
  6449. if (ret < 0) {
  6450. pr_err("add_host k=%d error=%d\n", k, -ret);
  6451. break;
  6452. }
  6453. }
  6454. }
  6455. if (sdebug_verbose)
  6456. pr_info("built %d host(s)\n", sdebug_num_hosts);
  6457. return 0;
  6458. bus_unreg:
  6459. bus_unregister(&pseudo_lld_bus);
  6460. dev_unreg:
  6461. root_device_unregister(pseudo_primary);
  6462. free_vm:
  6463. sdebug_erase_store(idx, NULL);
  6464. free_q_arr:
  6465. kfree(sdebug_q_arr);
  6466. return ret;
  6467. }
  6468. static void __exit scsi_debug_exit(void)
  6469. {
  6470. int k = sdebug_num_hosts;
  6471. stop_all_queued();
  6472. for (; k; k--)
  6473. sdebug_do_remove_host(true);
  6474. free_all_queued();
  6475. driver_unregister(&sdebug_driverfs_driver);
  6476. bus_unregister(&pseudo_lld_bus);
  6477. root_device_unregister(pseudo_primary);
  6478. sdebug_erase_all_stores(false);
  6479. xa_destroy(per_store_ap);
  6480. kfree(sdebug_q_arr);
  6481. }
  6482. device_initcall(scsi_debug_init);
  6483. module_exit(scsi_debug_exit);
  6484. static void sdebug_release_adapter(struct device *dev)
  6485. {
  6486. struct sdebug_host_info *sdbg_host;
  6487. sdbg_host = to_sdebug_host(dev);
  6488. kfree(sdbg_host);
  6489. }
  6490. /* idx must be valid, if sip is NULL then it will be obtained using idx */
  6491. static void sdebug_erase_store(int idx, struct sdeb_store_info *sip)
  6492. {
  6493. if (idx < 0)
  6494. return;
  6495. if (!sip) {
  6496. if (xa_empty(per_store_ap))
  6497. return;
  6498. sip = xa_load(per_store_ap, idx);
  6499. if (!sip)
  6500. return;
  6501. }
  6502. vfree(sip->map_storep);
  6503. vfree(sip->dif_storep);
  6504. vfree(sip->storep);
  6505. xa_erase(per_store_ap, idx);
  6506. kfree(sip);
  6507. }
  6508. /* Assume apart_from_first==false only in shutdown case. */
  6509. static void sdebug_erase_all_stores(bool apart_from_first)
  6510. {
  6511. unsigned long idx;
  6512. struct sdeb_store_info *sip = NULL;
  6513. xa_for_each(per_store_ap, idx, sip) {
  6514. if (apart_from_first)
  6515. apart_from_first = false;
  6516. else
  6517. sdebug_erase_store(idx, sip);
  6518. }
  6519. if (apart_from_first)
  6520. sdeb_most_recent_idx = sdeb_first_idx;
  6521. }
  6522. /*
  6523. * Returns store xarray new element index (idx) if >=0 else negated errno.
  6524. * Limit the number of stores to 65536.
  6525. */
  6526. static int sdebug_add_store(void)
  6527. {
  6528. int res;
  6529. u32 n_idx;
  6530. unsigned long iflags;
  6531. unsigned long sz = (unsigned long)sdebug_dev_size_mb * 1048576;
  6532. struct sdeb_store_info *sip = NULL;
  6533. struct xa_limit xal = { .max = 1 << 16, .min = 0 };
  6534. sip = kzalloc(sizeof(*sip), GFP_KERNEL);
  6535. if (!sip)
  6536. return -ENOMEM;
  6537. xa_lock_irqsave(per_store_ap, iflags);
  6538. res = __xa_alloc(per_store_ap, &n_idx, sip, xal, GFP_ATOMIC);
  6539. if (unlikely(res < 0)) {
  6540. xa_unlock_irqrestore(per_store_ap, iflags);
  6541. kfree(sip);
  6542. pr_warn("%s: xa_alloc() errno=%d\n", __func__, -res);
  6543. return res;
  6544. }
  6545. sdeb_most_recent_idx = n_idx;
  6546. if (sdeb_first_idx < 0)
  6547. sdeb_first_idx = n_idx;
  6548. xa_unlock_irqrestore(per_store_ap, iflags);
  6549. res = -ENOMEM;
  6550. sip->storep = vzalloc(sz);
  6551. if (!sip->storep) {
  6552. pr_err("user data oom\n");
  6553. goto err;
  6554. }
  6555. if (sdebug_num_parts > 0)
  6556. sdebug_build_parts(sip->storep, sz);
  6557. /* DIF/DIX: what T10 calls Protection Information (PI) */
  6558. if (sdebug_dix) {
  6559. int dif_size;
  6560. dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
  6561. sip->dif_storep = vmalloc(dif_size);
  6562. pr_info("dif_storep %u bytes @ %pK\n", dif_size,
  6563. sip->dif_storep);
  6564. if (!sip->dif_storep) {
  6565. pr_err("DIX oom\n");
  6566. goto err;
  6567. }
  6568. memset(sip->dif_storep, 0xff, dif_size);
  6569. }
  6570. /* Logical Block Provisioning */
  6571. if (scsi_debug_lbp()) {
  6572. map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
  6573. sip->map_storep = vmalloc(array_size(sizeof(long),
  6574. BITS_TO_LONGS(map_size)));
  6575. pr_info("%lu provisioning blocks\n", map_size);
  6576. if (!sip->map_storep) {
  6577. pr_err("LBP map oom\n");
  6578. goto err;
  6579. }
  6580. bitmap_zero(sip->map_storep, map_size);
  6581. /* Map first 1KB for partition table */
  6582. if (sdebug_num_parts)
  6583. map_region(sip, 0, 2);
  6584. }
  6585. rwlock_init(&sip->macc_lck);
  6586. return (int)n_idx;
  6587. err:
  6588. sdebug_erase_store((int)n_idx, sip);
  6589. pr_warn("%s: failed, errno=%d\n", __func__, -res);
  6590. return res;
  6591. }
  6592. static int sdebug_add_host_helper(int per_host_idx)
  6593. {
  6594. int k, devs_per_host, idx;
  6595. int error = -ENOMEM;
  6596. struct sdebug_host_info *sdbg_host;
  6597. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  6598. sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL);
  6599. if (!sdbg_host)
  6600. return -ENOMEM;
  6601. idx = (per_host_idx < 0) ? sdeb_first_idx : per_host_idx;
  6602. if (xa_get_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE))
  6603. xa_clear_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
  6604. sdbg_host->si_idx = idx;
  6605. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  6606. devs_per_host = sdebug_num_tgts * sdebug_max_luns;
  6607. for (k = 0; k < devs_per_host; k++) {
  6608. sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
  6609. if (!sdbg_devinfo)
  6610. goto clean;
  6611. }
  6612. spin_lock(&sdebug_host_list_lock);
  6613. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  6614. spin_unlock(&sdebug_host_list_lock);
  6615. sdbg_host->dev.bus = &pseudo_lld_bus;
  6616. sdbg_host->dev.parent = pseudo_primary;
  6617. sdbg_host->dev.release = &sdebug_release_adapter;
  6618. dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_num_hosts);
  6619. error = device_register(&sdbg_host->dev);
  6620. if (error) {
  6621. spin_lock(&sdebug_host_list_lock);
  6622. list_del(&sdbg_host->host_list);
  6623. spin_unlock(&sdebug_host_list_lock);
  6624. goto clean;
  6625. }
  6626. ++sdebug_num_hosts;
  6627. return 0;
  6628. clean:
  6629. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  6630. dev_list) {
  6631. list_del(&sdbg_devinfo->dev_list);
  6632. kfree(sdbg_devinfo->zstate);
  6633. kfree(sdbg_devinfo);
  6634. }
  6635. if (sdbg_host->dev.release)
  6636. put_device(&sdbg_host->dev);
  6637. else
  6638. kfree(sdbg_host);
  6639. pr_warn("%s: failed, errno=%d\n", __func__, -error);
  6640. return error;
  6641. }
  6642. static int sdebug_do_add_host(bool mk_new_store)
  6643. {
  6644. int ph_idx = sdeb_most_recent_idx;
  6645. if (mk_new_store) {
  6646. ph_idx = sdebug_add_store();
  6647. if (ph_idx < 0)
  6648. return ph_idx;
  6649. }
  6650. return sdebug_add_host_helper(ph_idx);
  6651. }
  6652. static void sdebug_do_remove_host(bool the_end)
  6653. {
  6654. int idx = -1;
  6655. struct sdebug_host_info *sdbg_host = NULL;
  6656. struct sdebug_host_info *sdbg_host2;
  6657. spin_lock(&sdebug_host_list_lock);
  6658. if (!list_empty(&sdebug_host_list)) {
  6659. sdbg_host = list_entry(sdebug_host_list.prev,
  6660. struct sdebug_host_info, host_list);
  6661. idx = sdbg_host->si_idx;
  6662. }
  6663. if (!the_end && idx >= 0) {
  6664. bool unique = true;
  6665. list_for_each_entry(sdbg_host2, &sdebug_host_list, host_list) {
  6666. if (sdbg_host2 == sdbg_host)
  6667. continue;
  6668. if (idx == sdbg_host2->si_idx) {
  6669. unique = false;
  6670. break;
  6671. }
  6672. }
  6673. if (unique) {
  6674. xa_set_mark(per_store_ap, idx, SDEB_XA_NOT_IN_USE);
  6675. if (idx == sdeb_most_recent_idx)
  6676. --sdeb_most_recent_idx;
  6677. }
  6678. }
  6679. if (sdbg_host)
  6680. list_del(&sdbg_host->host_list);
  6681. spin_unlock(&sdebug_host_list_lock);
  6682. if (!sdbg_host)
  6683. return;
  6684. device_unregister(&sdbg_host->dev);
  6685. --sdebug_num_hosts;
  6686. }
  6687. static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
  6688. {
  6689. int num_in_q = 0;
  6690. struct sdebug_dev_info *devip;
  6691. block_unblock_all_queues(true);
  6692. devip = (struct sdebug_dev_info *)sdev->hostdata;
  6693. if (NULL == devip) {
  6694. block_unblock_all_queues(false);
  6695. return -ENODEV;
  6696. }
  6697. num_in_q = atomic_read(&devip->num_in_q);
  6698. if (qdepth > SDEBUG_CANQUEUE) {
  6699. qdepth = SDEBUG_CANQUEUE;
  6700. pr_warn("%s: requested qdepth [%d] exceeds canqueue [%d], trim\n", __func__,
  6701. qdepth, SDEBUG_CANQUEUE);
  6702. }
  6703. if (qdepth < 1)
  6704. qdepth = 1;
  6705. if (qdepth != sdev->queue_depth)
  6706. scsi_change_queue_depth(sdev, qdepth);
  6707. if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
  6708. sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
  6709. __func__, qdepth, num_in_q);
  6710. }
  6711. block_unblock_all_queues(false);
  6712. return sdev->queue_depth;
  6713. }
  6714. static bool fake_timeout(struct scsi_cmnd *scp)
  6715. {
  6716. if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
  6717. if (sdebug_every_nth < -1)
  6718. sdebug_every_nth = -1;
  6719. if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
  6720. return true; /* ignore command causing timeout */
  6721. else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
  6722. scsi_medium_access_command(scp))
  6723. return true; /* time out reads and writes */
  6724. }
  6725. return false;
  6726. }
  6727. /* Response to TUR or media access command when device stopped */
  6728. static int resp_not_ready(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  6729. {
  6730. int stopped_state;
  6731. u64 diff_ns = 0;
  6732. ktime_t now_ts = ktime_get_boottime();
  6733. struct scsi_device *sdp = scp->device;
  6734. stopped_state = atomic_read(&devip->stopped);
  6735. if (stopped_state == 2) {
  6736. if (ktime_to_ns(now_ts) > ktime_to_ns(devip->create_ts)) {
  6737. diff_ns = ktime_to_ns(ktime_sub(now_ts, devip->create_ts));
  6738. if (diff_ns >= ((u64)sdeb_tur_ms_to_ready * 1000000)) {
  6739. /* tur_ms_to_ready timer extinguished */
  6740. atomic_set(&devip->stopped, 0);
  6741. return 0;
  6742. }
  6743. }
  6744. mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x1);
  6745. if (sdebug_verbose)
  6746. sdev_printk(KERN_INFO, sdp,
  6747. "%s: Not ready: in process of becoming ready\n", my_name);
  6748. if (scp->cmnd[0] == TEST_UNIT_READY) {
  6749. u64 tur_nanosecs_to_ready = (u64)sdeb_tur_ms_to_ready * 1000000;
  6750. if (diff_ns <= tur_nanosecs_to_ready)
  6751. diff_ns = tur_nanosecs_to_ready - diff_ns;
  6752. else
  6753. diff_ns = tur_nanosecs_to_ready;
  6754. /* As per 20-061r2 approved for spc6 by T10 on 20200716 */
  6755. do_div(diff_ns, 1000000); /* diff_ns becomes milliseconds */
  6756. scsi_set_sense_information(scp->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  6757. diff_ns);
  6758. return check_condition_result;
  6759. }
  6760. }
  6761. mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
  6762. if (sdebug_verbose)
  6763. sdev_printk(KERN_INFO, sdp, "%s: Not ready: initializing command required\n",
  6764. my_name);
  6765. return check_condition_result;
  6766. }
  6767. static void sdebug_map_queues(struct Scsi_Host *shost)
  6768. {
  6769. int i, qoff;
  6770. if (shost->nr_hw_queues == 1)
  6771. return;
  6772. for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
  6773. struct blk_mq_queue_map *map = &shost->tag_set.map[i];
  6774. map->nr_queues = 0;
  6775. if (i == HCTX_TYPE_DEFAULT)
  6776. map->nr_queues = submit_queues - poll_queues;
  6777. else if (i == HCTX_TYPE_POLL)
  6778. map->nr_queues = poll_queues;
  6779. if (!map->nr_queues) {
  6780. BUG_ON(i == HCTX_TYPE_DEFAULT);
  6781. continue;
  6782. }
  6783. map->queue_offset = qoff;
  6784. blk_mq_map_queues(map);
  6785. qoff += map->nr_queues;
  6786. }
  6787. }
  6788. static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
  6789. {
  6790. bool first;
  6791. bool retiring = false;
  6792. int num_entries = 0;
  6793. unsigned int qc_idx = 0;
  6794. unsigned long iflags;
  6795. ktime_t kt_from_boot = ktime_get_boottime();
  6796. struct sdebug_queue *sqp;
  6797. struct sdebug_queued_cmd *sqcp;
  6798. struct scsi_cmnd *scp;
  6799. struct sdebug_dev_info *devip;
  6800. struct sdebug_defer *sd_dp;
  6801. sqp = sdebug_q_arr + queue_num;
  6802. spin_lock_irqsave(&sqp->qc_lock, iflags);
  6803. qc_idx = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
  6804. if (qc_idx >= sdebug_max_queue)
  6805. goto unlock;
  6806. for (first = true; first || qc_idx + 1 < sdebug_max_queue; ) {
  6807. if (first) {
  6808. first = false;
  6809. if (!test_bit(qc_idx, sqp->in_use_bm))
  6810. continue;
  6811. } else {
  6812. qc_idx = find_next_bit(sqp->in_use_bm, sdebug_max_queue, qc_idx + 1);
  6813. }
  6814. if (qc_idx >= sdebug_max_queue)
  6815. break;
  6816. sqcp = &sqp->qc_arr[qc_idx];
  6817. sd_dp = sqcp->sd_dp;
  6818. if (unlikely(!sd_dp))
  6819. continue;
  6820. scp = sqcp->a_cmnd;
  6821. if (unlikely(scp == NULL)) {
  6822. pr_err("scp is NULL, queue_num=%d, qc_idx=%u from %s\n",
  6823. queue_num, qc_idx, __func__);
  6824. break;
  6825. }
  6826. if (READ_ONCE(sd_dp->defer_t) == SDEB_DEFER_POLL) {
  6827. if (kt_from_boot < sd_dp->cmpl_ts)
  6828. continue;
  6829. } else /* ignoring non REQ_POLLED requests */
  6830. continue;
  6831. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  6832. if (likely(devip))
  6833. atomic_dec(&devip->num_in_q);
  6834. else
  6835. pr_err("devip=NULL from %s\n", __func__);
  6836. if (unlikely(atomic_read(&retired_max_queue) > 0))
  6837. retiring = true;
  6838. sqcp->a_cmnd = NULL;
  6839. if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
  6840. pr_err("Unexpected completion sqp %p queue_num=%d qc_idx=%u from %s\n",
  6841. sqp, queue_num, qc_idx, __func__);
  6842. break;
  6843. }
  6844. if (unlikely(retiring)) { /* user has reduced max_queue */
  6845. int k, retval;
  6846. retval = atomic_read(&retired_max_queue);
  6847. if (qc_idx >= retval) {
  6848. pr_err("index %d too large\n", retval);
  6849. break;
  6850. }
  6851. k = find_last_bit(sqp->in_use_bm, retval);
  6852. if ((k < sdebug_max_queue) || (k == retval))
  6853. atomic_set(&retired_max_queue, 0);
  6854. else
  6855. atomic_set(&retired_max_queue, k + 1);
  6856. }
  6857. WRITE_ONCE(sd_dp->defer_t, SDEB_DEFER_NONE);
  6858. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  6859. scsi_done(scp); /* callback to mid level */
  6860. num_entries++;
  6861. spin_lock_irqsave(&sqp->qc_lock, iflags);
  6862. if (find_first_bit(sqp->in_use_bm, sdebug_max_queue) >= sdebug_max_queue)
  6863. break;
  6864. }
  6865. unlock:
  6866. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  6867. if (num_entries > 0)
  6868. atomic_add(num_entries, &sdeb_mq_poll_count);
  6869. return num_entries;
  6870. }
  6871. static int scsi_debug_queuecommand(struct Scsi_Host *shost,
  6872. struct scsi_cmnd *scp)
  6873. {
  6874. u8 sdeb_i;
  6875. struct scsi_device *sdp = scp->device;
  6876. const struct opcode_info_t *oip;
  6877. const struct opcode_info_t *r_oip;
  6878. struct sdebug_dev_info *devip;
  6879. u8 *cmd = scp->cmnd;
  6880. int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  6881. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
  6882. int k, na;
  6883. int errsts = 0;
  6884. u64 lun_index = sdp->lun & 0x3FFF;
  6885. u32 flags;
  6886. u16 sa;
  6887. u8 opcode = cmd[0];
  6888. bool has_wlun_rl;
  6889. bool inject_now;
  6890. scsi_set_resid(scp, 0);
  6891. if (sdebug_statistics) {
  6892. atomic_inc(&sdebug_cmnd_count);
  6893. inject_now = inject_on_this_cmd();
  6894. } else {
  6895. inject_now = false;
  6896. }
  6897. if (unlikely(sdebug_verbose &&
  6898. !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
  6899. char b[120];
  6900. int n, len, sb;
  6901. len = scp->cmd_len;
  6902. sb = (int)sizeof(b);
  6903. if (len > 32)
  6904. strcpy(b, "too long, over 32 bytes");
  6905. else {
  6906. for (k = 0, n = 0; k < len && n < sb; ++k)
  6907. n += scnprintf(b + n, sb - n, "%02x ",
  6908. (u32)cmd[k]);
  6909. }
  6910. sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
  6911. blk_mq_unique_tag(scsi_cmd_to_rq(scp)), b);
  6912. }
  6913. if (unlikely(inject_now && (sdebug_opts & SDEBUG_OPT_HOST_BUSY)))
  6914. return SCSI_MLQUEUE_HOST_BUSY;
  6915. has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
  6916. if (unlikely(lun_index >= sdebug_max_luns && !has_wlun_rl))
  6917. goto err_out;
  6918. sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
  6919. oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
  6920. devip = (struct sdebug_dev_info *)sdp->hostdata;
  6921. if (unlikely(!devip)) {
  6922. devip = find_build_dev_info(sdp);
  6923. if (NULL == devip)
  6924. goto err_out;
  6925. }
  6926. if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending)))
  6927. atomic_set(&sdeb_inject_pending, 1);
  6928. na = oip->num_attached;
  6929. r_pfp = oip->pfp;
  6930. if (na) { /* multiple commands with this opcode */
  6931. r_oip = oip;
  6932. if (FF_SA & r_oip->flags) {
  6933. if (F_SA_LOW & oip->flags)
  6934. sa = 0x1f & cmd[1];
  6935. else
  6936. sa = get_unaligned_be16(cmd + 8);
  6937. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  6938. if (opcode == oip->opcode && sa == oip->sa)
  6939. break;
  6940. }
  6941. } else { /* since no service action only check opcode */
  6942. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  6943. if (opcode == oip->opcode)
  6944. break;
  6945. }
  6946. }
  6947. if (k > na) {
  6948. if (F_SA_LOW & r_oip->flags)
  6949. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
  6950. else if (F_SA_HIGH & r_oip->flags)
  6951. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
  6952. else
  6953. mk_sense_invalid_opcode(scp);
  6954. goto check_cond;
  6955. }
  6956. } /* else (when na==0) we assume the oip is a match */
  6957. flags = oip->flags;
  6958. if (unlikely(F_INV_OP & flags)) {
  6959. mk_sense_invalid_opcode(scp);
  6960. goto check_cond;
  6961. }
  6962. if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
  6963. if (sdebug_verbose)
  6964. sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
  6965. my_name, opcode, " supported for wlun");
  6966. mk_sense_invalid_opcode(scp);
  6967. goto check_cond;
  6968. }
  6969. if (unlikely(sdebug_strict)) { /* check cdb against mask */
  6970. u8 rem;
  6971. int j;
  6972. for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
  6973. rem = ~oip->len_mask[k] & cmd[k];
  6974. if (rem) {
  6975. for (j = 7; j >= 0; --j, rem <<= 1) {
  6976. if (0x80 & rem)
  6977. break;
  6978. }
  6979. mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
  6980. goto check_cond;
  6981. }
  6982. }
  6983. }
  6984. if (unlikely(!(F_SKIP_UA & flags) &&
  6985. find_first_bit(devip->uas_bm,
  6986. SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
  6987. errsts = make_ua(scp, devip);
  6988. if (errsts)
  6989. goto check_cond;
  6990. }
  6991. if (unlikely(((F_M_ACCESS & flags) || scp->cmnd[0] == TEST_UNIT_READY) &&
  6992. atomic_read(&devip->stopped))) {
  6993. errsts = resp_not_ready(scp, devip);
  6994. if (errsts)
  6995. goto fini;
  6996. }
  6997. if (sdebug_fake_rw && (F_FAKE_RW & flags))
  6998. goto fini;
  6999. if (unlikely(sdebug_every_nth)) {
  7000. if (fake_timeout(scp))
  7001. return 0; /* ignore command: make trouble */
  7002. }
  7003. if (likely(oip->pfp))
  7004. pfp = oip->pfp; /* calls a resp_* function */
  7005. else
  7006. pfp = r_pfp; /* if leaf function ptr NULL, try the root's */
  7007. fini:
  7008. if (F_DELAY_OVERR & flags) /* cmds like INQUIRY respond asap */
  7009. return schedule_resp(scp, devip, errsts, pfp, 0, 0);
  7010. else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
  7011. sdebug_ndelay > 10000)) {
  7012. /*
  7013. * Skip long delays if ndelay <= 10 microseconds. Otherwise
  7014. * for Start Stop Unit (SSU) want at least 1 second delay and
  7015. * if sdebug_jdelay>1 want a long delay of that many seconds.
  7016. * For Synchronize Cache want 1/20 of SSU's delay.
  7017. */
  7018. int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
  7019. int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
  7020. jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
  7021. return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
  7022. } else
  7023. return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
  7024. sdebug_ndelay);
  7025. check_cond:
  7026. return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
  7027. err_out:
  7028. return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
  7029. }
  7030. static struct scsi_host_template sdebug_driver_template = {
  7031. .show_info = scsi_debug_show_info,
  7032. .write_info = scsi_debug_write_info,
  7033. .proc_name = sdebug_proc_name,
  7034. .name = "SCSI DEBUG",
  7035. .info = scsi_debug_info,
  7036. .slave_alloc = scsi_debug_slave_alloc,
  7037. .slave_configure = scsi_debug_slave_configure,
  7038. .slave_destroy = scsi_debug_slave_destroy,
  7039. .ioctl = scsi_debug_ioctl,
  7040. .queuecommand = scsi_debug_queuecommand,
  7041. .change_queue_depth = sdebug_change_qdepth,
  7042. .map_queues = sdebug_map_queues,
  7043. .mq_poll = sdebug_blk_mq_poll,
  7044. .eh_abort_handler = scsi_debug_abort,
  7045. .eh_device_reset_handler = scsi_debug_device_reset,
  7046. .eh_target_reset_handler = scsi_debug_target_reset,
  7047. .eh_bus_reset_handler = scsi_debug_bus_reset,
  7048. .eh_host_reset_handler = scsi_debug_host_reset,
  7049. .can_queue = SDEBUG_CANQUEUE,
  7050. .this_id = 7,
  7051. .sg_tablesize = SG_MAX_SEGMENTS,
  7052. .cmd_per_lun = DEF_CMD_PER_LUN,
  7053. .max_sectors = -1U,
  7054. .max_segment_size = -1U,
  7055. .module = THIS_MODULE,
  7056. .track_queue_depth = 1,
  7057. };
  7058. static int sdebug_driver_probe(struct device *dev)
  7059. {
  7060. int error = 0;
  7061. struct sdebug_host_info *sdbg_host;
  7062. struct Scsi_Host *hpnt;
  7063. int hprot;
  7064. sdbg_host = to_sdebug_host(dev);
  7065. sdebug_driver_template.can_queue = sdebug_max_queue;
  7066. sdebug_driver_template.cmd_per_lun = sdebug_max_queue;
  7067. if (!sdebug_clustering)
  7068. sdebug_driver_template.dma_boundary = PAGE_SIZE - 1;
  7069. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  7070. if (NULL == hpnt) {
  7071. pr_err("scsi_host_alloc failed\n");
  7072. error = -ENODEV;
  7073. return error;
  7074. }
  7075. if (submit_queues > nr_cpu_ids) {
  7076. pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
  7077. my_name, submit_queues, nr_cpu_ids);
  7078. submit_queues = nr_cpu_ids;
  7079. }
  7080. /*
  7081. * Decide whether to tell scsi subsystem that we want mq. The
  7082. * following should give the same answer for each host.
  7083. */
  7084. hpnt->nr_hw_queues = submit_queues;
  7085. if (sdebug_host_max_queue)
  7086. hpnt->host_tagset = 1;
  7087. /* poll queues are possible for nr_hw_queues > 1 */
  7088. if (hpnt->nr_hw_queues == 1 || (poll_queues < 1)) {
  7089. pr_warn("%s: trim poll_queues to 0. poll_q/nr_hw = (%d/%d)\n",
  7090. my_name, poll_queues, hpnt->nr_hw_queues);
  7091. poll_queues = 0;
  7092. }
  7093. /*
  7094. * Poll queues don't need interrupts, but we need at least one I/O queue
  7095. * left over for non-polled I/O.
  7096. * If condition not met, trim poll_queues to 1 (just for simplicity).
  7097. */
  7098. if (poll_queues >= submit_queues) {
  7099. if (submit_queues < 3)
  7100. pr_warn("%s: trim poll_queues to 1\n", my_name);
  7101. else
  7102. pr_warn("%s: trim poll_queues to 1. Perhaps try poll_queues=%d\n",
  7103. my_name, submit_queues - 1);
  7104. poll_queues = 1;
  7105. }
  7106. if (poll_queues)
  7107. hpnt->nr_maps = 3;
  7108. sdbg_host->shost = hpnt;
  7109. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  7110. if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
  7111. hpnt->max_id = sdebug_num_tgts + 1;
  7112. else
  7113. hpnt->max_id = sdebug_num_tgts;
  7114. /* = sdebug_max_luns; */
  7115. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  7116. hprot = 0;
  7117. switch (sdebug_dif) {
  7118. case T10_PI_TYPE1_PROTECTION:
  7119. hprot = SHOST_DIF_TYPE1_PROTECTION;
  7120. if (sdebug_dix)
  7121. hprot |= SHOST_DIX_TYPE1_PROTECTION;
  7122. break;
  7123. case T10_PI_TYPE2_PROTECTION:
  7124. hprot = SHOST_DIF_TYPE2_PROTECTION;
  7125. if (sdebug_dix)
  7126. hprot |= SHOST_DIX_TYPE2_PROTECTION;
  7127. break;
  7128. case T10_PI_TYPE3_PROTECTION:
  7129. hprot = SHOST_DIF_TYPE3_PROTECTION;
  7130. if (sdebug_dix)
  7131. hprot |= SHOST_DIX_TYPE3_PROTECTION;
  7132. break;
  7133. default:
  7134. if (sdebug_dix)
  7135. hprot |= SHOST_DIX_TYPE0_PROTECTION;
  7136. break;
  7137. }
  7138. scsi_host_set_prot(hpnt, hprot);
  7139. if (have_dif_prot || sdebug_dix)
  7140. pr_info("host protection%s%s%s%s%s%s%s\n",
  7141. (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
  7142. (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
  7143. (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
  7144. (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
  7145. (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
  7146. (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
  7147. (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
  7148. if (sdebug_guard == 1)
  7149. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
  7150. else
  7151. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
  7152. sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
  7153. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
  7154. if (sdebug_every_nth) /* need stats counters for every_nth */
  7155. sdebug_statistics = true;
  7156. error = scsi_add_host(hpnt, &sdbg_host->dev);
  7157. if (error) {
  7158. pr_err("scsi_add_host failed\n");
  7159. error = -ENODEV;
  7160. scsi_host_put(hpnt);
  7161. } else {
  7162. scsi_scan_host(hpnt);
  7163. }
  7164. return error;
  7165. }
  7166. static void sdebug_driver_remove(struct device *dev)
  7167. {
  7168. struct sdebug_host_info *sdbg_host;
  7169. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  7170. sdbg_host = to_sdebug_host(dev);
  7171. scsi_remove_host(sdbg_host->shost);
  7172. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  7173. dev_list) {
  7174. list_del(&sdbg_devinfo->dev_list);
  7175. kfree(sdbg_devinfo->zstate);
  7176. kfree(sdbg_devinfo);
  7177. }
  7178. scsi_host_put(sdbg_host->shost);
  7179. }
  7180. static int pseudo_lld_bus_match(struct device *dev,
  7181. struct device_driver *dev_driver)
  7182. {
  7183. return 1;
  7184. }
  7185. static struct bus_type pseudo_lld_bus = {
  7186. .name = "pseudo",
  7187. .match = pseudo_lld_bus_match,
  7188. .probe = sdebug_driver_probe,
  7189. .remove = sdebug_driver_remove,
  7190. .drv_groups = sdebug_drv_groups,
  7191. };