2 * Simulated SCSI driver.
4 * Copyright (C) 1999, 2001-2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
6 * Stephane Eranian <eranian@hpl.hp.com>
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
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>
17 #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>
24 #define DEBUG_SIMSCSI 0
26 #define SIMSCSI_REQ_QUEUE_LEN 64
27 #define DEFAULT_SIMSCSI_ROOT "/var/ski-disks/sd"
29 /* Simulator system calls: */
35 #define SSC_GET_COMPLETION 54
36 #define SSC_WAIT_COMPLETION 55
38 #define SSC_WRITE_ACCESS 2
39 #define SSC_READ_ACCESS 1
43 # define DBG simscsi_debug
48 static struct Scsi_Host *host;
50 static void simscsi_interrupt (unsigned long val);
51 static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
63 static int desc[16] = {
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
67 static struct queue_entry {
69 } queue[SIMSCSI_REQ_QUEUE_LEN];
72 static atomic_t num_reqs = ATOMIC_INIT(0);
74 /* base name for default disks */
75 static char *simscsi_root = DEFAULT_SIMSCSI_ROOT;
77 #define MAX_ROOT_LEN 128
80 * used to setup a new base for disk images
81 * to use /foo/bar/disk[a-z] as disk images
82 * you have to specify simscsi=/foo/bar/disk on the command line
85 simscsi_setup (char *s)
87 /* XXX Fix me we may need to strcpy() ? */
88 if (strlen(s) > MAX_ROOT_LEN) {
89 printk(KERN_ERR "simscsi_setup: prefix too long---using default %s\n",
96 __setup("simscsi=", simscsi_setup);
99 simscsi_interrupt (unsigned long val)
101 struct scsi_cmnd *sc;
103 while ((sc = queue[rd].sc) != NULL) {
104 atomic_dec(&num_reqs);
107 printk("simscsi_interrupt: done with %ld\n", sc->serial_number);
108 (*sc->scsi_done)(sc);
109 rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN;
114 simscsi_biosparam (struct scsi_device *sdev, struct block_device *n,
115 sector_t capacity, int ip[])
117 ip[0] = 64; /* heads */
118 ip[1] = 32; /* sectors */
119 ip[2] = capacity >> 11; /* cylinders */
124 simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset)
127 struct scatterlist *sl;
128 struct disk_stat stat;
131 stat.fd = desc[sc->device->id];
133 scsi_for_each_sg(sc, sl, scsi_sg_count(sc), i) {
134 req.addr = __pa(sg_virt(sl));
135 req.len = sl->length;
137 printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
138 mode == SSC_READ ? "read":"write", req.addr, offset,
139 scsi_sg_count(sc) - i, sl->length);
140 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
141 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
143 /* should not happen in our case */
144 if (stat.count != req.len) {
145 sc->result = DID_ERROR << 16;
148 offset += sl->length;
154 * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
156 * Added 02/26/99 S.Eranian
159 simscsi_readwrite6 (struct scsi_cmnd *sc, int mode)
161 unsigned long offset;
163 offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
164 simscsi_sg_readwrite(sc, mode, offset);
168 simscsi_get_disk_size (int fd)
170 struct disk_stat stat;
171 size_t bit, sectors = 0;
176 * This is a bit kludgey: the simulator doesn't provide a
177 * direct way of determining the disk size, so we do a binary
178 * search, assuming a maximum disk size of 128GB.
180 for (bit = (128UL << 30)/512; bit != 0; bit >>= 1) {
181 req.addr = __pa(&buf);
182 req.len = sizeof(buf);
183 ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
185 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
186 if (stat.count == sizeof(buf))
189 return sectors - 1; /* return last valid sector number */
193 simscsi_readwrite10 (struct scsi_cmnd *sc, int mode)
195 unsigned long offset;
197 offset = (((unsigned long)sc->cmnd[2] << 24)
198 | ((unsigned long)sc->cmnd[3] << 16)
199 | ((unsigned long)sc->cmnd[4] << 8)
200 | ((unsigned long)sc->cmnd[5] << 0))*512UL;
201 simscsi_sg_readwrite(sc, mode, offset);
205 simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
207 unsigned int target_id = sc->device->id;
208 char fname[MAX_ROOT_LEN+16];
213 register long sp asm ("sp");
216 printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
217 target_id, sc->cmnd[0], sc->serial_number, sp, done);
220 sc->result = DID_BAD_TARGET << 16;
221 sc->scsi_done = done;
222 if (target_id <= 15 && sc->device->lun == 0) {
223 switch (sc->cmnd[0]) {
225 if (scsi_bufflen(sc) < 35) {
228 sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
229 desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
231 if (desc[target_id] < 0) {
232 /* disk doesn't exist... */
236 buf[0] = 0; /* magnetic disk */
237 buf[1] = 0; /* not a removable medium */
238 buf[2] = 2; /* SCSI-2 compliant device */
239 buf[3] = 2; /* SCSI-2 response data format */
240 buf[4] = 31; /* additional length (bytes) */
241 buf[5] = 0; /* reserved */
242 buf[6] = 0; /* reserved */
243 buf[7] = 0; /* various flags */
244 memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28);
245 scsi_sg_copy_from_buffer(sc, buf, 36);
249 case TEST_UNIT_READY:
254 if (desc[target_id] < 0 )
256 simscsi_readwrite6(sc, SSC_READ);
260 if (desc[target_id] < 0 )
262 simscsi_readwrite10(sc, SSC_READ);
266 if (desc[target_id] < 0)
268 simscsi_readwrite6(sc, SSC_WRITE);
272 if (desc[target_id] < 0)
274 simscsi_readwrite10(sc, SSC_WRITE);
278 if (desc[target_id] < 0 || scsi_bufflen(sc) < 8) {
282 disk_size = simscsi_get_disk_size(desc[target_id]);
284 buf[0] = (disk_size >> 24) & 0xff;
285 buf[1] = (disk_size >> 16) & 0xff;
286 buf[2] = (disk_size >> 8) & 0xff;
287 buf[3] = (disk_size >> 0) & 0xff;
288 /* set block size of 512 bytes: */
293 scsi_sg_copy_from_buffer(sc, buf, 8);
299 /* sd.c uses this to determine whether disk does write-caching. */
300 scsi_sg_copy_from_buffer(sc, (char *)empty_zero_page,
306 printk(KERN_ERR "START_STOP\n");
310 panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
313 if (sc->result == DID_BAD_TARGET) {
314 sc->result |= DRIVER_SENSE << 24;
315 sc->sense_buffer[0] = 0x70;
316 sc->sense_buffer[2] = 0x00;
318 if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
319 panic("Attempt to queue command while command is pending!!");
321 atomic_inc(&num_reqs);
323 wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
325 tasklet_schedule(&simscsi_tasklet);
330 simscsi_host_reset (struct scsi_cmnd *sc)
332 printk(KERN_ERR "simscsi_host_reset: not implemented\n");
336 static struct scsi_host_template driver_template = {
337 .name = "simulated SCSI host adapter",
338 .proc_name = "simscsi",
339 .queuecommand = simscsi_queuecommand,
340 .eh_host_reset_handler = simscsi_host_reset,
341 .bios_param = simscsi_biosparam,
342 .can_queue = SIMSCSI_REQ_QUEUE_LEN,
344 .sg_tablesize = SG_ALL,
346 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
347 .use_clustering = DISABLE_CLUSTERING,
355 host = scsi_host_alloc(&driver_template, 0);
359 error = scsi_add_host(host, NULL);
362 scsi_scan_host(host);
373 scsi_remove_host(host);
377 module_init(simscsi_init);
378 module_exit(simscsi_exit);