simscsi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 "hpsim_ssc.h"
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_host.h>
  22. #define DEBUG_SIMSCSI 0
  23. #define SIMSCSI_REQ_QUEUE_LEN 64
  24. #define DEFAULT_SIMSCSI_ROOT "/var/ski-disks/sd"
  25. /* Simulator system calls: */
  26. #define SSC_OPEN 50
  27. #define SSC_CLOSE 51
  28. #define SSC_READ 52
  29. #define SSC_WRITE 53
  30. #define SSC_GET_COMPLETION 54
  31. #define SSC_WAIT_COMPLETION 55
  32. #define SSC_WRITE_ACCESS 2
  33. #define SSC_READ_ACCESS 1
  34. #if DEBUG_SIMSCSI
  35. int simscsi_debug;
  36. # define DBG simscsi_debug
  37. #else
  38. # define DBG 0
  39. #endif
  40. static struct Scsi_Host *host;
  41. static void simscsi_interrupt (unsigned long val);
  42. static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
  43. struct disk_req {
  44. unsigned long addr;
  45. unsigned len;
  46. };
  47. struct disk_stat {
  48. int fd;
  49. unsigned count;
  50. };
  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. } else
  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) != NULL) {
  84. atomic_dec(&num_reqs);
  85. queue[rd].sc = NULL;
  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_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset)
  103. {
  104. int i;
  105. struct scatterlist *sl;
  106. struct disk_stat stat;
  107. struct disk_req req;
  108. stat.fd = desc[sc->device->id];
  109. scsi_for_each_sg(sc, sl, scsi_sg_count(sc), i) {
  110. req.addr = __pa(sg_virt(sl));
  111. req.len = sl->length;
  112. if (DBG)
  113. printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
  114. mode == SSC_READ ? "read":"write", req.addr, offset,
  115. scsi_sg_count(sc) - i, sl->length);
  116. ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
  117. ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
  118. /* should not happen in our case */
  119. if (stat.count != req.len) {
  120. sc->result = DID_ERROR << 16;
  121. return;
  122. }
  123. offset += sl->length;
  124. }
  125. sc->result = GOOD;
  126. }
  127. /*
  128. * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
  129. * commands.
  130. * Added 02/26/99 S.Eranian
  131. */
  132. static void
  133. simscsi_readwrite6 (struct scsi_cmnd *sc, int mode)
  134. {
  135. unsigned long offset;
  136. offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
  137. simscsi_sg_readwrite(sc, mode, offset);
  138. }
  139. static size_t
  140. simscsi_get_disk_size (int fd)
  141. {
  142. struct disk_stat stat;
  143. size_t bit, sectors = 0;
  144. struct disk_req req;
  145. char buf[512];
  146. /*
  147. * This is a bit kludgey: the simulator doesn't provide a
  148. * direct way of determining the disk size, so we do a binary
  149. * search, assuming a maximum disk size of 128GB.
  150. */
  151. for (bit = (128UL << 30)/512; bit != 0; bit >>= 1) {
  152. req.addr = __pa(&buf);
  153. req.len = sizeof(buf);
  154. ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
  155. stat.fd = fd;
  156. ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
  157. if (stat.count == sizeof(buf))
  158. sectors |= bit;
  159. }
  160. return sectors - 1; /* return last valid sector number */
  161. }
  162. static void
  163. simscsi_readwrite10 (struct scsi_cmnd *sc, int mode)
  164. {
  165. unsigned long offset;
  166. offset = (((unsigned long)sc->cmnd[2] << 24)
  167. | ((unsigned long)sc->cmnd[3] << 16)
  168. | ((unsigned long)sc->cmnd[4] << 8)
  169. | ((unsigned long)sc->cmnd[5] << 0))*512UL;
  170. simscsi_sg_readwrite(sc, mode, offset);
  171. }
  172. static int
  173. simscsi_queuecommand_lck (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  174. {
  175. unsigned int target_id = sc->device->id;
  176. char fname[MAX_ROOT_LEN+16];
  177. size_t disk_size;
  178. char *buf;
  179. char localbuf[36];
  180. #if DEBUG_SIMSCSI
  181. register long sp asm ("sp");
  182. if (DBG)
  183. printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
  184. target_id, sc->cmnd[0], sc->serial_number, sp, done);
  185. #endif
  186. sc->result = DID_BAD_TARGET << 16;
  187. sc->scsi_done = done;
  188. if (target_id <= 15 && sc->device->lun == 0) {
  189. switch (sc->cmnd[0]) {
  190. case INQUIRY:
  191. if (scsi_bufflen(sc) < 35) {
  192. break;
  193. }
  194. sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
  195. desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
  196. 0, 0, SSC_OPEN);
  197. if (desc[target_id] < 0) {
  198. /* disk doesn't exist... */
  199. break;
  200. }
  201. buf = localbuf;
  202. buf[0] = 0; /* magnetic disk */
  203. buf[1] = 0; /* not a removable medium */
  204. buf[2] = 2; /* SCSI-2 compliant device */
  205. buf[3] = 2; /* SCSI-2 response data format */
  206. buf[4] = 31; /* additional length (bytes) */
  207. buf[5] = 0; /* reserved */
  208. buf[6] = 0; /* reserved */
  209. buf[7] = 0; /* various flags */
  210. memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28);
  211. scsi_sg_copy_from_buffer(sc, buf, 36);
  212. sc->result = GOOD;
  213. break;
  214. case TEST_UNIT_READY:
  215. sc->result = GOOD;
  216. break;
  217. case READ_6:
  218. if (desc[target_id] < 0 )
  219. break;
  220. simscsi_readwrite6(sc, SSC_READ);
  221. break;
  222. case READ_10:
  223. if (desc[target_id] < 0 )
  224. break;
  225. simscsi_readwrite10(sc, SSC_READ);
  226. break;
  227. case WRITE_6:
  228. if (desc[target_id] < 0)
  229. break;
  230. simscsi_readwrite6(sc, SSC_WRITE);
  231. break;
  232. case WRITE_10:
  233. if (desc[target_id] < 0)
  234. break;
  235. simscsi_readwrite10(sc, SSC_WRITE);
  236. break;
  237. case READ_CAPACITY:
  238. if (desc[target_id] < 0 || scsi_bufflen(sc) < 8) {
  239. break;
  240. }
  241. buf = localbuf;
  242. disk_size = simscsi_get_disk_size(desc[target_id]);
  243. buf[0] = (disk_size >> 24) & 0xff;
  244. buf[1] = (disk_size >> 16) & 0xff;
  245. buf[2] = (disk_size >> 8) & 0xff;
  246. buf[3] = (disk_size >> 0) & 0xff;
  247. /* set block size of 512 bytes: */
  248. buf[4] = 0;
  249. buf[5] = 0;
  250. buf[6] = 2;
  251. buf[7] = 0;
  252. scsi_sg_copy_from_buffer(sc, buf, 8);
  253. sc->result = GOOD;
  254. break;
  255. case MODE_SENSE:
  256. case MODE_SENSE_10:
  257. /* sd.c uses this to determine whether disk does write-caching. */
  258. scsi_sg_copy_from_buffer(sc, (char *)empty_zero_page,
  259. PAGE_SIZE);
  260. sc->result = GOOD;
  261. break;
  262. case START_STOP:
  263. printk(KERN_ERR "START_STOP\n");
  264. break;
  265. default:
  266. panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
  267. }
  268. }
  269. if (sc->result == DID_BAD_TARGET) {
  270. sc->result |= DRIVER_SENSE << 24;
  271. sc->sense_buffer[0] = 0x70;
  272. sc->sense_buffer[2] = 0x00;
  273. }
  274. if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
  275. panic("Attempt to queue command while command is pending!!");
  276. }
  277. atomic_inc(&num_reqs);
  278. queue[wr].sc = sc;
  279. wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
  280. tasklet_schedule(&simscsi_tasklet);
  281. return 0;
  282. }
  283. static DEF_SCSI_QCMD(simscsi_queuecommand)
  284. static int
  285. simscsi_host_reset (struct scsi_cmnd *sc)
  286. {
  287. printk(KERN_ERR "simscsi_host_reset: not implemented\n");
  288. return 0;
  289. }
  290. static struct scsi_host_template driver_template = {
  291. .name = "simulated SCSI host adapter",
  292. .proc_name = "simscsi",
  293. .queuecommand = simscsi_queuecommand,
  294. .eh_host_reset_handler = simscsi_host_reset,
  295. .bios_param = simscsi_biosparam,
  296. .can_queue = SIMSCSI_REQ_QUEUE_LEN,
  297. .this_id = -1,
  298. .sg_tablesize = SG_ALL,
  299. .max_sectors = 1024,
  300. .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
  301. .use_clustering = DISABLE_CLUSTERING,
  302. };
  303. static int __init
  304. simscsi_init(void)
  305. {
  306. int error;
  307. host = scsi_host_alloc(&driver_template, 0);
  308. if (!host)
  309. return -ENOMEM;
  310. error = scsi_add_host(host, NULL);
  311. if (error)
  312. goto free_host;
  313. scsi_scan_host(host);
  314. return 0;
  315. free_host:
  316. scsi_host_put(host);
  317. return error;
  318. }
  319. device_initcall(simscsi_init);