Import 2.3.18pre1
[davej-history.git] / drivers / scsi / sr.c
blob687b0beee4d851321f2f47e42db46299b66d670b
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
5 * adapted from:
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
10 * Modified by Eric Youngdale ericy@cais.com to
11 * add scatter-gather, multiple outstanding request, and other
12 * enhancements.
14 * Modified by Eric Youngdale eric@aib.com to support loadable
15 * low-level scsi drivers.
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18 * provide auto-eject.
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
23 * Modified by Jens Axboe <axboe@image.dk> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
28 #include <linux/module.h>
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/cdrom.h>
37 #include <linux/interrupt.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/uaccess.h>
42 #define MAJOR_NR SCSI_CDROM_MAJOR
43 #include <linux/blk.h>
44 #include "scsi.h"
45 #include "hosts.h"
46 #include "sr.h"
47 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
48 #include "constants.h"
50 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
52 #define MAX_RETRIES 3
53 #define SR_TIMEOUT (30 * HZ)
55 static int sr_init(void);
56 static void sr_finish(void);
57 static int sr_attach(Scsi_Device *);
58 static int sr_detect(Scsi_Device *);
59 static void sr_detach(Scsi_Device *);
61 struct Scsi_Device_Template sr_template = {
62 NULL, "cdrom", "sr", NULL, TYPE_ROM,
63 SCSI_CDROM_MAJOR, 0, 0, 0, 1,
64 sr_detect, sr_init,
65 sr_finish, sr_attach, sr_detach
68 Scsi_CD *scsi_CDs = NULL;
69 static int *sr_sizes = NULL;
71 static int *sr_blocksizes = NULL;
73 static int sr_open(struct cdrom_device_info *, int);
74 void get_sectorsize(int);
75 void get_capabilities(int);
77 void requeue_sr_request(Scsi_Cmnd * SCpnt);
78 static int sr_media_change(struct cdrom_device_info *, int);
79 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
81 static void sr_release(struct cdrom_device_info *cdi)
83 if (scsi_CDs[MINOR(cdi->dev)].sector_size > 2048)
84 sr_set_blocklength(MINOR(cdi->dev), 2048);
85 sync_dev(cdi->dev);
86 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
87 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
88 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
89 if (sr_template.module)
90 __MOD_DEC_USE_COUNT(sr_template.module);
93 static struct cdrom_device_ops sr_dops =
95 sr_open, /* open */
96 sr_release, /* release */
97 sr_drive_status, /* drive status */
98 sr_media_change, /* media changed */
99 sr_tray_move, /* tray move */
100 sr_lock_door, /* lock door */
101 sr_select_speed, /* select speed */
102 NULL, /* select disc */
103 sr_get_last_session, /* get last session */
104 sr_get_mcn, /* get universal product code */
105 sr_reset, /* hard reset */
106 sr_audio_ioctl, /* audio ioctl */
107 sr_dev_ioctl, /* device-specific ioctl */
108 CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED |
109 CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
110 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R | CDC_CD_RW |
111 CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET,
113 sr_packet
117 * This function checks to see if the media has been changed in the
118 * CDROM drive. It is possible that we have already sensed a change,
119 * or the drive may have sensed one and not yet reported it. We must
120 * be ready for either case. This function always reports the current
121 * value of the changed bit. If flag is 0, then the changed bit is reset.
122 * This function could be done as an ioctl, but we would need to have
123 * an inode for that to work, and we do not always have one.
126 int sr_media_change(struct cdrom_device_info *cdi, int slot)
128 int retval;
130 if (CDSL_CURRENT != slot) {
131 /* no changer support */
132 return -EINVAL;
134 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
135 SCSI_IOCTL_TEST_UNIT_READY, 0);
137 if (retval) {
138 /* Unable to test, unit probably not ready. This usually
139 * means there is no disc in the drive. Mark as changed,
140 * and we will figure it out later once the drive is
141 * available again. */
143 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
144 return 1; /* This will force a flush, if called from
145 * check_disk_change */
148 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
149 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
150 /* If the disk changed, the capacity will now be different,
151 * so we force a re-read of this information */
152 if (retval) {
153 /* check multisession offset etc */
154 sr_cd_check(cdi);
157 * If the disk changed, the capacity will now be different,
158 * so we force a re-read of this information
159 * Force 2048 for the sector size so that filesystems won't
160 * be trying to use something that is too small if the disc
161 * has changed.
163 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
165 scsi_CDs[MINOR(cdi->dev)].sector_size = 2048;
167 return retval;
171 * rw_intr is the interrupt routine for the device driver. It will be notified on the
172 * end of a SCSI read / write, and will take on of several actions based on success or failure.
175 static void rw_intr(Scsi_Cmnd * SCpnt)
177 int result = SCpnt->result;
178 int this_count = SCpnt->this_count;
179 int good_sectors = (result == 0 ? this_count : 0);
180 int block_sectors = 0;
182 #ifdef DEBUG
183 printk("sr.c done: %x %x\n", result, SCpnt->request.bh->b_data);
184 #endif
186 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
187 Since this is a relatively rare error condition, no care is taken to
188 avoid unnecessary additional work such as memcpy's that could be avoided.
191 if (driver_byte(result) != 0 && /* An error occurred */
192 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
193 (SCpnt->sense_buffer[2] == MEDIUM_ERROR ||
194 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
195 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
196 long error_sector = (SCpnt->sense_buffer[3] << 24) |
197 (SCpnt->sense_buffer[4] << 16) |
198 (SCpnt->sense_buffer[5] << 8) |
199 SCpnt->sense_buffer[6];
200 int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
201 if (SCpnt->request.bh != NULL)
202 block_sectors = SCpnt->request.bh->b_size >> 9;
203 if (block_sectors < 4)
204 block_sectors = 4;
205 if (scsi_CDs[device_nr].sector_size == 2048)
206 error_sector <<= 2;
207 error_sector &= ~(block_sectors - 1);
208 good_sectors = error_sector - SCpnt->request.sector;
209 if (good_sectors < 0 || good_sectors >= this_count)
210 good_sectors = 0;
212 The SCSI specification allows for the value returned by READ
213 CAPACITY to be up to 75 2K sectors past the last readable
214 block. Therefore, if we hit a medium error within the last
215 75 2K sectors, we decrease the saved size value.
217 if ((error_sector >> 1) < sr_sizes[device_nr] &&
218 scsi_CDs[device_nr].capacity - error_sector < 4 * 75)
219 sr_sizes[device_nr] = error_sector >> 1;
221 if (good_sectors > 0) { /* Some sectors were read successfully. */
222 if (SCpnt->use_sg == 0) {
223 if (SCpnt->buffer != SCpnt->request.buffer) {
224 int offset;
225 offset = (SCpnt->request.sector % 4) << 9;
226 memcpy((char *) SCpnt->request.buffer,
227 (char *) SCpnt->buffer + offset,
228 good_sectors << 9);
229 /* Even though we are not using scatter-gather, we look
230 * ahead and see if there is a linked request for the
231 * other half of this buffer. If there is, then satisfy
232 * it. */
233 if ((offset == 0) && good_sectors == 2 &&
234 SCpnt->request.nr_sectors > good_sectors &&
235 SCpnt->request.bh &&
236 SCpnt->request.bh->b_reqnext &&
237 SCpnt->request.bh->b_reqnext->b_size == 1024) {
238 memcpy((char *) SCpnt->request.bh->b_reqnext->b_data,
239 (char *) SCpnt->buffer + 1024,
240 1024);
241 good_sectors += 2;
244 scsi_free(SCpnt->buffer, 2048);
246 } else {
247 struct scatterlist *sgpnt;
248 int i;
249 sgpnt = (struct scatterlist *) SCpnt->buffer;
250 for (i = 0; i < SCpnt->use_sg; i++) {
251 if (sgpnt[i].alt_address) {
252 if (sgpnt[i].alt_address != sgpnt[i].address) {
253 memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
255 scsi_free(sgpnt[i].address, sgpnt[i].length);
258 scsi_free(SCpnt->buffer, SCpnt->sglist_len); /* Free list of scatter-gather pointers */
259 if (SCpnt->request.sector % 4)
260 good_sectors -= 2;
261 /* See if there is a padding record at the end that needs to be removed */
262 if (good_sectors > SCpnt->request.nr_sectors)
263 good_sectors -= 2;
266 #ifdef DEBUG
267 printk("(%x %x %x) ", SCpnt->request.bh, SCpnt->request.nr_sectors,
268 good_sectors);
269 #endif
270 if (SCpnt->request.nr_sectors > this_count) {
271 SCpnt->request.errors = 0;
272 if (!SCpnt->request.bh)
273 panic("sr.c: linked page request (%lx %x)",
274 SCpnt->request.sector, this_count);
276 SCpnt = end_scsi_request(SCpnt, 1, good_sectors); /* All done */
277 if (result == 0) {
278 requeue_sr_request(SCpnt);
279 return;
282 if (good_sectors == 0) {
283 /* We only come through here if no sectors were read successfully. */
285 /* Free up any indirection buffers we allocated for DMA purposes. */
286 if (SCpnt->use_sg) {
287 struct scatterlist *sgpnt;
288 int i;
289 sgpnt = (struct scatterlist *) SCpnt->buffer;
290 for (i = 0; i < SCpnt->use_sg; i++) {
291 if (sgpnt[i].alt_address) {
292 scsi_free(sgpnt[i].address, sgpnt[i].length);
295 scsi_free(SCpnt->buffer, SCpnt->sglist_len); /* Free list of scatter-gather pointers */
296 } else {
297 if (SCpnt->buffer != SCpnt->request.buffer)
298 scsi_free(SCpnt->buffer, SCpnt->bufflen);
302 if (driver_byte(result) != 0) {
303 if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
304 if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
305 /* detected disc change. set a bit and quietly refuse
306 * further access. */
308 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].device->changed = 1;
309 SCpnt = end_scsi_request(SCpnt, 0, this_count);
310 requeue_sr_request(SCpnt);
311 return;
314 if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
315 printk("sr%d: CD-ROM error: ",
316 DEVICE_NR(SCpnt->request.rq_dev));
317 print_sense("sr", SCpnt);
318 printk("command was: ");
319 print_command(SCpnt->cmnd);
320 if (scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].ten) {
321 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].ten = 0;
322 requeue_sr_request(SCpnt);
323 result = 0;
324 return;
325 } else {
326 SCpnt = end_scsi_request(SCpnt, 0, this_count);
327 requeue_sr_request(SCpnt); /* Do next request */
328 return;
332 if (SCpnt->sense_buffer[2] == NOT_READY) {
333 printk(KERN_INFO "sr%d: CD-ROM not ready. Make sure you have a disc in the drive.\n",
334 DEVICE_NR(SCpnt->request.rq_dev));
335 SCpnt = end_scsi_request(SCpnt, 0, this_count);
336 requeue_sr_request(SCpnt); /* Do next request */
337 return;
339 if (SCpnt->sense_buffer[2] == MEDIUM_ERROR) {
340 printk("scsi%d: MEDIUM ERROR on "
341 "channel %d, id %d, lun %d, CDB: ",
342 SCpnt->host->host_no, (int) SCpnt->channel,
343 (int) SCpnt->target, (int) SCpnt->lun);
344 print_command(SCpnt->cmnd);
345 print_sense("sr", SCpnt);
346 SCpnt = end_scsi_request(SCpnt, 0, block_sectors);
347 requeue_sr_request(SCpnt);
348 return;
350 if (SCpnt->sense_buffer[2] == VOLUME_OVERFLOW) {
351 printk("scsi%d: VOLUME OVERFLOW on "
352 "channel %d, id %d, lun %d, CDB: ",
353 SCpnt->host->host_no, (int) SCpnt->channel,
354 (int) SCpnt->target, (int) SCpnt->lun);
355 print_command(SCpnt->cmnd);
356 print_sense("sr", SCpnt);
357 SCpnt = end_scsi_request(SCpnt, 0, block_sectors);
358 requeue_sr_request(SCpnt);
359 return;
362 /* We only get this far if we have an error we have not recognized */
363 if (result) {
364 printk("SCSI CD error : host %d id %d lun %d return code = %03x\n",
365 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].device->host->host_no,
366 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].device->id,
367 scsi_CDs[DEVICE_NR(SCpnt->request.rq_dev)].device->lun,
368 result);
370 if (status_byte(result) == CHECK_CONDITION)
371 print_sense("sr", SCpnt);
373 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
374 requeue_sr_request(SCpnt);
378 static int sr_open(struct cdrom_device_info *cdi, int purpose)
380 check_disk_change(cdi->dev);
382 if (MINOR(cdi->dev) >= sr_template.dev_max
383 || !scsi_CDs[MINOR(cdi->dev)].device) {
384 return -ENXIO; /* No such device */
387 * If the device is in error recovery, wait until it is done.
388 * If the device is offline, then disallow any access to it.
390 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
391 return -ENXIO;
393 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
394 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
395 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
396 if (sr_template.module)
397 __MOD_INC_USE_COUNT(sr_template.module);
399 /* If this device did not have media in the drive at boot time, then
400 * we would have been unable to get the sector size. Check to see if
401 * this is the case, and try again.
404 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
405 get_sectorsize(MINOR(cdi->dev));
407 return 0;
411 * do_sr_request() is the request handler function for the sr driver.
412 * Its function in life is to take block device requests, and
413 * translate them to SCSI commands.
416 static void do_sr_request(void)
418 Scsi_Cmnd *SCpnt = NULL;
419 struct request *req = NULL;
420 Scsi_Device *SDev;
421 int flag = 0;
423 while (1 == 1) {
424 if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
425 return;
428 INIT_SCSI_REQUEST;
430 SDev = scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device;
433 * If the host for this device is in error recovery mode, don't
434 * do anything at all here. When the host leaves error recovery
435 * mode, it will automatically restart things and start queueing
436 * commands again.
438 if (SDev->host->in_recovery) {
439 return;
442 * I am not sure where the best place to do this is. We need
443 * to hook in a place where we are likely to come if in user
444 * space.
446 if (SDev->was_reset) {
448 * We need to relock the door, but we might
449 * be in an interrupt handler. Only do this
450 * from user space, since we do not want to
451 * sleep from an interrupt.
453 if (SDev->removable && !in_interrupt()) {
454 spin_unlock_irq(&io_request_lock); /* FIXME!!!! */
455 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
456 spin_lock_irq(&io_request_lock); /* FIXME!!!! */
457 /* scsi_ioctl may allow CURRENT to change, so start over. */
458 SDev->was_reset = 0;
459 continue;
461 SDev->was_reset = 0;
463 /* we do lazy blocksize switching (when reading XA sectors,
464 * see CDROMREADMODE2 ioctl) */
465 if (scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].sector_size > 2048) {
466 if (!in_interrupt())
467 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
468 #if 1
469 else
470 printk("sr: can't switch blocksize: in interrupt\n");
471 #endif
473 if (flag++ == 0)
474 SCpnt = scsi_allocate_device(&CURRENT,
475 scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device, 0);
476 else
477 SCpnt = NULL;
479 /* This is a performance enhancement. We dig down into the request list and
480 * try to find a queueable request (i.e. device not busy, and host able to
481 * accept another command. If we find one, then we queue it. This can
482 * make a big difference on systems with more than one disk drive. We want
483 * to have the interrupts off when monkeying with the request list, because
484 * otherwise the kernel might try to slip in a request in between somewhere. */
486 if (!SCpnt && sr_template.nr_dev > 1) {
487 struct request *req1;
488 req1 = NULL;
489 req = CURRENT;
490 while (req) {
491 SCpnt = scsi_request_queueable(req,
492 scsi_CDs[DEVICE_NR(req->rq_dev)].device);
493 if (SCpnt)
494 break;
495 req1 = req;
496 req = req->next;
498 if (SCpnt && req->rq_status == RQ_INACTIVE) {
499 if (req == CURRENT)
500 CURRENT = CURRENT->next;
501 else
502 req1->next = req->next;
505 if (!SCpnt)
506 return; /* Could not find anything to do */
508 wake_up(&wait_for_request);
510 /* Queue command */
511 requeue_sr_request(SCpnt);
512 } /* While */
515 void requeue_sr_request(Scsi_Cmnd * SCpnt)
517 unsigned int dev, block, realcount;
518 unsigned char cmd[10], *buffer, tries;
519 int this_count, start, end_rec;
521 tries = 2;
523 repeat:
524 if (!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
525 do_sr_request();
526 return;
528 dev = MINOR(SCpnt->request.rq_dev);
529 block = SCpnt->request.sector;
530 buffer = NULL;
531 this_count = 0;
533 if (dev >= sr_template.nr_dev) {
534 /* printk("CD-ROM request error: invalid device.\n"); */
535 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
536 tries = 2;
537 goto repeat;
539 if (!scsi_CDs[dev].use) {
540 /* printk("CD-ROM request error: device marked not in use.\n"); */
541 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
542 tries = 2;
543 goto repeat;
545 if (!scsi_CDs[dev].device->online) {
546 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
547 tries = 2;
548 goto repeat;
550 if (scsi_CDs[dev].device->changed) {
552 * quietly refuse to do anything to a changed disc
553 * until the changed bit has been reset
555 /* printk("CD-ROM has been changed. Prohibiting further I/O.\n"); */
556 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
557 tries = 2;
558 goto repeat;
560 switch (SCpnt->request.cmd) {
561 case WRITE:
562 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
563 goto repeat;
564 break;
565 case READ:
566 cmd[0] = READ_6;
567 break;
568 default:
569 panic("Unknown sr command %d\n", SCpnt->request.cmd);
572 cmd[1] = (SCpnt->lun << 5) & 0xe0;
575 * Now do the grungy work of figuring out which sectors we need, and
576 * where in memory we are going to put them.
578 * The variables we need are:
580 * this_count= number of 512 byte sectors being read
581 * block = starting cdrom sector to read.
582 * realcount = # of cdrom sectors to read
584 * The major difference between a scsi disk and a scsi cdrom
585 * is that we will always use scatter-gather if we can, because we can
586 * work around the fact that the buffer cache has a block size of 1024,
587 * and we have 2048 byte sectors. This code should work for buffers that
588 * are any multiple of 512 bytes long.
591 SCpnt->use_sg = 0;
593 if (SCpnt->host->sg_tablesize > 0 &&
594 (!scsi_need_isa_buffer ||
595 scsi_dma_free_sectors >= 10)) {
596 struct buffer_head *bh;
597 struct scatterlist *sgpnt;
598 int count, this_count_max;
599 bh = SCpnt->request.bh;
600 this_count = 0;
601 count = 0;
602 this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
603 /* Calculate how many links we can use. First see if we need
604 * a padding record at the start */
605 this_count = SCpnt->request.sector % 4;
606 if (this_count)
607 count++;
608 while (bh && count < SCpnt->host->sg_tablesize) {
609 if ((this_count + (bh->b_size >> 9)) > this_count_max)
610 break;
611 this_count += (bh->b_size >> 9);
612 count++;
613 bh = bh->b_reqnext;
615 /* Fix up in case of an odd record at the end */
616 end_rec = 0;
617 if (this_count % 4) {
618 if (count < SCpnt->host->sg_tablesize) {
619 count++;
620 end_rec = (4 - (this_count % 4)) << 9;
621 this_count += 4 - (this_count % 4);
622 } else {
623 count--;
624 this_count -= (this_count % 4);
627 SCpnt->use_sg = count; /* Number of chains */
628 /* scsi_malloc can only allocate in chunks of 512 bytes */
629 count = (SCpnt->use_sg * sizeof(struct scatterlist) + 511) & ~511;
631 SCpnt->sglist_len = count;
632 sgpnt = (struct scatterlist *) scsi_malloc(count);
633 if (!sgpnt) {
634 printk("Warning - running *really* short on DMA buffers\n");
635 SCpnt->use_sg = 0; /* No memory left - bail out */
636 } else {
637 buffer = (unsigned char *) sgpnt;
638 count = 0;
639 bh = SCpnt->request.bh;
640 if (SCpnt->request.sector % 4) {
641 sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
642 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
643 if (!sgpnt[count].address)
644 panic("SCSI DMA pool exhausted.");
645 sgpnt[count].alt_address = sgpnt[count].address; /* Flag to delete
646 if needed */
647 count++;
649 for (bh = SCpnt->request.bh; count < SCpnt->use_sg;
650 count++, bh = bh->b_reqnext) {
651 if (bh) { /* Need a placeholder at the end of the record? */
652 sgpnt[count].address = bh->b_data;
653 sgpnt[count].length = bh->b_size;
654 sgpnt[count].alt_address = NULL;
655 } else {
656 sgpnt[count].address = (char *) scsi_malloc(end_rec);
657 if (!sgpnt[count].address)
658 panic("SCSI DMA pool exhausted.");
659 sgpnt[count].length = end_rec;
660 sgpnt[count].alt_address = sgpnt[count].address;
661 if (count + 1 != SCpnt->use_sg)
662 panic("Bad sr request list");
663 break;
665 if (virt_to_phys(sgpnt[count].address) + sgpnt[count].length - 1 >
666 ISA_DMA_THRESHOLD && SCpnt->host->unchecked_isa_dma) {
667 sgpnt[count].alt_address = sgpnt[count].address;
668 /* We try to avoid exhausting the DMA pool, since it is easier
669 * to control usage here. In other places we might have a more
670 * pressing need, and we would be screwed if we ran out */
671 if (scsi_dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
672 sgpnt[count].address = NULL;
673 } else {
674 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
676 /* If we start running low on DMA buffers, we abort the scatter-gather
677 * operation, and free all of the memory we have allocated. We want to
678 * ensure that all scsi operations are able to do at least a non-scatter/gather
679 * operation */
680 if (sgpnt[count].address == NULL) { /* Out of dma memory */
681 printk("Warning: Running low on SCSI DMA buffers\n");
682 /* Try switching back to a non scatter-gather operation. */
683 while (--count >= 0) {
684 if (sgpnt[count].alt_address)
685 scsi_free(sgpnt[count].address, sgpnt[count].length);
687 SCpnt->use_sg = 0;
688 scsi_free(buffer, SCpnt->sglist_len);
689 break;
690 }; /* if address == NULL */
691 }; /* if need DMA fixup */
692 }; /* for loop to fill list */
693 #ifdef DEBUG
694 printk("SR: %d %d %d %d %d *** ", SCpnt->use_sg, SCpnt->request.sector,
695 this_count,
696 SCpnt->request.current_nr_sectors,
697 SCpnt->request.nr_sectors);
698 for (count = 0; count < SCpnt->use_sg; count++)
699 printk("SGlist: %d %x %x %x\n", count,
700 sgpnt[count].address,
701 sgpnt[count].alt_address,
702 sgpnt[count].length);
703 #endif
704 }; /* Able to allocate scatter-gather list */
707 if (SCpnt->use_sg == 0) {
708 /* We cannot use scatter-gather. Do this the old fashion way */
709 if (!SCpnt->request.bh)
710 this_count = SCpnt->request.nr_sectors;
711 else
712 this_count = (SCpnt->request.bh->b_size >> 9);
714 start = block % 4;
715 if (start) {
716 this_count = ((this_count > 4 - start) ?
717 (4 - start) : (this_count));
718 buffer = (unsigned char *) scsi_malloc(2048);
719 } else if (this_count < 4) {
720 buffer = (unsigned char *) scsi_malloc(2048);
721 } else {
722 this_count -= this_count % 4;
723 buffer = (unsigned char *) SCpnt->request.buffer;
724 if (virt_to_phys(buffer) + (this_count << 9) > ISA_DMA_THRESHOLD &&
725 SCpnt->host->unchecked_isa_dma)
726 buffer = (unsigned char *) scsi_malloc(this_count << 9);
730 if (scsi_CDs[dev].sector_size == 2048)
731 block = block >> 2; /* These are the sectors that the cdrom uses */
732 else
733 block = block & 0xfffffffc;
735 realcount = (this_count + 3) / 4;
737 if (scsi_CDs[dev].sector_size == 512)
738 realcount = realcount << 2;
741 * Note: The scsi standard says that READ_6 is *optional*, while
742 * READ_10 is mandatory. Thus there is no point in using
743 * READ_6.
745 if (scsi_CDs[dev].ten) {
746 if (realcount > 0xffff) {
747 realcount = 0xffff;
748 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
750 cmd[0] += READ_10 - READ_6;
751 cmd[2] = (unsigned char) (block >> 24) & 0xff;
752 cmd[3] = (unsigned char) (block >> 16) & 0xff;
753 cmd[4] = (unsigned char) (block >> 8) & 0xff;
754 cmd[5] = (unsigned char) block & 0xff;
755 cmd[6] = cmd[9] = 0;
756 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
757 cmd[8] = (unsigned char) realcount & 0xff;
758 } else {
759 if (realcount > 0xff) {
760 realcount = 0xff;
761 this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
763 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
764 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
765 cmd[3] = (unsigned char) block & 0xff;
766 cmd[4] = (unsigned char) realcount;
767 cmd[5] = 0;
770 #ifdef DEBUG
772 int i;
773 printk("ReadCD: %d %d %d %d\n", block, realcount, buffer, this_count);
774 printk("Use sg: %d\n", SCpnt->use_sg);
775 printk("Dumping command: ");
776 for (i = 0; i < 12; i++)
777 printk("%2.2x ", cmd[i]);
778 printk("\n");
780 #endif
782 /* Some dumb host adapters can speed transfers by knowing the
783 * minimum transfersize in advance.
785 * We shouldn't disconnect in the middle of a sector, but the cdrom
786 * sector size can be larger than the size of a buffer and the
787 * transfer may be split to the size of a buffer. So it's safe to
788 * assume that we can at least transfer the minimum of the buffer
789 * size (1024) and the sector size between each connect / disconnect.
792 SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
793 1024 : scsi_CDs[dev].sector_size;
795 SCpnt->this_count = this_count;
796 scsi_do_cmd(SCpnt, (void *) cmd, buffer,
797 realcount * scsi_CDs[dev].sector_size,
798 rw_intr, SR_TIMEOUT, MAX_RETRIES);
801 static int sr_detect(Scsi_Device * SDp)
804 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
805 return 0;
807 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
808 sr_template.dev_noticed++,
809 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
811 return 1;
814 static int sr_attach(Scsi_Device * SDp)
816 Scsi_CD *cpnt;
817 int i;
819 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
820 return 1;
822 if (sr_template.nr_dev >= sr_template.dev_max) {
823 SDp->attached--;
824 return 1;
826 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
827 if (!cpnt->device)
828 break;
830 if (i >= sr_template.dev_max)
831 panic("scsi_devices corrupt (sr)");
833 SDp->scsi_request_fn = do_sr_request;
834 scsi_CDs[i].device = SDp;
836 sr_template.nr_dev++;
837 if (sr_template.nr_dev > sr_template.dev_max)
838 panic("scsi_devices corrupt (sr)");
839 return 0;
843 static void sr_init_done(Scsi_Cmnd * SCpnt)
845 struct request *req;
847 req = &SCpnt->request;
848 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
850 if (req->sem != NULL) {
851 up(req->sem);
855 void get_sectorsize(int i)
857 unsigned char cmd[10];
858 unsigned char *buffer;
859 int the_result, retries;
860 Scsi_Cmnd *SCpnt;
861 unsigned long flags;
863 buffer = (unsigned char *) scsi_malloc(512);
864 SCpnt = scsi_allocate_device(NULL, scsi_CDs[i].device, 1);
866 retries = 3;
867 do {
868 cmd[0] = READ_CAPACITY;
869 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
870 memset((void *) &cmd[2], 0, 8);
871 SCpnt->request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
872 SCpnt->cmd_len = 0;
874 memset(buffer, 0, 8);
876 /* Do the command and wait.. */
878 DECLARE_MUTEX_LOCKED(sem);
879 SCpnt->request.sem = &sem;
880 spin_lock_irqsave(&io_request_lock, flags);
881 scsi_do_cmd(SCpnt,
882 (void *) cmd, (void *) buffer,
883 512, sr_init_done, SR_TIMEOUT,
884 MAX_RETRIES);
885 spin_unlock_irqrestore(&io_request_lock, flags);
886 down(&sem);
889 the_result = SCpnt->result;
890 retries--;
892 } while (the_result && retries);
895 wake_up(&SCpnt->device->device_wait);
896 scsi_release_command(SCpnt);
897 SCpnt = NULL;
899 if (the_result) {
900 scsi_CDs[i].capacity = 0x1fffff;
901 scsi_CDs[i].sector_size = 2048; /* A guess, just in case */
902 scsi_CDs[i].needs_sector_size = 1;
903 } else {
904 #if 0
905 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
906 (long *) &scsi_CDs[i].capacity))
907 #endif
908 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
909 (buffer[1] << 16) |
910 (buffer[2] << 8) |
911 buffer[3]);
912 scsi_CDs[i].sector_size = (buffer[4] << 24) |
913 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
914 switch (scsi_CDs[i].sector_size) {
916 * HP 4020i CD-Recorder reports 2340 byte sectors
917 * Philips CD-Writers report 2352 byte sectors
919 * Use 2k sectors for them..
921 case 0:
922 case 2340:
923 case 2352:
924 scsi_CDs[i].sector_size = 2048;
925 /* fall through */
926 case 2048:
927 scsi_CDs[i].capacity *= 4;
928 /* fall through */
929 case 512:
930 break;
931 default:
932 printk("sr%d: unsupported sector size %d.\n",
933 i, scsi_CDs[i].sector_size);
934 scsi_CDs[i].capacity = 0;
935 scsi_CDs[i].needs_sector_size = 1;
939 * Add this so that we have the ability to correctly gauge
940 * what the device is capable of.
942 scsi_CDs[i].needs_sector_size = 0;
943 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
945 scsi_free(buffer, 512);
948 void get_capabilities(int i)
950 unsigned char cmd[6];
951 unsigned char *buffer;
952 int rc, n;
954 static char *loadmech[] =
956 "caddy",
957 "tray",
958 "pop-up",
960 "changer",
961 "changer",
966 buffer = (unsigned char *) scsi_malloc(512);
967 cmd[0] = MODE_SENSE;
968 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
969 cmd[2] = 0x2a;
970 cmd[4] = 128;
971 cmd[3] = cmd[5] = 0;
972 rc = sr_do_ioctl(i, cmd, buffer, 128, 1);
974 if (-EINVAL == rc) {
975 /* failed, drive has'nt this mode page */
976 scsi_CDs[i].cdi.speed = 1;
977 /* disable speed select, drive probably can't do this either */
978 scsi_CDs[i].cdi.mask |= CDC_SELECT_SPEED;
979 scsi_free(buffer, 512);
980 return;
982 n = buffer[3] + 4;
983 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
984 scsi_CDs[i].readcd_known = 1;
985 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
986 /* print some capability bits */
987 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s\n", i,
988 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
989 scsi_CDs[i].cdi.speed,
990 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
991 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
992 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
993 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
994 loadmech[buffer[n + 6] >> 5]);
995 if ((buffer[n + 6] >> 5) == 0)
996 /* caddy drives can't close tray... */
997 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
998 if ((buffer[n + 2] & 0x8) == 0)
999 /* not a DVD drive */
1000 scsi_CDs[i].cdi.mask |= CDC_DVD;
1001 if ((buffer[n + 3] & 0x20) == 0)
1002 /* can't write DVD-RAM media */
1003 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
1004 if ((buffer[n + 3] & 0x10) == 0)
1005 /* can't write DVD-R media */
1006 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
1007 if ((buffer[n + 3] & 0x2) == 0)
1008 /* can't write CD-RW media */
1009 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
1010 if ((buffer[n + 3] & 0x1) == 0)
1011 /* can't write CD-R media */
1012 scsi_CDs[i].cdi.mask |= CDC_CD_R;
1014 scsi_free(buffer, 512);
1018 * sr_packet() is the entry point for the generic commands generated
1019 * by the Uniform CD-ROM layer.
1021 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
1023 Scsi_Cmnd *SCpnt;
1024 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
1025 DECLARE_MUTEX_LOCKED(sem);
1026 unsigned long flags;
1027 int stat;
1029 /* get the device */
1030 SCpnt = scsi_allocate_device(NULL, device, 1);
1031 if (SCpnt == NULL)
1032 return -ENODEV; /* this just doesn't seem right /axboe */
1034 /* set the LUN */
1035 cgc->cmd[1] |= device->lun << 5;
1037 /* do the locking and issue the command */
1038 SCpnt->request.rq_dev = cdi->dev;
1039 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1040 /* scsi_do_cmd sets the command length */
1041 SCpnt->cmd_len = 0;
1042 SCpnt->request.sem = &sem;
1043 spin_lock_irqsave(&io_request_lock, flags);
1044 scsi_do_cmd(SCpnt, (void *) cgc->cmd, (void *) cgc->buffer, cgc->buflen,
1045 sr_init_done, SR_TIMEOUT, MAX_RETRIES);
1046 spin_unlock_irqrestore(&io_request_lock, flags);
1047 down(&sem);
1049 stat = SCpnt->result;
1051 /* release */
1052 SCpnt->request.rq_dev = MKDEV(0, 0);
1053 scsi_release_command(SCpnt);
1054 SCpnt = NULL;
1056 return stat;
1059 static int sr_registered = 0;
1061 static int sr_init()
1063 int i;
1065 if (sr_template.dev_noticed == 0)
1066 return 0;
1068 if (!sr_registered) {
1069 if (register_blkdev(MAJOR_NR, "sr", &cdrom_fops)) {
1070 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
1071 return 1;
1073 sr_registered++;
1075 if (scsi_CDs)
1076 return 0;
1077 sr_template.dev_max =
1078 sr_template.dev_noticed + SR_EXTRA_DEVS;
1079 scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
1080 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
1082 sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
1083 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
1085 sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max *
1086 sizeof(int), GFP_ATOMIC);
1089 * These are good guesses for the time being.
1091 for (i = 0; i < sr_template.dev_max; i++)
1092 sr_blocksizes[i] = 2048;
1093 blksize_size[MAJOR_NR] = sr_blocksizes;
1094 return 0;
1097 void sr_finish()
1099 int i;
1100 char name[6];
1102 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1103 blk_size[MAJOR_NR] = sr_sizes;
1105 for (i = 0; i < sr_template.nr_dev; ++i) {
1106 /* If we have already seen this, then skip it. Comes up
1107 * with loadable modules. */
1108 if (scsi_CDs[i].capacity)
1109 continue;
1110 scsi_CDs[i].capacity = 0x1fffff;
1111 scsi_CDs[i].sector_size = 2048; /* A guess, just in case */
1112 scsi_CDs[i].needs_sector_size = 1;
1113 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
1114 #if 0
1115 /* seems better to leave this for later */
1116 get_sectorsize(i);
1117 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
1118 #endif
1119 scsi_CDs[i].use = 1;
1120 scsi_CDs[i].ten = 1;
1121 scsi_CDs[i].remap = 1;
1122 scsi_CDs[i].readcd_known = 0;
1123 scsi_CDs[i].readcd_cdda = 0;
1124 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
1126 scsi_CDs[i].cdi.ops = &sr_dops;
1127 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
1128 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
1129 scsi_CDs[i].cdi.mask = 0;
1130 scsi_CDs[i].cdi.capacity = 1;
1131 get_capabilities(i);
1132 sr_vendor_init(i);
1134 sprintf(name, "sr%d", i);
1135 strcpy(scsi_CDs[i].cdi.name, name);
1136 register_cdrom(&scsi_CDs[i].cdi);
1140 /* If our host adapter is capable of scatter-gather, then we increase
1141 * the read-ahead to 16 blocks (32 sectors). If not, we use
1142 * a two block (4 sector) read ahead. */
1143 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
1144 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
1145 else
1146 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
1148 return;
1151 static void sr_detach(Scsi_Device * SDp)
1153 Scsi_CD *cpnt;
1154 int i;
1156 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
1157 if (cpnt->device == SDp) {
1158 kdev_t devi = MKDEV(MAJOR_NR, i);
1159 struct super_block *sb = get_super(devi);
1162 * Since the cdrom is read-only, no need to sync the device.
1163 * We should be kind to our buffer cache, however.
1165 if (sb)
1166 invalidate_inodes(sb);
1167 invalidate_buffers(devi);
1170 * Reset things back to a sane state so that one can re-load a new
1171 * driver (perhaps the same one).
1173 unregister_cdrom(&(cpnt->cdi));
1174 cpnt->device = NULL;
1175 cpnt->capacity = 0;
1176 SDp->attached--;
1177 sr_template.nr_dev--;
1178 sr_template.dev_noticed--;
1179 sr_sizes[i] = 0;
1180 return;
1182 return;
1186 #ifdef MODULE
1188 int init_module(void)
1190 sr_template.module = &__this_module;
1191 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
1194 void cleanup_module(void)
1196 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
1197 unregister_blkdev(MAJOR_NR, "sr");
1198 sr_registered--;
1199 if (scsi_CDs != NULL) {
1200 scsi_init_free((char *) scsi_CDs,
1201 (sr_template.dev_noticed + SR_EXTRA_DEVS)
1202 * sizeof(Scsi_CD));
1204 scsi_init_free((char *) sr_sizes, sr_template.dev_max * sizeof(int));
1205 sr_sizes = NULL;
1207 scsi_init_free((char *) sr_blocksizes, sr_template.dev_max * sizeof(int));
1208 sr_blocksizes = NULL;
1210 blksize_size[MAJOR_NR] = NULL;
1211 blk_dev[MAJOR_NR].request_fn = NULL;
1212 blk_size[MAJOR_NR] = NULL;
1213 read_ahead[MAJOR_NR] = 0;
1215 sr_template.dev_max = 0;
1218 #endif /* MODULE */
1221 * Overrides for Emacs so that we follow Linus's tabbing style.
1222 * Emacs will notice this stuff at the end of the file and automatically
1223 * adjust the settings for this buffer only. This must remain at the end
1224 * of the file.
1225 * ---------------------------------------------------------------------------
1226 * Local variables:
1227 * c-indent-level: 4
1228 * c-brace-imaginary-offset: 0
1229 * c-brace-offset: -4
1230 * c-argdecl-indent: 4
1231 * c-label-offset: -4
1232 * c-continued-statement-offset: 4
1233 * c-continued-brace-offset: 0
1234 * indent-tabs-mode: nil
1235 * tab-width: 8
1236 * End: