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