scsi_debug.c 156 KB

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