scsi_debug.c 116 KB

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