scsi_debug.c 95 KB

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