scsi_debug.c 91 KB

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