scsi_debug.c 91 KB

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