Import 2.3.18pre1
[davej-history.git] / drivers / scsi / sd.c
blob71a869a8e04ed573a78bf54deebe70cb24ae7685
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.
18 * Modified by Jirka Hanika geo@ff.cuni.cz to support more
19 * scsi disks using eight major numbers.
22 #include <linux/module.h>
23 #ifdef MODULE
25 * This is a variable in scsi.c that is set when we are processing something
26 * after boot time. By definition, this is true when we are a loadable module
27 * ourselves.
29 #define MODULE_FLAG 1
30 #else
31 #define MODULE_FLAG scsi_loadable_module_flag
32 #endif /* MODULE */
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/mm.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/interrupt.h>
42 #include <linux/smp.h>
44 #include <asm/system.h>
45 #include <asm/io.h>
47 #define MAJOR_NR SCSI_DISK0_MAJOR
48 #include <linux/blk.h>
49 #include "scsi.h"
50 #include "hosts.h"
51 #include "sd.h"
52 #include <scsi/scsi_ioctl.h>
53 #include "constants.h"
55 #include <linux/genhd.h>
58 * static const char RCSid[] = "$Header:";
61 #define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
63 #define SCSI_DISKS_PER_MAJOR 16
64 #define SD_MAJOR_NUMBER(i) SD_MAJOR((i) >> 8)
65 #define SD_MINOR_NUMBER(i) ((i) & 255)
66 #define MKDEV_SD_PARTITION(i) MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
67 #define MKDEV_SD(index) MKDEV_SD_PARTITION((index) << 4)
68 #define N_USED_SCSI_DISKS (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
69 #define N_USED_SD_MAJORS (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
71 #define MAX_RETRIES 5
74 * Time out in seconds for disks and Magneto-opticals (which are slower).
77 #define SD_TIMEOUT (30 * HZ)
78 #define SD_MOD_TIMEOUT (75 * HZ)
80 #define CLUSTERABLE_DEVICE(SC) (SC->host->use_clustering && \
81 SC->device->type != TYPE_MOD)
83 struct hd_struct *sd;
85 Scsi_Disk *rscsi_disks = NULL;
86 static int *sd_sizes;
87 static int *sd_blocksizes;
88 static int *sd_hardsizes; /* Hardware sector size */
90 extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
92 static int check_scsidisk_media_change(kdev_t);
93 static int fop_revalidate_scsidisk(kdev_t);
95 static int sd_init_onedisk(int);
97 static void requeue_sd_request(Scsi_Cmnd * SCpnt);
99 static int sd_init(void);
100 static void sd_finish(void);
101 static int sd_attach(Scsi_Device *);
102 static int sd_detect(Scsi_Device *);
103 static void sd_detach(Scsi_Device *);
105 static void sd_devname(unsigned int disknum, char *buffer)
107 if (disknum < 26)
108 sprintf(buffer, "sd%c", 'a' + disknum);
109 else {
110 unsigned int min1;
111 unsigned int min2;
113 * For larger numbers of disks, we need to go to a new
114 * naming scheme.
116 min1 = disknum / 26;
117 min2 = disknum % 26;
118 sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
122 struct Scsi_Device_Template sd_template =
123 {NULL, "disk", "sd", NULL, TYPE_DISK,
124 SCSI_DISK0_MAJOR, 0, 0, 0, 1,
125 sd_detect, sd_init,
126 sd_finish, sd_attach, sd_detach
129 static int sd_open(struct inode *inode, struct file *filp)
131 int target;
132 target = DEVICE_NR(inode->i_rdev);
134 SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target, sd_template.dev_max));
136 if (target >= sd_template.dev_max || !rscsi_disks[target].device)
137 return -ENXIO; /* No such device */
140 * If the device is in error recovery, wait until it is done.
141 * If the device is offline, then disallow any access to it.
143 if (!scsi_block_when_processing_errors(rscsi_disks[target].device)) {
144 return -ENXIO;
147 * Make sure that only one process can do a check_change_disk at one time.
148 * This is also used to lock out further access when the partition table
149 * is being re-read.
152 while (rscsi_disks[target].device->busy)
153 barrier();
154 if (rscsi_disks[target].device->removable) {
155 check_disk_change(inode->i_rdev);
158 * If the drive is empty, just let the open fail.
160 if (!rscsi_disks[target].ready)
161 return -ENXIO;
164 * Similarly, if the device has the write protect tab set,
165 * have the open fail if the user expects to be able to write
166 * to the thing.
168 if ((rscsi_disks[target].write_prot) && (filp->f_mode & 2))
169 return -EROFS;
172 * It is possible that the disk changing stuff resulted in the device being taken
173 * offline. If this is the case, report this to the user, and don't pretend that
174 * the open actually succeeded.
176 if (!rscsi_disks[target].device->online) {
177 return -ENXIO;
180 * See if we are requesting a non-existent partition. Do this
181 * after checking for disk change.
183 if (sd_sizes[SD_PARTITION(inode->i_rdev)] == 0)
184 return -ENXIO;
186 if (rscsi_disks[target].device->removable)
187 if (!rscsi_disks[target].device->access_count)
188 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
190 rscsi_disks[target].device->access_count++;
191 if (rscsi_disks[target].device->host->hostt->module)
192 __MOD_INC_USE_COUNT(rscsi_disks[target].device->host->hostt->module);
193 if (sd_template.module)
194 __MOD_INC_USE_COUNT(sd_template.module);
195 return 0;
198 static int sd_release(struct inode *inode, struct file *file)
200 int target;
201 fsync_dev(inode->i_rdev);
203 target = DEVICE_NR(inode->i_rdev);
205 rscsi_disks[target].device->access_count--;
207 if (rscsi_disks[target].device->removable) {
208 if (!rscsi_disks[target].device->access_count)
209 sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
211 if (rscsi_disks[target].device->host->hostt->module)
212 __MOD_DEC_USE_COUNT(rscsi_disks[target].device->host->hostt->module);
213 if (sd_template.module)
214 __MOD_DEC_USE_COUNT(sd_template.module);
215 return 0;
218 static void sd_geninit(struct gendisk *);
220 static struct file_operations sd_fops =
222 NULL, /* lseek - default */
223 block_read, /* read - general block-dev read */
224 block_write, /* write - general block-dev write */
225 NULL, /* readdir - bad */
226 NULL, /* select */
227 sd_ioctl, /* ioctl */
228 NULL, /* mmap */
229 sd_open, /* open code */
230 NULL, /* flush */
231 sd_release, /* release */
232 block_fsync, /* fsync */
233 NULL, /* fasync */
234 check_scsidisk_media_change, /* Disk change */
235 fop_revalidate_scsidisk /* revalidate */
239 * If we need more than one SCSI disk major (i.e. more than
240 * 16 SCSI disks), we'll have to kmalloc() more gendisks later.
243 static struct gendisk sd_gendisk =
245 SCSI_DISK0_MAJOR, /* Major number */
246 "sd", /* Major name */
247 4, /* Bits to shift to get real from partition */
248 1 << 4, /* Number of partitions per real */
249 0, /* maximum number of real */
250 sd_geninit, /* init function */
251 NULL, /* hd struct */
252 NULL, /* block sizes */
253 0, /* number */
254 NULL, /* internal */
255 NULL /* next */
258 static struct gendisk *sd_gendisks = &sd_gendisk;
260 #define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
261 #define LAST_SD_GENDISK sd_gendisks[N_USED_SD_MAJORS - 1]
263 static void sd_geninit(struct gendisk *ignored)
265 int i;
267 for (i = 0; i < sd_template.dev_max; ++i)
268 if (rscsi_disks[i].device)
269 sd[i << 4].nr_sects = rscsi_disks[i].capacity;
273 * rw_intr is the interrupt routine for the device driver.
274 * It will be notified on the end of a SCSI read / write, and
275 * will take one of several actions based on success or failure.
278 static void rw_intr(Scsi_Cmnd * SCpnt)
280 int result = SCpnt->result;
281 char nbuff[6];
282 int this_count = SCpnt->bufflen >> 9;
283 int good_sectors = (result == 0 ? this_count : 0);
284 int block_sectors = 1;
286 sd_devname(DEVICE_NR(SCpnt->request.rq_dev), nbuff);
288 SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff,
289 SCpnt->host->host_no,
290 result,
291 SCpnt->sense_buffer[0],
292 SCpnt->sense_buffer[2]));
295 Handle MEDIUM ERRORs that indicate partial success. Since this is a
296 relatively rare error condition, no care is taken to avoid unnecessary
297 additional work such as memcpy's that could be avoided.
300 if (driver_byte(result) != 0 && /* An error occurred */
301 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
302 SCpnt->sense_buffer[2] == MEDIUM_ERROR) {
303 long error_sector = (SCpnt->sense_buffer[3] << 24) |
304 (SCpnt->sense_buffer[4] << 16) |
305 (SCpnt->sense_buffer[5] << 8) |
306 SCpnt->sense_buffer[6];
307 int sector_size =
308 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].sector_size;
309 if (SCpnt->request.bh != NULL)
310 block_sectors = SCpnt->request.bh->b_size >> 9;
311 if (sector_size == 1024) {
312 error_sector <<= 1;
313 if (block_sectors < 2)
314 block_sectors = 2;
315 } else if (sector_size == 2048) {
316 error_sector <<= 2;
317 if (block_sectors < 4)
318 block_sectors = 4;
319 } else if (sector_size == 256)
320 error_sector >>= 1;
321 error_sector -= sd[SD_PARTITION(SCpnt->request.rq_dev)].start_sect;
322 error_sector &= ~(block_sectors - 1);
323 good_sectors = error_sector - SCpnt->request.sector;
324 if (good_sectors < 0 || good_sectors >= this_count)
325 good_sectors = 0;
328 * First case : we assume that the command succeeded. One of two things
329 * will happen here. Either we will be finished, or there will be more
330 * sectors that we were unable to read last time.
333 if (good_sectors > 0) {
335 SCSI_LOG_HLCOMPLETE(1, printk("%s : %ld sectors remain.\n", nbuff,
336 SCpnt->request.nr_sectors));
337 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n ", SCpnt->use_sg));
339 if (SCpnt->use_sg) {
340 struct scatterlist *sgpnt;
341 int i;
342 sgpnt = (struct scatterlist *) SCpnt->buffer;
343 for (i = 0; i < SCpnt->use_sg; i++) {
345 #if 0
346 SCSI_LOG_HLCOMPLETE(3, printk(":%p %p %d\n", sgpnt[i].alt_address, sgpnt[i].address,
347 sgpnt[i].length));
348 #endif
350 if (sgpnt[i].alt_address) {
351 if (SCpnt->request.cmd == READ)
352 memcpy(sgpnt[i].alt_address, sgpnt[i].address,
353 sgpnt[i].length);
354 scsi_free(sgpnt[i].address, sgpnt[i].length);
358 /* Free list of scatter-gather pointers */
359 scsi_free(SCpnt->buffer, SCpnt->sglist_len);
360 } else {
361 if (SCpnt->buffer != SCpnt->request.buffer) {
362 SCSI_LOG_HLCOMPLETE(3, printk("nosg: %p %p %d\n",
363 SCpnt->request.buffer, SCpnt->buffer,
364 SCpnt->bufflen));
366 if (SCpnt->request.cmd == READ)
367 memcpy(SCpnt->request.buffer, SCpnt->buffer,
368 SCpnt->bufflen);
369 scsi_free(SCpnt->buffer, SCpnt->bufflen);
373 * If multiple sectors are requested in one buffer, then
374 * they will have been finished off by the first command.
375 * If not, then we have a multi-buffer command.
377 if (SCpnt->request.nr_sectors > this_count) {
378 SCpnt->request.errors = 0;
380 if (!SCpnt->request.bh) {
381 SCSI_LOG_HLCOMPLETE(2, printk("%s : handling page request, no buffer\n",
382 nbuff));
385 * The SCpnt->request.nr_sectors field is always done in
386 * 512 byte sectors, even if this really isn't the case.
388 panic("sd.c: linked page request (%lx %x)",
389 SCpnt->request.sector, this_count);
392 SCpnt = end_scsi_request(SCpnt, 1, good_sectors);
393 if (result == 0) {
394 requeue_sd_request(SCpnt);
395 return;
398 if (good_sectors == 0) {
400 /* Free up any indirection buffers we allocated for DMA purposes. */
401 if (SCpnt->use_sg) {
402 struct scatterlist *sgpnt;
403 int i;
404 sgpnt = (struct scatterlist *) SCpnt->buffer;
405 for (i = 0; i < SCpnt->use_sg; i++) {
406 SCSI_LOG_HLCOMPLETE(3, printk("err: %p %p %d\n",
407 SCpnt->request.buffer, SCpnt->buffer,
408 SCpnt->bufflen));
409 if (sgpnt[i].alt_address) {
410 scsi_free(sgpnt[i].address, sgpnt[i].length);
413 scsi_free(SCpnt->buffer, SCpnt->sglist_len); /* Free list of scatter-gather pointers */
414 } else {
415 SCSI_LOG_HLCOMPLETE(2, printk("nosgerr: %p %p %d\n",
416 SCpnt->request.buffer, SCpnt->buffer,
417 SCpnt->bufflen));
418 if (SCpnt->buffer != SCpnt->request.buffer)
419 scsi_free(SCpnt->buffer, SCpnt->bufflen);
423 * Now, if we were good little boys and girls, Santa left us a request
424 * sense buffer. We can extract information from this, so we
425 * can choose a block to remap, etc.
428 if (driver_byte(result) != 0) {
429 if (suggestion(result) == SUGGEST_REMAP) {
430 #ifdef REMAP
432 * Not yet implemented. A read will fail after being remapped,
433 * a write will call the strategy routine again.
435 if rscsi_disks
436 [DEVICE_NR(SCpnt->request.rq_dev)].remap
438 result = 0;
440 #endif
442 if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
443 if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
444 if (rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->removable) {
445 /* detected disc change. set a bit and quietly refuse
446 * further access.
448 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->changed = 1;
449 SCpnt = end_scsi_request(SCpnt, 0, this_count);
450 requeue_sd_request(SCpnt);
451 return;
452 } 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;
463 /* If we had an ILLEGAL REQUEST returned, then we may have
464 * performed an unsupported command. The only thing this should be
465 * would be a ten byte read where only a six byte read was supported.
466 * Also, on a system where READ CAPACITY failed, we have have read
467 * past the end of the disk.
470 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
471 if (rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].ten) {
472 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].ten = 0;
473 requeue_sd_request(SCpnt);
474 result = 0;
475 } else {
476 /* ???? */
479 if (SCpnt->sense_buffer[2] == MEDIUM_ERROR) {
480 printk("scsi%d: MEDIUM ERROR on channel %d, id %d, lun %d, CDB: ",
481 SCpnt->host->host_no, (int) SCpnt->channel,
482 (int) SCpnt->target, (int) SCpnt->lun);
483 print_command(SCpnt->cmnd);
484 print_sense("sd", SCpnt);
485 SCpnt = end_scsi_request(SCpnt, 0, block_sectors);
486 requeue_sd_request(SCpnt);
487 return;
489 } /* driver byte != 0 */
490 if (result) {
491 printk("SCSI disk error : host %d channel %d id %d lun %d return code = %x\n",
492 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->host->host_no,
493 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->channel,
494 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->id,
495 rscsi_disks[DEVICE_NR(SCpnt->request.rq_dev)].device->lun, result);
497 if (driver_byte(result) & DRIVER_SENSE)
498 print_sense("sd", SCpnt);
499 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
500 requeue_sd_request(SCpnt);
501 return;
505 * requeue_sd_request() is the request handler function for the sd driver.
506 * Its function in life is to take block device requests, and translate
507 * them to SCSI commands.
510 static void do_sd_request(void)
512 Scsi_Cmnd *SCpnt = NULL;
513 Scsi_Device *SDev;
514 struct request *req = NULL;
515 int flag = 0;
517 while (1 == 1) {
518 if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
519 return;
521 INIT_SCSI_REQUEST;
522 SDev = rscsi_disks[CURRENT_DEV].device;
525 * If the host for this device is in error recovery mode, don't
526 * do anything at all here. When the host leaves error recovery
527 * mode, it will automatically restart things and start queueing
528 * commands again.
530 if (SDev->host->in_recovery) {
531 return;
534 * I am not sure where the best place to do this is. We need
535 * to hook in a place where we are likely to come if in user
536 * space.
538 if (SDev->was_reset) {
540 * We need to relock the door, but we might
541 * be in an interrupt handler. Only do this
542 * from user space, since we do not want to
543 * sleep from an interrupt. FIXME(eric) - do this
544 * from the kernel error handling thred.
546 if (SDev->removable && !in_interrupt()) {
547 spin_unlock_irq(&io_request_lock); /* FIXME!!!! */
548 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
549 /* scsi_ioctl may allow CURRENT to change, so start over. */
550 SDev->was_reset = 0;
551 spin_lock_irq(&io_request_lock); /* FIXME!!!! */
552 continue;
554 SDev->was_reset = 0;
556 /* We have to be careful here. scsi_allocate_device will get a free pointer,
557 * but there is no guarantee that it is queueable. In normal usage,
558 * we want to call this, because other types of devices may have the
559 * host all tied up, and we want to make sure that we have at least
560 * one request pending for this type of device. We can also come
561 * through here while servicing an interrupt, because of the need to
562 * start another command. If we call scsi_allocate_device more than once,
563 * then the system can wedge if the command is not queueable. The
564 * scsi_request_queueable function is safe because it checks to make sure
565 * that the host is able to take another command before it returns
566 * a pointer.
569 if (flag++ == 0)
570 SCpnt = scsi_allocate_device(&CURRENT,
571 rscsi_disks[CURRENT_DEV].device, 0);
572 else
573 SCpnt = NULL;
576 * The following restore_flags leads to latency problems. FIXME.
577 * Using a "sti()" gets rid of the latency problems but causes
578 * race conditions and crashes.
581 /* This is a performance enhancement. We dig down into the request
582 * list and try to find a queueable request (i.e. device not busy,
583 * and host able to accept another command. If we find one, then we
584 * queue it. This can make a big difference on systems with more than
585 * one disk drive. We want to have the interrupts off when monkeying
586 * with the request list, because otherwise the kernel might try to
587 * slip in a request in between somewhere.
589 * FIXME(eric) - this doesn't belong at this level. The device code in
590 * ll_rw_blk.c should know how to dig down into the device queue to
591 * figure out what it can deal with, and what it can't. Consider
592 * possibility of pulling entire queue down into scsi layer.
594 if (!SCpnt && sd_template.nr_dev > 1) {
595 struct request *req1;
596 req1 = NULL;
597 req = CURRENT;
598 while (req) {
599 SCpnt = scsi_request_queueable(req,
600 rscsi_disks[DEVICE_NR(req->rq_dev)].device);
601 if (SCpnt)
602 break;
603 req1 = req;
604 req = req->next;
606 if (SCpnt && req->rq_status == RQ_INACTIVE) {
607 if (req == CURRENT)
608 CURRENT = CURRENT->next;
609 else
610 req1->next = req->next;
613 if (!SCpnt)
614 return; /* Could not find anything to do */
616 /* Queue command */
617 requeue_sd_request(SCpnt);
618 } /* While */
621 static void requeue_sd_request(Scsi_Cmnd * SCpnt)
623 int dev, devm, block, this_count;
624 unsigned char cmd[10];
625 char nbuff[6];
626 int bounce_size, contiguous;
627 int max_sg;
628 struct buffer_head *bh, *bhp;
629 char *buff, *bounce_buffer;
631 repeat:
633 if (!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
634 do_sd_request();
635 return;
637 devm = SD_PARTITION(SCpnt->request.rq_dev);
638 dev = DEVICE_NR(SCpnt->request.rq_dev);
640 block = SCpnt->request.sector;
641 this_count = 0;
643 SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = %d, block = %d\n", devm, block));
645 if (devm >= (sd_template.dev_max << 4) ||
646 !rscsi_disks[dev].device ||
647 !rscsi_disks[dev].device->online ||
648 block + SCpnt->request.nr_sectors > sd[devm].nr_sects) {
649 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
650 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
651 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
652 goto repeat;
654 block += sd[devm].start_sect;
656 if (rscsi_disks[dev].device->changed) {
658 * quietly refuse to do anything to a changed disc until the changed
659 * bit has been reset
661 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
662 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
663 goto repeat;
665 sd_devname(devm >> 4, nbuff);
666 SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
667 nbuff, dev, block));
670 * If we have a 1K hardware sectorsize, prevent access to single
671 * 512 byte sectors. In theory we could handle this - in fact
672 * the scsi cdrom driver must be able to handle this because
673 * we typically use 1K blocksizes, and cdroms typically have
674 * 2K hardware sectorsizes. Of course, things are simpler
675 * with the cdrom, since it is read-only. For performance
676 * reasons, the filesystems should be able to handle this
677 * and not force the scsi disk driver to use bounce buffers
678 * for this.
680 if (rscsi_disks[dev].sector_size == 1024)
681 if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
682 printk("sd.c:Bad block number/count requested");
683 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
684 goto repeat;
686 if (rscsi_disks[dev].sector_size == 2048)
687 if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
688 printk("sd.c:Bad block number/count requested");
689 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
690 goto repeat;
692 if (rscsi_disks[dev].sector_size == 4096)
693 if ((block & 7) || (SCpnt->request.nr_sectors & 7)) {
694 printk("sd.cBad block number/count requested");
695 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
696 goto repeat;
698 switch (SCpnt->request.cmd) {
699 case WRITE:
700 if (!rscsi_disks[dev].device->writeable) {
701 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
702 goto repeat;
704 cmd[0] = WRITE_6;
705 break;
706 case READ:
707 cmd[0] = READ_6;
708 break;
709 default:
710 panic("Unknown sd command %d\n", SCpnt->request.cmd);
713 SCpnt->this_count = 0;
715 /* If the host adapter can deal with very large scatter-gather
716 * requests, it is a waste of time to cluster
718 contiguous = (!CLUSTERABLE_DEVICE(SCpnt) ? 0 : 1);
719 bounce_buffer = NULL;
720 bounce_size = (SCpnt->request.nr_sectors << 9);
722 /* First see if we need a bounce buffer for this request. If we do, make
723 * sure that we can allocate a buffer. Do not waste space by allocating
724 * a bounce buffer if we are straddling the 16Mb line
726 if (contiguous && SCpnt->request.bh &&
727 virt_to_phys(SCpnt->request.bh->b_data)
728 + (SCpnt->request.nr_sectors << 9) - 1 > ISA_DMA_THRESHOLD
729 && SCpnt->host->unchecked_isa_dma) {
730 if (virt_to_phys(SCpnt->request.bh->b_data) > ISA_DMA_THRESHOLD)
731 bounce_buffer = (char *) scsi_malloc(bounce_size);
732 if (!bounce_buffer)
733 contiguous = 0;
735 if (contiguous && SCpnt->request.bh && SCpnt->request.bh->b_reqnext)
736 for (bh = SCpnt->request.bh, bhp = bh->b_reqnext; bhp; bh = bhp,
737 bhp = bhp->b_reqnext) {
738 if (!CONTIGUOUS_BUFFERS(bh, bhp)) {
739 if (bounce_buffer)
740 scsi_free(bounce_buffer, bounce_size);
741 contiguous = 0;
742 break;
745 if (!SCpnt->request.bh || contiguous) {
747 /* case of page request (i.e. raw device), or unlinked buffer */
748 this_count = SCpnt->request.nr_sectors;
749 buff = SCpnt->request.buffer;
750 SCpnt->use_sg = 0;
752 } else if (SCpnt->host->sg_tablesize == 0 ||
753 (scsi_need_isa_buffer && scsi_dma_free_sectors <= 10)) {
755 /* Case of host adapter that cannot scatter-gather. We also
756 * come here if we are running low on DMA buffer memory. We set
757 * a threshold higher than that we would need for this request so
758 * we leave room for other requests. Even though we would not need
759 * it all, we need to be conservative, because if we run low enough
760 * we have no choice but to panic.
762 if (SCpnt->host->sg_tablesize != 0 &&
763 scsi_need_isa_buffer &&
764 scsi_dma_free_sectors <= 10)
765 printk("Warning: SCSI DMA buffer space running low. Using non scatter-gather I/O.\n");
767 this_count = SCpnt->request.current_nr_sectors;
768 buff = SCpnt->request.buffer;
769 SCpnt->use_sg = 0;
771 } else {
773 /* Scatter-gather capable host adapter */
774 struct scatterlist *sgpnt;
775 int count, this_count_max;
776 int counted;
778 bh = SCpnt->request.bh;
779 this_count = 0;
780 this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
781 count = 0;
782 bhp = NULL;
783 while (bh) {
784 if ((this_count + (bh->b_size >> 9)) > this_count_max)
785 break;
786 if (!bhp || !CONTIGUOUS_BUFFERS(bhp, bh) ||
787 !CLUSTERABLE_DEVICE(SCpnt) ||
788 (SCpnt->host->unchecked_isa_dma &&
789 virt_to_phys(bh->b_data - 1) == ISA_DMA_THRESHOLD)) {
790 if (count < SCpnt->host->sg_tablesize)
791 count++;
792 else
793 break;
795 this_count += (bh->b_size >> 9);
796 bhp = bh;
797 bh = bh->b_reqnext;
799 #if 0
800 if (SCpnt->host->unchecked_isa_dma &&
801 virt_to_phys(SCpnt->request.bh->b_data - 1) == ISA_DMA_THRESHOLD)
802 count--;
803 #endif
804 SCpnt->use_sg = count; /* Number of chains */
805 /* scsi_malloc can only allocate in chunks of 512 bytes */
806 count = (SCpnt->use_sg * sizeof(struct scatterlist) + 511) & ~511;
808 SCpnt->sglist_len = count;
809 max_sg = count / sizeof(struct scatterlist);
810 if (SCpnt->host->sg_tablesize < max_sg)
811 max_sg = SCpnt->host->sg_tablesize;
812 sgpnt = (struct scatterlist *) scsi_malloc(count);
813 if (!sgpnt) {
814 printk("Warning - running *really* short on DMA buffers\n");
815 SCpnt->use_sg = 0; /* No memory left - bail out */
816 this_count = SCpnt->request.current_nr_sectors;
817 buff = SCpnt->request.buffer;
818 } else {
819 memset(sgpnt, 0, count); /* Zero so it is easy to fill, but only
820 * if memory is available
822 buff = (char *) sgpnt;
823 counted = 0;
824 for (count = 0, bh = SCpnt->request.bh, bhp = bh->b_reqnext;
825 count < SCpnt->use_sg && bh;
826 count++, bh = bhp) {
828 bhp = bh->b_reqnext;
830 if (!sgpnt[count].address)
831 sgpnt[count].address = bh->b_data;
832 sgpnt[count].length += bh->b_size;
833 counted += bh->b_size >> 9;
835 if (virt_to_phys(sgpnt[count].address) + sgpnt[count].length - 1 >
836 ISA_DMA_THRESHOLD && (SCpnt->host->unchecked_isa_dma) &&
837 !sgpnt[count].alt_address) {
838 sgpnt[count].alt_address = sgpnt[count].address;
839 /* We try to avoid exhausting the DMA pool, since it is
840 * easier to control usage here. In other places we might
841 * have a more pressing need, and we would be screwed if
842 * we ran out */
843 if (scsi_dma_free_sectors < (sgpnt[count].length >> 9) + 10) {
844 sgpnt[count].address = NULL;
845 } else {
846 sgpnt[count].address =
847 (char *) scsi_malloc(sgpnt[count].length);
849 /* If we start running low on DMA buffers, we abort the
850 * scatter-gather operation, and free all of the memory
851 * we have allocated. We want to ensure that all scsi
852 * operations are able to do at least a non-scatter/gather
853 * operation */
854 if (sgpnt[count].address == NULL) { /* Out of dma memory */
855 #if 0
856 printk("Warning: Running low on SCSI DMA buffers");
857 /* Try switching back to a non s-g operation. */
858 while (--count >= 0) {
859 if (sgpnt[count].alt_address)
860 scsi_free(sgpnt[count].address,
861 sgpnt[count].length);
863 this_count = SCpnt->request.current_nr_sectors;
864 buff = SCpnt->request.buffer;
865 SCpnt->use_sg = 0;
866 scsi_free(sgpnt, SCpnt->sglist_len);
867 #endif
868 SCpnt->use_sg = count;
869 this_count = counted -= bh->b_size >> 9;
870 break;
873 /* Only cluster buffers if we know that we can supply DMA
874 * buffers large enough to satisfy the request. Do not cluster
875 * a new request if this would mean that we suddenly need to
876 * start using DMA bounce buffers */
877 if (bhp && CONTIGUOUS_BUFFERS(bh, bhp)
878 && CLUSTERABLE_DEVICE(SCpnt)) {
879 char *tmp;
881 if (virt_to_phys(sgpnt[count].address) + sgpnt[count].length +
882 bhp->b_size - 1 > ISA_DMA_THRESHOLD &&
883 (SCpnt->host->unchecked_isa_dma) &&
884 !sgpnt[count].alt_address)
885 continue;
887 if (!sgpnt[count].alt_address) {
888 count--;
889 continue;
891 if (scsi_dma_free_sectors > 10)
892 tmp = (char *) scsi_malloc(sgpnt[count].length
893 + bhp->b_size);
894 else {
895 tmp = NULL;
896 max_sg = SCpnt->use_sg;
898 if (tmp) {
899 scsi_free(sgpnt[count].address, sgpnt[count].length);
900 sgpnt[count].address = tmp;
901 count--;
902 continue;
904 /* If we are allowed another sg chain, then increment
905 * counter so we can insert it. Otherwise we will end
906 up truncating */
908 if (SCpnt->use_sg < max_sg)
909 SCpnt->use_sg++;
910 } /* contiguous buffers */
911 } /* for loop */
913 /* This is actually how many we are going to transfer */
914 this_count = counted;
916 if (count < SCpnt->use_sg || SCpnt->use_sg
917 > SCpnt->host->sg_tablesize) {
918 bh = SCpnt->request.bh;
919 printk("Use sg, count %d %x %d\n",
920 SCpnt->use_sg, count, scsi_dma_free_sectors);
921 printk("maxsg = %x, counted = %d this_count = %d\n",
922 max_sg, counted, this_count);
923 while (bh) {
924 printk("[%p %x] ", bh->b_data, bh->b_size);
925 bh = bh->b_reqnext;
927 if (SCpnt->use_sg < 16)
928 for (count = 0; count < SCpnt->use_sg; count++)
929 printk("{%d:%p %p %d} ", count,
930 sgpnt[count].address,
931 sgpnt[count].alt_address,
932 sgpnt[count].length);
933 panic("Ooops");
935 if (SCpnt->request.cmd == WRITE)
936 for (count = 0; count < SCpnt->use_sg; count++)
937 if (sgpnt[count].alt_address)
938 memcpy(sgpnt[count].address, sgpnt[count].alt_address,
939 sgpnt[count].length);
940 } /* Able to malloc sgpnt */
941 } /* Host adapter capable of scatter-gather */
943 /* Now handle the possibility of DMA to addresses > 16Mb */
945 if (SCpnt->use_sg == 0) {
946 if (virt_to_phys(buff) + (this_count << 9) - 1 > ISA_DMA_THRESHOLD &&
947 (SCpnt->host->unchecked_isa_dma)) {
948 if (bounce_buffer)
949 buff = bounce_buffer;
950 else
951 buff = (char *) scsi_malloc(this_count << 9);
952 if (buff == NULL) { /* Try backing off a bit if we are low on mem */
953 this_count = SCpnt->request.current_nr_sectors;
954 buff = (char *) scsi_malloc(this_count << 9);
955 if (!buff)
956 panic("Ran out of DMA buffers.");
958 if (SCpnt->request.cmd == WRITE)
959 memcpy(buff, (char *) SCpnt->request.buffer, this_count << 9);
962 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
963 nbuff,
964 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
965 this_count, SCpnt->request.nr_sectors));
967 cmd[1] = (SCpnt->lun << 5) & 0xe0;
969 if (rscsi_disks[dev].sector_size == 4096) {
970 if (block & 7)
971 panic("sd.c:Bad block number requested");
972 if (this_count & 7)
973 panic("sd.c:Bad block number requested");
974 block = block >> 3;
975 this_count = block >> 3;
977 if (rscsi_disks[dev].sector_size == 2048) {
978 if (block & 3)
979 panic("sd.c:Bad block number requested");
980 if (this_count & 3)
981 panic("sd.c:Bad block number requested");
982 block = block >> 2;
983 this_count = this_count >> 2;
985 if (rscsi_disks[dev].sector_size == 1024) {
986 if (block & 1)
987 panic("sd.c:Bad block number requested");
988 if (this_count & 1)
989 panic("sd.c:Bad block number requested");
990 block = block >> 1;
991 this_count = this_count >> 1;
993 if (rscsi_disks[dev].sector_size == 256) {
994 block = block << 1;
995 this_count = this_count << 1;
997 if (((this_count > 0xff) || (block > 0x1fffff)) && rscsi_disks[dev].ten) {
998 if (this_count > 0xffff)
999 this_count = 0xffff;
1001 cmd[0] += READ_10 - READ_6;
1002 cmd[2] = (unsigned char) (block >> 24) & 0xff;
1003 cmd[3] = (unsigned char) (block >> 16) & 0xff;
1004 cmd[4] = (unsigned char) (block >> 8) & 0xff;
1005 cmd[5] = (unsigned char) block & 0xff;
1006 cmd[6] = cmd[9] = 0;
1007 cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
1008 cmd[8] = (unsigned char) this_count & 0xff;
1009 } else {
1010 if (this_count > 0xff)
1011 this_count = 0xff;
1013 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
1014 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
1015 cmd[3] = (unsigned char) block & 0xff;
1016 cmd[4] = (unsigned char) this_count;
1017 cmd[5] = 0;
1021 * We shouldn't disconnect in the middle of a sector, so with a dumb
1022 * host adapter, it's safe to assume that we can at least transfer
1023 * this many bytes between each connect / disconnect.
1026 SCpnt->transfersize = rscsi_disks[dev].sector_size;
1027 SCpnt->underflow = this_count << 9;
1028 SCpnt->cmd_len = 0;
1029 scsi_do_cmd(SCpnt, (void *) cmd, buff,
1030 this_count * rscsi_disks[dev].sector_size,
1031 rw_intr,
1032 (SCpnt->device->type == TYPE_DISK ?
1033 SD_TIMEOUT : SD_MOD_TIMEOUT),
1034 MAX_RETRIES);
1037 static int check_scsidisk_media_change(kdev_t full_dev)
1039 int retval;
1040 int target;
1041 struct inode inode;
1042 int flag = 0;
1044 target = DEVICE_NR(full_dev);
1046 if (target >= sd_template.dev_max ||
1047 !rscsi_disks[target].device) {
1048 printk("SCSI disk request error: invalid device.\n");
1049 return 0;
1051 if (!rscsi_disks[target].device->removable)
1052 return 0;
1055 * If the device is offline, don't send any commands - just pretend as if
1056 * the command failed. If the device ever comes back online, we can deal with
1057 * it then. It is only because of unrecoverable errors that we would ever
1058 * take a device offline in the first place.
1060 if (rscsi_disks[target].device->online == FALSE) {
1061 rscsi_disks[target].ready = 0;
1062 rscsi_disks[target].device->changed = 1;
1063 return 1; /* This will force a flush, if called from
1064 * check_disk_change */
1066 inode.i_rdev = full_dev; /* This is all we really need here */
1068 /* Using Start/Stop enables differentiation between drive with
1069 * no cartridge loaded - NOT READY, drive with changed cartridge -
1070 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1071 * This also handles drives that auto spin down. eg iomega jaz 1GB
1072 * as this will spin up the drive.
1074 retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_START_UNIT, 0);
1076 if (retval) { /* Unable to test, unit probably not ready. This usually
1077 * means there is no disc in the drive. Mark as changed,
1078 * and we will figure it out later once the drive is
1079 * available again. */
1081 rscsi_disks[target].ready = 0;
1082 rscsi_disks[target].device->changed = 1;
1083 return 1; /* This will force a flush, if called from
1084 * check_disk_change */
1087 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
1088 * presence of disk in the drive. This is kept in the Scsi_Disk
1089 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
1092 rscsi_disks[target].ready = 1; /* FLOPTICAL */
1094 retval = rscsi_disks[target].device->changed;
1095 if (!flag)
1096 rscsi_disks[target].device->changed = 0;
1097 return retval;
1100 static void sd_init_done(Scsi_Cmnd * SCpnt)
1102 struct request *req;
1104 req = &SCpnt->request;
1105 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
1107 if (req->sem != NULL) {
1108 up(req->sem);
1111 static int sd_init_onedisk(int i)
1113 unsigned char cmd[10];
1114 char nbuff[6];
1115 unsigned char *buffer;
1116 unsigned long spintime;
1117 int the_result, retries;
1118 Scsi_Cmnd *SCpnt;
1121 * Get the name of the disk, in case we need to log it somewhere.
1123 sd_devname(i, nbuff);
1126 * If the device is offline, don't try and read capacity or any of the other
1127 * nicities.
1129 if (rscsi_disks[i].device->online == FALSE) {
1130 return i;
1132 spin_lock_irq(&io_request_lock);
1134 /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
1135 * considered a fatal error, and many devices report such an error
1136 * just after a scsi bus reset.
1139 SCpnt = scsi_allocate_device(NULL, rscsi_disks[i].device, 1);
1140 buffer = (unsigned char *) scsi_malloc(512);
1142 spintime = 0;
1144 /* Spin up drives, as required. Only do this at boot time */
1145 /* Spinup needs to be done for module loads too. */
1146 do {
1147 retries = 0;
1148 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 DECLARE_MUTEX_LOCKED(sem);
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 DECLARE_MUTEX_LOCKED(sem);
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;
1211 time1 = jiffies + HZ;
1212 spin_unlock_irq(&io_request_lock);
1213 while (jiffies < time1); /* Wait 1 second for next try */
1214 printk(".");
1215 spin_lock_irq(&io_request_lock);
1217 } while (the_result && spintime && spintime + 100 * HZ > jiffies);
1218 if (spintime) {
1219 if (the_result)
1220 printk("not responding...\n");
1221 else
1222 printk("ready\n");
1224 retries = 3;
1225 do {
1226 cmd[0] = READ_CAPACITY;
1227 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1228 memset((void *) &cmd[2], 0, 8);
1229 memset((void *) buffer, 0, 8);
1230 SCpnt->cmd_len = 0;
1231 SCpnt->sense_buffer[0] = 0;
1232 SCpnt->sense_buffer[2] = 0;
1235 DECLARE_MUTEX_LOCKED(sem);
1236 /* Mark as really busy again */
1237 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1238 SCpnt->request.sem = &sem;
1239 scsi_do_cmd(SCpnt,
1240 (void *) cmd, (void *) buffer,
1241 8, sd_init_done, SD_TIMEOUT,
1242 MAX_RETRIES);
1243 spin_unlock_irq(&io_request_lock);
1244 down(&sem); /* sleep until it is ready */
1245 spin_lock_irq(&io_request_lock);
1246 SCpnt->request.sem = NULL;
1249 the_result = SCpnt->result;
1250 retries--;
1252 } while (the_result && retries);
1255 * The SCSI standard says:
1256 * "READ CAPACITY is necessary for self configuring software"
1257 * While not mandatory, support of READ CAPACITY is strongly encouraged.
1258 * We used to die if we couldn't successfully do a READ CAPACITY.
1259 * But, now we go on about our way. The side effects of this are
1261 * 1. We can't know block size with certainty. I have said "512 bytes
1262 * is it" as this is most common.
1264 * 2. Recovery from when some one attempts to read past the end of the
1265 * raw device will be slower.
1268 if (the_result) {
1269 printk("%s : READ CAPACITY failed.\n"
1270 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
1271 nbuff, nbuff,
1272 status_byte(the_result),
1273 msg_byte(the_result),
1274 host_byte(the_result),
1275 driver_byte(the_result)
1277 if (driver_byte(the_result) & DRIVER_SENSE)
1278 printk("%s : extended sense code = %1x \n",
1279 nbuff, SCpnt->sense_buffer[2] & 0xf);
1280 else
1281 printk("%s : sense not available. \n", nbuff);
1283 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
1284 nbuff);
1285 rscsi_disks[i].capacity = 0x1fffff;
1286 rscsi_disks[i].sector_size = 512;
1288 /* Set dirty bit for removable devices if not ready - sometimes drives
1289 * will not report this properly. */
1290 if (rscsi_disks[i].device->removable &&
1291 SCpnt->sense_buffer[2] == NOT_READY)
1292 rscsi_disks[i].device->changed = 1;
1294 } else {
1296 * FLOPTICAL , if read_capa is ok , drive is assumed to be ready
1298 rscsi_disks[i].ready = 1;
1300 rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
1301 (buffer[1] << 16) |
1302 (buffer[2] << 8) |
1303 buffer[3]);
1305 rscsi_disks[i].sector_size = (buffer[4] << 24) |
1306 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1308 if (rscsi_disks[i].sector_size == 0) {
1309 rscsi_disks[i].sector_size = 512;
1310 printk("%s : sector size 0 reported, assuming 512.\n", nbuff);
1312 if (rscsi_disks[i].sector_size != 512 &&
1313 rscsi_disks[i].sector_size != 1024 &&
1314 rscsi_disks[i].sector_size != 2048 &&
1315 rscsi_disks[i].sector_size != 4096 &&
1316 rscsi_disks[i].sector_size != 256) {
1317 printk("%s : unsupported sector size %d.\n",
1318 nbuff, rscsi_disks[i].sector_size);
1319 if (rscsi_disks[i].device->removable) {
1320 rscsi_disks[i].capacity = 0;
1321 } else {
1322 printk("scsi : deleting disk entry.\n");
1323 rscsi_disks[i].device = NULL;
1324 sd_template.nr_dev--;
1325 SD_GENDISK(i).nr_real--;
1327 /* Wake up a process waiting for device */
1328 wake_up(&SCpnt->device->device_wait);
1329 scsi_release_command(SCpnt);
1330 SCpnt = NULL;
1332 return i;
1335 if (rscsi_disks[i].sector_size == 2048) {
1336 int m;
1339 * We must fix the sd_blocksizes and sd_hardsizes
1340 * to allow us to read the partition tables.
1341 * The disk reading code does not allow for reading
1342 * of partial sectors.
1344 for (m = i << 4; m < ((i + 1) << 4); m++) {
1345 sd_blocksizes[m] = 2048;
1349 * The msdos fs needs to know the hardware sector size
1350 * So I have created this table. See ll_rw_blk.c
1351 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1353 int m, mb;
1354 int sz_quot, sz_rem;
1355 int hard_sector = rscsi_disks[i].sector_size;
1356 /* There are 16 minors allocated for each major device */
1357 for (m = i << 4; m < ((i + 1) << 4); m++) {
1358 sd_hardsizes[m] = hard_sector;
1360 mb = rscsi_disks[i].capacity / 1024 * hard_sector / 1024;
1361 /* sz = div(m/100, 10); this seems to not be in the libr */
1362 m = (mb + 50) / 100;
1363 sz_quot = m / 10;
1364 sz_rem = m - (10 * sz_quot);
1365 printk("SCSI device %s: hdwr sector= %d bytes."
1366 " Sectors= %d [%d MB] [%d.%1d GB]\n",
1367 nbuff, hard_sector, rscsi_disks[i].capacity,
1368 mb, sz_quot, sz_rem);
1370 if (rscsi_disks[i].sector_size == 4096)
1371 rscsi_disks[i].capacity <<= 3;
1372 if (rscsi_disks[i].sector_size == 2048)
1373 rscsi_disks[i].capacity <<= 2; /* Change into 512 byte sectors */
1374 if (rscsi_disks[i].sector_size == 1024)
1375 rscsi_disks[i].capacity <<= 1; /* Change into 512 byte sectors */
1376 if (rscsi_disks[i].sector_size == 256)
1377 rscsi_disks[i].capacity >>= 1; /* Change into 512 byte sectors */
1382 * Unless otherwise specified, this is not write protected.
1384 rscsi_disks[i].write_prot = 0;
1385 if (rscsi_disks[i].device->removable && rscsi_disks[i].ready) {
1386 /* FLOPTICAL */
1389 * for removable scsi disk ( FLOPTICAL ) we have to recognise
1390 * the Write Protect Flag. This flag is kept in the Scsi_Disk struct
1391 * and tested at open !
1392 * Daniel Roche ( dan@lectra.fr )
1395 memset((void *) &cmd[0], 0, 8);
1396 cmd[0] = MODE_SENSE;
1397 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1398 cmd[2] = 1; /* page code 1 ?? */
1399 cmd[4] = 12;
1400 SCpnt->cmd_len = 0;
1401 SCpnt->sense_buffer[0] = 0;
1402 SCpnt->sense_buffer[2] = 0;
1404 /* same code as READCAPA !! */
1406 DECLARE_MUTEX_LOCKED(sem);
1407 SCpnt->request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy again */
1408 SCpnt->request.sem = &sem;
1409 scsi_do_cmd(SCpnt,
1410 (void *) cmd, (void *) buffer,
1411 512, sd_init_done, SD_TIMEOUT,
1412 MAX_RETRIES);
1413 spin_unlock_irq(&io_request_lock);
1414 down(&sem);
1415 spin_lock_irq(&io_request_lock);
1416 SCpnt->request.sem = NULL;
1419 the_result = SCpnt->result;
1421 if (the_result) {
1422 printk("%s: test WP failed, assume Write Protected\n", nbuff);
1423 rscsi_disks[i].write_prot = 1;
1424 } else {
1425 rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
1426 printk("%s: Write Protect is %s\n", nbuff,
1427 rscsi_disks[i].write_prot ? "on" : "off");
1430 } /* check for write protect */
1431 /* Wake up a process waiting for device */
1432 wake_up(&SCpnt->device->device_wait);
1433 scsi_release_command(SCpnt);
1434 SCpnt = NULL;
1436 rscsi_disks[i].ten = 1;
1437 rscsi_disks[i].remap = 1;
1438 scsi_free(buffer, 512);
1439 spin_unlock_irq(&io_request_lock);
1440 return i;
1444 * The sd_init() function looks at all SCSI drives present, determines
1445 * their size, and reads partition table entries for them.
1448 static int sd_registered = 0;
1450 static int sd_init()
1452 int i;
1454 if (sd_template.dev_noticed == 0)
1455 return 0;
1457 if (!rscsi_disks)
1458 sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1460 if (sd_template.dev_max > N_SD_MAJORS * SCSI_DISKS_PER_MAJOR)
1461 sd_template.dev_max = N_SD_MAJORS * SCSI_DISKS_PER_MAJOR;
1463 if (!sd_registered) {
1464 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
1465 if (register_blkdev(SD_MAJOR(i), "sd", &sd_fops)) {
1466 printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i));
1467 return 1;
1470 sd_registered++;
1472 /* We do not support attaching loadable devices yet. */
1473 if (rscsi_disks)
1474 return 0;
1476 rscsi_disks = (Scsi_Disk *)
1477 scsi_init_malloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1478 memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1480 /* for every (necessary) major: */
1481 sd_sizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1482 memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1484 sd_blocksizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1485 sd_hardsizes = (int *) scsi_init_malloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1487 for (i = 0; i < sd_template.dev_max << 4; i++) {
1488 sd_blocksizes[i] = 1024;
1489 sd_hardsizes[i] = 512;
1492 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1493 blksize_size[SD_MAJOR(i)] = sd_blocksizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1494 hardsect_size[SD_MAJOR(i)] = sd_hardsizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1496 sd = (struct hd_struct *) scsi_init_malloc((sd_template.dev_max << 4) *
1497 sizeof(struct hd_struct),
1498 GFP_ATOMIC);
1500 if (N_USED_SD_MAJORS > 1)
1501 sd_gendisks = (struct gendisk *)
1502 kmalloc(N_USED_SD_MAJORS * sizeof(struct gendisk), GFP_ATOMIC);
1503 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1504 sd_gendisks[i].major = SD_MAJOR(i);
1505 sd_gendisks[i].major_name = "sd";
1506 sd_gendisks[i].minor_shift = 4;
1507 sd_gendisks[i].max_p = 1 << 4;
1508 sd_gendisks[i].max_nr = SCSI_DISKS_PER_MAJOR;
1509 sd_gendisks[i].init = sd_geninit;
1510 sd_gendisks[i].part = sd + (i * SCSI_DISKS_PER_MAJOR << 4);
1511 sd_gendisks[i].sizes = sd_sizes + (i * SCSI_DISKS_PER_MAJOR << 4);
1512 sd_gendisks[i].nr_real = 0;
1513 sd_gendisks[i].next = sd_gendisks + i + 1;
1514 sd_gendisks[i].real_devices =
1515 (void *) (rscsi_disks + i * SCSI_DISKS_PER_MAJOR);
1518 LAST_SD_GENDISK.max_nr =
1519 (sd_template.dev_max - 1) % SCSI_DISKS_PER_MAJOR + 1;
1520 LAST_SD_GENDISK.next = NULL;
1521 return 0;
1525 * sd_get_queue() returns the queue which corresponds to a given device.
1527 static struct request **sd_get_queue(kdev_t dev)
1529 return &blk_dev[MAJOR_NR].current_request;
1531 static void sd_finish()
1533 struct gendisk *gendisk;
1534 int i;
1536 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
1537 /* FIXME: After 2.2 we should implement multiple sd queues */
1538 blk_dev[SD_MAJOR(i)].request_fn = DEVICE_REQUEST;
1539 if (i)
1540 blk_dev[SD_MAJOR(i)].queue = sd_get_queue;
1542 for (gendisk = gendisk_head; gendisk != NULL; gendisk = gendisk->next)
1543 if (gendisk == sd_gendisks)
1544 break;
1545 if (gendisk == NULL) {
1546 LAST_SD_GENDISK.next = gendisk_head;
1547 gendisk_head = sd_gendisks;
1549 for (i = 0; i < sd_template.dev_max; ++i)
1550 if (!rscsi_disks[i].capacity &&
1551 rscsi_disks[i].device) {
1552 if (MODULE_FLAG
1553 && !rscsi_disks[i].has_part_table) {
1554 sd_sizes[i << 4] = rscsi_disks[i].capacity;
1555 /* revalidate does sd_init_onedisk via MAYBE_REINIT */
1556 revalidate_scsidisk(MKDEV_SD(i), 0);
1557 } else
1558 i = sd_init_onedisk(i);
1559 rscsi_disks[i].has_part_table = 1;
1561 /* If our host adapter is capable of scatter-gather, then we increase
1562 * the read-ahead to 60 blocks (120 sectors). If not, we use
1563 * a two block (4 sector) read ahead. We can only respect this with the
1564 * granularity of every 16 disks (one device major).
1566 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1567 read_ahead[SD_MAJOR(i)] =
1568 (rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device
1569 && rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device->host->sg_tablesize)
1570 ? 120 /* 120 sector read-ahead */
1571 : 4; /* 4 sector read-ahead */
1574 return;
1577 static int sd_detect(Scsi_Device * SDp)
1579 char nbuff[6];
1580 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1581 return 0;
1583 sd_devname(sd_template.dev_noticed++, nbuff);
1584 printk("Detected scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1585 SDp->removable ? "removable " : "",
1586 nbuff,
1587 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1589 return 1;
1591 static int sd_attach(Scsi_Device * SDp)
1593 Scsi_Disk *dpnt;
1594 int i;
1596 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1597 return 0;
1599 if (sd_template.nr_dev >= sd_template.dev_max) {
1600 SDp->attached--;
1601 return 1;
1603 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1604 if (!dpnt->device)
1605 break;
1607 if (i >= sd_template.dev_max)
1608 panic("scsi_devices corrupt (sd)");
1610 SDp->scsi_request_fn = do_sd_request;
1611 rscsi_disks[i].device = SDp;
1612 rscsi_disks[i].has_part_table = 0;
1613 sd_template.nr_dev++;
1614 SD_GENDISK(i).nr_real++;
1615 return 0;
1618 #define DEVICE_BUSY rscsi_disks[target].device->busy
1619 #define USAGE rscsi_disks[target].device->access_count
1620 #define CAPACITY rscsi_disks[target].capacity
1621 #define MAYBE_REINIT sd_init_onedisk(target)
1623 /* This routine is called to flush all partitions and partition tables
1624 * for a changed scsi disk, and then re-read the new partition table.
1625 * If we are revalidating a disk because of a media change, then we
1626 * enter with usage == 0. If we are using an ioctl, we automatically have
1627 * usage == 1 (we need an open channel to use an ioctl :-), so this
1628 * is our limit.
1630 int revalidate_scsidisk(kdev_t dev, int maxusage)
1632 int target;
1633 int max_p;
1634 int start;
1635 int i;
1637 target = DEVICE_NR(dev);
1639 if (DEVICE_BUSY || USAGE > maxusage) {
1640 printk("Device busy for revalidation (usage=%d)\n", USAGE);
1641 return -EBUSY;
1643 DEVICE_BUSY = 1;
1645 max_p = sd_gendisks->max_p;
1646 start = target << sd_gendisks->minor_shift;
1648 for (i = max_p - 1; i >= 0; i--) {
1649 int index = start + i;
1650 kdev_t devi = MKDEV_SD_PARTITION(index);
1651 struct super_block *sb = get_super(devi);
1652 sync_dev(devi);
1653 if (sb)
1654 invalidate_inodes(sb);
1655 invalidate_buffers(devi);
1656 sd_gendisks->part[index].start_sect = 0;
1657 sd_gendisks->part[index].nr_sects = 0;
1659 * Reset the blocksize for everything so that we can read
1660 * the partition table. Technically we will determine the
1661 * correct block size when we revalidate, but we do this just
1662 * to make sure that everything remains consistent.
1664 sd_blocksizes[index] = 1024;
1665 if (rscsi_disks[target].sector_size == 2048)
1666 sd_blocksizes[index] = 2048;
1667 else
1668 sd_blocksizes[index] = 1024;
1671 #ifdef MAYBE_REINIT
1672 MAYBE_REINIT;
1673 #endif
1675 sd_gendisks->part[start].nr_sects = CAPACITY;
1676 resetup_one_dev(&SD_GENDISK(target),
1677 target % SCSI_DISKS_PER_MAJOR);
1679 DEVICE_BUSY = 0;
1680 return 0;
1683 static int fop_revalidate_scsidisk(kdev_t dev)
1685 return revalidate_scsidisk(dev, 0);
1687 static void sd_detach(Scsi_Device * SDp)
1689 Scsi_Disk *dpnt;
1690 int i, j;
1691 int max_p;
1692 int start;
1694 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1695 if (dpnt->device == SDp) {
1697 /* If we are disconnecting a disk driver, sync and invalidate
1698 * everything */
1699 max_p = sd_gendisk.max_p;
1700 start = i << sd_gendisk.minor_shift;
1702 for (j = max_p - 1; j >= 0; j--) {
1703 int index = start + j;
1704 kdev_t devi = MKDEV_SD_PARTITION(index);
1705 struct super_block *sb = get_super(devi);
1706 sync_dev(devi);
1707 if (sb)
1708 invalidate_inodes(sb);
1709 invalidate_buffers(devi);
1710 sd_gendisks->part[index].start_sect = 0;
1711 sd_gendisks->part[index].nr_sects = 0;
1712 sd_sizes[index] = 0;
1714 dpnt->has_part_table = 0;
1715 dpnt->device = NULL;
1716 dpnt->capacity = 0;
1717 SDp->attached--;
1718 sd_template.dev_noticed--;
1719 sd_template.nr_dev--;
1720 SD_GENDISK(i).nr_real--;
1721 return;
1723 return;
1726 #ifdef MODULE
1728 int init_module(void)
1730 sd_template.module = &__this_module;
1731 return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1733 void cleanup_module(void)
1735 struct gendisk **prev_sdgd_link;
1736 struct gendisk *sdgd;
1737 int i;
1738 int removed = 0;
1740 scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1742 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++)
1743 unregister_blkdev(SD_MAJOR(i), "sd");
1745 sd_registered--;
1746 if (rscsi_disks != NULL) {
1747 scsi_init_free((char *) rscsi_disks,
1748 sd_template.dev_max * sizeof(Scsi_Disk));
1749 scsi_init_free((char *) sd_sizes, sd_template.dev_max * sizeof(int));
1750 scsi_init_free((char *) sd_blocksizes, sd_template.dev_max * sizeof(int));
1751 scsi_init_free((char *) sd_hardsizes, sd_template.dev_max * sizeof(int));
1752 scsi_init_free((char *) sd,
1753 (sd_template.dev_max << 4) * sizeof(struct hd_struct));
1756 * Now remove sd_gendisks from the linked list
1758 prev_sdgd_link = &gendisk_head;
1759 while ((sdgd = *prev_sdgd_link) != NULL) {
1760 if (sdgd >= sd_gendisks && sdgd <= &LAST_SD_GENDISK) {
1761 removed++;
1762 *prev_sdgd_link = sdgd->next;
1763 continue;
1765 prev_sdgd_link = &sdgd->next;
1768 if (removed != N_USED_SD_MAJORS)
1769 printk("%s %d sd_gendisks in disk chain",
1770 removed > N_USED_SD_MAJORS ? "total" : "just", removed);
1773 for (i = 0; i <= (sd_template.dev_max - 1) / SCSI_DISKS_PER_MAJOR; i++) {
1774 blk_dev[SD_MAJOR(i)].request_fn = NULL;
1775 blk_size[SD_MAJOR(i)] = NULL;
1776 hardsect_size[SD_MAJOR(i)] = NULL;
1777 read_ahead[SD_MAJOR(i)] = 0;
1779 sd_template.dev_max = 0;
1780 if (sd_gendisks != &sd_gendisk)
1781 kfree(sd_gendisks);
1783 #endif /* MODULE */
1786 * Overrides for Emacs so that we almost follow Linus's tabbing style.
1787 * Emacs will notice this stuff at the end of the file and automatically
1788 * adjust the settings for this buffer only. This must remain at the end
1789 * of the file.
1790 * ---------------------------------------------------------------------------
1791 * Local variables:
1792 * c-indent-level: 4
1793 * c-brace-imaginary-offset: 0
1794 * c-brace-offset: -4
1795 * c-argdecl-indent: 4
1796 * c-label-offset: -4
1797 * c-continued-statement-offset: 4
1798 * c-continued-brace-offset: 0
1799 * indent-tabs-mode: nil
1800 * tab-width: 8
1801 * End: