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>
50 #include <asm/bitops.h>
51 #include <asm/uaccess.h>
57 #define IDESCSI_DEBUG_LOG 0
59 typedef struct idescsi_pc_s {
60 u8 c[12]; /* Actual packet bytes */
61 int request_transfer; /* Bytes to transfer */
62 int actually_transferred; /* Bytes actually transferred */
63 int buffer_size; /* Size of our data buffer */
64 struct request *rq; /* The corresponding request */
65 u8 *buffer; /* Data buffer */
66 u8 *current_position; /* Pointer into the above buffer */
67 struct scatterlist *sg; /* Scatter gather table */
68 int b_count; /* Bytes transferred from current entry */
69 Scsi_Cmnd *scsi_cmd; /* SCSI command */
70 void (*done)(Scsi_Cmnd *); /* Scsi completion routine */
71 unsigned long flags; /* Status/Action flags */
72 unsigned long timeout; /* Command timeout */
76 * Packet command status bits.
78 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
79 #define PC_WRITING 1 /* Data direction */
80 #define PC_TRANSFORM 2 /* transform SCSI commands */
81 #define PC_TIMEDOUT 3 /* command timed out */
82 #define PC_DMA_OK 4 /* Use DMA */
85 * SCSI command transformation layer
87 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
88 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
93 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
97 idescsi_pc_t *pc; /* Current packet command */
98 unsigned long flags; /* Status/Action flags */
99 unsigned long transform; /* SCSI cmd translation layer */
100 unsigned long log; /* log flags */
103 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
105 return (idescsi_scsi_t*) (&host[1]);
108 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
110 return scsihost_to_idescsi(ide_drive->driver_data);
114 * Per ATAPI device status bits.
116 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
121 #define IDESCSI_PC_RQ 90
123 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
126 (void) HWIF(drive)->INB(IDE_DATA_REG);
129 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
132 HWIF(drive)->OUTB(0, IDE_DATA_REG);
136 * PIO data transfer routines using the scatter gather table.
138 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
144 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
145 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
146 idescsi_discard_data (drive, bcount);
149 count = min(pc->sg->length - pc->b_count, bcount);
150 buf = page_address(pc->sg->page) + pc->sg->offset;
151 atapi_input_bytes (drive, buf + pc->b_count, count);
152 bcount -= count; pc->b_count += count;
153 if (pc->b_count == pc->sg->length) {
160 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
166 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
167 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
168 idescsi_output_zeros (drive, bcount);
171 count = min(pc->sg->length - pc->b_count, bcount);
172 buf = page_address(pc->sg->page) + pc->sg->offset;
173 atapi_output_bytes (drive, buf + pc->b_count, count);
174 bcount -= count; pc->b_count += count;
175 if (pc->b_count == pc->sg->length) {
183 * Most of the SCSI commands are supported directly by ATAPI devices.
184 * idescsi_transform_pc handles the few exceptions.
186 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
188 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
191 if (!test_bit(PC_TRANSFORM, &pc->flags))
193 if (drive->media == ide_cdrom || drive->media == ide_optical) {
194 if (c[0] == READ_6 || c[0] == WRITE_6) {
195 c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
196 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
197 c[0] += (READ_10 - READ_6);
199 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
200 unsigned short new_len;
203 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
205 memset(atapi_buf, 0, pc->buffer_size + 4);
214 if (c[0] == MODE_SELECT_10) {
215 atapi_buf[1] = scsi_buf[0]; /* Mode data length */
216 atapi_buf[2] = scsi_buf[1]; /* Medium type */
217 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
218 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
219 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
221 pc->buffer = atapi_buf;
222 pc->request_transfer += 4;
223 pc->buffer_size += 4;
228 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
230 u8 *atapi_buf = pc->buffer;
231 u8 *sc = pc->scsi_cmd->cmnd;
232 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
234 if (!test_bit(PC_TRANSFORM, &pc->flags))
236 if (drive->media == ide_cdrom || drive->media == ide_optical) {
237 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
238 scsi_buf[0] = atapi_buf[1]; /* Mode data length */
239 scsi_buf[1] = atapi_buf[2]; /* Medium type */
240 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
241 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
242 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
244 if (pc->c[0] == INQUIRY) {
245 scsi_buf[2] |= 2; /* ansi_revision */
246 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
249 if (atapi_buf && atapi_buf != scsi_buf)
253 static inline void idescsi_free_bio (struct bio *bio)
264 static void hexdump(u8 *x, int len)
269 for (i = 0; i < len; i++)
274 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
276 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
281 /* stuff a sense request in front of our current request */
282 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
283 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
284 buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
285 if (pc == NULL || rq == NULL || buf == NULL) {
291 memset (pc, 0, sizeof (idescsi_pc_t));
292 memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
293 ide_init_drive_cmd(rq);
294 rq->special = (char *) pc;
297 pc->c[0] = REQUEST_SENSE;
298 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
299 rq->flags = REQ_SENSE;
300 pc->timeout = jiffies + WAIT_READY;
301 /* NOTE! Save the failed packet command in "rq->buffer" */
302 rq->buffer = (void *) failed_command->special;
303 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
304 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
305 printk ("ide-scsi: %s: queue cmd = ", drive->name);
308 return ide_do_drive_cmd(drive, rq, ide_preempt);
311 ide_startstop_t idescsi_atapi_error (ide_drive_t *drive, const char *msg, byte stat)
316 err = ide_dump_atapi_status(drive, msg, stat);
318 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
321 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
323 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
326 DRIVER(drive)->end_request(drive, 0, 0);
330 ide_startstop_t idescsi_atapi_abort (ide_drive_t *drive, const char *msg)
334 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
337 #if IDESCSI_DEBUG_LOG
338 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
339 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
341 rq->errors |= ERROR_MAX;
342 DRIVER(drive)->end_request(drive, 0, 0);
346 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
348 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
349 struct request *rq = HWGROUP(drive)->rq;
350 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
351 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
352 struct Scsi_Host *host;
356 if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) {
357 ide_end_request(drive, uptodate, nrsecs);
360 ide_end_drive_cmd (drive, 0, 0);
361 if (rq->flags & REQ_SENSE) {
362 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
364 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
365 hexdump(pc->buffer,16);
367 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
373 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
374 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
375 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
377 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
378 drive->name, pc->scsi_cmd->serial_number);
379 pc->scsi_cmd->result = DID_TIME_OUT << 16;
380 } else if (rq->errors >= ERROR_MAX) {
381 pc->scsi_cmd->result = DID_ERROR << 16;
383 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
384 } else if (rq->errors) {
386 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
387 if (!idescsi_check_condition(drive, rq))
388 /* we started a request sense, so we'll be back, exit for now */
390 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
392 pc->scsi_cmd->result = DID_OK << 16;
393 idescsi_transform_pc2 (drive, pc);
395 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
396 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
398 scsi_buf = pc->scsi_cmd->request_buffer;
399 hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
403 host = pc->scsi_cmd->device->host;
404 spin_lock_irqsave(host->host_lock, flags);
405 pc->done(pc->scsi_cmd);
406 spin_unlock_irqrestore(host->host_lock, flags);
407 idescsi_free_bio(rq->bio);
414 static inline unsigned long get_timeout(idescsi_pc_t *pc)
416 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
419 static int idescsi_expiry(ide_drive_t *drive)
421 idescsi_scsi_t *scsi = drive->driver_data;
422 idescsi_pc_t *pc = scsi->pc;
424 #if IDESCSI_DEBUG_LOG
425 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
427 set_bit(PC_TIMEDOUT, &pc->flags);
429 return 0; /* we do not want the ide subsystem to retry */
433 * Our interrupt handler.
435 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
437 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
438 idescsi_pc_t *pc=scsi->pc;
439 struct request *rq = pc->rq;
440 atapi_bcount_t bcount;
441 atapi_status_t status;
442 atapi_ireason_t ireason;
443 atapi_feature_t feature;
447 #if IDESCSI_DEBUG_LOG
448 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
449 #endif /* IDESCSI_DEBUG_LOG */
451 if (test_bit(PC_TIMEDOUT, &pc->flags)){
452 #if IDESCSI_DEBUG_LOG
453 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
454 pc->scsi_cmd->serial_number, jiffies);
456 /* end this request now - scsi should retry it*/
457 idescsi_end_request (drive, 1, 0);
460 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
461 #if IDESCSI_DEBUG_LOG
462 printk ("ide-scsi: %s: DMA complete\n", drive->name);
463 #endif /* IDESCSI_DEBUG_LOG */
464 pc->actually_transferred=pc->request_transfer;
465 (void) HWIF(drive)->ide_dma_end(drive);
469 /* Clear the interrupt */
470 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
473 /* No more interrupts */
474 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
475 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
479 idescsi_end_request (drive, 1, 0);
482 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
483 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
484 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
487 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
488 return ide_do_reset (drive);
491 temp = pc->actually_transferred + bcount.all;
492 if (temp > pc->request_transfer) {
493 if (temp > pc->buffer_size) {
494 printk(KERN_ERR "ide-scsi: The scsi wants to "
495 "send us more data than expected "
496 "- discarding data\n");
497 temp = pc->buffer_size - pc->actually_transferred;
499 clear_bit(PC_WRITING, &pc->flags);
501 idescsi_input_buffers(drive, pc, temp);
503 atapi_input_bytes(drive, pc->current_position, temp);
504 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
506 pc->actually_transferred += temp;
507 pc->current_position += temp;
508 idescsi_discard_data(drive, bcount.all - temp);
509 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
512 #if IDESCSI_DEBUG_LOG
513 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
514 #endif /* IDESCSI_DEBUG_LOG */
518 clear_bit(PC_WRITING, &pc->flags);
520 idescsi_input_buffers(drive, pc, bcount.all);
522 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
524 set_bit(PC_WRITING, &pc->flags);
526 idescsi_output_buffers (drive, pc, bcount.all);
528 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
530 /* Update the current position */
531 pc->actually_transferred += bcount.all;
532 pc->current_position += bcount.all;
534 /* And set the interrupt handler again */
535 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
539 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
541 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
542 idescsi_pc_t *pc = scsi->pc;
543 atapi_ireason_t ireason;
544 ide_startstop_t startstop;
546 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
547 printk(KERN_ERR "ide-scsi: Strange, packet command "
548 "initiated yet DRQ isn't asserted\n");
551 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
552 if (!ireason.b.cod || ireason.b.io) {
553 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
554 "issuing a packet command\n");
555 return ide_do_reset (drive);
557 if (HWGROUP(drive)->handler != NULL)
559 /* Set the interrupt routine */
560 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
561 /* Send the actual packet */
562 atapi_output_bytes(drive, scsi->pc->c, 12);
563 if (test_bit (PC_DMA_OK, &pc->flags)) {
564 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
565 (void) (HWIF(drive)->ide_dma_begin(drive));
571 * Issue a packet command
573 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
575 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
576 atapi_feature_t feature;
577 atapi_bcount_t bcount;
578 struct request *rq = pc->rq;
580 scsi->pc=pc; /* Set the current packet command */
581 pc->actually_transferred=0; /* We haven't transferred any data yet */
582 pc->current_position=pc->buffer;
583 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
586 if (drive->using_dma && rq->bio) {
587 if (test_bit(PC_WRITING, &pc->flags))
588 feature.b.dma = !HWIF(drive)->ide_dma_write(drive);
590 feature.b.dma = !HWIF(drive)->ide_dma_read(drive);
595 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
597 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
598 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
599 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
602 set_bit(PC_DMA_OK, &pc->flags);
604 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
605 if (HWGROUP(drive)->handler != NULL)
607 ide_set_handler(drive, &idescsi_transfer_pc,
608 get_timeout(pc), idescsi_expiry);
609 /* Issue the packet command */
610 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
613 /* Issue the packet command */
614 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
615 return idescsi_transfer_pc(drive);
620 * idescsi_do_request is our request handling function.
622 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
624 #if IDESCSI_DEBUG_LOG
625 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);
626 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
627 #endif /* IDESCSI_DEBUG_LOG */
629 if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) {
630 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
632 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
633 idescsi_end_request (drive, 0, 0);
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 ioctl ioctl data type min max mul_factor div_factor data pointer set function
644 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
645 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
646 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
647 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
648 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
652 * Driver initialization.
654 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
656 DRIVER(drive)->busy++;
657 drive->ready_stat = 0;
658 if (drive->id && (drive->id->config & 0x0060) == 0x20)
659 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
660 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
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);
666 DRIVER(drive)->busy--;
669 static int idescsi_cleanup (ide_drive_t *drive)
671 struct Scsi_Host *scsihost = drive->driver_data;
673 if (ide_unregister_subdriver(drive))
676 /* FIXME?: Are these two statements necessary? */
677 drive->driver_data = NULL;
678 drive->disk->fops = ide_fops;
680 scsi_remove_host(scsihost);
681 scsi_host_put(scsihost);
685 static int idescsi_attach(ide_drive_t *drive);
688 * IDE subdriver functions, registered with ide.c
690 static ide_driver_t idescsi_driver = {
691 .owner = THIS_MODULE,
693 .version = IDESCSI_VERSION,
696 .supports_dsc_overlap = 0,
697 .attach = idescsi_attach,
698 .cleanup = idescsi_cleanup,
699 .do_request = idescsi_do_request,
700 .end_request = idescsi_end_request,
701 .error = idescsi_atapi_error,
702 .abort = idescsi_atapi_abort,
703 .drives = LIST_HEAD_INIT(idescsi_driver.drives),
706 static int idescsi_ide_open(struct inode *inode, struct file *filp)
708 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
713 static int idescsi_ide_release(struct inode *inode, struct file *filp)
715 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
720 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
721 unsigned int cmd, unsigned long arg)
723 struct block_device *bdev = inode->i_bdev;
724 return generic_ide_ioctl(bdev, cmd, arg);
727 static struct block_device_operations idescsi_ops = {
728 .owner = THIS_MODULE,
729 .open = idescsi_ide_open,
730 .release = idescsi_ide_release,
731 .ioctl = idescsi_ide_ioctl,
734 static int idescsi_attach(ide_drive_t *drive);
736 static int idescsi_slave_configure(Scsi_Device * sdp)
738 /* Configure detected device */
739 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
743 static const char *idescsi_info (struct Scsi_Host *host)
745 return "SCSI host adapter emulation for IDE ATAPI devices";
748 static int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
750 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
752 if (cmd == SG_SET_TRANSFORM) {
754 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
756 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
758 } else if (cmd == SG_GET_TRANSFORM)
759 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
763 static inline struct bio *idescsi_kmalloc_bio (int count)
765 struct bio *bh, *bhp, *first_bh;
767 if ((first_bh = bhp = bh = bio_alloc(GFP_ATOMIC, 1)) == NULL)
772 if ((bh = bio_alloc(GFP_ATOMIC, 1)) == NULL)
782 idescsi_free_bio (first_bh);
786 static inline int idescsi_set_direction (idescsi_pc_t *pc)
789 case READ_6: case READ_10: case READ_12:
790 clear_bit (PC_WRITING, &pc->flags);
792 case WRITE_6: case WRITE_10: case WRITE_12:
793 set_bit (PC_WRITING, &pc->flags);
800 static inline struct bio *idescsi_dma_bio(ide_drive_t *drive, idescsi_pc_t *pc)
802 struct bio *bh = NULL, *first_bh = NULL;
803 int segments = pc->scsi_cmd->use_sg;
804 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
806 if (!drive->using_dma || !pc->request_transfer || pc->request_transfer % 1024)
808 if (idescsi_set_direction(pc))
811 if ((first_bh = bh = idescsi_kmalloc_bio (segments)) == NULL)
813 #if IDESCSI_DEBUG_LOG
814 printk ("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive->name, segments, pc->request_transfer >> 10);
815 #endif /* IDESCSI_DEBUG_LOG */
817 bh->bi_io_vec[0].bv_page = sg->page;
818 bh->bi_io_vec[0].bv_len = sg->length;
819 bh->bi_io_vec[0].bv_offset = sg->offset;
820 bh->bi_size = sg->length;
825 if ((first_bh = bh = idescsi_kmalloc_bio (1)) == NULL)
827 #if IDESCSI_DEBUG_LOG
828 printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive->name, pc->request_transfer >> 10);
829 #endif /* IDESCSI_DEBUG_LOG */
830 bh->bi_io_vec[0].bv_page = virt_to_page(pc->scsi_cmd->request_buffer);
831 bh->bi_io_vec[0].bv_offset = offset_in_page(pc->scsi_cmd->request_buffer);
832 bh->bi_io_vec[0].bv_len = pc->request_transfer;
833 bh->bi_size = pc->request_transfer;
838 static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
840 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
842 /* this was a layering violation and we can't support it
845 struct gendisk *disk = cmd->request->rq_disk;
848 struct Scsi_Device_Template **p = disk->private_data;
849 if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
850 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
853 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
856 static int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
858 struct Scsi_Host *host = cmd->device->host;
859 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
860 ide_drive_t *drive = scsi->drive;
861 struct request *rq = NULL;
862 idescsi_pc_t *pc = NULL;
865 printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->device->id);
868 scsi = drive_to_idescsi(drive);
869 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
870 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
871 if (rq == NULL || pc == NULL) {
872 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
876 memset (pc->c, 0, 12);
879 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
882 pc->sg = cmd->request_buffer;
884 pc->buffer = cmd->request_buffer;
888 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
891 pc->timeout = jiffies + cmd->timeout_per_command;
893 if (should_transform(drive, cmd))
894 set_bit(PC_TRANSFORM, &pc->flags);
895 idescsi_transform_pc1 (drive, pc);
897 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
898 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
899 hexdump(cmd->cmnd, cmd->cmd_len);
900 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
901 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
906 ide_init_drive_cmd (rq);
907 rq->special = (char *) pc;
908 rq->bio = idescsi_dma_bio (drive, pc);
909 rq->flags = REQ_SPECIAL;
910 spin_unlock_irq(host->host_lock);
911 (void) ide_do_drive_cmd (drive, rq, ide_end);
912 spin_lock_irq(host->host_lock);
917 cmd->result = DID_ERROR << 16;
922 static int idescsi_eh_abort (Scsi_Cmnd *cmd)
924 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
925 ide_drive_t *drive = scsi->drive;
929 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
931 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
932 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
935 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
940 /* First give it some more time, how much is "right" is hard to say :-( */
942 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
943 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
944 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
946 spin_lock_irq(&ide_lock);
948 /* If there is no pc running we're done (our interrupt took care of it) */
954 /* It's somewhere in flight. Does ide subsystem agree? */
955 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
956 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
958 * FIXME - not sure this condition can ever occur
960 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
962 idescsi_free_bio(scsi->pc->rq->bio);
963 if (scsi->pc->rq->flags & REQ_SENSE)
964 kfree(scsi->pc->buffer);
973 spin_unlock_irq(&ide_lock);
975 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
976 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
981 static int idescsi_eh_reset (Scsi_Cmnd *cmd)
984 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
985 ide_drive_t *drive = scsi->drive;
989 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
991 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
992 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
995 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
1000 spin_lock_irq(&ide_lock);
1002 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
1003 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
1004 spin_unlock(&ide_lock);
1008 /* kill current request */
1009 blkdev_dequeue_request(req);
1010 end_that_request_last(req);
1011 idescsi_free_bio(req->bio);
1012 if (req->flags & REQ_SENSE)
1013 kfree(scsi->pc->buffer);
1018 /* now nuke the drive queue */
1019 while ((req = elv_next_request(drive->queue))) {
1020 blkdev_dequeue_request(req);
1021 end_that_request_last(req);
1024 HWGROUP(drive)->rq = NULL;
1025 HWGROUP(drive)->handler = NULL;
1026 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
1027 spin_unlock_irq(&ide_lock);
1029 ide_do_reset(drive);
1031 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1034 set_current_state(TASK_UNINTERRUPTIBLE);
1035 spin_unlock_irq(cmd->device->host->host_lock);
1036 schedule_timeout(HZ/20);
1037 spin_lock_irq(cmd->device->host->host_lock);
1038 } while ( HWGROUP(drive)->handler );
1040 ready = drive_is_ready(drive);
1041 HWGROUP(drive)->busy--;
1043 printk (KERN_ERR "ide-scsi: reset failed!\n");
1050 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
1051 sector_t capacity, int *parm)
1053 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
1054 ide_drive_t *drive = idescsi->drive;
1056 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1057 parm[0] = drive->bios_head;
1058 parm[1] = drive->bios_sect;
1059 parm[2] = drive->bios_cyl;
1064 static Scsi_Host_Template idescsi_template = {
1065 .module = THIS_MODULE,
1067 .info = idescsi_info,
1068 .slave_configure = idescsi_slave_configure,
1069 .ioctl = idescsi_ioctl,
1070 .queuecommand = idescsi_queue,
1071 .eh_abort_handler = idescsi_eh_abort,
1072 .eh_host_reset_handler = idescsi_eh_reset,
1073 .bios_param = idescsi_bios,
1076 .sg_tablesize = 256,
1079 .use_clustering = DISABLE_CLUSTERING,
1081 .proc_name = "ide-scsi",
1084 static int idescsi_attach(ide_drive_t *drive)
1086 idescsi_scsi_t *idescsi;
1087 struct Scsi_Host *host;
1091 if (!warned && drive->media == ide_cdrom) {
1092 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1096 if (!strstr("ide-scsi", drive->driver_req) ||
1098 drive->media == ide_disk ||
1099 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1104 #if IDESCSI_DEBUG_LOG
1105 if (drive->id->last_lun)
1106 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1108 if ((drive->id->last_lun & 0x7) != 7)
1109 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1113 drive->driver_data = host;
1114 idescsi = scsihost_to_idescsi(host);
1115 idescsi->drive = drive;
1116 err = ide_register_subdriver(drive, &idescsi_driver);
1118 idescsi_setup (drive, idescsi);
1119 drive->disk->fops = &idescsi_ops;
1120 err = scsi_add_host(host, &drive->gendev);
1122 scsi_scan_host(host);
1125 /* fall through on error */
1126 ide_unregister_subdriver(drive);
1129 scsi_host_put(host);
1133 static int __init init_idescsi_module(void)
1135 return ide_register_driver(&idescsi_driver);
1138 static void __exit exit_idescsi_module(void)
1140 ide_unregister_driver(&idescsi_driver);
1143 module_init(init_idescsi_module);
1144 module_exit(exit_idescsi_module);
1145 MODULE_LICENSE("GPL");