scsi_debug.c 94 KB

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