blob: 8ba2d988d051f6b1c56262503605f9873992daef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Ven0b950672006-01-11 13:16:10 +010052#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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 Torvalds1da177e2005-04-16 15:20:36 -070063#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 Viro48970802006-02-26 08:34:10 -060092/*
93 * Size of the initial data buffer for mode and read capacity data
94 */
95#define SD_BUF_SIZE 512
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void scsi_disk_release(struct kref *kref);
98
99struct 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 Heo007365a2006-01-06 09:53:52 +0100111 unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114static DEFINE_IDR(sd_index_idr);
115static 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 Ven0b950672006-01-11 13:16:10 +0100120static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122static int sd_revalidate_disk(struct gendisk *disk);
123static void sd_rw_intr(struct scsi_cmnd * SCpnt);
124
125static int sd_probe(struct device *);
126static int sd_remove(struct device *);
127static void sd_shutdown(struct device *dev);
128static void sd_rescan(struct device *);
129static int sd_init_command(struct scsi_cmnd *);
130static int sd_issue_flush(struct device *, sector_t *);
Tejun Heo461d4e92006-01-06 09:52:55 +0100131static void sd_prepare_flush(request_queue_t *, struct request *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500133 unsigned char *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static 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 Torvalds1da177e2005-04-16 15:20:36 -0700146};
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 */
162static 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
179static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
180{
181 return container_of(disk->private_data, struct scsi_disk, driver);
182}
183
Alan Stern39b7f1e2005-11-04 14:44:41 -0500184static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct scsi_disk *sdkp = NULL;
187
Alan Stern39b7f1e2005-11-04 14:44:41 -0500188 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
198static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
199{
200 struct scsi_disk *sdkp;
201
Arjan van de Ven0b950672006-01-11 13:16:10 +0100202 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500203 sdkp = __scsi_disk_get(disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100204 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return sdkp;
Alan Stern39b7f1e2005-11-04 14:44:41 -0500206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Alan Stern39b7f1e2005-11-04 14:44:41 -0500208static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
209{
210 struct scsi_disk *sdkp;
211
Arjan van de Ven0b950672006-01-11 13:16:10 +0100212 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500213 sdkp = dev_get_drvdata(dev);
214 if (sdkp)
215 sdkp = __scsi_disk_get(sdkp->disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100216 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return sdkp;
218}
219
220static void scsi_disk_put(struct scsi_disk *sdkp)
221{
222 struct scsi_device *sdev = sdkp->device;
223
Arjan van de Ven0b950672006-01-11 13:16:10 +0100224 mutex_lock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 kref_put(&sdkp->kref, scsi_disk_release);
226 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100227 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
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 **/
238static int sd_init_command(struct scsi_cmnd * SCpnt)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct scsi_device *sdp = SCpnt->device;
241 struct request *rq = SCpnt->request;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100242 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 Torvalds1da177e2005-04-16 15:20:36 -0700246
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 Heo007365a2006-01-06 09:53:52 +0100331 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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 Heo007365a2006-01-06 09:53:52 +0100351 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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 Heo007365a2006-01-06 09:53:52 +0100360 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 Torvalds1da177e2005-04-16 15:20:36 -0700371 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 Torvalds1da177e2005-04-16 15:20:36 -0700388 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 **/
416static 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
475error_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 **/
491static 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 Hellwiga885c8c2006-01-08 01:02:50 -0800512static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
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 Hellwiga885c8c2006-01-08 01:02:50 -0800530 geo->heads = diskinfo[0];
531 geo->sectors = diskinfo[1];
532 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 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 **/
550static 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 Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
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
589static 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 **/
604static 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
659not_present:
660 set_media_not_present(sdkp);
661 return 1;
662}
663
664static int sd_sync_cache(struct scsi_device *sdp)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int retries, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500667 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (!scsi_device_online(sdp))
670 return -ENODEV;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 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 Bottomleyea73a9f2005-08-28 11:33:52 -0500681 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
682 SD_TIMEOUT, SD_MAX_RETRIES);
683 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685 }
686
James Bottomleyea73a9f2005-08-28 11:33:52 -0500687 if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 "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 Bottomleyea73a9f2005-08-28 11:33:52 -0500692 scsi_print_sense_hdr("sd", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return res;
696}
697
698static int sd_issue_flush(struct device *dev, sector_t *error_sector)
699{
Alan Stern39b7f1e2005-11-04 14:44:41 -0500700 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct scsi_device *sdp = to_scsi_device(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500702 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!sdkp)
705 return -ENODEV;
706
Alan Stern39b7f1e2005-11-04 14:44:41 -0500707 if (sdkp->WCE)
708 ret = sd_sync_cache(sdp);
709 scsi_disk_put(sdkp);
710 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Tejun Heo461d4e92006-01-06 09:52:55 +0100713static void sd_prepare_flush(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
James Bottomleyc0ed79a2005-11-08 09:21:07 -0500715 memset(rq->cmd, 0, sizeof(rq->cmd));
Tejun Heo461d4e92006-01-06 09:52:55 +0100716 rq->flags |= REQ_BLOCK_PC;
James Bottomleyc0ed79a2005-11-08 09:21:07 -0500717 rq->timeout = SD_TIMEOUT;
718 rq->cmd[0] = SYNCHRONIZE_CACHE;
Tejun Heo461d4e92006-01-06 09:52:55 +0100719 rq->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722static void sd_rescan(struct device *dev)
723{
Alan Stern39b7f1e2005-11-04 14:44:41 -0500724 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 Torvalds1da177e2005-04-16 15:20:36 -0700730}
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 */
738static 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
768static struct block_device_operations sd_fops = {
769 .owner = THIS_MODULE,
770 .open = sd_open,
771 .release = sd_release,
772 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800773 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#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 **/
788static 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 Hellwig776b23a2006-01-06 18:34:07 +0100821 if (driver_byte(result) != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 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 Bottomleyea73a9f2005-08-28 11:33:52 -0500901static int media_not_present(struct scsi_disk *sdkp,
902 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
James Bottomleyea73a9f2005-08-28 11:33:52 -0500905 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -0500908 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 Torvalds1da177e2005-04-16 15:20:36 -0700914 set_media_not_present(sdkp);
915 return 1;
916}
917
918/*
919 * spinup disk - called only in sd_revalidate_disk()
920 */
921static void
James Bottomleyea73a9f2005-08-28 11:33:52 -0500922sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -0400925 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 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 Bottomleyea73a9f2005-08-28 11:33:52 -0500942 the_result = scsi_execute_req(sdkp->device, cmd,
943 DMA_NONE, NULL, 0,
944 &sshdr, SD_TIMEOUT,
945 SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -0500948 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 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 Bottomleyea73a9f2005-08-28 11:33:52 -0500960 if (media_not_present(sdkp, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 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 Bottomleyea73a9f2005-08-28 11:33:52 -0500999 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1000 NULL, 0, &sshdr,
1001 SD_TIMEOUT, SD_MAX_RETRIES);
Alan Stern4451e472005-07-12 10:45:17 -04001002 spintime_expire = jiffies + 100 * HZ;
1003 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* Wait 1 second for next try */
1006 msleep(1000);
1007 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001008
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 Torvalds1da177e2005-04-16 15:20:36 -07001023 } 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 Bottomleyea73a9f2005-08-28 11:33:52 -05001029 scsi_print_sense_hdr("", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 break;
1032 }
1033
Alan Stern4451e472005-07-12 10:45:17 -04001034 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
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 */
1047static void
1048sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001049 unsigned char *buffer)
1050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 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 Bottomleyea73a9f2005-08-28 11:33:52 -05001057 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059repeat:
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 Bottomleyea73a9f2005-08-28 11:33:52 -05001074 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1075 buffer, longrc ? 12 : 8, &sshdr,
1076 SD_TIMEOUT, SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
James Bottomleyea73a9f2005-08-28 11:33:52 -05001078 if (media_not_present(sdkp, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001082 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 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 Bottomleyea73a9f2005-08-28 11:33:52 -05001097 scsi_print_sense_hdr("sd", &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 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
1169got_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 Bottomley7a691bd2005-10-28 13:17:30 -05001205 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 request_queue_t *queue = sdp->request_queue;
James Bottomley7a691bd2005-10-28 13:17:30 -05001207 sector_t mb = sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 blk_queue_hardsect_size(queue, hard_sector);
1210 /* avoid 64-bit division on 32-bit platforms */
James Bottomley7a691bd2005-10-28 13:17:30 -05001211 sector_div(sz, 625);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 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 */
1235static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001236sd_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 Torvalds1da177e2005-04-16 15:20:36 -07001239{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001240 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001241 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001242 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
1246 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001247 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
1249static void
1250sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001251 unsigned char *buffer)
1252{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001254 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct scsi_mode_data data;
1256
1257 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001258 if (sdp->skip_ms_page_3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1260 return;
1261 }
1262
James Bottomleyea73a9f2005-08-28 11:33:52 -05001263 if (sdp->use_192_bytes_for_3f) {
1264 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 } 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 Bottomleyea73a9f2005-08-28 11:33:52 -05001271 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
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 Bottomleyea73a9f2005-08-28 11:33:52 -05001280 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
1283 * Third attempt: ask 255 bytes, as we did earlier.
1284 */
1285 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001286 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1287 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
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 Viro48970802006-02-26 08:34:10 -06001305 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
1307static void
1308sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001309 unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001312 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Al Viro 631e8a12005-05-16 01:59:55 +01001314 int dbd;
1315 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct scsi_mode_data data;
1317 struct scsi_sense_hdr sshdr;
1318
James Bottomleyea73a9f2005-08-28 11:33:52 -05001319 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto defaults;
1321
James Bottomleyea73a9f2005-08-28 11:33:52 -05001322 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001323 modepage = 6;
1324 dbd = 8;
1325 } else {
1326 modepage = 8;
1327 dbd = 0;
1328 }
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001331 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
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 Viro48970802006-02-26 08:34:10 -06001350 if (len > SD_BUF_SIZE)
1351 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001354 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
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 631e8a12005-05-16 01:59:55 +01001362 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Al Viro48970802006-02-26 08:34:10 -06001364 if (offset >= SD_BUF_SIZE - 2) {
1365 printk(KERN_ERR "%s: malformed MODE SENSE response",
1366 diskname);
1367 goto defaults;
1368 }
1369
Al Viro 631e8a12005-05-16 01:59:55 +01001370 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 Torvalds1da177e2005-04-16 15:20:36 -07001382
Tejun Heo007365a2006-01-06 09:53:52 +01001383 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 Torvalds1da177e2005-04-16 15:20:36 -07001390 ct = sdkp->RCD + 2*sdkp->WCE;
1391
Tejun Heo007365a2006-01-06 09:53:52 +01001392 printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
1393 diskname, types[ct],
1394 sdkp->DPOFUA ? " w/ FUA" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 return;
1397 }
1398
1399bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001400 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 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
1409defaults:
1410 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1411 diskname);
1412 sdkp->WCE = 0;
1413 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001414 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
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 **/
1422static int sd_revalidate_disk(struct gendisk *disk)
1423{
1424 struct scsi_disk *sdkp = scsi_disk(disk);
1425 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01001427 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
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 Viro48970802006-02-26 08:34:10 -06001438 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!buffer) {
1440 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1441 "failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05001442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
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 Bottomleyea73a9f2005-08-28 11:33:52 -05001453 sd_spinup_disk(sdkp, disk->disk_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
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 Bottomleyea73a9f2005-08-28 11:33:52 -05001460 sd_read_capacity(sdkp, disk->disk_name, buffer);
Alan Stern38d76df2005-12-09 11:34:45 -05001461 sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001462 sd_read_cache_type(sdkp, disk->disk_name, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Tejun Heo461d4e92006-01-06 09:52:55 +01001464
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 Heo007365a2006-01-06 09:53:52 +01001472 ordered = sdkp->DPOFUA
1473 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01001474 else
1475 ordered = QUEUE_ORDERED_DRAIN;
1476
1477 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 set_capacity(disk, sdkp->capacity);
1480 kfree(buffer);
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 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 **/
1504static 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 631e8a12005-05-16 01:59:55 +01001513 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 goto out;
1515
James Bottomley9ccfc752005-10-02 11:45:08 -05001516 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1517 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05001520 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (!sdkp)
1522 goto out;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 kref_init(&sdkp->kref);
1525
1526 gd = alloc_disk(16);
1527 if (!gd)
1528 goto out_free;
1529
1530 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1531 goto out_put;
1532
1533 spin_lock(&sd_index_lock);
1534 error = idr_get_new(&sd_index_idr, NULL, &index);
1535 spin_unlock(&sd_index_lock);
1536
1537 if (index >= SD_MAX_DISKS)
1538 error = -EBUSY;
1539 if (error)
1540 goto out_put;
1541
Alan Stern39b7f1e2005-11-04 14:44:41 -05001542 get_device(&sdp->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 sdkp->device = sdp;
1544 sdkp->driver = &sd_template;
1545 sdkp->disk = gd;
1546 sdkp->index = index;
1547 sdkp->openers = 0;
1548
1549 if (!sdp->timeout) {
Al Viro 631e8a12005-05-16 01:59:55 +01001550 if (sdp->type != TYPE_MOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 sdp->timeout = SD_TIMEOUT;
1552 else
1553 sdp->timeout = SD_MOD_TIMEOUT;
1554 }
1555
1556 gd->major = sd_major((index & 0xf0) >> 4);
1557 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1558 gd->minors = 16;
1559 gd->fops = &sd_fops;
1560
1561 if (index < 26) {
1562 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1563 } else if (index < (26 + 1) * 26) {
1564 sprintf(gd->disk_name, "sd%c%c",
1565 'a' + index / 26 - 1,'a' + index % 26);
1566 } else {
1567 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1568 const unsigned int m2 = (index / 26 - 1) % 26;
1569 const unsigned int m3 = index % 26;
1570 sprintf(gd->disk_name, "sd%c%c%c",
1571 'a' + m1, 'a' + m2, 'a' + m3);
1572 }
1573
1574 strcpy(gd->devfs_name, sdp->devfs_name);
1575
1576 gd->private_data = &sdkp->driver;
Tejun Heo461d4e92006-01-06 09:52:55 +01001577 gd->queue = sdkp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 sd_revalidate_disk(gd);
1580
1581 gd->driverfs_dev = &sdp->sdev_gendev;
1582 gd->flags = GENHD_FL_DRIVERFS;
1583 if (sdp->removable)
1584 gd->flags |= GENHD_FL_REMOVABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 dev_set_drvdata(dev, sdkp);
1587 add_disk(gd);
1588
James Bottomley9ccfc752005-10-02 11:45:08 -05001589 sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
1590 sdp->removable ? "removable " : "", gd->disk_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 return 0;
1593
1594out_put:
1595 put_disk(gd);
1596out_free:
1597 kfree(sdkp);
1598out:
1599 return error;
1600}
1601
1602/**
1603 * sd_remove - called whenever a scsi disk (previously recognized by
1604 * sd_probe) is detached from the system. It is called (potentially
1605 * multiple times) during sd module unload.
1606 * @sdp: pointer to mid level scsi device object
1607 *
1608 * Note: this function is invoked from the scsi mid-level.
1609 * This function potentially frees up a device name (e.g. /dev/sdc)
1610 * that could be re-used by a subsequent sd_probe().
1611 * This function is not called when the built-in sd driver is "exit-ed".
1612 **/
1613static int sd_remove(struct device *dev)
1614{
1615 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1616
1617 del_gendisk(sdkp->disk);
1618 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001619
Arjan van de Ven0b950672006-01-11 13:16:10 +01001620 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001621 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 kref_put(&sdkp->kref, scsi_disk_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001623 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 return 0;
1626}
1627
1628/**
1629 * scsi_disk_release - Called to free the scsi_disk structure
1630 * @kref: pointer to embedded kref
1631 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001632 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * called on last put, you should always use the scsi_disk_get()
1634 * scsi_disk_put() helpers which manipulate the semaphore directly
1635 * and never do a direct kref_put().
1636 **/
1637static void scsi_disk_release(struct kref *kref)
1638{
1639 struct scsi_disk *sdkp = to_scsi_disk(kref);
1640 struct gendisk *disk = sdkp->disk;
1641
1642 spin_lock(&sd_index_lock);
1643 idr_remove(&sd_index_idr, sdkp->index);
1644 spin_unlock(&sd_index_lock);
1645
1646 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001648 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 kfree(sdkp);
1651}
1652
1653/*
1654 * Send a SYNCHRONIZE CACHE instruction down to the device through
1655 * the normal SCSI command structure. Wait for the command to
1656 * complete.
1657 */
1658static void sd_shutdown(struct device *dev)
1659{
1660 struct scsi_device *sdp = to_scsi_device(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001661 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 if (!sdkp)
1664 return; /* this can happen */
1665
Alan Stern39b7f1e2005-11-04 14:44:41 -05001666 if (sdkp->WCE) {
1667 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1668 sdkp->disk->disk_name);
1669 sd_sync_cache(sdp);
1670 }
1671 scsi_disk_put(sdkp);
1672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674/**
1675 * init_sd - entry point for this driver (both when built in or when
1676 * a module).
1677 *
1678 * Note: this function registers this driver with the scsi mid-level.
1679 **/
1680static int __init init_sd(void)
1681{
1682 int majors = 0, i;
1683
1684 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1685
1686 for (i = 0; i < SD_MAJORS; i++)
1687 if (register_blkdev(sd_major(i), "sd") == 0)
1688 majors++;
1689
1690 if (!majors)
1691 return -ENODEV;
1692
1693 return scsi_register_driver(&sd_template.gendrv);
1694}
1695
1696/**
1697 * exit_sd - exit point for this driver (when it is a module).
1698 *
1699 * Note: this function unregisters this driver from the scsi mid-level.
1700 **/
1701static void __exit exit_sd(void)
1702{
1703 int i;
1704
1705 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1706
1707 scsi_unregister_driver(&sd_template.gendrv);
1708 for (i = 0; i < SD_MAJORS; i++)
1709 unregister_blkdev(sd_major(i), "sd");
1710}
1711
1712MODULE_LICENSE("GPL");
1713MODULE_AUTHOR("Eric Youngdale");
1714MODULE_DESCRIPTION("SCSI disk (sd) driver");
1715
1716module_init(init_sd);
1717module_exit(exit_sd);