Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) != 0) { |
| 105 | atomic_dec(&num_reqs); |
| 106 | queue[rd].sc = 0; |
| 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_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset, unsigned long len) |
| 126 | { |
| 127 | struct disk_stat stat; |
| 128 | struct disk_req req; |
| 129 | |
| 130 | req.addr = __pa(sc->request_buffer); |
| 131 | req.len = len; /* # of bytes to transfer */ |
| 132 | |
| 133 | if (sc->request_bufflen < req.len) |
| 134 | return; |
| 135 | |
| 136 | stat.fd = desc[sc->device->id]; |
| 137 | if (DBG) |
| 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); |
| 142 | |
| 143 | if (stat.count == req.len) { |
| 144 | sc->result = GOOD; |
| 145 | } else { |
| 146 | sc->result = DID_ERROR << 16; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset) |
| 152 | { |
| 153 | int list_len = sc->use_sg; |
| 154 | struct scatterlist *sl = (struct scatterlist *)sc->buffer; |
| 155 | struct disk_stat stat; |
| 156 | struct disk_req req; |
| 157 | |
| 158 | stat.fd = desc[sc->device->id]; |
| 159 | |
| 160 | while (list_len) { |
| 161 | req.addr = __pa(page_address(sl->page) + sl->offset); |
| 162 | req.len = sl->length; |
| 163 | if (DBG) |
| 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); |
| 169 | |
| 170 | /* should not happen in our case */ |
| 171 | if (stat.count != req.len) { |
| 172 | sc->result = DID_ERROR << 16; |
| 173 | return; |
| 174 | } |
| 175 | offset += sl->length; |
| 176 | sl++; |
| 177 | list_len--; |
| 178 | } |
| 179 | sc->result = GOOD; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * function handling both READ_6/WRITE_6 (non-scatter/gather mode) |
| 184 | * commands. |
| 185 | * Added 02/26/99 S.Eranian |
| 186 | */ |
| 187 | static void |
| 188 | simscsi_readwrite6 (struct scsi_cmnd *sc, int mode) |
| 189 | { |
| 190 | unsigned long offset; |
| 191 | |
| 192 | offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512; |
| 193 | if (sc->use_sg > 0) |
| 194 | simscsi_sg_readwrite(sc, mode, offset); |
| 195 | else |
| 196 | simscsi_readwrite(sc, mode, offset, sc->cmnd[4]*512); |
| 197 | } |
| 198 | |
| 199 | static size_t |
| 200 | simscsi_get_disk_size (int fd) |
| 201 | { |
| 202 | struct disk_stat stat; |
| 203 | size_t bit, sectors = 0; |
| 204 | struct disk_req req; |
| 205 | char buf[512]; |
| 206 | |
| 207 | /* |
| 208 | * This is a bit kludgey: the simulator doesn't provide a direct way of determining |
| 209 | * the disk size, so we do a binary search, assuming a maximum disk size of 4GB. |
| 210 | */ |
| 211 | for (bit = (4UL << 30)/512; bit != 0; bit >>= 1) { |
| 212 | req.addr = __pa(&buf); |
| 213 | req.len = sizeof(buf); |
| 214 | ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ); |
| 215 | stat.fd = fd; |
| 216 | ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION); |
| 217 | if (stat.count == sizeof(buf)) |
| 218 | sectors |= bit; |
| 219 | } |
| 220 | return sectors - 1; /* return last valid sector number */ |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | simscsi_readwrite10 (struct scsi_cmnd *sc, int mode) |
| 225 | { |
| 226 | unsigned long offset; |
| 227 | |
| 228 | offset = ( (sc->cmnd[2] << 24) | (sc->cmnd[3] << 16) |
| 229 | | (sc->cmnd[4] << 8) | (sc->cmnd[5] << 0))*512; |
| 230 | if (sc->use_sg > 0) |
| 231 | simscsi_sg_readwrite(sc, mode, offset); |
| 232 | else |
| 233 | simscsi_readwrite(sc, mode, offset, ((sc->cmnd[7] << 8) | sc->cmnd[8])*512); |
| 234 | } |
| 235 | |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 236 | static void simscsi_fillresult(struct scsi_cmnd *sc, char *buf, unsigned len) |
| 237 | { |
| 238 | |
| 239 | int scatterlen = sc->use_sg; |
| 240 | struct scatterlist *slp; |
| 241 | |
| 242 | if (scatterlen == 0) |
| 243 | memcpy(sc->request_buffer, buf, len); |
| 244 | else for (slp = (struct scatterlist *)sc->buffer; scatterlen-- > 0 && len > 0; slp++) { |
| 245 | unsigned thislen = min(len, slp->length); |
| 246 | |
| 247 | memcpy(page_address(slp->page) + slp->offset, buf, thislen); |
| 248 | slp++; |
| 249 | len -= thislen; |
| 250 | } |
| 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static int |
| 254 | simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 255 | { |
| 256 | unsigned int target_id = sc->device->id; |
| 257 | char fname[MAX_ROOT_LEN+16]; |
| 258 | size_t disk_size; |
| 259 | char *buf; |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 260 | char localbuf[36]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | #if DEBUG_SIMSCSI |
| 262 | register long sp asm ("sp"); |
| 263 | |
| 264 | if (DBG) |
| 265 | printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n", |
| 266 | target_id, sc->cmnd[0], sc->serial_number, sp, done); |
| 267 | #endif |
| 268 | |
| 269 | sc->result = DID_BAD_TARGET << 16; |
| 270 | sc->scsi_done = done; |
| 271 | if (target_id <= 15 && sc->device->lun == 0) { |
| 272 | switch (sc->cmnd[0]) { |
| 273 | case INQUIRY: |
| 274 | if (sc->request_bufflen < 35) { |
| 275 | break; |
| 276 | } |
| 277 | sprintf (fname, "%s%c", simscsi_root, 'a' + target_id); |
| 278 | desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS, |
| 279 | 0, 0, SSC_OPEN); |
| 280 | if (desc[target_id] < 0) { |
| 281 | /* disk doesn't exist... */ |
| 282 | break; |
| 283 | } |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 284 | buf = localbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | buf[0] = 0; /* magnetic disk */ |
| 286 | buf[1] = 0; /* not a removable medium */ |
| 287 | buf[2] = 2; /* SCSI-2 compliant device */ |
| 288 | buf[3] = 2; /* SCSI-2 response data format */ |
| 289 | buf[4] = 31; /* additional length (bytes) */ |
| 290 | buf[5] = 0; /* reserved */ |
| 291 | buf[6] = 0; /* reserved */ |
| 292 | buf[7] = 0; /* various flags */ |
| 293 | memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28); |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 294 | simscsi_fillresult(sc, buf, 36); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | sc->result = GOOD; |
| 296 | break; |
| 297 | |
| 298 | case TEST_UNIT_READY: |
| 299 | sc->result = GOOD; |
| 300 | break; |
| 301 | |
| 302 | case READ_6: |
| 303 | if (desc[target_id] < 0 ) |
| 304 | break; |
| 305 | simscsi_readwrite6(sc, SSC_READ); |
| 306 | break; |
| 307 | |
| 308 | case READ_10: |
| 309 | if (desc[target_id] < 0 ) |
| 310 | break; |
| 311 | simscsi_readwrite10(sc, SSC_READ); |
| 312 | break; |
| 313 | |
| 314 | case WRITE_6: |
| 315 | if (desc[target_id] < 0) |
| 316 | break; |
| 317 | simscsi_readwrite6(sc, SSC_WRITE); |
| 318 | break; |
| 319 | |
| 320 | case WRITE_10: |
| 321 | if (desc[target_id] < 0) |
| 322 | break; |
| 323 | simscsi_readwrite10(sc, SSC_WRITE); |
| 324 | break; |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | case READ_CAPACITY: |
| 327 | if (desc[target_id] < 0 || sc->request_bufflen < 8) { |
| 328 | break; |
| 329 | } |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 330 | buf = localbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | disk_size = simscsi_get_disk_size(desc[target_id]); |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | buf[0] = (disk_size >> 24) & 0xff; |
| 334 | buf[1] = (disk_size >> 16) & 0xff; |
| 335 | buf[2] = (disk_size >> 8) & 0xff; |
| 336 | buf[3] = (disk_size >> 0) & 0xff; |
| 337 | /* set block size of 512 bytes: */ |
| 338 | buf[4] = 0; |
| 339 | buf[5] = 0; |
| 340 | buf[6] = 2; |
| 341 | buf[7] = 0; |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 342 | simscsi_fillresult(sc, buf, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | sc->result = GOOD; |
| 344 | break; |
| 345 | |
| 346 | case MODE_SENSE: |
| 347 | case MODE_SENSE_10: |
| 348 | /* sd.c uses this to determine whether disk does write-caching. */ |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame^] | 349 | simscsi_fillresult(sc, (char *)empty_zero_page, sc->request_bufflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | sc->result = GOOD; |
| 351 | break; |
| 352 | |
| 353 | case START_STOP: |
| 354 | printk(KERN_ERR "START_STOP\n"); |
| 355 | break; |
| 356 | |
| 357 | default: |
| 358 | panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]); |
| 359 | } |
| 360 | } |
| 361 | if (sc->result == DID_BAD_TARGET) { |
| 362 | sc->result |= DRIVER_SENSE << 24; |
| 363 | sc->sense_buffer[0] = 0x70; |
| 364 | sc->sense_buffer[2] = 0x00; |
| 365 | } |
| 366 | if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) { |
| 367 | panic("Attempt to queue command while command is pending!!"); |
| 368 | } |
| 369 | atomic_inc(&num_reqs); |
| 370 | queue[wr].sc = sc; |
| 371 | wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN; |
| 372 | |
| 373 | tasklet_schedule(&simscsi_tasklet); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int |
| 378 | simscsi_host_reset (struct scsi_cmnd *sc) |
| 379 | { |
| 380 | printk(KERN_ERR "simscsi_host_reset: not implemented\n"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static struct scsi_host_template driver_template = { |
| 385 | .name = "simulated SCSI host adapter", |
| 386 | .proc_name = "simscsi", |
| 387 | .queuecommand = simscsi_queuecommand, |
| 388 | .eh_host_reset_handler = simscsi_host_reset, |
| 389 | .bios_param = simscsi_biosparam, |
| 390 | .can_queue = SIMSCSI_REQ_QUEUE_LEN, |
| 391 | .this_id = -1, |
| 392 | .sg_tablesize = SG_ALL, |
| 393 | .max_sectors = 1024, |
| 394 | .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN, |
| 395 | .use_clustering = DISABLE_CLUSTERING, |
| 396 | }; |
| 397 | |
| 398 | static int __init |
| 399 | simscsi_init(void) |
| 400 | { |
| 401 | int error; |
| 402 | |
| 403 | host = scsi_host_alloc(&driver_template, 0); |
| 404 | if (!host) |
| 405 | return -ENOMEM; |
| 406 | |
| 407 | error = scsi_add_host(host, NULL); |
| 408 | if (!error) |
| 409 | scsi_scan_host(host); |
| 410 | return error; |
| 411 | } |
| 412 | |
| 413 | static void __exit |
| 414 | simscsi_exit(void) |
| 415 | { |
| 416 | scsi_remove_host(host); |
| 417 | scsi_host_put(host); |
| 418 | } |
| 419 | |
| 420 | module_init(simscsi_init); |
| 421 | module_exit(simscsi_exit); |