simscsi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Simulated SCSI driver.
  3. *
  4. * Copyright (C) 1999, 2001-2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. * Stephane Eranian <eranian@hpl.hp.com>
  7. *
  8. * 02/01/15 David Mosberger Updated for v2.5.1
  9. * 99/12/18 David Mosberger Added support for READ10/WRITE10 needed by linux v2.3.33
  10. */
  11. #include <linux/blkdev.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/timer.h>
  16. #include <asm/irq.h>
  17. #include <scsi/scsi.h>
  18. #include <scsi/scsi_cmnd.h>
  19. #include <scsi/scsi_device.h>
  20. #include <scsi/scsi_host.h>
  21. #define DEBUG_SIMSCSI 0
  22. #define SIMSCSI_REQ_QUEUE_LEN 64
  23. #define DEFAULT_SIMSCSI_ROOT "/var/ski-disks/sd"
  24. /* Simulator system calls: */
  25. #define SSC_OPEN 50
  26. #define SSC_CLOSE 51
  27. #define SSC_READ 52
  28. #define SSC_WRITE 53
  29. #define SSC_GET_COMPLETION 54
  30. #define SSC_WAIT_COMPLETION 55
  31. #define SSC_WRITE_ACCESS 2
  32. #define SSC_READ_ACCESS 1
  33. #if DEBUG_SIMSCSI
  34. int simscsi_debug;
  35. # define DBG simscsi_debug
  36. #else
  37. # define DBG 0
  38. #endif
  39. static struct Scsi_Host *host;
  40. static void simscsi_interrupt (unsigned long val);
  41. static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
  42. struct disk_req {
  43. unsigned long addr;
  44. unsigned len;
  45. };
  46. struct disk_stat {
  47. int fd;
  48. unsigned count;
  49. };
  50. extern long ia64_ssc (long arg0, long arg1, long arg2, long arg3, int nr);
  51. static int desc[16] = {
  52. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  53. };
  54. static struct queue_entry {
  55. struct scsi_cmnd *sc;
  56. } queue[SIMSCSI_REQ_QUEUE_LEN];
  57. static int rd, wr;
  58. static atomic_t num_reqs = ATOMIC_INIT(0);
  59. /* base name for default disks */
  60. static char *simscsi_root = DEFAULT_SIMSCSI_ROOT;
  61. #define MAX_ROOT_LEN 128
  62. /*
  63. * used to setup a new base for disk images
  64. * to use /foo/bar/disk[a-z] as disk images
  65. * you have to specify simscsi=/foo/bar/disk on the command line
  66. */
  67. static int __init
  68. simscsi_setup (char *s)
  69. {
  70. /* XXX Fix me we may need to strcpy() ? */
  71. if (strlen(s) > MAX_ROOT_LEN) {
  72. printk(KERN_ERR "simscsi_setup: prefix too long---using default %s\n",
  73. simscsi_root);
  74. }
  75. simscsi_root = s;
  76. return 1;
  77. }
  78. __setup("simscsi=", simscsi_setup);
  79. static void
  80. simscsi_interrupt (unsigned long val)
  81. {
  82. struct scsi_cmnd *sc;
  83. while ((sc = queue[rd].sc) != 0) {
  84. atomic_dec(&num_reqs);
  85. queue[rd].sc = 0;
  86. if (DBG)
  87. printk("simscsi_interrupt: done with %ld\n", sc->serial_number);
  88. (*sc->scsi_done)(sc);
  89. rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN;
  90. }
  91. }
  92. static int
  93. simscsi_biosparam (struct scsi_device *sdev, struct block_device *n,
  94. sector_t capacity, int ip[])
  95. {
  96. ip[0] = 64; /* heads */
  97. ip[1] = 32; /* sectors */
  98. ip[2] = capacity >> 11; /* cylinders */
  99. return 0;
  100. }
  101. static void
  102. simscsi_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset, unsigned long len)
  103. {
  104. struct disk_stat stat;
  105. struct disk_req req;
  106. req.addr = __pa(sc->request_buffer);
  107. req.len = len; /* # of bytes to transfer */
  108. if (sc->request_bufflen < req.len)
  109. return;
  110. stat.fd = desc[sc->device->id];
  111. if (DBG)
  112. printk("simscsi_%s @ %lx (off %lx)\n",
  113. mode == SSC_READ ? "read":"write", req.addr, offset);
  114. ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
  115. ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
  116. if (stat.count == req.len) {
  117. sc->result = GOOD;
  118. } else {
  119. sc->result = DID_ERROR << 16;
  120. }
  121. }
  122. static void
  123. simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset)
  124. {
  125. int list_len = sc->use_sg;
  126. struct scatterlist *sl = (struct scatterlist *)sc->buffer;
  127. struct disk_stat stat;
  128. struct disk_req req;
  129. stat.fd = desc[sc->device->id];
  130. while (list_len) {
  131. req.addr = __pa(page_address(sl->page) + sl->offset);
  132. req.len = sl->length;
  133. if (DBG)
  134. printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
  135. mode == SSC_READ ? "read":"write", req.addr, offset,
  136. list_len, sl->length);
  137. ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
  138. ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
  139. /* should not happen in our case */
  140. if (stat.count != req.len) {
  141. sc->result = DID_ERROR << 16;
  142. return;
  143. }
  144. offset += sl->length;
  145. sl++;
  146. list_len--;
  147. }
  148. sc->result = GOOD;
  149. }
  150. /*
  151. * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
  152. * commands.
  153. * Added 02/26/99 S.Eranian
  154. */
  155. static void
  156. simscsi_readwrite6 (struct scsi_cmnd *sc, int mode)
  157. {
  158. unsigned long offset;
  159. offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
  160. if (sc->use_sg > 0)
  161. simscsi_sg_readwrite(sc, mode, offset);
  162. else
  163. simscsi_readwrite(sc, mode, offset, sc->cmnd[4]*512);
  164. }
  165. static size_t
  166. simscsi_get_disk_size (int fd)
  167. {
  168. struct disk_stat stat;
  169. size_t bit, sectors = 0;
  170. struct disk_req req;
  171. char buf[512];
  172. /*
  173. * This is a bit kludgey: the simulator doesn't provide a direct way of determining
  174. * the disk size, so we do a binary search, assuming a maximum disk size of 4GB.
  175. */
  176. for (bit = (4UL << 30)/512; bit != 0; bit >>= 1) {
  177. req.addr = __pa(&buf);
  178. req.len = sizeof(buf);
  179. ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
  180. stat.fd = fd;
  181. ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
  182. if (stat.count == sizeof(buf))
  183. sectors |= bit;
  184. }
  185. return sectors - 1; /* return last valid sector number */
  186. }
  187. static void
  188. simscsi_readwrite10 (struct scsi_cmnd *sc, int mode)
  189. {
  190. unsigned long offset;
  191. offset = ( (sc->cmnd[2] << 24) | (sc->cmnd[3] << 16)
  192. | (sc->cmnd[4] << 8) | (sc->cmnd[5] << 0))*512;
  193. if (sc->use_sg > 0)
  194. simscsi_sg_readwrite(sc, mode, offset);
  195. else
  196. simscsi_readwrite(sc, mode, offset, ((sc->cmnd[7] << 8) | sc->cmnd[8])*512);
  197. }
  198. static void simscsi_fillresult(struct scsi_cmnd *sc, char *buf, unsigned len)
  199. {
  200. int scatterlen = sc->use_sg;
  201. struct scatterlist *slp;
  202. if (scatterlen == 0)
  203. memcpy(sc->request_buffer, buf, len);
  204. else for (slp = (struct scatterlist *)sc->buffer; scatterlen-- > 0 && len > 0; slp++) {
  205. unsigned thislen = min(len, slp->length);
  206. memcpy(page_address(slp->page) + slp->offset, buf, thislen);
  207. slp++;
  208. len -= thislen;
  209. }
  210. }
  211. static int
  212. simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  213. {
  214. unsigned int target_id = sc->device->id;
  215. char fname[MAX_ROOT_LEN+16];
  216. size_t disk_size;
  217. char *buf;
  218. char localbuf[36];
  219. #if DEBUG_SIMSCSI
  220. register long sp asm ("sp");
  221. if (DBG)
  222. printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
  223. target_id, sc->cmnd[0], sc->serial_number, sp, done);
  224. #endif
  225. sc->result = DID_BAD_TARGET << 16;
  226. sc->scsi_done = done;
  227. if (target_id <= 15 && sc->device->lun == 0) {
  228. switch (sc->cmnd[0]) {
  229. case INQUIRY:
  230. if (sc->request_bufflen < 35) {
  231. break;
  232. }
  233. sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
  234. desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
  235. 0, 0, SSC_OPEN);
  236. if (desc[target_id] < 0) {
  237. /* disk doesn't exist... */
  238. break;
  239. }
  240. buf = localbuf;
  241. buf[0] = 0; /* magnetic disk */
  242. buf[1] = 0; /* not a removable medium */
  243. buf[2] = 2; /* SCSI-2 compliant device */
  244. buf[3] = 2; /* SCSI-2 response data format */
  245. buf[4] = 31; /* additional length (bytes) */
  246. buf[5] = 0; /* reserved */
  247. buf[6] = 0; /* reserved */
  248. buf[7] = 0; /* various flags */
  249. memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28);
  250. simscsi_fillresult(sc, buf, 36);
  251. sc->result = GOOD;
  252. break;
  253. case TEST_UNIT_READY:
  254. sc->result = GOOD;
  255. break;
  256. case READ_6:
  257. if (desc[target_id] < 0 )
  258. break;
  259. simscsi_readwrite6(sc, SSC_READ);
  260. break;
  261. case READ_10:
  262. if (desc[target_id] < 0 )
  263. break;
  264. simscsi_readwrite10(sc, SSC_READ);
  265. break;
  266. case WRITE_6:
  267. if (desc[target_id] < 0)
  268. break;
  269. simscsi_readwrite6(sc, SSC_WRITE);
  270. break;
  271. case WRITE_10:
  272. if (desc[target_id] < 0)
  273. break;
  274. simscsi_readwrite10(sc, SSC_WRITE);
  275. break;
  276. case READ_CAPACITY:
  277. if (desc[target_id] < 0 || sc->request_bufflen < 8) {
  278. break;
  279. }
  280. buf = localbuf;
  281. disk_size = simscsi_get_disk_size(desc[target_id]);
  282. buf[0] = (disk_size >> 24) & 0xff;
  283. buf[1] = (disk_size >> 16) & 0xff;
  284. buf[2] = (disk_size >> 8) & 0xff;
  285. buf[3] = (disk_size >> 0) & 0xff;
  286. /* set block size of 512 bytes: */
  287. buf[4] = 0;
  288. buf[5] = 0;
  289. buf[6] = 2;
  290. buf[7] = 0;
  291. simscsi_fillresult(sc, buf, 8);
  292. sc->result = GOOD;
  293. break;
  294. case MODE_SENSE:
  295. case MODE_SENSE_10:
  296. /* sd.c uses this to determine whether disk does write-caching. */
  297. simscsi_fillresult(sc, (char *)empty_zero_page, sc->request_bufflen);
  298. sc->result = GOOD;
  299. break;
  300. case START_STOP:
  301. printk(KERN_ERR "START_STOP\n");
  302. break;
  303. default:
  304. panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
  305. }
  306. }
  307. if (sc->result == DID_BAD_TARGET) {
  308. sc->result |= DRIVER_SENSE << 24;
  309. sc->sense_buffer[0] = 0x70;
  310. sc->sense_buffer[2] = 0x00;
  311. }
  312. if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
  313. panic("Attempt to queue command while command is pending!!");
  314. }
  315. atomic_inc(&num_reqs);
  316. queue[wr].sc = sc;
  317. wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
  318. tasklet_schedule(&simscsi_tasklet);
  319. return 0;
  320. }
  321. static int
  322. simscsi_host_reset (struct scsi_cmnd *sc)
  323. {
  324. printk(KERN_ERR "simscsi_host_reset: not implemented\n");
  325. return 0;
  326. }
  327. static struct scsi_host_template driver_template = {
  328. .name = "simulated SCSI host adapter",
  329. .proc_name = "simscsi",
  330. .queuecommand = simscsi_queuecommand,
  331. .eh_host_reset_handler = simscsi_host_reset,
  332. .bios_param = simscsi_biosparam,
  333. .can_queue = SIMSCSI_REQ_QUEUE_LEN,
  334. .this_id = -1,
  335. .sg_tablesize = SG_ALL,
  336. .max_sectors = 1024,
  337. .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
  338. .use_clustering = DISABLE_CLUSTERING,
  339. };
  340. static int __init
  341. simscsi_init(void)
  342. {
  343. int error;
  344. host = scsi_host_alloc(&driver_template, 0);
  345. if (!host)
  346. return -ENOMEM;
  347. error = scsi_add_host(host, NULL);
  348. if (!error)
  349. scsi_scan_host(host);
  350. return error;
  351. }
  352. static void __exit
  353. simscsi_exit(void)
  354. {
  355. scsi_remove_host(host);
  356. scsi_host_put(host);
  357. }
  358. module_init(simscsi_init);
  359. module_exit(simscsi_exit);