scsi_debug.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /*
  2. * linux/kernel/scsi_debug.c
  3. * vvvvvvvvvvvvvvvvvvvvvvv Original vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  4. * Copyright (C) 1992 Eric Youngdale
  5. * Simulate a host adapter with 2 disks attached. Do a lot of checking
  6. * to make sure that we are not getting blocks mixed up, and PANIC if
  7. * anything out of the ordinary is seen.
  8. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. *
  10. * This version is more generic, simulating a variable number of disk
  11. * (or disk like devices) sharing a common amount of RAM
  12. *
  13. *
  14. * For documentation see http://www.torque.net/sg/sdebug26.html
  15. *
  16. * D. Gilbert (dpg) work for Magneto-Optical device test [20010421]
  17. * dpg: work for devfs large number of disks [20010809]
  18. * forked for lk 2.5 series [20011216, 20020101]
  19. * use vmalloc() more inquiry+mode_sense [20020302]
  20. * add timers for delayed responses [20020721]
  21. * Patrick Mansfield <patmans@us.ibm.com> max_luns+scsi_level [20021031]
  22. * Mike Anderson <andmike@us.ibm.com> sysfs work [20021118]
  23. * dpg: change style of boot options to "scsi_debug.num_tgts=2" and
  24. * module options to "modprobe scsi_debug num_tgts=2" [20021221]
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/timer.h>
  32. #include <linux/types.h>
  33. #include <linux/string.h>
  34. #include <linux/genhd.h>
  35. #include <linux/fs.h>
  36. #include <linux/init.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/blkdev.h>
  42. #include "scsi.h"
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsicam.h>
  45. #include <linux/stat.h>
  46. #include "scsi_logging.h"
  47. #include "scsi_debug.h"
  48. #define SCSI_DEBUG_VERSION "1.75"
  49. static const char * scsi_debug_version_date = "20050113";
  50. /* Additional Sense Code (ASC) used */
  51. #define NO_ADDED_SENSE 0x0
  52. #define UNRECOVERED_READ_ERR 0x11
  53. #define INVALID_OPCODE 0x20
  54. #define ADDR_OUT_OF_RANGE 0x21
  55. #define INVALID_FIELD_IN_CDB 0x24
  56. #define POWERON_RESET 0x29
  57. #define SAVING_PARAMS_UNSUP 0x39
  58. #define THRESHHOLD_EXCEEDED 0x5d
  59. #define SDEBUG_TAGGED_QUEUING 0 /* 0 | MSG_SIMPLE_TAG | MSG_ORDERED_TAG */
  60. /* Default values for driver parameters */
  61. #define DEF_NUM_HOST 1
  62. #define DEF_NUM_TGTS 1
  63. #define DEF_MAX_LUNS 1
  64. /* With these defaults, this driver will make 1 host with 1 target
  65. * (id 0) containing 1 logical unit (lun 0). That is 1 device.
  66. */
  67. #define DEF_DELAY 1
  68. #define DEF_DEV_SIZE_MB 8
  69. #define DEF_EVERY_NTH 0
  70. #define DEF_NUM_PARTS 0
  71. #define DEF_OPTS 0
  72. #define DEF_SCSI_LEVEL 5 /* INQUIRY, byte2 [5->SPC-3] */
  73. #define DEF_PTYPE 0
  74. #define DEF_D_SENSE 0
  75. /* bit mask values for scsi_debug_opts */
  76. #define SCSI_DEBUG_OPT_NOISE 1
  77. #define SCSI_DEBUG_OPT_MEDIUM_ERR 2
  78. #define SCSI_DEBUG_OPT_TIMEOUT 4
  79. #define SCSI_DEBUG_OPT_RECOVERED_ERR 8
  80. /* When "every_nth" > 0 then modulo "every_nth" commands:
  81. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  82. * - a RECOVERED_ERROR is simulated on successful read and write
  83. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  84. *
  85. * When "every_nth" < 0 then after "- every_nth" commands:
  86. * - a no response is simulated if SCSI_DEBUG_OPT_TIMEOUT is set
  87. * - a RECOVERED_ERROR is simulated on successful read and write
  88. * commands if SCSI_DEBUG_OPT_RECOVERED_ERR is set.
  89. * This will continue until some other action occurs (e.g. the user
  90. * writing a new value (other than -1 or 1) to every_nth via sysfs).
  91. */
  92. /* when 1==SCSI_DEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this
  93. * sector on read commands: */
  94. #define OPT_MEDIUM_ERR_ADDR 0x1234 /* that's sector 4660 in decimal */
  95. /* If REPORT LUNS has luns >= 256 it can choose "flat space" (value 1)
  96. * or "peripheral device" addressing (value 0) */
  97. #define SAM2_LUN_ADDRESS_METHOD 0
  98. static int scsi_debug_add_host = DEF_NUM_HOST;
  99. static int scsi_debug_delay = DEF_DELAY;
  100. static int scsi_debug_dev_size_mb = DEF_DEV_SIZE_MB;
  101. static int scsi_debug_every_nth = DEF_EVERY_NTH;
  102. static int scsi_debug_max_luns = DEF_MAX_LUNS;
  103. static int scsi_debug_num_parts = DEF_NUM_PARTS;
  104. static int scsi_debug_num_tgts = DEF_NUM_TGTS; /* targets per host */
  105. static int scsi_debug_opts = DEF_OPTS;
  106. static int scsi_debug_scsi_level = DEF_SCSI_LEVEL;
  107. static int scsi_debug_ptype = DEF_PTYPE; /* SCSI peripheral type (0==disk) */
  108. static int scsi_debug_dsense = DEF_D_SENSE;
  109. static int scsi_debug_cmnd_count = 0;
  110. #define DEV_READONLY(TGT) (0)
  111. #define DEV_REMOVEABLE(TGT) (0)
  112. static unsigned long sdebug_store_size; /* in bytes */
  113. static sector_t sdebug_capacity; /* in sectors */
  114. /* old BIOS stuff, kernel may get rid of them but some mode sense pages
  115. may still need them */
  116. static int sdebug_heads; /* heads per disk */
  117. static int sdebug_cylinders_per; /* cylinders per surface */
  118. static int sdebug_sectors_per; /* sectors per cylinder */
  119. /* default sector size is 512 bytes, 2**9 bytes */
  120. #define POW2_SECT_SIZE 9
  121. #define SECT_SIZE (1 << POW2_SECT_SIZE)
  122. #define SECT_SIZE_PER(TGT) SECT_SIZE
  123. #define SDEBUG_MAX_PARTS 4
  124. #define SDEBUG_SENSE_LEN 32
  125. struct sdebug_dev_info {
  126. struct list_head dev_list;
  127. unsigned char sense_buff[SDEBUG_SENSE_LEN]; /* weak nexus */
  128. unsigned int channel;
  129. unsigned int target;
  130. unsigned int lun;
  131. struct sdebug_host_info *sdbg_host;
  132. char reset;
  133. char used;
  134. };
  135. struct sdebug_host_info {
  136. struct list_head host_list;
  137. struct Scsi_Host *shost;
  138. struct device dev;
  139. struct list_head dev_info_list;
  140. };
  141. #define to_sdebug_host(d) \
  142. container_of(d, struct sdebug_host_info, dev)
  143. static LIST_HEAD(sdebug_host_list);
  144. static DEFINE_SPINLOCK(sdebug_host_list_lock);
  145. typedef void (* done_funct_t) (struct scsi_cmnd *);
  146. struct sdebug_queued_cmd {
  147. int in_use;
  148. struct timer_list cmnd_timer;
  149. done_funct_t done_funct;
  150. struct scsi_cmnd * a_cmnd;
  151. int scsi_result;
  152. };
  153. static struct sdebug_queued_cmd queued_arr[SCSI_DEBUG_CANQUEUE];
  154. static struct scsi_host_template sdebug_driver_template = {
  155. .proc_info = scsi_debug_proc_info,
  156. .name = "SCSI DEBUG",
  157. .info = scsi_debug_info,
  158. .slave_alloc = scsi_debug_slave_alloc,
  159. .slave_configure = scsi_debug_slave_configure,
  160. .slave_destroy = scsi_debug_slave_destroy,
  161. .ioctl = scsi_debug_ioctl,
  162. .queuecommand = scsi_debug_queuecommand,
  163. .eh_abort_handler = scsi_debug_abort,
  164. .eh_bus_reset_handler = scsi_debug_bus_reset,
  165. .eh_device_reset_handler = scsi_debug_device_reset,
  166. .eh_host_reset_handler = scsi_debug_host_reset,
  167. .bios_param = scsi_debug_biosparam,
  168. .can_queue = SCSI_DEBUG_CANQUEUE,
  169. .this_id = 7,
  170. .sg_tablesize = 64,
  171. .cmd_per_lun = 3,
  172. .max_sectors = 4096,
  173. .unchecked_isa_dma = 0,
  174. .use_clustering = DISABLE_CLUSTERING,
  175. .module = THIS_MODULE,
  176. };
  177. static unsigned char * fake_storep; /* ramdisk storage */
  178. static int num_aborts = 0;
  179. static int num_dev_resets = 0;
  180. static int num_bus_resets = 0;
  181. static int num_host_resets = 0;
  182. static DEFINE_SPINLOCK(queued_arr_lock);
  183. static DEFINE_RWLOCK(atomic_rw);
  184. static char sdebug_proc_name[] = "scsi_debug";
  185. static int sdebug_driver_probe(struct device *);
  186. static int sdebug_driver_remove(struct device *);
  187. static struct bus_type pseudo_lld_bus;
  188. static struct device_driver sdebug_driverfs_driver = {
  189. .name = sdebug_proc_name,
  190. .bus = &pseudo_lld_bus,
  191. .probe = sdebug_driver_probe,
  192. .remove = sdebug_driver_remove,
  193. };
  194. static const int check_condition_result =
  195. (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  196. /* function declarations */
  197. static int resp_inquiry(struct scsi_cmnd * SCpnt, int target,
  198. struct sdebug_dev_info * devip);
  199. static int resp_requests(struct scsi_cmnd * SCpnt,
  200. struct sdebug_dev_info * devip);
  201. static int resp_readcap(struct scsi_cmnd * SCpnt,
  202. struct sdebug_dev_info * devip);
  203. static int resp_mode_sense(struct scsi_cmnd * SCpnt, int target,
  204. struct sdebug_dev_info * devip);
  205. static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
  206. int num, struct sdebug_dev_info * devip);
  207. static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
  208. int num, struct sdebug_dev_info * devip);
  209. static int resp_report_luns(struct scsi_cmnd * SCpnt,
  210. struct sdebug_dev_info * devip);
  211. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  212. int arr_len);
  213. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  214. int max_arr_len);
  215. static void timer_intr_handler(unsigned long);
  216. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev);
  217. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  218. int asc, int asq);
  219. static int check_reset(struct scsi_cmnd * SCpnt,
  220. struct sdebug_dev_info * devip);
  221. static int schedule_resp(struct scsi_cmnd * cmnd,
  222. struct sdebug_dev_info * devip,
  223. done_funct_t done, int scsi_result, int delta_jiff);
  224. static void __init sdebug_build_parts(unsigned char * ramp);
  225. static void __init init_all_queued(void);
  226. static void stop_all_queued(void);
  227. static int stop_queued_cmnd(struct scsi_cmnd * cmnd);
  228. static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
  229. const char * dev_id_str, int dev_id_str_len);
  230. static void do_create_driverfs_files(void);
  231. static void do_remove_driverfs_files(void);
  232. static int sdebug_add_adapter(void);
  233. static void sdebug_remove_adapter(void);
  234. static void sdebug_max_tgts_luns(void);
  235. static struct device pseudo_primary;
  236. static struct bus_type pseudo_lld_bus;
  237. static
  238. int scsi_debug_queuecommand(struct scsi_cmnd * SCpnt, done_funct_t done)
  239. {
  240. unsigned char *cmd = (unsigned char *) SCpnt->cmnd;
  241. int block, upper_blk, num, k;
  242. int errsts = 0;
  243. int target = scmd_id(SCpnt);
  244. struct sdebug_dev_info * devip = NULL;
  245. int inj_recovered = 0;
  246. if (done == NULL)
  247. return 0; /* assume mid level reprocessing command */
  248. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmd) {
  249. printk(KERN_INFO "scsi_debug: cmd ");
  250. for (k = 0, num = SCpnt->cmd_len; k < num; ++k)
  251. printk("%02x ", (int)cmd[k]);
  252. printk("\n");
  253. }
  254. if(target == sdebug_driver_template.this_id) {
  255. printk(KERN_INFO "scsi_debug: initiator's id used as "
  256. "target!\n");
  257. return schedule_resp(SCpnt, NULL, done,
  258. DID_NO_CONNECT << 16, 0);
  259. }
  260. if (SCpnt->device->lun >= scsi_debug_max_luns)
  261. return schedule_resp(SCpnt, NULL, done,
  262. DID_NO_CONNECT << 16, 0);
  263. devip = devInfoReg(SCpnt->device);
  264. if (NULL == devip)
  265. return schedule_resp(SCpnt, NULL, done,
  266. DID_NO_CONNECT << 16, 0);
  267. if ((scsi_debug_every_nth != 0) &&
  268. (++scsi_debug_cmnd_count >= abs(scsi_debug_every_nth))) {
  269. scsi_debug_cmnd_count = 0;
  270. if (scsi_debug_every_nth < -1)
  271. scsi_debug_every_nth = -1;
  272. if (SCSI_DEBUG_OPT_TIMEOUT & scsi_debug_opts)
  273. return 0; /* ignore command causing timeout */
  274. else if (SCSI_DEBUG_OPT_RECOVERED_ERR & scsi_debug_opts)
  275. inj_recovered = 1; /* to reads and writes below */
  276. }
  277. switch (*cmd) {
  278. case INQUIRY: /* mandatory, ignore unit attention */
  279. errsts = resp_inquiry(SCpnt, target, devip);
  280. break;
  281. case REQUEST_SENSE: /* mandatory, ignore unit attention */
  282. errsts = resp_requests(SCpnt, devip);
  283. break;
  284. case REZERO_UNIT: /* actually this is REWIND for SSC */
  285. case START_STOP:
  286. errsts = check_reset(SCpnt, devip);
  287. break;
  288. case ALLOW_MEDIUM_REMOVAL:
  289. if ((errsts = check_reset(SCpnt, devip)))
  290. break;
  291. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  292. printk(KERN_INFO "scsi_debug: Medium removal %s\n",
  293. cmd[4] ? "inhibited" : "enabled");
  294. break;
  295. case SEND_DIAGNOSTIC: /* mandatory */
  296. errsts = check_reset(SCpnt, devip);
  297. break;
  298. case TEST_UNIT_READY: /* mandatory */
  299. errsts = check_reset(SCpnt, devip);
  300. break;
  301. case RESERVE:
  302. errsts = check_reset(SCpnt, devip);
  303. break;
  304. case RESERVE_10:
  305. errsts = check_reset(SCpnt, devip);
  306. break;
  307. case RELEASE:
  308. errsts = check_reset(SCpnt, devip);
  309. break;
  310. case RELEASE_10:
  311. errsts = check_reset(SCpnt, devip);
  312. break;
  313. case READ_CAPACITY:
  314. errsts = resp_readcap(SCpnt, devip);
  315. break;
  316. case READ_16:
  317. case READ_12:
  318. case READ_10:
  319. case READ_6:
  320. if ((errsts = check_reset(SCpnt, devip)))
  321. break;
  322. upper_blk = 0;
  323. if ((*cmd) == READ_16) {
  324. upper_blk = cmd[5] + (cmd[4] << 8) +
  325. (cmd[3] << 16) + (cmd[2] << 24);
  326. block = cmd[9] + (cmd[8] << 8) +
  327. (cmd[7] << 16) + (cmd[6] << 24);
  328. num = cmd[13] + (cmd[12] << 8) +
  329. (cmd[11] << 16) + (cmd[10] << 24);
  330. } else if ((*cmd) == READ_12) {
  331. block = cmd[5] + (cmd[4] << 8) +
  332. (cmd[3] << 16) + (cmd[2] << 24);
  333. num = cmd[9] + (cmd[8] << 8) +
  334. (cmd[7] << 16) + (cmd[6] << 24);
  335. } else if ((*cmd) == READ_10) {
  336. block = cmd[5] + (cmd[4] << 8) +
  337. (cmd[3] << 16) + (cmd[2] << 24);
  338. num = cmd[8] + (cmd[7] << 8);
  339. } else {
  340. block = cmd[3] + (cmd[2] << 8) +
  341. ((cmd[1] & 0x1f) << 16);
  342. num = cmd[4];
  343. }
  344. errsts = resp_read(SCpnt, upper_blk, block, num, devip);
  345. if (inj_recovered && (0 == errsts)) {
  346. mk_sense_buffer(devip, RECOVERED_ERROR,
  347. THRESHHOLD_EXCEEDED, 0);
  348. errsts = check_condition_result;
  349. }
  350. break;
  351. case REPORT_LUNS: /* mandatory, ignore unit attention */
  352. errsts = resp_report_luns(SCpnt, devip);
  353. break;
  354. case VERIFY: /* 10 byte SBC-2 command */
  355. errsts = check_reset(SCpnt, devip);
  356. break;
  357. case WRITE_16:
  358. case WRITE_12:
  359. case WRITE_10:
  360. case WRITE_6:
  361. if ((errsts = check_reset(SCpnt, devip)))
  362. break;
  363. upper_blk = 0;
  364. if ((*cmd) == WRITE_16) {
  365. upper_blk = cmd[5] + (cmd[4] << 8) +
  366. (cmd[3] << 16) + (cmd[2] << 24);
  367. block = cmd[9] + (cmd[8] << 8) +
  368. (cmd[7] << 16) + (cmd[6] << 24);
  369. num = cmd[13] + (cmd[12] << 8) +
  370. (cmd[11] << 16) + (cmd[10] << 24);
  371. } else if ((*cmd) == WRITE_12) {
  372. block = cmd[5] + (cmd[4] << 8) +
  373. (cmd[3] << 16) + (cmd[2] << 24);
  374. num = cmd[9] + (cmd[8] << 8) +
  375. (cmd[7] << 16) + (cmd[6] << 24);
  376. } else if ((*cmd) == WRITE_10) {
  377. block = cmd[5] + (cmd[4] << 8) +
  378. (cmd[3] << 16) + (cmd[2] << 24);
  379. num = cmd[8] + (cmd[7] << 8);
  380. } else {
  381. block = cmd[3] + (cmd[2] << 8) +
  382. ((cmd[1] & 0x1f) << 16);
  383. num = cmd[4];
  384. }
  385. errsts = resp_write(SCpnt, upper_blk, block, num, devip);
  386. if (inj_recovered && (0 == errsts)) {
  387. mk_sense_buffer(devip, RECOVERED_ERROR,
  388. THRESHHOLD_EXCEEDED, 0);
  389. errsts = check_condition_result;
  390. }
  391. break;
  392. case MODE_SENSE:
  393. case MODE_SENSE_10:
  394. errsts = resp_mode_sense(SCpnt, target, devip);
  395. break;
  396. case SYNCHRONIZE_CACHE:
  397. errsts = check_reset(SCpnt, devip);
  398. break;
  399. default:
  400. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  401. printk(KERN_INFO "scsi_debug: Opcode: 0x%x not "
  402. "supported\n", *cmd);
  403. if ((errsts = check_reset(SCpnt, devip)))
  404. break; /* Unit attention takes precedence */
  405. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_OPCODE, 0);
  406. errsts = check_condition_result;
  407. break;
  408. }
  409. return schedule_resp(SCpnt, devip, done, errsts, scsi_debug_delay);
  410. }
  411. static int scsi_debug_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  412. {
  413. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  414. printk(KERN_INFO "scsi_debug: ioctl: cmd=0x%x\n", cmd);
  415. }
  416. return -EINVAL;
  417. /* return -ENOTTY; // correct return but upsets fdisk */
  418. }
  419. static int check_reset(struct scsi_cmnd * SCpnt, struct sdebug_dev_info * devip)
  420. {
  421. if (devip->reset) {
  422. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  423. printk(KERN_INFO "scsi_debug: Reporting Unit "
  424. "attention: power on reset\n");
  425. devip->reset = 0;
  426. mk_sense_buffer(devip, UNIT_ATTENTION, POWERON_RESET, 0);
  427. return check_condition_result;
  428. }
  429. return 0;
  430. }
  431. /* Returns 0 if ok else (DID_ERROR << 16). Sets scp->resid . */
  432. static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  433. int arr_len)
  434. {
  435. int k, req_len, act_len, len, active;
  436. void * kaddr;
  437. void * kaddr_off;
  438. struct scatterlist * sgpnt;
  439. if (0 == scp->request_bufflen)
  440. return 0;
  441. if (NULL == scp->request_buffer)
  442. return (DID_ERROR << 16);
  443. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  444. (scp->sc_data_direction == DMA_FROM_DEVICE)))
  445. return (DID_ERROR << 16);
  446. if (0 == scp->use_sg) {
  447. req_len = scp->request_bufflen;
  448. act_len = (req_len < arr_len) ? req_len : arr_len;
  449. memcpy(scp->request_buffer, arr, act_len);
  450. scp->resid = req_len - act_len;
  451. return 0;
  452. }
  453. sgpnt = (struct scatterlist *)scp->request_buffer;
  454. active = 1;
  455. for (k = 0, req_len = 0, act_len = 0; k < scp->use_sg; ++k, ++sgpnt) {
  456. if (active) {
  457. kaddr = (unsigned char *)
  458. kmap_atomic(sgpnt->page, KM_USER0);
  459. if (NULL == kaddr)
  460. return (DID_ERROR << 16);
  461. kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
  462. len = sgpnt->length;
  463. if ((req_len + len) > arr_len) {
  464. active = 0;
  465. len = arr_len - req_len;
  466. }
  467. memcpy(kaddr_off, arr + req_len, len);
  468. kunmap_atomic(kaddr, KM_USER0);
  469. act_len += len;
  470. }
  471. req_len += sgpnt->length;
  472. }
  473. scp->resid = req_len - act_len;
  474. return 0;
  475. }
  476. /* Returns number of bytes fetched into 'arr' or -1 if error. */
  477. static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
  478. int max_arr_len)
  479. {
  480. int k, req_len, len, fin;
  481. void * kaddr;
  482. void * kaddr_off;
  483. struct scatterlist * sgpnt;
  484. if (0 == scp->request_bufflen)
  485. return 0;
  486. if (NULL == scp->request_buffer)
  487. return -1;
  488. if (! ((scp->sc_data_direction == DMA_BIDIRECTIONAL) ||
  489. (scp->sc_data_direction == DMA_TO_DEVICE)))
  490. return -1;
  491. if (0 == scp->use_sg) {
  492. req_len = scp->request_bufflen;
  493. len = (req_len < max_arr_len) ? req_len : max_arr_len;
  494. memcpy(arr, scp->request_buffer, len);
  495. return len;
  496. }
  497. sgpnt = (struct scatterlist *)scp->request_buffer;
  498. for (k = 0, req_len = 0, fin = 0; k < scp->use_sg; ++k, ++sgpnt) {
  499. kaddr = (unsigned char *)kmap_atomic(sgpnt->page, KM_USER0);
  500. if (NULL == kaddr)
  501. return -1;
  502. kaddr_off = (unsigned char *)kaddr + sgpnt->offset;
  503. len = sgpnt->length;
  504. if ((req_len + len) > max_arr_len) {
  505. len = max_arr_len - req_len;
  506. fin = 1;
  507. }
  508. memcpy(arr + req_len, kaddr_off, len);
  509. kunmap_atomic(kaddr, KM_USER0);
  510. if (fin)
  511. return req_len + len;
  512. req_len += sgpnt->length;
  513. }
  514. return req_len;
  515. }
  516. static const char * inq_vendor_id = "Linux ";
  517. static const char * inq_product_id = "scsi_debug ";
  518. static const char * inq_product_rev = "0004";
  519. static int inquiry_evpd_83(unsigned char * arr, int dev_id_num,
  520. const char * dev_id_str, int dev_id_str_len)
  521. {
  522. int num;
  523. /* Two identification descriptors: */
  524. /* T10 vendor identifier field format (faked) */
  525. arr[0] = 0x2; /* ASCII */
  526. arr[1] = 0x1;
  527. arr[2] = 0x0;
  528. memcpy(&arr[4], inq_vendor_id, 8);
  529. memcpy(&arr[12], inq_product_id, 16);
  530. memcpy(&arr[28], dev_id_str, dev_id_str_len);
  531. num = 8 + 16 + dev_id_str_len;
  532. arr[3] = num;
  533. num += 4;
  534. /* NAA IEEE registered identifier (faked) */
  535. arr[num] = 0x1; /* binary */
  536. arr[num + 1] = 0x3;
  537. arr[num + 2] = 0x0;
  538. arr[num + 3] = 0x8;
  539. arr[num + 4] = 0x51; /* ieee company id=0x123456 (faked) */
  540. arr[num + 5] = 0x23;
  541. arr[num + 6] = 0x45;
  542. arr[num + 7] = 0x60;
  543. arr[num + 8] = (dev_id_num >> 24);
  544. arr[num + 9] = (dev_id_num >> 16) & 0xff;
  545. arr[num + 10] = (dev_id_num >> 8) & 0xff;
  546. arr[num + 11] = dev_id_num & 0xff;
  547. return num + 12;
  548. }
  549. #define SDEBUG_LONG_INQ_SZ 96
  550. #define SDEBUG_MAX_INQ_ARR_SZ 128
  551. static int resp_inquiry(struct scsi_cmnd * scp, int target,
  552. struct sdebug_dev_info * devip)
  553. {
  554. unsigned char pq_pdt;
  555. unsigned char arr[SDEBUG_MAX_INQ_ARR_SZ];
  556. unsigned char *cmd = (unsigned char *)scp->cmnd;
  557. int alloc_len;
  558. alloc_len = (cmd[3] << 8) + cmd[4];
  559. memset(arr, 0, SDEBUG_MAX_INQ_ARR_SZ);
  560. pq_pdt = (scsi_debug_ptype & 0x1f);
  561. arr[0] = pq_pdt;
  562. if (0x2 & cmd[1]) { /* CMDDT bit set */
  563. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  564. 0);
  565. return check_condition_result;
  566. } else if (0x1 & cmd[1]) { /* EVPD bit set */
  567. int dev_id_num, len;
  568. char dev_id_str[6];
  569. dev_id_num = ((devip->sdbg_host->shost->host_no + 1) * 2000) +
  570. (devip->target * 1000) + devip->lun;
  571. len = scnprintf(dev_id_str, 6, "%d", dev_id_num);
  572. if (0 == cmd[2]) { /* supported vital product data pages */
  573. arr[3] = 3;
  574. arr[4] = 0x0; /* this page */
  575. arr[5] = 0x80; /* unit serial number */
  576. arr[6] = 0x83; /* device identification */
  577. } else if (0x80 == cmd[2]) { /* unit serial number */
  578. arr[1] = 0x80;
  579. arr[3] = len;
  580. memcpy(&arr[4], dev_id_str, len);
  581. } else if (0x83 == cmd[2]) { /* device identification */
  582. arr[1] = 0x83;
  583. arr[3] = inquiry_evpd_83(&arr[4], dev_id_num,
  584. dev_id_str, len);
  585. } else {
  586. /* Illegal request, invalid field in cdb */
  587. mk_sense_buffer(devip, ILLEGAL_REQUEST,
  588. INVALID_FIELD_IN_CDB, 0);
  589. return check_condition_result;
  590. }
  591. return fill_from_dev_buffer(scp, arr,
  592. min(alloc_len, SDEBUG_MAX_INQ_ARR_SZ));
  593. }
  594. /* drops through here for a standard inquiry */
  595. arr[1] = DEV_REMOVEABLE(target) ? 0x80 : 0; /* Removable disk */
  596. arr[2] = scsi_debug_scsi_level;
  597. arr[3] = 2; /* response_data_format==2 */
  598. arr[4] = SDEBUG_LONG_INQ_SZ - 5;
  599. arr[6] = 0x1; /* claim: ADDR16 */
  600. /* arr[6] |= 0x40; ... claim: EncServ (enclosure services) */
  601. arr[7] = 0x3a; /* claim: WBUS16, SYNC, LINKED + CMDQUE */
  602. memcpy(&arr[8], inq_vendor_id, 8);
  603. memcpy(&arr[16], inq_product_id, 16);
  604. memcpy(&arr[32], inq_product_rev, 4);
  605. /* version descriptors (2 bytes each) follow */
  606. arr[58] = 0x0; arr[59] = 0x40; /* SAM-2 */
  607. arr[60] = 0x3; arr[61] = 0x0; /* SPC-3 */
  608. if (scsi_debug_ptype == 0) {
  609. arr[62] = 0x1; arr[63] = 0x80; /* SBC */
  610. } else if (scsi_debug_ptype == 1) {
  611. arr[62] = 0x2; arr[63] = 0x00; /* SSC */
  612. }
  613. return fill_from_dev_buffer(scp, arr,
  614. min(alloc_len, SDEBUG_LONG_INQ_SZ));
  615. }
  616. static int resp_requests(struct scsi_cmnd * scp,
  617. struct sdebug_dev_info * devip)
  618. {
  619. unsigned char * sbuff;
  620. unsigned char *cmd = (unsigned char *)scp->cmnd;
  621. unsigned char arr[SDEBUG_SENSE_LEN];
  622. int len = 18;
  623. memset(arr, 0, SDEBUG_SENSE_LEN);
  624. if (devip->reset == 1)
  625. mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
  626. sbuff = devip->sense_buff;
  627. if ((cmd[1] & 1) && (! scsi_debug_dsense)) {
  628. /* DESC bit set and sense_buff in fixed format */
  629. arr[0] = 0x72;
  630. arr[1] = sbuff[2]; /* sense key */
  631. arr[2] = sbuff[12]; /* asc */
  632. arr[3] = sbuff[13]; /* ascq */
  633. len = 8;
  634. } else
  635. memcpy(arr, sbuff, SDEBUG_SENSE_LEN);
  636. mk_sense_buffer(devip, 0, NO_ADDED_SENSE, 0);
  637. return fill_from_dev_buffer(scp, arr, len);
  638. }
  639. #define SDEBUG_READCAP_ARR_SZ 8
  640. static int resp_readcap(struct scsi_cmnd * scp,
  641. struct sdebug_dev_info * devip)
  642. {
  643. unsigned char arr[SDEBUG_READCAP_ARR_SZ];
  644. unsigned long capac;
  645. int errsts;
  646. if ((errsts = check_reset(scp, devip)))
  647. return errsts;
  648. memset(arr, 0, SDEBUG_READCAP_ARR_SZ);
  649. capac = (unsigned long)sdebug_capacity - 1;
  650. arr[0] = (capac >> 24);
  651. arr[1] = (capac >> 16) & 0xff;
  652. arr[2] = (capac >> 8) & 0xff;
  653. arr[3] = capac & 0xff;
  654. arr[6] = (SECT_SIZE_PER(target) >> 8) & 0xff;
  655. arr[7] = SECT_SIZE_PER(target) & 0xff;
  656. return fill_from_dev_buffer(scp, arr, SDEBUG_READCAP_ARR_SZ);
  657. }
  658. /* <<Following mode page info copied from ST318451LW>> */
  659. static int resp_err_recov_pg(unsigned char * p, int pcontrol, int target)
  660. { /* Read-Write Error Recovery page for mode_sense */
  661. unsigned char err_recov_pg[] = {0x1, 0xa, 0xc0, 11, 240, 0, 0, 0,
  662. 5, 0, 0xff, 0xff};
  663. memcpy(p, err_recov_pg, sizeof(err_recov_pg));
  664. if (1 == pcontrol)
  665. memset(p + 2, 0, sizeof(err_recov_pg) - 2);
  666. return sizeof(err_recov_pg);
  667. }
  668. static int resp_disconnect_pg(unsigned char * p, int pcontrol, int target)
  669. { /* Disconnect-Reconnect page for mode_sense */
  670. unsigned char disconnect_pg[] = {0x2, 0xe, 128, 128, 0, 10, 0, 0,
  671. 0, 0, 0, 0, 0, 0, 0, 0};
  672. memcpy(p, disconnect_pg, sizeof(disconnect_pg));
  673. if (1 == pcontrol)
  674. memset(p + 2, 0, sizeof(disconnect_pg) - 2);
  675. return sizeof(disconnect_pg);
  676. }
  677. static int resp_format_pg(unsigned char * p, int pcontrol, int target)
  678. { /* Format device page for mode_sense */
  679. unsigned char format_pg[] = {0x3, 0x16, 0, 0, 0, 0, 0, 0,
  680. 0, 0, 0, 0, 0, 0, 0, 0,
  681. 0, 0, 0, 0, 0x40, 0, 0, 0};
  682. memcpy(p, format_pg, sizeof(format_pg));
  683. p[10] = (sdebug_sectors_per >> 8) & 0xff;
  684. p[11] = sdebug_sectors_per & 0xff;
  685. p[12] = (SECT_SIZE >> 8) & 0xff;
  686. p[13] = SECT_SIZE & 0xff;
  687. if (DEV_REMOVEABLE(target))
  688. p[20] |= 0x20; /* should agree with INQUIRY */
  689. if (1 == pcontrol)
  690. memset(p + 2, 0, sizeof(format_pg) - 2);
  691. return sizeof(format_pg);
  692. }
  693. static int resp_caching_pg(unsigned char * p, int pcontrol, int target)
  694. { /* Caching page for mode_sense */
  695. unsigned char caching_pg[] = {0x8, 18, 0x14, 0, 0xff, 0xff, 0, 0,
  696. 0xff, 0xff, 0xff, 0xff, 0x80, 0x14, 0, 0, 0, 0, 0, 0};
  697. memcpy(p, caching_pg, sizeof(caching_pg));
  698. if (1 == pcontrol)
  699. memset(p + 2, 0, sizeof(caching_pg) - 2);
  700. return sizeof(caching_pg);
  701. }
  702. static int resp_ctrl_m_pg(unsigned char * p, int pcontrol, int target)
  703. { /* Control mode page for mode_sense */
  704. unsigned char ctrl_m_pg[] = {0xa, 10, 2, 0, 0, 0, 0, 0,
  705. 0, 0, 0x2, 0x4b};
  706. if (scsi_debug_dsense)
  707. ctrl_m_pg[2] |= 0x4;
  708. memcpy(p, ctrl_m_pg, sizeof(ctrl_m_pg));
  709. if (1 == pcontrol)
  710. memset(p + 2, 0, sizeof(ctrl_m_pg) - 2);
  711. return sizeof(ctrl_m_pg);
  712. }
  713. static int resp_iec_m_pg(unsigned char * p, int pcontrol, int target)
  714. { /* Informational Exceptions control mode page for mode_sense */
  715. unsigned char iec_m_pg[] = {0x1c, 0xa, 0x08, 0, 0, 0, 0, 0,
  716. 0, 0, 0x0, 0x0};
  717. memcpy(p, iec_m_pg, sizeof(iec_m_pg));
  718. if (1 == pcontrol)
  719. memset(p + 2, 0, sizeof(iec_m_pg) - 2);
  720. return sizeof(iec_m_pg);
  721. }
  722. #define SDEBUG_MAX_MSENSE_SZ 256
  723. static int resp_mode_sense(struct scsi_cmnd * scp, int target,
  724. struct sdebug_dev_info * devip)
  725. {
  726. unsigned char dbd;
  727. int pcontrol, pcode, subpcode;
  728. unsigned char dev_spec;
  729. int alloc_len, msense_6, offset, len, errsts;
  730. unsigned char * ap;
  731. unsigned char arr[SDEBUG_MAX_MSENSE_SZ];
  732. unsigned char *cmd = (unsigned char *)scp->cmnd;
  733. if ((errsts = check_reset(scp, devip)))
  734. return errsts;
  735. dbd = cmd[1] & 0x8;
  736. pcontrol = (cmd[2] & 0xc0) >> 6;
  737. pcode = cmd[2] & 0x3f;
  738. subpcode = cmd[3];
  739. msense_6 = (MODE_SENSE == cmd[0]);
  740. alloc_len = msense_6 ? cmd[4] : ((cmd[7] << 8) | cmd[8]);
  741. memset(arr, 0, SDEBUG_MAX_MSENSE_SZ);
  742. if (0x3 == pcontrol) { /* Saving values not supported */
  743. mk_sense_buffer(devip, ILLEGAL_REQUEST, SAVING_PARAMS_UNSUP,
  744. 0);
  745. return check_condition_result;
  746. }
  747. dev_spec = DEV_READONLY(target) ? 0x80 : 0x0;
  748. if (msense_6) {
  749. arr[2] = dev_spec;
  750. offset = 4;
  751. } else {
  752. arr[3] = dev_spec;
  753. offset = 8;
  754. }
  755. ap = arr + offset;
  756. if (0 != subpcode) { /* TODO: Control Extension page */
  757. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  758. 0);
  759. return check_condition_result;
  760. }
  761. switch (pcode) {
  762. case 0x1: /* Read-Write error recovery page, direct access */
  763. len = resp_err_recov_pg(ap, pcontrol, target);
  764. offset += len;
  765. break;
  766. case 0x2: /* Disconnect-Reconnect page, all devices */
  767. len = resp_disconnect_pg(ap, pcontrol, target);
  768. offset += len;
  769. break;
  770. case 0x3: /* Format device page, direct access */
  771. len = resp_format_pg(ap, pcontrol, target);
  772. offset += len;
  773. break;
  774. case 0x8: /* Caching page, direct access */
  775. len = resp_caching_pg(ap, pcontrol, target);
  776. offset += len;
  777. break;
  778. case 0xa: /* Control Mode page, all devices */
  779. len = resp_ctrl_m_pg(ap, pcontrol, target);
  780. offset += len;
  781. break;
  782. case 0x1c: /* Informational Exceptions Mode page, all devices */
  783. len = resp_iec_m_pg(ap, pcontrol, target);
  784. offset += len;
  785. break;
  786. case 0x3f: /* Read all Mode pages */
  787. len = resp_err_recov_pg(ap, pcontrol, target);
  788. len += resp_disconnect_pg(ap + len, pcontrol, target);
  789. len += resp_format_pg(ap + len, pcontrol, target);
  790. len += resp_caching_pg(ap + len, pcontrol, target);
  791. len += resp_ctrl_m_pg(ap + len, pcontrol, target);
  792. len += resp_iec_m_pg(ap + len, pcontrol, target);
  793. offset += len;
  794. break;
  795. default:
  796. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  797. 0);
  798. return check_condition_result;
  799. }
  800. if (msense_6)
  801. arr[0] = offset - 1;
  802. else {
  803. arr[0] = ((offset - 2) >> 8) & 0xff;
  804. arr[1] = (offset - 2) & 0xff;
  805. }
  806. return fill_from_dev_buffer(scp, arr, min(alloc_len, offset));
  807. }
  808. static int resp_read(struct scsi_cmnd * SCpnt, int upper_blk, int block,
  809. int num, struct sdebug_dev_info * devip)
  810. {
  811. unsigned long iflags;
  812. int ret;
  813. if (upper_blk || (block + num > sdebug_capacity)) {
  814. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  815. 0);
  816. return check_condition_result;
  817. }
  818. if ((SCSI_DEBUG_OPT_MEDIUM_ERR & scsi_debug_opts) &&
  819. (block <= OPT_MEDIUM_ERR_ADDR) &&
  820. ((block + num) > OPT_MEDIUM_ERR_ADDR)) {
  821. mk_sense_buffer(devip, MEDIUM_ERROR, UNRECOVERED_READ_ERR,
  822. 0);
  823. /* claim unrecoverable read error */
  824. return check_condition_result;
  825. }
  826. read_lock_irqsave(&atomic_rw, iflags);
  827. ret = fill_from_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
  828. num * SECT_SIZE);
  829. read_unlock_irqrestore(&atomic_rw, iflags);
  830. return ret;
  831. }
  832. static int resp_write(struct scsi_cmnd * SCpnt, int upper_blk, int block,
  833. int num, struct sdebug_dev_info * devip)
  834. {
  835. unsigned long iflags;
  836. int res;
  837. if (upper_blk || (block + num > sdebug_capacity)) {
  838. mk_sense_buffer(devip, ILLEGAL_REQUEST, ADDR_OUT_OF_RANGE,
  839. 0);
  840. return check_condition_result;
  841. }
  842. write_lock_irqsave(&atomic_rw, iflags);
  843. res = fetch_to_dev_buffer(SCpnt, fake_storep + (block * SECT_SIZE),
  844. num * SECT_SIZE);
  845. write_unlock_irqrestore(&atomic_rw, iflags);
  846. if (-1 == res)
  847. return (DID_ERROR << 16);
  848. else if ((res < (num * SECT_SIZE)) &&
  849. (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts))
  850. printk(KERN_INFO "scsi_debug: write: cdb indicated=%d, "
  851. " IO sent=%d bytes\n", num * SECT_SIZE, res);
  852. return 0;
  853. }
  854. #define SDEBUG_RLUN_ARR_SZ 128
  855. static int resp_report_luns(struct scsi_cmnd * scp,
  856. struct sdebug_dev_info * devip)
  857. {
  858. unsigned int alloc_len;
  859. int lun_cnt, i, upper;
  860. unsigned char *cmd = (unsigned char *)scp->cmnd;
  861. int select_report = (int)cmd[2];
  862. struct scsi_lun *one_lun;
  863. unsigned char arr[SDEBUG_RLUN_ARR_SZ];
  864. alloc_len = cmd[9] + (cmd[8] << 8) + (cmd[7] << 16) + (cmd[6] << 24);
  865. if ((alloc_len < 16) || (select_report > 2)) {
  866. mk_sense_buffer(devip, ILLEGAL_REQUEST, INVALID_FIELD_IN_CDB,
  867. 0);
  868. return check_condition_result;
  869. }
  870. /* can produce response with up to 16k luns (lun 0 to lun 16383) */
  871. memset(arr, 0, SDEBUG_RLUN_ARR_SZ);
  872. lun_cnt = scsi_debug_max_luns;
  873. arr[2] = ((sizeof(struct scsi_lun) * lun_cnt) >> 8) & 0xff;
  874. arr[3] = (sizeof(struct scsi_lun) * lun_cnt) & 0xff;
  875. lun_cnt = min((int)((SDEBUG_RLUN_ARR_SZ - 8) /
  876. sizeof(struct scsi_lun)), lun_cnt);
  877. one_lun = (struct scsi_lun *) &arr[8];
  878. for (i = 0; i < lun_cnt; i++) {
  879. upper = (i >> 8) & 0x3f;
  880. if (upper)
  881. one_lun[i].scsi_lun[0] =
  882. (upper | (SAM2_LUN_ADDRESS_METHOD << 6));
  883. one_lun[i].scsi_lun[1] = i & 0xff;
  884. }
  885. return fill_from_dev_buffer(scp, arr,
  886. min((int)alloc_len, SDEBUG_RLUN_ARR_SZ));
  887. }
  888. /* When timer goes off this function is called. */
  889. static void timer_intr_handler(unsigned long indx)
  890. {
  891. struct sdebug_queued_cmd * sqcp;
  892. unsigned long iflags;
  893. if (indx >= SCSI_DEBUG_CANQUEUE) {
  894. printk(KERN_ERR "scsi_debug:timer_intr_handler: indx too "
  895. "large\n");
  896. return;
  897. }
  898. spin_lock_irqsave(&queued_arr_lock, iflags);
  899. sqcp = &queued_arr[(int)indx];
  900. if (! sqcp->in_use) {
  901. printk(KERN_ERR "scsi_debug:timer_intr_handler: Unexpected "
  902. "interrupt\n");
  903. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  904. return;
  905. }
  906. sqcp->in_use = 0;
  907. if (sqcp->done_funct) {
  908. sqcp->a_cmnd->result = sqcp->scsi_result;
  909. sqcp->done_funct(sqcp->a_cmnd); /* callback to mid level */
  910. }
  911. sqcp->done_funct = NULL;
  912. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  913. }
  914. static int scsi_debug_slave_alloc(struct scsi_device * sdp)
  915. {
  916. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  917. sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_alloc\n");
  918. return 0;
  919. }
  920. static int scsi_debug_slave_configure(struct scsi_device * sdp)
  921. {
  922. struct sdebug_dev_info * devip;
  923. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  924. sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_configure\n");
  925. if (sdp->host->max_cmd_len != SCSI_DEBUG_MAX_CMD_LEN)
  926. sdp->host->max_cmd_len = SCSI_DEBUG_MAX_CMD_LEN;
  927. devip = devInfoReg(sdp);
  928. sdp->hostdata = devip;
  929. if (sdp->host->cmd_per_lun)
  930. scsi_adjust_queue_depth(sdp, SDEBUG_TAGGED_QUEUING,
  931. sdp->host->cmd_per_lun);
  932. return 0;
  933. }
  934. static void scsi_debug_slave_destroy(struct scsi_device * sdp)
  935. {
  936. struct sdebug_dev_info * devip =
  937. (struct sdebug_dev_info *)sdp->hostdata;
  938. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  939. sdev_printk(KERN_INFO, sdp, "scsi_debug: slave_destroy\n");
  940. if (devip) {
  941. /* make this slot avaliable for re-use */
  942. devip->used = 0;
  943. sdp->hostdata = NULL;
  944. }
  945. }
  946. static struct sdebug_dev_info * devInfoReg(struct scsi_device * sdev)
  947. {
  948. struct sdebug_host_info * sdbg_host;
  949. struct sdebug_dev_info * open_devip = NULL;
  950. struct sdebug_dev_info * devip =
  951. (struct sdebug_dev_info *)sdev->hostdata;
  952. if (devip)
  953. return devip;
  954. sdbg_host = *(struct sdebug_host_info **) sdev->host->hostdata;
  955. if(! sdbg_host) {
  956. printk(KERN_ERR "Host info NULL\n");
  957. return NULL;
  958. }
  959. list_for_each_entry(devip, &sdbg_host->dev_info_list, dev_list) {
  960. if ((devip->used) && (devip->channel == sdev->channel) &&
  961. (devip->target == sdev->id) &&
  962. (devip->lun == sdev->lun))
  963. return devip;
  964. else {
  965. if ((!devip->used) && (!open_devip))
  966. open_devip = devip;
  967. }
  968. }
  969. if (NULL == open_devip) { /* try and make a new one */
  970. open_devip = kmalloc(sizeof(*open_devip),GFP_KERNEL);
  971. if (NULL == open_devip) {
  972. printk(KERN_ERR "%s: out of memory at line %d\n",
  973. __FUNCTION__, __LINE__);
  974. return NULL;
  975. }
  976. memset(open_devip, 0, sizeof(*open_devip));
  977. open_devip->sdbg_host = sdbg_host;
  978. list_add_tail(&open_devip->dev_list,
  979. &sdbg_host->dev_info_list);
  980. }
  981. if (open_devip) {
  982. open_devip->channel = sdev->channel;
  983. open_devip->target = sdev->id;
  984. open_devip->lun = sdev->lun;
  985. open_devip->sdbg_host = sdbg_host;
  986. open_devip->reset = 1;
  987. open_devip->used = 1;
  988. memset(open_devip->sense_buff, 0, SDEBUG_SENSE_LEN);
  989. if (scsi_debug_dsense)
  990. open_devip->sense_buff[0] = 0x72;
  991. else {
  992. open_devip->sense_buff[0] = 0x70;
  993. open_devip->sense_buff[7] = 0xa;
  994. }
  995. return open_devip;
  996. }
  997. return NULL;
  998. }
  999. static void mk_sense_buffer(struct sdebug_dev_info * devip, int key,
  1000. int asc, int asq)
  1001. {
  1002. unsigned char * sbuff;
  1003. sbuff = devip->sense_buff;
  1004. memset(sbuff, 0, SDEBUG_SENSE_LEN);
  1005. if (scsi_debug_dsense) {
  1006. sbuff[0] = 0x72; /* descriptor, current */
  1007. sbuff[1] = key;
  1008. sbuff[2] = asc;
  1009. sbuff[3] = asq;
  1010. } else {
  1011. sbuff[0] = 0x70; /* fixed, current */
  1012. sbuff[2] = key;
  1013. sbuff[7] = 0xa; /* implies 18 byte sense buffer */
  1014. sbuff[12] = asc;
  1015. sbuff[13] = asq;
  1016. }
  1017. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1018. printk(KERN_INFO "scsi_debug: [sense_key,asc,ascq]: "
  1019. "[0x%x,0x%x,0x%x]\n", key, asc, asq);
  1020. }
  1021. static int scsi_debug_abort(struct scsi_cmnd * SCpnt)
  1022. {
  1023. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1024. printk(KERN_INFO "scsi_debug: abort\n");
  1025. ++num_aborts;
  1026. stop_queued_cmnd(SCpnt);
  1027. return SUCCESS;
  1028. }
  1029. static int scsi_debug_biosparam(struct scsi_device *sdev,
  1030. struct block_device * bdev, sector_t capacity, int *info)
  1031. {
  1032. int res;
  1033. unsigned char *buf;
  1034. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1035. printk(KERN_INFO "scsi_debug: biosparam\n");
  1036. buf = scsi_bios_ptable(bdev);
  1037. if (buf) {
  1038. res = scsi_partsize(buf, capacity,
  1039. &info[2], &info[0], &info[1]);
  1040. kfree(buf);
  1041. if (! res)
  1042. return res;
  1043. }
  1044. info[0] = sdebug_heads;
  1045. info[1] = sdebug_sectors_per;
  1046. info[2] = sdebug_cylinders_per;
  1047. return 0;
  1048. }
  1049. static int scsi_debug_device_reset(struct scsi_cmnd * SCpnt)
  1050. {
  1051. struct sdebug_dev_info * devip;
  1052. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1053. printk(KERN_INFO "scsi_debug: device_reset\n");
  1054. ++num_dev_resets;
  1055. if (SCpnt) {
  1056. devip = devInfoReg(SCpnt->device);
  1057. if (devip)
  1058. devip->reset = 1;
  1059. }
  1060. return SUCCESS;
  1061. }
  1062. static int scsi_debug_bus_reset(struct scsi_cmnd * SCpnt)
  1063. {
  1064. struct sdebug_host_info *sdbg_host;
  1065. struct sdebug_dev_info * dev_info;
  1066. struct scsi_device * sdp;
  1067. struct Scsi_Host * hp;
  1068. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1069. printk(KERN_INFO "scsi_debug: bus_reset\n");
  1070. ++num_bus_resets;
  1071. if (SCpnt && ((sdp = SCpnt->device)) && ((hp = sdp->host))) {
  1072. sdbg_host = *(struct sdebug_host_info **) hp->hostdata;
  1073. if (sdbg_host) {
  1074. list_for_each_entry(dev_info,
  1075. &sdbg_host->dev_info_list,
  1076. dev_list)
  1077. dev_info->reset = 1;
  1078. }
  1079. }
  1080. return SUCCESS;
  1081. }
  1082. static int scsi_debug_host_reset(struct scsi_cmnd * SCpnt)
  1083. {
  1084. struct sdebug_host_info * sdbg_host;
  1085. struct sdebug_dev_info * dev_info;
  1086. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1087. printk(KERN_INFO "scsi_debug: host_reset\n");
  1088. ++num_host_resets;
  1089. spin_lock(&sdebug_host_list_lock);
  1090. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  1091. list_for_each_entry(dev_info, &sdbg_host->dev_info_list,
  1092. dev_list)
  1093. dev_info->reset = 1;
  1094. }
  1095. spin_unlock(&sdebug_host_list_lock);
  1096. stop_all_queued();
  1097. return SUCCESS;
  1098. }
  1099. /* Returns 1 if found 'cmnd' and deleted its timer. else returns 0 */
  1100. static int stop_queued_cmnd(struct scsi_cmnd * cmnd)
  1101. {
  1102. unsigned long iflags;
  1103. int k;
  1104. struct sdebug_queued_cmd * sqcp;
  1105. spin_lock_irqsave(&queued_arr_lock, iflags);
  1106. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  1107. sqcp = &queued_arr[k];
  1108. if (sqcp->in_use && (cmnd == sqcp->a_cmnd)) {
  1109. del_timer_sync(&sqcp->cmnd_timer);
  1110. sqcp->in_use = 0;
  1111. sqcp->a_cmnd = NULL;
  1112. break;
  1113. }
  1114. }
  1115. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1116. return (k < SCSI_DEBUG_CANQUEUE) ? 1 : 0;
  1117. }
  1118. /* Deletes (stops) timers of all queued commands */
  1119. static void stop_all_queued(void)
  1120. {
  1121. unsigned long iflags;
  1122. int k;
  1123. struct sdebug_queued_cmd * sqcp;
  1124. spin_lock_irqsave(&queued_arr_lock, iflags);
  1125. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  1126. sqcp = &queued_arr[k];
  1127. if (sqcp->in_use && sqcp->a_cmnd) {
  1128. del_timer_sync(&sqcp->cmnd_timer);
  1129. sqcp->in_use = 0;
  1130. sqcp->a_cmnd = NULL;
  1131. }
  1132. }
  1133. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1134. }
  1135. /* Initializes timers in queued array */
  1136. static void __init init_all_queued(void)
  1137. {
  1138. unsigned long iflags;
  1139. int k;
  1140. struct sdebug_queued_cmd * sqcp;
  1141. spin_lock_irqsave(&queued_arr_lock, iflags);
  1142. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  1143. sqcp = &queued_arr[k];
  1144. init_timer(&sqcp->cmnd_timer);
  1145. sqcp->in_use = 0;
  1146. sqcp->a_cmnd = NULL;
  1147. }
  1148. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1149. }
  1150. static void __init sdebug_build_parts(unsigned char * ramp)
  1151. {
  1152. struct partition * pp;
  1153. int starts[SDEBUG_MAX_PARTS + 2];
  1154. int sectors_per_part, num_sectors, k;
  1155. int heads_by_sects, start_sec, end_sec;
  1156. /* assume partition table already zeroed */
  1157. if ((scsi_debug_num_parts < 1) || (sdebug_store_size < 1048576))
  1158. return;
  1159. if (scsi_debug_num_parts > SDEBUG_MAX_PARTS) {
  1160. scsi_debug_num_parts = SDEBUG_MAX_PARTS;
  1161. printk(KERN_WARNING "scsi_debug:build_parts: reducing "
  1162. "partitions to %d\n", SDEBUG_MAX_PARTS);
  1163. }
  1164. num_sectors = (int)(sdebug_store_size / SECT_SIZE);
  1165. sectors_per_part = (num_sectors - sdebug_sectors_per)
  1166. / scsi_debug_num_parts;
  1167. heads_by_sects = sdebug_heads * sdebug_sectors_per;
  1168. starts[0] = sdebug_sectors_per;
  1169. for (k = 1; k < scsi_debug_num_parts; ++k)
  1170. starts[k] = ((k * sectors_per_part) / heads_by_sects)
  1171. * heads_by_sects;
  1172. starts[scsi_debug_num_parts] = num_sectors;
  1173. starts[scsi_debug_num_parts + 1] = 0;
  1174. ramp[510] = 0x55; /* magic partition markings */
  1175. ramp[511] = 0xAA;
  1176. pp = (struct partition *)(ramp + 0x1be);
  1177. for (k = 0; starts[k + 1]; ++k, ++pp) {
  1178. start_sec = starts[k];
  1179. end_sec = starts[k + 1] - 1;
  1180. pp->boot_ind = 0;
  1181. pp->cyl = start_sec / heads_by_sects;
  1182. pp->head = (start_sec - (pp->cyl * heads_by_sects))
  1183. / sdebug_sectors_per;
  1184. pp->sector = (start_sec % sdebug_sectors_per) + 1;
  1185. pp->end_cyl = end_sec / heads_by_sects;
  1186. pp->end_head = (end_sec - (pp->end_cyl * heads_by_sects))
  1187. / sdebug_sectors_per;
  1188. pp->end_sector = (end_sec % sdebug_sectors_per) + 1;
  1189. pp->start_sect = start_sec;
  1190. pp->nr_sects = end_sec - start_sec + 1;
  1191. pp->sys_ind = 0x83; /* plain Linux partition */
  1192. }
  1193. }
  1194. static int schedule_resp(struct scsi_cmnd * cmnd,
  1195. struct sdebug_dev_info * devip,
  1196. done_funct_t done, int scsi_result, int delta_jiff)
  1197. {
  1198. if ((SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) && cmnd) {
  1199. if (scsi_result) {
  1200. struct scsi_device * sdp = cmnd->device;
  1201. sdev_printk(KERN_INFO, sdp,
  1202. "non-zero result=0x%x\n",
  1203. scsi_result);
  1204. }
  1205. }
  1206. if (cmnd && devip) {
  1207. /* simulate autosense by this driver */
  1208. if (SAM_STAT_CHECK_CONDITION == (scsi_result & 0xff))
  1209. memcpy(cmnd->sense_buffer, devip->sense_buff,
  1210. (SCSI_SENSE_BUFFERSIZE > SDEBUG_SENSE_LEN) ?
  1211. SDEBUG_SENSE_LEN : SCSI_SENSE_BUFFERSIZE);
  1212. }
  1213. if (delta_jiff <= 0) {
  1214. if (cmnd)
  1215. cmnd->result = scsi_result;
  1216. if (done)
  1217. done(cmnd);
  1218. return 0;
  1219. } else {
  1220. unsigned long iflags;
  1221. int k;
  1222. struct sdebug_queued_cmd * sqcp = NULL;
  1223. spin_lock_irqsave(&queued_arr_lock, iflags);
  1224. for (k = 0; k < SCSI_DEBUG_CANQUEUE; ++k) {
  1225. sqcp = &queued_arr[k];
  1226. if (! sqcp->in_use)
  1227. break;
  1228. }
  1229. if (k >= SCSI_DEBUG_CANQUEUE) {
  1230. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1231. printk(KERN_WARNING "scsi_debug: can_queue exceeded\n");
  1232. return 1; /* report busy to mid level */
  1233. }
  1234. sqcp->in_use = 1;
  1235. sqcp->a_cmnd = cmnd;
  1236. sqcp->scsi_result = scsi_result;
  1237. sqcp->done_funct = done;
  1238. sqcp->cmnd_timer.function = timer_intr_handler;
  1239. sqcp->cmnd_timer.data = k;
  1240. sqcp->cmnd_timer.expires = jiffies + delta_jiff;
  1241. add_timer(&sqcp->cmnd_timer);
  1242. spin_unlock_irqrestore(&queued_arr_lock, iflags);
  1243. if (cmnd)
  1244. cmnd->result = 0;
  1245. return 0;
  1246. }
  1247. }
  1248. /* Set 'perm' (4th argument) to 0 to disable module_param's definition
  1249. * of sysfs parameters (which module_param doesn't yet support).
  1250. * Sysfs parameters defined explicitly below.
  1251. */
  1252. module_param_named(add_host, scsi_debug_add_host, int, 0); /* perm=0644 */
  1253. module_param_named(delay, scsi_debug_delay, int, 0); /* perm=0644 */
  1254. module_param_named(dev_size_mb, scsi_debug_dev_size_mb, int, 0);
  1255. module_param_named(dsense, scsi_debug_dsense, int, 0);
  1256. module_param_named(every_nth, scsi_debug_every_nth, int, 0);
  1257. module_param_named(max_luns, scsi_debug_max_luns, int, 0);
  1258. module_param_named(num_parts, scsi_debug_num_parts, int, 0);
  1259. module_param_named(num_tgts, scsi_debug_num_tgts, int, 0);
  1260. module_param_named(opts, scsi_debug_opts, int, 0); /* perm=0644 */
  1261. module_param_named(ptype, scsi_debug_ptype, int, 0);
  1262. module_param_named(scsi_level, scsi_debug_scsi_level, int, 0);
  1263. MODULE_AUTHOR("Eric Youngdale + Douglas Gilbert");
  1264. MODULE_DESCRIPTION("SCSI debug adapter driver");
  1265. MODULE_LICENSE("GPL");
  1266. MODULE_VERSION(SCSI_DEBUG_VERSION);
  1267. MODULE_PARM_DESC(add_host, "0..127 hosts allowed(def=1)");
  1268. MODULE_PARM_DESC(delay, "# of jiffies to delay response(def=1)");
  1269. MODULE_PARM_DESC(dev_size_mb, "size in MB of ram shared by devs");
  1270. MODULE_PARM_DESC(dsense, "use descriptor sense format(def: fixed)");
  1271. MODULE_PARM_DESC(every_nth, "timeout every nth command(def=100)");
  1272. MODULE_PARM_DESC(max_luns, "number of SCSI LUNs per target to simulate");
  1273. MODULE_PARM_DESC(num_parts, "number of partitions(def=0)");
  1274. MODULE_PARM_DESC(num_tgts, "number of SCSI targets per host to simulate");
  1275. MODULE_PARM_DESC(opts, "1->noise, 2->medium_error, 4->...");
  1276. MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
  1277. MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=5[SPC-3])");
  1278. static char sdebug_info[256];
  1279. static const char * scsi_debug_info(struct Scsi_Host * shp)
  1280. {
  1281. sprintf(sdebug_info, "scsi_debug, version %s [%s], "
  1282. "dev_size_mb=%d, opts=0x%x", SCSI_DEBUG_VERSION,
  1283. scsi_debug_version_date, scsi_debug_dev_size_mb,
  1284. scsi_debug_opts);
  1285. return sdebug_info;
  1286. }
  1287. /* scsi_debug_proc_info
  1288. * Used if the driver currently has no own support for /proc/scsi
  1289. */
  1290. static int scsi_debug_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1291. int length, int inout)
  1292. {
  1293. int len, pos, begin;
  1294. int orig_length;
  1295. orig_length = length;
  1296. if (inout == 1) {
  1297. char arr[16];
  1298. int minLen = length > 15 ? 15 : length;
  1299. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  1300. return -EACCES;
  1301. memcpy(arr, buffer, minLen);
  1302. arr[minLen] = '\0';
  1303. if (1 != sscanf(arr, "%d", &pos))
  1304. return -EINVAL;
  1305. scsi_debug_opts = pos;
  1306. if (scsi_debug_every_nth != 0)
  1307. scsi_debug_cmnd_count = 0;
  1308. return length;
  1309. }
  1310. begin = 0;
  1311. pos = len = sprintf(buffer, "scsi_debug adapter driver, version "
  1312. "%s [%s]\n"
  1313. "num_tgts=%d, shared (ram) size=%d MB, opts=0x%x, "
  1314. "every_nth=%d(curr:%d)\n"
  1315. "delay=%d, max_luns=%d, scsi_level=%d\n"
  1316. "sector_size=%d bytes, cylinders=%d, heads=%d, sectors=%d\n"
  1317. "number of aborts=%d, device_reset=%d, bus_resets=%d, "
  1318. "host_resets=%d\n",
  1319. SCSI_DEBUG_VERSION, scsi_debug_version_date, scsi_debug_num_tgts,
  1320. scsi_debug_dev_size_mb, scsi_debug_opts, scsi_debug_every_nth,
  1321. scsi_debug_cmnd_count, scsi_debug_delay,
  1322. scsi_debug_max_luns, scsi_debug_scsi_level,
  1323. SECT_SIZE, sdebug_cylinders_per, sdebug_heads, sdebug_sectors_per,
  1324. num_aborts, num_dev_resets, num_bus_resets, num_host_resets);
  1325. if (pos < offset) {
  1326. len = 0;
  1327. begin = pos;
  1328. }
  1329. *start = buffer + (offset - begin); /* Start of wanted data */
  1330. len -= (offset - begin);
  1331. if (len > length)
  1332. len = length;
  1333. return len;
  1334. }
  1335. static ssize_t sdebug_delay_show(struct device_driver * ddp, char * buf)
  1336. {
  1337. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_delay);
  1338. }
  1339. static ssize_t sdebug_delay_store(struct device_driver * ddp,
  1340. const char * buf, size_t count)
  1341. {
  1342. int delay;
  1343. char work[20];
  1344. if (1 == sscanf(buf, "%10s", work)) {
  1345. if ((1 == sscanf(work, "%d", &delay)) && (delay >= 0)) {
  1346. scsi_debug_delay = delay;
  1347. return count;
  1348. }
  1349. }
  1350. return -EINVAL;
  1351. }
  1352. DRIVER_ATTR(delay, S_IRUGO | S_IWUSR, sdebug_delay_show,
  1353. sdebug_delay_store);
  1354. static ssize_t sdebug_opts_show(struct device_driver * ddp, char * buf)
  1355. {
  1356. return scnprintf(buf, PAGE_SIZE, "0x%x\n", scsi_debug_opts);
  1357. }
  1358. static ssize_t sdebug_opts_store(struct device_driver * ddp,
  1359. const char * buf, size_t count)
  1360. {
  1361. int opts;
  1362. char work[20];
  1363. if (1 == sscanf(buf, "%10s", work)) {
  1364. if (0 == strnicmp(work,"0x", 2)) {
  1365. if (1 == sscanf(&work[2], "%x", &opts))
  1366. goto opts_done;
  1367. } else {
  1368. if (1 == sscanf(work, "%d", &opts))
  1369. goto opts_done;
  1370. }
  1371. }
  1372. return -EINVAL;
  1373. opts_done:
  1374. scsi_debug_opts = opts;
  1375. scsi_debug_cmnd_count = 0;
  1376. return count;
  1377. }
  1378. DRIVER_ATTR(opts, S_IRUGO | S_IWUSR, sdebug_opts_show,
  1379. sdebug_opts_store);
  1380. static ssize_t sdebug_ptype_show(struct device_driver * ddp, char * buf)
  1381. {
  1382. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_ptype);
  1383. }
  1384. static ssize_t sdebug_ptype_store(struct device_driver * ddp,
  1385. const char * buf, size_t count)
  1386. {
  1387. int n;
  1388. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  1389. scsi_debug_ptype = n;
  1390. return count;
  1391. }
  1392. return -EINVAL;
  1393. }
  1394. DRIVER_ATTR(ptype, S_IRUGO | S_IWUSR, sdebug_ptype_show, sdebug_ptype_store);
  1395. static ssize_t sdebug_dsense_show(struct device_driver * ddp, char * buf)
  1396. {
  1397. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dsense);
  1398. }
  1399. static ssize_t sdebug_dsense_store(struct device_driver * ddp,
  1400. const char * buf, size_t count)
  1401. {
  1402. int n;
  1403. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  1404. scsi_debug_dsense = n;
  1405. return count;
  1406. }
  1407. return -EINVAL;
  1408. }
  1409. DRIVER_ATTR(dsense, S_IRUGO | S_IWUSR, sdebug_dsense_show,
  1410. sdebug_dsense_store);
  1411. static ssize_t sdebug_num_tgts_show(struct device_driver * ddp, char * buf)
  1412. {
  1413. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_tgts);
  1414. }
  1415. static ssize_t sdebug_num_tgts_store(struct device_driver * ddp,
  1416. const char * buf, size_t count)
  1417. {
  1418. int n;
  1419. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  1420. scsi_debug_num_tgts = n;
  1421. sdebug_max_tgts_luns();
  1422. return count;
  1423. }
  1424. return -EINVAL;
  1425. }
  1426. DRIVER_ATTR(num_tgts, S_IRUGO | S_IWUSR, sdebug_num_tgts_show,
  1427. sdebug_num_tgts_store);
  1428. static ssize_t sdebug_dev_size_mb_show(struct device_driver * ddp, char * buf)
  1429. {
  1430. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_dev_size_mb);
  1431. }
  1432. DRIVER_ATTR(dev_size_mb, S_IRUGO, sdebug_dev_size_mb_show, NULL);
  1433. static ssize_t sdebug_num_parts_show(struct device_driver * ddp, char * buf)
  1434. {
  1435. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_num_parts);
  1436. }
  1437. DRIVER_ATTR(num_parts, S_IRUGO, sdebug_num_parts_show, NULL);
  1438. static ssize_t sdebug_every_nth_show(struct device_driver * ddp, char * buf)
  1439. {
  1440. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_every_nth);
  1441. }
  1442. static ssize_t sdebug_every_nth_store(struct device_driver * ddp,
  1443. const char * buf, size_t count)
  1444. {
  1445. int nth;
  1446. if ((count > 0) && (1 == sscanf(buf, "%d", &nth))) {
  1447. scsi_debug_every_nth = nth;
  1448. scsi_debug_cmnd_count = 0;
  1449. return count;
  1450. }
  1451. return -EINVAL;
  1452. }
  1453. DRIVER_ATTR(every_nth, S_IRUGO | S_IWUSR, sdebug_every_nth_show,
  1454. sdebug_every_nth_store);
  1455. static ssize_t sdebug_max_luns_show(struct device_driver * ddp, char * buf)
  1456. {
  1457. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_max_luns);
  1458. }
  1459. static ssize_t sdebug_max_luns_store(struct device_driver * ddp,
  1460. const char * buf, size_t count)
  1461. {
  1462. int n;
  1463. if ((count > 0) && (1 == sscanf(buf, "%d", &n)) && (n >= 0)) {
  1464. scsi_debug_max_luns = n;
  1465. sdebug_max_tgts_luns();
  1466. return count;
  1467. }
  1468. return -EINVAL;
  1469. }
  1470. DRIVER_ATTR(max_luns, S_IRUGO | S_IWUSR, sdebug_max_luns_show,
  1471. sdebug_max_luns_store);
  1472. static ssize_t sdebug_scsi_level_show(struct device_driver * ddp, char * buf)
  1473. {
  1474. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_scsi_level);
  1475. }
  1476. DRIVER_ATTR(scsi_level, S_IRUGO, sdebug_scsi_level_show, NULL);
  1477. static ssize_t sdebug_add_host_show(struct device_driver * ddp, char * buf)
  1478. {
  1479. return scnprintf(buf, PAGE_SIZE, "%d\n", scsi_debug_add_host);
  1480. }
  1481. static ssize_t sdebug_add_host_store(struct device_driver * ddp,
  1482. const char * buf, size_t count)
  1483. {
  1484. int delta_hosts;
  1485. char work[20];
  1486. if (1 != sscanf(buf, "%10s", work))
  1487. return -EINVAL;
  1488. { /* temporary hack around sscanf() problem with -ve nums */
  1489. int neg = 0;
  1490. if ('-' == *work)
  1491. neg = 1;
  1492. if (1 != sscanf(work + neg, "%d", &delta_hosts))
  1493. return -EINVAL;
  1494. if (neg)
  1495. delta_hosts = -delta_hosts;
  1496. }
  1497. if (delta_hosts > 0) {
  1498. do {
  1499. sdebug_add_adapter();
  1500. } while (--delta_hosts);
  1501. } else if (delta_hosts < 0) {
  1502. do {
  1503. sdebug_remove_adapter();
  1504. } while (++delta_hosts);
  1505. }
  1506. return count;
  1507. }
  1508. DRIVER_ATTR(add_host, S_IRUGO | S_IWUSR, sdebug_add_host_show,
  1509. sdebug_add_host_store);
  1510. static void do_create_driverfs_files(void)
  1511. {
  1512. driver_create_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  1513. driver_create_file(&sdebug_driverfs_driver, &driver_attr_delay);
  1514. driver_create_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  1515. driver_create_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  1516. driver_create_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  1517. driver_create_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  1518. driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  1519. driver_create_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  1520. driver_create_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  1521. driver_create_file(&sdebug_driverfs_driver, &driver_attr_opts);
  1522. driver_create_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  1523. }
  1524. static void do_remove_driverfs_files(void)
  1525. {
  1526. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_scsi_level);
  1527. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_opts);
  1528. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_ptype);
  1529. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_parts);
  1530. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_num_tgts);
  1531. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_max_luns);
  1532. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_every_nth);
  1533. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dsense);
  1534. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_dev_size_mb);
  1535. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_delay);
  1536. driver_remove_file(&sdebug_driverfs_driver, &driver_attr_add_host);
  1537. }
  1538. static int __init scsi_debug_init(void)
  1539. {
  1540. unsigned long sz;
  1541. int host_to_add;
  1542. int k;
  1543. if (scsi_debug_dev_size_mb < 1)
  1544. scsi_debug_dev_size_mb = 1; /* force minimum 1 MB ramdisk */
  1545. sdebug_store_size = (unsigned long)scsi_debug_dev_size_mb * 1048576;
  1546. sdebug_capacity = sdebug_store_size / SECT_SIZE;
  1547. /* play around with geometry, don't waste too much on track 0 */
  1548. sdebug_heads = 8;
  1549. sdebug_sectors_per = 32;
  1550. if (scsi_debug_dev_size_mb >= 16)
  1551. sdebug_heads = 32;
  1552. else if (scsi_debug_dev_size_mb >= 256)
  1553. sdebug_heads = 64;
  1554. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  1555. (sdebug_sectors_per * sdebug_heads);
  1556. if (sdebug_cylinders_per >= 1024) {
  1557. /* other LLDs do this; implies >= 1GB ram disk ... */
  1558. sdebug_heads = 255;
  1559. sdebug_sectors_per = 63;
  1560. sdebug_cylinders_per = (unsigned long)sdebug_capacity /
  1561. (sdebug_sectors_per * sdebug_heads);
  1562. }
  1563. sz = sdebug_store_size;
  1564. fake_storep = vmalloc(sz);
  1565. if (NULL == fake_storep) {
  1566. printk(KERN_ERR "scsi_debug_init: out of memory, 1\n");
  1567. return -ENOMEM;
  1568. }
  1569. memset(fake_storep, 0, sz);
  1570. if (scsi_debug_num_parts > 0)
  1571. sdebug_build_parts(fake_storep);
  1572. init_all_queued();
  1573. device_register(&pseudo_primary);
  1574. bus_register(&pseudo_lld_bus);
  1575. driver_register(&sdebug_driverfs_driver);
  1576. do_create_driverfs_files();
  1577. sdebug_driver_template.proc_name = (char *)sdebug_proc_name;
  1578. host_to_add = scsi_debug_add_host;
  1579. scsi_debug_add_host = 0;
  1580. for (k = 0; k < host_to_add; k++) {
  1581. if (sdebug_add_adapter()) {
  1582. printk(KERN_ERR "scsi_debug_init: "
  1583. "sdebug_add_adapter failed k=%d\n", k);
  1584. break;
  1585. }
  1586. }
  1587. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts) {
  1588. printk(KERN_INFO "scsi_debug_init: built %d host(s)\n",
  1589. scsi_debug_add_host);
  1590. }
  1591. return 0;
  1592. }
  1593. static void __exit scsi_debug_exit(void)
  1594. {
  1595. int k = scsi_debug_add_host;
  1596. stop_all_queued();
  1597. for (; k; k--)
  1598. sdebug_remove_adapter();
  1599. do_remove_driverfs_files();
  1600. driver_unregister(&sdebug_driverfs_driver);
  1601. bus_unregister(&pseudo_lld_bus);
  1602. device_unregister(&pseudo_primary);
  1603. vfree(fake_storep);
  1604. }
  1605. device_initcall(scsi_debug_init);
  1606. module_exit(scsi_debug_exit);
  1607. static void pseudo_0_release(struct device * dev)
  1608. {
  1609. if (SCSI_DEBUG_OPT_NOISE & scsi_debug_opts)
  1610. printk(KERN_INFO "scsi_debug: pseudo_0_release() called\n");
  1611. }
  1612. static struct device pseudo_primary = {
  1613. .bus_id = "pseudo_0",
  1614. .release = pseudo_0_release,
  1615. };
  1616. static int pseudo_lld_bus_match(struct device *dev,
  1617. struct device_driver *dev_driver)
  1618. {
  1619. return 1;
  1620. }
  1621. static struct bus_type pseudo_lld_bus = {
  1622. .name = "pseudo",
  1623. .match = pseudo_lld_bus_match,
  1624. };
  1625. static void sdebug_release_adapter(struct device * dev)
  1626. {
  1627. struct sdebug_host_info *sdbg_host;
  1628. sdbg_host = to_sdebug_host(dev);
  1629. kfree(sdbg_host);
  1630. }
  1631. static int sdebug_add_adapter(void)
  1632. {
  1633. int k, devs_per_host;
  1634. int error = 0;
  1635. struct sdebug_host_info *sdbg_host;
  1636. struct sdebug_dev_info *sdbg_devinfo;
  1637. struct list_head *lh, *lh_sf;
  1638. sdbg_host = kmalloc(sizeof(*sdbg_host),GFP_KERNEL);
  1639. if (NULL == sdbg_host) {
  1640. printk(KERN_ERR "%s: out of memory at line %d\n",
  1641. __FUNCTION__, __LINE__);
  1642. return -ENOMEM;
  1643. }
  1644. memset(sdbg_host, 0, sizeof(*sdbg_host));
  1645. INIT_LIST_HEAD(&sdbg_host->dev_info_list);
  1646. devs_per_host = scsi_debug_num_tgts * scsi_debug_max_luns;
  1647. for (k = 0; k < devs_per_host; k++) {
  1648. sdbg_devinfo = kmalloc(sizeof(*sdbg_devinfo),GFP_KERNEL);
  1649. if (NULL == sdbg_devinfo) {
  1650. printk(KERN_ERR "%s: out of memory at line %d\n",
  1651. __FUNCTION__, __LINE__);
  1652. error = -ENOMEM;
  1653. goto clean;
  1654. }
  1655. memset(sdbg_devinfo, 0, sizeof(*sdbg_devinfo));
  1656. sdbg_devinfo->sdbg_host = sdbg_host;
  1657. list_add_tail(&sdbg_devinfo->dev_list,
  1658. &sdbg_host->dev_info_list);
  1659. }
  1660. spin_lock(&sdebug_host_list_lock);
  1661. list_add_tail(&sdbg_host->host_list, &sdebug_host_list);
  1662. spin_unlock(&sdebug_host_list_lock);
  1663. sdbg_host->dev.bus = &pseudo_lld_bus;
  1664. sdbg_host->dev.parent = &pseudo_primary;
  1665. sdbg_host->dev.release = &sdebug_release_adapter;
  1666. sprintf(sdbg_host->dev.bus_id, "adapter%d", scsi_debug_add_host);
  1667. error = device_register(&sdbg_host->dev);
  1668. if (error)
  1669. goto clean;
  1670. ++scsi_debug_add_host;
  1671. return error;
  1672. clean:
  1673. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  1674. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  1675. dev_list);
  1676. list_del(&sdbg_devinfo->dev_list);
  1677. kfree(sdbg_devinfo);
  1678. }
  1679. kfree(sdbg_host);
  1680. return error;
  1681. }
  1682. static void sdebug_remove_adapter(void)
  1683. {
  1684. struct sdebug_host_info * sdbg_host = NULL;
  1685. spin_lock(&sdebug_host_list_lock);
  1686. if (!list_empty(&sdebug_host_list)) {
  1687. sdbg_host = list_entry(sdebug_host_list.prev,
  1688. struct sdebug_host_info, host_list);
  1689. list_del(&sdbg_host->host_list);
  1690. }
  1691. spin_unlock(&sdebug_host_list_lock);
  1692. if (!sdbg_host)
  1693. return;
  1694. device_unregister(&sdbg_host->dev);
  1695. --scsi_debug_add_host;
  1696. }
  1697. static int sdebug_driver_probe(struct device * dev)
  1698. {
  1699. int error = 0;
  1700. struct sdebug_host_info *sdbg_host;
  1701. struct Scsi_Host *hpnt;
  1702. sdbg_host = to_sdebug_host(dev);
  1703. hpnt = scsi_host_alloc(&sdebug_driver_template, sizeof(sdbg_host));
  1704. if (NULL == hpnt) {
  1705. printk(KERN_ERR "%s: scsi_register failed\n", __FUNCTION__);
  1706. error = -ENODEV;
  1707. return error;
  1708. }
  1709. sdbg_host->shost = hpnt;
  1710. *((struct sdebug_host_info **)hpnt->hostdata) = sdbg_host;
  1711. if ((hpnt->this_id >= 0) && (scsi_debug_num_tgts > hpnt->this_id))
  1712. hpnt->max_id = scsi_debug_num_tgts + 1;
  1713. else
  1714. hpnt->max_id = scsi_debug_num_tgts;
  1715. hpnt->max_lun = scsi_debug_max_luns;
  1716. error = scsi_add_host(hpnt, &sdbg_host->dev);
  1717. if (error) {
  1718. printk(KERN_ERR "%s: scsi_add_host failed\n", __FUNCTION__);
  1719. error = -ENODEV;
  1720. scsi_host_put(hpnt);
  1721. } else
  1722. scsi_scan_host(hpnt);
  1723. return error;
  1724. }
  1725. static int sdebug_driver_remove(struct device * dev)
  1726. {
  1727. struct list_head *lh, *lh_sf;
  1728. struct sdebug_host_info *sdbg_host;
  1729. struct sdebug_dev_info *sdbg_devinfo;
  1730. sdbg_host = to_sdebug_host(dev);
  1731. if (!sdbg_host) {
  1732. printk(KERN_ERR "%s: Unable to locate host info\n",
  1733. __FUNCTION__);
  1734. return -ENODEV;
  1735. }
  1736. scsi_remove_host(sdbg_host->shost);
  1737. list_for_each_safe(lh, lh_sf, &sdbg_host->dev_info_list) {
  1738. sdbg_devinfo = list_entry(lh, struct sdebug_dev_info,
  1739. dev_list);
  1740. list_del(&sdbg_devinfo->dev_list);
  1741. kfree(sdbg_devinfo);
  1742. }
  1743. scsi_host_put(sdbg_host->shost);
  1744. return 0;
  1745. }
  1746. static void sdebug_max_tgts_luns(void)
  1747. {
  1748. struct sdebug_host_info * sdbg_host;
  1749. struct Scsi_Host *hpnt;
  1750. spin_lock(&sdebug_host_list_lock);
  1751. list_for_each_entry(sdbg_host, &sdebug_host_list, host_list) {
  1752. hpnt = sdbg_host->shost;
  1753. if ((hpnt->this_id >= 0) &&
  1754. (scsi_debug_num_tgts > hpnt->this_id))
  1755. hpnt->max_id = scsi_debug_num_tgts + 1;
  1756. else
  1757. hpnt->max_id = scsi_debug_num_tgts;
  1758. hpnt->max_lun = scsi_debug_max_luns;
  1759. }
  1760. spin_unlock(&sdebug_host_list_lock);
  1761. }