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> |
Peter Chubb | 7b3166d | 2007-08-21 13:57:01 +1000 | [diff] [blame] | 17 | #include "hpsim_ssc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 48 | static struct Scsi_Host *host; |
| 49 | |
| 50 | static void simscsi_interrupt (unsigned long val); |
| 51 | static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0); |
| 52 | |
| 53 | struct disk_req { |
| 54 | unsigned long addr; |
| 55 | unsigned len; |
| 56 | }; |
| 57 | |
| 58 | struct disk_stat { |
| 59 | int fd; |
| 60 | unsigned count; |
| 61 | }; |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | struct 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 |
| 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 |
| 99 | simscsi_interrupt (unsigned long val) |
| 100 | { |
| 101 | struct scsi_cmnd *sc; |
| 102 | |
Yoann Padioleau | 1e4dcd2 | 2007-07-27 11:44:42 +0200 | [diff] [blame] | 103 | while ((sc = queue[rd].sc) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | atomic_dec(&num_reqs); |
Al Viro | cfa7fd7 | 2006-10-10 22:46:17 +0100 | [diff] [blame] | 105 | queue[rd].sc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (DBG) |
| 107 | printk("simscsi_interrupt: done with %ld\n", sc->serial_number); |
| 108 | (*sc->scsi_done)(sc); |
| 109 | rd = (rd + 1) % SIMSCSI_REQ_QUEUE_LEN; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | simscsi_biosparam (struct scsi_device *sdev, struct block_device *n, |
| 115 | sector_t capacity, int ip[]) |
| 116 | { |
| 117 | ip[0] = 64; /* heads */ |
| 118 | ip[1] = 32; /* sectors */ |
| 119 | ip[2] = capacity >> 11; /* cylinders */ |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset) |
| 125 | { |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 126 | int i; |
| 127 | struct scatterlist *sl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct disk_stat stat; |
| 129 | struct disk_req req; |
| 130 | |
| 131 | stat.fd = desc[sc->device->id]; |
| 132 | |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 133 | scsi_for_each_sg(sc, sl, scsi_sg_count(sc), i) { |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame^] | 134 | req.addr = __pa(sg_virt(sl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | req.len = sl->length; |
| 136 | if (DBG) |
| 137 | printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n", |
| 138 | mode == SSC_READ ? "read":"write", req.addr, offset, |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 139 | scsi_sg_count(sc) - i, sl->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | ia64_ssc(stat.fd, 1, __pa(&req), offset, mode); |
| 141 | ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION); |
| 142 | |
| 143 | /* should not happen in our case */ |
| 144 | if (stat.count != req.len) { |
| 145 | sc->result = DID_ERROR << 16; |
| 146 | return; |
| 147 | } |
| 148 | offset += sl->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | sc->result = GOOD; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * function handling both READ_6/WRITE_6 (non-scatter/gather mode) |
| 155 | * commands. |
| 156 | * Added 02/26/99 S.Eranian |
| 157 | */ |
| 158 | static void |
| 159 | simscsi_readwrite6 (struct scsi_cmnd *sc, int mode) |
| 160 | { |
| 161 | unsigned long offset; |
| 162 | |
| 163 | offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512; |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 164 | simscsi_sg_readwrite(sc, mode, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static size_t |
| 168 | simscsi_get_disk_size (int fd) |
| 169 | { |
| 170 | struct disk_stat stat; |
| 171 | size_t bit, sectors = 0; |
| 172 | struct disk_req req; |
| 173 | char buf[512]; |
| 174 | |
| 175 | /* |
Peter Chubb | b6a7e1e | 2005-10-20 12:31:19 +1000 | [diff] [blame] | 176 | * This is a bit kludgey: the simulator doesn't provide a |
| 177 | * direct way of determining the disk size, so we do a binary |
| 178 | * search, assuming a maximum disk size of 128GB. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | */ |
Peter Chubb | b6a7e1e | 2005-10-20 12:31:19 +1000 | [diff] [blame] | 180 | for (bit = (128UL << 30)/512; bit != 0; bit >>= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | req.addr = __pa(&buf); |
| 182 | req.len = sizeof(buf); |
| 183 | ia64_ssc(fd, 1, __pa(&req), ((sectors | bit) - 1)*512, SSC_READ); |
| 184 | stat.fd = fd; |
| 185 | ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION); |
| 186 | if (stat.count == sizeof(buf)) |
| 187 | sectors |= bit; |
| 188 | } |
| 189 | return sectors - 1; /* return last valid sector number */ |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | simscsi_readwrite10 (struct scsi_cmnd *sc, int mode) |
| 194 | { |
| 195 | unsigned long offset; |
| 196 | |
Peter Chubb | b6a7e1e | 2005-10-20 12:31:19 +1000 | [diff] [blame] | 197 | offset = (((unsigned long)sc->cmnd[2] << 24) |
| 198 | | ((unsigned long)sc->cmnd[3] << 16) |
| 199 | | ((unsigned long)sc->cmnd[4] << 8) |
| 200 | | ((unsigned long)sc->cmnd[5] << 0))*512UL; |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 201 | simscsi_sg_readwrite(sc, mode, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 204 | static void simscsi_fillresult(struct scsi_cmnd *sc, char *buf, unsigned len) |
| 205 | { |
| 206 | |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 207 | int i; |
| 208 | unsigned thislen; |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 209 | struct scatterlist *slp; |
| 210 | |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 211 | scsi_for_each_sg(sc, slp, scsi_sg_count(sc), i) { |
| 212 | if (!len) |
| 213 | break; |
| 214 | thislen = min(len, slp->length); |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame^] | 215 | memcpy(sg_virt(slp), buf, thislen); |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 216 | len -= thislen; |
| 217 | } |
| 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | static int |
| 221 | simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 222 | { |
| 223 | unsigned int target_id = sc->device->id; |
| 224 | char fname[MAX_ROOT_LEN+16]; |
| 225 | size_t disk_size; |
| 226 | char *buf; |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 227 | char localbuf[36]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | #if DEBUG_SIMSCSI |
| 229 | register long sp asm ("sp"); |
| 230 | |
| 231 | if (DBG) |
| 232 | printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n", |
| 233 | target_id, sc->cmnd[0], sc->serial_number, sp, done); |
| 234 | #endif |
| 235 | |
| 236 | sc->result = DID_BAD_TARGET << 16; |
| 237 | sc->scsi_done = done; |
| 238 | if (target_id <= 15 && sc->device->lun == 0) { |
| 239 | switch (sc->cmnd[0]) { |
| 240 | case INQUIRY: |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 241 | if (scsi_bufflen(sc) < 35) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | sprintf (fname, "%s%c", simscsi_root, 'a' + target_id); |
| 245 | desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS, |
| 246 | 0, 0, SSC_OPEN); |
| 247 | if (desc[target_id] < 0) { |
| 248 | /* disk doesn't exist... */ |
| 249 | break; |
| 250 | } |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 251 | buf = localbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | buf[0] = 0; /* magnetic disk */ |
| 253 | buf[1] = 0; /* not a removable medium */ |
| 254 | buf[2] = 2; /* SCSI-2 compliant device */ |
| 255 | buf[3] = 2; /* SCSI-2 response data format */ |
| 256 | buf[4] = 31; /* additional length (bytes) */ |
| 257 | buf[5] = 0; /* reserved */ |
| 258 | buf[6] = 0; /* reserved */ |
| 259 | buf[7] = 0; /* various flags */ |
| 260 | memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28); |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 261 | simscsi_fillresult(sc, buf, 36); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | sc->result = GOOD; |
| 263 | break; |
| 264 | |
| 265 | case TEST_UNIT_READY: |
| 266 | sc->result = GOOD; |
| 267 | break; |
| 268 | |
| 269 | case READ_6: |
| 270 | if (desc[target_id] < 0 ) |
| 271 | break; |
| 272 | simscsi_readwrite6(sc, SSC_READ); |
| 273 | break; |
| 274 | |
| 275 | case READ_10: |
| 276 | if (desc[target_id] < 0 ) |
| 277 | break; |
| 278 | simscsi_readwrite10(sc, SSC_READ); |
| 279 | break; |
| 280 | |
| 281 | case WRITE_6: |
| 282 | if (desc[target_id] < 0) |
| 283 | break; |
| 284 | simscsi_readwrite6(sc, SSC_WRITE); |
| 285 | break; |
| 286 | |
| 287 | case WRITE_10: |
| 288 | if (desc[target_id] < 0) |
| 289 | break; |
| 290 | simscsi_readwrite10(sc, SSC_WRITE); |
| 291 | break; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | case READ_CAPACITY: |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 294 | if (desc[target_id] < 0 || scsi_bufflen(sc) < 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | break; |
| 296 | } |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 297 | buf = localbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | disk_size = simscsi_get_disk_size(desc[target_id]); |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | buf[0] = (disk_size >> 24) & 0xff; |
| 301 | buf[1] = (disk_size >> 16) & 0xff; |
| 302 | buf[2] = (disk_size >> 8) & 0xff; |
| 303 | buf[3] = (disk_size >> 0) & 0xff; |
| 304 | /* set block size of 512 bytes: */ |
| 305 | buf[4] = 0; |
| 306 | buf[5] = 0; |
| 307 | buf[6] = 2; |
| 308 | buf[7] = 0; |
Peter Chubb | 83a78d9 | 2005-09-19 09:36:12 +1000 | [diff] [blame] | 309 | simscsi_fillresult(sc, buf, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | sc->result = GOOD; |
| 311 | break; |
| 312 | |
| 313 | case MODE_SENSE: |
| 314 | case MODE_SENSE_10: |
| 315 | /* sd.c uses this to determine whether disk does write-caching. */ |
FUJITA Tomonori | fd3adb2 | 2007-07-23 09:40:54 +0900 | [diff] [blame] | 316 | simscsi_fillresult(sc, (char *)empty_zero_page, scsi_bufflen(sc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | sc->result = GOOD; |
| 318 | break; |
| 319 | |
| 320 | case START_STOP: |
| 321 | printk(KERN_ERR "START_STOP\n"); |
| 322 | break; |
| 323 | |
| 324 | default: |
| 325 | panic("simscsi: unknown SCSI command %u\n", sc->cmnd[0]); |
| 326 | } |
| 327 | } |
| 328 | if (sc->result == DID_BAD_TARGET) { |
| 329 | sc->result |= DRIVER_SENSE << 24; |
| 330 | sc->sense_buffer[0] = 0x70; |
| 331 | sc->sense_buffer[2] = 0x00; |
| 332 | } |
| 333 | if (atomic_read(&num_reqs) >= SIMSCSI_REQ_QUEUE_LEN) { |
| 334 | panic("Attempt to queue command while command is pending!!"); |
| 335 | } |
| 336 | atomic_inc(&num_reqs); |
| 337 | queue[wr].sc = sc; |
| 338 | wr = (wr + 1) % SIMSCSI_REQ_QUEUE_LEN; |
| 339 | |
| 340 | tasklet_schedule(&simscsi_tasklet); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | simscsi_host_reset (struct scsi_cmnd *sc) |
| 346 | { |
| 347 | printk(KERN_ERR "simscsi_host_reset: not implemented\n"); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static struct scsi_host_template driver_template = { |
| 352 | .name = "simulated SCSI host adapter", |
| 353 | .proc_name = "simscsi", |
| 354 | .queuecommand = simscsi_queuecommand, |
| 355 | .eh_host_reset_handler = simscsi_host_reset, |
| 356 | .bios_param = simscsi_biosparam, |
| 357 | .can_queue = SIMSCSI_REQ_QUEUE_LEN, |
| 358 | .this_id = -1, |
| 359 | .sg_tablesize = SG_ALL, |
| 360 | .max_sectors = 1024, |
| 361 | .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN, |
| 362 | .use_clustering = DISABLE_CLUSTERING, |
FUJITA Tomonori | 9cb83c7 | 2007-10-16 11:24:32 +0200 | [diff] [blame] | 363 | .use_sg_chaining = ENABLE_SG_CHAINING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Matthew Wilcox | a60ebc5 | 2007-08-15 12:56:54 -0600 | [diff] [blame] | 376 | if (error) |
| 377 | goto free_host; |
| 378 | scsi_scan_host(host); |
| 379 | return 0; |
| 380 | |
| 381 | free_host: |
| 382 | scsi_host_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |