aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/sd.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/sd.c')
-rw-r--r--drivers/scsi/sd.c405
1 files changed, 225 insertions, 180 deletions
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 5a8f55fea5ff..00e46662296f 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -58,16 +58,10 @@
58#include <scsi/scsi_host.h> 58#include <scsi/scsi_host.h>
59#include <scsi/scsi_ioctl.h> 59#include <scsi/scsi_ioctl.h>
60#include <scsi/scsicam.h> 60#include <scsi/scsicam.h>
61#include <scsi/sd.h>
61 62
62#include "scsi_logging.h" 63#include "scsi_logging.h"
63 64
64/*
65 * More than enough for everybody ;) The huge number of majors
66 * is a leftover from 16bit dev_t days, we don't really need that
67 * much numberspace.
68 */
69#define SD_MAJORS 16
70
71MODULE_AUTHOR("Eric Youngdale"); 65MODULE_AUTHOR("Eric Youngdale");
72MODULE_DESCRIPTION("SCSI disk (sd) driver"); 66MODULE_DESCRIPTION("SCSI disk (sd) driver");
73MODULE_LICENSE("GPL"); 67MODULE_LICENSE("GPL");
@@ -88,45 +82,9 @@ MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
88MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR); 82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
89MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR); 83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
90MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR); 84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
91 85MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
92/* 86MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
93 * This is limited by the naming scheme enforced in sd_probe, 87MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
94 * add another character to it if you really need more disks.
95 */
96#define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
97
98/*
99 * Time out in seconds for disks and Magneto-opticals (which are slower).
100 */
101#define SD_TIMEOUT (30 * HZ)
102#define SD_MOD_TIMEOUT (75 * HZ)
103
104/*
105 * Number of allowed retries
106 */
107#define SD_MAX_RETRIES 5
108#define SD_PASSTHROUGH_RETRIES 1
109
110/*
111 * Size of the initial data buffer for mode and read capacity data
112 */
113#define SD_BUF_SIZE 512
114
115struct scsi_disk {
116 struct scsi_driver *driver; /* always &sd_template */
117 struct scsi_device *device;
118 struct class_device cdev;
119 struct gendisk *disk;
120 unsigned int openers; /* protected by BKL for now, yuck */
121 sector_t capacity; /* size in 512-byte sectors */
122 u32 index;
123 u8 media_present;
124 u8 write_prot;
125 unsigned WCE : 1; /* state of disk WCE bit */
126 unsigned RCD : 1; /* state of disk RCD bit, unused */
127 unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
128};
129#define to_scsi_disk(obj) container_of(obj,struct scsi_disk,cdev)
130 88
131static DEFINE_IDR(sd_index_idr); 89static DEFINE_IDR(sd_index_idr);
132static DEFINE_SPINLOCK(sd_index_lock); 90static DEFINE_SPINLOCK(sd_index_lock);
@@ -136,20 +94,6 @@ static DEFINE_SPINLOCK(sd_index_lock);
136 * object after last put) */ 94 * object after last put) */
137static DEFINE_MUTEX(sd_ref_mutex); 95static DEFINE_MUTEX(sd_ref_mutex);
138 96
139static int sd_revalidate_disk(struct gendisk *disk);
140static void sd_rw_intr(struct scsi_cmnd * SCpnt);
141
142static int sd_probe(struct device *);
143static int sd_remove(struct device *);
144static void sd_shutdown(struct device *dev);
145static void sd_rescan(struct device *);
146static int sd_init_command(struct scsi_cmnd *);
147static int sd_issue_flush(struct device *, sector_t *);
148static void sd_prepare_flush(request_queue_t *, struct request *);
149static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
150 unsigned char *buffer);
151static void scsi_disk_release(struct class_device *cdev);
152
153static const char *sd_cache_types[] = { 97static const char *sd_cache_types[] = {
154 "write through", "none", "write back", 98 "write through", "none", "write back",
155 "write back, no read (daft)" 99 "write back, no read (daft)"
@@ -199,13 +143,27 @@ static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
199 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT, 143 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
200 SD_MAX_RETRIES, &data, &sshdr)) { 144 SD_MAX_RETRIES, &data, &sshdr)) {
201 if (scsi_sense_valid(&sshdr)) 145 if (scsi_sense_valid(&sshdr))
202 scsi_print_sense_hdr(sdkp->disk->disk_name, &sshdr); 146 sd_print_sense_hdr(sdkp, &sshdr);
203 return -EINVAL; 147 return -EINVAL;
204 } 148 }
205 sd_revalidate_disk(sdkp->disk); 149 sd_revalidate_disk(sdkp->disk);
206 return count; 150 return count;
207} 151}
208 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
209static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf, 167static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
210 size_t count) 168 size_t count)
211{ 169{
@@ -238,6 +196,14 @@ static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
238 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA); 196 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
239} 197}
240 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
241static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf) 207static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
242{ 208{
243 struct scsi_disk *sdkp = to_scsi_disk(cdev); 209 struct scsi_disk *sdkp = to_scsi_disk(cdev);
@@ -251,6 +217,8 @@ static struct class_device_attribute sd_disk_attrs[] = {
251 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL), 217 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
252 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart, 218 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
253 sd_store_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),
254 __ATTR_NULL, 222 __ATTR_NULL,
255}; 223};
256 224
@@ -267,6 +235,8 @@ static struct scsi_driver sd_template = {
267 .name = "sd", 235 .name = "sd",
268 .probe = sd_probe, 236 .probe = sd_probe,
269 .remove = sd_remove, 237 .remove = sd_remove,
238 .suspend = sd_suspend,
239 .resume = sd_resume,
270 .shutdown = sd_shutdown, 240 .shutdown = sd_shutdown,
271 }, 241 },
272 .rescan = sd_rescan, 242 .rescan = sd_rescan,
@@ -371,15 +341,19 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
371 unsigned int this_count = SCpnt->request_bufflen >> 9; 341 unsigned int this_count = SCpnt->request_bufflen >> 9;
372 unsigned int timeout = sdp->timeout; 342 unsigned int timeout = sdp->timeout;
373 343
374 SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, " 344 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
375 "count=%d\n", disk->disk_name, 345 "sd_init_command: block=%llu, "
376 (unsigned long long)block, this_count)); 346 "count=%d\n",
347 (unsigned long long)block,
348 this_count));
377 349
378 if (!sdp || !scsi_device_online(sdp) || 350 if (!sdp || !scsi_device_online(sdp) ||
379 block + rq->nr_sectors > get_capacity(disk)) { 351 block + rq->nr_sectors > get_capacity(disk)) {
380 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", 352 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
381 rq->nr_sectors)); 353 "Finishing %ld sectors\n",
382 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); 354 rq->nr_sectors));
355 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
356 "Retry with 0x%p\n", SCpnt));
383 return 0; 357 return 0;
384 } 358 }
385 359
@@ -391,8 +365,8 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
391 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ 365 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
392 return 0; 366 return 0;
393 } 367 }
394 SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n", 368 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
395 disk->disk_name, (unsigned long long)block)); 369 (unsigned long long)block));
396 370
397 /* 371 /*
398 * If we have a 1K hardware sectorsize, prevent access to single 372 * If we have a 1K hardware sectorsize, prevent access to single
@@ -407,7 +381,8 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
407 */ 381 */
408 if (sdp->sector_size == 1024) { 382 if (sdp->sector_size == 1024) {
409 if ((block & 1) || (rq->nr_sectors & 1)) { 383 if ((block & 1) || (rq->nr_sectors & 1)) {
410 printk(KERN_ERR "sd: Bad block number requested"); 384 scmd_printk(KERN_ERR, SCpnt,
385 "Bad block number requested\n");
411 return 0; 386 return 0;
412 } else { 387 } else {
413 block = block >> 1; 388 block = block >> 1;
@@ -416,7 +391,8 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
416 } 391 }
417 if (sdp->sector_size == 2048) { 392 if (sdp->sector_size == 2048) {
418 if ((block & 3) || (rq->nr_sectors & 3)) { 393 if ((block & 3) || (rq->nr_sectors & 3)) {
419 printk(KERN_ERR "sd: Bad block number requested"); 394 scmd_printk(KERN_ERR, SCpnt,
395 "Bad block number requested\n");
420 return 0; 396 return 0;
421 } else { 397 } else {
422 block = block >> 2; 398 block = block >> 2;
@@ -425,7 +401,8 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
425 } 401 }
426 if (sdp->sector_size == 4096) { 402 if (sdp->sector_size == 4096) {
427 if ((block & 7) || (rq->nr_sectors & 7)) { 403 if ((block & 7) || (rq->nr_sectors & 7)) {
428 printk(KERN_ERR "sd: Bad block number requested"); 404 scmd_printk(KERN_ERR, SCpnt,
405 "Bad block number requested\n");
429 return 0; 406 return 0;
430 } else { 407 } else {
431 block = block >> 3; 408 block = block >> 3;
@@ -442,13 +419,15 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
442 SCpnt->cmnd[0] = READ_6; 419 SCpnt->cmnd[0] = READ_6;
443 SCpnt->sc_data_direction = DMA_FROM_DEVICE; 420 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
444 } else { 421 } else {
445 printk(KERN_ERR "sd: Unknown command %x\n", rq->cmd_flags); 422 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
446 return 0; 423 return 0;
447 } 424 }
448 425
449 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", 426 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
450 disk->disk_name, (rq_data_dir(rq) == WRITE) ? 427 "%s %d/%ld 512 byte blocks.\n",
451 "writing" : "reading", this_count, rq->nr_sectors)); 428 (rq_data_dir(rq) == WRITE) ?
429 "writing" : "reading", this_count,
430 rq->nr_sectors));
452 431
453 SCpnt->cmnd[1] = 0; 432 SCpnt->cmnd[1] = 0;
454 433
@@ -490,7 +469,8 @@ static int sd_init_command(struct scsi_cmnd * SCpnt)
490 * during operation and thus turned off 469 * during operation and thus turned off
491 * use_10_for_rw. 470 * use_10_for_rw.
492 */ 471 */
493 printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n"); 472 scmd_printk(KERN_ERR, SCpnt,
473 "FUA write on READ/WRITE(6) drive\n");
494 return 0; 474 return 0;
495 } 475 }
496 476
@@ -549,7 +529,7 @@ static int sd_open(struct inode *inode, struct file *filp)
549 return -ENXIO; 529 return -ENXIO;
550 530
551 531
552 SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name)); 532 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
553 533
554 sdev = sdkp->device; 534 sdev = sdkp->device;
555 535
@@ -619,7 +599,7 @@ static int sd_release(struct inode *inode, struct file *filp)
619 struct scsi_disk *sdkp = scsi_disk(disk); 599 struct scsi_disk *sdkp = scsi_disk(disk);
620 struct scsi_device *sdev = sdkp->device; 600 struct scsi_device *sdev = sdkp->device;
621 601
622 SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name)); 602 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
623 603
624 if (!--sdkp->openers && sdev->removable) { 604 if (!--sdkp->openers && sdev->removable) {
625 if (scsi_block_when_processing_errors(sdev)) 605 if (scsi_block_when_processing_errors(sdev))
@@ -732,8 +712,7 @@ static int sd_media_changed(struct gendisk *disk)
732 struct scsi_device *sdp = sdkp->device; 712 struct scsi_device *sdp = sdkp->device;
733 int retval; 713 int retval;
734 714
735 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n", 715 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
736 disk->disk_name));
737 716
738 if (!sdp->removable) 717 if (!sdp->removable)
739 return 0; 718 return 0;
@@ -786,9 +765,10 @@ not_present:
786 return 1; 765 return 1;
787} 766}
788 767
789static int sd_sync_cache(struct scsi_device *sdp) 768static int sd_sync_cache(struct scsi_disk *sdkp)
790{ 769{
791 int retries, res; 770 int retries, res;
771 struct scsi_device *sdp = sdkp->device;
792 struct scsi_sense_hdr sshdr; 772 struct scsi_sense_hdr sshdr;
793 773
794 if (!scsi_device_online(sdp)) 774 if (!scsi_device_online(sdp))
@@ -809,28 +789,27 @@ static int sd_sync_cache(struct scsi_device *sdp)
809 break; 789 break;
810 } 790 }
811 791
812 if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, " 792 if (res) {
813 "host = %d, driver = %02x\n ", 793 sd_print_result(sdkp, res);
814 status_byte(res), msg_byte(res), 794 if (driver_byte(res) & DRIVER_SENSE)
815 host_byte(res), driver_byte(res)); 795 sd_print_sense_hdr(sdkp, &sshdr);
816 if (driver_byte(res) & DRIVER_SENSE)
817 scsi_print_sense_hdr("sd", &sshdr);
818 } 796 }
819 797
820 return res; 798 if (res)
799 return -EIO;
800 return 0;
821} 801}
822 802
823static int sd_issue_flush(struct device *dev, sector_t *error_sector) 803static int sd_issue_flush(struct device *dev, sector_t *error_sector)
824{ 804{
825 int ret = 0; 805 int ret = 0;
826 struct scsi_device *sdp = to_scsi_device(dev);
827 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); 806 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
828 807
829 if (!sdkp) 808 if (!sdkp)
830 return -ENODEV; 809 return -ENODEV;
831 810
832 if (sdkp->WCE) 811 if (sdkp->WCE)
833 ret = sd_sync_cache(sdp); 812 ret = sd_sync_cache(sdkp);
834 scsi_disk_put(sdkp); 813 scsi_disk_put(sdkp);
835 return ret; 814 return ret;
836} 815}
@@ -928,12 +907,14 @@ static void sd_rw_intr(struct scsi_cmnd * SCpnt)
928 sense_deferred = scsi_sense_is_deferred(&sshdr); 907 sense_deferred = scsi_sense_is_deferred(&sshdr);
929 } 908 }
930#ifdef CONFIG_SCSI_LOGGING 909#ifdef CONFIG_SCSI_LOGGING
931 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n", 910 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
932 SCpnt->request->rq_disk->disk_name, result));
933 if (sense_valid) { 911 if (sense_valid) {
934 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc," 912 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
935 "ascq]=%x,%x,%x,%x\n", sshdr.response_code, 913 "sd_rw_intr: sb[respc,sk,asc,"
936 sshdr.sense_key, sshdr.asc, sshdr.ascq)); 914 "ascq]=%x,%x,%x,%x\n",
915 sshdr.response_code,
916 sshdr.sense_key, sshdr.asc,
917 sshdr.ascq));
937 } 918 }
938#endif 919#endif
939 if (driver_byte(result) != DRIVER_SENSE && 920 if (driver_byte(result) != DRIVER_SENSE &&
@@ -1025,7 +1006,7 @@ static int media_not_present(struct scsi_disk *sdkp,
1025 * spinup disk - called only in sd_revalidate_disk() 1006 * spinup disk - called only in sd_revalidate_disk()
1026 */ 1007 */
1027static void 1008static void
1028sd_spinup_disk(struct scsi_disk *sdkp, char *diskname) 1009sd_spinup_disk(struct scsi_disk *sdkp)
1029{ 1010{
1030 unsigned char cmd[10]; 1011 unsigned char cmd[10];
1031 unsigned long spintime_expire = 0; 1012 unsigned long spintime_expire = 0;
@@ -1069,9 +1050,10 @@ sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
1069 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) { 1050 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1070 /* no sense, TUR either succeeded or failed 1051 /* no sense, TUR either succeeded or failed
1071 * with a status error */ 1052 * with a status error */
1072 if(!spintime && !scsi_status_is_good(the_result)) 1053 if(!spintime && !scsi_status_is_good(the_result)) {
1073 printk(KERN_NOTICE "%s: Unit Not Ready, " 1054 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1074 "error = 0x%x\n", diskname, the_result); 1055 sd_print_result(sdkp, the_result);
1056 }
1075 break; 1057 break;
1076 } 1058 }
1077 1059
@@ -1096,8 +1078,7 @@ sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
1096 */ 1078 */
1097 } else if (sense_valid && sshdr.sense_key == NOT_READY) { 1079 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1098 if (!spintime) { 1080 if (!spintime) {
1099 printk(KERN_NOTICE "%s: Spinning up disk...", 1081 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1100 diskname);
1101 cmd[0] = START_STOP; 1082 cmd[0] = START_STOP;
1102 cmd[1] = 1; /* Return immediately */ 1083 cmd[1] = 1; /* Return immediately */
1103 memset((void *) &cmd[2], 0, 8); 1084 memset((void *) &cmd[2], 0, 8);
@@ -1130,9 +1111,8 @@ sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
1130 /* we don't understand the sense code, so it's 1111 /* we don't understand the sense code, so it's
1131 * probably pointless to loop */ 1112 * probably pointless to loop */
1132 if(!spintime) { 1113 if(!spintime) {
1133 printk(KERN_NOTICE "%s: Unit Not Ready, " 1114 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1134 "sense:\n", diskname); 1115 sd_print_sense_hdr(sdkp, &sshdr);
1135 scsi_print_sense_hdr("", &sshdr);
1136 } 1116 }
1137 break; 1117 break;
1138 } 1118 }
@@ -1151,8 +1131,7 @@ sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
1151 * read disk capacity 1131 * read disk capacity
1152 */ 1132 */
1153static void 1133static void
1154sd_read_capacity(struct scsi_disk *sdkp, char *diskname, 1134sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1155 unsigned char *buffer)
1156{ 1135{
1157 unsigned char cmd[16]; 1136 unsigned char cmd[16];
1158 int the_result, retries; 1137 int the_result, retries;
@@ -1191,18 +1170,12 @@ repeat:
1191 } while (the_result && retries); 1170 } while (the_result && retries);
1192 1171
1193 if (the_result && !longrc) { 1172 if (the_result && !longrc) {
1194 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n" 1173 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1195 "%s : status=%x, message=%02x, host=%d, driver=%02x \n", 1174 sd_print_result(sdkp, the_result);
1196 diskname, diskname,
1197 status_byte(the_result),
1198 msg_byte(the_result),
1199 host_byte(the_result),
1200 driver_byte(the_result));
1201
1202 if (driver_byte(the_result) & DRIVER_SENSE) 1175 if (driver_byte(the_result) & DRIVER_SENSE)
1203 scsi_print_sense_hdr("sd", &sshdr); 1176 sd_print_sense_hdr(sdkp, &sshdr);
1204 else 1177 else
1205 printk("%s : sense not available. \n", diskname); 1178 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1206 1179
1207 /* Set dirty bit for removable devices if not ready - 1180 /* Set dirty bit for removable devices if not ready -
1208 * sometimes drives will not report this properly. */ 1181 * sometimes drives will not report this properly. */
@@ -1218,16 +1191,10 @@ repeat:
1218 return; 1191 return;
1219 } else if (the_result && longrc) { 1192 } else if (the_result && longrc) {
1220 /* READ CAPACITY(16) has been failed */ 1193 /* READ CAPACITY(16) has been failed */
1221 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n" 1194 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1222 "%s : status=%x, message=%02x, host=%d, driver=%02x \n", 1195 sd_print_result(sdkp, the_result);
1223 diskname, diskname, 1196 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1224 status_byte(the_result), 1197
1225 msg_byte(the_result),
1226 host_byte(the_result),
1227 driver_byte(the_result));
1228 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1229 diskname);
1230
1231 sdkp->capacity = 1 + (sector_t) 0xffffffff; 1198 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1232 goto got_data; 1199 goto got_data;
1233 } 1200 }
@@ -1238,14 +1205,14 @@ repeat:
1238 if (buffer[0] == 0xff && buffer[1] == 0xff && 1205 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1239 buffer[2] == 0xff && buffer[3] == 0xff) { 1206 buffer[2] == 0xff && buffer[3] == 0xff) {
1240 if(sizeof(sdkp->capacity) > 4) { 1207 if(sizeof(sdkp->capacity) > 4) {
1241 printk(KERN_NOTICE "%s : very big device. try to use" 1208 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1242 " READ CAPACITY(16).\n", diskname); 1209 "Trying to use READ CAPACITY(16).\n");
1243 longrc = 1; 1210 longrc = 1;
1244 goto repeat; 1211 goto repeat;
1245 } 1212 }
1246 printk(KERN_ERR "%s: too big for this kernel. Use a " 1213 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1247 "kernel compiled with support for large block " 1214 "a kernel compiled with support for large "
1248 "devices.\n", diskname); 1215 "block devices.\n");
1249 sdkp->capacity = 0; 1216 sdkp->capacity = 0;
1250 goto got_data; 1217 goto got_data;
1251 } 1218 }
@@ -1284,8 +1251,8 @@ repeat:
1284got_data: 1251got_data:
1285 if (sector_size == 0) { 1252 if (sector_size == 0) {
1286 sector_size = 512; 1253 sector_size = 512;
1287 printk(KERN_NOTICE "%s : sector size 0 reported, " 1254 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1288 "assuming 512.\n", diskname); 1255 "assuming 512.\n");
1289 } 1256 }
1290 1257
1291 if (sector_size != 512 && 1258 if (sector_size != 512 &&
@@ -1293,8 +1260,8 @@ got_data:
1293 sector_size != 2048 && 1260 sector_size != 2048 &&
1294 sector_size != 4096 && 1261 sector_size != 4096 &&
1295 sector_size != 256) { 1262 sector_size != 256) {
1296 printk(KERN_NOTICE "%s : unsupported sector size " 1263 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1297 "%d.\n", diskname, sector_size); 1264 sector_size);
1298 /* 1265 /*
1299 * The user might want to re-format the drive with 1266 * The user might want to re-format the drive with
1300 * a supported sectorsize. Once this happens, it 1267 * a supported sectorsize. Once this happens, it
@@ -1327,10 +1294,10 @@ got_data:
1327 mb -= sz - 974; 1294 mb -= sz - 974;
1328 sector_div(mb, 1950); 1295 sector_div(mb, 1950);
1329 1296
1330 printk(KERN_NOTICE "SCSI device %s: " 1297 sd_printk(KERN_NOTICE, sdkp,
1331 "%llu %d-byte hdwr sectors (%llu MB)\n", 1298 "%llu %d-byte hardware sectors (%llu MB)\n",
1332 diskname, (unsigned long long)sdkp->capacity, 1299 (unsigned long long)sdkp->capacity,
1333 hard_sector, (unsigned long long)mb); 1300 hard_sector, (unsigned long long)mb);
1334 } 1301 }
1335 1302
1336 /* Rescale capacity to 512-byte units */ 1303 /* Rescale capacity to 512-byte units */
@@ -1362,8 +1329,7 @@ sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1362 * called with buffer of length SD_BUF_SIZE 1329 * called with buffer of length SD_BUF_SIZE
1363 */ 1330 */
1364static void 1331static void
1365sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname, 1332sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1366 unsigned char *buffer)
1367{ 1333{
1368 int res; 1334 int res;
1369 struct scsi_device *sdp = sdkp->device; 1335 struct scsi_device *sdp = sdkp->device;
@@ -1371,7 +1337,7 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1371 1337
1372 set_disk_ro(sdkp->disk, 0); 1338 set_disk_ro(sdkp->disk, 0);
1373 if (sdp->skip_ms_page_3f) { 1339 if (sdp->skip_ms_page_3f) {
1374 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname); 1340 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1375 return; 1341 return;
1376 } 1342 }
1377 1343
@@ -1403,15 +1369,16 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1403 } 1369 }
1404 1370
1405 if (!scsi_status_is_good(res)) { 1371 if (!scsi_status_is_good(res)) {
1406 printk(KERN_WARNING 1372 sd_printk(KERN_WARNING, sdkp,
1407 "%s: test WP failed, assume Write Enabled\n", diskname); 1373 "Test WP failed, assume Write Enabled\n");
1408 } else { 1374 } else {
1409 sdkp->write_prot = ((data.device_specific & 0x80) != 0); 1375 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1410 set_disk_ro(sdkp->disk, sdkp->write_prot); 1376 set_disk_ro(sdkp->disk, sdkp->write_prot);
1411 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname, 1377 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1412 sdkp->write_prot ? "on" : "off"); 1378 sdkp->write_prot ? "on" : "off");
1413 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n", 1379 sd_printk(KERN_DEBUG, sdkp,
1414 diskname, buffer[0], buffer[1], buffer[2], buffer[3]); 1380 "Mode Sense: %02x %02x %02x %02x\n",
1381 buffer[0], buffer[1], buffer[2], buffer[3]);
1415 } 1382 }
1416} 1383}
1417 1384
@@ -1420,8 +1387,7 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1420 * called with buffer of length SD_BUF_SIZE 1387 * called with buffer of length SD_BUF_SIZE
1421 */ 1388 */
1422static void 1389static void
1423sd_read_cache_type(struct scsi_disk *sdkp, char *diskname, 1390sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1424 unsigned char *buffer)
1425{ 1391{
1426 int len = 0, res; 1392 int len = 0, res;
1427 struct scsi_device *sdp = sdkp->device; 1393 struct scsi_device *sdp = sdkp->device;
@@ -1450,8 +1416,7 @@ sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1450 1416
1451 if (!data.header_length) { 1417 if (!data.header_length) {
1452 modepage = 6; 1418 modepage = 6;
1453 printk(KERN_ERR "%s: missing header in MODE_SENSE response\n", 1419 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1454 diskname);
1455 } 1420 }
1456 1421
1457 /* that went OK, now ask for the proper length */ 1422 /* that went OK, now ask for the proper length */
@@ -1478,13 +1443,12 @@ sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1478 int offset = data.header_length + data.block_descriptor_length; 1443 int offset = data.header_length + data.block_descriptor_length;
1479 1444
1480 if (offset >= SD_BUF_SIZE - 2) { 1445 if (offset >= SD_BUF_SIZE - 2) {
1481 printk(KERN_ERR "%s: malformed MODE SENSE response", 1446 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1482 diskname);
1483 goto defaults; 1447 goto defaults;
1484 } 1448 }
1485 1449
1486 if ((buffer[offset] & 0x3f) != modepage) { 1450 if ((buffer[offset] & 0x3f) != modepage) {
1487 printk(KERN_ERR "%s: got wrong page\n", diskname); 1451 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1488 goto defaults; 1452 goto defaults;
1489 } 1453 }
1490 1454
@@ -1498,14 +1462,13 @@ sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1498 1462
1499 sdkp->DPOFUA = (data.device_specific & 0x10) != 0; 1463 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1500 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) { 1464 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1501 printk(KERN_NOTICE "SCSI device %s: uses " 1465 sd_printk(KERN_NOTICE, sdkp,
1502 "READ/WRITE(6), disabling FUA\n", diskname); 1466 "Uses READ/WRITE(6), disabling FUA\n");
1503 sdkp->DPOFUA = 0; 1467 sdkp->DPOFUA = 0;
1504 } 1468 }
1505 1469
1506 printk(KERN_NOTICE "SCSI device %s: " 1470 sd_printk(KERN_NOTICE, sdkp,
1507 "write cache: %s, read cache: %s, %s\n", 1471 "Write cache: %s, read cache: %s, %s\n",
1508 diskname,
1509 sdkp->WCE ? "enabled" : "disabled", 1472 sdkp->WCE ? "enabled" : "disabled",
1510 sdkp->RCD ? "disabled" : "enabled", 1473 sdkp->RCD ? "disabled" : "enabled",
1511 sdkp->DPOFUA ? "supports DPO and FUA" 1474 sdkp->DPOFUA ? "supports DPO and FUA"
@@ -1518,15 +1481,13 @@ bad_sense:
1518 if (scsi_sense_valid(&sshdr) && 1481 if (scsi_sense_valid(&sshdr) &&
1519 sshdr.sense_key == ILLEGAL_REQUEST && 1482 sshdr.sense_key == ILLEGAL_REQUEST &&
1520 sshdr.asc == 0x24 && sshdr.ascq == 0x0) 1483 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1521 printk(KERN_NOTICE "%s: cache data unavailable\n", 1484 /* Invalid field in CDB */
1522 diskname); /* Invalid field in CDB */ 1485 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1523 else 1486 else
1524 printk(KERN_ERR "%s: asking for cache data failed\n", 1487 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1525 diskname);
1526 1488
1527defaults: 1489defaults:
1528 printk(KERN_ERR "%s: assuming drive cache: write through\n", 1490 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1529 diskname);
1530 sdkp->WCE = 0; 1491 sdkp->WCE = 0;
1531 sdkp->RCD = 0; 1492 sdkp->RCD = 0;
1532 sdkp->DPOFUA = 0; 1493 sdkp->DPOFUA = 0;
@@ -1544,7 +1505,8 @@ static int sd_revalidate_disk(struct gendisk *disk)
1544 unsigned char *buffer; 1505 unsigned char *buffer;
1545 unsigned ordered; 1506 unsigned ordered;
1546 1507
1547 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name)); 1508 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1509 "sd_revalidate_disk\n"));
1548 1510
1549 /* 1511 /*
1550 * If the device is offline, don't try and read capacity or any 1512 * If the device is offline, don't try and read capacity or any
@@ -1555,8 +1517,8 @@ static int sd_revalidate_disk(struct gendisk *disk)
1555 1517
1556 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA); 1518 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
1557 if (!buffer) { 1519 if (!buffer) {
1558 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation " 1520 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1559 "failure.\n"); 1521 "allocation failure.\n");
1560 goto out; 1522 goto out;
1561 } 1523 }
1562 1524
@@ -1568,16 +1530,16 @@ static int sd_revalidate_disk(struct gendisk *disk)
1568 sdkp->WCE = 0; 1530 sdkp->WCE = 0;
1569 sdkp->RCD = 0; 1531 sdkp->RCD = 0;
1570 1532
1571 sd_spinup_disk(sdkp, disk->disk_name); 1533 sd_spinup_disk(sdkp);
1572 1534
1573 /* 1535 /*
1574 * Without media there is no reason to ask; moreover, some devices 1536 * Without media there is no reason to ask; moreover, some devices
1575 * react badly if we do. 1537 * react badly if we do.
1576 */ 1538 */
1577 if (sdkp->media_present) { 1539 if (sdkp->media_present) {
1578 sd_read_capacity(sdkp, disk->disk_name, buffer); 1540 sd_read_capacity(sdkp, buffer);
1579 sd_read_write_protect_flag(sdkp, disk->disk_name, buffer); 1541 sd_read_write_protect_flag(sdkp, buffer);
1580 sd_read_cache_type(sdkp, disk->disk_name, buffer); 1542 sd_read_cache_type(sdkp, buffer);
1581 } 1543 }
1582 1544
1583 /* 1545 /*
@@ -1709,8 +1671,8 @@ static int sd_probe(struct device *dev)
1709 dev_set_drvdata(dev, sdkp); 1671 dev_set_drvdata(dev, sdkp);
1710 add_disk(gd); 1672 add_disk(gd);
1711 1673
1712 sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n", 1674 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1713 sdp->removable ? "removable " : "", gd->disk_name); 1675 sdp->removable ? "removable " : "");
1714 1676
1715 return 0; 1677 return 0;
1716 1678
@@ -1774,6 +1736,31 @@ static void scsi_disk_release(struct class_device *cdev)
1774 kfree(sdkp); 1736 kfree(sdkp);
1775} 1737}
1776 1738
1739static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
1740{
1741 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1742 struct scsi_sense_hdr sshdr;
1743 struct scsi_device *sdp = sdkp->device;
1744 int res;
1745
1746 if (start)
1747 cmd[4] |= 1; /* START */
1748
1749 if (!scsi_device_online(sdp))
1750 return -ENODEV;
1751
1752 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1753 SD_TIMEOUT, SD_MAX_RETRIES);
1754 if (res) {
1755 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1756 sd_print_result(sdkp, res);
1757 if (driver_byte(res) & DRIVER_SENSE)
1758 sd_print_sense_hdr(sdkp, &sshdr);
1759 }
1760
1761 return res;
1762}
1763
1777/* 1764/*
1778 * Send a SYNCHRONIZE CACHE instruction down to the device through 1765 * Send a SYNCHRONIZE CACHE instruction down to the device through
1779 * the normal SCSI command structure. Wait for the command to 1766 * the normal SCSI command structure. Wait for the command to
@@ -1781,20 +1768,62 @@ static void scsi_disk_release(struct class_device *cdev)
1781 */ 1768 */
1782static void sd_shutdown(struct device *dev) 1769static void sd_shutdown(struct device *dev)
1783{ 1770{
1784 struct scsi_device *sdp = to_scsi_device(dev);
1785 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); 1771 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1786 1772
1787 if (!sdkp) 1773 if (!sdkp)
1788 return; /* this can happen */ 1774 return; /* this can happen */
1789 1775
1790 if (sdkp->WCE) { 1776 if (sdkp->WCE) {
1791 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n", 1777 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1792 sdkp->disk->disk_name); 1778 sd_sync_cache(sdkp);
1793 sd_sync_cache(sdp); 1779 }
1780
1781 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1782 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1783 sd_start_stop_device(sdkp, 0);
1794 } 1784 }
1785
1795 scsi_disk_put(sdkp); 1786 scsi_disk_put(sdkp);
1796} 1787}
1797 1788
1789static int sd_suspend(struct device *dev, pm_message_t mesg)
1790{
1791 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1792 int ret;
1793
1794 if (!sdkp)
1795 return 0; /* this can happen */
1796
1797 if (sdkp->WCE) {
1798 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1799 ret = sd_sync_cache(sdkp);
1800 if (ret)
1801 return ret;
1802 }
1803
1804 if (mesg.event == PM_EVENT_SUSPEND &&
1805 sdkp->device->manage_start_stop) {
1806 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1807 ret = sd_start_stop_device(sdkp, 0);
1808 if (ret)
1809 return ret;
1810 }
1811
1812 return 0;
1813}
1814
1815static int sd_resume(struct device *dev)
1816{
1817 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1818
1819 if (!sdkp->device->manage_start_stop)
1820 return 0;
1821
1822 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
1823
1824 return sd_start_stop_device(sdkp, 1);
1825}
1826
1798/** 1827/**
1799 * init_sd - entry point for this driver (both when built in or when 1828 * init_sd - entry point for this driver (both when built in or when
1800 * a module). 1829 * a module).
@@ -1852,3 +1881,19 @@ static void __exit exit_sd(void)
1852 1881
1853module_init(init_sd); 1882module_init(init_sd);
1854module_exit(exit_sd); 1883module_exit(exit_sd);
1884
1885static void sd_print_sense_hdr(struct scsi_disk *sdkp,
1886 struct scsi_sense_hdr *sshdr)
1887{
1888 sd_printk(KERN_INFO, sdkp, "");
1889 scsi_show_sense_hdr(sshdr);
1890 sd_printk(KERN_INFO, sdkp, "");
1891 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
1892}
1893
1894static void sd_print_result(struct scsi_disk *sdkp, int result)
1895{
1896 sd_printk(KERN_INFO, sdkp, "");
1897 scsi_show_result(result);
1898}
1899