2 * linux/drivers/scsi/ide-scsi.c Version 0.9 Jul 4, 1999
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
8 * Emulation of a SCSI host adapter for IDE ATAPI devices.
10 * With this driver, one can use the Linux SCSI drivers instead of the
11 * native IDE ATAPI drivers.
13 * Ver 0.1 Dec 3 96 Initial version.
14 * Ver 0.2 Jan 26 97 Fixed bug in cleanup_module() and added emulation
15 * of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
16 * to Janos Farkas for pointing this out.
17 * Avoid using bitfields in structures for m68k.
18 * Added Scatter/Gather and DMA support.
19 * Ver 0.4 Dec 7 97 Add support for ATAPI PD/CD drives.
20 * Use variable timeout for each command.
21 * Ver 0.5 Jan 2 98 Fix previous PD/CD support.
22 * Allow disabling of SCSI-6 to SCSI-10 transformation.
23 * Ver 0.6 Jan 27 98 Allow disabling of SCSI command translation layer
24 * for access through /dev/sg.
25 * Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
26 * Ver 0.7 Dec 04 98 Ignore commands where lun != 0 to avoid multiple
27 * detection of devices with CONFIG_SCSI_MULTI_LUN
28 * Ver 0.8 Feb 05 99 Optical media need translation too. Reverse 0.7.
29 * Ver 0.9 Jul 04 99 Fix a bug in SG_SET_TRANSFORM.
30 * Ver 0.91 Jun 10 02 Fix "off by one" error in transforms
31 * Ver 0.92 Dec 31 02 Implement new SCSI mid level API
34 #define IDESCSI_VERSION "0.92"
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/string.h>
39 #include <linux/kernel.h>
41 #include <linux/ioport.h>
42 #include <linux/blkdev.h>
43 #include <linux/errno.h>
44 #include <linux/hdreg.h>
45 #include <linux/slab.h>
46 #include <linux/ide.h>
47 #include <linux/scatterlist.h>
48 #include <linux/delay.h>
49 #include <linux/mutex.h>
50 #include <linux/bitops.h>
53 #include <asm/uaccess.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_tcq.h>
62 #define IDESCSI_DEBUG_LOG 0
64 typedef struct idescsi_pc_s {
65 u8 c[12]; /* Actual packet bytes */
66 int request_transfer; /* Bytes to transfer */
67 int actually_transferred; /* Bytes actually transferred */
68 int buffer_size; /* Size of our data buffer */
69 struct request *rq; /* The corresponding request */
70 u8 *buffer; /* Data buffer */
71 u8 *current_position; /* Pointer into the above buffer */
72 struct scatterlist *sg; /* Scatter gather table */
73 unsigned int sg_cnt; /* Number of entries in sg */
74 int b_count; /* Bytes transferred from current entry */
75 struct scsi_cmnd *scsi_cmd; /* SCSI command */
76 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
77 unsigned long flags; /* Status/Action flags */
78 unsigned long timeout; /* Command timeout */
82 * Packet command status bits.
84 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
85 #define PC_WRITING 1 /* Data direction */
86 #define PC_TIMEDOUT 3 /* command timed out */
87 #define PC_DMA_OK 4 /* Use DMA */
90 * SCSI command transformation layer
92 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
97 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
99 typedef struct ide_scsi_obj {
101 ide_driver_t *driver;
102 struct gendisk *disk;
103 struct Scsi_Host *host;
105 idescsi_pc_t *pc; /* Current packet command */
106 unsigned long flags; /* Status/Action flags */
107 unsigned long transform; /* SCSI cmd translation layer */
108 unsigned long log; /* log flags */
111 static DEFINE_MUTEX(idescsi_ref_mutex);
112 static int idescsi_nocd; /* Set by module param to skip cd */
114 #define ide_scsi_g(disk) \
115 container_of((disk)->private_data, struct ide_scsi_obj, driver)
117 static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
119 struct ide_scsi_obj *scsi = NULL;
121 mutex_lock(&idescsi_ref_mutex);
122 scsi = ide_scsi_g(disk);
124 scsi_host_get(scsi->host);
125 mutex_unlock(&idescsi_ref_mutex);
129 static void ide_scsi_put(struct ide_scsi_obj *scsi)
131 mutex_lock(&idescsi_ref_mutex);
132 scsi_host_put(scsi->host);
133 mutex_unlock(&idescsi_ref_mutex);
136 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
138 return (idescsi_scsi_t*) (&host[1]);
141 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
143 return scsihost_to_idescsi(ide_drive->driver_data);
147 * Per ATAPI device status bits.
149 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
154 #define IDESCSI_PC_RQ 90
156 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
159 (void) HWIF(drive)->INB(IDE_DATA_REG);
162 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
165 HWIF(drive)->OUTB(0, IDE_DATA_REG);
169 * PIO data transfer routines using the scatter gather table.
171 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
177 count = min(pc->sg->length - pc->b_count, bcount);
178 if (PageHighMem(sg_page(pc->sg))) {
181 local_irq_save(flags);
182 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
184 drive->hwif->atapi_input_bytes(drive,
185 buf + pc->b_count, count);
186 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
187 local_irq_restore(flags);
189 buf = sg_virt(pc->sg);
190 drive->hwif->atapi_input_bytes(drive,
191 buf + pc->b_count, count);
193 bcount -= count; pc->b_count += count;
194 if (pc->b_count == pc->sg->length) {
197 pc->sg = sg_next(pc->sg);
203 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
204 idescsi_discard_data (drive, bcount);
208 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
214 count = min(pc->sg->length - pc->b_count, bcount);
215 if (PageHighMem(sg_page(pc->sg))) {
218 local_irq_save(flags);
219 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
221 drive->hwif->atapi_output_bytes(drive,
222 buf + pc->b_count, count);
223 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
224 local_irq_restore(flags);
226 buf = sg_virt(pc->sg);
227 drive->hwif->atapi_output_bytes(drive,
228 buf + pc->b_count, count);
230 bcount -= count; pc->b_count += count;
231 if (pc->b_count == pc->sg->length) {
234 pc->sg = sg_next(pc->sg);
240 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
241 idescsi_output_zeros (drive, bcount);
245 static void hexdump(u8 *x, int len)
250 for (i = 0; i < len; i++)
255 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
257 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
262 /* stuff a sense request in front of our current request */
263 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
264 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
265 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
266 if (!pc || !rq || !buf) {
272 ide_init_drive_cmd(rq);
273 rq->special = (char *) pc;
276 pc->c[0] = REQUEST_SENSE;
277 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
278 rq->cmd_type = REQ_TYPE_SENSE;
279 pc->timeout = jiffies + WAIT_READY;
280 /* NOTE! Save the failed packet command in "rq->buffer" */
281 rq->buffer = (void *) failed_command->special;
282 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
283 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
284 printk ("ide-scsi: %s: queue cmd = ", drive->name);
287 rq->rq_disk = scsi->disk;
288 return ide_do_drive_cmd(drive, rq, ide_preempt);
291 static int idescsi_end_request(ide_drive_t *, int, int);
293 static ide_startstop_t
294 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
296 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
298 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
302 idescsi_end_request(drive, 0, 0);
307 static ide_startstop_t
308 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
310 #if IDESCSI_DEBUG_LOG
311 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
312 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
314 rq->errors |= ERROR_MAX;
316 idescsi_end_request(drive, 0, 0);
321 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
323 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
324 struct request *rq = HWGROUP(drive)->rq;
325 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
326 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
327 struct Scsi_Host *host;
328 int errors = rq->errors;
331 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
332 ide_end_request(drive, uptodate, nrsecs);
335 ide_end_drive_cmd (drive, 0, 0);
336 if (blk_sense_request(rq)) {
337 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
339 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
340 hexdump(pc->buffer,16);
342 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
348 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
349 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
350 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
352 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
353 drive->name, pc->scsi_cmd->serial_number);
354 pc->scsi_cmd->result = DID_TIME_OUT << 16;
355 } else if (errors >= ERROR_MAX) {
356 pc->scsi_cmd->result = DID_ERROR << 16;
358 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
361 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
362 if (!idescsi_check_condition(drive, rq))
363 /* we started a request sense, so we'll be back, exit for now */
365 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
367 pc->scsi_cmd->result = DID_OK << 16;
369 host = pc->scsi_cmd->device->host;
370 spin_lock_irqsave(host->host_lock, flags);
371 pc->done(pc->scsi_cmd);
372 spin_unlock_irqrestore(host->host_lock, flags);
379 static inline unsigned long get_timeout(idescsi_pc_t *pc)
381 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
384 static int idescsi_expiry(ide_drive_t *drive)
386 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
387 idescsi_pc_t *pc = scsi->pc;
389 #if IDESCSI_DEBUG_LOG
390 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
392 set_bit(PC_TIMEDOUT, &pc->flags);
394 return 0; /* we do not want the ide subsystem to retry */
398 * Our interrupt handler.
400 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
402 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
403 idescsi_pc_t *pc=scsi->pc;
404 struct request *rq = pc->rq;
405 atapi_bcount_t bcount;
406 atapi_status_t status;
407 atapi_ireason_t ireason;
408 atapi_feature_t feature;
412 #if IDESCSI_DEBUG_LOG
413 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
414 #endif /* IDESCSI_DEBUG_LOG */
416 if (test_bit(PC_TIMEDOUT, &pc->flags)){
417 #if IDESCSI_DEBUG_LOG
418 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
419 pc->scsi_cmd->serial_number, jiffies);
421 /* end this request now - scsi should retry it*/
422 idescsi_end_request (drive, 1, 0);
425 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
426 #if IDESCSI_DEBUG_LOG
427 printk ("ide-scsi: %s: DMA complete\n", drive->name);
428 #endif /* IDESCSI_DEBUG_LOG */
429 pc->actually_transferred=pc->request_transfer;
430 (void) HWIF(drive)->ide_dma_end(drive);
434 /* Clear the interrupt */
435 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
438 /* No more interrupts */
439 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
440 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
441 local_irq_enable_in_hardirq();
444 idescsi_end_request (drive, 1, 0);
447 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
448 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
449 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
452 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
453 return ide_do_reset (drive);
456 temp = pc->actually_transferred + bcount.all;
457 if (temp > pc->request_transfer) {
458 if (temp > pc->buffer_size) {
459 printk(KERN_ERR "ide-scsi: The scsi wants to "
460 "send us more data than expected "
461 "- discarding data\n");
462 temp = pc->buffer_size - pc->actually_transferred;
464 clear_bit(PC_WRITING, &pc->flags);
466 idescsi_input_buffers(drive, pc, temp);
468 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
469 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
471 pc->actually_transferred += temp;
472 pc->current_position += temp;
473 idescsi_discard_data(drive, bcount.all - temp);
474 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
477 #if IDESCSI_DEBUG_LOG
478 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
479 #endif /* IDESCSI_DEBUG_LOG */
483 clear_bit(PC_WRITING, &pc->flags);
485 idescsi_input_buffers(drive, pc, bcount.all);
487 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
489 set_bit(PC_WRITING, &pc->flags);
491 idescsi_output_buffers (drive, pc, bcount.all);
493 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
495 /* Update the current position */
496 pc->actually_transferred += bcount.all;
497 pc->current_position += bcount.all;
499 /* And set the interrupt handler again */
500 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
504 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
506 ide_hwif_t *hwif = drive->hwif;
507 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
508 idescsi_pc_t *pc = scsi->pc;
509 atapi_ireason_t ireason;
510 ide_startstop_t startstop;
512 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
513 printk(KERN_ERR "ide-scsi: Strange, packet command "
514 "initiated yet DRQ isn't asserted\n");
517 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
518 if (!ireason.b.cod || ireason.b.io) {
519 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
520 "issuing a packet command\n");
521 return ide_do_reset (drive);
523 BUG_ON(HWGROUP(drive)->handler != NULL);
524 /* Set the interrupt routine */
525 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
526 /* Send the actual packet */
527 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
528 if (test_bit (PC_DMA_OK, &pc->flags)) {
529 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
530 hwif->dma_start(drive);
535 static inline int idescsi_set_direction(idescsi_pc_t *pc)
538 case READ_6: case READ_10: case READ_12:
539 clear_bit(PC_WRITING, &pc->flags);
541 case WRITE_6: case WRITE_10: case WRITE_12:
542 set_bit(PC_WRITING, &pc->flags);
549 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
551 ide_hwif_t *hwif = drive->hwif;
552 struct scatterlist *sg, *scsi_sg;
555 if (!pc->request_transfer || pc->request_transfer % 1024)
558 if (idescsi_set_direction(pc))
562 scsi_sg = scsi_sglist(pc->scsi_cmd);
563 segments = scsi_sg_count(pc->scsi_cmd);
565 if (segments > hwif->sg_max_nents)
568 hwif->sg_nents = segments;
569 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
575 * Issue a packet command
577 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
579 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
580 ide_hwif_t *hwif = drive->hwif;
581 atapi_feature_t feature;
582 atapi_bcount_t bcount;
584 scsi->pc=pc; /* Set the current packet command */
585 pc->actually_transferred=0; /* We haven't transferred any data yet */
586 pc->current_position=pc->buffer;
587 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
590 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
592 feature.b.dma = !hwif->dma_setup(drive);
598 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
600 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
601 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
602 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
605 set_bit(PC_DMA_OK, &pc->flags);
607 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
608 BUG_ON(HWGROUP(drive)->handler != NULL);
609 ide_set_handler(drive, &idescsi_transfer_pc,
610 get_timeout(pc), idescsi_expiry);
611 /* Issue the packet command */
612 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
615 /* Issue the packet command */
616 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
617 return idescsi_transfer_pc(drive);
622 * idescsi_do_request is our request handling function.
624 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
626 #if IDESCSI_DEBUG_LOG
627 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
628 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
629 #endif /* IDESCSI_DEBUG_LOG */
631 if (blk_sense_request(rq) || blk_special_request(rq)) {
632 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
634 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
635 idescsi_end_request (drive, 0, 0);
639 #ifdef CONFIG_IDE_PROC_FS
640 static void idescsi_add_settings(ide_drive_t *drive)
642 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
645 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
647 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
648 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
649 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
650 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
651 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
654 static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
658 * Driver initialization.
660 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
662 if (drive->id && (drive->id->config & 0x0060) == 0x20)
663 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
664 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
665 #if IDESCSI_DEBUG_LOG
666 set_bit(IDESCSI_LOG_CMD, &scsi->log);
667 #endif /* IDESCSI_DEBUG_LOG */
668 idescsi_add_settings(drive);
671 static void ide_scsi_remove(ide_drive_t *drive)
673 struct Scsi_Host *scsihost = drive->driver_data;
674 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
675 struct gendisk *g = scsi->disk;
677 scsi_remove_host(scsihost);
678 ide_proc_unregister_driver(drive, scsi->driver);
680 ide_unregister_region(g);
682 drive->driver_data = NULL;
683 g->private_data = NULL;
689 static int ide_scsi_probe(ide_drive_t *);
691 #ifdef CONFIG_IDE_PROC_FS
692 static ide_proc_entry_t idescsi_proc[] = {
693 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
694 { NULL, 0, NULL, NULL }
698 static ide_driver_t idescsi_driver = {
700 .owner = THIS_MODULE,
702 .bus = &ide_bus_type,
704 .probe = ide_scsi_probe,
705 .remove = ide_scsi_remove,
706 .version = IDESCSI_VERSION,
708 .supports_dsc_overlap = 0,
709 .do_request = idescsi_do_request,
710 .end_request = idescsi_end_request,
711 .error = idescsi_atapi_error,
712 .abort = idescsi_atapi_abort,
713 #ifdef CONFIG_IDE_PROC_FS
714 .proc = idescsi_proc,
718 static int idescsi_ide_open(struct inode *inode, struct file *filp)
720 struct gendisk *disk = inode->i_bdev->bd_disk;
721 struct ide_scsi_obj *scsi;
723 if (!(scsi = ide_scsi_get(disk)))
729 static int idescsi_ide_release(struct inode *inode, struct file *filp)
731 struct gendisk *disk = inode->i_bdev->bd_disk;
732 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
739 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
740 unsigned int cmd, unsigned long arg)
742 struct block_device *bdev = inode->i_bdev;
743 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
744 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
747 static struct block_device_operations idescsi_ops = {
748 .owner = THIS_MODULE,
749 .open = idescsi_ide_open,
750 .release = idescsi_ide_release,
751 .ioctl = idescsi_ide_ioctl,
754 static int idescsi_slave_configure(struct scsi_device * sdp)
756 /* Configure detected device */
757 sdp->use_10_for_rw = 1;
758 sdp->use_10_for_ms = 1;
759 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
763 static const char *idescsi_info (struct Scsi_Host *host)
765 return "SCSI host adapter emulation for IDE ATAPI devices";
768 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
770 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
772 if (cmd == SG_SET_TRANSFORM) {
774 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
776 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
778 } else if (cmd == SG_GET_TRANSFORM)
779 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
783 static int idescsi_queue (struct scsi_cmnd *cmd,
784 void (*done)(struct scsi_cmnd *))
786 struct Scsi_Host *host = cmd->device->host;
787 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
788 ide_drive_t *drive = scsi->drive;
789 struct request *rq = NULL;
790 idescsi_pc_t *pc = NULL;
793 scmd_printk (KERN_ERR, cmd, "drive not present\n");
796 scsi = drive_to_idescsi(drive);
797 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
798 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
799 if (rq == NULL || pc == NULL) {
800 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
804 memset (pc->c, 0, 12);
807 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
809 pc->sg = scsi_sglist(cmd);
810 pc->sg_cnt = scsi_sg_count(cmd);
812 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
815 pc->timeout = jiffies + cmd->timeout_per_command;
817 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
818 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
819 hexdump(cmd->cmnd, cmd->cmd_len);
820 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
821 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
826 ide_init_drive_cmd (rq);
827 rq->special = (char *) pc;
828 rq->cmd_type = REQ_TYPE_SPECIAL;
829 spin_unlock_irq(host->host_lock);
830 rq->rq_disk = scsi->disk;
831 (void) ide_do_drive_cmd (drive, rq, ide_end);
832 spin_lock_irq(host->host_lock);
837 cmd->result = DID_ERROR << 16;
842 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
844 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
845 ide_drive_t *drive = scsi->drive;
849 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
851 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
852 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
855 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
860 /* First give it some more time, how much is "right" is hard to say :-( */
862 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
863 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
864 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
866 spin_lock_irq(&ide_lock);
868 /* If there is no pc running we're done (our interrupt took care of it) */
874 /* It's somewhere in flight. Does ide subsystem agree? */
875 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
876 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
878 * FIXME - not sure this condition can ever occur
880 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
882 if (blk_sense_request(scsi->pc->rq))
883 kfree(scsi->pc->buffer);
892 spin_unlock_irq(&ide_lock);
894 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
895 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
900 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
903 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
904 ide_drive_t *drive = scsi->drive;
908 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
910 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
911 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
914 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
919 spin_lock_irq(cmd->device->host->host_lock);
920 spin_lock(&ide_lock);
922 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
923 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
924 spin_unlock(&ide_lock);
925 spin_unlock_irq(cmd->device->host->host_lock);
929 /* kill current request */
930 blkdev_dequeue_request(req);
931 end_that_request_last(req, 0);
932 if (blk_sense_request(req))
933 kfree(scsi->pc->buffer);
938 /* now nuke the drive queue */
939 while ((req = elv_next_request(drive->queue))) {
940 blkdev_dequeue_request(req);
941 end_that_request_last(req, 0);
944 HWGROUP(drive)->rq = NULL;
945 HWGROUP(drive)->handler = NULL;
946 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
947 spin_unlock(&ide_lock);
951 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
954 spin_unlock_irq(cmd->device->host->host_lock);
956 spin_lock_irq(cmd->device->host->host_lock);
957 } while ( HWGROUP(drive)->handler );
959 ready = drive_is_ready(drive);
960 HWGROUP(drive)->busy--;
962 printk (KERN_ERR "ide-scsi: reset failed!\n");
966 spin_unlock_irq(cmd->device->host->host_lock);
970 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
971 sector_t capacity, int *parm)
973 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
974 ide_drive_t *drive = idescsi->drive;
976 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
977 parm[0] = drive->bios_head;
978 parm[1] = drive->bios_sect;
979 parm[2] = drive->bios_cyl;
984 static struct scsi_host_template idescsi_template = {
985 .module = THIS_MODULE,
987 .info = idescsi_info,
988 .slave_configure = idescsi_slave_configure,
989 .ioctl = idescsi_ioctl,
990 .queuecommand = idescsi_queue,
991 .eh_abort_handler = idescsi_eh_abort,
992 .eh_host_reset_handler = idescsi_eh_reset,
993 .bios_param = idescsi_bios,
999 .use_clustering = DISABLE_CLUSTERING,
1001 .proc_name = "ide-scsi",
1004 static int ide_scsi_probe(ide_drive_t *drive)
1006 idescsi_scsi_t *idescsi;
1007 struct Scsi_Host *host;
1012 if (!warned && drive->media == ide_cdrom) {
1013 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1017 if (idescsi_nocd && drive->media == ide_cdrom)
1020 if (!strstr("ide-scsi", drive->driver_req) ||
1022 drive->media == ide_disk ||
1023 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1026 g = alloc_disk(1 << PARTN_BITS);
1030 ide_init_disk(g, drive);
1034 #if IDESCSI_DEBUG_LOG
1035 if (drive->id->last_lun)
1036 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1038 if ((drive->id->last_lun & 0x7) != 7)
1039 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1043 drive->driver_data = host;
1044 idescsi = scsihost_to_idescsi(host);
1045 idescsi->drive = drive;
1046 idescsi->driver = &idescsi_driver;
1047 idescsi->host = host;
1049 g->private_data = &idescsi->driver;
1050 ide_proc_register_driver(drive, &idescsi_driver);
1052 idescsi_setup(drive, idescsi);
1053 g->fops = &idescsi_ops;
1054 ide_register_region(g);
1055 err = scsi_add_host(host, &drive->gendev);
1057 scsi_scan_host(host);
1060 /* fall through on error */
1061 ide_unregister_region(g);
1062 ide_proc_unregister_driver(drive, &idescsi_driver);
1066 scsi_host_put(host);
1070 static int __init init_idescsi_module(void)
1072 return driver_register(&idescsi_driver.gen_driver);
1075 static void __exit exit_idescsi_module(void)
1077 driver_unregister(&idescsi_driver.gen_driver);
1080 module_param(idescsi_nocd, int, 0600);
1081 MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
1082 module_init(init_idescsi_module);
1083 module_exit(exit_idescsi_module);
1084 MODULE_LICENSE("GPL");