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