2 * linux/drivers/scsi/ide-scsi.c Version 0.93 June 10, 2002
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2001 - 2002 Andre Hedrick <andre@linux-ide.org>
9 * Emulation of a SCSI host adapter for IDE ATAPI devices.
11 * With this driver, one can use the Linux SCSI drivers instead of the
12 * native IDE ATAPI drivers.
14 * Ver 0.1 Dec 3 96 Initial version.
15 * Ver 0.2 Jan 26 97 Fixed bug in cleanup_module() and added emulation
16 * of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
17 * to Janos Farkas for pointing this out.
18 * Avoid using bitfields in structures for m68k.
19 * Added Scatter/Gather and DMA support.
20 * Ver 0.4 Dec 7 97 Add support for ATAPI PD/CD drives.
21 * Use variable timeout for each command.
22 * Ver 0.5 Jan 2 98 Fix previous PD/CD support.
23 * Allow disabling of SCSI-6 to SCSI-10 transformation.
24 * Ver 0.6 Jan 27 98 Allow disabling of SCSI command translation layer
25 * for access through /dev/sg.
26 * Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
27 * Ver 0.7 Dec 04 98 Ignore commands where lun != 0 to avoid multiple
28 * detection of devices with CONFIG_SCSI_MULTI_LUN
29 * Ver 0.8 Feb 05 99 Optical media need translation too. Reverse 0.7.
30 * Ver 0.9 Jul 04 99 Fix a bug in SG_SET_TRANSFORM.
31 * Ver 0.91 Jan 06 02 Added 'ignore' parameter when ide-scsi is a module
32 * so that use of scsi emulation can be made independent
33 * of load order when other IDE drivers are modules.
34 * Chris Ebenezer <chriseb@pobox.com>
35 * Ver 0.92 Mar 21 02 Include DevFs support
36 * Borsenkow Andrej <Andrej.Borsenkow@mow.siemens.ru>
37 * Ver 0.93 Jun 10 02 Fix "off by one" error in transforms
40 #define IDESCSI_VERSION "0.93"
42 #include <linux/module.h>
43 #include <linux/config.h>
44 #include <linux/types.h>
45 #include <linux/string.h>
46 #include <linux/kernel.h>
48 #include <linux/ioport.h>
49 #include <linux/blkdev.h>
50 #include <linux/errno.h>
51 #include <linux/hdreg.h>
52 #include <linux/slab.h>
53 #include <linux/ide.h>
56 #include <asm/bitops.h>
57 #include <asm/uaccess.h>
65 #define IDESCSI_DEBUG_LOG 0
67 typedef struct idescsi_pc_s {
68 u8 c[12]; /* Actual packet bytes */
69 int request_transfer; /* Bytes to transfer */
70 int actually_transferred; /* Bytes actually transferred */
71 int buffer_size; /* Size of our data buffer */
72 struct request *rq; /* The corresponding request */
73 u8 *buffer; /* Data buffer */
74 u8 *current_position; /* Pointer into the above buffer */
75 struct scatterlist *sg; /* Scatter gather table */
76 int b_count; /* Bytes transferred from current entry */
77 Scsi_Cmnd *scsi_cmd; /* SCSI command */
78 void (*done)(Scsi_Cmnd *); /* Scsi completion routine */
79 unsigned long flags; /* Status/Action flags */
80 unsigned long timeout; /* Command timeout */
84 * Packet command status bits.
86 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
87 #define PC_WRITING 1 /* Data direction */
88 #define PC_TRANSFORM 2 /* transform SCSI commands */
89 #define PC_DMA_OK 4 /* Use DMA */
92 * SCSI command transformation layer
94 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
95 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
100 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
102 #define IDESCSI_DEVFS
106 idescsi_pc_t *pc; /* Current packet command */
107 unsigned long flags; /* Status/Action flags */
108 unsigned long transform; /* SCSI cmd translation layer */
109 unsigned long log; /* log flags */
112 devfs_handle_t de; /* pointer to IDE device */
113 #endif /* IDESCSI_DEVFS */
117 * Per ATAPI device status bits.
119 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
124 #define IDESCSI_PC_RQ 90
126 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
129 (void) HWIF(drive)->INB(IDE_DATA_REG);
132 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
135 HWIF(drive)->OUTB(0, IDE_DATA_REG);
139 * PIO data transfer routines using the scatter gather table.
141 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
146 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
147 printk(KERN_ERR "ide-scsi: scatter gather "
148 "table too small, discarding data\n");
149 idescsi_discard_data(drive, bcount);
152 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
153 HWIF(drive)->atapi_input_bytes(drive, pc->sg->address + pc->b_count, count);
155 pc->b_count += count;
156 if (pc->b_count == pc->sg->length) {
163 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
168 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
169 printk(KERN_ERR "ide-scsi: scatter gather table "
170 "too small, padding with zeros\n");
171 idescsi_output_zeros(drive, bcount);
174 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
175 HWIF(drive)->atapi_output_bytes(drive, pc->sg->address + pc->b_count, count);
177 pc->b_count += count;
178 if (pc->b_count == pc->sg->length) {
186 * Most of the SCSI commands are supported directly by ATAPI devices.
187 * idescsi_transform_pc handles the few exceptions.
189 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
191 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
194 if (!test_bit(PC_TRANSFORM, &pc->flags))
196 if (drive->media == ide_cdrom || drive->media == ide_optical) {
197 if (c[0] == READ_6 || c[0] == WRITE_6) {
204 c[0] += (READ_10 - READ_6);
206 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
207 unsigned short new_len;
210 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
212 memset(atapi_buf, 0, pc->buffer_size + 4);
221 if (c[0] == MODE_SELECT_10) {
222 /* Mode data length */
223 atapi_buf[1] = scsi_buf[0];
225 atapi_buf[2] = scsi_buf[1];
226 /* Device specific parameter */
227 atapi_buf[3] = scsi_buf[2];
228 /* Block descriptor length */
229 atapi_buf[7] = scsi_buf[3];
230 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
232 pc->buffer = atapi_buf;
233 pc->request_transfer += 4;
234 pc->buffer_size += 4;
239 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
241 u8 *atapi_buf = pc->buffer;
242 u8 *sc = pc->scsi_cmd->cmnd;
243 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
245 if (!test_bit(PC_TRANSFORM, &pc->flags))
247 if (drive->media == ide_cdrom || drive->media == ide_optical) {
248 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
249 /* Mode data length */
250 scsi_buf[0] = atapi_buf[1];
252 scsi_buf[1] = atapi_buf[2];
253 /* Device specific parameter */
254 scsi_buf[2] = atapi_buf[3];
255 /* Block descriptor length */
256 scsi_buf[3] = atapi_buf[7];
257 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
259 if (pc->c[0] == INQUIRY) {
262 /* response data format */
263 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
266 if (atapi_buf && atapi_buf != scsi_buf)
270 static inline void idescsi_free_bh (struct buffer_head *bh)
272 struct buffer_head *bhp;
281 static void hexdump(u8 *x, int len)
286 for (i = 0; i < len; i++)
291 static int idescsi_do_end_request (ide_drive_t *drive, int uptodate)
297 spin_lock_irqsave(&io_request_lock, flags);
298 rq = HWGROUP(drive)->rq;
301 * decide whether to reenable DMA -- 3 is a random magic for now,
302 * if we DMA timeout more than 3 times, just stay in PIO
304 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
306 HWGROUP(drive)->hwif->ide_dma_on(drive);
309 if (!end_that_request_first(rq, uptodate, drive->name)) {
310 add_blkdev_randomness(MAJOR(rq->rq_dev));
311 blkdev_dequeue_request(rq);
312 HWGROUP(drive)->rq = NULL;
313 end_that_request_last(rq);
316 spin_unlock_irqrestore(&io_request_lock, flags);
320 static int idescsi_end_request (ide_drive_t *drive, int uptodate)
322 idescsi_scsi_t *scsi = drive->driver_data;
323 struct request *rq = HWGROUP(drive)->rq;
324 idescsi_pc_t *pc = (idescsi_pc_t *) rq->buffer;
325 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
329 if (rq->cmd != IDESCSI_PC_RQ) {
330 idescsi_do_end_request(drive, uptodate);
333 ide_end_drive_cmd(drive, 0, 0);
334 if (rq->errors >= ERROR_MAX) {
335 pc->scsi_cmd->result = DID_ERROR << 16;
337 printk("ide-scsi: %s: I/O error for %lu\n",
338 drive->name, pc->scsi_cmd->serial_number);
339 } else if (rq->errors) {
340 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
342 printk("ide-scsi: %s: check condition for %lu\n",
343 drive->name, pc->scsi_cmd->serial_number);
345 pc->scsi_cmd->result = DID_OK << 16;
346 idescsi_transform_pc2(drive, pc);
348 printk("ide-scsi: %s: suc %lu", drive->name,
349 pc->scsi_cmd->serial_number);
350 if (!test_bit(PC_WRITING, &pc->flags) &&
351 pc->actually_transferred &&
352 pc->actually_transferred <= 1024 &&
355 scsi_buf = pc->scsi_cmd->request_buffer;
356 hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen));
360 spin_lock_irqsave(&io_request_lock, flags);
361 pc->done(pc->scsi_cmd);
362 spin_unlock_irqrestore(&io_request_lock, flags);
363 idescsi_free_bh(rq->bh);
370 static inline unsigned long get_timeout(idescsi_pc_t *pc)
372 return IDE_MAX(WAIT_CMD, pc->timeout - jiffies);
376 * Our interrupt handler.
378 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
380 idescsi_scsi_t *scsi = drive->driver_data;
381 idescsi_pc_t *pc = scsi->pc;
382 struct request *rq = pc->rq;
383 atapi_bcount_t bcount;
384 atapi_status_t status;
385 atapi_ireason_t ireason;
386 atapi_feature_t feature;
389 #if IDESCSI_DEBUG_LOG
390 printk(KERN_INFO "ide-scsi: Reached idescsi_pc_intr "
391 "interrupt handler\n");
392 #endif /* IDESCSI_DEBUG_LOG */
394 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
395 #if IDESCSI_DEBUG_LOG
396 printk("ide-scsi: %s: DMA complete\n", drive->name);
397 #endif /* IDESCSI_DEBUG_LOG */
398 pc->actually_transferred = pc->request_transfer;
399 (void) (HWIF(drive)->ide_dma_end(drive));
403 /* Clear the interrupt */
404 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
407 /* No more interrupts */
408 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
409 printk(KERN_INFO "Packet command completed, %d "
410 "bytes transferred\n",
411 pc->actually_transferred);
415 idescsi_end_request(drive, 1);
419 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
420 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
421 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
424 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
425 return ide_do_reset(drive);
428 temp = pc->actually_transferred + bcount.all;
429 if (temp > pc->request_transfer) {
430 if (temp > pc->buffer_size) {
431 printk(KERN_ERR "ide-scsi: The scsi wants to "
432 "send us more data than expected "
433 "- discarding data\n");
434 printk(KERN_ERR "ide-scsi: [");
437 printk(KERN_ERR "ide-scsi: expected %d got %d limit %d\n",
438 pc->request_transfer, temp, pc->buffer_size);
439 temp = pc->buffer_size - pc->actually_transferred;
441 clear_bit(PC_WRITING, &pc->flags);
443 idescsi_input_buffers(drive, pc, temp);
445 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, temp);
446 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
448 pc->actually_transferred += temp;
449 pc->current_position += temp;
450 idescsi_discard_data(drive, bcount.all - temp);
451 if (HWGROUP(drive)->handler != NULL)
453 ide_set_handler(drive,
459 #if IDESCSI_DEBUG_LOG
460 printk(KERN_NOTICE "ide-scsi: The scsi wants to send "
461 "us more data than expected - "
462 "allowing transfer\n");
463 #endif /* IDESCSI_DEBUG_LOG */
467 clear_bit(PC_WRITING, &pc->flags);
469 idescsi_input_buffers(drive, pc, bcount.all);
471 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
473 set_bit(PC_WRITING, &pc->flags);
475 idescsi_output_buffers(drive, pc, bcount.all);
477 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
479 /* Update the current position */
480 pc->actually_transferred += bcount.all;
481 pc->current_position += bcount.all;
483 if (HWGROUP(drive)->handler != NULL)
485 /* And set the interrupt handler again */
486 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
490 static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive)
492 idescsi_scsi_t *scsi = drive->driver_data;
493 idescsi_pc_t *pc = scsi->pc;
494 atapi_ireason_t ireason;
495 ide_startstop_t startstop;
497 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
498 printk(KERN_ERR "ide-scsi: Strange, packet command "
499 "initiated yet DRQ isn't asserted\n");
503 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
505 if (!ireason.b.cod || ireason.b.io) {
506 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
507 "issuing a packet command\n");
508 return ide_do_reset(drive);
511 if (HWGROUP(drive)->handler != NULL)
513 /* Set the interrupt routine */
514 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
515 /* Send the actual packet */
516 HWIF(drive)->atapi_output_bytes(drive, scsi->pc->c, 12);
517 if (test_bit (PC_DMA_OK, &pc->flags)) {
518 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
519 (void) (HWIF(drive)->ide_dma_begin(drive));
525 * Issue a packet command
527 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
529 idescsi_scsi_t *scsi = drive->driver_data;
530 atapi_feature_t feature;
531 atapi_bcount_t bcount;
532 struct request *rq = pc->rq;
534 /* Set the current packet command */
536 /* We haven't transferred any data yet */
537 pc->actually_transferred = 0;
538 pc->current_position = pc->buffer;
539 /* Request to transfer the entire buffer at once */
540 bcount.all = IDE_MIN(pc->request_transfer, 63 * 1024);
542 if (drive->using_dma && rq->bh) {
543 if (test_bit(PC_WRITING, &pc->flags))
544 feature.b.dma = !HWIF(drive)->ide_dma_write(drive);
546 feature.b.dma = !HWIF(drive)->ide_dma_read(drive);
551 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
552 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
553 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
554 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
557 set_bit(PC_DMA_OK, &pc->flags);
559 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
560 if (HWGROUP(drive)->handler != NULL)
562 ide_set_handler(drive,
563 &idescsi_transfer_pc,
566 /* Issue the packet command */
567 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
570 /* Issue the packet command */
571 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
572 return idescsi_transfer_pc(drive);
577 * idescsi_do_request is our request handling function.
579 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
581 #if IDESCSI_DEBUG_LOG
582 printk(KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",
583 rq->rq_status, (unsigned int) rq->rq_dev, rq->cmd, rq->errors);
584 printk(KERN_INFO "sector: %ld, nr_sectors: %ld, "
585 "current_nr_sectors: %ld\n", rq->sector,
586 rq->nr_sectors, rq->current_nr_sectors);
587 #endif /* IDESCSI_DEBUG_LOG */
589 if (rq->cmd == IDESCSI_PC_RQ) {
590 return idescsi_issue_pc(drive, (idescsi_pc_t *) rq->buffer);
592 printk(KERN_ERR "ide-scsi: %s: unsupported command in request "
593 "queue (%x)\n", drive->name, rq->cmd);
594 idescsi_end_request(drive, 0);
598 static int idescsi_do_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
600 /* need to figure out how to parse scsi-atapi media type */
605 static int idescsi_ide_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
611 static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
616 static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
617 static int idescsi_initialized = 0;
618 static int drive_count = 0;
620 static void idescsi_add_settings(ide_drive_t *drive)
622 idescsi_scsi_t *scsi = drive->driver_data;
625 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
627 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
628 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
629 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
630 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
631 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
635 * Driver initialization.
637 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id)
639 int minor = (drive->select.b.unit) << PARTN_BITS;
641 DRIVER(drive)->busy++;
642 idescsi_drives[id] = drive;
643 drive->driver_data = scsi;
644 drive->ready_stat = 0;
645 memset(scsi, 0, sizeof(idescsi_scsi_t));
648 if (drive->id && (drive->id->config & 0x0060) == 0x20)
649 set_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags);
650 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
651 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
652 #if IDESCSI_DEBUG_LOG
653 set_bit(IDESCSI_LOG_CMD, &scsi->log);
654 #endif /* IDESCSI_DEBUG_LOG */
655 idescsi_add_settings(drive);
657 scsi->de = devfs_register(drive->de, "generic", DEVFS_FL_DEFAULT,
658 HWIF(drive)->major, minor,
659 S_IFBLK | S_IRUSR | S_IWUSR,
661 #endif /* IDESCSI_DEVFS */
663 DRIVER(drive)->busy--;
666 static int idescsi_cleanup (ide_drive_t *drive)
668 idescsi_scsi_t *scsi = drive->driver_data;
670 if (ide_unregister_subdriver(drive)) {
671 printk("%s: %s: failed to unregister! \n",
672 __FUNCTION__, drive->name);
673 printk("%s: usage %d, busy %d, driver %p, Dbusy %d\n",
674 drive->name, drive->usage, drive->busy,
675 drive->driver, DRIVER(drive)->busy);
678 idescsi_drives[scsi->id] = NULL;
681 devfs_unregister(scsi->de);
682 #endif /* IDESCSI_DEVFS */
683 drive->driver_data = NULL;
689 int idescsi_init(void);
690 int idescsi_attach(ide_drive_t *drive);
693 * IDE subdriver functions, registered with ide.c
695 static ide_driver_t idescsi_driver = {
697 version: IDESCSI_VERSION,
700 #ifdef CONFIG_IDEDMA_ONLYDISK
705 supports_dsc_overlap: 0,
706 cleanup: idescsi_cleanup,
711 do_request: idescsi_do_request,
712 end_request: idescsi_end_request,
715 ioctl: idescsi_do_ioctl,
716 open: idescsi_ide_open,
717 release: idescsi_ide_release,
725 attach: idescsi_attach,
726 ata_prebuilder: NULL,
727 atapi_prebuilder: NULL,
730 static ide_module_t idescsi_module = {
737 int idescsi_attach (ide_drive_t *drive)
739 idescsi_scsi_t *scsi;
740 u8 media[] = { TYPE_DISK, /* 0x00 */
741 TYPE_TAPE, /* 0x01 */
742 TYPE_PRINTER, /* 0x02 */
743 TYPE_PROCESSOR, /* 0x03 */
744 TYPE_WORM, /* 0x04 */
746 TYPE_SCANNER, /* 0x06 */
749 int i = 0, ret = 0, id = 0;
750 // int id = 2 * HWIF(drive)->index + drive->select.b.unit;
751 // int id = drive_count + 1;
753 for (id = 0; id < MAX_HWIFS*MAX_DRIVES; id++)
754 if (idescsi_drives[id] == NULL)
757 printk("%s: id = %d\n", drive->name, id);
759 if ((!idescsi_initialized) || (drive->media == ide_disk)) {
760 printk(KERN_ERR "ide-scsi: (%sinitialized) %s: "
761 "media-type (%ssupported)\n",
762 (idescsi_initialized) ? "" : "! ",
764 (drive->media == ide_disk) ? "! " : "");
765 return (drive->media == ide_disk) ? 2 : 0;
770 for (i = 0; media[i] != 255; i++) {
771 if (drive->media != media[i])
777 if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
778 printk(KERN_ERR "ide-scsi: %s: Can't allocate a scsi "
779 "structure\n", drive->name);
783 if (ide_register_subdriver(drive, &idescsi_driver,
784 IDE_SUBDRIVER_VERSION)) {
785 printk(KERN_ERR "ide-scsi: %s: Failed to register the "
786 "driver with ide.c\n", drive->name);
792 idescsi_setup(drive, scsi, id);
794 // scan_scsis(HBA, 1, channel, id, lun);
804 MODULE_PARM(ignore, "s");
807 int idescsi_init (void)
809 #ifdef CLASSIC_BUILTINS_METHOD
811 idescsi_scsi_t *scsi;
812 u8 media[] = { TYPE_DISK, /* 0x00 */
813 TYPE_TAPE, /* 0x01 */
814 TYPE_PRINTER, /* 0x02 */
815 TYPE_PROCESSOR, /* 0x03 */
816 TYPE_WORM, /* 0x04 */
818 TYPE_SCANNER, /* 0x06 */
824 if (idescsi_initialized)
826 idescsi_initialized = 1;
827 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
828 idescsi_drives[i] = NULL;
830 for (i = 0; media[i] != 255; i++) {
832 while ((drive = ide_scan_devices(media[i],
833 idescsi_driver.name, NULL, failed++)) != NULL) {
835 /* skip drives we were told to ignore */
836 if (ignore != NULL && strstr(ignore, drive->name)) {
837 printk("ide-scsi: ignoring drive %s\n",
843 if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
844 printk(KERN_ERR "ide-scsi: %s: Can't allocate "
845 "a scsi structure\n", drive->name);
848 if (ide_register_subdriver(drive, &idescsi_driver,
849 IDE_SUBDRIVER_VERSION)) {
850 printk(KERN_ERR "ide-scsi: %s: Failed to "
851 "register the driver with ide.c\n",
857 id < MAX_HWIFS*MAX_DRIVES && idescsi_drives[id];
859 idescsi_setup(drive, scsi, id);
863 #else /* ! CLASSIC_BUILTINS_METHOD */
866 if (idescsi_initialized)
868 idescsi_initialized = 1;
869 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
870 idescsi_drives[i] = NULL;
872 #endif /* CLASSIC_BUILTINS_METHOD */
873 ide_register_module(&idescsi_module);
878 int idescsi_detect (Scsi_Host_Template *host_template)
880 struct Scsi_Host *host;
884 host_template->proc_name = "ide-scsi";
885 host = scsi_register(host_template, 0);
887 printk(KERN_WARNING "%s: host failure!\n", __FUNCTION__);
891 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
892 last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
894 host->max_lun = last_lun + 1;
895 host->can_queue = host->cmd_per_lun * id;
899 int idescsi_release (struct Scsi_Host *host)
904 for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) {
905 drive = idescsi_drives[id];
907 DRIVER(drive)->busy = 0;
912 const char *idescsi_info (struct Scsi_Host *host)
914 return "SCSI host adapter emulation for IDE ATAPI devices";
917 int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
919 ide_drive_t *drive = idescsi_drives[dev->id];
920 idescsi_scsi_t *scsi = drive->driver_data;
922 if (cmd == SG_SET_TRANSFORM) {
924 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
926 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
928 } else if (cmd == SG_GET_TRANSFORM)
929 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
933 static inline struct buffer_head *idescsi_kmalloc_bh (int count)
935 struct buffer_head *bh, *bhp, *first_bh;
937 if ((first_bh = bhp = bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
939 memset(bh, 0, sizeof(struct buffer_head));
940 bh->b_reqnext = NULL;
942 if ((bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
944 memset(bh, 0, sizeof(struct buffer_head));
947 bh->b_reqnext = NULL;
951 idescsi_free_bh(first_bh);
955 static inline int idescsi_set_direction (idescsi_pc_t *pc)
961 clear_bit(PC_WRITING, &pc->flags);
966 set_bit(PC_WRITING, &pc->flags);
973 static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc)
975 struct buffer_head *bh = NULL, *first_bh = NULL;
976 int segments = pc->scsi_cmd->use_sg;
977 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
979 if (!drive->using_dma || !pc->request_transfer || pc->request_transfer & 1023)
981 if (idescsi_set_direction(pc))
984 if ((first_bh = bh = idescsi_kmalloc_bh(segments)) == NULL)
986 #if IDESCSI_DEBUG_LOG
987 printk("ide-scsi: %s: building DMA table, %d segments, "
988 "%dkB total\n", drive->name, segments,
989 pc->request_transfer >> 10);
990 #endif /* IDESCSI_DEBUG_LOG */
993 bh->b_data = sg->address;
996 bh->b_page = virt_to_page(sg->address);
997 bh->b_data = (char *) ((unsigned long) sg->address & ~PAGE_MASK);
998 } else if (sg->page) {
999 bh->b_page = sg->page;
1000 bh->b_data = (char *) sg->offset;
1003 bh->b_size = sg->length;
1009 * non-sg requests are guarenteed not to reside in highmem /jens
1011 if ((first_bh = bh = idescsi_kmalloc_bh(1)) == NULL)
1013 #if IDESCSI_DEBUG_LOG
1014 printk("ide-scsi: %s: building DMA table for a single "
1015 "buffer (%dkB)\n", drive->name,
1016 pc->request_transfer >> 10);
1017 #endif /* IDESCSI_DEBUG_LOG */
1018 bh->b_data = pc->scsi_cmd->request_buffer;
1019 bh->b_size = pc->request_transfer;
1024 static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
1026 idescsi_scsi_t *scsi = drive->driver_data;
1028 if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
1029 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
1030 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
1033 int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
1035 ide_drive_t *drive = idescsi_drives[cmd->target];
1036 idescsi_scsi_t *scsi;
1037 struct request *rq = NULL;
1038 idescsi_pc_t *pc = NULL;
1041 printk(KERN_ERR "ide-scsi: drive id %d not present\n",
1045 scsi = drive->driver_data;
1046 pc = kmalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
1047 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
1048 if (rq == NULL || pc == NULL) {
1049 printk(KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
1053 memset(pc->c, 0, 12);
1056 memcpy(pc->c, cmd->cmnd, cmd->cmd_len);
1059 pc->sg = cmd->request_buffer;
1061 pc->buffer = cmd->request_buffer;
1065 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
1068 pc->timeout = jiffies + cmd->timeout_per_command;
1070 if (should_transform(drive, cmd))
1071 set_bit(PC_TRANSFORM, &pc->flags);
1072 idescsi_transform_pc1(drive, pc);
1074 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
1075 printk("ide-scsi: %s: que %lu, cmd = ",
1076 drive->name, cmd->serial_number);
1077 hexdump(cmd->cmnd, cmd->cmd_len);
1078 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
1079 printk("ide-scsi: %s: que %lu, tsl = ",
1080 drive->name, cmd->serial_number);
1085 ide_init_drive_cmd(rq);
1086 rq->buffer = (char *) pc;
1087 rq->bh = idescsi_dma_bh(drive, pc);
1088 rq->cmd = IDESCSI_PC_RQ;
1089 spin_unlock_irq(&io_request_lock);
1090 (void) ide_do_drive_cmd(drive, rq, ide_end);
1091 spin_lock_irq(&io_request_lock);
1096 cmd->result = DID_ERROR << 16;
1101 int idescsi_abort (Scsi_Cmnd *cmd)
1103 return SCSI_ABORT_SNOOZE;
1106 int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags)
1108 ide_drive_t *drive = idescsi_drives[cmd->target];
1110 (void) ide_do_reset(drive);
1111 return SCSI_RESET_SUCCESS;
1114 int idescsi_bios (Disk *disk, kdev_t dev, int *parm)
1116 ide_drive_t *drive = idescsi_drives[disk->device->id];
1118 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1119 parm[0] = drive->bios_head;
1120 parm[1] = drive->bios_sect;
1121 parm[2] = drive->bios_cyl;
1126 static Scsi_Host_Template idescsi_template = IDESCSI;
1128 static int __init init_idescsi_module(void)
1132 idescsi_template.module = THIS_MODULE;
1133 scsi_register_module(MODULE_SCSI_HA, &idescsi_template);
1137 static void __exit exit_idescsi_module(void)
1140 u8 media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
1143 scsi_unregister_module(MODULE_SCSI_HA, &idescsi_template);
1144 for (i = 0; media[i] != 255; i++) {
1146 while ((drive = ide_scan_devices(media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
1147 if (idescsi_cleanup(drive)) {
1148 printk("%s: exit_idescsi_module() called while still busy\n", drive->name);
1152 ide_unregister_module(&idescsi_module);
1155 module_init(init_idescsi_module);
1156 module_exit(exit_idescsi_module);
1157 MODULE_LICENSE("GPL");