2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
11 * Modified by Eric Youngdale ericy@cais.com to
12 * add scatter-gather, multiple outstanding request, and other
15 * Modified by Eric Youngdale eric@aib.com to support loadable
16 * low-level scsi drivers.
18 * Modified by Jirka Hanika geo@ff.cuni.cz to support more
19 * scsi disks using eight major numbers.
22 #include <linux/module.h>
25 * This is a variable in scsi.c that is set when we are processing something
26 * after boot time. By definition, this is true when we are a loadable module
31 #define MODULE_FLAG scsi_loadable_module_flag
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/interrupt.h>
42 #include <linux/smp.h>
44 #include <asm/system.h>
47 #define MAJOR_NR SCSI_DISK0_MAJOR
48 #include <linux/blk.h>
52 #include <scsi/scsi_ioctl.h>
53 #include "constants.h"
55 #include <linux/genhd.h>
58 * static const char RCSid[] = "$Header:";
61 #define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
63 #define SCSI_DISKS_PER_MAJOR 16
64 #define SD_MAJOR_NUMBER(i) SD_MAJOR((i) >> 8)
65 #define SD_MINOR_NUMBER(i) ((i) & 255)
66 #define MKDEV_SD_PARTITION(i) MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
67 #define MKDEV_SD(index) MKDEV_SD_PARTITION((index) << 4)
68 #define N_USED_SCSI_DISKS (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
69 #define N_USED_SD_MAJORS (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
74 * Time out in seconds for disks and Magneto-opticals (which are slower).
77 #define SD_TIMEOUT (30 * HZ)
78 #define SD_MOD_TIMEOUT (75 * HZ)
80 #define CLUSTERABLE_DEVICE(SC) (SC->host->use_clustering && \
81 SC->device->type != TYPE_MOD)
85 Scsi_Disk
*rscsi_disks
= NULL
;
87 static int *sd_blocksizes
;
88 static int *sd_hardsizes
; /* Hardware sector size */
90 extern int sd_ioctl(struct inode
*, struct file
*, unsigned int, unsigned long);
92 static int check_scsidisk_media_change(kdev_t
);
93 static int fop_revalidate_scsidisk(kdev_t
);
95 static int sd_init_onedisk(int);
97 static void requeue_sd_request(Scsi_Cmnd
* SCpnt
);
99 static int sd_init(void);
100 static void sd_finish(void);
101 static int sd_attach(Scsi_Device
*);
102 static int sd_detect(Scsi_Device
*);
103 static void sd_detach(Scsi_Device
*);
105 static void sd_devname(unsigned int disknum
, char *buffer
)
108 sprintf(buffer
, "sd%c", 'a' + disknum
);
113 * For larger numbers of disks, we need to go to a new
118 sprintf(buffer
, "sd%c%c", 'a' + min1
- 1, 'a' + min2
);
122 struct Scsi_Device_Template sd_template
=
123 {NULL
, "disk", "sd", NULL
, TYPE_DISK
,
124 SCSI_DISK0_MAJOR
, 0, 0, 0, 1,
126 sd_finish
, sd_attach
, sd_detach
129 static int sd_open(struct inode
*inode
, struct file
*filp
)
132 target
= DEVICE_NR(inode
->i_rdev
);
134 SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target
, sd_template
.dev_max
));
136 if (target
>= sd_template
.dev_max
|| !rscsi_disks
[target
].device
)
137 return -ENXIO
; /* No such device */
140 * If the device is in error recovery, wait until it is done.
141 * If the device is offline, then disallow any access to it.
143 if (!scsi_block_when_processing_errors(rscsi_disks
[target
].device
)) {
147 * Make sure that only one process can do a check_change_disk at one time.
148 * This is also used to lock out further access when the partition table
152 while (rscsi_disks
[target
].device
->busy
)
154 if (rscsi_disks
[target
].device
->removable
) {
155 check_disk_change(inode
->i_rdev
);
158 * If the drive is empty, just let the open fail.
160 if (!rscsi_disks
[target
].ready
)
164 * Similarly, if the device has the write protect tab set,
165 * have the open fail if the user expects to be able to write
168 if ((rscsi_disks
[target
].write_prot
) && (filp
->f_mode
& 2))
172 * It is possible that the disk changing stuff resulted in the device being taken
173 * offline. If this is the case, report this to the user, and don't pretend that
174 * the open actually succeeded.
176 if (!rscsi_disks
[target
].device
->online
) {
180 * See if we are requesting a non-existent partition. Do this
181 * after checking for disk change.
183 if (sd_sizes
[SD_PARTITION(inode
->i_rdev
)] == 0)
186 if (rscsi_disks
[target
].device
->removable
)
187 if (!rscsi_disks
[target
].device
->access_count
)
188 sd_ioctl(inode
, NULL
, SCSI_IOCTL_DOORLOCK
, 0);
190 rscsi_disks
[target
].device
->access_count
++;
191 if (rscsi_disks
[target
].device
->host
->hostt
->module
)
192 __MOD_INC_USE_COUNT(rscsi_disks
[target
].device
->host
->hostt
->module
);
193 if (sd_template
.module
)
194 __MOD_INC_USE_COUNT(sd_template
.module
);
198 static int sd_release(struct inode
*inode
, struct file
*file
)
201 fsync_dev(inode
->i_rdev
);
203 target
= DEVICE_NR(inode
->i_rdev
);
205 rscsi_disks
[target
].device
->access_count
--;
207 if (rscsi_disks
[target
].device
->removable
) {
208 if (!rscsi_disks
[target
].device
->access_count
)
209 sd_ioctl(inode
, NULL
, SCSI_IOCTL_DOORUNLOCK
, 0);
211 if (rscsi_disks
[target
].device
->host
->hostt
->module
)
212 __MOD_DEC_USE_COUNT(rscsi_disks
[target
].device
->host
->hostt
->module
);
213 if (sd_template
.module
)
214 __MOD_DEC_USE_COUNT(sd_template
.module
);
218 static void sd_geninit(struct gendisk
*);
220 static struct file_operations sd_fops
=
222 NULL
, /* lseek - default */
223 block_read
, /* read - general block-dev read */
224 block_write
, /* write - general block-dev write */
225 NULL
, /* readdir - bad */
227 sd_ioctl
, /* ioctl */
229 sd_open
, /* open code */
231 sd_release
, /* release */
232 block_fsync
, /* fsync */
234 check_scsidisk_media_change
, /* Disk change */
235 fop_revalidate_scsidisk
/* revalidate */
239 * If we need more than one SCSI disk major (i.e. more than
240 * 16 SCSI disks), we'll have to kmalloc() more gendisks later.
243 static struct gendisk sd_gendisk
=
245 SCSI_DISK0_MAJOR
, /* Major number */
246 "sd", /* Major name */
247 4, /* Bits to shift to get real from partition */
248 1 << 4, /* Number of partitions per real */
249 0, /* maximum number of real */
250 sd_geninit
, /* init function */
251 NULL
, /* hd struct */
252 NULL
, /* block sizes */
258 static struct gendisk
*sd_gendisks
= &sd_gendisk
;
260 #define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
261 #define LAST_SD_GENDISK sd_gendisks[N_USED_SD_MAJORS - 1]
263 static void sd_geninit(struct gendisk
*ignored
)
267 for (i
= 0; i
< sd_template
.dev_max
; ++i
)
268 if (rscsi_disks
[i
].device
)
269 sd
[i
<< 4].nr_sects
= rscsi_disks
[i
].capacity
;
273 * rw_intr is the interrupt routine for the device driver.
274 * It will be notified on the end of a SCSI read / write, and
275 * will take one of several actions based on success or failure.
278 static void rw_intr(Scsi_Cmnd
* SCpnt
)
280 int result
= SCpnt
->result
;
282 int this_count
= SCpnt
->bufflen
>> 9;
283 int good_sectors
= (result
== 0 ? this_count
: 0);
284 int block_sectors
= 1;
286 sd_devname(DEVICE_NR(SCpnt
->request
.rq_dev
), nbuff
);
288 SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff
,
289 SCpnt
->host
->host_no
,
291 SCpnt
->sense_buffer
[0],
292 SCpnt
->sense_buffer
[2]));
295 Handle MEDIUM ERRORs that indicate partial success. Since this is a
296 relatively rare error condition, no care is taken to avoid unnecessary
297 additional work such as memcpy's that could be avoided.
300 if (driver_byte(result
) != 0 && /* An error occurred */
301 SCpnt
->sense_buffer
[0] == 0xF0 && /* Sense data is valid */
302 SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
) {
303 long error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
304 (SCpnt
->sense_buffer
[4] << 16) |
305 (SCpnt
->sense_buffer
[5] << 8) |
306 SCpnt
->sense_buffer
[6];
308 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].sector_size
;
309 if (SCpnt
->request
.bh
!= NULL
)
310 block_sectors
= SCpnt
->request
.bh
->b_size
>> 9;
311 if (sector_size
== 1024) {
313 if (block_sectors
< 2)
315 } else if (sector_size
== 2048) {
317 if (block_sectors
< 4)
319 } else if (sector_size
== 256)
321 error_sector
-= sd
[SD_PARTITION(SCpnt
->request
.rq_dev
)].start_sect
;
322 error_sector
&= ~(block_sectors
- 1);
323 good_sectors
= error_sector
- SCpnt
->request
.sector
;
324 if (good_sectors
< 0 || good_sectors
>= this_count
)
328 * First case : we assume that the command succeeded. One of two things
329 * will happen here. Either we will be finished, or there will be more
330 * sectors that we were unable to read last time.
333 if (good_sectors
> 0) {
335 SCSI_LOG_HLCOMPLETE(1, printk("%s : %ld sectors remain.\n", nbuff
,
336 SCpnt
->request
.nr_sectors
));
337 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n ", SCpnt
->use_sg
));
340 struct scatterlist
*sgpnt
;
342 sgpnt
= (struct scatterlist
*) SCpnt
->buffer
;
343 for (i
= 0; i
< SCpnt
->use_sg
; i
++) {
346 SCSI_LOG_HLCOMPLETE(3, printk(":%p %p %d\n", sgpnt
[i
].alt_address
, sgpnt
[i
].address
,
350 if (sgpnt
[i
].alt_address
) {
351 if (SCpnt
->request
.cmd
== READ
)
352 memcpy(sgpnt
[i
].alt_address
, sgpnt
[i
].address
,
354 scsi_free(sgpnt
[i
].address
, sgpnt
[i
].length
);
358 /* Free list of scatter-gather pointers */
359 scsi_free(SCpnt
->buffer
, SCpnt
->sglist_len
);
361 if (SCpnt
->buffer
!= SCpnt
->request
.buffer
) {
362 SCSI_LOG_HLCOMPLETE(3, printk("nosg: %p %p %d\n",
363 SCpnt
->request
.buffer
, SCpnt
->buffer
,
366 if (SCpnt
->request
.cmd
== READ
)
367 memcpy(SCpnt
->request
.buffer
, SCpnt
->buffer
,
369 scsi_free(SCpnt
->buffer
, SCpnt
->bufflen
);
373 * If multiple sectors are requested in one buffer, then
374 * they will have been finished off by the first command.
375 * If not, then we have a multi-buffer command.
377 if (SCpnt
->request
.nr_sectors
> this_count
) {
378 SCpnt
->request
.errors
= 0;
380 if (!SCpnt
->request
.bh
) {
381 SCSI_LOG_HLCOMPLETE(2, printk("%s : handling page request, no buffer\n",
385 * The SCpnt->request.nr_sectors field is always done in
386 * 512 byte sectors, even if this really isn't the case.
388 panic("sd.c: linked page request (%lx %x)",
389 SCpnt
->request
.sector
, this_count
);
392 SCpnt
= end_scsi_request(SCpnt
, 1, good_sectors
);
394 requeue_sd_request(SCpnt
);
398 if (good_sectors
== 0) {
400 /* Free up any indirection buffers we allocated for DMA purposes. */
402 struct scatterlist
*sgpnt
;
404 sgpnt
= (struct scatterlist
*) SCpnt
->buffer
;
405 for (i
= 0; i
< SCpnt
->use_sg
; i
++) {
406 SCSI_LOG_HLCOMPLETE(3, printk("err: %p %p %d\n",
407 SCpnt
->request
.buffer
, SCpnt
->buffer
,
409 if (sgpnt
[i
].alt_address
) {
410 scsi_free(sgpnt
[i
].address
, sgpnt
[i
].length
);
413 scsi_free(SCpnt
->buffer
, SCpnt
->sglist_len
); /* Free list of scatter-gather pointers */
415 SCSI_LOG_HLCOMPLETE(2, printk("nosgerr: %p %p %d\n",
416 SCpnt
->request
.buffer
, SCpnt
->buffer
,
418 if (SCpnt
->buffer
!= SCpnt
->request
.buffer
)
419 scsi_free(SCpnt
->buffer
, SCpnt
->bufflen
);
423 * Now, if we were good little boys and girls, Santa left us a request
424 * sense buffer. We can extract information from this, so we
425 * can choose a block to remap, etc.
428 if (driver_byte(result
) != 0) {
429 if (suggestion(result
) == SUGGEST_REMAP
) {
432 * Not yet implemented. A read will fail after being remapped,
433 * a write will call the strategy routine again.
436 [DEVICE_NR(SCpnt
->request
.rq_dev
)].remap
442 if ((SCpnt
->sense_buffer
[0] & 0x7f) == 0x70) {
443 if ((SCpnt
->sense_buffer
[2] & 0xf) == UNIT_ATTENTION
) {
444 if (rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->removable
) {
445 /* detected disc change. set a bit and quietly refuse
448 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->changed
= 1;
449 SCpnt
= end_scsi_request(SCpnt
, 0, this_count
);
450 requeue_sd_request(SCpnt
);
454 * Must have been a power glitch, or a bus reset.
455 * Could not have been a media change, so we just retry
456 * the request and see what happens.
458 requeue_sd_request(SCpnt
);
463 /* If we had an ILLEGAL REQUEST returned, then we may have
464 * performed an unsupported command. The only thing this should be
465 * would be a ten byte read where only a six byte read was supported.
466 * Also, on a system where READ CAPACITY failed, we have have read
467 * past the end of the disk.
470 if (SCpnt
->sense_buffer
[2] == ILLEGAL_REQUEST
) {
471 if (rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].ten
) {
472 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].ten
= 0;
473 requeue_sd_request(SCpnt
);
479 if (SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
) {
480 printk("scsi%d: MEDIUM ERROR on channel %d, id %d, lun %d, CDB: ",
481 SCpnt
->host
->host_no
, (int) SCpnt
->channel
,
482 (int) SCpnt
->target
, (int) SCpnt
->lun
);
483 print_command(SCpnt
->cmnd
);
484 print_sense("sd", SCpnt
);
485 SCpnt
= end_scsi_request(SCpnt
, 0, block_sectors
);
486 requeue_sd_request(SCpnt
);
489 } /* driver byte != 0 */
491 printk("SCSI disk error : host %d channel %d id %d lun %d return code = %x\n",
492 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->host
->host_no
,
493 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->channel
,
494 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->id
,
495 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->lun
, result
);
497 if (driver_byte(result
) & DRIVER_SENSE
)
498 print_sense("sd", SCpnt
);
499 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.current_nr_sectors
);
500 requeue_sd_request(SCpnt
);
505 * requeue_sd_request() is the request handler function for the sd driver.
506 * Its function in life is to take block device requests, and translate
507 * them to SCSI commands.
510 static void do_sd_request(void)
512 Scsi_Cmnd
*SCpnt
= NULL
;
514 struct request
*req
= NULL
;
518 if (CURRENT
!= NULL
&& CURRENT
->rq_status
== RQ_INACTIVE
) {
522 SDev
= rscsi_disks
[CURRENT_DEV
].device
;
525 * If the host for this device is in error recovery mode, don't
526 * do anything at all here. When the host leaves error recovery
527 * mode, it will automatically restart things and start queueing
530 if (SDev
->host
->in_recovery
) {
534 * I am not sure where the best place to do this is. We need
535 * to hook in a place where we are likely to come if in user
538 if (SDev
->was_reset
) {
540 * We need to relock the door, but we might
541 * be in an interrupt handler. Only do this
542 * from user space, since we do not want to
543 * sleep from an interrupt. FIXME(eric) - do this
544 * from the kernel error handling thred.
546 if (SDev
->removable
&& !in_interrupt()) {
547 spin_unlock_irq(&io_request_lock
); /* FIXME!!!! */
548 scsi_ioctl(SDev
, SCSI_IOCTL_DOORLOCK
, 0);
549 /* scsi_ioctl may allow CURRENT to change, so start over. */
551 spin_lock_irq(&io_request_lock
); /* FIXME!!!! */
556 /* We have to be careful here. scsi_allocate_device will get a free pointer,
557 * but there is no guarantee that it is queueable. In normal usage,
558 * we want to call this, because other types of devices may have the
559 * host all tied up, and we want to make sure that we have at least
560 * one request pending for this type of device. We can also come
561 * through here while servicing an interrupt, because of the need to
562 * start another command. If we call scsi_allocate_device more than once,
563 * then the system can wedge if the command is not queueable. The
564 * scsi_request_queueable function is safe because it checks to make sure
565 * that the host is able to take another command before it returns
570 SCpnt
= scsi_allocate_device(&CURRENT
,
571 rscsi_disks
[CURRENT_DEV
].device
, 0);
576 * The following restore_flags leads to latency problems. FIXME.
577 * Using a "sti()" gets rid of the latency problems but causes
578 * race conditions and crashes.
581 /* This is a performance enhancement. We dig down into the request
582 * list and try to find a queueable request (i.e. device not busy,
583 * and host able to accept another command. If we find one, then we
584 * queue it. This can make a big difference on systems with more than
585 * one disk drive. We want to have the interrupts off when monkeying
586 * with the request list, because otherwise the kernel might try to
587 * slip in a request in between somewhere.
589 * FIXME(eric) - this doesn't belong at this level. The device code in
590 * ll_rw_blk.c should know how to dig down into the device queue to
591 * figure out what it can deal with, and what it can't. Consider
592 * possibility of pulling entire queue down into scsi layer.
594 if (!SCpnt
&& sd_template
.nr_dev
> 1) {
595 struct request
*req1
;
599 SCpnt
= scsi_request_queueable(req
,
600 rscsi_disks
[DEVICE_NR(req
->rq_dev
)].device
);
606 if (SCpnt
&& req
->rq_status
== RQ_INACTIVE
) {
608 CURRENT
= CURRENT
->next
;
610 req1
->next
= req
->next
;
614 return; /* Could not find anything to do */
617 requeue_sd_request(SCpnt
);
621 static void requeue_sd_request(Scsi_Cmnd
* SCpnt
)
623 int dev
, devm
, block
, this_count
;
624 unsigned char cmd
[10];
626 int bounce_size
, contiguous
;
628 struct buffer_head
*bh
, *bhp
;
629 char *buff
, *bounce_buffer
;
633 if (!SCpnt
|| SCpnt
->request
.rq_status
== RQ_INACTIVE
) {
637 devm
= SD_PARTITION(SCpnt
->request
.rq_dev
);
638 dev
= DEVICE_NR(SCpnt
->request
.rq_dev
);
640 block
= SCpnt
->request
.sector
;
643 SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = %d, block = %d\n", devm
, block
));
645 if (devm
>= (sd_template
.dev_max
<< 4) ||
646 !rscsi_disks
[dev
].device
||
647 !rscsi_disks
[dev
].device
->online
||
648 block
+ SCpnt
->request
.nr_sectors
> sd
[devm
].nr_sects
) {
649 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt
->request
.nr_sectors
));
650 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
651 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
654 block
+= sd
[devm
].start_sect
;
656 if (rscsi_disks
[dev
].device
->changed
) {
658 * quietly refuse to do anything to a changed disc until the changed
661 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
662 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
665 sd_devname(devm
>> 4, nbuff
);
666 SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
670 * If we have a 1K hardware sectorsize, prevent access to single
671 * 512 byte sectors. In theory we could handle this - in fact
672 * the scsi cdrom driver must be able to handle this because
673 * we typically use 1K blocksizes, and cdroms typically have
674 * 2K hardware sectorsizes. Of course, things are simpler
675 * with the cdrom, since it is read-only. For performance
676 * reasons, the filesystems should be able to handle this
677 * and not force the scsi disk driver to use bounce buffers
680 if (rscsi_disks
[dev
].sector_size
== 1024)
681 if ((block
& 1) || (SCpnt
->request
.nr_sectors
& 1)) {
682 printk("sd.c:Bad block number/count requested");
683 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
686 if (rscsi_disks
[dev
].sector_size
== 2048)
687 if ((block
& 3) || (SCpnt
->request
.nr_sectors
& 3)) {
688 printk("sd.c:Bad block number/count requested");
689 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
692 if (rscsi_disks
[dev
].sector_size
== 4096)
693 if ((block
& 7) || (SCpnt
->request
.nr_sectors
& 7)) {
694 printk("sd.cBad block number/count requested");
695 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
698 switch (SCpnt
->request
.cmd
) {
700 if (!rscsi_disks
[dev
].device
->writeable
) {
701 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
710 panic("Unknown sd command %d\n", SCpnt
->request
.cmd
);
713 SCpnt
->this_count
= 0;
715 /* If the host adapter can deal with very large scatter-gather
716 * requests, it is a waste of time to cluster
718 contiguous
= (!CLUSTERABLE_DEVICE(SCpnt
) ? 0 : 1);
719 bounce_buffer
= NULL
;
720 bounce_size
= (SCpnt
->request
.nr_sectors
<< 9);
722 /* First see if we need a bounce buffer for this request. If we do, make
723 * sure that we can allocate a buffer. Do not waste space by allocating
724 * a bounce buffer if we are straddling the 16Mb line
726 if (contiguous
&& SCpnt
->request
.bh
&&
727 virt_to_phys(SCpnt
->request
.bh
->b_data
)
728 + (SCpnt
->request
.nr_sectors
<< 9) - 1 > ISA_DMA_THRESHOLD
729 && SCpnt
->host
->unchecked_isa_dma
) {
730 if (virt_to_phys(SCpnt
->request
.bh
->b_data
) > ISA_DMA_THRESHOLD
)
731 bounce_buffer
= (char *) scsi_malloc(bounce_size
);
735 if (contiguous
&& SCpnt
->request
.bh
&& SCpnt
->request
.bh
->b_reqnext
)
736 for (bh
= SCpnt
->request
.bh
, bhp
= bh
->b_reqnext
; bhp
; bh
= bhp
,
737 bhp
= bhp
->b_reqnext
) {
738 if (!CONTIGUOUS_BUFFERS(bh
, bhp
)) {
740 scsi_free(bounce_buffer
, bounce_size
);
745 if (!SCpnt
->request
.bh
|| contiguous
) {
747 /* case of page request (i.e. raw device), or unlinked buffer */
748 this_count
= SCpnt
->request
.nr_sectors
;
749 buff
= SCpnt
->request
.buffer
;
752 } else if (SCpnt
->host
->sg_tablesize
== 0 ||
753 (scsi_need_isa_buffer
&& scsi_dma_free_sectors
<= 10)) {
755 /* Case of host adapter that cannot scatter-gather. We also
756 * come here if we are running low on DMA buffer memory. We set
757 * a threshold higher than that we would need for this request so
758 * we leave room for other requests. Even though we would not need
759 * it all, we need to be conservative, because if we run low enough
760 * we have no choice but to panic.
762 if (SCpnt
->host
->sg_tablesize
!= 0 &&
763 scsi_need_isa_buffer
&&
764 scsi_dma_free_sectors
<= 10)
765 printk("Warning: SCSI DMA buffer space running low. Using non scatter-gather I/O.\n");
767 this_count
= SCpnt
->request
.current_nr_sectors
;
768 buff
= SCpnt
->request
.buffer
;
773 /* Scatter-gather capable host adapter */
774 struct scatterlist
*sgpnt
;
775 int count
, this_count_max
;
778 bh
= SCpnt
->request
.bh
;
780 this_count_max
= (rscsi_disks
[dev
].ten
? 0xffff : 0xff);
784 if ((this_count
+ (bh
->b_size
>> 9)) > this_count_max
)
786 if (!bhp
|| !CONTIGUOUS_BUFFERS(bhp
, bh
) ||
787 !CLUSTERABLE_DEVICE(SCpnt
) ||
788 (SCpnt
->host
->unchecked_isa_dma
&&
789 virt_to_phys(bh
->b_data
- 1) == ISA_DMA_THRESHOLD
)) {
790 if (count
< SCpnt
->host
->sg_tablesize
)
795 this_count
+= (bh
->b_size
>> 9);
800 if (SCpnt
->host
->unchecked_isa_dma
&&
801 virt_to_phys(SCpnt
->request
.bh
->b_data
- 1) == ISA_DMA_THRESHOLD
)
804 SCpnt
->use_sg
= count
; /* Number of chains */
805 /* scsi_malloc can only allocate in chunks of 512 bytes */
806 count
= (SCpnt
->use_sg
* sizeof(struct scatterlist
) + 511) & ~511;
808 SCpnt
->sglist_len
= count
;
809 max_sg
= count
/ sizeof(struct scatterlist
);
810 if (SCpnt
->host
->sg_tablesize
< max_sg
)
811 max_sg
= SCpnt
->host
->sg_tablesize
;
812 sgpnt
= (struct scatterlist
*) scsi_malloc(count
);
814 printk("Warning - running *really* short on DMA buffers\n");
815 SCpnt
->use_sg
= 0; /* No memory left - bail out */
816 this_count
= SCpnt
->request
.current_nr_sectors
;
817 buff
= SCpnt
->request
.buffer
;
819 memset(sgpnt
, 0, count
); /* Zero so it is easy to fill, but only
820 * if memory is available
822 buff
= (char *) sgpnt
;
824 for (count
= 0, bh
= SCpnt
->request
.bh
, bhp
= bh
->b_reqnext
;
825 count
< SCpnt
->use_sg
&& bh
;
830 if (!sgpnt
[count
].address
)
831 sgpnt
[count
].address
= bh
->b_data
;
832 sgpnt
[count
].length
+= bh
->b_size
;
833 counted
+= bh
->b_size
>> 9;
835 if (virt_to_phys(sgpnt
[count
].address
) + sgpnt
[count
].length
- 1 >
836 ISA_DMA_THRESHOLD
&& (SCpnt
->host
->unchecked_isa_dma
) &&
837 !sgpnt
[count
].alt_address
) {
838 sgpnt
[count
].alt_address
= sgpnt
[count
].address
;
839 /* We try to avoid exhausting the DMA pool, since it is
840 * easier to control usage here. In other places we might
841 * have a more pressing need, and we would be screwed if
843 if (scsi_dma_free_sectors
< (sgpnt
[count
].length
>> 9) + 10) {
844 sgpnt
[count
].address
= NULL
;
846 sgpnt
[count
].address
=
847 (char *) scsi_malloc(sgpnt
[count
].length
);
849 /* If we start running low on DMA buffers, we abort the
850 * scatter-gather operation, and free all of the memory
851 * we have allocated. We want to ensure that all scsi
852 * operations are able to do at least a non-scatter/gather
854 if (sgpnt
[count
].address
== NULL
) { /* Out of dma memory */
856 printk("Warning: Running low on SCSI DMA buffers");
857 /* Try switching back to a non s-g operation. */
858 while (--count
>= 0) {
859 if (sgpnt
[count
].alt_address
)
860 scsi_free(sgpnt
[count
].address
,
861 sgpnt
[count
].length
);
863 this_count
= SCpnt
->request
.current_nr_sectors
;
864 buff
= SCpnt
->request
.buffer
;
866 scsi_free(sgpnt
, SCpnt
->sglist_len
);
868 SCpnt
->use_sg
= count
;
869 this_count
= counted
-= bh
->b_size
>> 9;
873 /* Only cluster buffers if we know that we can supply DMA
874 * buffers large enough to satisfy the request. Do not cluster
875 * a new request if this would mean that we suddenly need to
876 * start using DMA bounce buffers */
877 if (bhp
&& CONTIGUOUS_BUFFERS(bh
, bhp
)
878 && CLUSTERABLE_DEVICE(SCpnt
)) {
881 if (virt_to_phys(sgpnt
[count
].address
) + sgpnt
[count
].length
+
882 bhp
->b_size
- 1 > ISA_DMA_THRESHOLD
&&
883 (SCpnt
->host
->unchecked_isa_dma
) &&
884 !sgpnt
[count
].alt_address
)
887 if (!sgpnt
[count
].alt_address
) {
891 if (scsi_dma_free_sectors
> 10)
892 tmp
= (char *) scsi_malloc(sgpnt
[count
].length
896 max_sg
= SCpnt
->use_sg
;
899 scsi_free(sgpnt
[count
].address
, sgpnt
[count
].length
);
900 sgpnt
[count
].address
= tmp
;
904 /* If we are allowed another sg chain, then increment
905 * counter so we can insert it. Otherwise we will end
908 if (SCpnt
->use_sg
< max_sg
)
910 } /* contiguous buffers */
913 /* This is actually how many we are going to transfer */
914 this_count
= counted
;
916 if (count
< SCpnt
->use_sg
|| SCpnt
->use_sg
917 > SCpnt
->host
->sg_tablesize
) {
918 bh
= SCpnt
->request
.bh
;
919 printk("Use sg, count %d %x %d\n",
920 SCpnt
->use_sg
, count
, scsi_dma_free_sectors
);
921 printk("maxsg = %x, counted = %d this_count = %d\n",
922 max_sg
, counted
, this_count
);
924 printk("[%p %x] ", bh
->b_data
, bh
->b_size
);
927 if (SCpnt
->use_sg
< 16)
928 for (count
= 0; count
< SCpnt
->use_sg
; count
++)
929 printk("{%d:%p %p %d} ", count
,
930 sgpnt
[count
].address
,
931 sgpnt
[count
].alt_address
,
932 sgpnt
[count
].length
);
935 if (SCpnt
->request
.cmd
== WRITE
)
936 for (count
= 0; count
< SCpnt
->use_sg
; count
++)
937 if (sgpnt
[count
].alt_address
)
938 memcpy(sgpnt
[count
].address
, sgpnt
[count
].alt_address
,
939 sgpnt
[count
].length
);
940 } /* Able to malloc sgpnt */
941 } /* Host adapter capable of scatter-gather */
943 /* Now handle the possibility of DMA to addresses > 16Mb */
945 if (SCpnt
->use_sg
== 0) {
946 if (virt_to_phys(buff
) + (this_count
<< 9) - 1 > ISA_DMA_THRESHOLD
&&
947 (SCpnt
->host
->unchecked_isa_dma
)) {
949 buff
= bounce_buffer
;
951 buff
= (char *) scsi_malloc(this_count
<< 9);
952 if (buff
== NULL
) { /* Try backing off a bit if we are low on mem */
953 this_count
= SCpnt
->request
.current_nr_sectors
;
954 buff
= (char *) scsi_malloc(this_count
<< 9);
956 panic("Ran out of DMA buffers.");
958 if (SCpnt
->request
.cmd
== WRITE
)
959 memcpy(buff
, (char *) SCpnt
->request
.buffer
, this_count
<< 9);
962 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
964 (SCpnt
->request
.cmd
== WRITE
) ? "writing" : "reading",
965 this_count
, SCpnt
->request
.nr_sectors
));
967 cmd
[1] = (SCpnt
->lun
<< 5) & 0xe0;
969 if (rscsi_disks
[dev
].sector_size
== 4096) {
971 panic("sd.c:Bad block number requested");
973 panic("sd.c:Bad block number requested");
975 this_count
= block
>> 3;
977 if (rscsi_disks
[dev
].sector_size
== 2048) {
979 panic("sd.c:Bad block number requested");
981 panic("sd.c:Bad block number requested");
983 this_count
= this_count
>> 2;
985 if (rscsi_disks
[dev
].sector_size
== 1024) {
987 panic("sd.c:Bad block number requested");
989 panic("sd.c:Bad block number requested");
991 this_count
= this_count
>> 1;
993 if (rscsi_disks
[dev
].sector_size
== 256) {
995 this_count
= this_count
<< 1;
997 if (((this_count
> 0xff) || (block
> 0x1fffff)) && rscsi_disks
[dev
].ten
) {
998 if (this_count
> 0xffff)
1001 cmd
[0] += READ_10
- READ_6
;
1002 cmd
[2] = (unsigned char) (block
>> 24) & 0xff;
1003 cmd
[3] = (unsigned char) (block
>> 16) & 0xff;
1004 cmd
[4] = (unsigned char) (block
>> 8) & 0xff;
1005 cmd
[5] = (unsigned char) block
& 0xff;
1006 cmd
[6] = cmd
[9] = 0;
1007 cmd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
1008 cmd
[8] = (unsigned char) this_count
& 0xff;
1010 if (this_count
> 0xff)
1013 cmd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
1014 cmd
[2] = (unsigned char) ((block
>> 8) & 0xff);
1015 cmd
[3] = (unsigned char) block
& 0xff;
1016 cmd
[4] = (unsigned char) this_count
;
1021 * We shouldn't disconnect in the middle of a sector, so with a dumb
1022 * host adapter, it's safe to assume that we can at least transfer
1023 * this many bytes between each connect / disconnect.
1026 SCpnt
->transfersize
= rscsi_disks
[dev
].sector_size
;
1027 SCpnt
->underflow
= this_count
<< 9;
1029 scsi_do_cmd(SCpnt
, (void *) cmd
, buff
,
1030 this_count
* rscsi_disks
[dev
].sector_size
,
1032 (SCpnt
->device
->type
== TYPE_DISK
?
1033 SD_TIMEOUT
: SD_MOD_TIMEOUT
),
1037 static int check_scsidisk_media_change(kdev_t full_dev
)
1044 target
= DEVICE_NR(full_dev
);
1046 if (target
>= sd_template
.dev_max
||
1047 !rscsi_disks
[target
].device
) {
1048 printk("SCSI disk request error: invalid device.\n");
1051 if (!rscsi_disks
[target
].device
->removable
)
1055 * If the device is offline, don't send any commands - just pretend as if
1056 * the command failed. If the device ever comes back online, we can deal with
1057 * it then. It is only because of unrecoverable errors that we would ever
1058 * take a device offline in the first place.
1060 if (rscsi_disks
[target
].device
->online
== FALSE
) {
1061 rscsi_disks
[target
].ready
= 0;
1062 rscsi_disks
[target
].device
->changed
= 1;
1063 return 1; /* This will force a flush, if called from
1064 * check_disk_change */
1066 inode
.i_rdev
= full_dev
; /* This is all we really need here */
1068 /* Using Start/Stop enables differentiation between drive with
1069 * no cartridge loaded - NOT READY, drive with changed cartridge -
1070 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1071 * This also handles drives that auto spin down. eg iomega jaz 1GB
1072 * as this will spin up the drive.
1074 retval
= sd_ioctl(&inode
, NULL
, SCSI_IOCTL_START_UNIT
, 0);
1076 if (retval
) { /* Unable to test, unit probably not ready. This usually
1077 * means there is no disc in the drive. Mark as changed,
1078 * and we will figure it out later once the drive is
1079 * available again. */
1081 rscsi_disks
[target
].ready
= 0;
1082 rscsi_disks
[target
].device
->changed
= 1;
1083 return 1; /* This will force a flush, if called from
1084 * check_disk_change */
1087 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
1088 * presence of disk in the drive. This is kept in the Scsi_Disk
1089 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
1092 rscsi_disks
[target
].ready
= 1; /* FLOPTICAL */
1094 retval
= rscsi_disks
[target
].device
->changed
;
1096 rscsi_disks
[target
].device
->changed
= 0;
1100 static void sd_wait_cmd (Scsi_Cmnd
* SCpnt
, const void *cmnd
,
1101 void *buffer
, unsigned bufflen
, void (*done
)(Scsi_Cmnd
*),
1102 int timeout
, int retries
)
1104 DECLARE_MUTEX_LOCKED(sem
);
1106 SCpnt
->request
.sem
= &sem
;
1107 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
;
1108 scsi_do_cmd (SCpnt
, (void *) cmnd
,
1109 buffer
, bufflen
, done
, timeout
, retries
);
1110 spin_unlock_irq(&io_request_lock
);
1112 spin_lock_irq(&io_request_lock
);
1113 SCpnt
->request
.sem
= NULL
;
1116 static void sd_init_done(Scsi_Cmnd
* SCpnt
)
1118 struct request
*req
;
1120 req
= &SCpnt
->request
;
1121 req
->rq_status
= RQ_SCSI_DONE
; /* Busy, but indicate request done */
1123 if (req
->sem
!= NULL
) {
1127 static int sd_init_onedisk(int i
)
1129 unsigned char cmd
[10];
1131 unsigned char *buffer
;
1132 unsigned long spintime_value
= 0;
1133 int the_result
, retries
, spintime
;
1137 * Get the name of the disk, in case we need to log it somewhere.
1139 sd_devname(i
, nbuff
);
1142 * If the device is offline, don't try and read capacity or any of the other
1145 if (rscsi_disks
[i
].device
->online
== FALSE
) {
1148 spin_lock_irq(&io_request_lock
);
1150 /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
1151 * considered a fatal error, and many devices report such an error
1152 * just after a scsi bus reset.
1155 SCpnt
= scsi_allocate_device(NULL
, rscsi_disks
[i
].device
, 1);
1156 buffer
= (unsigned char *) scsi_malloc(512);
1160 /* Spin up drives, as required. Only do this at boot time */
1161 /* Spinup needs to be done for module loads too. */
1165 while (retries
< 3) {
1166 cmd
[0] = TEST_UNIT_READY
;
1167 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1168 memset((void *) &cmd
[2], 0, 8);
1170 SCpnt
->sense_buffer
[0] = 0;
1171 SCpnt
->sense_buffer
[2] = 0;
1173 sd_wait_cmd (SCpnt
, (void *) cmd
, (void *) buffer
,
1174 512, sd_init_done
, SD_TIMEOUT
, MAX_RETRIES
);
1176 the_result
= SCpnt
->result
;
1179 || SCpnt
->sense_buffer
[2] != UNIT_ATTENTION
)
1183 /* Look for non-removable devices that return NOT_READY.
1184 * Issue command to spin up drive for these cases. */
1185 if (the_result
&& !rscsi_disks
[i
].device
->removable
&&
1186 SCpnt
->sense_buffer
[2] == NOT_READY
)
1188 unsigned long time1
;
1191 printk("%s: Spinning up disk...", nbuff
);
1192 cmd
[0] = START_STOP
;
1193 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1194 cmd
[1] |= 1; /* Return immediately */
1195 memset((void *) &cmd
[2], 0, 8);
1196 cmd
[4] = 1; /* Start spin cycle */
1198 SCpnt
->sense_buffer
[0] = 0;
1199 SCpnt
->sense_buffer
[2] = 0;
1201 sd_wait_cmd(SCpnt
, (void *) cmd
, (void *) buffer
,
1202 512, sd_init_done
, SD_TIMEOUT
, MAX_RETRIES
);
1206 spintime_value
= jiffies
;
1207 time1
= jiffies
+ HZ
;
1208 spin_unlock_irq(&io_request_lock
);
1209 while(time_before(jiffies
, time1
)); /* Wait 1 second for next try */
1211 spin_lock_irq(&io_request_lock
);
1213 } while(the_result
&& spintime
&& time_after(spintime_value
+100*HZ
, jiffies
));
1217 printk("not responding...\n");
1223 cmd
[0] = READ_CAPACITY
;
1224 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1225 memset((void *) &cmd
[2], 0, 8);
1226 memset((void *) buffer
, 0, 8);
1228 SCpnt
->sense_buffer
[0] = 0;
1229 SCpnt
->sense_buffer
[2] = 0;
1231 sd_wait_cmd(SCpnt
, (void *) cmd
, (void *) buffer
,
1232 8, sd_init_done
, SD_TIMEOUT
, MAX_RETRIES
);
1234 the_result
= SCpnt
->result
;
1237 } while (the_result
&& retries
);
1240 * The SCSI standard says:
1241 * "READ CAPACITY is necessary for self configuring software"
1242 * While not mandatory, support of READ CAPACITY is strongly encouraged.
1243 * We used to die if we couldn't successfully do a READ CAPACITY.
1244 * But, now we go on about our way. The side effects of this are
1246 * 1. We can't know block size with certainty. I have said "512 bytes
1247 * is it" as this is most common.
1249 * 2. Recovery from when some one attempts to read past the end of the
1250 * raw device will be slower.
1254 printk("%s : READ CAPACITY failed.\n"
1255 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
1257 status_byte(the_result
),
1258 msg_byte(the_result
),
1259 host_byte(the_result
),
1260 driver_byte(the_result
)
1262 if (driver_byte(the_result
) & DRIVER_SENSE
)
1263 printk("%s : extended sense code = %1x \n",
1264 nbuff
, SCpnt
->sense_buffer
[2] & 0xf);
1266 printk("%s : sense not available. \n", nbuff
);
1268 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
1270 rscsi_disks
[i
].capacity
= 0x1fffff;
1271 rscsi_disks
[i
].sector_size
= 512;
1273 /* Set dirty bit for removable devices if not ready - sometimes drives
1274 * will not report this properly. */
1275 if (rscsi_disks
[i
].device
->removable
&&
1276 SCpnt
->sense_buffer
[2] == NOT_READY
)
1277 rscsi_disks
[i
].device
->changed
= 1;
1281 * FLOPTICAL , if read_capa is ok , drive is assumed to be ready
1283 rscsi_disks
[i
].ready
= 1;
1285 rscsi_disks
[i
].capacity
= 1 + ((buffer
[0] << 24) |
1290 rscsi_disks
[i
].sector_size
= (buffer
[4] << 24) |
1291 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
1293 if (rscsi_disks
[i
].sector_size
== 0) {
1294 rscsi_disks
[i
].sector_size
= 512;
1295 printk("%s : sector size 0 reported, assuming 512.\n", nbuff
);
1297 if (rscsi_disks
[i
].sector_size
!= 512 &&
1298 rscsi_disks
[i
].sector_size
!= 1024 &&
1299 rscsi_disks
[i
].sector_size
!= 2048 &&
1300 rscsi_disks
[i
].sector_size
!= 4096 &&
1301 rscsi_disks
[i
].sector_size
!= 256) {
1302 printk("%s : unsupported sector size %d.\n",
1303 nbuff
, rscsi_disks
[i
].sector_size
);
1304 if (rscsi_disks
[i
].device
->removable
) {
1305 rscsi_disks
[i
].capacity
= 0;
1307 printk("scsi : deleting disk entry.\n");
1308 sd_detach(rscsi_disks
[i
].device
);
1309 rscsi_disks
[i
].device
= NULL
;
1311 /* Wake up a process waiting for device */
1312 wake_up(&SCpnt
->device
->device_wait
);
1313 scsi_release_command(SCpnt
);
1315 scsi_free(buffer
, 512);
1316 spin_unlock_irq(&io_request_lock
);
1321 if (rscsi_disks
[i
].sector_size
== 2048) {
1325 * We must fix the sd_blocksizes and sd_hardsizes
1326 * to allow us to read the partition tables.
1327 * The disk reading code does not allow for reading
1328 * of partial sectors.
1330 for (m
= i
<< 4; m
< ((i
+ 1) << 4); m
++) {
1331 sd_blocksizes
[m
] = 2048;
1335 * The msdos fs needs to know the hardware sector size
1336 * So I have created this table. See ll_rw_blk.c
1337 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1340 int sz_quot
, sz_rem
;
1341 int hard_sector
= rscsi_disks
[i
].sector_size
;
1342 /* There are 16 minors allocated for each major device */
1343 for (m
= i
<< 4; m
< ((i
+ 1) << 4); m
++) {
1344 sd_hardsizes
[m
] = hard_sector
;
1346 mb
= rscsi_disks
[i
].capacity
/ 1024 * hard_sector
/ 1024;
1347 /* sz = div(m/100, 10); this seems to not be in the libr */
1348 m
= (mb
+ 50) / 100;
1350 sz_rem
= m
- (10 * sz_quot
);
1351 printk("SCSI device %s: hdwr sector= %d bytes."
1352 " Sectors= %d [%d MB] [%d.%1d GB]\n",
1353 nbuff
, hard_sector
, rscsi_disks
[i
].capacity
,
1354 mb
, sz_quot
, sz_rem
);
1356 if (rscsi_disks
[i
].sector_size
== 4096)
1357 rscsi_disks
[i
].capacity
<<= 3;
1358 if (rscsi_disks
[i
].sector_size
== 2048)
1359 rscsi_disks
[i
].capacity
<<= 2; /* Change into 512 byte sectors */
1360 if (rscsi_disks
[i
].sector_size
== 1024)
1361 rscsi_disks
[i
].capacity
<<= 1; /* Change into 512 byte sectors */
1362 if (rscsi_disks
[i
].sector_size
== 256)
1363 rscsi_disks
[i
].capacity
>>= 1; /* Change into 512 byte sectors */
1368 * Unless otherwise specified, this is not write protected.
1370 rscsi_disks
[i
].write_prot
= 0;
1371 if (rscsi_disks
[i
].device
->removable
&& rscsi_disks
[i
].ready
) {
1375 * for removable scsi disk ( FLOPTICAL ) we have to recognise
1376 * the Write Protect Flag. This flag is kept in the Scsi_Disk struct
1377 * and tested at open !
1378 * Daniel Roche ( dan@lectra.fr )
1381 memset((void *) &cmd
[0], 0, 8);
1382 cmd
[0] = MODE_SENSE
;
1383 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1384 cmd
[2] = 1; /* page code 1 ?? */
1387 SCpnt
->sense_buffer
[0] = 0;
1388 SCpnt
->sense_buffer
[2] = 0;
1390 /* same code as READCAPA !! */
1391 sd_wait_cmd(SCpnt
, (void *) cmd
, (void *) buffer
,
1392 512, sd_init_done
, SD_TIMEOUT
, MAX_RETRIES
);
1394 the_result
= SCpnt
->result
;
1397 printk("%s: test WP failed, assume Write Protected\n", nbuff
);
1398 rscsi_disks
[i
].write_prot
= 1;
1400 rscsi_disks
[i
].write_prot
= ((buffer
[2] & 0x80) != 0);
1401 printk("%s: Write Protect is %s\n", nbuff
,
1402 rscsi_disks
[i
].write_prot
? "on" : "off");
1405 } /* check for write protect */
1406 /* Wake up a process waiting for device */
1407 wake_up(&SCpnt
->device
->device_wait
);
1408 scsi_release_command(SCpnt
);
1411 rscsi_disks
[i
].ten
= 1;
1412 rscsi_disks
[i
].remap
= 1;
1413 scsi_free(buffer
, 512);
1414 spin_unlock_irq(&io_request_lock
);
1419 * The sd_init() function looks at all SCSI drives present, determines
1420 * their size, and reads partition table entries for them.
1423 static int sd_registered
= 0;
1425 static int sd_init()
1429 if (sd_template
.dev_noticed
== 0)
1433 sd_template
.dev_max
= sd_template
.dev_noticed
+ SD_EXTRA_DEVS
;
1435 if (sd_template
.dev_max
> N_SD_MAJORS
* SCSI_DISKS_PER_MAJOR
)
1436 sd_template
.dev_max
= N_SD_MAJORS
* SCSI_DISKS_PER_MAJOR
;
1438 if (!sd_registered
) {
1439 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1440 if (register_blkdev(SD_MAJOR(i
), "sd", &sd_fops
)) {
1441 printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i
));
1447 /* We do not support attaching loadable devices yet. */
1451 rscsi_disks
= (Scsi_Disk
*)
1452 scsi_init_malloc(sd_template
.dev_max
* sizeof(Scsi_Disk
), GFP_ATOMIC
);
1453 memset(rscsi_disks
, 0, sd_template
.dev_max
* sizeof(Scsi_Disk
));
1455 /* for every (necessary) major: */
1456 sd_sizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1457 memset(sd_sizes
, 0, (sd_template
.dev_max
<< 4) * sizeof(int));
1459 sd_blocksizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1460 sd_hardsizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) * sizeof(int), GFP_ATOMIC
);
1462 for (i
= 0; i
< sd_template
.dev_max
<< 4; i
++) {
1463 sd_blocksizes
[i
] = 1024;
1464 sd_hardsizes
[i
] = 512;
1467 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1468 blksize_size
[SD_MAJOR(i
)] = sd_blocksizes
+ i
* (SCSI_DISKS_PER_MAJOR
<< 4);
1469 hardsect_size
[SD_MAJOR(i
)] = sd_hardsizes
+ i
* (SCSI_DISKS_PER_MAJOR
<< 4);
1471 sd
= (struct hd_struct
*) scsi_init_malloc((sd_template
.dev_max
<< 4) *
1472 sizeof(struct hd_struct
),
1475 if (N_USED_SD_MAJORS
> 1)
1476 sd_gendisks
= (struct gendisk
*)
1477 kmalloc(N_USED_SD_MAJORS
* sizeof(struct gendisk
), GFP_ATOMIC
);
1478 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1479 sd_gendisks
[i
].major
= SD_MAJOR(i
);
1480 sd_gendisks
[i
].major_name
= "sd";
1481 sd_gendisks
[i
].minor_shift
= 4;
1482 sd_gendisks
[i
].max_p
= 1 << 4;
1483 sd_gendisks
[i
].max_nr
= SCSI_DISKS_PER_MAJOR
;
1484 sd_gendisks
[i
].init
= sd_geninit
;
1485 sd_gendisks
[i
].part
= sd
+ (i
* SCSI_DISKS_PER_MAJOR
<< 4);
1486 sd_gendisks
[i
].sizes
= sd_sizes
+ (i
* SCSI_DISKS_PER_MAJOR
<< 4);
1487 sd_gendisks
[i
].nr_real
= 0;
1488 sd_gendisks
[i
].next
= sd_gendisks
+ i
+ 1;
1489 sd_gendisks
[i
].real_devices
=
1490 (void *) (rscsi_disks
+ i
* SCSI_DISKS_PER_MAJOR
);
1493 LAST_SD_GENDISK
.max_nr
=
1494 (sd_template
.dev_max
- 1) % SCSI_DISKS_PER_MAJOR
+ 1;
1495 LAST_SD_GENDISK
.next
= NULL
;
1500 * sd_get_queue() returns the queue which corresponds to a given device.
1502 static struct request
**sd_get_queue(kdev_t dev
)
1504 return &blk_dev
[MAJOR_NR
].current_request
;
1506 static void sd_finish()
1508 struct gendisk
*gendisk
;
1511 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1512 /* FIXME: After 2.2 we should implement multiple sd queues */
1513 blk_dev
[SD_MAJOR(i
)].request_fn
= DEVICE_REQUEST
;
1515 blk_dev
[SD_MAJOR(i
)].queue
= sd_get_queue
;
1517 for (gendisk
= gendisk_head
; gendisk
!= NULL
; gendisk
= gendisk
->next
)
1518 if (gendisk
== sd_gendisks
)
1520 if (gendisk
== NULL
) {
1521 LAST_SD_GENDISK
.next
= gendisk_head
;
1522 gendisk_head
= sd_gendisks
;
1524 for (i
= 0; i
< sd_template
.dev_max
; ++i
)
1525 if (!rscsi_disks
[i
].capacity
&&
1526 rscsi_disks
[i
].device
) {
1528 && !rscsi_disks
[i
].has_part_table
) {
1529 sd_sizes
[i
<< 4] = rscsi_disks
[i
].capacity
;
1530 /* revalidate does sd_init_onedisk via MAYBE_REINIT */
1531 revalidate_scsidisk(MKDEV_SD(i
), 0);
1533 i
= sd_init_onedisk(i
);
1534 rscsi_disks
[i
].has_part_table
= 1;
1536 /* If our host adapter is capable of scatter-gather, then we increase
1537 * the read-ahead to 60 blocks (120 sectors). If not, we use
1538 * a two block (4 sector) read ahead. We can only respect this with the
1539 * granularity of every 16 disks (one device major).
1541 for (i
= 0; i
< N_USED_SD_MAJORS
; i
++) {
1542 read_ahead
[SD_MAJOR(i
)] =
1543 (rscsi_disks
[i
* SCSI_DISKS_PER_MAJOR
].device
1544 && rscsi_disks
[i
* SCSI_DISKS_PER_MAJOR
].device
->host
->sg_tablesize
)
1545 ? 120 /* 120 sector read-ahead */
1546 : 4; /* 4 sector read-ahead */
1552 static int sd_detect(Scsi_Device
* SDp
)
1555 if (SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
)
1558 sd_devname(sd_template
.dev_noticed
++, nbuff
);
1559 printk("Detected scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1560 SDp
->removable
? "removable " : "",
1562 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
1566 static int sd_attach(Scsi_Device
* SDp
)
1571 if (SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
)
1574 if (sd_template
.nr_dev
>= sd_template
.dev_max
) {
1578 for (dpnt
= rscsi_disks
, i
= 0; i
< sd_template
.dev_max
; i
++, dpnt
++)
1582 if (i
>= sd_template
.dev_max
)
1583 panic("scsi_devices corrupt (sd)");
1585 SDp
->scsi_request_fn
= do_sd_request
;
1586 rscsi_disks
[i
].device
= SDp
;
1587 rscsi_disks
[i
].has_part_table
= 0;
1588 sd_template
.nr_dev
++;
1589 SD_GENDISK(i
).nr_real
++;
1593 #define DEVICE_BUSY rscsi_disks[target].device->busy
1594 #define USAGE rscsi_disks[target].device->access_count
1595 #define CAPACITY rscsi_disks[target].capacity
1596 #define MAYBE_REINIT sd_init_onedisk(target)
1598 /* This routine is called to flush all partitions and partition tables
1599 * for a changed scsi disk, and then re-read the new partition table.
1600 * If we are revalidating a disk because of a media change, then we
1601 * enter with usage == 0. If we are using an ioctl, we automatically have
1602 * usage == 1 (we need an open channel to use an ioctl :-), so this
1605 int revalidate_scsidisk(kdev_t dev
, int maxusage
)
1612 target
= DEVICE_NR(dev
);
1614 if (DEVICE_BUSY
|| USAGE
> maxusage
) {
1615 printk("Device busy for revalidation (usage=%d)\n", USAGE
);
1620 max_p
= sd_gendisks
->max_p
;
1621 start
= target
<< sd_gendisks
->minor_shift
;
1623 for (i
= max_p
- 1; i
>= 0; i
--) {
1624 int index
= start
+ i
;
1625 kdev_t devi
= MKDEV_SD_PARTITION(index
);
1626 struct super_block
*sb
= get_super(devi
);
1629 invalidate_inodes(sb
);
1630 invalidate_buffers(devi
);
1631 sd_gendisks
->part
[index
].start_sect
= 0;
1632 sd_gendisks
->part
[index
].nr_sects
= 0;
1634 * Reset the blocksize for everything so that we can read
1635 * the partition table. Technically we will determine the
1636 * correct block size when we revalidate, but we do this just
1637 * to make sure that everything remains consistent.
1639 sd_blocksizes
[index
] = 1024;
1640 if (rscsi_disks
[target
].sector_size
== 2048)
1641 sd_blocksizes
[index
] = 2048;
1643 sd_blocksizes
[index
] = 1024;
1650 sd_gendisks
->part
[start
].nr_sects
= CAPACITY
;
1651 if (!rscsi_disks
[target
].device
)
1653 resetup_one_dev(&SD_GENDISK(target
),
1654 target
% SCSI_DISKS_PER_MAJOR
);
1660 static int fop_revalidate_scsidisk(kdev_t dev
)
1662 return revalidate_scsidisk(dev
, 0);
1664 static void sd_detach(Scsi_Device
* SDp
)
1671 for (dpnt
= rscsi_disks
, i
= 0; i
< sd_template
.dev_max
; i
++, dpnt
++)
1672 if (dpnt
->device
== SDp
) {
1674 /* If we are disconnecting a disk driver, sync and invalidate
1676 max_p
= sd_gendisk
.max_p
;
1677 start
= i
<< sd_gendisk
.minor_shift
;
1679 for (j
= max_p
- 1; j
>= 0; j
--) {
1680 int index
= start
+ j
;
1681 kdev_t devi
= MKDEV_SD_PARTITION(index
);
1682 struct super_block
*sb
= get_super(devi
);
1685 invalidate_inodes(sb
);
1686 invalidate_buffers(devi
);
1687 sd_gendisks
->part
[index
].start_sect
= 0;
1688 sd_gendisks
->part
[index
].nr_sects
= 0;
1689 sd_sizes
[index
] = 0;
1691 dpnt
->has_part_table
= 0;
1692 dpnt
->device
= NULL
;
1695 sd_template
.dev_noticed
--;
1696 sd_template
.nr_dev
--;
1697 SD_GENDISK(i
).nr_real
--;
1705 int init_module(void)
1707 sd_template
.module
= &__this_module
;
1708 return scsi_register_module(MODULE_SCSI_DEV
, &sd_template
);
1710 void cleanup_module(void)
1712 struct gendisk
**prev_sdgd_link
;
1713 struct gendisk
*sdgd
;
1717 scsi_unregister_module(MODULE_SCSI_DEV
, &sd_template
);
1719 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++)
1720 unregister_blkdev(SD_MAJOR(i
), "sd");
1723 if (rscsi_disks
!= NULL
) {
1724 scsi_init_free((char *) rscsi_disks
,
1725 sd_template
.dev_max
* sizeof(Scsi_Disk
));
1726 scsi_init_free((char *) sd_sizes
, sd_template
.dev_max
* sizeof(int));
1727 scsi_init_free((char *) sd_blocksizes
, sd_template
.dev_max
* sizeof(int));
1728 scsi_init_free((char *) sd_hardsizes
, sd_template
.dev_max
* sizeof(int));
1729 scsi_init_free((char *) sd
,
1730 (sd_template
.dev_max
<< 4) * sizeof(struct hd_struct
));
1733 * Now remove sd_gendisks from the linked list
1735 prev_sdgd_link
= &gendisk_head
;
1736 while ((sdgd
= *prev_sdgd_link
) != NULL
) {
1737 if (sdgd
>= sd_gendisks
&& sdgd
<= &LAST_SD_GENDISK
) {
1739 *prev_sdgd_link
= sdgd
->next
;
1742 prev_sdgd_link
= &sdgd
->next
;
1745 if (removed
!= N_USED_SD_MAJORS
)
1746 printk("%s %d sd_gendisks in disk chain",
1747 removed
> N_USED_SD_MAJORS
? "total" : "just", removed
);
1750 for (i
= 0; i
<= (sd_template
.dev_max
- 1) / SCSI_DISKS_PER_MAJOR
; i
++) {
1751 blk_dev
[SD_MAJOR(i
)].request_fn
= NULL
;
1752 blk_size
[SD_MAJOR(i
)] = NULL
;
1753 hardsect_size
[SD_MAJOR(i
)] = NULL
;
1754 read_ahead
[SD_MAJOR(i
)] = 0;
1756 sd_template
.dev_max
= 0;
1757 if (sd_gendisks
!= &sd_gendisk
)
1763 * Overrides for Emacs so that we almost follow Linus's tabbing style.
1764 * Emacs will notice this stuff at the end of the file and automatically
1765 * adjust the settings for this buffer only. This must remain at the end
1767 * ---------------------------------------------------------------------------
1770 * c-brace-imaginary-offset: 0
1771 * c-brace-offset: -4
1772 * c-argdecl-indent: 4
1773 * c-label-offset: -4
1774 * c-continued-statement-offset: 4
1775 * c-continued-brace-offset: 0
1776 * indent-tabs-mode: nil