simscsi.c 8.6 KB

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