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