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