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.
19 #include <linux/module.h>
22 * This is a variable in scsi.c that is set when we are processing something
23 * after boot time. By definition, this is true when we are a loadable module
28 #define MODULE_FLAG scsi_loadable_module_flag
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
35 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
39 #include <linux/smp.h>
41 #include <asm/system.h>
44 #define MAJOR_NR SCSI_DISK_MAJOR
45 #include <linux/blk.h>
49 #include <scsi/scsi_ioctl.h>
50 #include "constants.h"
52 #include <linux/genhd.h>
55 * static const char RCSid[] = "$Header:";
61 * Time out in seconds for disks and Magneto-opticals (which are slower).
64 #define SD_TIMEOUT (15 * HZ)
65 #define SD_MOD_TIMEOUT (75 * HZ)
67 #define CLUSTERABLE_DEVICE(SC) (SC->host->use_clustering && \
68 SC->device->type != TYPE_MOD)
70 struct hd_struct
* sd
;
72 Scsi_Disk
* rscsi_disks
= NULL
;
73 static int * sd_sizes
;
74 static int * sd_blocksizes
;
75 static int * sd_hardsizes
; /* Hardware sector size */
77 extern int sd_ioctl(struct inode
*, struct file
*, unsigned int, unsigned long);
79 static int check_scsidisk_media_change(kdev_t
);
80 static int fop_revalidate_scsidisk(kdev_t
);
82 static int sd_init_onedisk(int);
84 static void requeue_sd_request (Scsi_Cmnd
* SCpnt
);
86 static int sd_init(void);
87 static void sd_finish(void);
88 static int sd_attach(Scsi_Device
*);
89 static int sd_detect(Scsi_Device
*);
90 static void sd_detach(Scsi_Device
*);
92 static void sd_devname(unsigned int disknum
, char * buffer
)
96 sprintf(buffer
, "sd%c", 'a' + disknum
);
103 * For larger numbers of disks, we need to go to a new
108 sprintf(buffer
, "sd%c%c", 'a' + min1
, 'a' + min2
);
112 struct Scsi_Device_Template sd_template
=
113 { NULL
, "disk", "sd", NULL
, TYPE_DISK
,
114 SCSI_DISK_MAJOR
, 0, 0, 0, 1,
116 sd_finish
, sd_attach
, sd_detach
119 static int sd_open(struct inode
* inode
, struct file
* filp
)
122 target
= DEVICE_NR(inode
->i_rdev
);
124 SCSI_LOG_HLQUEUE(1,printk("target=%d, max=%d\n", target
, sd_template
.dev_max
));
126 if(target
>= sd_template
.dev_max
|| !rscsi_disks
[target
].device
)
127 return -ENXIO
; /* No such device */
130 * If the device is in error recovery, wait until it is done.
131 * If the device is offline, then disallow any access to it.
133 if( !scsi_block_when_processing_errors(rscsi_disks
[target
].device
) )
139 * Make sure that only one process can do a check_change_disk at one time.
140 * This is also used to lock out further access when the partition table
144 while (rscsi_disks
[target
].device
->busy
)
146 if(rscsi_disks
[target
].device
->removable
) {
147 check_disk_change(inode
->i_rdev
);
150 * If the drive is empty, just let the open fail.
152 if ( !rscsi_disks
[target
].ready
)
156 * Similarly, if the device has the write protect tab set,
157 * have the open fail if the user expects to be able to write
160 if ( (rscsi_disks
[target
].write_prot
) && (filp
->f_mode
& 2) )
165 * It is possible that the disk changing stuff resulted in the device being taken
166 * offline. If this is the case, report this to the user, and don't pretend that
167 * the open actually succeeded.
169 if( !rscsi_disks
[target
].device
->online
)
175 * See if we are requesting a non-existent partition. Do this
176 * after checking for disk change.
178 if(sd_sizes
[MINOR(inode
->i_rdev
)] == 0)
181 if(rscsi_disks
[target
].device
->removable
)
182 if(!rscsi_disks
[target
].device
->access_count
)
183 sd_ioctl(inode
, NULL
, SCSI_IOCTL_DOORLOCK
, 0);
185 rscsi_disks
[target
].device
->access_count
++;
186 if (rscsi_disks
[target
].device
->host
->hostt
->module
)
187 __MOD_INC_USE_COUNT(rscsi_disks
[target
].device
->host
->hostt
->module
);
188 if(sd_template
.module
)
189 __MOD_INC_USE_COUNT(sd_template
.module
);
193 static int sd_release(struct inode
* inode
, struct file
* file
)
196 fsync_dev(inode
->i_rdev
);
198 target
= DEVICE_NR(inode
->i_rdev
);
200 rscsi_disks
[target
].device
->access_count
--;
202 if(rscsi_disks
[target
].device
->removable
) {
203 if(!rscsi_disks
[target
].device
->access_count
)
204 sd_ioctl(inode
, NULL
, SCSI_IOCTL_DOORUNLOCK
, 0);
207 if(rscsi_disks
[target
].device
->host
->hostt
->module
)
208 __MOD_DEC_USE_COUNT(rscsi_disks
[target
].device
->host
->hostt
->module
);
209 if(sd_template
.module
)
210 __MOD_DEC_USE_COUNT(sd_template
.module
);
214 static void sd_geninit(struct gendisk
*);
216 static struct file_operations sd_fops
= {
217 NULL
, /* lseek - default */
218 block_read
, /* read - general block-dev read */
219 block_write
, /* write - general block-dev write */
220 NULL
, /* readdir - bad */
222 sd_ioctl
, /* ioctl */
224 sd_open
, /* open code */
226 sd_release
, /* release */
227 block_fsync
, /* fsync */
229 check_scsidisk_media_change
, /* Disk change */
230 fop_revalidate_scsidisk
/* revalidate */
233 static struct gendisk sd_gendisk
= {
234 MAJOR_NR
, /* Major number */
235 "sd", /* Major name */
236 4, /* Bits to shift to get real from partition */
237 1 << 4, /* Number of partitions per real */
238 0, /* maximum number of real */
239 sd_geninit
, /* init function */
240 NULL
, /* hd struct */
241 NULL
, /* block sizes */
247 static void sd_geninit (struct gendisk
*ignored
)
251 for (i
= 0; i
< sd_template
.dev_max
; ++i
)
252 if(rscsi_disks
[i
].device
)
253 sd
[i
<< 4].nr_sects
= rscsi_disks
[i
].capacity
;
255 /* No longer needed - we keep track of this as we attach/detach */
256 sd_gendisk
.nr_real
= sd_template
.dev_max
;
261 * rw_intr is the interrupt routine for the device driver.
262 * It will be notified on the end of a SCSI read / write, and
263 * will take one of several actions based on success or failure.
266 static void rw_intr (Scsi_Cmnd
*SCpnt
)
268 int result
= SCpnt
->result
;
270 int this_count
= SCpnt
->bufflen
>> 9;
271 int good_sectors
= (result
== 0 ? this_count
: 0);
272 int block_sectors
= 1;
274 sd_devname(MINOR(SCpnt
->request
.rq_dev
) >> 4, nbuff
);
276 SCSI_LOG_HLCOMPLETE(1,printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff
,
277 SCpnt
->host
->host_no
,
279 SCpnt
->sense_buffer
[0],
280 SCpnt
->sense_buffer
[2]));
283 Handle MEDIUM ERRORs that indicate partial success. Since this is a
284 relatively rare error condition, no care is taken to avoid unnecessary
285 additional work such as memcpy's that could be avoided.
288 if (driver_byte(result
) != 0 && /* An error occurred */
289 SCpnt
->sense_buffer
[0] == 0xF0 && /* Sense data is valid */
290 SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
)
292 long error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
293 (SCpnt
->sense_buffer
[4] << 16) |
294 (SCpnt
->sense_buffer
[5] << 8) |
295 SCpnt
->sense_buffer
[6];
297 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].sector_size
;
298 if (SCpnt
->request
.bh
!= NULL
)
299 block_sectors
= SCpnt
->request
.bh
->b_size
>> 9;
300 if (sector_size
== 1024)
303 if (block_sectors
< 2) block_sectors
= 2;
305 else if (sector_size
== 2048)
308 if (block_sectors
< 4) block_sectors
= 4;
310 else if (sector_size
== 256)
312 error_sector
-= sd
[MINOR(SCpnt
->request
.rq_dev
)].start_sect
;
313 error_sector
&= ~ (block_sectors
- 1);
314 good_sectors
= error_sector
- SCpnt
->request
.sector
;
315 if (good_sectors
< 0 || good_sectors
>= this_count
)
320 * First case : we assume that the command succeeded. One of two things
321 * will happen here. Either we will be finished, or there will be more
322 * sectors that we were unable to read last time.
325 if (good_sectors
> 0) {
327 SCSI_LOG_HLCOMPLETE(1,printk("%s : %ld sectors remain.\n", nbuff
,
328 SCpnt
->request
.nr_sectors
));
329 SCSI_LOG_HLCOMPLETE(1,printk("use_sg is %d\n ",SCpnt
->use_sg
));
332 struct scatterlist
* sgpnt
;
334 sgpnt
= (struct scatterlist
*) SCpnt
->buffer
;
335 for(i
=0; i
<SCpnt
->use_sg
; i
++) {
338 SCSI_LOG_HLCOMPLETE(3,printk(":%p %p %d\n",sgpnt
[i
].alt_address
, sgpnt
[i
].address
,
342 if (sgpnt
[i
].alt_address
) {
343 if (SCpnt
->request
.cmd
== READ
)
344 memcpy(sgpnt
[i
].alt_address
, sgpnt
[i
].address
,
346 scsi_free(sgpnt
[i
].address
, sgpnt
[i
].length
);
350 /* Free list of scatter-gather pointers */
351 scsi_free(SCpnt
->buffer
, SCpnt
->sglist_len
);
353 if (SCpnt
->buffer
!= SCpnt
->request
.buffer
) {
354 SCSI_LOG_HLCOMPLETE(3,printk("nosg: %p %p %d\n",
355 SCpnt
->request
.buffer
, SCpnt
->buffer
,
358 if (SCpnt
->request
.cmd
== READ
)
359 memcpy(SCpnt
->request
.buffer
, SCpnt
->buffer
,
361 scsi_free(SCpnt
->buffer
, SCpnt
->bufflen
);
365 * If multiple sectors are requested in one buffer, then
366 * they will have been finished off by the first command.
367 * If not, then we have a multi-buffer command.
369 if (SCpnt
->request
.nr_sectors
> this_count
)
371 SCpnt
->request
.errors
= 0;
373 if (!SCpnt
->request
.bh
)
375 SCSI_LOG_HLCOMPLETE(2,printk("%s : handling page request, no buffer\n",
379 * The SCpnt->request.nr_sectors field is always done in
380 * 512 byte sectors, even if this really isn't the case.
382 panic("sd.c: linked page request (%lx %x)",
383 SCpnt
->request
.sector
, this_count
);
386 SCpnt
= end_scsi_request(SCpnt
, 1, good_sectors
);
389 requeue_sd_request(SCpnt
);
394 if (good_sectors
== 0) {
396 /* Free up any indirection buffers we allocated for DMA purposes. */
398 struct scatterlist
* sgpnt
;
400 sgpnt
= (struct scatterlist
*) SCpnt
->buffer
;
401 for(i
=0; i
<SCpnt
->use_sg
; i
++) {
402 SCSI_LOG_HLCOMPLETE(3,printk("err: %p %p %d\n",
403 SCpnt
->request
.buffer
, SCpnt
->buffer
,
405 if (sgpnt
[i
].alt_address
) {
406 scsi_free(sgpnt
[i
].address
, sgpnt
[i
].length
);
409 scsi_free(SCpnt
->buffer
, SCpnt
->sglist_len
); /* Free list of scatter-gather pointers */
411 SCSI_LOG_HLCOMPLETE(2,printk("nosgerr: %p %p %d\n",
412 SCpnt
->request
.buffer
, SCpnt
->buffer
,
414 if (SCpnt
->buffer
!= SCpnt
->request
.buffer
)
415 scsi_free(SCpnt
->buffer
, SCpnt
->bufflen
);
420 * Now, if we were good little boys and girls, Santa left us a request
421 * sense buffer. We can extract information from this, so we
422 * can choose a block to remap, etc.
425 if (driver_byte(result
) != 0) {
426 if (suggestion(result
) == SUGGEST_REMAP
) {
429 * Not yet implemented. A read will fail after being remapped,
430 * a write will call the strategy routine again.
432 if rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].remap
440 if ((SCpnt
->sense_buffer
[0] & 0x7f) == 0x70) {
441 if ((SCpnt
->sense_buffer
[2] & 0xf) == UNIT_ATTENTION
) {
442 if(rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->removable
) {
443 /* detected disc change. set a bit and quietly refuse
446 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->changed
= 1;
447 SCpnt
= end_scsi_request(SCpnt
, 0, this_count
);
448 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
);
465 /* If we had an ILLEGAL REQUEST returned, then we may have
466 * performed an unsupported command. The only thing this should be
467 * would be a ten byte read where only a six byte read was supported.
468 * Also, on a system where READ CAPACITY failed, we have have read
469 * past the end of the disk.
472 if (SCpnt
->sense_buffer
[2] == ILLEGAL_REQUEST
) {
473 if (rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].ten
) {
474 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].ten
= 0;
475 requeue_sd_request(SCpnt
);
482 if (SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
) {
483 printk("scsi%d: MEDIUM ERROR on channel %d, id %d, lun %d, CDB: ",
484 SCpnt
->host
->host_no
, (int) SCpnt
->channel
,
485 (int) SCpnt
->target
, (int) SCpnt
->lun
);
486 print_command(SCpnt
->cmnd
);
487 print_sense("sd", SCpnt
);
488 SCpnt
= end_scsi_request(SCpnt
, 0, block_sectors
);
489 requeue_sd_request(SCpnt
);
492 } /* driver byte != 0 */
494 printk("SCSI disk error : host %d channel %d id %d lun %d return code = %x\n",
495 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->host
->host_no
,
496 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->channel
,
497 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->id
,
498 rscsi_disks
[DEVICE_NR(SCpnt
->request
.rq_dev
)].device
->lun
, result
);
500 if (driver_byte(result
) & DRIVER_SENSE
)
501 print_sense("sd", SCpnt
);
502 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.current_nr_sectors
);
503 requeue_sd_request(SCpnt
);
509 * requeue_sd_request() is the request handler function for the sd driver.
510 * Its function in life is to take block device requests, and translate
511 * them to SCSI commands.
514 static void do_sd_request (void)
516 Scsi_Cmnd
* SCpnt
= NULL
;
518 struct request
* req
= NULL
;
522 if (CURRENT
!= NULL
&& CURRENT
->rq_status
== RQ_INACTIVE
) {
527 SDev
= rscsi_disks
[DEVICE_NR(CURRENT
->rq_dev
)].device
;
530 * If the host for this device is in error recovery mode, don't
531 * do anything at all here. When the host leaves error recovery
532 * mode, it will automatically restart things and start queueing
535 if( SDev
->host
->in_recovery
)
541 * I am not sure where the best place to do this is. We need
542 * to hook in a place where we are likely to come if in user
545 if( SDev
->was_reset
)
548 * We need to relock the door, but we might
549 * be in an interrupt handler. Only do this
550 * from user space, since we do not want to
551 * sleep from an interrupt. FIXME(eric) - do this
552 * from the kernel error handling thred.
554 if( SDev
->removable
&& !in_interrupt() )
556 spin_unlock_irq(&io_request_lock
); /* FIXME!!!! */
557 scsi_ioctl(SDev
, SCSI_IOCTL_DOORLOCK
, 0);
558 /* scsi_ioctl may allow CURRENT to change, so start over. */
560 spin_lock_irq(&io_request_lock
); /* FIXME!!!! */
566 /* We have to be careful here. scsi_allocate_device will get a free pointer,
567 * but there is no guarantee that it is queueable. In normal usage,
568 * we want to call this, because other types of devices may have the
569 * host all tied up, and we want to make sure that we have at least
570 * one request pending for this type of device. We can also come
571 * through here while servicing an interrupt, because of the need to
572 * start another command. If we call scsi_allocate_device more than once,
573 * then the system can wedge if the command is not queueable. The
574 * scsi_request_queueable function is safe because it checks to make sure
575 * that the host is able to take another command before it returns
580 SCpnt
= scsi_allocate_device(&CURRENT
,
581 rscsi_disks
[DEVICE_NR(CURRENT
->rq_dev
)].device
, 0);
585 * The following restore_flags leads to latency problems. FIXME.
586 * Using a "sti()" gets rid of the latency problems but causes
587 * race conditions and crashes.
590 /* This is a performance enhancement. We dig down into the request
591 * list and try to find a queueable request (i.e. device not busy,
592 * and host able to accept another command. If we find one, then we
593 * queue it. This can make a big difference on systems with more than
594 * one disk drive. We want to have the interrupts off when monkeying
595 * with the request list, because otherwise the kernel might try to
596 * slip in a request in between somewhere.
598 * FIXME(eric) - this doesn't belong at this level. The device code in
599 * ll_rw_blk.c should know how to dig down into the device queue to
600 * figure out what it can deal with, and what it can't. Consider
601 * possibility of pulling entire queue down into scsi layer.
603 if (!SCpnt
&& sd_template
.nr_dev
> 1){
604 struct request
*req1
;
608 SCpnt
= scsi_request_queueable(req
,
609 rscsi_disks
[DEVICE_NR(req
->rq_dev
)].device
);
614 if (SCpnt
&& req
->rq_status
== RQ_INACTIVE
) {
616 CURRENT
= CURRENT
->next
;
618 req1
->next
= req
->next
;
622 if (!SCpnt
) return; /* Could not find anything to do */
625 requeue_sd_request(SCpnt
);
629 static void requeue_sd_request (Scsi_Cmnd
* SCpnt
)
631 int dev
, devm
, block
, this_count
;
632 unsigned char cmd
[10];
634 int bounce_size
, contiguous
;
636 struct buffer_head
* bh
, *bhp
;
637 char * buff
, *bounce_buffer
;
641 if(!SCpnt
|| SCpnt
->request
.rq_status
== RQ_INACTIVE
) {
646 devm
= MINOR(SCpnt
->request
.rq_dev
);
647 dev
= DEVICE_NR(SCpnt
->request
.rq_dev
);
649 block
= SCpnt
->request
.sector
;
652 SCSI_LOG_HLQUEUE(1,printk("Doing sd request, dev = %d, block = %d\n", devm
, block
));
654 if (devm
>= (sd_template
.dev_max
<< 4) ||
655 !rscsi_disks
[dev
].device
||
656 !rscsi_disks
[dev
].device
->online
||
657 block
+ SCpnt
->request
.nr_sectors
> sd
[devm
].nr_sects
)
659 SCSI_LOG_HLQUEUE(2,printk("Finishing %ld sectors\n", SCpnt
->request
.nr_sectors
));
660 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
661 SCSI_LOG_HLQUEUE(2,printk("Retry with 0x%p\n", SCpnt
));
665 block
+= sd
[devm
].start_sect
;
667 if (rscsi_disks
[dev
].device
->changed
)
670 * quietly refuse to do anything to a changed disc until the changed
673 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
674 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
678 sd_devname(devm
, nbuff
);
679 SCSI_LOG_HLQUEUE(2,printk("%s : real dev = /dev/%d, block = %d\n",
683 * If we have a 1K hardware sectorsize, prevent access to single
684 * 512 byte sectors. In theory we could handle this - in fact
685 * the scsi cdrom driver must be able to handle this because
686 * we typically use 1K blocksizes, and cdroms typically have
687 * 2K hardware sectorsizes. Of course, things are simpler
688 * with the cdrom, since it is read-only. For performance
689 * reasons, the filesystems should be able to handle this
690 * and not force the scsi disk driver to use bounce buffers
693 if (rscsi_disks
[dev
].sector_size
== 1024)
694 if((block
& 1) || (SCpnt
->request
.nr_sectors
& 1)) {
695 printk("sd.c:Bad block number requested");
696 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
700 if (rscsi_disks
[dev
].sector_size
== 2048)
701 if((block
& 3) || (SCpnt
->request
.nr_sectors
& 3)) {
702 printk("sd.c:Bad block number requested");
703 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
707 switch (SCpnt
->request
.cmd
)
710 if (!rscsi_disks
[dev
].device
->writeable
)
712 SCpnt
= end_scsi_request(SCpnt
, 0, SCpnt
->request
.nr_sectors
);
721 panic ("Unknown sd command %d\n", SCpnt
->request
.cmd
);
724 SCpnt
->this_count
= 0;
726 /* If the host adapter can deal with very large scatter-gather
727 * requests, it is a waste of time to cluster
729 contiguous
= (!CLUSTERABLE_DEVICE(SCpnt
) ? 0 :1);
730 bounce_buffer
= NULL
;
731 bounce_size
= (SCpnt
->request
.nr_sectors
<< 9);
733 /* First see if we need a bounce buffer for this request. If we do, make
734 * sure that we can allocate a buffer. Do not waste space by allocating
735 * a bounce buffer if we are straddling the 16Mb line
737 if (contiguous
&& SCpnt
->request
.bh
&&
738 virt_to_phys(SCpnt
->request
.bh
->b_data
)
739 + (SCpnt
->request
.nr_sectors
<< 9) - 1 > ISA_DMA_THRESHOLD
740 && SCpnt
->host
->unchecked_isa_dma
) {
741 if(virt_to_phys(SCpnt
->request
.bh
->b_data
) > ISA_DMA_THRESHOLD
)
742 bounce_buffer
= (char *) scsi_malloc(bounce_size
);
743 if(!bounce_buffer
) contiguous
= 0;
746 if(contiguous
&& SCpnt
->request
.bh
&& SCpnt
->request
.bh
->b_reqnext
)
747 for(bh
= SCpnt
->request
.bh
, bhp
= bh
->b_reqnext
; bhp
; bh
= bhp
,
748 bhp
= bhp
->b_reqnext
) {
749 if(!CONTIGUOUS_BUFFERS(bh
,bhp
)) {
750 if(bounce_buffer
) scsi_free(bounce_buffer
, bounce_size
);
755 if (!SCpnt
->request
.bh
|| contiguous
) {
757 /* case of page request (i.e. raw device), or unlinked buffer */
758 this_count
= SCpnt
->request
.nr_sectors
;
759 buff
= SCpnt
->request
.buffer
;
762 } else if (SCpnt
->host
->sg_tablesize
== 0 ||
763 (scsi_need_isa_buffer
&& scsi_dma_free_sectors
<= 10)) {
765 /* Case of host adapter that cannot scatter-gather. We also
766 * come here if we are running low on DMA buffer memory. We set
767 * a threshold higher than that we would need for this request so
768 * we leave room for other requests. Even though we would not need
769 * it all, we need to be conservative, because if we run low enough
770 * we have no choice but to panic.
772 if (SCpnt
->host
->sg_tablesize
!= 0 &&
773 scsi_need_isa_buffer
&&
774 scsi_dma_free_sectors
<= 10)
775 printk("Warning: SCSI DMA buffer space running low. Using non scatter-gather I/O.\n");
777 this_count
= SCpnt
->request
.current_nr_sectors
;
778 buff
= SCpnt
->request
.buffer
;
783 /* Scatter-gather capable host adapter */
784 struct scatterlist
* sgpnt
;
785 int count
, this_count_max
;
788 bh
= SCpnt
->request
.bh
;
790 this_count_max
= (rscsi_disks
[dev
].ten
? 0xffff : 0xff);
794 if ((this_count
+ (bh
->b_size
>> 9)) > this_count_max
) break;
795 if(!bhp
|| !CONTIGUOUS_BUFFERS(bhp
,bh
) ||
796 !CLUSTERABLE_DEVICE(SCpnt
) ||
797 (SCpnt
->host
->unchecked_isa_dma
&&
798 virt_to_phys(bh
->b_data
-1) == ISA_DMA_THRESHOLD
)) {
799 if (count
< SCpnt
->host
->sg_tablesize
) count
++;
802 this_count
+= (bh
->b_size
>> 9);
807 if(SCpnt
->host
->unchecked_isa_dma
&&
808 virt_to_phys(SCpnt
->request
.bh
->b_data
-1) == ISA_DMA_THRESHOLD
) count
--;
810 SCpnt
->use_sg
= count
; /* Number of chains */
811 /* scsi_malloc can only allocate in chunks of 512 bytes */
812 count
= (SCpnt
->use_sg
* sizeof(struct scatterlist
) + 511) & ~511;
814 SCpnt
->sglist_len
= count
;
815 max_sg
= count
/ sizeof(struct scatterlist
);
816 if(SCpnt
->host
->sg_tablesize
< max_sg
)
817 max_sg
= SCpnt
->host
->sg_tablesize
;
818 sgpnt
= (struct scatterlist
* ) scsi_malloc(count
);
820 printk("Warning - running *really* short on DMA buffers\n");
821 SCpnt
->use_sg
= 0; /* No memory left - bail out */
822 this_count
= SCpnt
->request
.current_nr_sectors
;
823 buff
= SCpnt
->request
.buffer
;
825 memset(sgpnt
, 0, count
); /* Zero so it is easy to fill, but only
826 * if memory is available
828 buff
= (char *) sgpnt
;
830 for(count
= 0, bh
= SCpnt
->request
.bh
, bhp
= bh
->b_reqnext
;
831 count
< SCpnt
->use_sg
&& bh
;
836 if(!sgpnt
[count
].address
) sgpnt
[count
].address
= bh
->b_data
;
837 sgpnt
[count
].length
+= bh
->b_size
;
838 counted
+= bh
->b_size
>> 9;
840 if (virt_to_phys(sgpnt
[count
].address
) + sgpnt
[count
].length
- 1 >
841 ISA_DMA_THRESHOLD
&& (SCpnt
->host
->unchecked_isa_dma
) &&
842 !sgpnt
[count
].alt_address
) {
843 sgpnt
[count
].alt_address
= sgpnt
[count
].address
;
844 /* We try to avoid exhausting the DMA pool, since it is
845 * easier to control usage here. In other places we might
846 * have a more pressing need, and we would be screwed if
848 if(scsi_dma_free_sectors
< (sgpnt
[count
].length
>> 9) + 10) {
849 sgpnt
[count
].address
= NULL
;
851 sgpnt
[count
].address
=
852 (char *) scsi_malloc(sgpnt
[count
].length
);
854 /* If we start running low on DMA buffers, we abort the
855 * scatter-gather operation, and free all of the memory
856 * we have allocated. We want to ensure that all scsi
857 * operations are able to do at least a non-scatter/gather
859 if(sgpnt
[count
].address
== NULL
){ /* Out of dma memory */
861 printk("Warning: Running low on SCSI DMA buffers");
862 /* Try switching back to a non s-g operation. */
864 if(sgpnt
[count
].alt_address
)
865 scsi_free(sgpnt
[count
].address
,
866 sgpnt
[count
].length
);
868 this_count
= SCpnt
->request
.current_nr_sectors
;
869 buff
= SCpnt
->request
.buffer
;
871 scsi_free(sgpnt
, SCpnt
->sglist_len
);
873 SCpnt
->use_sg
= count
;
874 this_count
= counted
-= bh
->b_size
>> 9;
879 /* Only cluster buffers if we know that we can supply DMA
880 * buffers large enough to satisfy the request. Do not cluster
881 * a new request if this would mean that we suddenly need to
882 * start using DMA bounce buffers */
883 if(bhp
&& CONTIGUOUS_BUFFERS(bh
,bhp
)
884 && CLUSTERABLE_DEVICE(SCpnt
)) {
887 if (virt_to_phys(sgpnt
[count
].address
) + sgpnt
[count
].length
+
888 bhp
->b_size
- 1 > ISA_DMA_THRESHOLD
&&
889 (SCpnt
->host
->unchecked_isa_dma
) &&
890 !sgpnt
[count
].alt_address
) continue;
892 if(!sgpnt
[count
].alt_address
) {count
--; continue; }
893 if(scsi_dma_free_sectors
> 10)
894 tmp
= (char *) scsi_malloc(sgpnt
[count
].length
898 max_sg
= SCpnt
->use_sg
;
901 scsi_free(sgpnt
[count
].address
, sgpnt
[count
].length
);
902 sgpnt
[count
].address
= tmp
;
907 /* If we are allowed another sg chain, then increment
908 * counter so we can insert it. Otherwise we will end
911 if (SCpnt
->use_sg
< max_sg
) SCpnt
->use_sg
++;
912 } /* contiguous buffers */
915 /* This is actually how many we are going to transfer */
916 this_count
= counted
;
918 if(count
< SCpnt
->use_sg
|| SCpnt
->use_sg
919 > SCpnt
->host
->sg_tablesize
){
920 bh
= SCpnt
->request
.bh
;
921 printk("Use sg, count %d %x %d\n",
922 SCpnt
->use_sg
, count
, scsi_dma_free_sectors
);
923 printk("maxsg = %x, counted = %d this_count = %d\n",
924 max_sg
, counted
, this_count
);
926 printk("[%p %lx] ", bh
->b_data
, bh
->b_size
);
929 if(SCpnt
->use_sg
< 16)
930 for(count
=0; count
<SCpnt
->use_sg
; count
++)
931 printk("{%d:%p %p %d} ", count
,
932 sgpnt
[count
].address
,
933 sgpnt
[count
].alt_address
,
934 sgpnt
[count
].length
);
938 if (SCpnt
->request
.cmd
== WRITE
)
939 for(count
=0; count
<SCpnt
->use_sg
; count
++)
940 if(sgpnt
[count
].alt_address
)
941 memcpy(sgpnt
[count
].address
, sgpnt
[count
].alt_address
,
942 sgpnt
[count
].length
);
943 } /* Able to malloc sgpnt */
944 } /* Host adapter capable of scatter-gather */
946 /* Now handle the possibility of DMA to addresses > 16Mb */
948 if(SCpnt
->use_sg
== 0){
949 if (virt_to_phys(buff
) + (this_count
<< 9) - 1 > ISA_DMA_THRESHOLD
&&
950 (SCpnt
->host
->unchecked_isa_dma
)) {
952 buff
= bounce_buffer
;
954 buff
= (char *) scsi_malloc(this_count
<< 9);
955 if(buff
== NULL
) { /* Try backing off a bit if we are low on mem*/
956 this_count
= SCpnt
->request
.current_nr_sectors
;
957 buff
= (char *) scsi_malloc(this_count
<< 9);
958 if(!buff
) panic("Ran out of DMA buffers.");
960 if (SCpnt
->request
.cmd
== WRITE
)
961 memcpy(buff
, (char *)SCpnt
->request
.buffer
, this_count
<< 9);
964 SCSI_LOG_HLQUEUE(2,printk("%s : %s %d/%ld 512 byte blocks.\n",
966 (SCpnt
->request
.cmd
== WRITE
) ? "writing" : "reading",
967 this_count
, SCpnt
->request
.nr_sectors
));
969 cmd
[1] = (SCpnt
->lun
<< 5) & 0xe0;
971 if (rscsi_disks
[dev
].sector_size
== 2048){
972 if(block
& 3) panic("sd.c:Bad block number requested");
973 if(this_count
& 3) panic("sd.c:Bad block number requested");
975 this_count
= this_count
>> 2;
978 if (rscsi_disks
[dev
].sector_size
== 1024){
979 if(block
& 1) panic("sd.c:Bad block number requested");
980 if(this_count
& 1) panic("sd.c:Bad block number requested");
982 this_count
= this_count
>> 1;
985 if (rscsi_disks
[dev
].sector_size
== 256){
987 this_count
= this_count
<< 1;
990 if (((this_count
> 0xff) || (block
> 0x1fffff)) && rscsi_disks
[dev
].ten
)
992 if (this_count
> 0xffff)
995 cmd
[0] += READ_10
- READ_6
;
996 cmd
[2] = (unsigned char) (block
>> 24) & 0xff;
997 cmd
[3] = (unsigned char) (block
>> 16) & 0xff;
998 cmd
[4] = (unsigned char) (block
>> 8) & 0xff;
999 cmd
[5] = (unsigned char) block
& 0xff;
1000 cmd
[6] = cmd
[9] = 0;
1001 cmd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
1002 cmd
[8] = (unsigned char) this_count
& 0xff;
1006 if (this_count
> 0xff)
1009 cmd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
1010 cmd
[2] = (unsigned char) ((block
>> 8) & 0xff);
1011 cmd
[3] = (unsigned char) block
& 0xff;
1012 cmd
[4] = (unsigned char) this_count
;
1017 * We shouldn't disconnect in the middle of a sector, so with a dumb
1018 * host adapter, it's safe to assume that we can at least transfer
1019 * this many bytes between each connect / disconnect.
1022 SCpnt
->transfersize
= rscsi_disks
[dev
].sector_size
;
1023 SCpnt
->underflow
= this_count
<< 9;
1024 scsi_do_cmd (SCpnt
, (void *) cmd
, buff
,
1025 this_count
* rscsi_disks
[dev
].sector_size
,
1027 (SCpnt
->device
->type
== TYPE_DISK
?
1028 SD_TIMEOUT
: SD_MOD_TIMEOUT
),
1032 static int check_scsidisk_media_change(kdev_t full_dev
){
1038 target
= DEVICE_NR(full_dev
);
1040 if (target
>= sd_template
.dev_max
||
1041 !rscsi_disks
[target
].device
) {
1042 printk("SCSI disk request error: invalid device.\n");
1046 if(!rscsi_disks
[target
].device
->removable
) return 0;
1049 * If the device is offline, don't send any commands - just pretend as if
1050 * the command failed. If the device ever comes back online, we can deal with
1051 * it then. It is only because of unrecoverable errors that we would ever
1052 * take a device offline in the first place.
1054 if( rscsi_disks
[target
].device
->online
== FALSE
)
1056 rscsi_disks
[target
].ready
= 0;
1057 rscsi_disks
[target
].device
->changed
= 1;
1058 return 1; /* This will force a flush, if called from
1059 * check_disk_change */
1062 inode
.i_rdev
= full_dev
; /* This is all we really need here */
1064 /* Using Start/Stop enables differentiation between drive with
1065 * no cartridge loaded - NOT READY, drive with changed cartridge -
1066 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1067 * This also handles drives that auto spin down. eg iomega jaz 1GB
1068 * as this will spin up the drive.
1070 retval
= sd_ioctl(&inode
, NULL
, SCSI_IOCTL_START_UNIT
, 0);
1072 if(retval
){ /* Unable to test, unit probably not ready. This usually
1073 * means there is no disc in the drive. Mark as changed,
1074 * and we will figure it out later once the drive is
1075 * available again. */
1077 rscsi_disks
[target
].ready
= 0;
1078 rscsi_disks
[target
].device
->changed
= 1;
1079 return 1; /* This will force a flush, if called from
1080 * check_disk_change */
1084 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
1085 * presence of disk in the drive. This is kept in the Scsi_Disk
1086 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
1089 rscsi_disks
[target
].ready
= 1; /* FLOPTICAL */
1091 retval
= rscsi_disks
[target
].device
->changed
;
1092 if(!flag
) rscsi_disks
[target
].device
->changed
= 0;
1096 static void sd_init_done (Scsi_Cmnd
* SCpnt
)
1098 struct request
* req
;
1100 req
= &SCpnt
->request
;
1101 req
->rq_status
= RQ_SCSI_DONE
; /* Busy, but indicate request done */
1103 if (req
->sem
!= NULL
) {
1108 static int sd_init_onedisk(int i
)
1110 unsigned char cmd
[10];
1112 unsigned char *buffer
;
1113 unsigned long spintime
;
1114 int the_result
, retries
;
1118 * Get the name of the disk, in case we need to log it somewhere.
1120 sd_devname(i
, nbuff
);
1123 * If the device is offline, don't try and read capacity or any of the other
1126 if( rscsi_disks
[i
].device
->online
== FALSE
)
1131 spin_lock_irq(&io_request_lock
);
1133 /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
1134 * considered a fatal error, and many devices report such an error
1135 * just after a scsi bus reset.
1138 SCpnt
= scsi_allocate_device(NULL
, rscsi_disks
[i
].device
, 1);
1139 buffer
= (unsigned char *) scsi_malloc(512);
1143 /* Spin up drives, as required. Only do this at boot time */
1144 /* Spinup needs to be done for module loads too. */
1149 cmd
[0] = TEST_UNIT_READY
;
1150 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1151 memset ((void *) &cmd
[2], 0, 8);
1153 SCpnt
->sense_buffer
[0] = 0;
1154 SCpnt
->sense_buffer
[2] = 0;
1157 struct semaphore sem
= MUTEX_LOCKED
;
1158 /* Mark as really busy again */
1159 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
;
1160 SCpnt
->request
.sem
= &sem
;
1162 (void *) cmd
, (void *) buffer
,
1163 512, sd_init_done
, SD_TIMEOUT
,
1165 spin_unlock_irq(&io_request_lock
);
1167 spin_lock_irq(&io_request_lock
);
1168 SCpnt
->request
.sem
= NULL
;
1171 the_result
= SCpnt
->result
;
1174 || SCpnt
->sense_buffer
[2] != UNIT_ATTENTION
)
1178 /* Look for non-removable devices that return NOT_READY.
1179 * Issue command to spin up drive for these cases. */
1180 if(the_result
&& !rscsi_disks
[i
].device
->removable
&&
1181 SCpnt
->sense_buffer
[2] == NOT_READY
) {
1182 unsigned long time1
;
1184 printk( "%s: Spinning up disk...", nbuff
);
1185 cmd
[0] = START_STOP
;
1186 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1187 cmd
[1] |= 1; /* Return immediately */
1188 memset ((void *) &cmd
[2], 0, 8);
1189 cmd
[4] = 1; /* Start spin cycle */
1191 SCpnt
->sense_buffer
[0] = 0;
1192 SCpnt
->sense_buffer
[2] = 0;
1195 struct semaphore sem
= MUTEX_LOCKED
;
1196 /* Mark as really busy again */
1197 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
;
1198 SCpnt
->request
.sem
= &sem
;
1200 (void *) cmd
, (void *) buffer
,
1201 512, sd_init_done
, SD_TIMEOUT
,
1203 spin_unlock_irq(&io_request_lock
);
1205 spin_lock_irq(&io_request_lock
);
1206 SCpnt
->request
.sem
= NULL
;
1212 time1
= jiffies
+ HZ
;
1213 spin_unlock_irq(&io_request_lock
);
1214 while(jiffies
< time1
); /* Wait 1 second for next try */
1216 spin_lock_irq(&io_request_lock
);
1218 } while(the_result
&& spintime
&& spintime
+100*HZ
> jiffies
);
1221 printk( "not responding...\n" );
1223 printk( "ready\n" );
1228 cmd
[0] = READ_CAPACITY
;
1229 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1230 memset ((void *) &cmd
[2], 0, 8);
1231 memset ((void *) buffer
, 0, 8);
1233 SCpnt
->sense_buffer
[0] = 0;
1234 SCpnt
->sense_buffer
[2] = 0;
1237 struct semaphore sem
= MUTEX_LOCKED
;
1238 /* Mark as really busy again */
1239 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
;
1240 SCpnt
->request
.sem
= &sem
;
1242 (void *) cmd
, (void *) buffer
,
1243 8, sd_init_done
, SD_TIMEOUT
,
1245 spin_unlock_irq(&io_request_lock
);
1246 down(&sem
); /* sleep until it is ready */
1247 spin_lock_irq(&io_request_lock
);
1248 SCpnt
->request
.sem
= NULL
;
1251 the_result
= SCpnt
->result
;
1254 } while(the_result
&& retries
);
1257 * The SCSI standard says:
1258 * "READ CAPACITY is necessary for self configuring software"
1259 * While not mandatory, support of READ CAPACITY is strongly encouraged.
1260 * We used to die if we couldn't successfully do a READ CAPACITY.
1261 * But, now we go on about our way. The side effects of this are
1263 * 1. We can't know block size with certainty. I have said "512 bytes
1264 * is it" as this is most common.
1266 * 2. Recovery from when some one attempts to read past the end of the
1267 * raw device will be slower.
1272 printk ("%s : READ CAPACITY failed.\n"
1273 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
1275 status_byte(the_result
),
1276 msg_byte(the_result
),
1277 host_byte(the_result
),
1278 driver_byte(the_result
)
1280 if (driver_byte(the_result
) & DRIVER_SENSE
)
1281 printk("%s : extended sense code = %1x \n",
1282 nbuff
, SCpnt
->sense_buffer
[2] & 0xf);
1284 printk("%s : sense not available. \n", nbuff
);
1286 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
1288 rscsi_disks
[i
].capacity
= 0x1fffff;
1289 rscsi_disks
[i
].sector_size
= 512;
1291 /* Set dirty bit for removable devices if not ready - sometimes drives
1292 * will not report this properly. */
1293 if(rscsi_disks
[i
].device
->removable
&&
1294 SCpnt
->sense_buffer
[2] == NOT_READY
)
1295 rscsi_disks
[i
].device
->changed
= 1;
1301 * FLOPTICAL , if read_capa is ok , drive is assumed to be ready
1303 rscsi_disks
[i
].ready
= 1;
1305 rscsi_disks
[i
].capacity
= 1 + ((buffer
[0] << 24) |
1310 rscsi_disks
[i
].sector_size
= (buffer
[4] << 24) |
1311 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
1313 if (rscsi_disks
[i
].sector_size
== 0) {
1314 rscsi_disks
[i
].sector_size
= 512;
1315 printk("%s : sector size 0 reported, assuming 512.\n", nbuff
);
1319 if (rscsi_disks
[i
].sector_size
!= 512 &&
1320 rscsi_disks
[i
].sector_size
!= 1024 &&
1321 rscsi_disks
[i
].sector_size
!= 2048 &&
1322 rscsi_disks
[i
].sector_size
!= 256)
1324 printk ("%s : unsupported sector size %d.\n",
1325 nbuff
, rscsi_disks
[i
].sector_size
);
1326 if(rscsi_disks
[i
].device
->removable
){
1327 rscsi_disks
[i
].capacity
= 0;
1329 printk ("scsi : deleting disk entry.\n");
1330 rscsi_disks
[i
].device
= NULL
;
1331 sd_template
.nr_dev
--;
1332 sd_gendisk
.nr_real
--;
1334 /* Wake up a process waiting for device */
1335 wake_up(&SCpnt
->device
->device_wait
);
1336 scsi_release_command(SCpnt
);
1343 if( rscsi_disks
[i
].sector_size
== 2048 )
1348 * We must fix the sd_blocksizes and sd_hardsizes
1349 * to allow us to read the partition tables.
1350 * The disk reading code does not allow for reading
1351 * of partial sectors.
1353 for (m
=i
<<4; m
<((i
+1)<<4); m
++)
1355 sd_blocksizes
[m
] = 2048;
1360 * The msdos fs needs to know the hardware sector size
1361 * So I have created this table. See ll_rw_blk.c
1362 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1365 int sz_quot
, sz_rem
;
1366 int hard_sector
= rscsi_disks
[i
].sector_size
;
1367 /* There are 16 minors allocated for each major device */
1368 for (m
=i
<<4; m
<((i
+1)<<4); m
++){
1369 sd_hardsizes
[m
] = hard_sector
;
1371 mb
= rscsi_disks
[i
].capacity
/ 1024 * hard_sector
/ 1024;
1372 /* sz = div(m/100, 10); this seems to not be in the libr */
1373 m
= (mb
+ 50) / 100;
1375 sz_rem
= m
- (10 * sz_quot
);
1376 printk ("SCSI device %s: hdwr sector= %d bytes."
1377 " Sectors= %d [%d MB] [%d.%1d GB]\n",
1378 nbuff
, hard_sector
, rscsi_disks
[i
].capacity
,
1379 mb
, sz_quot
, sz_rem
);
1381 if(rscsi_disks
[i
].sector_size
== 2048)
1382 rscsi_disks
[i
].capacity
<<= 2; /* Change into 512 byte sectors */
1383 if(rscsi_disks
[i
].sector_size
== 1024)
1384 rscsi_disks
[i
].capacity
<<= 1; /* Change into 512 byte sectors */
1385 if(rscsi_disks
[i
].sector_size
== 256)
1386 rscsi_disks
[i
].capacity
>>= 1; /* Change into 512 byte sectors */
1391 * Unless otherwise specified, this is not write protected.
1393 rscsi_disks
[i
].write_prot
= 0;
1394 if ( rscsi_disks
[i
].device
->removable
&& rscsi_disks
[i
].ready
) {
1398 * for removable scsi disk ( FLOPTICAL ) we have to recognise
1399 * the Write Protect Flag. This flag is kept in the Scsi_Disk struct
1400 * and tested at open !
1401 * Daniel Roche ( dan@lectra.fr )
1404 memset ((void *) &cmd
[0], 0, 8);
1405 cmd
[0] = MODE_SENSE
;
1406 cmd
[1] = (rscsi_disks
[i
].device
->lun
<< 5) & 0xe0;
1407 cmd
[2] = 1; /* page code 1 ?? */
1410 SCpnt
->sense_buffer
[0] = 0;
1411 SCpnt
->sense_buffer
[2] = 0;
1413 /* same code as READCAPA !! */
1415 struct semaphore sem
= MUTEX_LOCKED
;
1416 SCpnt
->request
.rq_status
= RQ_SCSI_BUSY
; /* Mark as really busy again */
1417 SCpnt
->request
.sem
= &sem
;
1419 (void *) cmd
, (void *) buffer
,
1420 512, sd_init_done
, SD_TIMEOUT
,
1422 spin_unlock_irq(&io_request_lock
);
1424 spin_lock_irq(&io_request_lock
);
1425 SCpnt
->request
.sem
= NULL
;
1428 the_result
= SCpnt
->result
;
1431 printk ("%s: test WP failed, assume Write Protected\n",nbuff
);
1432 rscsi_disks
[i
].write_prot
= 1;
1434 rscsi_disks
[i
].write_prot
= ((buffer
[2] & 0x80) != 0);
1435 printk ("%s: Write Protect is %s\n",nbuff
,
1436 rscsi_disks
[i
].write_prot
? "on" : "off");
1439 } /* check for write protect */
1441 /* Wake up a process waiting for device */
1442 wake_up(&SCpnt
->device
->device_wait
);
1443 scsi_release_command(SCpnt
);
1446 rscsi_disks
[i
].ten
= 1;
1447 rscsi_disks
[i
].remap
= 1;
1448 scsi_free(buffer
, 512);
1449 spin_unlock_irq(&io_request_lock
);
1454 * The sd_init() function looks at all SCSI drives present, determines
1455 * their size, and reads partition table entries for them.
1458 static int sd_registered
= 0;
1460 static int sd_init()
1464 if (sd_template
.dev_noticed
== 0) return 0;
1466 if(!sd_registered
) {
1467 if (register_blkdev(MAJOR_NR
,"sd",&sd_fops
)) {
1468 printk("Unable to get major %d for SCSI disk\n",MAJOR_NR
);
1474 /* We do not support attaching loadable devices yet. */
1475 if(rscsi_disks
) return 0;
1477 sd_template
.dev_max
= sd_template
.dev_noticed
+ SD_EXTRA_DEVS
;
1479 rscsi_disks
= (Scsi_Disk
*)
1480 scsi_init_malloc(sd_template
.dev_max
* sizeof(Scsi_Disk
), GFP_ATOMIC
);
1481 memset(rscsi_disks
, 0, sd_template
.dev_max
* sizeof(Scsi_Disk
));
1483 sd_sizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) *
1484 sizeof(int), GFP_ATOMIC
);
1485 memset(sd_sizes
, 0, (sd_template
.dev_max
<< 4) * sizeof(int));
1487 sd_blocksizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) *
1488 sizeof(int), GFP_ATOMIC
);
1490 sd_hardsizes
= (int *) scsi_init_malloc((sd_template
.dev_max
<< 4) *
1491 sizeof(int), GFP_ATOMIC
);
1493 for(i
=0;i
<(sd_template
.dev_max
<< 4);i
++)
1495 sd_blocksizes
[i
] = 1024;
1496 sd_hardsizes
[i
] = 512;
1499 blksize_size
[MAJOR_NR
] = sd_blocksizes
;
1500 hardsect_size
[MAJOR_NR
] = sd_hardsizes
;
1501 sd
= (struct hd_struct
*) scsi_init_malloc((sd_template
.dev_max
<< 4) *
1502 sizeof(struct hd_struct
),
1506 sd_gendisk
.max_nr
= sd_template
.dev_max
;
1507 sd_gendisk
.part
= sd
;
1508 sd_gendisk
.sizes
= sd_sizes
;
1509 sd_gendisk
.real_devices
= (void *) rscsi_disks
;
1513 static void sd_finish()
1515 struct gendisk
*gendisk
;
1518 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
1520 for (gendisk
= gendisk_head
; gendisk
!= NULL
; gendisk
= gendisk
->next
)
1521 if (gendisk
== &sd_gendisk
)
1523 if (gendisk
== NULL
)
1525 sd_gendisk
.next
= gendisk_head
;
1526 gendisk_head
= &sd_gendisk
;
1529 for (i
= 0; i
< sd_template
.dev_max
; ++i
)
1530 if (!rscsi_disks
[i
].capacity
&&
1531 rscsi_disks
[i
].device
)
1534 && !rscsi_disks
[i
].has_part_table
) {
1535 sd_sizes
[i
<< 4] = rscsi_disks
[i
].capacity
;
1536 /* revalidate does sd_init_onedisk via MAYBE_REINIT*/
1537 revalidate_scsidisk(MKDEV(MAJOR_NR
, i
<< 4), 0);
1540 i
=sd_init_onedisk(i
);
1541 rscsi_disks
[i
].has_part_table
= 1;
1544 /* If our host adapter is capable of scatter-gather, then we increase
1545 * the read-ahead to 16 blocks (32 sectors). If not, we use
1546 * a two block (4 sector) read ahead.
1548 if(rscsi_disks
[0].device
&& rscsi_disks
[0].device
->host
->sg_tablesize
)
1549 read_ahead
[MAJOR_NR
] = 120; /* 120 sector read-ahead */
1551 read_ahead
[MAJOR_NR
] = 4; /* 4 sector read-ahead */
1556 static int sd_detect(Scsi_Device
* SDp
){
1558 if(SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
) return 0;
1560 sd_devname(sd_template
.dev_noticed
++, nbuff
);
1561 printk("Detected scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1562 SDp
->removable
? "removable " : "",
1564 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
1569 static int sd_attach(Scsi_Device
* SDp
){
1573 if(SDp
->type
!= TYPE_DISK
&& SDp
->type
!= TYPE_MOD
) return 0;
1575 if(sd_template
.nr_dev
>= sd_template
.dev_max
) {
1580 for(dpnt
= rscsi_disks
, i
=0; i
<sd_template
.dev_max
; i
++, dpnt
++)
1581 if(!dpnt
->device
) break;
1583 if(i
>= sd_template
.dev_max
) 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
.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)
1597 #define GENDISK_STRUCT sd_gendisk
1599 /* This routine is called to flush all partitions and partition tables
1600 * for a changed scsi disk, and then re-read the new partition table.
1601 * If we are revalidating a disk because of a media change, then we
1602 * enter with usage == 0. If we are using an ioctl, we automatically have
1603 * usage == 1 (we need an open channel to use an ioctl :-), so this
1606 int revalidate_scsidisk(kdev_t dev
, int maxusage
){
1608 struct gendisk
* gdev
;
1613 target
= DEVICE_NR(dev
);
1614 gdev
= &GENDISK_STRUCT
;
1616 if (DEVICE_BUSY
|| USAGE
> maxusage
) {
1617 printk("Device busy for revalidation (usage=%d)\n", USAGE
);
1622 max_p
= gdev
->max_p
;
1623 start
= target
<< gdev
->minor_shift
;
1625 for (i
=max_p
- 1; i
>=0 ; i
--) {
1626 int minor
= start
+i
;
1627 kdev_t devi
= MKDEV(MAJOR_NR
, minor
);
1628 struct super_block
*sb
= get_super(devi
);
1630 if (sb
) invalidate_inodes(sb
);
1631 invalidate_buffers(devi
);
1632 gdev
->part
[minor
].start_sect
= 0;
1633 gdev
->part
[minor
].nr_sects
= 0;
1635 * Reset the blocksize for everything so that we can read
1636 * the partition table. Technically we will determine the
1637 * correct block size when we revalidate, but we do this just
1638 * to make sure that everything remains consistent.
1640 blksize_size
[MAJOR_NR
][minor
] = 1024;
1641 if( rscsi_disks
[target
].sector_size
== 2048 )
1642 blksize_size
[MAJOR_NR
][minor
] = 2048;
1644 blksize_size
[MAJOR_NR
][minor
] = 1024;
1651 gdev
->part
[start
].nr_sects
= CAPACITY
;
1652 resetup_one_dev(gdev
, target
);
1658 static int fop_revalidate_scsidisk(kdev_t dev
){
1659 return revalidate_scsidisk(dev
, 0);
1663 static void sd_detach(Scsi_Device
* SDp
)
1670 for(dpnt
= rscsi_disks
, i
=0; i
<sd_template
.dev_max
; i
++, dpnt
++)
1671 if(dpnt
->device
== SDp
) {
1673 /* If we are disconnecting a disk driver, sync and invalidate
1675 max_p
= sd_gendisk
.max_p
;
1676 start
= i
<< sd_gendisk
.minor_shift
;
1678 for (i
=max_p
- 1; i
>=0 ; i
--) {
1679 int minor
= start
+i
;
1680 kdev_t devi
= MKDEV(MAJOR_NR
, minor
);
1681 struct super_block
*sb
= get_super(devi
);
1683 if (sb
) invalidate_inodes(sb
);
1684 invalidate_buffers(devi
);
1685 sd_gendisk
.part
[minor
].start_sect
= 0;
1686 sd_gendisk
.part
[minor
].nr_sects
= 0;
1687 sd_sizes
[minor
] = 0;
1690 dpnt
->has_part_table
= 0;
1691 dpnt
->device
= NULL
;
1694 sd_template
.dev_noticed
--;
1695 sd_template
.nr_dev
--;
1696 sd_gendisk
.nr_real
--;
1704 int init_module(void) {
1705 sd_template
.module
= &__this_module
;
1706 return scsi_register_module(MODULE_SCSI_DEV
, &sd_template
);
1709 void cleanup_module( void)
1711 struct gendisk
* prev_sdgd
;
1712 struct gendisk
* sdgd
;
1714 scsi_unregister_module(MODULE_SCSI_DEV
, &sd_template
);
1715 unregister_blkdev(SCSI_DISK_MAJOR
, "sd");
1717 if( rscsi_disks
!= NULL
)
1719 scsi_init_free((char *) rscsi_disks
,
1720 (sd_template
.dev_noticed
+ SD_EXTRA_DEVS
)
1721 * sizeof(Scsi_Disk
));
1723 scsi_init_free((char *) sd_sizes
, sd_template
.dev_max
* sizeof(int));
1724 scsi_init_free((char *) sd_blocksizes
, sd_template
.dev_max
* sizeof(int));
1725 scsi_init_free((char *) sd_hardsizes
, sd_template
.dev_max
* sizeof(int));
1726 scsi_init_free((char *) sd
,
1727 (sd_template
.dev_max
<< 4) * sizeof(struct hd_struct
));
1729 * Now remove sd_gendisk from the linked list
1731 sdgd
= gendisk_head
;
1733 while(sdgd
!= &sd_gendisk
)
1739 if(sdgd
!= &sd_gendisk
)
1740 printk("sd_gendisk not in disk chain.\n");
1742 if(prev_sdgd
!= NULL
)
1743 prev_sdgd
->next
= sdgd
->next
;
1745 gendisk_head
= sdgd
->next
;
1749 blksize_size
[MAJOR_NR
] = NULL
;
1750 blk_dev
[MAJOR_NR
].request_fn
= NULL
;
1751 blk_size
[MAJOR_NR
] = NULL
;
1752 hardsect_size
[MAJOR_NR
] = NULL
;
1753 read_ahead
[MAJOR_NR
] = 0;
1754 sd_template
.dev_max
= 0;
1759 * Overrides for Emacs so that we almost follow Linus's tabbing style.
1760 * Emacs will notice this stuff at the end of the file and automatically
1761 * adjust the settings for this buffer only. This must remain at the end
1763 * ---------------------------------------------------------------------------
1766 * c-brace-imaginary-offset: 0
1767 * c-brace-offset: -4
1768 * c-argdecl-indent: 4
1769 * c-label-offset: -4
1770 * c-continued-statement-offset: 4
1771 * c-continued-brace-offset: 0
1772 * indent-tabs-mode: nil