1 // SPDX-License-Identifier: GPL-2.0
3 * Simulated SCSI driver.
5 * Copyright (C) 1999, 2001-2003 Hewlett-Packard Co
6 * David Mosberger-Tang <davidm@hpl.hp.com>
7 * Stephane Eranian <eranian@hpl.hp.com>
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
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>
18 #include "hpsim_ssc.h"
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_host.h>
25 #define DEBUG_SIMSCSI 0
27 #define SIMSCSI_REQ_QUEUE_LEN 64
28 #define DEFAULT_SIMSCSI_ROOT "/var/ski-disks/sd"
30 /* Simulator system calls: */
36 #define SSC_GET_COMPLETION 54
37 #define SSC_WAIT_COMPLETION 55
39 #define SSC_WRITE_ACCESS 2
40 #define SSC_READ_ACCESS 1
44 # define DBG simscsi_debug
49 static struct Scsi_Host *host;
51 static void simscsi_interrupt (unsigned long val);
52 static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
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) != NULL) {
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_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset)
128 struct scatterlist *sl;
129 struct disk_stat stat;
132 stat.fd = desc[sc->device->id];
134 scsi_for_each_sg(sc, sl, scsi_sg_count(sc), i) {
135 req.addr = __pa(sg_virt(sl));
136 req.len = sl->length;
138 printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
139 mode == SSC_READ ? "read":"write", req.addr, offset,
140 scsi_sg_count(sc) - i, sl->length);
141 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
142 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
144 /* should not happen in our case */
145 if (stat.count != req.len) {
146 sc->result = DID_ERROR << 16;
149 offset += sl->length;
155 * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
157 * Added 02/26/99 S.Eranian
160 simscsi_readwrite6 (struct scsi_cmnd *sc, int mode)
162 unsigned long offset;
164 offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
165 simscsi_sg_readwrite(sc, mode, offset);
169 simscsi_get_disk_size (int fd)
171 struct disk_stat stat;
172 size_t bit, sectors = 0;
177 * This is a bit kludgey: the simulator doesn't provide a
178 * direct way of determining the disk size, so we do a binary
179 * search, assuming a maximum disk size of 128GB.
181 for (bit = (128UL << 30)/512; bit != 0; bit >>= 1) {
182 req.addr = __pa(&buf);
183 req.len = sizeof(buf);
184 ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
186 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
187 if (stat.count == sizeof(buf))
190 return sectors - 1; /* return last valid sector number */
194 simscsi_readwrite10 (struct scsi_cmnd *sc, int mode)
196 unsigned long offset;
198 offset = (((unsigned long)sc->cmnd[2] << 24)
199 | ((unsigned long)sc->cmnd[3] << 16)
200 | ((unsigned long)sc->cmnd[4] << 8)
201 | ((unsigned long)sc->cmnd[5] << 0))*512UL;
202 simscsi_sg_readwrite(sc, mode, offset);
206 simscsi_queuecommand_lck (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
208 unsigned int target_id = sc->device->id;
209 char fname[MAX_ROOT_LEN+16];
214 register long sp asm ("sp");
217 printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
218 target_id, sc->cmnd[0], sc->serial_number, sp, done);
221 sc->result = DID_BAD_TARGET << 16;
222 sc->scsi_done = done;
223 if (target_id <= 15 && sc->device->lun == 0) {
224 switch (sc->cmnd[0]) {
226 if (scsi_bufflen(sc) < 35) {
229 sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
230 desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
232 if (desc[target_id] < 0) {
233 /* disk doesn't exist... */
237 buf[0] = 0; /* magnetic disk */
238 buf[1] = 0; /* not a removable medium */
239 buf[2] = 2; /* SCSI-2 compliant device */
240 buf[3] = 2; /* SCSI-2 response data format */
241 buf[4] = 31; /* additional length (bytes) */
242 buf[5] = 0; /* reserved */
243 buf[6] = 0; /* reserved */
244 buf[7] = 0; /* various flags */
245 memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28);
246 scsi_sg_copy_from_buffer(sc, buf, 36);
250 case TEST_UNIT_READY:
255 if (desc[target_id] < 0 )
257 simscsi_readwrite6(sc, SSC_READ);
261 if (desc[target_id] < 0 )
263 simscsi_readwrite10(sc, SSC_READ);
267 if (desc[target_id] < 0)
269 simscsi_readwrite6(sc, SSC_WRITE);
273 if (desc[target_id] < 0)
275 simscsi_readwrite10(sc, SSC_WRITE);
279 if (desc[target_id] < 0 || scsi_bufflen(sc) < 8) {
283 disk_size = simscsi_get_disk_size(desc[target_id]);
285 buf[0] = (disk_size >> 24) & 0xff;
286 buf[1] = (disk_size >> 16) & 0xff;
287 buf[2] = (disk_size >> 8) & 0xff;
288 buf[3] = (disk_size >> 0) & 0xff;
289 /* set block size of 512 bytes: */
294 scsi_sg_copy_from_buffer(sc, buf, 8);
300 /* sd.c uses this to determine whether disk does write-caching. */
301 scsi_sg_copy_from_buffer(sc, (char *)empty_zero_page,
307 printk(KERN_ERR "START_STOP\n");
311 panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
314 if (sc->result == DID_BAD_TARGET) {
315 sc->result |= DRIVER_SENSE << 24;
316 sc->sense_buffer[0] = 0x70;
317 sc->sense_buffer[2] = 0x00;
319 if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
320 panic("Attempt to queue command while command is pending!!");
322 atomic_inc(&num_reqs);
324 wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
326 tasklet_schedule(&simscsi_tasklet);
330 static DEF_SCSI_QCMD(simscsi_queuecommand)
333 simscsi_host_reset (struct scsi_cmnd *sc)
335 printk(KERN_ERR "simscsi_host_reset: not implemented\n");
339 static struct scsi_host_template driver_template = {
340 .name = "simulated SCSI host adapter",
341 .proc_name = "simscsi",
342 .queuecommand = simscsi_queuecommand,
343 .eh_host_reset_handler = simscsi_host_reset,
344 .bios_param = simscsi_biosparam,
345 .can_queue = SIMSCSI_REQ_QUEUE_LEN,
347 .sg_tablesize = SG_ALL,
349 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
350 .use_clustering = DISABLE_CLUSTERING,
358 host = scsi_host_alloc(&driver_template, 0);
362 error = scsi_add_host(host, NULL);
365 scsi_scan_host(host);
372 device_initcall(simscsi_init);