• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/arch/ia64/hp/sim/
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/blkdev.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <asm/irq.h>
17#include "hpsim_ssc.h"
18
19#include <scsi/scsi.h>
20#include <scsi/scsi_cmnd.h>
21#include <scsi/scsi_device.h>
22#include <scsi/scsi_host.h>
23
24#define DEBUG_SIMSCSI	0
25
26#define SIMSCSI_REQ_QUEUE_LEN	64
27#define DEFAULT_SIMSCSI_ROOT	"/var/ski-disks/sd"
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
48static struct Scsi_Host *host;
49
50static void simscsi_interrupt (unsigned long val);
51static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0);
52
53struct disk_req {
54	unsigned long addr;
55	unsigned len;
56};
57
58struct disk_stat {
59	int fd;
60	unsigned count;
61};
62
63static int desc[16] = {
64	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
65};
66
67static struct queue_entry {
68	struct scsi_cmnd *sc;
69} queue[SIMSCSI_REQ_QUEUE_LEN];
70
71static int rd, wr;
72static atomic_t num_reqs = ATOMIC_INIT(0);
73
74/* base name for default disks */
75static 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 */
84static int __init
85simscsi_setup (char *s)
86{
87	if (strlen(s) > MAX_ROOT_LEN) {
88		printk(KERN_ERR "simscsi_setup: prefix too long---using default %s\n",
89		       simscsi_root);
90	}
91	simscsi_root = s;
92	return 1;
93}
94
95__setup("simscsi=", simscsi_setup);
96
97static void
98simscsi_interrupt (unsigned long val)
99{
100	struct scsi_cmnd *sc;
101
102	while ((sc = queue[rd].sc) != NULL) {
103		atomic_dec(&num_reqs);
104		queue[rd].sc = NULL;
105		if (DBG)
106			printk("simscsi_interrupt: done with %ld\n", sc->serial_number);
107		(*sc->scsi_done)(sc);
108		rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN;
109	}
110}
111
112static int
113simscsi_biosparam (struct scsi_device *sdev, struct block_device *n,
114		sector_t capacity, int ip[])
115{
116	ip[0] = 64;		/* heads */
117	ip[1] = 32;		/* sectors */
118	ip[2] = capacity >> 11;	/* cylinders */
119	return 0;
120}
121
122static void
123simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset)
124{
125	int i;
126	struct scatterlist *sl;
127	struct disk_stat stat;
128	struct disk_req req;
129
130	stat.fd = desc[sc->device->id];
131
132	scsi_for_each_sg(sc, sl, scsi_sg_count(sc), i) {
133		req.addr = __pa(sg_virt(sl));
134		req.len  = sl->length;
135		if (DBG)
136			printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n",
137			       mode == SSC_READ ? "read":"write", req.addr, offset,
138			       scsi_sg_count(sc) - i, sl->length);
139		ia64_ssc(stat.fd, 1, __pa(&req), offset, mode);
140		ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
141
142		/* should not happen in our case */
143		if (stat.count != req.len) {
144			sc->result = DID_ERROR << 16;
145			return;
146		}
147		offset +=  sl->length;
148	}
149	sc->result = GOOD;
150}
151
152/*
153 * function handling both READ_6/WRITE_6 (non-scatter/gather mode)
154 * commands.
155 * Added 02/26/99 S.Eranian
156 */
157static void
158simscsi_readwrite6 (struct scsi_cmnd *sc, int mode)
159{
160	unsigned long offset;
161
162	offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512;
163	simscsi_sg_readwrite(sc, mode, offset);
164}
165
166static size_t
167simscsi_get_disk_size (int fd)
168{
169	struct disk_stat stat;
170	size_t bit, sectors = 0;
171	struct disk_req req;
172	char buf[512];
173
174	/*
175	 * This is a bit kludgey: the simulator doesn't provide a
176	 * direct way of determining the disk size, so we do a binary
177	 * search, assuming a maximum disk size of 128GB.
178	 */
179	for (bit = (128UL << 30)/512; bit != 0; bit >>= 1) {
180		req.addr = __pa(&buf);
181		req.len = sizeof(buf);
182		ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ);
183		stat.fd = fd;
184		ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION);
185		if (stat.count == sizeof(buf))
186			sectors |= bit;
187	}
188	return sectors - 1;	/* return last valid sector number */
189}
190
191static void
192simscsi_readwrite10 (struct scsi_cmnd *sc, int mode)
193{
194	unsigned long offset;
195
196	offset = (((unsigned long)sc->cmnd[2] << 24)
197		| ((unsigned long)sc->cmnd[3] << 16)
198		| ((unsigned long)sc->cmnd[4] <<  8)
199		| ((unsigned long)sc->cmnd[5] <<  0))*512UL;
200	simscsi_sg_readwrite(sc, mode, offset);
201}
202
203static int
204simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
205{
206	unsigned int target_id = sc->device->id;
207	char fname[MAX_ROOT_LEN+16];
208	size_t disk_size;
209	char *buf;
210	char localbuf[36];
211#if DEBUG_SIMSCSI
212	register long sp asm ("sp");
213
214	if (DBG)
215		printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n",
216		       target_id, sc->cmnd[0], sc->serial_number, sp, done);
217#endif
218
219	sc->result = DID_BAD_TARGET << 16;
220	sc->scsi_done = done;
221	if (target_id <= 15 && sc->device->lun == 0) {
222		switch (sc->cmnd[0]) {
223		      case INQUIRY:
224			if (scsi_bufflen(sc) < 35) {
225				break;
226			}
227			sprintf (fname, "%s%c", simscsi_root, 'a' + target_id);
228			desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS,
229						   0, 0, SSC_OPEN);
230			if (desc[target_id] < 0) {
231				/* disk doesn't exist... */
232				break;
233			}
234			buf = localbuf;
235			buf[0] = 0;	/* magnetic disk */
236			buf[1] = 0;	/* not a removable medium */
237			buf[2] = 2;	/* SCSI-2 compliant device */
238			buf[3] = 2;	/* SCSI-2 response data format */
239			buf[4] = 31;	/* additional length (bytes) */
240			buf[5] = 0;	/* reserved */
241			buf[6] = 0;	/* reserved */
242			buf[7] = 0;	/* various flags */
243			memcpy(buf + 8, "HP      SIMULATED DISK  0.00",  28);
244			scsi_sg_copy_from_buffer(sc, buf, 36);
245			sc->result = GOOD;
246			break;
247
248		      case TEST_UNIT_READY:
249			sc->result = GOOD;
250			break;
251
252		      case READ_6:
253			if (desc[target_id] < 0 )
254				break;
255			simscsi_readwrite6(sc, SSC_READ);
256			break;
257
258		      case READ_10:
259			if (desc[target_id] < 0 )
260				break;
261			simscsi_readwrite10(sc, SSC_READ);
262			break;
263
264		      case WRITE_6:
265			if (desc[target_id] < 0)
266				break;
267			simscsi_readwrite6(sc, SSC_WRITE);
268			break;
269
270		      case WRITE_10:
271			if (desc[target_id] < 0)
272				break;
273			simscsi_readwrite10(sc, SSC_WRITE);
274			break;
275
276		      case READ_CAPACITY:
277			if (desc[target_id] < 0 || scsi_bufflen(sc) < 8) {
278				break;
279			}
280			buf = localbuf;
281			disk_size = simscsi_get_disk_size(desc[target_id]);
282
283			buf[0] = (disk_size >> 24) & 0xff;
284			buf[1] = (disk_size >> 16) & 0xff;
285			buf[2] = (disk_size >>  8) & 0xff;
286			buf[3] = (disk_size >>  0) & 0xff;
287			/* set block size of 512 bytes: */
288			buf[4] = 0;
289			buf[5] = 0;
290			buf[6] = 2;
291			buf[7] = 0;
292			scsi_sg_copy_from_buffer(sc, buf, 8);
293			sc->result = GOOD;
294			break;
295
296		      case MODE_SENSE:
297		      case MODE_SENSE_10:
298			/* sd.c uses this to determine whether disk does write-caching. */
299			scsi_sg_copy_from_buffer(sc, (char *)empty_zero_page,
300						 PAGE_SIZE);
301			sc->result = GOOD;
302			break;
303
304		      case START_STOP:
305			printk(KERN_ERR "START_STOP\n");
306			break;
307
308		      default:
309			panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]);
310		}
311	}
312	if (sc->result == DID_BAD_TARGET) {
313		sc->result |= DRIVER_SENSE << 24;
314		sc->sense_buffer[0] = 0x70;
315		sc->sense_buffer[2] = 0x00;
316	}
317	if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) {
318		panic("Attempt to queue command while command is pending!!");
319	}
320	atomic_inc(&num_reqs);
321	queue[wr].sc = sc;
322	wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN;
323
324	tasklet_schedule(&simscsi_tasklet);
325	return 0;
326}
327
328static int
329simscsi_host_reset (struct scsi_cmnd *sc)
330{
331	printk(KERN_ERR "simscsi_host_reset: not implemented\n");
332	return 0;
333}
334
335static struct scsi_host_template driver_template = {
336	.name			= "simulated SCSI host adapter",
337	.proc_name		= "simscsi",
338	.queuecommand		= simscsi_queuecommand,
339	.eh_host_reset_handler	= simscsi_host_reset,
340	.bios_param		= simscsi_biosparam,
341	.can_queue		= SIMSCSI_REQ_QUEUE_LEN,
342	.this_id		= -1,
343	.sg_tablesize		= SG_ALL,
344	.max_sectors		= 1024,
345	.cmd_per_lun		= SIMSCSI_REQ_QUEUE_LEN,
346	.use_clustering		= DISABLE_CLUSTERING,
347};
348
349static int __init
350simscsi_init(void)
351{
352	int error;
353
354	host = scsi_host_alloc(&driver_template, 0);
355	if (!host)
356		return -ENOMEM;
357
358	error = scsi_add_host(host, NULL);
359	if (error)
360		goto free_host;
361	scsi_scan_host(host);
362	return 0;
363
364 free_host:
365	scsi_host_put(host);
366	return error;
367}
368
369static void __exit
370simscsi_exit(void)
371{
372	scsi_remove_host(host);
373	scsi_host_put(host);
374}
375
376module_init(simscsi_init);
377module_exit(simscsi_exit);
378