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