ia64/xen-unstable

view xen/drivers/scsi/sd.c @ 926:0a901de56d7c

bitkeeper revision 1.588 (3fafd2ccYgSbWe9z2kLiH-DeviUaIA)

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