~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to xen/drivers/scsi/sd.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *      sd.c Copyright (C) 1992 Drew Eckhardt
3
 
 *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4
 
 *
5
 
 *      Linux scsi disk driver
6
 
 *              Initial versions: Drew Eckhardt
7
 
 *              Subsequent revisions: Eric Youngdale
8
 
 *
9
 
 *      <drew@colorado.edu>
10
 
 *
11
 
 *       Modified by Eric Youngdale ericy@andante.org to
12
 
 *       add scatter-gather, multiple outstanding request, and other
13
 
 *       enhancements.
14
 
 *
15
 
 *       Modified by Eric Youngdale eric@andante.org to support loadable
16
 
 *       low-level scsi drivers.
17
 
 *
18
 
 *       Modified by Jirka Hanika geo@ff.cuni.cz to support more
19
 
 *       scsi disks using eight major numbers.
20
 
 *
21
 
 *       Modified by Richard Gooch rgooch@atnf.csiro.au to support devfs.
22
 
 *      
23
 
 *       Modified by Torben Mathiasen tmm@image.dk
24
 
 *       Resource allocation fixes in sd_init and cleanups.
25
 
 *      
26
 
 *       Modified by Alex Davis <letmein@erols.com>
27
 
 *       Fix problem where partition info not being read in sd_open.
28
 
 *      
29
 
 *       Modified by Alex Davis <letmein@erols.com>
30
 
 *       Fix problem where removable media could be ejected after sd_open.
31
 
 */
32
 
 
33
 
#include <xeno/config.h>
34
 
#include <xeno/module.h>
35
 
#include <xeno/sched.h>
36
 
#include <xeno/hdreg.h>
37
 
#include <xeno/init.h>
38
 
 
39
 
#include <asm/uaccess.h>
40
 
#include <asm/system.h>
41
 
#include <asm/io.h>
42
 
 
43
 
#define MAJOR_NR SCSI_DISK0_MAJOR
44
 
#include <xeno/blk.h>
45
 
#include <xeno/blkpg.h>
46
 
#include "scsi.h"
47
 
#include "hosts.h"
48
 
#include "sd.h"
49
 
#include <scsi/scsi_ioctl.h>
50
 
#include "constants.h"
51
 
#include <scsi/scsicam.h>       /* must follow "hosts.h" */
52
 
 
53
 
#include <xeno/genhd.h>
54
 
 
55
 
#include <asm/domain_page.h>    /* SMH: for [un_]map_domain_mem() */
56
 
 
57
 
/*
58
 
 *  static const char RCSid[] = "$Header:";
59
 
 */
60
 
 
61
 
/* system major --> sd_gendisks index */
62
 
#define SD_MAJOR_IDX(i)         (MAJOR(i) & SD_MAJOR_MASK)
63
 
/* sd_gendisks index --> system major */
64
 
#define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
65
 
 
66
 
#define SD_PARTITION(dev)       ((SD_MAJOR_IDX(dev) << 8) | (MINOR(dev) & 255))
67
 
 
68
 
#define SCSI_DISKS_PER_MAJOR    16
69
 
#define SD_MAJOR_NUMBER(i)      SD_MAJOR((i) >> 8)
70
 
#define SD_MINOR_NUMBER(i)      ((i) & 255)
71
 
#define MKDEV_SD_PARTITION(i)   MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
72
 
#define MKDEV_SD(index)         MKDEV_SD_PARTITION((index) << 4)
73
 
#define N_USED_SCSI_DISKS  (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
74
 
#define N_USED_SD_MAJORS   (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
75
 
 
76
 
#define MAX_RETRIES 5
77
 
 
78
 
/*
79
 
 *  Time out in seconds for disks and Magneto-opticals (which are slower).
80
 
 */
81
 
 
82
 
#define SD_TIMEOUT (30 * HZ)
83
 
#define SD_MOD_TIMEOUT (75 * HZ)
84
 
 
85
 
static Scsi_Disk *rscsi_disks;
86
 
static struct gendisk *sd_gendisks;
87
 
static int *sd_sizes;
88
 
static int *sd_blocksizes;
89
 
static int *sd_hardsizes;       /* Hardware sector size */
90
 
static int *sd_max_sectors;
91
 
 
92
 
static int check_scsidisk_media_change(kdev_t);
93
 
static int fop_revalidate_scsidisk(kdev_t);
94
 
 
95
 
static int sd_init_onedisk(int);
96
 
 
97
 
 
98
 
static int sd_init(void);
99
 
static void sd_finish(void);
100
 
static int sd_attach(Scsi_Device *);
101
 
static int sd_detect(Scsi_Device *);
102
 
static void sd_detach(Scsi_Device *);
103
 
static int sd_init_command(Scsi_Cmnd *);
104
 
 
105
 
static struct Scsi_Device_Template sd_template = {
106
 
        name:"disk",
107
 
        tag:"sd",
108
 
        scsi_type:TYPE_DISK,
109
 
        major:SCSI_DISK0_MAJOR,
110
 
        /*
111
 
         * Secondary range of majors that this driver handles.
112
 
         */
113
 
        min_major:SCSI_DISK1_MAJOR,
114
 
        max_major:SCSI_DISK7_MAJOR,
115
 
        blk:1,
116
 
        detect:sd_detect,
117
 
        init:sd_init,
118
 
        finish:sd_finish,
119
 
        attach:sd_attach,
120
 
        detach:sd_detach,
121
 
        init_command:sd_init_command,
122
 
};
123
 
 
124
 
 
125
 
static void rw_intr(Scsi_Cmnd * SCpnt);
126
 
 
127
 
#if defined(CONFIG_PPC)
128
 
/*
129
 
 * Moved from arch/ppc/pmac_setup.c.  This is where it really belongs.
130
 
 */
131
 
kdev_t __init
132
 
sd_find_target(void *host, int tgt)
133
 
{
134
 
    Scsi_Disk *dp;
135
 
    int i;
136
 
    for (dp = rscsi_disks, i = 0; i < sd_template.dev_max; ++i, ++dp)
137
 
        if (dp->device != NULL && dp->device->host == host
138
 
            && dp->device->id == tgt)
139
 
            return MKDEV_SD(i);
140
 
    return 0;
141
 
}
142
 
#endif
143
 
 
144
 
static int sd_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
145
 
{
146
 
        kdev_t dev = inode->i_rdev;
147
 
        struct Scsi_Host * host;
148
 
        Scsi_Device * SDev;
149
 
        int diskinfo[4];
150
 
    
151
 
        SDev = rscsi_disks[DEVICE_NR(dev)].device;
152
 
        if (!SDev)
153
 
                return -ENODEV;
154
 
 
155
 
        /*
156
 
         * If we are in the middle of error recovery, don't let anyone
157
 
         * else try and use this device.  Also, if error recovery fails, it
158
 
         * may try and take the device offline, in which case all further
159
 
         * access to the device is prohibited.
160
 
         */
161
 
 
162
 
        if( !scsi_block_when_processing_errors(SDev) )
163
 
        {
164
 
                return -ENODEV;
165
 
        }
166
 
 
167
 
        switch (cmd) 
168
 
        {
169
 
                case HDIO_GETGEO:   /* Return BIOS disk parameters */
170
 
                {
171
 
                        struct hd_geometry *loc = (struct hd_geometry *) arg;
172
 
                        if(!loc)
173
 
                                return -EINVAL;
174
 
 
175
 
                        host = rscsi_disks[DEVICE_NR(dev)].device->host;
176
 
        
177
 
                        /* default to most commonly used values */
178
 
        
179
 
                        diskinfo[0] = 0x40;
180
 
                        diskinfo[1] = 0x20;
181
 
                        diskinfo[2] = 
182
 
                            rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
183
 
        
184
 
                        /* override with calculated, extended default,
185
 
                           or driver values */
186
 
        
187
 
                        if(host->hostt->bios_param != NULL)
188
 
                                host->hostt->bios_param(
189
 
                                    &rscsi_disks[DEVICE_NR(dev)], dev,
190
 
                                    &diskinfo[0]);
191
 
                        else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
192
 
                                                dev, &diskinfo[0]);
193
 
 
194
 
                        if (put_user(diskinfo[0], &loc->heads) ||
195
 
                                put_user(diskinfo[1], &loc->sectors) ||
196
 
                                put_user(diskinfo[2], &loc->cylinders) ||
197
 
                                put_user(sd_gendisks[SD_MAJOR_IDX(
198
 
                                    inode->i_rdev)].part[MINOR(
199
 
                                    inode->i_rdev)].start_sect, &loc->start))
200
 
                                return -EFAULT;
201
 
                        return 0;
202
 
                }
203
 
                case HDIO_GETGEO_BIG:
204
 
                {
205
 
                        struct hd_big_geometry *loc = 
206
 
                            (struct hd_big_geometry *) arg;
207
 
 
208
 
                        if(!loc)
209
 
                                return -EINVAL;
210
 
 
211
 
                        host = rscsi_disks[DEVICE_NR(dev)].device->host;
212
 
 
213
 
                        /* default to most commonly used values */
214
 
 
215
 
                        diskinfo[0] = 0x40;
216
 
                        diskinfo[1] = 0x20;
217
 
                        diskinfo[2] = 
218
 
                            rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
219
 
 
220
 
                        /* override with calculated, extended default,
221
 
                           or driver values */
222
 
 
223
 
                        if(host->hostt->bios_param != NULL)
224
 
                                host->hostt->bios_param(
225
 
                                    &rscsi_disks[DEVICE_NR(dev)], dev,
226
 
                                    &diskinfo[0]);
227
 
                        else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
228
 
                                                dev, &diskinfo[0]);
229
 
 
230
 
                        if (put_user(diskinfo[0], &loc->heads) ||
231
 
                                put_user(diskinfo[1], &loc->sectors) ||
232
 
                                put_user(diskinfo[2], 
233
 
                                         (unsigned int *) &loc->cylinders) ||
234
 
                                put_user(sd_gendisks[SD_MAJOR_IDX(
235
 
                                    inode->i_rdev)].part[MINOR(
236
 
                                    inode->i_rdev)].start_sect, &loc->start))
237
 
                                return -EFAULT;
238
 
                        return 0;
239
 
                }
240
 
#if 0
241
 
                case BLKGETSIZE:
242
 
                case BLKGETSIZE64:
243
 
                case BLKROSET:
244
 
                case BLKROGET:
245
 
                case BLKRASET:
246
 
                case BLKRAGET:
247
 
                case BLKFLSBUF:
248
 
                case BLKSSZGET:
249
 
                case BLKPG:
250
 
                case BLKELVGET:
251
 
                case BLKELVSET:
252
 
                case BLKBSZGET:
253
 
                case BLKBSZSET:
254
 
                        return blk_ioctl(inode->i_rdev, cmd, arg);
255
 
 
256
 
                case BLKRRPART: /* Re-read partition tables */
257
 
                        if (!capable(CAP_SYS_ADMIN))
258
 
                                return -EACCES;
259
 
                        return revalidate_scsidisk(dev, 1);
260
 
#endif
261
 
 
262
 
                default:
263
 
                        return scsi_ioctl(rscsi_disks[DEVICE_NR(dev)].device, 
264
 
                                          cmd, (void *) arg);
265
 
        }
266
 
}
267
 
 
268
 
static void sd_devname(unsigned int disknum, char *buffer)
269
 
{
270
 
    if (disknum < 26)
271
 
        sprintf(buffer, "sd%c", 'a' + disknum);
272
 
    else {
273
 
        unsigned int min1;
274
 
        unsigned int min2;
275
 
        /*
276
 
         * For larger numbers of disks, we need to go to a new
277
 
         * naming scheme.
278
 
         */
279
 
        min1 = disknum / 26;
280
 
        min2 = disknum % 26;
281
 
        sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
282
 
    }
283
 
}
284
 
 
285
 
static request_queue_t *sd_find_queue(kdev_t dev)
286
 
{
287
 
    Scsi_Disk *dpnt;
288
 
    int target;
289
 
    target = DEVICE_NR(dev);
290
 
 
291
 
    dpnt = &rscsi_disks[target];
292
 
    if (!dpnt->device)
293
 
        return NULL;    /* No such device */
294
 
    return &dpnt->device->request_queue;
295
 
}
296
 
 
297
 
static int sd_init_command(Scsi_Cmnd * SCpnt)
298
 
{
299
 
    int dev, block, this_count;
300
 
    struct hd_struct *ppnt;
301
 
    Scsi_Disk *dpnt;
302
 
#if CONFIG_SCSI_LOGGING
303
 
    char nbuff[6];
304
 
#endif
305
 
 
306
 
    ppnt = &sd_gendisks[SD_MAJOR_IDX(SCpnt->request.rq_dev)].part[MINOR(SCpnt->request.rq_dev)];
307
 
    dev = DEVICE_NR(SCpnt->request.rq_dev);
308
 
 
309
 
    block = SCpnt->request.sector;
310
 
    this_count = SCpnt->request_bufflen >> 9;
311
 
 
312
 
    SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = 0x%x, block = %d\n",
313
 
                               SCpnt->request.rq_dev, block));
314
 
 
315
 
    dpnt = &rscsi_disks[dev];
316
 
    if (dev >= sd_template.dev_max ||
317
 
        !dpnt->device ||
318
 
        !dpnt->device->online ||
319
 
        block + SCpnt->request.nr_sectors > ppnt->nr_sects) {
320
 
 
321
 
        SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", 
322
 
                                   SCpnt->request.nr_sectors));
323
 
        SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
324
 
        return 0;
325
 
    }
326
 
    block += ppnt->start_sect;
327
 
    if (dpnt->device->changed) {
328
 
        /*
329
 
         * quietly refuse to do anything to a changed disc until the changed
330
 
         * bit has been reset
331
 
         */
332
 
        /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
333
 
        return 0;
334
 
    }
335
 
    SCSI_LOG_HLQUEUE(2, sd_devname(dev, nbuff));
336
 
    SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
337
 
                               nbuff, dev, block));
338
 
 
339
 
    /*
340
 
     * If we have a 1K hardware sectorsize, prevent access to single
341
 
     * 512 byte sectors.  In theory we could handle this - in fact
342
 
     * the scsi cdrom driver must be able to handle this because
343
 
     * we typically use 1K blocksizes, and cdroms typically have
344
 
     * 2K hardware sectorsizes.  Of course, things are simpler
345
 
     * with the cdrom, since it is read-only.  For performance
346
 
     * reasons, the filesystems should be able to handle this
347
 
     * and not force the scsi disk driver to use bounce buffers
348
 
     * for this.
349
 
     */
350
 
    if (dpnt->device->sector_size == 1024) {
351
 
        if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
352
 
            printk("sd.c:Bad block number requested");
353
 
            return 0;
354
 
        } else {
355
 
            block = block >> 1;
356
 
            this_count = this_count >> 1;
357
 
        }
358
 
    }
359
 
    if (dpnt->device->sector_size == 2048) {
360
 
        if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
361
 
            printk("sd.c:Bad block number requested");
362
 
            return 0;
363
 
        } else {
364
 
            block = block >> 2;
365
 
            this_count = this_count >> 2;
366
 
        }
367
 
    }
368
 
    if (dpnt->device->sector_size == 4096) {
369
 
        if ((block & 7) || (SCpnt->request.nr_sectors & 7)) {
370
 
            printk("sd.c:Bad block number requested");
371
 
            return 0;
372
 
        } else {
373
 
            block = block >> 3;
374
 
            this_count = this_count >> 3;
375
 
        }
376
 
    }
377
 
    switch (SCpnt->request.cmd) {
378
 
    case WRITE:
379
 
        if (!dpnt->device->writeable) {
380
 
            return 0;
381
 
        }
382
 
        SCpnt->cmnd[0] = WRITE_6;
383
 
        SCpnt->sc_data_direction = SCSI_DATA_WRITE;
384
 
        break;
385
 
    case READ:
386
 
        SCpnt->cmnd[0] = READ_6;
387
 
        SCpnt->sc_data_direction = SCSI_DATA_READ;
388
 
        break;
389
 
    default:
390
 
        panic("Unknown sd command %d\n", SCpnt->request.cmd);
391
 
    }
392
 
 
393
 
    SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", nbuff,
394
 
                               (SCpnt->request.cmd == WRITE) ? "writing" : 
395
 
                               "reading", this_count, 
396
 
                               SCpnt->request.nr_sectors));
397
 
 
398
 
    SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
399
 
        ((SCpnt->lun << 5) & 0xe0) : 0;
400
 
 
401
 
    if (((this_count > 0xff) || (block > 0x1fffff)) || SCpnt->device->ten) {
402
 
        if (this_count > 0xffff)
403
 
            this_count = 0xffff;
404
 
 
405
 
        SCpnt->cmnd[0] += READ_10 - READ_6;
406
 
        SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
407
 
        SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
408
 
        SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
409
 
        SCpnt->cmnd[5] = (unsigned char) block & 0xff;
410
 
        SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
411
 
        SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
412
 
        SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
413
 
    } else {
414
 
        if (this_count > 0xff)
415
 
            this_count = 0xff;
416
 
 
417
 
        SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
418
 
        SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
419
 
        SCpnt->cmnd[3] = (unsigned char) block & 0xff;
420
 
        SCpnt->cmnd[4] = (unsigned char) this_count;
421
 
        SCpnt->cmnd[5] = 0;
422
 
    }
423
 
 
424
 
    /*
425
 
     * We shouldn't disconnect in the middle of a sector, so with a dumb
426
 
     * host adapter, it's safe to assume that we can at least transfer
427
 
     * this many bytes between each connect / disconnect.
428
 
     */
429
 
    SCpnt->transfersize = dpnt->device->sector_size;
430
 
    SCpnt->underflow = this_count << 9;
431
 
 
432
 
    SCpnt->allowed = MAX_RETRIES;
433
 
    SCpnt->timeout_per_command = (SCpnt->device->type == TYPE_DISK ?
434
 
                                  SD_TIMEOUT : SD_MOD_TIMEOUT);
435
 
 
436
 
    /*
437
 
     * This is the completion routine we use.  This is matched in terms
438
 
     * of capability to this function.
439
 
     */
440
 
    SCpnt->done = rw_intr;
441
 
 
442
 
    /*
443
 
     * This indicates that the command is ready from our end to be
444
 
     * queued.
445
 
     */
446
 
    return 1;
447
 
}
448
 
 
449
 
static int sd_open(struct inode *inode, struct file *filp)
450
 
{
451
 
    int target, retval = -ENXIO;
452
 
    Scsi_Device * SDev;
453
 
    target = DEVICE_NR(inode->i_rdev);
454
 
 
455
 
    SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target, sd_template.dev_max));
456
 
 
457
 
    if (target >= sd_template.dev_max || !rscsi_disks[target].device)
458
 
        return -ENXIO;  /* No such device */
459
 
 
460
 
    /*
461
 
     * If the device is in error recovery, wait until it is done.
462
 
     * If the device is offline, then disallow any access to it.
463
 
     */
464
 
    if (!scsi_block_when_processing_errors(rscsi_disks[target].device)) {
465
 
        return -ENXIO;
466
 
    }
467
 
    /*
468
 
     * Make sure that only one process can do a check_change_disk at one time.
469
 
     * This is also used to lock out further access when the partition table
470
 
     * is being re-read.
471
 
     */
472
 
 
473
 
    while (rscsi_disks[target].device->busy) {
474
 
        barrier();
475
 
        cpu_relax();
476
 
    }
477
 
    /*
478
 
     * The following code can sleep.
479
 
     * Module unloading must be prevented
480
 
     */
481
 
    SDev = rscsi_disks[target].device;
482
 
    if (SDev->host->hostt->module)
483
 
        __MOD_INC_USE_COUNT(SDev->host->hostt->module);
484
 
    if (sd_template.module)
485
 
        __MOD_INC_USE_COUNT(sd_template.module);
486
 
    SDev->access_count++;
487
 
 
488
 
#if 0 
489
 
    if (rscsi_disks[target].device->removable) {
490
 
        SDev->allow_revalidate = 1;
491
 
        check_disk_change(inode->i_rdev);
492
 
        SDev->allow_revalidate = 0;
493
 
 
494
 
 
495
 
        /*
496
 
         * If the drive is empty, just let the open fail.
497
 
         */
498
 
        if ((!rscsi_disks[target].ready) && !(filp->f_flags & O_NDELAY)) {
499
 
            retval = -ENOMEDIUM;
500
 
            goto error_out;
501
 
        }
502
 
 
503
 
        /*
504
 
         * Similarly, if the device has the write protect tab set,
505
 
         * have the open fail if the user expects to be able to write
506
 
         * to the thing.
507
 
         */
508
 
        if ((rscsi_disks[target].write_prot) && (filp->f_mode & 2)) {
509
 
            retval = -EROFS;
510
 
            goto error_out;
511
 
        }
512
 
    }
513
 
#endif
514
 
 
515
 
    /*
516
 
     * It is possible that the disk changing stuff resulted in the device
517
 
     * being taken offline.  If this is the case, report this to the user,
518
 
     * and don't pretend that
519
 
     * the open actually succeeded.
520
 
     */
521
 
    if (!SDev->online) {
522
 
        goto error_out;
523
 
    }
524
 
    /*
525
 
     * See if we are requesting a non-existent partition.  Do this
526
 
     * after checking for disk change.
527
 
     */
528
 
    if (sd_sizes[SD_PARTITION(inode->i_rdev)] == 0) {
529
 
        goto error_out;
530
 
    }
531
 
 
532
 
    if (SDev->removable)
533
 
        if (SDev->access_count==1)
534
 
            if (scsi_block_when_processing_errors(SDev))
535
 
                scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, NULL);
536
 
 
537
 
        
538
 
    return 0;
539
 
 
540
 
 error_out:
541
 
    SDev->access_count--;
542
 
    if (SDev->host->hostt->module)
543
 
        __MOD_DEC_USE_COUNT(SDev->host->hostt->module);
544
 
    if (sd_template.module)
545
 
        __MOD_DEC_USE_COUNT(sd_template.module);
546
 
    return retval;      
547
 
}
548
 
 
549
 
static int sd_release(struct inode *inode, struct file *file)
550
 
{
551
 
    int target;
552
 
    Scsi_Device * SDev;
553
 
    
554
 
    target = DEVICE_NR(inode->i_rdev);
555
 
    SDev = rscsi_disks[target].device;
556
 
    if (!SDev)
557
 
        return -ENODEV;
558
 
    
559
 
    SDev->access_count--;
560
 
    
561
 
    if (SDev->removable) {
562
 
        if (!SDev->access_count)
563
 
            if (scsi_block_when_processing_errors(SDev))
564
 
                scsi_ioctl(SDev, SCSI_IOCTL_DOORUNLOCK, NULL);
565
 
    }
566
 
    if (SDev->host->hostt->module)
567
 
        __MOD_DEC_USE_COUNT(SDev->host->hostt->module);
568
 
    if (sd_template.module)
569
 
        __MOD_DEC_USE_COUNT(sd_template.module);
570
 
    return 0;
571
 
}
572
 
 
573
 
static struct block_device_operations sd_fops =
574
 
{
575
 
/*      owner:                  THIS_MODULE, */
576
 
        open:                   sd_open,
577
 
        release:                sd_release,
578
 
        ioctl:                  sd_ioctl,
579
 
        check_media_change:     check_scsidisk_media_change,
580
 
        revalidate:             fop_revalidate_scsidisk
581
 
};
582
 
 
583
 
/*
584
 
 *    If we need more than one SCSI disk major (i.e. more than
585
 
 *      16 SCSI disks), we'll have to kmalloc() more gendisks later.
586
 
 */
587
 
 
588
 
static struct gendisk sd_gendisk =
589
 
{
590
 
        major:          SCSI_DISK0_MAJOR,
591
 
        major_name:     "sd",
592
 
        minor_shift:    4,
593
 
        max_p:          1 << 4,
594
 
        fops:           &sd_fops,
595
 
};
596
 
 
597
 
#define SD_GENDISK(i)    sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
598
 
 
599
 
/*
600
 
 * rw_intr is the interrupt routine for the device driver.
601
 
 * It will be notified on the end of a SCSI read / write, and
602
 
 * will take one of several actions based on success or failure.
603
 
 */
604
 
 
605
 
static void rw_intr(Scsi_Cmnd * SCpnt)
606
 
{
607
 
    int result = SCpnt->result;
608
 
#if CONFIG_SCSI_LOGGING
609
 
    char nbuff[6];
610
 
#endif
611
 
    int this_count = SCpnt->bufflen >> 9;
612
 
    int good_sectors = (result == 0 ? this_count : 0);
613
 
    int block_sectors = 1;
614
 
    long error_sector;
615
 
    
616
 
    SCSI_LOG_HLCOMPLETE(1, sd_devname(DEVICE_NR(SCpnt->request.rq_dev), 
617
 
                                      nbuff));
618
 
    
619
 
    SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff,
620
 
                                  SCpnt->host->host_no,
621
 
                                  result,
622
 
                                  SCpnt->sense_buffer[0],
623
 
                                  SCpnt->sense_buffer[2]));
624
 
    
625
 
    /*
626
 
      Handle MEDIUM ERRORs that indicate partial success.  Since this is a
627
 
      relatively rare error condition, no care is taken to avoid
628
 
      unnecessary additional work such as memcpy's that could be avoided.
629
 
    */
630
 
    
631
 
    /* An error occurred */
632
 
    if (driver_byte(result) != 0 &&     /* An error occured */
633
 
        SCpnt->sense_buffer[0] == 0xF0) {       /* Sense data is valid */
634
 
        switch (SCpnt->sense_buffer[2]) {
635
 
        case MEDIUM_ERROR:
636
 
            error_sector = (SCpnt->sense_buffer[3] << 24) |
637
 
                (SCpnt->sense_buffer[4] << 16) |
638
 
                (SCpnt->sense_buffer[5] << 8) |
639
 
                SCpnt->sense_buffer[6];
640
 
            if (SCpnt->request.bh != NULL)
641
 
                block_sectors = SCpnt->request.bh->b_size >> 9;
642
 
            switch (SCpnt->device->sector_size) {
643
 
            case 1024:
644
 
                error_sector <<= 1;
645
 
                if (block_sectors < 2)
646
 
                    block_sectors = 2;
647
 
                break;
648
 
            case 2048:
649
 
                error_sector <<= 2;
650
 
                if (block_sectors < 4)
651
 
                    block_sectors = 4;
652
 
                break;
653
 
            case 4096:
654
 
                error_sector <<=3;
655
 
                if (block_sectors < 8)
656
 
                    block_sectors = 8;
657
 
                break;
658
 
            case 256:
659
 
                error_sector >>= 1;
660
 
                break;
661
 
            default:
662
 
                break;
663
 
            }
664
 
            error_sector -= sd_gendisks[SD_MAJOR_IDX(
665
 
                SCpnt->request.rq_dev)].part[MINOR(
666
 
                    SCpnt->request.rq_dev)].start_sect;
667
 
            error_sector &= ~(block_sectors - 1);
668
 
            good_sectors = error_sector - SCpnt->request.sector;
669
 
            if (good_sectors < 0 || good_sectors >= this_count)
670
 
                good_sectors = 0;
671
 
            break;
672
 
            
673
 
        case RECOVERED_ERROR:
674
 
            /*
675
 
             * An error occured, but it recovered.  Inform the
676
 
             * user, but make sure that it's not treated as a
677
 
             * hard error.
678
 
             */
679
 
            print_sense("sd", SCpnt);
680
 
            result = 0;
681
 
            SCpnt->sense_buffer[0] = 0x0;
682
 
            good_sectors = this_count;
683
 
            break;
684
 
            
685
 
        case ILLEGAL_REQUEST:
686
 
            if (SCpnt->device->ten == 1) {
687
 
                if (SCpnt->cmnd[0] == READ_10 ||
688
 
                    SCpnt->cmnd[0] == WRITE_10)
689
 
                    SCpnt->device->ten = 0;
690
 
            }
691
 
            break;
692
 
            
693
 
        default:
694
 
            break;
695
 
        }
696
 
    }
697
 
    /*
698
 
     * This calls the generic completion function, now that we know
699
 
     * how many actual sectors finished, and how many sectors we need
700
 
     * to say have failed.
701
 
     */
702
 
    scsi_io_completion(SCpnt, good_sectors, block_sectors);
703
 
}
704
 
/*
705
 
 * requeue_sd_request() is the request handler function for the sd driver.
706
 
 * Its function in life is to take block device requests, and translate
707
 
 * them to SCSI commands.
708
 
 */
709
 
 
710
 
 
711
 
static int check_scsidisk_media_change(kdev_t full_dev)
712
 
{
713
 
    int retval;
714
 
    int target;
715
 
    int flag = 0;
716
 
    Scsi_Device * SDev;
717
 
 
718
 
    target = DEVICE_NR(full_dev);
719
 
    SDev = rscsi_disks[target].device;
720
 
 
721
 
    if (target >= sd_template.dev_max || !SDev) {
722
 
        printk("SCSI disk request error: invalid device.\n");
723
 
        return 0;
724
 
    }
725
 
    if (!SDev->removable)
726
 
        return 0;
727
 
 
728
 
    /*
729
 
     * If the device is offline, don't send any commands - just pretend as
730
 
     * if the command failed.  If the device ever comes back online, we
731
 
     * can deal with it then.  It is only because of unrecoverable errors
732
 
     * that we would ever take a device offline in the first place.
733
 
     */
734
 
    if (SDev->online == FALSE) {
735
 
        rscsi_disks[target].ready = 0;
736
 
        SDev->changed = 1;
737
 
        return 1;       /* This will force a flush, if called from
738
 
                         * check_disk_change */
739
 
    }
740
 
 
741
 
    /* Using Start/Stop enables differentiation between drive with
742
 
     * no cartridge loaded - NOT READY, drive with changed cartridge -
743
 
     * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
744
 
     * This also handles drives that auto spin down. eg iomega jaz 1GB
745
 
     * as this will spin up the drive.
746
 
     */
747
 
    retval = -ENODEV;
748
 
    if (scsi_block_when_processing_errors(SDev))
749
 
        retval = scsi_ioctl(SDev, SCSI_IOCTL_START_UNIT, NULL);
750
 
 
751
 
    if (retval) {               /* Unable to test, unit probably not ready.
752
 
                                 * This usually means there is no disc in the
753
 
                                 * drive.  Mark as changed, and we will figure
754
 
                                 * it out later once the drive is available
755
 
                                 * again.  */
756
 
 
757
 
        rscsi_disks[target].ready = 0;
758
 
        SDev->changed = 1;
759
 
        return 1;       /* This will force a flush, if called from
760
 
                         * check_disk_change */
761
 
    }
762
 
    /*
763
 
     * for removable scsi disk ( FLOPTICAL ) we have to recognise the
764
 
     * presence of disk in the drive. This is kept in the Scsi_Disk
765
 
     * struct and tested at open !  Daniel Roche ( dan@lectra.fr )
766
 
     */
767
 
 
768
 
    rscsi_disks[target].ready = 1;      /* FLOPTICAL */
769
 
 
770
 
    retval = SDev->changed;
771
 
    if (!flag)
772
 
        SDev->changed = 0;
773
 
    return retval;
774
 
}
775
 
 
776
 
static int sd_init_onedisk(int i)
777
 
{
778
 
    unsigned char cmd[10];
779
 
    char nbuff[6];
780
 
    unsigned char *buffer;
781
 
    unsigned long spintime_value = 0;
782
 
    int the_result, retries, spintime;
783
 
    int sector_size;
784
 
    Scsi_Request *SRpnt;
785
 
 
786
 
    /*
787
 
     * Get the name of the disk, in case we need to log it somewhere.
788
 
     */
789
 
    sd_devname(i, nbuff);
790
 
 
791
 
    /*
792
 
     * If the device is offline, don't try and read capacity or any
793
 
     * of the other niceties.
794
 
     */
795
 
    if (rscsi_disks[i].device->online == FALSE)
796
 
        return i;
797
 
 
798
 
    /*
799
 
     * We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
800
 
     * considered a fatal error, and many devices report such an error
801
 
     * just after a scsi bus reset.
802
 
     */
803
 
 
804
 
    SRpnt = scsi_allocate_request(rscsi_disks[i].device);
805
 
    if (!SRpnt) {
806
 
        printk(KERN_WARNING 
807
 
               "(sd_init_onedisk:) Request allocation failure.\n");
808
 
        return i;
809
 
    }
810
 
 
811
 
    buffer = (unsigned char *) scsi_malloc(512);
812
 
    if (!buffer) {
813
 
        printk(KERN_WARNING "(sd_init_onedisk:) Memory allocation failure.\n");
814
 
        scsi_release_request(SRpnt);
815
 
        return i;
816
 
    }
817
 
 
818
 
    spintime = 0;
819
 
 
820
 
    /* Spin up drives, as required.  Only do this at boot time */
821
 
    /* Spinup needs to be done for module loads too. */
822
 
    do {
823
 
        retries = 0;
824
 
 
825
 
        while (retries < 3) {
826
 
            cmd[0] = TEST_UNIT_READY;
827
 
            cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
828
 
                ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
829
 
            memset((void *) &cmd[2], 0, 8);
830
 
            SRpnt->sr_cmd_len = 0;
831
 
            SRpnt->sr_sense_buffer[0] = 0;
832
 
            SRpnt->sr_sense_buffer[2] = 0;
833
 
            SRpnt->sr_data_direction = SCSI_DATA_NONE;
834
 
 
835
 
            scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
836
 
                           0/*512*/, SD_TIMEOUT, MAX_RETRIES);
837
 
 
838
 
            the_result = SRpnt->sr_result;
839
 
            retries++;
840
 
            if (the_result == 0
841
 
                || SRpnt->sr_sense_buffer[2] != UNIT_ATTENTION)
842
 
                break;
843
 
        }
844
 
 
845
 
        /*
846
 
         * If the drive has indicated to us that it doesn't have
847
 
         * any media in it, don't bother with any of the rest of
848
 
         * this crap.
849
 
         */
850
 
        if( the_result != 0
851
 
            && ((driver_byte(the_result) & DRIVER_SENSE) != 0)
852
 
            && SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION
853
 
            && SRpnt->sr_sense_buffer[12] == 0x3A ) {
854
 
            rscsi_disks[i].capacity = 0x1fffff;
855
 
            sector_size = 512;
856
 
            rscsi_disks[i].device->changed = 1;
857
 
            rscsi_disks[i].ready = 0;
858
 
            break;
859
 
        }
860
 
 
861
 
        /* Look for non-removable devices that return NOT_READY.
862
 
         * Issue command to spin up drive for these cases. */
863
 
        if (the_result && !rscsi_disks[i].device->removable &&
864
 
            SRpnt->sr_sense_buffer[2] == NOT_READY) {
865
 
            unsigned long time1;
866
 
            if (!spintime) {
867
 
                printk("%s: Spinning up disk...", nbuff);
868
 
                cmd[0] = START_STOP;
869
 
                cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
870
 
                    ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
871
 
                cmd[1] |= 1;    /* Return immediately */
872
 
                memset((void *) &cmd[2], 0, 8);
873
 
                cmd[4] = 1;     /* Start spin cycle */
874
 
                SRpnt->sr_cmd_len = 0;
875
 
                SRpnt->sr_sense_buffer[0] = 0;
876
 
                SRpnt->sr_sense_buffer[2] = 0;
877
 
 
878
 
                SRpnt->sr_data_direction = SCSI_DATA_READ;
879
 
                scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
880
 
                              0/*512*/, SD_TIMEOUT, MAX_RETRIES);
881
 
                spintime_value = jiffies;
882
 
            }
883
 
            spintime = 1;
884
 
            time1 = HZ;
885
 
            /* Wait 1 second for next try */
886
 
            do {
887
 
                current->state = TASK_UNINTERRUPTIBLE;
888
 
                time1 = schedule_timeout(time1);
889
 
            } while(time1);
890
 
            printk(".");
891
 
        }
892
 
    } while (the_result && spintime &&
893
 
             time_after(spintime_value + 100 * HZ, jiffies));
894
 
    if (spintime) {
895
 
        if (the_result)
896
 
            printk("not responding...\n");
897
 
        else
898
 
            printk("ready\n");
899
 
    }
900
 
    retries = 3;
901
 
    do {
902
 
        cmd[0] = READ_CAPACITY;
903
 
        cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
904
 
            ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
905
 
        memset((void *) &cmd[2], 0, 8);
906
 
        memset((void *) buffer, 0, 8);
907
 
        SRpnt->sr_cmd_len = 0;
908
 
        SRpnt->sr_sense_buffer[0] = 0;
909
 
        SRpnt->sr_sense_buffer[2] = 0;
910
 
 
911
 
        SRpnt->sr_data_direction = SCSI_DATA_READ;
912
 
        scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
913
 
                      8, SD_TIMEOUT, MAX_RETRIES);
914
 
 
915
 
        the_result = SRpnt->sr_result;
916
 
        retries--;
917
 
 
918
 
    } while (the_result && retries);
919
 
 
920
 
    /*
921
 
     * The SCSI standard says:
922
 
     * "READ CAPACITY is necessary for self configuring software"
923
 
     *  While not mandatory, support of READ CAPACITY is strongly
924
 
     *  encouraged.
925
 
     *  We used to die if we couldn't successfully do a READ CAPACITY.
926
 
     *  But, now we go on about our way.  The side effects of this are
927
 
     *
928
 
     *  1. We can't know block size with certainty. I have said
929
 
     *     "512 bytes is it" as this is most common.
930
 
     *
931
 
     *  2. Recovery from when someone attempts to read past the
932
 
     *     end of the raw device will be slower.
933
 
     */
934
 
 
935
 
    if (the_result) {
936
 
        printk("%s : READ CAPACITY failed.\n"
937
 
               "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
938
 
               nbuff, nbuff,
939
 
               status_byte(the_result),
940
 
               msg_byte(the_result),
941
 
               host_byte(the_result),
942
 
               driver_byte(the_result)
943
 
            );
944
 
        if (driver_byte(the_result) & DRIVER_SENSE)
945
 
            print_req_sense("sd", SRpnt);
946
 
        else
947
 
            printk("%s : sense not available. \n", nbuff);
948
 
 
949
 
        printk("%s : block size assumed to be 512 bytes, disk size 1GB.  \n",
950
 
               nbuff);
951
 
        rscsi_disks[i].capacity = 0x1fffff;
952
 
        sector_size = 512;
953
 
 
954
 
        /* Set dirty bit for removable devices if not ready -
955
 
         * sometimes drives will not report this properly. */
956
 
        if (rscsi_disks[i].device->removable &&
957
 
            SRpnt->sr_sense_buffer[2] == NOT_READY)
958
 
            rscsi_disks[i].device->changed = 1;
959
 
 
960
 
    } else {
961
 
        /*
962
 
         * FLOPTICAL, if read_capa is ok, drive is assumed to be ready
963
 
         */
964
 
        rscsi_disks[i].ready = 1;
965
 
 
966
 
        rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
967
 
                                       (buffer[1] << 16) |
968
 
                                       (buffer[2] << 8) |
969
 
                                       buffer[3]);
970
 
 
971
 
        sector_size = (buffer[4] << 24) |
972
 
            (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
973
 
 
974
 
        if (sector_size == 0) {
975
 
            sector_size = 512;
976
 
            printk("%s : sector size 0 reported, assuming 512.\n",
977
 
                   nbuff);
978
 
        }
979
 
        if (sector_size != 512 &&
980
 
            sector_size != 1024 &&
981
 
            sector_size != 2048 &&
982
 
            sector_size != 4096 &&
983
 
            sector_size != 256) {
984
 
            printk("%s : unsupported sector size %d.\n",
985
 
                   nbuff, sector_size);
986
 
            /*
987
 
             * The user might want to re-format the drive with
988
 
             * a supported sectorsize.  Once this happens, it
989
 
             * would be relatively trivial to set the thing up.
990
 
             * For this reason, we leave the thing in the table.
991
 
             */
992
 
            rscsi_disks[i].capacity = 0;
993
 
        }
994
 
        if (sector_size > 1024) {
995
 
            int m;
996
 
 
997
 
            /*
998
 
             * We must fix the sd_blocksizes and sd_hardsizes
999
 
             * to allow us to read the partition tables.
1000
 
             * The disk reading code does not allow for reading
1001
 
             * of partial sectors.
1002
 
             */
1003
 
            for (m = i << 4; m < ((i + 1) << 4); m++) {
1004
 
                sd_blocksizes[m] = sector_size;
1005
 
            }
1006
 
        } {
1007
 
            /*
1008
 
             * The msdos fs needs to know the hardware sector size
1009
 
             * So I have created this table. See ll_rw_blk.c
1010
 
             * Jacques Gelinas (Jacques@solucorp.qc.ca)
1011
 
             */
1012
 
            int m;
1013
 
            int hard_sector = sector_size;
1014
 
            int sz = rscsi_disks[i].capacity * (hard_sector/256);
1015
 
 
1016
 
            /* There are 16 minors allocated for each major device */
1017
 
            for (m = i << 4; m < ((i + 1) << 4); m++) {
1018
 
                sd_hardsizes[m] = hard_sector;
1019
 
            }
1020
 
 
1021
 
            printk("SCSI device %s: "
1022
 
                   "%d %d-byte hdwr sectors (%d MB)\n",
1023
 
                   nbuff, rscsi_disks[i].capacity,
1024
 
                   hard_sector, (sz/2 - sz/1250 + 974)/1950);
1025
 
        }
1026
 
 
1027
 
        /* Rescale capacity to 512-byte units */
1028
 
        if (sector_size == 4096)
1029
 
            rscsi_disks[i].capacity <<= 3;
1030
 
        if (sector_size == 2048)
1031
 
            rscsi_disks[i].capacity <<= 2;
1032
 
        if (sector_size == 1024)
1033
 
            rscsi_disks[i].capacity <<= 1;
1034
 
        if (sector_size == 256)
1035
 
            rscsi_disks[i].capacity >>= 1;
1036
 
    }
1037
 
 
1038
 
 
1039
 
    /*
1040
 
     * Unless otherwise specified, this is not write protected.
1041
 
     */
1042
 
    rscsi_disks[i].write_prot = 0;
1043
 
    if (rscsi_disks[i].device->removable && rscsi_disks[i].ready) {
1044
 
        /* FLOPTICAL */
1045
 
 
1046
 
        /*
1047
 
         * For removable scsi disk ( FLOPTICAL ) we have to recognise
1048
 
         * the Write Protect Flag. This flag is kept in the Scsi_Disk
1049
 
         * struct and tested at open !
1050
 
         * Daniel Roche ( dan@lectra.fr )
1051
 
         *
1052
 
         * Changed to get all pages (0x3f) rather than page 1 to
1053
 
         * get around devices which do not have a page 1.  Since
1054
 
         * we're only interested in the header anyway, this should
1055
 
         * be fine.
1056
 
         *   -- Matthew Dharm (mdharm-scsi@one-eyed-alien.net)
1057
 
         */
1058
 
 
1059
 
        memset((void *) &cmd[0], 0, 8);
1060
 
        cmd[0] = MODE_SENSE;
1061
 
        cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
1062
 
            ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
1063
 
        cmd[2] = 0x3f;  /* Get all pages */
1064
 
        cmd[4] = 255;   /* Ask for 255 bytes, even tho we want just the first 8 */
1065
 
        SRpnt->sr_cmd_len = 0;
1066
 
        SRpnt->sr_sense_buffer[0] = 0;
1067
 
        SRpnt->sr_sense_buffer[2] = 0;
1068
 
 
1069
 
        /* same code as READCAPA !! */
1070
 
        SRpnt->sr_data_direction = SCSI_DATA_READ;
1071
 
        scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
1072
 
                      512, SD_TIMEOUT, MAX_RETRIES);
1073
 
 
1074
 
        the_result = SRpnt->sr_result;
1075
 
 
1076
 
        if (the_result) {
1077
 
            printk("%s: test WP failed, assume Write Enabled\n", nbuff);
1078
 
        } else {
1079
 
            rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
1080
 
            printk("%s: Write Protect is %s\n", nbuff,
1081
 
                   rscsi_disks[i].write_prot ? "on" : "off");
1082
 
        }
1083
 
 
1084
 
    }                   /* check for write protect */
1085
 
    SRpnt->sr_device->ten = 1;
1086
 
    SRpnt->sr_device->remap = 1;
1087
 
    SRpnt->sr_device->sector_size = sector_size;
1088
 
    /* Wake up a process waiting for device */
1089
 
    scsi_release_request(SRpnt);
1090
 
    SRpnt = NULL;
1091
 
 
1092
 
    scsi_free(buffer, 512);
1093
 
    return i;
1094
 
}
1095
 
 
1096
 
/*
1097
 
 * The sd_init() function looks at all SCSI drives present, determines
1098
 
 * their size, and reads partition table entries for them.
1099
 
 */
1100
 
 
1101
 
static int sd_registered;
1102
 
 
1103
 
static int sd_init()
1104
 
{
1105
 
    int i;
1106
 
 
1107
 
    if (sd_template.dev_noticed == 0)
1108
 
        return 0;
1109
 
 
1110
 
    if (!rscsi_disks)
1111
 
        sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1112
 
 
1113
 
    if (sd_template.dev_max > N_SD_MAJORS * SCSI_DISKS_PER_MAJOR)
1114
 
        sd_template.dev_max = N_SD_MAJORS * SCSI_DISKS_PER_MAJOR;
1115
 
 
1116
 
    if (!sd_registered) {
1117
 
        for (i = 0; i < N_USED_SD_MAJORS; i++) {
1118
 
#ifdef DEVFS_MUST_DIE
1119
 
            if (devfs_register_blkdev(SD_MAJOR(i), "sd", &sd_fops)) {
1120
 
                printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i));
1121
 
                sd_template.dev_noticed = 0;
1122
 
                return 1;
1123
 
            }
1124
 
#endif
1125
 
        }
1126
 
        sd_registered++;
1127
 
    }
1128
 
    /* We do not support attaching loadable devices yet. */
1129
 
    if (rscsi_disks) 
1130
 
        return 0; 
1131
 
 
1132
 
    rscsi_disks = kmalloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1133
 
    if (!rscsi_disks)
1134
 
        goto cleanup_devfs;
1135
 
    memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1136
 
 
1137
 
    /* for every (necessary) major: */
1138
 
    sd_sizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1139
 
    if (!sd_sizes)
1140
 
        goto cleanup_disks;
1141
 
    memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1142
 
 
1143
 
    sd_blocksizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), 
1144
 
                            GFP_ATOMIC);
1145
 
    if (!sd_blocksizes)
1146
 
        goto cleanup_sizes;
1147
 
        
1148
 
    sd_hardsizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), 
1149
 
                           GFP_ATOMIC);
1150
 
    if (!sd_hardsizes)
1151
 
        goto cleanup_blocksizes;
1152
 
 
1153
 
    sd_max_sectors = kmalloc((sd_template.dev_max << 4) * sizeof(int), 
1154
 
                             GFP_ATOMIC);
1155
 
    if (!sd_max_sectors)
1156
 
        goto cleanup_max_sectors;
1157
 
 
1158
 
    for (i = 0; i < sd_template.dev_max << 4; i++) {
1159
 
        sd_blocksizes[i] = 1024;
1160
 
        sd_hardsizes[i] = 512;
1161
 
        /*
1162
 
         * Allow lowlevel device drivers to generate 512k large scsi
1163
 
         * commands if they know what they're doing and they ask for it
1164
 
         * explicitly via the SHpnt->max_sectors API.
1165
 
         */
1166
 
        sd_max_sectors[i] = MAX_SEGMENTS*8;
1167
 
    }
1168
 
 
1169
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1170
 
        blksize_size[SD_MAJOR(i)] = sd_blocksizes + 
1171
 
            i * (SCSI_DISKS_PER_MAJOR << 4);
1172
 
        hardsect_size[SD_MAJOR(i)] = sd_hardsizes + 
1173
 
            i * (SCSI_DISKS_PER_MAJOR << 4);
1174
 
        max_sectors[SD_MAJOR(i)] = sd_max_sectors + 
1175
 
            i * (SCSI_DISKS_PER_MAJOR << 4);
1176
 
    }
1177
 
 
1178
 
    sd_gendisks = kmalloc(N_USED_SD_MAJORS * sizeof(struct gendisk), 
1179
 
                          GFP_ATOMIC);
1180
 
    if (!sd_gendisks)
1181
 
        goto cleanup_sd_gendisks;
1182
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1183
 
        sd_gendisks[i] = sd_gendisk;    /* memcpy */
1184
 
#ifdef DEVFS_MUST_DIE
1185
 
        sd_gendisks[i].de_arr = kmalloc (SCSI_DISKS_PER_MAJOR * 
1186
 
                                         sizeof *sd_gendisks[i].de_arr,
1187
 
                                         GFP_ATOMIC);
1188
 
        if (!sd_gendisks[i].de_arr)
1189
 
            goto cleanup_gendisks_de_arr;
1190
 
        memset (sd_gendisks[i].de_arr, 0,
1191
 
                SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].de_arr);
1192
 
#endif
1193
 
        sd_gendisks[i].flags = kmalloc (SCSI_DISKS_PER_MAJOR * 
1194
 
                                        sizeof *sd_gendisks[i].flags,
1195
 
                                        GFP_ATOMIC);
1196
 
        if (!sd_gendisks[i].flags)
1197
 
            goto cleanup_gendisks_flags;
1198
 
        memset (sd_gendisks[i].flags, 0,
1199
 
                SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].flags);
1200
 
        sd_gendisks[i].major = SD_MAJOR(i);
1201
 
        sd_gendisks[i].major_name = "sd";
1202
 
        sd_gendisks[i].minor_shift = 4;
1203
 
        sd_gendisks[i].max_p = 1 << 4;
1204
 
        sd_gendisks[i].part = kmalloc((SCSI_DISKS_PER_MAJOR << 4) * 
1205
 
                                      sizeof(struct hd_struct),
1206
 
                                      GFP_ATOMIC);
1207
 
        if (!sd_gendisks[i].part)
1208
 
            goto cleanup_gendisks_part;
1209
 
        memset(sd_gendisks[i].part, 0, (SCSI_DISKS_PER_MAJOR << 4) * 
1210
 
               sizeof(struct hd_struct));
1211
 
        sd_gendisks[i].sizes = sd_sizes + (i * SCSI_DISKS_PER_MAJOR << 4);
1212
 
        sd_gendisks[i].nr_real = 0;
1213
 
        sd_gendisks[i].real_devices =
1214
 
            (void *) (rscsi_disks + i * SCSI_DISKS_PER_MAJOR);
1215
 
    }
1216
 
 
1217
 
    return 0;
1218
 
 
1219
 
 cleanup_gendisks_part:
1220
 
    kfree(sd_gendisks[i].flags);
1221
 
 cleanup_gendisks_flags:
1222
 
#ifdef DEVFS_MUST_DIE
1223
 
    kfree(sd_gendisks[i].de_arr);
1224
 
 cleanup_gendisks_de_arr:
1225
 
#endif
1226
 
    while (--i >= 0 ) {
1227
 
#ifdef DEVFS_MUST_DIE
1228
 
        kfree(sd_gendisks[i].de_arr);
1229
 
#endif
1230
 
        kfree(sd_gendisks[i].flags);
1231
 
        kfree(sd_gendisks[i].part);
1232
 
    }
1233
 
    kfree(sd_gendisks);
1234
 
    sd_gendisks = NULL;
1235
 
 cleanup_sd_gendisks:
1236
 
    kfree(sd_max_sectors);
1237
 
 cleanup_max_sectors:
1238
 
    kfree(sd_hardsizes);
1239
 
 cleanup_blocksizes:
1240
 
    kfree(sd_blocksizes);
1241
 
 cleanup_sizes:
1242
 
    kfree(sd_sizes);
1243
 
 cleanup_disks:
1244
 
    kfree(rscsi_disks);
1245
 
    rscsi_disks = NULL;
1246
 
 cleanup_devfs:
1247
 
#ifdef DEVFS_MUST_DIE
1248
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1249
 
        devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1250
 
    }
1251
 
#endif
1252
 
    sd_registered--;
1253
 
    sd_template.dev_noticed = 0;
1254
 
    return 1;
1255
 
}
1256
 
 
1257
 
 
1258
 
static void sd_finish()
1259
 
{
1260
 
    int i;
1261
 
 
1262
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1263
 
        blk_dev[SD_MAJOR(i)].queue = sd_find_queue;
1264
 
        add_gendisk(&sd_gendisks[i]);
1265
 
    }
1266
 
 
1267
 
    for (i = 0; i < sd_template.dev_max; ++i)
1268
 
        if (!rscsi_disks[i].capacity && rscsi_disks[i].device) {
1269
 
            sd_init_onedisk(i);
1270
 
            if (!rscsi_disks[i].has_part_table) {
1271
 
                sd_sizes[i << 4] = rscsi_disks[i].capacity;
1272
 
                register_disk(&SD_GENDISK(i), MKDEV_SD(i),
1273
 
                              1<<4, &sd_fops,
1274
 
                              rscsi_disks[i].capacity);
1275
 
                rscsi_disks[i].has_part_table = 1;
1276
 
            }
1277
 
        }
1278
 
#if 0 
1279
 
    /* If our host adapter is capable of scatter-gather, then we increase
1280
 
     * the read-ahead to 60 blocks (120 sectors).  If not, we use
1281
 
     * a two block (4 sector) read ahead. We can only respect this with the
1282
 
     * granularity of every 16 disks (one device major).
1283
 
     */
1284
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1285
 
        read_ahead[SD_MAJOR(i)] =
1286
 
            (rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device
1287
 
             && rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device->host->sg_tablesize)
1288
 
            ? 120       /* 120 sector read-ahead */
1289
 
            : 4;        /* 4 sector read-ahead */
1290
 
    }
1291
 
#endif
1292
 
 
1293
 
#if 0
1294
 
        /* XXX SMH: turn on some logging */
1295
 
        scsi_logging_level = ~0;
1296
 
        SCSI_SET_LOGGING(SCSI_LOG_HLQUEUE_SHIFT, SCSI_LOG_HLQUEUE_BITS, 1); 
1297
 
#endif
1298
 
 
1299
 
    return;
1300
 
}
1301
 
 
1302
 
 
1303
 
/* 
1304
 
** scsi_probe_devices: 
1305
 
** 
1306
 
** add the scsi block devices for this domain to a xen_disk_info_t; 
1307
 
** we assume xdi->count points to the first unused place in the array. 
1308
 
**
1309
 
** XXX SMH: this is a rather gross 'probe' function to allow xeno world 
1310
 
** to grope us; this should really not be in the disk-specific code as 
1311
 
** it should report tapes, CDs, etc. But for now this looks like the 
1312
 
** easiest place to hook it in :-( 
1313
 
**
1314
 
*/
1315
 
 
1316
 
static kdev_t scsi_devs[] = { 
1317
 
    MKDEV(SCSI_DISK0_MAJOR,   0), MKDEV(SCSI_DISK0_MAJOR,  16), /* sda, sdb */
1318
 
    MKDEV(SCSI_DISK0_MAJOR,  32), MKDEV(SCSI_DISK0_MAJOR,  48), /* sdc, sdd */
1319
 
    MKDEV(SCSI_DISK0_MAJOR,  64), MKDEV(SCSI_DISK0_MAJOR,  80), /* sde, sdf */
1320
 
    MKDEV(SCSI_DISK0_MAJOR,  96), MKDEV(SCSI_DISK0_MAJOR, 112), /* sdg, sdh */
1321
 
    MKDEV(SCSI_DISK0_MAJOR, 128), MKDEV(SCSI_DISK0_MAJOR, 144), /* sdi, sdj */
1322
 
    MKDEV(SCSI_DISK0_MAJOR, 160), MKDEV(SCSI_DISK0_MAJOR, 176), /* sdk, sdl */
1323
 
    MKDEV(SCSI_DISK0_MAJOR, 192), MKDEV(SCSI_DISK0_MAJOR, 208), /* sdm, sdn */
1324
 
    MKDEV(SCSI_DISK0_MAJOR, 224), MKDEV(SCSI_DISK0_MAJOR, 240), /* sdo, sdp */
1325
 
};
1326
 
 
1327
 
 
1328
 
void scsi_probe_devices(xen_disk_info_t *xdi)
1329
 
{
1330
 
    int i;
1331
 
    Scsi_Disk *sd; 
1332
 
    xen_disk_t *xd = &xdi->disks[xdi->count];
1333
 
 
1334
 
    for ( sd = rscsi_disks, i = 0; i < sd_template.dev_max; i++, sd++ )
1335
 
    {
1336
 
        if ( sd->device == NULL )
1337
 
            continue;
1338
 
 
1339
 
        if ( xdi->count == xdi->max )
1340
 
            BUG();
1341
 
 
1342
 
        /* We export 'raw' linux device numbers to domain 0. */
1343
 
        xd->device   = scsi_devs[i]; 
1344
 
        xd->info     = XD_TYPE_DISK; /* XXX should determine properly */
1345
 
        xd->capacity = sd->capacity;
1346
 
        xd->domain   = 0;
1347
 
 
1348
 
        xdi->count++;
1349
 
        xd++;
1350
 
    }
1351
 
}       
1352
 
 
1353
 
 
1354
 
 
1355
 
 
1356
 
static int sd_detect(Scsi_Device * SDp)
1357
 
{
1358
 
    if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1359
 
        return 0;
1360
 
    sd_template.dev_noticed++;
1361
 
    return 1;
1362
 
}
1363
 
 
1364
 
static int sd_attach(Scsi_Device * SDp)
1365
 
{
1366
 
    unsigned int devnum;
1367
 
    Scsi_Disk *dpnt;
1368
 
    int i;
1369
 
    char nbuff[6];
1370
 
 
1371
 
    if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1372
 
        return 0;
1373
 
 
1374
 
    if (sd_template.nr_dev >= sd_template.dev_max || rscsi_disks == NULL) {
1375
 
        SDp->attached--;
1376
 
        return 1;
1377
 
    }
1378
 
    for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1379
 
        if (!dpnt->device)
1380
 
            break;
1381
 
 
1382
 
    if (i >= sd_template.dev_max) {
1383
 
        printk(KERN_WARNING "scsi_devices corrupt (sd),"
1384
 
               " nr_dev %d dev_max %d\n",
1385
 
               sd_template.nr_dev, sd_template.dev_max);
1386
 
        SDp->attached--;
1387
 
        return 1;
1388
 
    }
1389
 
 
1390
 
    rscsi_disks[i].device = SDp;
1391
 
    rscsi_disks[i].has_part_table = 0;
1392
 
    sd_template.nr_dev++;
1393
 
    SD_GENDISK(i).nr_real++;
1394
 
    devnum = i % SCSI_DISKS_PER_MAJOR;
1395
 
#ifdef DEVFS_MUST_DIE
1396
 
    SD_GENDISK(i).de_arr[devnum] = SDp->de;
1397
 
#endif
1398
 
    if (SDp->removable)
1399
 
        SD_GENDISK(i).flags[devnum] |= GENHD_FL_REMOVABLE;
1400
 
    sd_devname(i, nbuff);
1401
 
    printk("Attached scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1402
 
           SDp->removable ? "removable " : "",
1403
 
           nbuff, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1404
 
    return 0;
1405
 
}
1406
 
 
1407
 
#define DEVICE_BUSY rscsi_disks[target].device->busy
1408
 
#define ALLOW_REVALIDATE rscsi_disks[target].device->allow_revalidate
1409
 
#define USAGE rscsi_disks[target].device->access_count
1410
 
#define CAPACITY rscsi_disks[target].capacity
1411
 
#define MAYBE_REINIT  sd_init_onedisk(target)
1412
 
 
1413
 
/* This routine is called to flush all partitions and partition tables
1414
 
 * for a changed scsi disk, and then re-read the new partition table.
1415
 
 * If we are revalidating a disk because of a media change, then we
1416
 
 * enter with usage == 0.  If we are using an ioctl, we automatically have
1417
 
 * usage == 1 (we need an open channel to use an ioctl :-), so this
1418
 
 * is our limit.
1419
 
 */
1420
 
int revalidate_scsidisk(kdev_t dev, int maxusage)
1421
 
{
1422
 
    struct gendisk *sdgd;
1423
 
    int target;
1424
 
    int max_p;
1425
 
    int start;
1426
 
    int i;
1427
 
 
1428
 
    target = DEVICE_NR(dev);
1429
 
 
1430
 
    if (DEVICE_BUSY || (ALLOW_REVALIDATE == 0 && USAGE > maxusage)) {
1431
 
        printk("Device busy for revalidation (usage=%d)\n", USAGE);
1432
 
        return -EBUSY;
1433
 
    }
1434
 
    DEVICE_BUSY = 1;
1435
 
 
1436
 
    sdgd = &SD_GENDISK(target);
1437
 
    max_p = sd_gendisk.max_p;
1438
 
    start = target << sd_gendisk.minor_shift;
1439
 
 
1440
 
    for (i = max_p - 1; i >= 0; i--) {
1441
 
        int index = start + i;
1442
 
        invalidate_device(MKDEV_SD_PARTITION(index), 1);
1443
 
        sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1444
 
        sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1445
 
        /*
1446
 
         * Reset the blocksize for everything so that we can read
1447
 
         * the partition table.  Technically we will determine the
1448
 
         * correct block size when we revalidate, but we do this just
1449
 
         * to make sure that everything remains consistent.
1450
 
         */
1451
 
        sd_blocksizes[index] = 1024;
1452
 
        if (rscsi_disks[target].device->sector_size == 2048)
1453
 
            sd_blocksizes[index] = 2048;
1454
 
        else
1455
 
            sd_blocksizes[index] = 1024;
1456
 
    }
1457
 
 
1458
 
#ifdef MAYBE_REINIT
1459
 
    MAYBE_REINIT;
1460
 
#endif
1461
 
 
1462
 
    grok_partitions(&SD_GENDISK(target), target % SCSI_DISKS_PER_MAJOR,
1463
 
                    1<<4, CAPACITY);
1464
 
 
1465
 
    DEVICE_BUSY = 0;
1466
 
    return 0;
1467
 
}
1468
 
 
1469
 
static int fop_revalidate_scsidisk(kdev_t dev)
1470
 
{
1471
 
    return revalidate_scsidisk(dev, 0);
1472
 
}
1473
 
 
1474
 
static void sd_detach(Scsi_Device * SDp)
1475
 
{
1476
 
    Scsi_Disk *dpnt;
1477
 
    struct gendisk *sdgd;
1478
 
    int i, j;
1479
 
    int max_p;
1480
 
    int start;
1481
 
    
1482
 
    if (rscsi_disks == NULL)
1483
 
        return;
1484
 
    
1485
 
    for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1486
 
        if (dpnt->device == SDp) {
1487
 
            
1488
 
            /* If we are disconnecting a disk driver, sync and invalidate
1489
 
             * everything */
1490
 
            sdgd = &SD_GENDISK(i);
1491
 
            max_p = sd_gendisk.max_p;
1492
 
            start = i << sd_gendisk.minor_shift;
1493
 
            
1494
 
            for (j = max_p - 1; j >= 0; j--) {
1495
 
                int index = start + j;
1496
 
                invalidate_device(MKDEV_SD_PARTITION(index), 1);
1497
 
                sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1498
 
                sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1499
 
                sd_sizes[index] = 0;
1500
 
            }
1501
 
#ifdef DEVFS_MUST_DIE
1502
 
            devfs_register_partitions (sdgd,
1503
 
                                       SD_MINOR_NUMBER (start), 1);
1504
 
#endif
1505
 
            /* unregister_disk() */
1506
 
            dpnt->has_part_table = 0;
1507
 
            dpnt->device = NULL;
1508
 
            dpnt->capacity = 0;
1509
 
            SDp->attached--;
1510
 
            sd_template.dev_noticed--;
1511
 
            sd_template.nr_dev--;
1512
 
            SD_GENDISK(i).nr_real--;
1513
 
            return;
1514
 
        }
1515
 
    return;
1516
 
}
1517
 
 
1518
 
static int __init init_sd(void)
1519
 
{
1520
 
    extern int scsi_register_module(int, void *);
1521
 
 
1522
 
    sd_template.module = THIS_MODULE;
1523
 
    return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1524
 
}
1525
 
 
1526
 
static void __exit exit_sd(void)
1527
 
{
1528
 
    int i;
1529
 
    
1530
 
#if 0
1531
 
    scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1532
 
#endif
1533
 
    
1534
 
#ifdef DEVFS_MUST_DIE
1535
 
    for (i = 0; i < N_USED_SD_MAJORS; i++)
1536
 
        devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1537
 
#endif
1538
 
    
1539
 
    sd_registered--;
1540
 
    if (rscsi_disks != NULL) {
1541
 
        kfree(rscsi_disks);
1542
 
        kfree(sd_sizes);
1543
 
        kfree(sd_blocksizes);
1544
 
        kfree(sd_hardsizes);
1545
 
        for (i = 0; i < N_USED_SD_MAJORS; i++) {
1546
 
#if 0 /* XXX aren't we forgetting to deallocate something? */
1547
 
            kfree(sd_gendisks[i].de_arr);
1548
 
            kfree(sd_gendisks[i].flags);
1549
 
#endif
1550
 
            kfree(sd_gendisks[i].part);
1551
 
        }
1552
 
    }
1553
 
    for (i = 0; i < N_USED_SD_MAJORS; i++) {
1554
 
        del_gendisk(&sd_gendisks[i]);
1555
 
        blk_size[SD_MAJOR(i)] = NULL;   /* XXX blksize_size actually? */
1556
 
        hardsect_size[SD_MAJOR(i)] = NULL;
1557
 
#if 0
1558
 
        read_ahead[SD_MAJOR(i)] = 0;
1559
 
#endif
1560
 
    }
1561
 
    sd_template.dev_max = 0;
1562
 
    if (sd_gendisks != NULL)    /* kfree tests for 0, but leave explicit */
1563
 
        kfree(sd_gendisks);
1564
 
}
1565
 
 
1566
 
module_init(init_sd);
1567
 
module_exit(exit_sd);
1568
 
MODULE_LICENSE("GPL");