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 ide_scsi_hex_dump(u8 *data, int len)
247 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
250 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
252 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
257 /* stuff a sense request in front of our current request */
258 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
259 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
260 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
261 if (!pc || !rq || !buf) {
267 ide_init_drive_cmd(rq);
268 rq->special = (char *) pc;
271 pc->c[0] = REQUEST_SENSE;
272 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
273 rq->cmd_type = REQ_TYPE_SENSE;
274 pc->timeout = jiffies + WAIT_READY;
275 /* NOTE! Save the failed packet command in "rq->buffer" */
276 rq->buffer = (void *) failed_command->special;
277 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
278 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
279 printk ("ide-scsi: %s: queue cmd = ", drive->name);
280 ide_scsi_hex_dump(pc->c, 6);
282 rq->rq_disk = scsi->disk;
283 return ide_do_drive_cmd(drive, rq, ide_preempt);
286 static int idescsi_end_request(ide_drive_t *, int, int);
288 static ide_startstop_t
289 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
291 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
293 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
297 idescsi_end_request(drive, 0, 0);
302 static ide_startstop_t
303 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
305 #if IDESCSI_DEBUG_LOG
306 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
307 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
309 rq->errors |= ERROR_MAX;
311 idescsi_end_request(drive, 0, 0);
316 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
318 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
319 struct request *rq = HWGROUP(drive)->rq;
320 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
321 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
322 struct Scsi_Host *host;
323 int errors = rq->errors;
326 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
327 ide_end_request(drive, uptodate, nrsecs);
330 ide_end_drive_cmd (drive, 0, 0);
331 if (blk_sense_request(rq)) {
332 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
334 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
335 ide_scsi_hex_dump(pc->buffer, 16);
337 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
343 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
344 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
345 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
347 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
348 drive->name, pc->scsi_cmd->serial_number);
349 pc->scsi_cmd->result = DID_TIME_OUT << 16;
350 } else if (errors >= ERROR_MAX) {
351 pc->scsi_cmd->result = DID_ERROR << 16;
353 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
356 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
357 if (!idescsi_check_condition(drive, rq))
358 /* we started a request sense, so we'll be back, exit for now */
360 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
362 pc->scsi_cmd->result = DID_OK << 16;
364 host = pc->scsi_cmd->device->host;
365 spin_lock_irqsave(host->host_lock, flags);
366 pc->done(pc->scsi_cmd);
367 spin_unlock_irqrestore(host->host_lock, flags);
374 static inline unsigned long get_timeout(idescsi_pc_t *pc)
376 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
379 static int idescsi_expiry(ide_drive_t *drive)
381 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
382 idescsi_pc_t *pc = scsi->pc;
384 #if IDESCSI_DEBUG_LOG
385 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
387 set_bit(PC_TIMEDOUT, &pc->flags);
389 return 0; /* we do not want the ide subsystem to retry */
393 * Our interrupt handler.
395 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
397 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
398 ide_hwif_t *hwif = drive->hwif;
399 idescsi_pc_t *pc = scsi->pc;
400 struct request *rq = pc->rq;
401 atapi_ireason_t ireason;
406 #if IDESCSI_DEBUG_LOG
407 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
408 #endif /* IDESCSI_DEBUG_LOG */
410 if (test_bit(PC_TIMEDOUT, &pc->flags)){
411 #if IDESCSI_DEBUG_LOG
412 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
413 pc->scsi_cmd->serial_number, jiffies);
415 /* end this request now - scsi should retry it*/
416 idescsi_end_request (drive, 1, 0);
419 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
420 #if IDESCSI_DEBUG_LOG
421 printk ("ide-scsi: %s: DMA complete\n", drive->name);
422 #endif /* IDESCSI_DEBUG_LOG */
423 pc->actually_transferred=pc->request_transfer;
424 (void) HWIF(drive)->ide_dma_end(drive);
427 /* Clear the interrupt */
428 stat = drive->hwif->INB(IDE_STATUS_REG);
430 if ((stat & DRQ_STAT) == 0) {
431 /* No more interrupts */
432 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
433 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
434 local_irq_enable_in_hardirq();
437 idescsi_end_request (drive, 1, 0);
440 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
441 hwif->INB(IDE_BCOUNTL_REG);
442 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
445 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
446 return ide_do_reset (drive);
449 temp = pc->actually_transferred + bcount;
450 if (temp > pc->request_transfer) {
451 if (temp > pc->buffer_size) {
452 printk(KERN_ERR "ide-scsi: The scsi wants to "
453 "send us more data than expected "
454 "- discarding data\n");
455 temp = pc->buffer_size - pc->actually_transferred;
457 clear_bit(PC_WRITING, &pc->flags);
459 idescsi_input_buffers(drive, pc, temp);
461 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
462 printk(KERN_ERR "ide-scsi: transferred"
466 pc->actually_transferred += temp;
467 pc->current_position += temp;
468 idescsi_discard_data(drive, bcount - temp);
469 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
472 #if IDESCSI_DEBUG_LOG
473 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
474 #endif /* IDESCSI_DEBUG_LOG */
478 clear_bit(PC_WRITING, &pc->flags);
480 idescsi_input_buffers(drive, pc, bcount);
482 hwif->atapi_input_bytes(drive, pc->current_position,
485 set_bit(PC_WRITING, &pc->flags);
487 idescsi_output_buffers(drive, pc, bcount);
489 hwif->atapi_output_bytes(drive, pc->current_position,
492 /* Update the current position */
493 pc->actually_transferred += bcount;
494 pc->current_position += bcount;
496 /* And set the interrupt handler again */
497 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
501 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
503 ide_hwif_t *hwif = drive->hwif;
504 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
505 idescsi_pc_t *pc = scsi->pc;
506 atapi_ireason_t ireason;
507 ide_startstop_t startstop;
509 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
510 printk(KERN_ERR "ide-scsi: Strange, packet command "
511 "initiated yet DRQ isn't asserted\n");
514 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
515 if (!ireason.b.cod || ireason.b.io) {
516 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
517 "issuing a packet command\n");
518 return ide_do_reset (drive);
520 BUG_ON(HWGROUP(drive)->handler != NULL);
521 /* Set the interrupt routine */
522 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
523 /* Send the actual packet */
524 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
525 if (test_bit (PC_DMA_OK, &pc->flags)) {
526 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
527 hwif->dma_start(drive);
532 static inline int idescsi_set_direction(idescsi_pc_t *pc)
535 case READ_6: case READ_10: case READ_12:
536 clear_bit(PC_WRITING, &pc->flags);
538 case WRITE_6: case WRITE_10: case WRITE_12:
539 set_bit(PC_WRITING, &pc->flags);
546 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
548 ide_hwif_t *hwif = drive->hwif;
549 struct scatterlist *sg, *scsi_sg;
552 if (!pc->request_transfer || pc->request_transfer % 1024)
555 if (idescsi_set_direction(pc))
559 scsi_sg = scsi_sglist(pc->scsi_cmd);
560 segments = scsi_sg_count(pc->scsi_cmd);
562 if (segments > hwif->sg_max_nents)
565 hwif->sg_nents = segments;
566 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
572 * Issue a packet command
574 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
576 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
577 ide_hwif_t *hwif = drive->hwif;
581 scsi->pc=pc; /* Set the current packet command */
582 pc->actually_transferred=0; /* We haven't transferred any data yet */
583 pc->current_position=pc->buffer;
584 /* Request to transfer the entire buffer at once */
585 bcount = min(pc->request_transfer, 63 * 1024);
587 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
589 dma = !hwif->dma_setup(drive);
595 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
597 hwif->OUTB(dma, IDE_FEATURE_REG);
598 hwif->OUTB((bcount >> 8) & 0xff, IDE_BCOUNTH_REG);
599 hwif->OUTB(bcount & 0xff, IDE_BCOUNTL_REG);
602 set_bit(PC_DMA_OK, &pc->flags);
604 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
605 BUG_ON(HWGROUP(drive)->handler != NULL);
606 ide_set_handler(drive, &idescsi_transfer_pc,
607 get_timeout(pc), idescsi_expiry);
608 /* Issue the packet command */
609 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
612 /* Issue the packet command */
613 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
614 return idescsi_transfer_pc(drive);
619 * idescsi_do_request is our request handling function.
621 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
623 #if IDESCSI_DEBUG_LOG
624 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
625 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
626 #endif /* IDESCSI_DEBUG_LOG */
628 if (blk_sense_request(rq) || blk_special_request(rq)) {
629 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
631 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
632 idescsi_end_request (drive, 0, 0);
636 #ifdef CONFIG_IDE_PROC_FS
637 static void idescsi_add_settings(ide_drive_t *drive)
639 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
642 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
644 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
645 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
646 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
647 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
648 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
651 static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
655 * Driver initialization.
657 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
659 if (drive->id && (drive->id->config & 0x0060) == 0x20)
660 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
661 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
662 #if IDESCSI_DEBUG_LOG
663 set_bit(IDESCSI_LOG_CMD, &scsi->log);
664 #endif /* IDESCSI_DEBUG_LOG */
665 idescsi_add_settings(drive);
668 static void ide_scsi_remove(ide_drive_t *drive)
670 struct Scsi_Host *scsihost = drive->driver_data;
671 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
672 struct gendisk *g = scsi->disk;
674 scsi_remove_host(scsihost);
675 ide_proc_unregister_driver(drive, scsi->driver);
677 ide_unregister_region(g);
679 drive->driver_data = NULL;
680 g->private_data = NULL;
686 static int ide_scsi_probe(ide_drive_t *);
688 #ifdef CONFIG_IDE_PROC_FS
689 static ide_proc_entry_t idescsi_proc[] = {
690 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
691 { NULL, 0, NULL, NULL }
695 static ide_driver_t idescsi_driver = {
697 .owner = THIS_MODULE,
699 .bus = &ide_bus_type,
701 .probe = ide_scsi_probe,
702 .remove = ide_scsi_remove,
703 .version = IDESCSI_VERSION,
705 .supports_dsc_overlap = 0,
706 .do_request = idescsi_do_request,
707 .end_request = idescsi_end_request,
708 .error = idescsi_atapi_error,
709 .abort = idescsi_atapi_abort,
710 #ifdef CONFIG_IDE_PROC_FS
711 .proc = idescsi_proc,
715 static int idescsi_ide_open(struct inode *inode, struct file *filp)
717 struct gendisk *disk = inode->i_bdev->bd_disk;
718 struct ide_scsi_obj *scsi;
720 if (!(scsi = ide_scsi_get(disk)))
726 static int idescsi_ide_release(struct inode *inode, struct file *filp)
728 struct gendisk *disk = inode->i_bdev->bd_disk;
729 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
736 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
737 unsigned int cmd, unsigned long arg)
739 struct block_device *bdev = inode->i_bdev;
740 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
741 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
744 static struct block_device_operations idescsi_ops = {
745 .owner = THIS_MODULE,
746 .open = idescsi_ide_open,
747 .release = idescsi_ide_release,
748 .ioctl = idescsi_ide_ioctl,
751 static int idescsi_slave_configure(struct scsi_device * sdp)
753 /* Configure detected device */
754 sdp->use_10_for_rw = 1;
755 sdp->use_10_for_ms = 1;
756 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
760 static const char *idescsi_info (struct Scsi_Host *host)
762 return "SCSI host adapter emulation for IDE ATAPI devices";
765 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
767 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
769 if (cmd == SG_SET_TRANSFORM) {
771 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
773 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
775 } else if (cmd == SG_GET_TRANSFORM)
776 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
780 static int idescsi_queue (struct scsi_cmnd *cmd,
781 void (*done)(struct scsi_cmnd *))
783 struct Scsi_Host *host = cmd->device->host;
784 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
785 ide_drive_t *drive = scsi->drive;
786 struct request *rq = NULL;
787 idescsi_pc_t *pc = NULL;
790 scmd_printk (KERN_ERR, cmd, "drive not present\n");
793 scsi = drive_to_idescsi(drive);
794 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
795 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
796 if (rq == NULL || pc == NULL) {
797 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
801 memset (pc->c, 0, 12);
804 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
806 pc->sg = scsi_sglist(cmd);
807 pc->sg_cnt = scsi_sg_count(cmd);
809 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
812 pc->timeout = jiffies + cmd->timeout_per_command;
814 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
815 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
816 ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
817 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
818 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
819 ide_scsi_hex_dump(pc->c, 12);
823 ide_init_drive_cmd (rq);
824 rq->special = (char *) pc;
825 rq->cmd_type = REQ_TYPE_SPECIAL;
826 spin_unlock_irq(host->host_lock);
827 rq->rq_disk = scsi->disk;
828 (void) ide_do_drive_cmd (drive, rq, ide_end);
829 spin_lock_irq(host->host_lock);
834 cmd->result = DID_ERROR << 16;
839 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
841 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
842 ide_drive_t *drive = scsi->drive;
846 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
848 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
849 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
852 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
857 /* First give it some more time, how much is "right" is hard to say :-( */
859 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
860 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
861 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
863 spin_lock_irq(&ide_lock);
865 /* If there is no pc running we're done (our interrupt took care of it) */
871 /* It's somewhere in flight. Does ide subsystem agree? */
872 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
873 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
875 * FIXME - not sure this condition can ever occur
877 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
879 if (blk_sense_request(scsi->pc->rq))
880 kfree(scsi->pc->buffer);
889 spin_unlock_irq(&ide_lock);
891 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
892 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
897 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
900 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
901 ide_drive_t *drive = scsi->drive;
905 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
907 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
908 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
911 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
916 spin_lock_irq(cmd->device->host->host_lock);
917 spin_lock(&ide_lock);
919 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
920 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
921 spin_unlock(&ide_lock);
922 spin_unlock_irq(cmd->device->host->host_lock);
926 /* kill current request */
927 blkdev_dequeue_request(req);
928 end_that_request_last(req, 0);
929 if (blk_sense_request(req))
930 kfree(scsi->pc->buffer);
935 /* now nuke the drive queue */
936 while ((req = elv_next_request(drive->queue))) {
937 blkdev_dequeue_request(req);
938 end_that_request_last(req, 0);
941 HWGROUP(drive)->rq = NULL;
942 HWGROUP(drive)->handler = NULL;
943 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
944 spin_unlock(&ide_lock);
948 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
951 spin_unlock_irq(cmd->device->host->host_lock);
953 spin_lock_irq(cmd->device->host->host_lock);
954 } while ( HWGROUP(drive)->handler );
956 ready = drive_is_ready(drive);
957 HWGROUP(drive)->busy--;
959 printk (KERN_ERR "ide-scsi: reset failed!\n");
963 spin_unlock_irq(cmd->device->host->host_lock);
967 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
968 sector_t capacity, int *parm)
970 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
971 ide_drive_t *drive = idescsi->drive;
973 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
974 parm[0] = drive->bios_head;
975 parm[1] = drive->bios_sect;
976 parm[2] = drive->bios_cyl;
981 static struct scsi_host_template idescsi_template = {
982 .module = THIS_MODULE,
984 .info = idescsi_info,
985 .slave_configure = idescsi_slave_configure,
986 .ioctl = idescsi_ioctl,
987 .queuecommand = idescsi_queue,
988 .eh_abort_handler = idescsi_eh_abort,
989 .eh_host_reset_handler = idescsi_eh_reset,
990 .bios_param = idescsi_bios,
996 .use_clustering = DISABLE_CLUSTERING,
998 .proc_name = "ide-scsi",
1001 static int ide_scsi_probe(ide_drive_t *drive)
1003 idescsi_scsi_t *idescsi;
1004 struct Scsi_Host *host;
1009 if (!warned && drive->media == ide_cdrom) {
1010 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1014 if (idescsi_nocd && drive->media == ide_cdrom)
1017 if (!strstr("ide-scsi", drive->driver_req) ||
1019 drive->media == ide_disk ||
1020 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1023 g = alloc_disk(1 << PARTN_BITS);
1027 ide_init_disk(g, drive);
1031 #if IDESCSI_DEBUG_LOG
1032 if (drive->id->last_lun)
1033 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1035 if ((drive->id->last_lun & 0x7) != 7)
1036 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1040 drive->driver_data = host;
1041 idescsi = scsihost_to_idescsi(host);
1042 idescsi->drive = drive;
1043 idescsi->driver = &idescsi_driver;
1044 idescsi->host = host;
1046 g->private_data = &idescsi->driver;
1047 ide_proc_register_driver(drive, &idescsi_driver);
1049 idescsi_setup(drive, idescsi);
1050 g->fops = &idescsi_ops;
1051 ide_register_region(g);
1052 err = scsi_add_host(host, &drive->gendev);
1054 scsi_scan_host(host);
1057 /* fall through on error */
1058 ide_unregister_region(g);
1059 ide_proc_unregister_driver(drive, &idescsi_driver);
1063 scsi_host_put(host);
1067 static int __init init_idescsi_module(void)
1069 return driver_register(&idescsi_driver.gen_driver);
1072 static void __exit exit_idescsi_module(void)
1074 driver_unregister(&idescsi_driver.gen_driver);
1077 module_param(idescsi_nocd, int, 0600);
1078 MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
1079 module_init(init_idescsi_module);
1080 module_exit(exit_idescsi_module);
1081 MODULE_LICENSE("GPL");