scsi_debug.c 119 KB

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