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>
35 #include <linux/kernel.h>
36 #include <linux/sched.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>
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
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
78 struct scsi_driver
*driver
; /* always &sd_template */
79 struct scsi_device
*device
;
81 unsigned int openers
; /* protected by BKL for now, yuck */
82 sector_t capacity
; /* size in 512-byte sectors */
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
,
110 .shutdown
= sd_shutdown
,
113 .init_command
= sd_init_command
,
116 static int sd_major(int major_idx
)
120 return SCSI_DISK0_MAJOR
;
122 return SCSI_DISK1_MAJOR
+ major_idx
- 1;
124 return SCSI_DISK8_MAJOR
+ major_idx
- 8;
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
;
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
))
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
;
170 SCpnt
->sc_data_direction
= DMA_NONE
;
172 this_count
= rq
->data_len
;
174 timeout
= rq
->timeout
;
176 SCpnt
->transfersize
= rq
->data_len
;
181 * we only do REQ_CMD and REQ_BLOCK_PC
183 if (!(SCpnt
->request
->flags
& REQ_CMD
))
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
));
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"); */
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
223 if (sdp
->sector_size
== 1024) {
224 if ((block
& 1) || (SCpnt
->request
->nr_sectors
& 1)) {
225 printk(KERN_ERR
"sd: Bad block number requested");
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");
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");
247 this_count
= this_count
>> 3;
250 if (rq_data_dir(SCpnt
->request
) == WRITE
) {
251 if (!sdp
->writeable
) {
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
;
260 printk(KERN_ERR
"sd: Unknown command %lx\n",
261 SCpnt
->request
->flags
);
262 /* overkill panic("Unknown sd command %lx\n", SCpnt->request->flags); */
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
));
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)
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;
301 if (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
;
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;
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
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
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
;
358 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk
->disk_name
));
360 retval
= scsi_device_get(sdev
);
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.
369 if (!scsi_block_when_processing_errors(sdev
))
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.
379 if (sdev
->removable
&& !sdkp
->media_present
&&
380 !(filp
->f_flags
& O_NDELAY
))
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.
388 if (sdkp
->write_prot
&& (filp
->f_mode
& FMODE_WRITE
))
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.
401 if (!sdkp
->openers
++ && sdev
->removable
) {
402 if (scsi_block_when_processing_errors(sdev
))
403 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
409 scsi_device_put(sdev
);
414 * sd_release - invoked when the (last) close(2) is called on this
416 * @inode: only i_rdev member may be used
417 * @filp: only f_mode and f_flags may be used
421 * Note: may block (uninterruptible) if error recovery is underway
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
);
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
;
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
);
461 scsicam_bios_param(bdev
, sdkp
->capacity
, diskinfo
);
463 if (put_user(diskinfo
[0], &loc
->heads
))
465 if (put_user(diskinfo
[1], &loc
->sectors
))
467 if (put_user(diskinfo
[2], &loc
->cylinders
))
469 if (put_user((unsigned)get_start_sect(bdev
),
470 (unsigned long *)&loc
->start
))
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
;
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
))
509 if (cmd
== HDIO_GETGEO
) {
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
521 case SCSI_IOCTL_GET_IDLUN
:
522 case SCSI_IOCTL_GET_BUS_NUMBER
:
523 return scsi_ioctl(sdp
, cmd
, (void *)arg
);
525 error
= scsi_cmd_ioctl(bdev
, cmd
, arg
);
526 if (error
!= -ENOTTY
)
529 return scsi_ioctl(sdp
, cmd
, (void *)arg
);
532 static void set_media_not_present(struct scsi_disk
*sdkp
)
534 sdkp
->media_present
= 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
;
553 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
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.
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.
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
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
;
603 set_media_not_present(sdkp
);
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
,
616 .release
= sd_release
,
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
));
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]));
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]) {
657 if (!(SCpnt
->sense_buffer
[0] & 0x80))
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
) {
668 if (block_sectors
< 2)
673 if (block_sectors
< 4)
678 if (block_sectors
< 8)
688 error_sector
&= ~(block_sectors
- 1);
689 good_sectors
= error_sector
- SCpnt
->request
->sector
;
690 if (good_sectors
< 0 || good_sectors
>= this_count
)
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
700 print_sense("sd", SCpnt
);
702 SCpnt
->sense_buffer
[0] = 0x0;
703 good_sectors
= this_count
;
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;
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
)
733 if (!(driver_byte(srp
->sr_result
) & DRIVER_SENSE
))
735 if (srp
->sr_sense_buffer
[2] != NOT_READY
&&
736 srp
->sr_sense_buffer
[2] != UNIT_ATTENTION
)
738 if (srp
->sr_sense_buffer
[12] != 0x3A) /* medium not present */
741 set_media_not_present(sdkp
);
746 * spinup disk - called only in sd_revalidate_disk()
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
;
758 /* Spin up drives, as required. Only do this at boot time */
759 /* Spinup needs to be done for module loads too. */
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
;
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
787 if (media_not_present(sdkp
, SRpnt
))
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
);
799 * The device does not want the automatic start to be issued.
801 if (sdkp
->device
->no_start_on_add
) {
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
) {
820 printk(KERN_NOTICE
"%s: Spinning up disk...",
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
;
838 /* Wait 1 second for next try */
840 current
->state
= TASK_UNINTERRUPTIBLE
;
841 time1
= schedule_timeout(time1
);
845 /* we don't understand the sense code, so it's
846 * probably pointless to loop */
848 printk(KERN_NOTICE
"%s: Unit Not Ready, sense:\n", diskname
);
849 print_req_sense("", SRpnt
);
855 time_after(spintime_value
+ 100 * HZ
, jiffies
));
858 if (scsi_status_is_good(the_result
))
861 printk("not responding...\n");
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
;
881 memset((void *) cmd
, 0, 16);
882 cmd
[0] = SERVICE_ACTION_IN
;
883 cmd
[1] = SAI_READ_CAPACITY_16
;
885 memset((void *) buffer
, 0, 12);
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
))
903 the_result
= SRpnt
->sr_result
;
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",
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
);
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
)
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 */
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",
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",
946 sdkp
->capacity
= 1 + (sector_t
) 0xffffffff;
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
);
961 printk(KERN_ERR
"%s: too big for kernel. Assuming maximum 2Tb\n", diskname
);
964 sdkp
->capacity
= 1 + (((sector_t
)buffer
[0] << 24) |
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];
983 if (sector_size
== 0) {
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.
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
;
1015 blk_queue_hardsect_size(queue
, hard_sector
);
1016 /* avoid 64-bit division on 32-bit platforms */
1018 sector_div(sz
, 1250);
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 */
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
1055 sd_read_write_protect_flag(struct scsi_disk
*sdkp
, char *diskname
,
1056 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
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
)) {
1083 "%s: test WP failed, assume Write Enabled\n", diskname
);
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
1098 sd_read_cache_type(struct scsi_disk
*sdkp
, char *diskname
,
1099 struct scsi_request
*SRpnt
, unsigned char *buffer
) {
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
))
1113 /* that went OK, now ask for the proper length */
1117 * We're only interested in the first three bytes, actually.
1118 * But the data cache page is defined for the first 20.
1125 /* Take headers and block descriptors into account */
1126 len
+= data
.header_length
+ data
.block_descriptor_length
;
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)"
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
]);
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",
1160 printk(KERN_ERR
"%s: asking for cache data failed\n",
1163 printk(KERN_ERR
"%s: assuming drive cache: write through\n",
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.
1190 sreq
= scsi_allocate_request(sdp
, GFP_KERNEL
);
1192 printk(KERN_WARNING
"(sd_revalidate_disk:) Request allocation "
1197 buffer
= kmalloc(512, GFP_KERNEL
| __GFP_DMA
);
1199 printk(KERN_WARNING
"(sd_revalidate_disk:) Memory allocation "
1201 goto out_release_request
;
1204 /* defaults, until the device tells us otherwise */
1205 sdp
->sector_size
= 512;
1207 sdkp
->media_present
= 1;
1208 sdkp
->write_prot
= 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
);
1221 sd_read_write_protect_flag(sdkp
, disk
->disk_name
,
1223 sd_read_cache_type(sdkp
, disk
->disk_name
, sreq
, buffer
);
1226 set_capacity(disk
, sdkp
->capacity
);
1229 out_release_request
:
1230 scsi_release_request(sreq
);
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
;
1262 if ((sdp
->type
!= TYPE_DISK
) && (sdp
->type
!= TYPE_MOD
))
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
));
1269 sdkp
= kmalloc(sizeof(*sdkp
), GFP_KERNEL
);
1273 gd
= alloc_disk(16);
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
);
1284 __set_bit(index
, sd_index_bits
);
1285 spin_unlock(&sd_index_lock
);
1288 sdkp
->driver
= &sd_template
;
1290 sdkp
->index
= index
;
1293 gd
->major
= sd_major(index
>> 4);
1294 gd
->first_minor
= (index
& 15) << 4;
1296 gd
->fops
= &sd_fops
;
1299 sprintf(gd
->disk_name
, "sd%c%c",
1300 'a' + index
/26-1,'a' + index
% 26);
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
;
1314 gd
->flags
|= GENHD_FL_REMOVABLE
;
1315 gd
->queue
= sdkp
->device
->request_queue
;
1317 dev_set_drvdata(dev
, sdkp
);
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
,
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
);
1357 put_disk(sdkp
->disk
);
1364 * Send a SYNCHRONIZE CACHE instruction down to the device through
1365 * the normal SCSI command structure. Wait for the command to
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
;
1375 sdkp
= dev_get_drvdata(dev
);
1377 return; /* this can happen */
1379 if (!sdp
->online
|| !sdkp
->WCE
)
1382 printk(KERN_NOTICE
"Synchronizing SCSI cache for disk %s: ",
1383 sdkp
->disk
->disk_name
);
1385 sreq
= scsi_allocate_request(sdp
, GFP_KERNEL
);
1387 printk("FAILED\n No memory for request\n");
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
1400 scsi_wait_req(sreq
, cmd
, NULL
, 0, SD_TIMEOUT
, SD_MAX_RETRIES
);
1401 if (sreq
->sr_result
== 0)
1405 res
= sreq
->sr_result
;
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
);
1420 * init_sd - entry point for this driver (both when built in or when
1423 * Note: this function registers this driver with the scsi mid-level.
1425 static int __init
init_sd(void)
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)
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)
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
);