scsi_debug.c 59 KB

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