scsi_debug.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  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://www.torque.net/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/types.h>
  32. #include <linux/string.h>
  33. #include <linux/genhd.h>
  34. #include <linux/fs.h>
  35. #include <linux/init.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/scatterlist.h>
  40. #include <linux/blkdev.h>
  41. #include "scsi.h"
  42. #include <scsi/scsi_host.h>
  43. #include <scsi/scsicam.h>
  44. #include <linux/stat.h>
  45. #include "scsi_logging.h"
  46. #include "scsi_debug.h"
  47. #define SCSI_DEBUG_VERSION "1.81"
  48. static const char * scsi_debug_version_date = "20070104";
  49. /* Additional Sense Code (ASC) */
  50. #define NO_ADDITIONAL_SENSE 0x0
  51. #define LOGICAL_UNIT_NOT_READY 0x4
  52. #define UNRECOVERED_READ_ERR 0x11
  53. #define PARAMETER_LIST_LENGTH_ERR 0x1a
  54. #define INVALID_OPCODE 0x20
  55. #define ADDR_OUT_OF_RANGE 0x21
  56. #define INVALID_FIELD_IN_CDB 0x24
  57. #define INVALID_FIELD_IN_PARAM_LIST 0x26
  58. #define POWERON_RESET 0x29
  59. #define SAVING_PARAMS_UNSUP 0x39
  60. #define TRANSPORT_PROBLEM 0x4b
  61. #define THRESHOLD_EXCEEDED 0x5d
  62. #define LOW_POWER_COND_ON 0x5e
  63. /* Additional Sense Code Qualifier (ASCQ) */
  64. #define ACK_NAK_TO 0x3
  65. #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
  66. /* Default values for driver parameters */
  67. #define DEF_NUM_HOST 1
  68. #define DEF_NUM_TGTS 1
  69. #define DEF_MAX_LUNS 1
  70. /* With these defaults, this driver will make 1 host with 1 target
  71. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  72. */
  73. #define DEF_DELAY 1
  74. #define DEF_DEV_SIZE_MB 8
  75. #define DEF_EVERY_NTH 0
  76. #define DEF_NUM_PARTS 0
  77. #define DEF_OPTS 0
  78. #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
  79. #define DEF_PTYPE 0
  80. #define DEF_D_SENSE 0
  81. #define DEF_NO_LUN_0 0
  82. #define DEF_VIRTUAL_GB 0
  83. #define DEF_FAKE_RW 0
  84. #define DEF_VPD_USE_HOSTNO 1
  85. /* bit mask values for scsi_debug_opts */
  86. #define SCSI_DEBUG_OPT_NOISE 1
  87. #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
  88. #define SCSI_DEBUG_OPT_TIMEOUT 4
  89. #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
  90. #define SCSI_DEBUG_OPT_TRANSPORT_ERR 16
  91. /* When "every_nth" > 0 then modulo "every_nth" commands:
  92. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  93. * - a RECOVERED_ERROR is simulated on successful read and write
  94. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  95. * - a TRANSPORT_ERROR is simulated on successful read and write
  96. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  97. *
  98. * When "every_nth" < 0 then after "- every_nth" commands:
  99. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  100. * - a RECOVERED_ERROR is simulated on successful read and write
  101. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  102. * - a TRANSPORT_ERROR is simulated on successful read and write
  103. * commands if SCSI_DEBUG_OPT_TRANSPORT_ERR is set.
  104. * This will continue until some other action occurs (e.g. the user
  105. * writing a new value (other than -1 or 1) to every_nth via sysfs).
  106. */
  107. /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  108. * sector on read commands: */
  109. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  110. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  111. * or "peripheral device" addressing (value 0) */
  112. #define SAM2_LUN_ADDRESS_METHOD 0
  113. #define SAM2_WLUN_REPORT_LUNS 0xc101
  114. static int scsi_debug_add_host = DEF_NUM_HOST;
  115. static int scsi_debug_delay = DEF_DELAY;
  116. static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
  117. static int scsi_debug_every_nth = DEF_EVERY_NTH;
  118. static int scsi_debug_max_luns = DEF_MAX_LUNS;
  119. static int scsi_debug_num_parts = DEF_NUM_PARTS;
  120. static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  121. static int scsi_debug_opts = DEF_OPTS;
  122. static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
  123. static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
  124. static int scsi_debug_dsense = DEF_D_SENSE;
  125. static int scsi_debug_no_lun_0 = DEF_NO_LUN_0;
  126. static int scsi_debug_virtual_gb = DEF_VIRTUAL_GB;
  127. static int scsi_debug_fake_rw = DEF_FAKE_RW;
  128. static int scsi_debug_vpd_use_hostno = DEF_VPD_USE_HOSTNO;
  129. static int scsi_debug_cmnd_count = 0;
  130. #define DEV_READONLY(TGT) (0)
  131. #define DEV_REMOVEABLE(TGT) (0)
  132. static unsigned int sdebug_store_size; /* in bytes */
  133. static unsigned int sdebug_store_sectors;
  134. static sector_t sdebug_capacity; /* in sectors */
  135. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  136. may still need them */
  137. static int sdebug_heads; /* heads per disk */
  138. static int sdebug_cylinders_per; /* cylinders per surface */
  139. static int sdebug_sectors_per; /* sectors per cylinder */
  140. /* default sector size is 512 bytes, 2**9 bytes */
  141. #define POW2_SECT_SIZE 9
  142. #define SECT_SIZE (1 << POW2_SECT_SIZE)
  143. #define SECT_SIZE_PER(TGT) SECT_SIZE
  144. #define SDEBUG_MAX_PARTS 4
  145. #define SDEBUG_SENSE_LEN 32
  146. struct sdebug_dev_info {
  147. struct list_head dev_list;
  148. unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
  149. unsigned int channel;
  150. unsigned int target;
  151. unsigned int lun;
  152. struct sdebug_host_info *sdbg_host;
  153. unsigned int wlun;
  154. char reset;
  155. char stopped;
  156. char used;
  157. };
  158. struct sdebug_host_info {
  159. struct list_head host_list;
  160. struct Scsi_Host *shost;
  161. struct device dev;
  162. struct list_head dev_info_list;
  163. };
  164. #define to_sdebug_host(d) \
  165. container_of(d, struct sdebug_host_info, dev)
  166. static LIST_HEAD(sdebug_host_list);
  167. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  168. typedef void (* done_funct_t) (struct scsi_cmnd *);
  169. struct sdebug_queued_cmd {
  170. int in_use;
  171. struct timer_list cmnd_timer;
  172. done_funct_t done_funct;
  173. struct scsi_cmnd * a_cmnd;
  174. int scsi_result;
  175. };
  176. static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
  177. static struct scsi_host_template sdebug_driver_template = {
  178. .proc_info = scsi_debug_proc_info,
  179. .name = "SCSI DEBUG",
  180. .info = scsi_debug_info,
  181. .slave_alloc = scsi_debug_slave_alloc,
  182. .slave_configure = scsi_debug_slave_configure,
  183. .slave_destroy = scsi_debug_slave_destroy,
  184. .ioctl = scsi_debug_ioctl,
  185. .queuecommand = scsi_debug_queuecommand,
  186. .eh_abort_handler = scsi_debug_abort,
  187. .eh_bus_reset_handler = scsi_debug_bus_reset,
  188. .eh_device_reset_handler = scsi_debug_device_reset,
  189. .eh_host_reset_handler = scsi_debug_host_reset,
  190. .bios_param = scsi_debug_biosparam,
  191. .can_queue = SCSI_DEBUG_CANQUEUE,
  192. .this_id = 7,
  193. .sg_tablesize = 256,
  194. .cmd_per_lun = 16,
  195. .max_sectors = 0xffff,
  196. .unchecked_isa_dma = 0,
  197. .use_clustering = ENABLE_CLUSTERING,
  198. .module = THIS_MODULE,
  199. };
  200. static unsigned char * fake_storep; /* ramdisk storage */
  201. static int num_aborts = 0;
  202. static int num_dev_resets = 0;
  203. static int num_bus_resets = 0;
  204. static int num_host_resets = 0;
  205. static DEFINE_SPINLOCK(queued_arr_lock);
  206. static DEFINE_RWLOCK(atomic_rw);
  207. static char sdebug_proc_name[] = "scsi_debug";
  208. static int sdebug_driver_probe(struct device *);
  209. static int sdebug_driver_remove(struct device *);
  210. static struct bus_type pseudo_lld_bus;
  211. static struct device_driver sdebug_driverfs_driver = {
  212. .name = sdebug_proc_name,
  213. .bus = &pseudo_lld_bus,
  214. };
  215. static const int check_condition_result =
  216. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  217. static unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  218. 0, 0, 0x2, 0x4b};
  219. static unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  220. 0, 0, 0x0, 0x0};
  221. /* function declarations */
  222. static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
  223. struct sdebug_dev_info * devip);
  224. static int resp_requests(struct scsi_cmnd * SCpnt,
  225. struct sdebug_dev_info * devip);
  226. static int resp_start_stop(struct scsi_cmnd * scp,
  227. struct sdebug_dev_info * devip);
  228. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  229. struct sdebug_dev_info * devip);
  230. static int resp_readcap(struct scsi_cmnd * SCpnt,
  231. struct sdebug_dev_info * devip);
  232. static int resp_readcap16(struct scsi_cmnd * SCpnt,
  233. struct sdebug_dev_info * devip);
  234. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  235. struct sdebug_dev_info * devip);
  236. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  237. struct sdebug_dev_info * devip);
  238. static int resp_log_sense(struct scsi_cmnd * scp,
  239. struct sdebug_dev_info * devip);
  240. static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
  241. unsigned int num, struct sdebug_dev_info * devip);
  242. static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
  243. unsigned int num, struct sdebug_dev_info * devip);
  244. static int resp_report_luns(struct scsi_cmnd * SCpnt,
  245. struct sdebug_dev_info * devip);
  246. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  247. int arr_len);
  248. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  249. int max_arr_len);
  250. static void timer_intr_handler(unsigned long);
  251. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
  252. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  253. int asc, int asq);
  254. static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
  255. struct sdebug_dev_info * devip);
  256. static int schedule_resp(struct scsi_cmnd * cmnd,
  257. struct sdebug_dev_info * devip,
  258. done_funct_t done, int scsi_result, int delta_jiff);
  259. static void __init sdebug_build_parts(unsigned char * ramp);
  260. static void __init init_all_queued(void);
  261. static void stop_all_queued(void);
  262. static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
  263. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  264. int target_dev_id, int dev_id_num,
  265. const char * dev_id_str, int dev_id_str_len);
  266. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id);
  267. static int do_create_driverfs_files(void);
  268. static void do_remove_driverfs_files(void);
  269. static int sdebug_add_adapter(void);
  270. static void sdebug_remove_adapter(void);
  271. static void sdebug_max_tgts_luns(void);
  272. static struct device pseudo_primary;
  273. static struct bus_type pseudo_lld_bus;
  274. static
  275. int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
  276. {
  277. unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
  278. int len, k, j;
  279. unsigned int num;
  280. unsigned long long lba;
  281. int errsts = 0;
  282. int target = SCpnt->device->id;
  283. struct sdebug_dev_info * devip = NULL;
  284. int inj_recovered = 0;
  285. int inj_transport = 0;
  286. int delay_override = 0;
  287. if (done == NULL)
  288. return 0; /* assume mid level reprocessing command */
  289. scsi_set_resid(SCpnt, 0);
  290. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
  291. printk(KERN_INFO "scsi_debug: cmd ");
  292. for (k = 0, len = SCpnt->cmd_len; k < len; ++k)
  293. printk("%02x ", (int)cmd[k]);
  294. printk("\n");
  295. }
  296. if(target == sdebug_driver_template.this_id) {
  297. printk(KERN_INFO "scsi_debug: initiator's id used as "
  298. "target!\n");
  299. return schedule_resp(SCpnt, NULL, done,
  300. DID_NO_CONNECT << 16, 0);
  301. }
  302. if ((SCpnt->device->lun >= scsi_debug_max_luns) &&
  303. (SCpnt->device->lun != SAM2_WLUN_REPORT_LUNS))
  304. return schedule_resp(SCpnt, NULL, done,
  305. DID_NO_CONNECT << 16, 0);
  306. devip = devInfoReg(SCpnt->device);
  307. if (NULL == devip)
  308. return schedule_resp(SCpnt, NULL, done,
  309. DID_NO_CONNECT << 16, 0);
  310. if ((scsi_debug_every_nth != 0) &&
  311. (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
  312. scsi_debug_cmnd_count = 0;
  313. if (scsi_debug_every_nth < -1)
  314. scsi_debug_every_nth = -1;
  315. if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
  316. return 0; /* ignore command causing timeout */
  317. else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
  318. inj_recovered = 1; /* to reads and writes below */
  319. else if (SCSI_DEBUG_OPT_TRANSPORT_ERR & scsi_debug_opts)
  320. inj_transport = 1; /* to reads and writes below */
  321. }
  322. if (devip->wlun) {
  323. switch (*cmd) {
  324. case INQUIRY:
  325. case REQUEST_SENSE:
  326. case TEST_UNIT_READY:
  327. case REPORT_LUNS:
  328. break; /* only allowable wlun commands */
  329. default:
  330. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  331. printk(KERN_INFO "scsi_debug: Opcode: 0x%x "
  332. "not supported for wlun\n", *cmd);
  333. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  334. INVALID_OPCODE, 0);
  335. errsts = check_condition_result;
  336. return schedule_resp(SCpnt, devip, done, errsts,
  337. 0);
  338. }
  339. }
  340. switch (*cmd) {
  341. case INQUIRY: /* mandatory, ignore unit attention */
  342. delay_override = 1;
  343. errsts = resp_inquiry(SCpnt, target, devip);
  344. break;
  345. case REQUEST_SENSE: /* mandatory, ignore unit attention */
  346. delay_override = 1;
  347. errsts = resp_requests(SCpnt, devip);
  348. break;
  349. case REZERO_UNIT: /* actually this is REWIND for SSC */
  350. case START_STOP:
  351. errsts = resp_start_stop(SCpnt, devip);
  352. break;
  353. case ALLOW_MEDIUM_REMOVAL:
  354. if ((errsts = check_readiness(SCpnt, 1, devip)))
  355. break;
  356. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  357. printk(KERN_INFO "scsi_debug: Medium removal %s\n",
  358. cmd[4] ? "inhibited" : "enabled");
  359. break;
  360. case SEND_DIAGNOSTIC: /* mandatory */
  361. errsts = check_readiness(SCpnt, 1, devip);
  362. break;
  363. case TEST_UNIT_READY: /* mandatory */
  364. delay_override = 1;
  365. errsts = check_readiness(SCpnt, 0, devip);
  366. break;
  367. case RESERVE:
  368. errsts = check_readiness(SCpnt, 1, devip);
  369. break;
  370. case RESERVE_10:
  371. errsts = check_readiness(SCpnt, 1, devip);
  372. break;
  373. case RELEASE:
  374. errsts = check_readiness(SCpnt, 1, devip);
  375. break;
  376. case RELEASE_10:
  377. errsts = check_readiness(SCpnt, 1, devip);
  378. break;
  379. case READ_CAPACITY:
  380. errsts = resp_readcap(SCpnt, devip);
  381. break;
  382. case SERVICE_ACTION_IN:
  383. if (SAI_READ_CAPACITY_16 != cmd[1]) {
  384. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  385. INVALID_OPCODE, 0);
  386. errsts = check_condition_result;
  387. break;
  388. }
  389. errsts = resp_readcap16(SCpnt, devip);
  390. break;
  391. case MAINTENANCE_IN:
  392. if (MI_REPORT_TARGET_PGS != cmd[1]) {
  393. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  394. INVALID_OPCODE, 0);
  395. errsts = check_condition_result;
  396. break;
  397. }
  398. errsts = resp_report_tgtpgs(SCpnt, devip);
  399. break;
  400. case READ_16:
  401. case READ_12:
  402. case READ_10:
  403. case READ_6:
  404. if ((errsts = check_readiness(SCpnt, 0, devip)))
  405. break;
  406. if (scsi_debug_fake_rw)
  407. break;
  408. if ((*cmd) == READ_16) {
  409. for (lba = 0, j = 0; j < 8; ++j) {
  410. if (j > 0)
  411. lba <<= 8;
  412. lba += cmd[2 + j];
  413. }
  414. num = cmd[13] + (cmd[12] << 8) +
  415. (cmd[11] << 16) + (cmd[10] << 24);
  416. } else if ((*cmd) == READ_12) {
  417. lba = cmd[5] + (cmd[4] << 8) +
  418. (cmd[3] << 16) + (cmd[2] << 24);
  419. num = cmd[9] + (cmd[8] << 8) +
  420. (cmd[7] << 16) + (cmd[6] << 24);
  421. } else if ((*cmd) == READ_10) {
  422. lba = cmd[5] + (cmd[4] << 8) +
  423. (cmd[3] << 16) + (cmd[2] << 24);
  424. num = cmd[8] + (cmd[7] << 8);
  425. } else { /* READ (6) */
  426. lba = cmd[3] + (cmd[2] << 8) +
  427. ((cmd[1] & 0x1f) << 16);
  428. num = (0 == cmd[4]) ? 256 : cmd[4];
  429. }
  430. errsts = resp_read(SCpnt, lba, num, devip);
  431. if (inj_recovered && (0 == errsts)) {
  432. mk_sense_buffer(devip, RECOVERED_ERROR,
  433. THRESHOLD_EXCEEDED, 0);
  434. errsts = check_condition_result;
  435. } else if (inj_transport && (0 == errsts)) {
  436. mk_sense_buffer(devip, ABORTED_COMMAND,
  437. TRANSPORT_PROBLEM, ACK_NAK_TO);
  438. errsts = check_condition_result;
  439. }
  440. break;
  441. case REPORT_LUNS: /* mandatory, ignore unit attention */
  442. delay_override = 1;
  443. errsts = resp_report_luns(SCpnt, devip);
  444. break;
  445. case VERIFY: /* 10 byte SBC-2 command */
  446. errsts = check_readiness(SCpnt, 0, devip);
  447. break;
  448. case WRITE_16:
  449. case WRITE_12:
  450. case WRITE_10:
  451. case WRITE_6:
  452. if ((errsts = check_readiness(SCpnt, 0, devip)))
  453. break;
  454. if (scsi_debug_fake_rw)
  455. break;
  456. if ((*cmd) == WRITE_16) {
  457. for (lba = 0, j = 0; j < 8; ++j) {
  458. if (j > 0)
  459. lba <<= 8;
  460. lba += cmd[2 + j];
  461. }
  462. num = cmd[13] + (cmd[12] << 8) +
  463. (cmd[11] << 16) + (cmd[10] << 24);
  464. } else if ((*cmd) == WRITE_12) {
  465. lba = cmd[5] + (cmd[4] << 8) +
  466. (cmd[3] << 16) + (cmd[2] << 24);
  467. num = cmd[9] + (cmd[8] << 8) +
  468. (cmd[7] << 16) + (cmd[6] << 24);
  469. } else if ((*cmd) == WRITE_10) {
  470. lba = cmd[5] + (cmd[4] << 8) +
  471. (cmd[3] << 16) + (cmd[2] << 24);
  472. num = cmd[8] + (cmd[7] << 8);
  473. } else { /* WRITE (6) */
  474. lba = cmd[3] + (cmd[2] << 8) +
  475. ((cmd[1] & 0x1f) << 16);
  476. num = (0 == cmd[4]) ? 256 : cmd[4];
  477. }
  478. errsts = resp_write(SCpnt, lba, num, devip);
  479. if (inj_recovered && (0 == errsts)) {
  480. mk_sense_buffer(devip, RECOVERED_ERROR,
  481. THRESHOLD_EXCEEDED, 0);
  482. errsts = check_condition_result;
  483. }
  484. break;
  485. case MODE_SENSE:
  486. case MODE_SENSE_10:
  487. errsts = resp_mode_sense(SCpnt, target, devip);
  488. break;
  489. case MODE_SELECT:
  490. errsts = resp_mode_select(SCpnt, 1, devip);
  491. break;
  492. case MODE_SELECT_10:
  493. errsts = resp_mode_select(SCpnt, 0, devip);
  494. break;
  495. case LOG_SENSE:
  496. errsts = resp_log_sense(SCpnt, devip);
  497. break;
  498. case SYNCHRONIZE_CACHE:
  499. delay_override = 1;
  500. errsts = check_readiness(SCpnt, 0, devip);
  501. break;
  502. case WRITE_BUFFER:
  503. errsts = check_readiness(SCpnt, 1, devip);
  504. break;
  505. default:
  506. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  507. printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
  508. "supported\n", *cmd);
  509. if ((errsts = check_readiness(SCpnt, 1, devip)))
  510. break; /* Unit attention takes precedence */
  511. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  512. errsts = check_condition_result;
  513. break;
  514. }
  515. return schedule_resp(SCpnt, devip, done, errsts,
  516. (delay_override ? 0 : scsi_debug_delay));
  517. }
  518. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  519. {
  520. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  521. printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
  522. }
  523. return -EINVAL;
  524. /* return -ENOTTY; // correct return but upsets fdisk */
  525. }
  526. static int check_readiness(struct scsi_cmnd * SCpnt, int reset_only,
  527. struct sdebug_dev_info * devip)
  528. {
  529. if (devip->reset) {
  530. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  531. printk(KERN_INFO "scsi_debug: Reporting Unit "
  532. "attention: power on reset\n");
  533. devip->reset = 0;
  534. mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
  535. return check_condition_result;
  536. }
  537. if ((0 == reset_only) && devip->stopped) {
  538. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  539. printk(KERN_INFO "scsi_debug: Reporting Not "
  540. "ready: initializing command required\n");
  541. mk_sense_buffer(devip, NOT_READY, LOGICAL_UNIT_NOT_READY,
  542. 0x2);
  543. return check_condition_result;
  544. }
  545. return 0;
  546. }
  547. /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
  548. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  549. int arr_len)
  550. {
  551. int k, req_len, act_len, len, active;
  552. void * kaddr;
  553. void * kaddr_off;
  554. struct scatterlist * sg;
  555. if (0 == scsi_bufflen(scp))
  556. return 0;
  557. if (NULL == scsi_sglist(scp))
  558. return (DID_ERROR << 16);
  559. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  560. (scp->sc_data_direction == DMA_FROM_DEVICE)))
  561. return (DID_ERROR << 16);
  562. active = 1;
  563. req_len = act_len = 0;
  564. scsi_for_each_sg(scp, sg, scsi_sg_count(scp), k) {
  565. if (active) {
  566. kaddr = (unsigned char *)
  567. kmap_atomic(sg_page(sg), KM_USER0);
  568. if (NULL == kaddr)
  569. return (DID_ERROR << 16);
  570. kaddr_off = (unsigned char *)kaddr + sg->offset;
  571. len = sg->length;
  572. if ((req_len + len) > arr_len) {
  573. active = 0;
  574. len = arr_len - req_len;
  575. }
  576. memcpy(kaddr_off, arr + req_len, len);
  577. kunmap_atomic(kaddr, KM_USER0);
  578. act_len += len;
  579. }
  580. req_len += sg->length;
  581. }
  582. if (scsi_get_resid(scp))
  583. scsi_set_resid(scp, scsi_get_resid(scp) - act_len);
  584. else
  585. scsi_set_resid(scp, req_len - act_len);
  586. return 0;
  587. }
  588. /* Returns number of bytes fetched into 'arr' or -1 if error. */
  589. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  590. int max_arr_len)
  591. {
  592. int k, req_len, len, fin;
  593. void * kaddr;
  594. void * kaddr_off;
  595. struct scatterlist * sg;
  596. if (0 == scsi_bufflen(scp))
  597. return 0;
  598. if (NULL == scsi_sglist(scp))
  599. return -1;
  600. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  601. (scp->sc_data_direction == DMA_TO_DEVICE)))
  602. return -1;
  603. req_len = fin = 0;
  604. scsi_for_each_sg(scp, sg, scsi_sg_count(scp), k) {
  605. kaddr = (unsigned char *)kmap_atomic(sg_page(sg), KM_USER0);
  606. if (NULL == kaddr)
  607. return -1;
  608. kaddr_off = (unsigned char *)kaddr + sg->offset;
  609. len = sg->length;
  610. if ((req_len + len) > max_arr_len) {
  611. len = max_arr_len - req_len;
  612. fin = 1;
  613. }
  614. memcpy(arr + req_len, kaddr_off, len);
  615. kunmap_atomic(kaddr, KM_USER0);
  616. if (fin)
  617. return req_len + len;
  618. req_len += sg->length;
  619. }
  620. return req_len;
  621. }
  622. static const char * inq_vendor_id = "Linux ";
  623. static const char * inq_product_id = "scsi_debug ";
  624. static const char * inq_product_rev = "0004";
  625. static int inquiry_evpd_83(unsigned char * arr, int port_group_id,
  626. int target_dev_id, int dev_id_num,
  627. const char * dev_id_str,
  628. int dev_id_str_len)
  629. {
  630. int num, port_a;
  631. char b[32];
  632. port_a = target_dev_id + 1;
  633. /* T10 vendor identifier field format (faked) */
  634. arr[0] = 0x2; /* ASCII */
  635. arr[1] = 0x1;
  636. arr[2] = 0x0;
  637. memcpy(&arr[4], inq_vendor_id, 8);
  638. memcpy(&arr[12], inq_product_id, 16);
  639. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  640. num = 8 + 16 + dev_id_str_len;
  641. arr[3] = num;
  642. num += 4;
  643. if (dev_id_num >= 0) {
  644. /* NAA-5, Logical unit identifier (binary) */
  645. arr[num++] = 0x1; /* binary (not necessarily sas) */
  646. arr[num++] = 0x3; /* PIV=0, lu, naa */
  647. arr[num++] = 0x0;
  648. arr[num++] = 0x8;
  649. arr[num++] = 0x53; /* naa-5 ieee company id=0x333333 (fake) */
  650. arr[num++] = 0x33;
  651. arr[num++] = 0x33;
  652. arr[num++] = 0x30;
  653. arr[num++] = (dev_id_num >> 24);
  654. arr[num++] = (dev_id_num >> 16) & 0xff;
  655. arr[num++] = (dev_id_num >> 8) & 0xff;
  656. arr[num++] = dev_id_num & 0xff;
  657. /* Target relative port number */
  658. arr[num++] = 0x61; /* proto=sas, binary */
  659. arr[num++] = 0x94; /* PIV=1, target port, rel port */
  660. arr[num++] = 0x0; /* reserved */
  661. arr[num++] = 0x4; /* length */
  662. arr[num++] = 0x0; /* reserved */
  663. arr[num++] = 0x0; /* reserved */
  664. arr[num++] = 0x0;
  665. arr[num++] = 0x1; /* relative port A */
  666. }
  667. /* NAA-5, Target port identifier */
  668. arr[num++] = 0x61; /* proto=sas, binary */
  669. arr[num++] = 0x93; /* piv=1, target port, naa */
  670. arr[num++] = 0x0;
  671. arr[num++] = 0x8;
  672. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  673. arr[num++] = 0x22;
  674. arr[num++] = 0x22;
  675. arr[num++] = 0x20;
  676. arr[num++] = (port_a >> 24);
  677. arr[num++] = (port_a >> 16) & 0xff;
  678. arr[num++] = (port_a >> 8) & 0xff;
  679. arr[num++] = port_a & 0xff;
  680. /* NAA-5, Target port group identifier */
  681. arr[num++] = 0x61; /* proto=sas, binary */
  682. arr[num++] = 0x95; /* piv=1, target port group id */
  683. arr[num++] = 0x0;
  684. arr[num++] = 0x4;
  685. arr[num++] = 0;
  686. arr[num++] = 0;
  687. arr[num++] = (port_group_id >> 8) & 0xff;
  688. arr[num++] = port_group_id & 0xff;
  689. /* NAA-5, Target device identifier */
  690. arr[num++] = 0x61; /* proto=sas, binary */
  691. arr[num++] = 0xa3; /* piv=1, target device, naa */
  692. arr[num++] = 0x0;
  693. arr[num++] = 0x8;
  694. arr[num++] = 0x52; /* naa-5, company id=0x222222 (fake) */
  695. arr[num++] = 0x22;
  696. arr[num++] = 0x22;
  697. arr[num++] = 0x20;
  698. arr[num++] = (target_dev_id >> 24);
  699. arr[num++] = (target_dev_id >> 16) & 0xff;
  700. arr[num++] = (target_dev_id >> 8) & 0xff;
  701. arr[num++] = target_dev_id & 0xff;
  702. /* SCSI name string: Target device identifier */
  703. arr[num++] = 0x63; /* proto=sas, UTF-8 */
  704. arr[num++] = 0xa8; /* piv=1, target device, SCSI name string */
  705. arr[num++] = 0x0;
  706. arr[num++] = 24;
  707. memcpy(arr + num, "naa.52222220", 12);
  708. num += 12;
  709. snprintf(b, sizeof(b), "%08X", target_dev_id);
  710. memcpy(arr + num, b, 8);
  711. num += 8;
  712. memset(arr + num, 0, 4);
  713. num += 4;
  714. return num;
  715. }
  716. static unsigned char vpd84_data[] = {
  717. /* from 4th byte */ 0x22,0x22,0x22,0x0,0xbb,0x0,
  718. 0x22,0x22,0x22,0x0,0xbb,0x1,
  719. 0x22,0x22,0x22,0x0,0xbb,0x2,
  720. };
  721. static int inquiry_evpd_84(unsigned char * arr)
  722. {
  723. memcpy(arr, vpd84_data, sizeof(vpd84_data));
  724. return sizeof(vpd84_data);
  725. }
  726. static int inquiry_evpd_85(unsigned char * arr)
  727. {
  728. int num = 0;
  729. const char * na1 = "https://www.kernel.org/config";
  730. const char * na2 = "http://www.kernel.org/log";
  731. int plen, olen;
  732. arr[num++] = 0x1; /* lu, storage config */
  733. arr[num++] = 0x0; /* reserved */
  734. arr[num++] = 0x0;
  735. olen = strlen(na1);
  736. plen = olen + 1;
  737. if (plen % 4)
  738. plen = ((plen / 4) + 1) * 4;
  739. arr[num++] = plen; /* length, null termianted, padded */
  740. memcpy(arr + num, na1, olen);
  741. memset(arr + num + olen, 0, plen - olen);
  742. num += plen;
  743. arr[num++] = 0x4; /* lu, logging */
  744. arr[num++] = 0x0; /* reserved */
  745. arr[num++] = 0x0;
  746. olen = strlen(na2);
  747. plen = olen + 1;
  748. if (plen % 4)
  749. plen = ((plen / 4) + 1) * 4;
  750. arr[num++] = plen; /* length, null terminated, padded */
  751. memcpy(arr + num, na2, olen);
  752. memset(arr + num + olen, 0, plen - olen);
  753. num += plen;
  754. return num;
  755. }
  756. /* SCSI ports VPD page */
  757. static int inquiry_evpd_88(unsigned char * arr, int target_dev_id)
  758. {
  759. int num = 0;
  760. int port_a, port_b;
  761. port_a = target_dev_id + 1;
  762. port_b = port_a + 1;
  763. arr[num++] = 0x0; /* reserved */
  764. arr[num++] = 0x0; /* reserved */
  765. arr[num++] = 0x0;
  766. arr[num++] = 0x1; /* relative port 1 (primary) */
  767. memset(arr + num, 0, 6);
  768. num += 6;
  769. arr[num++] = 0x0;
  770. arr[num++] = 12; /* length tp descriptor */
  771. /* naa-5 target port identifier (A) */
  772. arr[num++] = 0x61; /* proto=sas, binary */
  773. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  774. arr[num++] = 0x0; /* reserved */
  775. arr[num++] = 0x8; /* length */
  776. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  777. arr[num++] = 0x22;
  778. arr[num++] = 0x22;
  779. arr[num++] = 0x20;
  780. arr[num++] = (port_a >> 24);
  781. arr[num++] = (port_a >> 16) & 0xff;
  782. arr[num++] = (port_a >> 8) & 0xff;
  783. arr[num++] = port_a & 0xff;
  784. arr[num++] = 0x0; /* reserved */
  785. arr[num++] = 0x0; /* reserved */
  786. arr[num++] = 0x0;
  787. arr[num++] = 0x2; /* relative port 2 (secondary) */
  788. memset(arr + num, 0, 6);
  789. num += 6;
  790. arr[num++] = 0x0;
  791. arr[num++] = 12; /* length tp descriptor */
  792. /* naa-5 target port identifier (B) */
  793. arr[num++] = 0x61; /* proto=sas, binary */
  794. arr[num++] = 0x93; /* PIV=1, target port, NAA */
  795. arr[num++] = 0x0; /* reserved */
  796. arr[num++] = 0x8; /* length */
  797. arr[num++] = 0x52; /* NAA-5, company_id=0x222222 (fake) */
  798. arr[num++] = 0x22;
  799. arr[num++] = 0x22;
  800. arr[num++] = 0x20;
  801. arr[num++] = (port_b >> 24);
  802. arr[num++] = (port_b >> 16) & 0xff;
  803. arr[num++] = (port_b >> 8) & 0xff;
  804. arr[num++] = port_b & 0xff;
  805. return num;
  806. }
  807. static unsigned char vpd89_data[] = {
  808. /* from 4th byte */ 0,0,0,0,
  809. 'l','i','n','u','x',' ',' ',' ',
  810. 'S','A','T',' ','s','c','s','i','_','d','e','b','u','g',' ',' ',
  811. '1','2','3','4',
  812. 0x34,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  813. 0xec,0,0,0,
  814. 0x5a,0xc,0xff,0x3f,0x37,0xc8,0x10,0,0,0,0,0,0x3f,0,0,0,
  815. 0,0,0,0,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x58,0x20,0x20,0x20,0x20,
  816. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0,0,0,0x40,0x4,0,0x2e,0x33,
  817. 0x38,0x31,0x20,0x20,0x20,0x20,0x54,0x53,0x38,0x33,0x30,0x30,0x33,0x31,
  818. 0x53,0x41,
  819. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  820. 0x20,0x20,
  821. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  822. 0x10,0x80,
  823. 0,0,0,0x2f,0,0,0,0x2,0,0x2,0x7,0,0xff,0xff,0x1,0,
  824. 0x3f,0,0xc1,0xff,0x3e,0,0x10,0x1,0xb0,0xf8,0x50,0x9,0,0,0x7,0,
  825. 0x3,0,0x78,0,0x78,0,0xf0,0,0x78,0,0,0,0,0,0,0,
  826. 0,0,0,0,0,0,0,0,0x2,0,0,0,0,0,0,0,
  827. 0x7e,0,0x1b,0,0x6b,0x34,0x1,0x7d,0x3,0x40,0x69,0x34,0x1,0x3c,0x3,0x40,
  828. 0x7f,0x40,0,0,0,0,0xfe,0xfe,0,0,0,0,0,0xfe,0,0,
  829. 0,0,0,0,0,0,0,0,0xb0,0xf8,0x50,0x9,0,0,0,0,
  830. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  831. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  832. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  833. 0x1,0,0xb0,0xf8,0x50,0x9,0xb0,0xf8,0x50,0x9,0x20,0x20,0x2,0,0xb6,0x42,
  834. 0,0x80,0x8a,0,0x6,0x3c,0xa,0x3c,0xff,0xff,0xc6,0x7,0,0x1,0,0x8,
  835. 0xf0,0xf,0,0x10,0x2,0,0x30,0,0,0,0,0,0,0,0x6,0xfe,
  836. 0,0,0x2,0,0x50,0,0x8a,0,0x4f,0x95,0,0,0x21,0,0xb,0,
  837. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  838. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  839. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  840. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  841. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  842. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  843. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  844. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  845. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  846. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  847. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  848. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xa5,0x51,
  849. };
  850. static int inquiry_evpd_89(unsigned char * arr)
  851. {
  852. memcpy(arr, vpd89_data, sizeof(vpd89_data));
  853. return sizeof(vpd89_data);
  854. }
  855. static unsigned char vpdb0_data[] = {
  856. /* from 4th byte */ 0,0,0,4,
  857. 0,0,0x4,0,
  858. 0,0,0,64,
  859. };
  860. static int inquiry_evpd_b0(unsigned char * arr)
  861. {
  862. memcpy(arr, vpdb0_data, sizeof(vpdb0_data));
  863. if (sdebug_store_sectors > 0x400) {
  864. arr[4] = (sdebug_store_sectors >> 24) & 0xff;
  865. arr[5] = (sdebug_store_sectors >> 16) & 0xff;
  866. arr[6] = (sdebug_store_sectors >> 8) & 0xff;
  867. arr[7] = sdebug_store_sectors & 0xff;
  868. }
  869. return sizeof(vpdb0_data);
  870. }
  871. #define SDEBUG_LONG_INQ_SZ 96
  872. #define SDEBUG_MAX_INQ_ARR_SZ 584
  873. static int resp_inquiry(struct scsi_cmnd * scp, int target,
  874. struct sdebug_dev_info * devip)
  875. {
  876. unsigned char pq_pdt;
  877. unsigned char * arr;
  878. unsigned char *cmd = (unsigned char *)scp->cmnd;
  879. int alloc_len, n, ret;
  880. alloc_len = (cmd[3] << 8) + cmd[4];
  881. arr = kzalloc(SDEBUG_MAX_INQ_ARR_SZ, GFP_ATOMIC);
  882. if (! arr)
  883. return DID_REQUEUE << 16;
  884. if (devip->wlun)
  885. pq_pdt = 0x1e; /* present, wlun */
  886. else if (scsi_debug_no_lun_0 && (0 == devip->lun))
  887. pq_pdt = 0x7f; /* not present, no device type */
  888. else
  889. pq_pdt = (scsi_debug_ptype & 0x1f);
  890. arr[0] = pq_pdt;
  891. if (0x2 & cmd[1]) { /* CMDDT bit set */
  892. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  893. 0);
  894. kfree(arr);
  895. return check_condition_result;
  896. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  897. int lu_id_num, port_group_id, target_dev_id, len;
  898. char lu_id_str[6];
  899. int host_no = devip->sdbg_host->shost->host_no;
  900. port_group_id = (((host_no + 1) & 0x7f) << 8) +
  901. (devip->channel & 0x7f);
  902. if (0 == scsi_debug_vpd_use_hostno)
  903. host_no = 0;
  904. lu_id_num = devip->wlun ? -1 : (((host_no + 1) * 2000) +
  905. (devip->target * 1000) + devip->lun);
  906. target_dev_id = ((host_no + 1) * 2000) +
  907. (devip->target * 1000) - 3;
  908. len = scnprintf(lu_id_str, 6, "%d", lu_id_num);
  909. if (0 == cmd[2]) { /* supported vital product data pages */
  910. arr[1] = cmd[2]; /*sanity */
  911. n = 4;
  912. arr[n++] = 0x0; /* this page */
  913. arr[n++] = 0x80; /* unit serial number */
  914. arr[n++] = 0x83; /* device identification */
  915. arr[n++] = 0x84; /* software interface ident. */
  916. arr[n++] = 0x85; /* management network addresses */
  917. arr[n++] = 0x86; /* extended inquiry */
  918. arr[n++] = 0x87; /* mode page policy */
  919. arr[n++] = 0x88; /* SCSI ports */
  920. arr[n++] = 0x89; /* ATA information */
  921. arr[n++] = 0xb0; /* Block limits (SBC) */
  922. arr[3] = n - 4; /* number of supported VPD pages */
  923. } else if (0x80 == cmd[2]) { /* unit serial number */
  924. arr[1] = cmd[2]; /*sanity */
  925. arr[3] = len;
  926. memcpy(&arr[4], lu_id_str, len);
  927. } else if (0x83 == cmd[2]) { /* device identification */
  928. arr[1] = cmd[2]; /*sanity */
  929. arr[3] = inquiry_evpd_83(&arr[4], port_group_id,
  930. target_dev_id, lu_id_num,
  931. lu_id_str, len);
  932. } else if (0x84 == cmd[2]) { /* Software interface ident. */
  933. arr[1] = cmd[2]; /*sanity */
  934. arr[3] = inquiry_evpd_84(&arr[4]);
  935. } else if (0x85 == cmd[2]) { /* Management network addresses */
  936. arr[1] = cmd[2]; /*sanity */
  937. arr[3] = inquiry_evpd_85(&arr[4]);
  938. } else if (0x86 == cmd[2]) { /* extended inquiry */
  939. arr[1] = cmd[2]; /*sanity */
  940. arr[3] = 0x3c; /* number of following entries */
  941. arr[4] = 0x0; /* no protection stuff */
  942. arr[5] = 0x7; /* head of q, ordered + simple q's */
  943. } else if (0x87 == cmd[2]) { /* mode page policy */
  944. arr[1] = cmd[2]; /*sanity */
  945. arr[3] = 0x8; /* number of following entries */
  946. arr[4] = 0x2; /* disconnect-reconnect mp */
  947. arr[6] = 0x80; /* mlus, shared */
  948. arr[8] = 0x18; /* protocol specific lu */
  949. arr[10] = 0x82; /* mlus, per initiator port */
  950. } else if (0x88 == cmd[2]) { /* SCSI Ports */
  951. arr[1] = cmd[2]; /*sanity */
  952. arr[3] = inquiry_evpd_88(&arr[4], target_dev_id);
  953. } else if (0x89 == cmd[2]) { /* ATA information */
  954. arr[1] = cmd[2]; /*sanity */
  955. n = inquiry_evpd_89(&arr[4]);
  956. arr[2] = (n >> 8);
  957. arr[3] = (n & 0xff);
  958. } else if (0xb0 == cmd[2]) { /* Block limits (SBC) */
  959. arr[1] = cmd[2]; /*sanity */
  960. arr[3] = inquiry_evpd_b0(&arr[4]);
  961. } else {
  962. /* Illegal request, invalid field in cdb */
  963. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  964. INVALID_FIELD_IN_CDB, 0);
  965. kfree(arr);
  966. return check_condition_result;
  967. }
  968. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  969. ret = fill_from_dev_buffer(scp, arr,
  970. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  971. kfree(arr);
  972. return ret;
  973. }
  974. /* drops through here for a standard inquiry */
  975. arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
  976. arr[2] = scsi_debug_scsi_level;
  977. arr[3] = 2; /* response_data_format==2 */
  978. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  979. if (0 == scsi_debug_vpd_use_hostno)
  980. arr[5] = 0x10; /* claim: implicit TGPS */
  981. arr[6] = 0x10; /* claim: MultiP */
  982. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  983. arr[7] = 0xa; /* claim: LINKED + CMDQUE */
  984. memcpy(&arr[8], inq_vendor_id, 8);
  985. memcpy(&arr[16], inq_product_id, 16);
  986. memcpy(&arr[32], inq_product_rev, 4);
  987. /* version descriptors (2 bytes each) follow */
  988. arr[58] = 0x0; arr[59] = 0x77; /* SAM-3 ANSI */
  989. arr[60] = 0x3; arr[61] = 0x14; /* SPC-3 ANSI */
  990. n = 62;
  991. if (scsi_debug_ptype == 0) {
  992. arr[n++] = 0x3; arr[n++] = 0x3d; /* SBC-2 ANSI */
  993. } else if (scsi_debug_ptype == 1) {
  994. arr[n++] = 0x3; arr[n++] = 0x60; /* SSC-2 no version */
  995. }
  996. arr[n++] = 0xc; arr[n++] = 0xf; /* SAS-1.1 rev 10 */
  997. ret = fill_from_dev_buffer(scp, arr,
  998. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  999. kfree(arr);
  1000. return ret;
  1001. }
  1002. static int resp_requests(struct scsi_cmnd * scp,
  1003. struct sdebug_dev_info * devip)
  1004. {
  1005. unsigned char * sbuff;
  1006. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1007. unsigned char arr[SDEBUG_SENSE_LEN];
  1008. int want_dsense;
  1009. int len = 18;
  1010. memset(arr, 0, sizeof(arr));
  1011. if (devip->reset == 1)
  1012. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  1013. want_dsense = !!(cmd[1] & 1) || scsi_debug_dsense;
  1014. sbuff = devip->sense_buff;
  1015. if ((iec_m_pg[2] & 0x4) && (6 == (iec_m_pg[3] & 0xf))) {
  1016. if (want_dsense) {
  1017. arr[0] = 0x72;
  1018. arr[1] = 0x0; /* NO_SENSE in sense_key */
  1019. arr[2] = THRESHOLD_EXCEEDED;
  1020. arr[3] = 0xff; /* TEST set and MRIE==6 */
  1021. } else {
  1022. arr[0] = 0x70;
  1023. arr[2] = 0x0; /* NO_SENSE in sense_key */
  1024. arr[7] = 0xa; /* 18 byte sense buffer */
  1025. arr[12] = THRESHOLD_EXCEEDED;
  1026. arr[13] = 0xff; /* TEST set and MRIE==6 */
  1027. }
  1028. } else {
  1029. memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
  1030. if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
  1031. /* DESC bit set and sense_buff in fixed format */
  1032. memset(arr, 0, sizeof(arr));
  1033. arr[0] = 0x72;
  1034. arr[1] = sbuff[2]; /* sense key */
  1035. arr[2] = sbuff[12]; /* asc */
  1036. arr[3] = sbuff[13]; /* ascq */
  1037. len = 8;
  1038. }
  1039. }
  1040. mk_sense_buffer(devip, 0, NO_ADDITIONAL_SENSE, 0);
  1041. return fill_from_dev_buffer(scp, arr, len);
  1042. }
  1043. static int resp_start_stop(struct scsi_cmnd * scp,
  1044. struct sdebug_dev_info * devip)
  1045. {
  1046. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1047. int power_cond, errsts, start;
  1048. if ((errsts = check_readiness(scp, 1, devip)))
  1049. return errsts;
  1050. power_cond = (cmd[4] & 0xf0) >> 4;
  1051. if (power_cond) {
  1052. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1053. 0);
  1054. return check_condition_result;
  1055. }
  1056. start = cmd[4] & 1;
  1057. if (start == devip->stopped)
  1058. devip->stopped = !start;
  1059. return 0;
  1060. }
  1061. #define SDEBUG_READCAP_ARR_SZ 8
  1062. static int resp_readcap(struct scsi_cmnd * scp,
  1063. struct sdebug_dev_info * devip)
  1064. {
  1065. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  1066. unsigned int capac;
  1067. int errsts;
  1068. if ((errsts = check_readiness(scp, 1, devip)))
  1069. return errsts;
  1070. /* following just in case virtual_gb changed */
  1071. if (scsi_debug_virtual_gb > 0) {
  1072. sdebug_capacity = 2048 * 1024;
  1073. sdebug_capacity *= scsi_debug_virtual_gb;
  1074. } else
  1075. sdebug_capacity = sdebug_store_sectors;
  1076. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  1077. if (sdebug_capacity < 0xffffffff) {
  1078. capac = (unsigned int)sdebug_capacity - 1;
  1079. arr[0] = (capac >> 24);
  1080. arr[1] = (capac >> 16) & 0xff;
  1081. arr[2] = (capac >> 8) & 0xff;
  1082. arr[3] = capac & 0xff;
  1083. } else {
  1084. arr[0] = 0xff;
  1085. arr[1] = 0xff;
  1086. arr[2] = 0xff;
  1087. arr[3] = 0xff;
  1088. }
  1089. arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1090. arr[7] = SECT_SIZE_PER(target) & 0xff;
  1091. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  1092. }
  1093. #define SDEBUG_READCAP16_ARR_SZ 32
  1094. static int resp_readcap16(struct scsi_cmnd * scp,
  1095. struct sdebug_dev_info * devip)
  1096. {
  1097. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1098. unsigned char arr[SDEBUG_READCAP16_ARR_SZ];
  1099. unsigned long long capac;
  1100. int errsts, k, alloc_len;
  1101. if ((errsts = check_readiness(scp, 1, devip)))
  1102. return errsts;
  1103. alloc_len = ((cmd[10] << 24) + (cmd[11] << 16) + (cmd[12] << 8)
  1104. + cmd[13]);
  1105. /* following just in case virtual_gb changed */
  1106. if (scsi_debug_virtual_gb > 0) {
  1107. sdebug_capacity = 2048 * 1024;
  1108. sdebug_capacity *= scsi_debug_virtual_gb;
  1109. } else
  1110. sdebug_capacity = sdebug_store_sectors;
  1111. memset(arr, 0, SDEBUG_READCAP16_ARR_SZ);
  1112. capac = sdebug_capacity - 1;
  1113. for (k = 0; k < 8; ++k, capac >>= 8)
  1114. arr[7 - k] = capac & 0xff;
  1115. arr[8] = (SECT_SIZE_PER(target) >> 24) & 0xff;
  1116. arr[9] = (SECT_SIZE_PER(target) >> 16) & 0xff;
  1117. arr[10] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1118. arr[11] = SECT_SIZE_PER(target) & 0xff;
  1119. return fill_from_dev_buffer(scp, arr,
  1120. min(alloc_len, SDEBUG_READCAP16_ARR_SZ));
  1121. }
  1122. #define SDEBUG_MAX_TGTPGS_ARR_SZ 1412
  1123. static int resp_report_tgtpgs(struct scsi_cmnd * scp,
  1124. struct sdebug_dev_info * devip)
  1125. {
  1126. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1127. unsigned char * arr;
  1128. int host_no = devip->sdbg_host->shost->host_no;
  1129. int n, ret, alen, rlen;
  1130. int port_group_a, port_group_b, port_a, port_b;
  1131. alen = ((cmd[6] << 24) + (cmd[7] << 16) + (cmd[8] << 8)
  1132. + cmd[9]);
  1133. arr = kzalloc(SDEBUG_MAX_TGTPGS_ARR_SZ, GFP_ATOMIC);
  1134. if (! arr)
  1135. return DID_REQUEUE << 16;
  1136. /*
  1137. * EVPD page 0x88 states we have two ports, one
  1138. * real and a fake port with no device connected.
  1139. * So we create two port groups with one port each
  1140. * and set the group with port B to unavailable.
  1141. */
  1142. port_a = 0x1; /* relative port A */
  1143. port_b = 0x2; /* relative port B */
  1144. port_group_a = (((host_no + 1) & 0x7f) << 8) +
  1145. (devip->channel & 0x7f);
  1146. port_group_b = (((host_no + 1) & 0x7f) << 8) +
  1147. (devip->channel & 0x7f) + 0x80;
  1148. /*
  1149. * The asymmetric access state is cycled according to the host_id.
  1150. */
  1151. n = 4;
  1152. if (0 == scsi_debug_vpd_use_hostno) {
  1153. arr[n++] = host_no % 3; /* Asymm access state */
  1154. arr[n++] = 0x0F; /* claim: all states are supported */
  1155. } else {
  1156. arr[n++] = 0x0; /* Active/Optimized path */
  1157. arr[n++] = 0x01; /* claim: only support active/optimized paths */
  1158. }
  1159. arr[n++] = (port_group_a >> 8) & 0xff;
  1160. arr[n++] = port_group_a & 0xff;
  1161. arr[n++] = 0; /* Reserved */
  1162. arr[n++] = 0; /* Status code */
  1163. arr[n++] = 0; /* Vendor unique */
  1164. arr[n++] = 0x1; /* One port per group */
  1165. arr[n++] = 0; /* Reserved */
  1166. arr[n++] = 0; /* Reserved */
  1167. arr[n++] = (port_a >> 8) & 0xff;
  1168. arr[n++] = port_a & 0xff;
  1169. arr[n++] = 3; /* Port unavailable */
  1170. arr[n++] = 0x08; /* claim: only unavailalbe paths are supported */
  1171. arr[n++] = (port_group_b >> 8) & 0xff;
  1172. arr[n++] = port_group_b & 0xff;
  1173. arr[n++] = 0; /* Reserved */
  1174. arr[n++] = 0; /* Status code */
  1175. arr[n++] = 0; /* Vendor unique */
  1176. arr[n++] = 0x1; /* One port per group */
  1177. arr[n++] = 0; /* Reserved */
  1178. arr[n++] = 0; /* Reserved */
  1179. arr[n++] = (port_b >> 8) & 0xff;
  1180. arr[n++] = port_b & 0xff;
  1181. rlen = n - 4;
  1182. arr[0] = (rlen >> 24) & 0xff;
  1183. arr[1] = (rlen >> 16) & 0xff;
  1184. arr[2] = (rlen >> 8) & 0xff;
  1185. arr[3] = rlen & 0xff;
  1186. /*
  1187. * Return the smallest value of either
  1188. * - The allocated length
  1189. * - The constructed command length
  1190. * - The maximum array size
  1191. */
  1192. rlen = min(alen,n);
  1193. ret = fill_from_dev_buffer(scp, arr,
  1194. min(rlen, SDEBUG_MAX_TGTPGS_ARR_SZ));
  1195. kfree(arr);
  1196. return ret;
  1197. }
  1198. /* <<Following mode page info copied from ST318451LW>> */
  1199. static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
  1200. { /* Read-Write Error Recovery page for mode_sense */
  1201. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  1202. 5, 0, 0xff, 0xff};
  1203. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  1204. if (1 == pcontrol)
  1205. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  1206. return sizeof(err_recov_pg);
  1207. }
  1208. static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
  1209. { /* Disconnect-Reconnect page for mode_sense */
  1210. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  1211. 0, 0, 0, 0, 0, 0, 0, 0};
  1212. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  1213. if (1 == pcontrol)
  1214. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  1215. return sizeof(disconnect_pg);
  1216. }
  1217. static int resp_format_pg(unsigned char * p, int pcontrol, int target)
  1218. { /* Format device page for mode_sense */
  1219. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  1220. 0, 0, 0, 0, 0, 0, 0, 0,
  1221. 0, 0, 0, 0, 0x40, 0, 0, 0};
  1222. memcpy(p, format_pg, sizeof(format_pg));
  1223. p[10] = (sdebug_sectors_per >> 8) & 0xff;
  1224. p[11] = sdebug_sectors_per & 0xff;
  1225. p[12] = (SECT_SIZE >> 8) & 0xff;
  1226. p[13] = SECT_SIZE & 0xff;
  1227. if (DEV_REMOVEABLE(target))
  1228. p[20] |= 0x20; /* should agree with INQUIRY */
  1229. if (1 == pcontrol)
  1230. memset(p + 2, 0, sizeof(format_pg) - 2);
  1231. return sizeof(format_pg);
  1232. }
  1233. static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
  1234. { /* Caching page for mode_sense */
  1235. unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  1236. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  1237. memcpy(p, caching_pg, sizeof(caching_pg));
  1238. if (1 == pcontrol)
  1239. memset(p + 2, 0, sizeof(caching_pg) - 2);
  1240. return sizeof(caching_pg);
  1241. }
  1242. static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
  1243. { /* Control mode page for mode_sense */
  1244. unsigned char ch_ctrl_m_pg[] = {/* 0xa, 10, */ 0x6, 0, 0, 0, 0, 0,
  1245. 0, 0, 0, 0};
  1246. unsigned char d_ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  1247. 0, 0, 0x2, 0x4b};
  1248. if (scsi_debug_dsense)
  1249. ctrl_m_pg[2] |= 0x4;
  1250. else
  1251. ctrl_m_pg[2] &= ~0x4;
  1252. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  1253. if (1 == pcontrol)
  1254. memcpy(p + 2, ch_ctrl_m_pg, sizeof(ch_ctrl_m_pg));
  1255. else if (2 == pcontrol)
  1256. memcpy(p, d_ctrl_m_pg, sizeof(d_ctrl_m_pg));
  1257. return sizeof(ctrl_m_pg);
  1258. }
  1259. static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
  1260. { /* Informational Exceptions control mode page for mode_sense */
  1261. unsigned char ch_iec_m_pg[] = {/* 0x1c, 0xa, */ 0x4, 0xf, 0, 0, 0, 0,
  1262. 0, 0, 0x0, 0x0};
  1263. unsigned char d_iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  1264. 0, 0, 0x0, 0x0};
  1265. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  1266. if (1 == pcontrol)
  1267. memcpy(p + 2, ch_iec_m_pg, sizeof(ch_iec_m_pg));
  1268. else if (2 == pcontrol)
  1269. memcpy(p, d_iec_m_pg, sizeof(d_iec_m_pg));
  1270. return sizeof(iec_m_pg);
  1271. }
  1272. static int resp_sas_sf_m_pg(unsigned char * p, int pcontrol, int target)
  1273. { /* SAS SSP mode page - short format for mode_sense */
  1274. unsigned char sas_sf_m_pg[] = {0x19, 0x6,
  1275. 0x6, 0x0, 0x7, 0xd0, 0x0, 0x0};
  1276. memcpy(p, sas_sf_m_pg, sizeof(sas_sf_m_pg));
  1277. if (1 == pcontrol)
  1278. memset(p + 2, 0, sizeof(sas_sf_m_pg) - 2);
  1279. return sizeof(sas_sf_m_pg);
  1280. }
  1281. static int resp_sas_pcd_m_spg(unsigned char * p, int pcontrol, int target,
  1282. int target_dev_id)
  1283. { /* SAS phy control and discover mode page for mode_sense */
  1284. unsigned char sas_pcd_m_pg[] = {0x59, 0x1, 0, 0x64, 0, 0x6, 0, 2,
  1285. 0, 0, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1286. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1287. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1288. 0x2, 0, 0, 0, 0, 0, 0, 0,
  1289. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1290. 0, 0, 0, 0, 0, 0, 0, 0,
  1291. 0, 1, 0, 0, 0x10, 0x9, 0x8, 0x0,
  1292. 0x52, 0x22, 0x22, 0x20, 0x0, 0x0, 0x0, 0x0,
  1293. 0x51, 0x11, 0x11, 0x10, 0x0, 0x0, 0x0, 0x1,
  1294. 0x3, 0, 0, 0, 0, 0, 0, 0,
  1295. 0x88, 0x99, 0, 0, 0, 0, 0, 0,
  1296. 0, 0, 0, 0, 0, 0, 0, 0,
  1297. };
  1298. int port_a, port_b;
  1299. port_a = target_dev_id + 1;
  1300. port_b = port_a + 1;
  1301. memcpy(p, sas_pcd_m_pg, sizeof(sas_pcd_m_pg));
  1302. p[20] = (port_a >> 24);
  1303. p[21] = (port_a >> 16) & 0xff;
  1304. p[22] = (port_a >> 8) & 0xff;
  1305. p[23] = port_a & 0xff;
  1306. p[48 + 20] = (port_b >> 24);
  1307. p[48 + 21] = (port_b >> 16) & 0xff;
  1308. p[48 + 22] = (port_b >> 8) & 0xff;
  1309. p[48 + 23] = port_b & 0xff;
  1310. if (1 == pcontrol)
  1311. memset(p + 4, 0, sizeof(sas_pcd_m_pg) - 4);
  1312. return sizeof(sas_pcd_m_pg);
  1313. }
  1314. static int resp_sas_sha_m_spg(unsigned char * p, int pcontrol)
  1315. { /* SAS SSP shared protocol specific port mode subpage */
  1316. unsigned char sas_sha_m_pg[] = {0x59, 0x2, 0, 0xc, 0, 0x6, 0x10, 0,
  1317. 0, 0, 0, 0, 0, 0, 0, 0,
  1318. };
  1319. memcpy(p, sas_sha_m_pg, sizeof(sas_sha_m_pg));
  1320. if (1 == pcontrol)
  1321. memset(p + 4, 0, sizeof(sas_sha_m_pg) - 4);
  1322. return sizeof(sas_sha_m_pg);
  1323. }
  1324. #define SDEBUG_MAX_MSENSE_SZ 256
  1325. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  1326. struct sdebug_dev_info * devip)
  1327. {
  1328. unsigned char dbd, llbaa;
  1329. int pcontrol, pcode, subpcode, bd_len;
  1330. unsigned char dev_spec;
  1331. int k, alloc_len, msense_6, offset, len, errsts, target_dev_id;
  1332. unsigned char * ap;
  1333. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  1334. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1335. if ((errsts = check_readiness(scp, 1, devip)))
  1336. return errsts;
  1337. dbd = !!(cmd[1] & 0x8);
  1338. pcontrol = (cmd[2] & 0xc0) >> 6;
  1339. pcode = cmd[2] & 0x3f;
  1340. subpcode = cmd[3];
  1341. msense_6 = (MODE_SENSE == cmd[0]);
  1342. llbaa = msense_6 ? 0 : !!(cmd[1] & 0x10);
  1343. if ((0 == scsi_debug_ptype) && (0 == dbd))
  1344. bd_len = llbaa ? 16 : 8;
  1345. else
  1346. bd_len = 0;
  1347. alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
  1348. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  1349. if (0x3 == pcontrol) { /* Saving values not supported */
  1350. mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
  1351. 0);
  1352. return check_condition_result;
  1353. }
  1354. target_dev_id = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  1355. (devip->target * 1000) - 3;
  1356. /* set DPOFUA bit for disks */
  1357. if (0 == scsi_debug_ptype)
  1358. dev_spec = (DEV_READONLY(target) ? 0x80 : 0x0) | 0x10;
  1359. else
  1360. dev_spec = 0x0;
  1361. if (msense_6) {
  1362. arr[2] = dev_spec;
  1363. arr[3] = bd_len;
  1364. offset = 4;
  1365. } else {
  1366. arr[3] = dev_spec;
  1367. if (16 == bd_len)
  1368. arr[4] = 0x1; /* set LONGLBA bit */
  1369. arr[7] = bd_len; /* assume 255 or less */
  1370. offset = 8;
  1371. }
  1372. ap = arr + offset;
  1373. if ((bd_len > 0) && (0 == sdebug_capacity)) {
  1374. if (scsi_debug_virtual_gb > 0) {
  1375. sdebug_capacity = 2048 * 1024;
  1376. sdebug_capacity *= scsi_debug_virtual_gb;
  1377. } else
  1378. sdebug_capacity = sdebug_store_sectors;
  1379. }
  1380. if (8 == bd_len) {
  1381. if (sdebug_capacity > 0xfffffffe) {
  1382. ap[0] = 0xff;
  1383. ap[1] = 0xff;
  1384. ap[2] = 0xff;
  1385. ap[3] = 0xff;
  1386. } else {
  1387. ap[0] = (sdebug_capacity >> 24) & 0xff;
  1388. ap[1] = (sdebug_capacity >> 16) & 0xff;
  1389. ap[2] = (sdebug_capacity >> 8) & 0xff;
  1390. ap[3] = sdebug_capacity & 0xff;
  1391. }
  1392. ap[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1393. ap[7] = SECT_SIZE_PER(target) & 0xff;
  1394. offset += bd_len;
  1395. ap = arr + offset;
  1396. } else if (16 == bd_len) {
  1397. unsigned long long capac = sdebug_capacity;
  1398. for (k = 0; k < 8; ++k, capac >>= 8)
  1399. ap[7 - k] = capac & 0xff;
  1400. ap[12] = (SECT_SIZE_PER(target) >> 24) & 0xff;
  1401. ap[13] = (SECT_SIZE_PER(target) >> 16) & 0xff;
  1402. ap[14] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  1403. ap[15] = SECT_SIZE_PER(target) & 0xff;
  1404. offset += bd_len;
  1405. ap = arr + offset;
  1406. }
  1407. if ((subpcode > 0x0) && (subpcode < 0xff) && (0x19 != pcode)) {
  1408. /* TODO: Control Extension page */
  1409. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1410. 0);
  1411. return check_condition_result;
  1412. }
  1413. switch (pcode) {
  1414. case 0x1: /* Read-Write error recovery page, direct access */
  1415. len = resp_err_recov_pg(ap, pcontrol, target);
  1416. offset += len;
  1417. break;
  1418. case 0x2: /* Disconnect-Reconnect page, all devices */
  1419. len = resp_disconnect_pg(ap, pcontrol, target);
  1420. offset += len;
  1421. break;
  1422. case 0x3: /* Format device page, direct access */
  1423. len = resp_format_pg(ap, pcontrol, target);
  1424. offset += len;
  1425. break;
  1426. case 0x8: /* Caching page, direct access */
  1427. len = resp_caching_pg(ap, pcontrol, target);
  1428. offset += len;
  1429. break;
  1430. case 0xa: /* Control Mode page, all devices */
  1431. len = resp_ctrl_m_pg(ap, pcontrol, target);
  1432. offset += len;
  1433. break;
  1434. case 0x19: /* if spc==1 then sas phy, control+discover */
  1435. if ((subpcode > 0x2) && (subpcode < 0xff)) {
  1436. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1437. INVALID_FIELD_IN_CDB, 0);
  1438. return check_condition_result;
  1439. }
  1440. len = 0;
  1441. if ((0x0 == subpcode) || (0xff == subpcode))
  1442. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1443. if ((0x1 == subpcode) || (0xff == subpcode))
  1444. len += resp_sas_pcd_m_spg(ap + len, pcontrol, target,
  1445. target_dev_id);
  1446. if ((0x2 == subpcode) || (0xff == subpcode))
  1447. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1448. offset += len;
  1449. break;
  1450. case 0x1c: /* Informational Exceptions Mode page, all devices */
  1451. len = resp_iec_m_pg(ap, pcontrol, target);
  1452. offset += len;
  1453. break;
  1454. case 0x3f: /* Read all Mode pages */
  1455. if ((0 == subpcode) || (0xff == subpcode)) {
  1456. len = resp_err_recov_pg(ap, pcontrol, target);
  1457. len += resp_disconnect_pg(ap + len, pcontrol, target);
  1458. len += resp_format_pg(ap + len, pcontrol, target);
  1459. len += resp_caching_pg(ap + len, pcontrol, target);
  1460. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  1461. len += resp_sas_sf_m_pg(ap + len, pcontrol, target);
  1462. if (0xff == subpcode) {
  1463. len += resp_sas_pcd_m_spg(ap + len, pcontrol,
  1464. target, target_dev_id);
  1465. len += resp_sas_sha_m_spg(ap + len, pcontrol);
  1466. }
  1467. len += resp_iec_m_pg(ap + len, pcontrol, target);
  1468. } else {
  1469. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1470. INVALID_FIELD_IN_CDB, 0);
  1471. return check_condition_result;
  1472. }
  1473. offset += len;
  1474. break;
  1475. default:
  1476. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1477. 0);
  1478. return check_condition_result;
  1479. }
  1480. if (msense_6)
  1481. arr[0] = offset - 1;
  1482. else {
  1483. arr[0] = ((offset - 2) >> 8) & 0xff;
  1484. arr[1] = (offset - 2) & 0xff;
  1485. }
  1486. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  1487. }
  1488. #define SDEBUG_MAX_MSELECT_SZ 512
  1489. static int resp_mode_select(struct scsi_cmnd * scp, int mselect6,
  1490. struct sdebug_dev_info * devip)
  1491. {
  1492. int pf, sp, ps, md_len, bd_len, off, spf, pg_len;
  1493. int param_len, res, errsts, mpage;
  1494. unsigned char arr[SDEBUG_MAX_MSELECT_SZ];
  1495. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1496. if ((errsts = check_readiness(scp, 1, devip)))
  1497. return errsts;
  1498. memset(arr, 0, sizeof(arr));
  1499. pf = cmd[1] & 0x10;
  1500. sp = cmd[1] & 0x1;
  1501. param_len = mselect6 ? cmd[4] : ((cmd[7] << 8) + cmd[8]);
  1502. if ((0 == pf) || sp || (param_len > SDEBUG_MAX_MSELECT_SZ)) {
  1503. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1504. INVALID_FIELD_IN_CDB, 0);
  1505. return check_condition_result;
  1506. }
  1507. res = fetch_to_dev_buffer(scp, arr, param_len);
  1508. if (-1 == res)
  1509. return (DID_ERROR << 16);
  1510. else if ((res < param_len) &&
  1511. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1512. printk(KERN_INFO "scsi_debug: mode_select: cdb indicated=%d, "
  1513. " IO sent=%d bytes\n", param_len, res);
  1514. md_len = mselect6 ? (arr[0] + 1) : ((arr[0] << 8) + arr[1] + 2);
  1515. bd_len = mselect6 ? arr[3] : ((arr[6] << 8) + arr[7]);
  1516. if (md_len > 2) {
  1517. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1518. INVALID_FIELD_IN_PARAM_LIST, 0);
  1519. return check_condition_result;
  1520. }
  1521. off = bd_len + (mselect6 ? 4 : 8);
  1522. mpage = arr[off] & 0x3f;
  1523. ps = !!(arr[off] & 0x80);
  1524. if (ps) {
  1525. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1526. INVALID_FIELD_IN_PARAM_LIST, 0);
  1527. return check_condition_result;
  1528. }
  1529. spf = !!(arr[off] & 0x40);
  1530. pg_len = spf ? ((arr[off + 2] << 8) + arr[off + 3] + 4) :
  1531. (arr[off + 1] + 2);
  1532. if ((pg_len + off) > param_len) {
  1533. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1534. PARAMETER_LIST_LENGTH_ERR, 0);
  1535. return check_condition_result;
  1536. }
  1537. switch (mpage) {
  1538. case 0xa: /* Control Mode page */
  1539. if (ctrl_m_pg[1] == arr[off + 1]) {
  1540. memcpy(ctrl_m_pg + 2, arr + off + 2,
  1541. sizeof(ctrl_m_pg) - 2);
  1542. scsi_debug_dsense = !!(ctrl_m_pg[2] & 0x4);
  1543. return 0;
  1544. }
  1545. break;
  1546. case 0x1c: /* Informational Exceptions Mode page */
  1547. if (iec_m_pg[1] == arr[off + 1]) {
  1548. memcpy(iec_m_pg + 2, arr + off + 2,
  1549. sizeof(iec_m_pg) - 2);
  1550. return 0;
  1551. }
  1552. break;
  1553. default:
  1554. break;
  1555. }
  1556. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1557. INVALID_FIELD_IN_PARAM_LIST, 0);
  1558. return check_condition_result;
  1559. }
  1560. static int resp_temp_l_pg(unsigned char * arr)
  1561. {
  1562. unsigned char temp_l_pg[] = {0x0, 0x0, 0x3, 0x2, 0x0, 38,
  1563. 0x0, 0x1, 0x3, 0x2, 0x0, 65,
  1564. };
  1565. memcpy(arr, temp_l_pg, sizeof(temp_l_pg));
  1566. return sizeof(temp_l_pg);
  1567. }
  1568. static int resp_ie_l_pg(unsigned char * arr)
  1569. {
  1570. unsigned char ie_l_pg[] = {0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 38,
  1571. };
  1572. memcpy(arr, ie_l_pg, sizeof(ie_l_pg));
  1573. if (iec_m_pg[2] & 0x4) { /* TEST bit set */
  1574. arr[4] = THRESHOLD_EXCEEDED;
  1575. arr[5] = 0xff;
  1576. }
  1577. return sizeof(ie_l_pg);
  1578. }
  1579. #define SDEBUG_MAX_LSENSE_SZ 512
  1580. static int resp_log_sense(struct scsi_cmnd * scp,
  1581. struct sdebug_dev_info * devip)
  1582. {
  1583. int ppc, sp, pcontrol, pcode, subpcode, alloc_len, errsts, len, n;
  1584. unsigned char arr[SDEBUG_MAX_LSENSE_SZ];
  1585. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1586. if ((errsts = check_readiness(scp, 1, devip)))
  1587. return errsts;
  1588. memset(arr, 0, sizeof(arr));
  1589. ppc = cmd[1] & 0x2;
  1590. sp = cmd[1] & 0x1;
  1591. if (ppc || sp) {
  1592. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1593. INVALID_FIELD_IN_CDB, 0);
  1594. return check_condition_result;
  1595. }
  1596. pcontrol = (cmd[2] & 0xc0) >> 6;
  1597. pcode = cmd[2] & 0x3f;
  1598. subpcode = cmd[3] & 0xff;
  1599. alloc_len = (cmd[7] << 8) + cmd[8];
  1600. arr[0] = pcode;
  1601. if (0 == subpcode) {
  1602. switch (pcode) {
  1603. case 0x0: /* Supported log pages log page */
  1604. n = 4;
  1605. arr[n++] = 0x0; /* this page */
  1606. arr[n++] = 0xd; /* Temperature */
  1607. arr[n++] = 0x2f; /* Informational exceptions */
  1608. arr[3] = n - 4;
  1609. break;
  1610. case 0xd: /* Temperature log page */
  1611. arr[3] = resp_temp_l_pg(arr + 4);
  1612. break;
  1613. case 0x2f: /* Informational exceptions log page */
  1614. arr[3] = resp_ie_l_pg(arr + 4);
  1615. break;
  1616. default:
  1617. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1618. INVALID_FIELD_IN_CDB, 0);
  1619. return check_condition_result;
  1620. }
  1621. } else if (0xff == subpcode) {
  1622. arr[0] |= 0x40;
  1623. arr[1] = subpcode;
  1624. switch (pcode) {
  1625. case 0x0: /* Supported log pages and subpages log page */
  1626. n = 4;
  1627. arr[n++] = 0x0;
  1628. arr[n++] = 0x0; /* 0,0 page */
  1629. arr[n++] = 0x0;
  1630. arr[n++] = 0xff; /* this page */
  1631. arr[n++] = 0xd;
  1632. arr[n++] = 0x0; /* Temperature */
  1633. arr[n++] = 0x2f;
  1634. arr[n++] = 0x0; /* Informational exceptions */
  1635. arr[3] = n - 4;
  1636. break;
  1637. case 0xd: /* Temperature subpages */
  1638. n = 4;
  1639. arr[n++] = 0xd;
  1640. arr[n++] = 0x0; /* Temperature */
  1641. arr[3] = n - 4;
  1642. break;
  1643. case 0x2f: /* Informational exceptions subpages */
  1644. n = 4;
  1645. arr[n++] = 0x2f;
  1646. arr[n++] = 0x0; /* Informational exceptions */
  1647. arr[3] = n - 4;
  1648. break;
  1649. default:
  1650. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1651. INVALID_FIELD_IN_CDB, 0);
  1652. return check_condition_result;
  1653. }
  1654. } else {
  1655. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  1656. INVALID_FIELD_IN_CDB, 0);
  1657. return check_condition_result;
  1658. }
  1659. len = min(((arr[2] << 8) + arr[3]) + 4, alloc_len);
  1660. return fill_from_dev_buffer(scp, arr,
  1661. min(len, SDEBUG_MAX_INQ_ARR_SZ));
  1662. }
  1663. static int resp_read(struct scsi_cmnd * SCpnt, unsigned long long lba,
  1664. unsigned int num, struct sdebug_dev_info * devip)
  1665. {
  1666. unsigned long iflags;
  1667. unsigned int block, from_bottom;
  1668. unsigned long long u;
  1669. int ret;
  1670. if (lba + num > sdebug_capacity) {
  1671. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  1672. 0);
  1673. return check_condition_result;
  1674. }
  1675. /* transfer length excessive (tie in to block limits VPD page) */
  1676. if (num > sdebug_store_sectors) {
  1677. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1678. 0);
  1679. return check_condition_result;
  1680. }
  1681. if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
  1682. (lba <= OPT_MEDIUM_ERR_ADDR) &&
  1683. ((lba + num) > OPT_MEDIUM_ERR_ADDR)) {
  1684. /* claim unrecoverable read error */
  1685. mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
  1686. 0);
  1687. /* set info field and valid bit for fixed descriptor */
  1688. if (0x70 == (devip->sense_buff[0] & 0x7f)) {
  1689. devip->sense_buff[0] |= 0x80; /* Valid bit */
  1690. ret = OPT_MEDIUM_ERR_ADDR;
  1691. devip->sense_buff[3] = (ret >> 24) & 0xff;
  1692. devip->sense_buff[4] = (ret >> 16) & 0xff;
  1693. devip->sense_buff[5] = (ret >> 8) & 0xff;
  1694. devip->sense_buff[6] = ret & 0xff;
  1695. }
  1696. return check_condition_result;
  1697. }
  1698. read_lock_irqsave(&atomic_rw, iflags);
  1699. if ((lba + num) <= sdebug_store_sectors)
  1700. ret = fill_from_dev_buffer(SCpnt,
  1701. fake_storep + (lba * SECT_SIZE),
  1702. num * SECT_SIZE);
  1703. else {
  1704. /* modulo when one arg is 64 bits needs do_div() */
  1705. u = lba;
  1706. block = do_div(u, sdebug_store_sectors);
  1707. from_bottom = 0;
  1708. if ((block + num) > sdebug_store_sectors)
  1709. from_bottom = (block + num) - sdebug_store_sectors;
  1710. ret = fill_from_dev_buffer(SCpnt,
  1711. fake_storep + (block * SECT_SIZE),
  1712. (num - from_bottom) * SECT_SIZE);
  1713. if ((0 == ret) && (from_bottom > 0))
  1714. ret = fill_from_dev_buffer(SCpnt, fake_storep,
  1715. from_bottom * SECT_SIZE);
  1716. }
  1717. read_unlock_irqrestore(&atomic_rw, iflags);
  1718. return ret;
  1719. }
  1720. static int resp_write(struct scsi_cmnd * SCpnt, unsigned long long lba,
  1721. unsigned int num, struct sdebug_dev_info * devip)
  1722. {
  1723. unsigned long iflags;
  1724. unsigned int block, to_bottom;
  1725. unsigned long long u;
  1726. int res;
  1727. if (lba + num > sdebug_capacity) {
  1728. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  1729. 0);
  1730. return check_condition_result;
  1731. }
  1732. /* transfer length excessive (tie in to block limits VPD page) */
  1733. if (num > sdebug_store_sectors) {
  1734. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1735. 0);
  1736. return check_condition_result;
  1737. }
  1738. write_lock_irqsave(&atomic_rw, iflags);
  1739. if ((lba + num) <= sdebug_store_sectors)
  1740. res = fetch_to_dev_buffer(SCpnt,
  1741. fake_storep + (lba * SECT_SIZE),
  1742. num * SECT_SIZE);
  1743. else {
  1744. /* modulo when one arg is 64 bits needs do_div() */
  1745. u = lba;
  1746. block = do_div(u, sdebug_store_sectors);
  1747. to_bottom = 0;
  1748. if ((block + num) > sdebug_store_sectors)
  1749. to_bottom = (block + num) - sdebug_store_sectors;
  1750. res = fetch_to_dev_buffer(SCpnt,
  1751. fake_storep + (block * SECT_SIZE),
  1752. (num - to_bottom) * SECT_SIZE);
  1753. if ((0 == res) && (to_bottom > 0))
  1754. res = fetch_to_dev_buffer(SCpnt, fake_storep,
  1755. to_bottom * SECT_SIZE);
  1756. }
  1757. write_unlock_irqrestore(&atomic_rw, iflags);
  1758. if (-1 == res)
  1759. return (DID_ERROR << 16);
  1760. else if ((res < (num * SECT_SIZE)) &&
  1761. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  1762. printk(KERN_INFO "scsi_debug: write: cdb indicated=%u, "
  1763. " IO sent=%d bytes\n", num * SECT_SIZE, res);
  1764. return 0;
  1765. }
  1766. #define SDEBUG_RLUN_ARR_SZ 256
  1767. static int resp_report_luns(struct scsi_cmnd * scp,
  1768. struct sdebug_dev_info * devip)
  1769. {
  1770. unsigned int alloc_len;
  1771. int lun_cnt, i, upper, num, n, wlun, lun;
  1772. unsigned char *cmd = (unsigned char *)scp->cmnd;
  1773. int select_report = (int)cmd[2];
  1774. struct scsi_lun *one_lun;
  1775. unsigned char arr[SDEBUG_RLUN_ARR_SZ];
  1776. unsigned char * max_addr;
  1777. alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
  1778. if ((alloc_len < 4) || (select_report > 2)) {
  1779. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  1780. 0);
  1781. return check_condition_result;
  1782. }
  1783. /* can produce response with up to 16k luns (lun 0 to lun 16383) */
  1784. memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
  1785. lun_cnt = scsi_debug_max_luns;
  1786. if (1 == select_report)
  1787. lun_cnt = 0;
  1788. else if (scsi_debug_no_lun_0 && (lun_cnt > 0))
  1789. --lun_cnt;
  1790. wlun = (select_report > 0) ? 1 : 0;
  1791. num = lun_cnt + wlun;
  1792. arr[2] = ((sizeof(struct scsi_lun) * num) >> 8) & 0xff;
  1793. arr[3] = (sizeof(struct scsi_lun) * num) & 0xff;
  1794. n = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
  1795. sizeof(struct scsi_lun)), num);
  1796. if (n < num) {
  1797. wlun = 0;
  1798. lun_cnt = n;
  1799. }
  1800. one_lun = (struct scsi_lun *) &arr[8];
  1801. max_addr = arr + SDEBUG_RLUN_ARR_SZ;
  1802. for (i = 0, lun = (scsi_debug_no_lun_0 ? 1 : 0);
  1803. ((i < lun_cnt) && ((unsigned char *)(one_lun + i) < max_addr));
  1804. i++, lun++) {
  1805. upper = (lun >> 8) & 0x3f;
  1806. if (upper)
  1807. one_lun[i].scsi_lun[0] =
  1808. (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
  1809. one_lun[i].scsi_lun[1] = lun & 0xff;
  1810. }
  1811. if (wlun) {
  1812. one_lun[i].scsi_lun[0] = (SAM2_WLUN_REPORT_LUNS >> 8) & 0xff;
  1813. one_lun[i].scsi_lun[1] = SAM2_WLUN_REPORT_LUNS & 0xff;
  1814. i++;
  1815. }
  1816. alloc_len = (unsigned char *)(one_lun + i) - arr;
  1817. return fill_from_dev_buffer(scp, arr,
  1818. min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
  1819. }
  1820. /* When timer goes off this function is called. */
  1821. static void timer_intr_handler(unsigned long indx)
  1822. {
  1823. struct sdebug_queued_cmd * sqcp;
  1824. unsigned long iflags;
  1825. if (indx >= SCSI_DEBUG_CANQUEUE) {
  1826. printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
  1827. "large\n");
  1828. return;
  1829. }
  1830. spin_lock_irqsave(&queued_arr_lock, iflags);
  1831. sqcp = &queued_arr[(int)indx];
  1832. if (! sqcp->in_use) {
  1833. printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
  1834. "interrupt\n");
  1835. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1836. return;
  1837. }
  1838. sqcp->in_use = 0;
  1839. if (sqcp->done_funct) {
  1840. sqcp->a_cmnd->result = sqcp->scsi_result;
  1841. sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
  1842. }
  1843. sqcp->done_funct = NULL;
  1844. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1845. }
  1846. static int scsi_debug_slave_alloc(struct scsi_device * sdp)
  1847. {
  1848. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1849. printk(KERN_INFO "scsi_debug: slave_alloc <%u %u %u %u>\n",
  1850. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1851. return 0;
  1852. }
  1853. static int scsi_debug_slave_configure(struct scsi_device * sdp)
  1854. {
  1855. struct sdebug_dev_info * devip;
  1856. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1857. printk(KERN_INFO "scsi_debug: slave_configure <%u %u %u %u>\n",
  1858. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1859. if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
  1860. sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
  1861. devip = devInfoReg(sdp);
  1862. if (NULL == devip)
  1863. return 1; /* no resources, will be marked offline */
  1864. sdp->hostdata = devip;
  1865. if (sdp->host->cmd_per_lun)
  1866. scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
  1867. sdp->host->cmd_per_lun);
  1868. blk_queue_max_segment_size(sdp->request_queue, 256 * 1024);
  1869. return 0;
  1870. }
  1871. static void scsi_debug_slave_destroy(struct scsi_device * sdp)
  1872. {
  1873. struct sdebug_dev_info * devip =
  1874. (struct sdebug_dev_info *)sdp->hostdata;
  1875. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1876. printk(KERN_INFO "scsi_debug: slave_destroy <%u %u %u %u>\n",
  1877. sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);
  1878. if (devip) {
  1879. /* make this slot avaliable for re-use */
  1880. devip->used = 0;
  1881. sdp->hostdata = NULL;
  1882. }
  1883. }
  1884. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
  1885. {
  1886. struct sdebug_host_info * sdbg_host;
  1887. struct sdebug_dev_info * open_devip = NULL;
  1888. struct sdebug_dev_info * devip =
  1889. (struct sdebug_dev_info *)sdev->hostdata;
  1890. if (devip)
  1891. return devip;
  1892. sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
  1893. if(! sdbg_host) {
  1894. printk(KERN_ERR "Host info NULL\n");
  1895. return NULL;
  1896. }
  1897. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  1898. if ((devip->used) && (devip->channel == sdev->channel) &&
  1899. (devip->target == sdev->id) &&
  1900. (devip->lun == sdev->lun))
  1901. return devip;
  1902. else {
  1903. if ((!devip->used) && (!open_devip))
  1904. open_devip = devip;
  1905. }
  1906. }
  1907. if (NULL == open_devip) { /* try and make a new one */
  1908. open_devip = kzalloc(sizeof(*open_devip),GFP_ATOMIC);
  1909. if (NULL == open_devip) {
  1910. printk(KERN_ERR "%s: out of memory at line %d\n",
  1911. __FUNCTION__, __LINE__);
  1912. return NULL;
  1913. }
  1914. open_devip->sdbg_host = sdbg_host;
  1915. list_add_tail(&open_devip->dev_list,
  1916. &sdbg_host->dev_info_list);
  1917. }
  1918. if (open_devip) {
  1919. open_devip->channel = sdev->channel;
  1920. open_devip->target = sdev->id;
  1921. open_devip->lun = sdev->lun;
  1922. open_devip->sdbg_host = sdbg_host;
  1923. open_devip->reset = 1;
  1924. open_devip->used = 1;
  1925. memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
  1926. if (scsi_debug_dsense)
  1927. open_devip->sense_buff[0] = 0x72;
  1928. else {
  1929. open_devip->sense_buff[0] = 0x70;
  1930. open_devip->sense_buff[7] = 0xa;
  1931. }
  1932. if (sdev->lun == SAM2_WLUN_REPORT_LUNS)
  1933. open_devip->wlun = SAM2_WLUN_REPORT_LUNS & 0xff;
  1934. return open_devip;
  1935. }
  1936. return NULL;
  1937. }
  1938. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  1939. int asc, int asq)
  1940. {
  1941. unsigned char * sbuff;
  1942. sbuff = devip->sense_buff;
  1943. memset(sbuff, 0, SDEBUG_SENSE_LEN);
  1944. if (scsi_debug_dsense) {
  1945. sbuff[0] = 0x72; /* descriptor, current */
  1946. sbuff[1] = key;
  1947. sbuff[2] = asc;
  1948. sbuff[3] = asq;
  1949. } else {
  1950. sbuff[0] = 0x70; /* fixed, current */
  1951. sbuff[2] = key;
  1952. sbuff[7] = 0xa; /* implies 18 byte sense buffer */
  1953. sbuff[12] = asc;
  1954. sbuff[13] = asq;
  1955. }
  1956. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1957. printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
  1958. "[0x%x,0x%x,0x%x]\n", key, asc, asq);
  1959. }
  1960. static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
  1961. {
  1962. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1963. printk(KERN_INFO "scsi_debug: abort\n");
  1964. ++num_aborts;
  1965. stop_queued_cmnd(SCpnt);
  1966. return SUCCESS;
  1967. }
  1968. static int scsi_debug_biosparam(struct scsi_device *sdev,
  1969. struct block_device * bdev, sector_t capacity, int *info)
  1970. {
  1971. int res;
  1972. unsigned char *buf;
  1973. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1974. printk(KERN_INFO "scsi_debug: biosparam\n");
  1975. buf = scsi_bios_ptable(bdev);
  1976. if (buf) {
  1977. res = scsi_partsize(buf, capacity,
  1978. &info[2], &info[0], &info[1]);
  1979. kfree(buf);
  1980. if (! res)
  1981. return res;
  1982. }
  1983. info[0] = sdebug_heads;
  1984. info[1] = sdebug_sectors_per;
  1985. info[2] = sdebug_cylinders_per;
  1986. return 0;
  1987. }
  1988. static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
  1989. {
  1990. struct sdebug_dev_info * devip;
  1991. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1992. printk(KERN_INFO "scsi_debug: device_reset\n");
  1993. ++num_dev_resets;
  1994. if (SCpnt) {
  1995. devip = devInfoReg(SCpnt->device);
  1996. if (devip)
  1997. devip->reset = 1;
  1998. }
  1999. return SUCCESS;
  2000. }
  2001. static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
  2002. {
  2003. struct sdebug_host_info *sdbg_host;
  2004. struct sdebug_dev_info * dev_info;
  2005. struct scsi_device * sdp;
  2006. struct Scsi_Host * hp;
  2007. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2008. printk(KERN_INFO "scsi_debug: bus_reset\n");
  2009. ++num_bus_resets;
  2010. if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
  2011. sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
  2012. if (sdbg_host) {
  2013. list_for_each_entry(dev_info,
  2014. &sdbg_host->dev_info_list,
  2015. dev_list)
  2016. dev_info->reset = 1;
  2017. }
  2018. }
  2019. return SUCCESS;
  2020. }
  2021. static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
  2022. {
  2023. struct sdebug_host_info * sdbg_host;
  2024. struct sdebug_dev_info * dev_info;
  2025. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2026. printk(KERN_INFO "scsi_debug: host_reset\n");
  2027. ++num_host_resets;
  2028. spin_lock(&sdebug_host_list_lock);
  2029. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2030. list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
  2031. dev_list)
  2032. dev_info->reset = 1;
  2033. }
  2034. spin_unlock(&sdebug_host_list_lock);
  2035. stop_all_queued();
  2036. return SUCCESS;
  2037. }
  2038. /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
  2039. static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
  2040. {
  2041. unsigned long iflags;
  2042. int k;
  2043. struct sdebug_queued_cmd * sqcp;
  2044. spin_lock_irqsave(&queued_arr_lock, iflags);
  2045. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2046. sqcp = &queued_arr[k];
  2047. if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
  2048. del_timer_sync(&sqcp->cmnd_timer);
  2049. sqcp->in_use = 0;
  2050. sqcp->a_cmnd = NULL;
  2051. break;
  2052. }
  2053. }
  2054. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2055. return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
  2056. }
  2057. /* Deletes (stops) timers of all queued commands */
  2058. static void stop_all_queued(void)
  2059. {
  2060. unsigned long iflags;
  2061. int k;
  2062. struct sdebug_queued_cmd * sqcp;
  2063. spin_lock_irqsave(&queued_arr_lock, iflags);
  2064. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2065. sqcp = &queued_arr[k];
  2066. if (sqcp->in_use && sqcp->a_cmnd) {
  2067. del_timer_sync(&sqcp->cmnd_timer);
  2068. sqcp->in_use = 0;
  2069. sqcp->a_cmnd = NULL;
  2070. }
  2071. }
  2072. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2073. }
  2074. /* Initializes timers in queued array */
  2075. static void __init init_all_queued(void)
  2076. {
  2077. unsigned long iflags;
  2078. int k;
  2079. struct sdebug_queued_cmd * sqcp;
  2080. spin_lock_irqsave(&queued_arr_lock, iflags);
  2081. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2082. sqcp = &queued_arr[k];
  2083. init_timer(&sqcp->cmnd_timer);
  2084. sqcp->in_use = 0;
  2085. sqcp->a_cmnd = NULL;
  2086. }
  2087. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2088. }
  2089. static void __init sdebug_build_parts(unsigned char * ramp)
  2090. {
  2091. struct partition * pp;
  2092. int starts[SDEBUG_MAX_PARTS + 2];
  2093. int sectors_per_part, num_sectors, k;
  2094. int heads_by_sects, start_sec, end_sec;
  2095. /* assume partition table already zeroed */
  2096. if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
  2097. return;
  2098. if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
  2099. scsi_debug_num_parts = SDEBUG_MAX_PARTS;
  2100. printk(KERN_WARNING "scsi_debug:build_parts: reducing "
  2101. "partitions to %d\n", SDEBUG_MAX_PARTS);
  2102. }
  2103. num_sectors = (int)sdebug_store_sectors;
  2104. sectors_per_part = (num_sectors - sdebug_sectors_per)
  2105. / scsi_debug_num_parts;
  2106. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  2107. starts[0] = sdebug_sectors_per;
  2108. for (k = 1; k < scsi_debug_num_parts; ++k)
  2109. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  2110. * heads_by_sects;
  2111. starts[scsi_debug_num_parts] = num_sectors;
  2112. starts[scsi_debug_num_parts + 1] = 0;
  2113. ramp[510] = 0x55; /* magic partition markings */
  2114. ramp[511] = 0xAA;
  2115. pp = (struct partition *)(ramp + 0x1be);
  2116. for (k = 0; starts[k + 1]; ++k, ++pp) {
  2117. start_sec = starts[k];
  2118. end_sec = starts[k + 1] - 1;
  2119. pp->boot_ind = 0;
  2120. pp->cyl = start_sec / heads_by_sects;
  2121. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  2122. / sdebug_sectors_per;
  2123. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  2124. pp->end_cyl = end_sec / heads_by_sects;
  2125. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  2126. / sdebug_sectors_per;
  2127. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  2128. pp->start_sect = start_sec;
  2129. pp->nr_sects = end_sec - start_sec + 1;
  2130. pp->sys_ind = 0x83; /* plain Linux partition */
  2131. }
  2132. }
  2133. static int schedule_resp(struct scsi_cmnd * cmnd,
  2134. struct sdebug_dev_info * devip,
  2135. done_funct_t done, int scsi_result, int delta_jiff)
  2136. {
  2137. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
  2138. if (scsi_result) {
  2139. struct scsi_device * sdp = cmnd->device;
  2140. printk(KERN_INFO "scsi_debug: <%u %u %u %u> "
  2141. "non-zero result=0x%x\n", sdp->host->host_no,
  2142. sdp->channel, sdp->id, sdp->lun, scsi_result);
  2143. }
  2144. }
  2145. if (cmnd && devip) {
  2146. /* simulate autosense by this driver */
  2147. if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
  2148. memcpy(cmnd->sense_buffer, devip->sense_buff,
  2149. (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
  2150. SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
  2151. }
  2152. if (delta_jiff <= 0) {
  2153. if (cmnd)
  2154. cmnd->result = scsi_result;
  2155. if (done)
  2156. done(cmnd);
  2157. return 0;
  2158. } else {
  2159. unsigned long iflags;
  2160. int k;
  2161. struct sdebug_queued_cmd * sqcp = NULL;
  2162. spin_lock_irqsave(&queued_arr_lock, iflags);
  2163. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  2164. sqcp = &queued_arr[k];
  2165. if (! sqcp->in_use)
  2166. break;
  2167. }
  2168. if (k >= SCSI_DEBUG_CANQUEUE) {
  2169. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2170. printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
  2171. return 1; /* report busy to mid level */
  2172. }
  2173. sqcp->in_use = 1;
  2174. sqcp->a_cmnd = cmnd;
  2175. sqcp->scsi_result = scsi_result;
  2176. sqcp->done_funct = done;
  2177. sqcp->cmnd_timer.function = timer_intr_handler;
  2178. sqcp->cmnd_timer.data = k;
  2179. sqcp->cmnd_timer.expires = jiffies + delta_jiff;
  2180. add_timer(&sqcp->cmnd_timer);
  2181. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  2182. if (cmnd)
  2183. cmnd->result = 0;
  2184. return 0;
  2185. }
  2186. }
  2187. /* Note: The following macros create attribute files in the
  2188. /sys/module/scsi_debug/parameters directory. Unfortunately this
  2189. driver is unaware of a change and cannot trigger auxiliary actions
  2190. as it can when the corresponding attribute in the
  2191. /sys/bus/pseudo/drivers/scsi_debug directory is changed.
  2192. */
  2193. module_param_named(add_host, scsi_debug_add_host, int, S_IRUGO | S_IWUSR);
  2194. module_param_named(delay, scsi_debug_delay, int, S_IRUGO | S_IWUSR);
  2195. module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, S_IRUGO);
  2196. module_param_named(dsense, scsi_debug_dsense, int, S_IRUGO | S_IWUSR);
  2197. module_param_named(every_nth, scsi_debug_every_nth, int, S_IRUGO | S_IWUSR);
  2198. module_param_named(fake_rw, scsi_debug_fake_rw, int, S_IRUGO | S_IWUSR);
  2199. module_param_named(max_luns, scsi_debug_max_luns, int, S_IRUGO | S_IWUSR);
  2200. module_param_named(no_lun_0, scsi_debug_no_lun_0, int, S_IRUGO | S_IWUSR);
  2201. module_param_named(num_parts, scsi_debug_num_parts, int, S_IRUGO);
  2202. module_param_named(num_tgts, scsi_debug_num_tgts, int, S_IRUGO | S_IWUSR);
  2203. module_param_named(opts, scsi_debug_opts, int, S_IRUGO | S_IWUSR);
  2204. module_param_named(ptype, scsi_debug_ptype, int, S_IRUGO | S_IWUSR);
  2205. module_param_named(scsi_level, scsi_debug_scsi_level, int, S_IRUGO);
  2206. module_param_named(virtual_gb, scsi_debug_virtual_gb, int, S_IRUGO | S_IWUSR);
  2207. module_param_named(vpd_use_hostno, scsi_debug_vpd_use_hostno, int,
  2208. S_IRUGO | S_IWUSR);
  2209. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  2210. MODULE_DESCRIPTION("SCSI debug adapter driver");
  2211. MODULE_LICENSE("GPL");
  2212. MODULE_VERSION(SCSI_DEBUG_VERSION);
  2213. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  2214. MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
  2215. MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs(def=8)");
  2216. MODULE_PARM_DESC(dsense, "use descriptor sense format(def=0 -> fixed)");
  2217. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=0)");
  2218. MODULE_PARM_DESC(fake_rw, "fake reads/writes instead of copying (def=0)");
  2219. MODULE_PARM_DESC(max_luns, "number of LUNs per target to simulate(def=1)");
  2220. MODULE_PARM_DESC(no_lun_0, "no LU number 0 (def=0 -> have lun 0)");
  2221. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  2222. MODULE_PARM_DESC(num_tgts, "number of targets per host to simulate(def=1)");
  2223. MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err... (def=0)");
  2224. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  2225. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
  2226. MODULE_PARM_DESC(virtual_gb, "virtual gigabyte size (def=0 -> use dev_size_mb)");
  2227. MODULE_PARM_DESC(vpd_use_hostno, "0 -> dev ids ignore hostno (def=1 -> unique dev ids)");
  2228. static char sdebug_info[256];
  2229. static const char * scsi_debug_info(struct Scsi_Host * shp)
  2230. {
  2231. sprintf(sdebug_info, "scsi_debug, version %s [%s], "
  2232. "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
  2233. scsi_debug_version_date, scsi_debug_dev_size_mb,
  2234. scsi_debug_opts);
  2235. return sdebug_info;
  2236. }
  2237. /* scsi_debug_proc_info
  2238. * Used if the driver currently has no own support for /proc/scsi
  2239. */
  2240. static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  2241. int length, int inout)
  2242. {
  2243. int len, pos, begin;
  2244. int orig_length;
  2245. orig_length = length;
  2246. if (inout == 1) {
  2247. char arr[16];
  2248. int minLen = length > 15 ? 15 : length;
  2249. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2250. return -EACCES;
  2251. memcpy(arr, buffer, minLen);
  2252. arr[minLen] = '\0';
  2253. if (1 != sscanf(arr, "%d", &pos))
  2254. return -EINVAL;
  2255. scsi_debug_opts = pos;
  2256. if (scsi_debug_every_nth != 0)
  2257. scsi_debug_cmnd_count = 0;
  2258. return length;
  2259. }
  2260. begin = 0;
  2261. pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
  2262. "%s [%s]\n"
  2263. "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
  2264. "every_nth=%d(curr:%d)\n"
  2265. "delay=%d, max_luns=%d, scsi_level=%d\n"
  2266. "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
  2267. "number of aborts=%d, device_reset=%d, bus_resets=%d, "
  2268. "host_resets=%d\n",
  2269. SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
  2270. scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
  2271. scsi_debug_cmnd_count, scsi_debug_delay,
  2272. scsi_debug_max_luns, scsi_debug_scsi_level,
  2273. SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  2274. num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
  2275. if (pos < offset) {
  2276. len = 0;
  2277. begin = pos;
  2278. }
  2279. *start = buffer + (offset - begin); /* Start of wanted data */
  2280. len -= (offset - begin);
  2281. if (len > length)
  2282. len = length;
  2283. return len;
  2284. }
  2285. static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
  2286. {
  2287. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
  2288. }
  2289. static ssize_t sdebug_delay_store(struct device_driver * ddp,
  2290. const char * buf, size_t count)
  2291. {
  2292. int delay;
  2293. char work[20];
  2294. if (1 == sscanf(buf, "%10s", work)) {
  2295. if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
  2296. scsi_debug_delay = delay;
  2297. return count;
  2298. }
  2299. }
  2300. return -EINVAL;
  2301. }
  2302. DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
  2303. sdebug_delay_store);
  2304. static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
  2305. {
  2306. return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
  2307. }
  2308. static ssize_t sdebug_opts_store(struct device_driver * ddp,
  2309. const char * buf, size_t count)
  2310. {
  2311. int opts;
  2312. char work[20];
  2313. if (1 == sscanf(buf, "%10s", work)) {
  2314. if (0 == strnicmp(work,"0x", 2)) {
  2315. if (1 == sscanf(&work[2], "%x", &opts))
  2316. goto opts_done;
  2317. } else {
  2318. if (1 == sscanf(work, "%d", &opts))
  2319. goto opts_done;
  2320. }
  2321. }
  2322. return -EINVAL;
  2323. opts_done:
  2324. scsi_debug_opts = opts;
  2325. scsi_debug_cmnd_count = 0;
  2326. return count;
  2327. }
  2328. DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
  2329. sdebug_opts_store);
  2330. static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
  2331. {
  2332. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
  2333. }
  2334. static ssize_t sdebug_ptype_store(struct device_driver * ddp,
  2335. const char * buf, size_t count)
  2336. {
  2337. int n;
  2338. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2339. scsi_debug_ptype = n;
  2340. return count;
  2341. }
  2342. return -EINVAL;
  2343. }
  2344. DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
  2345. static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
  2346. {
  2347. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
  2348. }
  2349. static ssize_t sdebug_dsense_store(struct device_driver * ddp,
  2350. const char * buf, size_t count)
  2351. {
  2352. int n;
  2353. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2354. scsi_debug_dsense = n;
  2355. return count;
  2356. }
  2357. return -EINVAL;
  2358. }
  2359. DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
  2360. sdebug_dsense_store);
  2361. static ssize_t sdebug_fake_rw_show(struct device_driver * ddp, char * buf)
  2362. {
  2363. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_fake_rw);
  2364. }
  2365. static ssize_t sdebug_fake_rw_store(struct device_driver * ddp,
  2366. const char * buf, size_t count)
  2367. {
  2368. int n;
  2369. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2370. scsi_debug_fake_rw = n;
  2371. return count;
  2372. }
  2373. return -EINVAL;
  2374. }
  2375. DRIVER_ATTR(fake_rw, S_IRUGO | S_IWUSR, sdebug_fake_rw_show,
  2376. sdebug_fake_rw_store);
  2377. static ssize_t sdebug_no_lun_0_show(struct device_driver * ddp, char * buf)
  2378. {
  2379. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_no_lun_0);
  2380. }
  2381. static ssize_t sdebug_no_lun_0_store(struct device_driver * ddp,
  2382. const char * buf, size_t count)
  2383. {
  2384. int n;
  2385. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2386. scsi_debug_no_lun_0 = n;
  2387. return count;
  2388. }
  2389. return -EINVAL;
  2390. }
  2391. DRIVER_ATTR(no_lun_0, S_IRUGO | S_IWUSR, sdebug_no_lun_0_show,
  2392. sdebug_no_lun_0_store);
  2393. static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
  2394. {
  2395. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
  2396. }
  2397. static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
  2398. const char * buf, size_t count)
  2399. {
  2400. int n;
  2401. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2402. scsi_debug_num_tgts = n;
  2403. sdebug_max_tgts_luns();
  2404. return count;
  2405. }
  2406. return -EINVAL;
  2407. }
  2408. DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
  2409. sdebug_num_tgts_store);
  2410. static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
  2411. {
  2412. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
  2413. }
  2414. DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
  2415. static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
  2416. {
  2417. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
  2418. }
  2419. DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
  2420. static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
  2421. {
  2422. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
  2423. }
  2424. static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
  2425. const char * buf, size_t count)
  2426. {
  2427. int nth;
  2428. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  2429. scsi_debug_every_nth = nth;
  2430. scsi_debug_cmnd_count = 0;
  2431. return count;
  2432. }
  2433. return -EINVAL;
  2434. }
  2435. DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
  2436. sdebug_every_nth_store);
  2437. static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
  2438. {
  2439. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
  2440. }
  2441. static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
  2442. const char * buf, size_t count)
  2443. {
  2444. int n;
  2445. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2446. scsi_debug_max_luns = n;
  2447. sdebug_max_tgts_luns();
  2448. return count;
  2449. }
  2450. return -EINVAL;
  2451. }
  2452. DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
  2453. sdebug_max_luns_store);
  2454. static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
  2455. {
  2456. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
  2457. }
  2458. DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
  2459. static ssize_t sdebug_virtual_gb_show(struct device_driver * ddp, char * buf)
  2460. {
  2461. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_virtual_gb);
  2462. }
  2463. static ssize_t sdebug_virtual_gb_store(struct device_driver * ddp,
  2464. const char * buf, size_t count)
  2465. {
  2466. int n;
  2467. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2468. scsi_debug_virtual_gb = n;
  2469. if (scsi_debug_virtual_gb > 0) {
  2470. sdebug_capacity = 2048 * 1024;
  2471. sdebug_capacity *= scsi_debug_virtual_gb;
  2472. } else
  2473. sdebug_capacity = sdebug_store_sectors;
  2474. return count;
  2475. }
  2476. return -EINVAL;
  2477. }
  2478. DRIVER_ATTR(virtual_gb, S_IRUGO | S_IWUSR, sdebug_virtual_gb_show,
  2479. sdebug_virtual_gb_store);
  2480. static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
  2481. {
  2482. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
  2483. }
  2484. static ssize_t sdebug_add_host_store(struct device_driver * ddp,
  2485. const char * buf, size_t count)
  2486. {
  2487. int delta_hosts;
  2488. char work[20];
  2489. if (1 != sscanf(buf, "%10s", work))
  2490. return -EINVAL;
  2491. { /* temporary hack around sscanf() problem with -ve nums */
  2492. int neg = 0;
  2493. if ('-' == *work)
  2494. neg = 1;
  2495. if (1 != sscanf(work + neg, "%d", &delta_hosts))
  2496. return -EINVAL;
  2497. if (neg)
  2498. delta_hosts = -delta_hosts;
  2499. }
  2500. if (delta_hosts > 0) {
  2501. do {
  2502. sdebug_add_adapter();
  2503. } while (--delta_hosts);
  2504. } else if (delta_hosts < 0) {
  2505. do {
  2506. sdebug_remove_adapter();
  2507. } while (++delta_hosts);
  2508. }
  2509. return count;
  2510. }
  2511. DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
  2512. sdebug_add_host_store);
  2513. static ssize_t sdebug_vpd_use_hostno_show(struct device_driver * ddp,
  2514. char * buf)
  2515. {
  2516. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_vpd_use_hostno);
  2517. }
  2518. static ssize_t sdebug_vpd_use_hostno_store(struct device_driver * ddp,
  2519. const char * buf, size_t count)
  2520. {
  2521. int n;
  2522. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  2523. scsi_debug_vpd_use_hostno = n;
  2524. return count;
  2525. }
  2526. return -EINVAL;
  2527. }
  2528. DRIVER_ATTR(vpd_use_hostno, S_IRUGO | S_IWUSR, sdebug_vpd_use_hostno_show,
  2529. sdebug_vpd_use_hostno_store);
  2530. /* Note: The following function creates attribute files in the
  2531. /sys/bus/pseudo/drivers/scsi_debug directory. The advantage of these
  2532. files (over those found in the /sys/module/scsi_debug/parameters
  2533. directory) is that auxiliary actions can be triggered when an attribute
  2534. is changed. For example see: sdebug_add_host_store() above.
  2535. */
  2536. static int do_create_driverfs_files(void)
  2537. {
  2538. int ret;
  2539. ret = driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  2540. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
  2541. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  2542. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  2543. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  2544. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
  2545. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  2546. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
  2547. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  2548. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  2549. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  2550. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
  2551. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  2552. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
  2553. ret |= driver_create_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
  2554. return ret;
  2555. }
  2556. static void do_remove_driverfs_files(void)
  2557. {
  2558. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_vpd_use_hostno);
  2559. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_virtual_gb);
  2560. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  2561. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
  2562. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  2563. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  2564. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  2565. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_no_lun_0);
  2566. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  2567. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_fake_rw);
  2568. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  2569. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  2570. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  2571. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
  2572. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  2573. }
  2574. static int __init scsi_debug_init(void)
  2575. {
  2576. unsigned int sz;
  2577. int host_to_add;
  2578. int k;
  2579. int ret;
  2580. if (scsi_debug_dev_size_mb < 1)
  2581. scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  2582. sdebug_store_size = (unsigned int)scsi_debug_dev_size_mb * 1048576;
  2583. sdebug_store_sectors = sdebug_store_size / SECT_SIZE;
  2584. if (scsi_debug_virtual_gb > 0) {
  2585. sdebug_capacity = 2048 * 1024;
  2586. sdebug_capacity *= scsi_debug_virtual_gb;
  2587. } else
  2588. sdebug_capacity = sdebug_store_sectors;
  2589. /* play around with geometry, don't waste too much on track 0 */
  2590. sdebug_heads = 8;
  2591. sdebug_sectors_per = 32;
  2592. if (scsi_debug_dev_size_mb >= 16)
  2593. sdebug_heads = 32;
  2594. else if (scsi_debug_dev_size_mb >= 256)
  2595. sdebug_heads = 64;
  2596. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2597. (sdebug_sectors_per * sdebug_heads);
  2598. if (sdebug_cylinders_per >= 1024) {
  2599. /* other LLDs do this; implies >= 1GB ram disk ... */
  2600. sdebug_heads = 255;
  2601. sdebug_sectors_per = 63;
  2602. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  2603. (sdebug_sectors_per * sdebug_heads);
  2604. }
  2605. sz = sdebug_store_size;
  2606. fake_storep = vmalloc(sz);
  2607. if (NULL == fake_storep) {
  2608. printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
  2609. return -ENOMEM;
  2610. }
  2611. memset(fake_storep, 0, sz);
  2612. if (scsi_debug_num_parts > 0)
  2613. sdebug_build_parts(fake_storep);
  2614. ret = device_register(&pseudo_primary);
  2615. if (ret < 0) {
  2616. printk(KERN_WARNING "scsi_debug: device_register error: %d\n",
  2617. ret);
  2618. goto free_vm;
  2619. }
  2620. ret = bus_register(&pseudo_lld_bus);
  2621. if (ret < 0) {
  2622. printk(KERN_WARNING "scsi_debug: bus_register error: %d\n",
  2623. ret);
  2624. goto dev_unreg;
  2625. }
  2626. ret = driver_register(&sdebug_driverfs_driver);
  2627. if (ret < 0) {
  2628. printk(KERN_WARNING "scsi_debug: driver_register error: %d\n",
  2629. ret);
  2630. goto bus_unreg;
  2631. }
  2632. ret = do_create_driverfs_files();
  2633. if (ret < 0) {
  2634. printk(KERN_WARNING "scsi_debug: driver_create_file error: %d\n",
  2635. ret);
  2636. goto del_files;
  2637. }
  2638. init_all_queued();
  2639. sdebug_driver_template.proc_name = sdebug_proc_name;
  2640. host_to_add = scsi_debug_add_host;
  2641. scsi_debug_add_host = 0;
  2642. for (k = 0; k < host_to_add; k++) {
  2643. if (sdebug_add_adapter()) {
  2644. printk(KERN_ERR "scsi_debug_init: "
  2645. "sdebug_add_adapter failed k=%d\n", k);
  2646. break;
  2647. }
  2648. }
  2649. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  2650. printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
  2651. scsi_debug_add_host);
  2652. }
  2653. return 0;
  2654. del_files:
  2655. do_remove_driverfs_files();
  2656. driver_unregister(&sdebug_driverfs_driver);
  2657. bus_unreg:
  2658. bus_unregister(&pseudo_lld_bus);
  2659. dev_unreg:
  2660. device_unregister(&pseudo_primary);
  2661. free_vm:
  2662. vfree(fake_storep);
  2663. return ret;
  2664. }
  2665. static void __exit scsi_debug_exit(void)
  2666. {
  2667. int k = scsi_debug_add_host;
  2668. stop_all_queued();
  2669. for (; k; k--)
  2670. sdebug_remove_adapter();
  2671. do_remove_driverfs_files();
  2672. driver_unregister(&sdebug_driverfs_driver);
  2673. bus_unregister(&pseudo_lld_bus);
  2674. device_unregister(&pseudo_primary);
  2675. vfree(fake_storep);
  2676. }
  2677. device_initcall(scsi_debug_init);
  2678. module_exit(scsi_debug_exit);
  2679. static void pseudo_0_release(struct device * dev)
  2680. {
  2681. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  2682. printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
  2683. }
  2684. static struct device pseudo_primary = {
  2685. .bus_id = "pseudo_0",
  2686. .release = pseudo_0_release,
  2687. };
  2688. static int pseudo_lld_bus_match(struct device *dev,
  2689. struct device_driver *dev_driver)
  2690. {
  2691. return 1;
  2692. }
  2693. static struct bus_type pseudo_lld_bus = {
  2694. .name = "pseudo",
  2695. .match = pseudo_lld_bus_match,
  2696. .probe = sdebug_driver_probe,
  2697. .remove = sdebug_driver_remove,
  2698. };
  2699. static void sdebug_release_adapter(struct device * dev)
  2700. {
  2701. struct sdebug_host_info *sdbg_host;
  2702. sdbg_host = to_sdebug_host(dev);
  2703. kfree(sdbg_host);
  2704. }
  2705. static int sdebug_add_adapter(void)
  2706. {
  2707. int k, devs_per_host;
  2708. int error = 0;
  2709. struct sdebug_host_info *sdbg_host;
  2710. struct sdebug_dev_info *sdbg_devinfo;
  2711. struct list_head *lh, *lh_sf;
  2712. sdbg_host = kzalloc(sizeof(*sdbg_host),GFP_KERNEL);
  2713. if (NULL == sdbg_host) {
  2714. printk(KERN_ERR "%s: out of memory at line %d\n",
  2715. __FUNCTION__, __LINE__);
  2716. return -ENOMEM;
  2717. }
  2718. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  2719. devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
  2720. for (k = 0; k < devs_per_host; k++) {
  2721. sdbg_devinfo = kzalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
  2722. if (NULL == sdbg_devinfo) {
  2723. printk(KERN_ERR "%s: out of memory at line %d\n",
  2724. __FUNCTION__, __LINE__);
  2725. error = -ENOMEM;
  2726. goto clean;
  2727. }
  2728. sdbg_devinfo->sdbg_host = sdbg_host;
  2729. list_add_tail(&sdbg_devinfo->dev_list,
  2730. &sdbg_host->dev_info_list);
  2731. }
  2732. spin_lock(&sdebug_host_list_lock);
  2733. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  2734. spin_unlock(&sdebug_host_list_lock);
  2735. sdbg_host->dev.bus = &pseudo_lld_bus;
  2736. sdbg_host->dev.parent = &pseudo_primary;
  2737. sdbg_host->dev.release = &sdebug_release_adapter;
  2738. sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
  2739. error = device_register(&sdbg_host->dev);
  2740. if (error)
  2741. goto clean;
  2742. ++scsi_debug_add_host;
  2743. return error;
  2744. clean:
  2745. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  2746. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  2747. dev_list);
  2748. list_del(&sdbg_devinfo->dev_list);
  2749. kfree(sdbg_devinfo);
  2750. }
  2751. kfree(sdbg_host);
  2752. return error;
  2753. }
  2754. static void sdebug_remove_adapter(void)
  2755. {
  2756. struct sdebug_host_info * sdbg_host = NULL;
  2757. spin_lock(&sdebug_host_list_lock);
  2758. if (!list_empty(&sdebug_host_list)) {
  2759. sdbg_host = list_entry(sdebug_host_list.prev,
  2760. struct sdebug_host_info, host_list);
  2761. list_del(&sdbg_host->host_list);
  2762. }
  2763. spin_unlock(&sdebug_host_list_lock);
  2764. if (!sdbg_host)
  2765. return;
  2766. device_unregister(&sdbg_host->dev);
  2767. --scsi_debug_add_host;
  2768. }
  2769. static int sdebug_driver_probe(struct device * dev)
  2770. {
  2771. int error = 0;
  2772. struct sdebug_host_info *sdbg_host;
  2773. struct Scsi_Host *hpnt;
  2774. sdbg_host = to_sdebug_host(dev);
  2775. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  2776. if (NULL == hpnt) {
  2777. printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
  2778. error = -ENODEV;
  2779. return error;
  2780. }
  2781. sdbg_host->shost = hpnt;
  2782. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  2783. if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
  2784. hpnt->max_id = scsi_debug_num_tgts + 1;
  2785. else
  2786. hpnt->max_id = scsi_debug_num_tgts;
  2787. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* = scsi_debug_max_luns; */
  2788. error = scsi_add_host(hpnt, &sdbg_host->dev);
  2789. if (error) {
  2790. printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
  2791. error = -ENODEV;
  2792. scsi_host_put(hpnt);
  2793. } else
  2794. scsi_scan_host(hpnt);
  2795. return error;
  2796. }
  2797. static int sdebug_driver_remove(struct device * dev)
  2798. {
  2799. struct list_head *lh, *lh_sf;
  2800. struct sdebug_host_info *sdbg_host;
  2801. struct sdebug_dev_info *sdbg_devinfo;
  2802. sdbg_host = to_sdebug_host(dev);
  2803. if (!sdbg_host) {
  2804. printk(KERN_ERR "%s: Unable to locate host info\n",
  2805. __FUNCTION__);
  2806. return -ENODEV;
  2807. }
  2808. scsi_remove_host(sdbg_host->shost);
  2809. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  2810. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  2811. dev_list);
  2812. list_del(&sdbg_devinfo->dev_list);
  2813. kfree(sdbg_devinfo);
  2814. }
  2815. scsi_host_put(sdbg_host->shost);
  2816. return 0;
  2817. }
  2818. static void sdebug_max_tgts_luns(void)
  2819. {
  2820. struct sdebug_host_info * sdbg_host;
  2821. struct Scsi_Host *hpnt;
  2822. spin_lock(&sdebug_host_list_lock);
  2823. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  2824. hpnt = sdbg_host->shost;
  2825. if ((hpnt->this_id >= 0) &&
  2826. (scsi_debug_num_tgts > hpnt->this_id))
  2827. hpnt->max_id = scsi_debug_num_tgts + 1;
  2828. else
  2829. hpnt->max_id = scsi_debug_num_tgts;
  2830. hpnt->max_lun = SAM2_WLUN_REPORT_LUNS; /* scsi_debug_max_luns; */
  2831. }
  2832. spin_unlock(&sdebug_host_list_lock);
  2833. }