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> |
James Bottomley | 7404ad3 | 2008-08-31 10:41:52 -0500 | [diff] [blame] | 50 | #include <linux/string_helpers.h> |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 51 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/uaccess.h> |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 53 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #include <scsi/scsi.h> |
| 56 | #include <scsi/scsi_cmnd.h> |
| 57 | #include <scsi/scsi_dbg.h> |
| 58 | #include <scsi/scsi_device.h> |
| 59 | #include <scsi/scsi_driver.h> |
| 60 | #include <scsi/scsi_eh.h> |
| 61 | #include <scsi/scsi_host.h> |
| 62 | #include <scsi/scsi_ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <scsi/scsicam.h> |
| 64 | |
Martin K. Petersen | aa91696 | 2008-06-17 12:47:32 -0400 | [diff] [blame] | 65 | #include "sd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include "scsi_logging.h" |
| 67 | |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 68 | MODULE_AUTHOR("Eric Youngdale"); |
| 69 | MODULE_DESCRIPTION("SCSI disk (sd) driver"); |
| 70 | MODULE_LICENSE("GPL"); |
| 71 | |
| 72 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR); |
| 73 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR); |
| 74 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR); |
| 75 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR); |
| 76 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR); |
| 77 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR); |
| 78 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR); |
| 79 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR); |
| 80 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR); |
| 81 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR); |
| 82 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR); |
| 83 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR); |
| 84 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR); |
| 85 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR); |
| 86 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR); |
| 87 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR); |
Michael Tokarev | d7b8bcb | 2006-10-27 16:02:37 +0400 | [diff] [blame] | 88 | MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK); |
| 89 | MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD); |
| 90 | MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC); |
Rene Herman | f018fa5 | 2006-03-08 00:14:20 -0800 | [diff] [blame] | 91 | |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 92 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
Tejun Heo | f615b48 | 2008-08-25 19:47:24 +0900 | [diff] [blame] | 93 | #define SD_MINORS 16 |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 94 | #else |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 95 | #define SD_MINORS 0 |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 96 | #endif |
| 97 | |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 98 | static int sd_revalidate_disk(struct gendisk *); |
| 99 | static int sd_probe(struct device *); |
| 100 | static int sd_remove(struct device *); |
| 101 | static void sd_shutdown(struct device *); |
| 102 | static int sd_suspend(struct device *, pm_message_t state); |
| 103 | static int sd_resume(struct device *); |
| 104 | static void sd_rescan(struct device *); |
| 105 | static int sd_done(struct scsi_cmnd *); |
| 106 | static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 107 | static void scsi_disk_release(struct device *cdev); |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 108 | static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *); |
| 109 | static void sd_print_result(struct scsi_disk *, int); |
| 110 | |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 111 | static DEFINE_SPINLOCK(sd_index_lock); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 112 | static DEFINE_IDA(sd_index_ida); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* This semaphore is used to mediate the 0->1 reference get in the |
| 115 | * face of object destruction (i.e. we can't allow a get on an |
| 116 | * object after last put) */ |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 117 | static DEFINE_MUTEX(sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 119 | struct kmem_cache *sd_cdb_cache; |
| 120 | mempool_t *sd_cdb_pool; |
| 121 | |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 122 | static const char *sd_cache_types[] = { |
| 123 | "write through", "none", "write back", |
| 124 | "write back, no read (daft)" |
| 125 | }; |
| 126 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 127 | static ssize_t |
| 128 | sd_store_cache_type(struct device *dev, struct device_attribute *attr, |
| 129 | const char *buf, size_t count) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 130 | { |
| 131 | int i, ct = -1, rcd, wce, sp; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 132 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 133 | struct scsi_device *sdp = sdkp->device; |
| 134 | char buffer[64]; |
| 135 | char *buffer_data; |
| 136 | struct scsi_mode_data data; |
| 137 | struct scsi_sense_hdr sshdr; |
| 138 | int len; |
| 139 | |
| 140 | if (sdp->type != TYPE_DISK) |
| 141 | /* no cache control on RBC devices; theoretically they |
| 142 | * can do it, but there's probably so many exceptions |
| 143 | * it's not worth the risk */ |
| 144 | return -EINVAL; |
| 145 | |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 146 | for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) { |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 147 | const int len = strlen(sd_cache_types[i]); |
| 148 | if (strncmp(sd_cache_types[i], buf, len) == 0 && |
| 149 | buf[len] == '\n') { |
| 150 | ct = i; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | if (ct < 0) |
| 155 | return -EINVAL; |
| 156 | rcd = ct & 0x01 ? 1 : 0; |
| 157 | wce = ct & 0x02 ? 1 : 0; |
| 158 | if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT, |
| 159 | SD_MAX_RETRIES, &data, NULL)) |
| 160 | return -EINVAL; |
Andrew Morton | a9312fb | 2006-03-25 03:08:30 -0800 | [diff] [blame] | 161 | len = min_t(size_t, sizeof(buffer), data.length - data.header_length - |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 162 | data.block_descriptor_length); |
| 163 | buffer_data = buffer + data.header_length + |
| 164 | data.block_descriptor_length; |
| 165 | buffer_data[2] &= ~0x05; |
| 166 | buffer_data[2] |= wce << 2 | rcd; |
| 167 | sp = buffer_data[0] & 0x80 ? 1 : 0; |
| 168 | |
| 169 | if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT, |
| 170 | SD_MAX_RETRIES, &data, &sshdr)) { |
| 171 | if (scsi_sense_valid(&sshdr)) |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 172 | sd_print_sense_hdr(sdkp, &sshdr); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | } |
Andrew Patterson | f98a8ca | 2008-09-04 14:27:35 -0600 | [diff] [blame] | 175 | revalidate_disk(sdkp->disk); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 176 | return count; |
| 177 | } |
| 178 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 179 | static ssize_t |
| 180 | sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr, |
| 181 | const char *buf, size_t count) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 182 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 183 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 184 | struct scsi_device *sdp = sdkp->device; |
| 185 | |
| 186 | if (!capable(CAP_SYS_ADMIN)) |
| 187 | return -EACCES; |
| 188 | |
| 189 | sdp->manage_start_stop = simple_strtoul(buf, NULL, 10); |
| 190 | |
| 191 | return count; |
| 192 | } |
| 193 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 194 | static ssize_t |
| 195 | sd_store_allow_restart(struct device *dev, struct device_attribute *attr, |
| 196 | const char *buf, size_t count) |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 197 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 198 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 199 | struct scsi_device *sdp = sdkp->device; |
| 200 | |
| 201 | if (!capable(CAP_SYS_ADMIN)) |
| 202 | return -EACCES; |
| 203 | |
| 204 | if (sdp->type != TYPE_DISK) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | sdp->allow_restart = simple_strtoul(buf, NULL, 10); |
| 208 | |
| 209 | return count; |
| 210 | } |
| 211 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 212 | static ssize_t |
| 213 | sd_show_cache_type(struct device *dev, struct device_attribute *attr, |
| 214 | char *buf) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 215 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 216 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 217 | int ct = sdkp->RCD + 2*sdkp->WCE; |
| 218 | |
| 219 | return snprintf(buf, 40, "%s\n", sd_cache_types[ct]); |
| 220 | } |
| 221 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 222 | static ssize_t |
| 223 | sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf) |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 224 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 225 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 226 | |
| 227 | return snprintf(buf, 20, "%u\n", sdkp->DPOFUA); |
| 228 | } |
| 229 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 230 | static ssize_t |
| 231 | sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr, |
| 232 | char *buf) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 233 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 234 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 235 | struct scsi_device *sdp = sdkp->device; |
| 236 | |
| 237 | return snprintf(buf, 20, "%u\n", sdp->manage_start_stop); |
| 238 | } |
| 239 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 240 | static ssize_t |
| 241 | sd_show_allow_restart(struct device *dev, struct device_attribute *attr, |
| 242 | char *buf) |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 243 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 244 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 245 | |
| 246 | return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart); |
| 247 | } |
| 248 | |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 249 | static ssize_t |
| 250 | sd_show_protection_type(struct device *dev, struct device_attribute *attr, |
| 251 | char *buf) |
| 252 | { |
| 253 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 254 | |
| 255 | return snprintf(buf, 20, "%u\n", sdkp->protection_type); |
| 256 | } |
| 257 | |
| 258 | static ssize_t |
| 259 | sd_show_app_tag_own(struct device *dev, struct device_attribute *attr, |
| 260 | char *buf) |
| 261 | { |
| 262 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 263 | |
| 264 | return snprintf(buf, 20, "%u\n", sdkp->ATO); |
| 265 | } |
| 266 | |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 267 | static ssize_t |
| 268 | sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr, |
| 269 | char *buf) |
| 270 | { |
| 271 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
| 272 | |
| 273 | return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning); |
| 274 | } |
| 275 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 276 | static struct device_attribute sd_disk_attrs[] = { |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 277 | __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type, |
| 278 | sd_store_cache_type), |
| 279 | __ATTR(FUA, S_IRUGO, sd_show_fua, NULL), |
Brian King | a144c5a | 2006-06-27 11:10:31 -0500 | [diff] [blame] | 280 | __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart, |
| 281 | sd_store_allow_restart), |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 282 | __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop, |
| 283 | sd_store_manage_start_stop), |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 284 | __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL), |
| 285 | __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL), |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 286 | __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL), |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 287 | __ATTR_NULL, |
| 288 | }; |
| 289 | |
| 290 | static struct class sd_disk_class = { |
| 291 | .name = "scsi_disk", |
| 292 | .owner = THIS_MODULE, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 293 | .dev_release = scsi_disk_release, |
| 294 | .dev_attrs = sd_disk_attrs, |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 295 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | static struct scsi_driver sd_template = { |
| 298 | .owner = THIS_MODULE, |
| 299 | .gendrv = { |
| 300 | .name = "sd", |
| 301 | .probe = sd_probe, |
| 302 | .remove = sd_remove, |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 303 | .suspend = sd_suspend, |
| 304 | .resume = sd_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | .shutdown = sd_shutdown, |
| 306 | }, |
| 307 | .rescan = sd_rescan, |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 308 | .done = sd_done, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | /* |
| 312 | * Device no to disk mapping: |
| 313 | * |
| 314 | * major disc2 disc p1 |
| 315 | * |............|.............|....|....| <- dev_t |
| 316 | * 31 20 19 8 7 4 3 0 |
| 317 | * |
| 318 | * Inside a major, we have 16k disks, however mapped non- |
| 319 | * contiguously. The first 16 disks are for major0, the next |
| 320 | * ones with major1, ... Disk 256 is for major0 again, disk 272 |
| 321 | * for major1, ... |
| 322 | * As we stay compatible with our numbering scheme, we can reuse |
| 323 | * the well-know SCSI majors 8, 65--71, 136--143. |
| 324 | */ |
| 325 | static int sd_major(int major_idx) |
| 326 | { |
| 327 | switch (major_idx) { |
| 328 | case 0: |
| 329 | return SCSI_DISK0_MAJOR; |
| 330 | case 1 ... 7: |
| 331 | return SCSI_DISK1_MAJOR + major_idx - 1; |
| 332 | case 8 ... 15: |
| 333 | return SCSI_DISK8_MAJOR + major_idx - 8; |
| 334 | default: |
| 335 | BUG(); |
| 336 | return 0; /* shut up gcc */ |
| 337 | } |
| 338 | } |
| 339 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 340 | static struct scsi_disk *__scsi_disk_get(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | struct scsi_disk *sdkp = NULL; |
| 343 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 344 | if (disk->private_data) { |
| 345 | sdkp = scsi_disk(disk); |
| 346 | if (scsi_device_get(sdkp->device) == 0) |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 347 | get_device(&sdkp->dev); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 348 | else |
| 349 | sdkp = NULL; |
| 350 | } |
| 351 | return sdkp; |
| 352 | } |
| 353 | |
| 354 | static struct scsi_disk *scsi_disk_get(struct gendisk *disk) |
| 355 | { |
| 356 | struct scsi_disk *sdkp; |
| 357 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 358 | mutex_lock(&sd_ref_mutex); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 359 | sdkp = __scsi_disk_get(disk); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 360 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return sdkp; |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 364 | static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev) |
| 365 | { |
| 366 | struct scsi_disk *sdkp; |
| 367 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 368 | mutex_lock(&sd_ref_mutex); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 369 | sdkp = dev_get_drvdata(dev); |
| 370 | if (sdkp) |
| 371 | sdkp = __scsi_disk_get(sdkp->disk); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 372 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return sdkp; |
| 374 | } |
| 375 | |
| 376 | static void scsi_disk_put(struct scsi_disk *sdkp) |
| 377 | { |
| 378 | struct scsi_device *sdev = sdkp->device; |
| 379 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 380 | mutex_lock(&sd_ref_mutex); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 381 | put_device(&sdkp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | scsi_device_put(sdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 383 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 386 | static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif) |
| 387 | { |
| 388 | unsigned int prot_op = SCSI_PROT_NORMAL; |
| 389 | unsigned int dix = scsi_prot_sg_count(scmd); |
| 390 | |
| 391 | if (scmd->sc_data_direction == DMA_FROM_DEVICE) { |
| 392 | if (dif && dix) |
| 393 | prot_op = SCSI_PROT_READ_PASS; |
| 394 | else if (dif && !dix) |
| 395 | prot_op = SCSI_PROT_READ_STRIP; |
| 396 | else if (!dif && dix) |
| 397 | prot_op = SCSI_PROT_READ_INSERT; |
| 398 | } else { |
| 399 | if (dif && dix) |
| 400 | prot_op = SCSI_PROT_WRITE_PASS; |
| 401 | else if (dif && !dix) |
| 402 | prot_op = SCSI_PROT_WRITE_INSERT; |
| 403 | else if (!dif && dix) |
| 404 | prot_op = SCSI_PROT_WRITE_STRIP; |
| 405 | } |
| 406 | |
| 407 | scsi_set_prot_op(scmd, prot_op); |
| 408 | scsi_set_prot_type(scmd, dif); |
| 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | /** |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 412 | * sd_prepare_discard - unmap blocks on thinly provisioned device |
| 413 | * @rq: Request to prepare |
| 414 | * |
| 415 | * Will issue either UNMAP or WRITE SAME(16) depending on preference |
| 416 | * indicated by target device. |
| 417 | **/ |
| 418 | static int sd_prepare_discard(struct request *rq) |
| 419 | { |
| 420 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 421 | struct bio *bio = rq->bio; |
| 422 | sector_t sector = bio->bi_sector; |
| 423 | unsigned int num = bio_sectors(bio); |
| 424 | |
| 425 | if (sdkp->device->sector_size == 4096) { |
| 426 | sector >>= 3; |
| 427 | num >>= 3; |
| 428 | } |
| 429 | |
| 430 | rq->cmd_type = REQ_TYPE_BLOCK_PC; |
| 431 | rq->timeout = SD_TIMEOUT; |
| 432 | |
| 433 | memset(rq->cmd, 0, rq->cmd_len); |
| 434 | |
| 435 | if (sdkp->unmap) { |
| 436 | char *buf = kmap_atomic(bio_page(bio), KM_USER0); |
| 437 | |
| 438 | rq->cmd[0] = UNMAP; |
| 439 | rq->cmd[8] = 24; |
| 440 | rq->cmd_len = 10; |
| 441 | |
| 442 | /* Ensure that data length matches payload */ |
| 443 | rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24; |
| 444 | |
| 445 | put_unaligned_be16(6 + 16, &buf[0]); |
| 446 | put_unaligned_be16(16, &buf[2]); |
| 447 | put_unaligned_be64(sector, &buf[8]); |
| 448 | put_unaligned_be32(num, &buf[16]); |
| 449 | |
| 450 | kunmap_atomic(buf, KM_USER0); |
| 451 | } else { |
| 452 | rq->cmd[0] = WRITE_SAME_16; |
| 453 | rq->cmd[1] = 0x8; /* UNMAP */ |
| 454 | put_unaligned_be64(sector, &rq->cmd[2]); |
| 455 | put_unaligned_be32(num, &rq->cmd[10]); |
| 456 | rq->cmd_len = 16; |
| 457 | } |
| 458 | |
| 459 | return BLKPREP_OK; |
| 460 | } |
| 461 | |
| 462 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * sd_init_command - build a scsi (read or write) command from |
| 464 | * information in the request structure. |
| 465 | * @SCpnt: pointer to mid-level's per scsi command structure that |
| 466 | * contains request and into which the scsi command is written |
| 467 | * |
| 468 | * Returns 1 if successful and 0 if error (or cannot be done now). |
| 469 | **/ |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 470 | static int sd_prep_fn(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 472 | struct scsi_cmnd *SCpnt; |
| 473 | struct scsi_device *sdp = q->queuedata; |
Christoph Hellwig | 776b23a | 2006-01-06 18:34:07 +0100 | [diff] [blame] | 474 | struct gendisk *disk = rq->rq_disk; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 475 | struct scsi_disk *sdkp; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 476 | sector_t block = blk_rq_pos(rq); |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 477 | sector_t threshold; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 478 | unsigned int this_count = blk_rq_sectors(rq); |
Martin K. Petersen | bd623e7 | 2008-09-19 18:47:19 -0400 | [diff] [blame] | 479 | int ret, host_dif; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 480 | unsigned char protect; |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 481 | |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 482 | /* |
| 483 | * Discard request come in as REQ_TYPE_FS but we turn them into |
| 484 | * block PC requests to make life easier. |
| 485 | */ |
| 486 | if (blk_discard_rq(rq)) |
| 487 | ret = sd_prepare_discard(rq); |
| 488 | |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 489 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
| 490 | ret = scsi_setup_blk_pc_cmnd(sdp, rq); |
| 491 | goto out; |
| 492 | } else if (rq->cmd_type != REQ_TYPE_FS) { |
| 493 | ret = BLKPREP_KILL; |
| 494 | goto out; |
| 495 | } |
| 496 | ret = scsi_setup_fs_cmnd(sdp, rq); |
| 497 | if (ret != BLKPREP_OK) |
| 498 | goto out; |
| 499 | SCpnt = rq->special; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 500 | sdkp = scsi_disk(disk); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 501 | |
| 502 | /* from here on until we're complete, any goto out |
| 503 | * is used for a killable error condition */ |
| 504 | ret = BLKPREP_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 506 | SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, |
| 507 | "sd_init_command: block=%llu, " |
| 508 | "count=%d\n", |
| 509 | (unsigned long long)block, |
| 510 | this_count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | if (!sdp || !scsi_device_online(sdp) || |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 513 | block + blk_rq_sectors(rq) > get_capacity(disk)) { |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 514 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 515 | "Finishing %u sectors\n", |
| 516 | blk_rq_sectors(rq))); |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 517 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
| 518 | "Retry with 0x%p\n", SCpnt)); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 519 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (sdp->changed) { |
| 523 | /* |
| 524 | * quietly refuse to do anything to a changed disc until |
| 525 | * the changed bit has been reset |
| 526 | */ |
| 527 | /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 528 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 530 | |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 531 | /* |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 532 | * Some SD card readers can't handle multi-sector accesses which touch |
| 533 | * the last one or two hardware sectors. Split accesses as needed. |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 534 | */ |
Linus Torvalds | 1835107 | 2008-08-05 21:42:21 -0700 | [diff] [blame] | 535 | threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS * |
| 536 | (sdp->sector_size / 512); |
| 537 | |
| 538 | if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) { |
| 539 | if (block < threshold) { |
| 540 | /* Access up to the threshold but not beyond */ |
| 541 | this_count = threshold - block; |
| 542 | } else { |
| 543 | /* Access only a single hardware sector */ |
| 544 | this_count = sdp->sector_size / 512; |
| 545 | } |
| 546 | } |
Hans de Goede | a0899d4 | 2008-01-20 11:12:26 +0100 | [diff] [blame] | 547 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 548 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n", |
| 549 | (unsigned long long)block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * If we have a 1K hardware sectorsize, prevent access to single |
| 553 | * 512 byte sectors. In theory we could handle this - in fact |
| 554 | * the scsi cdrom driver must be able to handle this because |
| 555 | * we typically use 1K blocksizes, and cdroms typically have |
| 556 | * 2K hardware sectorsizes. Of course, things are simpler |
| 557 | * with the cdrom, since it is read-only. For performance |
| 558 | * reasons, the filesystems should be able to handle this |
| 559 | * and not force the scsi disk driver to use bounce buffers |
| 560 | * for this. |
| 561 | */ |
| 562 | if (sdp->sector_size == 1024) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 563 | if ((block & 1) || (blk_rq_sectors(rq) & 1)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 564 | scmd_printk(KERN_ERR, SCpnt, |
| 565 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 566 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } else { |
| 568 | block = block >> 1; |
| 569 | this_count = this_count >> 1; |
| 570 | } |
| 571 | } |
| 572 | if (sdp->sector_size == 2048) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 573 | if ((block & 3) || (blk_rq_sectors(rq) & 3)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 574 | scmd_printk(KERN_ERR, SCpnt, |
| 575 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 576 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } else { |
| 578 | block = block >> 2; |
| 579 | this_count = this_count >> 2; |
| 580 | } |
| 581 | } |
| 582 | if (sdp->sector_size == 4096) { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 583 | if ((block & 7) || (blk_rq_sectors(rq) & 7)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 584 | scmd_printk(KERN_ERR, SCpnt, |
| 585 | "Bad block number requested\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 586 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } else { |
| 588 | block = block >> 3; |
| 589 | this_count = this_count >> 3; |
| 590 | } |
| 591 | } |
| 592 | if (rq_data_dir(rq) == WRITE) { |
| 593 | if (!sdp->writeable) { |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 594 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | SCpnt->cmnd[0] = WRITE_6; |
| 597 | SCpnt->sc_data_direction = DMA_TO_DEVICE; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 598 | |
| 599 | if (blk_integrity_rq(rq) && |
| 600 | sd_dif_prepare(rq, block, sdp->sector_size) == -EIO) |
| 601 | goto out; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } else if (rq_data_dir(rq) == READ) { |
| 604 | SCpnt->cmnd[0] = READ_6; |
| 605 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; |
| 606 | } else { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 607 | scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 608 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 611 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 612 | "%s %d/%u 512 byte blocks.\n", |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 613 | (rq_data_dir(rq) == WRITE) ? |
| 614 | "writing" : "reading", this_count, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 615 | blk_rq_sectors(rq))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 617 | /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */ |
Martin K. Petersen | bd623e7 | 2008-09-19 18:47:19 -0400 | [diff] [blame] | 618 | host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type); |
| 619 | if (host_dif) |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 620 | protect = 1 << 5; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 621 | else |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 622 | protect = 0; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 623 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 624 | if (host_dif == SD_DIF_TYPE2_PROTECTION) { |
| 625 | SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC); |
| 626 | |
| 627 | if (unlikely(SCpnt->cmnd == NULL)) { |
| 628 | ret = BLKPREP_DEFER; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | SCpnt->cmd_len = SD_EXT_CDB_SIZE; |
| 633 | memset(SCpnt->cmnd, 0, SCpnt->cmd_len); |
| 634 | SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD; |
| 635 | SCpnt->cmnd[7] = 0x18; |
| 636 | SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32; |
| 637 | SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0); |
| 638 | |
| 639 | /* LBA */ |
| 640 | SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; |
| 641 | SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; |
| 642 | SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; |
| 643 | SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; |
| 644 | SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff; |
| 645 | SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff; |
| 646 | SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff; |
| 647 | SCpnt->cmnd[19] = (unsigned char) block & 0xff; |
| 648 | |
| 649 | /* Expected Indirect LBA */ |
| 650 | SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff; |
| 651 | SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff; |
| 652 | SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff; |
| 653 | SCpnt->cmnd[23] = (unsigned char) block & 0xff; |
| 654 | |
| 655 | /* Transfer length */ |
| 656 | SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff; |
| 657 | SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff; |
| 658 | SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff; |
| 659 | SCpnt->cmnd[31] = (unsigned char) this_count & 0xff; |
| 660 | } else if (block > 0xffffffff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | SCpnt->cmnd[0] += READ_16 - READ_6; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 662 | SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; |
| 664 | SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; |
| 665 | SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; |
| 666 | SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; |
| 667 | SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff; |
| 668 | SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff; |
| 669 | SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff; |
| 670 | SCpnt->cmnd[9] = (unsigned char) block & 0xff; |
| 671 | SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff; |
| 672 | SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff; |
| 673 | SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff; |
| 674 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; |
| 675 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; |
| 676 | } else if ((this_count > 0xff) || (block > 0x1fffff) || |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 677 | scsi_device_protection(SCpnt->device) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | SCpnt->device->use_10_for_rw) { |
| 679 | if (this_count > 0xffff) |
| 680 | this_count = 0xffff; |
| 681 | |
| 682 | SCpnt->cmnd[0] += READ_10 - READ_6; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 683 | SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; |
| 685 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; |
| 686 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; |
| 687 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; |
| 688 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; |
| 689 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; |
| 690 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; |
| 691 | } else { |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 692 | if (unlikely(blk_fua_rq(rq))) { |
| 693 | /* |
| 694 | * This happens only if this drive failed |
| 695 | * 10byte rw command with ILLEGAL_REQUEST |
| 696 | * during operation and thus turned off |
| 697 | * use_10_for_rw. |
| 698 | */ |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 699 | scmd_printk(KERN_ERR, SCpnt, |
| 700 | "FUA write on READ/WRITE(6) drive\n"); |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 701 | goto out; |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f); |
| 705 | SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff); |
| 706 | SCpnt->cmnd[3] = (unsigned char) block & 0xff; |
| 707 | SCpnt->cmnd[4] = (unsigned char) this_count; |
| 708 | SCpnt->cmnd[5] = 0; |
| 709 | } |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 710 | SCpnt->sdb.length = this_count * sdp->sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 712 | /* If DIF or DIX is enabled, tell HBA how to handle request */ |
Martin K. Petersen | bd623e7 | 2008-09-19 18:47:19 -0400 | [diff] [blame] | 713 | if (host_dif || scsi_prot_sg_count(SCpnt)) |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 714 | sd_prot_op(SCpnt, host_dif); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | /* |
| 717 | * We shouldn't disconnect in the middle of a sector, so with a dumb |
| 718 | * host adapter, it's safe to assume that we can at least transfer |
| 719 | * this many bytes between each connect / disconnect. |
| 720 | */ |
| 721 | SCpnt->transfersize = sdp->sector_size; |
| 722 | SCpnt->underflow = this_count << 9; |
| 723 | SCpnt->allowed = SD_MAX_RETRIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | * This indicates that the command is ready from our end to be |
| 727 | * queued. |
| 728 | */ |
James Bottomley | 7f9a6bc | 2007-08-04 10:06:25 -0500 | [diff] [blame] | 729 | ret = BLKPREP_OK; |
| 730 | out: |
| 731 | return scsi_prep_return(q, rq, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /** |
| 735 | * sd_open - open a scsi disk device |
| 736 | * @inode: only i_rdev member may be used |
| 737 | * @filp: only f_mode and f_flags may be used |
| 738 | * |
| 739 | * Returns 0 if successful. Returns a negated errno value in case |
| 740 | * of error. |
| 741 | * |
| 742 | * Note: This can be called from a user context (e.g. fsck(1) ) |
| 743 | * or from within the kernel (e.g. as a result of a mount(1) ). |
| 744 | * In the latter case @inode and @filp carry an abridged amount |
| 745 | * of information as noted above. |
| 746 | **/ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 747 | static int sd_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 749 | struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | struct scsi_device *sdev; |
| 751 | int retval; |
| 752 | |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 753 | if (!sdkp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return -ENXIO; |
| 755 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 756 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | sdev = sdkp->device; |
| 759 | |
| 760 | /* |
| 761 | * If the device is in error recovery, wait until it is done. |
| 762 | * If the device is offline, then disallow any access to it. |
| 763 | */ |
| 764 | retval = -ENXIO; |
| 765 | if (!scsi_block_when_processing_errors(sdev)) |
| 766 | goto error_out; |
| 767 | |
| 768 | if (sdev->removable || sdkp->write_prot) |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 769 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * If the drive is empty, just let the open fail. |
| 773 | */ |
| 774 | retval = -ENOMEDIUM; |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 775 | if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | goto error_out; |
| 777 | |
| 778 | /* |
| 779 | * If the device has the write protect tab set, have the open fail |
| 780 | * if the user expects to be able to write to the thing. |
| 781 | */ |
| 782 | retval = -EROFS; |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 783 | if (sdkp->write_prot && (mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | goto error_out; |
| 785 | |
| 786 | /* |
| 787 | * It is possible that the disk changing stuff resulted in |
| 788 | * the device being taken offline. If this is the case, |
| 789 | * report this to the user, and don't pretend that the |
| 790 | * open actually succeeded. |
| 791 | */ |
| 792 | retval = -ENXIO; |
| 793 | if (!scsi_device_online(sdev)) |
| 794 | goto error_out; |
| 795 | |
| 796 | if (!sdkp->openers++ && sdev->removable) { |
| 797 | if (scsi_block_when_processing_errors(sdev)) |
| 798 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | |
| 803 | error_out: |
| 804 | scsi_disk_put(sdkp); |
| 805 | return retval; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * sd_release - invoked when the (last) close(2) is called on this |
| 810 | * scsi disk. |
| 811 | * @inode: only i_rdev member may be used |
| 812 | * @filp: only f_mode and f_flags may be used |
| 813 | * |
| 814 | * Returns 0. |
| 815 | * |
| 816 | * Note: may block (uninterruptible) if error recovery is underway |
| 817 | * on this disk. |
| 818 | **/ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 819 | static int sd_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 822 | struct scsi_device *sdev = sdkp->device; |
| 823 | |
James Bottomley | 56937f7 | 2007-03-11 12:25:33 -0500 | [diff] [blame] | 824 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | if (!--sdkp->openers && sdev->removable) { |
| 827 | if (scsi_block_when_processing_errors(sdev)) |
| 828 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * XXX and what if there are packets in flight and this close() |
| 833 | * XXX is followed by a "rmmod sd_mod"? |
| 834 | */ |
| 835 | scsi_disk_put(sdkp); |
| 836 | return 0; |
| 837 | } |
| 838 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 839 | static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
| 841 | struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); |
| 842 | struct scsi_device *sdp = sdkp->device; |
| 843 | struct Scsi_Host *host = sdp->host; |
| 844 | int diskinfo[4]; |
| 845 | |
| 846 | /* default to most commonly used values */ |
| 847 | diskinfo[0] = 0x40; /* 1 << 6 */ |
| 848 | diskinfo[1] = 0x20; /* 1 << 5 */ |
| 849 | diskinfo[2] = sdkp->capacity >> 11; |
| 850 | |
| 851 | /* override with calculated, extended default, or driver values */ |
| 852 | if (host->hostt->bios_param) |
| 853 | host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo); |
| 854 | else |
| 855 | scsicam_bios_param(bdev, sdkp->capacity, diskinfo); |
| 856 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 857 | geo->heads = diskinfo[0]; |
| 858 | geo->sectors = diskinfo[1]; |
| 859 | geo->cylinders = diskinfo[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * sd_ioctl - process an ioctl |
| 865 | * @inode: only i_rdev/i_bdev members may be used |
| 866 | * @filp: only f_mode and f_flags may be used |
| 867 | * @cmd: ioctl command number |
| 868 | * @arg: this is third argument given to ioctl(2) system call. |
| 869 | * Often contains a pointer. |
| 870 | * |
| 871 | * Returns 0 if successful (some ioctls return postive numbers on |
| 872 | * success as well). Returns a negated errno value in case of error. |
| 873 | * |
| 874 | * Note: most ioctls are forward onto the block subsystem or further |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 875 | * down in the scsi subsystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | **/ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 877 | static int sd_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | unsigned int cmd, unsigned long arg) |
| 879 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | struct gendisk *disk = bdev->bd_disk; |
| 881 | struct scsi_device *sdp = scsi_disk(disk)->device; |
| 882 | void __user *p = (void __user *)arg; |
| 883 | int error; |
| 884 | |
| 885 | SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n", |
| 886 | disk->disk_name, cmd)); |
| 887 | |
| 888 | /* |
| 889 | * If we are in the middle of error recovery, don't let anyone |
| 890 | * else try and use this device. Also, if error recovery fails, it |
| 891 | * may try and take the device offline, in which case all further |
| 892 | * access to the device is prohibited. |
| 893 | */ |
Al Viro | 83ff6fe | 2008-03-02 08:15:49 -0500 | [diff] [blame] | 894 | error = scsi_nonblockable_ioctl(sdp, cmd, p, |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 895 | (mode & FMODE_NDELAY) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (!scsi_block_when_processing_errors(sdp) || !error) |
| 897 | return error; |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | /* |
| 900 | * Send SCSI addressing ioctls directly to mid level, send other |
| 901 | * ioctls to block level and then onto mid level if they can't be |
| 902 | * resolved. |
| 903 | */ |
| 904 | switch (cmd) { |
| 905 | case SCSI_IOCTL_GET_IDLUN: |
| 906 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 907 | return scsi_ioctl(sdp, cmd, p); |
| 908 | default: |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 909 | error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (error != -ENOTTY) |
| 911 | return error; |
| 912 | } |
| 913 | return scsi_ioctl(sdp, cmd, p); |
| 914 | } |
| 915 | |
| 916 | static void set_media_not_present(struct scsi_disk *sdkp) |
| 917 | { |
| 918 | sdkp->media_present = 0; |
| 919 | sdkp->capacity = 0; |
| 920 | sdkp->device->changed = 1; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * sd_media_changed - check if our medium changed |
| 925 | * @disk: kernel device descriptor |
| 926 | * |
| 927 | * Returns 0 if not applicable or no change; 1 if change |
| 928 | * |
| 929 | * Note: this function is invoked from the block subsystem. |
| 930 | **/ |
| 931 | static int sd_media_changed(struct gendisk *disk) |
| 932 | { |
| 933 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 934 | struct scsi_device *sdp = sdkp->device; |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 935 | struct scsi_sense_hdr *sshdr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | int retval; |
| 937 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 938 | 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] | 939 | |
| 940 | if (!sdp->removable) |
| 941 | return 0; |
| 942 | |
| 943 | /* |
| 944 | * If the device is offline, don't send any commands - just pretend as |
| 945 | * if the command failed. If the device ever comes back online, we |
| 946 | * can deal with it then. It is only because of unrecoverable errors |
| 947 | * that we would ever take a device offline in the first place. |
| 948 | */ |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 949 | if (!scsi_device_online(sdp)) { |
| 950 | set_media_not_present(sdkp); |
| 951 | retval = 1; |
| 952 | goto out; |
| 953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | /* |
| 956 | * Using TEST_UNIT_READY enables differentiation between drive with |
| 957 | * no cartridge loaded - NOT READY, drive with changed cartridge - |
| 958 | * UNIT ATTENTION, or with same cartridge - GOOD STATUS. |
| 959 | * |
| 960 | * Drives that auto spin down. eg iomega jaz 1G, will be started |
| 961 | * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever |
| 962 | * sd_revalidate() is called. |
| 963 | */ |
| 964 | retval = -ENODEV; |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 965 | |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 966 | if (scsi_block_when_processing_errors(sdp)) { |
| 967 | sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL); |
| 968 | retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES, |
| 969 | sshdr); |
| 970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
| 972 | /* |
| 973 | * Unable to test, unit probably not ready. This usually |
| 974 | * means there is no disc in the drive. Mark as changed, |
| 975 | * and we will figure it out later once the drive is |
| 976 | * available again. |
| 977 | */ |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 978 | if (retval || (scsi_sense_valid(sshdr) && |
| 979 | /* 0x3a is medium not present */ |
| 980 | sshdr->asc == 0x3a)) { |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 981 | set_media_not_present(sdkp); |
| 982 | retval = 1; |
| 983 | goto out; |
| 984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | /* |
| 987 | * For removable scsi disk we have to recognise the presence |
| 988 | * of a disk in the drive. This is kept in the struct scsi_disk |
| 989 | * struct and tested at open ! Daniel Roche (dan@lectra.fr) |
| 990 | */ |
| 991 | sdkp->media_present = 1; |
| 992 | |
| 993 | retval = sdp->changed; |
| 994 | sdp->changed = 0; |
Kay Sievers | 285e967 | 2007-08-14 14:10:39 +0200 | [diff] [blame] | 995 | out: |
| 996 | if (retval != sdkp->previous_state) |
| 997 | sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL); |
| 998 | sdkp->previous_state = retval; |
James Bottomley | 001aac2 | 2007-12-02 19:10:40 +0200 | [diff] [blame] | 999 | kfree(sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1003 | static int sd_sync_cache(struct scsi_disk *sdkp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | int retries, res; |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1006 | struct scsi_device *sdp = sdkp->device; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1007 | struct scsi_sense_hdr sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (!scsi_device_online(sdp)) |
| 1010 | return -ENODEV; |
| 1011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | for (retries = 3; retries > 0; --retries) { |
| 1014 | unsigned char cmd[10] = { 0 }; |
| 1015 | |
| 1016 | cmd[0] = SYNCHRONIZE_CACHE; |
| 1017 | /* |
| 1018 | * Leave the rest of the command zero to indicate |
| 1019 | * flush everything. |
| 1020 | */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1021 | res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 1022 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1023 | if (res == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | break; |
| 1025 | } |
| 1026 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1027 | if (res) { |
| 1028 | sd_print_result(sdkp, res); |
| 1029 | if (driver_byte(res) & DRIVER_SENSE) |
| 1030 | sd_print_sense_hdr(sdkp, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Tejun Heo | 3721050 | 2007-03-21 00:07:18 +0900 | [diff] [blame] | 1033 | if (res) |
| 1034 | return -EIO; |
| 1035 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1038 | static void sd_prepare_flush(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 1040 | rq->cmd_type = REQ_TYPE_BLOCK_PC; |
James Bottomley | c0ed79a | 2005-11-08 09:21:07 -0500 | [diff] [blame] | 1041 | rq->timeout = SD_TIMEOUT; |
| 1042 | rq->cmd[0] = SYNCHRONIZE_CACHE; |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 1043 | rq->cmd_len = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static void sd_rescan(struct device *dev) |
| 1047 | { |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 1048 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
| 1049 | |
| 1050 | if (sdkp) { |
Andrew Patterson | f98a8ca | 2008-09-04 14:27:35 -0600 | [diff] [blame] | 1051 | revalidate_disk(sdkp->disk); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 1052 | scsi_disk_put(sdkp); |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | #ifdef CONFIG_COMPAT |
| 1058 | /* |
| 1059 | * This gets directly called from VFS. When the ioctl |
| 1060 | * is not recognized we go back to the other translation paths. |
| 1061 | */ |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1062 | static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1063 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | { |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1065 | struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * If we are in the middle of error recovery, don't let anyone |
| 1069 | * else try and use this device. Also, if error recovery fails, it |
| 1070 | * may try and take the device offline, in which case all further |
| 1071 | * access to the device is prohibited. |
| 1072 | */ |
| 1073 | if (!scsi_block_when_processing_errors(sdev)) |
| 1074 | return -ENODEV; |
| 1075 | |
| 1076 | if (sdev->host->hostt->compat_ioctl) { |
| 1077 | int ret; |
| 1078 | |
| 1079 | ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); |
| 1080 | |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Let the static ioctl translation table take care of it. |
| 1086 | */ |
| 1087 | return -ENOIOCTLCMD; |
| 1088 | } |
| 1089 | #endif |
| 1090 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1091 | static const struct block_device_operations sd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | .owner = THIS_MODULE, |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1093 | .open = sd_open, |
| 1094 | .release = sd_release, |
| 1095 | .locked_ioctl = sd_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1096 | .getgeo = sd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | #ifdef CONFIG_COMPAT |
Al Viro | 0338e29 | 2008-03-02 10:41:04 -0500 | [diff] [blame] | 1098 | .compat_ioctl = sd_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | #endif |
| 1100 | .media_changed = sd_media_changed, |
| 1101 | .revalidate_disk = sd_revalidate_disk, |
| 1102 | }; |
| 1103 | |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1104 | static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd) |
| 1105 | { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1106 | u64 start_lba = blk_rq_pos(scmd->request); |
| 1107 | u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512); |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1108 | u64 bad_lba; |
| 1109 | int info_valid; |
| 1110 | |
| 1111 | if (!blk_fs_request(scmd->request)) |
| 1112 | return 0; |
| 1113 | |
| 1114 | info_valid = scsi_get_sense_info_fld(scmd->sense_buffer, |
| 1115 | SCSI_SENSE_BUFFERSIZE, |
| 1116 | &bad_lba); |
| 1117 | if (!info_valid) |
| 1118 | return 0; |
| 1119 | |
| 1120 | if (scsi_bufflen(scmd) <= scmd->device->sector_size) |
| 1121 | return 0; |
| 1122 | |
| 1123 | if (scmd->device->sector_size < 512) { |
| 1124 | /* only legitimate sector_size here is 256 */ |
| 1125 | start_lba <<= 1; |
| 1126 | end_lba <<= 1; |
| 1127 | } else { |
| 1128 | /* be careful ... don't want any overflows */ |
| 1129 | u64 factor = scmd->device->sector_size / 512; |
| 1130 | do_div(start_lba, factor); |
| 1131 | do_div(end_lba, factor); |
| 1132 | } |
| 1133 | |
| 1134 | /* The bad lba was reported incorrectly, we have no idea where |
| 1135 | * the error is. |
| 1136 | */ |
| 1137 | if (bad_lba < start_lba || bad_lba >= end_lba) |
| 1138 | return 0; |
| 1139 | |
| 1140 | /* This computation should always be done in terms of |
| 1141 | * the resolution of the device's medium. |
| 1142 | */ |
| 1143 | return (bad_lba - start_lba) * scmd->device->sector_size; |
| 1144 | } |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | /** |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1147 | * sd_done - bottom half handler: called when the lower level |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | * driver has completed (successfully or otherwise) a scsi command. |
| 1149 | * @SCpnt: mid-level's per command structure. |
| 1150 | * |
| 1151 | * Note: potentially run from within an ISR. Must not block. |
| 1152 | **/ |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1153 | static int sd_done(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | { |
| 1155 | int result = SCpnt->result; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1156 | unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | struct scsi_sense_hdr sshdr; |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1158 | struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | int sense_valid = 0; |
| 1160 | int sense_deferred = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
| 1162 | if (result) { |
| 1163 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); |
| 1164 | if (sense_valid) |
| 1165 | sense_deferred = scsi_sense_is_deferred(&sshdr); |
| 1166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | #ifdef CONFIG_SCSI_LOGGING |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1168 | SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (sense_valid) { |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1170 | SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt, |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1171 | "sd_done: sb[respc,sk,asc," |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 1172 | "ascq]=%x,%x,%x,%x\n", |
| 1173 | sshdr.response_code, |
| 1174 | sshdr.sense_key, sshdr.asc, |
| 1175 | sshdr.ascq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | #endif |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1178 | if (driver_byte(result) != DRIVER_SENSE && |
| 1179 | (!sense_valid || sense_deferred)) |
| 1180 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1182 | switch (sshdr.sense_key) { |
| 1183 | case HARDWARE_ERROR: |
| 1184 | case MEDIUM_ERROR: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1185 | good_bytes = sd_completed_bytes(SCpnt); |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1186 | break; |
| 1187 | case RECOVERED_ERROR: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1188 | good_bytes = scsi_bufflen(SCpnt); |
| 1189 | break; |
Jamie Wellnitz | 10dab22 | 2008-09-11 21:39:36 -0400 | [diff] [blame] | 1190 | case NO_SENSE: |
| 1191 | /* This indicates a false check condition, so ignore it. An |
| 1192 | * unknown amount of data was transferred so treat it as an |
| 1193 | * error. |
| 1194 | */ |
| 1195 | scsi_print_sense("sd", SCpnt); |
| 1196 | SCpnt->result = 0; |
| 1197 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
| 1198 | break; |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1199 | case ABORTED_COMMAND: |
| 1200 | if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */ |
| 1201 | scsi_print_result(SCpnt); |
| 1202 | scsi_print_sense("sd", SCpnt); |
| 1203 | good_bytes = sd_completed_bytes(SCpnt); |
| 1204 | } |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1205 | break; |
| 1206 | case ILLEGAL_REQUEST: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1207 | if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */ |
| 1208 | scsi_print_result(SCpnt); |
| 1209 | scsi_print_sense("sd", SCpnt); |
| 1210 | good_bytes = sd_completed_bytes(SCpnt); |
| 1211 | } |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1212 | break; |
| 1213 | default: |
| 1214 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | } |
Luben Tuikov | 03aba2f | 2006-06-23 09:39:09 -0700 | [diff] [blame] | 1216 | out: |
Martin K. Petersen | af55ff6 | 2008-07-17 04:28:35 -0400 | [diff] [blame] | 1217 | if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt)) |
| 1218 | sd_dif_complete(SCpnt, good_bytes); |
| 1219 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1220 | if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type) |
| 1221 | == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd) |
| 1222 | mempool_free(SCpnt->cmnd, sd_cdb_pool); |
| 1223 | |
Linus Torvalds | 7b3d954 | 2008-01-06 10:17:12 -0800 | [diff] [blame] | 1224 | return good_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1227 | static int media_not_present(struct scsi_disk *sdkp, |
| 1228 | struct scsi_sense_hdr *sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1231 | if (!scsi_sense_valid(sshdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | return 0; |
| 1233 | /* not invoked for commands that could return deferred errors */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1234 | if (sshdr->sense_key != NOT_READY && |
| 1235 | sshdr->sense_key != UNIT_ATTENTION) |
| 1236 | return 0; |
| 1237 | if (sshdr->asc != 0x3A) /* medium not present */ |
| 1238 | return 0; |
| 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | set_media_not_present(sdkp); |
| 1241 | return 1; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * spinup disk - called only in sd_revalidate_disk() |
| 1246 | */ |
| 1247 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1248 | sd_spinup_disk(struct scsi_disk *sdkp) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1249 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | unsigned char cmd[10]; |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 1251 | unsigned long spintime_expire = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | int retries, spintime; |
| 1253 | unsigned int the_result; |
| 1254 | struct scsi_sense_hdr sshdr; |
| 1255 | int sense_valid = 0; |
| 1256 | |
| 1257 | spintime = 0; |
| 1258 | |
| 1259 | /* Spin up drives, as required. Only do this at boot time */ |
| 1260 | /* Spinup needs to be done for module loads too. */ |
| 1261 | do { |
| 1262 | retries = 0; |
| 1263 | |
| 1264 | do { |
| 1265 | cmd[0] = TEST_UNIT_READY; |
| 1266 | memset((void *) &cmd[1], 0, 9); |
| 1267 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1268 | the_result = scsi_execute_req(sdkp->device, cmd, |
| 1269 | DMA_NONE, NULL, 0, |
| 1270 | &sshdr, SD_TIMEOUT, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 1271 | SD_MAX_RETRIES, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
Alan Stern | b4d38e3 | 2006-10-11 16:48:28 -0400 | [diff] [blame] | 1273 | /* |
| 1274 | * If the drive has indicated to us that it |
| 1275 | * doesn't have any media in it, don't bother |
| 1276 | * with any more polling. |
| 1277 | */ |
| 1278 | if (media_not_present(sdkp, &sshdr)) |
| 1279 | return; |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | if (the_result) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1282 | sense_valid = scsi_sense_valid(&sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | retries++; |
| 1284 | } while (retries < 3 && |
| 1285 | (!scsi_status_is_good(the_result) || |
| 1286 | ((driver_byte(the_result) & DRIVER_SENSE) && |
| 1287 | sense_valid && sshdr.sense_key == UNIT_ATTENTION))); |
| 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if ((driver_byte(the_result) & DRIVER_SENSE) == 0) { |
| 1290 | /* no sense, TUR either succeeded or failed |
| 1291 | * with a status error */ |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1292 | if(!spintime && !scsi_status_is_good(the_result)) { |
| 1293 | sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); |
| 1294 | sd_print_result(sdkp, the_result); |
| 1295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * The device does not want the automatic start to be issued. |
| 1301 | */ |
Matthew Wilcox | 33dd6f9 | 2009-02-20 06:53:48 -0700 | [diff] [blame] | 1302 | if (sdkp->device->no_start_on_add) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Matthew Wilcox | 33dd6f9 | 2009-02-20 06:53:48 -0700 | [diff] [blame] | 1305 | if (sense_valid && sshdr.sense_key == NOT_READY) { |
| 1306 | if (sshdr.asc == 4 && sshdr.ascq == 3) |
| 1307 | break; /* manual intervention required */ |
| 1308 | if (sshdr.asc == 4 && sshdr.ascq == 0xb) |
| 1309 | break; /* standby */ |
| 1310 | if (sshdr.asc == 4 && sshdr.ascq == 0xc) |
| 1311 | break; /* unavailable */ |
| 1312 | /* |
| 1313 | * Issue command to spin up drive when not ready |
| 1314 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (!spintime) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1316 | sd_printk(KERN_NOTICE, sdkp, "Spinning up disk..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | cmd[0] = START_STOP; |
| 1318 | cmd[1] = 1; /* Return immediately */ |
| 1319 | memset((void *) &cmd[2], 0, 8); |
| 1320 | cmd[4] = 1; /* Start spin cycle */ |
Stefan Richter | d2886ea | 2008-05-11 00:34:07 +0200 | [diff] [blame] | 1321 | if (sdkp->device->start_stop_pwr_cond) |
| 1322 | cmd[4] |= 1 << 4; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1323 | scsi_execute_req(sdkp->device, cmd, DMA_NONE, |
| 1324 | NULL, 0, &sshdr, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 1325 | SD_TIMEOUT, SD_MAX_RETRIES, |
| 1326 | NULL); |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 1327 | spintime_expire = jiffies + 100 * HZ; |
| 1328 | spintime = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | /* Wait 1 second for next try */ |
| 1331 | msleep(1000); |
| 1332 | printk("."); |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 1333 | |
| 1334 | /* |
| 1335 | * Wait for USB flash devices with slow firmware. |
| 1336 | * Yes, this sense key/ASC combination shouldn't |
| 1337 | * occur here. It's characteristic of these devices. |
| 1338 | */ |
| 1339 | } else if (sense_valid && |
| 1340 | sshdr.sense_key == UNIT_ATTENTION && |
| 1341 | sshdr.asc == 0x28) { |
| 1342 | if (!spintime) { |
| 1343 | spintime_expire = jiffies + 5 * HZ; |
| 1344 | spintime = 1; |
| 1345 | } |
| 1346 | /* Wait 1 second for next try */ |
| 1347 | msleep(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } else { |
| 1349 | /* we don't understand the sense code, so it's |
| 1350 | * probably pointless to loop */ |
| 1351 | if(!spintime) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1352 | sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); |
| 1353 | sd_print_sense_hdr(sdkp, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
| 1355 | break; |
| 1356 | } |
| 1357 | |
Alan Stern | 4451e47 | 2005-07-12 10:45:17 -0400 | [diff] [blame] | 1358 | } while (spintime && time_before_eq(jiffies, spintime_expire)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
| 1360 | if (spintime) { |
| 1361 | if (scsi_status_is_good(the_result)) |
| 1362 | printk("ready\n"); |
| 1363 | else |
| 1364 | printk("not responding...\n"); |
| 1365 | } |
| 1366 | } |
| 1367 | |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 1368 | |
| 1369 | /* |
| 1370 | * Determine whether disk supports Data Integrity Field. |
| 1371 | */ |
| 1372 | void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer) |
| 1373 | { |
| 1374 | struct scsi_device *sdp = sdkp->device; |
| 1375 | u8 type; |
| 1376 | |
| 1377 | if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 1378 | return; |
| 1379 | |
| 1380 | type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */ |
| 1381 | |
| 1382 | if (type == sdkp->protection_type || !sdkp->first_scan) |
| 1383 | return; |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 1384 | |
Martin K. Petersen | be922f4 | 2008-09-19 18:47:20 -0400 | [diff] [blame] | 1385 | sdkp->protection_type = type; |
| 1386 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 1387 | if (type > SD_DIF_TYPE3_PROTECTION) { |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 1388 | sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \ |
| 1389 | "protection type %u. Disabling disk!\n", type); |
| 1390 | sdkp->capacity = 0; |
| 1391 | return; |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
Martin K. Petersen | 35e1a5d | 2009-09-18 17:33:00 -0400 | [diff] [blame] | 1394 | if (scsi_host_dif_capable(sdp->host, type)) |
| 1395 | sd_printk(KERN_NOTICE, sdkp, |
| 1396 | "Enabling DIF Type %u protection\n", type); |
| 1397 | else |
| 1398 | sd_printk(KERN_NOTICE, sdkp, |
| 1399 | "Disabling DIF Type %u protection\n", type); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 1400 | } |
| 1401 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1402 | static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 1403 | struct scsi_sense_hdr *sshdr, int sense_valid, |
| 1404 | int the_result) |
| 1405 | { |
| 1406 | sd_print_result(sdkp, the_result); |
| 1407 | if (driver_byte(the_result) & DRIVER_SENSE) |
| 1408 | sd_print_sense_hdr(sdkp, sshdr); |
| 1409 | else |
| 1410 | sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n"); |
| 1411 | |
| 1412 | /* |
| 1413 | * Set dirty bit for removable devices if not ready - |
| 1414 | * sometimes drives will not report this properly. |
| 1415 | */ |
| 1416 | if (sdp->removable && |
| 1417 | sense_valid && sshdr->sense_key == NOT_READY) |
| 1418 | sdp->changed = 1; |
| 1419 | |
| 1420 | /* |
| 1421 | * We used to set media_present to 0 here to indicate no media |
| 1422 | * in the drive, but some drives fail read capacity even with |
| 1423 | * media present, so we can't do that. |
| 1424 | */ |
| 1425 | sdkp->capacity = 0; /* unknown mapped to zero - as usual */ |
| 1426 | } |
| 1427 | |
| 1428 | #define RC16_LEN 32 |
| 1429 | #if RC16_LEN > SD_BUF_SIZE |
| 1430 | #error RC16_LEN must not be more than SD_BUF_SIZE |
| 1431 | #endif |
| 1432 | |
| 1433 | static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 1434 | unsigned char *buffer) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1435 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | unsigned char cmd[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | struct scsi_sense_hdr sshdr; |
| 1438 | int sense_valid = 0; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1439 | int the_result; |
| 1440 | int retries = 3; |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1441 | unsigned int alignment; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1442 | unsigned long long lba; |
| 1443 | unsigned sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | do { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1446 | memset(cmd, 0, 16); |
| 1447 | cmd[0] = SERVICE_ACTION_IN; |
| 1448 | cmd[1] = SAI_READ_CAPACITY_16; |
| 1449 | cmd[13] = RC16_LEN; |
| 1450 | memset(buffer, 0, RC16_LEN); |
| 1451 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1452 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1453 | buffer, RC16_LEN, &sshdr, |
| 1454 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1456 | if (media_not_present(sdkp, &sshdr)) |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1457 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 1459 | if (the_result) { |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1460 | sense_valid = scsi_sense_valid(&sshdr); |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 1461 | if (sense_valid && |
| 1462 | sshdr.sense_key == ILLEGAL_REQUEST && |
| 1463 | (sshdr.asc == 0x20 || sshdr.asc == 0x24) && |
| 1464 | sshdr.ascq == 0x00) |
| 1465 | /* Invalid Command Operation Code or |
| 1466 | * Invalid Field in CDB, just retry |
| 1467 | * silently with RC10 */ |
| 1468 | return -EINVAL; |
| 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | retries--; |
| 1471 | |
| 1472 | } while (the_result && retries); |
| 1473 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1474 | if (the_result) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1475 | sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n"); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1476 | read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); |
| 1477 | return -EINVAL; |
| 1478 | } |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1479 | |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 1480 | sector_size = get_unaligned_be32(&buffer[8]); |
| 1481 | lba = get_unaligned_be64(&buffer[0]); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1482 | |
| 1483 | sd_read_protection_type(sdkp, buffer); |
| 1484 | |
| 1485 | if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) { |
| 1486 | sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a " |
| 1487 | "kernel compiled with support for large block " |
| 1488 | "devices.\n"); |
| 1489 | sdkp->capacity = 0; |
| 1490 | return -EOVERFLOW; |
| 1491 | } |
| 1492 | |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1493 | /* Logical blocks per physical block exponent */ |
| 1494 | sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size; |
| 1495 | |
| 1496 | /* Lowest aligned logical block */ |
| 1497 | alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size; |
| 1498 | blk_queue_alignment_offset(sdp->request_queue, alignment); |
| 1499 | if (alignment && sdkp->first_scan) |
| 1500 | sd_printk(KERN_NOTICE, sdkp, |
| 1501 | "physical block alignment offset: %u\n", alignment); |
| 1502 | |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 1503 | if (buffer[14] & 0x80) { /* TPE */ |
| 1504 | struct request_queue *q = sdp->request_queue; |
| 1505 | |
| 1506 | sdkp->thin_provisioning = 1; |
| 1507 | q->limits.discard_granularity = sdkp->hw_sector_size; |
| 1508 | q->limits.max_discard_sectors = 0xffffffff; |
| 1509 | |
| 1510 | if (buffer[14] & 0x40) /* TPRZ */ |
| 1511 | q->limits.discard_zeroes_data = 1; |
| 1512 | |
| 1513 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 1514 | } |
| 1515 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1516 | sdkp->capacity = lba + 1; |
| 1517 | return sector_size; |
| 1518 | } |
| 1519 | |
| 1520 | static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp, |
| 1521 | unsigned char *buffer) |
| 1522 | { |
| 1523 | unsigned char cmd[16]; |
| 1524 | struct scsi_sense_hdr sshdr; |
| 1525 | int sense_valid = 0; |
| 1526 | int the_result; |
| 1527 | int retries = 3; |
| 1528 | sector_t lba; |
| 1529 | unsigned sector_size; |
| 1530 | |
| 1531 | do { |
| 1532 | cmd[0] = READ_CAPACITY; |
| 1533 | memset(&cmd[1], 0, 9); |
| 1534 | memset(buffer, 0, 8); |
| 1535 | |
| 1536 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
| 1537 | buffer, 8, &sshdr, |
| 1538 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
| 1539 | |
| 1540 | if (media_not_present(sdkp, &sshdr)) |
| 1541 | return -ENODEV; |
| 1542 | |
| 1543 | if (the_result) |
| 1544 | sense_valid = scsi_sense_valid(&sshdr); |
| 1545 | retries--; |
| 1546 | |
| 1547 | } while (the_result && retries); |
| 1548 | |
| 1549 | if (the_result) { |
| 1550 | sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n"); |
| 1551 | read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result); |
| 1552 | return -EINVAL; |
| 1553 | } |
| 1554 | |
Dave Hansen | 8f76d15 | 2009-04-21 16:43:27 -0700 | [diff] [blame] | 1555 | sector_size = get_unaligned_be32(&buffer[4]); |
| 1556 | lba = get_unaligned_be32(&buffer[0]); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1557 | |
| 1558 | if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) { |
| 1559 | sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a " |
| 1560 | "kernel compiled with support for large block " |
| 1561 | "devices.\n"); |
| 1562 | sdkp->capacity = 0; |
| 1563 | return -EOVERFLOW; |
| 1564 | } |
| 1565 | |
| 1566 | sdkp->capacity = lba + 1; |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1567 | sdkp->hw_sector_size = sector_size; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1568 | return sector_size; |
| 1569 | } |
| 1570 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 1571 | static int sd_try_rc16_first(struct scsi_device *sdp) |
| 1572 | { |
| 1573 | if (sdp->scsi_level > SCSI_SPC_2) |
| 1574 | return 1; |
| 1575 | if (scsi_device_protection(sdp)) |
| 1576 | return 1; |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1580 | /* |
| 1581 | * read disk capacity |
| 1582 | */ |
| 1583 | static void |
| 1584 | sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) |
| 1585 | { |
| 1586 | int sector_size; |
| 1587 | struct scsi_device *sdp = sdkp->device; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1588 | sector_t old_capacity = sdkp->capacity; |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1589 | |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 1590 | if (sd_try_rc16_first(sdp)) { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1591 | sector_size = read_capacity_16(sdkp, sdp, buffer); |
| 1592 | if (sector_size == -EOVERFLOW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | goto got_data; |
Matthew Wilcox | 2b30130 | 2009-03-12 14:20:30 -0400 | [diff] [blame] | 1594 | if (sector_size == -ENODEV) |
| 1595 | return; |
| 1596 | if (sector_size < 0) |
| 1597 | sector_size = read_capacity_10(sdkp, sdp, buffer); |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1598 | if (sector_size < 0) |
| 1599 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | } else { |
Matthew Wilcox | 0da205e | 2009-03-12 14:20:29 -0400 | [diff] [blame] | 1601 | sector_size = read_capacity_10(sdkp, sdp, buffer); |
| 1602 | if (sector_size == -EOVERFLOW) |
| 1603 | goto got_data; |
| 1604 | if (sector_size < 0) |
| 1605 | return; |
| 1606 | if ((sizeof(sdkp->capacity) > 4) && |
| 1607 | (sdkp->capacity > 0xffffffffULL)) { |
| 1608 | int old_sector_size = sector_size; |
| 1609 | sd_printk(KERN_NOTICE, sdkp, "Very big device. " |
| 1610 | "Trying to use READ CAPACITY(16).\n"); |
| 1611 | sector_size = read_capacity_16(sdkp, sdp, buffer); |
| 1612 | if (sector_size < 0) { |
| 1613 | sd_printk(KERN_NOTICE, sdkp, |
| 1614 | "Using 0xffffffff as device size\n"); |
| 1615 | sdkp->capacity = 1 + (sector_t) 0xffffffff; |
| 1616 | sector_size = old_sector_size; |
| 1617 | goto got_data; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Alan Stern | 5c211ca | 2009-02-18 10:54:44 -0500 | [diff] [blame] | 1622 | /* Some devices are known to return the total number of blocks, |
| 1623 | * not the highest block number. Some devices have versions |
| 1624 | * which do this and others which do not. Some devices we might |
| 1625 | * suspect of doing this but we don't know for certain. |
| 1626 | * |
| 1627 | * If we know the reported capacity is wrong, decrement it. If |
| 1628 | * we can only guess, then assume the number of blocks is even |
| 1629 | * (usually true but not always) and err on the side of lowering |
| 1630 | * the capacity. |
| 1631 | */ |
| 1632 | if (sdp->fix_capacity || |
| 1633 | (sdp->guess_capacity && (sdkp->capacity & 0x01))) { |
| 1634 | sd_printk(KERN_INFO, sdkp, "Adjusting the sector count " |
| 1635 | "from its reported value: %llu\n", |
| 1636 | (unsigned long long) sdkp->capacity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | --sdkp->capacity; |
Oliver Neukum | 61bf54b | 2007-02-08 09:04:48 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | got_data: |
| 1641 | if (sector_size == 0) { |
| 1642 | sector_size = 512; |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1643 | sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, " |
| 1644 | "assuming 512.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | if (sector_size != 512 && |
| 1648 | sector_size != 1024 && |
| 1649 | sector_size != 2048 && |
| 1650 | sector_size != 4096 && |
| 1651 | sector_size != 256) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1652 | sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n", |
| 1653 | sector_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | /* |
| 1655 | * The user might want to re-format the drive with |
| 1656 | * a supported sectorsize. Once this happens, it |
| 1657 | * would be relatively trivial to set the thing up. |
| 1658 | * For this reason, we leave the thing in the table. |
| 1659 | */ |
| 1660 | sdkp->capacity = 0; |
| 1661 | /* |
| 1662 | * set a bogus sector size so the normal read/write |
| 1663 | * logic in the block layer will eventually refuse any |
| 1664 | * request on this device without tripping over power |
| 1665 | * of two sector size assumptions |
| 1666 | */ |
| 1667 | sector_size = 512; |
| 1668 | } |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1669 | blk_queue_logical_block_size(sdp->request_queue, sector_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
James Bottomley | 7404ad3 | 2008-08-31 10:41:52 -0500 | [diff] [blame] | 1671 | { |
| 1672 | char cap_str_2[10], cap_str_10[10]; |
H. Peter Anvin | 520a2c2 | 2008-10-14 11:34:20 -0700 | [diff] [blame] | 1673 | u64 sz = (u64)sdkp->capacity << ilog2(sector_size); |
James Bottomley | 7404ad3 | 2008-08-31 10:41:52 -0500 | [diff] [blame] | 1674 | |
| 1675 | string_get_size(sz, STRING_UNITS_2, cap_str_2, |
| 1676 | sizeof(cap_str_2)); |
| 1677 | string_get_size(sz, STRING_UNITS_10, cap_str_10, |
| 1678 | sizeof(cap_str_10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1680 | if (sdkp->first_scan || old_capacity != sdkp->capacity) { |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1681 | sd_printk(KERN_NOTICE, sdkp, |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1682 | "%llu %d-byte logical blocks: (%s/%s)\n", |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1683 | (unsigned long long)sdkp->capacity, |
| 1684 | sector_size, cap_str_10, cap_str_2); |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1685 | |
| 1686 | if (sdkp->hw_sector_size != sector_size) |
| 1687 | sd_printk(KERN_NOTICE, sdkp, |
| 1688 | "%u-byte physical blocks\n", |
| 1689 | sdkp->hw_sector_size); |
| 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /* Rescale capacity to 512-byte units */ |
| 1694 | if (sector_size == 4096) |
| 1695 | sdkp->capacity <<= 3; |
| 1696 | else if (sector_size == 2048) |
| 1697 | sdkp->capacity <<= 2; |
| 1698 | else if (sector_size == 1024) |
| 1699 | sdkp->capacity <<= 1; |
| 1700 | else if (sector_size == 256) |
| 1701 | sdkp->capacity >>= 1; |
| 1702 | |
Martin K. Petersen | ea09bcc | 2009-05-23 11:43:37 -0400 | [diff] [blame] | 1703 | blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | sdkp->device->sector_size = sector_size; |
| 1705 | } |
| 1706 | |
| 1707 | /* called with buffer of length 512 */ |
| 1708 | static inline int |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1709 | sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage, |
| 1710 | unsigned char *buffer, int len, struct scsi_mode_data *data, |
| 1711 | struct scsi_sense_hdr *sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | { |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1713 | return scsi_mode_sense(sdp, dbd, modepage, buffer, len, |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 1714 | SD_TIMEOUT, SD_MAX_RETRIES, data, |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1715 | sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * read write protect setting, if possible - called only in sd_revalidate_disk() |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1720 | * called with buffer of length SD_BUF_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | */ |
| 1722 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1723 | sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1724 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | int res; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1726 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | struct scsi_mode_data data; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1728 | int old_wp = sdkp->write_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
| 1730 | set_disk_ro(sdkp->disk, 0); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1731 | if (sdp->skip_ms_page_3f) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1732 | sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | return; |
| 1734 | } |
| 1735 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1736 | if (sdp->use_192_bytes_for_3f) { |
| 1737 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | } else { |
| 1739 | /* |
| 1740 | * First attempt: ask for all pages (0x3F), but only 4 bytes. |
| 1741 | * We have to start carefully: some devices hang if we ask |
| 1742 | * for more than is available. |
| 1743 | */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1744 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* |
| 1747 | * Second attempt: ask for page 0 When only page 0 is |
| 1748 | * implemented, a request for page 3F may return Sense Key |
| 1749 | * 5: Illegal Request, Sense Code 24: Invalid field in |
| 1750 | * CDB. |
| 1751 | */ |
| 1752 | if (!scsi_status_is_good(res)) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1753 | res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
| 1755 | /* |
| 1756 | * Third attempt: ask 255 bytes, as we did earlier. |
| 1757 | */ |
| 1758 | if (!scsi_status_is_good(res)) |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1759 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255, |
| 1760 | &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | if (!scsi_status_is_good(res)) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1764 | sd_printk(KERN_WARNING, sdkp, |
| 1765 | "Test WP failed, assume Write Enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | } else { |
| 1767 | sdkp->write_prot = ((data.device_specific & 0x80) != 0); |
| 1768 | set_disk_ro(sdkp->disk, sdkp->write_prot); |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1769 | if (sdkp->first_scan || old_wp != sdkp->write_prot) { |
| 1770 | sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n", |
| 1771 | sdkp->write_prot ? "on" : "off"); |
| 1772 | sd_printk(KERN_DEBUG, sdkp, |
| 1773 | "Mode Sense: %02x %02x %02x %02x\n", |
| 1774 | buffer[0], buffer[1], buffer[2], buffer[3]); |
| 1775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * sd_read_cache_type - called only from sd_revalidate_disk() |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1781 | * called with buffer of length SD_BUF_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | */ |
| 1783 | static void |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1784 | sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer) |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1785 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | int len = 0, res; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1787 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1789 | int dbd; |
| 1790 | int modepage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | struct scsi_mode_data data; |
| 1792 | struct scsi_sense_hdr sshdr; |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1793 | int old_wce = sdkp->WCE; |
| 1794 | int old_rcd = sdkp->RCD; |
| 1795 | int old_dpofua = sdkp->DPOFUA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1797 | if (sdp->skip_ms_page_8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | goto defaults; |
| 1799 | |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1800 | if (sdp->type == TYPE_RBC) { |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1801 | modepage = 6; |
| 1802 | dbd = 8; |
| 1803 | } else { |
| 1804 | modepage = 8; |
| 1805 | dbd = 0; |
| 1806 | } |
| 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | /* cautiously ask */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1809 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
| 1811 | if (!scsi_status_is_good(res)) |
| 1812 | goto bad_sense; |
| 1813 | |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 1814 | if (!data.header_length) { |
| 1815 | modepage = 6; |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1816 | sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n"); |
Al Viro | 6d73c85 | 2006-02-23 02:03:16 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | /* that went OK, now ask for the proper length */ |
| 1820 | len = data.length; |
| 1821 | |
| 1822 | /* |
| 1823 | * We're only interested in the first three bytes, actually. |
| 1824 | * But the data cache page is defined for the first 20. |
| 1825 | */ |
| 1826 | if (len < 3) |
| 1827 | goto bad_sense; |
| 1828 | if (len > 20) |
| 1829 | len = 20; |
| 1830 | |
| 1831 | /* Take headers and block descriptors into account */ |
| 1832 | len += data.header_length + data.block_descriptor_length; |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1833 | if (len > SD_BUF_SIZE) |
| 1834 | goto bad_sense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
| 1836 | /* Get the data */ |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1837 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
| 1839 | if (scsi_status_is_good(res)) { |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1840 | int offset = data.header_length + data.block_descriptor_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1842 | if (offset >= SD_BUF_SIZE - 2) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1843 | sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n"); |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1844 | goto defaults; |
| 1845 | } |
| 1846 | |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1847 | if ((buffer[offset] & 0x3f) != modepage) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1848 | sd_printk(KERN_ERR, sdkp, "Got wrong page\n"); |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 1849 | goto defaults; |
| 1850 | } |
| 1851 | |
| 1852 | if (modepage == 8) { |
| 1853 | sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0); |
| 1854 | sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0); |
| 1855 | } else { |
| 1856 | sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0); |
| 1857 | sdkp->RCD = 0; |
| 1858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 1860 | sdkp->DPOFUA = (data.device_specific & 0x10) != 0; |
| 1861 | if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1862 | sd_printk(KERN_NOTICE, sdkp, |
| 1863 | "Uses READ/WRITE(6), disabling FUA\n"); |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 1864 | sdkp->DPOFUA = 0; |
| 1865 | } |
| 1866 | |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 1867 | if (sdkp->first_scan || old_wce != sdkp->WCE || |
| 1868 | old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA) |
| 1869 | sd_printk(KERN_NOTICE, sdkp, |
| 1870 | "Write cache: %s, read cache: %s, %s\n", |
| 1871 | sdkp->WCE ? "enabled" : "disabled", |
| 1872 | sdkp->RCD ? "disabled" : "enabled", |
| 1873 | sdkp->DPOFUA ? "supports DPO and FUA" |
| 1874 | : "doesn't support DPO or FUA"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | bad_sense: |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1880 | if (scsi_sense_valid(&sshdr) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | sshdr.sense_key == ILLEGAL_REQUEST && |
| 1882 | sshdr.asc == 0x24 && sshdr.ascq == 0x0) |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1883 | /* Invalid field in CDB */ |
| 1884 | sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | else |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1886 | sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | |
| 1888 | defaults: |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 1889 | sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | sdkp->WCE = 0; |
| 1891 | sdkp->RCD = 0; |
Al Viro | 4897080 | 2006-02-26 08:34:10 -0600 | [diff] [blame] | 1892 | sdkp->DPOFUA = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 1895 | /* |
| 1896 | * The ATO bit indicates whether the DIF application tag is available |
| 1897 | * for use by the operating system. |
| 1898 | */ |
| 1899 | void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer) |
| 1900 | { |
| 1901 | int res, offset; |
| 1902 | struct scsi_device *sdp = sdkp->device; |
| 1903 | struct scsi_mode_data data; |
| 1904 | struct scsi_sense_hdr sshdr; |
| 1905 | |
| 1906 | if (sdp->type != TYPE_DISK) |
| 1907 | return; |
| 1908 | |
| 1909 | if (sdkp->protection_type == 0) |
| 1910 | return; |
| 1911 | |
| 1912 | res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT, |
| 1913 | SD_MAX_RETRIES, &data, &sshdr); |
| 1914 | |
| 1915 | if (!scsi_status_is_good(res) || !data.header_length || |
| 1916 | data.length < 6) { |
| 1917 | sd_printk(KERN_WARNING, sdkp, |
| 1918 | "getting Control mode page failed, assume no ATO\n"); |
| 1919 | |
| 1920 | if (scsi_sense_valid(&sshdr)) |
| 1921 | sd_print_sense_hdr(sdkp, &sshdr); |
| 1922 | |
| 1923 | return; |
| 1924 | } |
| 1925 | |
| 1926 | offset = data.header_length + data.block_descriptor_length; |
| 1927 | |
| 1928 | if ((buffer[offset] & 0x3f) != 0x0a) { |
| 1929 | sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n"); |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | if ((buffer[offset + 5] & 0x80) == 0) |
| 1934 | return; |
| 1935 | |
| 1936 | sdkp->ATO = 1; |
| 1937 | |
| 1938 | return; |
| 1939 | } |
| 1940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | /** |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 1942 | * sd_read_block_limits - Query disk device for preferred I/O sizes. |
| 1943 | * @disk: disk to query |
| 1944 | */ |
| 1945 | static void sd_read_block_limits(struct scsi_disk *sdkp) |
| 1946 | { |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 1947 | struct request_queue *q = sdkp->disk->queue; |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 1948 | unsigned int sector_sz = sdkp->device->sector_size; |
| 1949 | char *buffer; |
| 1950 | |
| 1951 | /* Block Limits VPD */ |
| 1952 | buffer = scsi_get_vpd_page(sdkp->device, 0xb0); |
| 1953 | |
| 1954 | if (buffer == NULL) |
| 1955 | return; |
| 1956 | |
| 1957 | blk_queue_io_min(sdkp->disk->queue, |
| 1958 | get_unaligned_be16(&buffer[6]) * sector_sz); |
| 1959 | blk_queue_io_opt(sdkp->disk->queue, |
| 1960 | get_unaligned_be32(&buffer[12]) * sector_sz); |
| 1961 | |
Martin K. Petersen | e339c1a | 2009-11-26 12:00:40 -0500 | [diff] [blame] | 1962 | /* Thin provisioning enabled and page length indicates TP support */ |
| 1963 | if (sdkp->thin_provisioning && buffer[3] == 0x3c) { |
| 1964 | unsigned int lba_count, desc_count, granularity; |
| 1965 | |
| 1966 | lba_count = get_unaligned_be32(&buffer[20]); |
| 1967 | desc_count = get_unaligned_be32(&buffer[24]); |
| 1968 | |
| 1969 | if (lba_count) { |
| 1970 | q->limits.max_discard_sectors = |
| 1971 | lba_count * sector_sz >> 9; |
| 1972 | |
| 1973 | if (desc_count) |
| 1974 | sdkp->unmap = 1; |
| 1975 | } |
| 1976 | |
| 1977 | granularity = get_unaligned_be32(&buffer[28]); |
| 1978 | |
| 1979 | if (granularity) |
| 1980 | q->limits.discard_granularity = granularity * sector_sz; |
| 1981 | |
| 1982 | if (buffer[32] & 0x80) |
| 1983 | q->limits.discard_alignment = |
| 1984 | get_unaligned_be32(&buffer[32]) & ~(1 << 31); |
| 1985 | } |
| 1986 | |
Martin K. Petersen | d11b691 | 2009-05-23 11:43:39 -0400 | [diff] [blame] | 1987 | kfree(buffer); |
| 1988 | } |
| 1989 | |
| 1990 | /** |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 1991 | * sd_read_block_characteristics - Query block dev. characteristics |
| 1992 | * @disk: disk to query |
| 1993 | */ |
| 1994 | static void sd_read_block_characteristics(struct scsi_disk *sdkp) |
| 1995 | { |
| 1996 | char *buffer; |
| 1997 | u16 rot; |
| 1998 | |
| 1999 | /* Block Device Characteristics VPD */ |
| 2000 | buffer = scsi_get_vpd_page(sdkp->device, 0xb1); |
| 2001 | |
| 2002 | if (buffer == NULL) |
| 2003 | return; |
| 2004 | |
| 2005 | rot = get_unaligned_be16(&buffer[4]); |
| 2006 | |
| 2007 | if (rot == 1) |
| 2008 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue); |
| 2009 | |
| 2010 | kfree(buffer); |
| 2011 | } |
| 2012 | |
Martin K. Petersen | ffd4bc2 | 2009-07-29 14:06:53 -0400 | [diff] [blame] | 2013 | static int sd_try_extended_inquiry(struct scsi_device *sdp) |
| 2014 | { |
| 2015 | /* |
| 2016 | * Although VPD inquiries can go to SCSI-2 type devices, |
| 2017 | * some USB ones crash on receiving them, and the pages |
| 2018 | * we currently ask for are for SPC-3 and beyond |
| 2019 | */ |
| 2020 | if (sdp->scsi_level > SCSI_SPC_2) |
| 2021 | return 1; |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2025 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | * sd_revalidate_disk - called the first time a new disk is seen, |
| 2027 | * performs disk spin up, read_capacity, etc. |
| 2028 | * @disk: struct gendisk we care about |
| 2029 | **/ |
| 2030 | static int sd_revalidate_disk(struct gendisk *disk) |
| 2031 | { |
| 2032 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 2033 | struct scsi_device *sdp = sdkp->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | unsigned char *buffer; |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 2035 | unsigned ordered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
Martin K. Petersen | fa0d34b | 2007-02-27 22:41:19 -0500 | [diff] [blame] | 2037 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, |
| 2038 | "sd_revalidate_disk\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
| 2040 | /* |
| 2041 | * If the device is offline, don't try and read capacity or any |
| 2042 | * of the other niceties. |
| 2043 | */ |
| 2044 | if (!scsi_device_online(sdp)) |
| 2045 | goto out; |
| 2046 | |
Bernhard Walle | a6123f1 | 2007-05-21 17:15:26 +0200 | [diff] [blame] | 2047 | buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | if (!buffer) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2049 | sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " |
| 2050 | "allocation failure.\n"); |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 2051 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2054 | sd_spinup_disk(sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
| 2056 | /* |
| 2057 | * Without media there is no reason to ask; moreover, some devices |
| 2058 | * react badly if we do. |
| 2059 | */ |
| 2060 | if (sdkp->media_present) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2061 | sd_read_capacity(sdkp, buffer); |
Martin K. Petersen | ffd4bc2 | 2009-07-29 14:06:53 -0400 | [diff] [blame] | 2062 | |
| 2063 | if (sd_try_extended_inquiry(sdp)) { |
| 2064 | sd_read_block_limits(sdkp); |
| 2065 | sd_read_block_characteristics(sdkp); |
| 2066 | } |
| 2067 | |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2068 | sd_read_write_protect_flag(sdkp, buffer); |
| 2069 | sd_read_cache_type(sdkp, buffer); |
Martin K. Petersen | e0597d7 | 2008-07-17 04:28:34 -0400 | [diff] [blame] | 2070 | sd_read_app_tag_own(sdkp, buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 2072 | |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2073 | sdkp->first_scan = 0; |
| 2074 | |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 2075 | /* |
| 2076 | * We now have all cache related info, determine how we deal |
| 2077 | * with ordered requests. Note that as the current SCSI |
| 2078 | * dispatch function can alter request order, we cannot use |
| 2079 | * QUEUE_ORDERED_TAG_* even when ordered tag is supported. |
| 2080 | */ |
| 2081 | if (sdkp->WCE) |
Tejun Heo | 007365a | 2006-01-06 09:53:52 +0100 | [diff] [blame] | 2082 | ordered = sdkp->DPOFUA |
| 2083 | ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH; |
Tejun Heo | 461d4e9 | 2006-01-06 09:52:55 +0100 | [diff] [blame] | 2084 | else |
| 2085 | ordered = QUEUE_ORDERED_DRAIN; |
| 2086 | |
| 2087 | blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush); |
| 2088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | set_capacity(disk, sdkp->capacity); |
| 2090 | kfree(buffer); |
| 2091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | out: |
| 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | /** |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 2097 | * sd_format_disk_name - format disk name |
| 2098 | * @prefix: name prefix - ie. "sd" for SCSI disks |
| 2099 | * @index: index of the disk to format name for |
| 2100 | * @buf: output buffer |
| 2101 | * @buflen: length of the output buffer |
| 2102 | * |
| 2103 | * SCSI disk names starts at sda. The 26th device is sdz and the |
| 2104 | * 27th is sdaa. The last one for two lettered suffix is sdzz |
| 2105 | * which is followed by sdaaa. |
| 2106 | * |
| 2107 | * This is basically 26 base counting with one extra 'nil' entry |
| 2108 | * at the beggining from the second digit on and can be |
| 2109 | * determined using similar method as 26 base conversion with the |
| 2110 | * index shifted -1 after each digit is computed. |
| 2111 | * |
| 2112 | * CONTEXT: |
| 2113 | * Don't care. |
| 2114 | * |
| 2115 | * RETURNS: |
| 2116 | * 0 on success, -errno on failure. |
| 2117 | */ |
| 2118 | static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen) |
| 2119 | { |
| 2120 | const int base = 'z' - 'a' + 1; |
| 2121 | char *begin = buf + strlen(prefix); |
| 2122 | char *end = buf + buflen; |
| 2123 | char *p; |
| 2124 | int unit; |
| 2125 | |
| 2126 | p = end - 1; |
| 2127 | *p = '\0'; |
| 2128 | unit = base; |
| 2129 | do { |
| 2130 | if (p == begin) |
| 2131 | return -EINVAL; |
| 2132 | *--p = 'a' + (index % unit); |
| 2133 | index = (index / unit) - 1; |
| 2134 | } while (index >= 0); |
| 2135 | |
| 2136 | memmove(begin, p, end - p); |
| 2137 | memcpy(buf, prefix, strlen(prefix)); |
| 2138 | |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2142 | /* |
| 2143 | * The asynchronous part of sd_probe |
| 2144 | */ |
| 2145 | static void sd_probe_async(void *data, async_cookie_t cookie) |
| 2146 | { |
| 2147 | struct scsi_disk *sdkp = data; |
| 2148 | struct scsi_device *sdp; |
| 2149 | struct gendisk *gd; |
| 2150 | u32 index; |
| 2151 | struct device *dev; |
| 2152 | |
| 2153 | sdp = sdkp->device; |
| 2154 | gd = sdkp->disk; |
| 2155 | index = sdkp->index; |
| 2156 | dev = &sdp->sdev_gendev; |
| 2157 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2158 | if (index < SD_MAX_DISKS) { |
| 2159 | gd->major = sd_major((index & 0xf0) >> 4); |
| 2160 | gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); |
| 2161 | gd->minors = SD_MINORS; |
| 2162 | } |
| 2163 | gd->fops = &sd_fops; |
| 2164 | gd->private_data = &sdkp->driver; |
| 2165 | gd->queue = sdkp->device->request_queue; |
| 2166 | |
Martin K. Petersen | 70a9b87 | 2009-03-09 11:33:31 -0400 | [diff] [blame] | 2167 | /* defaults, until the device tells us otherwise */ |
| 2168 | sdp->sector_size = 512; |
| 2169 | sdkp->capacity = 0; |
| 2170 | sdkp->media_present = 1; |
| 2171 | sdkp->write_prot = 0; |
| 2172 | sdkp->WCE = 0; |
| 2173 | sdkp->RCD = 0; |
| 2174 | sdkp->ATO = 0; |
| 2175 | sdkp->first_scan = 1; |
| 2176 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2177 | sd_revalidate_disk(gd); |
| 2178 | |
| 2179 | blk_queue_prep_rq(sdp->request_queue, sd_prep_fn); |
| 2180 | |
| 2181 | gd->driverfs_dev = &sdp->sdev_gendev; |
| 2182 | gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS; |
| 2183 | if (sdp->removable) |
| 2184 | gd->flags |= GENHD_FL_REMOVABLE; |
| 2185 | |
| 2186 | dev_set_drvdata(dev, sdkp); |
| 2187 | add_disk(gd); |
| 2188 | sd_dif_config_host(sdkp); |
| 2189 | |
Martin K. Petersen | 3821d76 | 2009-05-23 11:43:38 -0400 | [diff] [blame] | 2190 | sd_revalidate_disk(gd); |
| 2191 | |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2192 | sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", |
| 2193 | sdp->removable ? "removable " : ""); |
James Bottomley | ea038f6 | 2009-08-21 09:47:54 -0600 | [diff] [blame] | 2194 | put_device(&sdkp->dev); |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 2197 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | * sd_probe - called during driver initialization and whenever a |
| 2199 | * new scsi device is attached to the system. It is called once |
| 2200 | * for each scsi device (not just disks) present. |
| 2201 | * @dev: pointer to device object |
| 2202 | * |
| 2203 | * Returns 0 if successful (or not interested in this scsi device |
| 2204 | * (e.g. scanner)); 1 when there is an error. |
| 2205 | * |
| 2206 | * Note: this function is invoked from the scsi mid-level. |
| 2207 | * This function sets up the mapping between a given |
| 2208 | * <host,channel,id,lun> (found in sdp) and new device name |
| 2209 | * (e.g. /dev/sda). More precisely it is the block device major |
| 2210 | * and minor number that is chosen here. |
| 2211 | * |
| 2212 | * Assume sd_attach is not re-entrant (for time being) |
| 2213 | * Also think about sd_attach() and sd_remove() running coincidentally. |
| 2214 | **/ |
| 2215 | static int sd_probe(struct device *dev) |
| 2216 | { |
| 2217 | struct scsi_device *sdp = to_scsi_device(dev); |
| 2218 | struct scsi_disk *sdkp; |
| 2219 | struct gendisk *gd; |
| 2220 | u32 index; |
| 2221 | int error; |
| 2222 | |
| 2223 | error = -ENODEV; |
Al Viro | 631e8a1 | 2005-05-16 01:59:55 +0100 | [diff] [blame] | 2224 | 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] | 2225 | goto out; |
| 2226 | |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 2227 | SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp, |
| 2228 | "sd_attach\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
| 2230 | error = -ENOMEM; |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 2231 | sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | if (!sdkp) |
| 2233 | goto out; |
| 2234 | |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 2235 | gd = alloc_disk(SD_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | if (!gd) |
| 2237 | goto out_free; |
| 2238 | |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2239 | do { |
| 2240 | if (!ida_pre_get(&sd_index_ida, GFP_KERNEL)) |
| 2241 | goto out_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2243 | spin_lock(&sd_index_lock); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2244 | error = ida_get_new(&sd_index_ida, &index); |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2245 | spin_unlock(&sd_index_lock); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2246 | } while (error == -EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | if (error) |
| 2249 | goto out_put; |
| 2250 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 2251 | error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN); |
| 2252 | if (error) |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2253 | goto out_free_index; |
| 2254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | sdkp->device = sdp; |
| 2256 | sdkp->driver = &sd_template; |
| 2257 | sdkp->disk = gd; |
| 2258 | sdkp->index = index; |
| 2259 | sdkp->openers = 0; |
Kay Sievers | c02e600 | 2008-03-19 13:09:56 +0100 | [diff] [blame] | 2260 | sdkp->previous_state = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 2262 | if (!sdp->request_queue->rq_timeout) { |
| 2263 | if (sdp->type != TYPE_MOD) |
| 2264 | blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT); |
| 2265 | else |
| 2266 | blk_queue_rq_timeout(sdp->request_queue, |
| 2267 | SD_MOD_TIMEOUT); |
| 2268 | } |
| 2269 | |
| 2270 | device_initialize(&sdkp->dev); |
| 2271 | sdkp->dev.parent = &sdp->sdev_gendev; |
| 2272 | sdkp->dev.class = &sd_disk_class; |
| 2273 | dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev)); |
| 2274 | |
| 2275 | if (device_add(&sdkp->dev)) |
| 2276 | goto out_free_index; |
| 2277 | |
| 2278 | get_device(&sdp->sdev_gendev); |
| 2279 | |
James Bottomley | ea038f6 | 2009-08-21 09:47:54 -0600 | [diff] [blame] | 2280 | get_device(&sdkp->dev); /* prevent release before async_schedule */ |
Arjan van de Ven | 4ace92f | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 2281 | async_schedule(sd_probe_async, sdkp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | |
| 2283 | return 0; |
| 2284 | |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2285 | out_free_index: |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2286 | spin_lock(&sd_index_lock); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2287 | ida_remove(&sd_index_ida, index); |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2288 | spin_unlock(&sd_index_lock); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 2289 | out_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | put_disk(gd); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 2291 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | kfree(sdkp); |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 2293 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | return error; |
| 2295 | } |
| 2296 | |
| 2297 | /** |
| 2298 | * sd_remove - called whenever a scsi disk (previously recognized by |
| 2299 | * sd_probe) is detached from the system. It is called (potentially |
| 2300 | * multiple times) during sd module unload. |
| 2301 | * @sdp: pointer to mid level scsi device object |
| 2302 | * |
| 2303 | * Note: this function is invoked from the scsi mid-level. |
| 2304 | * This function potentially frees up a device name (e.g. /dev/sdc) |
| 2305 | * that could be re-used by a subsequent sd_probe(). |
| 2306 | * This function is not called when the built-in sd driver is "exit-ed". |
| 2307 | **/ |
| 2308 | static int sd_remove(struct device *dev) |
| 2309 | { |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 2310 | struct scsi_disk *sdkp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | |
James Bottomley | 601e763 | 2009-05-26 20:35:48 +0000 | [diff] [blame] | 2312 | async_synchronize_full(); |
| 2313 | sdkp = dev_get_drvdata(dev); |
Hannes Reinecke | b391277 | 2009-06-18 09:57:18 +0200 | [diff] [blame] | 2314 | blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2315 | device_del(&sdkp->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | del_gendisk(sdkp->disk); |
| 2317 | sd_shutdown(dev); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2318 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 2319 | mutex_lock(&sd_ref_mutex); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2320 | dev_set_drvdata(dev, NULL); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2321 | put_device(&sdkp->dev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 2322 | mutex_unlock(&sd_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
| 2324 | return 0; |
| 2325 | } |
| 2326 | |
| 2327 | /** |
| 2328 | * scsi_disk_release - Called to free the scsi_disk structure |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2329 | * @dev: pointer to embedded class device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | * |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 2331 | * sd_ref_mutex must be held entering this routine. Because it is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | * called on last put, you should always use the scsi_disk_get() |
| 2333 | * scsi_disk_put() helpers which manipulate the semaphore directly |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2334 | * and never do a direct put_device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | **/ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2336 | static void scsi_disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2338 | struct scsi_disk *sdkp = to_scsi_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | struct gendisk *disk = sdkp->disk; |
| 2340 | |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2341 | spin_lock(&sd_index_lock); |
Tejun Heo | f27bac2 | 2008-07-14 14:59:30 +0900 | [diff] [blame] | 2342 | ida_remove(&sd_index_ida, sdkp->index); |
Tejun Heo | 4034cc6 | 2009-02-21 11:04:45 +0900 | [diff] [blame] | 2343 | spin_unlock(&sd_index_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
| 2345 | disk->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | put_disk(disk); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2347 | put_device(&sdkp->device->sdev_gendev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
| 2349 | kfree(sdkp); |
| 2350 | } |
| 2351 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2352 | static int sd_start_stop_device(struct scsi_disk *sdkp, int start) |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2353 | { |
| 2354 | unsigned char cmd[6] = { START_STOP }; /* START_VALID */ |
| 2355 | struct scsi_sense_hdr sshdr; |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2356 | struct scsi_device *sdp = sdkp->device; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2357 | int res; |
| 2358 | |
| 2359 | if (start) |
| 2360 | cmd[4] |= 1; /* START */ |
| 2361 | |
Stefan Richter | d2886ea | 2008-05-11 00:34:07 +0200 | [diff] [blame] | 2362 | if (sdp->start_stop_pwr_cond) |
| 2363 | cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */ |
| 2364 | |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2365 | if (!scsi_device_online(sdp)) |
| 2366 | return -ENODEV; |
| 2367 | |
| 2368 | res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 2369 | SD_TIMEOUT, SD_MAX_RETRIES, NULL); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2370 | if (res) { |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2371 | sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n"); |
| 2372 | sd_print_result(sdkp, res); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2373 | if (driver_byte(res) & DRIVER_SENSE) |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2374 | sd_print_sense_hdr(sdkp, &sshdr); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | return res; |
| 2378 | } |
| 2379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | /* |
| 2381 | * Send a SYNCHRONIZE CACHE instruction down to the device through |
| 2382 | * the normal SCSI command structure. Wait for the command to |
| 2383 | * complete. |
| 2384 | */ |
| 2385 | static void sd_shutdown(struct device *dev) |
| 2386 | { |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2387 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
| 2389 | if (!sdkp) |
| 2390 | return; /* this can happen */ |
| 2391 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2392 | if (sdkp->WCE) { |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2393 | sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); |
| 2394 | sd_sync_cache(sdkp); |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2395 | } |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2396 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2397 | if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) { |
| 2398 | sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); |
| 2399 | sd_start_stop_device(sdkp, 0); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2400 | } |
| 2401 | |
Alan Stern | 39b7f1e | 2005-11-04 14:44:41 -0500 | [diff] [blame] | 2402 | scsi_disk_put(sdkp); |
| 2403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2405 | static int sd_suspend(struct device *dev, pm_message_t mesg) |
| 2406 | { |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2407 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2408 | int ret = 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2409 | |
| 2410 | if (!sdkp) |
| 2411 | return 0; /* this can happen */ |
| 2412 | |
| 2413 | if (sdkp->WCE) { |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2414 | sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2415 | ret = sd_sync_cache(sdkp); |
| 2416 | if (ret) |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2417 | goto done; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2418 | } |
| 2419 | |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 2420 | if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) { |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2421 | sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n"); |
| 2422 | ret = sd_start_stop_device(sdkp, 0); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2423 | } |
| 2424 | |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2425 | done: |
| 2426 | scsi_disk_put(sdkp); |
| 2427 | return ret; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | static int sd_resume(struct device *dev) |
| 2431 | { |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2432 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2433 | int ret = 0; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2434 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2435 | if (!sdkp->device->manage_start_stop) |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2436 | goto done; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2437 | |
James Bottomley | cc5d2c8 | 2007-03-20 12:26:03 -0500 | [diff] [blame] | 2438 | sd_printk(KERN_NOTICE, sdkp, "Starting disk\n"); |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2439 | ret = sd_start_stop_device(sdkp, 1); |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2440 | |
Alan Stern | 09ff92f | 2007-05-21 09:55:04 -0400 | [diff] [blame] | 2441 | done: |
| 2442 | scsi_disk_put(sdkp); |
| 2443 | return ret; |
Tejun Heo | c3c94c5 | 2007-03-21 00:13:59 +0900 | [diff] [blame] | 2444 | } |
| 2445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | /** |
| 2447 | * init_sd - entry point for this driver (both when built in or when |
| 2448 | * a module). |
| 2449 | * |
| 2450 | * Note: this function registers this driver with the scsi mid-level. |
| 2451 | **/ |
| 2452 | static int __init init_sd(void) |
| 2453 | { |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2454 | int majors = 0, i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
| 2456 | SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); |
| 2457 | |
| 2458 | for (i = 0; i < SD_MAJORS; i++) |
| 2459 | if (register_blkdev(sd_major(i), "sd") == 0) |
| 2460 | majors++; |
| 2461 | |
| 2462 | if (!majors) |
| 2463 | return -ENODEV; |
| 2464 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2465 | err = class_register(&sd_disk_class); |
| 2466 | if (err) |
| 2467 | goto err_out; |
James Bottomley | 6bdaa1f | 2006-03-18 14:14:21 -0600 | [diff] [blame] | 2468 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2469 | err = scsi_register_driver(&sd_template.gendrv); |
| 2470 | if (err) |
| 2471 | goto err_out_class; |
| 2472 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 2473 | sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE, |
| 2474 | 0, 0, NULL); |
| 2475 | if (!sd_cdb_cache) { |
| 2476 | printk(KERN_ERR "sd: can't init extended cdb cache\n"); |
| 2477 | goto err_out_class; |
| 2478 | } |
| 2479 | |
| 2480 | sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache); |
| 2481 | if (!sd_cdb_pool) { |
| 2482 | printk(KERN_ERR "sd: can't init extended cdb pool\n"); |
| 2483 | goto err_out_cache; |
| 2484 | } |
| 2485 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2486 | return 0; |
| 2487 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 2488 | err_out_cache: |
| 2489 | kmem_cache_destroy(sd_cdb_cache); |
| 2490 | |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2491 | err_out_class: |
| 2492 | class_unregister(&sd_disk_class); |
| 2493 | err_out: |
| 2494 | for (i = 0; i < SD_MAJORS; i++) |
| 2495 | unregister_blkdev(sd_major(i), "sd"); |
| 2496 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * exit_sd - exit point for this driver (when it is a module). |
| 2501 | * |
| 2502 | * Note: this function unregisters this driver from the scsi mid-level. |
| 2503 | **/ |
| 2504 | static void __exit exit_sd(void) |
| 2505 | { |
| 2506 | int i; |
| 2507 | |
| 2508 | SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); |
| 2509 | |
Martin K. Petersen | 4e7392e | 2009-09-20 16:49:38 -0400 | [diff] [blame] | 2510 | mempool_destroy(sd_cdb_pool); |
| 2511 | kmem_cache_destroy(sd_cdb_cache); |
| 2512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | scsi_unregister_driver(&sd_template.gendrv); |
Jeff Garzik | 5e4009b | 2006-10-04 05:32:54 -0400 | [diff] [blame] | 2514 | class_unregister(&sd_disk_class); |
| 2515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | for (i = 0; i < SD_MAJORS; i++) |
| 2517 | unregister_blkdev(sd_major(i), "sd"); |
| 2518 | } |
| 2519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | module_init(init_sd); |
| 2521 | module_exit(exit_sd); |
Martin K. Petersen | e73aec8 | 2007-02-27 22:40:55 -0500 | [diff] [blame] | 2522 | |
| 2523 | static void sd_print_sense_hdr(struct scsi_disk *sdkp, |
| 2524 | struct scsi_sense_hdr *sshdr) |
| 2525 | { |
| 2526 | sd_printk(KERN_INFO, sdkp, ""); |
| 2527 | scsi_show_sense_hdr(sshdr); |
| 2528 | sd_printk(KERN_INFO, sdkp, ""); |
| 2529 | scsi_show_extd_sense(sshdr->asc, sshdr->ascq); |
| 2530 | } |
| 2531 | |
| 2532 | static void sd_print_result(struct scsi_disk *sdkp, int result) |
| 2533 | { |
| 2534 | sd_printk(KERN_INFO, sdkp, ""); |
| 2535 | scsi_show_result(result); |
| 2536 | } |
| 2537 | |