scsi_debug.c 275 KB

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