[BLOCK] scsi: add FUA support to sd
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / sd.c
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>
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
91 static void scsi_disk_release(struct kref *kref);
92
93 struct 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 */
105 unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
106 };
107
108 static DEFINE_IDR(sd_index_idr);
109 static 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) */
114 static DECLARE_MUTEX(sd_ref_sem);
115
116 static int sd_revalidate_disk(struct gendisk *disk);
117 static void sd_rw_intr(struct scsi_cmnd * SCpnt);
118
119 static int sd_probe(struct device *);
120 static int sd_remove(struct device *);
121 static void sd_shutdown(struct device *dev);
122 static void sd_rescan(struct device *);
123 static int sd_init_command(struct scsi_cmnd *);
124 static int sd_issue_flush(struct device *, sector_t *);
125 static void sd_prepare_flush(request_queue_t *, struct request *);
126 static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
127 unsigned char *buffer);
128
129 static 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,
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 */
156 static 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
173 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
174 {
175 return container_of(disk->private_data, struct scsi_disk, driver);
176 }
177
178 static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
179 {
180 struct scsi_disk *sdkp = NULL;
181
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
192 static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
193 {
194 struct scsi_disk *sdkp;
195
196 down(&sd_ref_sem);
197 sdkp = __scsi_disk_get(disk);
198 up(&sd_ref_sem);
199 return sdkp;
200 }
201
202 static 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);
210 up(&sd_ref_sem);
211 return sdkp;
212 }
213
214 static 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 **/
232 static 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)) {
246 scsi_setup_blk_pc_cmnd(SCpnt);
247 if (rq->timeout)
248 timeout = rq->timeout;
249
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;
347 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
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;
367 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
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 {
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
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
405 queue:
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 **/
434 static 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
493 error_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 **/
509 static 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
530 static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *loc)
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
548 if (put_user(diskinfo[0], &loc->heads))
549 return -EFAULT;
550 if (put_user(diskinfo[1], &loc->sectors))
551 return -EFAULT;
552 if (put_user(diskinfo[2], &loc->cylinders))
553 return -EFAULT;
554 if (put_user((unsigned)get_start_sect(bdev),
555 (unsigned long __user *)&loc->start))
556 return -EFAULT;
557 return 0;
558 }
559
560 /**
561 * sd_ioctl - process an ioctl
562 * @inode: only i_rdev/i_bdev members may be used
563 * @filp: only f_mode and f_flags may be used
564 * @cmd: ioctl command number
565 * @arg: this is third argument given to ioctl(2) system call.
566 * Often contains a pointer.
567 *
568 * Returns 0 if successful (some ioctls return postive numbers on
569 * success as well). Returns a negated errno value in case of error.
570 *
571 * Note: most ioctls are forward onto the block subsystem or further
572 * down in the scsi subsytem.
573 **/
574 static int sd_ioctl(struct inode * inode, struct file * filp,
575 unsigned int cmd, unsigned long arg)
576 {
577 struct block_device *bdev = inode->i_bdev;
578 struct gendisk *disk = bdev->bd_disk;
579 struct scsi_device *sdp = scsi_disk(disk)->device;
580 void __user *p = (void __user *)arg;
581 int error;
582
583 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
584 disk->disk_name, cmd));
585
586 /*
587 * If we are in the middle of error recovery, don't let anyone
588 * else try and use this device. Also, if error recovery fails, it
589 * may try and take the device offline, in which case all further
590 * access to the device is prohibited.
591 */
592 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
593 if (!scsi_block_when_processing_errors(sdp) || !error)
594 return error;
595
596 if (cmd == HDIO_GETGEO) {
597 if (!arg)
598 return -EINVAL;
599 return sd_hdio_getgeo(bdev, p);
600 }
601
602 /*
603 * Send SCSI addressing ioctls directly to mid level, send other
604 * ioctls to block level and then onto mid level if they can't be
605 * resolved.
606 */
607 switch (cmd) {
608 case SCSI_IOCTL_GET_IDLUN:
609 case SCSI_IOCTL_GET_BUS_NUMBER:
610 return scsi_ioctl(sdp, cmd, p);
611 default:
612 error = scsi_cmd_ioctl(filp, disk, cmd, p);
613 if (error != -ENOTTY)
614 return error;
615 }
616 return scsi_ioctl(sdp, cmd, p);
617 }
618
619 static void set_media_not_present(struct scsi_disk *sdkp)
620 {
621 sdkp->media_present = 0;
622 sdkp->capacity = 0;
623 sdkp->device->changed = 1;
624 }
625
626 /**
627 * sd_media_changed - check if our medium changed
628 * @disk: kernel device descriptor
629 *
630 * Returns 0 if not applicable or no change; 1 if change
631 *
632 * Note: this function is invoked from the block subsystem.
633 **/
634 static int sd_media_changed(struct gendisk *disk)
635 {
636 struct scsi_disk *sdkp = scsi_disk(disk);
637 struct scsi_device *sdp = sdkp->device;
638 int retval;
639
640 SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
641 disk->disk_name));
642
643 if (!sdp->removable)
644 return 0;
645
646 /*
647 * If the device is offline, don't send any commands - just pretend as
648 * if the command failed. If the device ever comes back online, we
649 * can deal with it then. It is only because of unrecoverable errors
650 * that we would ever take a device offline in the first place.
651 */
652 if (!scsi_device_online(sdp))
653 goto not_present;
654
655 /*
656 * Using TEST_UNIT_READY enables differentiation between drive with
657 * no cartridge loaded - NOT READY, drive with changed cartridge -
658 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
659 *
660 * Drives that auto spin down. eg iomega jaz 1G, will be started
661 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
662 * sd_revalidate() is called.
663 */
664 retval = -ENODEV;
665 if (scsi_block_when_processing_errors(sdp))
666 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
667
668 /*
669 * Unable to test, unit probably not ready. This usually
670 * means there is no disc in the drive. Mark as changed,
671 * and we will figure it out later once the drive is
672 * available again.
673 */
674 if (retval)
675 goto not_present;
676
677 /*
678 * For removable scsi disk we have to recognise the presence
679 * of a disk in the drive. This is kept in the struct scsi_disk
680 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
681 */
682 sdkp->media_present = 1;
683
684 retval = sdp->changed;
685 sdp->changed = 0;
686
687 return retval;
688
689 not_present:
690 set_media_not_present(sdkp);
691 return 1;
692 }
693
694 static int sd_sync_cache(struct scsi_device *sdp)
695 {
696 int retries, res;
697 struct scsi_sense_hdr sshdr;
698
699 if (!scsi_device_online(sdp))
700 return -ENODEV;
701
702
703 for (retries = 3; retries > 0; --retries) {
704 unsigned char cmd[10] = { 0 };
705
706 cmd[0] = SYNCHRONIZE_CACHE;
707 /*
708 * Leave the rest of the command zero to indicate
709 * flush everything.
710 */
711 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
712 SD_TIMEOUT, SD_MAX_RETRIES);
713 if (res == 0)
714 break;
715 }
716
717 if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
718 "host = %d, driver = %02x\n ",
719 status_byte(res), msg_byte(res),
720 host_byte(res), driver_byte(res));
721 if (driver_byte(res) & DRIVER_SENSE)
722 scsi_print_sense_hdr("sd", &sshdr);
723 }
724
725 return res;
726 }
727
728 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
729 {
730 int ret = 0;
731 struct scsi_device *sdp = to_scsi_device(dev);
732 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
733
734 if (!sdkp)
735 return -ENODEV;
736
737 if (sdkp->WCE)
738 ret = sd_sync_cache(sdp);
739 scsi_disk_put(sdkp);
740 return ret;
741 }
742
743 static void sd_prepare_flush(request_queue_t *q, struct request *rq)
744 {
745 memset(rq->cmd, 0, sizeof(rq->cmd));
746 rq->flags |= REQ_BLOCK_PC;
747 rq->timeout = SD_TIMEOUT;
748 rq->cmd[0] = SYNCHRONIZE_CACHE;
749 rq->cmd_len = 10;
750 }
751
752 static void sd_rescan(struct device *dev)
753 {
754 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
755
756 if (sdkp) {
757 sd_revalidate_disk(sdkp->disk);
758 scsi_disk_put(sdkp);
759 }
760 }
761
762
763 #ifdef CONFIG_COMPAT
764 /*
765 * This gets directly called from VFS. When the ioctl
766 * is not recognized we go back to the other translation paths.
767 */
768 static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
769 {
770 struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
771 struct gendisk *disk = bdev->bd_disk;
772 struct scsi_device *sdev = scsi_disk(disk)->device;
773
774 /*
775 * If we are in the middle of error recovery, don't let anyone
776 * else try and use this device. Also, if error recovery fails, it
777 * may try and take the device offline, in which case all further
778 * access to the device is prohibited.
779 */
780 if (!scsi_block_when_processing_errors(sdev))
781 return -ENODEV;
782
783 if (sdev->host->hostt->compat_ioctl) {
784 int ret;
785
786 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
787
788 return ret;
789 }
790
791 /*
792 * Let the static ioctl translation table take care of it.
793 */
794 return -ENOIOCTLCMD;
795 }
796 #endif
797
798 static struct block_device_operations sd_fops = {
799 .owner = THIS_MODULE,
800 .open = sd_open,
801 .release = sd_release,
802 .ioctl = sd_ioctl,
803 #ifdef CONFIG_COMPAT
804 .compat_ioctl = sd_compat_ioctl,
805 #endif
806 .media_changed = sd_media_changed,
807 .revalidate_disk = sd_revalidate_disk,
808 };
809
810 /**
811 * sd_rw_intr - bottom half handler: called when the lower level
812 * driver has completed (successfully or otherwise) a scsi command.
813 * @SCpnt: mid-level's per command structure.
814 *
815 * Note: potentially run from within an ISR. Must not block.
816 **/
817 static void sd_rw_intr(struct scsi_cmnd * SCpnt)
818 {
819 int result = SCpnt->result;
820 int this_count = SCpnt->bufflen;
821 int good_bytes = (result == 0 ? this_count : 0);
822 sector_t block_sectors = 1;
823 u64 first_err_block;
824 sector_t error_sector;
825 struct scsi_sense_hdr sshdr;
826 int sense_valid = 0;
827 int sense_deferred = 0;
828 int info_valid;
829
830 if (result) {
831 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
832 if (sense_valid)
833 sense_deferred = scsi_sense_is_deferred(&sshdr);
834 }
835
836 #ifdef CONFIG_SCSI_LOGGING
837 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
838 SCpnt->request->rq_disk->disk_name, result));
839 if (sense_valid) {
840 SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
841 "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
842 sshdr.sense_key, sshdr.asc, sshdr.ascq));
843 }
844 #endif
845 /*
846 Handle MEDIUM ERRORs that indicate partial success. Since this is a
847 relatively rare error condition, no care is taken to avoid
848 unnecessary additional work such as memcpy's that could be avoided.
849 */
850
851 /*
852 * If SG_IO from block layer then set good_bytes to stop retries;
853 * else if errors, check them, and if necessary prepare for
854 * (partial) retries.
855 */
856 if (blk_pc_request(SCpnt->request))
857 good_bytes = this_count;
858 else if (driver_byte(result) != 0 &&
859 sense_valid && !sense_deferred) {
860 switch (sshdr.sense_key) {
861 case MEDIUM_ERROR:
862 if (!blk_fs_request(SCpnt->request))
863 break;
864 info_valid = scsi_get_sense_info_fld(
865 SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE,
866 &first_err_block);
867 /*
868 * May want to warn and skip if following cast results
869 * in actual truncation (if sector_t < 64 bits)
870 */
871 error_sector = (sector_t)first_err_block;
872 if (SCpnt->request->bio != NULL)
873 block_sectors = bio_sectors(SCpnt->request->bio);
874 switch (SCpnt->device->sector_size) {
875 case 1024:
876 error_sector <<= 1;
877 if (block_sectors < 2)
878 block_sectors = 2;
879 break;
880 case 2048:
881 error_sector <<= 2;
882 if (block_sectors < 4)
883 block_sectors = 4;
884 break;
885 case 4096:
886 error_sector <<=3;
887 if (block_sectors < 8)
888 block_sectors = 8;
889 break;
890 case 256:
891 error_sector >>= 1;
892 break;
893 default:
894 break;
895 }
896
897 error_sector &= ~(block_sectors - 1);
898 good_bytes = (error_sector - SCpnt->request->sector) << 9;
899 if (good_bytes < 0 || good_bytes >= this_count)
900 good_bytes = 0;
901 break;
902
903 case RECOVERED_ERROR: /* an error occurred, but it recovered */
904 case NO_SENSE: /* LLDD got sense data */
905 /*
906 * Inform the user, but make sure that it's not treated
907 * as a hard error.
908 */
909 scsi_print_sense("sd", SCpnt);
910 SCpnt->result = 0;
911 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
912 good_bytes = this_count;
913 break;
914
915 case ILLEGAL_REQUEST:
916 if (SCpnt->device->use_10_for_rw &&
917 (SCpnt->cmnd[0] == READ_10 ||
918 SCpnt->cmnd[0] == WRITE_10))
919 SCpnt->device->use_10_for_rw = 0;
920 if (SCpnt->device->use_10_for_ms &&
921 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
922 SCpnt->cmnd[0] == MODE_SELECT_10))
923 SCpnt->device->use_10_for_ms = 0;
924 break;
925
926 default:
927 break;
928 }
929 }
930 /*
931 * This calls the generic completion function, now that we know
932 * how many actual sectors finished, and how many sectors we need
933 * to say have failed.
934 */
935 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
936 }
937
938 static int media_not_present(struct scsi_disk *sdkp,
939 struct scsi_sense_hdr *sshdr)
940 {
941
942 if (!scsi_sense_valid(sshdr))
943 return 0;
944 /* not invoked for commands that could return deferred errors */
945 if (sshdr->sense_key != NOT_READY &&
946 sshdr->sense_key != UNIT_ATTENTION)
947 return 0;
948 if (sshdr->asc != 0x3A) /* medium not present */
949 return 0;
950
951 set_media_not_present(sdkp);
952 return 1;
953 }
954
955 /*
956 * spinup disk - called only in sd_revalidate_disk()
957 */
958 static void
959 sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
960 {
961 unsigned char cmd[10];
962 unsigned long spintime_expire = 0;
963 int retries, spintime;
964 unsigned int the_result;
965 struct scsi_sense_hdr sshdr;
966 int sense_valid = 0;
967
968 spintime = 0;
969
970 /* Spin up drives, as required. Only do this at boot time */
971 /* Spinup needs to be done for module loads too. */
972 do {
973 retries = 0;
974
975 do {
976 cmd[0] = TEST_UNIT_READY;
977 memset((void *) &cmd[1], 0, 9);
978
979 the_result = scsi_execute_req(sdkp->device, cmd,
980 DMA_NONE, NULL, 0,
981 &sshdr, SD_TIMEOUT,
982 SD_MAX_RETRIES);
983
984 if (the_result)
985 sense_valid = scsi_sense_valid(&sshdr);
986 retries++;
987 } while (retries < 3 &&
988 (!scsi_status_is_good(the_result) ||
989 ((driver_byte(the_result) & DRIVER_SENSE) &&
990 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
991
992 /*
993 * If the drive has indicated to us that it doesn't have
994 * any media in it, don't bother with any of the rest of
995 * this crap.
996 */
997 if (media_not_present(sdkp, &sshdr))
998 return;
999
1000 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1001 /* no sense, TUR either succeeded or failed
1002 * with a status error */
1003 if(!spintime && !scsi_status_is_good(the_result))
1004 printk(KERN_NOTICE "%s: Unit Not Ready, "
1005 "error = 0x%x\n", diskname, the_result);
1006 break;
1007 }
1008
1009 /*
1010 * The device does not want the automatic start to be issued.
1011 */
1012 if (sdkp->device->no_start_on_add) {
1013 break;
1014 }
1015
1016 /*
1017 * If manual intervention is required, or this is an
1018 * absent USB storage device, a spinup is meaningless.
1019 */
1020 if (sense_valid &&
1021 sshdr.sense_key == NOT_READY &&
1022 sshdr.asc == 4 && sshdr.ascq == 3) {
1023 break; /* manual intervention required */
1024
1025 /*
1026 * Issue command to spin up drive when not ready
1027 */
1028 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1029 if (!spintime) {
1030 printk(KERN_NOTICE "%s: Spinning up disk...",
1031 diskname);
1032 cmd[0] = START_STOP;
1033 cmd[1] = 1; /* Return immediately */
1034 memset((void *) &cmd[2], 0, 8);
1035 cmd[4] = 1; /* Start spin cycle */
1036 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1037 NULL, 0, &sshdr,
1038 SD_TIMEOUT, SD_MAX_RETRIES);
1039 spintime_expire = jiffies + 100 * HZ;
1040 spintime = 1;
1041 }
1042 /* Wait 1 second for next try */
1043 msleep(1000);
1044 printk(".");
1045
1046 /*
1047 * Wait for USB flash devices with slow firmware.
1048 * Yes, this sense key/ASC combination shouldn't
1049 * occur here. It's characteristic of these devices.
1050 */
1051 } else if (sense_valid &&
1052 sshdr.sense_key == UNIT_ATTENTION &&
1053 sshdr.asc == 0x28) {
1054 if (!spintime) {
1055 spintime_expire = jiffies + 5 * HZ;
1056 spintime = 1;
1057 }
1058 /* Wait 1 second for next try */
1059 msleep(1000);
1060 } else {
1061 /* we don't understand the sense code, so it's
1062 * probably pointless to loop */
1063 if(!spintime) {
1064 printk(KERN_NOTICE "%s: Unit Not Ready, "
1065 "sense:\n", diskname);
1066 scsi_print_sense_hdr("", &sshdr);
1067 }
1068 break;
1069 }
1070
1071 } while (spintime && time_before_eq(jiffies, spintime_expire));
1072
1073 if (spintime) {
1074 if (scsi_status_is_good(the_result))
1075 printk("ready\n");
1076 else
1077 printk("not responding...\n");
1078 }
1079 }
1080
1081 /*
1082 * read disk capacity
1083 */
1084 static void
1085 sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
1086 unsigned char *buffer)
1087 {
1088 unsigned char cmd[16];
1089 int the_result, retries;
1090 int sector_size = 0;
1091 int longrc = 0;
1092 struct scsi_sense_hdr sshdr;
1093 int sense_valid = 0;
1094 struct scsi_device *sdp = sdkp->device;
1095
1096 repeat:
1097 retries = 3;
1098 do {
1099 if (longrc) {
1100 memset((void *) cmd, 0, 16);
1101 cmd[0] = SERVICE_ACTION_IN;
1102 cmd[1] = SAI_READ_CAPACITY_16;
1103 cmd[13] = 12;
1104 memset((void *) buffer, 0, 12);
1105 } else {
1106 cmd[0] = READ_CAPACITY;
1107 memset((void *) &cmd[1], 0, 9);
1108 memset((void *) buffer, 0, 8);
1109 }
1110
1111 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1112 buffer, longrc ? 12 : 8, &sshdr,
1113 SD_TIMEOUT, SD_MAX_RETRIES);
1114
1115 if (media_not_present(sdkp, &sshdr))
1116 return;
1117
1118 if (the_result)
1119 sense_valid = scsi_sense_valid(&sshdr);
1120 retries--;
1121
1122 } while (the_result && retries);
1123
1124 if (the_result && !longrc) {
1125 printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
1126 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1127 diskname, diskname,
1128 status_byte(the_result),
1129 msg_byte(the_result),
1130 host_byte(the_result),
1131 driver_byte(the_result));
1132
1133 if (driver_byte(the_result) & DRIVER_SENSE)
1134 scsi_print_sense_hdr("sd", &sshdr);
1135 else
1136 printk("%s : sense not available. \n", diskname);
1137
1138 /* Set dirty bit for removable devices if not ready -
1139 * sometimes drives will not report this properly. */
1140 if (sdp->removable &&
1141 sense_valid && sshdr.sense_key == NOT_READY)
1142 sdp->changed = 1;
1143
1144 /* Either no media are present but the drive didn't tell us,
1145 or they are present but the read capacity command fails */
1146 /* sdkp->media_present = 0; -- not always correct */
1147 sdkp->capacity = 0x200000; /* 1 GB - random */
1148
1149 return;
1150 } else if (the_result && longrc) {
1151 /* READ CAPACITY(16) has been failed */
1152 printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
1153 "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
1154 diskname, diskname,
1155 status_byte(the_result),
1156 msg_byte(the_result),
1157 host_byte(the_result),
1158 driver_byte(the_result));
1159 printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
1160 diskname);
1161
1162 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1163 goto got_data;
1164 }
1165
1166 if (!longrc) {
1167 sector_size = (buffer[4] << 24) |
1168 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1169 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1170 buffer[2] == 0xff && buffer[3] == 0xff) {
1171 if(sizeof(sdkp->capacity) > 4) {
1172 printk(KERN_NOTICE "%s : very big device. try to use"
1173 " READ CAPACITY(16).\n", diskname);
1174 longrc = 1;
1175 goto repeat;
1176 }
1177 printk(KERN_ERR "%s: too big for this kernel. Use a "
1178 "kernel compiled with support for large block "
1179 "devices.\n", diskname);
1180 sdkp->capacity = 0;
1181 goto got_data;
1182 }
1183 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1184 (buffer[1] << 16) |
1185 (buffer[2] << 8) |
1186 buffer[3]);
1187 } else {
1188 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1189 ((u64)buffer[1] << 48) |
1190 ((u64)buffer[2] << 40) |
1191 ((u64)buffer[3] << 32) |
1192 ((sector_t)buffer[4] << 24) |
1193 ((sector_t)buffer[5] << 16) |
1194 ((sector_t)buffer[6] << 8) |
1195 (sector_t)buffer[7]);
1196
1197 sector_size = (buffer[8] << 24) |
1198 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
1199 }
1200
1201 /* Some devices return the total number of sectors, not the
1202 * highest sector number. Make the necessary adjustment. */
1203 if (sdp->fix_capacity)
1204 --sdkp->capacity;
1205
1206 got_data:
1207 if (sector_size == 0) {
1208 sector_size = 512;
1209 printk(KERN_NOTICE "%s : sector size 0 reported, "
1210 "assuming 512.\n", diskname);
1211 }
1212
1213 if (sector_size != 512 &&
1214 sector_size != 1024 &&
1215 sector_size != 2048 &&
1216 sector_size != 4096 &&
1217 sector_size != 256) {
1218 printk(KERN_NOTICE "%s : unsupported sector size "
1219 "%d.\n", diskname, sector_size);
1220 /*
1221 * The user might want to re-format the drive with
1222 * a supported sectorsize. Once this happens, it
1223 * would be relatively trivial to set the thing up.
1224 * For this reason, we leave the thing in the table.
1225 */
1226 sdkp->capacity = 0;
1227 /*
1228 * set a bogus sector size so the normal read/write
1229 * logic in the block layer will eventually refuse any
1230 * request on this device without tripping over power
1231 * of two sector size assumptions
1232 */
1233 sector_size = 512;
1234 }
1235 {
1236 /*
1237 * The msdos fs needs to know the hardware sector size
1238 * So I have created this table. See ll_rw_blk.c
1239 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1240 */
1241 int hard_sector = sector_size;
1242 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
1243 request_queue_t *queue = sdp->request_queue;
1244 sector_t mb = sz;
1245
1246 blk_queue_hardsect_size(queue, hard_sector);
1247 /* avoid 64-bit division on 32-bit platforms */
1248 sector_div(sz, 625);
1249 mb -= sz - 974;
1250 sector_div(mb, 1950);
1251
1252 printk(KERN_NOTICE "SCSI device %s: "
1253 "%llu %d-byte hdwr sectors (%llu MB)\n",
1254 diskname, (unsigned long long)sdkp->capacity,
1255 hard_sector, (unsigned long long)mb);
1256 }
1257
1258 /* Rescale capacity to 512-byte units */
1259 if (sector_size == 4096)
1260 sdkp->capacity <<= 3;
1261 else if (sector_size == 2048)
1262 sdkp->capacity <<= 2;
1263 else if (sector_size == 1024)
1264 sdkp->capacity <<= 1;
1265 else if (sector_size == 256)
1266 sdkp->capacity >>= 1;
1267
1268 sdkp->device->sector_size = sector_size;
1269 }
1270
1271 /* called with buffer of length 512 */
1272 static inline int
1273 sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1274 unsigned char *buffer, int len, struct scsi_mode_data *data,
1275 struct scsi_sense_hdr *sshdr)
1276 {
1277 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1278 SD_TIMEOUT, SD_MAX_RETRIES, data,
1279 sshdr);
1280 }
1281
1282 /*
1283 * read write protect setting, if possible - called only in sd_revalidate_disk()
1284 * called with buffer of length 512
1285 */
1286 static void
1287 sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
1288 unsigned char *buffer)
1289 {
1290 int res;
1291 struct scsi_device *sdp = sdkp->device;
1292 struct scsi_mode_data data;
1293
1294 set_disk_ro(sdkp->disk, 0);
1295 if (sdp->skip_ms_page_3f) {
1296 printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
1297 return;
1298 }
1299
1300 if (sdp->use_192_bytes_for_3f) {
1301 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1302 } else {
1303 /*
1304 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1305 * We have to start carefully: some devices hang if we ask
1306 * for more than is available.
1307 */
1308 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1309
1310 /*
1311 * Second attempt: ask for page 0 When only page 0 is
1312 * implemented, a request for page 3F may return Sense Key
1313 * 5: Illegal Request, Sense Code 24: Invalid field in
1314 * CDB.
1315 */
1316 if (!scsi_status_is_good(res))
1317 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1318
1319 /*
1320 * Third attempt: ask 255 bytes, as we did earlier.
1321 */
1322 if (!scsi_status_is_good(res))
1323 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1324 &data, NULL);
1325 }
1326
1327 if (!scsi_status_is_good(res)) {
1328 printk(KERN_WARNING
1329 "%s: test WP failed, assume Write Enabled\n", diskname);
1330 } else {
1331 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1332 set_disk_ro(sdkp->disk, sdkp->write_prot);
1333 printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
1334 sdkp->write_prot ? "on" : "off");
1335 printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
1336 diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
1337 }
1338 }
1339
1340 /*
1341 * sd_read_cache_type - called only from sd_revalidate_disk()
1342 * called with buffer of length 512
1343 */
1344 static void
1345 sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
1346 unsigned char *buffer)
1347 {
1348 int len = 0, res;
1349 struct scsi_device *sdp = sdkp->device;
1350
1351 int dbd;
1352 int modepage;
1353 struct scsi_mode_data data;
1354 struct scsi_sense_hdr sshdr;
1355
1356 if (sdp->skip_ms_page_8)
1357 goto defaults;
1358
1359 if (sdp->type == TYPE_RBC) {
1360 modepage = 6;
1361 dbd = 8;
1362 } else {
1363 modepage = 8;
1364 dbd = 0;
1365 }
1366
1367 /* cautiously ask */
1368 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1369
1370 if (!scsi_status_is_good(res))
1371 goto bad_sense;
1372
1373 /* that went OK, now ask for the proper length */
1374 len = data.length;
1375
1376 /*
1377 * We're only interested in the first three bytes, actually.
1378 * But the data cache page is defined for the first 20.
1379 */
1380 if (len < 3)
1381 goto bad_sense;
1382 if (len > 20)
1383 len = 20;
1384
1385 /* Take headers and block descriptors into account */
1386 len += data.header_length + data.block_descriptor_length;
1387
1388 /* Get the data */
1389 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1390
1391 if (scsi_status_is_good(res)) {
1392 const char *types[] = {
1393 "write through", "none", "write back",
1394 "write back, no read (daft)"
1395 };
1396 int ct = 0;
1397 int offset = data.header_length + data.block_descriptor_length;
1398
1399 if ((buffer[offset] & 0x3f) != modepage) {
1400 printk(KERN_ERR "%s: got wrong page\n", diskname);
1401 goto defaults;
1402 }
1403
1404 if (modepage == 8) {
1405 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1406 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1407 } else {
1408 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1409 sdkp->RCD = 0;
1410 }
1411
1412 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1413 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1414 printk(KERN_NOTICE "SCSI device %s: uses "
1415 "READ/WRITE(6), disabling FUA\n", diskname);
1416 sdkp->DPOFUA = 0;
1417 }
1418
1419 ct = sdkp->RCD + 2*sdkp->WCE;
1420
1421 printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
1422 diskname, types[ct],
1423 sdkp->DPOFUA ? " w/ FUA" : "");
1424
1425 return;
1426 }
1427
1428 bad_sense:
1429 if (scsi_sense_valid(&sshdr) &&
1430 sshdr.sense_key == ILLEGAL_REQUEST &&
1431 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1432 printk(KERN_NOTICE "%s: cache data unavailable\n",
1433 diskname); /* Invalid field in CDB */
1434 else
1435 printk(KERN_ERR "%s: asking for cache data failed\n",
1436 diskname);
1437
1438 defaults:
1439 printk(KERN_ERR "%s: assuming drive cache: write through\n",
1440 diskname);
1441 sdkp->WCE = 0;
1442 sdkp->RCD = 0;
1443 }
1444
1445 /**
1446 * sd_revalidate_disk - called the first time a new disk is seen,
1447 * performs disk spin up, read_capacity, etc.
1448 * @disk: struct gendisk we care about
1449 **/
1450 static int sd_revalidate_disk(struct gendisk *disk)
1451 {
1452 struct scsi_disk *sdkp = scsi_disk(disk);
1453 struct scsi_device *sdp = sdkp->device;
1454 unsigned char *buffer;
1455 unsigned ordered;
1456
1457 SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
1458
1459 /*
1460 * If the device is offline, don't try and read capacity or any
1461 * of the other niceties.
1462 */
1463 if (!scsi_device_online(sdp))
1464 goto out;
1465
1466 buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
1467 if (!buffer) {
1468 printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
1469 "failure.\n");
1470 goto out;
1471 }
1472
1473 /* defaults, until the device tells us otherwise */
1474 sdp->sector_size = 512;
1475 sdkp->capacity = 0;
1476 sdkp->media_present = 1;
1477 sdkp->write_prot = 0;
1478 sdkp->WCE = 0;
1479 sdkp->RCD = 0;
1480
1481 sd_spinup_disk(sdkp, disk->disk_name);
1482
1483 /*
1484 * Without media there is no reason to ask; moreover, some devices
1485 * react badly if we do.
1486 */
1487 if (sdkp->media_present) {
1488 sd_read_capacity(sdkp, disk->disk_name, buffer);
1489 sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
1490 sd_read_cache_type(sdkp, disk->disk_name, buffer);
1491 }
1492
1493 /*
1494 * We now have all cache related info, determine how we deal
1495 * with ordered requests. Note that as the current SCSI
1496 * dispatch function can alter request order, we cannot use
1497 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1498 */
1499 if (sdkp->WCE)
1500 ordered = sdkp->DPOFUA
1501 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
1502 else
1503 ordered = QUEUE_ORDERED_DRAIN;
1504
1505 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1506
1507 set_capacity(disk, sdkp->capacity);
1508 kfree(buffer);
1509
1510 out:
1511 return 0;
1512 }
1513
1514 /**
1515 * sd_probe - called during driver initialization and whenever a
1516 * new scsi device is attached to the system. It is called once
1517 * for each scsi device (not just disks) present.
1518 * @dev: pointer to device object
1519 *
1520 * Returns 0 if successful (or not interested in this scsi device
1521 * (e.g. scanner)); 1 when there is an error.
1522 *
1523 * Note: this function is invoked from the scsi mid-level.
1524 * This function sets up the mapping between a given
1525 * <host,channel,id,lun> (found in sdp) and new device name
1526 * (e.g. /dev/sda). More precisely it is the block device major
1527 * and minor number that is chosen here.
1528 *
1529 * Assume sd_attach is not re-entrant (for time being)
1530 * Also think about sd_attach() and sd_remove() running coincidentally.
1531 **/
1532 static int sd_probe(struct device *dev)
1533 {
1534 struct scsi_device *sdp = to_scsi_device(dev);
1535 struct scsi_disk *sdkp;
1536 struct gendisk *gd;
1537 u32 index;
1538 int error;
1539
1540 error = -ENODEV;
1541 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
1542 goto out;
1543
1544 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1545 "sd_attach\n"));
1546
1547 error = -ENOMEM;
1548 sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
1549 if (!sdkp)
1550 goto out;
1551
1552 memset (sdkp, 0, sizeof(*sdkp));
1553 kref_init(&sdkp->kref);
1554
1555 gd = alloc_disk(16);
1556 if (!gd)
1557 goto out_free;
1558
1559 if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
1560 goto out_put;
1561
1562 spin_lock(&sd_index_lock);
1563 error = idr_get_new(&sd_index_idr, NULL, &index);
1564 spin_unlock(&sd_index_lock);
1565
1566 if (index >= SD_MAX_DISKS)
1567 error = -EBUSY;
1568 if (error)
1569 goto out_put;
1570
1571 get_device(&sdp->sdev_gendev);
1572 sdkp->device = sdp;
1573 sdkp->driver = &sd_template;
1574 sdkp->disk = gd;
1575 sdkp->index = index;
1576 sdkp->openers = 0;
1577
1578 if (!sdp->timeout) {
1579 if (sdp->type != TYPE_MOD)
1580 sdp->timeout = SD_TIMEOUT;
1581 else
1582 sdp->timeout = SD_MOD_TIMEOUT;
1583 }
1584
1585 gd->major = sd_major((index & 0xf0) >> 4);
1586 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1587 gd->minors = 16;
1588 gd->fops = &sd_fops;
1589
1590 if (index < 26) {
1591 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1592 } else if (index < (26 + 1) * 26) {
1593 sprintf(gd->disk_name, "sd%c%c",
1594 'a' + index / 26 - 1,'a' + index % 26);
1595 } else {
1596 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1597 const unsigned int m2 = (index / 26 - 1) % 26;
1598 const unsigned int m3 = index % 26;
1599 sprintf(gd->disk_name, "sd%c%c%c",
1600 'a' + m1, 'a' + m2, 'a' + m3);
1601 }
1602
1603 strcpy(gd->devfs_name, sdp->devfs_name);
1604
1605 gd->private_data = &sdkp->driver;
1606 gd->queue = sdkp->device->request_queue;
1607
1608 sd_revalidate_disk(gd);
1609
1610 gd->driverfs_dev = &sdp->sdev_gendev;
1611 gd->flags = GENHD_FL_DRIVERFS;
1612 if (sdp->removable)
1613 gd->flags |= GENHD_FL_REMOVABLE;
1614
1615 dev_set_drvdata(dev, sdkp);
1616 add_disk(gd);
1617
1618 sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
1619 sdp->removable ? "removable " : "", gd->disk_name);
1620
1621 return 0;
1622
1623 out_put:
1624 put_disk(gd);
1625 out_free:
1626 kfree(sdkp);
1627 out:
1628 return error;
1629 }
1630
1631 /**
1632 * sd_remove - called whenever a scsi disk (previously recognized by
1633 * sd_probe) is detached from the system. It is called (potentially
1634 * multiple times) during sd module unload.
1635 * @sdp: pointer to mid level scsi device object
1636 *
1637 * Note: this function is invoked from the scsi mid-level.
1638 * This function potentially frees up a device name (e.g. /dev/sdc)
1639 * that could be re-used by a subsequent sd_probe().
1640 * This function is not called when the built-in sd driver is "exit-ed".
1641 **/
1642 static int sd_remove(struct device *dev)
1643 {
1644 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1645
1646 del_gendisk(sdkp->disk);
1647 sd_shutdown(dev);
1648
1649 down(&sd_ref_sem);
1650 dev_set_drvdata(dev, NULL);
1651 kref_put(&sdkp->kref, scsi_disk_release);
1652 up(&sd_ref_sem);
1653
1654 return 0;
1655 }
1656
1657 /**
1658 * scsi_disk_release - Called to free the scsi_disk structure
1659 * @kref: pointer to embedded kref
1660 *
1661 * sd_ref_sem must be held entering this routine. Because it is
1662 * called on last put, you should always use the scsi_disk_get()
1663 * scsi_disk_put() helpers which manipulate the semaphore directly
1664 * and never do a direct kref_put().
1665 **/
1666 static void scsi_disk_release(struct kref *kref)
1667 {
1668 struct scsi_disk *sdkp = to_scsi_disk(kref);
1669 struct gendisk *disk = sdkp->disk;
1670
1671 spin_lock(&sd_index_lock);
1672 idr_remove(&sd_index_idr, sdkp->index);
1673 spin_unlock(&sd_index_lock);
1674
1675 disk->private_data = NULL;
1676 put_disk(disk);
1677 put_device(&sdkp->device->sdev_gendev);
1678
1679 kfree(sdkp);
1680 }
1681
1682 /*
1683 * Send a SYNCHRONIZE CACHE instruction down to the device through
1684 * the normal SCSI command structure. Wait for the command to
1685 * complete.
1686 */
1687 static void sd_shutdown(struct device *dev)
1688 {
1689 struct scsi_device *sdp = to_scsi_device(dev);
1690 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1691
1692 if (!sdkp)
1693 return; /* this can happen */
1694
1695 if (sdkp->WCE) {
1696 printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
1697 sdkp->disk->disk_name);
1698 sd_sync_cache(sdp);
1699 }
1700 scsi_disk_put(sdkp);
1701 }
1702
1703 /**
1704 * init_sd - entry point for this driver (both when built in or when
1705 * a module).
1706 *
1707 * Note: this function registers this driver with the scsi mid-level.
1708 **/
1709 static int __init init_sd(void)
1710 {
1711 int majors = 0, i;
1712
1713 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
1714
1715 for (i = 0; i < SD_MAJORS; i++)
1716 if (register_blkdev(sd_major(i), "sd") == 0)
1717 majors++;
1718
1719 if (!majors)
1720 return -ENODEV;
1721
1722 return scsi_register_driver(&sd_template.gendrv);
1723 }
1724
1725 /**
1726 * exit_sd - exit point for this driver (when it is a module).
1727 *
1728 * Note: this function unregisters this driver from the scsi mid-level.
1729 **/
1730 static void __exit exit_sd(void)
1731 {
1732 int i;
1733
1734 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
1735
1736 scsi_unregister_driver(&sd_template.gendrv);
1737 for (i = 0; i < SD_MAJORS; i++)
1738 unregister_blkdev(sd_major(i), "sd");
1739 }
1740
1741 MODULE_LICENSE("GPL");
1742 MODULE_AUTHOR("Eric Youngdale");
1743 MODULE_DESCRIPTION("SCSI disk (sd) driver");
1744
1745 module_init(init_sd);
1746 module_exit(exit_sd);