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