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/config.h>
12 #include <linux/blk.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/timer.h>
18 #include <scsi/scsi.h>
22 #include "../drivers/scsi/scsi.h"
23 #include "../drivers/scsi/hosts.h"
26 #define DEBUG_SIMSCSI 0
28 /* Simulator system calls: */
34 #define SSC_GET_COMPLETION 54
35 #define SSC_WAIT_COMPLETION 55
37 #define SSC_WRITE_ACCESS 2
38 #define SSC_READ_ACCESS 1
42 # define DBG simscsi_debug
47 static struct Scsi_Host *host;
49 static void simscsi_interrupt (unsigned long val);
50 DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
62 extern long ia64_ssc (long arg0, long arg1, long arg2, long arg3, int nr);
64 static int desc[16] = {
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
68 static struct queue_entry {
70 } queue[SIMSCSI_REQ_QUEUE_LEN];
73 static atomic_t num_reqs = ATOMIC_INIT(0);
75 /* base name for default disks */
76 static char *simscsi_root = DEFAULT_SIMSCSI_ROOT;
78 #define MAX_ROOT_LEN 128
81 * used to setup a new base for disk images
82 * to use /foo/bar/disk[a-z] as disk images
83 * you have to specify simscsi=/foo/bar/disk on the command line
86 simscsi_setup (char *s)
88 /* XXX Fix me we may need to strcpy() ? */
89 if (strlen(s) > MAX_ROOT_LEN) {
90 printk(KERN_ERR "simscsi_setup: prefix too long---using default %s\n",
97 __setup("simscsi=", simscsi_setup);
100 simscsi_interrupt (unsigned long val)
104 while ((sc = queue[rd].sc) != 0) {
105 atomic_dec(&num_reqs);
108 printk("simscsi_interrupt: done with %ld\n", sc->serial_number);
109 (*sc->scsi_done)(sc);
110 rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN;
115 simscsi_detect (Scsi_Host_Template *templ)
117 templ->proc_name = "simscsi";
118 host = scsi_register(templ, 0);
122 return 1; /* fake one SCSI host adapter */
126 simscsi_release (struct Scsi_Host *host)
128 return 0; /* this is easy... */
132 simscsi_info (struct Scsi_Host *host)
134 return "simulated SCSI host adapter";
138 simscsi_biosparam (struct scsi_device *sdev, struct block_device *n,
139 sector_t capacity, int ip[])
141 ip[0] = 64; /* heads */
142 ip[1] = 32; /* sectors */
143 ip[2] = capacity >> 11; /* cylinders */
148 simscsi_readwrite (Scsi_Cmnd *sc, int mode, unsigned long offset, unsigned long len)
150 struct disk_stat stat;
153 req.addr = __pa(sc->request_buffer);
154 req.len = len; /* # of bytes to transfer */
156 if (sc->request_bufflen < req.len)
159 stat.fd = desc[sc->device->id];
161 printk("simscsi_%s @ %lx (off %lx)\n",
162 mode == SSC_READ ? "read":"write", req.addr, offset);
163 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
164 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
166 if (stat.count == req.len) {
169 sc->result = DID_ERROR << 16;
174 simscsi_sg_readwrite (Scsi_Cmnd *sc, int mode, unsigned long offset)
176 int list_len = sc->use_sg;
177 struct scatterlist *sl = (struct scatterlist *)sc->buffer;
178 struct disk_stat stat;
181 stat.fd = desc[sc->device->id];
184 req.addr = __pa(page_address(sl->page) + sl->offset);
185 req.len = sl->length;
187 printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
188 mode == SSC_READ ? "read":"write", req.addr, offset,
189 list_len, sl->length);
190 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
191 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
193 /* should not happen in our case */
194 if (stat.count != req.len) {
195 sc->result = DID_ERROR << 16;
198 offset += sl->length;
206 * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
208 * Added 02/26/99 S.Eranian
211 simscsi_readwrite6 (Scsi_Cmnd *sc, int mode)
213 unsigned long offset;
215 offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
217 simscsi_sg_readwrite(sc, mode, offset);
219 simscsi_readwrite(sc, mode, offset, sc->cmnd[4]*512);
223 simscsi_get_disk_size (int fd)
225 struct disk_stat stat;
226 size_t bit, sectors = 0;
231 * This is a bit kludgey: the simulator doesn't provide a direct way of determining
232 * the disk size, so we do a binary search, assuming a maximum disk size of 4GB.
234 for (bit = (4UL << 30)/512; bit != 0; bit >>= 1) {
235 req.addr = __pa(&buf);
236 req.len = sizeof(buf);
237 ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
239 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
240 if (stat.count == sizeof(buf))
243 return sectors - 1; /* return last valid sector number */
247 simscsi_readwrite10 (Scsi_Cmnd *sc, int mode)
249 unsigned long offset;
251 offset = ( (sc->cmnd[2] << 24) | (sc->cmnd[3] << 16)
252 | (sc->cmnd[4] << 8) | (sc->cmnd[5] << 0))*512;
254 simscsi_sg_readwrite(sc, mode, offset);
256 simscsi_readwrite(sc, mode, offset, ((sc->cmnd[7] << 8) | sc->cmnd[8])*512);
260 simscsi_queuecommand (Scsi_Cmnd *sc, void (*done)(Scsi_Cmnd *))
262 unsigned int target_id = sc->device->id;
263 char fname[MAX_ROOT_LEN+16];
267 register long sp asm ("sp");
270 printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
271 target_id, sc->cmnd[0], sc->serial_number, sp, done);
274 sc->result = DID_BAD_TARGET << 16;
275 sc->scsi_done = done;
276 if (target_id <= 15 && sc->device->lun == 0) {
277 switch (sc->cmnd[0]) {
279 if (sc->request_bufflen < 35) {
282 sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
283 desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
285 if (desc[target_id] < 0) {
286 /* disk doesn't exist... */
289 buf = sc->request_buffer;
290 buf[0] = 0; /* magnetic disk */
291 buf[1] = 0; /* not a removable medium */
292 buf[2] = 2; /* SCSI-2 compliant device */
293 buf[3] = 2; /* SCSI-2 response data format */
294 buf[4] = 31; /* additional length (bytes) */
295 buf[5] = 0; /* reserved */
296 buf[6] = 0; /* reserved */
297 buf[7] = 0; /* various flags */
298 memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28);
302 case TEST_UNIT_READY:
307 if (desc[target_id] < 0 )
309 simscsi_readwrite6(sc, SSC_READ);
313 if (desc[target_id] < 0 )
315 simscsi_readwrite10(sc, SSC_READ);
319 if (desc[target_id] < 0)
321 simscsi_readwrite6(sc, SSC_WRITE);
325 if (desc[target_id] < 0)
327 simscsi_readwrite10(sc, SSC_WRITE);
332 if (desc[target_id] < 0 || sc->request_bufflen < 8) {
335 buf = sc->request_buffer;
337 disk_size = simscsi_get_disk_size(desc[target_id]);
339 /* pretend to be a 1GB disk (partition table contains real stuff): */
340 buf[0] = (disk_size >> 24) & 0xff;
341 buf[1] = (disk_size >> 16) & 0xff;
342 buf[2] = (disk_size >> 8) & 0xff;
343 buf[3] = (disk_size >> 0) & 0xff;
344 /* set block size of 512 bytes: */
354 /* sd.c uses this to determine whether disk does write-caching. */
355 memset(sc->request_buffer, 0, 128);
360 printk(KERN_ERR "START_STOP\n");
364 panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
367 if (sc->result == DID_BAD_TARGET) {
368 sc->result |= DRIVER_SENSE << 24;
369 sc->sense_buffer[0] = 0x70;
370 sc->sense_buffer[2] = 0x00;
372 if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
373 panic("Attempt to queue command while command is pending!!");
375 atomic_inc(&num_reqs);
377 wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
379 tasklet_schedule(&simscsi_tasklet);
384 simscsi_host_reset (Scsi_Cmnd *sc)
386 printk(KERN_ERR "simscsi_host_reset: not implemented\n");
390 static Scsi_Host_Template driver_template = {
392 .detect = simscsi_detect,
393 .release = simscsi_release,
394 .info = simscsi_info,
395 .queuecommand = simscsi_queuecommand,
396 .eh_host_reset_handler = simscsi_host_reset,
397 .bios_param = simscsi_biosparam,
398 .can_queue = SIMSCSI_REQ_QUEUE_LEN,
400 .sg_tablesize = SG_ALL,
402 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
403 .use_clustering = DISABLE_CLUSTERING,
405 #include "../drivers/scsi/scsi_module.c"