[PATCH] update copyright and licensing
[linux-2.6/history.git] / drivers / scsi / sd.c
blob8ef4b3e20d29c939c90b280e4b31f94f1f859e87
1 /*
2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
8 * Modification history:
9 * - Drew Eckhardt <drew@colorado.edu> original
10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 * outstanding request, and other enhancements.
12 * Support loadable low-level scsi drivers.
13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 * eight major numbers.
15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 * sd_init and cleanups.
18 * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 * not being read in sd_open. Fix problem where removable media
20 * could be ejected after sd_open.
21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
23 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
24 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
25 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
26 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
27 * - entering other commands: SCSI_LOG_HLQUEUE level 3
28 * Note: when the logging level is set by the user, it must be greater
29 * than the level indicated above to trigger output.
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <linux/bio.h>
39 #include <linux/genhd.h>
40 #include <linux/hdreg.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/init.h>
44 #include <linux/reboot.h>
45 #include <linux/vmalloc.h>
46 #include <linux/blkdev.h>
47 #include <linux/blkpg.h>
48 #include <asm/uaccess.h>
50 #include "scsi.h"
51 #include "hosts.h"
53 #include <scsi/scsi_driver.h>
54 #include <scsi/scsi_ioctl.h>
55 #include <scsi/scsicam.h>
57 #include "scsi_logging.h"
61 * Remaining dev_t-handling stuff
63 #define SD_MAJORS 16
64 #define SD_DISKS (SD_MAJORS << 4)
67 * Time out in seconds for disks and Magneto-opticals (which are slower).
69 #define SD_TIMEOUT (30 * HZ)
70 #define SD_MOD_TIMEOUT (75 * HZ)
73 * Number of allowed retries
75 #define SD_MAX_RETRIES 5
77 struct scsi_disk {
78 struct scsi_driver *driver; /* always &sd_template */
79 struct scsi_device *device;
80 struct gendisk *disk;
81 unsigned int openers; /* protected by BKL for now, yuck */
82 sector_t capacity; /* size in 512-byte sectors */
83 u32 index;
84 u8 media_present;
85 u8 write_prot;
86 unsigned WCE : 1; /* state of disk WCE bit */
87 unsigned RCD : 1; /* state of disk RCD bit, unused */
90 static unsigned long sd_index_bits[SD_DISKS / BITS_PER_LONG];
91 static spinlock_t sd_index_lock = SPIN_LOCK_UNLOCKED;
93 static int sd_revalidate_disk(struct gendisk *disk);
94 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
96 static int sd_probe(struct device *);
97 static int sd_remove(struct device *);
98 static void sd_shutdown(struct device *dev);
99 static void sd_rescan(struct device *);
100 static int sd_init_command(struct scsi_cmnd *);
101 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
102 struct scsi_request *SRpnt, unsigned char *buffer);
104 static struct scsi_driver sd_template = {
105 .owner = THIS_MODULE,
106 .gendrv = {
107 .name = "sd",
108 .probe = sd_probe,
109 .remove = sd_remove,
110 .shutdown = sd_shutdown,
112 .rescan = sd_rescan,
113 .init_command = sd_init_command,
116 static int sd_major(int major_idx)
118 switch (major_idx) {
119 case 0:
120 return SCSI_DISK0_MAJOR;
121 case 1 ... 7:
122 return SCSI_DISK1_MAJOR + major_idx - 1;
123 case 8 ... 15:
124 return SCSI_DISK8_MAJOR + major_idx - 8;
125 default:
126 BUG();
127 return 0; /* shut up gcc */
131 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
133 return container_of(disk->private_data, struct scsi_disk, driver);
137 * sd_init_command - build a scsi (read or write) command from
138 * information in the request structure.
139 * @SCpnt: pointer to mid-level's per scsi command structure that
140 * contains request and into which the scsi command is written
142 * Returns 1 if successful and 0 if error (or cannot be done now).
144 static int sd_init_command(struct scsi_cmnd * SCpnt)
146 unsigned int this_count, timeout;
147 struct gendisk *disk;
148 sector_t block;
149 struct scsi_device *sdp = SCpnt->device;
151 timeout = SD_TIMEOUT;
152 if (SCpnt->device->type != TYPE_DISK)
153 timeout = SD_MOD_TIMEOUT;
156 * these are already setup, just copy cdb basically
158 if (SCpnt->request->flags & REQ_BLOCK_PC) {
159 struct request *rq = SCpnt->request;
161 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
162 return 0;
164 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
165 if (rq_data_dir(rq) == WRITE)
166 SCpnt->sc_data_direction = DMA_TO_DEVICE;
167 else if (rq->data_len)
168 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
169 else
170 SCpnt->sc_data_direction = DMA_NONE;
172 this_count = rq->data_len;
173 if (rq->timeout)
174 timeout = rq->timeout;
176 SCpnt->transfersize = rq->data_len;
177 goto queue;
181 * we only do REQ_CMD and REQ_BLOCK_PC
183 if (!(SCpnt->request->flags & REQ_CMD))
184 return 0;
186 disk = SCpnt->request->rq_disk;
187 block = SCpnt->request->sector;
188 this_count = SCpnt->request_bufflen >> 9;
190 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
191 "count=%d\n", disk->disk_name, (unsigned long long)block, this_count));
193 if (!sdp || !sdp->online ||
194 block + SCpnt->request->nr_sectors > get_capacity(disk)) {
195 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
196 SCpnt->request->nr_sectors));
197 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
198 return 0;
201 if (sdp->changed) {
203 * quietly refuse to do anything to a changed disc until
204 * the changed bit has been reset
206 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
207 return 0;
209 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
210 disk->disk_name, (unsigned long long)block));
213 * If we have a 1K hardware sectorsize, prevent access to single
214 * 512 byte sectors. In theory we could handle this - in fact
215 * the scsi cdrom driver must be able to handle this because
216 * we typically use 1K blocksizes, and cdroms typically have
217 * 2K hardware sectorsizes. Of course, things are simpler
218 * with the cdrom, since it is read-only. For performance
219 * reasons, the filesystems should be able to handle this
220 * and not force the scsi disk driver to use bounce buffers
221 * for this.
223 if (sdp->sector_size == 1024) {
224 if ((block & 1) || (SCpnt->request->nr_sectors & 1)) {
225 printk(KERN_ERR "sd: Bad block number requested");
226 return 0;
227 } else {
228 block = block >> 1;
229 this_count = this_count >> 1;
232 if (sdp->sector_size == 2048) {
233 if ((block & 3) || (SCpnt->request->nr_sectors & 3)) {
234 printk(KERN_ERR "sd: Bad block number requested");
235 return 0;
236 } else {
237 block = block >> 2;
238 this_count = this_count >> 2;
241 if (sdp->sector_size == 4096) {
242 if ((block & 7) || (SCpnt->request->nr_sectors & 7)) {
243 printk(KERN_ERR "sd: Bad block number requested");
244 return 0;
245 } else {
246 block = block >> 3;
247 this_count = this_count >> 3;
250 if (rq_data_dir(SCpnt->request) == WRITE) {
251 if (!sdp->writeable) {
252 return 0;
254 SCpnt->cmnd[0] = WRITE_6;
255 SCpnt->sc_data_direction = DMA_TO_DEVICE;
256 } else if (rq_data_dir(SCpnt->request) == READ) {
257 SCpnt->cmnd[0] = READ_6;
258 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
259 } else {
260 printk(KERN_ERR "sd: Unknown command %lx\n",
261 SCpnt->request->flags);
262 /* overkill panic("Unknown sd command %lx\n", SCpnt->request->flags); */
263 return 0;
266 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
267 disk->disk_name, (rq_data_dir(SCpnt->request) == WRITE) ?
268 "writing" : "reading", this_count, SCpnt->request->nr_sectors));
270 SCpnt->cmnd[1] = 0;
272 if (block > 0xffffffff) {
273 SCpnt->cmnd[0] += READ_16 - READ_6;
274 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
275 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
276 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
277 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
278 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
279 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
280 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
281 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
282 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
283 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
284 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
285 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
286 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
287 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
288 SCpnt->device->use_10_for_rw) {
289 if (this_count > 0xffff)
290 this_count = 0xffff;
292 SCpnt->cmnd[0] += READ_10 - READ_6;
293 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
294 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
295 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
296 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
297 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
298 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
299 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
300 } else {
301 if (this_count > 0xff)
302 this_count = 0xff;
304 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
305 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
306 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
307 SCpnt->cmnd[4] = (unsigned char) this_count;
308 SCpnt->cmnd[5] = 0;
310 SCpnt->request_bufflen = SCpnt->bufflen =
311 this_count * sdp->sector_size;
314 * We shouldn't disconnect in the middle of a sector, so with a dumb
315 * host adapter, it's safe to assume that we can at least transfer
316 * this many bytes between each connect / disconnect.
318 SCpnt->transfersize = sdp->sector_size;
319 SCpnt->underflow = this_count << 9;
321 queue:
322 SCpnt->allowed = SD_MAX_RETRIES;
323 SCpnt->timeout_per_command = timeout;
326 * This is the completion routine we use. This is matched in terms
327 * of capability to this function.
329 SCpnt->done = sd_rw_intr;
332 * This indicates that the command is ready from our end to be
333 * queued.
335 return 1;
339 * sd_open - open a scsi disk device
340 * @inode: only i_rdev member may be used
341 * @filp: only f_mode and f_flags may be used
343 * Returns 0 if successful. Returns a negated errno value in case
344 * of error.
346 * Note: This can be called from a user context (e.g. fsck(1) )
347 * or from within the kernel (e.g. as a result of a mount(1) ).
348 * In the latter case @inode and @filp carry an abridged amount
349 * of information as noted above.
351 static int sd_open(struct inode *inode, struct file *filp)
353 struct gendisk *disk = inode->i_bdev->bd_disk;
354 struct scsi_disk *sdkp = scsi_disk(disk);
355 struct scsi_device *sdev = sdkp->device;
356 int retval;
358 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
360 retval = scsi_device_get(sdev);
361 if (retval)
362 return retval;
365 * If the device is in error recovery, wait until it is done.
366 * If the device is offline, then disallow any access to it.
368 retval = -ENXIO;
369 if (!scsi_block_when_processing_errors(sdev))
370 goto error_out;
372 if (sdev->removable || sdkp->write_prot)
373 check_disk_change(inode->i_bdev);
376 * If the drive is empty, just let the open fail.
378 retval = -ENOMEDIUM;
379 if (sdev->removable && !sdkp->media_present &&
380 !(filp->f_flags & O_NDELAY))
381 goto error_out;
384 * If the device has the write protect tab set, have the open fail
385 * if the user expects to be able to write to the thing.
387 retval = -EROFS;
388 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
389 goto error_out;
392 * It is possible that the disk changing stuff resulted in
393 * the device being taken offline. If this is the case,
394 * report this to the user, and don't pretend that the
395 * open actually succeeded.
397 retval = -ENXIO;
398 if (!sdev->online)
399 goto error_out;
401 if (!sdkp->openers++ && sdev->removable) {
402 if (scsi_block_when_processing_errors(sdev))
403 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
406 return 0;
408 error_out:
409 scsi_device_put(sdev);
410 return retval;
414 * sd_release - invoked when the (last) close(2) is called on this
415 * scsi disk.
416 * @inode: only i_rdev member may be used
417 * @filp: only f_mode and f_flags may be used
419 * Returns 0.
421 * Note: may block (uninterruptible) if error recovery is underway
422 * on this disk.
424 static int sd_release(struct inode *inode, struct file *filp)
426 struct gendisk *disk = inode->i_bdev->bd_disk;
427 struct scsi_disk *sdkp = scsi_disk(disk);
428 struct scsi_device *sdev = sdkp->device;
430 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
432 if (!--sdkp->openers && sdev->removable) {
433 if (scsi_block_when_processing_errors(sdev))
434 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
438 * XXX and what if there are packets in flight and this close()
439 * XXX is followed by a "rmmod sd_mod"?
441 scsi_device_put(sdev);
442 return 0;
445 static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry *loc)
447 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
448 struct scsi_device *sdp = sdkp->device;
449 struct Scsi_Host *host = sdp->host;
450 int diskinfo[4];
452 /* default to most commonly used values */
453 diskinfo[0] = 0x40; /* 1 << 6 */
454 diskinfo[1] = 0x20; /* 1 << 5 */
455 diskinfo[2] = sdkp->capacity >> 11;
457 /* override with calculated, extended default, or driver values */
458 if (host->hostt->bios_param)
459 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
460 else
461 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
463 if (put_user(diskinfo[0], &loc->heads))
464 return -EFAULT;
465 if (put_user(diskinfo[1], &loc->sectors))
466 return -EFAULT;
467 if (put_user(diskinfo[2], &loc->cylinders))
468 return -EFAULT;
469 if (put_user((unsigned)get_start_sect(bdev),
470 (unsigned long *)&loc->start))
471 return -EFAULT;
472 return 0;
476 * sd_ioctl - process an ioctl
477 * @inode: only i_rdev/i_bdev members may be used
478 * @filp: only f_mode and f_flags may be used
479 * @cmd: ioctl command number
480 * @arg: this is third argument given to ioctl(2) system call.
481 * Often contains a pointer.
483 * Returns 0 if successful (some ioctls return postive numbers on
484 * success as well). Returns a negated errno value in case of error.
486 * Note: most ioctls are forward onto the block subsystem or further
487 * down in the scsi subsytem.
489 static int sd_ioctl(struct inode * inode, struct file * filp,
490 unsigned int cmd, unsigned long arg)
492 struct block_device *bdev = inode->i_bdev;
493 struct gendisk *disk = bdev->bd_disk;
494 struct scsi_device *sdp = scsi_disk(disk)->device;
495 int error;
497 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
498 disk->disk_name, cmd));
501 * If we are in the middle of error recovery, don't let anyone
502 * else try and use this device. Also, if error recovery fails, it
503 * may try and take the device offline, in which case all further
504 * access to the device is prohibited.
506 if (!scsi_block_when_processing_errors(sdp))
507 return -ENODEV;
509 if (cmd == HDIO_GETGEO) {
510 if (!arg)
511 return -EINVAL;
512 return sd_hdio_getgeo(bdev, (struct hd_geometry *)arg);
516 * Send SCSI addressing ioctls directly to mid level, send other
517 * ioctls to block level and then onto mid level if they can't be
518 * resolved.
520 switch (cmd) {
521 case SCSI_IOCTL_GET_IDLUN:
522 case SCSI_IOCTL_GET_BUS_NUMBER:
523 return scsi_ioctl(sdp, cmd, (void *)arg);
524 default:
525 error = scsi_cmd_ioctl(bdev, cmd, arg);
526 if (error != -ENOTTY)
527 return error;
529 return scsi_ioctl(sdp, cmd, (void *)arg);
532 static void set_media_not_present(struct scsi_disk *sdkp)
534 sdkp->media_present = 0;
535 sdkp->capacity = 0;
536 sdkp->device->changed = 1;
540 * sd_media_changed - check if our medium changed
541 * @disk: kernel device descriptor
543 * Returns 0 if not applicable or no change; 1 if change
545 * Note: this function is invoked from the block subsystem.
547 static int sd_media_changed(struct gendisk *disk)
549 struct scsi_disk *sdkp = scsi_disk(disk);
550 struct scsi_device *sdp = sdkp->device;
551 int retval;
553 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
554 disk->disk_name));
556 if (!sdp->removable)
557 return 0;
560 * If the device is offline, don't send any commands - just pretend as
561 * if the command failed. If the device ever comes back online, we
562 * can deal with it then. It is only because of unrecoverable errors
563 * that we would ever take a device offline in the first place.
565 if (!sdp->online)
566 goto not_present;
569 * Using TEST_UNIT_READY enables differentiation between drive with
570 * no cartridge loaded - NOT READY, drive with changed cartridge -
571 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
573 * Drives that auto spin down. eg iomega jaz 1G, will be started
574 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
575 * sd_revalidate() is called.
577 retval = -ENODEV;
578 if (scsi_block_when_processing_errors(sdp))
579 retval = scsi_ioctl(sdp, SCSI_IOCTL_TEST_UNIT_READY, NULL);
582 * Unable to test, unit probably not ready. This usually
583 * means there is no disc in the drive. Mark as changed,
584 * and we will figure it out later once the drive is
585 * available again.
587 if (retval)
588 goto not_present;
591 * For removable scsi disk we have to recognise the presence
592 * of a disk in the drive. This is kept in the struct scsi_disk
593 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
595 sdkp->media_present = 1;
597 retval = sdp->changed;
598 sdp->changed = 0;
600 return retval;
602 not_present:
603 set_media_not_present(sdkp);
604 return 1;
607 static void sd_rescan(struct device *dev)
609 struct scsi_disk *sdkp = dev_get_drvdata(dev);
610 sd_revalidate_disk(sdkp->disk);
613 static struct block_device_operations sd_fops = {
614 .owner = THIS_MODULE,
615 .open = sd_open,
616 .release = sd_release,
617 .ioctl = sd_ioctl,
618 .media_changed = sd_media_changed,
619 .revalidate_disk = sd_revalidate_disk,
623 * sd_rw_intr - bottom half handler: called when the lower level
624 * driver has completed (successfully or otherwise) a scsi command.
625 * @SCpnt: mid-level's per command structure.
627 * Note: potentially run from within an ISR. Must not block.
629 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
631 int result = SCpnt->result;
632 int this_count = SCpnt->bufflen >> 9;
633 int good_sectors = (result == 0 ? this_count : 0);
634 sector_t block_sectors = 1;
635 sector_t error_sector;
636 #ifdef CONFIG_SCSI_LOGGING
637 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
638 SCpnt->request->rq_disk->disk_name, result));
639 if (0 != result) {
640 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[0,2,asc,ascq]"
641 "=%x,%x,%x,%x\n", SCpnt->sense_buffer[0],
642 SCpnt->sense_buffer[2], SCpnt->sense_buffer[12],
643 SCpnt->sense_buffer[13]));
645 #endif
647 Handle MEDIUM ERRORs that indicate partial success. Since this is a
648 relatively rare error condition, no care is taken to avoid
649 unnecessary additional work such as memcpy's that could be avoided.
652 /* An error occurred */
653 if (driver_byte(result) != 0 && /* An error occurred */
654 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
655 switch (SCpnt->sense_buffer[2]) {
656 case MEDIUM_ERROR:
657 if (!(SCpnt->sense_buffer[0] & 0x80))
658 break;
659 error_sector = (SCpnt->sense_buffer[3] << 24) |
660 (SCpnt->sense_buffer[4] << 16) |
661 (SCpnt->sense_buffer[5] << 8) |
662 SCpnt->sense_buffer[6];
663 if (SCpnt->request->bio != NULL)
664 block_sectors = bio_sectors(SCpnt->request->bio);
665 switch (SCpnt->device->sector_size) {
666 case 1024:
667 error_sector <<= 1;
668 if (block_sectors < 2)
669 block_sectors = 2;
670 break;
671 case 2048:
672 error_sector <<= 2;
673 if (block_sectors < 4)
674 block_sectors = 4;
675 break;
676 case 4096:
677 error_sector <<=3;
678 if (block_sectors < 8)
679 block_sectors = 8;
680 break;
681 case 256:
682 error_sector >>= 1;
683 break;
684 default:
685 break;
688 error_sector &= ~(block_sectors - 1);
689 good_sectors = error_sector - SCpnt->request->sector;
690 if (good_sectors < 0 || good_sectors >= this_count)
691 good_sectors = 0;
692 break;
694 case RECOVERED_ERROR:
696 * An error occurred, but it recovered. Inform the
697 * user, but make sure that it's not treated as a
698 * hard error.
700 print_sense("sd", SCpnt);
701 SCpnt->result = 0;
702 SCpnt->sense_buffer[0] = 0x0;
703 good_sectors = this_count;
704 break;
706 case ILLEGAL_REQUEST:
707 if (SCpnt->device->use_10_for_rw &&
708 (SCpnt->cmnd[0] == READ_10 ||
709 SCpnt->cmnd[0] == WRITE_10))
710 SCpnt->device->use_10_for_rw = 0;
711 if (SCpnt->device->use_10_for_ms &&
712 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
713 SCpnt->cmnd[0] == MODE_SELECT_10))
714 SCpnt->device->use_10_for_ms = 0;
715 break;
717 default:
718 break;
722 * This calls the generic completion function, now that we know
723 * how many actual sectors finished, and how many sectors we need
724 * to say have failed.
726 scsi_io_completion(SCpnt, good_sectors, block_sectors);
729 static int media_not_present(struct scsi_disk *sdkp, struct scsi_request *srp)
731 if (!srp->sr_result)
732 return 0;
733 if (!(driver_byte(srp->sr_result) & DRIVER_SENSE))
734 return 0;
735 if (srp->sr_sense_buffer[2] != NOT_READY &&
736 srp->sr_sense_buffer[2] != UNIT_ATTENTION)
737 return 0;
738 if (srp->sr_sense_buffer[12] != 0x3A) /* medium not present */
739 return 0;
741 set_media_not_present(sdkp);
742 return 1;
746 * spinup disk - called only in sd_revalidate_disk()
748 static void
749 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname,
750 struct scsi_request *SRpnt, unsigned char *buffer) {
751 unsigned char cmd[10];
752 unsigned long spintime_value = 0;
753 int retries, spintime;
754 unsigned int the_result;
756 spintime = 0;
758 /* Spin up drives, as required. Only do this at boot time */
759 /* Spinup needs to be done for module loads too. */
760 do {
761 retries = 0;
763 do {
764 cmd[0] = TEST_UNIT_READY;
765 memset((void *) &cmd[1], 0, 9);
767 SRpnt->sr_cmd_len = 0;
768 SRpnt->sr_sense_buffer[0] = 0;
769 SRpnt->sr_sense_buffer[2] = 0;
770 SRpnt->sr_data_direction = DMA_NONE;
772 scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
773 0/*512*/, SD_TIMEOUT, SD_MAX_RETRIES);
775 the_result = SRpnt->sr_result;
776 retries++;
777 } while (retries < 3 &&
778 (!scsi_status_is_good(the_result) ||
779 ((driver_byte(the_result) & DRIVER_SENSE) &&
780 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
783 * If the drive has indicated to us that it doesn't have
784 * any media in it, don't bother with any of the rest of
785 * this crap.
787 if (media_not_present(sdkp, SRpnt))
788 return;
790 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
791 /* no sense, TUR either succeeded or failed
792 * with a status error */
793 if(!spintime && !scsi_status_is_good(the_result))
794 printk(KERN_NOTICE "%s: Unit Not Ready, error = 0x%x\n", diskname, the_result);
795 break;
799 * The device does not want the automatic start to be issued.
801 if (sdkp->device->no_start_on_add) {
802 break;
806 * If manual intervention is required, or this is an
807 * absent USB storage device, a spinup is meaningless.
809 if (SRpnt->sr_sense_buffer[2] == NOT_READY &&
810 SRpnt->sr_sense_buffer[12] == 4 /* not ready */ &&
811 SRpnt->sr_sense_buffer[13] == 3) {
812 break; /* manual intervention required */
815 * Issue command to spin up drive when not ready
817 } else if (SRpnt->sr_sense_buffer[2] == NOT_READY) {
818 unsigned long time1;
819 if (!spintime) {
820 printk(KERN_NOTICE "%s: Spinning up disk...",
821 diskname);
822 cmd[0] = START_STOP;
823 cmd[1] = 1; /* Return immediately */
824 memset((void *) &cmd[2], 0, 8);
825 cmd[4] = 1; /* Start spin cycle */
826 SRpnt->sr_cmd_len = 0;
827 SRpnt->sr_sense_buffer[0] = 0;
828 SRpnt->sr_sense_buffer[2] = 0;
830 SRpnt->sr_data_direction = DMA_NONE;
831 scsi_wait_req(SRpnt, (void *)cmd,
832 (void *) buffer, 0/*512*/,
833 SD_TIMEOUT, SD_MAX_RETRIES);
834 spintime_value = jiffies;
836 spintime = 1;
837 time1 = HZ;
838 /* Wait 1 second for next try */
839 do {
840 current->state = TASK_UNINTERRUPTIBLE;
841 time1 = schedule_timeout(time1);
842 } while(time1);
843 printk(".");
844 } else {
845 /* we don't understand the sense code, so it's
846 * probably pointless to loop */
847 if(!spintime) {
848 printk(KERN_NOTICE "%s: Unit Not Ready, sense:\n", diskname);
849 print_req_sense("", SRpnt);
851 break;
854 } while (spintime &&
855 time_after(spintime_value + 100 * HZ, jiffies));
857 if (spintime) {
858 if (scsi_status_is_good(the_result))
859 printk("ready\n");
860 else
861 printk("not responding...\n");
866 * read disk capacity
868 static void
869 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
870 struct scsi_request *SRpnt, unsigned char *buffer) {
871 unsigned char cmd[16];
872 struct scsi_device *sdp = sdkp->device;
873 int the_result, retries;
874 int sector_size = 0;
875 int longrc = 0;
877 repeat:
878 retries = 3;
879 do {
880 if (longrc) {
881 memset((void *) cmd, 0, 16);
882 cmd[0] = SERVICE_ACTION_IN;
883 cmd[1] = SAI_READ_CAPACITY_16;
884 cmd[13] = 12;
885 memset((void *) buffer, 0, 12);
886 } else {
887 cmd[0] = READ_CAPACITY;
888 memset((void *) &cmd[1], 0, 9);
889 memset((void *) buffer, 0, 8);
892 SRpnt->sr_cmd_len = 0;
893 SRpnt->sr_sense_buffer[0] = 0;
894 SRpnt->sr_sense_buffer[2] = 0;
895 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
897 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
898 longrc ? 12 : 8, SD_TIMEOUT, SD_MAX_RETRIES);
900 if (media_not_present(sdkp, SRpnt))
901 return;
903 the_result = SRpnt->sr_result;
904 retries--;
906 } while (the_result && retries);
908 if (the_result && !longrc) {
909 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
910 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
911 diskname, diskname,
912 status_byte(the_result),
913 msg_byte(the_result),
914 host_byte(the_result),
915 driver_byte(the_result));
917 if (driver_byte(the_result) & DRIVER_SENSE)
918 print_req_sense("sd", SRpnt);
919 else
920 printk("%s : sense not available. \n", diskname);
922 /* Set dirty bit for removable devices if not ready -
923 * sometimes drives will not report this properly. */
924 if (sdp->removable &&
925 SRpnt->sr_sense_buffer[2] == NOT_READY)
926 sdp->changed = 1;
928 /* Either no media are present but the drive didn't tell us,
929 or they are present but the read capacity command fails */
930 /* sdkp->media_present = 0; -- not always correct */
931 sdkp->capacity = 0x200000; /* 1 GB - random */
933 return;
934 } else if (the_result && longrc) {
935 /* READ CAPACITY(16) has been failed */
936 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
937 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
938 diskname, diskname,
939 status_byte(the_result),
940 msg_byte(the_result),
941 host_byte(the_result),
942 driver_byte(the_result));
943 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
944 diskname);
946 sdkp->capacity = 1 + (sector_t) 0xffffffff;
947 goto got_data;
950 if (!longrc) {
951 sector_size = (buffer[4] << 24) |
952 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
953 if (buffer[0] == 0xff && buffer[1] == 0xff &&
954 buffer[2] == 0xff && buffer[3] == 0xff) {
955 if(sizeof(sdkp->capacity) > 4) {
956 printk(KERN_NOTICE "%s : very big device. try to use"
957 " READ CAPACITY(16).\n", diskname);
958 longrc = 1;
959 goto repeat;
960 } else {
961 printk(KERN_ERR "%s: too big for kernel. Assuming maximum 2Tb\n", diskname);
964 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
965 (buffer[1] << 16) |
966 (buffer[2] << 8) |
967 buffer[3]);
968 } else {
969 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
970 ((u64)buffer[1] << 48) |
971 ((u64)buffer[2] << 40) |
972 ((u64)buffer[3] << 32) |
973 ((sector_t)buffer[4] << 24) |
974 ((sector_t)buffer[5] << 16) |
975 ((sector_t)buffer[6] << 8) |
976 (sector_t)buffer[7]);
978 sector_size = (buffer[8] << 24) |
979 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
982 got_data:
983 if (sector_size == 0) {
984 sector_size = 512;
985 printk(KERN_NOTICE "%s : sector size 0 reported, "
986 "assuming 512.\n", diskname);
989 if (sector_size != 512 &&
990 sector_size != 1024 &&
991 sector_size != 2048 &&
992 sector_size != 4096 &&
993 sector_size != 256) {
994 printk(KERN_NOTICE "%s : unsupported sector size "
995 "%d.\n", diskname, sector_size);
997 * The user might want to re-format the drive with
998 * a supported sectorsize. Once this happens, it
999 * would be relatively trivial to set the thing up.
1000 * For this reason, we leave the thing in the table.
1002 sdkp->capacity = 0;
1006 * The msdos fs needs to know the hardware sector size
1007 * So I have created this table. See ll_rw_blk.c
1008 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1010 int hard_sector = sector_size;
1011 sector_t sz = sdkp->capacity * (hard_sector/256);
1012 request_queue_t *queue = sdp->request_queue;
1013 sector_t mb;
1015 blk_queue_hardsect_size(queue, hard_sector);
1016 /* avoid 64-bit division on 32-bit platforms */
1017 mb = sz >> 1;
1018 sector_div(sz, 1250);
1019 mb -= sz - 974;
1020 sector_div(mb, 1950);
1022 printk(KERN_NOTICE "SCSI device %s: "
1023 "%llu %d-byte hdwr sectors (%llu MB)\n",
1024 diskname, (unsigned long long)sdkp->capacity,
1025 hard_sector, (unsigned long long)mb);
1028 /* Rescale capacity to 512-byte units */
1029 if (sector_size == 4096)
1030 sdkp->capacity <<= 3;
1031 else if (sector_size == 2048)
1032 sdkp->capacity <<= 2;
1033 else if (sector_size == 1024)
1034 sdkp->capacity <<= 1;
1035 else if (sector_size == 256)
1036 sdkp->capacity >>= 1;
1038 sdkp->device->sector_size = sector_size;
1041 /* called with buffer of length 512 */
1042 static inline int
1043 sd_do_mode_sense(struct scsi_request *SRpnt, int dbd, int modepage,
1044 unsigned char *buffer, int len, struct scsi_mode_data *data)
1046 return __scsi_mode_sense(SRpnt, dbd, modepage, buffer, len,
1047 SD_TIMEOUT, SD_MAX_RETRIES, data);
1051 * read write protect setting, if possible - called only in sd_revalidate_disk()
1052 * called with buffer of length 512
1054 static void
1055 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1056 struct scsi_request *SRpnt, unsigned char *buffer) {
1057 int res;
1058 struct scsi_mode_data data;
1061 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1062 * We have to start carefully: some devices hang if we ask
1063 * for more than is available.
1065 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 4, &data);
1068 * Second attempt: ask for page 0
1069 * When only page 0 is implemented, a request for page 3F may return
1070 * Sense Key 5: Illegal Request, Sense Code 24: Invalid field in CDB.
1072 if (!scsi_status_is_good(res))
1073 res = sd_do_mode_sense(SRpnt, 0, 0, buffer, 4, &data);
1076 * Third attempt: ask 255 bytes, as we did earlier.
1078 if (!scsi_status_is_good(res))
1079 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 255, &data);
1081 if (!scsi_status_is_good(res)) {
1082 printk(KERN_WARNING
1083 "%s: test WP failed, assume Write Enabled\n", diskname);
1084 } else {
1085 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1086 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1087 sdkp->write_prot ? "on" : "off");
1088 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1089 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1094 * sd_read_cache_type - called only from sd_revalidate_disk()
1095 * called with buffer of length 512
1097 static void
1098 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1099 struct scsi_request *SRpnt, unsigned char *buffer) {
1100 int len = 0, res;
1102 const int dbd = 0; /* DBD */
1103 const int modepage = 0x08; /* current values, cache page */
1104 struct scsi_mode_data data;
1107 /* cautiously ask */
1108 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, 4, &data);
1110 if (!scsi_status_is_good(res))
1111 goto bad_sense;
1113 /* that went OK, now ask for the proper length */
1114 len = data.length;
1117 * We're only interested in the first three bytes, actually.
1118 * But the data cache page is defined for the first 20.
1120 if (len < 3)
1121 goto bad_sense;
1122 if (len > 20)
1123 len = 20;
1125 /* Take headers and block descriptors into account */
1126 len += data.header_length + data.block_descriptor_length;
1128 /* Get the data */
1129 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, len, &data);
1131 if (scsi_status_is_good(res)) {
1132 const char *types[] = {
1133 "write through", "none", "write back",
1134 "write back, no read (daft)"
1136 int ct = 0;
1137 int offset = data.header_length +
1138 data.block_descriptor_length + 2;
1140 sdkp->WCE = ((buffer[offset] & 0x04) != 0);
1141 sdkp->RCD = ((buffer[offset] & 0x01) != 0);
1143 ct = sdkp->RCD + 2*sdkp->WCE;
1145 printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n",
1146 diskname, types[ct]);
1148 return;
1151 bad_sense:
1152 if ((SRpnt->sr_sense_buffer[0] & 0x70) == 0x70
1153 && (SRpnt->sr_sense_buffer[2] & 0x0f) == ILLEGAL_REQUEST
1154 /* ASC 0x24 ASCQ 0x00: Invalid field in CDB */
1155 && SRpnt->sr_sense_buffer[12] == 0x24
1156 && SRpnt->sr_sense_buffer[13] == 0x00) {
1157 printk(KERN_NOTICE "%s: cache data unavailable\n",
1158 diskname);
1159 } else {
1160 printk(KERN_ERR "%s: asking for cache data failed\n",
1161 diskname);
1163 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1164 diskname);
1165 sdkp->WCE = 0;
1166 sdkp->RCD = 0;
1170 * sd_revalidate_disk - called the first time a new disk is seen,
1171 * performs disk spin up, read_capacity, etc.
1172 * @disk: struct gendisk we care about
1174 static int sd_revalidate_disk(struct gendisk *disk)
1176 struct scsi_disk *sdkp = scsi_disk(disk);
1177 struct scsi_device *sdp = sdkp->device;
1178 struct scsi_request *sreq;
1179 unsigned char *buffer;
1181 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1184 * If the device is offline, don't try and read capacity or any
1185 * of the other niceties.
1187 if (!sdp->online)
1188 goto out;
1190 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1191 if (!sreq) {
1192 printk(KERN_WARNING "(sd_revalidate_disk:) Request allocation "
1193 "failure.\n");
1194 goto out;
1197 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
1198 if (!buffer) {
1199 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1200 "failure.\n");
1201 goto out_release_request;
1204 /* defaults, until the device tells us otherwise */
1205 sdp->sector_size = 512;
1206 sdkp->capacity = 0;
1207 sdkp->media_present = 1;
1208 sdkp->write_prot = 0;
1209 sdkp->WCE = 0;
1210 sdkp->RCD = 0;
1212 sd_spinup_disk(sdkp, disk->disk_name, sreq, buffer);
1215 * Without media there is no reason to ask; moreover, some devices
1216 * react badly if we do.
1218 if (sdkp->media_present) {
1219 sd_read_capacity(sdkp, disk->disk_name, sreq, buffer);
1220 if (sdp->removable)
1221 sd_read_write_protect_flag(sdkp, disk->disk_name,
1222 sreq, buffer);
1223 sd_read_cache_type(sdkp, disk->disk_name, sreq, buffer);
1226 set_capacity(disk, sdkp->capacity);
1227 kfree(buffer);
1229 out_release_request:
1230 scsi_release_request(sreq);
1231 out:
1232 return 0;
1236 * sd_probe - called during driver initialization and whenever a
1237 * new scsi device is attached to the system. It is called once
1238 * for each scsi device (not just disks) present.
1239 * @dev: pointer to device object
1241 * Returns 0 if successful (or not interested in this scsi device
1242 * (e.g. scanner)); 1 when there is an error.
1244 * Note: this function is invoked from the scsi mid-level.
1245 * This function sets up the mapping between a given
1246 * <host,channel,id,lun> (found in sdp) and new device name
1247 * (e.g. /dev/sda). More precisely it is the block device major
1248 * and minor number that is chosen here.
1250 * Assume sd_attach is not re-entrant (for time being)
1251 * Also think about sd_attach() and sd_remove() running coincidentally.
1253 static int sd_probe(struct device *dev)
1255 struct scsi_device *sdp = to_scsi_device(dev);
1256 struct scsi_disk *sdkp;
1257 struct gendisk *gd;
1258 u32 index;
1259 int error;
1261 error = -ENODEV;
1262 if ((sdp->type != TYPE_DISK) && (sdp->type != TYPE_MOD))
1263 goto out;
1265 SCSI_LOG_HLQUEUE(3, printk("sd_attach: scsi device: <%d,%d,%d,%d>\n",
1266 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun));
1268 error = -ENOMEM;
1269 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1270 if (!sdkp)
1271 goto out;
1273 gd = alloc_disk(16);
1274 if (!gd)
1275 goto out_free;
1277 spin_lock(&sd_index_lock);
1278 index = find_first_zero_bit(sd_index_bits, SD_DISKS);
1279 if (index == SD_DISKS) {
1280 spin_unlock(&sd_index_lock);
1281 error = -EBUSY;
1282 goto out_put;
1284 __set_bit(index, sd_index_bits);
1285 spin_unlock(&sd_index_lock);
1287 sdkp->device = sdp;
1288 sdkp->driver = &sd_template;
1289 sdkp->disk = gd;
1290 sdkp->index = index;
1291 sdkp->openers = 0;
1293 gd->major = sd_major(index >> 4);
1294 gd->first_minor = (index & 15) << 4;
1295 gd->minors = 16;
1296 gd->fops = &sd_fops;
1298 if (index >= 26) {
1299 sprintf(gd->disk_name, "sd%c%c",
1300 'a' + index/26-1,'a' + index % 26);
1301 } else {
1302 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1305 strcpy(gd->devfs_name, sdp->devfs_name);
1307 gd->private_data = &sdkp->driver;
1309 sd_revalidate_disk(gd);
1311 gd->driverfs_dev = &sdp->sdev_gendev;
1312 gd->flags = GENHD_FL_DRIVERFS;
1313 if (sdp->removable)
1314 gd->flags |= GENHD_FL_REMOVABLE;
1315 gd->queue = sdkp->device->request_queue;
1317 dev_set_drvdata(dev, sdkp);
1318 add_disk(gd);
1320 printk(KERN_NOTICE "Attached scsi %sdisk %s at scsi%d, channel %d, "
1321 "id %d, lun %d\n", sdp->removable ? "removable " : "",
1322 gd->disk_name, sdp->host->host_no, sdp->channel,
1323 sdp->id, sdp->lun);
1325 return 0;
1327 out_put:
1328 put_disk(gd);
1329 out_free:
1330 kfree(sdkp);
1331 out:
1332 return error;
1336 * sd_remove - called whenever a scsi disk (previously recognized by
1337 * sd_probe) is detached from the system. It is called (potentially
1338 * multiple times) during sd module unload.
1339 * @sdp: pointer to mid level scsi device object
1341 * Note: this function is invoked from the scsi mid-level.
1342 * This function potentially frees up a device name (e.g. /dev/sdc)
1343 * that could be re-used by a subsequent sd_probe().
1344 * This function is not called when the built-in sd driver is "exit-ed".
1346 static int sd_remove(struct device *dev)
1348 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1350 del_gendisk(sdkp->disk);
1352 spin_lock(&sd_index_lock);
1353 clear_bit(sdkp->index, sd_index_bits);
1354 spin_unlock(&sd_index_lock);
1356 sd_shutdown(dev);
1357 put_disk(sdkp->disk);
1358 kfree(sdkp);
1360 return 0;
1364 * Send a SYNCHRONIZE CACHE instruction down to the device through
1365 * the normal SCSI command structure. Wait for the command to
1366 * complete.
1368 static void sd_shutdown(struct device *dev)
1370 struct scsi_device *sdp = to_scsi_device(dev);
1371 struct scsi_disk *sdkp;
1372 struct scsi_request *sreq;
1373 int retries, res;
1375 sdkp = dev_get_drvdata(dev);
1376 if (!sdkp)
1377 return; /* this can happen */
1379 if (!sdp->online || !sdkp->WCE)
1380 return;
1382 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: ",
1383 sdkp->disk->disk_name);
1385 sreq = scsi_allocate_request(sdp, GFP_KERNEL);
1386 if (!sreq) {
1387 printk("FAILED\n No memory for request\n");
1388 return;
1391 sreq->sr_data_direction = DMA_NONE;
1392 for (retries = 3; retries > 0; --retries) {
1393 unsigned char cmd[10] = { 0 };
1395 cmd[0] = SYNCHRONIZE_CACHE;
1397 * Leave the rest of the command zero to indicate
1398 * flush everything.
1400 scsi_wait_req(sreq, cmd, NULL, 0, SD_TIMEOUT, SD_MAX_RETRIES);
1401 if (sreq->sr_result == 0)
1402 break;
1405 res = sreq->sr_result;
1406 if (res) {
1407 printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
1408 "host = %d, driver = %02x\n ",
1409 status_byte(res), msg_byte(res),
1410 host_byte(res), driver_byte(res));
1411 if (driver_byte(res) & DRIVER_SENSE)
1412 print_req_sense("sd", sreq);
1415 scsi_release_request(sreq);
1416 printk("\n");
1420 * init_sd - entry point for this driver (both when built in or when
1421 * a module).
1423 * Note: this function registers this driver with the scsi mid-level.
1425 static int __init init_sd(void)
1427 int majors = 0, i;
1429 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1431 for (i = 0; i < SD_MAJORS; i++)
1432 if (register_blkdev(sd_major(i), "sd") == 0)
1433 majors++;
1435 if (!majors)
1436 return -ENODEV;
1438 return scsi_register_driver(&sd_template.gendrv);
1442 * exit_sd - exit point for this driver (when it is a module).
1444 * Note: this function unregisters this driver from the scsi mid-level.
1446 static void __exit exit_sd(void)
1448 int i;
1450 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1452 scsi_unregister_driver(&sd_template.gendrv);
1453 for (i = 0; i < SD_MAJORS; i++)
1454 unregister_blkdev(sd_major(i), "sd");
1457 MODULE_LICENSE("GPL");
1458 MODULE_AUTHOR("Eric Youngdale");
1459 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1461 module_init(init_sd);
1462 module_exit(exit_sd);