[SCSI] return success after retries in scsi_eh_tur
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / sr.c
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * adapted from:
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
9 *
10 * Modified by Eric Youngdale ericy@andante.org to
11 * add scatter-gather, multiple outstanding request, and other
12 * enhancements.
13 *
14 * Modified by Eric Youngdale eric@andante.org to support loadable
15 * low-level scsi drivers.
16 *
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18 * provide auto-eject.
19 *
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
22 *
23 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
25 *
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
27 *
28 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29 * transparently and lose the GHOST hack
30 *
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
33 */
34
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/bio.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/cdrom.h>
44 #include <linux/interrupt.h>
45 #include <linux/init.h>
46 #include <linux/blkdev.h>
47 #include <asm/uaccess.h>
48
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_dbg.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_eh.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
56 #include <scsi/scsi_request.h>
57
58 #include "scsi_logging.h"
59 #include "sr.h"
60
61
62 #define SR_DISKS 256
63
64 #define MAX_RETRIES 3
65 #define SR_TIMEOUT (30 * HZ)
66 #define SR_CAPABILITIES \
67 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
68 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
69 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
70 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
71 CDC_MRW|CDC_MRW_W|CDC_RAM)
72
73 static int sr_probe(struct device *);
74 static int sr_remove(struct device *);
75 static int sr_init_command(struct scsi_cmnd *);
76
77 static struct scsi_driver sr_template = {
78 .owner = THIS_MODULE,
79 .gendrv = {
80 .name = "sr",
81 .probe = sr_probe,
82 .remove = sr_remove,
83 },
84 .init_command = sr_init_command,
85 };
86
87 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
88 static DEFINE_SPINLOCK(sr_index_lock);
89
90 /* This semaphore is used to mediate the 0->1 reference get in the
91 * face of object destruction (i.e. we can't allow a get on an
92 * object after last put) */
93 static DECLARE_MUTEX(sr_ref_sem);
94
95 static int sr_open(struct cdrom_device_info *, int);
96 static void sr_release(struct cdrom_device_info *);
97
98 static void get_sectorsize(struct scsi_cd *);
99 static void get_capabilities(struct scsi_cd *);
100
101 static int sr_media_change(struct cdrom_device_info *, int);
102 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
103
104 static struct cdrom_device_ops sr_dops = {
105 .open = sr_open,
106 .release = sr_release,
107 .drive_status = sr_drive_status,
108 .media_changed = sr_media_change,
109 .tray_move = sr_tray_move,
110 .lock_door = sr_lock_door,
111 .select_speed = sr_select_speed,
112 .get_last_session = sr_get_last_session,
113 .get_mcn = sr_get_mcn,
114 .reset = sr_reset,
115 .audio_ioctl = sr_audio_ioctl,
116 .dev_ioctl = sr_dev_ioctl,
117 .capability = SR_CAPABILITIES,
118 .generic_packet = sr_packet,
119 };
120
121 static void sr_kref_release(struct kref *kref);
122
123 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
124 {
125 return container_of(disk->private_data, struct scsi_cd, driver);
126 }
127
128 /*
129 * The get and put routines for the struct scsi_cd. Note this entity
130 * has a scsi_device pointer and owns a reference to this.
131 */
132 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
133 {
134 struct scsi_cd *cd = NULL;
135
136 down(&sr_ref_sem);
137 if (disk->private_data == NULL)
138 goto out;
139 cd = scsi_cd(disk);
140 kref_get(&cd->kref);
141 if (scsi_device_get(cd->device))
142 goto out_put;
143 goto out;
144
145 out_put:
146 kref_put(&cd->kref, sr_kref_release);
147 cd = NULL;
148 out:
149 up(&sr_ref_sem);
150 return cd;
151 }
152
153 static inline void scsi_cd_put(struct scsi_cd *cd)
154 {
155 struct scsi_device *sdev = cd->device;
156
157 down(&sr_ref_sem);
158 kref_put(&cd->kref, sr_kref_release);
159 scsi_device_put(sdev);
160 up(&sr_ref_sem);
161 }
162
163 /*
164 * This function checks to see if the media has been changed in the
165 * CDROM drive. It is possible that we have already sensed a change,
166 * or the drive may have sensed one and not yet reported it. We must
167 * be ready for either case. This function always reports the current
168 * value of the changed bit. If flag is 0, then the changed bit is reset.
169 * This function could be done as an ioctl, but we would need to have
170 * an inode for that to work, and we do not always have one.
171 */
172
173 int sr_media_change(struct cdrom_device_info *cdi, int slot)
174 {
175 struct scsi_cd *cd = cdi->handle;
176 int retval;
177
178 if (CDSL_CURRENT != slot) {
179 /* no changer support */
180 return -EINVAL;
181 }
182
183 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
184 if (retval) {
185 /* Unable to test, unit probably not ready. This usually
186 * means there is no disc in the drive. Mark as changed,
187 * and we will figure it out later once the drive is
188 * available again. */
189 cd->device->changed = 1;
190 return 1; /* This will force a flush, if called from
191 * check_disk_change */
192 };
193
194 retval = cd->device->changed;
195 cd->device->changed = 0;
196 /* If the disk changed, the capacity will now be different,
197 * so we force a re-read of this information */
198 if (retval) {
199 /* check multisession offset etc */
200 sr_cd_check(cdi);
201
202 get_sectorsize(cd);
203 }
204 return retval;
205 }
206
207 /*
208 * rw_intr is the interrupt routine for the device driver.
209 *
210 * It will be notified on the end of a SCSI read / write, and will take on
211 * of several actions based on success or failure.
212 */
213 static void rw_intr(struct scsi_cmnd * SCpnt)
214 {
215 int result = SCpnt->result;
216 int this_count = SCpnt->bufflen;
217 int good_bytes = (result == 0 ? this_count : 0);
218 int block_sectors = 0;
219 long error_sector;
220 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
221
222 #ifdef DEBUG
223 printk("sr.c done: %x\n", result);
224 #endif
225
226 /*
227 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
228 * success. Since this is a relatively rare error condition, no
229 * care is taken to avoid unnecessary additional work such as
230 * memcpy's that could be avoided.
231 */
232 if (driver_byte(result) != 0 && /* An error occurred */
233 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
234 switch (SCpnt->sense_buffer[2]) {
235 case MEDIUM_ERROR:
236 case VOLUME_OVERFLOW:
237 case ILLEGAL_REQUEST:
238 if (!(SCpnt->sense_buffer[0] & 0x90))
239 break;
240 if (!blk_fs_request(SCpnt->request))
241 break;
242 error_sector = (SCpnt->sense_buffer[3] << 24) |
243 (SCpnt->sense_buffer[4] << 16) |
244 (SCpnt->sense_buffer[5] << 8) |
245 SCpnt->sense_buffer[6];
246 if (SCpnt->request->bio != NULL)
247 block_sectors =
248 bio_sectors(SCpnt->request->bio);
249 if (block_sectors < 4)
250 block_sectors = 4;
251 if (cd->device->sector_size == 2048)
252 error_sector <<= 2;
253 error_sector &= ~(block_sectors - 1);
254 good_bytes = (error_sector - SCpnt->request->sector) << 9;
255 if (good_bytes < 0 || good_bytes >= this_count)
256 good_bytes = 0;
257 /*
258 * The SCSI specification allows for the value
259 * returned by READ CAPACITY to be up to 75 2K
260 * sectors past the last readable block.
261 * Therefore, if we hit a medium error within the
262 * last 75 2K sectors, we decrease the saved size
263 * value.
264 */
265 if (error_sector < get_capacity(cd->disk) &&
266 cd->capacity - error_sector < 4 * 75)
267 set_capacity(cd->disk, error_sector);
268 break;
269
270 case RECOVERED_ERROR:
271
272 /*
273 * An error occured, but it recovered. Inform the
274 * user, but make sure that it's not treated as a
275 * hard error.
276 */
277 scsi_print_sense("sr", SCpnt);
278 SCpnt->result = 0;
279 SCpnt->sense_buffer[0] = 0x0;
280 good_bytes = this_count;
281 break;
282
283 default:
284 break;
285 }
286 }
287
288 /*
289 * This calls the generic completion function, now that we know
290 * how many actual sectors finished, and how many sectors we need
291 * to say have failed.
292 */
293 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
294 }
295
296 static int sr_init_command(struct scsi_cmnd * SCpnt)
297 {
298 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
299 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
300
301 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
302 cd->disk->disk_name, block));
303
304 if (!cd->device || !scsi_device_online(cd->device)) {
305 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
306 SCpnt->request->nr_sectors));
307 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
308 return 0;
309 }
310
311 if (cd->device->changed) {
312 /*
313 * quietly refuse to do anything to a changed disc until the
314 * changed bit has been reset
315 */
316 return 0;
317 }
318
319 /*
320 * these are already setup, just copy cdb basically
321 */
322 if (SCpnt->request->flags & REQ_BLOCK_PC) {
323 struct request *rq = SCpnt->request;
324
325 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
326 return 0;
327
328 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
329 if (!rq->data_len)
330 SCpnt->sc_data_direction = DMA_NONE;
331 else if (rq_data_dir(rq) == WRITE)
332 SCpnt->sc_data_direction = DMA_TO_DEVICE;
333 else
334 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
335
336 this_count = rq->data_len;
337 if (rq->timeout)
338 timeout = rq->timeout;
339
340 SCpnt->transfersize = rq->data_len;
341 goto queue;
342 }
343
344 if (!(SCpnt->request->flags & REQ_CMD)) {
345 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
346 return 0;
347 }
348
349 /*
350 * we do lazy blocksize switching (when reading XA sectors,
351 * see CDROMREADMODE2 ioctl)
352 */
353 s_size = cd->device->sector_size;
354 if (s_size > 2048) {
355 if (!in_interrupt())
356 sr_set_blocklength(cd, 2048);
357 else
358 printk("sr: can't switch blocksize: in interrupt\n");
359 }
360
361 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
362 printk("sr: bad sector size %d\n", s_size);
363 return 0;
364 }
365
366 if (rq_data_dir(SCpnt->request) == WRITE) {
367 if (!cd->device->writeable)
368 return 0;
369 SCpnt->cmnd[0] = WRITE_10;
370 SCpnt->sc_data_direction = DMA_TO_DEVICE;
371 cd->cdi.media_written = 1;
372 } else if (rq_data_dir(SCpnt->request) == READ) {
373 SCpnt->cmnd[0] = READ_10;
374 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
375 } else {
376 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
377 return 0;
378 }
379
380 {
381 struct scatterlist *sg = SCpnt->request_buffer;
382 int i, size = 0;
383 for (i = 0; i < SCpnt->use_sg; i++)
384 size += sg[i].length;
385
386 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
387 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
388 size, SCpnt->request_bufflen);
389 if (SCpnt->request_bufflen > size)
390 SCpnt->request_bufflen = SCpnt->bufflen = size;
391 }
392 }
393
394 /*
395 * request doesn't start on hw block boundary, add scatter pads
396 */
397 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
398 (SCpnt->request_bufflen % s_size)) {
399 printk("sr: unaligned transfer\n");
400 return 0;
401 }
402
403 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
404
405
406 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
407 cd->cdi.name,
408 (rq_data_dir(SCpnt->request) == WRITE) ?
409 "writing" : "reading",
410 this_count, SCpnt->request->nr_sectors));
411
412 SCpnt->cmnd[1] = 0;
413 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
414
415 if (this_count > 0xffff) {
416 this_count = 0xffff;
417 SCpnt->request_bufflen = SCpnt->bufflen =
418 this_count * s_size;
419 }
420
421 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
422 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
423 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
424 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
425 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
426 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
427 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
428
429 /*
430 * We shouldn't disconnect in the middle of a sector, so with a dumb
431 * host adapter, it's safe to assume that we can at least transfer
432 * this many bytes between each connect / disconnect.
433 */
434 SCpnt->transfersize = cd->device->sector_size;
435 SCpnt->underflow = this_count << 9;
436
437 queue:
438 SCpnt->allowed = MAX_RETRIES;
439 SCpnt->timeout_per_command = timeout;
440
441 /*
442 * This is the completion routine we use. This is matched in terms
443 * of capability to this function.
444 */
445 SCpnt->done = rw_intr;
446
447 /*
448 * This indicates that the command is ready from our end to be
449 * queued.
450 */
451 return 1;
452 }
453
454 static int sr_block_open(struct inode *inode, struct file *file)
455 {
456 struct gendisk *disk = inode->i_bdev->bd_disk;
457 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
458 int ret = 0;
459
460 if(!(cd = scsi_cd_get(disk)))
461 return -ENXIO;
462
463 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
464 scsi_cd_put(cd);
465
466 return ret;
467 }
468
469 static int sr_block_release(struct inode *inode, struct file *file)
470 {
471 int ret;
472 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
473 ret = cdrom_release(&cd->cdi, file);
474 if(ret)
475 return ret;
476
477 scsi_cd_put(cd);
478
479 return 0;
480 }
481
482 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
483 unsigned long arg)
484 {
485 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
486 struct scsi_device *sdev = cd->device;
487
488 /*
489 * Send SCSI addressing ioctls directly to mid level, send other
490 * ioctls to cdrom/block level.
491 */
492 switch (cmd) {
493 case SCSI_IOCTL_GET_IDLUN:
494 case SCSI_IOCTL_GET_BUS_NUMBER:
495 return scsi_ioctl(sdev, cmd, (void __user *)arg);
496 }
497 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
498 }
499
500 static int sr_block_media_changed(struct gendisk *disk)
501 {
502 struct scsi_cd *cd = scsi_cd(disk);
503 return cdrom_media_changed(&cd->cdi);
504 }
505
506 static struct block_device_operations sr_bdops =
507 {
508 .owner = THIS_MODULE,
509 .open = sr_block_open,
510 .release = sr_block_release,
511 .ioctl = sr_block_ioctl,
512 .media_changed = sr_block_media_changed,
513 /*
514 * No compat_ioctl for now because sr_block_ioctl never
515 * seems to pass arbitary ioctls down to host drivers.
516 */
517 };
518
519 static int sr_open(struct cdrom_device_info *cdi, int purpose)
520 {
521 struct scsi_cd *cd = cdi->handle;
522 struct scsi_device *sdev = cd->device;
523 int retval;
524
525 /*
526 * If the device is in error recovery, wait until it is done.
527 * If the device is offline, then disallow any access to it.
528 */
529 retval = -ENXIO;
530 if (!scsi_block_when_processing_errors(sdev))
531 goto error_out;
532
533 return 0;
534
535 error_out:
536 return retval;
537 }
538
539 static void sr_release(struct cdrom_device_info *cdi)
540 {
541 struct scsi_cd *cd = cdi->handle;
542
543 if (cd->device->sector_size > 2048)
544 sr_set_blocklength(cd, 2048);
545
546 }
547
548 static int sr_probe(struct device *dev)
549 {
550 struct scsi_device *sdev = to_scsi_device(dev);
551 struct gendisk *disk;
552 struct scsi_cd *cd;
553 int minor, error;
554
555 error = -ENODEV;
556 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
557 goto fail;
558
559 error = -ENOMEM;
560 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
561 if (!cd)
562 goto fail;
563 memset(cd, 0, sizeof(*cd));
564
565 kref_init(&cd->kref);
566
567 disk = alloc_disk(1);
568 if (!disk)
569 goto fail_free;
570
571 spin_lock(&sr_index_lock);
572 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
573 if (minor == SR_DISKS) {
574 spin_unlock(&sr_index_lock);
575 error = -EBUSY;
576 goto fail_put;
577 }
578 __set_bit(minor, sr_index_bits);
579 spin_unlock(&sr_index_lock);
580
581 disk->major = SCSI_CDROM_MAJOR;
582 disk->first_minor = minor;
583 sprintf(disk->disk_name, "sr%d", minor);
584 disk->fops = &sr_bdops;
585 disk->flags = GENHD_FL_CD;
586
587 cd->device = sdev;
588 cd->disk = disk;
589 cd->driver = &sr_template;
590 cd->disk = disk;
591 cd->capacity = 0x1fffff;
592 cd->device->changed = 1; /* force recheck CD type */
593 cd->use = 1;
594 cd->readcd_known = 0;
595 cd->readcd_cdda = 0;
596
597 cd->cdi.ops = &sr_dops;
598 cd->cdi.handle = cd;
599 cd->cdi.mask = 0;
600 cd->cdi.capacity = 1;
601 sprintf(cd->cdi.name, "sr%d", minor);
602
603 sdev->sector_size = 2048; /* A guess, just in case */
604
605 /* FIXME: need to handle a get_capabilities failure properly ?? */
606 get_capabilities(cd);
607 sr_vendor_init(cd);
608
609 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
610 "%s/cd", sdev->devfs_name);
611 disk->driverfs_dev = &sdev->sdev_gendev;
612 set_capacity(disk, cd->capacity);
613 disk->private_data = &cd->driver;
614 disk->queue = sdev->request_queue;
615 cd->cdi.disk = disk;
616
617 if (register_cdrom(&cd->cdi))
618 goto fail_put;
619
620 dev_set_drvdata(dev, cd);
621 disk->flags |= GENHD_FL_REMOVABLE;
622 add_disk(disk);
623
624 printk(KERN_DEBUG
625 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
626 cd->cdi.name, sdev->host->host_no, sdev->channel,
627 sdev->id, sdev->lun);
628 return 0;
629
630 fail_put:
631 put_disk(disk);
632 fail_free:
633 kfree(cd);
634 fail:
635 return error;
636 }
637
638
639 static void get_sectorsize(struct scsi_cd *cd)
640 {
641 unsigned char cmd[10];
642 unsigned char *buffer;
643 int the_result, retries = 3;
644 int sector_size;
645 struct scsi_request *SRpnt = NULL;
646 request_queue_t *queue;
647
648 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
649 if (!buffer)
650 goto Enomem;
651 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
652 if (!SRpnt)
653 goto Enomem;
654
655 do {
656 cmd[0] = READ_CAPACITY;
657 memset((void *) &cmd[1], 0, 9);
658 /* Mark as really busy */
659 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
660 SRpnt->sr_cmd_len = 0;
661
662 memset(buffer, 0, 8);
663
664 /* Do the command and wait.. */
665 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
666 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
667 8, SR_TIMEOUT, MAX_RETRIES);
668
669 the_result = SRpnt->sr_result;
670 retries--;
671
672 } while (the_result && retries);
673
674
675 scsi_release_request(SRpnt);
676 SRpnt = NULL;
677
678 if (the_result) {
679 cd->capacity = 0x1fffff;
680 sector_size = 2048; /* A guess, just in case */
681 } else {
682 #if 0
683 if (cdrom_get_last_written(&cd->cdi,
684 &cd->capacity))
685 #endif
686 cd->capacity = 1 + ((buffer[0] << 24) |
687 (buffer[1] << 16) |
688 (buffer[2] << 8) |
689 buffer[3]);
690 sector_size = (buffer[4] << 24) |
691 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
692 switch (sector_size) {
693 /*
694 * HP 4020i CD-Recorder reports 2340 byte sectors
695 * Philips CD-Writers report 2352 byte sectors
696 *
697 * Use 2k sectors for them..
698 */
699 case 0:
700 case 2340:
701 case 2352:
702 sector_size = 2048;
703 /* fall through */
704 case 2048:
705 cd->capacity *= 4;
706 /* fall through */
707 case 512:
708 break;
709 default:
710 printk("%s: unsupported sector size %d.\n",
711 cd->cdi.name, sector_size);
712 cd->capacity = 0;
713 }
714
715 cd->device->sector_size = sector_size;
716
717 /*
718 * Add this so that we have the ability to correctly gauge
719 * what the device is capable of.
720 */
721 set_capacity(cd->disk, cd->capacity);
722 }
723
724 queue = cd->device->request_queue;
725 blk_queue_hardsect_size(queue, sector_size);
726 out:
727 kfree(buffer);
728 return;
729
730 Enomem:
731 cd->capacity = 0x1fffff;
732 cd->device->sector_size = 2048; /* A guess, just in case */
733 if (SRpnt)
734 scsi_release_request(SRpnt);
735 goto out;
736 }
737
738 static void get_capabilities(struct scsi_cd *cd)
739 {
740 unsigned char *buffer;
741 struct scsi_mode_data data;
742 struct scsi_request *SRpnt;
743 unsigned char cmd[MAX_COMMAND_SIZE];
744 unsigned int the_result;
745 int retries, rc, n;
746
747 static char *loadmech[] =
748 {
749 "caddy",
750 "tray",
751 "pop-up",
752 "",
753 "changer",
754 "cartridge changer",
755 "",
756 ""
757 };
758
759 /* allocate a request for the TEST_UNIT_READY */
760 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
761 if (!SRpnt) {
762 printk(KERN_WARNING "(get_capabilities:) Request allocation "
763 "failure.\n");
764 return;
765 }
766
767 /* allocate transfer buffer */
768 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
769 if (!buffer) {
770 printk(KERN_ERR "sr: out of memory.\n");
771 scsi_release_request(SRpnt);
772 return;
773 }
774
775 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
776 * conditions are gone, or a timeout happens
777 */
778 retries = 0;
779 do {
780 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
781 cmd[0] = TEST_UNIT_READY;
782
783 SRpnt->sr_cmd_len = 0;
784 SRpnt->sr_sense_buffer[0] = 0;
785 SRpnt->sr_sense_buffer[2] = 0;
786 SRpnt->sr_data_direction = DMA_NONE;
787
788 scsi_wait_req (SRpnt, (void *) cmd, buffer,
789 0, SR_TIMEOUT, MAX_RETRIES);
790
791 the_result = SRpnt->sr_result;
792 retries++;
793 } while (retries < 5 &&
794 (!scsi_status_is_good(the_result) ||
795 ((driver_byte(the_result) & DRIVER_SENSE) &&
796 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
797
798 /* ask for mode page 0x2a */
799 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
800 SR_TIMEOUT, 3, &data);
801
802 if (!scsi_status_is_good(rc)) {
803 /* failed, drive doesn't have capabilities mode page */
804 cd->cdi.speed = 1;
805 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
806 CDC_DVD | CDC_DVD_RAM |
807 CDC_SELECT_DISC | CDC_SELECT_SPEED);
808 scsi_release_request(SRpnt);
809 kfree(buffer);
810 printk("%s: scsi-1 drive\n", cd->cdi.name);
811 return;
812 }
813
814 n = data.header_length + data.block_descriptor_length;
815 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
816 cd->readcd_known = 1;
817 cd->readcd_cdda = buffer[n + 5] & 0x01;
818 /* print some capability bits */
819 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
820 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
821 cd->cdi.speed,
822 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
823 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
824 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
825 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
826 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
827 loadmech[buffer[n + 6] >> 5]);
828 if ((buffer[n + 6] >> 5) == 0)
829 /* caddy drives can't close tray... */
830 cd->cdi.mask |= CDC_CLOSE_TRAY;
831 if ((buffer[n + 2] & 0x8) == 0)
832 /* not a DVD drive */
833 cd->cdi.mask |= CDC_DVD;
834 if ((buffer[n + 3] & 0x20) == 0)
835 /* can't write DVD-RAM media */
836 cd->cdi.mask |= CDC_DVD_RAM;
837 if ((buffer[n + 3] & 0x10) == 0)
838 /* can't write DVD-R media */
839 cd->cdi.mask |= CDC_DVD_R;
840 if ((buffer[n + 3] & 0x2) == 0)
841 /* can't write CD-RW media */
842 cd->cdi.mask |= CDC_CD_RW;
843 if ((buffer[n + 3] & 0x1) == 0)
844 /* can't write CD-R media */
845 cd->cdi.mask |= CDC_CD_R;
846 if ((buffer[n + 6] & 0x8) == 0)
847 /* can't eject */
848 cd->cdi.mask |= CDC_OPEN_TRAY;
849
850 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
851 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
852 cd->cdi.capacity =
853 cdrom_number_of_slots(&cd->cdi);
854 if (cd->cdi.capacity <= 1)
855 /* not a changer */
856 cd->cdi.mask |= CDC_SELECT_DISC;
857 /*else I don't think it can close its tray
858 cd->cdi.mask |= CDC_CLOSE_TRAY; */
859
860 /*
861 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
862 */
863 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
864 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
865 cd->device->writeable = 1;
866 }
867
868 scsi_release_request(SRpnt);
869 kfree(buffer);
870 }
871
872 /*
873 * sr_packet() is the entry point for the generic commands generated
874 * by the Uniform CD-ROM layer.
875 */
876 static int sr_packet(struct cdrom_device_info *cdi,
877 struct packet_command *cgc)
878 {
879 if (cgc->timeout <= 0)
880 cgc->timeout = IOCTL_TIMEOUT;
881
882 sr_do_ioctl(cdi->handle, cgc);
883
884 return cgc->stat;
885 }
886
887 /**
888 * sr_kref_release - Called to free the scsi_cd structure
889 * @kref: pointer to embedded kref
890 *
891 * sr_ref_sem must be held entering this routine. Because it is
892 * called on last put, you should always use the scsi_cd_get()
893 * scsi_cd_put() helpers which manipulate the semaphore directly
894 * and never do a direct kref_put().
895 **/
896 static void sr_kref_release(struct kref *kref)
897 {
898 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
899 struct gendisk *disk = cd->disk;
900
901 spin_lock(&sr_index_lock);
902 clear_bit(disk->first_minor, sr_index_bits);
903 spin_unlock(&sr_index_lock);
904
905 unregister_cdrom(&cd->cdi);
906
907 disk->private_data = NULL;
908
909 put_disk(disk);
910
911 kfree(cd);
912 }
913
914 static int sr_remove(struct device *dev)
915 {
916 struct scsi_cd *cd = dev_get_drvdata(dev);
917
918 del_gendisk(cd->disk);
919
920 down(&sr_ref_sem);
921 kref_put(&cd->kref, sr_kref_release);
922 up(&sr_ref_sem);
923
924 return 0;
925 }
926
927 static int __init init_sr(void)
928 {
929 int rc;
930
931 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
932 if (rc)
933 return rc;
934 return scsi_register_driver(&sr_template.gendrv);
935 }
936
937 static void __exit exit_sr(void)
938 {
939 scsi_unregister_driver(&sr_template.gendrv);
940 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
941 }
942
943 module_init(init_sr);
944 module_exit(exit_sr);
945 MODULE_LICENSE("GPL");