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