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 47static struct Scsi_Host *host; 48 49static void simscsi_interrupt (unsigned long val); 50static DECLARE_TASKLET(simscsi_tasklet, simscsi_interrupt, 0); 51 52struct disk_req { 53 unsigned long addr; 54 unsigned len; 55}; 56 57struct disk_stat { 58 int fd; 59 unsigned count; 60}; 61 62extern long ia64_ssc (long arg0, long arg1, long arg2, long arg3, int nr); 63 64static int desc[16] = { 65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 66}; 67 68static struct queue_entry { 69 struct scsi_cmnd *sc; 70} queue[SIMSCSI_REQ_QUEUE_LEN]; 71 72static int rd, wr; 73static atomic_t num_reqs = ATOMIC_INIT(0); 74 75/* base name for default disks */ 76static 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 */ 85static int __init 86simscsi_setup (char *s) 87{ 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 98static void 99simscsi_interrupt (unsigned long val) 100{ 101 struct scsi_cmnd *sc; 102 103 while ((sc = queue[rd].sc) != 0) { 104 atomic_dec(&num_reqs); 105 queue[rd].sc = NULL; 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 113static int 114simscsi_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 123static void 124simscsi_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset, unsigned long len) 125{ 126 struct disk_stat stat; 127 struct disk_req req; 128 129 req.addr = __pa(sc->request_buffer); 130 req.len = len; /* # of bytes to transfer */ 131 132 if (sc->request_bufflen < req.len) 133 return; 134 135 stat.fd = desc[sc->device->id]; 136 if (DBG) 137 printk("simscsi_%s @ %lx (off %lx)\n", 138 mode == SSC_READ ? "read":"write", req.addr, offset); 139 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode); 140 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION); 141 142 if (stat.count == req.len) { 143 sc->result = GOOD; 144 } else { 145 sc->result = DID_ERROR << 16; 146 } 147} 148 149static void 150simscsi_sg_readwrite (struct scsi_cmnd *sc, int mode, unsigned long offset) 151{ 152 int list_len = sc->use_sg; 153 struct scatterlist *sl = (struct scatterlist *)sc->request_buffer; 154 struct disk_stat stat; 155 struct disk_req req; 156 157 stat.fd = desc[sc->device->id]; 158 159 while (list_len) { 160 req.addr = __pa(page_address(sl->page) + sl->offset); 161 req.len = sl->length; 162 if (DBG) 163 printk("simscsi_sg_%s @ %lx (off %lx) use_sg=%d len=%d\n", 164 mode == SSC_READ ? "read":"write", req.addr, offset, 165 list_len, sl->length); 166 ia64_ssc(stat.fd, 1, __pa(&req), offset, mode); 167 ia64_ssc(__pa(&stat), 0, 0, 0, SSC_WAIT_COMPLETION); 168 169 /* should not happen in our case */ 170 if (stat.count != req.len) { 171 sc->result = DID_ERROR << 16; 172 return; 173 } 174 offset += sl->length; 175 sl++; 176 list_len--; 177 } 178 sc->result = GOOD; 179} 180 181/* 182 * function handling both READ_6/WRITE_6 (non-scatter/gather mode) 183 * commands. 184 * Added 02/26/99 S.Eranian 185 */ 186static void 187simscsi_readwrite6 (struct scsi_cmnd *sc, int mode) 188{ 189 unsigned long offset; 190 191 offset = (((sc->cmnd[1] & 0x1f) << 16) | (sc->cmnd[2] << 8) | sc->cmnd[3])*512; 192 if (sc->use_sg > 0) 193 simscsi_sg_readwrite(sc, mode, offset); 194 else 195 simscsi_readwrite(sc, mode, offset, sc->cmnd[4]*512); 196} 197 198static size_t 199simscsi_get_disk_size (int fd) 200{ 201 struct disk_stat stat; 202 size_t bit, sectors = 0; 203 struct disk_req req; 204 char buf[512]; 205 206 /* 207 * This is a bit kludgey: the simulator doesn't provide a 208 * direct way of determining the disk size, so we do a binary 209 * search, assuming a maximum disk size of 128GB. 210 */ 211 for (bit = (128UL << 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 223static void 224simscsi_readwrite10 (struct scsi_cmnd *sc, int mode) 225{ 226 unsigned long offset; 227 228 offset = (((unsigned long)sc->cmnd[2] << 24) 229 | ((unsigned long)sc->cmnd[3] << 16) 230 | ((unsigned long)sc->cmnd[4] << 8) 231 | ((unsigned long)sc->cmnd[5] << 0))*512UL; 232 if (sc->use_sg > 0) 233 simscsi_sg_readwrite(sc, mode, offset); 234 else 235 simscsi_readwrite(sc, mode, offset, ((sc->cmnd[7] << 8) | sc->cmnd[8])*512); 236} 237 238static void simscsi_fillresult(struct scsi_cmnd *sc, char *buf, unsigned len) 239{ 240 241 int scatterlen = sc->use_sg; 242 struct scatterlist *slp; 243 244 if (scatterlen == 0) 245 memcpy(sc->request_buffer, buf, len); 246 else for (slp = (struct scatterlist *)sc->request_buffer; 247 scatterlen-- > 0 && len > 0; slp++) { 248 unsigned thislen = min(len, slp->length); 249 250 memcpy(page_address(slp->page) + slp->offset, buf, thislen); 251 slp++; 252 len -= thislen; 253 } 254} 255 256static int 257simscsi_queuecommand (struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) 258{ 259 unsigned int target_id = sc->device->id; 260 char fname[MAX_ROOT_LEN+16]; 261 size_t disk_size; 262 char *buf; 263 char localbuf[36]; 264#if DEBUG_SIMSCSI 265 register long sp asm ("sp"); 266 267 if (DBG) 268 printk("simscsi_queuecommand: target=%d,cmnd=%u,sc=%lu,sp=%lx,done=%p\n", 269 target_id, sc->cmnd[0], sc->serial_number, sp, done); 270#endif 271 272 sc->result = DID_BAD_TARGET << 16; 273 sc->scsi_done = done; 274 if (target_id <= 15 && sc->device->lun == 0) { 275 switch (sc->cmnd[0]) { 276 case INQUIRY: 277 if (sc->request_bufflen < 35) { 278 break; 279 } 280 sprintf (fname, "%s%c", simscsi_root, 'a' + target_id); 281 desc[target_id] = ia64_ssc(__pa(fname), SSC_READ_ACCESS|SSC_WRITE_ACCESS, 282 0, 0, SSC_OPEN); 283 if (desc[target_id] < 0) { 284 /* disk doesn't exist... */ 285 break; 286 } 287 buf = localbuf; 288 buf[0] = 0; /* magnetic disk */ 289 buf[1] = 0; /* not a removable medium */ 290 buf[2] = 2; /* SCSI-2 compliant device */ 291 buf[3] = 2; /* SCSI-2 response data format */ 292 buf[4] = 31; /* additional length (bytes) */ 293 buf[5] = 0; /* reserved */ 294 buf[6] = 0; /* reserved */ 295 buf[7] = 0; /* various flags */ 296 memcpy(buf + 8, "HP SIMULATED DISK 0.00", 28); 297 simscsi_fillresult(sc, buf, 36); 298 sc->result = GOOD; 299 break; 300 301 case TEST_UNIT_READY: 302 sc->result = GOOD; 303 break; 304 305 case READ_6: 306 if (desc[target_id] < 0 ) 307 break; 308 simscsi_readwrite6(sc, SSC_READ); 309 break; 310 311 case READ_10: 312 if (desc[target_id] < 0 ) 313 break; 314 simscsi_readwrite10(sc, SSC_READ); 315 break; 316 317 case WRITE_6: 318 if (desc[target_id] < 0) 319 break; 320 simscsi_readwrite6(sc, SSC_WRITE); 321 break; 322 323 case WRITE_10: 324 if (desc[target_id] < 0) 325 break; 326 simscsi_readwrite10(sc, SSC_WRITE); 327 break; 328 329 case READ_CAPACITY: 330 if (desc[target_id] < 0 || sc->request_bufflen < 8) { 331 break; 332 } 333 buf = localbuf; 334 disk_size = simscsi_get_disk_size(desc[target_id]); 335 336 buf[0] = (disk_size >> 24) & 0xff; 337 buf[1] = (disk_size >> 16) & 0xff; 338 buf[2] = (disk_size >> 8) & 0xff; 339 buf[3] = (disk_size >> 0) & 0xff; 340 /* set block size of 512 bytes: */ 341 buf[4] = 0; 342 buf[5] = 0; 343 buf[6] = 2; 344 buf[7] = 0; 345 simscsi_fillresult(sc, buf, 8); 346 sc->result = GOOD; 347 break; 348 349 case MODE_SENSE: 350 case MODE_SENSE_10: 351 /* sd.c uses this to determine whether disk does write-caching. */ 352 simscsi_fillresult(sc, (char *)empty_zero_page, sc->request_bufflen); 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 380static int 381simscsi_host_reset (struct scsi_cmnd *sc) 382{ 383 printk(KERN_ERR "simscsi_host_reset: not implemented\n"); 384 return 0; 385} 386 387static struct scsi_host_template driver_template = { 388 .name = "simulated SCSI host adapter", 389 .proc_name = "simscsi", 390 .queuecommand = simscsi_queuecommand, 391 .eh_host_reset_handler = simscsi_host_reset, 392 .bios_param = simscsi_biosparam, 393 .can_queue = SIMSCSI_REQ_QUEUE_LEN, 394 .this_id = -1, 395 .sg_tablesize = SG_ALL, 396 .max_sectors = 1024, 397 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN, 398 .use_clustering = DISABLE_CLUSTERING, 399}; 400 401static int __init 402simscsi_init(void) 403{ 404 int error; 405 406 host = scsi_host_alloc(&driver_template, 0); 407 if (!host) 408 return -ENOMEM; 409 410 error = scsi_add_host(host, NULL); 411 if (!error) 412 scsi_scan_host(host); 413 return error; 414} 415 416static void __exit 417simscsi_exit(void) 418{ 419 scsi_remove_host(host); 420 scsi_host_put(host); 421} 422 423module_init(simscsi_init); 424module_exit(simscsi_exit); 425