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/config.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
42 #include <linux/ioport.h>
43 #include <linux/blkdev.h>
44 #include <linux/errno.h>
45 #include <linux/hdreg.h>
46 #include <linux/slab.h>
47 #include <linux/ide.h>
48 #include <linux/scatterlist.h>
51 #include <asm/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 int b_count; /* Bytes transferred from current entry */
73 struct scsi_cmnd *scsi_cmd; /* SCSI command */
74 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
75 unsigned long flags; /* Status/Action flags */
76 unsigned long timeout; /* Command timeout */
80 * Packet command status bits.
82 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
83 #define PC_WRITING 1 /* Data direction */
84 #define PC_TRANSFORM 2 /* transform SCSI commands */
85 #define PC_TIMEDOUT 3 /* command timed out */
86 #define PC_DMA_OK 4 /* Use DMA */
89 * SCSI command transformation layer
91 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
92 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
97 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
101 idescsi_pc_t *pc; /* Current packet command */
102 unsigned long flags; /* Status/Action flags */
103 unsigned long transform; /* SCSI cmd translation layer */
104 unsigned long log; /* log flags */
107 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
109 return (idescsi_scsi_t*) (&host[1]);
112 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
114 return scsihost_to_idescsi(ide_drive->driver_data);
118 * Per ATAPI device status bits.
120 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
125 #define IDESCSI_PC_RQ 90
127 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
130 (void) HWIF(drive)->INB(IDE_DATA_REG);
133 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
136 HWIF(drive)->OUTB(0, IDE_DATA_REG);
140 * PIO data transfer routines using the scatter gather table.
142 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
148 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
149 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
150 idescsi_discard_data (drive, bcount);
153 count = min(pc->sg->length - pc->b_count, bcount);
154 buf = page_address(pc->sg->page) + pc->sg->offset;
155 drive->hwif->atapi_input_bytes(drive, buf + pc->b_count, count);
156 bcount -= count; pc->b_count += count;
157 if (pc->b_count == pc->sg->length) {
164 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
170 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
171 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
172 idescsi_output_zeros (drive, bcount);
175 count = min(pc->sg->length - pc->b_count, bcount);
176 buf = page_address(pc->sg->page) + pc->sg->offset;
177 drive->hwif->atapi_output_bytes(drive, buf + pc->b_count, count);
178 bcount -= count; pc->b_count += count;
179 if (pc->b_count == pc->sg->length) {
187 * Most of the SCSI commands are supported directly by ATAPI devices.
188 * idescsi_transform_pc handles the few exceptions.
190 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
192 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
195 if (!test_bit(PC_TRANSFORM, &pc->flags))
197 if (drive->media == ide_cdrom || drive->media == ide_optical) {
198 if (c[0] == READ_6 || c[0] == WRITE_6) {
199 c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
200 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
201 c[0] += (READ_10 - READ_6);
203 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
204 unsigned short new_len;
207 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
209 memset(atapi_buf, 0, pc->buffer_size + 4);
218 if (c[0] == MODE_SELECT_10) {
219 atapi_buf[1] = scsi_buf[0]; /* Mode data length */
220 atapi_buf[2] = scsi_buf[1]; /* Medium type */
221 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
222 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
223 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
225 pc->buffer = atapi_buf;
226 pc->request_transfer += 4;
227 pc->buffer_size += 4;
232 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
234 u8 *atapi_buf = pc->buffer;
235 u8 *sc = pc->scsi_cmd->cmnd;
236 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
238 if (!test_bit(PC_TRANSFORM, &pc->flags))
240 if (drive->media == ide_cdrom || drive->media == ide_optical) {
241 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
242 scsi_buf[0] = atapi_buf[1]; /* Mode data length */
243 scsi_buf[1] = atapi_buf[2]; /* Medium type */
244 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
245 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
246 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
248 if (pc->c[0] == INQUIRY) {
249 scsi_buf[2] |= 2; /* ansi_revision */
250 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
253 if (atapi_buf && atapi_buf != scsi_buf)
257 static void hexdump(u8 *x, int len)
262 for (i = 0; i < len; i++)
267 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
269 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
274 /* stuff a sense request in front of our current request */
275 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
276 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
277 buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
278 if (pc == NULL || rq == NULL || buf == NULL) {
284 memset (pc, 0, sizeof (idescsi_pc_t));
285 memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
286 ide_init_drive_cmd(rq);
287 rq->special = (char *) pc;
290 pc->c[0] = REQUEST_SENSE;
291 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
292 rq->flags = REQ_SENSE;
293 pc->timeout = jiffies + WAIT_READY;
294 /* NOTE! Save the failed packet command in "rq->buffer" */
295 rq->buffer = (void *) failed_command->special;
296 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
297 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
298 printk ("ide-scsi: %s: queue cmd = ", drive->name);
301 return ide_do_drive_cmd(drive, rq, ide_preempt);
304 static ide_startstop_t
305 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
307 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
309 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
312 DRIVER(drive)->end_request(drive, 0, 0);
316 static ide_startstop_t
317 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
319 #if IDESCSI_DEBUG_LOG
320 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
321 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
323 rq->errors |= ERROR_MAX;
324 DRIVER(drive)->end_request(drive, 0, 0);
328 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
330 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
331 struct request *rq = HWGROUP(drive)->rq;
332 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
333 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
334 struct Scsi_Host *host;
338 if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) {
339 ide_end_request(drive, uptodate, nrsecs);
342 ide_end_drive_cmd (drive, 0, 0);
343 if (rq->flags & REQ_SENSE) {
344 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
346 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
347 hexdump(pc->buffer,16);
349 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
355 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
356 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
357 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
359 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
360 drive->name, pc->scsi_cmd->serial_number);
361 pc->scsi_cmd->result = DID_TIME_OUT << 16;
362 } else if (rq->errors >= ERROR_MAX) {
363 pc->scsi_cmd->result = DID_ERROR << 16;
365 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
366 } else if (rq->errors) {
368 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
369 if (!idescsi_check_condition(drive, rq))
370 /* we started a request sense, so we'll be back, exit for now */
372 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
374 pc->scsi_cmd->result = DID_OK << 16;
375 idescsi_transform_pc2 (drive, pc);
377 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
378 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
380 scsi_buf = pc->scsi_cmd->request_buffer;
381 hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
385 host = pc->scsi_cmd->device->host;
386 spin_lock_irqsave(host->host_lock, flags);
387 pc->done(pc->scsi_cmd);
388 spin_unlock_irqrestore(host->host_lock, flags);
395 static inline unsigned long get_timeout(idescsi_pc_t *pc)
397 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
400 static int idescsi_expiry(ide_drive_t *drive)
402 idescsi_scsi_t *scsi = drive->driver_data;
403 idescsi_pc_t *pc = scsi->pc;
405 #if IDESCSI_DEBUG_LOG
406 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
408 set_bit(PC_TIMEDOUT, &pc->flags);
410 return 0; /* we do not want the ide subsystem to retry */
414 * Our interrupt handler.
416 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
418 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
419 idescsi_pc_t *pc=scsi->pc;
420 struct request *rq = pc->rq;
421 atapi_bcount_t bcount;
422 atapi_status_t status;
423 atapi_ireason_t ireason;
424 atapi_feature_t feature;
428 #if IDESCSI_DEBUG_LOG
429 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
430 #endif /* IDESCSI_DEBUG_LOG */
432 if (test_bit(PC_TIMEDOUT, &pc->flags)){
433 #if IDESCSI_DEBUG_LOG
434 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
435 pc->scsi_cmd->serial_number, jiffies);
437 /* end this request now - scsi should retry it*/
438 idescsi_end_request (drive, 1, 0);
441 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
442 #if IDESCSI_DEBUG_LOG
443 printk ("ide-scsi: %s: DMA complete\n", drive->name);
444 #endif /* IDESCSI_DEBUG_LOG */
445 pc->actually_transferred=pc->request_transfer;
446 (void) HWIF(drive)->ide_dma_end(drive);
450 /* Clear the interrupt */
451 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
454 /* No more interrupts */
455 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
456 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
460 idescsi_end_request (drive, 1, 0);
463 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
464 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
465 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
468 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
469 return ide_do_reset (drive);
472 temp = pc->actually_transferred + bcount.all;
473 if (temp > pc->request_transfer) {
474 if (temp > pc->buffer_size) {
475 printk(KERN_ERR "ide-scsi: The scsi wants to "
476 "send us more data than expected "
477 "- discarding data\n");
478 temp = pc->buffer_size - pc->actually_transferred;
480 clear_bit(PC_WRITING, &pc->flags);
482 idescsi_input_buffers(drive, pc, temp);
484 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
485 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
487 pc->actually_transferred += temp;
488 pc->current_position += temp;
489 idescsi_discard_data(drive, bcount.all - temp);
490 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
493 #if IDESCSI_DEBUG_LOG
494 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
495 #endif /* IDESCSI_DEBUG_LOG */
499 clear_bit(PC_WRITING, &pc->flags);
501 idescsi_input_buffers(drive, pc, bcount.all);
503 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
505 set_bit(PC_WRITING, &pc->flags);
507 idescsi_output_buffers (drive, pc, bcount.all);
509 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
511 /* Update the current position */
512 pc->actually_transferred += bcount.all;
513 pc->current_position += bcount.all;
515 /* And set the interrupt handler again */
516 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
520 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
522 ide_hwif_t *hwif = drive->hwif;
523 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
524 idescsi_pc_t *pc = scsi->pc;
525 atapi_ireason_t ireason;
526 ide_startstop_t startstop;
528 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
529 printk(KERN_ERR "ide-scsi: Strange, packet command "
530 "initiated yet DRQ isn't asserted\n");
533 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
534 if (!ireason.b.cod || ireason.b.io) {
535 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
536 "issuing a packet command\n");
537 return ide_do_reset (drive);
539 if (HWGROUP(drive)->handler != NULL)
541 /* Set the interrupt routine */
542 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
543 /* Send the actual packet */
544 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
545 if (test_bit (PC_DMA_OK, &pc->flags)) {
546 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
547 hwif->dma_start(drive);
552 static inline int idescsi_set_direction(idescsi_pc_t *pc)
555 case READ_6: case READ_10: case READ_12:
556 clear_bit(PC_WRITING, &pc->flags);
558 case WRITE_6: case WRITE_10: case WRITE_12:
559 set_bit(PC_WRITING, &pc->flags);
566 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
568 ide_hwif_t *hwif = drive->hwif;
569 struct scatterlist *sg, *scsi_sg;
572 if (!pc->request_transfer || pc->request_transfer % 1024)
575 if (idescsi_set_direction(pc))
579 scsi_sg = pc->scsi_cmd->request_buffer;
580 segments = pc->scsi_cmd->use_sg;
582 if (segments > hwif->sg_max_nents)
587 sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
589 hwif->sg_nents = segments;
590 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
597 * Issue a packet command
599 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
601 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
602 ide_hwif_t *hwif = drive->hwif;
603 atapi_feature_t feature;
604 atapi_bcount_t bcount;
606 scsi->pc=pc; /* Set the current packet command */
607 pc->actually_transferred=0; /* We haven't transferred any data yet */
608 pc->current_position=pc->buffer;
609 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
612 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
614 feature.b.dma = !hwif->dma_setup(drive);
620 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
622 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
623 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
624 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
627 set_bit(PC_DMA_OK, &pc->flags);
629 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
630 if (HWGROUP(drive)->handler != NULL)
632 ide_set_handler(drive, &idescsi_transfer_pc,
633 get_timeout(pc), idescsi_expiry);
634 /* Issue the packet command */
635 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
638 /* Issue the packet command */
639 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
640 return idescsi_transfer_pc(drive);
645 * idescsi_do_request is our request handling function.
647 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
649 #if IDESCSI_DEBUG_LOG
650 printk (KERN_INFO "rq_status: %d, dev: %s, cmd: %x, errors: %d\n",rq->rq_status, rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
651 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
652 #endif /* IDESCSI_DEBUG_LOG */
654 if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) {
655 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
657 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
658 idescsi_end_request (drive, 0, 0);
662 static void idescsi_add_settings(ide_drive_t *drive)
664 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
667 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
669 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
670 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
671 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
672 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
673 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
677 * Driver initialization.
679 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
681 DRIVER(drive)->busy++;
682 drive->ready_stat = 0;
683 if (drive->id && (drive->id->config & 0x0060) == 0x20)
684 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
685 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
686 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
687 #if IDESCSI_DEBUG_LOG
688 set_bit(IDESCSI_LOG_CMD, &scsi->log);
689 #endif /* IDESCSI_DEBUG_LOG */
690 idescsi_add_settings(drive);
691 DRIVER(drive)->busy--;
694 static int idescsi_cleanup (ide_drive_t *drive)
696 struct Scsi_Host *scsihost = drive->driver_data;
698 if (ide_unregister_subdriver(drive))
701 /* FIXME?: Are these two statements necessary? */
702 drive->driver_data = NULL;
703 drive->disk->fops = ide_fops;
705 scsi_remove_host(scsihost);
706 scsi_host_put(scsihost);
710 static int idescsi_attach(ide_drive_t *drive);
713 * IDE subdriver functions, registered with ide.c
715 static ide_driver_t idescsi_driver = {
716 .owner = THIS_MODULE,
718 .version = IDESCSI_VERSION,
721 .supports_dsc_overlap = 0,
722 .attach = idescsi_attach,
723 .cleanup = idescsi_cleanup,
724 .do_request = idescsi_do_request,
725 .end_request = idescsi_end_request,
726 .error = idescsi_atapi_error,
727 .abort = idescsi_atapi_abort,
728 .drives = LIST_HEAD_INIT(idescsi_driver.drives),
731 static int idescsi_ide_open(struct inode *inode, struct file *filp)
733 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
738 static int idescsi_ide_release(struct inode *inode, struct file *filp)
740 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
745 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
746 unsigned int cmd, unsigned long arg)
748 struct block_device *bdev = inode->i_bdev;
749 return generic_ide_ioctl(file, bdev, cmd, arg);
752 static struct block_device_operations idescsi_ops = {
753 .owner = THIS_MODULE,
754 .open = idescsi_ide_open,
755 .release = idescsi_ide_release,
756 .ioctl = idescsi_ide_ioctl,
759 static int idescsi_attach(ide_drive_t *drive);
761 static int idescsi_slave_configure(struct scsi_device * sdp)
763 /* Configure detected device */
764 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
768 static const char *idescsi_info (struct Scsi_Host *host)
770 return "SCSI host adapter emulation for IDE ATAPI devices";
773 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
775 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
777 if (cmd == SG_SET_TRANSFORM) {
779 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
781 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
783 } else if (cmd == SG_GET_TRANSFORM)
784 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
788 static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
790 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
792 /* this was a layering violation and we can't support it
795 struct gendisk *disk = cmd->request->rq_disk;
798 struct Scsi_Device_Template **p = disk->private_data;
799 if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
800 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
803 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
806 static int idescsi_queue (struct scsi_cmnd *cmd,
807 void (*done)(struct scsi_cmnd *))
809 struct Scsi_Host *host = cmd->device->host;
810 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
811 ide_drive_t *drive = scsi->drive;
812 struct request *rq = NULL;
813 idescsi_pc_t *pc = NULL;
816 printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->device->id);
819 scsi = drive_to_idescsi(drive);
820 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
821 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
822 if (rq == NULL || pc == NULL) {
823 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
827 memset (pc->c, 0, 12);
830 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
833 pc->sg = cmd->request_buffer;
835 pc->buffer = cmd->request_buffer;
839 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
842 pc->timeout = jiffies + cmd->timeout_per_command;
844 if (should_transform(drive, cmd))
845 set_bit(PC_TRANSFORM, &pc->flags);
846 idescsi_transform_pc1 (drive, pc);
848 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
849 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
850 hexdump(cmd->cmnd, cmd->cmd_len);
851 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
852 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
857 ide_init_drive_cmd (rq);
858 rq->special = (char *) pc;
859 rq->flags = REQ_SPECIAL;
860 spin_unlock_irq(host->host_lock);
861 (void) ide_do_drive_cmd (drive, rq, ide_end);
862 spin_lock_irq(host->host_lock);
867 cmd->result = DID_ERROR << 16;
872 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
874 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
875 ide_drive_t *drive = scsi->drive;
879 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
881 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
882 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
885 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
890 /* First give it some more time, how much is "right" is hard to say :-( */
892 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
893 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
894 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
896 spin_lock_irq(&ide_lock);
898 /* If there is no pc running we're done (our interrupt took care of it) */
904 /* It's somewhere in flight. Does ide subsystem agree? */
905 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
906 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
908 * FIXME - not sure this condition can ever occur
910 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
912 if (scsi->pc->rq->flags & REQ_SENSE)
913 kfree(scsi->pc->buffer);
922 spin_unlock_irq(&ide_lock);
924 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
925 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
930 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
933 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
934 ide_drive_t *drive = scsi->drive;
938 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
940 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
941 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
944 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
949 spin_lock_irq(&ide_lock);
951 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
952 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
953 spin_unlock(&ide_lock);
957 /* kill current request */
958 blkdev_dequeue_request(req);
959 end_that_request_last(req);
960 if (req->flags & REQ_SENSE)
961 kfree(scsi->pc->buffer);
966 /* now nuke the drive queue */
967 while ((req = elv_next_request(drive->queue))) {
968 blkdev_dequeue_request(req);
969 end_that_request_last(req);
972 HWGROUP(drive)->rq = NULL;
973 HWGROUP(drive)->handler = NULL;
974 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
975 spin_unlock_irq(&ide_lock);
979 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
982 set_current_state(TASK_UNINTERRUPTIBLE);
983 spin_unlock_irq(cmd->device->host->host_lock);
984 schedule_timeout(HZ/20);
985 spin_lock_irq(cmd->device->host->host_lock);
986 } while ( HWGROUP(drive)->handler );
988 ready = drive_is_ready(drive);
989 HWGROUP(drive)->busy--;
991 printk (KERN_ERR "ide-scsi: reset failed!\n");
998 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
999 sector_t capacity, int *parm)
1001 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
1002 ide_drive_t *drive = idescsi->drive;
1004 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1005 parm[0] = drive->bios_head;
1006 parm[1] = drive->bios_sect;
1007 parm[2] = drive->bios_cyl;
1012 static struct scsi_host_template idescsi_template = {
1013 .module = THIS_MODULE,
1015 .info = idescsi_info,
1016 .slave_configure = idescsi_slave_configure,
1017 .ioctl = idescsi_ioctl,
1018 .queuecommand = idescsi_queue,
1019 .eh_abort_handler = idescsi_eh_abort,
1020 .eh_host_reset_handler = idescsi_eh_reset,
1021 .bios_param = idescsi_bios,
1024 .sg_tablesize = 256,
1027 .use_clustering = DISABLE_CLUSTERING,
1029 .proc_name = "ide-scsi",
1032 static int idescsi_attach(ide_drive_t *drive)
1034 idescsi_scsi_t *idescsi;
1035 struct Scsi_Host *host;
1039 if (!warned && drive->media == ide_cdrom) {
1040 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1044 if (!strstr("ide-scsi", drive->driver_req) ||
1046 drive->media == ide_disk ||
1047 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1052 #if IDESCSI_DEBUG_LOG
1053 if (drive->id->last_lun)
1054 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1056 if ((drive->id->last_lun & 0x7) != 7)
1057 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1061 drive->driver_data = host;
1062 idescsi = scsihost_to_idescsi(host);
1063 idescsi->drive = drive;
1064 err = ide_register_subdriver(drive, &idescsi_driver);
1066 idescsi_setup (drive, idescsi);
1067 drive->disk->fops = &idescsi_ops;
1068 err = scsi_add_host(host, &drive->gendev);
1070 scsi_scan_host(host);
1073 /* fall through on error */
1074 ide_unregister_subdriver(drive);
1077 scsi_host_put(host);
1081 static int __init init_idescsi_module(void)
1083 return ide_register_driver(&idescsi_driver);
1086 static void __exit exit_idescsi_module(void)
1088 ide_unregister_driver(&idescsi_driver);
1091 module_init(init_idescsi_module);
1092 module_exit(exit_idescsi_module);
1093 MODULE_LICENSE("GPL");