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