scsi_debug.c 174 KB

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