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>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
20 #include <scsi/scsi_device.h>
21 #include <scsi/scsi_host.h>
23 #define DEBUG_SIMSCSI 0
25 #define SIMSCSI_REQ_QUEUE_LEN 64
26 #define DEFAULT_SIMSCSI_ROOT "/var/ski-disks/sd"
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 static 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
)
102 struct scsi_cmnd
*sc
;
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_biosparam (struct scsi_device
*sdev
, struct block_device
*n
,
116 sector_t capacity
, int ip
[])
118 ip
[0] = 64; /* heads */
119 ip
[1] = 32; /* sectors */
120 ip
[2] = capacity
>> 11; /* cylinders */
125 simscsi_readwrite (struct scsi_cmnd
*sc
, int mode
, unsigned long offset
, unsigned long len
)
127 struct disk_stat stat
;
130 req
.addr
= __pa(sc
->request_buffer
);
131 req
.len
= len
; /* # of bytes to transfer */
133 if (sc
->request_bufflen
< req
.len
)
136 stat
.fd
= desc
[sc
->device
->id
];
138 printk("simscsi_%s @ %lx (off %lx)\n",
139 mode
== SSC_READ
? "read":"write", req
.addr
, offset
);
140 ia64_ssc(stat
.fd
, 1, __pa(&req
), offset
, mode
);
141 ia64_ssc(__pa(&stat
), 0, 0, 0, SSC_WAIT_COMPLETION
);
143 if (stat
.count
== req
.len
) {
146 sc
->result
= DID_ERROR
<< 16;
151 simscsi_sg_readwrite (struct scsi_cmnd
*sc
, int mode
, unsigned long offset
)
153 int list_len
= sc
->use_sg
;
154 struct scatterlist
*sl
= (struct scatterlist
*)sc
->request_buffer
;
155 struct disk_stat stat
;
158 stat
.fd
= desc
[sc
->device
->id
];
161 req
.addr
= __pa(page_address(sl
->page
) + sl
->offset
);
162 req
.len
= sl
->length
;
164 printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
165 mode
== SSC_READ
? "read":"write", req
.addr
, offset
,
166 list_len
, sl
->length
);
167 ia64_ssc(stat
.fd
, 1, __pa(&req
), offset
, mode
);
168 ia64_ssc(__pa(&stat
), 0, 0, 0, SSC_WAIT_COMPLETION
);
170 /* should not happen in our case */
171 if (stat
.count
!= req
.len
) {
172 sc
->result
= DID_ERROR
<< 16;
175 offset
+= sl
->length
;
183 * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
185 * Added 02/26/99 S.Eranian
188 simscsi_readwrite6 (struct scsi_cmnd
*sc
, int mode
)
190 unsigned long offset
;
192 offset
= (((sc
->cmnd
[1] & 0x1f) << 16) | (sc
->cmnd
[2] << 8) | sc
->cmnd
[3])*512;
194 simscsi_sg_readwrite(sc
, mode
, offset
);
196 simscsi_readwrite(sc
, mode
, offset
, sc
->cmnd
[4]*512);
200 simscsi_get_disk_size (int fd
)
202 struct disk_stat stat
;
203 size_t bit
, sectors
= 0;
208 * This is a bit kludgey: the simulator doesn't provide a
209 * direct way of determining the disk size, so we do a binary
210 * search, assuming a maximum disk size of 128GB.
212 for (bit
= (128UL << 30)/512; bit
!= 0; bit
>>= 1) {
213 req
.addr
= __pa(&buf
);
214 req
.len
= sizeof(buf
);
215 ia64_ssc(fd
, 1, __pa(&req
), ((sectors
| bit
) - 1)*512, SSC_READ
);
217 ia64_ssc(__pa(&stat
), 0, 0, 0, SSC_WAIT_COMPLETION
);
218 if (stat
.count
== sizeof(buf
))
221 return sectors
- 1; /* return last valid sector number */
225 simscsi_readwrite10 (struct scsi_cmnd
*sc
, int mode
)
227 unsigned long offset
;
229 offset
= (((unsigned long)sc
->cmnd
[2] << 24)
230 | ((unsigned long)sc
->cmnd
[3] << 16)
231 | ((unsigned long)sc
->cmnd
[4] << 8)
232 | ((unsigned long)sc
->cmnd
[5] << 0))*512UL;
234 simscsi_sg_readwrite(sc
, mode
, offset
);
236 simscsi_readwrite(sc
, mode
, offset
, ((sc
->cmnd
[7] << 8) | sc
->cmnd
[8])*512);
239 static void simscsi_fillresult(struct scsi_cmnd
*sc
, char *buf
, unsigned len
)
242 int scatterlen
= sc
->use_sg
;
243 struct scatterlist
*slp
;
246 memcpy(sc
->request_buffer
, buf
, len
);
247 else for (slp
= (struct scatterlist
*)sc
->request_buffer
;
248 scatterlen
-- > 0 && len
> 0; slp
++) {
249 unsigned thislen
= min(len
, slp
->length
);
251 memcpy(page_address(slp
->page
) + slp
->offset
, buf
, thislen
);
258 simscsi_queuecommand (struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
260 unsigned int target_id
= sc
->device
->id
;
261 char fname
[MAX_ROOT_LEN
+16];
266 register long sp
asm ("sp");
269 printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
270 target_id
, sc
->cmnd
[0], sc
->serial_number
, sp
, done
);
273 sc
->result
= DID_BAD_TARGET
<< 16;
274 sc
->scsi_done
= done
;
275 if (target_id
<= 15 && sc
->device
->lun
== 0) {
276 switch (sc
->cmnd
[0]) {
278 if (sc
->request_bufflen
< 35) {
281 sprintf (fname
, "%s%c", simscsi_root
, 'a' + target_id
);
282 desc
[target_id
] = ia64_ssc(__pa(fname
), SSC_READ_ACCESS
|SSC_WRITE_ACCESS
,
284 if (desc
[target_id
] < 0) {
285 /* disk doesn't exist... */
289 buf
[0] = 0; /* magnetic disk */
290 buf
[1] = 0; /* not a removable medium */
291 buf
[2] = 2; /* SCSI-2 compliant device */
292 buf
[3] = 2; /* SCSI-2 response data format */
293 buf
[4] = 31; /* additional length (bytes) */
294 buf
[5] = 0; /* reserved */
295 buf
[6] = 0; /* reserved */
296 buf
[7] = 0; /* various flags */
297 memcpy(buf
+ 8, "HP SIMULATED DISK 0.00", 28);
298 simscsi_fillresult(sc
, buf
, 36);
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
);
331 if (desc
[target_id
] < 0 || sc
->request_bufflen
< 8) {
335 disk_size
= simscsi_get_disk_size(desc
[target_id
]);
337 buf
[0] = (disk_size
>> 24) & 0xff;
338 buf
[1] = (disk_size
>> 16) & 0xff;
339 buf
[2] = (disk_size
>> 8) & 0xff;
340 buf
[3] = (disk_size
>> 0) & 0xff;
341 /* set block size of 512 bytes: */
346 simscsi_fillresult(sc
, buf
, 8);
352 /* sd.c uses this to determine whether disk does write-caching. */
353 simscsi_fillresult(sc
, (char *)empty_zero_page
, sc
->request_bufflen
);
358 printk(KERN_ERR
"START_STOP\n");
362 panic("simscsi: unknown SCSI command %u\n", sc
->cmnd
[0]);
365 if (sc
->result
== DID_BAD_TARGET
) {
366 sc
->result
|= DRIVER_SENSE
<< 24;
367 sc
->sense_buffer
[0] = 0x70;
368 sc
->sense_buffer
[2] = 0x00;
370 if (atomic_read(&num_reqs
) >= SIMSCSI_REQ_QUEUE_LEN
) {
371 panic("Attempt to queue command while command is pending!!");
373 atomic_inc(&num_reqs
);
375 wr
= (wr
+ 1) % SIMSCSI_REQ_QUEUE_LEN
;
377 tasklet_schedule(&simscsi_tasklet
);
382 simscsi_host_reset (struct scsi_cmnd
*sc
)
384 printk(KERN_ERR
"simscsi_host_reset: not implemented\n");
388 static struct scsi_host_template driver_template
= {
389 .name
= "simulated SCSI host adapter",
390 .proc_name
= "simscsi",
391 .queuecommand
= simscsi_queuecommand
,
392 .eh_host_reset_handler
= simscsi_host_reset
,
393 .bios_param
= simscsi_biosparam
,
394 .can_queue
= SIMSCSI_REQ_QUEUE_LEN
,
396 .sg_tablesize
= SG_ALL
,
398 .cmd_per_lun
= SIMSCSI_REQ_QUEUE_LEN
,
399 .use_clustering
= DISABLE_CLUSTERING
,
407 host
= scsi_host_alloc(&driver_template
, 0);
411 error
= scsi_add_host(host
, NULL
);
413 scsi_scan_host(host
);
420 scsi_remove_host(host
);
424 module_init(simscsi_init
);
425 module_exit(simscsi_exit
);