1 /*
2  * Simulated SCSI driver.
3  *
4  * Copyright (C) 1999, 2001-2003 Hewlett-Packard Co
5  *	David Mosberger-Tang <davidm@hpl.hp.com>
6  *	Stephane Eranian <eranian@hpl.hp.com>
7  *
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
10  */
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>
17 
18 #include <scsi/scsi.h>
19 
20 #include <asm/irq.h>
21 
22 #include "../drivers/scsi/scsi.h"
23 #include "../drivers/scsi/sd.h"
24 #include "../drivers/scsi/hosts.h"
25 #include "simscsi.h"
26 
27 #define DEBUG_SIMSCSI	1
28 
29 /* Simulator system calls: */
30 
31 #define SSC_OPEN			50
32 #define SSC_CLOSE			51
33 #define SSC_READ			52
34 #define SSC_WRITE			53
35 #define SSC_GET_COMPLETION		54
36 #define SSC_WAIT_COMPLETION		55
37 
38 #define SSC_WRITE_ACCESS		2
39 #define SSC_READ_ACCESS			1
40 
41 #if DEBUG_SIMSCSI
42   int simscsi_debug;
43 # define DBG	simscsi_debug
44 #else
45 # define DBG	0
46 #endif
47 
48 static void simscsi_interrupt (unsigned long val);
49 DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
50 
51 struct disk_req {
52 	unsigned long addr;
53 	unsigned len;
54 };
55 
56 struct disk_stat {
57 	int fd;
58 	unsigned count;
59 };
60 
61 extern long ia64_ssc (long arg0, long arg1, long arg2, long arg3, int nr);
62 
63 static int desc[16] = {
64 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
65 };
66 
67 static struct queue_entry {
68 	Scsi_Cmnd *sc;
69 } queue[SIMSCSI_REQ_QUEUE_LEN];
70 
71 static int rd, wr;
72 static atomic_t num_reqs = ATOMIC_INIT(0);
73 
74 /* base name for default disks */
75 static char *simscsi_root = DEFAULT_SIMSCSI_ROOT;
76 
77 #define MAX_ROOT_LEN	128
78 
79 /*
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
83  */
84 static int __init
simscsi_setup(char * s)85 simscsi_setup (char *s)
86 {
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",
90 		       simscsi_root);
91 	}
92 	simscsi_root = s;
93 	return 1;
94 }
95 
96 __setup("simscsi=", simscsi_setup);
97 
98 static void
simscsi_interrupt(unsigned long val)99 simscsi_interrupt (unsigned long val)
100 {
101 	unsigned long flags;
102 	Scsi_Cmnd *sc;
103 
104 	spin_lock_irqsave(&io_request_lock, flags);
105 	{
106 		while ((sc = queue[rd].sc) != 0) {
107 			atomic_dec(&num_reqs);
108 			queue[rd].sc = 0;
109 			if (DBG)
110 				printk("simscsi_interrupt: done with %ld\n", sc->serial_number);
111 			(*sc->scsi_done)(sc);
112 			rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN;
113 		}
114 	}
115 	spin_unlock_irqrestore(&io_request_lock, flags);
116 }
117 
118 int
simscsi_detect(Scsi_Host_Template * templ)119 simscsi_detect (Scsi_Host_Template *templ)
120 {
121 	templ->proc_name = "simscsi";
122 	return 1;	/* fake one SCSI host adapter */
123 }
124 
125 int
simscsi_release(struct Scsi_Host * host)126 simscsi_release (struct Scsi_Host *host)
127 {
128 	return 0;	/* this is easy...  */
129 }
130 
131 const char *
simscsi_info(struct Scsi_Host * host)132 simscsi_info (struct Scsi_Host *host)
133 {
134 	return "simulated SCSI host adapter";
135 }
136 
137 int
simscsi_biosparam(Disk * disk,kdev_t n,int ip[])138 simscsi_biosparam (Disk *disk, kdev_t n, int ip[])
139 {
140 	unsigned capacity = disk->capacity;
141 
142 	ip[0] = 64;		/* heads */
143 	ip[1] = 32;		/* sectors */
144 	ip[2] = capacity >> 11;	/* cylinders */
145 	return 0;
146 }
147 
148 static void
simscsi_readwrite(Scsi_Cmnd * sc,int mode,unsigned long offset,unsigned long len)149 simscsi_readwrite (Scsi_Cmnd *sc, int mode, unsigned long offset, unsigned long len)
150 {
151 	struct disk_stat stat;
152 	struct disk_req req;
153 
154 	req.addr = __pa(sc->request_buffer);
155 	req.len  = len;			/* # of bytes to transfer */
156 
157 	if (sc->request_bufflen < req.len)
158 		return;
159 
160 	stat.fd = desc[sc->target];
161 	if (DBG)
162 		printk("simscsi_%s @ %lx (off %lx)\n",
163 		       mode == SSC_READ ? "read":"write", req.addr, offset);
164 	ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
165 	ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
166 
167 	if (stat.count == req.len) {
168 		sc->result = GOOD;
169 	} else {
170 		sc->result = DID_ERROR << 16;
171 	}
172 }
173 
174 static void
simscsi_sg_readwrite(Scsi_Cmnd * sc,int mode,unsigned long offset)175 simscsi_sg_readwrite (Scsi_Cmnd *sc, int mode, unsigned long offset)
176 {
177 	int list_len = sc->use_sg;
178 	struct scatterlist *sl = (struct scatterlist *)sc->buffer;
179 	struct disk_stat stat;
180 	struct disk_req req;
181 
182 	stat.fd = desc[sc->target];
183 
184 	while (list_len) {
185 		req.addr = __pa(sl->address);
186 		req.len  = sl->length;
187 		if (DBG)
188 			printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
189 			       mode == SSC_READ ? "read":"write", req.addr, offset,
190 			       list_len, sl->length);
191 		ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
192 		ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
193 
194 		/* should not happen in our case */
195 		if (stat.count != req.len) {
196 			sc->result = DID_ERROR << 16;
197 			return;
198 		}
199 		offset +=  sl->length;
200 		sl++;
201 		list_len--;
202 	}
203 	sc->result = GOOD;
204 }
205 
206 /*
207  * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
208  * commands.
209  * Added 02/26/99 S.Eranian
210  */
211 static void
simscsi_readwrite6(Scsi_Cmnd * sc,int mode)212 simscsi_readwrite6 (Scsi_Cmnd *sc, int mode)
213 {
214 	unsigned long offset;
215 
216 	offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
217 	if (sc->use_sg > 0)
218 		simscsi_sg_readwrite(sc, mode, offset);
219 	else
220 		simscsi_readwrite(sc, mode, offset, sc->cmnd[4]*512);
221 }
222 
223 static size_t
simscsi_get_disk_size(int fd)224 simscsi_get_disk_size (int fd)
225 {
226 	struct disk_stat stat;
227 	size_t bit, sectors = 0;
228 	struct disk_req req;
229 	char buf[512];
230 
231 	/*
232 	 * This is a bit kludgey: the simulator doesn't provide a direct way of determining
233 	 * the disk size, so we do a binary search, assuming a maximum disk size of 4GB.
234 	 */
235 	for (bit = (4UL << 30)/512; bit != 0; bit >>= 1) {
236 		req.addr = __pa(&buf);
237 		req.len = sizeof(buf);
238 		ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
239 		stat.fd = fd;
240 		ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
241 		if (stat.count == sizeof(buf))
242 			sectors |= bit;
243 	}
244 	return sectors - 1;	/* return last valid sector number */
245 }
246 
247 static void
simscsi_readwrite10(Scsi_Cmnd * sc,int mode)248 simscsi_readwrite10 (Scsi_Cmnd *sc, int mode)
249 {
250 	unsigned long offset;
251 
252 	offset = (  (sc->cmnd[2] << 24) | (sc->cmnd[3] << 16)
253 		  | (sc->cmnd[4] <<  8) | (sc->cmnd[5] <<  0))*512;
254 	if (sc->use_sg > 0)
255 		simscsi_sg_readwrite(sc, mode, offset);
256 	else
257 		simscsi_readwrite(sc, mode, offset, ((sc->cmnd[7] << 8) | sc->cmnd[8])*512);
258 }
259 
260 int
simscsi_queuecommand(Scsi_Cmnd * sc,void (* done)(Scsi_Cmnd *))261 simscsi_queuecommand (Scsi_Cmnd *sc, void (*done)(Scsi_Cmnd *))
262 {
263 	char fname[MAX_ROOT_LEN+16];
264 	size_t disk_size;
265 	char *buf;
266 #if DEBUG_SIMSCSI
267 	register long sp asm ("sp");
268 
269 	if (DBG)
270 		printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
271 		       sc->target, sc->cmnd[0], sc->serial_number, sp, done);
272 #endif
273 
274 	sc->result = DID_BAD_TARGET << 16;
275 	sc->scsi_done = done;
276 	if (sc->target <= 15 && sc->lun == 0) {
277 		switch (sc->cmnd[0]) {
278 		      case INQUIRY:
279 			if (sc->request_bufflen < 35) {
280 				break;
281 			}
282 			sprintf (fname, "%s%c", simscsi_root, 'a' + sc->target);
283 			desc[sc->target] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
284 						    0, 0, SSC_OPEN);
285 			if (desc[sc->target] < 0) {
286 				/* disk doesn't exist... */
287 				break;
288 			}
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);
299 			sc->result = GOOD;
300 			break;
301 
302 		      case TEST_UNIT_READY:
303 			sc->result = GOOD;
304 			break;
305 
306 		      case READ_6:
307 			if (desc[sc->target] < 0 )
308 				break;
309 			simscsi_readwrite6(sc, SSC_READ);
310 			break;
311 
312 		      case READ_10:
313 			if (desc[sc->target] < 0 )
314 				break;
315 			simscsi_readwrite10(sc, SSC_READ);
316 			break;
317 
318 		      case WRITE_6:
319 			if (desc[sc->target] < 0)
320 				break;
321 			simscsi_readwrite6(sc, SSC_WRITE);
322 			break;
323 
324 		      case WRITE_10:
325 			if (desc[sc->target] < 0)
326 				break;
327 			simscsi_readwrite10(sc, SSC_WRITE);
328 			break;
329 
330 
331 		      case READ_CAPACITY:
332 			if (desc[sc->target] < 0 || sc->request_bufflen < 8) {
333 				break;
334 			}
335 			buf = sc->request_buffer;
336 
337 			disk_size = simscsi_get_disk_size(desc[sc->target]);
338 			buf[0] = (disk_size >> 24) & 0xff;
339 			buf[1] = (disk_size >> 16) & 0xff;
340 			buf[2] = (disk_size >>  8) & 0xff;
341 			buf[3] = (disk_size >>  0) & 0xff;
342 			/* set block size of 512 bytes: */
343 			buf[4] = 0;
344 			buf[5] = 0;
345 			buf[6] = 2;
346 			buf[7] = 0;
347 			sc->result = GOOD;
348 			break;
349 
350 		      case MODE_SENSE:
351 			/* sd.c uses this to determine whether disk does write-caching. */
352 			memset(sc->request_buffer, 0, 128);
353 			sc->result = GOOD;
354 			break;
355 
356 		      case START_STOP:
357 			printk(KERN_ERR "START_STOP\n");
358 			break;
359 
360 		      default:
361 			panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
362 		}
363 	}
364 	if (sc->result == DID_BAD_TARGET) {
365 		sc->result |= DRIVER_SENSE << 24;
366 		sc->sense_buffer[0] = 0x70;
367 		sc->sense_buffer[2] = 0x00;
368 	}
369 	if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
370 		panic("Attempt to queue command while command is pending!!");
371 	}
372 	atomic_inc(&num_reqs);
373 	queue[wr].sc = sc;
374 	wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
375 
376 	tasklet_schedule(&simscsi_tasklet);
377 	return 0;
378 }
379 
380 int
simscsi_reset(Scsi_Cmnd * cmd,unsigned int reset_flags)381 simscsi_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
382 {
383 	printk(KERN_ERR "simscsi_reset: unimplemented\n");
384 	return SCSI_RESET_SUCCESS;
385 }
386 
387 int
simscsi_abort(Scsi_Cmnd * cmd)388 simscsi_abort (Scsi_Cmnd *cmd)
389 {
390 	printk(KERN_ERR "simscsi_abort: unimplemented\n");
391 	return SCSI_ABORT_SUCCESS;
392 }
393 
394 static Scsi_Host_Template driver_template = SIMSCSI;
395 
396 #include "../drivers/scsi/scsi_module.c"
397