scsi_debug.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068
  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 <net/checksum.h>
  44. #include <asm/unaligned.h>
  45. #include <scsi/scsi.h>
  46. #include <scsi/scsi_cmnd.h>
  47. #include <scsi/scsi_device.h>
  48. #include <scsi/scsi_host.h>
  49. #include <scsi/scsicam.h>
  50. #include <scsi/scsi_eh.h>
  51. #include <scsi/scsi_dbg.h>
  52. #include "sd.h"
  53. #include "scsi_logging.h"
  54. #define SCSI_DEBUG_VERSION "1.82"
  55. static const char * scsi_debug_version_date = "20100324";
  56. /* Additional Sense Code (ASC) */
  57. #define NO_ADDITIONAL_SENSE 0x0
  58. #define LOGICAL_UNIT_NOT_READY 0x4
  59. #define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
  60. #define UNRECOVERED_READ_ERR 0x11
  61. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  62. #define INVALID_OPCODE 0x20
  63. #define ADDR_OUT_OF_RANGE 0x21
  64. #define INVALID_COMMAND_OPCODE 0x20
  65. #define INVALID_FIELD_IN_CDB 0x24
  66. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  67. #define POWERON_RESET 0x29
  68. #define SAVING_PARAMS_UNSUP 0x39
  69. #define TRANSPORT_PROBLEM 0x4b
  70. #define THRESHOLD_EXCEEDED 0x5d
  71. #define LOW_POWER_COND_ON 0x5e
  72. /* Additional Sense Code Qualifier (ASCQ) */
  73. #define ACK_NAK_TO 0x3
  74. #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
  75. /* Default values for driver parameters */
  76. #define DEF_NUM_HOST 1
  77. #define DEF_NUM_TGTS 1
  78. #define DEF_MAX_LUNS 1
  79. /* With these defaults, this driver will make 1 host with 1 target
  80. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  81. */
  82. #define DEF_ATO 1
  83. #define DEF_DELAY 1
  84. #define DEF_DEV_SIZE_MB 8
  85. #define DEF_DIF 0
  86. #define DEF_DIX 0
  87. #define DEF_D_SENSE 0
  88. #define DEF_EVERY_NTH 0
  89. #define DEF_FAKE_RW 0
  90. #define DEF_GUARD 0
  91. #define DEF_LBPU 0
  92. #define DEF_LBPWS 0
  93. #define DEF_LBPWS10 0
  94. #define DEF_LBPRZ 1
  95. #define DEF_LOWEST_ALIGNED 0
  96. #define DEF_NO_LUN_0 0
  97. #define DEF_NUM_PARTS 0
  98. #define DEF_OPTS 0
  99. #define DEF_OPT_BLKS 64
  100. #define DEF_PHYSBLK_EXP 0
  101. #define DEF_PTYPE 0
  102. #define DEF_REMOVABLE false
  103. #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
  104. #define DEF_SECTOR_SIZE 512
  105. #define DEF_UNMAP_ALIGNMENT 0
  106. #define DEF_UNMAP_GRANULARITY 1
  107. #define DEF_UNMAP_MAX_BLOCKS 0xFFFFFFFF
  108. #define DEF_UNMAP_MAX_DESC 256
  109. #define DEF_VIRTUAL_GB 0
  110. #define DEF_VPD_USE_HOSTNO 1
  111. #define DEF_WRITESAME_LENGTH 0xFFFF
  112. /* bit mask values for scsi_debug_opts */
  113. #define SCSI_DEBUG_OPT_NOISE 1
  114. #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
  115. #define SCSI_DEBUG_OPT_TIMEOUT 4
  116. #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
  117. #define SCSI_DEBUG_OPT_TRANSPORT_ERR 16
  118. #define SCSI_DEBUG_OPT_DIF_ERR 32
  119. #define SCSI_DEBUG_OPT_DIX_ERR 64
  120. #define SCSI_DEBUG_OPT_MAC_TIMEOUT 128
  121. /* When "every_nth" > 0 then modulo "every_nth" commands:
  122. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  123. * - a RECOVERED_ERROR is simulated on successful read and write
  124. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  125. * - a TRANSPORT_ERROR is simulated on successful read and write
  126. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  127. *
  128. * When "every_nth" < 0 then after "- every_nth" commands:
  129. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  130. * - a RECOVERED_ERROR is simulated on successful read and write
  131. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  132. * - a TRANSPORT_ERROR is simulated on successful read and write
  133. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  134. * This will continue until some other action occurs (e.g. the user
  135. * writing a new value (other than -1 or 1) to every_nth via sysfs).
  136. */
  137. /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  138. * sector on read commands: */
  139. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  140. #define OPT_MEDIUM_ERR_NUM 10 /* number of consecutive medium errs */
  141. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  142. * or "peripheral device" addressing (value 0) */
  143. #define SAM2_LUN_ADDRESS_METHOD 0
  144. #define SAM2_WLUN_REPORT_LUNS 0xc101
  145. /* Can queue up to this number of commands. Typically commands that
  146. * that have a non-zero delay are queued. */
  147. #define SCSI_DEBUG_CANQUEUE 255
  148. static int scsi_debug_add_host = DEF_NUM_HOST;
  149. static int scsi_debug_ato = DEF_ATO;
  150. static int scsi_debug_delay = DEF_DELAY;
  151. static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
  152. static int scsi_debug_dif = DEF_DIF;
  153. static int scsi_debug_dix = DEF_DIX;
  154. static int scsi_debug_dsense = DEF_D_SENSE;
  155. static int scsi_debug_every_nth = DEF_EVERY_NTH;
  156. static int scsi_debug_fake_rw = DEF_FAKE_RW;
  157. static unsigned int scsi_debug_guard = DEF_GUARD;
  158. static int scsi_debug_lowest_aligned = DEF_LOWEST_ALIGNED;
  159. static int scsi_debug_max_luns = DEF_MAX_LUNS;
  160. static int scsi_debug_max_queue = SCSI_DEBUG_CANQUEUE;
  161. static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
  162. static int scsi_debug_no_uld = 0;
  163. static int scsi_debug_num_parts = DEF_NUM_PARTS;
  164. static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  165. static int scsi_debug_opt_blks = DEF_OPT_BLKS;
  166. static int scsi_debug_opts = DEF_OPTS;
  167. static int scsi_debug_physblk_exp = DEF_PHYSBLK_EXP;
  168. static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
  169. static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
  170. static int scsi_debug_sector_size = DEF_SECTOR_SIZE;
  171. static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
  172. static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  173. static unsigned int scsi_debug_lbpu = DEF_LBPU;
  174. static unsigned int scsi_debug_lbpws = DEF_LBPWS;
  175. static unsigned int scsi_debug_lbpws10 = DEF_LBPWS10;
  176. static unsigned int scsi_debug_lbprz = DEF_LBPRZ;
  177. static unsigned int scsi_debug_unmap_alignment = DEF_UNMAP_ALIGNMENT;
  178. static unsigned int scsi_debug_unmap_granularity = DEF_UNMAP_GRANULARITY;
  179. static unsigned int scsi_debug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
  180. static unsigned int scsi_debug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
  181. static unsigned int scsi_debug_write_same_length = DEF_WRITESAME_LENGTH;
  182. static bool scsi_debug_removable = DEF_REMOVABLE;
  183. static bool scsi_debug_clustering;
  184. static int scsi_debug_cmnd_count = 0;
  185. #define DEV_READONLY(TGT) (0)
  186. static unsigned int sdebug_store_sectors;
  187. static sector_t sdebug_capacity; /* in sectors */
  188. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  189. may still need them */
  190. static int sdebug_heads; /* heads per disk */
  191. static int sdebug_cylinders_per; /* cylinders per surface */
  192. static int sdebug_sectors_per; /* sectors per cylinder */
  193. #define SDEBUG_MAX_PARTS 4
  194. #define SDEBUG_SENSE_LEN 32
  195. #define SCSI_DEBUG_MAX_CMD_LEN 32
  196. static unsigned int scsi_debug_lbp(void)
  197. {
  198. return scsi_debug_lbpu | scsi_debug_lbpws | scsi_debug_lbpws10;
  199. }
  200. struct sdebug_dev_info {
  201. struct list_head dev_list;
  202. unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
  203. unsigned int channel;
  204. unsigned int target;
  205. unsigned int lun;
  206. struct sdebug_host_info *sdbg_host;
  207. unsigned int wlun;
  208. char reset;
  209. char stopped;
  210. char used;
  211. };
  212. struct sdebug_host_info {
  213. struct list_head host_list;
  214. struct Scsi_Host *shost;
  215. struct device dev;
  216. struct list_head dev_info_list;
  217. };
  218. #define to_sdebug_host(d) \
  219. container_of(d, struct sdebug_host_info, dev)
  220. static LIST_HEAD(sdebug_host_list);
  221. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  222. typedef void (* done_funct_t) (struct scsi_cmnd *);
  223. struct sdebug_queued_cmd {
  224. int in_use;
  225. struct timer_list cmnd_timer;
  226. done_funct_t done_funct;
  227. struct scsi_cmnd * a_cmnd;
  228. int scsi_result;
  229. };
  230. static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
  231. static unsigned char * fake_storep; /* ramdisk storage */
  232. static struct sd_dif_tuple *dif_storep; /* protection info */
  233. static void *map_storep; /* provisioning map */
  234. static unsigned long map_size;
  235. static int num_aborts = 0;
  236. static int num_dev_resets = 0;
  237. static int num_bus_resets = 0;
  238. static int num_host_resets = 0;
  239. static int dix_writes;
  240. static int dix_reads;
  241. static int dif_errors;
  242. static DEFINE_SPINLOCK(queued_arr_lock);
  243. static DEFINE_RWLOCK(atomic_rw);
  244. static char sdebug_proc_name[] = "scsi_debug";
  245. static struct bus_type pseudo_lld_bus;
  246. static struct device_driver sdebug_driverfs_driver = {
  247. .name = sdebug_proc_name,
  248. .bus = &pseudo_lld_bus,
  249. };
  250. static const int check_condition_result =
  251. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  252. static const int illegal_condition_result =
  253. (DRIVER_SENSE << 24) | (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
  254. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  255. 0, 0, 0x2, 0x4b};
  256. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  257. 0, 0, 0x0, 0x0};
  258. static void *fake_store(unsigned long long lba)
  259. {
  260. lba = do_div(lba, sdebug_store_sectors);
  261. return fake_storep + lba * scsi_debug_sector_size;
  262. }
  263. static struct sd_dif_tuple *dif_store(sector_t sector)
  264. {
  265. sector = do_div(sector, sdebug_store_sectors);
  266. return dif_storep + sector;
  267. }
  268. static int sdebug_add_adapter(void);
  269. static void sdebug_remove_adapter(void);
  270. static void sdebug_max_tgts_luns(void)
  271. {
  272. struct sdebug_host_info *sdbg_host;
  273. struct Scsi_Host *hpnt;
  274. spin_lock(&sdebug_host_list_lock);
  275. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  276. hpnt = sdbg_host->shost;
  277. if ((hpnt->this_id >= 0) &&
  278. (scsi_debug_num_tgts > hpnt->this_id))
  279. hpnt->max_id = scsi_debug_num_tgts + 1;
  280. else
  281. hpnt->max_id = scsi_debug_num_tgts;
  282. /* scsi_debug_max_luns; */
  283. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS;
  284. }
  285. spin_unlock(&sdebug_host_list_lock);
  286. }
  287. static void mk_sense_buffer(struct sdebug_dev_info *devip, int key,
  288. int asc, int asq)
  289. {
  290. unsigned char *sbuff;
  291. sbuff = devip->sense_buff;
  292. memset(sbuff, 0, SDEBUG_SENSE_LEN);
  293. scsi_build_sense_buffer(scsi_debug_dsense, sbuff, key, asc, asq);
  294. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  295. printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
  296. "[0x%x,0x%x,0x%x]\n", key, asc, asq);
  297. }
  298. static void get_data_transfer_info(unsigned char *cmd,
  299. unsigned long long *lba, unsigned int *num,
  300. u32 *ei_lba)
  301. {
  302. *ei_lba = 0;
  303. switch (*cmd) {
  304. case VARIABLE_LENGTH_CMD:
  305. *lba = (u64)cmd[19] | (u64)cmd[18] << 8 |
  306. (u64)cmd[17] << 16 | (u64)cmd[16] << 24 |
  307. (u64)cmd[15] << 32 | (u64)cmd[14] << 40 |
  308. (u64)cmd[13] << 48 | (u64)cmd[12] << 56;
  309. *ei_lba = (u32)cmd[23] | (u32)cmd[22] << 8 |
  310. (u32)cmd[21] << 16 | (u32)cmd[20] << 24;
  311. *num = (u32)cmd[31] | (u32)cmd[30] << 8 | (u32)cmd[29] << 16 |
  312. (u32)cmd[28] << 24;
  313. break;
  314. case WRITE_SAME_16:
  315. case WRITE_16:
  316. case READ_16:
  317. *lba = (u64)cmd[9] | (u64)cmd[8] << 8 |
  318. (u64)cmd[7] << 16 | (u64)cmd[6] << 24 |
  319. (u64)cmd[5] << 32 | (u64)cmd[4] << 40 |
  320. (u64)cmd[3] << 48 | (u64)cmd[2] << 56;
  321. *num = (u32)cmd[13] | (u32)cmd[12] << 8 | (u32)cmd[11] << 16 |
  322. (u32)cmd[10] << 24;
  323. break;
  324. case WRITE_12:
  325. case READ_12:
  326. *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
  327. (u32)cmd[2] << 24;
  328. *num = (u32)cmd[9] | (u32)cmd[8] << 8 | (u32)cmd[7] << 16 |
  329. (u32)cmd[6] << 24;
  330. break;
  331. case WRITE_SAME:
  332. case WRITE_10:
  333. case READ_10:
  334. case XDWRITEREAD_10:
  335. *lba = (u32)cmd[5] | (u32)cmd[4] << 8 | (u32)cmd[3] << 16 |
  336. (u32)cmd[2] << 24;
  337. *num = (u32)cmd[8] | (u32)cmd[7] << 8;
  338. break;
  339. case WRITE_6:
  340. case READ_6:
  341. *lba = (u32)cmd[3] | (u32)cmd[2] << 8 |
  342. (u32)(cmd[1] & 0x1f) << 16;
  343. *num = (0 == cmd[4]) ? 256 : cmd[4];
  344. break;
  345. default:
  346. break;
  347. }
  348. }
  349. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  350. {
  351. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  352. printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
  353. }
  354. return -EINVAL;
  355. /* return -ENOTTY; // correct return but upsets fdisk */
  356. }
  357. static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
  358. struct sdebug_dev_info * devip)
  359. {
  360. if (devip->reset) {
  361. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  362. printk(KERN_INFO "scsi_debug: Reporting Unit "
  363. "attention: power on reset\n");
  364. devip->reset = 0;
  365. mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
  366. return check_condition_result;
  367. }
  368. if ((0 == reset_only) && devip->stopped) {
  369. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  370. printk(KERN_INFO "scsi_debug: Reporting Not "
  371. "ready: initializing command required\n");
  372. mk_sense_buffer(devip, NOT_READY, LOGICAL_UNIT_NOT_READY,
  373. 0x2);
  374. return check_condition_result;
  375. }
  376. return 0;
  377. }
  378. /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
  379. static int fill_from_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  380. int arr_len)
  381. {
  382. int act_len;
  383. struct scsi_data_buffer *sdb = scsi_in(scp);
  384. if (!sdb->length)
  385. return 0;
  386. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_FROM_DEVICE))
  387. return (DID_ERROR << 16);
  388. act_len = sg_copy_from_buffer(sdb->table.sgl, sdb->table.nents,
  389. arr, arr_len);
  390. sdb->resid = scsi_bufflen(scp) - act_len;
  391. return 0;
  392. }
  393. /* Returns number of bytes fetched into 'arr' or -1 if error. */
  394. static int fetch_to_dev_buffer(struct scsi_cmnd *scp, unsigned char *arr,
  395. int arr_len)
  396. {
  397. if (!scsi_bufflen(scp))
  398. return 0;
  399. if (!(scsi_bidi_cmnd(scp) || scp->sc_data_direction == DMA_TO_DEVICE))
  400. return -1;
  401. return scsi_sg_copy_to_buffer(scp, arr, arr_len);
  402. }
  403. static const char * inq_vendor_id = "Linux ";
  404. static const char * inq_product_id = "scsi_debug ";
  405. static const char * inq_product_rev = "0004";
  406. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  407. int target_dev_id, int dev_id_num,
  408. const char * dev_id_str,
  409. int dev_id_str_len)
  410. {
  411. int num, port_a;
  412. char b[32];
  413. port_a = target_dev_id + 1;
  414. /* T10 vendor identifier field format (faked) */
  415. arr[0] = 0x2; /* ASCII */
  416. arr[1] = 0x1;
  417. arr[2] = 0x0;
  418. memcpy(&arr[4], inq_vendor_id, 8);
  419. memcpy(&arr[12], inq_product_id, 16);
  420. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  421. num = 8 + 16 + dev_id_str_len;
  422. arr[3] = num;
  423. num += 4;
  424. if (dev_id_num >= 0) {
  425. /* NAA-5, Logical unit identifier (binary) */
  426. arr[num++] = 0x1; /* binary (not necessarily sas) */
  427. arr[num++] = 0x3; /* PIV=0, lu, naa */
  428. arr[num++] = 0x0;
  429. arr[num++] = 0x8;
  430. arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
  431. arr[num++] = 0x33;
  432. arr[num++] = 0x33;
  433. arr[num++] = 0x30;
  434. arr[num++] = (dev_id_num >> 24);
  435. arr[num++] = (dev_id_num >> 16) & 0xff;
  436. arr[num++] = (dev_id_num >> 8) & 0xff;
  437. arr[num++] = dev_id_num & 0xff;
  438. /* Target relative port number */
  439. arr[num++] = 0x61; /* proto=sas, binary */
  440. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  441. arr[num++] = 0x0; /* reserved */
  442. arr[num++] = 0x4; /* length */
  443. arr[num++] = 0x0; /* reserved */
  444. arr[num++] = 0x0; /* reserved */
  445. arr[num++] = 0x0;
  446. arr[num++] = 0x1; /* relative port A */
  447. }
  448. /* NAA-5, Target port identifier */
  449. arr[num++] = 0x61; /* proto=sas, binary */
  450. arr[num++] = 0x93; /* piv=1, target port, naa */
  451. arr[num++] = 0x0;
  452. arr[num++] = 0x8;
  453. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  454. arr[num++] = 0x22;
  455. arr[num++] = 0x22;
  456. arr[num++] = 0x20;
  457. arr[num++] = (port_a >> 24);
  458. arr[num++] = (port_a >> 16) & 0xff;
  459. arr[num++] = (port_a >> 8) & 0xff;
  460. arr[num++] = port_a & 0xff;
  461. /* NAA-5, Target port group identifier */
  462. arr[num++] = 0x61; /* proto=sas, binary */
  463. arr[num++] = 0x95; /* piv=1, target port group id */
  464. arr[num++] = 0x0;
  465. arr[num++] = 0x4;
  466. arr[num++] = 0;
  467. arr[num++] = 0;
  468. arr[num++] = (port_group_id >> 8) & 0xff;
  469. arr[num++] = port_group_id & 0xff;
  470. /* NAA-5, Target device identifier */
  471. arr[num++] = 0x61; /* proto=sas, binary */
  472. arr[num++] = 0xa3; /* piv=1, target device, naa */
  473. arr[num++] = 0x0;
  474. arr[num++] = 0x8;
  475. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  476. arr[num++] = 0x22;
  477. arr[num++] = 0x22;
  478. arr[num++] = 0x20;
  479. arr[num++] = (target_dev_id >> 24);
  480. arr[num++] = (target_dev_id >> 16) & 0xff;
  481. arr[num++] = (target_dev_id >> 8) & 0xff;
  482. arr[num++] = target_dev_id & 0xff;
  483. /* SCSI name string: Target device identifier */
  484. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  485. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  486. arr[num++] = 0x0;
  487. arr[num++] = 24;
  488. memcpy(arr + num, "naa.52222220", 12);
  489. num += 12;
  490. snprintf(b, sizeof(b), "%08X", target_dev_id);
  491. memcpy(arr + num, b, 8);
  492. num += 8;
  493. memset(arr + num, 0, 4);
  494. num += 4;
  495. return num;
  496. }
  497. static unsigned char vpd84_data[] = {
  498. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  499. 0x22,0x22,0x22,0x0,0xbb,0x1,
  500. 0x22,0x22,0x22,0x0,0xbb,0x2,
  501. };
  502. static int inquiry_evpd_84(unsigned char * arr)
  503. {
  504. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  505. return sizeof(vpd84_data);
  506. }
  507. static int inquiry_evpd_85(unsigned char * arr)
  508. {
  509. int num = 0;
  510. const char * na1 = "https://www.kernel.org/config";
  511. const char * na2 = "http://www.kernel.org/log";
  512. int plen, olen;
  513. arr[num++] = 0x1; /* lu, storage config */
  514. arr[num++] = 0x0; /* reserved */
  515. arr[num++] = 0x0;
  516. olen = strlen(na1);
  517. plen = olen + 1;
  518. if (plen % 4)
  519. plen = ((plen / 4) + 1) * 4;
  520. arr[num++] = plen; /* length, null termianted, padded */
  521. memcpy(arr + num, na1, olen);
  522. memset(arr + num + olen, 0, plen - olen);
  523. num += plen;
  524. arr[num++] = 0x4; /* lu, logging */
  525. arr[num++] = 0x0; /* reserved */
  526. arr[num++] = 0x0;
  527. olen = strlen(na2);
  528. plen = olen + 1;
  529. if (plen % 4)
  530. plen = ((plen / 4) + 1) * 4;
  531. arr[num++] = plen; /* length, null terminated, padded */
  532. memcpy(arr + num, na2, olen);
  533. memset(arr + num + olen, 0, plen - olen);
  534. num += plen;
  535. return num;
  536. }
  537. /* SCSI ports VPD page */
  538. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
  539. {
  540. int num = 0;
  541. int port_a, port_b;
  542. port_a = target_dev_id + 1;
  543. port_b = port_a + 1;
  544. arr[num++] = 0x0; /* reserved */
  545. arr[num++] = 0x0; /* reserved */
  546. arr[num++] = 0x0;
  547. arr[num++] = 0x1; /* relative port 1 (primary) */
  548. memset(arr + num, 0, 6);
  549. num += 6;
  550. arr[num++] = 0x0;
  551. arr[num++] = 12; /* length tp descriptor */
  552. /* naa-5 target port identifier (A) */
  553. arr[num++] = 0x61; /* proto=sas, binary */
  554. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  555. arr[num++] = 0x0; /* reserved */
  556. arr[num++] = 0x8; /* length */
  557. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  558. arr[num++] = 0x22;
  559. arr[num++] = 0x22;
  560. arr[num++] = 0x20;
  561. arr[num++] = (port_a >> 24);
  562. arr[num++] = (port_a >> 16) & 0xff;
  563. arr[num++] = (port_a >> 8) & 0xff;
  564. arr[num++] = port_a & 0xff;
  565. arr[num++] = 0x0; /* reserved */
  566. arr[num++] = 0x0; /* reserved */
  567. arr[num++] = 0x0;
  568. arr[num++] = 0x2; /* relative port 2 (secondary) */
  569. memset(arr + num, 0, 6);
  570. num += 6;
  571. arr[num++] = 0x0;
  572. arr[num++] = 12; /* length tp descriptor */
  573. /* naa-5 target port identifier (B) */
  574. arr[num++] = 0x61; /* proto=sas, binary */
  575. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  576. arr[num++] = 0x0; /* reserved */
  577. arr[num++] = 0x8; /* length */
  578. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  579. arr[num++] = 0x22;
  580. arr[num++] = 0x22;
  581. arr[num++] = 0x20;
  582. arr[num++] = (port_b >> 24);
  583. arr[num++] = (port_b >> 16) & 0xff;
  584. arr[num++] = (port_b >> 8) & 0xff;
  585. arr[num++] = port_b & 0xff;
  586. return num;
  587. }
  588. static unsigned char vpd89_data[] = {
  589. /* from 4th byte */ 0,0,0,0,
  590. 'l','i','n','u','x',' ',' ',' ',
  591. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  592. '1','2','3','4',
  593. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  594. 0xec,0,0,0,
  595. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  596. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  597. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  598. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  599. 0x53,0x41,
  600. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  601. 0x20,0x20,
  602. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  603. 0x10,0x80,
  604. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  605. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  606. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  607. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  608. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  609. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  610. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  611. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  612. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  613. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  614. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  615. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  616. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  617. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  618. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  619. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  620. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  621. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  622. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  623. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  624. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  625. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  626. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  627. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  628. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  629. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  630. };
  631. static int inquiry_evpd_89(unsigned char * arr)
  632. {
  633. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  634. return sizeof(vpd89_data);
  635. }
  636. /* Block limits VPD page (SBC-3) */
  637. static unsigned char vpdb0_data[] = {
  638. /* from 4th byte */ 0,0,0,4, 0,0,0x4,0, 0,0,0,64,
  639. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  640. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  641. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  642. };
  643. static int inquiry_evpd_b0(unsigned char * arr)
  644. {
  645. unsigned int gran;
  646. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  647. /* Optimal transfer length granularity */
  648. gran = 1 << scsi_debug_physblk_exp;
  649. arr[2] = (gran >> 8) & 0xff;
  650. arr[3] = gran & 0xff;
  651. /* Maximum Transfer Length */
  652. if (sdebug_store_sectors > 0x400) {
  653. arr[4] = (sdebug_store_sectors >> 24) & 0xff;
  654. arr[5] = (sdebug_store_sectors >> 16) & 0xff;
  655. arr[6] = (sdebug_store_sectors >> 8) & 0xff;
  656. arr[7] = sdebug_store_sectors & 0xff;
  657. }
  658. /* Optimal Transfer Length */
  659. put_unaligned_be32(scsi_debug_opt_blks, &arr[8]);
  660. if (scsi_debug_lbpu) {
  661. /* Maximum Unmap LBA Count */
  662. put_unaligned_be32(scsi_debug_unmap_max_blocks, &arr[16]);
  663. /* Maximum Unmap Block Descriptor Count */
  664. put_unaligned_be32(scsi_debug_unmap_max_desc, &arr[20]);
  665. }
  666. /* Unmap Granularity Alignment */
  667. if (scsi_debug_unmap_alignment) {
  668. put_unaligned_be32(scsi_debug_unmap_alignment, &arr[28]);
  669. arr[28] |= 0x80; /* UGAVALID */
  670. }
  671. /* Optimal Unmap Granularity */
  672. put_unaligned_be32(scsi_debug_unmap_granularity, &arr[24]);
  673. /* Maximum WRITE SAME Length */
  674. put_unaligned_be64(scsi_debug_write_same_length, &arr[32]);
  675. return 0x3c; /* Mandatory page length for Logical Block Provisioning */
  676. return sizeof(vpdb0_data);
  677. }
  678. /* Block device characteristics VPD page (SBC-3) */
  679. static int inquiry_evpd_b1(unsigned char *arr)
  680. {
  681. memset(arr, 0, 0x3c);
  682. arr[0] = 0;
  683. arr[1] = 1; /* non rotating medium (e.g. solid state) */
  684. arr[2] = 0;
  685. arr[3] = 5; /* less than 1.8" */
  686. return 0x3c;
  687. }
  688. /* Logical block provisioning VPD page (SBC-3) */
  689. static int inquiry_evpd_b2(unsigned char *arr)
  690. {
  691. memset(arr, 0, 0x4);
  692. arr[0] = 0; /* threshold exponent */
  693. if (scsi_debug_lbpu)
  694. arr[1] = 1 << 7;
  695. if (scsi_debug_lbpws)
  696. arr[1] |= 1 << 6;
  697. if (scsi_debug_lbpws10)
  698. arr[1] |= 1 << 5;
  699. if (scsi_debug_lbprz)
  700. arr[1] |= 1 << 2;
  701. return 0x4;
  702. }
  703. #define SDEBUG_LONG_INQ_SZ 96
  704. #define SDEBUG_MAX_INQ_ARR_SZ 584
  705. static int resp_inquiry(struct scsi_cmnd * scp, int target,
  706. struct sdebug_dev_info * devip)
  707. {
  708. unsigned char pq_pdt;
  709. unsigned char * arr;
  710. unsigned char *cmd = (unsigned char *)scp->cmnd;
  711. int alloc_len, n, ret;
  712. alloc_len = (cmd[3] << 8) + cmd[4];
  713. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  714. if (! arr)
  715. return DID_REQUEUE << 16;
  716. if (devip->wlun)
  717. pq_pdt = 0x1e; /* present, wlun */
  718. else if (scsi_debug_no_lun_0 && (0 == devip->lun))
  719. pq_pdt = 0x7f; /* not present, no device type */
  720. else
  721. pq_pdt = (scsi_debug_ptype & 0x1f);
  722. arr[0] = pq_pdt;
  723. if (0x2 & cmd[1]) { /* CMDDT bit set */
  724. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  725. 0);
  726. kfree(arr);
  727. return check_condition_result;
  728. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  729. int lu_id_num, port_group_id, target_dev_id, len;
  730. char lu_id_str[6];
  731. int host_no = devip->sdbg_host->shost->host_no;
  732. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  733. (devip->channel & 0x7f);
  734. if (0 == scsi_debug_vpd_use_hostno)
  735. host_no = 0;
  736. lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
  737. (devip->target * 1000) + devip->lun);
  738. target_dev_id = ((host_no + 1) * 2000) +
  739. (devip->target * 1000) - 3;
  740. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  741. if (0 == cmd[2]) { /* supported vital product data pages */
  742. arr[1] = cmd[2]; /*sanity */
  743. n = 4;
  744. arr[n++] = 0x0; /* this page */
  745. arr[n++] = 0x80; /* unit serial number */
  746. arr[n++] = 0x83; /* device identification */
  747. arr[n++] = 0x84; /* software interface ident. */
  748. arr[n++] = 0x85; /* management network addresses */
  749. arr[n++] = 0x86; /* extended inquiry */
  750. arr[n++] = 0x87; /* mode page policy */
  751. arr[n++] = 0x88; /* SCSI ports */
  752. arr[n++] = 0x89; /* ATA information */
  753. arr[n++] = 0xb0; /* Block limits (SBC) */
  754. arr[n++] = 0xb1; /* Block characteristics (SBC) */
  755. if (scsi_debug_lbp()) /* Logical Block Prov. (SBC) */
  756. arr[n++] = 0xb2;
  757. arr[3] = n - 4; /* number of supported VPD pages */
  758. } else if (0x80 == cmd[2]) { /* unit serial number */
  759. arr[1] = cmd[2]; /*sanity */
  760. arr[3] = len;
  761. memcpy(&arr[4], lu_id_str, len);
  762. } else if (0x83 == cmd[2]) { /* device identification */
  763. arr[1] = cmd[2]; /*sanity */
  764. arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
  765. target_dev_id, lu_id_num,
  766. lu_id_str, len);
  767. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  768. arr[1] = cmd[2]; /*sanity */
  769. arr[3] = inquiry_evpd_84(&arr[4]);
  770. } else if (0x85 == cmd[2]) { /* Management network addresses */
  771. arr[1] = cmd[2]; /*sanity */
  772. arr[3] = inquiry_evpd_85(&arr[4]);
  773. } else if (0x86 == cmd[2]) { /* extended inquiry */
  774. arr[1] = cmd[2]; /*sanity */
  775. arr[3] = 0x3c; /* number of following entries */
  776. if (scsi_debug_dif == SD_DIF_TYPE3_PROTECTION)
  777. arr[4] = 0x4; /* SPT: GRD_CHK:1 */
  778. else if (scsi_debug_dif)
  779. arr[4] = 0x5; /* SPT: GRD_CHK:1, REF_CHK:1 */
  780. else
  781. arr[4] = 0x0; /* no protection stuff */
  782. arr[5] = 0x7; /* head of q, ordered + simple q's */
  783. } else if (0x87 == cmd[2]) { /* mode page policy */
  784. arr[1] = cmd[2]; /*sanity */
  785. arr[3] = 0x8; /* number of following entries */
  786. arr[4] = 0x2; /* disconnect-reconnect mp */
  787. arr[6] = 0x80; /* mlus, shared */
  788. arr[8] = 0x18; /* protocol specific lu */
  789. arr[10] = 0x82; /* mlus, per initiator port */
  790. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  791. arr[1] = cmd[2]; /*sanity */
  792. arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
  793. } else if (0x89 == cmd[2]) { /* ATA information */
  794. arr[1] = cmd[2]; /*sanity */
  795. n = inquiry_evpd_89(&arr[4]);
  796. arr[2] = (n >> 8);
  797. arr[3] = (n & 0xff);
  798. } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
  799. arr[1] = cmd[2]; /*sanity */
  800. arr[3] = inquiry_evpd_b0(&arr[4]);
  801. } else if (0xb1 == cmd[2]) { /* Block characteristics (SBC) */
  802. arr[1] = cmd[2]; /*sanity */
  803. arr[3] = inquiry_evpd_b1(&arr[4]);
  804. } else if (0xb2 == cmd[2]) { /* Logical Block Prov. (SBC) */
  805. arr[1] = cmd[2]; /*sanity */
  806. arr[3] = inquiry_evpd_b2(&arr[4]);
  807. } else {
  808. /* Illegal request, invalid field in cdb */
  809. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  810. INVALID_FIELD_IN_CDB, 0);
  811. kfree(arr);
  812. return check_condition_result;
  813. }
  814. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  815. ret = fill_from_dev_buffer(scp, arr,
  816. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  817. kfree(arr);
  818. return ret;
  819. }
  820. /* drops through here for a standard inquiry */
  821. arr[1] = scsi_debug_removable ? 0x80 : 0; /* Removable disk */
  822. arr[2] = scsi_debug_scsi_level;
  823. arr[3] = 2; /* response_data_format==2 */
  824. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  825. arr[5] = scsi_debug_dif ? 1 : 0; /* PROTECT bit */
  826. if (0 == scsi_debug_vpd_use_hostno)
  827. arr[5] = 0x10; /* claim: implicit TGPS */
  828. arr[6] = 0x10; /* claim: MultiP */
  829. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  830. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  831. memcpy(&arr[8], inq_vendor_id, 8);
  832. memcpy(&arr[16], inq_product_id, 16);
  833. memcpy(&arr[32], inq_product_rev, 4);
  834. /* version descriptors (2 bytes each) follow */
  835. arr[58] = 0x0; arr[59] = 0x77; /* SAM-3 ANSI */
  836. arr[60] = 0x3; arr[61] = 0x14; /* SPC-3 ANSI */
  837. n = 62;
  838. if (scsi_debug_ptype == 0) {
  839. arr[n++] = 0x3; arr[n++] = 0x3d; /* SBC-2 ANSI */
  840. } else if (scsi_debug_ptype == 1) {
  841. arr[n++] = 0x3; arr[n++] = 0x60; /* SSC-2 no version */
  842. }
  843. arr[n++] = 0xc; arr[n++] = 0xf; /* SAS-1.1 rev 10 */
  844. ret = fill_from_dev_buffer(scp, arr,
  845. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  846. kfree(arr);
  847. return ret;
  848. }
  849. static int resp_requests(struct scsi_cmnd * scp,
  850. struct sdebug_dev_info * devip)
  851. {
  852. unsigned char * sbuff;
  853. unsigned char *cmd = (unsigned char *)scp->cmnd;
  854. unsigned char arr[SDEBUG_SENSE_LEN];
  855. int want_dsense;
  856. int len = 18;
  857. memset(arr, 0, sizeof(arr));
  858. if (devip->reset == 1)
  859. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  860. want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
  861. sbuff = devip->sense_buff;
  862. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  863. if (want_dsense) {
  864. arr[0] = 0x72;
  865. arr[1] = 0x0; /* NO_SENSE in sense_key */
  866. arr[2] = THRESHOLD_EXCEEDED;
  867. arr[3] = 0xff; /* TEST set and MRIE==6 */
  868. } else {
  869. arr[0] = 0x70;
  870. arr[2] = 0x0; /* NO_SENSE in sense_key */
  871. arr[7] = 0xa; /* 18 byte sense buffer */
  872. arr[12] = THRESHOLD_EXCEEDED;
  873. arr[13] = 0xff; /* TEST set and MRIE==6 */
  874. }
  875. } else {
  876. memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
  877. if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
  878. /* DESC bit set and sense_buff in fixed format */
  879. memset(arr, 0, sizeof(arr));
  880. arr[0] = 0x72;
  881. arr[1] = sbuff[2]; /* sense key */
  882. arr[2] = sbuff[12]; /* asc */
  883. arr[3] = sbuff[13]; /* ascq */
  884. len = 8;
  885. }
  886. }
  887. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  888. return fill_from_dev_buffer(scp, arr, len);
  889. }
  890. static int resp_start_stop(struct scsi_cmnd * scp,
  891. struct sdebug_dev_info * devip)
  892. {
  893. unsigned char *cmd = (unsigned char *)scp->cmnd;
  894. int power_cond, errsts, start;
  895. if ((errsts = check_readiness(scp, 1, devip)))
  896. return errsts;
  897. power_cond = (cmd[4] & 0xf0) >> 4;
  898. if (power_cond) {
  899. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  900. 0);
  901. return check_condition_result;
  902. }
  903. start = cmd[4] & 1;
  904. if (start == devip->stopped)
  905. devip->stopped = !start;
  906. return 0;
  907. }
  908. static sector_t get_sdebug_capacity(void)
  909. {
  910. if (scsi_debug_virtual_gb > 0)
  911. return (sector_t)scsi_debug_virtual_gb *
  912. (1073741824 / scsi_debug_sector_size);
  913. else
  914. return sdebug_store_sectors;
  915. }
  916. #define SDEBUG_READCAP_ARR_SZ 8
  917. static int resp_readcap(struct scsi_cmnd * scp,
  918. struct sdebug_dev_info * devip)
  919. {
  920. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  921. unsigned int capac;
  922. int errsts;
  923. if ((errsts = check_readiness(scp, 1, devip)))
  924. return errsts;
  925. /* following just in case virtual_gb changed */
  926. sdebug_capacity = get_sdebug_capacity();
  927. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  928. if (sdebug_capacity < 0xffffffff) {
  929. capac = (unsigned int)sdebug_capacity - 1;
  930. arr[0] = (capac >> 24);
  931. arr[1] = (capac >> 16) & 0xff;
  932. arr[2] = (capac >> 8) & 0xff;
  933. arr[3] = capac & 0xff;
  934. } else {
  935. arr[0] = 0xff;
  936. arr[1] = 0xff;
  937. arr[2] = 0xff;
  938. arr[3] = 0xff;
  939. }
  940. arr[6] = (scsi_debug_sector_size >> 8) & 0xff;
  941. arr[7] = scsi_debug_sector_size & 0xff;
  942. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  943. }
  944. #define SDEBUG_READCAP16_ARR_SZ 32
  945. static int resp_readcap16(struct scsi_cmnd * scp,
  946. struct sdebug_dev_info * devip)
  947. {
  948. unsigned char *cmd = (unsigned char *)scp->cmnd;
  949. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  950. unsigned long long capac;
  951. int errsts, k, alloc_len;
  952. if ((errsts = check_readiness(scp, 1, devip)))
  953. return errsts;
  954. alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
  955. + cmd[13]);
  956. /* following just in case virtual_gb changed */
  957. sdebug_capacity = get_sdebug_capacity();
  958. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  959. capac = sdebug_capacity - 1;
  960. for (k = 0; k < 8; ++k, capac >>= 8)
  961. arr[7 - k] = capac & 0xff;
  962. arr[8] = (scsi_debug_sector_size >> 24) & 0xff;
  963. arr[9] = (scsi_debug_sector_size >> 16) & 0xff;
  964. arr[10] = (scsi_debug_sector_size >> 8) & 0xff;
  965. arr[11] = scsi_debug_sector_size & 0xff;
  966. arr[13] = scsi_debug_physblk_exp & 0xf;
  967. arr[14] = (scsi_debug_lowest_aligned >> 8) & 0x3f;
  968. if (scsi_debug_lbp()) {
  969. arr[14] |= 0x80; /* LBPME */
  970. if (scsi_debug_lbprz)
  971. arr[14] |= 0x40; /* LBPRZ */
  972. }
  973. arr[15] = scsi_debug_lowest_aligned & 0xff;
  974. if (scsi_debug_dif) {
  975. arr[12] = (scsi_debug_dif - 1) << 1; /* P_TYPE */
  976. arr[12] |= 1; /* PROT_EN */
  977. }
  978. return fill_from_dev_buffer(scp, arr,
  979. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  980. }
  981. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  982. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  983. struct sdebug_dev_info * devip)
  984. {
  985. unsigned char *cmd = (unsigned char *)scp->cmnd;
  986. unsigned char * arr;
  987. int host_no = devip->sdbg_host->shost->host_no;
  988. int n, ret, alen, rlen;
  989. int port_group_a, port_group_b, port_a, port_b;
  990. alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
  991. + cmd[9]);
  992. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  993. if (! arr)
  994. return DID_REQUEUE << 16;
  995. /*
  996. * EVPD page 0x88 states we have two ports, one
  997. * real and a fake port with no device connected.
  998. * So we create two port groups with one port each
  999. * and set the group with port B to unavailable.
  1000. */
  1001. port_a = 0x1; /* relative port A */
  1002. port_b = 0x2; /* relative port B */
  1003. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1004. (devip->channel & 0x7f);
  1005. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1006. (devip->channel & 0x7f) + 0x80;
  1007. /*
  1008. * The asymmetric access state is cycled according to the host_id.
  1009. */
  1010. n = 4;
  1011. if (0 == scsi_debug_vpd_use_hostno) {
  1012. arr[n++] = host_no % 3; /* Asymm access state */
  1013. arr[n++] = 0x0F; /* claim: all states are supported */
  1014. } else {
  1015. arr[n++] = 0x0; /* Active/Optimized path */
  1016. arr[n++] = 0x01; /* claim: only support active/optimized paths */
  1017. }
  1018. arr[n++] = (port_group_a >> 8) & 0xff;
  1019. arr[n++] = port_group_a & 0xff;
  1020. arr[n++] = 0; /* Reserved */
  1021. arr[n++] = 0; /* Status code */
  1022. arr[n++] = 0; /* Vendor unique */
  1023. arr[n++] = 0x1; /* One port per group */
  1024. arr[n++] = 0; /* Reserved */
  1025. arr[n++] = 0; /* Reserved */
  1026. arr[n++] = (port_a >> 8) & 0xff;
  1027. arr[n++] = port_a & 0xff;
  1028. arr[n++] = 3; /* Port unavailable */
  1029. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1030. arr[n++] = (port_group_b >> 8) & 0xff;
  1031. arr[n++] = port_group_b & 0xff;
  1032. arr[n++] = 0; /* Reserved */
  1033. arr[n++] = 0; /* Status code */
  1034. arr[n++] = 0; /* Vendor unique */
  1035. arr[n++] = 0x1; /* One port per group */
  1036. arr[n++] = 0; /* Reserved */
  1037. arr[n++] = 0; /* Reserved */
  1038. arr[n++] = (port_b >> 8) & 0xff;
  1039. arr[n++] = port_b & 0xff;
  1040. rlen = n - 4;
  1041. arr[0] = (rlen >> 24) & 0xff;
  1042. arr[1] = (rlen >> 16) & 0xff;
  1043. arr[2] = (rlen >> 8) & 0xff;
  1044. arr[3] = rlen & 0xff;
  1045. /*
  1046. * Return the smallest value of either
  1047. * - The allocated length
  1048. * - The constructed command length
  1049. * - The maximum array size
  1050. */
  1051. rlen = min(alen,n);
  1052. ret = fill_from_dev_buffer(scp, arr,
  1053. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1054. kfree(arr);
  1055. return ret;
  1056. }
  1057. /* <<Following mode page info copied from ST318451LW>> */
  1058. static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
  1059. { /* Read-Write Error Recovery page for mode_sense */
  1060. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1061. 5, 0, 0xff, 0xff};
  1062. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1063. if (1 == pcontrol)
  1064. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1065. return sizeof(err_recov_pg);
  1066. }
  1067. static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
  1068. { /* Disconnect-Reconnect page for mode_sense */
  1069. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1070. 0, 0, 0, 0, 0, 0, 0, 0};
  1071. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1072. if (1 == pcontrol)
  1073. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1074. return sizeof(disconnect_pg);
  1075. }
  1076. static int resp_format_pg(unsigned char * p, int pcontrol, int target)
  1077. { /* Format device page for mode_sense */
  1078. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1079. 0, 0, 0, 0, 0, 0, 0, 0,
  1080. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1081. memcpy(p, format_pg, sizeof(format_pg));
  1082. p[10] = (sdebug_sectors_per >> 8) & 0xff;
  1083. p[11] = sdebug_sectors_per & 0xff;
  1084. p[12] = (scsi_debug_sector_size >> 8) & 0xff;
  1085. p[13] = scsi_debug_sector_size & 0xff;
  1086. if (scsi_debug_removable)
  1087. p[20] |= 0x20; /* should agree with INQUIRY */
  1088. if (1 == pcontrol)
  1089. memset(p + 2, 0, sizeof(format_pg) - 2);
  1090. return sizeof(format_pg);
  1091. }
  1092. static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
  1093. { /* Caching page for mode_sense */
  1094. unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1095. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1096. memcpy(p, caching_pg, sizeof(caching_pg));
  1097. if (1 == pcontrol)
  1098. memset(p + 2, 0, sizeof(caching_pg) - 2);
  1099. return sizeof(caching_pg);
  1100. }
  1101. static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
  1102. { /* Control mode page for mode_sense */
  1103. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1104. 0, 0, 0, 0};
  1105. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1106. 0, 0, 0x2, 0x4b};
  1107. if (scsi_debug_dsense)
  1108. ctrl_m_pg[2] |= 0x4;
  1109. else
  1110. ctrl_m_pg[2] &= ~0x4;
  1111. if (scsi_debug_ato)
  1112. ctrl_m_pg[5] |= 0x80; /* ATO=1 */
  1113. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1114. if (1 == pcontrol)
  1115. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1116. else if (2 == pcontrol)
  1117. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1118. return sizeof(ctrl_m_pg);
  1119. }
  1120. static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
  1121. { /* Informational Exceptions control mode page for mode_sense */
  1122. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1123. 0, 0, 0x0, 0x0};
  1124. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1125. 0, 0, 0x0, 0x0};
  1126. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1127. if (1 == pcontrol)
  1128. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1129. else if (2 == pcontrol)
  1130. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1131. return sizeof(iec_m_pg);
  1132. }
  1133. static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
  1134. { /* SAS SSP mode page - short format for mode_sense */
  1135. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1136. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1137. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1138. if (1 == pcontrol)
  1139. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1140. return sizeof(sas_sf_m_pg);
  1141. }
  1142. static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
  1143. int target_dev_id)
  1144. { /* SAS phy control and discover mode page for mode_sense */
  1145. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1146. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1147. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1148. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1149. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1150. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1151. 0, 0, 0, 0, 0, 0, 0, 0,
  1152. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1153. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1154. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1155. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1156. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1157. 0, 0, 0, 0, 0, 0, 0, 0,
  1158. };
  1159. int port_a, port_b;
  1160. port_a = target_dev_id + 1;
  1161. port_b = port_a + 1;
  1162. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1163. p[20] = (port_a >> 24);
  1164. p[21] = (port_a >> 16) & 0xff;
  1165. p[22] = (port_a >> 8) & 0xff;
  1166. p[23] = port_a & 0xff;
  1167. p[48 + 20] = (port_b >> 24);
  1168. p[48 + 21] = (port_b >> 16) & 0xff;
  1169. p[48 + 22] = (port_b >> 8) & 0xff;
  1170. p[48 + 23] = port_b & 0xff;
  1171. if (1 == pcontrol)
  1172. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1173. return sizeof(sas_pcd_m_pg);
  1174. }
  1175. static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
  1176. { /* SAS SSP shared protocol specific port mode subpage */
  1177. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1178. 0, 0, 0, 0, 0, 0, 0, 0,
  1179. };
  1180. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1181. if (1 == pcontrol)
  1182. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1183. return sizeof(sas_sha_m_pg);
  1184. }
  1185. #define SDEBUG_MAX_MSENSE_SZ 256
  1186. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  1187. struct sdebug_dev_info * devip)
  1188. {
  1189. unsigned char dbd, llbaa;
  1190. int pcontrol, pcode, subpcode, bd_len;
  1191. unsigned char dev_spec;
  1192. int k, alloc_len, msense_6, offset, len, errsts, target_dev_id;
  1193. unsigned char * ap;
  1194. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1195. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1196. if ((errsts = check_readiness(scp, 1, devip)))
  1197. return errsts;
  1198. dbd = !!(cmd[1] & 0x8);
  1199. pcontrol = (cmd[2] & 0xc0) >> 6;
  1200. pcode = cmd[2] & 0x3f;
  1201. subpcode = cmd[3];
  1202. msense_6 = (MODE_SENSE == cmd[0]);
  1203. llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
  1204. if ((0 == scsi_debug_ptype) && (0 == dbd))
  1205. bd_len = llbaa ? 16 : 8;
  1206. else
  1207. bd_len = 0;
  1208. alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
  1209. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1210. if (0x3 == pcontrol) { /* Saving values not supported */
  1211. mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
  1212. 0);
  1213. return check_condition_result;
  1214. }
  1215. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1216. (devip->target * 1000) - 3;
  1217. /* set DPOFUA bit for disks */
  1218. if (0 == scsi_debug_ptype)
  1219. dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
  1220. else
  1221. dev_spec = 0x0;
  1222. if (msense_6) {
  1223. arr[2] = dev_spec;
  1224. arr[3] = bd_len;
  1225. offset = 4;
  1226. } else {
  1227. arr[3] = dev_spec;
  1228. if (16 == bd_len)
  1229. arr[4] = 0x1; /* set LONGLBA bit */
  1230. arr[7] = bd_len; /* assume 255 or less */
  1231. offset = 8;
  1232. }
  1233. ap = arr + offset;
  1234. if ((bd_len > 0) && (!sdebug_capacity))
  1235. sdebug_capacity = get_sdebug_capacity();
  1236. if (8 == bd_len) {
  1237. if (sdebug_capacity > 0xfffffffe) {
  1238. ap[0] = 0xff;
  1239. ap[1] = 0xff;
  1240. ap[2] = 0xff;
  1241. ap[3] = 0xff;
  1242. } else {
  1243. ap[0] = (sdebug_capacity >> 24) & 0xff;
  1244. ap[1] = (sdebug_capacity >> 16) & 0xff;
  1245. ap[2] = (sdebug_capacity >> 8) & 0xff;
  1246. ap[3] = sdebug_capacity & 0xff;
  1247. }
  1248. ap[6] = (scsi_debug_sector_size >> 8) & 0xff;
  1249. ap[7] = scsi_debug_sector_size & 0xff;
  1250. offset += bd_len;
  1251. ap = arr + offset;
  1252. } else if (16 == bd_len) {
  1253. unsigned long long capac = sdebug_capacity;
  1254. for (k = 0; k < 8; ++k, capac >>= 8)
  1255. ap[7 - k] = capac & 0xff;
  1256. ap[12] = (scsi_debug_sector_size >> 24) & 0xff;
  1257. ap[13] = (scsi_debug_sector_size >> 16) & 0xff;
  1258. ap[14] = (scsi_debug_sector_size >> 8) & 0xff;
  1259. ap[15] = scsi_debug_sector_size & 0xff;
  1260. offset += bd_len;
  1261. ap = arr + offset;
  1262. }
  1263. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1264. /* TODO: Control Extension page */
  1265. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1266. 0);
  1267. return check_condition_result;
  1268. }
  1269. switch (pcode) {
  1270. case 0x1: /* Read-Write error recovery page, direct access */
  1271. len = resp_err_recov_pg(ap, pcontrol, target);
  1272. offset += len;
  1273. break;
  1274. case 0x2: /* Disconnect-Reconnect page, all devices */
  1275. len = resp_disconnect_pg(ap, pcontrol, target);
  1276. offset += len;
  1277. break;
  1278. case 0x3: /* Format device page, direct access */
  1279. len = resp_format_pg(ap, pcontrol, target);
  1280. offset += len;
  1281. break;
  1282. case 0x8: /* Caching page, direct access */
  1283. len = resp_caching_pg(ap, pcontrol, target);
  1284. offset += len;
  1285. break;
  1286. case 0xa: /* Control Mode page, all devices */
  1287. len = resp_ctrl_m_pg(ap, pcontrol, target);
  1288. offset += len;
  1289. break;
  1290. case 0x19: /* if spc==1 then sas phy, control+discover */
  1291. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  1292. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1293. INVALID_FIELD_IN_CDB, 0);
  1294. return check_condition_result;
  1295. }
  1296. len = 0;
  1297. if ((0x0 == subpcode) || (0xff == subpcode))
  1298. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1299. if ((0x1 == subpcode) || (0xff == subpcode))
  1300. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  1301. target_dev_id);
  1302. if ((0x2 == subpcode) || (0xff == subpcode))
  1303. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1304. offset += len;
  1305. break;
  1306. case 0x1c: /* Informational Exceptions Mode page, all devices */
  1307. len = resp_iec_m_pg(ap, pcontrol, target);
  1308. offset += len;
  1309. break;
  1310. case 0x3f: /* Read all Mode pages */
  1311. if ((0 == subpcode) || (0xff == subpcode)) {
  1312. len = resp_err_recov_pg(ap, pcontrol, target);
  1313. len += resp_disconnect_pg(ap + len, pcontrol, target);
  1314. len += resp_format_pg(ap + len, pcontrol, target);
  1315. len += resp_caching_pg(ap + len, pcontrol, target);
  1316. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  1317. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1318. if (0xff == subpcode) {
  1319. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  1320. target, target_dev_id);
  1321. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1322. }
  1323. len += resp_iec_m_pg(ap + len, pcontrol, target);
  1324. } else {
  1325. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1326. INVALID_FIELD_IN_CDB, 0);
  1327. return check_condition_result;
  1328. }
  1329. offset += len;
  1330. break;
  1331. default:
  1332. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1333. 0);
  1334. return check_condition_result;
  1335. }
  1336. if (msense_6)
  1337. arr[0] = offset - 1;
  1338. else {
  1339. arr[0] = ((offset - 2) >> 8) & 0xff;
  1340. arr[1] = (offset - 2) & 0xff;
  1341. }
  1342. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  1343. }
  1344. #define SDEBUG_MAX_MSELECT_SZ 512
  1345. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  1346. struct sdebug_dev_info * devip)
  1347. {
  1348. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  1349. int param_len, res, errsts, mpage;
  1350. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  1351. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1352. if ((errsts = check_readiness(scp, 1, devip)))
  1353. return errsts;
  1354. memset(arr, 0, sizeof(arr));
  1355. pf = cmd[1] & 0x10;
  1356. sp = cmd[1] & 0x1;
  1357. param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
  1358. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  1359. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1360. INVALID_FIELD_IN_CDB, 0);
  1361. return check_condition_result;
  1362. }
  1363. res = fetch_to_dev_buffer(scp, arr, param_len);
  1364. if (-1 == res)
  1365. return (DID_ERROR << 16);
  1366. else if ((res < param_len) &&
  1367. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1368. printk(KERN_INFO "scsi_debug: mode_select: cdb indicated=%d, "
  1369. " IO sent=%d bytes\n", param_len, res);
  1370. md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
  1371. bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
  1372. if (md_len > 2) {
  1373. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1374. INVALID_FIELD_IN_PARAM_LIST, 0);
  1375. return check_condition_result;
  1376. }
  1377. off = bd_len + (mselect6 ? 4 : 8);
  1378. mpage = arr[off] & 0x3f;
  1379. ps = !!(arr[off] & 0x80);
  1380. if (ps) {
  1381. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1382. INVALID_FIELD_IN_PARAM_LIST, 0);
  1383. return check_condition_result;
  1384. }
  1385. spf = !!(arr[off] & 0x40);
  1386. pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
  1387. (arr[off + 1] + 2);
  1388. if ((pg_len + off) > param_len) {
  1389. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1390. PARAMETER_LIST_LENGTH_ERR, 0);
  1391. return check_condition_result;
  1392. }
  1393. switch (mpage) {
  1394. case 0xa: /* Control Mode page */
  1395. if (ctrl_m_pg[1] == arr[off + 1]) {
  1396. memcpy(ctrl_m_pg + 2, arr + off + 2,
  1397. sizeof(ctrl_m_pg) - 2);
  1398. scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
  1399. return 0;
  1400. }
  1401. break;
  1402. case 0x1c: /* Informational Exceptions Mode page */
  1403. if (iec_m_pg[1] == arr[off + 1]) {
  1404. memcpy(iec_m_pg + 2, arr + off + 2,
  1405. sizeof(iec_m_pg) - 2);
  1406. return 0;
  1407. }
  1408. break;
  1409. default:
  1410. break;
  1411. }
  1412. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1413. INVALID_FIELD_IN_PARAM_LIST, 0);
  1414. return check_condition_result;
  1415. }
  1416. static int resp_temp_l_pg(unsigned char * arr)
  1417. {
  1418. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  1419. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  1420. };
  1421. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  1422. return sizeof(temp_l_pg);
  1423. }
  1424. static int resp_ie_l_pg(unsigned char * arr)
  1425. {
  1426. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  1427. };
  1428. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  1429. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  1430. arr[4] = THRESHOLD_EXCEEDED;
  1431. arr[5] = 0xff;
  1432. }
  1433. return sizeof(ie_l_pg);
  1434. }
  1435. #define SDEBUG_MAX_LSENSE_SZ 512
  1436. static int resp_log_sense(struct scsi_cmnd * scp,
  1437. struct sdebug_dev_info * devip)
  1438. {
  1439. int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n;
  1440. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  1441. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1442. if ((errsts = check_readiness(scp, 1, devip)))
  1443. return errsts;
  1444. memset(arr, 0, sizeof(arr));
  1445. ppc = cmd[1] & 0x2;
  1446. sp = cmd[1] & 0x1;
  1447. if (ppc || sp) {
  1448. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1449. INVALID_FIELD_IN_CDB, 0);
  1450. return check_condition_result;
  1451. }
  1452. pcontrol = (cmd[2] & 0xc0) >> 6;
  1453. pcode = cmd[2] & 0x3f;
  1454. subpcode = cmd[3] & 0xff;
  1455. alloc_len = (cmd[7] << 8) + cmd[8];
  1456. arr[0] = pcode;
  1457. if (0 == subpcode) {
  1458. switch (pcode) {
  1459. case 0x0: /* Supported log pages log page */
  1460. n = 4;
  1461. arr[n++] = 0x0; /* this page */
  1462. arr[n++] = 0xd; /* Temperature */
  1463. arr[n++] = 0x2f; /* Informational exceptions */
  1464. arr[3] = n - 4;
  1465. break;
  1466. case 0xd: /* Temperature log page */
  1467. arr[3] = resp_temp_l_pg(arr + 4);
  1468. break;
  1469. case 0x2f: /* Informational exceptions log page */
  1470. arr[3] = resp_ie_l_pg(arr + 4);
  1471. break;
  1472. default:
  1473. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1474. INVALID_FIELD_IN_CDB, 0);
  1475. return check_condition_result;
  1476. }
  1477. } else if (0xff == subpcode) {
  1478. arr[0] |= 0x40;
  1479. arr[1] = subpcode;
  1480. switch (pcode) {
  1481. case 0x0: /* Supported log pages and subpages log page */
  1482. n = 4;
  1483. arr[n++] = 0x0;
  1484. arr[n++] = 0x0; /* 0,0 page */
  1485. arr[n++] = 0x0;
  1486. arr[n++] = 0xff; /* this page */
  1487. arr[n++] = 0xd;
  1488. arr[n++] = 0x0; /* Temperature */
  1489. arr[n++] = 0x2f;
  1490. arr[n++] = 0x0; /* Informational exceptions */
  1491. arr[3] = n - 4;
  1492. break;
  1493. case 0xd: /* Temperature subpages */
  1494. n = 4;
  1495. arr[n++] = 0xd;
  1496. arr[n++] = 0x0; /* Temperature */
  1497. arr[3] = n - 4;
  1498. break;
  1499. case 0x2f: /* Informational exceptions subpages */
  1500. n = 4;
  1501. arr[n++] = 0x2f;
  1502. arr[n++] = 0x0; /* Informational exceptions */
  1503. arr[3] = n - 4;
  1504. break;
  1505. default:
  1506. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1507. INVALID_FIELD_IN_CDB, 0);
  1508. return check_condition_result;
  1509. }
  1510. } else {
  1511. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1512. INVALID_FIELD_IN_CDB, 0);
  1513. return check_condition_result;
  1514. }
  1515. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  1516. return fill_from_dev_buffer(scp, arr,
  1517. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1518. }
  1519. static int check_device_access_params(struct sdebug_dev_info *devi,
  1520. unsigned long long lba, unsigned int num)
  1521. {
  1522. if (lba + num > sdebug_capacity) {
  1523. mk_sense_buffer(devi, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE, 0);
  1524. return check_condition_result;
  1525. }
  1526. /* transfer length excessive (tie in to block limits VPD page) */
  1527. if (num > sdebug_store_sectors) {
  1528. mk_sense_buffer(devi, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB, 0);
  1529. return check_condition_result;
  1530. }
  1531. return 0;
  1532. }
  1533. /* Returns number of bytes copied or -1 if error. */
  1534. static int do_device_access(struct scsi_cmnd *scmd,
  1535. struct sdebug_dev_info *devi,
  1536. unsigned long long lba, unsigned int num, int write)
  1537. {
  1538. int ret;
  1539. unsigned long long block, rest = 0;
  1540. struct scsi_data_buffer *sdb;
  1541. enum dma_data_direction dir;
  1542. size_t (*func)(struct scatterlist *, unsigned int, void *, size_t,
  1543. off_t);
  1544. if (write) {
  1545. sdb = scsi_out(scmd);
  1546. dir = DMA_TO_DEVICE;
  1547. func = sg_pcopy_to_buffer;
  1548. } else {
  1549. sdb = scsi_in(scmd);
  1550. dir = DMA_FROM_DEVICE;
  1551. func = sg_pcopy_from_buffer;
  1552. }
  1553. if (!sdb->length)
  1554. return 0;
  1555. if (!(scsi_bidi_cmnd(scmd) || scmd->sc_data_direction == dir))
  1556. return -1;
  1557. block = do_div(lba, sdebug_store_sectors);
  1558. if (block + num > sdebug_store_sectors)
  1559. rest = block + num - sdebug_store_sectors;
  1560. ret = func(sdb->table.sgl, sdb->table.nents,
  1561. fake_storep + (block * scsi_debug_sector_size),
  1562. (num - rest) * scsi_debug_sector_size, 0);
  1563. if (ret != (num - rest) * scsi_debug_sector_size)
  1564. return ret;
  1565. if (rest) {
  1566. ret += func(sdb->table.sgl, sdb->table.nents,
  1567. fake_storep, rest * scsi_debug_sector_size,
  1568. (num - rest) * scsi_debug_sector_size);
  1569. }
  1570. return ret;
  1571. }
  1572. static __be16 dif_compute_csum(const void *buf, int len)
  1573. {
  1574. __be16 csum;
  1575. if (scsi_debug_guard)
  1576. csum = (__force __be16)ip_compute_csum(buf, len);
  1577. else
  1578. csum = cpu_to_be16(crc_t10dif(buf, len));
  1579. return csum;
  1580. }
  1581. static int dif_verify(struct sd_dif_tuple *sdt, const void *data,
  1582. sector_t sector, u32 ei_lba)
  1583. {
  1584. __be16 csum = dif_compute_csum(data, scsi_debug_sector_size);
  1585. if (sdt->guard_tag != csum) {
  1586. pr_err("%s: GUARD check failed on sector %lu rcvd 0x%04x, data 0x%04x\n",
  1587. __func__,
  1588. (unsigned long)sector,
  1589. be16_to_cpu(sdt->guard_tag),
  1590. be16_to_cpu(csum));
  1591. return 0x01;
  1592. }
  1593. if (scsi_debug_dif == SD_DIF_TYPE1_PROTECTION &&
  1594. be32_to_cpu(sdt->ref_tag) != (sector & 0xffffffff)) {
  1595. pr_err("%s: REF check failed on sector %lu\n",
  1596. __func__, (unsigned long)sector);
  1597. return 0x03;
  1598. }
  1599. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  1600. be32_to_cpu(sdt->ref_tag) != ei_lba) {
  1601. pr_err("%s: REF check failed on sector %lu\n",
  1602. __func__, (unsigned long)sector);
  1603. return 0x03;
  1604. }
  1605. return 0;
  1606. }
  1607. static void dif_copy_prot(struct scsi_cmnd *SCpnt, sector_t sector,
  1608. unsigned int sectors, bool read)
  1609. {
  1610. size_t resid;
  1611. void *paddr;
  1612. const void *dif_store_end = dif_storep + sdebug_store_sectors;
  1613. struct sg_mapping_iter miter;
  1614. /* Bytes of protection data to copy into sgl */
  1615. resid = sectors * sizeof(*dif_storep);
  1616. sg_miter_start(&miter, scsi_prot_sglist(SCpnt),
  1617. scsi_prot_sg_count(SCpnt), SG_MITER_ATOMIC |
  1618. (read ? SG_MITER_TO_SG : SG_MITER_FROM_SG));
  1619. while (sg_miter_next(&miter) && resid > 0) {
  1620. size_t len = min(miter.length, resid);
  1621. void *start = dif_store(sector);
  1622. size_t rest = 0;
  1623. if (dif_store_end < start + len)
  1624. rest = start + len - dif_store_end;
  1625. paddr = miter.addr;
  1626. if (read)
  1627. memcpy(paddr, start, len - rest);
  1628. else
  1629. memcpy(start, paddr, len - rest);
  1630. if (rest) {
  1631. if (read)
  1632. memcpy(paddr + len - rest, dif_storep, rest);
  1633. else
  1634. memcpy(dif_storep, paddr + len - rest, rest);
  1635. }
  1636. sector += len / sizeof(*dif_storep);
  1637. resid -= len;
  1638. }
  1639. sg_miter_stop(&miter);
  1640. }
  1641. static int prot_verify_read(struct scsi_cmnd *SCpnt, sector_t start_sec,
  1642. unsigned int sectors, u32 ei_lba)
  1643. {
  1644. unsigned int i;
  1645. struct sd_dif_tuple *sdt;
  1646. sector_t sector;
  1647. for (i = 0; i < sectors; i++, ei_lba++) {
  1648. int ret;
  1649. sector = start_sec + i;
  1650. sdt = dif_store(sector);
  1651. if (sdt->app_tag == cpu_to_be16(0xffff))
  1652. continue;
  1653. ret = dif_verify(sdt, fake_store(sector), sector, ei_lba);
  1654. if (ret) {
  1655. dif_errors++;
  1656. return ret;
  1657. }
  1658. }
  1659. dif_copy_prot(SCpnt, start_sec, sectors, true);
  1660. dix_reads++;
  1661. return 0;
  1662. }
  1663. static int resp_read(struct scsi_cmnd *SCpnt, unsigned long long lba,
  1664. unsigned int num, struct sdebug_dev_info *devip,
  1665. u32 ei_lba)
  1666. {
  1667. unsigned long iflags;
  1668. int ret;
  1669. ret = check_device_access_params(devip, lba, num);
  1670. if (ret)
  1671. return ret;
  1672. if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
  1673. (lba <= (OPT_MEDIUM_ERR_ADDR + OPT_MEDIUM_ERR_NUM - 1)) &&
  1674. ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
  1675. /* claim unrecoverable read error */
  1676. mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR, 0);
  1677. /* set info field and valid bit for fixed descriptor */
  1678. if (0x70 == (devip->sense_buff[0] & 0x7f)) {
  1679. devip->sense_buff[0] |= 0x80; /* Valid bit */
  1680. ret = (lba < OPT_MEDIUM_ERR_ADDR)
  1681. ? OPT_MEDIUM_ERR_ADDR : (int)lba;
  1682. devip->sense_buff[3] = (ret >> 24) & 0xff;
  1683. devip->sense_buff[4] = (ret >> 16) & 0xff;
  1684. devip->sense_buff[5] = (ret >> 8) & 0xff;
  1685. devip->sense_buff[6] = ret & 0xff;
  1686. }
  1687. scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
  1688. return check_condition_result;
  1689. }
  1690. read_lock_irqsave(&atomic_rw, iflags);
  1691. /* DIX + T10 DIF */
  1692. if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) {
  1693. int prot_ret = prot_verify_read(SCpnt, lba, num, ei_lba);
  1694. if (prot_ret) {
  1695. read_unlock_irqrestore(&atomic_rw, iflags);
  1696. mk_sense_buffer(devip, ABORTED_COMMAND, 0x10, prot_ret);
  1697. return illegal_condition_result;
  1698. }
  1699. }
  1700. ret = do_device_access(SCpnt, devip, lba, num, 0);
  1701. read_unlock_irqrestore(&atomic_rw, iflags);
  1702. if (ret == -1)
  1703. return DID_ERROR << 16;
  1704. scsi_in(SCpnt)->resid = scsi_bufflen(SCpnt) - ret;
  1705. return 0;
  1706. }
  1707. void dump_sector(unsigned char *buf, int len)
  1708. {
  1709. int i, j;
  1710. printk(KERN_ERR ">>> Sector Dump <<<\n");
  1711. for (i = 0 ; i < len ; i += 16) {
  1712. printk(KERN_ERR "%04d: ", i);
  1713. for (j = 0 ; j < 16 ; j++) {
  1714. unsigned char c = buf[i+j];
  1715. if (c >= 0x20 && c < 0x7e)
  1716. printk(" %c ", buf[i+j]);
  1717. else
  1718. printk("%02x ", buf[i+j]);
  1719. }
  1720. printk("\n");
  1721. }
  1722. }
  1723. static int prot_verify_write(struct scsi_cmnd *SCpnt, sector_t start_sec,
  1724. unsigned int sectors, u32 ei_lba)
  1725. {
  1726. int ret;
  1727. struct sd_dif_tuple *sdt;
  1728. void *daddr;
  1729. sector_t sector = start_sec;
  1730. int ppage_offset;
  1731. int dpage_offset;
  1732. struct sg_mapping_iter diter;
  1733. struct sg_mapping_iter piter;
  1734. BUG_ON(scsi_sg_count(SCpnt) == 0);
  1735. BUG_ON(scsi_prot_sg_count(SCpnt) == 0);
  1736. sg_miter_start(&piter, scsi_prot_sglist(SCpnt),
  1737. scsi_prot_sg_count(SCpnt),
  1738. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  1739. sg_miter_start(&diter, scsi_sglist(SCpnt), scsi_sg_count(SCpnt),
  1740. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  1741. /* For each protection page */
  1742. while (sg_miter_next(&piter)) {
  1743. dpage_offset = 0;
  1744. if (WARN_ON(!sg_miter_next(&diter))) {
  1745. ret = 0x01;
  1746. goto out;
  1747. }
  1748. for (ppage_offset = 0; ppage_offset < piter.length;
  1749. ppage_offset += sizeof(struct sd_dif_tuple)) {
  1750. /* If we're at the end of the current
  1751. * data page advance to the next one
  1752. */
  1753. if (dpage_offset >= diter.length) {
  1754. if (WARN_ON(!sg_miter_next(&diter))) {
  1755. ret = 0x01;
  1756. goto out;
  1757. }
  1758. dpage_offset = 0;
  1759. }
  1760. sdt = piter.addr + ppage_offset;
  1761. daddr = diter.addr + dpage_offset;
  1762. ret = dif_verify(sdt, daddr, sector, ei_lba);
  1763. if (ret) {
  1764. dump_sector(daddr, scsi_debug_sector_size);
  1765. goto out;
  1766. }
  1767. sector++;
  1768. ei_lba++;
  1769. dpage_offset += scsi_debug_sector_size;
  1770. }
  1771. diter.consumed = dpage_offset;
  1772. sg_miter_stop(&diter);
  1773. }
  1774. sg_miter_stop(&piter);
  1775. dif_copy_prot(SCpnt, start_sec, sectors, false);
  1776. dix_writes++;
  1777. return 0;
  1778. out:
  1779. dif_errors++;
  1780. sg_miter_stop(&diter);
  1781. sg_miter_stop(&piter);
  1782. return ret;
  1783. }
  1784. static unsigned long lba_to_map_index(sector_t lba)
  1785. {
  1786. if (scsi_debug_unmap_alignment) {
  1787. lba += scsi_debug_unmap_granularity -
  1788. scsi_debug_unmap_alignment;
  1789. }
  1790. do_div(lba, scsi_debug_unmap_granularity);
  1791. return lba;
  1792. }
  1793. static sector_t map_index_to_lba(unsigned long index)
  1794. {
  1795. sector_t lba = index * scsi_debug_unmap_granularity;
  1796. if (scsi_debug_unmap_alignment) {
  1797. lba -= scsi_debug_unmap_granularity -
  1798. scsi_debug_unmap_alignment;
  1799. }
  1800. return lba;
  1801. }
  1802. static unsigned int map_state(sector_t lba, unsigned int *num)
  1803. {
  1804. sector_t end;
  1805. unsigned int mapped;
  1806. unsigned long index;
  1807. unsigned long next;
  1808. index = lba_to_map_index(lba);
  1809. mapped = test_bit(index, map_storep);
  1810. if (mapped)
  1811. next = find_next_zero_bit(map_storep, map_size, index);
  1812. else
  1813. next = find_next_bit(map_storep, map_size, index);
  1814. end = min_t(sector_t, sdebug_store_sectors, map_index_to_lba(next));
  1815. *num = end - lba;
  1816. return mapped;
  1817. }
  1818. static void map_region(sector_t lba, unsigned int len)
  1819. {
  1820. sector_t end = lba + len;
  1821. while (lba < end) {
  1822. unsigned long index = lba_to_map_index(lba);
  1823. if (index < map_size)
  1824. set_bit(index, map_storep);
  1825. lba = map_index_to_lba(index + 1);
  1826. }
  1827. }
  1828. static void unmap_region(sector_t lba, unsigned int len)
  1829. {
  1830. sector_t end = lba + len;
  1831. while (lba < end) {
  1832. unsigned long index = lba_to_map_index(lba);
  1833. if (lba == map_index_to_lba(index) &&
  1834. lba + scsi_debug_unmap_granularity <= end &&
  1835. index < map_size) {
  1836. clear_bit(index, map_storep);
  1837. if (scsi_debug_lbprz) {
  1838. memset(fake_storep +
  1839. lba * scsi_debug_sector_size, 0,
  1840. scsi_debug_sector_size *
  1841. scsi_debug_unmap_granularity);
  1842. }
  1843. if (dif_storep) {
  1844. memset(dif_storep + lba, 0xff,
  1845. sizeof(*dif_storep) *
  1846. scsi_debug_unmap_granularity);
  1847. }
  1848. }
  1849. lba = map_index_to_lba(index + 1);
  1850. }
  1851. }
  1852. static int resp_write(struct scsi_cmnd *SCpnt, unsigned long long lba,
  1853. unsigned int num, struct sdebug_dev_info *devip,
  1854. u32 ei_lba)
  1855. {
  1856. unsigned long iflags;
  1857. int ret;
  1858. ret = check_device_access_params(devip, lba, num);
  1859. if (ret)
  1860. return ret;
  1861. write_lock_irqsave(&atomic_rw, iflags);
  1862. /* DIX + T10 DIF */
  1863. if (scsi_debug_dix && scsi_prot_sg_count(SCpnt)) {
  1864. int prot_ret = prot_verify_write(SCpnt, lba, num, ei_lba);
  1865. if (prot_ret) {
  1866. write_unlock_irqrestore(&atomic_rw, iflags);
  1867. mk_sense_buffer(devip, ILLEGAL_REQUEST, 0x10, prot_ret);
  1868. return illegal_condition_result;
  1869. }
  1870. }
  1871. ret = do_device_access(SCpnt, devip, lba, num, 1);
  1872. if (scsi_debug_lbp())
  1873. map_region(lba, num);
  1874. write_unlock_irqrestore(&atomic_rw, iflags);
  1875. if (-1 == ret)
  1876. return (DID_ERROR << 16);
  1877. else if ((ret < (num * scsi_debug_sector_size)) &&
  1878. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1879. printk(KERN_INFO "scsi_debug: write: cdb indicated=%u, "
  1880. " IO sent=%d bytes\n", num * scsi_debug_sector_size, ret);
  1881. return 0;
  1882. }
  1883. static int resp_write_same(struct scsi_cmnd *scmd, unsigned long long lba,
  1884. unsigned int num, struct sdebug_dev_info *devip,
  1885. u32 ei_lba, unsigned int unmap)
  1886. {
  1887. unsigned long iflags;
  1888. unsigned long long i;
  1889. int ret;
  1890. ret = check_device_access_params(devip, lba, num);
  1891. if (ret)
  1892. return ret;
  1893. if (num > scsi_debug_write_same_length) {
  1894. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1895. 0);
  1896. return check_condition_result;
  1897. }
  1898. write_lock_irqsave(&atomic_rw, iflags);
  1899. if (unmap && scsi_debug_lbp()) {
  1900. unmap_region(lba, num);
  1901. goto out;
  1902. }
  1903. /* Else fetch one logical block */
  1904. ret = fetch_to_dev_buffer(scmd,
  1905. fake_storep + (lba * scsi_debug_sector_size),
  1906. scsi_debug_sector_size);
  1907. if (-1 == ret) {
  1908. write_unlock_irqrestore(&atomic_rw, iflags);
  1909. return (DID_ERROR << 16);
  1910. } else if ((ret < (num * scsi_debug_sector_size)) &&
  1911. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1912. printk(KERN_INFO "scsi_debug: write same: cdb indicated=%u, "
  1913. " IO sent=%d bytes\n", num * scsi_debug_sector_size, ret);
  1914. /* Copy first sector to remaining blocks */
  1915. for (i = 1 ; i < num ; i++)
  1916. memcpy(fake_storep + ((lba + i) * scsi_debug_sector_size),
  1917. fake_storep + (lba * scsi_debug_sector_size),
  1918. scsi_debug_sector_size);
  1919. if (scsi_debug_lbp())
  1920. map_region(lba, num);
  1921. out:
  1922. write_unlock_irqrestore(&atomic_rw, iflags);
  1923. return 0;
  1924. }
  1925. struct unmap_block_desc {
  1926. __be64 lba;
  1927. __be32 blocks;
  1928. __be32 __reserved;
  1929. };
  1930. static int resp_unmap(struct scsi_cmnd * scmd, struct sdebug_dev_info * devip)
  1931. {
  1932. unsigned char *buf;
  1933. struct unmap_block_desc *desc;
  1934. unsigned int i, payload_len, descriptors;
  1935. int ret;
  1936. unsigned long iflags;
  1937. ret = check_readiness(scmd, 1, devip);
  1938. if (ret)
  1939. return ret;
  1940. payload_len = get_unaligned_be16(&scmd->cmnd[7]);
  1941. BUG_ON(scsi_bufflen(scmd) != payload_len);
  1942. descriptors = (payload_len - 8) / 16;
  1943. buf = kmalloc(scsi_bufflen(scmd), GFP_ATOMIC);
  1944. if (!buf)
  1945. return check_condition_result;
  1946. scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
  1947. BUG_ON(get_unaligned_be16(&buf[0]) != payload_len - 2);
  1948. BUG_ON(get_unaligned_be16(&buf[2]) != descriptors * 16);
  1949. desc = (void *)&buf[8];
  1950. write_lock_irqsave(&atomic_rw, iflags);
  1951. for (i = 0 ; i < descriptors ; i++) {
  1952. unsigned long long lba = get_unaligned_be64(&desc[i].lba);
  1953. unsigned int num = get_unaligned_be32(&desc[i].blocks);
  1954. ret = check_device_access_params(devip, lba, num);
  1955. if (ret)
  1956. goto out;
  1957. unmap_region(lba, num);
  1958. }
  1959. ret = 0;
  1960. out:
  1961. write_unlock_irqrestore(&atomic_rw, iflags);
  1962. kfree(buf);
  1963. return ret;
  1964. }
  1965. #define SDEBUG_GET_LBA_STATUS_LEN 32
  1966. static int resp_get_lba_status(struct scsi_cmnd * scmd,
  1967. struct sdebug_dev_info * devip)
  1968. {
  1969. unsigned long long lba;
  1970. unsigned int alloc_len, mapped, num;
  1971. unsigned char arr[SDEBUG_GET_LBA_STATUS_LEN];
  1972. int ret;
  1973. ret = check_readiness(scmd, 1, devip);
  1974. if (ret)
  1975. return ret;
  1976. lba = get_unaligned_be64(&scmd->cmnd[2]);
  1977. alloc_len = get_unaligned_be32(&scmd->cmnd[10]);
  1978. if (alloc_len < 24)
  1979. return 0;
  1980. ret = check_device_access_params(devip, lba, 1);
  1981. if (ret)
  1982. return ret;
  1983. mapped = map_state(lba, &num);
  1984. memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
  1985. put_unaligned_be32(20, &arr[0]); /* Parameter Data Length */
  1986. put_unaligned_be64(lba, &arr[8]); /* LBA */
  1987. put_unaligned_be32(num, &arr[16]); /* Number of blocks */
  1988. arr[20] = !mapped; /* mapped = 0, unmapped = 1 */
  1989. return fill_from_dev_buffer(scmd, arr, SDEBUG_GET_LBA_STATUS_LEN);
  1990. }
  1991. #define SDEBUG_RLUN_ARR_SZ 256
  1992. static int resp_report_luns(struct scsi_cmnd * scp,
  1993. struct sdebug_dev_info * devip)
  1994. {
  1995. unsigned int alloc_len;
  1996. int lun_cnt, i, upper, num, n, wlun, lun;
  1997. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1998. int select_report = (int)cmd[2];
  1999. struct scsi_lun *one_lun;
  2000. unsigned char arr[SDEBUG_RLUN_ARR_SZ];
  2001. unsigned char * max_addr;
  2002. alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
  2003. if ((alloc_len < 4) || (select_report > 2)) {
  2004. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  2005. 0);
  2006. return check_condition_result;
  2007. }
  2008. /* can produce response with up to 16k luns (lun 0 to lun 16383) */
  2009. memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
  2010. lun_cnt = scsi_debug_max_luns;
  2011. if (1 == select_report)
  2012. lun_cnt = 0;
  2013. else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
  2014. --lun_cnt;
  2015. wlun = (select_report > 0) ? 1 : 0;
  2016. num = lun_cnt + wlun;
  2017. arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
  2018. arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
  2019. n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
  2020. sizeof(struct scsi_lun)), num);
  2021. if (n < num) {
  2022. wlun = 0;
  2023. lun_cnt = n;
  2024. }
  2025. one_lun = (struct scsi_lun *) &arr[8];
  2026. max_addr = arr + SDEBUG_RLUN_ARR_SZ;
  2027. for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
  2028. ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
  2029. i++, lun++) {
  2030. upper = (lun >> 8) & 0x3f;
  2031. if (upper)
  2032. one_lun[i].scsi_lun[0] =
  2033. (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
  2034. one_lun[i].scsi_lun[1] = lun & 0xff;
  2035. }
  2036. if (wlun) {
  2037. one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
  2038. one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
  2039. i++;
  2040. }
  2041. alloc_len = (unsigned char *)(one_lun + i) - arr;
  2042. return fill_from_dev_buffer(scp, arr,
  2043. min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
  2044. }
  2045. static int resp_xdwriteread(struct scsi_cmnd *scp, unsigned long long lba,
  2046. unsigned int num, struct sdebug_dev_info *devip)
  2047. {
  2048. int j;
  2049. unsigned char *kaddr, *buf;
  2050. unsigned int offset;
  2051. struct scsi_data_buffer *sdb = scsi_in(scp);
  2052. struct sg_mapping_iter miter;
  2053. /* better not to use temporary buffer. */
  2054. buf = kmalloc(scsi_bufflen(scp), GFP_ATOMIC);
  2055. if (!buf) {
  2056. mk_sense_buffer(devip, NOT_READY,
  2057. LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
  2058. return check_condition_result;
  2059. }
  2060. scsi_sg_copy_to_buffer(scp, buf, scsi_bufflen(scp));
  2061. offset = 0;
  2062. sg_miter_start(&miter, sdb->table.sgl, sdb->table.nents,
  2063. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  2064. while (sg_miter_next(&miter)) {
  2065. kaddr = miter.addr;
  2066. for (j = 0; j < miter.length; j++)
  2067. *(kaddr + j) ^= *(buf + offset + j);
  2068. offset += miter.length;
  2069. }
  2070. sg_miter_stop(&miter);
  2071. kfree(buf);
  2072. return 0;
  2073. }
  2074. /* When timer goes off this function is called. */
  2075. static void timer_intr_handler(unsigned long indx)
  2076. {
  2077. struct sdebug_queued_cmd * sqcp;
  2078. unsigned long iflags;
  2079. if (indx >= scsi_debug_max_queue) {
  2080. printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
  2081. "large\n");
  2082. return;
  2083. }
  2084. spin_lock_irqsave(&queued_arr_lock, iflags);
  2085. sqcp = &queued_arr[(int)indx];
  2086. if (! sqcp->in_use) {
  2087. printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
  2088. "interrupt\n");
  2089. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2090. return;
  2091. }
  2092. sqcp->in_use = 0;
  2093. if (sqcp->done_funct) {
  2094. sqcp->a_cmnd->result = sqcp->scsi_result;
  2095. sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
  2096. }
  2097. sqcp->done_funct = NULL;
  2098. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2099. }
  2100. static struct sdebug_dev_info *
  2101. sdebug_device_create(struct sdebug_host_info *sdbg_host, gfp_t flags)
  2102. {
  2103. struct sdebug_dev_info *devip;
  2104. devip = kzalloc(sizeof(*devip), flags);
  2105. if (devip) {
  2106. devip->sdbg_host = sdbg_host;
  2107. list_add_tail(&devip->dev_list, &sdbg_host->dev_info_list);
  2108. }
  2109. return devip;
  2110. }
  2111. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
  2112. {
  2113. struct sdebug_host_info * sdbg_host;
  2114. struct sdebug_dev_info * open_devip = NULL;
  2115. struct sdebug_dev_info * devip =
  2116. (struct sdebug_dev_info *)sdev->hostdata;
  2117. if (devip)
  2118. return devip;
  2119. sdbg_host = *(struct sdebug_host_info **)shost_priv(sdev->host);
  2120. if (!sdbg_host) {
  2121. printk(KERN_ERR "Host info NULL\n");
  2122. return NULL;
  2123. }
  2124. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  2125. if ((devip->used) && (devip->channel == sdev->channel) &&
  2126. (devip->target == sdev->id) &&
  2127. (devip->lun == sdev->lun))
  2128. return devip;
  2129. else {
  2130. if ((!devip->used) && (!open_devip))
  2131. open_devip = devip;
  2132. }
  2133. }
  2134. if (!open_devip) { /* try and make a new one */
  2135. open_devip = sdebug_device_create(sdbg_host, GFP_ATOMIC);
  2136. if (!open_devip) {
  2137. printk(KERN_ERR "%s: out of memory at line %d\n",
  2138. __func__, __LINE__);
  2139. return NULL;
  2140. }
  2141. }
  2142. open_devip->channel = sdev->channel;
  2143. open_devip->target = sdev->id;
  2144. open_devip->lun = sdev->lun;
  2145. open_devip->sdbg_host = sdbg_host;
  2146. open_devip->reset = 1;
  2147. open_devip->used = 1;
  2148. memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
  2149. if (scsi_debug_dsense)
  2150. open_devip->sense_buff[0] = 0x72;
  2151. else {
  2152. open_devip->sense_buff[0] = 0x70;
  2153. open_devip->sense_buff[7] = 0xa;
  2154. }
  2155. if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
  2156. open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
  2157. return open_devip;
  2158. }
  2159. static int scsi_debug_slave_alloc(struct scsi_device *sdp)
  2160. {
  2161. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2162. printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
  2163. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2164. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, sdp->request_queue);
  2165. return 0;
  2166. }
  2167. static int scsi_debug_slave_configure(struct scsi_device *sdp)
  2168. {
  2169. struct sdebug_dev_info *devip;
  2170. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2171. printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
  2172. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2173. if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
  2174. sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
  2175. devip = devInfoReg(sdp);
  2176. if (NULL == devip)
  2177. return 1; /* no resources, will be marked offline */
  2178. sdp->hostdata = devip;
  2179. if (sdp->host->cmd_per_lun)
  2180. scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
  2181. sdp->host->cmd_per_lun);
  2182. blk_queue_max_segment_size(sdp->request_queue, 256 * 1024);
  2183. if (scsi_debug_no_uld)
  2184. sdp->no_uld_attach = 1;
  2185. return 0;
  2186. }
  2187. static void scsi_debug_slave_destroy(struct scsi_device *sdp)
  2188. {
  2189. struct sdebug_dev_info *devip =
  2190. (struct sdebug_dev_info *)sdp->hostdata;
  2191. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2192. printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
  2193. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  2194. if (devip) {
  2195. /* make this slot available for re-use */
  2196. devip->used = 0;
  2197. sdp->hostdata = NULL;
  2198. }
  2199. }
  2200. /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
  2201. static int stop_queued_cmnd(struct scsi_cmnd *cmnd)
  2202. {
  2203. unsigned long iflags;
  2204. int k;
  2205. struct sdebug_queued_cmd *sqcp;
  2206. spin_lock_irqsave(&queued_arr_lock, iflags);
  2207. for (k = 0; k < scsi_debug_max_queue; ++k) {
  2208. sqcp = &queued_arr[k];
  2209. if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
  2210. del_timer_sync(&sqcp->cmnd_timer);
  2211. sqcp->in_use = 0;
  2212. sqcp->a_cmnd = NULL;
  2213. break;
  2214. }
  2215. }
  2216. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2217. return (k < scsi_debug_max_queue) ? 1 : 0;
  2218. }
  2219. /* Deletes (stops) timers of all queued commands */
  2220. static void stop_all_queued(void)
  2221. {
  2222. unsigned long iflags;
  2223. int k;
  2224. struct sdebug_queued_cmd *sqcp;
  2225. spin_lock_irqsave(&queued_arr_lock, iflags);
  2226. for (k = 0; k < scsi_debug_max_queue; ++k) {
  2227. sqcp = &queued_arr[k];
  2228. if (sqcp->in_use && sqcp->a_cmnd) {
  2229. del_timer_sync(&sqcp->cmnd_timer);
  2230. sqcp->in_use = 0;
  2231. sqcp->a_cmnd = NULL;
  2232. }
  2233. }
  2234. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2235. }
  2236. static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
  2237. {
  2238. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2239. printk(KERN_INFO "scsi_debug: abort\n");
  2240. ++num_aborts;
  2241. stop_queued_cmnd(SCpnt);
  2242. return SUCCESS;
  2243. }
  2244. static int scsi_debug_biosparam(struct scsi_device *sdev,
  2245. struct block_device * bdev, sector_t capacity, int *info)
  2246. {
  2247. int res;
  2248. unsigned char *buf;
  2249. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2250. printk(KERN_INFO "scsi_debug: biosparam\n");
  2251. buf = scsi_bios_ptable(bdev);
  2252. if (buf) {
  2253. res = scsi_partsize(buf, capacity,
  2254. &info[2], &info[0], &info[1]);
  2255. kfree(buf);
  2256. if (! res)
  2257. return res;
  2258. }
  2259. info[0] = sdebug_heads;
  2260. info[1] = sdebug_sectors_per;
  2261. info[2] = sdebug_cylinders_per;
  2262. return 0;
  2263. }
  2264. static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
  2265. {
  2266. struct sdebug_dev_info * devip;
  2267. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2268. printk(KERN_INFO "scsi_debug: device_reset\n");
  2269. ++num_dev_resets;
  2270. if (SCpnt) {
  2271. devip = devInfoReg(SCpnt->device);
  2272. if (devip)
  2273. devip->reset = 1;
  2274. }
  2275. return SUCCESS;
  2276. }
  2277. static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
  2278. {
  2279. struct sdebug_host_info *sdbg_host;
  2280. struct sdebug_dev_info * dev_info;
  2281. struct scsi_device * sdp;
  2282. struct Scsi_Host * hp;
  2283. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2284. printk(KERN_INFO "scsi_debug: bus_reset\n");
  2285. ++num_bus_resets;
  2286. if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
  2287. sdbg_host = *(struct sdebug_host_info **)shost_priv(hp);
  2288. if (sdbg_host) {
  2289. list_for_each_entry(dev_info,
  2290. &sdbg_host->dev_info_list,
  2291. dev_list)
  2292. dev_info->reset = 1;
  2293. }
  2294. }
  2295. return SUCCESS;
  2296. }
  2297. static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
  2298. {
  2299. struct sdebug_host_info * sdbg_host;
  2300. struct sdebug_dev_info * dev_info;
  2301. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2302. printk(KERN_INFO "scsi_debug: host_reset\n");
  2303. ++num_host_resets;
  2304. spin_lock(&sdebug_host_list_lock);
  2305. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2306. list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
  2307. dev_list)
  2308. dev_info->reset = 1;
  2309. }
  2310. spin_unlock(&sdebug_host_list_lock);
  2311. stop_all_queued();
  2312. return SUCCESS;
  2313. }
  2314. /* Initializes timers in queued array */
  2315. static void __init init_all_queued(void)
  2316. {
  2317. unsigned long iflags;
  2318. int k;
  2319. struct sdebug_queued_cmd * sqcp;
  2320. spin_lock_irqsave(&queued_arr_lock, iflags);
  2321. for (k = 0; k < scsi_debug_max_queue; ++k) {
  2322. sqcp = &queued_arr[k];
  2323. init_timer(&sqcp->cmnd_timer);
  2324. sqcp->in_use = 0;
  2325. sqcp->a_cmnd = NULL;
  2326. }
  2327. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2328. }
  2329. static void __init sdebug_build_parts(unsigned char *ramp,
  2330. unsigned long store_size)
  2331. {
  2332. struct partition * pp;
  2333. int starts[SDEBUG_MAX_PARTS + 2];
  2334. int sectors_per_part, num_sectors, k;
  2335. int heads_by_sects, start_sec, end_sec;
  2336. /* assume partition table already zeroed */
  2337. if ((scsi_debug_num_parts < 1) || (store_size < 1048576))
  2338. return;
  2339. if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
  2340. scsi_debug_num_parts = SDEBUG_MAX_PARTS;
  2341. printk(KERN_WARNING "scsi_debug:build_parts: reducing "
  2342. "partitions to %d\n", SDEBUG_MAX_PARTS);
  2343. }
  2344. num_sectors = (int)sdebug_store_sectors;
  2345. sectors_per_part = (num_sectors - sdebug_sectors_per)
  2346. / scsi_debug_num_parts;
  2347. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  2348. starts[0] = sdebug_sectors_per;
  2349. for (k = 1; k < scsi_debug_num_parts; ++k)
  2350. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  2351. * heads_by_sects;
  2352. starts[scsi_debug_num_parts] = num_sectors;
  2353. starts[scsi_debug_num_parts + 1] = 0;
  2354. ramp[510] = 0x55; /* magic partition markings */
  2355. ramp[511] = 0xAA;
  2356. pp = (struct partition *)(ramp + 0x1be);
  2357. for (k = 0; starts[k + 1]; ++k, ++pp) {
  2358. start_sec = starts[k];
  2359. end_sec = starts[k + 1] - 1;
  2360. pp->boot_ind = 0;
  2361. pp->cyl = start_sec / heads_by_sects;
  2362. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  2363. / sdebug_sectors_per;
  2364. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  2365. pp->end_cyl = end_sec / heads_by_sects;
  2366. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  2367. / sdebug_sectors_per;
  2368. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  2369. pp->start_sect = cpu_to_le32(start_sec);
  2370. pp->nr_sects = cpu_to_le32(end_sec - start_sec + 1);
  2371. pp->sys_ind = 0x83; /* plain Linux partition */
  2372. }
  2373. }
  2374. static int schedule_resp(struct scsi_cmnd * cmnd,
  2375. struct sdebug_dev_info * devip,
  2376. done_funct_t done, int scsi_result, int delta_jiff)
  2377. {
  2378. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
  2379. if (scsi_result) {
  2380. struct scsi_device * sdp = cmnd->device;
  2381. printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
  2382. "non-zero result=0x%x\n", sdp->host->host_no,
  2383. sdp->channel, sdp->id, sdp->lun, scsi_result);
  2384. }
  2385. }
  2386. if (cmnd && devip) {
  2387. /* simulate autosense by this driver */
  2388. if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
  2389. memcpy(cmnd->sense_buffer, devip->sense_buff,
  2390. (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
  2391. SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
  2392. }
  2393. if (delta_jiff <= 0) {
  2394. if (cmnd)
  2395. cmnd->result = scsi_result;
  2396. if (done)
  2397. done(cmnd);
  2398. return 0;
  2399. } else {
  2400. unsigned long iflags;
  2401. int k;
  2402. struct sdebug_queued_cmd * sqcp = NULL;
  2403. spin_lock_irqsave(&queued_arr_lock, iflags);
  2404. for (k = 0; k < scsi_debug_max_queue; ++k) {
  2405. sqcp = &queued_arr[k];
  2406. if (! sqcp->in_use)
  2407. break;
  2408. }
  2409. if (k >= scsi_debug_max_queue) {
  2410. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2411. printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
  2412. return 1; /* report busy to mid level */
  2413. }
  2414. sqcp->in_use = 1;
  2415. sqcp->a_cmnd = cmnd;
  2416. sqcp->scsi_result = scsi_result;
  2417. sqcp->done_funct = done;
  2418. sqcp->cmnd_timer.function = timer_intr_handler;
  2419. sqcp->cmnd_timer.data = k;
  2420. sqcp->cmnd_timer.expires = jiffies + delta_jiff;
  2421. add_timer(&sqcp->cmnd_timer);
  2422. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2423. if (cmnd)
  2424. cmnd->result = 0;
  2425. return 0;
  2426. }
  2427. }
  2428. /* Note: The following macros create attribute files in the
  2429. /sys/module/scsi_debug/parameters directory. Unfortunately this
  2430. driver is unaware of a change and cannot trigger auxiliary actions
  2431. as it can when the corresponding attribute in the
  2432. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  2433. */
  2434. module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
  2435. module_param_named(ato, scsi_debug_ato, int, S_IRUGO);
  2436. module_param_named(clustering, scsi_debug_clustering, bool, S_IRUGO | S_IWUSR);
  2437. module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
  2438. module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
  2439. module_param_named(dif, scsi_debug_dif, int, S_IRUGO);
  2440. module_param_named(dix, scsi_debug_dix, int, S_IRUGO);
  2441. module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
  2442. module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
  2443. module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
  2444. module_param_named(guard, scsi_debug_guard, uint, S_IRUGO);
  2445. module_param_named(lbpu, scsi_debug_lbpu, int, S_IRUGO);
  2446. module_param_named(lbpws, scsi_debug_lbpws, int, S_IRUGO);
  2447. module_param_named(lbpws10, scsi_debug_lbpws10, int, S_IRUGO);
  2448. module_param_named(lbprz, scsi_debug_lbprz, int, S_IRUGO);
  2449. module_param_named(lowest_aligned, scsi_debug_lowest_aligned, int, S_IRUGO);
  2450. module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
  2451. module_param_named(max_queue, scsi_debug_max_queue, int, S_IRUGO | S_IWUSR);
  2452. module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
  2453. module_param_named(no_uld, scsi_debug_no_uld, int, S_IRUGO);
  2454. module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
  2455. module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
  2456. module_param_named(opt_blks, scsi_debug_opt_blks, int, S_IRUGO);
  2457. module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
  2458. module_param_named(physblk_exp, scsi_debug_physblk_exp, int, S_IRUGO);
  2459. module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
  2460. module_param_named(removable, scsi_debug_removable, bool, S_IRUGO | S_IWUSR);
  2461. module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
  2462. module_param_named(sector_size, scsi_debug_sector_size, int, S_IRUGO);
  2463. module_param_named(unmap_alignment, scsi_debug_unmap_alignment, int, S_IRUGO);
  2464. module_param_named(unmap_granularity, scsi_debug_unmap_granularity, int, S_IRUGO);
  2465. module_param_named(unmap_max_blocks, scsi_debug_unmap_max_blocks, int, S_IRUGO);
  2466. module_param_named(unmap_max_desc, scsi_debug_unmap_max_desc, int, S_IRUGO);
  2467. module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
  2468. module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
  2469. S_IRUGO | S_IWUSR);
  2470. module_param_named(write_same_length, scsi_debug_write_same_length, int,
  2471. S_IRUGO | S_IWUSR);
  2472. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  2473. MODULE_DESCRIPTION("SCSI debug adapter driver");
  2474. MODULE_LICENSE("GPL");
  2475. MODULE_VERSION(SCSI_DEBUG_VERSION);
  2476. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  2477. MODULE_PARM_DESC(ato, "application tag ownership: 0=disk 1=host (def=1)");
  2478. MODULE_PARM_DESC(clustering, "when set enables larger transfers (def=0)");
  2479. MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
  2480. MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
  2481. MODULE_PARM_DESC(dif, "data integrity field type: 0-3 (def=0)");
  2482. MODULE_PARM_DESC(dix, "data integrity extensions mask (def=0)");
  2483. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  2484. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  2485. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  2486. MODULE_PARM_DESC(guard, "protection checksum: 0=crc, 1=ip (def=0)");
  2487. MODULE_PARM_DESC(lbpu, "enable LBP, support UNMAP command (def=0)");
  2488. MODULE_PARM_DESC(lbpws, "enable LBP, support WRITE SAME(16) with UNMAP bit (def=0)");
  2489. MODULE_PARM_DESC(lbpws10, "enable LBP, support WRITE SAME(10) with UNMAP bit (def=0)");
  2490. MODULE_PARM_DESC(lbprz, "unmapped blocks return 0 on read (def=1)");
  2491. MODULE_PARM_DESC(lowest_aligned, "lowest aligned lba (def=0)");
  2492. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  2493. MODULE_PARM_DESC(max_queue, "max number of queued commands (1 to 255(def))");
  2494. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  2495. MODULE_PARM_DESC(no_uld, "stop ULD (e.g. sd driver) attaching (def=0))");
  2496. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  2497. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  2498. MODULE_PARM_DESC(opt_blks, "optimal transfer length in block (def=64)");
  2499. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  2500. MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
  2501. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  2502. MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
  2503. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
  2504. MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
  2505. MODULE_PARM_DESC(unmap_alignment, "lowest aligned thin provisioning lba (def=0)");
  2506. MODULE_PARM_DESC(unmap_granularity, "thin provisioning granularity in blocks (def=1)");
  2507. MODULE_PARM_DESC(unmap_max_blocks, "max # of blocks can be unmapped in one cmd (def=0xffffffff)");
  2508. MODULE_PARM_DESC(unmap_max_desc, "max # of ranges that can be unmapped in one cmd (def=256)");
  2509. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
  2510. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  2511. MODULE_PARM_DESC(write_same_length, "Maximum blocks per WRITE SAME cmd (def=0xffff)");
  2512. static char sdebug_info[256];
  2513. static const char * scsi_debug_info(struct Scsi_Host * shp)
  2514. {
  2515. sprintf(sdebug_info, "scsi_debug, version %s [%s], "
  2516. "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
  2517. scsi_debug_version_date, scsi_debug_dev_size_mb,
  2518. scsi_debug_opts);
  2519. return sdebug_info;
  2520. }
  2521. /* scsi_debug_proc_info
  2522. * Used if the driver currently has no own support for /proc/scsi
  2523. */
  2524. static int scsi_debug_write_info(struct Scsi_Host *host, char *buffer, int length)
  2525. {
  2526. char arr[16];
  2527. int opts;
  2528. int minLen = length > 15 ? 15 : length;
  2529. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2530. return -EACCES;
  2531. memcpy(arr, buffer, minLen);
  2532. arr[minLen] = '\0';
  2533. if (1 != sscanf(arr, "%d", &opts))
  2534. return -EINVAL;
  2535. scsi_debug_opts = opts;
  2536. if (scsi_debug_every_nth != 0)
  2537. scsi_debug_cmnd_count = 0;
  2538. return length;
  2539. }
  2540. static int scsi_debug_show_info(struct seq_file *m, struct Scsi_Host *host)
  2541. {
  2542. seq_printf(m, "scsi_debug adapter driver, version "
  2543. "%s [%s]\n"
  2544. "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
  2545. "every_nth=%d(curr:%d)\n"
  2546. "delay=%d, max_luns=%d, scsi_level=%d\n"
  2547. "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
  2548. "number of aborts=%d, device_reset=%d, bus_resets=%d, "
  2549. "host_resets=%d\ndix_reads=%d dix_writes=%d dif_errors=%d\n",
  2550. SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
  2551. scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
  2552. scsi_debug_cmnd_count, scsi_debug_delay,
  2553. scsi_debug_max_luns, scsi_debug_scsi_level,
  2554. scsi_debug_sector_size, sdebug_cylinders_per, sdebug_heads,
  2555. sdebug_sectors_per, num_aborts, num_dev_resets, num_bus_resets,
  2556. num_host_resets, dix_reads, dix_writes, dif_errors);
  2557. return 0;
  2558. }
  2559. static ssize_t delay_show(struct device_driver *ddp, char *buf)
  2560. {
  2561. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
  2562. }
  2563. static ssize_t delay_store(struct device_driver *ddp, const char *buf,
  2564. size_t count)
  2565. {
  2566. int delay;
  2567. char work[20];
  2568. if (1 == sscanf(buf, "%10s", work)) {
  2569. if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
  2570. scsi_debug_delay = delay;
  2571. return count;
  2572. }
  2573. }
  2574. return -EINVAL;
  2575. }
  2576. static DRIVER_ATTR_RW(delay);
  2577. static ssize_t opts_show(struct device_driver *ddp, char *buf)
  2578. {
  2579. return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
  2580. }
  2581. static ssize_t opts_store(struct device_driver *ddp, const char *buf,
  2582. size_t count)
  2583. {
  2584. int opts;
  2585. char work[20];
  2586. if (1 == sscanf(buf, "%10s", work)) {
  2587. if (0 == strnicmp(work,"0x", 2)) {
  2588. if (1 == sscanf(&work[2], "%x", &opts))
  2589. goto opts_done;
  2590. } else {
  2591. if (1 == sscanf(work, "%d", &opts))
  2592. goto opts_done;
  2593. }
  2594. }
  2595. return -EINVAL;
  2596. opts_done:
  2597. scsi_debug_opts = opts;
  2598. scsi_debug_cmnd_count = 0;
  2599. return count;
  2600. }
  2601. static DRIVER_ATTR_RW(opts);
  2602. static ssize_t ptype_show(struct device_driver *ddp, char *buf)
  2603. {
  2604. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
  2605. }
  2606. static ssize_t ptype_store(struct device_driver *ddp, const char *buf,
  2607. size_t count)
  2608. {
  2609. int n;
  2610. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2611. scsi_debug_ptype = n;
  2612. return count;
  2613. }
  2614. return -EINVAL;
  2615. }
  2616. static DRIVER_ATTR_RW(ptype);
  2617. static ssize_t dsense_show(struct device_driver *ddp, char *buf)
  2618. {
  2619. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
  2620. }
  2621. static ssize_t dsense_store(struct device_driver *ddp, const char *buf,
  2622. size_t count)
  2623. {
  2624. int n;
  2625. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2626. scsi_debug_dsense = n;
  2627. return count;
  2628. }
  2629. return -EINVAL;
  2630. }
  2631. static DRIVER_ATTR_RW(dsense);
  2632. static ssize_t fake_rw_show(struct device_driver *ddp, char *buf)
  2633. {
  2634. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
  2635. }
  2636. static ssize_t fake_rw_store(struct device_driver *ddp, const char *buf,
  2637. size_t count)
  2638. {
  2639. int n;
  2640. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2641. scsi_debug_fake_rw = n;
  2642. return count;
  2643. }
  2644. return -EINVAL;
  2645. }
  2646. static DRIVER_ATTR_RW(fake_rw);
  2647. static ssize_t no_lun_0_show(struct device_driver *ddp, char *buf)
  2648. {
  2649. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
  2650. }
  2651. static ssize_t no_lun_0_store(struct device_driver *ddp, const char *buf,
  2652. size_t count)
  2653. {
  2654. int n;
  2655. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2656. scsi_debug_no_lun_0 = n;
  2657. return count;
  2658. }
  2659. return -EINVAL;
  2660. }
  2661. static DRIVER_ATTR_RW(no_lun_0);
  2662. static ssize_t num_tgts_show(struct device_driver *ddp, char *buf)
  2663. {
  2664. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
  2665. }
  2666. static ssize_t num_tgts_store(struct device_driver *ddp, const char *buf,
  2667. size_t count)
  2668. {
  2669. int n;
  2670. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2671. scsi_debug_num_tgts = n;
  2672. sdebug_max_tgts_luns();
  2673. return count;
  2674. }
  2675. return -EINVAL;
  2676. }
  2677. static DRIVER_ATTR_RW(num_tgts);
  2678. static ssize_t dev_size_mb_show(struct device_driver *ddp, char *buf)
  2679. {
  2680. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
  2681. }
  2682. static DRIVER_ATTR_RO(dev_size_mb);
  2683. static ssize_t num_parts_show(struct device_driver *ddp, char *buf)
  2684. {
  2685. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
  2686. }
  2687. static DRIVER_ATTR_RO(num_parts);
  2688. static ssize_t every_nth_show(struct device_driver *ddp, char *buf)
  2689. {
  2690. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
  2691. }
  2692. static ssize_t every_nth_store(struct device_driver *ddp, const char *buf,
  2693. size_t count)
  2694. {
  2695. int nth;
  2696. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  2697. scsi_debug_every_nth = nth;
  2698. scsi_debug_cmnd_count = 0;
  2699. return count;
  2700. }
  2701. return -EINVAL;
  2702. }
  2703. static DRIVER_ATTR_RW(every_nth);
  2704. static ssize_t max_luns_show(struct device_driver *ddp, char *buf)
  2705. {
  2706. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
  2707. }
  2708. static ssize_t max_luns_store(struct device_driver *ddp, const char *buf,
  2709. size_t count)
  2710. {
  2711. int n;
  2712. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2713. scsi_debug_max_luns = n;
  2714. sdebug_max_tgts_luns();
  2715. return count;
  2716. }
  2717. return -EINVAL;
  2718. }
  2719. static DRIVER_ATTR_RW(max_luns);
  2720. static ssize_t max_queue_show(struct device_driver *ddp, char *buf)
  2721. {
  2722. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_queue);
  2723. }
  2724. static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,
  2725. size_t count)
  2726. {
  2727. int n;
  2728. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n > 0) &&
  2729. (n <= SCSI_DEBUG_CANQUEUE)) {
  2730. scsi_debug_max_queue = n;
  2731. return count;
  2732. }
  2733. return -EINVAL;
  2734. }
  2735. static DRIVER_ATTR_RW(max_queue);
  2736. static ssize_t no_uld_show(struct device_driver *ddp, char *buf)
  2737. {
  2738. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_uld);
  2739. }
  2740. static DRIVER_ATTR_RO(no_uld);
  2741. static ssize_t scsi_level_show(struct device_driver *ddp, char *buf)
  2742. {
  2743. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
  2744. }
  2745. static DRIVER_ATTR_RO(scsi_level);
  2746. static ssize_t virtual_gb_show(struct device_driver *ddp, char *buf)
  2747. {
  2748. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
  2749. }
  2750. static ssize_t virtual_gb_store(struct device_driver *ddp, const char *buf,
  2751. size_t count)
  2752. {
  2753. int n;
  2754. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2755. scsi_debug_virtual_gb = n;
  2756. sdebug_capacity = get_sdebug_capacity();
  2757. return count;
  2758. }
  2759. return -EINVAL;
  2760. }
  2761. static DRIVER_ATTR_RW(virtual_gb);
  2762. static ssize_t add_host_show(struct device_driver *ddp, char *buf)
  2763. {
  2764. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
  2765. }
  2766. static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
  2767. size_t count)
  2768. {
  2769. int delta_hosts;
  2770. if (sscanf(buf, "%d", &delta_hosts) != 1)
  2771. return -EINVAL;
  2772. if (delta_hosts > 0) {
  2773. do {
  2774. sdebug_add_adapter();
  2775. } while (--delta_hosts);
  2776. } else if (delta_hosts < 0) {
  2777. do {
  2778. sdebug_remove_adapter();
  2779. } while (++delta_hosts);
  2780. }
  2781. return count;
  2782. }
  2783. static DRIVER_ATTR_RW(add_host);
  2784. static ssize_t vpd_use_hostno_show(struct device_driver *ddp, char *buf)
  2785. {
  2786. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
  2787. }
  2788. static ssize_t vpd_use_hostno_store(struct device_driver *ddp, const char *buf,
  2789. size_t count)
  2790. {
  2791. int n;
  2792. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2793. scsi_debug_vpd_use_hostno = n;
  2794. return count;
  2795. }
  2796. return -EINVAL;
  2797. }
  2798. static DRIVER_ATTR_RW(vpd_use_hostno);
  2799. static ssize_t sector_size_show(struct device_driver *ddp, char *buf)
  2800. {
  2801. return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_sector_size);
  2802. }
  2803. static DRIVER_ATTR_RO(sector_size);
  2804. static ssize_t dix_show(struct device_driver *ddp, char *buf)
  2805. {
  2806. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dix);
  2807. }
  2808. static DRIVER_ATTR_RO(dix);
  2809. static ssize_t dif_show(struct device_driver *ddp, char *buf)
  2810. {
  2811. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dif);
  2812. }
  2813. static DRIVER_ATTR_RO(dif);
  2814. static ssize_t guard_show(struct device_driver *ddp, char *buf)
  2815. {
  2816. return scnprintf(buf, PAGE_SIZE, "%u\n", scsi_debug_guard);
  2817. }
  2818. static DRIVER_ATTR_RO(guard);
  2819. static ssize_t ato_show(struct device_driver *ddp, char *buf)
  2820. {
  2821. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ato);
  2822. }
  2823. static DRIVER_ATTR_RO(ato);
  2824. static ssize_t map_show(struct device_driver *ddp, char *buf)
  2825. {
  2826. ssize_t count;
  2827. if (!scsi_debug_lbp())
  2828. return scnprintf(buf, PAGE_SIZE, "0-%u\n",
  2829. sdebug_store_sectors);
  2830. count = bitmap_scnlistprintf(buf, PAGE_SIZE, map_storep, map_size);
  2831. buf[count++] = '\n';
  2832. buf[count++] = 0;
  2833. return count;
  2834. }
  2835. static DRIVER_ATTR_RO(map);
  2836. static ssize_t removable_show(struct device_driver *ddp, char *buf)
  2837. {
  2838. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_removable ? 1 : 0);
  2839. }
  2840. static ssize_t removable_store(struct device_driver *ddp, const char *buf,
  2841. size_t count)
  2842. {
  2843. int n;
  2844. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2845. scsi_debug_removable = (n > 0);
  2846. return count;
  2847. }
  2848. return -EINVAL;
  2849. }
  2850. static DRIVER_ATTR_RW(removable);
  2851. /* Note: The following array creates attribute files in the
  2852. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  2853. files (over those found in the /sys/module/scsi_debug/parameters
  2854. directory) is that auxiliary actions can be triggered when an attribute
  2855. is changed. For example see: sdebug_add_host_store() above.
  2856. */
  2857. static struct attribute *sdebug_drv_attrs[] = {
  2858. &driver_attr_delay.attr,
  2859. &driver_attr_opts.attr,
  2860. &driver_attr_ptype.attr,
  2861. &driver_attr_dsense.attr,
  2862. &driver_attr_fake_rw.attr,
  2863. &driver_attr_no_lun_0.attr,
  2864. &driver_attr_num_tgts.attr,
  2865. &driver_attr_dev_size_mb.attr,
  2866. &driver_attr_num_parts.attr,
  2867. &driver_attr_every_nth.attr,
  2868. &driver_attr_max_luns.attr,
  2869. &driver_attr_max_queue.attr,
  2870. &driver_attr_no_uld.attr,
  2871. &driver_attr_scsi_level.attr,
  2872. &driver_attr_virtual_gb.attr,
  2873. &driver_attr_add_host.attr,
  2874. &driver_attr_vpd_use_hostno.attr,
  2875. &driver_attr_sector_size.attr,
  2876. &driver_attr_dix.attr,
  2877. &driver_attr_dif.attr,
  2878. &driver_attr_guard.attr,
  2879. &driver_attr_ato.attr,
  2880. &driver_attr_map.attr,
  2881. &driver_attr_removable.attr,
  2882. NULL,
  2883. };
  2884. ATTRIBUTE_GROUPS(sdebug_drv);
  2885. static struct device *pseudo_primary;
  2886. static int __init scsi_debug_init(void)
  2887. {
  2888. unsigned long sz;
  2889. int host_to_add;
  2890. int k;
  2891. int ret;
  2892. switch (scsi_debug_sector_size) {
  2893. case 512:
  2894. case 1024:
  2895. case 2048:
  2896. case 4096:
  2897. break;
  2898. default:
  2899. printk(KERN_ERR "scsi_debug_init: invalid sector_size %d\n",
  2900. scsi_debug_sector_size);
  2901. return -EINVAL;
  2902. }
  2903. switch (scsi_debug_dif) {
  2904. case SD_DIF_TYPE0_PROTECTION:
  2905. case SD_DIF_TYPE1_PROTECTION:
  2906. case SD_DIF_TYPE2_PROTECTION:
  2907. case SD_DIF_TYPE3_PROTECTION:
  2908. break;
  2909. default:
  2910. printk(KERN_ERR "scsi_debug_init: dif must be 0, 1, 2 or 3\n");
  2911. return -EINVAL;
  2912. }
  2913. if (scsi_debug_guard > 1) {
  2914. printk(KERN_ERR "scsi_debug_init: guard must be 0 or 1\n");
  2915. return -EINVAL;
  2916. }
  2917. if (scsi_debug_ato > 1) {
  2918. printk(KERN_ERR "scsi_debug_init: ato must be 0 or 1\n");
  2919. return -EINVAL;
  2920. }
  2921. if (scsi_debug_physblk_exp > 15) {
  2922. printk(KERN_ERR "scsi_debug_init: invalid physblk_exp %u\n",
  2923. scsi_debug_physblk_exp);
  2924. return -EINVAL;
  2925. }
  2926. if (scsi_debug_lowest_aligned > 0x3fff) {
  2927. printk(KERN_ERR "scsi_debug_init: lowest_aligned too big: %u\n",
  2928. scsi_debug_lowest_aligned);
  2929. return -EINVAL;
  2930. }
  2931. if (scsi_debug_dev_size_mb < 1)
  2932. scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  2933. sz = (unsigned long)scsi_debug_dev_size_mb * 1048576;
  2934. sdebug_store_sectors = sz / scsi_debug_sector_size;
  2935. sdebug_capacity = get_sdebug_capacity();
  2936. /* play around with geometry, don't waste too much on track 0 */
  2937. sdebug_heads = 8;
  2938. sdebug_sectors_per = 32;
  2939. if (scsi_debug_dev_size_mb >= 16)
  2940. sdebug_heads = 32;
  2941. else if (scsi_debug_dev_size_mb >= 256)
  2942. sdebug_heads = 64;
  2943. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2944. (sdebug_sectors_per * sdebug_heads);
  2945. if (sdebug_cylinders_per >= 1024) {
  2946. /* other LLDs do this; implies >= 1GB ram disk ... */
  2947. sdebug_heads = 255;
  2948. sdebug_sectors_per = 63;
  2949. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2950. (sdebug_sectors_per * sdebug_heads);
  2951. }
  2952. fake_storep = vmalloc(sz);
  2953. if (NULL == fake_storep) {
  2954. printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
  2955. return -ENOMEM;
  2956. }
  2957. memset(fake_storep, 0, sz);
  2958. if (scsi_debug_num_parts > 0)
  2959. sdebug_build_parts(fake_storep, sz);
  2960. if (scsi_debug_dix) {
  2961. int dif_size;
  2962. dif_size = sdebug_store_sectors * sizeof(struct sd_dif_tuple);
  2963. dif_storep = vmalloc(dif_size);
  2964. printk(KERN_ERR "scsi_debug_init: dif_storep %u bytes @ %p\n",
  2965. dif_size, dif_storep);
  2966. if (dif_storep == NULL) {
  2967. printk(KERN_ERR "scsi_debug_init: out of mem. (DIX)\n");
  2968. ret = -ENOMEM;
  2969. goto free_vm;
  2970. }
  2971. memset(dif_storep, 0xff, dif_size);
  2972. }
  2973. /* Logical Block Provisioning */
  2974. if (scsi_debug_lbp()) {
  2975. scsi_debug_unmap_max_blocks =
  2976. clamp(scsi_debug_unmap_max_blocks, 0U, 0xffffffffU);
  2977. scsi_debug_unmap_max_desc =
  2978. clamp(scsi_debug_unmap_max_desc, 0U, 256U);
  2979. scsi_debug_unmap_granularity =
  2980. clamp(scsi_debug_unmap_granularity, 1U, 0xffffffffU);
  2981. if (scsi_debug_unmap_alignment &&
  2982. scsi_debug_unmap_granularity <=
  2983. scsi_debug_unmap_alignment) {
  2984. printk(KERN_ERR
  2985. "%s: ERR: unmap_granularity <= unmap_alignment\n",
  2986. __func__);
  2987. return -EINVAL;
  2988. }
  2989. map_size = lba_to_map_index(sdebug_store_sectors - 1) + 1;
  2990. map_storep = vmalloc(BITS_TO_LONGS(map_size) * sizeof(long));
  2991. printk(KERN_INFO "scsi_debug_init: %lu provisioning blocks\n",
  2992. map_size);
  2993. if (map_storep == NULL) {
  2994. printk(KERN_ERR "scsi_debug_init: out of mem. (MAP)\n");
  2995. ret = -ENOMEM;
  2996. goto free_vm;
  2997. }
  2998. bitmap_zero(map_storep, map_size);
  2999. /* Map first 1KB for partition table */
  3000. if (scsi_debug_num_parts)
  3001. map_region(0, 2);
  3002. }
  3003. pseudo_primary = root_device_register("pseudo_0");
  3004. if (IS_ERR(pseudo_primary)) {
  3005. printk(KERN_WARNING "scsi_debug: root_device_register() error\n");
  3006. ret = PTR_ERR(pseudo_primary);
  3007. goto free_vm;
  3008. }
  3009. ret = bus_register(&pseudo_lld_bus);
  3010. if (ret < 0) {
  3011. printk(KERN_WARNING "scsi_debug: bus_register error: %d\n",
  3012. ret);
  3013. goto dev_unreg;
  3014. }
  3015. ret = driver_register(&sdebug_driverfs_driver);
  3016. if (ret < 0) {
  3017. printk(KERN_WARNING "scsi_debug: driver_register error: %d\n",
  3018. ret);
  3019. goto bus_unreg;
  3020. }
  3021. init_all_queued();
  3022. host_to_add = scsi_debug_add_host;
  3023. scsi_debug_add_host = 0;
  3024. for (k = 0; k < host_to_add; k++) {
  3025. if (sdebug_add_adapter()) {
  3026. printk(KERN_ERR "scsi_debug_init: "
  3027. "sdebug_add_adapter failed k=%d\n", k);
  3028. break;
  3029. }
  3030. }
  3031. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  3032. printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
  3033. scsi_debug_add_host);
  3034. }
  3035. return 0;
  3036. bus_unreg:
  3037. bus_unregister(&pseudo_lld_bus);
  3038. dev_unreg:
  3039. root_device_unregister(pseudo_primary);
  3040. free_vm:
  3041. if (map_storep)
  3042. vfree(map_storep);
  3043. if (dif_storep)
  3044. vfree(dif_storep);
  3045. vfree(fake_storep);
  3046. return ret;
  3047. }
  3048. static void __exit scsi_debug_exit(void)
  3049. {
  3050. int k = scsi_debug_add_host;
  3051. stop_all_queued();
  3052. for (; k; k--)
  3053. sdebug_remove_adapter();
  3054. driver_unregister(&sdebug_driverfs_driver);
  3055. bus_unregister(&pseudo_lld_bus);
  3056. root_device_unregister(pseudo_primary);
  3057. if (dif_storep)
  3058. vfree(dif_storep);
  3059. vfree(fake_storep);
  3060. }
  3061. device_initcall(scsi_debug_init);
  3062. module_exit(scsi_debug_exit);
  3063. static void sdebug_release_adapter(struct device * dev)
  3064. {
  3065. struct sdebug_host_info *sdbg_host;
  3066. sdbg_host = to_sdebug_host(dev);
  3067. kfree(sdbg_host);
  3068. }
  3069. static int sdebug_add_adapter(void)
  3070. {
  3071. int k, devs_per_host;
  3072. int error = 0;
  3073. struct sdebug_host_info *sdbg_host;
  3074. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  3075. sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
  3076. if (NULL == sdbg_host) {
  3077. printk(KERN_ERR "%s: out of memory at line %d\n",
  3078. __func__, __LINE__);
  3079. return -ENOMEM;
  3080. }
  3081. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  3082. devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
  3083. for (k = 0; k < devs_per_host; k++) {
  3084. sdbg_devinfo = sdebug_device_create(sdbg_host, GFP_KERNEL);
  3085. if (!sdbg_devinfo) {
  3086. printk(KERN_ERR "%s: out of memory at line %d\n",
  3087. __func__, __LINE__);
  3088. error = -ENOMEM;
  3089. goto clean;
  3090. }
  3091. }
  3092. spin_lock(&sdebug_host_list_lock);
  3093. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  3094. spin_unlock(&sdebug_host_list_lock);
  3095. sdbg_host->dev.bus = &pseudo_lld_bus;
  3096. sdbg_host->dev.parent = pseudo_primary;
  3097. sdbg_host->dev.release = &sdebug_release_adapter;
  3098. dev_set_name(&sdbg_host->dev, "adapter%d", scsi_debug_add_host);
  3099. error = device_register(&sdbg_host->dev);
  3100. if (error)
  3101. goto clean;
  3102. ++scsi_debug_add_host;
  3103. return error;
  3104. clean:
  3105. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  3106. dev_list) {
  3107. list_del(&sdbg_devinfo->dev_list);
  3108. kfree(sdbg_devinfo);
  3109. }
  3110. kfree(sdbg_host);
  3111. return error;
  3112. }
  3113. static void sdebug_remove_adapter(void)
  3114. {
  3115. struct sdebug_host_info * sdbg_host = NULL;
  3116. spin_lock(&sdebug_host_list_lock);
  3117. if (!list_empty(&sdebug_host_list)) {
  3118. sdbg_host = list_entry(sdebug_host_list.prev,
  3119. struct sdebug_host_info, host_list);
  3120. list_del(&sdbg_host->host_list);
  3121. }
  3122. spin_unlock(&sdebug_host_list_lock);
  3123. if (!sdbg_host)
  3124. return;
  3125. device_unregister(&sdbg_host->dev);
  3126. --scsi_debug_add_host;
  3127. }
  3128. static
  3129. int scsi_debug_queuecommand_lck(struct scsi_cmnd *SCpnt, done_funct_t done)
  3130. {
  3131. unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
  3132. int len, k;
  3133. unsigned int num;
  3134. unsigned long long lba;
  3135. u32 ei_lba;
  3136. int errsts = 0;
  3137. int target = SCpnt->device->id;
  3138. struct sdebug_dev_info *devip = NULL;
  3139. int inj_recovered = 0;
  3140. int inj_transport = 0;
  3141. int inj_dif = 0;
  3142. int inj_dix = 0;
  3143. int delay_override = 0;
  3144. int unmap = 0;
  3145. scsi_set_resid(SCpnt, 0);
  3146. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
  3147. printk(KERN_INFO "scsi_debug: cmd ");
  3148. for (k = 0, len = SCpnt->cmd_len; k < len; ++k)
  3149. printk("%02x ", (int)cmd[k]);
  3150. printk("\n");
  3151. }
  3152. if (target == SCpnt->device->host->hostt->this_id) {
  3153. printk(KERN_INFO "scsi_debug: initiator's id used as "
  3154. "target!\n");
  3155. return schedule_resp(SCpnt, NULL, done,
  3156. DID_NO_CONNECT << 16, 0);
  3157. }
  3158. if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
  3159. (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
  3160. return schedule_resp(SCpnt, NULL, done,
  3161. DID_NO_CONNECT << 16, 0);
  3162. devip = devInfoReg(SCpnt->device);
  3163. if (NULL == devip)
  3164. return schedule_resp(SCpnt, NULL, done,
  3165. DID_NO_CONNECT << 16, 0);
  3166. if ((scsi_debug_every_nth != 0) &&
  3167. (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
  3168. scsi_debug_cmnd_count = 0;
  3169. if (scsi_debug_every_nth < -1)
  3170. scsi_debug_every_nth = -1;
  3171. if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
  3172. return 0; /* ignore command causing timeout */
  3173. else if (SCSI_DEBUG_OPT_MAC_TIMEOUT & scsi_debug_opts &&
  3174. scsi_medium_access_command(SCpnt))
  3175. return 0; /* time out reads and writes */
  3176. else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
  3177. inj_recovered = 1; /* to reads and writes below */
  3178. else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & scsi_debug_opts)
  3179. inj_transport = 1; /* to reads and writes below */
  3180. else if (SCSI_DEBUG_OPT_DIF_ERR & scsi_debug_opts)
  3181. inj_dif = 1; /* to reads and writes below */
  3182. else if (SCSI_DEBUG_OPT_DIX_ERR & scsi_debug_opts)
  3183. inj_dix = 1; /* to reads and writes below */
  3184. }
  3185. if (devip->wlun) {
  3186. switch (*cmd) {
  3187. case INQUIRY:
  3188. case REQUEST_SENSE:
  3189. case TEST_UNIT_READY:
  3190. case REPORT_LUNS:
  3191. break; /* only allowable wlun commands */
  3192. default:
  3193. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3194. printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
  3195. "not supported for wlun\n", *cmd);
  3196. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3197. INVALID_OPCODE, 0);
  3198. errsts = check_condition_result;
  3199. return schedule_resp(SCpnt, devip, done, errsts,
  3200. 0);
  3201. }
  3202. }
  3203. switch (*cmd) {
  3204. case INQUIRY: /* mandatory, ignore unit attention */
  3205. delay_override = 1;
  3206. errsts = resp_inquiry(SCpnt, target, devip);
  3207. break;
  3208. case REQUEST_SENSE: /* mandatory, ignore unit attention */
  3209. delay_override = 1;
  3210. errsts = resp_requests(SCpnt, devip);
  3211. break;
  3212. case REZERO_UNIT: /* actually this is REWIND for SSC */
  3213. case START_STOP:
  3214. errsts = resp_start_stop(SCpnt, devip);
  3215. break;
  3216. case ALLOW_MEDIUM_REMOVAL:
  3217. errsts = check_readiness(SCpnt, 1, devip);
  3218. if (errsts)
  3219. break;
  3220. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3221. printk(KERN_INFO "scsi_debug: Medium removal %s\n",
  3222. cmd[4] ? "inhibited" : "enabled");
  3223. break;
  3224. case SEND_DIAGNOSTIC: /* mandatory */
  3225. errsts = check_readiness(SCpnt, 1, devip);
  3226. break;
  3227. case TEST_UNIT_READY: /* mandatory */
  3228. delay_override = 1;
  3229. errsts = check_readiness(SCpnt, 0, devip);
  3230. break;
  3231. case RESERVE:
  3232. errsts = check_readiness(SCpnt, 1, devip);
  3233. break;
  3234. case RESERVE_10:
  3235. errsts = check_readiness(SCpnt, 1, devip);
  3236. break;
  3237. case RELEASE:
  3238. errsts = check_readiness(SCpnt, 1, devip);
  3239. break;
  3240. case RELEASE_10:
  3241. errsts = check_readiness(SCpnt, 1, devip);
  3242. break;
  3243. case READ_CAPACITY:
  3244. errsts = resp_readcap(SCpnt, devip);
  3245. break;
  3246. case SERVICE_ACTION_IN:
  3247. if (cmd[1] == SAI_READ_CAPACITY_16)
  3248. errsts = resp_readcap16(SCpnt, devip);
  3249. else if (cmd[1] == SAI_GET_LBA_STATUS) {
  3250. if (scsi_debug_lbp() == 0) {
  3251. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3252. INVALID_COMMAND_OPCODE, 0);
  3253. errsts = check_condition_result;
  3254. } else
  3255. errsts = resp_get_lba_status(SCpnt, devip);
  3256. } else {
  3257. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3258. INVALID_OPCODE, 0);
  3259. errsts = check_condition_result;
  3260. }
  3261. break;
  3262. case MAINTENANCE_IN:
  3263. if (MI_REPORT_TARGET_PGS != cmd[1]) {
  3264. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3265. INVALID_OPCODE, 0);
  3266. errsts = check_condition_result;
  3267. break;
  3268. }
  3269. errsts = resp_report_tgtpgs(SCpnt, devip);
  3270. break;
  3271. case READ_16:
  3272. case READ_12:
  3273. case READ_10:
  3274. /* READ{10,12,16} and DIF Type 2 are natural enemies */
  3275. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  3276. cmd[1] & 0xe0) {
  3277. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3278. INVALID_COMMAND_OPCODE, 0);
  3279. errsts = check_condition_result;
  3280. break;
  3281. }
  3282. if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
  3283. scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
  3284. (cmd[1] & 0xe0) == 0)
  3285. printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
  3286. /* fall through */
  3287. case READ_6:
  3288. read:
  3289. errsts = check_readiness(SCpnt, 0, devip);
  3290. if (errsts)
  3291. break;
  3292. if (scsi_debug_fake_rw)
  3293. break;
  3294. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3295. errsts = resp_read(SCpnt, lba, num, devip, ei_lba);
  3296. if (inj_recovered && (0 == errsts)) {
  3297. mk_sense_buffer(devip, RECOVERED_ERROR,
  3298. THRESHOLD_EXCEEDED, 0);
  3299. errsts = check_condition_result;
  3300. } else if (inj_transport && (0 == errsts)) {
  3301. mk_sense_buffer(devip, ABORTED_COMMAND,
  3302. TRANSPORT_PROBLEM, ACK_NAK_TO);
  3303. errsts = check_condition_result;
  3304. } else if (inj_dif && (0 == errsts)) {
  3305. mk_sense_buffer(devip, ABORTED_COMMAND, 0x10, 1);
  3306. errsts = illegal_condition_result;
  3307. } else if (inj_dix && (0 == errsts)) {
  3308. mk_sense_buffer(devip, ILLEGAL_REQUEST, 0x10, 1);
  3309. errsts = illegal_condition_result;
  3310. }
  3311. break;
  3312. case REPORT_LUNS: /* mandatory, ignore unit attention */
  3313. delay_override = 1;
  3314. errsts = resp_report_luns(SCpnt, devip);
  3315. break;
  3316. case VERIFY: /* 10 byte SBC-2 command */
  3317. errsts = check_readiness(SCpnt, 0, devip);
  3318. break;
  3319. case WRITE_16:
  3320. case WRITE_12:
  3321. case WRITE_10:
  3322. /* WRITE{10,12,16} and DIF Type 2 are natural enemies */
  3323. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION &&
  3324. cmd[1] & 0xe0) {
  3325. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3326. INVALID_COMMAND_OPCODE, 0);
  3327. errsts = check_condition_result;
  3328. break;
  3329. }
  3330. if ((scsi_debug_dif == SD_DIF_TYPE1_PROTECTION ||
  3331. scsi_debug_dif == SD_DIF_TYPE3_PROTECTION) &&
  3332. (cmd[1] & 0xe0) == 0)
  3333. printk(KERN_ERR "Unprotected RD/WR to DIF device\n");
  3334. /* fall through */
  3335. case WRITE_6:
  3336. write:
  3337. errsts = check_readiness(SCpnt, 0, devip);
  3338. if (errsts)
  3339. break;
  3340. if (scsi_debug_fake_rw)
  3341. break;
  3342. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3343. errsts = resp_write(SCpnt, lba, num, devip, ei_lba);
  3344. if (inj_recovered && (0 == errsts)) {
  3345. mk_sense_buffer(devip, RECOVERED_ERROR,
  3346. THRESHOLD_EXCEEDED, 0);
  3347. errsts = check_condition_result;
  3348. } else if (inj_dif && (0 == errsts)) {
  3349. mk_sense_buffer(devip, ABORTED_COMMAND, 0x10, 1);
  3350. errsts = illegal_condition_result;
  3351. } else if (inj_dix && (0 == errsts)) {
  3352. mk_sense_buffer(devip, ILLEGAL_REQUEST, 0x10, 1);
  3353. errsts = illegal_condition_result;
  3354. }
  3355. break;
  3356. case WRITE_SAME_16:
  3357. case WRITE_SAME:
  3358. if (cmd[1] & 0x8) {
  3359. if ((*cmd == WRITE_SAME_16 && scsi_debug_lbpws == 0) ||
  3360. (*cmd == WRITE_SAME && scsi_debug_lbpws10 == 0)) {
  3361. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3362. INVALID_FIELD_IN_CDB, 0);
  3363. errsts = check_condition_result;
  3364. } else
  3365. unmap = 1;
  3366. }
  3367. if (errsts)
  3368. break;
  3369. errsts = check_readiness(SCpnt, 0, devip);
  3370. if (errsts)
  3371. break;
  3372. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3373. errsts = resp_write_same(SCpnt, lba, num, devip, ei_lba, unmap);
  3374. break;
  3375. case UNMAP:
  3376. errsts = check_readiness(SCpnt, 0, devip);
  3377. if (errsts)
  3378. break;
  3379. if (scsi_debug_unmap_max_desc == 0 || scsi_debug_lbpu == 0) {
  3380. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3381. INVALID_COMMAND_OPCODE, 0);
  3382. errsts = check_condition_result;
  3383. } else
  3384. errsts = resp_unmap(SCpnt, devip);
  3385. break;
  3386. case MODE_SENSE:
  3387. case MODE_SENSE_10:
  3388. errsts = resp_mode_sense(SCpnt, target, devip);
  3389. break;
  3390. case MODE_SELECT:
  3391. errsts = resp_mode_select(SCpnt, 1, devip);
  3392. break;
  3393. case MODE_SELECT_10:
  3394. errsts = resp_mode_select(SCpnt, 0, devip);
  3395. break;
  3396. case LOG_SENSE:
  3397. errsts = resp_log_sense(SCpnt, devip);
  3398. break;
  3399. case SYNCHRONIZE_CACHE:
  3400. delay_override = 1;
  3401. errsts = check_readiness(SCpnt, 0, devip);
  3402. break;
  3403. case WRITE_BUFFER:
  3404. errsts = check_readiness(SCpnt, 1, devip);
  3405. break;
  3406. case XDWRITEREAD_10:
  3407. if (!scsi_bidi_cmnd(SCpnt)) {
  3408. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3409. INVALID_FIELD_IN_CDB, 0);
  3410. errsts = check_condition_result;
  3411. break;
  3412. }
  3413. errsts = check_readiness(SCpnt, 0, devip);
  3414. if (errsts)
  3415. break;
  3416. if (scsi_debug_fake_rw)
  3417. break;
  3418. get_data_transfer_info(cmd, &lba, &num, &ei_lba);
  3419. errsts = resp_read(SCpnt, lba, num, devip, ei_lba);
  3420. if (errsts)
  3421. break;
  3422. errsts = resp_write(SCpnt, lba, num, devip, ei_lba);
  3423. if (errsts)
  3424. break;
  3425. errsts = resp_xdwriteread(SCpnt, lba, num, devip);
  3426. break;
  3427. case VARIABLE_LENGTH_CMD:
  3428. if (scsi_debug_dif == SD_DIF_TYPE2_PROTECTION) {
  3429. if ((cmd[10] & 0xe0) == 0)
  3430. printk(KERN_ERR
  3431. "Unprotected RD/WR to DIF device\n");
  3432. if (cmd[9] == READ_32) {
  3433. BUG_ON(SCpnt->cmd_len < 32);
  3434. goto read;
  3435. }
  3436. if (cmd[9] == WRITE_32) {
  3437. BUG_ON(SCpnt->cmd_len < 32);
  3438. goto write;
  3439. }
  3440. }
  3441. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  3442. INVALID_FIELD_IN_CDB, 0);
  3443. errsts = check_condition_result;
  3444. break;
  3445. default:
  3446. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  3447. printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
  3448. "supported\n", *cmd);
  3449. errsts = check_readiness(SCpnt, 1, devip);
  3450. if (errsts)
  3451. break; /* Unit attention takes precedence */
  3452. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  3453. errsts = check_condition_result;
  3454. break;
  3455. }
  3456. return schedule_resp(SCpnt, devip, done, errsts,
  3457. (delay_override ? 0 : scsi_debug_delay));
  3458. }
  3459. static DEF_SCSI_QCMD(scsi_debug_queuecommand)
  3460. static struct scsi_host_template sdebug_driver_template = {
  3461. .show_info = scsi_debug_show_info,
  3462. .write_info = scsi_debug_write_info,
  3463. .proc_name = sdebug_proc_name,
  3464. .name = "SCSI DEBUG",
  3465. .info = scsi_debug_info,
  3466. .slave_alloc = scsi_debug_slave_alloc,
  3467. .slave_configure = scsi_debug_slave_configure,
  3468. .slave_destroy = scsi_debug_slave_destroy,
  3469. .ioctl = scsi_debug_ioctl,
  3470. .queuecommand = scsi_debug_queuecommand,
  3471. .eh_abort_handler = scsi_debug_abort,
  3472. .eh_bus_reset_handler = scsi_debug_bus_reset,
  3473. .eh_device_reset_handler = scsi_debug_device_reset,
  3474. .eh_host_reset_handler = scsi_debug_host_reset,
  3475. .bios_param = scsi_debug_biosparam,
  3476. .can_queue = SCSI_DEBUG_CANQUEUE,
  3477. .this_id = 7,
  3478. .sg_tablesize = 256,
  3479. .cmd_per_lun = 16,
  3480. .max_sectors = 0xffff,
  3481. .use_clustering = DISABLE_CLUSTERING,
  3482. .module = THIS_MODULE,
  3483. };
  3484. static int sdebug_driver_probe(struct device * dev)
  3485. {
  3486. int error = 0;
  3487. struct sdebug_host_info *sdbg_host;
  3488. struct Scsi_Host *hpnt;
  3489. int host_prot;
  3490. sdbg_host = to_sdebug_host(dev);
  3491. sdebug_driver_template.can_queue = scsi_debug_max_queue;
  3492. if (scsi_debug_clustering)
  3493. sdebug_driver_template.use_clustering = ENABLE_CLUSTERING;
  3494. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  3495. if (NULL == hpnt) {
  3496. printk(KERN_ERR "%s: scsi_register failed\n", __func__);
  3497. error = -ENODEV;
  3498. return error;
  3499. }
  3500. sdbg_host->shost = hpnt;
  3501. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  3502. if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
  3503. hpnt->max_id = scsi_debug_num_tgts + 1;
  3504. else
  3505. hpnt->max_id = scsi_debug_num_tgts;
  3506. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
  3507. host_prot = 0;
  3508. switch (scsi_debug_dif) {
  3509. case SD_DIF_TYPE1_PROTECTION:
  3510. host_prot = SHOST_DIF_TYPE1_PROTECTION;
  3511. if (scsi_debug_dix)
  3512. host_prot |= SHOST_DIX_TYPE1_PROTECTION;
  3513. break;
  3514. case SD_DIF_TYPE2_PROTECTION:
  3515. host_prot = SHOST_DIF_TYPE2_PROTECTION;
  3516. if (scsi_debug_dix)
  3517. host_prot |= SHOST_DIX_TYPE2_PROTECTION;
  3518. break;
  3519. case SD_DIF_TYPE3_PROTECTION:
  3520. host_prot = SHOST_DIF_TYPE3_PROTECTION;
  3521. if (scsi_debug_dix)
  3522. host_prot |= SHOST_DIX_TYPE3_PROTECTION;
  3523. break;
  3524. default:
  3525. if (scsi_debug_dix)
  3526. host_prot |= SHOST_DIX_TYPE0_PROTECTION;
  3527. break;
  3528. }
  3529. scsi_host_set_prot(hpnt, host_prot);
  3530. printk(KERN_INFO "scsi_debug: host protection%s%s%s%s%s%s%s\n",
  3531. (host_prot & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
  3532. (host_prot & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
  3533. (host_prot & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
  3534. (host_prot & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
  3535. (host_prot & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
  3536. (host_prot & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
  3537. (host_prot & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
  3538. if (scsi_debug_guard == 1)
  3539. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_IP);
  3540. else
  3541. scsi_host_set_guard(hpnt, SHOST_DIX_GUARD_CRC);
  3542. error = scsi_add_host(hpnt, &sdbg_host->dev);
  3543. if (error) {
  3544. printk(KERN_ERR "%s: scsi_add_host failed\n", __func__);
  3545. error = -ENODEV;
  3546. scsi_host_put(hpnt);
  3547. } else
  3548. scsi_scan_host(hpnt);
  3549. return error;
  3550. }
  3551. static int sdebug_driver_remove(struct device * dev)
  3552. {
  3553. struct sdebug_host_info *sdbg_host;
  3554. struct sdebug_dev_info *sdbg_devinfo, *tmp;
  3555. sdbg_host = to_sdebug_host(dev);
  3556. if (!sdbg_host) {
  3557. printk(KERN_ERR "%s: Unable to locate host info\n",
  3558. __func__);
  3559. return -ENODEV;
  3560. }
  3561. scsi_remove_host(sdbg_host->shost);
  3562. list_for_each_entry_safe(sdbg_devinfo, tmp, &sdbg_host->dev_info_list,
  3563. dev_list) {
  3564. list_del(&sdbg_devinfo->dev_list);
  3565. kfree(sdbg_devinfo);
  3566. }
  3567. scsi_host_put(sdbg_host->shost);
  3568. return 0;
  3569. }
  3570. static int pseudo_lld_bus_match(struct device *dev,
  3571. struct device_driver *dev_driver)
  3572. {
  3573. return 1;
  3574. }
  3575. static struct bus_type pseudo_lld_bus = {
  3576. .name = "pseudo",
  3577. .match = pseudo_lld_bus_match,
  3578. .probe = sdebug_driver_probe,
  3579. .remove = sdebug_driver_remove,
  3580. .drv_groups = sdebug_drv_groups,
  3581. };