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