scsi_debug.c 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999
  1. /*
  2. * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  3. * Copyright (C) 1992 Eric Youngdale
  4. * Simulate a host adapter with 2 disks attached. Do a lot of checking
  5. * to make sure that we are not getting blocks mixed up, and PANIC if
  6. * anything out of the ordinary is seen.
  7. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. *
  9. * Copyright (C) 2001 - 2018 Douglas Gilbert
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * For documentation see http://sg.danny.cz/sg/sdebug26.html
  17. *
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/string.h>
  27. #include <linux/genhd.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/crc-t10dif.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/atomic.h>
  39. #include <linux/hrtimer.h>
  40. #include <linux/uuid.h>
  41. #include <linux/t10-pi.h>
  42. #include <net/checksum.h>
  43. #include <asm/unaligned.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <scsi/scsi_device.h>
  47. #include <scsi/scsi_host.h>
  48. #include <scsi/scsicam.h>
  49. #include <scsi/scsi_eh.h>
  50. #include <scsi/scsi_tcq.h>
  51. #include <scsi/scsi_dbg.h>
  52. #include "sd.h"
  53. #include "scsi_logging.h"
  54. /* make sure inq_product_rev string corresponds to this version */
  55. #define SDEBUG_VERSION "0188" /* format to fit INQUIRY revision field */
  56. static const char *sdebug_version_date = "20190125";
  57. #define MY_NAME "scsi_debug"
  58. /* Additional Sense Code (ASC) */
  59. #define NO_ADDITIONAL_SENSE 0x0
  60. #define LOGICAL_UNIT_NOT_READY 0x4
  61. #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
  62. #define UNRECOVERED_READ_ERR 0x11
  63. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  64. #define INVALID_OPCODE 0x20
  65. #define LBA_OUT_OF_RANGE 0x21
  66. #define INVALID_FIELD_IN_CDB 0x24
  67. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  68. #define UA_RESET_ASC 0x29
  69. #define UA_CHANGED_ASC 0x2a
  70. #define TARGET_CHANGED_ASC 0x3f
  71. #define LUNS_CHANGED_ASCQ 0x0e
  72. #define INSUFF_RES_ASC 0x55
  73. #define INSUFF_RES_ASCQ 0x3
  74. #define POWER_ON_RESET_ASCQ 0x0
  75. #define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
  76. #define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
  77. #define CAPACITY_CHANGED_ASCQ 0x9
  78. #define SAVING_PARAMS_UNSUP 0x39
  79. #define TRANSPORT_PROBLEM 0x4b
  80. #define THRESHOLD_EXCEEDED 0x5d
  81. #define LOW_POWER_COND_ON 0x5e
  82. #define MISCOMPARE_VERIFY_ASC 0x1d
  83. #define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
  84. #define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
  85. #define WRITE_ERROR_ASC 0xc
  86. /* Additional Sense Code Qualifier (ASCQ) */
  87. #define ACK_NAK_TO 0x3
  88. /* Default values for driver parameters */
  89. #define DEF_NUM_HOST 1
  90. #define DEF_NUM_TGTS 1
  91. #define DEF_MAX_LUNS 1
  92. /* With these defaults, this driver will make 1 host with 1 target
  93. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  94. */
  95. #define DEF_ATO 1
  96. #define DEF_CDB_LEN 10
  97. #define DEF_JDELAY 1 /* if > 0 unit is a jiffy */
  98. #define DEF_DEV_SIZE_MB 8
  99. #define DEF_DIF 0
  100. #define DEF_DIX 0
  101. #define DEF_D_SENSE 0
  102. #define DEF_EVERY_NTH 0
  103. #define DEF_FAKE_RW 0
  104. #define DEF_GUARD 0
  105. #define DEF_HOST_LOCK 0
  106. #define DEF_LBPU 0
  107. #define DEF_LBPWS 0
  108. #define DEF_LBPWS10 0
  109. #define DEF_LBPRZ 1
  110. #define DEF_LOWEST_ALIGNED 0
  111. #define DEF_NDELAY 0 /* if > 0 unit is a nanosecond */
  112. #define DEF_NO_LUN_0 0
  113. #define DEF_NUM_PARTS 0
  114. #define DEF_OPTS 0
  115. #define DEF_OPT_BLKS 1024
  116. #define DEF_PHYSBLK_EXP 0
  117. #define DEF_OPT_XFERLEN_EXP 0
  118. #define DEF_PTYPE TYPE_DISK
  119. #define DEF_REMOVABLE false
  120. #define DEF_SCSI_LEVEL 7 /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
  121. #define DEF_SECTOR_SIZE 512
  122. #define DEF_UNMAP_ALIGNMENT 0
  123. #define DEF_UNMAP_GRANULARITY 1
  124. #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
  125. #define DEF_UNMAP_MAX_DESC 256
  126. #define DEF_VIRTUAL_GB 0
  127. #define DEF_VPD_USE_HOSTNO 1
  128. #define DEF_WRITESAME_LENGTH 0xFFFF
  129. #define DEF_STRICT 0
  130. #define DEF_STATISTICS false
  131. #define DEF_SUBMIT_QUEUES 1
  132. #define DEF_UUID_CTL 0
  133. #define JDELAY_OVERRIDDEN -9999
  134. #define SDEBUG_LUN_0_VAL 0
  135. /* bit mask values for sdebug_opts */
  136. #define SDEBUG_OPT_NOISE 1
  137. #define SDEBUG_OPT_MEDIUM_ERR 2
  138. #define SDEBUG_OPT_TIMEOUT 4
  139. #define SDEBUG_OPT_RECOVERED_ERR 8
  140. #define SDEBUG_OPT_TRANSPORT_ERR 16
  141. #define SDEBUG_OPT_DIF_ERR 32
  142. #define SDEBUG_OPT_DIX_ERR 64
  143. #define SDEBUG_OPT_MAC_TIMEOUT 128
  144. #define SDEBUG_OPT_SHORT_TRANSFER 0x100
  145. #define SDEBUG_OPT_Q_NOISE 0x200
  146. #define SDEBUG_OPT_ALL_TSF 0x400
  147. #define SDEBUG_OPT_RARE_TSF 0x800
  148. #define SDEBUG_OPT_N_WCE 0x1000
  149. #define SDEBUG_OPT_RESET_NOISE 0x2000
  150. #define SDEBUG_OPT_NO_CDB_NOISE 0x4000
  151. #define SDEBUG_OPT_HOST_BUSY 0x8000
  152. #define SDEBUG_OPT_CMD_ABORT 0x10000
  153. #define SDEBUG_OPT_ALL_NOISE (SDEBUG_OPT_NOISE | SDEBUG_OPT_Q_NOISE | \
  154. SDEBUG_OPT_RESET_NOISE)
  155. #define SDEBUG_OPT_ALL_INJECTING (SDEBUG_OPT_RECOVERED_ERR | \
  156. SDEBUG_OPT_TRANSPORT_ERR | \
  157. SDEBUG_OPT_DIF_ERR | SDEBUG_OPT_DIX_ERR | \
  158. SDEBUG_OPT_SHORT_TRANSFER | \
  159. SDEBUG_OPT_HOST_BUSY | \
  160. SDEBUG_OPT_CMD_ABORT)
  161. /* When "every_nth" > 0 then modulo "every_nth" commands:
  162. * - a missing response is simulated if SDEBUG_OPT_TIMEOUT is set
  163. * - a RECOVERED_ERROR is simulated on successful read and write
  164. * commands if SDEBUG_OPT_RECOVERED_ERR is set.
  165. * - a TRANSPORT_ERROR is simulated on successful read and write
  166. * commands if SDEBUG_OPT_TRANSPORT_ERR is set.
  167. * - similarly for DIF_ERR, DIX_ERR, SHORT_TRANSFER, HOST_BUSY and
  168. * CMD_ABORT
  169. *
  170. * When "every_nth" < 0 then after "- every_nth" commands the selected
  171. * error will be injected. The error will be injected on every subsequent
  172. * command until some other action occurs; for example, the user writing
  173. * a new value (other than -1 or 1) to every_nth:
  174. * echo 0 > /sys/bus/pseudo/drivers/scsi_debug/every_nth
  175. */
  176. /* As indicated in SAM-5 and SPC-4 Unit Attentions (UAs) are returned in
  177. * priority order. In the subset implemented here lower numbers have higher
  178. * priority. The UA numbers should be a sequence starting from 0 with
  179. * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */
  180. #define SDEBUG_UA_POR 0 /* Power on, reset, or bus device reset */
  181. #define SDEBUG_UA_BUS_RESET 1
  182. #define SDEBUG_UA_MODE_CHANGED 2
  183. #define SDEBUG_UA_CAPACITY_CHANGED 3
  184. #define SDEBUG_UA_LUNS_CHANGED 4
  185. #define SDEBUG_UA_MICROCODE_CHANGED 5 /* simulate firmware change */
  186. #define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6
  187. #define SDEBUG_NUM_UAS 7
  188. /* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  189. * sector on read commands: */
  190. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  191. #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
  192. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  193. * or "peripheral device" addressing (value 0) */
  194. #define SAM2_LUN_ADDRESS_METHOD 0
  195. /* SDEBUG_CANQUEUE is the maximum number of commands that can be queued
  196. * (for response) per submit queue at one time. Can be reduced by max_queue
  197. * option. Command responses are not queued when jdelay=0 and ndelay=0. The
  198. * per-device DEF_CMD_PER_LUN can be changed via sysfs:
  199. * /sys/class/scsi_device/<h:c:t:l>/device/queue_depth
  200. * but cannot exceed SDEBUG_CANQUEUE .
  201. */
  202. #define SDEBUG_CANQUEUE_WORDS 3 /* a WORD is bits in a long */
  203. #define SDEBUG_CANQUEUE (SDEBUG_CANQUEUE_WORDS * BITS_PER_LONG)
  204. #define DEF_CMD_PER_LUN 255
  205. #define F_D_IN 1
  206. #define F_D_OUT 2
  207. #define F_D_OUT_MAYBE 4 /* WRITE SAME, NDOB bit */
  208. #define F_D_UNKN 8
  209. #define F_RL_WLUN_OK 0x10
  210. #define F_SKIP_UA 0x20
  211. #define F_DELAY_OVERR 0x40
  212. #define F_SA_LOW 0x80 /* cdb byte 1, bits 4 to 0 */
  213. #define F_SA_HIGH 0x100 /* as used by variable length cdbs */
  214. #define F_INV_OP 0x200
  215. #define F_FAKE_RW 0x400
  216. #define F_M_ACCESS 0x800 /* media access */
  217. #define F_SSU_DELAY 0x1000
  218. #define F_SYNC_DELAY 0x2000
  219. #define FF_RESPOND (F_RL_WLUN_OK | F_SKIP_UA | F_DELAY_OVERR)
  220. #define FF_MEDIA_IO (F_M_ACCESS | F_FAKE_RW)
  221. #define FF_SA (F_SA_HIGH | F_SA_LOW)
  222. #define F_LONG_DELAY (F_SSU_DELAY | F_SYNC_DELAY)
  223. #define SDEBUG_MAX_PARTS 4
  224. #define SDEBUG_MAX_CMD_LEN 32
  225. struct sdebug_dev_info {
  226. struct list_head dev_list;
  227. unsigned int channel;
  228. unsigned int target;
  229. u64 lun;
  230. uuid_t lu_name;
  231. struct sdebug_host_info *sdbg_host;
  232. unsigned long uas_bm[1];
  233. atomic_t num_in_q;
  234. atomic_t stopped;
  235. bool used;
  236. };
  237. struct sdebug_host_info {
  238. struct list_head host_list;
  239. struct Scsi_Host *shost;
  240. struct device dev;
  241. struct list_head dev_info_list;
  242. };
  243. #define to_sdebug_host(d) \
  244. container_of(d, struct sdebug_host_info, dev)
  245. enum sdeb_defer_type {SDEB_DEFER_NONE = 0, SDEB_DEFER_HRT = 1,
  246. SDEB_DEFER_WQ = 2};
  247. struct sdebug_defer {
  248. struct hrtimer hrt;
  249. struct execute_work ew;
  250. int sqa_idx; /* index of sdebug_queue array */
  251. int qc_idx; /* index of sdebug_queued_cmd array within sqa_idx */
  252. int issuing_cpu;
  253. bool init_hrt;
  254. bool init_wq;
  255. bool aborted; /* true when blk_abort_request() already called */
  256. enum sdeb_defer_type defer_t;
  257. };
  258. struct sdebug_queued_cmd {
  259. /* corresponding bit set in in_use_bm[] in owning struct sdebug_queue
  260. * instance indicates this slot is in use.
  261. */
  262. struct sdebug_defer *sd_dp;
  263. struct scsi_cmnd *a_cmnd;
  264. unsigned int inj_recovered:1;
  265. unsigned int inj_transport:1;
  266. unsigned int inj_dif:1;
  267. unsigned int inj_dix:1;
  268. unsigned int inj_short:1;
  269. unsigned int inj_host_busy:1;
  270. unsigned int inj_cmd_abort:1;
  271. };
  272. struct sdebug_queue {
  273. struct sdebug_queued_cmd qc_arr[SDEBUG_CANQUEUE];
  274. unsigned long in_use_bm[SDEBUG_CANQUEUE_WORDS];
  275. spinlock_t qc_lock;
  276. atomic_t blocked; /* to temporarily stop more being queued */
  277. };
  278. static atomic_t sdebug_cmnd_count; /* number of incoming commands */
  279. static atomic_t sdebug_completions; /* count of deferred completions */
  280. static atomic_t sdebug_miss_cpus; /* submission + completion cpus differ */
  281. static atomic_t sdebug_a_tsf; /* 'almost task set full' counter */
  282. struct opcode_info_t {
  283. u8 num_attached; /* 0 if this is it (i.e. a leaf); use 0xff */
  284. /* for terminating element */
  285. u8 opcode; /* if num_attached > 0, preferred */
  286. u16 sa; /* service action */
  287. u32 flags; /* OR-ed set of SDEB_F_* */
  288. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  289. const struct opcode_info_t *arrp; /* num_attached elements or NULL */
  290. u8 len_mask[16]; /* len_mask[0]-->cdb_len, then mask for cdb */
  291. /* 1 to min(cdb_len, 15); ignore cdb[15...] */
  292. };
  293. /* SCSI opcodes (first byte of cdb) of interest mapped onto these indexes */
  294. enum sdeb_opcode_index {
  295. SDEB_I_INVALID_OPCODE = 0,
  296. SDEB_I_INQUIRY = 1,
  297. SDEB_I_REPORT_LUNS = 2,
  298. SDEB_I_REQUEST_SENSE = 3,
  299. SDEB_I_TEST_UNIT_READY = 4,
  300. SDEB_I_MODE_SENSE = 5, /* 6, 10 */
  301. SDEB_I_MODE_SELECT = 6, /* 6, 10 */
  302. SDEB_I_LOG_SENSE = 7,
  303. SDEB_I_READ_CAPACITY = 8, /* 10; 16 is in SA_IN(16) */
  304. SDEB_I_READ = 9, /* 6, 10, 12, 16 */
  305. SDEB_I_WRITE = 10, /* 6, 10, 12, 16 */
  306. SDEB_I_START_STOP = 11,
  307. SDEB_I_SERV_ACT_IN_16 = 12, /* add ...SERV_ACT_IN_12 if needed */
  308. SDEB_I_SERV_ACT_OUT_16 = 13, /* add ...SERV_ACT_OUT_12 if needed */
  309. SDEB_I_MAINT_IN = 14,
  310. SDEB_I_MAINT_OUT = 15,
  311. SDEB_I_VERIFY = 16, /* 10 only */
  312. SDEB_I_VARIABLE_LEN = 17, /* READ(32), WRITE(32), WR_SCAT(32) */
  313. SDEB_I_RESERVE = 18, /* 6, 10 */
  314. SDEB_I_RELEASE = 19, /* 6, 10 */
  315. SDEB_I_ALLOW_REMOVAL = 20, /* PREVENT ALLOW MEDIUM REMOVAL */
  316. SDEB_I_REZERO_UNIT = 21, /* REWIND in SSC */
  317. SDEB_I_ATA_PT = 22, /* 12, 16 */
  318. SDEB_I_SEND_DIAG = 23,
  319. SDEB_I_UNMAP = 24,
  320. SDEB_I_XDWRITEREAD = 25, /* 10 only */
  321. SDEB_I_WRITE_BUFFER = 26,
  322. SDEB_I_WRITE_SAME = 27, /* 10, 16 */
  323. SDEB_I_SYNC_CACHE = 28, /* 10, 16 */
  324. SDEB_I_COMP_WRITE = 29,
  325. SDEB_I_LAST_ELEMENT = 30, /* keep this last (previous + 1) */
  326. };
  327. static const unsigned char opcode_ind_arr[256] = {
  328. /* 0x0; 0x0->0x1f: 6 byte cdbs */
  329. SDEB_I_TEST_UNIT_READY, SDEB_I_REZERO_UNIT, 0, SDEB_I_REQUEST_SENSE,
  330. 0, 0, 0, 0,
  331. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, 0,
  332. 0, 0, SDEB_I_INQUIRY, 0, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  333. SDEB_I_RELEASE,
  334. 0, 0, SDEB_I_MODE_SENSE, SDEB_I_START_STOP, 0, SDEB_I_SEND_DIAG,
  335. SDEB_I_ALLOW_REMOVAL, 0,
  336. /* 0x20; 0x20->0x3f: 10 byte cdbs */
  337. 0, 0, 0, 0, 0, SDEB_I_READ_CAPACITY, 0, 0,
  338. SDEB_I_READ, 0, SDEB_I_WRITE, 0, 0, 0, 0, SDEB_I_VERIFY,
  339. 0, 0, 0, 0, 0, SDEB_I_SYNC_CACHE, 0, 0,
  340. 0, 0, 0, SDEB_I_WRITE_BUFFER, 0, 0, 0, 0,
  341. /* 0x40; 0x40->0x5f: 10 byte cdbs */
  342. 0, SDEB_I_WRITE_SAME, SDEB_I_UNMAP, 0, 0, 0, 0, 0,
  343. 0, 0, 0, 0, 0, SDEB_I_LOG_SENSE, 0, 0,
  344. 0, 0, 0, SDEB_I_XDWRITEREAD, 0, SDEB_I_MODE_SELECT, SDEB_I_RESERVE,
  345. SDEB_I_RELEASE,
  346. 0, 0, SDEB_I_MODE_SENSE, 0, 0, 0, 0, 0,
  347. /* 0x60; 0x60->0x7d are reserved, 0x7e is "extended cdb" */
  348. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  349. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  350. 0, SDEB_I_VARIABLE_LEN,
  351. /* 0x80; 0x80->0x9f: 16 byte cdbs */
  352. 0, 0, 0, 0, 0, SDEB_I_ATA_PT, 0, 0,
  353. SDEB_I_READ, SDEB_I_COMP_WRITE, SDEB_I_WRITE, 0, 0, 0, 0, 0,
  354. 0, SDEB_I_SYNC_CACHE, 0, SDEB_I_WRITE_SAME, 0, 0, 0, 0,
  355. 0, 0, 0, 0, 0, 0, SDEB_I_SERV_ACT_IN_16, SDEB_I_SERV_ACT_OUT_16,
  356. /* 0xa0; 0xa0->0xbf: 12 byte cdbs */
  357. SDEB_I_REPORT_LUNS, SDEB_I_ATA_PT, 0, SDEB_I_MAINT_IN,
  358. SDEB_I_MAINT_OUT, 0, 0, 0,
  359. SDEB_I_READ, 0 /* SDEB_I_SERV_ACT_OUT_12 */, SDEB_I_WRITE,
  360. 0 /* SDEB_I_SERV_ACT_IN_12 */, 0, 0, 0, 0,
  361. 0, 0, 0, 0, 0, 0, 0, 0,
  362. 0, 0, 0, 0, 0, 0, 0, 0,
  363. /* 0xc0; 0xc0->0xff: vendor specific */
  364. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  365. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  366. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  367. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  368. };
  369. /*
  370. * The following "response" functions return the SCSI mid-level's 4 byte
  371. * tuple-in-an-int. To handle commands with an IMMED bit, for a faster
  372. * command completion, they can mask their return value with
  373. * SDEG_RES_IMMED_MASK .
  374. */
  375. #define SDEG_RES_IMMED_MASK 0x40000000
  376. static int resp_inquiry(struct scsi_cmnd *, struct sdebug_dev_info *);
  377. static int resp_report_luns(struct scsi_cmnd *, struct sdebug_dev_info *);
  378. static int resp_requests(struct scsi_cmnd *, struct sdebug_dev_info *);
  379. static int resp_mode_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  380. static int resp_mode_select(struct scsi_cmnd *, struct sdebug_dev_info *);
  381. static int resp_log_sense(struct scsi_cmnd *, struct sdebug_dev_info *);
  382. static int resp_readcap(struct scsi_cmnd *, struct sdebug_dev_info *);
  383. static int resp_read_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  384. static int resp_write_dt0(struct scsi_cmnd *, struct sdebug_dev_info *);
  385. static int resp_write_scat(struct scsi_cmnd *, struct sdebug_dev_info *);
  386. static int resp_start_stop(struct scsi_cmnd *, struct sdebug_dev_info *);
  387. static int resp_readcap16(struct scsi_cmnd *, struct sdebug_dev_info *);
  388. static int resp_get_lba_status(struct scsi_cmnd *, struct sdebug_dev_info *);
  389. static int resp_report_tgtpgs(struct scsi_cmnd *, struct sdebug_dev_info *);
  390. static int resp_unmap(struct scsi_cmnd *, struct sdebug_dev_info *);
  391. static int resp_rsup_opcodes(struct scsi_cmnd *, struct sdebug_dev_info *);
  392. static int resp_rsup_tmfs(struct scsi_cmnd *, struct sdebug_dev_info *);
  393. static int resp_write_same_10(struct scsi_cmnd *, struct sdebug_dev_info *);
  394. static int resp_write_same_16(struct scsi_cmnd *, struct sdebug_dev_info *);
  395. static int resp_xdwriteread_10(struct scsi_cmnd *, struct sdebug_dev_info *);
  396. static int resp_comp_write(struct scsi_cmnd *, struct sdebug_dev_info *);
  397. static int resp_write_buffer(struct scsi_cmnd *, struct sdebug_dev_info *);
  398. static int resp_sync_cache(struct scsi_cmnd *, struct sdebug_dev_info *);
  399. /*
  400. * The following are overflow arrays for cdbs that "hit" the same index in
  401. * the opcode_info_arr array. The most time sensitive (or commonly used) cdb
  402. * should be placed in opcode_info_arr[], the others should be placed here.
  403. */
  404. static const struct opcode_info_t msense_iarr[] = {
  405. {0, 0x1a, 0, F_D_IN, NULL, NULL,
  406. {6, 0xe8, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  407. };
  408. static const struct opcode_info_t mselect_iarr[] = {
  409. {0, 0x15, 0, F_D_OUT, NULL, NULL,
  410. {6, 0xf1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  411. };
  412. static const struct opcode_info_t read_iarr[] = {
  413. {0, 0x28, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(10) */
  414. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  415. 0, 0, 0, 0} },
  416. {0, 0x8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL, /* READ(6) */
  417. {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  418. {0, 0xa8, 0, F_D_IN | FF_MEDIA_IO, resp_read_dt0, NULL,/* READ(12) */
  419. {12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf,
  420. 0xc7, 0, 0, 0, 0} },
  421. };
  422. static const struct opcode_info_t write_iarr[] = {
  423. {0, 0x2a, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(10) */
  424. NULL, {10, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
  425. 0, 0, 0, 0, 0, 0} },
  426. {0, 0xa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(6) */
  427. NULL, {6, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0,
  428. 0, 0, 0} },
  429. {0, 0xaa, 0, F_D_OUT | FF_MEDIA_IO, resp_write_dt0, /* WRITE(12) */
  430. NULL, {12, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  431. 0xbf, 0xc7, 0, 0, 0, 0} },
  432. };
  433. static const struct opcode_info_t sa_in_16_iarr[] = {
  434. {0, 0x9e, 0x12, F_SA_LOW | F_D_IN, resp_get_lba_status, NULL,
  435. {16, 0x12, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  436. 0xff, 0xff, 0xff, 0, 0xc7} }, /* GET LBA STATUS(16) */
  437. };
  438. static const struct opcode_info_t vl_iarr[] = { /* VARIABLE LENGTH */
  439. {0, 0x7f, 0xb, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_dt0,
  440. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0xb, 0xfa,
  441. 0, 0xff, 0xff, 0xff, 0xff} }, /* WRITE(32) */
  442. {0, 0x7f, 0x11, F_SA_HIGH | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  443. NULL, {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x11, 0xf8,
  444. 0, 0xff, 0xff, 0x0, 0x0} }, /* WRITE SCATTERED(32) */
  445. };
  446. static const struct opcode_info_t maint_in_iarr[] = { /* MAINT IN */
  447. {0, 0xa3, 0xc, F_SA_LOW | F_D_IN, resp_rsup_opcodes, NULL,
  448. {12, 0xc, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0,
  449. 0xc7, 0, 0, 0, 0} }, /* REPORT SUPPORTED OPERATION CODES */
  450. {0, 0xa3, 0xd, F_SA_LOW | F_D_IN, resp_rsup_tmfs, NULL,
  451. {12, 0xd, 0x80, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  452. 0, 0} }, /* REPORTED SUPPORTED TASK MANAGEMENT FUNCTIONS */
  453. };
  454. static const struct opcode_info_t write_same_iarr[] = {
  455. {0, 0x93, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, resp_write_same_16, NULL,
  456. {16, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  457. 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* WRITE SAME(16) */
  458. };
  459. static const struct opcode_info_t reserve_iarr[] = {
  460. {0, 0x16, 0, F_D_OUT, NULL, NULL, /* RESERVE(6) */
  461. {6, 0x1f, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  462. };
  463. static const struct opcode_info_t release_iarr[] = {
  464. {0, 0x17, 0, F_D_OUT, NULL, NULL, /* RELEASE(6) */
  465. {6, 0x1f, 0xff, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  466. };
  467. static const struct opcode_info_t sync_cache_iarr[] = {
  468. {0, 0x91, 0, F_SYNC_DELAY | F_M_ACCESS, resp_sync_cache, NULL,
  469. {16, 0x6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  470. 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc7} }, /* SYNC_CACHE (16) */
  471. };
  472. /* This array is accessed via SDEB_I_* values. Make sure all are mapped,
  473. * plus the terminating elements for logic that scans this table such as
  474. * REPORT SUPPORTED OPERATION CODES. */
  475. static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEMENT + 1] = {
  476. /* 0 */
  477. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* unknown opcodes */
  478. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  479. {0, 0x12, 0, FF_RESPOND | F_D_IN, resp_inquiry, NULL, /* INQUIRY */
  480. {6, 0xe3, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  481. {0, 0xa0, 0, FF_RESPOND | F_D_IN, resp_report_luns, NULL,
  482. {12, 0xe3, 0xff, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0xc7, 0, 0,
  483. 0, 0} }, /* REPORT LUNS */
  484. {0, 0x3, 0, FF_RESPOND | F_D_IN, resp_requests, NULL,
  485. {6, 0xe1, 0, 0, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  486. {0, 0x0, 0, F_M_ACCESS | F_RL_WLUN_OK, NULL, NULL,/* TEST UNIT READY */
  487. {6, 0, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  488. /* 5 */
  489. {ARRAY_SIZE(msense_iarr), 0x5a, 0, F_D_IN, /* MODE SENSE(10) */
  490. resp_mode_sense, msense_iarr, {10, 0xf8, 0xff, 0xff, 0, 0, 0,
  491. 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  492. {ARRAY_SIZE(mselect_iarr), 0x55, 0, F_D_OUT, /* MODE SELECT(10) */
  493. resp_mode_select, mselect_iarr, {10, 0xf1, 0, 0, 0, 0, 0, 0xff,
  494. 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  495. {0, 0x4d, 0, F_D_IN, resp_log_sense, NULL, /* LOG SENSE */
  496. {10, 0xe3, 0xff, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0, 0,
  497. 0, 0, 0} },
  498. {0, 0x25, 0, F_D_IN, resp_readcap, NULL, /* READ CAPACITY(10) */
  499. {10, 0xe1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0x1, 0xc7, 0, 0, 0, 0,
  500. 0, 0} },
  501. {ARRAY_SIZE(read_iarr), 0x88, 0, F_D_IN | FF_MEDIA_IO, /* READ(16) */
  502. resp_read_dt0, read_iarr, {16, 0xfe, 0xff, 0xff, 0xff, 0xff,
  503. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  504. /* 10 */
  505. {ARRAY_SIZE(write_iarr), 0x8a, 0, F_D_OUT | FF_MEDIA_IO,
  506. resp_write_dt0, write_iarr, /* WRITE(16) */
  507. {16, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  508. 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7} },
  509. {0, 0x1b, 0, F_SSU_DELAY, resp_start_stop, NULL,/* START STOP UNIT */
  510. {6, 0x1, 0, 0xf, 0xf7, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  511. {ARRAY_SIZE(sa_in_16_iarr), 0x9e, 0x10, F_SA_LOW | F_D_IN,
  512. resp_readcap16, sa_in_16_iarr, /* SA_IN(16), READ CAPACITY(16) */
  513. {16, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  514. 0xff, 0xff, 0xff, 0xff, 0x1, 0xc7} },
  515. {0, 0x9f, 0x12, F_SA_LOW | F_D_OUT | FF_MEDIA_IO, resp_write_scat,
  516. NULL, {16, 0x12, 0xf9, 0x0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0xff,
  517. 0xff, 0xff, 0xff, 0xff, 0xc7} }, /* SA_OUT(16), WRITE SCAT(16) */
  518. {ARRAY_SIZE(maint_in_iarr), 0xa3, 0xa, F_SA_LOW | F_D_IN,
  519. resp_report_tgtpgs, /* MAINT IN, REPORT TARGET PORT GROUPS */
  520. maint_in_iarr, {12, 0xea, 0, 0, 0, 0, 0xff, 0xff, 0xff,
  521. 0xff, 0, 0xc7, 0, 0, 0, 0} },
  522. /* 15 */
  523. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* MAINT OUT */
  524. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  525. {0, 0x2f, 0, F_D_OUT_MAYBE | FF_MEDIA_IO, NULL, NULL, /* VERIFY(10) */
  526. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7,
  527. 0, 0, 0, 0, 0, 0} },
  528. {ARRAY_SIZE(vl_iarr), 0x7f, 0x9, F_SA_HIGH | F_D_IN | FF_MEDIA_IO,
  529. resp_read_dt0, vl_iarr, /* VARIABLE LENGTH, READ(32) */
  530. {32, 0xc7, 0, 0, 0, 0, 0x3f, 0x18, 0x0, 0x9, 0xfe, 0, 0xff, 0xff,
  531. 0xff, 0xff} },
  532. {ARRAY_SIZE(reserve_iarr), 0x56, 0, F_D_OUT,
  533. NULL, reserve_iarr, /* RESERVE(10) <no response function> */
  534. {10, 0xff, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  535. 0} },
  536. {ARRAY_SIZE(release_iarr), 0x57, 0, F_D_OUT,
  537. NULL, release_iarr, /* RELEASE(10) <no response function> */
  538. {10, 0x13, 0xff, 0xff, 0, 0, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0,
  539. 0} },
  540. /* 20 */
  541. {0, 0x1e, 0, 0, NULL, NULL, /* ALLOW REMOVAL */
  542. {6, 0, 0, 0, 0x3, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  543. {0, 0x1, 0, 0, resp_start_stop, NULL, /* REWIND ?? */
  544. {6, 0x1, 0, 0, 0, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  545. {0, 0, 0, F_INV_OP | FF_RESPOND, NULL, NULL, /* ATA_PT */
  546. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  547. {0, 0x1d, F_D_OUT, 0, NULL, NULL, /* SEND DIAGNOSTIC */
  548. {6, 0xf7, 0, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  549. {0, 0x42, 0, F_D_OUT | FF_MEDIA_IO, resp_unmap, NULL, /* UNMAP */
  550. {10, 0x1, 0, 0, 0, 0, 0x3f, 0xff, 0xff, 0xc7, 0, 0, 0, 0, 0, 0} },
  551. /* 25 */
  552. {0, 0x53, 0, F_D_IN | F_D_OUT | FF_MEDIA_IO, resp_xdwriteread_10,
  553. NULL, {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7,
  554. 0, 0, 0, 0, 0, 0} }, /* XDWRITEREAD(10) */
  555. {0, 0x3b, 0, F_D_OUT_MAYBE, resp_write_buffer, NULL,
  556. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0, 0,
  557. 0, 0, 0, 0} }, /* WRITE_BUFFER */
  558. {ARRAY_SIZE(write_same_iarr), 0x41, 0, F_D_OUT_MAYBE | FF_MEDIA_IO,
  559. resp_write_same_10, write_same_iarr, /* WRITE SAME(10) */
  560. {10, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0,
  561. 0, 0, 0, 0, 0} },
  562. {ARRAY_SIZE(sync_cache_iarr), 0x35, 0, F_SYNC_DELAY | F_M_ACCESS,
  563. resp_sync_cache, sync_cache_iarr,
  564. {10, 0x7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc7, 0, 0,
  565. 0, 0, 0, 0} }, /* SYNC_CACHE (10) */
  566. {0, 0x89, 0, F_D_OUT | FF_MEDIA_IO, resp_comp_write, NULL,
  567. {16, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0,
  568. 0, 0xff, 0x3f, 0xc7} }, /* COMPARE AND WRITE */
  569. /* 30 */
  570. {0xff, 0, 0, 0, NULL, NULL, /* terminating element */
  571. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} },
  572. };
  573. static int sdebug_add_host = DEF_NUM_HOST;
  574. static int sdebug_ato = DEF_ATO;
  575. static int sdebug_cdb_len = DEF_CDB_LEN;
  576. static int sdebug_jdelay = DEF_JDELAY; /* if > 0 then unit is jiffies */
  577. static int sdebug_dev_size_mb = DEF_DEV_SIZE_MB;
  578. static int sdebug_dif = DEF_DIF;
  579. static int sdebug_dix = DEF_DIX;
  580. static int sdebug_dsense = DEF_D_SENSE;
  581. static int sdebug_every_nth = DEF_EVERY_NTH;
  582. static int sdebug_fake_rw = DEF_FAKE_RW;
  583. static unsigned int sdebug_guard = DEF_GUARD;
  584. static int sdebug_lowest_aligned = DEF_LOWEST_ALIGNED;
  585. static int sdebug_max_luns = DEF_MAX_LUNS;
  586. static int sdebug_max_queue = SDEBUG_CANQUEUE; /* per submit queue */
  587. static unsigned int sdebug_medium_error_start = OPT_MEDIUM_ERR_ADDR;
  588. static int sdebug_medium_error_count = OPT_MEDIUM_ERR_NUM;
  589. static atomic_t retired_max_queue; /* if > 0 then was prior max_queue */
  590. static int sdebug_ndelay = DEF_NDELAY; /* if > 0 then unit is nanoseconds */
  591. static int sdebug_no_lun_0 = DEF_NO_LUN_0;
  592. static int sdebug_no_uld;
  593. static int sdebug_num_parts = DEF_NUM_PARTS;
  594. static int sdebug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  595. static int sdebug_opt_blks = DEF_OPT_BLKS;
  596. static int sdebug_opts = DEF_OPTS;
  597. static int sdebug_physblk_exp = DEF_PHYSBLK_EXP;
  598. static int sdebug_opt_xferlen_exp = DEF_OPT_XFERLEN_EXP;
  599. static int sdebug_ptype = DEF_PTYPE; /* SCSI peripheral device type */
  600. static int sdebug_scsi_level = DEF_SCSI_LEVEL;
  601. static int sdebug_sector_size = DEF_SECTOR_SIZE;
  602. static int sdebug_virtual_gb = DEF_VIRTUAL_GB;
  603. static int sdebug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  604. static unsigned int sdebug_lbpu = DEF_LBPU;
  605. static unsigned int sdebug_lbpws = DEF_LBPWS;
  606. static unsigned int sdebug_lbpws10 = DEF_LBPWS10;
  607. static unsigned int sdebug_lbprz = DEF_LBPRZ;
  608. static unsigned int sdebug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
  609. static unsigned int sdebug_unmap_granularity = DEF_UNMAP_GRANULARITY;
  610. static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
  611. static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
  612. static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
  613. static int sdebug_uuid_ctl = DEF_UUID_CTL;
  614. static bool sdebug_removable = DEF_REMOVABLE;
  615. static bool sdebug_clustering;
  616. static bool sdebug_host_lock = DEF_HOST_LOCK;
  617. static bool sdebug_strict = DEF_STRICT;
  618. static bool sdebug_any_injecting_opt;
  619. static bool sdebug_verbose;
  620. static bool have_dif_prot;
  621. static bool write_since_sync;
  622. static bool sdebug_statistics = DEF_STATISTICS;
  623. static unsigned int sdebug_store_sectors;
  624. static sector_t sdebug_capacity; /* in sectors */
  625. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  626. may still need them */
  627. static int sdebug_heads; /* heads per disk */
  628. static int sdebug_cylinders_per; /* cylinders per surface */
  629. static int sdebug_sectors_per; /* sectors per cylinder */
  630. static LIST_HEAD(sdebug_host_list);
  631. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  632. static unsigned char *fake_storep; /* ramdisk storage */
  633. static struct t10_pi_tuple *dif_storep; /* protection info */
  634. static void *map_storep; /* provisioning map */
  635. static unsigned long map_size;
  636. static int num_aborts;
  637. static int num_dev_resets;
  638. static int num_target_resets;
  639. static int num_bus_resets;
  640. static int num_host_resets;
  641. static int dix_writes;
  642. static int dix_reads;
  643. static int dif_errors;
  644. static int submit_queues = DEF_SUBMIT_QUEUES; /* > 1 for multi-queue (mq) */
  645. static struct sdebug_queue *sdebug_q_arr; /* ptr to array of submit queues */
  646. static DEFINE_RWLOCK(atomic_rw);
  647. static char sdebug_proc_name[] = MY_NAME;
  648. static const char *my_name = MY_NAME;
  649. static struct bus_type pseudo_lld_bus;
  650. static struct device_driver sdebug_driverfs_driver = {
  651. .name = sdebug_proc_name,
  652. .bus = &pseudo_lld_bus,
  653. };
  654. static const int check_condition_result =
  655. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  656. static const int illegal_condition_result =
  657. (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
  658. static const int device_qfull_result =
  659. (DID_OK << 16) | (COMMAND_COMPLETE << 8) | SAM_STAT_TASK_SET_FULL;
  660. /* Only do the extra work involved in logical block provisioning if one or
  661. * more of the lbpu, lbpws or lbpws10 parameters are given and we are doing
  662. * real reads and writes (i.e. not skipping them for speed).
  663. */
  664. static inline bool scsi_debug_lbp(void)
  665. {
  666. return 0 == sdebug_fake_rw &&
  667. (sdebug_lbpu || sdebug_lbpws || sdebug_lbpws10);
  668. }
  669. static void *lba2fake_store(unsigned long long lba)
  670. {
  671. lba = do_div(lba, sdebug_store_sectors);
  672. return fake_storep + lba * sdebug_sector_size;
  673. }
  674. static struct t10_pi_tuple *dif_store(sector_t sector)
  675. {
  676. sector = sector_div(sector, sdebug_store_sectors);
  677. return dif_storep + sector;
  678. }
  679. static void sdebug_max_tgts_luns(void)
  680. {
  681. struct sdebug_host_info *sdbg_host;
  682. struct Scsi_Host *hpnt;
  683. spin_lock(&sdebug_host_list_lock);
  684. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  685. hpnt = sdbg_host->shost;
  686. if ((hpnt->this_id >= 0) &&
  687. (sdebug_num_tgts > hpnt->this_id))
  688. hpnt->max_id = sdebug_num_tgts + 1;
  689. else
  690. hpnt->max_id = sdebug_num_tgts;
  691. /* sdebug_max_luns; */
  692. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  693. }
  694. spin_unlock(&sdebug_host_list_lock);
  695. }
  696. enum sdeb_cmd_data {SDEB_IN_DATA = 0, SDEB_IN_CDB = 1};
  697. /* Set in_bit to -1 to indicate no bit position of invalid field */
  698. static void mk_sense_invalid_fld(struct scsi_cmnd *scp,
  699. enum sdeb_cmd_data c_d,
  700. int in_byte, int in_bit)
  701. {
  702. unsigned char *sbuff;
  703. u8 sks[4];
  704. int sl, asc;
  705. sbuff = scp->sense_buffer;
  706. if (!sbuff) {
  707. sdev_printk(KERN_ERR, scp->device,
  708. "%s: sense_buffer is NULL\n", __func__);
  709. return;
  710. }
  711. asc = c_d ? INVALID_FIELD_IN_CDB : INVALID_FIELD_IN_PARAM_LIST;
  712. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  713. scsi_build_sense_buffer(sdebug_dsense, sbuff, ILLEGAL_REQUEST, asc, 0);
  714. memset(sks, 0, sizeof(sks));
  715. sks[0] = 0x80;
  716. if (c_d)
  717. sks[0] |= 0x40;
  718. if (in_bit >= 0) {
  719. sks[0] |= 0x8;
  720. sks[0] |= 0x7 & in_bit;
  721. }
  722. put_unaligned_be16(in_byte, sks + 1);
  723. if (sdebug_dsense) {
  724. sl = sbuff[7] + 8;
  725. sbuff[7] = sl;
  726. sbuff[sl] = 0x2;
  727. sbuff[sl + 1] = 0x6;
  728. memcpy(sbuff + sl + 4, sks, 3);
  729. } else
  730. memcpy(sbuff + 15, sks, 3);
  731. if (sdebug_verbose)
  732. sdev_printk(KERN_INFO, scp->device, "%s: [sense_key,asc,ascq"
  733. "]: [0x5,0x%x,0x0] %c byte=%d, bit=%d\n",
  734. my_name, asc, c_d ? 'C' : 'D', in_byte, in_bit);
  735. }
  736. static void mk_sense_buffer(struct scsi_cmnd *scp, int key, int asc, int asq)
  737. {
  738. unsigned char *sbuff;
  739. sbuff = scp->sense_buffer;
  740. if (!sbuff) {
  741. sdev_printk(KERN_ERR, scp->device,
  742. "%s: sense_buffer is NULL\n", __func__);
  743. return;
  744. }
  745. memset(sbuff, 0, SCSI_SENSE_BUFFERSIZE);
  746. scsi_build_sense_buffer(sdebug_dsense, sbuff, key, asc, asq);
  747. if (sdebug_verbose)
  748. sdev_printk(KERN_INFO, scp->device,
  749. "%s: [sense_key,asc,ascq]: [0x%x,0x%x,0x%x]\n",
  750. my_name, key, asc, asq);
  751. }
  752. static void mk_sense_invalid_opcode(struct scsi_cmnd *scp)
  753. {
  754. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  755. }
  756. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  757. {
  758. if (sdebug_verbose) {
  759. if (0x1261 == cmd)
  760. sdev_printk(KERN_INFO, dev,
  761. "%s: BLKFLSBUF [0x1261]\n", __func__);
  762. else if (0x5331 == cmd)
  763. sdev_printk(KERN_INFO, dev,
  764. "%s: CDROM_GET_CAPABILITY [0x5331]\n",
  765. __func__);
  766. else
  767. sdev_printk(KERN_INFO, dev, "%s: cmd=0x%x\n",
  768. __func__, cmd);
  769. }
  770. return -EINVAL;
  771. /* return -ENOTTY; // correct return but upsets fdisk */
  772. }
  773. static void config_cdb_len(struct scsi_device *sdev)
  774. {
  775. switch (sdebug_cdb_len) {
  776. case 6: /* suggest 6 byte READ, WRITE and MODE SENSE/SELECT */
  777. sdev->use_10_for_rw = false;
  778. sdev->use_16_for_rw = false;
  779. sdev->use_10_for_ms = false;
  780. break;
  781. case 10: /* suggest 10 byte RWs and 6 byte MODE SENSE/SELECT */
  782. sdev->use_10_for_rw = true;
  783. sdev->use_16_for_rw = false;
  784. sdev->use_10_for_ms = false;
  785. break;
  786. case 12: /* suggest 10 byte RWs and 10 byte MODE SENSE/SELECT */
  787. sdev->use_10_for_rw = true;
  788. sdev->use_16_for_rw = false;
  789. sdev->use_10_for_ms = true;
  790. break;
  791. case 16:
  792. sdev->use_10_for_rw = false;
  793. sdev->use_16_for_rw = true;
  794. sdev->use_10_for_ms = true;
  795. break;
  796. case 32: /* No knobs to suggest this so same as 16 for now */
  797. sdev->use_10_for_rw = false;
  798. sdev->use_16_for_rw = true;
  799. sdev->use_10_for_ms = true;
  800. break;
  801. default:
  802. pr_warn("unexpected cdb_len=%d, force to 10\n",
  803. sdebug_cdb_len);
  804. sdev->use_10_for_rw = true;
  805. sdev->use_16_for_rw = false;
  806. sdev->use_10_for_ms = false;
  807. sdebug_cdb_len = 10;
  808. break;
  809. }
  810. }
  811. static void all_config_cdb_len(void)
  812. {
  813. struct sdebug_host_info *sdbg_host;
  814. struct Scsi_Host *shost;
  815. struct scsi_device *sdev;
  816. spin_lock(&sdebug_host_list_lock);
  817. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  818. shost = sdbg_host->shost;
  819. shost_for_each_device(sdev, shost) {
  820. config_cdb_len(sdev);
  821. }
  822. }
  823. spin_unlock(&sdebug_host_list_lock);
  824. }
  825. static void clear_luns_changed_on_target(struct sdebug_dev_info *devip)
  826. {
  827. struct sdebug_host_info *sdhp;
  828. struct sdebug_dev_info *dp;
  829. spin_lock(&sdebug_host_list_lock);
  830. list_for_each_entry(sdhp, &sdebug_host_list, host_list) {
  831. list_for_each_entry(dp, &sdhp->dev_info_list, dev_list) {
  832. if ((devip->sdbg_host == dp->sdbg_host) &&
  833. (devip->target == dp->target))
  834. clear_bit(SDEBUG_UA_LUNS_CHANGED, dp->uas_bm);
  835. }
  836. }
  837. spin_unlock(&sdebug_host_list_lock);
  838. }
  839. static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  840. {
  841. int k;
  842. k = find_first_bit(devip->uas_bm, SDEBUG_NUM_UAS);
  843. if (k != SDEBUG_NUM_UAS) {
  844. const char *cp = NULL;
  845. switch (k) {
  846. case SDEBUG_UA_POR:
  847. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  848. POWER_ON_RESET_ASCQ);
  849. if (sdebug_verbose)
  850. cp = "power on reset";
  851. break;
  852. case SDEBUG_UA_BUS_RESET:
  853. mk_sense_buffer(scp, UNIT_ATTENTION, UA_RESET_ASC,
  854. BUS_RESET_ASCQ);
  855. if (sdebug_verbose)
  856. cp = "bus reset";
  857. break;
  858. case SDEBUG_UA_MODE_CHANGED:
  859. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  860. MODE_CHANGED_ASCQ);
  861. if (sdebug_verbose)
  862. cp = "mode parameters changed";
  863. break;
  864. case SDEBUG_UA_CAPACITY_CHANGED:
  865. mk_sense_buffer(scp, UNIT_ATTENTION, UA_CHANGED_ASC,
  866. CAPACITY_CHANGED_ASCQ);
  867. if (sdebug_verbose)
  868. cp = "capacity data changed";
  869. break;
  870. case SDEBUG_UA_MICROCODE_CHANGED:
  871. mk_sense_buffer(scp, UNIT_ATTENTION,
  872. TARGET_CHANGED_ASC,
  873. MICROCODE_CHANGED_ASCQ);
  874. if (sdebug_verbose)
  875. cp = "microcode has been changed";
  876. break;
  877. case SDEBUG_UA_MICROCODE_CHANGED_WO_RESET:
  878. mk_sense_buffer(scp, UNIT_ATTENTION,
  879. TARGET_CHANGED_ASC,
  880. MICROCODE_CHANGED_WO_RESET_ASCQ);
  881. if (sdebug_verbose)
  882. cp = "microcode has been changed without reset";
  883. break;
  884. case SDEBUG_UA_LUNS_CHANGED:
  885. /*
  886. * SPC-3 behavior is to report a UNIT ATTENTION with
  887. * ASC/ASCQ REPORTED LUNS DATA HAS CHANGED on every LUN
  888. * on the target, until a REPORT LUNS command is
  889. * received. SPC-4 behavior is to report it only once.
  890. * NOTE: sdebug_scsi_level does not use the same
  891. * values as struct scsi_device->scsi_level.
  892. */
  893. if (sdebug_scsi_level >= 6) /* SPC-4 and above */
  894. clear_luns_changed_on_target(devip);
  895. mk_sense_buffer(scp, UNIT_ATTENTION,
  896. TARGET_CHANGED_ASC,
  897. LUNS_CHANGED_ASCQ);
  898. if (sdebug_verbose)
  899. cp = "reported luns data has changed";
  900. break;
  901. default:
  902. pr_warn("unexpected unit attention code=%d\n", k);
  903. if (sdebug_verbose)
  904. cp = "unknown";
  905. break;
  906. }
  907. clear_bit(k, devip->uas_bm);
  908. if (sdebug_verbose)
  909. sdev_printk(KERN_INFO, scp->device,
  910. "%s reports: Unit attention: %s\n",
  911. my_name, cp);
  912. return check_condition_result;
  913. }
  914. return 0;
  915. }
  916. /* Build SCSI "data-in" buffer. Returns 0 if ok else (DID_ERROR << 16). */
  917. static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  918. int arr_len)
  919. {
  920. int act_len;
  921. struct scsi_data_buffer *sdb = scsi_in(scp);
  922. if (!sdb->length)
  923. return 0;
  924. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  925. return DID_ERROR << 16;
  926. act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
  927. arr, arr_len);
  928. sdb->resid = scsi_bufflen(scp) - act_len;
  929. return 0;
  930. }
  931. /* Partial build of SCSI "data-in" buffer. Returns 0 if ok else
  932. * (DID_ERROR << 16). Can write to offset in data-in buffer. If multiple
  933. * calls, not required to write in ascending offset order. Assumes resid
  934. * set to scsi_bufflen() prior to any calls.
  935. */
  936. static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,
  937. int arr_len, unsigned int off_dst)
  938. {
  939. int act_len, n;
  940. struct scsi_data_buffer *sdb = scsi_in(scp);
  941. off_t skip = off_dst;
  942. if (sdb->length <= off_dst)
  943. return 0;
  944. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  945. return DID_ERROR << 16;
  946. act_len = sg_pcopy_from_buffer(sdb->table.sgl, sdb->table.nents,
  947. arr, arr_len, skip);
  948. pr_debug("%s: off_dst=%u, scsi_bufflen=%u, act_len=%u, resid=%d\n",
  949. __func__, off_dst, scsi_bufflen(scp), act_len, sdb->resid);
  950. n = (int)scsi_bufflen(scp) - ((int)off_dst + act_len);
  951. sdb->resid = min(sdb->resid, n);
  952. return 0;
  953. }
  954. /* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into
  955. * 'arr' or -1 if error.
  956. */
  957. static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  958. int arr_len)
  959. {
  960. if (!scsi_bufflen(scp))
  961. return 0;
  962. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
  963. return -1;
  964. return scsi_sg_copy_to_buffer(scp, arr, arr_len);
  965. }
  966. static char sdebug_inq_vendor_id[9] = "Linux ";
  967. static char sdebug_inq_product_id[17] = "scsi_debug ";
  968. static char sdebug_inq_product_rev[5] = SDEBUG_VERSION;
  969. /* Use some locally assigned NAAs for SAS addresses. */
  970. static const u64 naa3_comp_a = 0x3222222000000000ULL;
  971. static const u64 naa3_comp_b = 0x3333333000000000ULL;
  972. static const u64 naa3_comp_c = 0x3111111000000000ULL;
  973. /* Device identification VPD page. Returns number of bytes placed in arr */
  974. static int inquiry_vpd_83(unsigned char *arr, int port_group_id,
  975. int target_dev_id, int dev_id_num,
  976. const char *dev_id_str, int dev_id_str_len,
  977. const uuid_t *lu_name)
  978. {
  979. int num, port_a;
  980. char b[32];
  981. port_a = target_dev_id + 1;
  982. /* T10 vendor identifier field format (faked) */
  983. arr[0] = 0x2; /* ASCII */
  984. arr[1] = 0x1;
  985. arr[2] = 0x0;
  986. memcpy(&arr[4], sdebug_inq_vendor_id, 8);
  987. memcpy(&arr[12], sdebug_inq_product_id, 16);
  988. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  989. num = 8 + 16 + dev_id_str_len;
  990. arr[3] = num;
  991. num += 4;
  992. if (dev_id_num >= 0) {
  993. if (sdebug_uuid_ctl) {
  994. /* Locally assigned UUID */
  995. arr[num++] = 0x1; /* binary (not necessarily sas) */
  996. arr[num++] = 0xa; /* PIV=0, lu, naa */
  997. arr[num++] = 0x0;
  998. arr[num++] = 0x12;
  999. arr[num++] = 0x10; /* uuid type=1, locally assigned */
  1000. arr[num++] = 0x0;
  1001. memcpy(arr + num, lu_name, 16);
  1002. num += 16;
  1003. } else {
  1004. /* NAA-3, Logical unit identifier (binary) */
  1005. arr[num++] = 0x1; /* binary (not necessarily sas) */
  1006. arr[num++] = 0x3; /* PIV=0, lu, naa */
  1007. arr[num++] = 0x0;
  1008. arr[num++] = 0x8;
  1009. put_unaligned_be64(naa3_comp_b + dev_id_num, arr + num);
  1010. num += 8;
  1011. }
  1012. /* Target relative port number */
  1013. arr[num++] = 0x61; /* proto=sas, binary */
  1014. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  1015. arr[num++] = 0x0; /* reserved */
  1016. arr[num++] = 0x4; /* length */
  1017. arr[num++] = 0x0; /* reserved */
  1018. arr[num++] = 0x0; /* reserved */
  1019. arr[num++] = 0x0;
  1020. arr[num++] = 0x1; /* relative port A */
  1021. }
  1022. /* NAA-3, Target port identifier */
  1023. arr[num++] = 0x61; /* proto=sas, binary */
  1024. arr[num++] = 0x93; /* piv=1, target port, naa */
  1025. arr[num++] = 0x0;
  1026. arr[num++] = 0x8;
  1027. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1028. num += 8;
  1029. /* NAA-3, Target port group identifier */
  1030. arr[num++] = 0x61; /* proto=sas, binary */
  1031. arr[num++] = 0x95; /* piv=1, target port group id */
  1032. arr[num++] = 0x0;
  1033. arr[num++] = 0x4;
  1034. arr[num++] = 0;
  1035. arr[num++] = 0;
  1036. put_unaligned_be16(port_group_id, arr + num);
  1037. num += 2;
  1038. /* NAA-3, Target device identifier */
  1039. arr[num++] = 0x61; /* proto=sas, binary */
  1040. arr[num++] = 0xa3; /* piv=1, target device, naa */
  1041. arr[num++] = 0x0;
  1042. arr[num++] = 0x8;
  1043. put_unaligned_be64(naa3_comp_a + target_dev_id, arr + num);
  1044. num += 8;
  1045. /* SCSI name string: Target device identifier */
  1046. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  1047. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  1048. arr[num++] = 0x0;
  1049. arr[num++] = 24;
  1050. memcpy(arr + num, "naa.32222220", 12);
  1051. num += 12;
  1052. snprintf(b, sizeof(b), "%08X", target_dev_id);
  1053. memcpy(arr + num, b, 8);
  1054. num += 8;
  1055. memset(arr + num, 0, 4);
  1056. num += 4;
  1057. return num;
  1058. }
  1059. static unsigned char vpd84_data[] = {
  1060. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  1061. 0x22,0x22,0x22,0x0,0xbb,0x1,
  1062. 0x22,0x22,0x22,0x0,0xbb,0x2,
  1063. };
  1064. /* Software interface identification VPD page */
  1065. static int inquiry_vpd_84(unsigned char *arr)
  1066. {
  1067. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  1068. return sizeof(vpd84_data);
  1069. }
  1070. /* Management network addresses VPD page */
  1071. static int inquiry_vpd_85(unsigned char *arr)
  1072. {
  1073. int num = 0;
  1074. const char *na1 = "https://www.kernel.org/config";
  1075. const char *na2 = "http://www.kernel.org/log";
  1076. int plen, olen;
  1077. arr[num++] = 0x1; /* lu, storage config */
  1078. arr[num++] = 0x0; /* reserved */
  1079. arr[num++] = 0x0;
  1080. olen = strlen(na1);
  1081. plen = olen + 1;
  1082. if (plen % 4)
  1083. plen = ((plen / 4) + 1) * 4;
  1084. arr[num++] = plen; /* length, null termianted, padded */
  1085. memcpy(arr + num, na1, olen);
  1086. memset(arr + num + olen, 0, plen - olen);
  1087. num += plen;
  1088. arr[num++] = 0x4; /* lu, logging */
  1089. arr[num++] = 0x0; /* reserved */
  1090. arr[num++] = 0x0;
  1091. olen = strlen(na2);
  1092. plen = olen + 1;
  1093. if (plen % 4)
  1094. plen = ((plen / 4) + 1) * 4;
  1095. arr[num++] = plen; /* length, null terminated, padded */
  1096. memcpy(arr + num, na2, olen);
  1097. memset(arr + num + olen, 0, plen - olen);
  1098. num += plen;
  1099. return num;
  1100. }
  1101. /* SCSI ports VPD page */
  1102. static int inquiry_vpd_88(unsigned char *arr, int target_dev_id)
  1103. {
  1104. int num = 0;
  1105. int port_a, port_b;
  1106. port_a = target_dev_id + 1;
  1107. port_b = port_a + 1;
  1108. arr[num++] = 0x0; /* reserved */
  1109. arr[num++] = 0x0; /* reserved */
  1110. arr[num++] = 0x0;
  1111. arr[num++] = 0x1; /* relative port 1 (primary) */
  1112. memset(arr + num, 0, 6);
  1113. num += 6;
  1114. arr[num++] = 0x0;
  1115. arr[num++] = 12; /* length tp descriptor */
  1116. /* naa-5 target port identifier (A) */
  1117. arr[num++] = 0x61; /* proto=sas, binary */
  1118. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1119. arr[num++] = 0x0; /* reserved */
  1120. arr[num++] = 0x8; /* length */
  1121. put_unaligned_be64(naa3_comp_a + port_a, arr + num);
  1122. num += 8;
  1123. arr[num++] = 0x0; /* reserved */
  1124. arr[num++] = 0x0; /* reserved */
  1125. arr[num++] = 0x0;
  1126. arr[num++] = 0x2; /* relative port 2 (secondary) */
  1127. memset(arr + num, 0, 6);
  1128. num += 6;
  1129. arr[num++] = 0x0;
  1130. arr[num++] = 12; /* length tp descriptor */
  1131. /* naa-5 target port identifier (B) */
  1132. arr[num++] = 0x61; /* proto=sas, binary */
  1133. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  1134. arr[num++] = 0x0; /* reserved */
  1135. arr[num++] = 0x8; /* length */
  1136. put_unaligned_be64(naa3_comp_a + port_b, arr + num);
  1137. num += 8;
  1138. return num;
  1139. }
  1140. static unsigned char vpd89_data[] = {
  1141. /* from 4th byte */ 0,0,0,0,
  1142. 'l','i','n','u','x',' ',' ',' ',
  1143. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  1144. '1','2','3','4',
  1145. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  1146. 0xec,0,0,0,
  1147. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  1148. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  1149. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  1150. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  1151. 0x53,0x41,
  1152. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1153. 0x20,0x20,
  1154. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  1155. 0x10,0x80,
  1156. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  1157. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  1158. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  1159. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  1160. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  1161. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  1162. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  1163. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1164. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1165. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1166. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  1167. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  1168. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  1169. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  1170. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1171. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1172. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1173. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1174. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1175. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1176. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1177. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1178. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1179. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1180. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1181. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  1182. };
  1183. /* ATA Information VPD page */
  1184. static int inquiry_vpd_89(unsigned char *arr)
  1185. {
  1186. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  1187. return sizeof(vpd89_data);
  1188. }
  1189. static unsigned char vpdb0_data[] = {
  1190. /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
  1191. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1192. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1193. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1194. };
  1195. /* Block limits VPD page (SBC-3) */
  1196. static int inquiry_vpd_b0(unsigned char *arr)
  1197. {
  1198. unsigned int gran;
  1199. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  1200. /* Optimal transfer length granularity */
  1201. if (sdebug_opt_xferlen_exp != 0 &&
  1202. sdebug_physblk_exp < sdebug_opt_xferlen_exp)
  1203. gran = 1 << sdebug_opt_xferlen_exp;
  1204. else
  1205. gran = 1 << sdebug_physblk_exp;
  1206. put_unaligned_be16(gran, arr + 2);
  1207. /* Maximum Transfer Length */
  1208. if (sdebug_store_sectors > 0x400)
  1209. put_unaligned_be32(sdebug_store_sectors, arr + 4);
  1210. /* Optimal Transfer Length */
  1211. put_unaligned_be32(sdebug_opt_blks, &arr[8]);
  1212. if (sdebug_lbpu) {
  1213. /* Maximum Unmap LBA Count */
  1214. put_unaligned_be32(sdebug_unmap_max_blocks, &arr[16]);
  1215. /* Maximum Unmap Block Descriptor Count */
  1216. put_unaligned_be32(sdebug_unmap_max_desc, &arr[20]);
  1217. }
  1218. /* Unmap Granularity Alignment */
  1219. if (sdebug_unmap_alignment) {
  1220. put_unaligned_be32(sdebug_unmap_alignment, &arr[28]);
  1221. arr[28] |= 0x80; /* UGAVALID */
  1222. }
  1223. /* Optimal Unmap Granularity */
  1224. put_unaligned_be32(sdebug_unmap_granularity, &arr[24]);
  1225. /* Maximum WRITE SAME Length */
  1226. put_unaligned_be64(sdebug_write_same_length, &arr[32]);
  1227. return 0x3c; /* Mandatory page length for Logical Block Provisioning */
  1228. return sizeof(vpdb0_data);
  1229. }
  1230. /* Block device characteristics VPD page (SBC-3) */
  1231. static int inquiry_vpd_b1(unsigned char *arr)
  1232. {
  1233. memset(arr, 0, 0x3c);
  1234. arr[0] = 0;
  1235. arr[1] = 1; /* non rotating medium (e.g. solid state) */
  1236. arr[2] = 0;
  1237. arr[3] = 5; /* less than 1.8" */
  1238. return 0x3c;
  1239. }
  1240. /* Logical block provisioning VPD page (SBC-4) */
  1241. static int inquiry_vpd_b2(unsigned char *arr)
  1242. {
  1243. memset(arr, 0, 0x4);
  1244. arr[0] = 0; /* threshold exponent */
  1245. if (sdebug_lbpu)
  1246. arr[1] = 1 << 7;
  1247. if (sdebug_lbpws)
  1248. arr[1] |= 1 << 6;
  1249. if (sdebug_lbpws10)
  1250. arr[1] |= 1 << 5;
  1251. if (sdebug_lbprz && scsi_debug_lbp())
  1252. arr[1] |= (sdebug_lbprz & 0x7) << 2; /* sbc4r07 and later */
  1253. /* anc_sup=0; dp=0 (no provisioning group descriptor) */
  1254. /* minimum_percentage=0; provisioning_type=0 (unknown) */
  1255. /* threshold_percentage=0 */
  1256. return 0x4;
  1257. }
  1258. #define SDEBUG_LONG_INQ_SZ 96
  1259. #define SDEBUG_MAX_INQ_ARR_SZ 584
  1260. static int resp_inquiry(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  1261. {
  1262. unsigned char pq_pdt;
  1263. unsigned char *arr;
  1264. unsigned char *cmd = scp->cmnd;
  1265. int alloc_len, n, ret;
  1266. bool have_wlun, is_disk;
  1267. alloc_len = get_unaligned_be16(cmd + 3);
  1268. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  1269. if (! arr)
  1270. return DID_REQUEUE << 16;
  1271. is_disk = (sdebug_ptype == TYPE_DISK);
  1272. have_wlun = scsi_is_wlun(scp->device->lun);
  1273. if (have_wlun)
  1274. pq_pdt = TYPE_WLUN; /* present, wlun */
  1275. else if (sdebug_no_lun_0 && (devip->lun == SDEBUG_LUN_0_VAL))
  1276. pq_pdt = 0x7f; /* not present, PQ=3, PDT=0x1f */
  1277. else
  1278. pq_pdt = (sdebug_ptype & 0x1f);
  1279. arr[0] = pq_pdt;
  1280. if (0x2 & cmd[1]) { /* CMDDT bit set */
  1281. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 1);
  1282. kfree(arr);
  1283. return check_condition_result;
  1284. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  1285. int lu_id_num, port_group_id, target_dev_id, len;
  1286. char lu_id_str[6];
  1287. int host_no = devip->sdbg_host->shost->host_no;
  1288. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  1289. (devip->channel & 0x7f);
  1290. if (sdebug_vpd_use_hostno == 0)
  1291. host_no = 0;
  1292. lu_id_num = have_wlun ? -1 : (((host_no + 1) * 2000) +
  1293. (devip->target * 1000) + devip->lun);
  1294. target_dev_id = ((host_no + 1) * 2000) +
  1295. (devip->target * 1000) - 3;
  1296. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  1297. if (0 == cmd[2]) { /* supported vital product data pages */
  1298. arr[1] = cmd[2]; /*sanity */
  1299. n = 4;
  1300. arr[n++] = 0x0; /* this page */
  1301. arr[n++] = 0x80; /* unit serial number */
  1302. arr[n++] = 0x83; /* device identification */
  1303. arr[n++] = 0x84; /* software interface ident. */
  1304. arr[n++] = 0x85; /* management network addresses */
  1305. arr[n++] = 0x86; /* extended inquiry */
  1306. arr[n++] = 0x87; /* mode page policy */
  1307. arr[n++] = 0x88; /* SCSI ports */
  1308. if (is_disk) { /* SBC only */
  1309. arr[n++] = 0x89; /* ATA information */
  1310. arr[n++] = 0xb0; /* Block limits */
  1311. arr[n++] = 0xb1; /* Block characteristics */
  1312. arr[n++] = 0xb2; /* Logical Block Prov */
  1313. }
  1314. arr[3] = n - 4; /* number of supported VPD pages */
  1315. } else if (0x80 == cmd[2]) { /* unit serial number */
  1316. arr[1] = cmd[2]; /*sanity */
  1317. arr[3] = len;
  1318. memcpy(&arr[4], lu_id_str, len);
  1319. } else if (0x83 == cmd[2]) { /* device identification */
  1320. arr[1] = cmd[2]; /*sanity */
  1321. arr[3] = inquiry_vpd_83(&arr[4], port_group_id,
  1322. target_dev_id, lu_id_num,
  1323. lu_id_str, len,
  1324. &devip->lu_name);
  1325. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  1326. arr[1] = cmd[2]; /*sanity */
  1327. arr[3] = inquiry_vpd_84(&arr[4]);
  1328. } else if (0x85 == cmd[2]) { /* Management network addresses */
  1329. arr[1] = cmd[2]; /*sanity */
  1330. arr[3] = inquiry_vpd_85(&arr[4]);
  1331. } else if (0x86 == cmd[2]) { /* extended inquiry */
  1332. arr[1] = cmd[2]; /*sanity */
  1333. arr[3] = 0x3c; /* number of following entries */
  1334. if (sdebug_dif == T10_PI_TYPE3_PROTECTION)
  1335. arr[4] = 0x4; /* SPT: GRD_CHK:1 */
  1336. else if (have_dif_prot)
  1337. arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
  1338. else
  1339. arr[4] = 0x0; /* no protection stuff */
  1340. arr[5] = 0x7; /* head of q, ordered + simple q's */
  1341. } else if (0x87 == cmd[2]) { /* mode page policy */
  1342. arr[1] = cmd[2]; /*sanity */
  1343. arr[3] = 0x8; /* number of following entries */
  1344. arr[4] = 0x2; /* disconnect-reconnect mp */
  1345. arr[6] = 0x80; /* mlus, shared */
  1346. arr[8] = 0x18; /* protocol specific lu */
  1347. arr[10] = 0x82; /* mlus, per initiator port */
  1348. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  1349. arr[1] = cmd[2]; /*sanity */
  1350. arr[3] = inquiry_vpd_88(&arr[4], target_dev_id);
  1351. } else if (is_disk && 0x89 == cmd[2]) { /* ATA information */
  1352. arr[1] = cmd[2]; /*sanity */
  1353. n = inquiry_vpd_89(&arr[4]);
  1354. put_unaligned_be16(n, arr + 2);
  1355. } else if (is_disk && 0xb0 == cmd[2]) { /* Block limits */
  1356. arr[1] = cmd[2]; /*sanity */
  1357. arr[3] = inquiry_vpd_b0(&arr[4]);
  1358. } else if (is_disk && 0xb1 == cmd[2]) { /* Block char. */
  1359. arr[1] = cmd[2]; /*sanity */
  1360. arr[3] = inquiry_vpd_b1(&arr[4]);
  1361. } else if (is_disk && 0xb2 == cmd[2]) { /* LB Prov. */
  1362. arr[1] = cmd[2]; /*sanity */
  1363. arr[3] = inquiry_vpd_b2(&arr[4]);
  1364. } else {
  1365. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  1366. kfree(arr);
  1367. return check_condition_result;
  1368. }
  1369. len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
  1370. ret = fill_from_dev_buffer(scp, arr,
  1371. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1372. kfree(arr);
  1373. return ret;
  1374. }
  1375. /* drops through here for a standard inquiry */
  1376. arr[1] = sdebug_removable ? 0x80 : 0; /* Removable disk */
  1377. arr[2] = sdebug_scsi_level;
  1378. arr[3] = 2; /* response_data_format==2 */
  1379. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  1380. arr[5] = (int)have_dif_prot; /* PROTECT bit */
  1381. if (sdebug_vpd_use_hostno == 0)
  1382. arr[5] |= 0x10; /* claim: implicit TPGS */
  1383. arr[6] = 0x10; /* claim: MultiP */
  1384. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  1385. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  1386. memcpy(&arr[8], sdebug_inq_vendor_id, 8);
  1387. memcpy(&arr[16], sdebug_inq_product_id, 16);
  1388. memcpy(&arr[32], sdebug_inq_product_rev, 4);
  1389. /* Use Vendor Specific area to place driver date in ASCII hex */
  1390. memcpy(&arr[36], sdebug_version_date, 8);
  1391. /* version descriptors (2 bytes each) follow */
  1392. put_unaligned_be16(0xc0, arr + 58); /* SAM-6 no version claimed */
  1393. put_unaligned_be16(0x5c0, arr + 60); /* SPC-5 no version claimed */
  1394. n = 62;
  1395. if (is_disk) { /* SBC-4 no version claimed */
  1396. put_unaligned_be16(0x600, arr + n);
  1397. n += 2;
  1398. } else if (sdebug_ptype == TYPE_TAPE) { /* SSC-4 rev 3 */
  1399. put_unaligned_be16(0x525, arr + n);
  1400. n += 2;
  1401. }
  1402. put_unaligned_be16(0x2100, arr + n); /* SPL-4 no version claimed */
  1403. ret = fill_from_dev_buffer(scp, arr,
  1404. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  1405. kfree(arr);
  1406. return ret;
  1407. }
  1408. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1409. 0, 0, 0x0, 0x0};
  1410. static int resp_requests(struct scsi_cmnd *scp,
  1411. struct sdebug_dev_info *devip)
  1412. {
  1413. unsigned char *sbuff;
  1414. unsigned char *cmd = scp->cmnd;
  1415. unsigned char arr[SCSI_SENSE_BUFFERSIZE];
  1416. bool dsense;
  1417. int len = 18;
  1418. memset(arr, 0, sizeof(arr));
  1419. dsense = !!(cmd[1] & 1);
  1420. sbuff = scp->sense_buffer;
  1421. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  1422. if (dsense) {
  1423. arr[0] = 0x72;
  1424. arr[1] = 0x0; /* NO_SENSE in sense_key */
  1425. arr[2] = THRESHOLD_EXCEEDED;
  1426. arr[3] = 0xff; /* TEST set and MRIE==6 */
  1427. len = 8;
  1428. } else {
  1429. arr[0] = 0x70;
  1430. arr[2] = 0x0; /* NO_SENSE in sense_key */
  1431. arr[7] = 0xa; /* 18 byte sense buffer */
  1432. arr[12] = THRESHOLD_EXCEEDED;
  1433. arr[13] = 0xff; /* TEST set and MRIE==6 */
  1434. }
  1435. } else {
  1436. memcpy(arr, sbuff, SCSI_SENSE_BUFFERSIZE);
  1437. if (arr[0] >= 0x70 && dsense == sdebug_dsense)
  1438. ; /* have sense and formats match */
  1439. else if (arr[0] <= 0x70) {
  1440. if (dsense) {
  1441. memset(arr, 0, 8);
  1442. arr[0] = 0x72;
  1443. len = 8;
  1444. } else {
  1445. memset(arr, 0, 18);
  1446. arr[0] = 0x70;
  1447. arr[7] = 0xa;
  1448. }
  1449. } else if (dsense) {
  1450. memset(arr, 0, 8);
  1451. arr[0] = 0x72;
  1452. arr[1] = sbuff[2]; /* sense key */
  1453. arr[2] = sbuff[12]; /* asc */
  1454. arr[3] = sbuff[13]; /* ascq */
  1455. len = 8;
  1456. } else {
  1457. memset(arr, 0, 18);
  1458. arr[0] = 0x70;
  1459. arr[2] = sbuff[1];
  1460. arr[7] = 0xa;
  1461. arr[12] = sbuff[1];
  1462. arr[13] = sbuff[3];
  1463. }
  1464. }
  1465. mk_sense_buffer(scp, 0, NO_ADDITIONAL_SENSE, 0);
  1466. return fill_from_dev_buffer(scp, arr, len);
  1467. }
  1468. static int resp_start_stop(struct scsi_cmnd *scp,
  1469. struct sdebug_dev_info *devip)
  1470. {
  1471. unsigned char *cmd = scp->cmnd;
  1472. int power_cond, stop;
  1473. bool changing;
  1474. power_cond = (cmd[4] & 0xf0) >> 4;
  1475. if (power_cond) {
  1476. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, 7);
  1477. return check_condition_result;
  1478. }
  1479. stop = !(cmd[4] & 1);
  1480. changing = atomic_read(&devip->stopped) == !stop;
  1481. atomic_xchg(&devip->stopped, stop);
  1482. if (!changing || cmd[1] & 0x1) /* state unchanged or IMMED set */
  1483. return SDEG_RES_IMMED_MASK;
  1484. else
  1485. return 0;
  1486. }
  1487. static sector_t get_sdebug_capacity(void)
  1488. {
  1489. static const unsigned int gibibyte = 1073741824;
  1490. if (sdebug_virtual_gb > 0)
  1491. return (sector_t)sdebug_virtual_gb *
  1492. (gibibyte / sdebug_sector_size);
  1493. else
  1494. return sdebug_store_sectors;
  1495. }
  1496. #define SDEBUG_READCAP_ARR_SZ 8
  1497. static int resp_readcap(struct scsi_cmnd *scp,
  1498. struct sdebug_dev_info *devip)
  1499. {
  1500. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1501. unsigned int capac;
  1502. /* following just in case virtual_gb changed */
  1503. sdebug_capacity = get_sdebug_capacity();
  1504. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1505. if (sdebug_capacity < 0xffffffff) {
  1506. capac = (unsigned int)sdebug_capacity - 1;
  1507. put_unaligned_be32(capac, arr + 0);
  1508. } else
  1509. put_unaligned_be32(0xffffffff, arr + 0);
  1510. put_unaligned_be16(sdebug_sector_size, arr + 6);
  1511. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1512. }
  1513. #define SDEBUG_READCAP16_ARR_SZ 32
  1514. static int resp_readcap16(struct scsi_cmnd *scp,
  1515. struct sdebug_dev_info *devip)
  1516. {
  1517. unsigned char *cmd = scp->cmnd;
  1518. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1519. int alloc_len;
  1520. alloc_len = get_unaligned_be32(cmd + 10);
  1521. /* following just in case virtual_gb changed */
  1522. sdebug_capacity = get_sdebug_capacity();
  1523. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1524. put_unaligned_be64((u64)(sdebug_capacity - 1), arr + 0);
  1525. put_unaligned_be32(sdebug_sector_size, arr + 8);
  1526. arr[13] = sdebug_physblk_exp & 0xf;
  1527. arr[14] = (sdebug_lowest_aligned >> 8) & 0x3f;
  1528. if (scsi_debug_lbp()) {
  1529. arr[14] |= 0x80; /* LBPME */
  1530. /* from sbc4r07, this LBPRZ field is 1 bit, but the LBPRZ in
  1531. * the LB Provisioning VPD page is 3 bits. Note that lbprz=2
  1532. * in the wider field maps to 0 in this field.
  1533. */
  1534. if (sdebug_lbprz & 1) /* precisely what the draft requires */
  1535. arr[14] |= 0x40;
  1536. }
  1537. arr[15] = sdebug_lowest_aligned & 0xff;
  1538. if (have_dif_prot) {
  1539. arr[12] = (sdebug_dif - 1) << 1; /* P_TYPE */
  1540. arr[12] |= 1; /* PROT_EN */
  1541. }
  1542. return fill_from_dev_buffer(scp, arr,
  1543. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1544. }
  1545. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1546. static int resp_report_tgtpgs(struct scsi_cmnd *scp,
  1547. struct sdebug_dev_info *devip)
  1548. {
  1549. unsigned char *cmd = scp->cmnd;
  1550. unsigned char *arr;
  1551. int host_no = devip->sdbg_host->shost->host_no;
  1552. int n, ret, alen, rlen;
  1553. int port_group_a, port_group_b, port_a, port_b;
  1554. alen = get_unaligned_be32(cmd + 6);
  1555. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  1556. if (! arr)
  1557. return DID_REQUEUE << 16;
  1558. /*
  1559. * EVPD page 0x88 states we have two ports, one
  1560. * real and a fake port with no device connected.
  1561. * So we create two port groups with one port each
  1562. * and set the group with port B to unavailable.
  1563. */
  1564. port_a = 0x1; /* relative port A */
  1565. port_b = 0x2; /* relative port B */
  1566. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1567. (devip->channel & 0x7f);
  1568. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1569. (devip->channel & 0x7f) + 0x80;
  1570. /*
  1571. * The asymmetric access state is cycled according to the host_id.
  1572. */
  1573. n = 4;
  1574. if (sdebug_vpd_use_hostno == 0) {
  1575. arr[n++] = host_no % 3; /* Asymm access state */
  1576. arr[n++] = 0x0F; /* claim: all states are supported */
  1577. } else {
  1578. arr[n++] = 0x0; /* Active/Optimized path */
  1579. arr[n++] = 0x01; /* only support active/optimized paths */
  1580. }
  1581. put_unaligned_be16(port_group_a, arr + n);
  1582. n += 2;
  1583. arr[n++] = 0; /* Reserved */
  1584. arr[n++] = 0; /* Status code */
  1585. arr[n++] = 0; /* Vendor unique */
  1586. arr[n++] = 0x1; /* One port per group */
  1587. arr[n++] = 0; /* Reserved */
  1588. arr[n++] = 0; /* Reserved */
  1589. put_unaligned_be16(port_a, arr + n);
  1590. n += 2;
  1591. arr[n++] = 3; /* Port unavailable */
  1592. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1593. put_unaligned_be16(port_group_b, arr + n);
  1594. n += 2;
  1595. arr[n++] = 0; /* Reserved */
  1596. arr[n++] = 0; /* Status code */
  1597. arr[n++] = 0; /* Vendor unique */
  1598. arr[n++] = 0x1; /* One port per group */
  1599. arr[n++] = 0; /* Reserved */
  1600. arr[n++] = 0; /* Reserved */
  1601. put_unaligned_be16(port_b, arr + n);
  1602. n += 2;
  1603. rlen = n - 4;
  1604. put_unaligned_be32(rlen, arr + 0);
  1605. /*
  1606. * Return the smallest value of either
  1607. * - The allocated length
  1608. * - The constructed command length
  1609. * - The maximum array size
  1610. */
  1611. rlen = min(alen,n);
  1612. ret = fill_from_dev_buffer(scp, arr,
  1613. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1614. kfree(arr);
  1615. return ret;
  1616. }
  1617. static int resp_rsup_opcodes(struct scsi_cmnd *scp,
  1618. struct sdebug_dev_info *devip)
  1619. {
  1620. bool rctd;
  1621. u8 reporting_opts, req_opcode, sdeb_i, supp;
  1622. u16 req_sa, u;
  1623. u32 alloc_len, a_len;
  1624. int k, offset, len, errsts, count, bump, na;
  1625. const struct opcode_info_t *oip;
  1626. const struct opcode_info_t *r_oip;
  1627. u8 *arr;
  1628. u8 *cmd = scp->cmnd;
  1629. rctd = !!(cmd[2] & 0x80);
  1630. reporting_opts = cmd[2] & 0x7;
  1631. req_opcode = cmd[3];
  1632. req_sa = get_unaligned_be16(cmd + 4);
  1633. alloc_len = get_unaligned_be32(cmd + 6);
  1634. if (alloc_len < 4 || alloc_len > 0xffff) {
  1635. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1636. return check_condition_result;
  1637. }
  1638. if (alloc_len > 8192)
  1639. a_len = 8192;
  1640. else
  1641. a_len = alloc_len;
  1642. arr = kzalloc((a_len < 256) ? 320 : a_len + 64, GFP_ATOMIC);
  1643. if (NULL == arr) {
  1644. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  1645. INSUFF_RES_ASCQ);
  1646. return check_condition_result;
  1647. }
  1648. switch (reporting_opts) {
  1649. case 0: /* all commands */
  1650. /* count number of commands */
  1651. for (count = 0, oip = opcode_info_arr;
  1652. oip->num_attached != 0xff; ++oip) {
  1653. if (F_INV_OP & oip->flags)
  1654. continue;
  1655. count += (oip->num_attached + 1);
  1656. }
  1657. bump = rctd ? 20 : 8;
  1658. put_unaligned_be32(count * bump, arr);
  1659. for (offset = 4, oip = opcode_info_arr;
  1660. oip->num_attached != 0xff && offset < a_len; ++oip) {
  1661. if (F_INV_OP & oip->flags)
  1662. continue;
  1663. na = oip->num_attached;
  1664. arr[offset] = oip->opcode;
  1665. put_unaligned_be16(oip->sa, arr + offset + 2);
  1666. if (rctd)
  1667. arr[offset + 5] |= 0x2;
  1668. if (FF_SA & oip->flags)
  1669. arr[offset + 5] |= 0x1;
  1670. put_unaligned_be16(oip->len_mask[0], arr + offset + 6);
  1671. if (rctd)
  1672. put_unaligned_be16(0xa, arr + offset + 8);
  1673. r_oip = oip;
  1674. for (k = 0, oip = oip->arrp; k < na; ++k, ++oip) {
  1675. if (F_INV_OP & oip->flags)
  1676. continue;
  1677. offset += bump;
  1678. arr[offset] = oip->opcode;
  1679. put_unaligned_be16(oip->sa, arr + offset + 2);
  1680. if (rctd)
  1681. arr[offset + 5] |= 0x2;
  1682. if (FF_SA & oip->flags)
  1683. arr[offset + 5] |= 0x1;
  1684. put_unaligned_be16(oip->len_mask[0],
  1685. arr + offset + 6);
  1686. if (rctd)
  1687. put_unaligned_be16(0xa,
  1688. arr + offset + 8);
  1689. }
  1690. oip = r_oip;
  1691. offset += bump;
  1692. }
  1693. break;
  1694. case 1: /* one command: opcode only */
  1695. case 2: /* one command: opcode plus service action */
  1696. case 3: /* one command: if sa==0 then opcode only else opcode+sa */
  1697. sdeb_i = opcode_ind_arr[req_opcode];
  1698. oip = &opcode_info_arr[sdeb_i];
  1699. if (F_INV_OP & oip->flags) {
  1700. supp = 1;
  1701. offset = 4;
  1702. } else {
  1703. if (1 == reporting_opts) {
  1704. if (FF_SA & oip->flags) {
  1705. mk_sense_invalid_fld(scp, SDEB_IN_CDB,
  1706. 2, 2);
  1707. kfree(arr);
  1708. return check_condition_result;
  1709. }
  1710. req_sa = 0;
  1711. } else if (2 == reporting_opts &&
  1712. 0 == (FF_SA & oip->flags)) {
  1713. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 4, -1);
  1714. kfree(arr); /* point at requested sa */
  1715. return check_condition_result;
  1716. }
  1717. if (0 == (FF_SA & oip->flags) &&
  1718. req_opcode == oip->opcode)
  1719. supp = 3;
  1720. else if (0 == (FF_SA & oip->flags)) {
  1721. na = oip->num_attached;
  1722. for (k = 0, oip = oip->arrp; k < na;
  1723. ++k, ++oip) {
  1724. if (req_opcode == oip->opcode)
  1725. break;
  1726. }
  1727. supp = (k >= na) ? 1 : 3;
  1728. } else if (req_sa != oip->sa) {
  1729. na = oip->num_attached;
  1730. for (k = 0, oip = oip->arrp; k < na;
  1731. ++k, ++oip) {
  1732. if (req_sa == oip->sa)
  1733. break;
  1734. }
  1735. supp = (k >= na) ? 1 : 3;
  1736. } else
  1737. supp = 3;
  1738. if (3 == supp) {
  1739. u = oip->len_mask[0];
  1740. put_unaligned_be16(u, arr + 2);
  1741. arr[4] = oip->opcode;
  1742. for (k = 1; k < u; ++k)
  1743. arr[4 + k] = (k < 16) ?
  1744. oip->len_mask[k] : 0xff;
  1745. offset = 4 + u;
  1746. } else
  1747. offset = 4;
  1748. }
  1749. arr[1] = (rctd ? 0x80 : 0) | supp;
  1750. if (rctd) {
  1751. put_unaligned_be16(0xa, arr + offset);
  1752. offset += 12;
  1753. }
  1754. break;
  1755. default:
  1756. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 2);
  1757. kfree(arr);
  1758. return check_condition_result;
  1759. }
  1760. offset = (offset < a_len) ? offset : a_len;
  1761. len = (offset < alloc_len) ? offset : alloc_len;
  1762. errsts = fill_from_dev_buffer(scp, arr, len);
  1763. kfree(arr);
  1764. return errsts;
  1765. }
  1766. static int resp_rsup_tmfs(struct scsi_cmnd *scp,
  1767. struct sdebug_dev_info *devip)
  1768. {
  1769. bool repd;
  1770. u32 alloc_len, len;
  1771. u8 arr[16];
  1772. u8 *cmd = scp->cmnd;
  1773. memset(arr, 0, sizeof(arr));
  1774. repd = !!(cmd[2] & 0x80);
  1775. alloc_len = get_unaligned_be32(cmd + 6);
  1776. if (alloc_len < 4) {
  1777. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  1778. return check_condition_result;
  1779. }
  1780. arr[0] = 0xc8; /* ATS | ATSS | LURS */
  1781. arr[1] = 0x1; /* ITNRS */
  1782. if (repd) {
  1783. arr[3] = 0xc;
  1784. len = 16;
  1785. } else
  1786. len = 4;
  1787. len = (len < alloc_len) ? len : alloc_len;
  1788. return fill_from_dev_buffer(scp, arr, len);
  1789. }
  1790. /* <<Following mode page info copied from ST318451LW>> */
  1791. static int resp_err_recov_pg(unsigned char *p, int pcontrol, int target)
  1792. { /* Read-Write Error Recovery page for mode_sense */
  1793. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1794. 5, 0, 0xff, 0xff};
  1795. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1796. if (1 == pcontrol)
  1797. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1798. return sizeof(err_recov_pg);
  1799. }
  1800. static int resp_disconnect_pg(unsigned char *p, int pcontrol, int target)
  1801. { /* Disconnect-Reconnect page for mode_sense */
  1802. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1803. 0, 0, 0, 0, 0, 0, 0, 0};
  1804. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1805. if (1 == pcontrol)
  1806. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1807. return sizeof(disconnect_pg);
  1808. }
  1809. static int resp_format_pg(unsigned char *p, int pcontrol, int target)
  1810. { /* Format device page for mode_sense */
  1811. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1812. 0, 0, 0, 0, 0, 0, 0, 0,
  1813. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1814. memcpy(p, format_pg, sizeof(format_pg));
  1815. put_unaligned_be16(sdebug_sectors_per, p + 10);
  1816. put_unaligned_be16(sdebug_sector_size, p + 12);
  1817. if (sdebug_removable)
  1818. p[20] |= 0x20; /* should agree with INQUIRY */
  1819. if (1 == pcontrol)
  1820. memset(p + 2, 0, sizeof(format_pg) - 2);
  1821. return sizeof(format_pg);
  1822. }
  1823. static unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1824. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0,
  1825. 0, 0, 0, 0};
  1826. static int resp_caching_pg(unsigned char *p, int pcontrol, int target)
  1827. { /* Caching page for mode_sense */
  1828. unsigned char ch_caching_pg[] = {/* 0x8, 18, */ 0x4, 0, 0, 0, 0, 0,
  1829. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1830. unsigned char d_caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1831. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1832. if (SDEBUG_OPT_N_WCE & sdebug_opts)
  1833. caching_pg[2] &= ~0x4; /* set WCE=0 (default WCE=1) */
  1834. memcpy(p, caching_pg, sizeof(caching_pg));
  1835. if (1 == pcontrol)
  1836. memcpy(p + 2, ch_caching_pg, sizeof(ch_caching_pg));
  1837. else if (2 == pcontrol)
  1838. memcpy(p, d_caching_pg, sizeof(d_caching_pg));
  1839. return sizeof(caching_pg);
  1840. }
  1841. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1842. 0, 0, 0x2, 0x4b};
  1843. static int resp_ctrl_m_pg(unsigned char *p, int pcontrol, int target)
  1844. { /* Control mode page for mode_sense */
  1845. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1846. 0, 0, 0, 0};
  1847. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1848. 0, 0, 0x2, 0x4b};
  1849. if (sdebug_dsense)
  1850. ctrl_m_pg[2] |= 0x4;
  1851. else
  1852. ctrl_m_pg[2] &= ~0x4;
  1853. if (sdebug_ato)
  1854. ctrl_m_pg[5] |= 0x80; /* ATO=1 */
  1855. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1856. if (1 == pcontrol)
  1857. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1858. else if (2 == pcontrol)
  1859. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1860. return sizeof(ctrl_m_pg);
  1861. }
  1862. static int resp_iec_m_pg(unsigned char *p, int pcontrol, int target)
  1863. { /* Informational Exceptions control mode page for mode_sense */
  1864. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1865. 0, 0, 0x0, 0x0};
  1866. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1867. 0, 0, 0x0, 0x0};
  1868. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1869. if (1 == pcontrol)
  1870. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1871. else if (2 == pcontrol)
  1872. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1873. return sizeof(iec_m_pg);
  1874. }
  1875. static int resp_sas_sf_m_pg(unsigned char *p, int pcontrol, int target)
  1876. { /* SAS SSP mode page - short format for mode_sense */
  1877. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1878. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1879. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1880. if (1 == pcontrol)
  1881. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1882. return sizeof(sas_sf_m_pg);
  1883. }
  1884. static int resp_sas_pcd_m_spg(unsigned char *p, int pcontrol, int target,
  1885. int target_dev_id)
  1886. { /* SAS phy control and discover mode page for mode_sense */
  1887. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1888. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1889. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1890. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1891. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1892. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1893. 0, 0, 0, 0, 0, 0, 0, 0,
  1894. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1895. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1896. 0, 0, 0, 0, 0, 0, 0, 0, /* insert SAS addr */
  1897. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1898. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1899. 0, 0, 0, 0, 0, 0, 0, 0,
  1900. };
  1901. int port_a, port_b;
  1902. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 16);
  1903. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 24);
  1904. put_unaligned_be64(naa3_comp_a, sas_pcd_m_pg + 64);
  1905. put_unaligned_be64(naa3_comp_c + 1, sas_pcd_m_pg + 72);
  1906. port_a = target_dev_id + 1;
  1907. port_b = port_a + 1;
  1908. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1909. put_unaligned_be32(port_a, p + 20);
  1910. put_unaligned_be32(port_b, p + 48 + 20);
  1911. if (1 == pcontrol)
  1912. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1913. return sizeof(sas_pcd_m_pg);
  1914. }
  1915. static int resp_sas_sha_m_spg(unsigned char *p, int pcontrol)
  1916. { /* SAS SSP shared protocol specific port mode subpage */
  1917. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1918. 0, 0, 0, 0, 0, 0, 0, 0,
  1919. };
  1920. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1921. if (1 == pcontrol)
  1922. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1923. return sizeof(sas_sha_m_pg);
  1924. }
  1925. #define SDEBUG_MAX_MSENSE_SZ 256
  1926. static int resp_mode_sense(struct scsi_cmnd *scp,
  1927. struct sdebug_dev_info *devip)
  1928. {
  1929. int pcontrol, pcode, subpcode, bd_len;
  1930. unsigned char dev_spec;
  1931. int alloc_len, offset, len, target_dev_id;
  1932. int target = scp->device->id;
  1933. unsigned char *ap;
  1934. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1935. unsigned char *cmd = scp->cmnd;
  1936. bool dbd, llbaa, msense_6, is_disk, bad_pcode;
  1937. dbd = !!(cmd[1] & 0x8); /* disable block descriptors */
  1938. pcontrol = (cmd[2] & 0xc0) >> 6;
  1939. pcode = cmd[2] & 0x3f;
  1940. subpcode = cmd[3];
  1941. msense_6 = (MODE_SENSE == cmd[0]);
  1942. llbaa = msense_6 ? false : !!(cmd[1] & 0x10);
  1943. is_disk = (sdebug_ptype == TYPE_DISK);
  1944. if (is_disk && !dbd)
  1945. bd_len = llbaa ? 16 : 8;
  1946. else
  1947. bd_len = 0;
  1948. alloc_len = msense_6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  1949. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1950. if (0x3 == pcontrol) { /* Saving values not supported */
  1951. mk_sense_buffer(scp, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP, 0);
  1952. return check_condition_result;
  1953. }
  1954. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1955. (devip->target * 1000) - 3;
  1956. /* for disks set DPOFUA bit and clear write protect (WP) bit */
  1957. if (is_disk)
  1958. dev_spec = 0x10; /* =0x90 if WP=1 implies read-only */
  1959. else
  1960. dev_spec = 0x0;
  1961. if (msense_6) {
  1962. arr[2] = dev_spec;
  1963. arr[3] = bd_len;
  1964. offset = 4;
  1965. } else {
  1966. arr[3] = dev_spec;
  1967. if (16 == bd_len)
  1968. arr[4] = 0x1; /* set LONGLBA bit */
  1969. arr[7] = bd_len; /* assume 255 or less */
  1970. offset = 8;
  1971. }
  1972. ap = arr + offset;
  1973. if ((bd_len > 0) && (!sdebug_capacity))
  1974. sdebug_capacity = get_sdebug_capacity();
  1975. if (8 == bd_len) {
  1976. if (sdebug_capacity > 0xfffffffe)
  1977. put_unaligned_be32(0xffffffff, ap + 0);
  1978. else
  1979. put_unaligned_be32(sdebug_capacity, ap + 0);
  1980. put_unaligned_be16(sdebug_sector_size, ap + 6);
  1981. offset += bd_len;
  1982. ap = arr + offset;
  1983. } else if (16 == bd_len) {
  1984. put_unaligned_be64((u64)sdebug_capacity, ap + 0);
  1985. put_unaligned_be32(sdebug_sector_size, ap + 12);
  1986. offset += bd_len;
  1987. ap = arr + offset;
  1988. }
  1989. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1990. /* TODO: Control Extension page */
  1991. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  1992. return check_condition_result;
  1993. }
  1994. bad_pcode = false;
  1995. switch (pcode) {
  1996. case 0x1: /* Read-Write error recovery page, direct access */
  1997. len = resp_err_recov_pg(ap, pcontrol, target);
  1998. offset += len;
  1999. break;
  2000. case 0x2: /* Disconnect-Reconnect page, all devices */
  2001. len = resp_disconnect_pg(ap, pcontrol, target);
  2002. offset += len;
  2003. break;
  2004. case 0x3: /* Format device page, direct access */
  2005. if (is_disk) {
  2006. len = resp_format_pg(ap, pcontrol, target);
  2007. offset += len;
  2008. } else
  2009. bad_pcode = true;
  2010. break;
  2011. case 0x8: /* Caching page, direct access */
  2012. if (is_disk) {
  2013. len = resp_caching_pg(ap, pcontrol, target);
  2014. offset += len;
  2015. } else
  2016. bad_pcode = true;
  2017. break;
  2018. case 0xa: /* Control Mode page, all devices */
  2019. len = resp_ctrl_m_pg(ap, pcontrol, target);
  2020. offset += len;
  2021. break;
  2022. case 0x19: /* if spc==1 then sas phy, control+discover */
  2023. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  2024. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2025. return check_condition_result;
  2026. }
  2027. len = 0;
  2028. if ((0x0 == subpcode) || (0xff == subpcode))
  2029. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2030. if ((0x1 == subpcode) || (0xff == subpcode))
  2031. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  2032. target_dev_id);
  2033. if ((0x2 == subpcode) || (0xff == subpcode))
  2034. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2035. offset += len;
  2036. break;
  2037. case 0x1c: /* Informational Exceptions Mode page, all devices */
  2038. len = resp_iec_m_pg(ap, pcontrol, target);
  2039. offset += len;
  2040. break;
  2041. case 0x3f: /* Read all Mode pages */
  2042. if ((0 == subpcode) || (0xff == subpcode)) {
  2043. len = resp_err_recov_pg(ap, pcontrol, target);
  2044. len += resp_disconnect_pg(ap + len, pcontrol, target);
  2045. if (is_disk) {
  2046. len += resp_format_pg(ap + len, pcontrol,
  2047. target);
  2048. len += resp_caching_pg(ap + len, pcontrol,
  2049. target);
  2050. }
  2051. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  2052. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  2053. if (0xff == subpcode) {
  2054. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  2055. target, target_dev_id);
  2056. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  2057. }
  2058. len += resp_iec_m_pg(ap + len, pcontrol, target);
  2059. offset += len;
  2060. } else {
  2061. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2062. return check_condition_result;
  2063. }
  2064. break;
  2065. default:
  2066. bad_pcode = true;
  2067. break;
  2068. }
  2069. if (bad_pcode) {
  2070. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2071. return check_condition_result;
  2072. }
  2073. if (msense_6)
  2074. arr[0] = offset - 1;
  2075. else
  2076. put_unaligned_be16((offset - 2), arr + 0);
  2077. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  2078. }
  2079. #define SDEBUG_MAX_MSELECT_SZ 512
  2080. static int resp_mode_select(struct scsi_cmnd *scp,
  2081. struct sdebug_dev_info *devip)
  2082. {
  2083. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  2084. int param_len, res, mpage;
  2085. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  2086. unsigned char *cmd = scp->cmnd;
  2087. int mselect6 = (MODE_SELECT == cmd[0]);
  2088. memset(arr, 0, sizeof(arr));
  2089. pf = cmd[1] & 0x10;
  2090. sp = cmd[1] & 0x1;
  2091. param_len = mselect6 ? cmd[4] : get_unaligned_be16(cmd + 7);
  2092. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  2093. mk_sense_invalid_fld(scp, SDEB_IN_CDB, mselect6 ? 4 : 7, -1);
  2094. return check_condition_result;
  2095. }
  2096. res = fetch_to_dev_buffer(scp, arr, param_len);
  2097. if (-1 == res)
  2098. return DID_ERROR << 16;
  2099. else if (sdebug_verbose && (res < param_len))
  2100. sdev_printk(KERN_INFO, scp->device,
  2101. "%s: cdb indicated=%d, IO sent=%d bytes\n",
  2102. __func__, param_len, res);
  2103. md_len = mselect6 ? (arr[0] + 1) : (get_unaligned_be16(arr + 0) + 2);
  2104. bd_len = mselect6 ? arr[3] : get_unaligned_be16(arr + 6);
  2105. if (md_len > 2) {
  2106. mk_sense_invalid_fld(scp, SDEB_IN_DATA, 0, -1);
  2107. return check_condition_result;
  2108. }
  2109. off = bd_len + (mselect6 ? 4 : 8);
  2110. mpage = arr[off] & 0x3f;
  2111. ps = !!(arr[off] & 0x80);
  2112. if (ps) {
  2113. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 7);
  2114. return check_condition_result;
  2115. }
  2116. spf = !!(arr[off] & 0x40);
  2117. pg_len = spf ? (get_unaligned_be16(arr + off + 2) + 4) :
  2118. (arr[off + 1] + 2);
  2119. if ((pg_len + off) > param_len) {
  2120. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2121. PARAMETER_LIST_LENGTH_ERR, 0);
  2122. return check_condition_result;
  2123. }
  2124. switch (mpage) {
  2125. case 0x8: /* Caching Mode page */
  2126. if (caching_pg[1] == arr[off + 1]) {
  2127. memcpy(caching_pg + 2, arr + off + 2,
  2128. sizeof(caching_pg) - 2);
  2129. goto set_mode_changed_ua;
  2130. }
  2131. break;
  2132. case 0xa: /* Control Mode page */
  2133. if (ctrl_m_pg[1] == arr[off + 1]) {
  2134. memcpy(ctrl_m_pg + 2, arr + off + 2,
  2135. sizeof(ctrl_m_pg) - 2);
  2136. sdebug_dsense = !!(ctrl_m_pg[2] & 0x4);
  2137. goto set_mode_changed_ua;
  2138. }
  2139. break;
  2140. case 0x1c: /* Informational Exceptions Mode page */
  2141. if (iec_m_pg[1] == arr[off + 1]) {
  2142. memcpy(iec_m_pg + 2, arr + off + 2,
  2143. sizeof(iec_m_pg) - 2);
  2144. goto set_mode_changed_ua;
  2145. }
  2146. break;
  2147. default:
  2148. break;
  2149. }
  2150. mk_sense_invalid_fld(scp, SDEB_IN_DATA, off, 5);
  2151. return check_condition_result;
  2152. set_mode_changed_ua:
  2153. set_bit(SDEBUG_UA_MODE_CHANGED, devip->uas_bm);
  2154. return 0;
  2155. }
  2156. static int resp_temp_l_pg(unsigned char *arr)
  2157. {
  2158. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  2159. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  2160. };
  2161. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  2162. return sizeof(temp_l_pg);
  2163. }
  2164. static int resp_ie_l_pg(unsigned char *arr)
  2165. {
  2166. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  2167. };
  2168. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  2169. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  2170. arr[4] = THRESHOLD_EXCEEDED;
  2171. arr[5] = 0xff;
  2172. }
  2173. return sizeof(ie_l_pg);
  2174. }
  2175. #define SDEBUG_MAX_LSENSE_SZ 512
  2176. static int resp_log_sense(struct scsi_cmnd *scp,
  2177. struct sdebug_dev_info *devip)
  2178. {
  2179. int ppc, sp, pcode, subpcode, alloc_len, len, n;
  2180. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  2181. unsigned char *cmd = scp->cmnd;
  2182. memset(arr, 0, sizeof(arr));
  2183. ppc = cmd[1] & 0x2;
  2184. sp = cmd[1] & 0x1;
  2185. if (ppc || sp) {
  2186. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, ppc ? 1 : 0);
  2187. return check_condition_result;
  2188. }
  2189. pcode = cmd[2] & 0x3f;
  2190. subpcode = cmd[3] & 0xff;
  2191. alloc_len = get_unaligned_be16(cmd + 7);
  2192. arr[0] = pcode;
  2193. if (0 == subpcode) {
  2194. switch (pcode) {
  2195. case 0x0: /* Supported log pages log page */
  2196. n = 4;
  2197. arr[n++] = 0x0; /* this page */
  2198. arr[n++] = 0xd; /* Temperature */
  2199. arr[n++] = 0x2f; /* Informational exceptions */
  2200. arr[3] = n - 4;
  2201. break;
  2202. case 0xd: /* Temperature log page */
  2203. arr[3] = resp_temp_l_pg(arr + 4);
  2204. break;
  2205. case 0x2f: /* Informational exceptions log page */
  2206. arr[3] = resp_ie_l_pg(arr + 4);
  2207. break;
  2208. default:
  2209. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2210. return check_condition_result;
  2211. }
  2212. } else if (0xff == subpcode) {
  2213. arr[0] |= 0x40;
  2214. arr[1] = subpcode;
  2215. switch (pcode) {
  2216. case 0x0: /* Supported log pages and subpages log page */
  2217. n = 4;
  2218. arr[n++] = 0x0;
  2219. arr[n++] = 0x0; /* 0,0 page */
  2220. arr[n++] = 0x0;
  2221. arr[n++] = 0xff; /* this page */
  2222. arr[n++] = 0xd;
  2223. arr[n++] = 0x0; /* Temperature */
  2224. arr[n++] = 0x2f;
  2225. arr[n++] = 0x0; /* Informational exceptions */
  2226. arr[3] = n - 4;
  2227. break;
  2228. case 0xd: /* Temperature subpages */
  2229. n = 4;
  2230. arr[n++] = 0xd;
  2231. arr[n++] = 0x0; /* Temperature */
  2232. arr[3] = n - 4;
  2233. break;
  2234. case 0x2f: /* Informational exceptions subpages */
  2235. n = 4;
  2236. arr[n++] = 0x2f;
  2237. arr[n++] = 0x0; /* Informational exceptions */
  2238. arr[3] = n - 4;
  2239. break;
  2240. default:
  2241. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, 5);
  2242. return check_condition_result;
  2243. }
  2244. } else {
  2245. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 3, -1);
  2246. return check_condition_result;
  2247. }
  2248. len = min(get_unaligned_be16(arr + 2) + 4, alloc_len);
  2249. return fill_from_dev_buffer(scp, arr,
  2250. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  2251. }
  2252. static int check_device_access_params(struct scsi_cmnd *scp,
  2253. unsigned long long lba, unsigned int num)
  2254. {
  2255. if (lba + num > sdebug_capacity) {
  2256. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2257. return check_condition_result;
  2258. }
  2259. /* transfer length excessive (tie in to block limits VPD page) */
  2260. if (num > sdebug_store_sectors) {
  2261. /* needs work to find which cdb byte 'num' comes from */
  2262. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2263. return check_condition_result;
  2264. }
  2265. return 0;
  2266. }
  2267. /* Returns number of bytes copied or -1 if error. */
  2268. static int do_device_access(struct scsi_cmnd *scmd, u32 sg_skip, u64 lba,
  2269. u32 num, bool do_write)
  2270. {
  2271. int ret;
  2272. u64 block, rest = 0;
  2273. struct scsi_data_buffer *sdb;
  2274. enum dma_data_direction dir;
  2275. if (do_write) {
  2276. sdb = scsi_out(scmd);
  2277. dir = DMA_TO_DEVICE;
  2278. write_since_sync = true;
  2279. } else {
  2280. sdb = scsi_in(scmd);
  2281. dir = DMA_FROM_DEVICE;
  2282. }
  2283. if (!sdb->length)
  2284. return 0;
  2285. if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
  2286. return -1;
  2287. block = do_div(lba, sdebug_store_sectors);
  2288. if (block + num > sdebug_store_sectors)
  2289. rest = block + num - sdebug_store_sectors;
  2290. ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2291. fake_storep + (block * sdebug_sector_size),
  2292. (num - rest) * sdebug_sector_size, sg_skip, do_write);
  2293. if (ret != (num - rest) * sdebug_sector_size)
  2294. return ret;
  2295. if (rest) {
  2296. ret += sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
  2297. fake_storep, rest * sdebug_sector_size,
  2298. sg_skip + ((num - rest) * sdebug_sector_size),
  2299. do_write);
  2300. }
  2301. return ret;
  2302. }
  2303. /* If lba2fake_store(lba,num) compares equal to arr(num), then copy top half of
  2304. * arr into lba2fake_store(lba,num) and return true. If comparison fails then
  2305. * return false. */
  2306. static bool comp_write_worker(u64 lba, u32 num, const u8 *arr)
  2307. {
  2308. bool res;
  2309. u64 block, rest = 0;
  2310. u32 store_blks = sdebug_store_sectors;
  2311. u32 lb_size = sdebug_sector_size;
  2312. block = do_div(lba, store_blks);
  2313. if (block + num > store_blks)
  2314. rest = block + num - store_blks;
  2315. res = !memcmp(fake_storep + (block * lb_size), arr,
  2316. (num - rest) * lb_size);
  2317. if (!res)
  2318. return res;
  2319. if (rest)
  2320. res = memcmp(fake_storep, arr + ((num - rest) * lb_size),
  2321. rest * lb_size);
  2322. if (!res)
  2323. return res;
  2324. arr += num * lb_size;
  2325. memcpy(fake_storep + (block * lb_size), arr, (num - rest) * lb_size);
  2326. if (rest)
  2327. memcpy(fake_storep, arr + ((num - rest) * lb_size),
  2328. rest * lb_size);
  2329. return res;
  2330. }
  2331. static __be16 dif_compute_csum(const void *buf, int len)
  2332. {
  2333. __be16 csum;
  2334. if (sdebug_guard)
  2335. csum = (__force __be16)ip_compute_csum(buf, len);
  2336. else
  2337. csum = cpu_to_be16(crc_t10dif(buf, len));
  2338. return csum;
  2339. }
  2340. static int dif_verify(struct t10_pi_tuple *sdt, const void *data,
  2341. sector_t sector, u32 ei_lba)
  2342. {
  2343. __be16 csum = dif_compute_csum(data, sdebug_sector_size);
  2344. if (sdt->guard_tag != csum) {
  2345. pr_err("GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
  2346. (unsigned long)sector,
  2347. be16_to_cpu(sdt->guard_tag),
  2348. be16_to_cpu(csum));
  2349. return 0x01;
  2350. }
  2351. if (sdebug_dif == T10_PI_TYPE1_PROTECTION &&
  2352. be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  2353. pr_err("REF check failed on sector %lu\n",
  2354. (unsigned long)sector);
  2355. return 0x03;
  2356. }
  2357. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2358. be32_to_cpu(sdt->ref_tag) != ei_lba) {
  2359. pr_err("REF check failed on sector %lu\n",
  2360. (unsigned long)sector);
  2361. return 0x03;
  2362. }
  2363. return 0;
  2364. }
  2365. static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
  2366. unsigned int sectors, bool read)
  2367. {
  2368. size_t resid;
  2369. void *paddr;
  2370. const void *dif_store_end = dif_storep + sdebug_store_sectors;
  2371. struct sg_mapping_iter miter;
  2372. /* Bytes of protection data to copy into sgl */
  2373. resid = sectors * sizeof(*dif_storep);
  2374. sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
  2375. scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
  2376. (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
  2377. while (sg_miter_next(&miter) && resid > 0) {
  2378. size_t len = min(miter.length, resid);
  2379. void *start = dif_store(sector);
  2380. size_t rest = 0;
  2381. if (dif_store_end < start + len)
  2382. rest = start + len - dif_store_end;
  2383. paddr = miter.addr;
  2384. if (read)
  2385. memcpy(paddr, start, len - rest);
  2386. else
  2387. memcpy(start, paddr, len - rest);
  2388. if (rest) {
  2389. if (read)
  2390. memcpy(paddr + len - rest, dif_storep, rest);
  2391. else
  2392. memcpy(dif_storep, paddr + len - rest, rest);
  2393. }
  2394. sector += len / sizeof(*dif_storep);
  2395. resid -= len;
  2396. }
  2397. sg_miter_stop(&miter);
  2398. }
  2399. static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
  2400. unsigned int sectors, u32 ei_lba)
  2401. {
  2402. unsigned int i;
  2403. struct t10_pi_tuple *sdt;
  2404. sector_t sector;
  2405. for (i = 0; i < sectors; i++, ei_lba++) {
  2406. int ret;
  2407. sector = start_sec + i;
  2408. sdt = dif_store(sector);
  2409. if (sdt->app_tag == cpu_to_be16(0xffff))
  2410. continue;
  2411. ret = dif_verify(sdt, lba2fake_store(sector), sector, ei_lba);
  2412. if (ret) {
  2413. dif_errors++;
  2414. return ret;
  2415. }
  2416. }
  2417. dif_copy_prot(SCpnt, start_sec, sectors, true);
  2418. dix_reads++;
  2419. return 0;
  2420. }
  2421. static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  2422. {
  2423. u8 *cmd = scp->cmnd;
  2424. struct sdebug_queued_cmd *sqcp;
  2425. u64 lba;
  2426. u32 num;
  2427. u32 ei_lba;
  2428. unsigned long iflags;
  2429. int ret;
  2430. bool check_prot;
  2431. switch (cmd[0]) {
  2432. case READ_16:
  2433. ei_lba = 0;
  2434. lba = get_unaligned_be64(cmd + 2);
  2435. num = get_unaligned_be32(cmd + 10);
  2436. check_prot = true;
  2437. break;
  2438. case READ_10:
  2439. ei_lba = 0;
  2440. lba = get_unaligned_be32(cmd + 2);
  2441. num = get_unaligned_be16(cmd + 7);
  2442. check_prot = true;
  2443. break;
  2444. case READ_6:
  2445. ei_lba = 0;
  2446. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  2447. (u32)(cmd[1] & 0x1f) << 16;
  2448. num = (0 == cmd[4]) ? 256 : cmd[4];
  2449. check_prot = true;
  2450. break;
  2451. case READ_12:
  2452. ei_lba = 0;
  2453. lba = get_unaligned_be32(cmd + 2);
  2454. num = get_unaligned_be32(cmd + 6);
  2455. check_prot = true;
  2456. break;
  2457. case XDWRITEREAD_10:
  2458. ei_lba = 0;
  2459. lba = get_unaligned_be32(cmd + 2);
  2460. num = get_unaligned_be16(cmd + 7);
  2461. check_prot = false;
  2462. break;
  2463. default: /* assume READ(32) */
  2464. lba = get_unaligned_be64(cmd + 12);
  2465. ei_lba = get_unaligned_be32(cmd + 20);
  2466. num = get_unaligned_be32(cmd + 28);
  2467. check_prot = false;
  2468. break;
  2469. }
  2470. if (unlikely(have_dif_prot && check_prot)) {
  2471. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2472. (cmd[1] & 0xe0)) {
  2473. mk_sense_invalid_opcode(scp);
  2474. return check_condition_result;
  2475. }
  2476. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2477. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2478. (cmd[1] & 0xe0) == 0)
  2479. sdev_printk(KERN_ERR, scp->device, "Unprotected RD "
  2480. "to DIF device\n");
  2481. }
  2482. if (unlikely(sdebug_any_injecting_opt)) {
  2483. sqcp = (struct sdebug_queued_cmd *)scp->host_scribble;
  2484. if (sqcp) {
  2485. if (sqcp->inj_short)
  2486. num /= 2;
  2487. }
  2488. } else
  2489. sqcp = NULL;
  2490. /* inline check_device_access_params() */
  2491. if (unlikely(lba + num > sdebug_capacity)) {
  2492. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2493. return check_condition_result;
  2494. }
  2495. /* transfer length excessive (tie in to block limits VPD page) */
  2496. if (unlikely(num > sdebug_store_sectors)) {
  2497. /* needs work to find which cdb byte 'num' comes from */
  2498. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2499. return check_condition_result;
  2500. }
  2501. if (unlikely((SDEBUG_OPT_MEDIUM_ERR & sdebug_opts) &&
  2502. (lba <= (sdebug_medium_error_start + sdebug_medium_error_count - 1)) &&
  2503. ((lba + num) > sdebug_medium_error_start))) {
  2504. /* claim unrecoverable read error */
  2505. mk_sense_buffer(scp, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
  2506. /* set info field and valid bit for fixed descriptor */
  2507. if (0x70 == (scp->sense_buffer[0] & 0x7f)) {
  2508. scp->sense_buffer[0] |= 0x80; /* Valid bit */
  2509. ret = (lba < OPT_MEDIUM_ERR_ADDR)
  2510. ? OPT_MEDIUM_ERR_ADDR : (int)lba;
  2511. put_unaligned_be32(ret, scp->sense_buffer + 3);
  2512. }
  2513. scsi_set_resid(scp, scsi_bufflen(scp));
  2514. return check_condition_result;
  2515. }
  2516. read_lock_irqsave(&atomic_rw, iflags);
  2517. /* DIX + T10 DIF */
  2518. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2519. int prot_ret = prot_verify_read(scp, lba, num, ei_lba);
  2520. if (prot_ret) {
  2521. read_unlock_irqrestore(&atomic_rw, iflags);
  2522. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
  2523. return illegal_condition_result;
  2524. }
  2525. }
  2526. ret = do_device_access(scp, 0, lba, num, false);
  2527. read_unlock_irqrestore(&atomic_rw, iflags);
  2528. if (unlikely(ret == -1))
  2529. return DID_ERROR << 16;
  2530. scsi_in(scp)->resid = scsi_bufflen(scp) - ret;
  2531. if (unlikely(sqcp)) {
  2532. if (sqcp->inj_recovered) {
  2533. mk_sense_buffer(scp, RECOVERED_ERROR,
  2534. THRESHOLD_EXCEEDED, 0);
  2535. return check_condition_result;
  2536. } else if (sqcp->inj_transport) {
  2537. mk_sense_buffer(scp, ABORTED_COMMAND,
  2538. TRANSPORT_PROBLEM, ACK_NAK_TO);
  2539. return check_condition_result;
  2540. } else if (sqcp->inj_dif) {
  2541. /* Logical block guard check failed */
  2542. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  2543. return illegal_condition_result;
  2544. } else if (sqcp->inj_dix) {
  2545. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  2546. return illegal_condition_result;
  2547. }
  2548. }
  2549. return 0;
  2550. }
  2551. static void dump_sector(unsigned char *buf, int len)
  2552. {
  2553. int i, j, n;
  2554. pr_err(">>> Sector Dump <<<\n");
  2555. for (i = 0 ; i < len ; i += 16) {
  2556. char b[128];
  2557. for (j = 0, n = 0; j < 16; j++) {
  2558. unsigned char c = buf[i+j];
  2559. if (c >= 0x20 && c < 0x7e)
  2560. n += scnprintf(b + n, sizeof(b) - n,
  2561. " %c ", buf[i+j]);
  2562. else
  2563. n += scnprintf(b + n, sizeof(b) - n,
  2564. "%02x ", buf[i+j]);
  2565. }
  2566. pr_err("%04d: %s\n", i, b);
  2567. }
  2568. }
  2569. static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
  2570. unsigned int sectors, u32 ei_lba)
  2571. {
  2572. int ret;
  2573. struct t10_pi_tuple *sdt;
  2574. void *daddr;
  2575. sector_t sector = start_sec;
  2576. int ppage_offset;
  2577. int dpage_offset;
  2578. struct sg_mapping_iter diter;
  2579. struct sg_mapping_iter piter;
  2580. BUG_ON(scsi_sg_count(SCpnt) == 0);
  2581. BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
  2582. sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
  2583. scsi_prot_sg_count(SCpnt),
  2584. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  2585. sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  2586. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  2587. /* For each protection page */
  2588. while (sg_miter_next(&piter)) {
  2589. dpage_offset = 0;
  2590. if (WARN_ON(!sg_miter_next(&diter))) {
  2591. ret = 0x01;
  2592. goto out;
  2593. }
  2594. for (ppage_offset = 0; ppage_offset < piter.length;
  2595. ppage_offset += sizeof(struct t10_pi_tuple)) {
  2596. /* If we're at the end of the current
  2597. * data page advance to the next one
  2598. */
  2599. if (dpage_offset >= diter.length) {
  2600. if (WARN_ON(!sg_miter_next(&diter))) {
  2601. ret = 0x01;
  2602. goto out;
  2603. }
  2604. dpage_offset = 0;
  2605. }
  2606. sdt = piter.addr + ppage_offset;
  2607. daddr = diter.addr + dpage_offset;
  2608. ret = dif_verify(sdt, daddr, sector, ei_lba);
  2609. if (ret) {
  2610. dump_sector(daddr, sdebug_sector_size);
  2611. goto out;
  2612. }
  2613. sector++;
  2614. ei_lba++;
  2615. dpage_offset += sdebug_sector_size;
  2616. }
  2617. diter.consumed = dpage_offset;
  2618. sg_miter_stop(&diter);
  2619. }
  2620. sg_miter_stop(&piter);
  2621. dif_copy_prot(SCpnt, start_sec, sectors, false);
  2622. dix_writes++;
  2623. return 0;
  2624. out:
  2625. dif_errors++;
  2626. sg_miter_stop(&diter);
  2627. sg_miter_stop(&piter);
  2628. return ret;
  2629. }
  2630. static unsigned long lba_to_map_index(sector_t lba)
  2631. {
  2632. if (sdebug_unmap_alignment)
  2633. lba += sdebug_unmap_granularity - sdebug_unmap_alignment;
  2634. sector_div(lba, sdebug_unmap_granularity);
  2635. return lba;
  2636. }
  2637. static sector_t map_index_to_lba(unsigned long index)
  2638. {
  2639. sector_t lba = index * sdebug_unmap_granularity;
  2640. if (sdebug_unmap_alignment)
  2641. lba -= sdebug_unmap_granularity - sdebug_unmap_alignment;
  2642. return lba;
  2643. }
  2644. static unsigned int map_state(sector_t lba, unsigned int *num)
  2645. {
  2646. sector_t end;
  2647. unsigned int mapped;
  2648. unsigned long index;
  2649. unsigned long next;
  2650. index = lba_to_map_index(lba);
  2651. mapped = test_bit(index, map_storep);
  2652. if (mapped)
  2653. next = find_next_zero_bit(map_storep, map_size, index);
  2654. else
  2655. next = find_next_bit(map_storep, map_size, index);
  2656. end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
  2657. *num = end - lba;
  2658. return mapped;
  2659. }
  2660. static void map_region(sector_t lba, unsigned int len)
  2661. {
  2662. sector_t end = lba + len;
  2663. while (lba < end) {
  2664. unsigned long index = lba_to_map_index(lba);
  2665. if (index < map_size)
  2666. set_bit(index, map_storep);
  2667. lba = map_index_to_lba(index + 1);
  2668. }
  2669. }
  2670. static void unmap_region(sector_t lba, unsigned int len)
  2671. {
  2672. sector_t end = lba + len;
  2673. while (lba < end) {
  2674. unsigned long index = lba_to_map_index(lba);
  2675. if (lba == map_index_to_lba(index) &&
  2676. lba + sdebug_unmap_granularity <= end &&
  2677. index < map_size) {
  2678. clear_bit(index, map_storep);
  2679. if (sdebug_lbprz) { /* for LBPRZ=2 return 0xff_s */
  2680. memset(fake_storep +
  2681. lba * sdebug_sector_size,
  2682. (sdebug_lbprz & 1) ? 0 : 0xff,
  2683. sdebug_sector_size *
  2684. sdebug_unmap_granularity);
  2685. }
  2686. if (dif_storep) {
  2687. memset(dif_storep + lba, 0xff,
  2688. sizeof(*dif_storep) *
  2689. sdebug_unmap_granularity);
  2690. }
  2691. }
  2692. lba = map_index_to_lba(index + 1);
  2693. }
  2694. }
  2695. static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  2696. {
  2697. u8 *cmd = scp->cmnd;
  2698. u64 lba;
  2699. u32 num;
  2700. u32 ei_lba;
  2701. unsigned long iflags;
  2702. int ret;
  2703. bool check_prot;
  2704. switch (cmd[0]) {
  2705. case WRITE_16:
  2706. ei_lba = 0;
  2707. lba = get_unaligned_be64(cmd + 2);
  2708. num = get_unaligned_be32(cmd + 10);
  2709. check_prot = true;
  2710. break;
  2711. case WRITE_10:
  2712. ei_lba = 0;
  2713. lba = get_unaligned_be32(cmd + 2);
  2714. num = get_unaligned_be16(cmd + 7);
  2715. check_prot = true;
  2716. break;
  2717. case WRITE_6:
  2718. ei_lba = 0;
  2719. lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  2720. (u32)(cmd[1] & 0x1f) << 16;
  2721. num = (0 == cmd[4]) ? 256 : cmd[4];
  2722. check_prot = true;
  2723. break;
  2724. case WRITE_12:
  2725. ei_lba = 0;
  2726. lba = get_unaligned_be32(cmd + 2);
  2727. num = get_unaligned_be32(cmd + 6);
  2728. check_prot = true;
  2729. break;
  2730. case 0x53: /* XDWRITEREAD(10) */
  2731. ei_lba = 0;
  2732. lba = get_unaligned_be32(cmd + 2);
  2733. num = get_unaligned_be16(cmd + 7);
  2734. check_prot = false;
  2735. break;
  2736. default: /* assume WRITE(32) */
  2737. lba = get_unaligned_be64(cmd + 12);
  2738. ei_lba = get_unaligned_be32(cmd + 20);
  2739. num = get_unaligned_be32(cmd + 28);
  2740. check_prot = false;
  2741. break;
  2742. }
  2743. if (unlikely(have_dif_prot && check_prot)) {
  2744. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2745. (cmd[1] & 0xe0)) {
  2746. mk_sense_invalid_opcode(scp);
  2747. return check_condition_result;
  2748. }
  2749. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2750. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2751. (cmd[1] & 0xe0) == 0)
  2752. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  2753. "to DIF device\n");
  2754. }
  2755. /* inline check_device_access_params() */
  2756. if (unlikely(lba + num > sdebug_capacity)) {
  2757. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  2758. return check_condition_result;
  2759. }
  2760. /* transfer length excessive (tie in to block limits VPD page) */
  2761. if (unlikely(num > sdebug_store_sectors)) {
  2762. /* needs work to find which cdb byte 'num' comes from */
  2763. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2764. return check_condition_result;
  2765. }
  2766. write_lock_irqsave(&atomic_rw, iflags);
  2767. /* DIX + T10 DIF */
  2768. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2769. int prot_ret = prot_verify_write(scp, lba, num, ei_lba);
  2770. if (prot_ret) {
  2771. write_unlock_irqrestore(&atomic_rw, iflags);
  2772. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
  2773. return illegal_condition_result;
  2774. }
  2775. }
  2776. ret = do_device_access(scp, 0, lba, num, true);
  2777. if (unlikely(scsi_debug_lbp()))
  2778. map_region(lba, num);
  2779. write_unlock_irqrestore(&atomic_rw, iflags);
  2780. if (unlikely(-1 == ret))
  2781. return DID_ERROR << 16;
  2782. else if (unlikely(sdebug_verbose &&
  2783. (ret < (num * sdebug_sector_size))))
  2784. sdev_printk(KERN_INFO, scp->device,
  2785. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  2786. my_name, num * sdebug_sector_size, ret);
  2787. if (unlikely(sdebug_any_injecting_opt)) {
  2788. struct sdebug_queued_cmd *sqcp =
  2789. (struct sdebug_queued_cmd *)scp->host_scribble;
  2790. if (sqcp) {
  2791. if (sqcp->inj_recovered) {
  2792. mk_sense_buffer(scp, RECOVERED_ERROR,
  2793. THRESHOLD_EXCEEDED, 0);
  2794. return check_condition_result;
  2795. } else if (sqcp->inj_dif) {
  2796. /* Logical block guard check failed */
  2797. mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, 1);
  2798. return illegal_condition_result;
  2799. } else if (sqcp->inj_dix) {
  2800. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, 1);
  2801. return illegal_condition_result;
  2802. }
  2803. }
  2804. }
  2805. return 0;
  2806. }
  2807. /*
  2808. * T10 has only specified WRITE SCATTERED(16) and WRITE SCATTERED(32).
  2809. * No READ GATHERED yet (requires bidi or long cdb holding gather list).
  2810. */
  2811. static int resp_write_scat(struct scsi_cmnd *scp,
  2812. struct sdebug_dev_info *devip)
  2813. {
  2814. u8 *cmd = scp->cmnd;
  2815. u8 *lrdp = NULL;
  2816. u8 *up;
  2817. u8 wrprotect;
  2818. u16 lbdof, num_lrd, k;
  2819. u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
  2820. u32 lb_size = sdebug_sector_size;
  2821. u32 ei_lba;
  2822. u64 lba;
  2823. unsigned long iflags;
  2824. int ret, res;
  2825. bool is_16;
  2826. static const u32 lrd_size = 32; /* + parameter list header size */
  2827. if (cmd[0] == VARIABLE_LENGTH_CMD) {
  2828. is_16 = false;
  2829. wrprotect = (cmd[10] >> 5) & 0x7;
  2830. lbdof = get_unaligned_be16(cmd + 12);
  2831. num_lrd = get_unaligned_be16(cmd + 16);
  2832. bt_len = get_unaligned_be32(cmd + 28);
  2833. } else { /* that leaves WRITE SCATTERED(16) */
  2834. is_16 = true;
  2835. wrprotect = (cmd[2] >> 5) & 0x7;
  2836. lbdof = get_unaligned_be16(cmd + 4);
  2837. num_lrd = get_unaligned_be16(cmd + 8);
  2838. bt_len = get_unaligned_be32(cmd + 10);
  2839. if (unlikely(have_dif_prot)) {
  2840. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  2841. wrprotect) {
  2842. mk_sense_invalid_opcode(scp);
  2843. return illegal_condition_result;
  2844. }
  2845. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  2846. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  2847. wrprotect == 0)
  2848. sdev_printk(KERN_ERR, scp->device,
  2849. "Unprotected WR to DIF device\n");
  2850. }
  2851. }
  2852. if ((num_lrd == 0) || (bt_len == 0))
  2853. return 0; /* T10 says these do-nothings are not errors */
  2854. if (lbdof == 0) {
  2855. if (sdebug_verbose)
  2856. sdev_printk(KERN_INFO, scp->device,
  2857. "%s: %s: LB Data Offset field bad\n",
  2858. my_name, __func__);
  2859. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2860. return illegal_condition_result;
  2861. }
  2862. lbdof_blen = lbdof * lb_size;
  2863. if ((lrd_size + (num_lrd * lrd_size)) > lbdof_blen) {
  2864. if (sdebug_verbose)
  2865. sdev_printk(KERN_INFO, scp->device,
  2866. "%s: %s: LBA range descriptors don't fit\n",
  2867. my_name, __func__);
  2868. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  2869. return illegal_condition_result;
  2870. }
  2871. lrdp = kzalloc(lbdof_blen, GFP_ATOMIC);
  2872. if (lrdp == NULL)
  2873. return SCSI_MLQUEUE_HOST_BUSY;
  2874. if (sdebug_verbose)
  2875. sdev_printk(KERN_INFO, scp->device,
  2876. "%s: %s: Fetch header+scatter_list, lbdof_blen=%u\n",
  2877. my_name, __func__, lbdof_blen);
  2878. res = fetch_to_dev_buffer(scp, lrdp, lbdof_blen);
  2879. if (res == -1) {
  2880. ret = DID_ERROR << 16;
  2881. goto err_out;
  2882. }
  2883. write_lock_irqsave(&atomic_rw, iflags);
  2884. sg_off = lbdof_blen;
  2885. /* Spec says Buffer xfer Length field in number of LBs in dout */
  2886. cum_lb = 0;
  2887. for (k = 0, up = lrdp + lrd_size; k < num_lrd; ++k, up += lrd_size) {
  2888. lba = get_unaligned_be64(up + 0);
  2889. num = get_unaligned_be32(up + 8);
  2890. if (sdebug_verbose)
  2891. sdev_printk(KERN_INFO, scp->device,
  2892. "%s: %s: k=%d LBA=0x%llx num=%u sg_off=%u\n",
  2893. my_name, __func__, k, lba, num, sg_off);
  2894. if (num == 0)
  2895. continue;
  2896. ret = check_device_access_params(scp, lba, num);
  2897. if (ret)
  2898. goto err_out_unlock;
  2899. num_by = num * lb_size;
  2900. ei_lba = is_16 ? 0 : get_unaligned_be32(up + 12);
  2901. if ((cum_lb + num) > bt_len) {
  2902. if (sdebug_verbose)
  2903. sdev_printk(KERN_INFO, scp->device,
  2904. "%s: %s: sum of blocks > data provided\n",
  2905. my_name, __func__);
  2906. mk_sense_buffer(scp, ILLEGAL_REQUEST, WRITE_ERROR_ASC,
  2907. 0);
  2908. ret = illegal_condition_result;
  2909. goto err_out_unlock;
  2910. }
  2911. /* DIX + T10 DIF */
  2912. if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
  2913. int prot_ret = prot_verify_write(scp, lba, num,
  2914. ei_lba);
  2915. if (prot_ret) {
  2916. mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10,
  2917. prot_ret);
  2918. ret = illegal_condition_result;
  2919. goto err_out_unlock;
  2920. }
  2921. }
  2922. ret = do_device_access(scp, sg_off, lba, num, true);
  2923. if (unlikely(scsi_debug_lbp()))
  2924. map_region(lba, num);
  2925. if (unlikely(-1 == ret)) {
  2926. ret = DID_ERROR << 16;
  2927. goto err_out_unlock;
  2928. } else if (unlikely(sdebug_verbose && (ret < num_by)))
  2929. sdev_printk(KERN_INFO, scp->device,
  2930. "%s: write: cdb indicated=%u, IO sent=%d bytes\n",
  2931. my_name, num_by, ret);
  2932. if (unlikely(sdebug_any_injecting_opt)) {
  2933. struct sdebug_queued_cmd *sqcp =
  2934. (struct sdebug_queued_cmd *)scp->host_scribble;
  2935. if (sqcp) {
  2936. if (sqcp->inj_recovered) {
  2937. mk_sense_buffer(scp, RECOVERED_ERROR,
  2938. THRESHOLD_EXCEEDED, 0);
  2939. ret = illegal_condition_result;
  2940. goto err_out_unlock;
  2941. } else if (sqcp->inj_dif) {
  2942. /* Logical block guard check failed */
  2943. mk_sense_buffer(scp, ABORTED_COMMAND,
  2944. 0x10, 1);
  2945. ret = illegal_condition_result;
  2946. goto err_out_unlock;
  2947. } else if (sqcp->inj_dix) {
  2948. mk_sense_buffer(scp, ILLEGAL_REQUEST,
  2949. 0x10, 1);
  2950. ret = illegal_condition_result;
  2951. goto err_out_unlock;
  2952. }
  2953. }
  2954. }
  2955. sg_off += num_by;
  2956. cum_lb += num;
  2957. }
  2958. ret = 0;
  2959. err_out_unlock:
  2960. write_unlock_irqrestore(&atomic_rw, iflags);
  2961. err_out:
  2962. kfree(lrdp);
  2963. return ret;
  2964. }
  2965. static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
  2966. u32 ei_lba, bool unmap, bool ndob)
  2967. {
  2968. int ret;
  2969. unsigned long iflags;
  2970. unsigned long long i;
  2971. u32 lb_size = sdebug_sector_size;
  2972. u64 block, lbaa;
  2973. u8 *fs1p;
  2974. ret = check_device_access_params(scp, lba, num);
  2975. if (ret)
  2976. return ret;
  2977. write_lock_irqsave(&atomic_rw, iflags);
  2978. if (unmap && scsi_debug_lbp()) {
  2979. unmap_region(lba, num);
  2980. goto out;
  2981. }
  2982. lbaa = lba;
  2983. block = do_div(lbaa, sdebug_store_sectors);
  2984. /* if ndob then zero 1 logical block, else fetch 1 logical block */
  2985. fs1p = fake_storep + (block * lb_size);
  2986. if (ndob) {
  2987. memset(fs1p, 0, lb_size);
  2988. ret = 0;
  2989. } else
  2990. ret = fetch_to_dev_buffer(scp, fs1p, lb_size);
  2991. if (-1 == ret) {
  2992. write_unlock_irqrestore(&atomic_rw, iflags);
  2993. return DID_ERROR << 16;
  2994. } else if (sdebug_verbose && !ndob && (ret < lb_size))
  2995. sdev_printk(KERN_INFO, scp->device,
  2996. "%s: %s: lb size=%u, IO sent=%d bytes\n",
  2997. my_name, "write same", lb_size, ret);
  2998. /* Copy first sector to remaining blocks */
  2999. for (i = 1 ; i < num ; i++) {
  3000. lbaa = lba + i;
  3001. block = do_div(lbaa, sdebug_store_sectors);
  3002. memmove(fake_storep + (block * lb_size), fs1p, lb_size);
  3003. }
  3004. if (scsi_debug_lbp())
  3005. map_region(lba, num);
  3006. out:
  3007. write_unlock_irqrestore(&atomic_rw, iflags);
  3008. return 0;
  3009. }
  3010. static int resp_write_same_10(struct scsi_cmnd *scp,
  3011. struct sdebug_dev_info *devip)
  3012. {
  3013. u8 *cmd = scp->cmnd;
  3014. u32 lba;
  3015. u16 num;
  3016. u32 ei_lba = 0;
  3017. bool unmap = false;
  3018. if (cmd[1] & 0x8) {
  3019. if (sdebug_lbpws10 == 0) {
  3020. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3021. return check_condition_result;
  3022. } else
  3023. unmap = true;
  3024. }
  3025. lba = get_unaligned_be32(cmd + 2);
  3026. num = get_unaligned_be16(cmd + 7);
  3027. if (num > sdebug_write_same_length) {
  3028. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3029. return check_condition_result;
  3030. }
  3031. return resp_write_same(scp, lba, num, ei_lba, unmap, false);
  3032. }
  3033. static int resp_write_same_16(struct scsi_cmnd *scp,
  3034. struct sdebug_dev_info *devip)
  3035. {
  3036. u8 *cmd = scp->cmnd;
  3037. u64 lba;
  3038. u32 num;
  3039. u32 ei_lba = 0;
  3040. bool unmap = false;
  3041. bool ndob = false;
  3042. if (cmd[1] & 0x8) { /* UNMAP */
  3043. if (sdebug_lbpws == 0) {
  3044. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 3);
  3045. return check_condition_result;
  3046. } else
  3047. unmap = true;
  3048. }
  3049. if (cmd[1] & 0x1) /* NDOB (no data-out buffer, assumes zeroes) */
  3050. ndob = true;
  3051. lba = get_unaligned_be64(cmd + 2);
  3052. num = get_unaligned_be32(cmd + 10);
  3053. if (num > sdebug_write_same_length) {
  3054. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 10, -1);
  3055. return check_condition_result;
  3056. }
  3057. return resp_write_same(scp, lba, num, ei_lba, unmap, ndob);
  3058. }
  3059. /* Note the mode field is in the same position as the (lower) service action
  3060. * field. For the Report supported operation codes command, SPC-4 suggests
  3061. * each mode of this command should be reported separately; for future. */
  3062. static int resp_write_buffer(struct scsi_cmnd *scp,
  3063. struct sdebug_dev_info *devip)
  3064. {
  3065. u8 *cmd = scp->cmnd;
  3066. struct scsi_device *sdp = scp->device;
  3067. struct sdebug_dev_info *dp;
  3068. u8 mode;
  3069. mode = cmd[1] & 0x1f;
  3070. switch (mode) {
  3071. case 0x4: /* download microcode (MC) and activate (ACT) */
  3072. /* set UAs on this device only */
  3073. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3074. set_bit(SDEBUG_UA_MICROCODE_CHANGED, devip->uas_bm);
  3075. break;
  3076. case 0x5: /* download MC, save and ACT */
  3077. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET, devip->uas_bm);
  3078. break;
  3079. case 0x6: /* download MC with offsets and ACT */
  3080. /* set UAs on most devices (LUs) in this target */
  3081. list_for_each_entry(dp,
  3082. &devip->sdbg_host->dev_info_list,
  3083. dev_list)
  3084. if (dp->target == sdp->id) {
  3085. set_bit(SDEBUG_UA_BUS_RESET, dp->uas_bm);
  3086. if (devip != dp)
  3087. set_bit(SDEBUG_UA_MICROCODE_CHANGED,
  3088. dp->uas_bm);
  3089. }
  3090. break;
  3091. case 0x7: /* download MC with offsets, save, and ACT */
  3092. /* set UA on all devices (LUs) in this target */
  3093. list_for_each_entry(dp,
  3094. &devip->sdbg_host->dev_info_list,
  3095. dev_list)
  3096. if (dp->target == sdp->id)
  3097. set_bit(SDEBUG_UA_MICROCODE_CHANGED_WO_RESET,
  3098. dp->uas_bm);
  3099. break;
  3100. default:
  3101. /* do nothing for this command for other mode values */
  3102. break;
  3103. }
  3104. return 0;
  3105. }
  3106. static int resp_comp_write(struct scsi_cmnd *scp,
  3107. struct sdebug_dev_info *devip)
  3108. {
  3109. u8 *cmd = scp->cmnd;
  3110. u8 *arr;
  3111. u8 *fake_storep_hold;
  3112. u64 lba;
  3113. u32 dnum;
  3114. u32 lb_size = sdebug_sector_size;
  3115. u8 num;
  3116. unsigned long iflags;
  3117. int ret;
  3118. int retval = 0;
  3119. lba = get_unaligned_be64(cmd + 2);
  3120. num = cmd[13]; /* 1 to a maximum of 255 logical blocks */
  3121. if (0 == num)
  3122. return 0; /* degenerate case, not an error */
  3123. if (sdebug_dif == T10_PI_TYPE2_PROTECTION &&
  3124. (cmd[1] & 0xe0)) {
  3125. mk_sense_invalid_opcode(scp);
  3126. return check_condition_result;
  3127. }
  3128. if ((sdebug_dif == T10_PI_TYPE1_PROTECTION ||
  3129. sdebug_dif == T10_PI_TYPE3_PROTECTION) &&
  3130. (cmd[1] & 0xe0) == 0)
  3131. sdev_printk(KERN_ERR, scp->device, "Unprotected WR "
  3132. "to DIF device\n");
  3133. /* inline check_device_access_params() */
  3134. if (lba + num > sdebug_capacity) {
  3135. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3136. return check_condition_result;
  3137. }
  3138. /* transfer length excessive (tie in to block limits VPD page) */
  3139. if (num > sdebug_store_sectors) {
  3140. /* needs work to find which cdb byte 'num' comes from */
  3141. mk_sense_buffer(scp, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  3142. return check_condition_result;
  3143. }
  3144. dnum = 2 * num;
  3145. arr = kcalloc(lb_size, dnum, GFP_ATOMIC);
  3146. if (NULL == arr) {
  3147. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3148. INSUFF_RES_ASCQ);
  3149. return check_condition_result;
  3150. }
  3151. write_lock_irqsave(&atomic_rw, iflags);
  3152. /* trick do_device_access() to fetch both compare and write buffers
  3153. * from data-in into arr. Safe (atomic) since write_lock held. */
  3154. fake_storep_hold = fake_storep;
  3155. fake_storep = arr;
  3156. ret = do_device_access(scp, 0, 0, dnum, true);
  3157. fake_storep = fake_storep_hold;
  3158. if (ret == -1) {
  3159. retval = DID_ERROR << 16;
  3160. goto cleanup;
  3161. } else if (sdebug_verbose && (ret < (dnum * lb_size)))
  3162. sdev_printk(KERN_INFO, scp->device, "%s: compare_write: cdb "
  3163. "indicated=%u, IO sent=%d bytes\n", my_name,
  3164. dnum * lb_size, ret);
  3165. if (!comp_write_worker(lba, num, arr)) {
  3166. mk_sense_buffer(scp, MISCOMPARE, MISCOMPARE_VERIFY_ASC, 0);
  3167. retval = check_condition_result;
  3168. goto cleanup;
  3169. }
  3170. if (scsi_debug_lbp())
  3171. map_region(lba, num);
  3172. cleanup:
  3173. write_unlock_irqrestore(&atomic_rw, iflags);
  3174. kfree(arr);
  3175. return retval;
  3176. }
  3177. struct unmap_block_desc {
  3178. __be64 lba;
  3179. __be32 blocks;
  3180. __be32 __reserved;
  3181. };
  3182. static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
  3183. {
  3184. unsigned char *buf;
  3185. struct unmap_block_desc *desc;
  3186. unsigned int i, payload_len, descriptors;
  3187. int ret;
  3188. unsigned long iflags;
  3189. if (!scsi_debug_lbp())
  3190. return 0; /* fib and say its done */
  3191. payload_len = get_unaligned_be16(scp->cmnd + 7);
  3192. BUG_ON(scsi_bufflen(scp) != payload_len);
  3193. descriptors = (payload_len - 8) / 16;
  3194. if (descriptors > sdebug_unmap_max_desc) {
  3195. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 7, -1);
  3196. return check_condition_result;
  3197. }
  3198. buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
  3199. if (!buf) {
  3200. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3201. INSUFF_RES_ASCQ);
  3202. return check_condition_result;
  3203. }
  3204. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  3205. BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
  3206. BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
  3207. desc = (void *)&buf[8];
  3208. write_lock_irqsave(&atomic_rw, iflags);
  3209. for (i = 0 ; i < descriptors ; i++) {
  3210. unsigned long long lba = get_unaligned_be64(&desc[i].lba);
  3211. unsigned int num = get_unaligned_be32(&desc[i].blocks);
  3212. ret = check_device_access_params(scp, lba, num);
  3213. if (ret)
  3214. goto out;
  3215. unmap_region(lba, num);
  3216. }
  3217. ret = 0;
  3218. out:
  3219. write_unlock_irqrestore(&atomic_rw, iflags);
  3220. kfree(buf);
  3221. return ret;
  3222. }
  3223. #define SDEBUG_GET_LBA_STATUS_LEN 32
  3224. static int resp_get_lba_status(struct scsi_cmnd *scp,
  3225. struct sdebug_dev_info *devip)
  3226. {
  3227. u8 *cmd = scp->cmnd;
  3228. u64 lba;
  3229. u32 alloc_len, mapped, num;
  3230. u8 arr[SDEBUG_GET_LBA_STATUS_LEN];
  3231. int ret;
  3232. lba = get_unaligned_be64(cmd + 2);
  3233. alloc_len = get_unaligned_be32(cmd + 10);
  3234. if (alloc_len < 24)
  3235. return 0;
  3236. ret = check_device_access_params(scp, lba, 1);
  3237. if (ret)
  3238. return ret;
  3239. if (scsi_debug_lbp())
  3240. mapped = map_state(lba, &num);
  3241. else {
  3242. mapped = 1;
  3243. /* following just in case virtual_gb changed */
  3244. sdebug_capacity = get_sdebug_capacity();
  3245. if (sdebug_capacity - lba <= 0xffffffff)
  3246. num = sdebug_capacity - lba;
  3247. else
  3248. num = 0xffffffff;
  3249. }
  3250. memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
  3251. put_unaligned_be32(20, arr); /* Parameter Data Length */
  3252. put_unaligned_be64(lba, arr + 8); /* LBA */
  3253. put_unaligned_be32(num, arr + 16); /* Number of blocks */
  3254. arr[20] = !mapped; /* prov_stat=0: mapped; 1: dealloc */
  3255. return fill_from_dev_buffer(scp, arr, SDEBUG_GET_LBA_STATUS_LEN);
  3256. }
  3257. static int resp_sync_cache(struct scsi_cmnd *scp,
  3258. struct sdebug_dev_info *devip)
  3259. {
  3260. int res = 0;
  3261. u64 lba;
  3262. u32 num_blocks;
  3263. u8 *cmd = scp->cmnd;
  3264. if (cmd[0] == SYNCHRONIZE_CACHE) { /* 10 byte cdb */
  3265. lba = get_unaligned_be32(cmd + 2);
  3266. num_blocks = get_unaligned_be16(cmd + 7);
  3267. } else { /* SYNCHRONIZE_CACHE(16) */
  3268. lba = get_unaligned_be64(cmd + 2);
  3269. num_blocks = get_unaligned_be32(cmd + 10);
  3270. }
  3271. if (lba + num_blocks > sdebug_capacity) {
  3272. mk_sense_buffer(scp, ILLEGAL_REQUEST, LBA_OUT_OF_RANGE, 0);
  3273. return check_condition_result;
  3274. }
  3275. if (!write_since_sync || cmd[1] & 0x2)
  3276. res = SDEG_RES_IMMED_MASK;
  3277. else /* delay if write_since_sync and IMMED clear */
  3278. write_since_sync = false;
  3279. return res;
  3280. }
  3281. #define RL_BUCKET_ELEMS 8
  3282. /* Even though each pseudo target has a REPORT LUNS "well known logical unit"
  3283. * (W-LUN), the normal Linux scanning logic does not associate it with a
  3284. * device (e.g. /dev/sg7). The following magic will make that association:
  3285. * "cd /sys/class/scsi_host/host<n> ; echo '- - 49409' > scan"
  3286. * where <n> is a host number. If there are multiple targets in a host then
  3287. * the above will associate a W-LUN to each target. To only get a W-LUN
  3288. * for target 2, then use "echo '- 2 49409' > scan" .
  3289. */
  3290. static int resp_report_luns(struct scsi_cmnd *scp,
  3291. struct sdebug_dev_info *devip)
  3292. {
  3293. unsigned char *cmd = scp->cmnd;
  3294. unsigned int alloc_len;
  3295. unsigned char select_report;
  3296. u64 lun;
  3297. struct scsi_lun *lun_p;
  3298. u8 arr[RL_BUCKET_ELEMS * sizeof(struct scsi_lun)];
  3299. unsigned int lun_cnt; /* normal LUN count (max: 256) */
  3300. unsigned int wlun_cnt; /* report luns W-LUN count */
  3301. unsigned int tlun_cnt; /* total LUN count */
  3302. unsigned int rlen; /* response length (in bytes) */
  3303. int k, j, n, res;
  3304. unsigned int off_rsp = 0;
  3305. const int sz_lun = sizeof(struct scsi_lun);
  3306. clear_luns_changed_on_target(devip);
  3307. select_report = cmd[2];
  3308. alloc_len = get_unaligned_be32(cmd + 6);
  3309. if (alloc_len < 4) {
  3310. pr_err("alloc len too small %d\n", alloc_len);
  3311. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 6, -1);
  3312. return check_condition_result;
  3313. }
  3314. switch (select_report) {
  3315. case 0: /* all LUNs apart from W-LUNs */
  3316. lun_cnt = sdebug_max_luns;
  3317. wlun_cnt = 0;
  3318. break;
  3319. case 1: /* only W-LUNs */
  3320. lun_cnt = 0;
  3321. wlun_cnt = 1;
  3322. break;
  3323. case 2: /* all LUNs */
  3324. lun_cnt = sdebug_max_luns;
  3325. wlun_cnt = 1;
  3326. break;
  3327. case 0x10: /* only administrative LUs */
  3328. case 0x11: /* see SPC-5 */
  3329. case 0x12: /* only subsiduary LUs owned by referenced LU */
  3330. default:
  3331. pr_debug("select report invalid %d\n", select_report);
  3332. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 2, -1);
  3333. return check_condition_result;
  3334. }
  3335. if (sdebug_no_lun_0 && (lun_cnt > 0))
  3336. --lun_cnt;
  3337. tlun_cnt = lun_cnt + wlun_cnt;
  3338. rlen = tlun_cnt * sz_lun; /* excluding 8 byte header */
  3339. scsi_set_resid(scp, scsi_bufflen(scp));
  3340. pr_debug("select_report %d luns = %d wluns = %d no_lun0 %d\n",
  3341. select_report, lun_cnt, wlun_cnt, sdebug_no_lun_0);
  3342. /* loops rely on sizeof response header same as sizeof lun (both 8) */
  3343. lun = sdebug_no_lun_0 ? 1 : 0;
  3344. for (k = 0, j = 0, res = 0; true; ++k, j = 0) {
  3345. memset(arr, 0, sizeof(arr));
  3346. lun_p = (struct scsi_lun *)&arr[0];
  3347. if (k == 0) {
  3348. put_unaligned_be32(rlen, &arr[0]);
  3349. ++lun_p;
  3350. j = 1;
  3351. }
  3352. for ( ; j < RL_BUCKET_ELEMS; ++j, ++lun_p) {
  3353. if ((k * RL_BUCKET_ELEMS) + j > lun_cnt)
  3354. break;
  3355. int_to_scsilun(lun++, lun_p);
  3356. }
  3357. if (j < RL_BUCKET_ELEMS)
  3358. break;
  3359. n = j * sz_lun;
  3360. res = p_fill_from_dev_buffer(scp, arr, n, off_rsp);
  3361. if (res)
  3362. return res;
  3363. off_rsp += n;
  3364. }
  3365. if (wlun_cnt) {
  3366. int_to_scsilun(SCSI_W_LUN_REPORT_LUNS, lun_p);
  3367. ++j;
  3368. }
  3369. if (j > 0)
  3370. res = p_fill_from_dev_buffer(scp, arr, j * sz_lun, off_rsp);
  3371. return res;
  3372. }
  3373. static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
  3374. unsigned int num, struct sdebug_dev_info *devip)
  3375. {
  3376. int j;
  3377. unsigned char *kaddr, *buf;
  3378. unsigned int offset;
  3379. struct scsi_data_buffer *sdb = scsi_in(scp);
  3380. struct sg_mapping_iter miter;
  3381. /* better not to use temporary buffer. */
  3382. buf = kzalloc(scsi_bufflen(scp), GFP_ATOMIC);
  3383. if (!buf) {
  3384. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3385. INSUFF_RES_ASCQ);
  3386. return check_condition_result;
  3387. }
  3388. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  3389. offset = 0;
  3390. sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
  3391. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  3392. while (sg_miter_next(&miter)) {
  3393. kaddr = miter.addr;
  3394. for (j = 0; j < miter.length; j++)
  3395. *(kaddr + j) ^= *(buf + offset + j);
  3396. offset += miter.length;
  3397. }
  3398. sg_miter_stop(&miter);
  3399. kfree(buf);
  3400. return 0;
  3401. }
  3402. static int resp_xdwriteread_10(struct scsi_cmnd *scp,
  3403. struct sdebug_dev_info *devip)
  3404. {
  3405. u8 *cmd = scp->cmnd;
  3406. u64 lba;
  3407. u32 num;
  3408. int errsts;
  3409. if (!scsi_bidi_cmnd(scp)) {
  3410. mk_sense_buffer(scp, ILLEGAL_REQUEST, INSUFF_RES_ASC,
  3411. INSUFF_RES_ASCQ);
  3412. return check_condition_result;
  3413. }
  3414. errsts = resp_read_dt0(scp, devip);
  3415. if (errsts)
  3416. return errsts;
  3417. if (!(cmd[1] & 0x4)) { /* DISABLE_WRITE is not set */
  3418. errsts = resp_write_dt0(scp, devip);
  3419. if (errsts)
  3420. return errsts;
  3421. }
  3422. lba = get_unaligned_be32(cmd + 2);
  3423. num = get_unaligned_be16(cmd + 7);
  3424. return resp_xdwriteread(scp, lba, num, devip);
  3425. }
  3426. static struct sdebug_queue *get_queue(struct scsi_cmnd *cmnd)
  3427. {
  3428. u32 tag = blk_mq_unique_tag(cmnd->request);
  3429. u16 hwq = blk_mq_unique_tag_to_hwq(tag);
  3430. pr_debug("tag=%#x, hwq=%d\n", tag, hwq);
  3431. if (WARN_ON_ONCE(hwq >= submit_queues))
  3432. hwq = 0;
  3433. return sdebug_q_arr + hwq;
  3434. }
  3435. /* Queued (deferred) command completions converge here. */
  3436. static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)
  3437. {
  3438. bool aborted = sd_dp->aborted;
  3439. int qc_idx;
  3440. int retiring = 0;
  3441. unsigned long iflags;
  3442. struct sdebug_queue *sqp;
  3443. struct sdebug_queued_cmd *sqcp;
  3444. struct scsi_cmnd *scp;
  3445. struct sdebug_dev_info *devip;
  3446. sd_dp->defer_t = SDEB_DEFER_NONE;
  3447. if (unlikely(aborted))
  3448. sd_dp->aborted = false;
  3449. qc_idx = sd_dp->qc_idx;
  3450. sqp = sdebug_q_arr + sd_dp->sqa_idx;
  3451. if (sdebug_statistics) {
  3452. atomic_inc(&sdebug_completions);
  3453. if (raw_smp_processor_id() != sd_dp->issuing_cpu)
  3454. atomic_inc(&sdebug_miss_cpus);
  3455. }
  3456. if (unlikely((qc_idx < 0) || (qc_idx >= SDEBUG_CANQUEUE))) {
  3457. pr_err("wild qc_idx=%d\n", qc_idx);
  3458. return;
  3459. }
  3460. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3461. sqcp = &sqp->qc_arr[qc_idx];
  3462. scp = sqcp->a_cmnd;
  3463. if (unlikely(scp == NULL)) {
  3464. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3465. pr_err("scp is NULL, sqa_idx=%d, qc_idx=%d\n",
  3466. sd_dp->sqa_idx, qc_idx);
  3467. return;
  3468. }
  3469. devip = (struct sdebug_dev_info *)scp->device->hostdata;
  3470. if (likely(devip))
  3471. atomic_dec(&devip->num_in_q);
  3472. else
  3473. pr_err("devip=NULL\n");
  3474. if (unlikely(atomic_read(&retired_max_queue) > 0))
  3475. retiring = 1;
  3476. sqcp->a_cmnd = NULL;
  3477. if (unlikely(!test_and_clear_bit(qc_idx, sqp->in_use_bm))) {
  3478. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3479. pr_err("Unexpected completion\n");
  3480. return;
  3481. }
  3482. if (unlikely(retiring)) { /* user has reduced max_queue */
  3483. int k, retval;
  3484. retval = atomic_read(&retired_max_queue);
  3485. if (qc_idx >= retval) {
  3486. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3487. pr_err("index %d too large\n", retval);
  3488. return;
  3489. }
  3490. k = find_last_bit(sqp->in_use_bm, retval);
  3491. if ((k < sdebug_max_queue) || (k == retval))
  3492. atomic_set(&retired_max_queue, 0);
  3493. else
  3494. atomic_set(&retired_max_queue, k + 1);
  3495. }
  3496. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3497. if (unlikely(aborted)) {
  3498. if (sdebug_verbose)
  3499. pr_info("bypassing scsi_done() due to aborted cmd\n");
  3500. return;
  3501. }
  3502. scp->scsi_done(scp); /* callback to mid level */
  3503. }
  3504. /* When high resolution timer goes off this function is called. */
  3505. static enum hrtimer_restart sdebug_q_cmd_hrt_complete(struct hrtimer *timer)
  3506. {
  3507. struct sdebug_defer *sd_dp = container_of(timer, struct sdebug_defer,
  3508. hrt);
  3509. sdebug_q_cmd_complete(sd_dp);
  3510. return HRTIMER_NORESTART;
  3511. }
  3512. /* When work queue schedules work, it calls this function. */
  3513. static void sdebug_q_cmd_wq_complete(struct work_struct *work)
  3514. {
  3515. struct sdebug_defer *sd_dp = container_of(work, struct sdebug_defer,
  3516. ew.work);
  3517. sdebug_q_cmd_complete(sd_dp);
  3518. }
  3519. static bool got_shared_uuid;
  3520. static uuid_t shared_uuid;
  3521. static struct sdebug_dev_info *sdebug_device_create(
  3522. struct sdebug_host_info *sdbg_host, gfp_t flags)
  3523. {
  3524. struct sdebug_dev_info *devip;
  3525. devip = kzalloc(sizeof(*devip), flags);
  3526. if (devip) {
  3527. if (sdebug_uuid_ctl == 1)
  3528. uuid_gen(&devip->lu_name);
  3529. else if (sdebug_uuid_ctl == 2) {
  3530. if (got_shared_uuid)
  3531. devip->lu_name = shared_uuid;
  3532. else {
  3533. uuid_gen(&shared_uuid);
  3534. got_shared_uuid = true;
  3535. devip->lu_name = shared_uuid;
  3536. }
  3537. }
  3538. devip->sdbg_host = sdbg_host;
  3539. list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
  3540. }
  3541. return devip;
  3542. }
  3543. static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev)
  3544. {
  3545. struct sdebug_host_info *sdbg_host;
  3546. struct sdebug_dev_info *open_devip = NULL;
  3547. struct sdebug_dev_info *devip;
  3548. sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
  3549. if (!sdbg_host) {
  3550. pr_err("Host info NULL\n");
  3551. return NULL;
  3552. }
  3553. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  3554. if ((devip->used) && (devip->channel == sdev->channel) &&
  3555. (devip->target == sdev->id) &&
  3556. (devip->lun == sdev->lun))
  3557. return devip;
  3558. else {
  3559. if ((!devip->used) && (!open_devip))
  3560. open_devip = devip;
  3561. }
  3562. }
  3563. if (!open_devip) { /* try and make a new one */
  3564. open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
  3565. if (!open_devip) {
  3566. pr_err("out of memory at line %d\n", __LINE__);
  3567. return NULL;
  3568. }
  3569. }
  3570. open_devip->channel = sdev->channel;
  3571. open_devip->target = sdev->id;
  3572. open_devip->lun = sdev->lun;
  3573. open_devip->sdbg_host = sdbg_host;
  3574. atomic_set(&open_devip->num_in_q, 0);
  3575. set_bit(SDEBUG_UA_POR, open_devip->uas_bm);
  3576. open_devip->used = true;
  3577. return open_devip;
  3578. }
  3579. static int scsi_debug_slave_alloc(struct scsi_device *sdp)
  3580. {
  3581. if (sdebug_verbose)
  3582. pr_info("slave_alloc <%u %u %u %llu>\n",
  3583. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3584. blk_queue_flag_set(QUEUE_FLAG_BIDI, sdp->request_queue);
  3585. return 0;
  3586. }
  3587. static int scsi_debug_slave_configure(struct scsi_device *sdp)
  3588. {
  3589. struct sdebug_dev_info *devip =
  3590. (struct sdebug_dev_info *)sdp->hostdata;
  3591. if (sdebug_verbose)
  3592. pr_info("slave_configure <%u %u %u %llu>\n",
  3593. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3594. if (sdp->host->max_cmd_len != SDEBUG_MAX_CMD_LEN)
  3595. sdp->host->max_cmd_len = SDEBUG_MAX_CMD_LEN;
  3596. if (devip == NULL) {
  3597. devip = find_build_dev_info(sdp);
  3598. if (devip == NULL)
  3599. return 1; /* no resources, will be marked offline */
  3600. }
  3601. sdp->hostdata = devip;
  3602. blk_queue_max_segment_size(sdp->request_queue, -1U);
  3603. if (sdebug_no_uld)
  3604. sdp->no_uld_attach = 1;
  3605. config_cdb_len(sdp);
  3606. return 0;
  3607. }
  3608. static void scsi_debug_slave_destroy(struct scsi_device *sdp)
  3609. {
  3610. struct sdebug_dev_info *devip =
  3611. (struct sdebug_dev_info *)sdp->hostdata;
  3612. if (sdebug_verbose)
  3613. pr_info("slave_destroy <%u %u %u %llu>\n",
  3614. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  3615. if (devip) {
  3616. /* make this slot available for re-use */
  3617. devip->used = false;
  3618. sdp->hostdata = NULL;
  3619. }
  3620. }
  3621. static void stop_qc_helper(struct sdebug_defer *sd_dp,
  3622. enum sdeb_defer_type defer_t)
  3623. {
  3624. if (!sd_dp)
  3625. return;
  3626. if (defer_t == SDEB_DEFER_HRT)
  3627. hrtimer_cancel(&sd_dp->hrt);
  3628. else if (defer_t == SDEB_DEFER_WQ)
  3629. cancel_work_sync(&sd_dp->ew.work);
  3630. }
  3631. /* If @cmnd found deletes its timer or work queue and returns true; else
  3632. returns false */
  3633. static bool stop_queued_cmnd(struct scsi_cmnd *cmnd)
  3634. {
  3635. unsigned long iflags;
  3636. int j, k, qmax, r_qmax;
  3637. enum sdeb_defer_type l_defer_t;
  3638. struct sdebug_queue *sqp;
  3639. struct sdebug_queued_cmd *sqcp;
  3640. struct sdebug_dev_info *devip;
  3641. struct sdebug_defer *sd_dp;
  3642. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3643. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3644. qmax = sdebug_max_queue;
  3645. r_qmax = atomic_read(&retired_max_queue);
  3646. if (r_qmax > qmax)
  3647. qmax = r_qmax;
  3648. for (k = 0; k < qmax; ++k) {
  3649. if (test_bit(k, sqp->in_use_bm)) {
  3650. sqcp = &sqp->qc_arr[k];
  3651. if (cmnd != sqcp->a_cmnd)
  3652. continue;
  3653. /* found */
  3654. devip = (struct sdebug_dev_info *)
  3655. cmnd->device->hostdata;
  3656. if (devip)
  3657. atomic_dec(&devip->num_in_q);
  3658. sqcp->a_cmnd = NULL;
  3659. sd_dp = sqcp->sd_dp;
  3660. if (sd_dp) {
  3661. l_defer_t = sd_dp->defer_t;
  3662. sd_dp->defer_t = SDEB_DEFER_NONE;
  3663. } else
  3664. l_defer_t = SDEB_DEFER_NONE;
  3665. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3666. stop_qc_helper(sd_dp, l_defer_t);
  3667. clear_bit(k, sqp->in_use_bm);
  3668. return true;
  3669. }
  3670. }
  3671. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3672. }
  3673. return false;
  3674. }
  3675. /* Deletes (stops) timers or work queues of all queued commands */
  3676. static void stop_all_queued(void)
  3677. {
  3678. unsigned long iflags;
  3679. int j, k;
  3680. enum sdeb_defer_type l_defer_t;
  3681. struct sdebug_queue *sqp;
  3682. struct sdebug_queued_cmd *sqcp;
  3683. struct sdebug_dev_info *devip;
  3684. struct sdebug_defer *sd_dp;
  3685. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3686. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3687. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  3688. if (test_bit(k, sqp->in_use_bm)) {
  3689. sqcp = &sqp->qc_arr[k];
  3690. if (sqcp->a_cmnd == NULL)
  3691. continue;
  3692. devip = (struct sdebug_dev_info *)
  3693. sqcp->a_cmnd->device->hostdata;
  3694. if (devip)
  3695. atomic_dec(&devip->num_in_q);
  3696. sqcp->a_cmnd = NULL;
  3697. sd_dp = sqcp->sd_dp;
  3698. if (sd_dp) {
  3699. l_defer_t = sd_dp->defer_t;
  3700. sd_dp->defer_t = SDEB_DEFER_NONE;
  3701. } else
  3702. l_defer_t = SDEB_DEFER_NONE;
  3703. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3704. stop_qc_helper(sd_dp, l_defer_t);
  3705. clear_bit(k, sqp->in_use_bm);
  3706. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3707. }
  3708. }
  3709. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3710. }
  3711. }
  3712. /* Free queued command memory on heap */
  3713. static void free_all_queued(void)
  3714. {
  3715. int j, k;
  3716. struct sdebug_queue *sqp;
  3717. struct sdebug_queued_cmd *sqcp;
  3718. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  3719. for (k = 0; k < SDEBUG_CANQUEUE; ++k) {
  3720. sqcp = &sqp->qc_arr[k];
  3721. kfree(sqcp->sd_dp);
  3722. sqcp->sd_dp = NULL;
  3723. }
  3724. }
  3725. }
  3726. static int scsi_debug_abort(struct scsi_cmnd *SCpnt)
  3727. {
  3728. bool ok;
  3729. ++num_aborts;
  3730. if (SCpnt) {
  3731. ok = stop_queued_cmnd(SCpnt);
  3732. if (SCpnt->device && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  3733. sdev_printk(KERN_INFO, SCpnt->device,
  3734. "%s: command%s found\n", __func__,
  3735. ok ? "" : " not");
  3736. }
  3737. return SUCCESS;
  3738. }
  3739. static int scsi_debug_device_reset(struct scsi_cmnd *SCpnt)
  3740. {
  3741. ++num_dev_resets;
  3742. if (SCpnt && SCpnt->device) {
  3743. struct scsi_device *sdp = SCpnt->device;
  3744. struct sdebug_dev_info *devip =
  3745. (struct sdebug_dev_info *)sdp->hostdata;
  3746. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3747. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3748. if (devip)
  3749. set_bit(SDEBUG_UA_POR, devip->uas_bm);
  3750. }
  3751. return SUCCESS;
  3752. }
  3753. static int scsi_debug_target_reset(struct scsi_cmnd *SCpnt)
  3754. {
  3755. struct sdebug_host_info *sdbg_host;
  3756. struct sdebug_dev_info *devip;
  3757. struct scsi_device *sdp;
  3758. struct Scsi_Host *hp;
  3759. int k = 0;
  3760. ++num_target_resets;
  3761. if (!SCpnt)
  3762. goto lie;
  3763. sdp = SCpnt->device;
  3764. if (!sdp)
  3765. goto lie;
  3766. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3767. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3768. hp = sdp->host;
  3769. if (!hp)
  3770. goto lie;
  3771. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  3772. if (sdbg_host) {
  3773. list_for_each_entry(devip,
  3774. &sdbg_host->dev_info_list,
  3775. dev_list)
  3776. if (devip->target == sdp->id) {
  3777. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3778. ++k;
  3779. }
  3780. }
  3781. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3782. sdev_printk(KERN_INFO, sdp,
  3783. "%s: %d device(s) found in target\n", __func__, k);
  3784. lie:
  3785. return SUCCESS;
  3786. }
  3787. static int scsi_debug_bus_reset(struct scsi_cmnd *SCpnt)
  3788. {
  3789. struct sdebug_host_info *sdbg_host;
  3790. struct sdebug_dev_info *devip;
  3791. struct scsi_device *sdp;
  3792. struct Scsi_Host *hp;
  3793. int k = 0;
  3794. ++num_bus_resets;
  3795. if (!(SCpnt && SCpnt->device))
  3796. goto lie;
  3797. sdp = SCpnt->device;
  3798. if (SDEBUG_OPT_ALL_NOISE & sdebug_opts)
  3799. sdev_printk(KERN_INFO, sdp, "%s\n", __func__);
  3800. hp = sdp->host;
  3801. if (hp) {
  3802. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  3803. if (sdbg_host) {
  3804. list_for_each_entry(devip,
  3805. &sdbg_host->dev_info_list,
  3806. dev_list) {
  3807. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3808. ++k;
  3809. }
  3810. }
  3811. }
  3812. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3813. sdev_printk(KERN_INFO, sdp,
  3814. "%s: %d device(s) found in host\n", __func__, k);
  3815. lie:
  3816. return SUCCESS;
  3817. }
  3818. static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt)
  3819. {
  3820. struct sdebug_host_info *sdbg_host;
  3821. struct sdebug_dev_info *devip;
  3822. int k = 0;
  3823. ++num_host_resets;
  3824. if ((SCpnt->device) && (SDEBUG_OPT_ALL_NOISE & sdebug_opts))
  3825. sdev_printk(KERN_INFO, SCpnt->device, "%s\n", __func__);
  3826. spin_lock(&sdebug_host_list_lock);
  3827. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  3828. list_for_each_entry(devip, &sdbg_host->dev_info_list,
  3829. dev_list) {
  3830. set_bit(SDEBUG_UA_BUS_RESET, devip->uas_bm);
  3831. ++k;
  3832. }
  3833. }
  3834. spin_unlock(&sdebug_host_list_lock);
  3835. stop_all_queued();
  3836. if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)
  3837. sdev_printk(KERN_INFO, SCpnt->device,
  3838. "%s: %d device(s) found\n", __func__, k);
  3839. return SUCCESS;
  3840. }
  3841. static void __init sdebug_build_parts(unsigned char *ramp,
  3842. unsigned long store_size)
  3843. {
  3844. struct partition *pp;
  3845. int starts[SDEBUG_MAX_PARTS + 2];
  3846. int sectors_per_part, num_sectors, k;
  3847. int heads_by_sects, start_sec, end_sec;
  3848. /* assume partition table already zeroed */
  3849. if ((sdebug_num_parts < 1) || (store_size < 1048576))
  3850. return;
  3851. if (sdebug_num_parts > SDEBUG_MAX_PARTS) {
  3852. sdebug_num_parts = SDEBUG_MAX_PARTS;
  3853. pr_warn("reducing partitions to %d\n", SDEBUG_MAX_PARTS);
  3854. }
  3855. num_sectors = (int)sdebug_store_sectors;
  3856. sectors_per_part = (num_sectors - sdebug_sectors_per)
  3857. / sdebug_num_parts;
  3858. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  3859. starts[0] = sdebug_sectors_per;
  3860. for (k = 1; k < sdebug_num_parts; ++k)
  3861. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  3862. * heads_by_sects;
  3863. starts[sdebug_num_parts] = num_sectors;
  3864. starts[sdebug_num_parts + 1] = 0;
  3865. ramp[510] = 0x55; /* magic partition markings */
  3866. ramp[511] = 0xAA;
  3867. pp = (struct partition *)(ramp + 0x1be);
  3868. for (k = 0; starts[k + 1]; ++k, ++pp) {
  3869. start_sec = starts[k];
  3870. end_sec = starts[k + 1] - 1;
  3871. pp->boot_ind = 0;
  3872. pp->cyl = start_sec / heads_by_sects;
  3873. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  3874. / sdebug_sectors_per;
  3875. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  3876. pp->end_cyl = end_sec / heads_by_sects;
  3877. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  3878. / sdebug_sectors_per;
  3879. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  3880. pp->start_sect = cpu_to_le32(start_sec);
  3881. pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
  3882. pp->sys_ind = 0x83; /* plain Linux partition */
  3883. }
  3884. }
  3885. static void block_unblock_all_queues(bool block)
  3886. {
  3887. int j;
  3888. struct sdebug_queue *sqp;
  3889. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp)
  3890. atomic_set(&sqp->blocked, (int)block);
  3891. }
  3892. /* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
  3893. * commands will be processed normally before triggers occur.
  3894. */
  3895. static void tweak_cmnd_count(void)
  3896. {
  3897. int count, modulo;
  3898. modulo = abs(sdebug_every_nth);
  3899. if (modulo < 2)
  3900. return;
  3901. block_unblock_all_queues(true);
  3902. count = atomic_read(&sdebug_cmnd_count);
  3903. atomic_set(&sdebug_cmnd_count, (count / modulo) * modulo);
  3904. block_unblock_all_queues(false);
  3905. }
  3906. static void clear_queue_stats(void)
  3907. {
  3908. atomic_set(&sdebug_cmnd_count, 0);
  3909. atomic_set(&sdebug_completions, 0);
  3910. atomic_set(&sdebug_miss_cpus, 0);
  3911. atomic_set(&sdebug_a_tsf, 0);
  3912. }
  3913. static void setup_inject(struct sdebug_queue *sqp,
  3914. struct sdebug_queued_cmd *sqcp)
  3915. {
  3916. if ((atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) > 0) {
  3917. if (sdebug_every_nth > 0)
  3918. sqcp->inj_recovered = sqcp->inj_transport
  3919. = sqcp->inj_dif
  3920. = sqcp->inj_dix = sqcp->inj_short
  3921. = sqcp->inj_host_busy = sqcp->inj_cmd_abort = 0;
  3922. return;
  3923. }
  3924. sqcp->inj_recovered = !!(SDEBUG_OPT_RECOVERED_ERR & sdebug_opts);
  3925. sqcp->inj_transport = !!(SDEBUG_OPT_TRANSPORT_ERR & sdebug_opts);
  3926. sqcp->inj_dif = !!(SDEBUG_OPT_DIF_ERR & sdebug_opts);
  3927. sqcp->inj_dix = !!(SDEBUG_OPT_DIX_ERR & sdebug_opts);
  3928. sqcp->inj_short = !!(SDEBUG_OPT_SHORT_TRANSFER & sdebug_opts);
  3929. sqcp->inj_host_busy = !!(SDEBUG_OPT_HOST_BUSY & sdebug_opts);
  3930. sqcp->inj_cmd_abort = !!(SDEBUG_OPT_CMD_ABORT & sdebug_opts);
  3931. }
  3932. /* Complete the processing of the thread that queued a SCSI command to this
  3933. * driver. It either completes the command by calling cmnd_done() or
  3934. * schedules a hr timer or work queue then returns 0. Returns
  3935. * SCSI_MLQUEUE_HOST_BUSY if temporarily out of resources.
  3936. */
  3937. static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
  3938. int scsi_result,
  3939. int (*pfp)(struct scsi_cmnd *,
  3940. struct sdebug_dev_info *),
  3941. int delta_jiff, int ndelay)
  3942. {
  3943. unsigned long iflags;
  3944. int k, num_in_q, qdepth, inject;
  3945. struct sdebug_queue *sqp;
  3946. struct sdebug_queued_cmd *sqcp;
  3947. struct scsi_device *sdp;
  3948. struct sdebug_defer *sd_dp;
  3949. if (unlikely(devip == NULL)) {
  3950. if (scsi_result == 0)
  3951. scsi_result = DID_NO_CONNECT << 16;
  3952. goto respond_in_thread;
  3953. }
  3954. sdp = cmnd->device;
  3955. if (delta_jiff == 0)
  3956. goto respond_in_thread;
  3957. /* schedule the response at a later time if resources permit */
  3958. sqp = get_queue(cmnd);
  3959. spin_lock_irqsave(&sqp->qc_lock, iflags);
  3960. if (unlikely(atomic_read(&sqp->blocked))) {
  3961. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3962. return SCSI_MLQUEUE_HOST_BUSY;
  3963. }
  3964. num_in_q = atomic_read(&devip->num_in_q);
  3965. qdepth = cmnd->device->queue_depth;
  3966. inject = 0;
  3967. if (unlikely((qdepth > 0) && (num_in_q >= qdepth))) {
  3968. if (scsi_result) {
  3969. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3970. goto respond_in_thread;
  3971. } else
  3972. scsi_result = device_qfull_result;
  3973. } else if (unlikely(sdebug_every_nth &&
  3974. (SDEBUG_OPT_RARE_TSF & sdebug_opts) &&
  3975. (scsi_result == 0))) {
  3976. if ((num_in_q == (qdepth - 1)) &&
  3977. (atomic_inc_return(&sdebug_a_tsf) >=
  3978. abs(sdebug_every_nth))) {
  3979. atomic_set(&sdebug_a_tsf, 0);
  3980. inject = 1;
  3981. scsi_result = device_qfull_result;
  3982. }
  3983. }
  3984. k = find_first_zero_bit(sqp->in_use_bm, sdebug_max_queue);
  3985. if (unlikely(k >= sdebug_max_queue)) {
  3986. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  3987. if (scsi_result)
  3988. goto respond_in_thread;
  3989. else if (SDEBUG_OPT_ALL_TSF & sdebug_opts)
  3990. scsi_result = device_qfull_result;
  3991. if (SDEBUG_OPT_Q_NOISE & sdebug_opts)
  3992. sdev_printk(KERN_INFO, sdp,
  3993. "%s: max_queue=%d exceeded, %s\n",
  3994. __func__, sdebug_max_queue,
  3995. (scsi_result ? "status: TASK SET FULL" :
  3996. "report: host busy"));
  3997. if (scsi_result)
  3998. goto respond_in_thread;
  3999. else
  4000. return SCSI_MLQUEUE_HOST_BUSY;
  4001. }
  4002. __set_bit(k, sqp->in_use_bm);
  4003. atomic_inc(&devip->num_in_q);
  4004. sqcp = &sqp->qc_arr[k];
  4005. sqcp->a_cmnd = cmnd;
  4006. cmnd->host_scribble = (unsigned char *)sqcp;
  4007. sd_dp = sqcp->sd_dp;
  4008. spin_unlock_irqrestore(&sqp->qc_lock, iflags);
  4009. if (unlikely(sdebug_every_nth && sdebug_any_injecting_opt))
  4010. setup_inject(sqp, sqcp);
  4011. if (sd_dp == NULL) {
  4012. sd_dp = kzalloc(sizeof(*sd_dp), GFP_ATOMIC);
  4013. if (sd_dp == NULL)
  4014. return SCSI_MLQUEUE_HOST_BUSY;
  4015. }
  4016. cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
  4017. if (cmnd->result & SDEG_RES_IMMED_MASK) {
  4018. /*
  4019. * This is the F_DELAY_OVERR case. No delay.
  4020. */
  4021. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  4022. delta_jiff = ndelay = 0;
  4023. }
  4024. if (cmnd->result == 0 && scsi_result != 0)
  4025. cmnd->result = scsi_result;
  4026. if (unlikely(sdebug_verbose && cmnd->result))
  4027. sdev_printk(KERN_INFO, sdp, "%s: non-zero result=0x%x\n",
  4028. __func__, cmnd->result);
  4029. if (delta_jiff > 0 || ndelay > 0) {
  4030. ktime_t kt;
  4031. if (delta_jiff > 0) {
  4032. kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ));
  4033. } else
  4034. kt = ndelay;
  4035. if (!sd_dp->init_hrt) {
  4036. sd_dp->init_hrt = true;
  4037. sqcp->sd_dp = sd_dp;
  4038. hrtimer_init(&sd_dp->hrt, CLOCK_MONOTONIC,
  4039. HRTIMER_MODE_REL_PINNED);
  4040. sd_dp->hrt.function = sdebug_q_cmd_hrt_complete;
  4041. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  4042. sd_dp->qc_idx = k;
  4043. }
  4044. if (sdebug_statistics)
  4045. sd_dp->issuing_cpu = raw_smp_processor_id();
  4046. sd_dp->defer_t = SDEB_DEFER_HRT;
  4047. hrtimer_start(&sd_dp->hrt, kt, HRTIMER_MODE_REL_PINNED);
  4048. } else { /* jdelay < 0, use work queue */
  4049. if (!sd_dp->init_wq) {
  4050. sd_dp->init_wq = true;
  4051. sqcp->sd_dp = sd_dp;
  4052. sd_dp->sqa_idx = sqp - sdebug_q_arr;
  4053. sd_dp->qc_idx = k;
  4054. INIT_WORK(&sd_dp->ew.work, sdebug_q_cmd_wq_complete);
  4055. }
  4056. if (sdebug_statistics)
  4057. sd_dp->issuing_cpu = raw_smp_processor_id();
  4058. sd_dp->defer_t = SDEB_DEFER_WQ;
  4059. if (unlikely(sqcp->inj_cmd_abort))
  4060. sd_dp->aborted = true;
  4061. schedule_work(&sd_dp->ew.work);
  4062. if (unlikely(sqcp->inj_cmd_abort)) {
  4063. sdev_printk(KERN_INFO, sdp, "abort request tag %d\n",
  4064. cmnd->request->tag);
  4065. blk_abort_request(cmnd->request);
  4066. }
  4067. }
  4068. if (unlikely((SDEBUG_OPT_Q_NOISE & sdebug_opts) &&
  4069. (scsi_result == device_qfull_result)))
  4070. sdev_printk(KERN_INFO, sdp,
  4071. "%s: num_in_q=%d +1, %s%s\n", __func__,
  4072. num_in_q, (inject ? "<inject> " : ""),
  4073. "status: TASK SET FULL");
  4074. return 0;
  4075. respond_in_thread: /* call back to mid-layer using invocation thread */
  4076. cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
  4077. cmnd->result &= ~SDEG_RES_IMMED_MASK;
  4078. if (cmnd->result == 0 && scsi_result != 0)
  4079. cmnd->result = scsi_result;
  4080. cmnd->scsi_done(cmnd);
  4081. return 0;
  4082. }
  4083. /* Note: The following macros create attribute files in the
  4084. /sys/module/scsi_debug/parameters directory. Unfortunately this
  4085. driver is unaware of a change and cannot trigger auxiliary actions
  4086. as it can when the corresponding attribute in the
  4087. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  4088. */
  4089. module_param_named(add_host, sdebug_add_host, int, S_IRUGO | S_IWUSR);
  4090. module_param_named(ato, sdebug_ato, int, S_IRUGO);
  4091. module_param_named(cdb_len, sdebug_cdb_len, int, 0644);
  4092. module_param_named(clustering, sdebug_clustering, bool, S_IRUGO | S_IWUSR);
  4093. module_param_named(delay, sdebug_jdelay, int, S_IRUGO | S_IWUSR);
  4094. module_param_named(dev_size_mb, sdebug_dev_size_mb, int, S_IRUGO);
  4095. module_param_named(dif, sdebug_dif, int, S_IRUGO);
  4096. module_param_named(dix, sdebug_dix, int, S_IRUGO);
  4097. module_param_named(dsense, sdebug_dsense, int, S_IRUGO | S_IWUSR);
  4098. module_param_named(every_nth, sdebug_every_nth, int, S_IRUGO | S_IWUSR);
  4099. module_param_named(fake_rw, sdebug_fake_rw, int, S_IRUGO | S_IWUSR);
  4100. module_param_named(guard, sdebug_guard, uint, S_IRUGO);
  4101. module_param_named(host_lock, sdebug_host_lock, bool, S_IRUGO | S_IWUSR);
  4102. module_param_string(inq_vendor, sdebug_inq_vendor_id,
  4103. sizeof(sdebug_inq_vendor_id), S_IRUGO|S_IWUSR);
  4104. module_param_string(inq_product, sdebug_inq_product_id,
  4105. sizeof(sdebug_inq_product_id), S_IRUGO|S_IWUSR);
  4106. module_param_string(inq_rev, sdebug_inq_product_rev,
  4107. sizeof(sdebug_inq_product_rev), S_IRUGO|S_IWUSR);
  4108. module_param_named(lbpu, sdebug_lbpu, int, S_IRUGO);
  4109. module_param_named(lbpws, sdebug_lbpws, int, S_IRUGO);
  4110. module_param_named(lbpws10, sdebug_lbpws10, int, S_IRUGO);
  4111. module_param_named(lbprz, sdebug_lbprz, int, S_IRUGO);
  4112. module_param_named(lowest_aligned, sdebug_lowest_aligned, int, S_IRUGO);
  4113. module_param_named(max_luns, sdebug_max_luns, int, S_IRUGO | S_IWUSR);
  4114. module_param_named(max_queue, sdebug_max_queue, int, S_IRUGO | S_IWUSR);
  4115. module_param_named(medium_error_start, sdebug_medium_error_start, int, S_IRUGO | S_IWUSR);
  4116. module_param_named(medium_error_count, sdebug_medium_error_count, int, S_IRUGO | S_IWUSR);
  4117. module_param_named(ndelay, sdebug_ndelay, int, S_IRUGO | S_IWUSR);
  4118. module_param_named(no_lun_0, sdebug_no_lun_0, int, S_IRUGO | S_IWUSR);
  4119. module_param_named(no_uld, sdebug_no_uld, int, S_IRUGO);
  4120. module_param_named(num_parts, sdebug_num_parts, int, S_IRUGO);
  4121. module_param_named(num_tgts, sdebug_num_tgts, int, S_IRUGO | S_IWUSR);
  4122. module_param_named(opt_blks, sdebug_opt_blks, int, S_IRUGO);
  4123. module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
  4124. module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
  4125. module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
  4126. module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
  4127. module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
  4128. module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
  4129. module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
  4130. module_param_named(statistics, sdebug_statistics, bool, S_IRUGO | S_IWUSR);
  4131. module_param_named(strict, sdebug_strict, bool, S_IRUGO | S_IWUSR);
  4132. module_param_named(submit_queues, submit_queues, int, S_IRUGO);
  4133. module_param_named(unmap_alignment, sdebug_unmap_alignment, int, S_IRUGO);
  4134. module_param_named(unmap_granularity, sdebug_unmap_granularity, int, S_IRUGO);
  4135. module_param_named(unmap_max_blocks, sdebug_unmap_max_blocks, int, S_IRUGO);
  4136. module_param_named(unmap_max_desc, sdebug_unmap_max_desc, int, S_IRUGO);
  4137. module_param_named(virtual_gb, sdebug_virtual_gb, int, S_IRUGO | S_IWUSR);
  4138. module_param_named(uuid_ctl, sdebug_uuid_ctl, int, S_IRUGO);
  4139. module_param_named(vpd_use_hostno, sdebug_vpd_use_hostno, int,
  4140. S_IRUGO | S_IWUSR);
  4141. module_param_named(write_same_length, sdebug_write_same_length, int,
  4142. S_IRUGO | S_IWUSR);
  4143. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  4144. MODULE_DESCRIPTION("SCSI debug adapter driver");
  4145. MODULE_LICENSE("GPL");
  4146. MODULE_VERSION(SDEBUG_VERSION);
  4147. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  4148. MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
  4149. MODULE_PARM_DESC(cdb_len, "suggest CDB lengths to drivers (def=10)");
  4150. MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
  4151. MODULE_PARM_DESC(delay, "response delay (def=1 jiffy); 0:imm, -1,-2:tiny");
  4152. MODULE_PARM_DESC(dev_size_mb, "size in MiB of ram shared by devs(def=8)");
  4153. MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
  4154. MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
  4155. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  4156. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  4157. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  4158. MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
  4159. MODULE_PARM_DESC(host_lock, "host_lock is ignored (def=0)");
  4160. MODULE_PARM_DESC(inq_vendor, "SCSI INQUIRY vendor string (def=\"Linux\")");
  4161. MODULE_PARM_DESC(inq_product, "SCSI INQUIRY product string (def=\"scsi_debug\")");
  4162. MODULE_PARM_DESC(inq_rev, "SCSI INQUIRY revision string (def=\""
  4163. SDEBUG_VERSION "\")");
  4164. MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
  4165. MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
  4166. MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
  4167. MODULE_PARM_DESC(lbprz,
  4168. "on read unmapped LBs return 0 when 1 (def), return 0xff when 2");
  4169. MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
  4170. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  4171. MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to max(def))");
  4172. MODULE_PARM_DESC(medium_error_start, "starting sector number to return MEDIUM error");
  4173. MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUM error");
  4174. MODULE_PARM_DESC(ndelay, "response delay in nanoseconds (def=0 -> ignore)");
  4175. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  4176. MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
  4177. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  4178. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  4179. MODULE_PARM_DESC(opt_blks, "optimal transfer length in blocks (def=1024)");
  4180. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  4181. MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
  4182. MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
  4183. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  4184. MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
  4185. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
  4186. MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
  4187. MODULE_PARM_DESC(statistics, "collect statistics on commands, queues (def=0)");
  4188. MODULE_PARM_DESC(strict, "stricter checks: reserved field in cdb (def=0)");
  4189. MODULE_PARM_DESC(submit_queues, "support for block multi-queue (def=1)");
  4190. MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
  4191. MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
  4192. MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
  4193. MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
  4194. MODULE_PARM_DESC(uuid_ctl,
  4195. "1->use uuid for lu name, 0->don't, 2->all use same (def=0)");
  4196. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte (GiB) size (def=0 -> use dev_size_mb)");
  4197. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  4198. MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
  4199. #define SDEBUG_INFO_LEN 256
  4200. static char sdebug_info[SDEBUG_INFO_LEN];
  4201. static const char *scsi_debug_info(struct Scsi_Host *shp)
  4202. {
  4203. int k;
  4204. k = scnprintf(sdebug_info, SDEBUG_INFO_LEN, "%s: version %s [%s]\n",
  4205. my_name, SDEBUG_VERSION, sdebug_version_date);
  4206. if (k >= (SDEBUG_INFO_LEN - 1))
  4207. return sdebug_info;
  4208. scnprintf(sdebug_info + k, SDEBUG_INFO_LEN - k,
  4209. " dev_size_mb=%d, opts=0x%x, submit_queues=%d, %s=%d",
  4210. sdebug_dev_size_mb, sdebug_opts, submit_queues,
  4211. "statistics", (int)sdebug_statistics);
  4212. return sdebug_info;
  4213. }
  4214. /* 'echo <val> > /proc/scsi/scsi_debug/<host_id>' writes to opts */
  4215. static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer,
  4216. int length)
  4217. {
  4218. char arr[16];
  4219. int opts;
  4220. int minLen = length > 15 ? 15 : length;
  4221. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  4222. return -EACCES;
  4223. memcpy(arr, buffer, minLen);
  4224. arr[minLen] = '\0';
  4225. if (1 != sscanf(arr, "%d", &opts))
  4226. return -EINVAL;
  4227. sdebug_opts = opts;
  4228. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  4229. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  4230. if (sdebug_every_nth != 0)
  4231. tweak_cmnd_count();
  4232. return length;
  4233. }
  4234. /* Output seen with 'cat /proc/scsi/scsi_debug/<host_id>'. It will be the
  4235. * same for each scsi_debug host (if more than one). Some of the counters
  4236. * output are not atomics so might be inaccurate in a busy system. */
  4237. static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
  4238. {
  4239. int f, j, l;
  4240. struct sdebug_queue *sqp;
  4241. seq_printf(m, "scsi_debug adapter driver, version %s [%s]\n",
  4242. SDEBUG_VERSION, sdebug_version_date);
  4243. seq_printf(m, "num_tgts=%d, %ssize=%d MB, opts=0x%x, every_nth=%d\n",
  4244. sdebug_num_tgts, "shared (ram) ", sdebug_dev_size_mb,
  4245. sdebug_opts, sdebug_every_nth);
  4246. seq_printf(m, "delay=%d, ndelay=%d, max_luns=%d, sector_size=%d %s\n",
  4247. sdebug_jdelay, sdebug_ndelay, sdebug_max_luns,
  4248. sdebug_sector_size, "bytes");
  4249. seq_printf(m, "cylinders=%d, heads=%d, sectors=%d, command aborts=%d\n",
  4250. sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  4251. num_aborts);
  4252. seq_printf(m, "RESETs: device=%d, target=%d, bus=%d, host=%d\n",
  4253. num_dev_resets, num_target_resets, num_bus_resets,
  4254. num_host_resets);
  4255. seq_printf(m, "dix_reads=%d, dix_writes=%d, dif_errors=%d\n",
  4256. dix_reads, dix_writes, dif_errors);
  4257. seq_printf(m, "usec_in_jiffy=%lu, statistics=%d\n", TICK_NSEC / 1000,
  4258. sdebug_statistics);
  4259. seq_printf(m, "cmnd_count=%d, completions=%d, %s=%d, a_tsf=%d\n",
  4260. atomic_read(&sdebug_cmnd_count),
  4261. atomic_read(&sdebug_completions),
  4262. "miss_cpus", atomic_read(&sdebug_miss_cpus),
  4263. atomic_read(&sdebug_a_tsf));
  4264. seq_printf(m, "submit_queues=%d\n", submit_queues);
  4265. for (j = 0, sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
  4266. seq_printf(m, " queue %d:\n", j);
  4267. f = find_first_bit(sqp->in_use_bm, sdebug_max_queue);
  4268. if (f != sdebug_max_queue) {
  4269. l = find_last_bit(sqp->in_use_bm, sdebug_max_queue);
  4270. seq_printf(m, " in_use_bm BUSY: %s: %d,%d\n",
  4271. "first,last bits", f, l);
  4272. }
  4273. }
  4274. return 0;
  4275. }
  4276. static ssize_t delay_show(struct device_driver *ddp, char *buf)
  4277. {
  4278. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_jdelay);
  4279. }
  4280. /* Returns -EBUSY if jdelay is being changed and commands are queued. The unit
  4281. * of delay is jiffies.
  4282. */
  4283. static ssize_t delay_store(struct device_driver *ddp, const char *buf,
  4284. size_t count)
  4285. {
  4286. int jdelay, res;
  4287. if (count > 0 && sscanf(buf, "%d", &jdelay) == 1) {
  4288. res = count;
  4289. if (sdebug_jdelay != jdelay) {
  4290. int j, k;
  4291. struct sdebug_queue *sqp;
  4292. block_unblock_all_queues(true);
  4293. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4294. ++j, ++sqp) {
  4295. k = find_first_bit(sqp->in_use_bm,
  4296. sdebug_max_queue);
  4297. if (k != sdebug_max_queue) {
  4298. res = -EBUSY; /* queued commands */
  4299. break;
  4300. }
  4301. }
  4302. if (res > 0) {
  4303. sdebug_jdelay = jdelay;
  4304. sdebug_ndelay = 0;
  4305. }
  4306. block_unblock_all_queues(false);
  4307. }
  4308. return res;
  4309. }
  4310. return -EINVAL;
  4311. }
  4312. static DRIVER_ATTR_RW(delay);
  4313. static ssize_t ndelay_show(struct device_driver *ddp, char *buf)
  4314. {
  4315. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ndelay);
  4316. }
  4317. /* Returns -EBUSY if ndelay is being changed and commands are queued */
  4318. /* If > 0 and accepted then sdebug_jdelay is set to JDELAY_OVERRIDDEN */
  4319. static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,
  4320. size_t count)
  4321. {
  4322. int ndelay, res;
  4323. if ((count > 0) && (1 == sscanf(buf, "%d", &ndelay)) &&
  4324. (ndelay >= 0) && (ndelay < (1000 * 1000 * 1000))) {
  4325. res = count;
  4326. if (sdebug_ndelay != ndelay) {
  4327. int j, k;
  4328. struct sdebug_queue *sqp;
  4329. block_unblock_all_queues(true);
  4330. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4331. ++j, ++sqp) {
  4332. k = find_first_bit(sqp->in_use_bm,
  4333. sdebug_max_queue);
  4334. if (k != sdebug_max_queue) {
  4335. res = -EBUSY; /* queued commands */
  4336. break;
  4337. }
  4338. }
  4339. if (res > 0) {
  4340. sdebug_ndelay = ndelay;
  4341. sdebug_jdelay = ndelay ? JDELAY_OVERRIDDEN
  4342. : DEF_JDELAY;
  4343. }
  4344. block_unblock_all_queues(false);
  4345. }
  4346. return res;
  4347. }
  4348. return -EINVAL;
  4349. }
  4350. static DRIVER_ATTR_RW(ndelay);
  4351. static ssize_t opts_show(struct device_driver *ddp, char *buf)
  4352. {
  4353. return scnprintf(buf, PAGE_SIZE, "0x%x\n", sdebug_opts);
  4354. }
  4355. static ssize_t opts_store(struct device_driver *ddp, const char *buf,
  4356. size_t count)
  4357. {
  4358. int opts;
  4359. char work[20];
  4360. if (sscanf(buf, "%10s", work) == 1) {
  4361. if (strncasecmp(work, "0x", 2) == 0) {
  4362. if (kstrtoint(work + 2, 16, &opts) == 0)
  4363. goto opts_done;
  4364. } else {
  4365. if (kstrtoint(work, 10, &opts) == 0)
  4366. goto opts_done;
  4367. }
  4368. }
  4369. return -EINVAL;
  4370. opts_done:
  4371. sdebug_opts = opts;
  4372. sdebug_verbose = !!(SDEBUG_OPT_NOISE & opts);
  4373. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & opts);
  4374. tweak_cmnd_count();
  4375. return count;
  4376. }
  4377. static DRIVER_ATTR_RW(opts);
  4378. static ssize_t ptype_show(struct device_driver *ddp, char *buf)
  4379. {
  4380. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ptype);
  4381. }
  4382. static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
  4383. size_t count)
  4384. {
  4385. int n;
  4386. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4387. sdebug_ptype = n;
  4388. return count;
  4389. }
  4390. return -EINVAL;
  4391. }
  4392. static DRIVER_ATTR_RW(ptype);
  4393. static ssize_t dsense_show(struct device_driver *ddp, char *buf)
  4394. {
  4395. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dsense);
  4396. }
  4397. static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
  4398. size_t count)
  4399. {
  4400. int n;
  4401. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4402. sdebug_dsense = n;
  4403. return count;
  4404. }
  4405. return -EINVAL;
  4406. }
  4407. static DRIVER_ATTR_RW(dsense);
  4408. static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
  4409. {
  4410. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_fake_rw);
  4411. }
  4412. static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
  4413. size_t count)
  4414. {
  4415. int n;
  4416. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4417. n = (n > 0);
  4418. sdebug_fake_rw = (sdebug_fake_rw > 0);
  4419. if (sdebug_fake_rw != n) {
  4420. if ((0 == n) && (NULL == fake_storep)) {
  4421. unsigned long sz =
  4422. (unsigned long)sdebug_dev_size_mb *
  4423. 1048576;
  4424. fake_storep = vzalloc(sz);
  4425. if (NULL == fake_storep) {
  4426. pr_err("out of memory, 9\n");
  4427. return -ENOMEM;
  4428. }
  4429. }
  4430. sdebug_fake_rw = n;
  4431. }
  4432. return count;
  4433. }
  4434. return -EINVAL;
  4435. }
  4436. static DRIVER_ATTR_RW(fake_rw);
  4437. static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
  4438. {
  4439. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_lun_0);
  4440. }
  4441. static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
  4442. size_t count)
  4443. {
  4444. int n;
  4445. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4446. sdebug_no_lun_0 = n;
  4447. return count;
  4448. }
  4449. return -EINVAL;
  4450. }
  4451. static DRIVER_ATTR_RW(no_lun_0);
  4452. static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
  4453. {
  4454. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_tgts);
  4455. }
  4456. static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
  4457. size_t count)
  4458. {
  4459. int n;
  4460. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4461. sdebug_num_tgts = n;
  4462. sdebug_max_tgts_luns();
  4463. return count;
  4464. }
  4465. return -EINVAL;
  4466. }
  4467. static DRIVER_ATTR_RW(num_tgts);
  4468. static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
  4469. {
  4470. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dev_size_mb);
  4471. }
  4472. static DRIVER_ATTR_RO(dev_size_mb);
  4473. static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
  4474. {
  4475. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_num_parts);
  4476. }
  4477. static DRIVER_ATTR_RO(num_parts);
  4478. static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
  4479. {
  4480. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_every_nth);
  4481. }
  4482. static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
  4483. size_t count)
  4484. {
  4485. int nth;
  4486. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  4487. sdebug_every_nth = nth;
  4488. if (nth && !sdebug_statistics) {
  4489. pr_info("every_nth needs statistics=1, set it\n");
  4490. sdebug_statistics = true;
  4491. }
  4492. tweak_cmnd_count();
  4493. return count;
  4494. }
  4495. return -EINVAL;
  4496. }
  4497. static DRIVER_ATTR_RW(every_nth);
  4498. static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
  4499. {
  4500. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_luns);
  4501. }
  4502. static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
  4503. size_t count)
  4504. {
  4505. int n;
  4506. bool changed;
  4507. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4508. if (n > 256) {
  4509. pr_warn("max_luns can be no more than 256\n");
  4510. return -EINVAL;
  4511. }
  4512. changed = (sdebug_max_luns != n);
  4513. sdebug_max_luns = n;
  4514. sdebug_max_tgts_luns();
  4515. if (changed && (sdebug_scsi_level >= 5)) { /* >= SPC-3 */
  4516. struct sdebug_host_info *sdhp;
  4517. struct sdebug_dev_info *dp;
  4518. spin_lock(&sdebug_host_list_lock);
  4519. list_for_each_entry(sdhp, &sdebug_host_list,
  4520. host_list) {
  4521. list_for_each_entry(dp, &sdhp->dev_info_list,
  4522. dev_list) {
  4523. set_bit(SDEBUG_UA_LUNS_CHANGED,
  4524. dp->uas_bm);
  4525. }
  4526. }
  4527. spin_unlock(&sdebug_host_list_lock);
  4528. }
  4529. return count;
  4530. }
  4531. return -EINVAL;
  4532. }
  4533. static DRIVER_ATTR_RW(max_luns);
  4534. static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
  4535. {
  4536. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_max_queue);
  4537. }
  4538. /* N.B. max_queue can be changed while there are queued commands. In flight
  4539. * commands beyond the new max_queue will be completed. */
  4540. static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
  4541. size_t count)
  4542. {
  4543. int j, n, k, a;
  4544. struct sdebug_queue *sqp;
  4545. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
  4546. (n <= SDEBUG_CANQUEUE)) {
  4547. block_unblock_all_queues(true);
  4548. k = 0;
  4549. for (j = 0, sqp = sdebug_q_arr; j < submit_queues;
  4550. ++j, ++sqp) {
  4551. a = find_last_bit(sqp->in_use_bm, SDEBUG_CANQUEUE);
  4552. if (a > k)
  4553. k = a;
  4554. }
  4555. sdebug_max_queue = n;
  4556. if (k == SDEBUG_CANQUEUE)
  4557. atomic_set(&retired_max_queue, 0);
  4558. else if (k >= n)
  4559. atomic_set(&retired_max_queue, k + 1);
  4560. else
  4561. atomic_set(&retired_max_queue, 0);
  4562. block_unblock_all_queues(false);
  4563. return count;
  4564. }
  4565. return -EINVAL;
  4566. }
  4567. static DRIVER_ATTR_RW(max_queue);
  4568. static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
  4569. {
  4570. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_no_uld);
  4571. }
  4572. static DRIVER_ATTR_RO(no_uld);
  4573. static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
  4574. {
  4575. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_scsi_level);
  4576. }
  4577. static DRIVER_ATTR_RO(scsi_level);
  4578. static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
  4579. {
  4580. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_virtual_gb);
  4581. }
  4582. static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
  4583. size_t count)
  4584. {
  4585. int n;
  4586. bool changed;
  4587. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4588. changed = (sdebug_virtual_gb != n);
  4589. sdebug_virtual_gb = n;
  4590. sdebug_capacity = get_sdebug_capacity();
  4591. if (changed) {
  4592. struct sdebug_host_info *sdhp;
  4593. struct sdebug_dev_info *dp;
  4594. spin_lock(&sdebug_host_list_lock);
  4595. list_for_each_entry(sdhp, &sdebug_host_list,
  4596. host_list) {
  4597. list_for_each_entry(dp, &sdhp->dev_info_list,
  4598. dev_list) {
  4599. set_bit(SDEBUG_UA_CAPACITY_CHANGED,
  4600. dp->uas_bm);
  4601. }
  4602. }
  4603. spin_unlock(&sdebug_host_list_lock);
  4604. }
  4605. return count;
  4606. }
  4607. return -EINVAL;
  4608. }
  4609. static DRIVER_ATTR_RW(virtual_gb);
  4610. static ssize_t add_host_show(struct device_driver *ddp, char *buf)
  4611. {
  4612. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_add_host);
  4613. }
  4614. static int sdebug_add_adapter(void);
  4615. static void sdebug_remove_adapter(void);
  4616. static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
  4617. size_t count)
  4618. {
  4619. int delta_hosts;
  4620. if (sscanf(buf, "%d", &delta_hosts) != 1)
  4621. return -EINVAL;
  4622. if (delta_hosts > 0) {
  4623. do {
  4624. sdebug_add_adapter();
  4625. } while (--delta_hosts);
  4626. } else if (delta_hosts < 0) {
  4627. do {
  4628. sdebug_remove_adapter();
  4629. } while (++delta_hosts);
  4630. }
  4631. return count;
  4632. }
  4633. static DRIVER_ATTR_RW(add_host);
  4634. static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
  4635. {
  4636. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_vpd_use_hostno);
  4637. }
  4638. static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
  4639. size_t count)
  4640. {
  4641. int n;
  4642. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4643. sdebug_vpd_use_hostno = n;
  4644. return count;
  4645. }
  4646. return -EINVAL;
  4647. }
  4648. static DRIVER_ATTR_RW(vpd_use_hostno);
  4649. static ssize_t statistics_show(struct device_driver *ddp, char *buf)
  4650. {
  4651. return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_statistics);
  4652. }
  4653. static ssize_t statistics_store(struct device_driver *ddp, const char *buf,
  4654. size_t count)
  4655. {
  4656. int n;
  4657. if ((count > 0) && (sscanf(buf, "%d", &n) == 1) && (n >= 0)) {
  4658. if (n > 0)
  4659. sdebug_statistics = true;
  4660. else {
  4661. clear_queue_stats();
  4662. sdebug_statistics = false;
  4663. }
  4664. return count;
  4665. }
  4666. return -EINVAL;
  4667. }
  4668. static DRIVER_ATTR_RW(statistics);
  4669. static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
  4670. {
  4671. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_sector_size);
  4672. }
  4673. static DRIVER_ATTR_RO(sector_size);
  4674. static ssize_t submit_queues_show(struct device_driver *ddp, char *buf)
  4675. {
  4676. return scnprintf(buf, PAGE_SIZE, "%d\n", submit_queues);
  4677. }
  4678. static DRIVER_ATTR_RO(submit_queues);
  4679. static ssize_t dix_show(struct device_driver *ddp, char *buf)
  4680. {
  4681. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dix);
  4682. }
  4683. static DRIVER_ATTR_RO(dix);
  4684. static ssize_t dif_show(struct device_driver *ddp, char *buf)
  4685. {
  4686. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_dif);
  4687. }
  4688. static DRIVER_ATTR_RO(dif);
  4689. static ssize_t guard_show(struct device_driver *ddp, char *buf)
  4690. {
  4691. return scnprintf(buf, PAGE_SIZE, "%u\n", sdebug_guard);
  4692. }
  4693. static DRIVER_ATTR_RO(guard);
  4694. static ssize_t ato_show(struct device_driver *ddp, char *buf)
  4695. {
  4696. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_ato);
  4697. }
  4698. static DRIVER_ATTR_RO(ato);
  4699. static ssize_t map_show(struct device_driver *ddp, char *buf)
  4700. {
  4701. ssize_t count;
  4702. if (!scsi_debug_lbp())
  4703. return scnprintf(buf, PAGE_SIZE, "0-%u\n",
  4704. sdebug_store_sectors);
  4705. count = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  4706. (int)map_size, map_storep);
  4707. buf[count++] = '\n';
  4708. buf[count] = '\0';
  4709. return count;
  4710. }
  4711. static DRIVER_ATTR_RO(map);
  4712. static ssize_t removable_show(struct device_driver *ddp, char *buf)
  4713. {
  4714. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
  4715. }
  4716. static ssize_t removable_store(struct device_driver *ddp, const char *buf,
  4717. size_t count)
  4718. {
  4719. int n;
  4720. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4721. sdebug_removable = (n > 0);
  4722. return count;
  4723. }
  4724. return -EINVAL;
  4725. }
  4726. static DRIVER_ATTR_RW(removable);
  4727. static ssize_t host_lock_show(struct device_driver *ddp, char *buf)
  4728. {
  4729. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_host_lock);
  4730. }
  4731. /* N.B. sdebug_host_lock does nothing, kept for backward compatibility */
  4732. static ssize_t host_lock_store(struct device_driver *ddp, const char *buf,
  4733. size_t count)
  4734. {
  4735. int n;
  4736. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4737. sdebug_host_lock = (n > 0);
  4738. return count;
  4739. }
  4740. return -EINVAL;
  4741. }
  4742. static DRIVER_ATTR_RW(host_lock);
  4743. static ssize_t strict_show(struct device_driver *ddp, char *buf)
  4744. {
  4745. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_strict);
  4746. }
  4747. static ssize_t strict_store(struct device_driver *ddp, const char *buf,
  4748. size_t count)
  4749. {
  4750. int n;
  4751. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  4752. sdebug_strict = (n > 0);
  4753. return count;
  4754. }
  4755. return -EINVAL;
  4756. }
  4757. static DRIVER_ATTR_RW(strict);
  4758. static ssize_t uuid_ctl_show(struct device_driver *ddp, char *buf)
  4759. {
  4760. return scnprintf(buf, PAGE_SIZE, "%d\n", !!sdebug_uuid_ctl);
  4761. }
  4762. static DRIVER_ATTR_RO(uuid_ctl);
  4763. static ssize_t cdb_len_show(struct device_driver *ddp, char *buf)
  4764. {
  4765. return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_cdb_len);
  4766. }
  4767. static ssize_t cdb_len_store(struct device_driver *ddp, const char *buf,
  4768. size_t count)
  4769. {
  4770. int ret, n;
  4771. ret = kstrtoint(buf, 0, &n);
  4772. if (ret)
  4773. return ret;
  4774. sdebug_cdb_len = n;
  4775. all_config_cdb_len();
  4776. return count;
  4777. }
  4778. static DRIVER_ATTR_RW(cdb_len);
  4779. /* Note: The following array creates attribute files in the
  4780. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  4781. files (over those found in the /sys/module/scsi_debug/parameters
  4782. directory) is that auxiliary actions can be triggered when an attribute
  4783. is changed. For example see: sdebug_add_host_store() above.
  4784. */
  4785. static struct attribute *sdebug_drv_attrs[] = {
  4786. &driver_attr_delay.attr,
  4787. &driver_attr_opts.attr,
  4788. &driver_attr_ptype.attr,
  4789. &driver_attr_dsense.attr,
  4790. &driver_attr_fake_rw.attr,
  4791. &driver_attr_no_lun_0.attr,
  4792. &driver_attr_num_tgts.attr,
  4793. &driver_attr_dev_size_mb.attr,
  4794. &driver_attr_num_parts.attr,
  4795. &driver_attr_every_nth.attr,
  4796. &driver_attr_max_luns.attr,
  4797. &driver_attr_max_queue.attr,
  4798. &driver_attr_no_uld.attr,
  4799. &driver_attr_scsi_level.attr,
  4800. &driver_attr_virtual_gb.attr,
  4801. &driver_attr_add_host.attr,
  4802. &driver_attr_vpd_use_hostno.attr,
  4803. &driver_attr_sector_size.attr,
  4804. &driver_attr_statistics.attr,
  4805. &driver_attr_submit_queues.attr,
  4806. &driver_attr_dix.attr,
  4807. &driver_attr_dif.attr,
  4808. &driver_attr_guard.attr,
  4809. &driver_attr_ato.attr,
  4810. &driver_attr_map.attr,
  4811. &driver_attr_removable.attr,
  4812. &driver_attr_host_lock.attr,
  4813. &driver_attr_ndelay.attr,
  4814. &driver_attr_strict.attr,
  4815. &driver_attr_uuid_ctl.attr,
  4816. &driver_attr_cdb_len.attr,
  4817. NULL,
  4818. };
  4819. ATTRIBUTE_GROUPS(sdebug_drv);
  4820. static struct device *pseudo_primary;
  4821. static int __init scsi_debug_init(void)
  4822. {
  4823. unsigned long sz;
  4824. int host_to_add;
  4825. int k;
  4826. int ret;
  4827. atomic_set(&retired_max_queue, 0);
  4828. if (sdebug_ndelay >= 1000 * 1000 * 1000) {
  4829. pr_warn("ndelay must be less than 1 second, ignored\n");
  4830. sdebug_ndelay = 0;
  4831. } else if (sdebug_ndelay > 0)
  4832. sdebug_jdelay = JDELAY_OVERRIDDEN;
  4833. switch (sdebug_sector_size) {
  4834. case 512:
  4835. case 1024:
  4836. case 2048:
  4837. case 4096:
  4838. break;
  4839. default:
  4840. pr_err("invalid sector_size %d\n", sdebug_sector_size);
  4841. return -EINVAL;
  4842. }
  4843. switch (sdebug_dif) {
  4844. case T10_PI_TYPE0_PROTECTION:
  4845. break;
  4846. case T10_PI_TYPE1_PROTECTION:
  4847. case T10_PI_TYPE2_PROTECTION:
  4848. case T10_PI_TYPE3_PROTECTION:
  4849. have_dif_prot = true;
  4850. break;
  4851. default:
  4852. pr_err("dif must be 0, 1, 2 or 3\n");
  4853. return -EINVAL;
  4854. }
  4855. if (sdebug_num_tgts < 0) {
  4856. pr_err("num_tgts must be >= 0\n");
  4857. return -EINVAL;
  4858. }
  4859. if (sdebug_guard > 1) {
  4860. pr_err("guard must be 0 or 1\n");
  4861. return -EINVAL;
  4862. }
  4863. if (sdebug_ato > 1) {
  4864. pr_err("ato must be 0 or 1\n");
  4865. return -EINVAL;
  4866. }
  4867. if (sdebug_physblk_exp > 15) {
  4868. pr_err("invalid physblk_exp %u\n", sdebug_physblk_exp);
  4869. return -EINVAL;
  4870. }
  4871. if (sdebug_max_luns > 256) {
  4872. pr_warn("max_luns can be no more than 256, use default\n");
  4873. sdebug_max_luns = DEF_MAX_LUNS;
  4874. }
  4875. if (sdebug_lowest_aligned > 0x3fff) {
  4876. pr_err("lowest_aligned too big: %u\n", sdebug_lowest_aligned);
  4877. return -EINVAL;
  4878. }
  4879. if (submit_queues < 1) {
  4880. pr_err("submit_queues must be 1 or more\n");
  4881. return -EINVAL;
  4882. }
  4883. sdebug_q_arr = kcalloc(submit_queues, sizeof(struct sdebug_queue),
  4884. GFP_KERNEL);
  4885. if (sdebug_q_arr == NULL)
  4886. return -ENOMEM;
  4887. for (k = 0; k < submit_queues; ++k)
  4888. spin_lock_init(&sdebug_q_arr[k].qc_lock);
  4889. if (sdebug_dev_size_mb < 1)
  4890. sdebug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  4891. sz = (unsigned long)sdebug_dev_size_mb * 1048576;
  4892. sdebug_store_sectors = sz / sdebug_sector_size;
  4893. sdebug_capacity = get_sdebug_capacity();
  4894. /* play around with geometry, don't waste too much on track 0 */
  4895. sdebug_heads = 8;
  4896. sdebug_sectors_per = 32;
  4897. if (sdebug_dev_size_mb >= 256)
  4898. sdebug_heads = 64;
  4899. else if (sdebug_dev_size_mb >= 16)
  4900. sdebug_heads = 32;
  4901. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  4902. (sdebug_sectors_per * sdebug_heads);
  4903. if (sdebug_cylinders_per >= 1024) {
  4904. /* other LLDs do this; implies >= 1GB ram disk ... */
  4905. sdebug_heads = 255;
  4906. sdebug_sectors_per = 63;
  4907. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  4908. (sdebug_sectors_per * sdebug_heads);
  4909. }
  4910. if (sdebug_fake_rw == 0) {
  4911. fake_storep = vzalloc(sz);
  4912. if (NULL == fake_storep) {
  4913. pr_err("out of memory, 1\n");
  4914. ret = -ENOMEM;
  4915. goto free_q_arr;
  4916. }
  4917. if (sdebug_num_parts > 0)
  4918. sdebug_build_parts(fake_storep, sz);
  4919. }
  4920. if (sdebug_dix) {
  4921. int dif_size;
  4922. dif_size = sdebug_store_sectors * sizeof(struct t10_pi_tuple);
  4923. dif_storep = vmalloc(dif_size);
  4924. pr_err("dif_storep %u bytes @ %p\n", dif_size, dif_storep);
  4925. if (dif_storep == NULL) {
  4926. pr_err("out of mem. (DIX)\n");
  4927. ret = -ENOMEM;
  4928. goto free_vm;
  4929. }
  4930. memset(dif_storep, 0xff, dif_size);
  4931. }
  4932. /* Logical Block Provisioning */
  4933. if (scsi_debug_lbp()) {
  4934. sdebug_unmap_max_blocks =
  4935. clamp(sdebug_unmap_max_blocks, 0U, 0xffffffffU);
  4936. sdebug_unmap_max_desc =
  4937. clamp(sdebug_unmap_max_desc, 0U, 256U);
  4938. sdebug_unmap_granularity =
  4939. clamp(sdebug_unmap_granularity, 1U, 0xffffffffU);
  4940. if (sdebug_unmap_alignment &&
  4941. sdebug_unmap_granularity <=
  4942. sdebug_unmap_alignment) {
  4943. pr_err("ERR: unmap_granularity <= unmap_alignment\n");
  4944. ret = -EINVAL;
  4945. goto free_vm;
  4946. }
  4947. map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
  4948. map_storep = vmalloc(array_size(sizeof(long),
  4949. BITS_TO_LONGS(map_size)));
  4950. pr_info("%lu provisioning blocks\n", map_size);
  4951. if (map_storep == NULL) {
  4952. pr_err("out of mem. (MAP)\n");
  4953. ret = -ENOMEM;
  4954. goto free_vm;
  4955. }
  4956. bitmap_zero(map_storep, map_size);
  4957. /* Map first 1KB for partition table */
  4958. if (sdebug_num_parts)
  4959. map_region(0, 2);
  4960. }
  4961. pseudo_primary = root_device_register("pseudo_0");
  4962. if (IS_ERR(pseudo_primary)) {
  4963. pr_warn("root_device_register() error\n");
  4964. ret = PTR_ERR(pseudo_primary);
  4965. goto free_vm;
  4966. }
  4967. ret = bus_register(&pseudo_lld_bus);
  4968. if (ret < 0) {
  4969. pr_warn("bus_register error: %d\n", ret);
  4970. goto dev_unreg;
  4971. }
  4972. ret = driver_register(&sdebug_driverfs_driver);
  4973. if (ret < 0) {
  4974. pr_warn("driver_register error: %d\n", ret);
  4975. goto bus_unreg;
  4976. }
  4977. host_to_add = sdebug_add_host;
  4978. sdebug_add_host = 0;
  4979. for (k = 0; k < host_to_add; k++) {
  4980. if (sdebug_add_adapter()) {
  4981. pr_err("sdebug_add_adapter failed k=%d\n", k);
  4982. break;
  4983. }
  4984. }
  4985. if (sdebug_verbose)
  4986. pr_info("built %d host(s)\n", sdebug_add_host);
  4987. return 0;
  4988. bus_unreg:
  4989. bus_unregister(&pseudo_lld_bus);
  4990. dev_unreg:
  4991. root_device_unregister(pseudo_primary);
  4992. free_vm:
  4993. vfree(map_storep);
  4994. vfree(dif_storep);
  4995. vfree(fake_storep);
  4996. free_q_arr:
  4997. kfree(sdebug_q_arr);
  4998. return ret;
  4999. }
  5000. static void __exit scsi_debug_exit(void)
  5001. {
  5002. int k = sdebug_add_host;
  5003. stop_all_queued();
  5004. for (; k; k--)
  5005. sdebug_remove_adapter();
  5006. free_all_queued();
  5007. driver_unregister(&sdebug_driverfs_driver);
  5008. bus_unregister(&pseudo_lld_bus);
  5009. root_device_unregister(pseudo_primary);
  5010. vfree(map_storep);
  5011. vfree(dif_storep);
  5012. vfree(fake_storep);
  5013. kfree(sdebug_q_arr);
  5014. }
  5015. device_initcall(scsi_debug_init);
  5016. module_exit(scsi_debug_exit);
  5017. static void sdebug_release_adapter(struct device *dev)
  5018. {
  5019. struct sdebug_host_info *sdbg_host;
  5020. sdbg_host = to_sdebug_host(dev);
  5021. kfree(sdbg_host);
  5022. }
  5023. static int sdebug_add_adapter(void)
  5024. {
  5025. int k, devs_per_host;
  5026. int error = 0;
  5027. struct sdebug_host_info *sdbg_host;
  5028. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  5029. sdbg_host = kzalloc(sizeof(*sdbg_host), GFP_KERNEL);
  5030. if (sdbg_host == NULL) {
  5031. pr_err("out of memory at line %d\n", __LINE__);
  5032. return -ENOMEM;
  5033. }
  5034. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  5035. devs_per_host = sdebug_num_tgts * sdebug_max_luns;
  5036. for (k = 0; k < devs_per_host; k++) {
  5037. sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
  5038. if (!sdbg_devinfo) {
  5039. pr_err("out of memory at line %d\n", __LINE__);
  5040. error = -ENOMEM;
  5041. goto clean;
  5042. }
  5043. }
  5044. spin_lock(&sdebug_host_list_lock);
  5045. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  5046. spin_unlock(&sdebug_host_list_lock);
  5047. sdbg_host->dev.bus = &pseudo_lld_bus;
  5048. sdbg_host->dev.parent = pseudo_primary;
  5049. sdbg_host->dev.release = &sdebug_release_adapter;
  5050. dev_set_name(&sdbg_host->dev, "adapter%d", sdebug_add_host);
  5051. error = device_register(&sdbg_host->dev);
  5052. if (error)
  5053. goto clean;
  5054. ++sdebug_add_host;
  5055. return error;
  5056. clean:
  5057. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  5058. dev_list) {
  5059. list_del(&sdbg_devinfo->dev_list);
  5060. kfree(sdbg_devinfo);
  5061. }
  5062. kfree(sdbg_host);
  5063. return error;
  5064. }
  5065. static void sdebug_remove_adapter(void)
  5066. {
  5067. struct sdebug_host_info *sdbg_host = NULL;
  5068. spin_lock(&sdebug_host_list_lock);
  5069. if (!list_empty(&sdebug_host_list)) {
  5070. sdbg_host = list_entry(sdebug_host_list.prev,
  5071. struct sdebug_host_info, host_list);
  5072. list_del(&sdbg_host->host_list);
  5073. }
  5074. spin_unlock(&sdebug_host_list_lock);
  5075. if (!sdbg_host)
  5076. return;
  5077. device_unregister(&sdbg_host->dev);
  5078. --sdebug_add_host;
  5079. }
  5080. static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)
  5081. {
  5082. int num_in_q = 0;
  5083. struct sdebug_dev_info *devip;
  5084. block_unblock_all_queues(true);
  5085. devip = (struct sdebug_dev_info *)sdev->hostdata;
  5086. if (NULL == devip) {
  5087. block_unblock_all_queues(false);
  5088. return -ENODEV;
  5089. }
  5090. num_in_q = atomic_read(&devip->num_in_q);
  5091. if (qdepth < 1)
  5092. qdepth = 1;
  5093. /* allow to exceed max host qc_arr elements for testing */
  5094. if (qdepth > SDEBUG_CANQUEUE + 10)
  5095. qdepth = SDEBUG_CANQUEUE + 10;
  5096. scsi_change_queue_depth(sdev, qdepth);
  5097. if (SDEBUG_OPT_Q_NOISE & sdebug_opts) {
  5098. sdev_printk(KERN_INFO, sdev, "%s: qdepth=%d, num_in_q=%d\n",
  5099. __func__, qdepth, num_in_q);
  5100. }
  5101. block_unblock_all_queues(false);
  5102. return sdev->queue_depth;
  5103. }
  5104. static bool fake_timeout(struct scsi_cmnd *scp)
  5105. {
  5106. if (0 == (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth))) {
  5107. if (sdebug_every_nth < -1)
  5108. sdebug_every_nth = -1;
  5109. if (SDEBUG_OPT_TIMEOUT & sdebug_opts)
  5110. return true; /* ignore command causing timeout */
  5111. else if (SDEBUG_OPT_MAC_TIMEOUT & sdebug_opts &&
  5112. scsi_medium_access_command(scp))
  5113. return true; /* time out reads and writes */
  5114. }
  5115. return false;
  5116. }
  5117. static bool fake_host_busy(struct scsi_cmnd *scp)
  5118. {
  5119. return (sdebug_opts & SDEBUG_OPT_HOST_BUSY) &&
  5120. (atomic_read(&sdebug_cmnd_count) % abs(sdebug_every_nth)) == 0;
  5121. }
  5122. static int scsi_debug_queuecommand(struct Scsi_Host *shost,
  5123. struct scsi_cmnd *scp)
  5124. {
  5125. u8 sdeb_i;
  5126. struct scsi_device *sdp = scp->device;
  5127. const struct opcode_info_t *oip;
  5128. const struct opcode_info_t *r_oip;
  5129. struct sdebug_dev_info *devip;
  5130. u8 *cmd = scp->cmnd;
  5131. int (*r_pfp)(struct scsi_cmnd *, struct sdebug_dev_info *);
  5132. int (*pfp)(struct scsi_cmnd *, struct sdebug_dev_info *) = NULL;
  5133. int k, na;
  5134. int errsts = 0;
  5135. u32 flags;
  5136. u16 sa;
  5137. u8 opcode = cmd[0];
  5138. bool has_wlun_rl;
  5139. scsi_set_resid(scp, 0);
  5140. if (sdebug_statistics)
  5141. atomic_inc(&sdebug_cmnd_count);
  5142. if (unlikely(sdebug_verbose &&
  5143. !(SDEBUG_OPT_NO_CDB_NOISE & sdebug_opts))) {
  5144. char b[120];
  5145. int n, len, sb;
  5146. len = scp->cmd_len;
  5147. sb = (int)sizeof(b);
  5148. if (len > 32)
  5149. strcpy(b, "too long, over 32 bytes");
  5150. else {
  5151. for (k = 0, n = 0; k < len && n < sb; ++k)
  5152. n += scnprintf(b + n, sb - n, "%02x ",
  5153. (u32)cmd[k]);
  5154. }
  5155. sdev_printk(KERN_INFO, sdp, "%s: tag=%#x, cmd %s\n", my_name,
  5156. blk_mq_unique_tag(scp->request), b);
  5157. }
  5158. if (fake_host_busy(scp))
  5159. return SCSI_MLQUEUE_HOST_BUSY;
  5160. has_wlun_rl = (sdp->lun == SCSI_W_LUN_REPORT_LUNS);
  5161. if (unlikely((sdp->lun >= sdebug_max_luns) && !has_wlun_rl))
  5162. goto err_out;
  5163. sdeb_i = opcode_ind_arr[opcode]; /* fully mapped */
  5164. oip = &opcode_info_arr[sdeb_i]; /* safe if table consistent */
  5165. devip = (struct sdebug_dev_info *)sdp->hostdata;
  5166. if (unlikely(!devip)) {
  5167. devip = find_build_dev_info(sdp);
  5168. if (NULL == devip)
  5169. goto err_out;
  5170. }
  5171. na = oip->num_attached;
  5172. r_pfp = oip->pfp;
  5173. if (na) { /* multiple commands with this opcode */
  5174. r_oip = oip;
  5175. if (FF_SA & r_oip->flags) {
  5176. if (F_SA_LOW & oip->flags)
  5177. sa = 0x1f & cmd[1];
  5178. else
  5179. sa = get_unaligned_be16(cmd + 8);
  5180. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  5181. if (opcode == oip->opcode && sa == oip->sa)
  5182. break;
  5183. }
  5184. } else { /* since no service action only check opcode */
  5185. for (k = 0; k <= na; oip = r_oip->arrp + k++) {
  5186. if (opcode == oip->opcode)
  5187. break;
  5188. }
  5189. }
  5190. if (k > na) {
  5191. if (F_SA_LOW & r_oip->flags)
  5192. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 1, 4);
  5193. else if (F_SA_HIGH & r_oip->flags)
  5194. mk_sense_invalid_fld(scp, SDEB_IN_CDB, 8, 7);
  5195. else
  5196. mk_sense_invalid_opcode(scp);
  5197. goto check_cond;
  5198. }
  5199. } /* else (when na==0) we assume the oip is a match */
  5200. flags = oip->flags;
  5201. if (unlikely(F_INV_OP & flags)) {
  5202. mk_sense_invalid_opcode(scp);
  5203. goto check_cond;
  5204. }
  5205. if (unlikely(has_wlun_rl && !(F_RL_WLUN_OK & flags))) {
  5206. if (sdebug_verbose)
  5207. sdev_printk(KERN_INFO, sdp, "%s: Opcode 0x%x not%s\n",
  5208. my_name, opcode, " supported for wlun");
  5209. mk_sense_invalid_opcode(scp);
  5210. goto check_cond;
  5211. }
  5212. if (unlikely(sdebug_strict)) { /* check cdb against mask */
  5213. u8 rem;
  5214. int j;
  5215. for (k = 1; k < oip->len_mask[0] && k < 16; ++k) {
  5216. rem = ~oip->len_mask[k] & cmd[k];
  5217. if (rem) {
  5218. for (j = 7; j >= 0; --j, rem <<= 1) {
  5219. if (0x80 & rem)
  5220. break;
  5221. }
  5222. mk_sense_invalid_fld(scp, SDEB_IN_CDB, k, j);
  5223. goto check_cond;
  5224. }
  5225. }
  5226. }
  5227. if (unlikely(!(F_SKIP_UA & flags) &&
  5228. find_first_bit(devip->uas_bm,
  5229. SDEBUG_NUM_UAS) != SDEBUG_NUM_UAS)) {
  5230. errsts = make_ua(scp, devip);
  5231. if (errsts)
  5232. goto check_cond;
  5233. }
  5234. if (unlikely((F_M_ACCESS & flags) && atomic_read(&devip->stopped))) {
  5235. mk_sense_buffer(scp, NOT_READY, LOGICAL_UNIT_NOT_READY, 0x2);
  5236. if (sdebug_verbose)
  5237. sdev_printk(KERN_INFO, sdp, "%s reports: Not ready: "
  5238. "%s\n", my_name, "initializing command "
  5239. "required");
  5240. errsts = check_condition_result;
  5241. goto fini;
  5242. }
  5243. if (sdebug_fake_rw && (F_FAKE_RW & flags))
  5244. goto fini;
  5245. if (unlikely(sdebug_every_nth)) {
  5246. if (fake_timeout(scp))
  5247. return 0; /* ignore command: make trouble */
  5248. }
  5249. if (likely(oip->pfp))
  5250. pfp = oip->pfp; /* calls a resp_* function */
  5251. else
  5252. pfp = r_pfp; /* if leaf function ptr NULL, try the root's */
  5253. fini:
  5254. if (F_DELAY_OVERR & flags)
  5255. return schedule_resp(scp, devip, errsts, pfp, 0, 0);
  5256. else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
  5257. sdebug_ndelay > 10000)) {
  5258. /*
  5259. * Skip long delays if ndelay <= 10 microseconds. Otherwise
  5260. * for Start Stop Unit (SSU) want at least 1 second delay and
  5261. * if sdebug_jdelay>1 want a long delay of that many seconds.
  5262. * For Synchronize Cache want 1/20 of SSU's delay.
  5263. */
  5264. int jdelay = (sdebug_jdelay < 2) ? 1 : sdebug_jdelay;
  5265. int denom = (flags & F_SYNC_DELAY) ? 20 : 1;
  5266. jdelay = mult_frac(USER_HZ * jdelay, HZ, denom * USER_HZ);
  5267. return schedule_resp(scp, devip, errsts, pfp, jdelay, 0);
  5268. } else
  5269. return schedule_resp(scp, devip, errsts, pfp, sdebug_jdelay,
  5270. sdebug_ndelay);
  5271. check_cond:
  5272. return schedule_resp(scp, devip, check_condition_result, NULL, 0, 0);
  5273. err_out:
  5274. return schedule_resp(scp, NULL, DID_NO_CONNECT << 16, NULL, 0, 0);
  5275. }
  5276. static struct scsi_host_template sdebug_driver_template = {
  5277. .show_info = scsi_debug_show_info,
  5278. .write_info = scsi_debug_write_info,
  5279. .proc_name = sdebug_proc_name,
  5280. .name = "SCSI DEBUG",
  5281. .info = scsi_debug_info,
  5282. .slave_alloc = scsi_debug_slave_alloc,
  5283. .slave_configure = scsi_debug_slave_configure,
  5284. .slave_destroy = scsi_debug_slave_destroy,
  5285. .ioctl = scsi_debug_ioctl,
  5286. .queuecommand = scsi_debug_queuecommand,
  5287. .change_queue_depth = sdebug_change_qdepth,
  5288. .eh_abort_handler = scsi_debug_abort,
  5289. .eh_device_reset_handler = scsi_debug_device_reset,
  5290. .eh_target_reset_handler = scsi_debug_target_reset,
  5291. .eh_bus_reset_handler = scsi_debug_bus_reset,
  5292. .eh_host_reset_handler = scsi_debug_host_reset,
  5293. .can_queue = SDEBUG_CANQUEUE,
  5294. .this_id = 7,
  5295. .sg_tablesize = SG_MAX_SEGMENTS,
  5296. .cmd_per_lun = DEF_CMD_PER_LUN,
  5297. .max_sectors = -1U,
  5298. .use_clustering = DISABLE_CLUSTERING,
  5299. .module = THIS_MODULE,
  5300. .track_queue_depth = 1,
  5301. };
  5302. static int sdebug_driver_probe(struct device *dev)
  5303. {
  5304. int error = 0;
  5305. struct sdebug_host_info *sdbg_host;
  5306. struct Scsi_Host *hpnt;
  5307. int hprot;
  5308. sdbg_host = to_sdebug_host(dev);
  5309. sdebug_driver_template.can_queue = sdebug_max_queue;
  5310. if (sdebug_clustering)
  5311. sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
  5312. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  5313. if (NULL == hpnt) {
  5314. pr_err("scsi_host_alloc failed\n");
  5315. error = -ENODEV;
  5316. return error;
  5317. }
  5318. if (submit_queues > nr_cpu_ids) {
  5319. pr_warn("%s: trim submit_queues (was %d) to nr_cpu_ids=%u\n",
  5320. my_name, submit_queues, nr_cpu_ids);
  5321. submit_queues = nr_cpu_ids;
  5322. }
  5323. /* Decide whether to tell scsi subsystem that we want mq */
  5324. /* Following should give the same answer for each host */
  5325. if (shost_use_blk_mq(hpnt))
  5326. hpnt->nr_hw_queues = submit_queues;
  5327. sdbg_host->shost = hpnt;
  5328. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  5329. if ((hpnt->this_id >= 0) && (sdebug_num_tgts > hpnt->this_id))
  5330. hpnt->max_id = sdebug_num_tgts + 1;
  5331. else
  5332. hpnt->max_id = sdebug_num_tgts;
  5333. /* = sdebug_max_luns; */
  5334. hpnt->max_lun = SCSI_W_LUN_REPORT_LUNS + 1;
  5335. hprot = 0;
  5336. switch (sdebug_dif) {
  5337. case T10_PI_TYPE1_PROTECTION:
  5338. hprot = SHOST_DIF_TYPE1_PROTECTION;
  5339. if (sdebug_dix)
  5340. hprot |= SHOST_DIX_TYPE1_PROTECTION;
  5341. break;
  5342. case T10_PI_TYPE2_PROTECTION:
  5343. hprot = SHOST_DIF_TYPE2_PROTECTION;
  5344. if (sdebug_dix)
  5345. hprot |= SHOST_DIX_TYPE2_PROTECTION;
  5346. break;
  5347. case T10_PI_TYPE3_PROTECTION:
  5348. hprot = SHOST_DIF_TYPE3_PROTECTION;
  5349. if (sdebug_dix)
  5350. hprot |= SHOST_DIX_TYPE3_PROTECTION;
  5351. break;
  5352. default:
  5353. if (sdebug_dix)
  5354. hprot |= SHOST_DIX_TYPE0_PROTECTION;
  5355. break;
  5356. }
  5357. scsi_host_set_prot(hpnt, hprot);
  5358. if (have_dif_prot || sdebug_dix)
  5359. pr_info("host protection%s%s%s%s%s%s%s\n",
  5360. (hprot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
  5361. (hprot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
  5362. (hprot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
  5363. (hprot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
  5364. (hprot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
  5365. (hprot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
  5366. (hprot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
  5367. if (sdebug_guard == 1)
  5368. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
  5369. else
  5370. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
  5371. sdebug_verbose = !!(SDEBUG_OPT_NOISE & sdebug_opts);
  5372. sdebug_any_injecting_opt = !!(SDEBUG_OPT_ALL_INJECTING & sdebug_opts);
  5373. if (sdebug_every_nth) /* need stats counters for every_nth */
  5374. sdebug_statistics = true;
  5375. error = scsi_add_host(hpnt, &sdbg_host->dev);
  5376. if (error) {
  5377. pr_err("scsi_add_host failed\n");
  5378. error = -ENODEV;
  5379. scsi_host_put(hpnt);
  5380. } else
  5381. scsi_scan_host(hpnt);
  5382. return error;
  5383. }
  5384. static int sdebug_driver_remove(struct device *dev)
  5385. {
  5386. struct sdebug_host_info *sdbg_host;
  5387. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  5388. sdbg_host = to_sdebug_host(dev);
  5389. if (!sdbg_host) {
  5390. pr_err("Unable to locate host info\n");
  5391. return -ENODEV;
  5392. }
  5393. scsi_remove_host(sdbg_host->shost);
  5394. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  5395. dev_list) {
  5396. list_del(&sdbg_devinfo->dev_list);
  5397. kfree(sdbg_devinfo);
  5398. }
  5399. scsi_host_put(sdbg_host->shost);
  5400. return 0;
  5401. }
  5402. static int pseudo_lld_bus_match(struct device *dev,
  5403. struct device_driver *dev_driver)
  5404. {
  5405. return 1;
  5406. }
  5407. static struct bus_type pseudo_lld_bus = {
  5408. .name = "pseudo",
  5409. .match = pseudo_lld_bus_match,
  5410. .probe = sdebug_driver_probe,
  5411. .remove = sdebug_driver_remove,
  5412. .drv_groups = sdebug_drv_groups,
  5413. };