1 /*
2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
8 *
9 * <drew@colorado.edu>
10 *
11 * Modified by Eric Youngdale ericy@andante.org to
12 * add scatter-gather, multiple outstanding request, and other
13 * enhancements.
14 *
15 * Modified by Eric Youngdale eric@andante.org to support loadable
16 * low-level scsi drivers.
17 *
18 * Modified by Jirka Hanika geo@ff.cuni.cz to support more
19 * scsi disks using eight major numbers.
20 *
21 * Modified by Richard Gooch rgooch@atnf.csiro.au to support devfs.
22 *
23 * Modified by Torben Mathiasen tmm@image.dk
24 * Resource allocation fixes in sd_init and cleanups.
25 *
26 * Modified by Alex Davis <letmein@erols.com>
27 * Fix problem where partition info not being read in sd_open.
28 *
29 * Modified by Alex Davis <letmein@erols.com>
30 * Fix problem where removable media could be ejected after sd_open.
31 */
32
33 #include <linux/config.h>
34 #include <linux/module.h>
35
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/string.h>
41 #include <linux/hdreg.h>
42 #include <linux/errno.h>
43 #include <linux/interrupt.h>
44 #include <linux/init.h>
45
46 #include <linux/smp.h>
47
48 #include <asm/uaccess.h>
49 #include <asm/system.h>
50 #include <asm/io.h>
51
52 #define MAJOR_NR SCSI_DISK0_MAJOR
53 #include <linux/blk.h>
54 #include <linux/blkpg.h>
55 #include "scsi.h"
56 #include "hosts.h"
57 #include "sd.h"
58 #include <scsi/scsi_ioctl.h>
59 #include "constants.h"
60 #include <scsi/scsicam.h> /* must follow "hosts.h" */
61
62 #include <linux/genhd.h>
63
64 /*
65 * static const char RCSid[] = "$Header:";
66 */
67
68 /* system major --> sd_gendisks index */
69 #define SD_MAJOR_IDX(i) (MAJOR(i) & SD_MAJOR_MASK)
70 /* sd_gendisks index --> system major */
71 #define SD_MAJOR(i) (!(i) ? SCSI_DISK0_MAJOR : SCSI_DISK1_MAJOR-1+(i))
72
73 #define SD_PARTITION(dev) ((SD_MAJOR_IDX(dev) << 8) | (MINOR(dev) & 255))
74
75 #define SCSI_DISKS_PER_MAJOR 16
76 #define SD_MAJOR_NUMBER(i) SD_MAJOR((i) >> 8)
77 #define SD_MINOR_NUMBER(i) ((i) & 255)
78 #define MKDEV_SD_PARTITION(i) MKDEV(SD_MAJOR_NUMBER(i), (i) & 255)
79 #define MKDEV_SD(index) MKDEV_SD_PARTITION((index) << 4)
80 #define N_USED_SCSI_DISKS (sd_template.dev_max + SCSI_DISKS_PER_MAJOR - 1)
81 #define N_USED_SD_MAJORS (N_USED_SCSI_DISKS / SCSI_DISKS_PER_MAJOR)
82
83 #define MAX_RETRIES 5
84
85 /*
86 * Time out in seconds for disks and Magneto-opticals (which are slower).
87 */
88
89 #define SD_TIMEOUT (30 * HZ)
90 #define SD_MOD_TIMEOUT (75 * HZ)
91
92 static Scsi_Disk *rscsi_disks;
93 static struct gendisk *sd_gendisks;
94 static int *sd_sizes;
95 static int *sd_blocksizes;
96 static int *sd_hardsizes; /* Hardware sector size */
97 static int *sd_max_sectors;
98
99 static int check_scsidisk_media_change(kdev_t);
100 static int fop_revalidate_scsidisk(kdev_t);
101
102 static int sd_init_onedisk(int);
103
104
105 static int sd_init(void);
106 static void sd_finish(void);
107 static int sd_attach(Scsi_Device *);
108 static int sd_detect(Scsi_Device *);
109 static void sd_detach(Scsi_Device *);
110 static int sd_init_command(Scsi_Cmnd *);
111
112 static struct Scsi_Device_Template sd_template = {
113 name:"disk",
114 tag:"sd",
115 scsi_type:TYPE_DISK,
116 major:SCSI_DISK0_MAJOR,
117 /*
118 * Secondary range of majors that this driver handles.
119 */
120 min_major:SCSI_DISK1_MAJOR,
121 max_major:SCSI_DISK7_MAJOR,
122 blk:1,
123 detect:sd_detect,
124 init:sd_init,
125 finish:sd_finish,
126 attach:sd_attach,
127 detach:sd_detach,
128 init_command:sd_init_command,
129 };
130
131
132 static void rw_intr(Scsi_Cmnd * SCpnt);
133
134 #if defined(CONFIG_PPC)
135 /*
136 * Moved from arch/ppc/pmac_setup.c. This is where it really belongs.
137 */
138 kdev_t __init
sd_find_target(void * host,int tgt)139 sd_find_target(void *host, int tgt)
140 {
141 Scsi_Disk *dp;
142 int i;
143 for (dp = rscsi_disks, i = 0; i < sd_template.dev_max; ++i, ++dp)
144 if (dp->device != NULL && dp->device->host == host
145 && dp->device->id == tgt)
146 return MKDEV_SD(i);
147 return 0;
148 }
149 #endif
150
sd_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)151 static int sd_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
152 {
153 kdev_t dev = inode->i_rdev;
154 struct Scsi_Host * host;
155 Scsi_Device * SDev;
156 int diskinfo[4];
157
158 SDev = rscsi_disks[DEVICE_NR(dev)].device;
159 if (!SDev)
160 return -ENODEV;
161
162 /*
163 * If we are in the middle of error recovery, don't let anyone
164 * else try and use this device. Also, if error recovery fails, it
165 * may try and take the device offline, in which case all further
166 * access to the device is prohibited.
167 */
168
169 if( !scsi_block_when_processing_errors(SDev) )
170 {
171 return -ENODEV;
172 }
173
174 switch (cmd)
175 {
176 case HDIO_GETGEO: /* Return BIOS disk parameters */
177 {
178 struct hd_geometry *loc = (struct hd_geometry *) arg;
179 if(!loc)
180 return -EINVAL;
181
182 host = rscsi_disks[DEVICE_NR(dev)].device->host;
183
184 /* default to most commonly used values */
185
186 diskinfo[0] = 0x40;
187 diskinfo[1] = 0x20;
188 diskinfo[2] = rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
189
190 /* override with calculated, extended default, or driver values */
191
192 if(host->hostt->bios_param != NULL)
193 host->hostt->bios_param(&rscsi_disks[DEVICE_NR(dev)],
194 dev,
195 &diskinfo[0]);
196 else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
197 dev, &diskinfo[0]);
198
199 if (put_user(diskinfo[0], &loc->heads) ||
200 put_user(diskinfo[1], &loc->sectors) ||
201 put_user(diskinfo[2], &loc->cylinders) ||
202 put_user(sd_gendisks[SD_MAJOR_IDX(
203 inode->i_rdev)].part[MINOR(
204 inode->i_rdev)].start_sect, &loc->start))
205 return -EFAULT;
206 return 0;
207 }
208 case HDIO_GETGEO_BIG:
209 {
210 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
211
212 if(!loc)
213 return -EINVAL;
214
215 host = rscsi_disks[DEVICE_NR(dev)].device->host;
216
217 /* default to most commonly used values */
218
219 diskinfo[0] = 0x40;
220 diskinfo[1] = 0x20;
221 diskinfo[2] = rscsi_disks[DEVICE_NR(dev)].capacity >> 11;
222
223 /* override with calculated, extended default, or driver values */
224
225 if(host->hostt->bios_param != NULL)
226 host->hostt->bios_param(&rscsi_disks[DEVICE_NR(dev)],
227 dev,
228 &diskinfo[0]);
229 else scsicam_bios_param(&rscsi_disks[DEVICE_NR(dev)],
230 dev, &diskinfo[0]);
231
232 if (put_user(diskinfo[0], &loc->heads) ||
233 put_user(diskinfo[1], &loc->sectors) ||
234 put_user(diskinfo[2], (unsigned int *) &loc->cylinders) ||
235 put_user(sd_gendisks[SD_MAJOR_IDX(
236 inode->i_rdev)].part[MINOR(
237 inode->i_rdev)].start_sect, &loc->start))
238 return -EFAULT;
239 return 0;
240 }
241 case BLKGETSIZE:
242 case BLKGETSIZE64:
243 case BLKROSET:
244 case BLKROGET:
245 case BLKRASET:
246 case BLKRAGET:
247 case BLKFLSBUF:
248 case BLKSSZGET:
249 case BLKPG:
250 case BLKELVGET:
251 case BLKELVSET:
252 case BLKBSZGET:
253 case BLKBSZSET:
254 return blk_ioctl(inode->i_rdev, cmd, arg);
255
256 case BLKRRPART: /* Re-read partition tables */
257 if (!capable(CAP_SYS_ADMIN))
258 return -EACCES;
259 return revalidate_scsidisk(dev, 1);
260
261 default:
262 return scsi_ioctl(rscsi_disks[DEVICE_NR(dev)].device , cmd, (void *) arg);
263 }
264 }
265
sd_devname(unsigned int disknum,char * buffer)266 static void sd_devname(unsigned int disknum, char *buffer)
267 {
268 if (disknum < 26)
269 sprintf(buffer, "sd%c", 'a' + disknum);
270 else {
271 unsigned int min1;
272 unsigned int min2;
273 /*
274 * For larger numbers of disks, we need to go to a new
275 * naming scheme.
276 */
277 min1 = disknum / 26;
278 min2 = disknum % 26;
279 sprintf(buffer, "sd%c%c", 'a' + min1 - 1, 'a' + min2);
280 }
281 }
282
sd_find_queue(kdev_t dev)283 static request_queue_t *sd_find_queue(kdev_t dev)
284 {
285 Scsi_Disk *dpnt;
286 int target;
287 target = DEVICE_NR(dev);
288
289 dpnt = &rscsi_disks[target];
290 if (!dpnt->device)
291 return NULL; /* No such device */
292 return &dpnt->device->request_queue;
293 }
294
sd_init_command(Scsi_Cmnd * SCpnt)295 static int sd_init_command(Scsi_Cmnd * SCpnt)
296 {
297 int dev, block, this_count;
298 struct hd_struct *ppnt;
299 Scsi_Disk *dpnt;
300 #if CONFIG_SCSI_LOGGING
301 char nbuff[6];
302 #endif
303
304 ppnt = &sd_gendisks[SD_MAJOR_IDX(SCpnt->request.rq_dev)].part[MINOR(SCpnt->request.rq_dev)];
305 dev = DEVICE_NR(SCpnt->request.rq_dev);
306
307 block = SCpnt->request.sector;
308 this_count = SCpnt->request_bufflen >> 9;
309
310 SCSI_LOG_HLQUEUE(1, printk("Doing sd request, dev = 0x%x, block = %d\n",
311 SCpnt->request.rq_dev, block));
312
313 dpnt = &rscsi_disks[dev];
314 if (dev >= sd_template.dev_max ||
315 !dpnt->device ||
316 !dpnt->device->online ||
317 block + SCpnt->request.nr_sectors > ppnt->nr_sects) {
318 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
319 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
320 return 0;
321 }
322 block += ppnt->start_sect;
323 if (dpnt->device->changed) {
324 /*
325 * quietly refuse to do anything to a changed disc until the changed
326 * bit has been reset
327 */
328 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
329 return 0;
330 }
331 SCSI_LOG_HLQUEUE(2, sd_devname(dev, nbuff));
332 SCSI_LOG_HLQUEUE(2, printk("%s : real dev = /dev/%d, block = %d\n",
333 nbuff, dev, block));
334
335 /*
336 * If we have a 1K hardware sectorsize, prevent access to single
337 * 512 byte sectors. In theory we could handle this - in fact
338 * the scsi cdrom driver must be able to handle this because
339 * we typically use 1K blocksizes, and cdroms typically have
340 * 2K hardware sectorsizes. Of course, things are simpler
341 * with the cdrom, since it is read-only. For performance
342 * reasons, the filesystems should be able to handle this
343 * and not force the scsi disk driver to use bounce buffers
344 * for this.
345 */
346 if (dpnt->device->sector_size == 1024) {
347 if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
348 printk("sd.c:Bad block number requested");
349 return 0;
350 } else {
351 block = block >> 1;
352 this_count = this_count >> 1;
353 }
354 }
355 if (dpnt->device->sector_size == 2048) {
356 if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
357 printk("sd.c:Bad block number requested");
358 return 0;
359 } else {
360 block = block >> 2;
361 this_count = this_count >> 2;
362 }
363 }
364 if (dpnt->device->sector_size == 4096) {
365 if ((block & 7) || (SCpnt->request.nr_sectors & 7)) {
366 printk("sd.c:Bad block number requested");
367 return 0;
368 } else {
369 block = block >> 3;
370 this_count = this_count >> 3;
371 }
372 }
373 switch (SCpnt->request.cmd) {
374 case WRITE:
375 if (!dpnt->device->writeable) {
376 return 0;
377 }
378 SCpnt->cmnd[0] = WRITE_6;
379 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
380 break;
381 case READ:
382 SCpnt->cmnd[0] = READ_6;
383 SCpnt->sc_data_direction = SCSI_DATA_READ;
384 break;
385 default:
386 panic("Unknown sd command %d\n", SCpnt->request.cmd);
387 }
388
389 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
390 nbuff,
391 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
392 this_count, SCpnt->request.nr_sectors));
393
394 SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
395 ((SCpnt->lun << 5) & 0xe0) : 0;
396
397 if (((this_count > 0xff) || (block > 0x1fffff)) || SCpnt->device->ten) {
398 if (this_count > 0xffff)
399 this_count = 0xffff;
400
401 SCpnt->cmnd[0] += READ_10 - READ_6;
402 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
403 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
404 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
405 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
406 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
407 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
408 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
409 } else {
410 if (this_count > 0xff)
411 this_count = 0xff;
412
413 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
414 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
415 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
416 SCpnt->cmnd[4] = (unsigned char) this_count;
417 SCpnt->cmnd[5] = 0;
418 }
419
420 /*
421 * We shouldn't disconnect in the middle of a sector, so with a dumb
422 * host adapter, it's safe to assume that we can at least transfer
423 * this many bytes between each connect / disconnect.
424 */
425 SCpnt->transfersize = dpnt->device->sector_size;
426 SCpnt->underflow = this_count << 9;
427
428 SCpnt->allowed = MAX_RETRIES;
429 SCpnt->timeout_per_command = (SCpnt->device->type == TYPE_DISK ?
430 SD_TIMEOUT : SD_MOD_TIMEOUT);
431
432 /*
433 * This is the completion routine we use. This is matched in terms
434 * of capability to this function.
435 */
436 SCpnt->done = rw_intr;
437
438 /*
439 * This indicates that the command is ready from our end to be
440 * queued.
441 */
442 return 1;
443 }
444
sd_open(struct inode * inode,struct file * filp)445 static int sd_open(struct inode *inode, struct file *filp)
446 {
447 int target, retval = -ENXIO;
448 Scsi_Device * SDev;
449 target = DEVICE_NR(inode->i_rdev);
450
451 SCSI_LOG_HLQUEUE(1, printk("target=%d, max=%d\n", target, sd_template.dev_max));
452
453 if (target >= sd_template.dev_max || !rscsi_disks[target].device)
454 return -ENXIO; /* No such device */
455
456 /*
457 * If the device is in error recovery, wait until it is done.
458 * If the device is offline, then disallow any access to it.
459 */
460 if (!scsi_block_when_processing_errors(rscsi_disks[target].device)) {
461 return -ENXIO;
462 }
463 /*
464 * Make sure that only one process can do a check_change_disk at one time.
465 * This is also used to lock out further access when the partition table
466 * is being re-read.
467 */
468
469 while (rscsi_disks[target].device->busy) {
470 barrier();
471 cpu_relax();
472 }
473 /*
474 * The following code can sleep.
475 * Module unloading must be prevented
476 */
477 SDev = rscsi_disks[target].device;
478 if (SDev->host->hostt->module)
479 __MOD_INC_USE_COUNT(SDev->host->hostt->module);
480 if (sd_template.module)
481 __MOD_INC_USE_COUNT(sd_template.module);
482 SDev->access_count++;
483
484 if (rscsi_disks[target].device->removable) {
485 SDev->allow_revalidate = 1;
486 check_disk_change(inode->i_rdev);
487 SDev->allow_revalidate = 0;
488
489 /*
490 * If the drive is empty, just let the open fail.
491 */
492 if ((!rscsi_disks[target].ready) && !(filp->f_flags & O_NDELAY)) {
493 retval = -ENOMEDIUM;
494 goto error_out;
495 }
496
497 /*
498 * Similarly, if the device has the write protect tab set,
499 * have the open fail if the user expects to be able to write
500 * to the thing.
501 */
502 if ((rscsi_disks[target].write_prot) && (filp->f_mode & 2)) {
503 retval = -EROFS;
504 goto error_out;
505 }
506 }
507 /*
508 * It is possible that the disk changing stuff resulted in the device
509 * being taken offline. If this is the case, report this to the user,
510 * and don't pretend that
511 * the open actually succeeded.
512 */
513 if (!SDev->online) {
514 goto error_out;
515 }
516 /*
517 * See if we are requesting a non-existent partition. Do this
518 * after checking for disk change.
519 */
520 if (sd_sizes[SD_PARTITION(inode->i_rdev)] == 0) {
521 goto error_out;
522 }
523
524 if (SDev->removable)
525 if (SDev->access_count==1)
526 if (scsi_block_when_processing_errors(SDev))
527 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, NULL);
528
529
530 return 0;
531
532 error_out:
533 SDev->access_count--;
534 if (SDev->host->hostt->module)
535 __MOD_DEC_USE_COUNT(SDev->host->hostt->module);
536 if (sd_template.module)
537 __MOD_DEC_USE_COUNT(sd_template.module);
538 return retval;
539 }
540
sd_release(struct inode * inode,struct file * file)541 static int sd_release(struct inode *inode, struct file *file)
542 {
543 int target;
544 Scsi_Device * SDev;
545
546 target = DEVICE_NR(inode->i_rdev);
547 SDev = rscsi_disks[target].device;
548 if (!SDev)
549 return -ENODEV;
550
551 SDev->access_count--;
552
553 if (SDev->removable) {
554 if (!SDev->access_count)
555 if (scsi_block_when_processing_errors(SDev))
556 scsi_ioctl(SDev, SCSI_IOCTL_DOORUNLOCK, NULL);
557 }
558 if (SDev->host->hostt->module)
559 __MOD_DEC_USE_COUNT(SDev->host->hostt->module);
560 if (sd_template.module)
561 __MOD_DEC_USE_COUNT(sd_template.module);
562 return 0;
563 }
564
565 static struct block_device_operations sd_fops =
566 {
567 owner: THIS_MODULE,
568 open: sd_open,
569 release: sd_release,
570 ioctl: sd_ioctl,
571 check_media_change: check_scsidisk_media_change,
572 revalidate: fop_revalidate_scsidisk
573 };
574
575 /*
576 * If we need more than one SCSI disk major (i.e. more than
577 * 16 SCSI disks), we'll have to kmalloc() more gendisks later.
578 */
579
580 static struct gendisk sd_gendisk =
581 {
582 major: SCSI_DISK0_MAJOR,
583 major_name: "sd",
584 minor_shift: 4,
585 max_p: 1 << 4,
586 fops: &sd_fops,
587 };
588
589 #define SD_GENDISK(i) sd_gendisks[(i) / SCSI_DISKS_PER_MAJOR]
590
591 /*
592 * rw_intr is the interrupt routine for the device driver.
593 * It will be notified on the end of a SCSI read / write, and
594 * will take one of several actions based on success or failure.
595 */
596
rw_intr(Scsi_Cmnd * SCpnt)597 static void rw_intr(Scsi_Cmnd * SCpnt)
598 {
599 int result = SCpnt->result;
600 #if CONFIG_SCSI_LOGGING
601 char nbuff[6];
602 #endif
603 int this_count = SCpnt->bufflen >> 9;
604 int good_sectors = (result == 0 ? this_count : 0);
605 int block_sectors = 1;
606 long error_sector;
607
608 SCSI_LOG_HLCOMPLETE(1, sd_devname(DEVICE_NR(SCpnt->request.rq_dev), nbuff));
609
610 SCSI_LOG_HLCOMPLETE(1, printk("%s : rw_intr(%d, %x [%x %x])\n", nbuff,
611 SCpnt->host->host_no,
612 result,
613 SCpnt->sense_buffer[0],
614 SCpnt->sense_buffer[2]));
615
616 /*
617 Handle MEDIUM ERRORs that indicate partial success. Since this is a
618 relatively rare error condition, no care is taken to avoid
619 unnecessary additional work such as memcpy's that could be avoided.
620 */
621
622 /* An error occurred */
623 if (driver_byte(result) != 0 && /* An error occured */
624 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense data is valid */
625 switch (SCpnt->sense_buffer[2]) {
626 case MEDIUM_ERROR:
627 if (!(SCpnt->sense_buffer[0] & 0x80))
628 break;
629 error_sector = (SCpnt->sense_buffer[3] << 24) |
630 (SCpnt->sense_buffer[4] << 16) |
631 (SCpnt->sense_buffer[5] << 8) |
632 SCpnt->sense_buffer[6];
633 if (SCpnt->request.bh != NULL)
634 block_sectors = SCpnt->request.bh->b_size >> 9;
635 switch (SCpnt->device->sector_size) {
636 case 1024:
637 error_sector <<= 1;
638 if (block_sectors < 2)
639 block_sectors = 2;
640 break;
641 case 2048:
642 error_sector <<= 2;
643 if (block_sectors < 4)
644 block_sectors = 4;
645 break;
646 case 4096:
647 error_sector <<=3;
648 if (block_sectors < 8)
649 block_sectors = 8;
650 break;
651 case 256:
652 error_sector >>= 1;
653 break;
654 default:
655 break;
656 }
657 error_sector -= sd_gendisks[SD_MAJOR_IDX(
658 SCpnt->request.rq_dev)].part[MINOR(
659 SCpnt->request.rq_dev)].start_sect;
660 error_sector &= ~(block_sectors - 1);
661 good_sectors = error_sector - SCpnt->request.sector;
662 if (good_sectors < 0 || good_sectors >= this_count)
663 good_sectors = 0;
664 break;
665
666 case RECOVERED_ERROR:
667 /*
668 * An error occured, but it recovered. Inform the
669 * user, but make sure that it's not treated as a
670 * hard error.
671 */
672 print_sense("sd", SCpnt);
673 SCpnt->result = 0;
674 SCpnt->sense_buffer[0] = 0x0;
675 good_sectors = this_count;
676 break;
677
678 case ILLEGAL_REQUEST:
679 if (SCpnt->device->ten == 1) {
680 if (SCpnt->cmnd[0] == READ_10 ||
681 SCpnt->cmnd[0] == WRITE_10)
682 SCpnt->device->ten = 0;
683 }
684 break;
685
686 default:
687 break;
688 }
689 }
690 /*
691 * This calls the generic completion function, now that we know
692 * how many actual sectors finished, and how many sectors we need
693 * to say have failed.
694 */
695 scsi_io_completion(SCpnt, good_sectors, block_sectors);
696 }
697 /*
698 * requeue_sd_request() is the request handler function for the sd driver.
699 * Its function in life is to take block device requests, and translate
700 * them to SCSI commands.
701 */
702
703
check_scsidisk_media_change(kdev_t full_dev)704 static int check_scsidisk_media_change(kdev_t full_dev)
705 {
706 int retval;
707 int target;
708 int flag = 0;
709 Scsi_Device * SDev;
710
711 target = DEVICE_NR(full_dev);
712 SDev = rscsi_disks[target].device;
713
714 if (target >= sd_template.dev_max || !SDev) {
715 printk("SCSI disk request error: invalid device.\n");
716 return 0;
717 }
718 if (!SDev->removable)
719 return 0;
720
721 /*
722 * If the device is offline, don't send any commands - just pretend as
723 * if the command failed. If the device ever comes back online, we
724 * can deal with it then. It is only because of unrecoverable errors
725 * that we would ever take a device offline in the first place.
726 */
727 if (SDev->online == FALSE) {
728 rscsi_disks[target].ready = 0;
729 SDev->changed = 1;
730 return 1; /* This will force a flush, if called from
731 * check_disk_change */
732 }
733
734 /*
735 * Using TEST_UNIT_READY enables differentiation between drive with
736 * no cartridge loaded - NOT READY, drive with changed cartridge -
737 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
738 *
739 * Drives that auto spin down. eg iomega jaz 1G, will be started
740 * by sd_init_onedisk(), whenever revalidate_scsidisk() is called.
741 */
742 retval = -ENODEV;
743 if (scsi_block_when_processing_errors(SDev))
744 retval = scsi_ioctl(SDev, SCSI_IOCTL_TEST_UNIT_READY, NULL);
745
746 if (retval) { /* Unable to test, unit probably not ready.
747 * This usually means there is no disc in the
748 * drive. Mark as changed, and we will figure
749 * it out later once the drive is available
750 * again. */
751
752 rscsi_disks[target].ready = 0;
753 SDev->changed = 1;
754 return 1; /* This will force a flush, if called from
755 * check_disk_change */
756 }
757 /*
758 * for removable scsi disk ( FLOPTICAL ) we have to recognise the
759 * presence of disk in the drive. This is kept in the Scsi_Disk
760 * struct and tested at open ! Daniel Roche ( dan@lectra.fr )
761 */
762
763 rscsi_disks[target].ready = 1; /* FLOPTICAL */
764
765 retval = SDev->changed;
766 if (!flag)
767 SDev->changed = 0;
768 return retval;
769 }
770
sd_init_onedisk(int i)771 static int sd_init_onedisk(int i)
772 {
773 unsigned char cmd[10];
774 char nbuff[6];
775 unsigned char *buffer;
776 unsigned long spintime_value = 0;
777 int retries, spintime;
778 unsigned int the_result;
779 int sector_size;
780 Scsi_Request *SRpnt;
781
782 /*
783 * Get the name of the disk, in case we need to log it somewhere.
784 */
785 sd_devname(i, nbuff);
786
787 /*
788 * If the device is offline, don't try and read capacity or any
789 * of the other niceties.
790 */
791 if (rscsi_disks[i].device->online == FALSE)
792 return i;
793
794 /*
795 * We need to retry the READ_CAPACITY because a UNIT_ATTENTION is
796 * considered a fatal error, and many devices report such an error
797 * just after a scsi bus reset.
798 */
799
800 SRpnt = scsi_allocate_request(rscsi_disks[i].device);
801 if (!SRpnt) {
802 printk(KERN_WARNING "(sd_init_onedisk:) Request allocation failure.\n");
803 return i;
804 }
805
806 buffer = (unsigned char *) scsi_malloc(512);
807 if (!buffer) {
808 printk(KERN_WARNING "(sd_init_onedisk:) Memory allocation failure.\n");
809 scsi_release_request(SRpnt);
810 return i;
811 }
812
813 spintime = 0;
814
815 /* Spin up drives, as required. Only do this at boot time */
816 /* Spinup needs to be done for module loads too. */
817 do {
818 retries = 0;
819
820 do {
821 cmd[0] = TEST_UNIT_READY;
822 cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
823 ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
824 memset((void *) &cmd[2], 0, 8);
825 SRpnt->sr_cmd_len = 0;
826 SRpnt->sr_sense_buffer[0] = 0;
827 SRpnt->sr_sense_buffer[2] = 0;
828 SRpnt->sr_data_direction = SCSI_DATA_NONE;
829
830 scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer,
831 0/*512*/, SD_TIMEOUT, MAX_RETRIES);
832
833 the_result = SRpnt->sr_result;
834 retries++;
835 } while (retries < 3
836 && (the_result !=0
837 || ((driver_byte(the_result) & DRIVER_SENSE)
838 && SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
839
840 /*
841 * If the drive has indicated to us that it doesn't have
842 * any media in it, don't bother with any of the rest of
843 * this crap.
844 */
845 if( the_result != 0
846 && ((driver_byte(the_result) & DRIVER_SENSE) != 0)
847 && SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION
848 && SRpnt->sr_sense_buffer[12] == 0x3A ) {
849 rscsi_disks[i].capacity = 0x1fffff;
850 sector_size = 512;
851 rscsi_disks[i].device->changed = 1;
852 rscsi_disks[i].ready = 0;
853 break;
854 }
855
856 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
857 /* no sense, TUR either succeeded or failed
858 * with a status error */
859 if(!spintime && the_result != 0)
860 printk(KERN_NOTICE "%s: Unit Not Ready, error = 0x%x\n", nbuff, the_result);
861 break;
862 }
863
864 /*
865 * The device does not want the automatic start to be issued.
866 */
867 if (rscsi_disks[i].device->no_start_on_add) {
868 break;
869 }
870
871 /*
872 * If manual intervention is required, or this is an
873 * absent USB storage device, a spinup is meaningless.
874 */
875 if (SRpnt->sr_sense_buffer[2] == NOT_READY &&
876 SRpnt->sr_sense_buffer[12] == 4 /* not ready */ &&
877 SRpnt->sr_sense_buffer[13] == 3) {
878 break; /* manual intervention required */
879 /* Look for non-removable devices that return NOT_READY.
880 * Issue command to spin up drive for these cases. */
881 } else if (the_result && !rscsi_disks[i].device->removable &&
882 SRpnt->sr_sense_buffer[2] == NOT_READY) {
883 unsigned long time1;
884 if (!spintime) {
885 printk("%s: Spinning up disk...", nbuff);
886 cmd[0] = START_STOP;
887 cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
888 ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
889 cmd[1] |= 1; /* Return immediately */
890 memset((void *) &cmd[2], 0, 8);
891 cmd[4] = 1; /* Start spin cycle */
892 SRpnt->sr_cmd_len = 0;
893 SRpnt->sr_sense_buffer[0] = 0;
894 SRpnt->sr_sense_buffer[2] = 0;
895
896 SRpnt->sr_data_direction = SCSI_DATA_NONE;
897 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
898 0/*512*/, SD_TIMEOUT, MAX_RETRIES);
899 spintime_value = jiffies;
900 }
901 spintime = 1;
902 time1 = HZ;
903 /* Wait 1 second for next try */
904 do {
905 current->state = TASK_UNINTERRUPTIBLE;
906 time1 = schedule_timeout(time1);
907 } while(time1);
908 printk(".");
909 } else {
910 /* we don't understand the sense code, so it's
911 * probably pointless to loop */
912 if(!spintime) {
913 printk(KERN_NOTICE "%s: Unit Not Ready, sense:\n", nbuff);
914 print_req_sense("", SRpnt);
915 }
916 break;
917 }
918 } while (the_result && spintime &&
919 time_after(spintime_value + 100 * HZ, jiffies));
920 if (spintime) {
921 if (the_result)
922 printk("not responding...\n");
923 else
924 printk("ready\n");
925 }
926 retries = 3;
927 do {
928 cmd[0] = READ_CAPACITY;
929 cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
930 ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
931 memset((void *) &cmd[2], 0, 8);
932 memset((void *) buffer, 0, 8);
933 SRpnt->sr_cmd_len = 0;
934 SRpnt->sr_sense_buffer[0] = 0;
935 SRpnt->sr_sense_buffer[2] = 0;
936
937 SRpnt->sr_data_direction = SCSI_DATA_READ;
938 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
939 8, SD_TIMEOUT, MAX_RETRIES);
940
941 the_result = SRpnt->sr_result;
942 retries--;
943
944 } while (the_result && retries);
945
946 /*
947 * The SCSI standard says:
948 * "READ CAPACITY is necessary for self configuring software"
949 * While not mandatory, support of READ CAPACITY is strongly
950 * encouraged.
951 * We used to die if we couldn't successfully do a READ CAPACITY.
952 * But, now we go on about our way. The side effects of this are
953 *
954 * 1. We can't know block size with certainty. I have said
955 * "512 bytes is it" as this is most common.
956 *
957 * 2. Recovery from when someone attempts to read past the
958 * end of the raw device will be slower.
959 */
960
961 if (the_result) {
962 printk("%s : READ CAPACITY failed.\n"
963 "%s : status = %x, message = %02x, host = %d, driver = %02x \n",
964 nbuff, nbuff,
965 status_byte(the_result),
966 msg_byte(the_result),
967 host_byte(the_result),
968 driver_byte(the_result)
969 );
970 if (driver_byte(the_result) & DRIVER_SENSE)
971 print_req_sense("sd", SRpnt);
972 else
973 printk("%s : sense not available. \n", nbuff);
974
975 printk("%s : block size assumed to be 512 bytes, disk size 1GB. \n",
976 nbuff);
977 rscsi_disks[i].capacity = 0x1fffff;
978 sector_size = 512;
979
980 /* Set dirty bit for removable devices if not ready -
981 * sometimes drives will not report this properly. */
982 if (rscsi_disks[i].device->removable &&
983 SRpnt->sr_sense_buffer[2] == NOT_READY)
984 rscsi_disks[i].device->changed = 1;
985
986 } else {
987 /*
988 * FLOPTICAL, if read_capa is ok, drive is assumed to be ready
989 */
990 rscsi_disks[i].ready = 1;
991
992 rscsi_disks[i].capacity = 1 + ((buffer[0] << 24) |
993 (buffer[1] << 16) |
994 (buffer[2] << 8) |
995 buffer[3]);
996
997 sector_size = (buffer[4] << 24) |
998 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
999
1000 if (sector_size == 0) {
1001 sector_size = 512;
1002 printk("%s : sector size 0 reported, assuming 512.\n",
1003 nbuff);
1004 }
1005 if (sector_size != 512 &&
1006 sector_size != 1024 &&
1007 sector_size != 2048 &&
1008 sector_size != 4096 &&
1009 sector_size != 256) {
1010 printk("%s : unsupported sector size %d.\n",
1011 nbuff, sector_size);
1012 /*
1013 * The user might want to re-format the drive with
1014 * a supported sectorsize. Once this happens, it
1015 * would be relatively trivial to set the thing up.
1016 * For this reason, we leave the thing in the table.
1017 */
1018 rscsi_disks[i].capacity = 0;
1019 }
1020 if (sector_size > 1024) {
1021 int m;
1022
1023 /*
1024 * We must fix the sd_blocksizes and sd_hardsizes
1025 * to allow us to read the partition tables.
1026 * The disk reading code does not allow for reading
1027 * of partial sectors.
1028 */
1029 for (m = i << 4; m < ((i + 1) << 4); m++) {
1030 sd_blocksizes[m] = sector_size;
1031 }
1032 } {
1033 /*
1034 * The msdos fs needs to know the hardware sector size
1035 * So I have created this table. See ll_rw_blk.c
1036 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1037 */
1038 int m;
1039 int hard_sector = sector_size;
1040 unsigned int sz = (rscsi_disks[i].capacity/2) * (hard_sector/256);
1041
1042 /* There are 16 minors allocated for each major device */
1043 for (m = i << 4; m < ((i + 1) << 4); m++) {
1044 sd_hardsizes[m] = hard_sector;
1045 }
1046
1047 printk("SCSI device %s: "
1048 "%u %d-byte hdwr sectors (%u MB)\n",
1049 nbuff, rscsi_disks[i].capacity,
1050 hard_sector, (sz - sz/625 + 974)/1950);
1051 }
1052
1053 /* Rescale capacity to 512-byte units */
1054 if (sector_size == 4096)
1055 rscsi_disks[i].capacity <<= 3;
1056 if (sector_size == 2048)
1057 rscsi_disks[i].capacity <<= 2;
1058 if (sector_size == 1024)
1059 rscsi_disks[i].capacity <<= 1;
1060 if (sector_size == 256)
1061 rscsi_disks[i].capacity >>= 1;
1062 }
1063
1064
1065 /*
1066 * Unless otherwise specified, this is not write protected.
1067 */
1068 rscsi_disks[i].write_prot = 0;
1069 if (rscsi_disks[i].device->removable && rscsi_disks[i].ready) {
1070 /* FLOPTICAL */
1071
1072 /*
1073 * For removable scsi disk ( FLOPTICAL ) we have to recognise
1074 * the Write Protect Flag. This flag is kept in the Scsi_Disk
1075 * struct and tested at open !
1076 * Daniel Roche ( dan@lectra.fr )
1077 *
1078 * Changed to get all pages (0x3f) rather than page 1 to
1079 * get around devices which do not have a page 1. Since
1080 * we're only interested in the header anyway, this should
1081 * be fine.
1082 * -- Matthew Dharm (mdharm-scsi@one-eyed-alien.net)
1083 */
1084
1085 memset((void *) &cmd[0], 0, 8);
1086 cmd[0] = MODE_SENSE;
1087 cmd[1] = (rscsi_disks[i].device->scsi_level <= SCSI_2) ?
1088 ((rscsi_disks[i].device->lun << 5) & 0xe0) : 0;
1089 cmd[2] = 0x3f; /* Get all pages */
1090 cmd[4] = 255; /* Ask for 255 bytes, even tho we want just the first 8 */
1091 SRpnt->sr_cmd_len = 0;
1092 SRpnt->sr_sense_buffer[0] = 0;
1093 SRpnt->sr_sense_buffer[2] = 0;
1094
1095 /* same code as READCAPA !! */
1096 SRpnt->sr_data_direction = SCSI_DATA_READ;
1097 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
1098 512, SD_TIMEOUT, MAX_RETRIES);
1099
1100 the_result = SRpnt->sr_result;
1101
1102 if (the_result) {
1103 printk("%s: test WP failed, assume Write Enabled\n", nbuff);
1104 } else {
1105 rscsi_disks[i].write_prot = ((buffer[2] & 0x80) != 0);
1106 printk("%s: Write Protect is %s\n", nbuff,
1107 rscsi_disks[i].write_prot ? "on" : "off");
1108 }
1109
1110 } /* check for write protect */
1111 SRpnt->sr_device->ten = 1;
1112 SRpnt->sr_device->remap = 1;
1113 SRpnt->sr_device->sector_size = sector_size;
1114 /* Wake up a process waiting for device */
1115 scsi_release_request(SRpnt);
1116 SRpnt = NULL;
1117
1118 scsi_free(buffer, 512);
1119 return i;
1120 }
1121
1122 /*
1123 * The sd_init() function looks at all SCSI drives present, determines
1124 * their size, and reads partition table entries for them.
1125 */
1126
1127 static int sd_registered;
1128
sd_init()1129 static int sd_init()
1130 {
1131 int i;
1132
1133 if (sd_template.dev_noticed == 0)
1134 return 0;
1135
1136 if (!rscsi_disks)
1137 sd_template.dev_max = sd_template.dev_noticed + SD_EXTRA_DEVS;
1138
1139 if (sd_template.dev_max > N_SD_MAJORS * SCSI_DISKS_PER_MAJOR)
1140 sd_template.dev_max = N_SD_MAJORS * SCSI_DISKS_PER_MAJOR;
1141
1142 if (!sd_registered) {
1143 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1144 if (devfs_register_blkdev(SD_MAJOR(i), "sd", &sd_fops)) {
1145 printk("Unable to get major %d for SCSI disk\n", SD_MAJOR(i));
1146 sd_template.dev_noticed = 0;
1147 return 1;
1148 }
1149 }
1150 sd_registered++;
1151 }
1152 /* We do not support attaching loadable devices yet. */
1153 if (rscsi_disks)
1154 return 0;
1155
1156 rscsi_disks = kmalloc(sd_template.dev_max * sizeof(Scsi_Disk), GFP_ATOMIC);
1157 if (!rscsi_disks)
1158 goto cleanup_devfs;
1159 memset(rscsi_disks, 0, sd_template.dev_max * sizeof(Scsi_Disk));
1160
1161 /* for every (necessary) major: */
1162 sd_sizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1163 if (!sd_sizes)
1164 goto cleanup_disks;
1165 memset(sd_sizes, 0, (sd_template.dev_max << 4) * sizeof(int));
1166
1167 sd_blocksizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1168 if (!sd_blocksizes)
1169 goto cleanup_sizes;
1170
1171 sd_hardsizes = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1172 if (!sd_hardsizes)
1173 goto cleanup_blocksizes;
1174
1175 sd_max_sectors = kmalloc((sd_template.dev_max << 4) * sizeof(int), GFP_ATOMIC);
1176 if (!sd_max_sectors)
1177 goto cleanup_max_sectors;
1178
1179 for (i = 0; i < sd_template.dev_max << 4; i++) {
1180 sd_blocksizes[i] = 1024;
1181 sd_hardsizes[i] = 512;
1182 /*
1183 * Allow lowlevel device drivers to generate 512k large scsi
1184 * commands if they know what they're doing and they ask for it
1185 * explicitly via the SHpnt->max_sectors API.
1186 */
1187 sd_max_sectors[i] = MAX_SEGMENTS*8;
1188 }
1189
1190 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1191 blksize_size[SD_MAJOR(i)] = sd_blocksizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1192 hardsect_size[SD_MAJOR(i)] = sd_hardsizes + i * (SCSI_DISKS_PER_MAJOR << 4);
1193 max_sectors[SD_MAJOR(i)] = sd_max_sectors + i * (SCSI_DISKS_PER_MAJOR << 4);
1194 }
1195
1196 sd_gendisks = kmalloc(N_USED_SD_MAJORS * sizeof(struct gendisk), GFP_ATOMIC);
1197 if (!sd_gendisks)
1198 goto cleanup_sd_gendisks;
1199 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1200 sd_gendisks[i] = sd_gendisk; /* memcpy */
1201 sd_gendisks[i].de_arr = kmalloc (SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].de_arr,
1202 GFP_ATOMIC);
1203 if (!sd_gendisks[i].de_arr)
1204 goto cleanup_gendisks_de_arr;
1205 memset (sd_gendisks[i].de_arr, 0,
1206 SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].de_arr);
1207 sd_gendisks[i].flags = kmalloc (SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].flags,
1208 GFP_ATOMIC);
1209 if (!sd_gendisks[i].flags)
1210 goto cleanup_gendisks_flags;
1211 memset (sd_gendisks[i].flags, 0,
1212 SCSI_DISKS_PER_MAJOR * sizeof *sd_gendisks[i].flags);
1213 sd_gendisks[i].major = SD_MAJOR(i);
1214 sd_gendisks[i].major_name = "sd";
1215 sd_gendisks[i].minor_shift = 4;
1216 sd_gendisks[i].max_p = 1 << 4;
1217 sd_gendisks[i].part = kmalloc((SCSI_DISKS_PER_MAJOR << 4) * sizeof(struct hd_struct),
1218 GFP_ATOMIC);
1219 if (!sd_gendisks[i].part)
1220 goto cleanup_gendisks_part;
1221 memset(sd_gendisks[i].part, 0, (SCSI_DISKS_PER_MAJOR << 4) * sizeof(struct hd_struct));
1222 sd_gendisks[i].sizes = sd_sizes + (i * SCSI_DISKS_PER_MAJOR << 4);
1223 sd_gendisks[i].nr_real = SCSI_DISKS_PER_MAJOR;
1224 sd_gendisks[i].real_devices =
1225 (void *) (rscsi_disks + i * SCSI_DISKS_PER_MAJOR);
1226 }
1227
1228 return 0;
1229
1230 cleanup_gendisks_part:
1231 kfree(sd_gendisks[i].flags);
1232 cleanup_gendisks_flags:
1233 kfree(sd_gendisks[i].de_arr);
1234 cleanup_gendisks_de_arr:
1235 while (--i >= 0 ) {
1236 kfree(sd_gendisks[i].de_arr);
1237 kfree(sd_gendisks[i].flags);
1238 kfree(sd_gendisks[i].part);
1239 }
1240 kfree(sd_gendisks);
1241 sd_gendisks = NULL;
1242 cleanup_sd_gendisks:
1243 kfree(sd_max_sectors);
1244 cleanup_max_sectors:
1245 kfree(sd_hardsizes);
1246 cleanup_blocksizes:
1247 kfree(sd_blocksizes);
1248 cleanup_sizes:
1249 kfree(sd_sizes);
1250 cleanup_disks:
1251 kfree(rscsi_disks);
1252 rscsi_disks = NULL;
1253 cleanup_devfs:
1254 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1255 devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1256 }
1257 sd_registered--;
1258 sd_template.dev_noticed = 0;
1259 return 1;
1260 }
1261
1262
sd_finish()1263 static void sd_finish()
1264 {
1265 int i;
1266
1267 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1268 blk_dev[SD_MAJOR(i)].queue = sd_find_queue;
1269 add_gendisk(&sd_gendisks[i]);
1270 }
1271
1272 for (i = 0; i < sd_template.dev_max; ++i)
1273 if (!rscsi_disks[i].capacity && rscsi_disks[i].device) {
1274 sd_init_onedisk(i);
1275 if (!rscsi_disks[i].has_part_table) {
1276 sd_sizes[i << 4] = rscsi_disks[i].capacity;
1277 register_disk(&SD_GENDISK(i), MKDEV_SD(i),
1278 1<<4, &sd_fops,
1279 rscsi_disks[i].capacity);
1280 rscsi_disks[i].has_part_table = 1;
1281 }
1282 }
1283 /* If our host adapter is capable of scatter-gather, then we increase
1284 * the read-ahead to 60 blocks (120 sectors). If not, we use
1285 * a two block (4 sector) read ahead. We can only respect this with the
1286 * granularity of every 16 disks (one device major).
1287 */
1288 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1289 read_ahead[SD_MAJOR(i)] =
1290 (rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device
1291 && rscsi_disks[i * SCSI_DISKS_PER_MAJOR].device->host->sg_tablesize)
1292 ? 120 /* 120 sector read-ahead */
1293 : 4; /* 4 sector read-ahead */
1294 }
1295
1296 return;
1297 }
1298
sd_detect(Scsi_Device * SDp)1299 static int sd_detect(Scsi_Device * SDp)
1300 {
1301 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1302 return 0;
1303 sd_template.dev_noticed++;
1304 return 1;
1305 }
1306
sd_attach(Scsi_Device * SDp)1307 static int sd_attach(Scsi_Device * SDp)
1308 {
1309 unsigned int devnum;
1310 Scsi_Disk *dpnt;
1311 int i;
1312 char nbuff[6];
1313
1314 if (SDp->type != TYPE_DISK && SDp->type != TYPE_MOD)
1315 return 0;
1316
1317 if (sd_template.nr_dev >= sd_template.dev_max || rscsi_disks == NULL) {
1318 SDp->attached--;
1319 return 1;
1320 }
1321 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1322 if (!dpnt->device)
1323 break;
1324
1325 if (i >= sd_template.dev_max) {
1326 printk(KERN_WARNING "scsi_devices corrupt (sd),"
1327 " nr_dev %d dev_max %d\n",
1328 sd_template.nr_dev, sd_template.dev_max);
1329 SDp->attached--;
1330 return 1;
1331 }
1332
1333 rscsi_disks[i].device = SDp;
1334 rscsi_disks[i].has_part_table = 0;
1335 sd_template.nr_dev++;
1336 devnum = i % SCSI_DISKS_PER_MAJOR;
1337 SD_GENDISK(i).de_arr[devnum] = SDp->de;
1338 if (SDp->removable)
1339 SD_GENDISK(i).flags[devnum] |= GENHD_FL_REMOVABLE;
1340 sd_devname(i, nbuff);
1341 printk("Attached scsi %sdisk %s at scsi%d, channel %d, id %d, lun %d\n",
1342 SDp->removable ? "removable " : "",
1343 nbuff, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
1344 return 0;
1345 }
1346
1347 #define DEVICE_BUSY rscsi_disks[target].device->busy
1348 #define ALLOW_REVALIDATE rscsi_disks[target].device->allow_revalidate
1349 #define USAGE rscsi_disks[target].device->access_count
1350 #define CAPACITY rscsi_disks[target].capacity
1351 #define MAYBE_REINIT sd_init_onedisk(target)
1352
1353 /* This routine is called to flush all partitions and partition tables
1354 * for a changed scsi disk, and then re-read the new partition table.
1355 * If we are revalidating a disk because of a media change, then we
1356 * enter with usage == 0. If we are using an ioctl, we automatically have
1357 * usage == 1 (we need an open channel to use an ioctl :-), so this
1358 * is our limit.
1359 */
revalidate_scsidisk(kdev_t dev,int maxusage)1360 int revalidate_scsidisk(kdev_t dev, int maxusage)
1361 {
1362 struct gendisk *sdgd;
1363 int target;
1364 int max_p;
1365 int start;
1366 int i;
1367
1368 target = DEVICE_NR(dev);
1369
1370 if (DEVICE_BUSY || (ALLOW_REVALIDATE == 0 && USAGE > maxusage)) {
1371 printk("Device busy for revalidation (usage=%d)\n", USAGE);
1372 return -EBUSY;
1373 }
1374 DEVICE_BUSY = 1;
1375
1376 sdgd = &SD_GENDISK(target);
1377 max_p = sd_gendisk.max_p;
1378 start = target << sd_gendisk.minor_shift;
1379
1380 for (i = max_p - 1; i >= 0; i--) {
1381 int index = start + i;
1382 invalidate_device(MKDEV_SD_PARTITION(index), 1);
1383 sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1384 sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1385 /*
1386 * Reset the blocksize for everything so that we can read
1387 * the partition table. Technically we will determine the
1388 * correct block size when we revalidate, but we do this just
1389 * to make sure that everything remains consistent.
1390 */
1391 sd_blocksizes[index] = 1024;
1392 if (rscsi_disks[target].device->sector_size == 2048)
1393 sd_blocksizes[index] = 2048;
1394 else
1395 sd_blocksizes[index] = 1024;
1396 }
1397
1398 #ifdef MAYBE_REINIT
1399 MAYBE_REINIT;
1400 #endif
1401
1402 grok_partitions(&SD_GENDISK(target), target % SCSI_DISKS_PER_MAJOR,
1403 1<<4, CAPACITY);
1404
1405 DEVICE_BUSY = 0;
1406 return 0;
1407 }
1408
fop_revalidate_scsidisk(kdev_t dev)1409 static int fop_revalidate_scsidisk(kdev_t dev)
1410 {
1411 return revalidate_scsidisk(dev, 0);
1412 }
sd_detach(Scsi_Device * SDp)1413 static void sd_detach(Scsi_Device * SDp)
1414 {
1415 Scsi_Disk *dpnt;
1416 struct gendisk *sdgd;
1417 int i, j;
1418 int max_p;
1419 int start;
1420
1421 if (rscsi_disks == NULL)
1422 return;
1423
1424 for (dpnt = rscsi_disks, i = 0; i < sd_template.dev_max; i++, dpnt++)
1425 if (dpnt->device == SDp) {
1426
1427 /* If we are disconnecting a disk driver, sync and invalidate
1428 * everything */
1429 sdgd = &SD_GENDISK(i);
1430 max_p = sd_gendisk.max_p;
1431 start = i << sd_gendisk.minor_shift;
1432
1433 for (j = max_p - 1; j >= 0; j--) {
1434 int index = start + j;
1435 invalidate_device(MKDEV_SD_PARTITION(index), 1);
1436 sdgd->part[SD_MINOR_NUMBER(index)].start_sect = 0;
1437 sdgd->part[SD_MINOR_NUMBER(index)].nr_sects = 0;
1438 sd_sizes[index] = 0;
1439 }
1440 devfs_register_partitions (sdgd,
1441 SD_MINOR_NUMBER (start), 1);
1442 /* unregister_disk() */
1443 dpnt->has_part_table = 0;
1444 dpnt->device = NULL;
1445 dpnt->capacity = 0;
1446 SDp->attached--;
1447 sd_template.dev_noticed--;
1448 sd_template.nr_dev--;
1449 return;
1450 }
1451 return;
1452 }
1453
init_sd(void)1454 static int __init init_sd(void)
1455 {
1456 sd_template.module = THIS_MODULE;
1457 return scsi_register_module(MODULE_SCSI_DEV, &sd_template);
1458 }
1459
exit_sd(void)1460 static void __exit exit_sd(void)
1461 {
1462 int i;
1463
1464 scsi_unregister_module(MODULE_SCSI_DEV, &sd_template);
1465
1466 for (i = 0; i < N_USED_SD_MAJORS; i++)
1467 devfs_unregister_blkdev(SD_MAJOR(i), "sd");
1468
1469 sd_registered--;
1470 if (rscsi_disks != NULL) {
1471 kfree(rscsi_disks);
1472 kfree(sd_sizes);
1473 kfree(sd_blocksizes);
1474 kfree(sd_hardsizes);
1475 kfree(sd_max_sectors);
1476 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1477 kfree(sd_gendisks[i].de_arr);
1478 kfree(sd_gendisks[i].flags);
1479 kfree(sd_gendisks[i].part);
1480 }
1481 }
1482 for (i = 0; i < N_USED_SD_MAJORS; i++) {
1483 del_gendisk(&sd_gendisks[i]);
1484 blksize_size[SD_MAJOR(i)] = NULL;
1485 hardsect_size[SD_MAJOR(i)] = NULL;
1486 max_sectors[SD_MAJOR(i)] = NULL;
1487 read_ahead[SD_MAJOR(i)] = 0;
1488 }
1489 sd_template.dev_max = 0;
1490 if (sd_gendisks != NULL) /* kfree tests for 0, but leave explicit */
1491 kfree(sd_gendisks);
1492 }
1493
1494 module_init(init_sd);
1495 module_exit(exit_sd);
1496 MODULE_LICENSE("GPL");
1497