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