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 * Modification history: 9 * - Drew Eckhardt <drew@colorado.edu> original 10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple 11 * outstanding request, and other enhancements. 12 * Support loadable low-level scsi drivers. 13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using 14 * eight major numbers. 15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs. 16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in 17 * sd_init and cleanups. 18 * - Alex Davis <letmein@erols.com> Fix problem where partition info 19 * not being read in sd_open. Fix problem where removable media 20 * could be ejected after sd_open. 21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x 22 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox 23 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>: 24 * Support 32k/1M disks. 25 * 26 * Logging policy (needs CONFIG_SCSI_LOGGING defined): 27 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2 28 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1 29 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1 30 * - entering other commands: SCSI_LOG_HLQUEUE level 3 31 * Note: when the logging level is set by the user, it must be greater 32 * than the level indicated above to trigger output. 33 */ 34 35 #include <linux/config.h> 36 #include <linux/module.h> 37 #include <linux/fs.h> 38 #include <linux/kernel.h> 39 #include <linux/sched.h> 40 #include <linux/mm.h> 41 #include <linux/bio.h> 42 #include <linux/genhd.h> 43 #include <linux/hdreg.h> 44 #include <linux/errno.h> 45 #include <linux/idr.h> 46 #include <linux/interrupt.h> 47 #include <linux/init.h> 48 #include <linux/blkdev.h> 49 #include <linux/blkpg.h> 50 #include <linux/kref.h> 51 #include <linux/delay.h> 52 #include <asm/uaccess.h> 53 54 #include <scsi/scsi.h> 55 #include <scsi/scsi_cmnd.h> 56 #include <scsi/scsi_dbg.h> 57 #include <scsi/scsi_device.h> 58 #include <scsi/scsi_driver.h> 59 #include <scsi/scsi_eh.h> 60 #include <scsi/scsi_host.h> 61 #include <scsi/scsi_ioctl.h> 62 #include <scsi/scsi_request.h> 63 #include <scsi/scsicam.h> 64 65 #include "scsi_logging.h" 66 67 /* 68 * More than enough for everybody ;) The huge number of majors 69 * is a leftover from 16bit dev_t days, we don't really need that 70 * much numberspace. 71 */ 72 #define SD_MAJORS 16 73 74 /* 75 * This is limited by the naming scheme enforced in sd_probe, 76 * add another character to it if you really need more disks. 77 */ 78 #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26) 79 80 /* 81 * Time out in seconds for disks and Magneto-opticals (which are slower). 82 */ 83 #define SD_TIMEOUT (30 * HZ) 84 #define SD_MOD_TIMEOUT (75 * HZ) 85 86 /* 87 * Number of allowed retries 88 */ 89 #define SD_MAX_RETRIES 5 90 #define SD_PASSTHROUGH_RETRIES 1 91 92 static void scsi_disk_release(struct kref *kref); 93 94 struct scsi_disk { 95 struct scsi_driver *driver; /* always &sd_template */ 96 struct scsi_device *device; 97 struct kref kref; 98 struct gendisk *disk; 99 unsigned int openers; /* protected by BKL for now, yuck */ 100 sector_t capacity; /* size in 512-byte sectors */ 101 u32 index; 102 u8 media_present; 103 u8 write_prot; 104 unsigned WCE : 1; /* state of disk WCE bit */ 105 unsigned RCD : 1; /* state of disk RCD bit, unused */ 106 }; 107 108 static DEFINE_IDR(sd_index_idr); 109 static DEFINE_SPINLOCK(sd_index_lock); 110 111 /* This semaphore is used to mediate the 0->1 reference get in the 112 * face of object destruction (i.e. we can't allow a get on an 113 * object after last put) */ 114 static DECLARE_MUTEX(sd_ref_sem); 115 116 static int sd_revalidate_disk(struct gendisk *disk); 117 static void sd_rw_intr(struct scsi_cmnd * SCpnt); 118 119 static int sd_probe(struct device *); 120 static int sd_remove(struct device *); 121 static void sd_shutdown(struct device *dev); 122 static void sd_rescan(struct device *); 123 static int sd_init_command(struct scsi_cmnd *); 124 static int sd_issue_flush(struct device *, sector_t *); 125 static void sd_end_flush(request_queue_t *, struct request *); 126 static int sd_prepare_flush(request_queue_t *, struct request *); 127 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname, 128 struct scsi_request *SRpnt, unsigned char *buffer); 129 130 static struct scsi_driver sd_template = { 131 .owner = THIS_MODULE, 132 .gendrv = { 133 .name = "sd", 134 .probe = sd_probe, 135 .remove = sd_remove, 136 .shutdown = sd_shutdown, 137 }, 138 .rescan = sd_rescan, 139 .init_command = sd_init_command, 140 .issue_flush = sd_issue_flush, 141 .prepare_flush = sd_prepare_flush, 142 .end_flush = sd_end_flush, 143 }; 144 145 /* 146 * Device no to disk mapping: 147 * 148 * major disc2 disc p1 149 * |............|.............|....|....| <- dev_t 150 * 31 20 19 8 7 4 3 0 151 * 152 * Inside a major, we have 16k disks, however mapped non- 153 * contiguously. The first 16 disks are for major0, the next 154 * ones with major1, ... Disk 256 is for major0 again, disk 272 155 * for major1, ... 156 * As we stay compatible with our numbering scheme, we can reuse 157 * the well-know SCSI majors 8, 65--71, 136--143. 158 */ 159 static int sd_major(int major_idx) 160 { 161 switch (major_idx) { 162 case 0: 163 return SCSI_DISK0_MAJOR; 164 case 1 ... 7: 165 return SCSI_DISK1_MAJOR + major_idx - 1; 166 case 8 ... 15: 167 return SCSI_DISK8_MAJOR + major_idx - 8; 168 default: 169 BUG(); 170 return 0; /* shut up gcc */ 171 } 172 } 173 174 #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref) 175 176 static inline struct scsi_disk *scsi_disk(struct gendisk *disk) 177 { 178 return container_of(disk->private_data, struct scsi_disk, driver); 179 } 180 181 static struct scsi_disk *scsi_disk_get(struct gendisk *disk) 182 { 183 struct scsi_disk *sdkp = NULL; 184 185 down(&sd_ref_sem); 186 if (disk->private_data == NULL) 187 goto out; 188 sdkp = scsi_disk(disk); 189 kref_get(&sdkp->kref); 190 if (scsi_device_get(sdkp->device)) 191 goto out_put; 192 up(&sd_ref_sem); 193 return sdkp; 194 195 out_put: 196 kref_put(&sdkp->kref, scsi_disk_release); 197 sdkp = NULL; 198 out: 199 up(&sd_ref_sem); 200 return sdkp; 201 } 202 203 static void scsi_disk_put(struct scsi_disk *sdkp) 204 { 205 struct scsi_device *sdev = sdkp->device; 206 207 down(&sd_ref_sem); 208 kref_put(&sdkp->kref, scsi_disk_release); 209 scsi_device_put(sdev); 210 up(&sd_ref_sem); 211 } 212 213 /** 214 * sd_init_command - build a scsi (read or write) command from 215 * information in the request structure. 216 * @SCpnt: pointer to mid-level's per scsi command structure that 217 * contains request and into which the scsi command is written 218 * 219 * Returns 1 if successful and 0 if error (or cannot be done now). 220 **/ 221 static int sd_init_command(struct scsi_cmnd * SCpnt) 222 { 223 unsigned int this_count, timeout; 224 struct gendisk *disk; 225 sector_t block; 226 struct scsi_device *sdp = SCpnt->device; 227 struct request *rq = SCpnt->request; 228 229 timeout = sdp->timeout; 230 231 /* 232 * SG_IO from block layer already setup, just copy cdb basically 233 */ 234 if (blk_pc_request(rq)) { 235 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd)) 236 return 0; 237 238 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd)); 239 if (rq_data_dir(rq) == WRITE) 240 SCpnt->sc_data_direction = DMA_TO_DEVICE; 241 else if (rq->data_len) 242 SCpnt->sc_data_direction = DMA_FROM_DEVICE; 243 else 244 SCpnt->sc_data_direction = DMA_NONE; 245 246 this_count = rq->data_len; 247 if (rq->timeout) 248 timeout = rq->timeout; 249 250 SCpnt->transfersize = rq->data_len; 251 SCpnt->allowed = SD_PASSTHROUGH_RETRIES; 252 goto queue; 253 } 254 255 /* 256 * we only do REQ_CMD and REQ_BLOCK_PC 257 */ 258 if (!blk_fs_request(rq)) 259 return 0; 260 261 disk = rq->rq_disk; 262 block = rq->sector; 263 this_count = SCpnt->request_bufflen >> 9; 264 265 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, " 266 "count=%d\n", disk->disk_name, 267 (unsigned long long)block, this_count)); 268 269 if (!sdp || !scsi_device_online(sdp) || 270 block + rq->nr_sectors > get_capacity(disk)) { 271 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", 272 rq->nr_sectors)); 273 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); 274 return 0; 275 } 276 277 if (sdp->changed) { 278 /* 279 * quietly refuse to do anything to a changed disc until 280 * the changed bit has been reset 281 */ 282 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ 283 return 0; 284 } 285 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n", 286 disk->disk_name, (unsigned long long)block)); 287 288 /* 289 * If we have a 1K hardware sectorsize, prevent access to single 290 * 512 byte sectors. In theory we could handle this - in fact 291 * the scsi cdrom driver must be able to handle this because 292 * we typically use 1K blocksizes, and cdroms typically have 293 * 2K hardware sectorsizes. Of course, things are simpler 294 * with the cdrom, since it is read-only. For performance 295 * reasons, the filesystems should be able to handle this 296 * and not force the scsi disk driver to use bounce buffers 297 * for this. 298 */ 299 if (sdp->sector_size == 1024) { 300 if ((block & 1) || (rq->nr_sectors & 1)) { 301 printk(KERN_ERR "sd: Bad block number requested"); 302 return 0; 303 } else { 304 block = block >> 1; 305 this_count = this_count >> 1; 306 } 307 } 308 if (sdp->sector_size == 2048) { 309 if ((block & 3) || (rq->nr_sectors & 3)) { 310 printk(KERN_ERR "sd: Bad block number requested"); 311 return 0; 312 } else { 313 block = block >> 2; 314 this_count = this_count >> 2; 315 } 316 } 317 if (sdp->sector_size == 4096) { 318 if ((block & 7) || (rq->nr_sectors & 7)) { 319 printk(KERN_ERR "sd: Bad block number requested"); 320 return 0; 321 } else { 322 block = block >> 3; 323 this_count = this_count >> 3; 324 } 325 } 326 if (rq_data_dir(rq) == WRITE) { 327 if (!sdp->writeable) { 328 return 0; 329 } 330 SCpnt->cmnd[0] = WRITE_6; 331 SCpnt->sc_data_direction = DMA_TO_DEVICE; 332 } else if (rq_data_dir(rq) == READ) { 333 SCpnt->cmnd[0] = READ_6; 334 SCpnt->sc_data_direction = DMA_FROM_DEVICE; 335 } else { 336 printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags); 337 /* overkill panic("Unknown sd command %lx\n", rq->flags); */ 338 return 0; 339 } 340 341 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", 342 disk->disk_name, (rq_data_dir(rq) == WRITE) ? 343 "writing" : "reading", this_count, rq->nr_sectors)); 344 345 SCpnt->cmnd[1] = 0; 346 347 if (block > 0xffffffff) { 348 SCpnt->cmnd[0] += READ_16 - READ_6; 349 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; 350 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; 351 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; 352 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; 353 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff; 354 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff; 355 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff; 356 SCpnt->cmnd[9] = (unsigned char) block & 0xff; 357 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff; 358 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff; 359 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff; 360 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; 361 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; 362 } else if ((this_count > 0xff) || (block > 0x1fffff) || 363 SCpnt->device->use_10_for_rw) { 364 if (this_count > 0xffff) 365 this_count = 0xffff; 366 367 SCpnt->cmnd[0] += READ_10 - READ_6; 368 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; 369 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; 370 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; 371 SCpnt->cmnd[5] = (unsigned char) block & 0xff; 372 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; 373 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; 374 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; 375 } else { 376 if (this_count > 0xff) 377 this_count = 0xff; 378 379 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f); 380 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff); 381 SCpnt->cmnd[3] = (unsigned char) block & 0xff; 382 SCpnt->cmnd[4] = (unsigned char) this_count; 383 SCpnt->cmnd[5] = 0; 384 } 385 SCpnt->request_bufflen = SCpnt->bufflen = 386 this_count * sdp->sector_size; 387 388 /* 389 * We shouldn't disconnect in the middle of a sector, so with a dumb 390 * host adapter, it's safe to assume that we can at least transfer 391 * this many bytes between each connect / disconnect. 392 */ 393 SCpnt->transfersize = sdp->sector_size; 394 SCpnt->underflow = this_count << 9; 395 SCpnt->allowed = SD_MAX_RETRIES; 396 397 queue: 398 SCpnt->timeout_per_command = timeout; 399 400 /* 401 * This is the completion routine we use. This is matched in terms 402 * of capability to this function. 403 */ 404 SCpnt->done = sd_rw_intr; 405 406 /* 407 * This indicates that the command is ready from our end to be 408 * queued. 409 */ 410 return 1; 411 } 412 413 /** 414 * sd_open - open a scsi disk device 415 * @inode: only i_rdev member may be used 416 * @filp: only f_mode and f_flags may be used 417 * 418 * Returns 0 if successful. Returns a negated errno value in case 419 * of error. 420 * 421 * Note: This can be called from a user context (e.g. fsck(1) ) 422 * or from within the kernel (e.g. as a result of a mount(1) ). 423 * In the latter case @inode and @filp carry an abridged amount 424 * of information as noted above. 425 **/ 426 static int sd_open(struct inode *inode, struct file *filp) 427 { 428 struct gendisk *disk = inode->i_bdev->bd_disk; 429 struct scsi_disk *sdkp; 430 struct scsi_device *sdev; 431 int retval; 432 433 if (!(sdkp = scsi_disk_get(disk))) 434 return -ENXIO; 435 436 437 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name)); 438 439 sdev = sdkp->device; 440 441 /* 442 * If the device is in error recovery, wait until it is done. 443 * If the device is offline, then disallow any access to it. 444 */ 445 retval = -ENXIO; 446 if (!scsi_block_when_processing_errors(sdev)) 447 goto error_out; 448 449 if (sdev->removable || sdkp->write_prot) 450 check_disk_change(inode->i_bdev); 451 452 /* 453 * If the drive is empty, just let the open fail. 454 */ 455 retval = -ENOMEDIUM; 456 if (sdev->removable && !sdkp->media_present && 457 !(filp->f_flags & O_NDELAY)) 458 goto error_out; 459 460 /* 461 * If the device has the write protect tab set, have the open fail 462 * if the user expects to be able to write to the thing. 463 */ 464 retval = -EROFS; 465 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE)) 466 goto error_out; 467 468 /* 469 * It is possible that the disk changing stuff resulted in 470 * the device being taken offline. If this is the case, 471 * report this to the user, and don't pretend that the 472 * open actually succeeded. 473 */ 474 retval = -ENXIO; 475 if (!scsi_device_online(sdev)) 476 goto error_out; 477 478 if (!sdkp->openers++ && sdev->removable) { 479 if (scsi_block_when_processing_errors(sdev)) 480 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); 481 } 482 483 return 0; 484 485 error_out: 486 scsi_disk_put(sdkp); 487 return retval; 488 } 489 490 /** 491 * sd_release - invoked when the (last) close(2) is called on this 492 * scsi disk. 493 * @inode: only i_rdev member may be used 494 * @filp: only f_mode and f_flags may be used 495 * 496 * Returns 0. 497 * 498 * Note: may block (uninterruptible) if error recovery is underway 499 * on this disk. 500 **/ 501 static int sd_release(struct inode *inode, struct file *filp) 502 { 503 struct gendisk *disk = inode->i_bdev->bd_disk; 504 struct scsi_disk *sdkp = scsi_disk(disk); 505 struct scsi_device *sdev = sdkp->device; 506 507 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name)); 508 509 if (!--sdkp->openers && sdev->removable) { 510 if (scsi_block_when_processing_errors(sdev)) 511 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); 512 } 513 514 /* 515 * XXX and what if there are packets in flight and this close() 516 * XXX is followed by a "rmmod sd_mod"? 517 */ 518 scsi_disk_put(sdkp); 519 return 0; 520 } 521 522 static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *loc) 523 { 524 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); 525 struct scsi_device *sdp = sdkp->device; 526 struct Scsi_Host *host = sdp->host; 527 int diskinfo[4]; 528 529 /* default to most commonly used values */ 530 diskinfo[0] = 0x40; /* 1 << 6 */ 531 diskinfo[1] = 0x20; /* 1 << 5 */ 532 diskinfo[2] = sdkp->capacity >> 11; 533 534 /* override with calculated, extended default, or driver values */ 535 if (host->hostt->bios_param) 536 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo); 537 else 538 scsicam_bios_param(bdev, sdkp->capacity, diskinfo); 539 540 if (put_user(diskinfo[0], &loc->heads)) 541 return -EFAULT; 542 if (put_user(diskinfo[1], &loc->sectors)) 543 return -EFAULT; 544 if (put_user(diskinfo[2], &loc->cylinders)) 545 return -EFAULT; 546 if (put_user((unsigned)get_start_sect(bdev), 547 (unsigned long __user *)&loc->start)) 548 return -EFAULT; 549 return 0; 550 } 551 552 /** 553 * sd_ioctl - process an ioctl 554 * @inode: only i_rdev/i_bdev members may be used 555 * @filp: only f_mode and f_flags may be used 556 * @cmd: ioctl command number 557 * @arg: this is third argument given to ioctl(2) system call. 558 * Often contains a pointer. 559 * 560 * Returns 0 if successful (some ioctls return postive numbers on 561 * success as well). Returns a negated errno value in case of error. 562 * 563 * Note: most ioctls are forward onto the block subsystem or further 564 * down in the scsi subsytem. 565 **/ 566 static int sd_ioctl(struct inode * inode, struct file * filp, 567 unsigned int cmd, unsigned long arg) 568 { 569 struct block_device *bdev = inode->i_bdev; 570 struct gendisk *disk = bdev->bd_disk; 571 struct scsi_device *sdp = scsi_disk(disk)->device; 572 void __user *p = (void __user *)arg; 573 int error; 574 575 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n", 576 disk->disk_name, cmd)); 577 578 /* 579 * If we are in the middle of error recovery, don't let anyone 580 * else try and use this device. Also, if error recovery fails, it 581 * may try and take the device offline, in which case all further 582 * access to the device is prohibited. 583 */ 584 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp); 585 if (!scsi_block_when_processing_errors(sdp) || !error) 586 return error; 587 588 if (cmd == HDIO_GETGEO) { 589 if (!arg) 590 return -EINVAL; 591 return sd_hdio_getgeo(bdev, p); 592 } 593 594 /* 595 * Send SCSI addressing ioctls directly to mid level, send other 596 * ioctls to block level and then onto mid level if they can't be 597 * resolved. 598 */ 599 switch (cmd) { 600 case SCSI_IOCTL_GET_IDLUN: 601 case SCSI_IOCTL_GET_BUS_NUMBER: 602 return scsi_ioctl(sdp, cmd, p); 603 default: 604 error = scsi_cmd_ioctl(filp, disk, cmd, p); 605 if (error != -ENOTTY) 606 return error; 607 } 608 return scsi_ioctl(sdp, cmd, p); 609 } 610 611 static void set_media_not_present(struct scsi_disk *sdkp) 612 { 613 sdkp->media_present = 0; 614 sdkp->capacity = 0; 615 sdkp->device->changed = 1; 616 } 617 618 /** 619 * sd_media_changed - check if our medium changed 620 * @disk: kernel device descriptor 621 * 622 * Returns 0 if not applicable or no change; 1 if change 623 * 624 * Note: this function is invoked from the block subsystem. 625 **/ 626 static int sd_media_changed(struct gendisk *disk) 627 { 628 struct scsi_disk *sdkp = scsi_disk(disk); 629 struct scsi_device *sdp = sdkp->device; 630 int retval; 631 632 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n", 633 disk->disk_name)); 634 635 if (!sdp->removable) 636 return 0; 637 638 /* 639 * If the device is offline, don't send any commands - just pretend as 640 * if the command failed. If the device ever comes back online, we 641 * can deal with it then. It is only because of unrecoverable errors 642 * that we would ever take a device offline in the first place. 643 */ 644 if (!scsi_device_online(sdp)) 645 goto not_present; 646 647 /* 648 * Using TEST_UNIT_READY enables differentiation between drive with 649 * no cartridge loaded - NOT READY, drive with changed cartridge - 650 * UNIT ATTENTION, or with same cartridge - GOOD STATUS. 651 * 652 * Drives that auto spin down. eg iomega jaz 1G, will be started 653 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever 654 * sd_revalidate() is called. 655 */ 656 retval = -ENODEV; 657 if (scsi_block_when_processing_errors(sdp)) 658 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES); 659 660 /* 661 * Unable to test, unit probably not ready. This usually 662 * means there is no disc in the drive. Mark as changed, 663 * and we will figure it out later once the drive is 664 * available again. 665 */ 666 if (retval) 667 goto not_present; 668 669 /* 670 * For removable scsi disk we have to recognise the presence 671 * of a disk in the drive. This is kept in the struct scsi_disk 672 * struct and tested at open ! Daniel Roche (dan@lectra.fr) 673 */ 674 sdkp->media_present = 1; 675 676 retval = sdp->changed; 677 sdp->changed = 0; 678 679 return retval; 680 681 not_present: 682 set_media_not_present(sdkp); 683 return 1; 684 } 685 686 static int sd_sync_cache(struct scsi_device *sdp) 687 { 688 struct scsi_request *sreq; 689 int retries, res; 690 691 if (!scsi_device_online(sdp)) 692 return -ENODEV; 693 694 sreq = scsi_allocate_request(sdp, GFP_KERNEL); 695 if (!sreq) { 696 printk("FAILED\n No memory for request\n"); 697 return -ENOMEM; 698 } 699 700 sreq->sr_data_direction = DMA_NONE; 701 for (retries = 3; retries > 0; --retries) { 702 unsigned char cmd[10] = { 0 }; 703 704 cmd[0] = SYNCHRONIZE_CACHE; 705 /* 706 * Leave the rest of the command zero to indicate 707 * flush everything. 708 */ 709 scsi_wait_req(sreq, cmd, NULL, 0, SD_TIMEOUT, SD_MAX_RETRIES); 710 if (sreq->sr_result == 0) 711 break; 712 } 713 714 res = sreq->sr_result; 715 if (res) { 716 printk(KERN_WARNING "FAILED\n status = %x, message = %02x, " 717 "host = %d, driver = %02x\n ", 718 status_byte(res), msg_byte(res), 719 host_byte(res), driver_byte(res)); 720 if (driver_byte(res) & DRIVER_SENSE) 721 scsi_print_req_sense("sd", sreq); 722 } 723 724 scsi_release_request(sreq); 725 return res; 726 } 727 728 static int sd_issue_flush(struct device *dev, sector_t *error_sector) 729 { 730 struct scsi_device *sdp = to_scsi_device(dev); 731 struct scsi_disk *sdkp = dev_get_drvdata(dev); 732 733 if (!sdkp) 734 return -ENODEV; 735 736 if (!sdkp->WCE) 737 return 0; 738 739 return sd_sync_cache(sdp); 740 } 741 742 static void sd_end_flush(request_queue_t *q, struct request *flush_rq) 743 { 744 struct request *rq = flush_rq->end_io_data; 745 struct scsi_cmnd *cmd = rq->special; 746 unsigned int bytes = rq->hard_nr_sectors << 9; 747 748 if (!flush_rq->errors) { 749 spin_unlock(q->queue_lock); 750 scsi_io_completion(cmd, bytes, 0); 751 spin_lock(q->queue_lock); 752 } else if (blk_barrier_postflush(rq)) { 753 spin_unlock(q->queue_lock); 754 scsi_io_completion(cmd, 0, bytes); 755 spin_lock(q->queue_lock); 756 } else { 757 /* 758 * force journal abort of barriers 759 */ 760 end_that_request_first(rq, -EOPNOTSUPP, rq->hard_nr_sectors); 761 end_that_request_last(rq); 762 } 763 } 764 765 static int sd_prepare_flush(request_queue_t *q, struct request *rq) 766 { 767 struct scsi_device *sdev = q->queuedata; 768 struct scsi_disk *sdkp = dev_get_drvdata(&sdev->sdev_gendev); 769 770 if (sdkp->WCE) { 771 memset(rq->cmd, 0, sizeof(rq->cmd)); 772 rq->flags |= REQ_BLOCK_PC | REQ_SOFTBARRIER; 773 rq->timeout = SD_TIMEOUT; 774 rq->cmd[0] = SYNCHRONIZE_CACHE; 775 return 1; 776 } 777 778 return 0; 779 } 780 781 static void sd_rescan(struct device *dev) 782 { 783 struct scsi_disk *sdkp = dev_get_drvdata(dev); 784 sd_revalidate_disk(sdkp->disk); 785 } 786 787 788 #ifdef CONFIG_COMPAT 789 /* 790 * This gets directly called from VFS. When the ioctl 791 * is not recognized we go back to the other translation paths. 792 */ 793 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 794 { 795 struct block_device *bdev = file->f_dentry->d_inode->i_bdev; 796 struct gendisk *disk = bdev->bd_disk; 797 struct scsi_device *sdev = scsi_disk(disk)->device; 798 799 /* 800 * If we are in the middle of error recovery, don't let anyone 801 * else try and use this device. Also, if error recovery fails, it 802 * may try and take the device offline, in which case all further 803 * access to the device is prohibited. 804 */ 805 if (!scsi_block_when_processing_errors(sdev)) 806 return -ENODEV; 807 808 if (sdev->host->hostt->compat_ioctl) { 809 int ret; 810 811 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); 812 813 return ret; 814 } 815 816 /* 817 * Let the static ioctl translation table take care of it. 818 */ 819 return -ENOIOCTLCMD; 820 } 821 #endif 822 823 static struct block_device_operations sd_fops = { 824 .owner = THIS_MODULE, 825 .open = sd_open, 826 .release = sd_release, 827 .ioctl = sd_ioctl, 828 #ifdef CONFIG_COMPAT 829 .compat_ioctl = sd_compat_ioctl, 830 #endif 831 .media_changed = sd_media_changed, 832 .revalidate_disk = sd_revalidate_disk, 833 }; 834 835 /** 836 * sd_rw_intr - bottom half handler: called when the lower level 837 * driver has completed (successfully or otherwise) a scsi command. 838 * @SCpnt: mid-level's per command structure. 839 * 840 * Note: potentially run from within an ISR. Must not block. 841 **/ 842 static void sd_rw_intr(struct scsi_cmnd * SCpnt) 843 { 844 int result = SCpnt->result; 845 int this_count = SCpnt->bufflen; 846 int good_bytes = (result == 0 ? this_count : 0); 847 sector_t block_sectors = 1; 848 u64 first_err_block; 849 sector_t error_sector; 850 struct scsi_sense_hdr sshdr; 851 int sense_valid = 0; 852 int sense_deferred = 0; 853 int info_valid; 854 855 if (result) { 856 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); 857 if (sense_valid) 858 sense_deferred = scsi_sense_is_deferred(&sshdr); 859 } 860 861 #ifdef CONFIG_SCSI_LOGGING 862 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n", 863 SCpnt->request->rq_disk->disk_name, result)); 864 if (sense_valid) { 865 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc," 866 "ascq]=%x,%x,%x,%x\n", sshdr.response_code, 867 sshdr.sense_key, sshdr.asc, sshdr.ascq)); 868 } 869 #endif 870 /* 871 Handle MEDIUM ERRORs that indicate partial success. Since this is a 872 relatively rare error condition, no care is taken to avoid 873 unnecessary additional work such as memcpy's that could be avoided. 874 */ 875 876 /* 877 * If SG_IO from block layer then set good_bytes to stop retries; 878 * else if errors, check them, and if necessary prepare for 879 * (partial) retries. 880 */ 881 if (blk_pc_request(SCpnt->request)) 882 good_bytes = this_count; 883 else if (driver_byte(result) != 0 && 884 sense_valid && !sense_deferred) { 885 switch (sshdr.sense_key) { 886 case MEDIUM_ERROR: 887 if (!blk_fs_request(SCpnt->request)) 888 break; 889 info_valid = scsi_get_sense_info_fld( 890 SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE, 891 &first_err_block); 892 /* 893 * May want to warn and skip if following cast results 894 * in actual truncation (if sector_t < 64 bits) 895 */ 896 error_sector = (sector_t)first_err_block; 897 if (SCpnt->request->bio != NULL) 898 block_sectors = bio_sectors(SCpnt->request->bio); 899 switch (SCpnt->device->sector_size) { 900 case 1024: 901 error_sector <<= 1; 902 if (block_sectors < 2) 903 block_sectors = 2; 904 break; 905 case 2048: 906 error_sector <<= 2; 907 if (block_sectors < 4) 908 block_sectors = 4; 909 break; 910 case 4096: 911 error_sector <<=3; 912 if (block_sectors < 8) 913 block_sectors = 8; 914 break; 915 case 256: 916 error_sector >>= 1; 917 break; 918 default: 919 break; 920 } 921 922 error_sector &= ~(block_sectors - 1); 923 good_bytes = (error_sector - SCpnt->request->sector) << 9; 924 if (good_bytes < 0 || good_bytes >= this_count) 925 good_bytes = 0; 926 break; 927 928 case RECOVERED_ERROR: /* an error occurred, but it recovered */ 929 case NO_SENSE: /* LLDD got sense data */ 930 /* 931 * Inform the user, but make sure that it's not treated 932 * as a hard error. 933 */ 934 scsi_print_sense("sd", SCpnt); 935 SCpnt->result = 0; 936 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 937 good_bytes = this_count; 938 break; 939 940 case ILLEGAL_REQUEST: 941 if (SCpnt->device->use_10_for_rw && 942 (SCpnt->cmnd[0] == READ_10 || 943 SCpnt->cmnd[0] == WRITE_10)) 944 SCpnt->device->use_10_for_rw = 0; 945 if (SCpnt->device->use_10_for_ms && 946 (SCpnt->cmnd[0] == MODE_SENSE_10 || 947 SCpnt->cmnd[0] == MODE_SELECT_10)) 948 SCpnt->device->use_10_for_ms = 0; 949 break; 950 951 default: 952 break; 953 } 954 } 955 /* 956 * This calls the generic completion function, now that we know 957 * how many actual sectors finished, and how many sectors we need 958 * to say have failed. 959 */ 960 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9); 961 } 962 963 static int media_not_present(struct scsi_disk *sdkp, struct scsi_request *srp) 964 { 965 struct scsi_sense_hdr sshdr; 966 967 if (!srp->sr_result) 968 return 0; 969 if (!(driver_byte(srp->sr_result) & DRIVER_SENSE)) 970 return 0; 971 /* not invoked for commands that could return deferred errors */ 972 if (scsi_request_normalize_sense(srp, &sshdr)) { 973 if (sshdr.sense_key != NOT_READY && 974 sshdr.sense_key != UNIT_ATTENTION) 975 return 0; 976 if (sshdr.asc != 0x3A) /* medium not present */ 977 return 0; 978 } 979 set_media_not_present(sdkp); 980 return 1; 981 } 982 983 /* 984 * spinup disk - called only in sd_revalidate_disk() 985 */ 986 static void 987 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname, 988 struct scsi_request *SRpnt, unsigned char *buffer) { 989 unsigned char cmd[10]; 990 unsigned long spintime_value = 0; 991 int retries, spintime; 992 unsigned int the_result; 993 struct scsi_sense_hdr sshdr; 994 int sense_valid = 0; 995 996 spintime = 0; 997 998 /* Spin up drives, as required. Only do this at boot time */ 999 /* Spinup needs to be done for module loads too. */ 1000 do { 1001 retries = 0; 1002 1003 do { 1004 cmd[0] = TEST_UNIT_READY; 1005 memset((void *) &cmd[1], 0, 9); 1006 1007 SRpnt->sr_cmd_len = 0; 1008 memset(SRpnt->sr_sense_buffer, 0, 1009 SCSI_SENSE_BUFFERSIZE); 1010 SRpnt->sr_data_direction = DMA_NONE; 1011 1012 scsi_wait_req (SRpnt, (void *) cmd, (void *) buffer, 1013 0/*512*/, SD_TIMEOUT, SD_MAX_RETRIES); 1014 1015 the_result = SRpnt->sr_result; 1016 if (the_result) 1017 sense_valid = scsi_request_normalize_sense( 1018 SRpnt, &sshdr); 1019 retries++; 1020 } while (retries < 3 && 1021 (!scsi_status_is_good(the_result) || 1022 ((driver_byte(the_result) & DRIVER_SENSE) && 1023 sense_valid && sshdr.sense_key == UNIT_ATTENTION))); 1024 1025 /* 1026 * If the drive has indicated to us that it doesn't have 1027 * any media in it, don't bother with any of the rest of 1028 * this crap. 1029 */ 1030 if (media_not_present(sdkp, SRpnt)) 1031 return; 1032 1033 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) { 1034 /* no sense, TUR either succeeded or failed 1035 * with a status error */ 1036 if(!spintime && !scsi_status_is_good(the_result)) 1037 printk(KERN_NOTICE "%s: Unit Not Ready, " 1038 "error = 0x%x\n", diskname, the_result); 1039 break; 1040 } 1041 1042 /* 1043 * The device does not want the automatic start to be issued. 1044 */ 1045 if (sdkp->device->no_start_on_add) { 1046 break; 1047 } 1048 1049 /* 1050 * If manual intervention is required, or this is an 1051 * absent USB storage device, a spinup is meaningless. 1052 */ 1053 if (sense_valid && 1054 sshdr.sense_key == NOT_READY && 1055 sshdr.asc == 4 && sshdr.ascq == 3) { 1056 break; /* manual intervention required */ 1057 1058 /* 1059 * Issue command to spin up drive when not ready 1060 */ 1061 } else if (sense_valid && sshdr.sense_key == NOT_READY) { 1062 if (!spintime) { 1063 printk(KERN_NOTICE "%s: Spinning up disk...", 1064 diskname); 1065 cmd[0] = START_STOP; 1066 cmd[1] = 1; /* Return immediately */ 1067 memset((void *) &cmd[2], 0, 8); 1068 cmd[4] = 1; /* Start spin cycle */ 1069 SRpnt->sr_cmd_len = 0; 1070 memset(SRpnt->sr_sense_buffer, 0, 1071 SCSI_SENSE_BUFFERSIZE); 1072 1073 SRpnt->sr_data_direction = DMA_NONE; 1074 scsi_wait_req(SRpnt, (void *)cmd, 1075 (void *) buffer, 0/*512*/, 1076 SD_TIMEOUT, SD_MAX_RETRIES); 1077 spintime_value = jiffies; 1078 } 1079 spintime = 1; 1080 /* Wait 1 second for next try */ 1081 msleep(1000); 1082 printk("."); 1083 } else { 1084 /* we don't understand the sense code, so it's 1085 * probably pointless to loop */ 1086 if(!spintime) { 1087 printk(KERN_NOTICE "%s: Unit Not Ready, " 1088 "sense:\n", diskname); 1089 scsi_print_req_sense("", SRpnt); 1090 } 1091 break; 1092 } 1093 1094 } while (spintime && 1095 time_after(spintime_value + 100 * HZ, jiffies)); 1096 1097 if (spintime) { 1098 if (scsi_status_is_good(the_result)) 1099 printk("ready\n"); 1100 else 1101 printk("not responding...\n"); 1102 } 1103 } 1104 1105 /* 1106 * read disk capacity 1107 */ 1108 static void 1109 sd_read_capacity(struct scsi_disk *sdkp, char *diskname, 1110 struct scsi_request *SRpnt, unsigned char *buffer) { 1111 unsigned char cmd[16]; 1112 struct scsi_device *sdp = sdkp->device; 1113 int the_result, retries; 1114 int sector_size = 0; 1115 int longrc = 0; 1116 struct scsi_sense_hdr sshdr; 1117 int sense_valid = 0; 1118 1119 repeat: 1120 retries = 3; 1121 do { 1122 if (longrc) { 1123 memset((void *) cmd, 0, 16); 1124 cmd[0] = SERVICE_ACTION_IN; 1125 cmd[1] = SAI_READ_CAPACITY_16; 1126 cmd[13] = 12; 1127 memset((void *) buffer, 0, 12); 1128 } else { 1129 cmd[0] = READ_CAPACITY; 1130 memset((void *) &cmd[1], 0, 9); 1131 memset((void *) buffer, 0, 8); 1132 } 1133 1134 SRpnt->sr_cmd_len = 0; 1135 memset(SRpnt->sr_sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 1136 SRpnt->sr_data_direction = DMA_FROM_DEVICE; 1137 1138 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer, 1139 longrc ? 12 : 8, SD_TIMEOUT, SD_MAX_RETRIES); 1140 1141 if (media_not_present(sdkp, SRpnt)) 1142 return; 1143 1144 the_result = SRpnt->sr_result; 1145 if (the_result) 1146 sense_valid = scsi_request_normalize_sense(SRpnt, 1147 &sshdr); 1148 retries--; 1149 1150 } while (the_result && retries); 1151 1152 if (the_result && !longrc) { 1153 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n" 1154 "%s : status=%x, message=%02x, host=%d, driver=%02x \n", 1155 diskname, diskname, 1156 status_byte(the_result), 1157 msg_byte(the_result), 1158 host_byte(the_result), 1159 driver_byte(the_result)); 1160 1161 if (driver_byte(the_result) & DRIVER_SENSE) 1162 scsi_print_req_sense("sd", SRpnt); 1163 else 1164 printk("%s : sense not available. \n", diskname); 1165 1166 /* Set dirty bit for removable devices if not ready - 1167 * sometimes drives will not report this properly. */ 1168 if (sdp->removable && 1169 sense_valid && sshdr.sense_key == NOT_READY) 1170 sdp->changed = 1; 1171 1172 /* Either no media are present but the drive didn't tell us, 1173 or they are present but the read capacity command fails */ 1174 /* sdkp->media_present = 0; -- not always correct */ 1175 sdkp->capacity = 0x200000; /* 1 GB - random */ 1176 1177 return; 1178 } else if (the_result && longrc) { 1179 /* READ CAPACITY(16) has been failed */ 1180 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n" 1181 "%s : status=%x, message=%02x, host=%d, driver=%02x \n", 1182 diskname, diskname, 1183 status_byte(the_result), 1184 msg_byte(the_result), 1185 host_byte(the_result), 1186 driver_byte(the_result)); 1187 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n", 1188 diskname); 1189 1190 sdkp->capacity = 1 + (sector_t) 0xffffffff; 1191 goto got_data; 1192 } 1193 1194 if (!longrc) { 1195 sector_size = (buffer[4] << 24) | 1196 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; 1197 if (buffer[0] == 0xff && buffer[1] == 0xff && 1198 buffer[2] == 0xff && buffer[3] == 0xff) { 1199 if(sizeof(sdkp->capacity) > 4) { 1200 printk(KERN_NOTICE "%s : very big device. try to use" 1201 " READ CAPACITY(16).\n", diskname); 1202 longrc = 1; 1203 goto repeat; 1204 } 1205 printk(KERN_ERR "%s: too big for this kernel. Use a " 1206 "kernel compiled with support for large block " 1207 "devices.\n", diskname); 1208 sdkp->capacity = 0; 1209 goto got_data; 1210 } 1211 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) | 1212 (buffer[1] << 16) | 1213 (buffer[2] << 8) | 1214 buffer[3]); 1215 } else { 1216 sdkp->capacity = 1 + (((u64)buffer[0] << 56) | 1217 ((u64)buffer[1] << 48) | 1218 ((u64)buffer[2] << 40) | 1219 ((u64)buffer[3] << 32) | 1220 ((sector_t)buffer[4] << 24) | 1221 ((sector_t)buffer[5] << 16) | 1222 ((sector_t)buffer[6] << 8) | 1223 (sector_t)buffer[7]); 1224 1225 sector_size = (buffer[8] << 24) | 1226 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; 1227 } 1228 1229 /* Some devices return the total number of sectors, not the 1230 * highest sector number. Make the necessary adjustment. */ 1231 if (sdp->fix_capacity) 1232 --sdkp->capacity; 1233 1234 got_data: 1235 if (sector_size == 0) { 1236 sector_size = 512; 1237 printk(KERN_NOTICE "%s : sector size 0 reported, " 1238 "assuming 512.\n", diskname); 1239 } 1240 1241 if (sector_size != 512 && 1242 sector_size != 1024 && 1243 sector_size != 2048 && 1244 sector_size != 4096 && 1245 sector_size != 256) { 1246 printk(KERN_NOTICE "%s : unsupported sector size " 1247 "%d.\n", diskname, sector_size); 1248 /* 1249 * The user might want to re-format the drive with 1250 * a supported sectorsize. Once this happens, it 1251 * would be relatively trivial to set the thing up. 1252 * For this reason, we leave the thing in the table. 1253 */ 1254 sdkp->capacity = 0; 1255 /* 1256 * set a bogus sector size so the normal read/write 1257 * logic in the block layer will eventually refuse any 1258 * request on this device without tripping over power 1259 * of two sector size assumptions 1260 */ 1261 sector_size = 512; 1262 } 1263 { 1264 /* 1265 * The msdos fs needs to know the hardware sector size 1266 * So I have created this table. See ll_rw_blk.c 1267 * Jacques Gelinas (Jacques@solucorp.qc.ca) 1268 */ 1269 int hard_sector = sector_size; 1270 sector_t sz = sdkp->capacity * (hard_sector/256); 1271 request_queue_t *queue = sdp->request_queue; 1272 sector_t mb; 1273 1274 blk_queue_hardsect_size(queue, hard_sector); 1275 /* avoid 64-bit division on 32-bit platforms */ 1276 mb = sz >> 1; 1277 sector_div(sz, 1250); 1278 mb -= sz - 974; 1279 sector_div(mb, 1950); 1280 1281 printk(KERN_NOTICE "SCSI device %s: " 1282 "%llu %d-byte hdwr sectors (%llu MB)\n", 1283 diskname, (unsigned long long)sdkp->capacity, 1284 hard_sector, (unsigned long long)mb); 1285 } 1286 1287 /* Rescale capacity to 512-byte units */ 1288 if (sector_size == 4096) 1289 sdkp->capacity <<= 3; 1290 else if (sector_size == 2048) 1291 sdkp->capacity <<= 2; 1292 else if (sector_size == 1024) 1293 sdkp->capacity <<= 1; 1294 else if (sector_size == 256) 1295 sdkp->capacity >>= 1; 1296 1297 sdkp->device->sector_size = sector_size; 1298 } 1299 1300 /* called with buffer of length 512 */ 1301 static inline int 1302 sd_do_mode_sense(struct scsi_request *SRpnt, int dbd, int modepage, 1303 unsigned char *buffer, int len, struct scsi_mode_data *data) 1304 { 1305 return __scsi_mode_sense(SRpnt, dbd, modepage, buffer, len, 1306 SD_TIMEOUT, SD_MAX_RETRIES, data); 1307 } 1308 1309 /* 1310 * read write protect setting, if possible - called only in sd_revalidate_disk() 1311 * called with buffer of length 512 1312 */ 1313 static void 1314 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname, 1315 struct scsi_request *SRpnt, unsigned char *buffer) { 1316 int res; 1317 struct scsi_mode_data data; 1318 1319 set_disk_ro(sdkp->disk, 0); 1320 if (sdkp->device->skip_ms_page_3f) { 1321 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname); 1322 return; 1323 } 1324 1325 if (sdkp->device->use_192_bytes_for_3f) { 1326 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 192, &data); 1327 } else { 1328 /* 1329 * First attempt: ask for all pages (0x3F), but only 4 bytes. 1330 * We have to start carefully: some devices hang if we ask 1331 * for more than is available. 1332 */ 1333 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 4, &data); 1334 1335 /* 1336 * Second attempt: ask for page 0 When only page 0 is 1337 * implemented, a request for page 3F may return Sense Key 1338 * 5: Illegal Request, Sense Code 24: Invalid field in 1339 * CDB. 1340 */ 1341 if (!scsi_status_is_good(res)) 1342 res = sd_do_mode_sense(SRpnt, 0, 0, buffer, 4, &data); 1343 1344 /* 1345 * Third attempt: ask 255 bytes, as we did earlier. 1346 */ 1347 if (!scsi_status_is_good(res)) 1348 res = sd_do_mode_sense(SRpnt, 0, 0x3F, buffer, 255, 1349 &data); 1350 } 1351 1352 if (!scsi_status_is_good(res)) { 1353 printk(KERN_WARNING 1354 "%s: test WP failed, assume Write Enabled\n", diskname); 1355 } else { 1356 sdkp->write_prot = ((data.device_specific & 0x80) != 0); 1357 set_disk_ro(sdkp->disk, sdkp->write_prot); 1358 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname, 1359 sdkp->write_prot ? "on" : "off"); 1360 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n", 1361 diskname, buffer[0], buffer[1], buffer[2], buffer[3]); 1362 } 1363 } 1364 1365 /* 1366 * sd_read_cache_type - called only from sd_revalidate_disk() 1367 * called with buffer of length 512 1368 */ 1369 static void 1370 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname, 1371 struct scsi_request *SRpnt, unsigned char *buffer) { 1372 int len = 0, res; 1373 1374 const int dbd = 0; /* DBD */ 1375 const int modepage = 0x08; /* current values, cache page */ 1376 struct scsi_mode_data data; 1377 struct scsi_sense_hdr sshdr; 1378 1379 if (sdkp->device->skip_ms_page_8) 1380 goto defaults; 1381 1382 /* cautiously ask */ 1383 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, 4, &data); 1384 1385 if (!scsi_status_is_good(res)) 1386 goto bad_sense; 1387 1388 /* that went OK, now ask for the proper length */ 1389 len = data.length; 1390 1391 /* 1392 * We're only interested in the first three bytes, actually. 1393 * But the data cache page is defined for the first 20. 1394 */ 1395 if (len < 3) 1396 goto bad_sense; 1397 if (len > 20) 1398 len = 20; 1399 1400 /* Take headers and block descriptors into account */ 1401 len += data.header_length + data.block_descriptor_length; 1402 1403 /* Get the data */ 1404 res = sd_do_mode_sense(SRpnt, dbd, modepage, buffer, len, &data); 1405 1406 if (scsi_status_is_good(res)) { 1407 const char *types[] = { 1408 "write through", "none", "write back", 1409 "write back, no read (daft)" 1410 }; 1411 int ct = 0; 1412 int offset = data.header_length + 1413 data.block_descriptor_length + 2; 1414 1415 sdkp->WCE = ((buffer[offset] & 0x04) != 0); 1416 sdkp->RCD = ((buffer[offset] & 0x01) != 0); 1417 1418 ct = sdkp->RCD + 2*sdkp->WCE; 1419 1420 printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n", 1421 diskname, types[ct]); 1422 1423 return; 1424 } 1425 1426 bad_sense: 1427 if (scsi_request_normalize_sense(SRpnt, &sshdr) && 1428 sshdr.sense_key == ILLEGAL_REQUEST && 1429 sshdr.asc == 0x24 && sshdr.ascq == 0x0) 1430 printk(KERN_NOTICE "%s: cache data unavailable\n", 1431 diskname); /* Invalid field in CDB */ 1432 else 1433 printk(KERN_ERR "%s: asking for cache data failed\n", 1434 diskname); 1435 1436 defaults: 1437 printk(KERN_ERR "%s: assuming drive cache: write through\n", 1438 diskname); 1439 sdkp->WCE = 0; 1440 sdkp->RCD = 0; 1441 } 1442 1443 /** 1444 * sd_revalidate_disk - called the first time a new disk is seen, 1445 * performs disk spin up, read_capacity, etc. 1446 * @disk: struct gendisk we care about 1447 **/ 1448 static int sd_revalidate_disk(struct gendisk *disk) 1449 { 1450 struct scsi_disk *sdkp = scsi_disk(disk); 1451 struct scsi_device *sdp = sdkp->device; 1452 struct scsi_request *sreq; 1453 unsigned char *buffer; 1454 1455 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name)); 1456 1457 /* 1458 * If the device is offline, don't try and read capacity or any 1459 * of the other niceties. 1460 */ 1461 if (!scsi_device_online(sdp)) 1462 goto out; 1463 1464 sreq = scsi_allocate_request(sdp, GFP_KERNEL); 1465 if (!sreq) { 1466 printk(KERN_WARNING "(sd_revalidate_disk:) Request allocation " 1467 "failure.\n"); 1468 goto out; 1469 } 1470 1471 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA); 1472 if (!buffer) { 1473 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation " 1474 "failure.\n"); 1475 goto out_release_request; 1476 } 1477 1478 /* defaults, until the device tells us otherwise */ 1479 sdp->sector_size = 512; 1480 sdkp->capacity = 0; 1481 sdkp->media_present = 1; 1482 sdkp->write_prot = 0; 1483 sdkp->WCE = 0; 1484 sdkp->RCD = 0; 1485 1486 sd_spinup_disk(sdkp, disk->disk_name, sreq, buffer); 1487 1488 /* 1489 * Without media there is no reason to ask; moreover, some devices 1490 * react badly if we do. 1491 */ 1492 if (sdkp->media_present) { 1493 sd_read_capacity(sdkp, disk->disk_name, sreq, buffer); 1494 if (sdp->removable) 1495 sd_read_write_protect_flag(sdkp, disk->disk_name, 1496 sreq, buffer); 1497 sd_read_cache_type(sdkp, disk->disk_name, sreq, buffer); 1498 } 1499 1500 set_capacity(disk, sdkp->capacity); 1501 kfree(buffer); 1502 1503 out_release_request: 1504 scsi_release_request(sreq); 1505 out: 1506 return 0; 1507 } 1508 1509 /** 1510 * sd_probe - called during driver initialization and whenever a 1511 * new scsi device is attached to the system. It is called once 1512 * for each scsi device (not just disks) present. 1513 * @dev: pointer to device object 1514 * 1515 * Returns 0 if successful (or not interested in this scsi device 1516 * (e.g. scanner)); 1 when there is an error. 1517 * 1518 * Note: this function is invoked from the scsi mid-level. 1519 * This function sets up the mapping between a given 1520 * <host,channel,id,lun> (found in sdp) and new device name 1521 * (e.g. /dev/sda). More precisely it is the block device major 1522 * and minor number that is chosen here. 1523 * 1524 * Assume sd_attach is not re-entrant (for time being) 1525 * Also think about sd_attach() and sd_remove() running coincidentally. 1526 **/ 1527 static int sd_probe(struct device *dev) 1528 { 1529 struct scsi_device *sdp = to_scsi_device(dev); 1530 struct scsi_disk *sdkp; 1531 struct gendisk *gd; 1532 u32 index; 1533 int error; 1534 1535 error = -ENODEV; 1536 if ((sdp->type != TYPE_DISK) && (sdp->type != TYPE_MOD)) 1537 goto out; 1538 1539 SCSI_LOG_HLQUEUE(3, printk("sd_attach: scsi device: <%d,%d,%d,%d>\n", 1540 sdp->host->host_no, sdp->channel, sdp->id, sdp->lun)); 1541 1542 error = -ENOMEM; 1543 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL); 1544 if (!sdkp) 1545 goto out; 1546 1547 memset (sdkp, 0, sizeof(*sdkp)); 1548 kref_init(&sdkp->kref); 1549 1550 gd = alloc_disk(16); 1551 if (!gd) 1552 goto out_free; 1553 1554 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL)) 1555 goto out_put; 1556 1557 spin_lock(&sd_index_lock); 1558 error = idr_get_new(&sd_index_idr, NULL, &index); 1559 spin_unlock(&sd_index_lock); 1560 1561 if (index >= SD_MAX_DISKS) 1562 error = -EBUSY; 1563 if (error) 1564 goto out_put; 1565 1566 sdkp->device = sdp; 1567 sdkp->driver = &sd_template; 1568 sdkp->disk = gd; 1569 sdkp->index = index; 1570 sdkp->openers = 0; 1571 1572 if (!sdp->timeout) { 1573 if (sdp->type == TYPE_DISK) 1574 sdp->timeout = SD_TIMEOUT; 1575 else 1576 sdp->timeout = SD_MOD_TIMEOUT; 1577 } 1578 1579 gd->major = sd_major((index & 0xf0) >> 4); 1580 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); 1581 gd->minors = 16; 1582 gd->fops = &sd_fops; 1583 1584 if (index < 26) { 1585 sprintf(gd->disk_name, "sd%c", 'a' + index % 26); 1586 } else if (index < (26 + 1) * 26) { 1587 sprintf(gd->disk_name, "sd%c%c", 1588 'a' + index / 26 - 1,'a' + index % 26); 1589 } else { 1590 const unsigned int m1 = (index / 26 - 1) / 26 - 1; 1591 const unsigned int m2 = (index / 26 - 1) % 26; 1592 const unsigned int m3 = index % 26; 1593 sprintf(gd->disk_name, "sd%c%c%c", 1594 'a' + m1, 'a' + m2, 'a' + m3); 1595 } 1596 1597 strcpy(gd->devfs_name, sdp->devfs_name); 1598 1599 gd->private_data = &sdkp->driver; 1600 1601 sd_revalidate_disk(gd); 1602 1603 gd->driverfs_dev = &sdp->sdev_gendev; 1604 gd->flags = GENHD_FL_DRIVERFS; 1605 if (sdp->removable) 1606 gd->flags |= GENHD_FL_REMOVABLE; 1607 gd->queue = sdkp->device->request_queue; 1608 1609 dev_set_drvdata(dev, sdkp); 1610 add_disk(gd); 1611 1612 printk(KERN_NOTICE "Attached scsi %sdisk %s at scsi%d, channel %d, " 1613 "id %d, lun %d\n", sdp->removable ? "removable " : "", 1614 gd->disk_name, sdp->host->host_no, sdp->channel, 1615 sdp->id, sdp->lun); 1616 1617 return 0; 1618 1619 out_put: 1620 put_disk(gd); 1621 out_free: 1622 kfree(sdkp); 1623 out: 1624 return error; 1625 } 1626 1627 /** 1628 * sd_remove - called whenever a scsi disk (previously recognized by 1629 * sd_probe) is detached from the system. It is called (potentially 1630 * multiple times) during sd module unload. 1631 * @sdp: pointer to mid level scsi device object 1632 * 1633 * Note: this function is invoked from the scsi mid-level. 1634 * This function potentially frees up a device name (e.g. /dev/sdc) 1635 * that could be re-used by a subsequent sd_probe(). 1636 * This function is not called when the built-in sd driver is "exit-ed". 1637 **/ 1638 static int sd_remove(struct device *dev) 1639 { 1640 struct scsi_disk *sdkp = dev_get_drvdata(dev); 1641 1642 del_gendisk(sdkp->disk); 1643 sd_shutdown(dev); 1644 down(&sd_ref_sem); 1645 kref_put(&sdkp->kref, scsi_disk_release); 1646 up(&sd_ref_sem); 1647 1648 return 0; 1649 } 1650 1651 /** 1652 * scsi_disk_release - Called to free the scsi_disk structure 1653 * @kref: pointer to embedded kref 1654 * 1655 * sd_ref_sem must be held entering this routine. Because it is 1656 * called on last put, you should always use the scsi_disk_get() 1657 * scsi_disk_put() helpers which manipulate the semaphore directly 1658 * and never do a direct kref_put(). 1659 **/ 1660 static void scsi_disk_release(struct kref *kref) 1661 { 1662 struct scsi_disk *sdkp = to_scsi_disk(kref); 1663 struct gendisk *disk = sdkp->disk; 1664 1665 spin_lock(&sd_index_lock); 1666 idr_remove(&sd_index_idr, sdkp->index); 1667 spin_unlock(&sd_index_lock); 1668 1669 disk->private_data = NULL; 1670 1671 put_disk(disk); 1672 1673 kfree(sdkp); 1674 } 1675 1676 /* 1677 * Send a SYNCHRONIZE CACHE instruction down to the device through 1678 * the normal SCSI command structure. Wait for the command to 1679 * complete. 1680 */ 1681 static void sd_shutdown(struct device *dev) 1682 { 1683 struct scsi_device *sdp = to_scsi_device(dev); 1684 struct scsi_disk *sdkp = dev_get_drvdata(dev); 1685 1686 if (!sdkp) 1687 return; /* this can happen */ 1688 1689 if (!sdkp->WCE) 1690 return; 1691 1692 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n", 1693 sdkp->disk->disk_name); 1694 sd_sync_cache(sdp); 1695 } 1696 1697 /** 1698 * init_sd - entry point for this driver (both when built in or when 1699 * a module). 1700 * 1701 * Note: this function registers this driver with the scsi mid-level. 1702 **/ 1703 static int __init init_sd(void) 1704 { 1705 int majors = 0, i; 1706 1707 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); 1708 1709 for (i = 0; i < SD_MAJORS; i++) 1710 if (register_blkdev(sd_major(i), "sd") == 0) 1711 majors++; 1712 1713 if (!majors) 1714 return -ENODEV; 1715 1716 return scsi_register_driver(&sd_template.gendrv); 1717 } 1718 1719 /** 1720 * exit_sd - exit point for this driver (when it is a module). 1721 * 1722 * Note: this function unregisters this driver from the scsi mid-level. 1723 **/ 1724 static void __exit exit_sd(void) 1725 { 1726 int i; 1727 1728 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); 1729 1730 scsi_unregister_driver(&sd_template.gendrv); 1731 for (i = 0; i < SD_MAJORS; i++) 1732 unregister_blkdev(sd_major(i), "sd"); 1733 } 1734 1735 MODULE_LICENSE("GPL"); 1736 MODULE_AUTHOR("Eric Youngdale"); 1737 MODULE_DESCRIPTION("SCSI disk (sd) driver"); 1738 1739 module_init(init_sd); 1740 module_exit(exit_sd); 1741