2 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
3 * Copyright (C) 2004-2005 Bartlomiej Zolnierkiewicz
7 * Emulation of a SCSI host adapter for IDE ATAPI devices.
9 * With this driver, one can use the Linux SCSI drivers instead of the
10 * native IDE ATAPI drivers.
12 * Ver 0.1 Dec 3 96 Initial version.
13 * Ver 0.2 Jan 26 97 Fixed bug in cleanup_module() and added emulation
14 * of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
15 * to Janos Farkas for pointing this out.
16 * Avoid using bitfields in structures for m68k.
17 * Added Scatter/Gather and DMA support.
18 * Ver 0.4 Dec 7 97 Add support for ATAPI PD/CD drives.
19 * Use variable timeout for each command.
20 * Ver 0.5 Jan 2 98 Fix previous PD/CD support.
21 * Allow disabling of SCSI-6 to SCSI-10 transformation.
22 * Ver 0.6 Jan 27 98 Allow disabling of SCSI command translation layer
23 * for access through /dev/sg.
24 * Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
25 * Ver 0.7 Dec 04 98 Ignore commands where lun != 0 to avoid multiple
26 * detection of devices with CONFIG_SCSI_MULTI_LUN
27 * Ver 0.8 Feb 05 99 Optical media need translation too. Reverse 0.7.
28 * Ver 0.9 Jul 04 99 Fix a bug in SG_SET_TRANSFORM.
29 * Ver 0.91 Jun 10 02 Fix "off by one" error in transforms
30 * Ver 0.92 Dec 31 02 Implement new SCSI mid level API
33 #define IDESCSI_VERSION "0.92"
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
40 #include <linux/ioport.h>
41 #include <linux/blkdev.h>
42 #include <linux/errno.h>
43 #include <linux/hdreg.h>
44 #include <linux/slab.h>
45 #include <linux/ide.h>
46 #include <linux/scatterlist.h>
47 #include <linux/delay.h>
48 #include <linux/mutex.h>
49 #include <linux/bitops.h>
52 #include <asm/uaccess.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_tcq.h>
61 #define IDESCSI_DEBUG_LOG 0
63 typedef struct idescsi_pc_s {
64 u8 c[12]; /* Actual packet bytes */
65 int request_transfer; /* Bytes to transfer */
66 int actually_transferred; /* Bytes actually transferred */
67 int buffer_size; /* Size of our data buffer */
68 struct request *rq; /* The corresponding request */
69 u8 *buffer; /* Data buffer */
70 u8 *current_position; /* Pointer into the above buffer */
71 struct scatterlist *sg; /* Scatter gather table */
72 unsigned int sg_cnt; /* Number of entries in sg */
73 int b_count; /* Bytes transferred from current entry */
74 struct scsi_cmnd *scsi_cmd; /* SCSI command */
75 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
76 unsigned long flags; /* Status/Action flags */
77 unsigned long timeout; /* Command timeout */
81 * Packet command status bits.
83 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
84 #define PC_WRITING 1 /* Data direction */
85 #define PC_TIMEDOUT 3 /* command timed out */
86 #define PC_DMA_OK 4 /* Use DMA */
89 * SCSI command transformation layer
91 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
96 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
98 typedef struct ide_scsi_obj {
100 ide_driver_t *driver;
101 struct gendisk *disk;
102 struct Scsi_Host *host;
104 idescsi_pc_t *pc; /* Current packet command */
105 unsigned long flags; /* Status/Action flags */
106 unsigned long transform; /* SCSI cmd translation layer */
107 unsigned long log; /* log flags */
110 static DEFINE_MUTEX(idescsi_ref_mutex);
111 static int idescsi_nocd; /* Set by module param to skip cd */
113 #define ide_scsi_g(disk) \
114 container_of((disk)->private_data, struct ide_scsi_obj, driver)
116 static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
118 struct ide_scsi_obj *scsi = NULL;
120 mutex_lock(&idescsi_ref_mutex);
121 scsi = ide_scsi_g(disk);
123 scsi_host_get(scsi->host);
124 mutex_unlock(&idescsi_ref_mutex);
128 static void ide_scsi_put(struct ide_scsi_obj *scsi)
130 mutex_lock(&idescsi_ref_mutex);
131 scsi_host_put(scsi->host);
132 mutex_unlock(&idescsi_ref_mutex);
135 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
137 return (idescsi_scsi_t*) (&host[1]);
140 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
142 return scsihost_to_idescsi(ide_drive->driver_data);
146 * Per ATAPI device status bits.
148 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
153 #define IDESCSI_PC_RQ 90
155 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
158 (void) HWIF(drive)->INB(IDE_DATA_REG);
161 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
164 HWIF(drive)->OUTB(0, IDE_DATA_REG);
168 * PIO data transfer routines using the scatter gather table.
170 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
176 count = min(pc->sg->length - pc->b_count, bcount);
177 if (PageHighMem(sg_page(pc->sg))) {
180 local_irq_save(flags);
181 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
183 drive->hwif->atapi_input_bytes(drive,
184 buf + pc->b_count, count);
185 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
186 local_irq_restore(flags);
188 buf = sg_virt(pc->sg);
189 drive->hwif->atapi_input_bytes(drive,
190 buf + pc->b_count, count);
192 bcount -= count; pc->b_count += count;
193 if (pc->b_count == pc->sg->length) {
196 pc->sg = sg_next(pc->sg);
202 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
203 idescsi_discard_data (drive, bcount);
207 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
213 count = min(pc->sg->length - pc->b_count, bcount);
214 if (PageHighMem(sg_page(pc->sg))) {
217 local_irq_save(flags);
218 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
220 drive->hwif->atapi_output_bytes(drive,
221 buf + pc->b_count, count);
222 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
223 local_irq_restore(flags);
225 buf = sg_virt(pc->sg);
226 drive->hwif->atapi_output_bytes(drive,
227 buf + pc->b_count, count);
229 bcount -= count; pc->b_count += count;
230 if (pc->b_count == pc->sg->length) {
233 pc->sg = sg_next(pc->sg);
239 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
240 idescsi_output_zeros (drive, bcount);
244 static void ide_scsi_hex_dump(u8 *data, int len)
246 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
249 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
251 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
256 /* stuff a sense request in front of our current request */
257 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
258 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
259 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
260 if (!pc || !rq || !buf) {
266 ide_init_drive_cmd(rq);
267 rq->special = (char *) pc;
270 pc->c[0] = REQUEST_SENSE;
271 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
272 rq->cmd_type = REQ_TYPE_SENSE;
273 pc->timeout = jiffies + WAIT_READY;
274 /* NOTE! Save the failed packet command in "rq->buffer" */
275 rq->buffer = (void *) failed_command->special;
276 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
277 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
278 printk ("ide-scsi: %s: queue cmd = ", drive->name);
279 ide_scsi_hex_dump(pc->c, 6);
281 rq->rq_disk = scsi->disk;
282 return ide_do_drive_cmd(drive, rq, ide_preempt);
285 static int idescsi_end_request(ide_drive_t *, int, int);
287 static ide_startstop_t
288 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
290 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
292 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
296 idescsi_end_request(drive, 0, 0);
301 static ide_startstop_t
302 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
304 #if IDESCSI_DEBUG_LOG
305 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
306 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
308 rq->errors |= ERROR_MAX;
310 idescsi_end_request(drive, 0, 0);
315 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
317 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
318 struct request *rq = HWGROUP(drive)->rq;
319 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
320 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
321 struct Scsi_Host *host;
322 int errors = rq->errors;
325 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
326 ide_end_request(drive, uptodate, nrsecs);
329 ide_end_drive_cmd (drive, 0, 0);
330 if (blk_sense_request(rq)) {
331 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
333 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
334 ide_scsi_hex_dump(pc->buffer, 16);
336 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
342 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
343 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
344 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
346 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
347 drive->name, pc->scsi_cmd->serial_number);
348 pc->scsi_cmd->result = DID_TIME_OUT << 16;
349 } else if (errors >= ERROR_MAX) {
350 pc->scsi_cmd->result = DID_ERROR << 16;
352 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
355 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
356 if (!idescsi_check_condition(drive, rq))
357 /* we started a request sense, so we'll be back, exit for now */
359 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
361 pc->scsi_cmd->result = DID_OK << 16;
363 host = pc->scsi_cmd->device->host;
364 spin_lock_irqsave(host->host_lock, flags);
365 pc->done(pc->scsi_cmd);
366 spin_unlock_irqrestore(host->host_lock, flags);
373 static inline unsigned long get_timeout(idescsi_pc_t *pc)
375 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
378 static int idescsi_expiry(ide_drive_t *drive)
380 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
381 idescsi_pc_t *pc = scsi->pc;
383 #if IDESCSI_DEBUG_LOG
384 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
386 set_bit(PC_TIMEDOUT, &pc->flags);
388 return 0; /* we do not want the ide subsystem to retry */
392 * Our interrupt handler.
394 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
396 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
397 ide_hwif_t *hwif = drive->hwif;
398 idescsi_pc_t *pc = scsi->pc;
399 struct request *rq = pc->rq;
404 #if IDESCSI_DEBUG_LOG
405 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
406 #endif /* IDESCSI_DEBUG_LOG */
408 if (test_bit(PC_TIMEDOUT, &pc->flags)){
409 #if IDESCSI_DEBUG_LOG
410 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
411 pc->scsi_cmd->serial_number, jiffies);
413 /* end this request now - scsi should retry it*/
414 idescsi_end_request (drive, 1, 0);
417 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
418 #if IDESCSI_DEBUG_LOG
419 printk ("ide-scsi: %s: DMA complete\n", drive->name);
420 #endif /* IDESCSI_DEBUG_LOG */
421 pc->actually_transferred=pc->request_transfer;
422 (void) HWIF(drive)->ide_dma_end(drive);
425 /* Clear the interrupt */
426 stat = drive->hwif->INB(IDE_STATUS_REG);
428 if ((stat & DRQ_STAT) == 0) {
429 /* No more interrupts */
430 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
431 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
432 local_irq_enable_in_hardirq();
435 idescsi_end_request (drive, 1, 0);
438 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
439 hwif->INB(IDE_BCOUNTL_REG);
440 ireason = hwif->INB(IDE_IREASON_REG);
443 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
444 return ide_do_reset (drive);
447 temp = pc->actually_transferred + bcount;
448 if (temp > pc->request_transfer) {
449 if (temp > pc->buffer_size) {
450 printk(KERN_ERR "ide-scsi: The scsi wants to "
451 "send us more data than expected "
452 "- discarding data\n");
453 temp = pc->buffer_size - pc->actually_transferred;
455 clear_bit(PC_WRITING, &pc->flags);
457 idescsi_input_buffers(drive, pc, temp);
459 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
460 printk(KERN_ERR "ide-scsi: transferred"
464 pc->actually_transferred += temp;
465 pc->current_position += temp;
466 idescsi_discard_data(drive, bcount - temp);
467 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
470 #if IDESCSI_DEBUG_LOG
471 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
472 #endif /* IDESCSI_DEBUG_LOG */
476 clear_bit(PC_WRITING, &pc->flags);
478 idescsi_input_buffers(drive, pc, bcount);
480 hwif->atapi_input_bytes(drive, pc->current_position,
483 set_bit(PC_WRITING, &pc->flags);
485 idescsi_output_buffers(drive, pc, bcount);
487 hwif->atapi_output_bytes(drive, pc->current_position,
490 /* Update the current position */
491 pc->actually_transferred += bcount;
492 pc->current_position += bcount;
494 /* And set the interrupt handler again */
495 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
499 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
501 ide_hwif_t *hwif = drive->hwif;
502 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
503 idescsi_pc_t *pc = scsi->pc;
504 ide_startstop_t startstop;
507 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
508 printk(KERN_ERR "ide-scsi: Strange, packet command "
509 "initiated yet DRQ isn't asserted\n");
512 ireason = hwif->INB(IDE_IREASON_REG);
513 if ((ireason & CD) == 0 || (ireason & IO)) {
514 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
515 "issuing a packet command\n");
516 return ide_do_reset (drive);
518 BUG_ON(HWGROUP(drive)->handler != NULL);
519 /* Set the interrupt routine */
520 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
521 /* Send the actual packet */
522 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
523 if (test_bit (PC_DMA_OK, &pc->flags)) {
524 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
525 hwif->dma_start(drive);
530 static inline int idescsi_set_direction(idescsi_pc_t *pc)
533 case READ_6: case READ_10: case READ_12:
534 clear_bit(PC_WRITING, &pc->flags);
536 case WRITE_6: case WRITE_10: case WRITE_12:
537 set_bit(PC_WRITING, &pc->flags);
544 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
546 ide_hwif_t *hwif = drive->hwif;
547 struct scatterlist *sg, *scsi_sg;
550 if (!pc->request_transfer || pc->request_transfer % 1024)
553 if (idescsi_set_direction(pc))
557 scsi_sg = scsi_sglist(pc->scsi_cmd);
558 segments = scsi_sg_count(pc->scsi_cmd);
560 if (segments > hwif->sg_max_nents)
563 hwif->sg_nents = segments;
564 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
570 * Issue a packet command
572 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
574 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
575 ide_hwif_t *hwif = drive->hwif;
579 scsi->pc=pc; /* Set the current packet command */
580 pc->actually_transferred=0; /* We haven't transferred any data yet */
581 pc->current_position=pc->buffer;
582 /* Request to transfer the entire buffer at once */
583 bcount = min(pc->request_transfer, 63 * 1024);
585 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
587 dma = !hwif->dma_setup(drive);
593 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK, bcount, dma);
596 set_bit(PC_DMA_OK, &pc->flags);
598 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
599 ide_execute_command(drive, WIN_PACKETCMD, &idescsi_transfer_pc,
600 get_timeout(pc), idescsi_expiry);
603 /* Issue the packet command */
604 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
605 return idescsi_transfer_pc(drive);
610 * idescsi_do_request is our request handling function.
612 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
614 #if IDESCSI_DEBUG_LOG
615 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
616 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
617 #endif /* IDESCSI_DEBUG_LOG */
619 if (blk_sense_request(rq) || blk_special_request(rq)) {
620 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
622 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
623 idescsi_end_request (drive, 0, 0);
627 #ifdef CONFIG_IDE_PROC_FS
628 static void idescsi_add_settings(ide_drive_t *drive)
630 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
633 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
635 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
636 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
637 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
638 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
639 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
642 static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
646 * Driver initialization.
648 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
650 if (drive->id && (drive->id->config & 0x0060) == 0x20)
651 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
652 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
653 #if IDESCSI_DEBUG_LOG
654 set_bit(IDESCSI_LOG_CMD, &scsi->log);
655 #endif /* IDESCSI_DEBUG_LOG */
656 idescsi_add_settings(drive);
659 static void ide_scsi_remove(ide_drive_t *drive)
661 struct Scsi_Host *scsihost = drive->driver_data;
662 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
663 struct gendisk *g = scsi->disk;
665 scsi_remove_host(scsihost);
666 ide_proc_unregister_driver(drive, scsi->driver);
668 ide_unregister_region(g);
670 drive->driver_data = NULL;
671 g->private_data = NULL;
677 static int ide_scsi_probe(ide_drive_t *);
679 #ifdef CONFIG_IDE_PROC_FS
680 static ide_proc_entry_t idescsi_proc[] = {
681 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
682 { NULL, 0, NULL, NULL }
686 static ide_driver_t idescsi_driver = {
688 .owner = THIS_MODULE,
690 .bus = &ide_bus_type,
692 .probe = ide_scsi_probe,
693 .remove = ide_scsi_remove,
694 .version = IDESCSI_VERSION,
696 .supports_dsc_overlap = 0,
697 .do_request = idescsi_do_request,
698 .end_request = idescsi_end_request,
699 .error = idescsi_atapi_error,
700 .abort = idescsi_atapi_abort,
701 #ifdef CONFIG_IDE_PROC_FS
702 .proc = idescsi_proc,
706 static int idescsi_ide_open(struct inode *inode, struct file *filp)
708 struct gendisk *disk = inode->i_bdev->bd_disk;
709 struct ide_scsi_obj *scsi;
711 if (!(scsi = ide_scsi_get(disk)))
717 static int idescsi_ide_release(struct inode *inode, struct file *filp)
719 struct gendisk *disk = inode->i_bdev->bd_disk;
720 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
727 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
728 unsigned int cmd, unsigned long arg)
730 struct block_device *bdev = inode->i_bdev;
731 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
732 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
735 static struct block_device_operations idescsi_ops = {
736 .owner = THIS_MODULE,
737 .open = idescsi_ide_open,
738 .release = idescsi_ide_release,
739 .ioctl = idescsi_ide_ioctl,
742 static int idescsi_slave_configure(struct scsi_device * sdp)
744 /* Configure detected device */
745 sdp->use_10_for_rw = 1;
746 sdp->use_10_for_ms = 1;
747 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
751 static const char *idescsi_info (struct Scsi_Host *host)
753 return "SCSI host adapter emulation for IDE ATAPI devices";
756 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
758 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
760 if (cmd == SG_SET_TRANSFORM) {
762 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
764 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
766 } else if (cmd == SG_GET_TRANSFORM)
767 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
771 static int idescsi_queue (struct scsi_cmnd *cmd,
772 void (*done)(struct scsi_cmnd *))
774 struct Scsi_Host *host = cmd->device->host;
775 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
776 ide_drive_t *drive = scsi->drive;
777 struct request *rq = NULL;
778 idescsi_pc_t *pc = NULL;
781 scmd_printk (KERN_ERR, cmd, "drive not present\n");
784 scsi = drive_to_idescsi(drive);
785 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
786 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
787 if (rq == NULL || pc == NULL) {
788 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
792 memset (pc->c, 0, 12);
795 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
797 pc->sg = scsi_sglist(cmd);
798 pc->sg_cnt = scsi_sg_count(cmd);
800 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
803 pc->timeout = jiffies + cmd->timeout_per_command;
805 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
806 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
807 ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
808 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
809 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
810 ide_scsi_hex_dump(pc->c, 12);
814 ide_init_drive_cmd (rq);
815 rq->special = (char *) pc;
816 rq->cmd_type = REQ_TYPE_SPECIAL;
817 spin_unlock_irq(host->host_lock);
818 rq->rq_disk = scsi->disk;
819 (void) ide_do_drive_cmd (drive, rq, ide_end);
820 spin_lock_irq(host->host_lock);
825 cmd->result = DID_ERROR << 16;
830 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
832 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
833 ide_drive_t *drive = scsi->drive;
837 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
839 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
840 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
843 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
848 /* First give it some more time, how much is "right" is hard to say :-( */
850 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
851 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
852 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
854 spin_lock_irq(&ide_lock);
856 /* If there is no pc running we're done (our interrupt took care of it) */
862 /* It's somewhere in flight. Does ide subsystem agree? */
863 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
864 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
866 * FIXME - not sure this condition can ever occur
868 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
870 if (blk_sense_request(scsi->pc->rq))
871 kfree(scsi->pc->buffer);
880 spin_unlock_irq(&ide_lock);
882 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
883 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
888 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
891 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
892 ide_drive_t *drive = scsi->drive;
896 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
898 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
899 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
902 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
907 spin_lock_irq(cmd->device->host->host_lock);
908 spin_lock(&ide_lock);
910 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
911 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
912 spin_unlock(&ide_lock);
913 spin_unlock_irq(cmd->device->host->host_lock);
917 /* kill current request */
918 if (__blk_end_request(req, -EIO, 0))
920 if (blk_sense_request(req))
921 kfree(scsi->pc->buffer);
926 /* now nuke the drive queue */
927 while ((req = elv_next_request(drive->queue))) {
928 if (__blk_end_request(req, -EIO, 0))
932 HWGROUP(drive)->rq = NULL;
933 HWGROUP(drive)->handler = NULL;
934 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
935 spin_unlock(&ide_lock);
939 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
942 spin_unlock_irq(cmd->device->host->host_lock);
944 spin_lock_irq(cmd->device->host->host_lock);
945 } while ( HWGROUP(drive)->handler );
947 ready = drive_is_ready(drive);
948 HWGROUP(drive)->busy--;
950 printk (KERN_ERR "ide-scsi: reset failed!\n");
954 spin_unlock_irq(cmd->device->host->host_lock);
958 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
959 sector_t capacity, int *parm)
961 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
962 ide_drive_t *drive = idescsi->drive;
964 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
965 parm[0] = drive->bios_head;
966 parm[1] = drive->bios_sect;
967 parm[2] = drive->bios_cyl;
972 static struct scsi_host_template idescsi_template = {
973 .module = THIS_MODULE,
975 .info = idescsi_info,
976 .slave_configure = idescsi_slave_configure,
977 .ioctl = idescsi_ioctl,
978 .queuecommand = idescsi_queue,
979 .eh_abort_handler = idescsi_eh_abort,
980 .eh_host_reset_handler = idescsi_eh_reset,
981 .bios_param = idescsi_bios,
987 .use_clustering = DISABLE_CLUSTERING,
989 .proc_name = "ide-scsi",
992 static int ide_scsi_probe(ide_drive_t *drive)
994 idescsi_scsi_t *idescsi;
995 struct Scsi_Host *host;
1000 if (!warned && drive->media == ide_cdrom) {
1001 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1005 if (idescsi_nocd && drive->media == ide_cdrom)
1008 if (!strstr("ide-scsi", drive->driver_req) ||
1010 drive->media == ide_disk ||
1011 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1014 g = alloc_disk(1 << PARTN_BITS);
1018 ide_init_disk(g, drive);
1022 #if IDESCSI_DEBUG_LOG
1023 if (drive->id->last_lun)
1024 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1026 if ((drive->id->last_lun & 0x7) != 7)
1027 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1031 drive->driver_data = host;
1032 idescsi = scsihost_to_idescsi(host);
1033 idescsi->drive = drive;
1034 idescsi->driver = &idescsi_driver;
1035 idescsi->host = host;
1037 g->private_data = &idescsi->driver;
1038 ide_proc_register_driver(drive, &idescsi_driver);
1040 idescsi_setup(drive, idescsi);
1041 g->fops = &idescsi_ops;
1042 ide_register_region(g);
1043 err = scsi_add_host(host, &drive->gendev);
1045 scsi_scan_host(host);
1048 /* fall through on error */
1049 ide_unregister_region(g);
1050 ide_proc_unregister_driver(drive, &idescsi_driver);
1054 scsi_host_put(host);
1058 static int __init init_idescsi_module(void)
1060 return driver_register(&idescsi_driver.gen_driver);
1063 static void __exit exit_idescsi_module(void)
1065 driver_unregister(&idescsi_driver.gen_driver);
1068 module_param(idescsi_nocd, int, 0600);
1069 MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
1070 module_init(init_idescsi_module);
1071 module_exit(exit_idescsi_module);
1072 MODULE_LICENSE("GPL");