Import 2.1.118
[davej-history.git] / drivers / scsi / sd.c
bloba7d86efba2c9215ae446620cfc14308221201886
1 /*
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
9 * <drew@colorado.edu>
11 * Modified by Eric Youngdale ericy@cais.com to
12 * add scatter-gather, multiple outstanding request, and other
13 * enhancements.
15 * Modified by Eric Youngdale eric@aib.com to support loadable
16 * low-level scsi drivers.
19 #include <linux/module.h>
20 #ifdef MODULE
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
24 * ourselves.
26 #define MODULE_FLAG 1
27 #else
28 #define MODULE_FLAG scsi_loadable_module_flag
29 #endif /* MODULE */
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/mm.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>
42 #include <asm/io.h>
44 #define MAJOR_NR SCSI_DISK_MAJOR
45 #include <linux/blk.h>
46 #include "scsi.h"
47 #include "hosts.h"
48 #include "sd.h"
49 #include <scsi/scsi_ioctl.h>
50 #include "constants.h"
52 #include <linux/genhd.h>
55 * static const char RCSid[] = "$Header:";
58 #define MAX_RETRIES 5
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)
94 if( disknum <= 26 )
96 sprintf(buffer, "sd%c", 'a' + disknum);
98 else
100 unsigned int min1;
101 unsigned int min2;
103 * For larger numbers of disks, we need to go to a new
104 * naming scheme.
106 min1 = disknum / 26;
107 min2 = disknum % 26;
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,
115 sd_detect, sd_init,
116 sd_finish, sd_attach, sd_detach
119 static int sd_open(struct inode * inode, struct file * filp)
121 int target;
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) )
135 return -ENXIO;
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
141 * is being re-read.
144 while (rscsi_disks[target].device->busy)
145 barrier();
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 )
153 return -ENXIO;
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
158 * to the thing.
160 if ( (rscsi_disks[target].write_prot) && (filp->f_mode & 2) )
161 return -EROFS;
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 )
171 return -ENXIO;
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)
179 return -ENXIO;
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);
190 return 0;
193 static int sd_release(struct inode * inode, struct file * file)
195 int target;
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);
211 return 0;
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 */
221 NULL, /* select */
222 sd_ioctl, /* ioctl */
223 NULL, /* mmap */
224 sd_open, /* open code */
225 NULL, /* flush */
226 sd_release, /* release */
227 block_fsync, /* fsync */
228 NULL, /* fasync */
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 */
242 0, /* number */
243 NULL, /* internal */
244 NULL /* next */
247 static void sd_geninit (struct gendisk *ignored)
249 int i;
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;
254 #if 0
255 /* No longer needed - we keep track of this as we attach/detach */
256 sd_gendisk.nr_real = sd_template.dev_max;
257 #endif
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;
269 char nbuff[6];
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,
278 result,
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];
296 int sector_size =
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)
302 error_sector <<= 1;
303 if (block_sectors < 2) block_sectors = 2;
305 else if (sector_size == 2048)
307 error_sector <<= 2;
308 if (block_sectors < 4) block_sectors = 4;
310 else if (sector_size == 256)
311 error_sector >>= 1;
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)
316 good_sectors = 0;
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));
331 if (SCpnt->use_sg) {
332 struct scatterlist * sgpnt;
333 int i;
334 sgpnt = (struct scatterlist *) SCpnt->buffer;
335 for(i=0; i<SCpnt->use_sg; i++) {
337 #if 0
338 SCSI_LOG_HLCOMPLETE(3,printk(":%p %p %d\n",sgpnt[i].alt_address, sgpnt[i].address,
339 sgpnt[i].length));
340 #endif
342 if (sgpnt[i].alt_address) {
343 if (SCpnt->request.cmd == READ)
344 memcpy(sgpnt[i].alt_address, sgpnt[i].address,
345 sgpnt[i].length);
346 scsi_free(sgpnt[i].address, sgpnt[i].length);
350 /* Free list of scatter-gather pointers */
351 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
352 } else {
353 if (SCpnt->buffer != SCpnt->request.buffer) {
354 SCSI_LOG_HLCOMPLETE(3,printk("nosg: %p %p %d\n",
355 SCpnt->request.buffer, SCpnt->buffer,
356 SCpnt->bufflen));
358 if (SCpnt->request.cmd == READ)
359 memcpy(SCpnt->request.buffer, SCpnt->buffer,
360 SCpnt->bufflen);
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",
376 nbuff));
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);
387 if (result == 0)
389 requeue_sd_request(SCpnt);
390 return;
394 if (good_sectors == 0) {
396 /* Free up any indirection buffers we allocated for DMA purposes. */
397 if (SCpnt->use_sg) {
398 struct scatterlist * sgpnt;
399 int i;
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,
404 SCpnt->bufflen));
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 */
410 } else {
411 SCSI_LOG_HLCOMPLETE(2,printk("nosgerr: %p %p %d\n",
412 SCpnt->request.buffer, SCpnt->buffer,
413 SCpnt->bufflen));
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) {
427 #ifdef 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
434 result = 0;
436 else
437 #endif
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
444 * further access.
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);
449 return;
451 else
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);
459 return;
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);
476 result = 0;
477 } else {
478 /* ???? */
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);
490 return;
492 } /* driver byte != 0 */
493 if (result) {
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);
504 return;
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;
517 Scsi_Device * SDev;
518 struct request * req = NULL;
519 int flag = 0;
521 while (1==1){
522 if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
523 return;
526 INIT_SCSI_REQUEST;
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
533 * commands again.
535 if( SDev->host->in_recovery )
537 return;
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
543 * space.
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. */
559 SDev->was_reset = 0;
560 spin_lock_irq(&io_request_lock); /* FIXME!!!! */
561 continue;
563 SDev->was_reset = 0;
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
576 * a pointer.
579 if (flag++ == 0)
580 SCpnt = scsi_allocate_device(&CURRENT,
581 rscsi_disks[DEVICE_NR(CURRENT->rq_dev)].device, 0);
582 else SCpnt = NULL;
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;
605 req1 = NULL;
606 req = CURRENT;
607 while(req){
608 SCpnt = scsi_request_queueable(req,
609 rscsi_disks[DEVICE_NR(req->rq_dev)].device);
610 if(SCpnt) break;
611 req1 = req;
612 req = req->next;
614 if (SCpnt && req->rq_status == RQ_INACTIVE) {
615 if (req == CURRENT)
616 CURRENT = CURRENT->next;
617 else
618 req1->next = req->next;
622 if (!SCpnt) return; /* Could not find anything to do */
624 /* Queue command */
625 requeue_sd_request(SCpnt);
626 } /* While */
629 static void requeue_sd_request (Scsi_Cmnd * SCpnt)
631 int dev, devm, block, this_count;
632 unsigned char cmd[10];
633 char nbuff[6];
634 int bounce_size, contiguous;
635 int max_sg;
636 struct buffer_head * bh, *bhp;
637 char * buff, *bounce_buffer;
639 repeat:
641 if(!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
642 do_sd_request();
643 return;
646 devm = MINOR(SCpnt->request.rq_dev);
647 dev = DEVICE_NR(SCpnt->request.rq_dev);
649 block = SCpnt->request.sector;
650 this_count = 0;
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));
662 goto repeat;
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
671 * bit has been reset
673 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
674 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
675 goto repeat;
678 sd_devname(devm, nbuff);
679 SCSI_LOG_HLQUEUE(2,printk("%s : real dev = /dev/%d, block = %d\n",
680 nbuff, dev, block));
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
691 * for this.
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);
697 goto repeat;
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);
704 goto repeat;
707 switch (SCpnt->request.cmd)
709 case WRITE :
710 if (!rscsi_disks[dev].device->writeable)
712 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
713 goto repeat;
715 cmd[0] = WRITE_6;
716 break;
717 case READ :
718 cmd[0] = READ_6;
719 break;
720 default :
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);
751 contiguous = 0;
752 break;
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;
760 SCpnt->use_sg = 0;
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;
779 SCpnt->use_sg = 0;
781 } else {
783 /* Scatter-gather capable host adapter */
784 struct scatterlist * sgpnt;
785 int count, this_count_max;
786 int counted;
788 bh = SCpnt->request.bh;
789 this_count = 0;
790 this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
791 count = 0;
792 bhp = NULL;
793 while(bh) {
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++;
800 else break;
802 this_count += (bh->b_size >> 9);
803 bhp = bh;
804 bh = bh->b_reqnext;
806 #if 0
807 if(SCpnt->host->unchecked_isa_dma &&
808 virt_to_phys(SCpnt->request.bh->b_data-1) == ISA_DMA_THRESHOLD) count--;
809 #endif
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);
819 if (!sgpnt) {
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;
824 } else {
825 memset(sgpnt, 0, count); /* Zero so it is easy to fill, but only
826 * if memory is available
828 buff = (char *) sgpnt;
829 counted = 0;
830 for(count = 0, bh = SCpnt->request.bh, bhp = bh->b_reqnext;
831 count < SCpnt->use_sg && bh;
832 count++, bh = bhp) {
834 bhp = bh->b_reqnext;
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
847 * we ran out */
848 if(scsi_dma_free_sectors < (sgpnt[count].length >> 9) + 10) {
849 sgpnt[count].address = NULL;
850 } else {
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
858 * operation */
859 if(sgpnt[count].address == NULL){ /* Out of dma memory */
860 #if 0
861 printk("Warning: Running low on SCSI DMA buffers");
862 /* Try switching back to a non s-g operation. */
863 while(--count >= 0){
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;
870 SCpnt->use_sg = 0;
871 scsi_free(sgpnt, SCpnt->sglist_len);
872 #endif
873 SCpnt->use_sg = count;
874 this_count = counted -= bh->b_size >> 9;
875 break;
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)) {
885 char * tmp;
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
895 + bhp->b_size);
896 else {
897 tmp = NULL;
898 max_sg = SCpnt->use_sg;
900 if(tmp){
901 scsi_free(sgpnt[count].address, sgpnt[count].length);
902 sgpnt[count].address = tmp;
903 count--;
904 continue;
907 /* If we are allowed another sg chain, then increment
908 * counter so we can insert it. Otherwise we will end
909 up truncating */
911 if (SCpnt->use_sg < max_sg) SCpnt->use_sg++;
912 } /* contiguous buffers */
913 } /* for loop */
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);
925 while(bh){
926 printk("[%p %lx] ", bh->b_data, bh->b_size);
927 bh = bh->b_reqnext;
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);
935 panic("Ooops");
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)) {
951 if(bounce_buffer)
952 buff = bounce_buffer;
953 else
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",
965 nbuff,
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");
974 block = block >> 2;
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");
981 block = block >> 1;
982 this_count = this_count >> 1;
985 if (rscsi_disks[dev].sector_size == 256){
986 block = block << 1;
987 this_count = this_count << 1;
990 if (((this_count > 0xff) || (block > 0x1fffff)) && rscsi_disks[dev].ten)
992 if (this_count > 0xffff)
993 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;
1004 else
1006 if (this_count > 0xff)
1007 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;
1013 cmd[5] = 0;
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,
1026 rw_intr,
1027 (SCpnt->device->type == TYPE_DISK ?
1028 SD_TIMEOUT : SD_MOD_TIMEOUT),
1029 MAX_RETRIES);
1032 static int check_scsidisk_media_change(kdev_t full_dev){
1033 int retval;
1034 int target;
1035 struct inode inode;
1036 int flag = 0;
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");
1043 return 0;
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;
1093 return retval;
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) {
1104 up(req->sem);
1108 static int sd_init_onedisk(int i)
1110 unsigned char cmd[10];
1111 char nbuff[6];
1112 unsigned char *buffer;
1113 unsigned long spintime;
1114 int the_result, retries;
1115 Scsi_Cmnd * SCpnt;
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
1124 * nicities.
1126 if( rscsi_disks[i].device->online == FALSE )
1128 return i;
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);
1141 spintime = 0;
1143 /* Spin up drives, as required. Only do this at boot time */
1144 /* Spinup needs to be done for module loads too. */
1146 retries = 0;
1147 while(retries < 3)
1149 cmd[0] = TEST_UNIT_READY;
1150 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1151 memset ((void *) &cmd[2], 0, 8);
1152 SCpnt->cmd_len = 0;
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;
1161 scsi_do_cmd (SCpnt,
1162 (void *) cmd, (void *) buffer,
1163 512, sd_init_done, SD_TIMEOUT,
1164 MAX_RETRIES);
1165 spin_unlock_irq(&io_request_lock);
1166 down(&sem);
1167 spin_lock_irq(&io_request_lock);
1168 SCpnt->request.sem = NULL;
1171 the_result = SCpnt->result;
1172 retries++;
1173 if( the_result == 0
1174 || SCpnt->sense_buffer[2] != UNIT_ATTENTION)
1175 break;
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;
1183 if(!spintime){
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 */
1190 SCpnt->cmd_len = 0;
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;
1199 scsi_do_cmd (SCpnt,
1200 (void *) cmd, (void *) buffer,
1201 512, sd_init_done, SD_TIMEOUT,
1202 MAX_RETRIES);
1203 spin_unlock_irq(&io_request_lock);
1204 down(&sem);
1205 spin_lock_irq(&io_request_lock);
1206 SCpnt->request.sem = NULL;
1209 spintime = jiffies;
1212 time1 = jiffies + HZ;
1213 spin_unlock_irq(&io_request_lock);
1214 while(jiffies < time1); /* Wait 1 second for next try */
1215 printk( "." );
1216 spin_lock_irq(&io_request_lock);
1218 } while(the_result && spintime && spintime+100*HZ > jiffies);
1219 if (spintime) {
1220 if (the_result)
1221 printk( "not responding...\n" );
1222 else
1223 printk( "ready\n" );
1226 retries = 3;
1227 do {
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);
1232 SCpnt->cmd_len = 0;
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;
1241 scsi_do_cmd (SCpnt,
1242 (void *) cmd, (void *) buffer,
1243 8, sd_init_done, SD_TIMEOUT,
1244 MAX_RETRIES);
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;
1252 retries--;
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.
1270 if (the_result)
1272 printk ("%s : READ CAPACITY failed.\n"
1273 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
1274 nbuff, nbuff,
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);
1283 else
1284 printk("%s : sense not available. \n", nbuff);
1286 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
1287 nbuff);
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;
1298 else
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) |
1306 (buffer[1] << 16) |
1307 (buffer[2] << 8) |
1308 buffer[3]);
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;
1328 } else {
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);
1337 SCpnt = NULL;
1339 return i;
1343 if( rscsi_disks[i].sector_size == 2048 )
1345 int m;
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)
1364 int m, mb;
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;
1374 sz_quot = m / 10;
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 ) {
1395 /* FLOPTICAL */
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 ?? */
1408 cmd[4] = 12;
1409 SCpnt->cmd_len = 0;
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;
1418 scsi_do_cmd (SCpnt,
1419 (void *) cmd, (void *) buffer,
1420 512, sd_init_done, SD_TIMEOUT,
1421 MAX_RETRIES);
1422 spin_unlock_irq(&io_request_lock);
1423 down(&sem);
1424 spin_lock_irq(&io_request_lock);
1425 SCpnt->request.sem = NULL;
1428 the_result = SCpnt->result;
1430 if ( the_result ) {
1431 printk ("%s: test WP failed, assume Write Protected\n",nbuff);
1432 rscsi_disks[i].write_prot = 1;
1433 } else {
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);
1444 SCpnt = NULL;
1446 rscsi_disks[i].ten = 1;
1447 rscsi_disks[i].remap = 1;
1448 scsi_free(buffer, 512);
1449 spin_unlock_irq(&io_request_lock);
1450 return i;
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()
1462 int i;
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);
1469 return 1;
1471 sd_registered++;
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),
1503 GFP_ATOMIC);
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;
1510 return 0;
1513 static void sd_finish()
1515 struct gendisk *gendisk;
1516 int i;
1518 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1520 for (gendisk = gendisk_head; gendisk != NULL; gendisk = gendisk->next)
1521 if (gendisk == &sd_gendisk)
1522 break;
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)
1533 if (MODULE_FLAG
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);
1539 else
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 */
1550 else
1551 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
1553 return;
1556 static int sd_detect(Scsi_Device * SDp){
1557 char nbuff[6];
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 " : "",
1563 nbuff,
1564 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1566 return 1;
1569 static int sd_attach(Scsi_Device * SDp){
1570 Scsi_Disk * dpnt;
1571 int i;
1573 if(SDp->type != TYPE_DISK && SDp->type != TYPE_MOD) return 0;
1575 if(sd_template.nr_dev >= sd_template.dev_max) {
1576 SDp->attached--;
1577 return 1;
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++;
1590 return 0;
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
1604 * is our limit.
1606 int revalidate_scsidisk(kdev_t dev, int maxusage){
1607 int target;
1608 struct gendisk * gdev;
1609 int max_p;
1610 int start;
1611 int i;
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);
1618 return -EBUSY;
1620 DEVICE_BUSY = 1;
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);
1629 sync_dev(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;
1643 else
1644 blksize_size[MAJOR_NR][minor] = 1024;
1647 #ifdef MAYBE_REINIT
1648 MAYBE_REINIT;
1649 #endif
1651 gdev->part[start].nr_sects = CAPACITY;
1652 resetup_one_dev(gdev, target);
1654 DEVICE_BUSY = 0;
1655 return 0;
1658 static int fop_revalidate_scsidisk(kdev_t dev){
1659 return revalidate_scsidisk(dev, 0);
1663 static void sd_detach(Scsi_Device * SDp)
1665 Scsi_Disk * dpnt;
1666 int i;
1667 int max_p;
1668 int start;
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
1674 * everything */
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);
1682 sync_dev(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;
1692 dpnt->capacity = 0;
1693 SDp->attached--;
1694 sd_template.dev_noticed--;
1695 sd_template.nr_dev--;
1696 sd_gendisk.nr_real--;
1697 return;
1699 return;
1702 #ifdef MODULE
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");
1716 sd_registered--;
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;
1732 prev_sdgd = NULL;
1733 while(sdgd != &sd_gendisk)
1735 prev_sdgd = sdgd;
1736 sdgd = sdgd->next;
1739 if(sdgd != &sd_gendisk)
1740 printk("sd_gendisk not in disk chain.\n");
1741 else {
1742 if(prev_sdgd != NULL)
1743 prev_sdgd->next = sdgd->next;
1744 else
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;
1756 #endif /* MODULE */
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
1762 * of the file.
1763 * ---------------------------------------------------------------------------
1764 * Local variables:
1765 * c-indent-level: 4
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
1773 * tab-width: 8
1774 * End: