2 * linux/drivers/scsi/ide-scsi.c Version 0.94 Sept 09, 2003
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
38 * Ver 0.94 Sep 09 03 Added transform for reading ATAPI tape drive block
39 * limits (ATAPI tapes report block limits in mode
40 * page 0x2A, not by "read block limits" command)
43 #define IDESCSI_VERSION "0.94"
45 #include <linux/module.h>
46 #include <linux/config.h>
47 #include <linux/types.h>
48 #include <linux/string.h>
49 #include <linux/kernel.h>
51 #include <linux/ioport.h>
52 #include <linux/blkdev.h>
53 #include <linux/errno.h>
54 #include <linux/hdreg.h>
55 #include <linux/slab.h>
56 #include <linux/ide.h>
59 #include <asm/bitops.h>
60 #include <asm/uaccess.h>
68 #define IDESCSI_DEBUG_LOG 0
70 typedef struct idescsi_pc_s {
71 u8 c[12]; /* Actual packet bytes */
72 int request_transfer; /* Bytes to transfer */
73 int actually_transferred; /* Bytes actually transferred */
74 int buffer_size; /* Size of our data buffer */
75 struct request *rq; /* The corresponding request */
76 u8 *buffer; /* Data buffer */
77 u8 *current_position; /* Pointer into the above buffer */
78 struct scatterlist *sg; /* Scatter gather table */
79 int b_count; /* Bytes transferred from current entry */
80 Scsi_Cmnd *scsi_cmd; /* SCSI command */
81 void (*done)(Scsi_Cmnd *); /* Scsi completion routine */
82 unsigned long flags; /* Status/Action flags */
83 unsigned long timeout; /* Command timeout */
87 * Packet command status bits.
89 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
90 #define PC_WRITING 1 /* Data direction */
91 #define PC_TRANSFORM 2 /* transform SCSI commands */
92 #define PC_DMA_OK 4 /* Use DMA */
95 * SCSI command transformation layer
97 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
98 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
103 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
105 #define IDESCSI_DEVFS
109 idescsi_pc_t *pc; /* Current packet command */
110 unsigned long flags; /* Status/Action flags */
111 unsigned long transform; /* SCSI cmd translation layer */
112 unsigned long log; /* log flags */
115 devfs_handle_t de; /* pointer to IDE device */
116 #endif /* IDESCSI_DEVFS */
120 * Per ATAPI device status bits.
122 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
127 #define IDESCSI_PC_RQ 90
129 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
132 (void) HWIF(drive)->INB(IDE_DATA_REG);
135 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
138 HWIF(drive)->OUTB(0, IDE_DATA_REG);
142 * PIO data transfer routines using the scatter gather table.
144 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
149 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
150 printk(KERN_ERR "ide-scsi: scatter gather "
151 "table too small, discarding data\n");
152 idescsi_discard_data(drive, bcount);
155 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
156 HWIF(drive)->atapi_input_bytes(drive, pc->sg->address + pc->b_count, count);
158 pc->b_count += count;
159 if (pc->b_count == pc->sg->length) {
166 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
171 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
172 printk(KERN_ERR "ide-scsi: scatter gather table "
173 "too small, padding with zeros\n");
174 idescsi_output_zeros(drive, bcount);
177 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
178 HWIF(drive)->atapi_output_bytes(drive, pc->sg->address + pc->b_count, count);
180 pc->b_count += count;
181 if (pc->b_count == pc->sg->length) {
189 * Most of the SCSI commands are supported directly by ATAPI devices.
190 * idescsi_transform_pc handles the few exceptions.
192 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
194 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
197 if (!test_bit(PC_TRANSFORM, &pc->flags))
199 if (drive->media == ide_cdrom || drive->media == ide_optical) {
200 if (c[0] == READ_6 || c[0] == WRITE_6) {
207 c[0] += (READ_10 - READ_6);
209 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
210 unsigned short new_len;
213 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
215 memset(atapi_buf, 0, pc->buffer_size + 4);
224 if (c[0] == MODE_SELECT_10) {
225 /* Mode data length */
226 atapi_buf[1] = scsi_buf[0];
228 atapi_buf[2] = scsi_buf[1];
229 /* Device specific parameter */
230 atapi_buf[3] = scsi_buf[2];
231 /* Block descriptor length */
232 atapi_buf[7] = scsi_buf[3];
233 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
235 pc->buffer = atapi_buf;
236 pc->request_transfer += 4;
237 pc->buffer_size += 4;
240 if (drive->media == ide_tape) {
241 if (sc[0] == READ_BLOCK_LIMITS) { /* IDE tapes have blk lmts in mode page 0x2a */
244 /* buffer size should be 6 for READ_BLOCK_LIMITS */
245 /* we need 12 bytes (4 for header + 8 for mode page */
246 if ((atapi_buf = kmalloc(12, GFP_ATOMIC)) == NULL)
248 memset(atapi_buf, 0, 12);
251 c[1] = 8; /* no block descriptors */
252 c[2] = 0x2A; /* mode page 0x2A */
253 c[4] = 12; /* buffer length 12 decimal */
254 pc->buffer = atapi_buf;
255 pc->request_transfer = 12;
256 pc->buffer_size = 12;
261 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
263 u8 *atapi_buf = pc->buffer;
264 u8 *sc = pc->scsi_cmd->cmnd;
265 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
267 if (!test_bit(PC_TRANSFORM, &pc->flags))
269 if (drive->media == ide_cdrom || drive->media == ide_optical) {
270 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
271 /* Mode data length */
272 scsi_buf[0] = atapi_buf[1];
274 scsi_buf[1] = atapi_buf[2];
275 /* Device specific parameter */
276 scsi_buf[2] = atapi_buf[3];
277 /* Block descriptor length */
278 scsi_buf[3] = atapi_buf[7];
279 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
281 if (pc->c[0] == INQUIRY) {
284 /* response data format */
285 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
288 if (drive->media == ide_tape) {
289 if (sc[0] == READ_BLOCK_LIMITS) {
290 memset(scsi_buf, 0, pc->scsi_cmd->request_bufflen);
291 /* granularity of 9 (always 9 for ide tapes) */
293 /* block length of 1024 bytes supported? */
294 scsi_buf[2] = (atapi_buf[11] & 0x04) ? 4 : 2;
295 /* block length of 512 bytes supported? */
296 scsi_buf[4] = (atapi_buf[11] & 0x02) ? 2 : 4;
300 if (atapi_buf && atapi_buf != scsi_buf)
304 static inline void idescsi_free_bh (struct buffer_head *bh)
306 struct buffer_head *bhp;
315 static void hexdump(u8 *x, int len)
320 for (i = 0; i < len; i++)
325 static int idescsi_do_end_request (ide_drive_t *drive, int uptodate)
331 spin_lock_irqsave(&io_request_lock, flags);
332 rq = HWGROUP(drive)->rq;
335 * decide whether to reenable DMA -- 3 is a random magic for now,
336 * if we DMA timeout more than 3 times, just stay in PIO
338 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
340 HWGROUP(drive)->hwif->ide_dma_on(drive);
343 if (!end_that_request_first(rq, uptodate, drive->name)) {
344 add_blkdev_randomness(MAJOR(rq->rq_dev));
345 blkdev_dequeue_request(rq);
346 HWGROUP(drive)->rq = NULL;
347 end_that_request_last(rq);
350 spin_unlock_irqrestore(&io_request_lock, flags);
354 static int idescsi_end_request (ide_drive_t *drive, int uptodate)
356 idescsi_scsi_t *scsi = drive->driver_data;
357 struct request *rq = HWGROUP(drive)->rq;
358 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
359 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
363 if (rq->cmd != IDESCSI_PC_RQ) {
364 idescsi_do_end_request(drive, uptodate);
367 ide_end_drive_cmd(drive, 0, 0);
368 if (rq->errors >= ERROR_MAX) {
369 pc->scsi_cmd->result = DID_ERROR << 16;
371 printk("ide-scsi: %s: I/O error for %lu\n",
372 drive->name, pc->scsi_cmd->serial_number);
373 } else if (rq->errors) {
374 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
376 printk("ide-scsi: %s: check condition for %lu\n",
377 drive->name, pc->scsi_cmd->serial_number);
379 pc->scsi_cmd->result = DID_OK << 16;
380 idescsi_transform_pc2(drive, pc);
382 printk("ide-scsi: %s: suc %lu", drive->name,
383 pc->scsi_cmd->serial_number);
384 if (!test_bit(PC_WRITING, &pc->flags) &&
385 pc->actually_transferred &&
386 pc->actually_transferred <= 1024 &&
389 scsi_buf = pc->scsi_cmd->request_buffer;
390 hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen));
394 spin_lock_irqsave(&io_request_lock, flags);
395 pc->done(pc->scsi_cmd);
396 spin_unlock_irqrestore(&io_request_lock, flags);
397 idescsi_free_bh(rq->bh);
404 static inline unsigned long get_timeout(idescsi_pc_t *pc)
406 return IDE_MAX(WAIT_CMD, pc->timeout - jiffies);
410 * Our interrupt handler.
412 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
414 idescsi_scsi_t *scsi = drive->driver_data;
415 idescsi_pc_t *pc = scsi->pc;
416 struct request *rq = pc->rq;
417 atapi_bcount_t bcount;
418 atapi_status_t status;
419 atapi_ireason_t ireason;
420 atapi_feature_t feature;
423 #if IDESCSI_DEBUG_LOG
424 printk(KERN_INFO "ide-scsi: Reached idescsi_pc_intr "
425 "interrupt handler\n");
426 #endif /* IDESCSI_DEBUG_LOG */
428 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
429 #if IDESCSI_DEBUG_LOG
430 printk("ide-scsi: %s: DMA complete\n", drive->name);
431 #endif /* IDESCSI_DEBUG_LOG */
432 pc->actually_transferred = pc->request_transfer;
433 (void) (HWIF(drive)->ide_dma_end(drive));
437 /* Clear the interrupt */
438 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
441 /* No more interrupts */
442 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
443 printk(KERN_INFO "Packet command completed, %d "
444 "bytes transferred\n",
445 pc->actually_transferred);
449 idescsi_end_request(drive, 1);
453 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
454 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
455 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
458 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
459 return ide_do_reset(drive);
462 temp = pc->actually_transferred + bcount.all;
463 if (temp > pc->request_transfer) {
464 if (temp > pc->buffer_size) {
465 printk(KERN_ERR "ide-scsi: The scsi wants to "
466 "send us more data than expected "
467 "- discarding data\n");
468 printk(KERN_ERR "ide-scsi: [");
471 printk(KERN_ERR "ide-scsi: expected %d got %d limit %d\n",
472 pc->request_transfer, temp, pc->buffer_size);
473 temp = pc->buffer_size - pc->actually_transferred;
475 clear_bit(PC_WRITING, &pc->flags);
477 idescsi_input_buffers(drive, pc, temp);
479 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, temp);
480 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
482 pc->actually_transferred += temp;
483 pc->current_position += temp;
484 idescsi_discard_data(drive, bcount.all - temp);
485 if (HWGROUP(drive)->handler != NULL)
487 ide_set_handler(drive,
493 #if IDESCSI_DEBUG_LOG
494 printk(KERN_NOTICE "ide-scsi: The scsi wants to send "
495 "us more data than expected - "
496 "allowing transfer\n");
497 #endif /* IDESCSI_DEBUG_LOG */
501 clear_bit(PC_WRITING, &pc->flags);
503 idescsi_input_buffers(drive, pc, bcount.all);
505 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
507 set_bit(PC_WRITING, &pc->flags);
509 idescsi_output_buffers(drive, pc, bcount.all);
511 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
513 /* Update the current position */
514 pc->actually_transferred += bcount.all;
515 pc->current_position += bcount.all;
517 if (HWGROUP(drive)->handler != NULL)
519 /* And set the interrupt handler again */
520 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
524 static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive)
526 idescsi_scsi_t *scsi = drive->driver_data;
527 idescsi_pc_t *pc = scsi->pc;
528 atapi_ireason_t ireason;
529 ide_startstop_t startstop;
531 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
532 printk(KERN_ERR "ide-scsi: Strange, packet command "
533 "initiated yet DRQ isn't asserted\n");
537 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
539 if (!ireason.b.cod || ireason.b.io) {
540 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
541 "issuing a packet command\n");
542 return ide_do_reset(drive);
545 if (HWGROUP(drive)->handler != NULL)
547 /* Set the interrupt routine */
548 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
549 /* Send the actual packet */
550 HWIF(drive)->atapi_output_bytes(drive, scsi->pc->c, 12);
551 if (test_bit (PC_DMA_OK, &pc->flags)) {
552 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
553 (void) (HWIF(drive)->ide_dma_begin(drive));
559 * Issue a packet command
561 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
563 idescsi_scsi_t *scsi = drive->driver_data;
564 atapi_feature_t feature;
565 atapi_bcount_t bcount;
566 struct request *rq = pc->rq;
570 /* Set the current packet command */
572 /* We haven't transferred any data yet */
573 pc->actually_transferred = 0;
574 pc->current_position = pc->buffer;
575 /* Request to transfer the entire buffer at once */
576 bcount.all = IDE_MIN(pc->request_transfer, 63 * 1024);
579 if (drive->using_dma && rq->bh) {
580 if (test_bit(PC_WRITING, &pc->flags))
581 feature.b.dma = !HWIF(drive)->ide_dma_write(drive);
583 feature.b.dma = !HWIF(drive)->ide_dma_read(drive);
588 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
589 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
590 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
591 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
594 set_bit(PC_DMA_OK, &pc->flags);
596 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
597 if (HWGROUP(drive)->handler != NULL)
599 ide_set_handler(drive,
600 &idescsi_transfer_pc,
603 /* Issue the packet command */
604 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
607 /* Issue the packet command */
608 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
609 return idescsi_transfer_pc(drive);
614 * idescsi_do_request is our request handling function.
616 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
618 #if IDESCSI_DEBUG_LOG
619 printk(KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",
620 rq->rq_status, (unsigned int) rq->rq_dev, rq->cmd, rq->errors);
621 printk(KERN_INFO "sector: %ld, nr_sectors: %ld, "
622 "current_nr_sectors: %ld\n", rq->sector,
623 rq->nr_sectors, rq->current_nr_sectors);
624 #endif /* IDESCSI_DEBUG_LOG */
626 if (rq->cmd == IDESCSI_PC_RQ) {
627 return idescsi_issue_pc(drive, rq->special);
629 printk(KERN_ERR "ide-scsi: %s: unsupported command in request "
630 "queue (%x)\n", drive->name, rq->cmd);
631 idescsi_end_request(drive, 0);
635 static int idescsi_do_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
637 /* need to figure out how to parse scsi-atapi media type */
642 static int idescsi_ide_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
648 static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
653 static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
654 static int idescsi_initialized = 0;
655 static int drive_count = 0;
657 static void idescsi_add_settings(ide_drive_t *drive)
659 idescsi_scsi_t *scsi = drive->driver_data;
662 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
664 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
665 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
666 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
667 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
668 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
672 * Driver initialization.
674 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id)
676 int minor = (drive->select.b.unit) << PARTN_BITS;
678 DRIVER(drive)->busy++;
679 idescsi_drives[id] = drive;
680 drive->driver_data = scsi;
681 drive->ready_stat = 0;
682 memset(scsi, 0, sizeof(idescsi_scsi_t));
685 if (drive->id && (drive->id->config & 0x0060) == 0x20)
686 set_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags);
687 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
688 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
689 #if IDESCSI_DEBUG_LOG
690 set_bit(IDESCSI_LOG_CMD, &scsi->log);
691 #endif /* IDESCSI_DEBUG_LOG */
692 idescsi_add_settings(drive);
694 scsi->de = devfs_register(drive->de, "generic", DEVFS_FL_DEFAULT,
695 HWIF(drive)->major, minor,
696 S_IFBLK | S_IRUSR | S_IWUSR,
698 #endif /* IDESCSI_DEVFS */
700 DRIVER(drive)->busy--;
703 static int idescsi_cleanup (ide_drive_t *drive)
705 idescsi_scsi_t *scsi = drive->driver_data;
707 if (ide_unregister_subdriver(drive)) {
708 printk("%s: %s: failed to unregister! \n",
709 __FUNCTION__, drive->name);
710 printk("%s: usage %d, busy %d, driver %p, Dbusy %d\n",
711 drive->name, drive->usage, drive->busy,
712 drive->driver, DRIVER(drive)->busy);
715 idescsi_drives[scsi->id] = NULL;
718 devfs_unregister(scsi->de);
719 #endif /* IDESCSI_DEVFS */
720 drive->driver_data = NULL;
726 int idescsi_init(void);
727 int idescsi_attach(ide_drive_t *drive);
730 * IDE subdriver functions, registered with ide.c
732 static ide_driver_t idescsi_driver = {
734 version: IDESCSI_VERSION,
737 #ifdef CONFIG_IDEDMA_ONLYDISK
742 supports_dsc_overlap: 0,
743 cleanup: idescsi_cleanup,
748 do_request: idescsi_do_request,
749 end_request: idescsi_end_request,
752 ioctl: idescsi_do_ioctl,
753 open: idescsi_ide_open,
754 release: idescsi_ide_release,
762 attach: idescsi_attach,
763 ata_prebuilder: NULL,
764 atapi_prebuilder: NULL,
767 static ide_module_t idescsi_module = {
774 int idescsi_attach (ide_drive_t *drive)
776 idescsi_scsi_t *scsi;
777 u8 media[] = { TYPE_DISK, /* 0x00 */
778 TYPE_TAPE, /* 0x01 */
779 TYPE_PRINTER, /* 0x02 */
780 TYPE_PROCESSOR, /* 0x03 */
781 TYPE_WORM, /* 0x04 */
783 TYPE_SCANNER, /* 0x06 */
786 int i = 0, ret = 0, id = 0;
787 // int id = 2 * HWIF(drive)->index + drive->select.b.unit;
788 // int id = drive_count + 1;
790 for (id = 0; id < MAX_HWIFS*MAX_DRIVES; id++)
791 if (idescsi_drives[id] == NULL)
794 printk("%s: id = %d\n", drive->name, id);
796 if ((!idescsi_initialized) || (drive->media == ide_disk)) {
797 printk(KERN_ERR "ide-scsi: (%sinitialized) %s: "
798 "media-type (%ssupported)\n",
799 (idescsi_initialized) ? "" : "! ",
801 (drive->media == ide_disk) ? "! " : "");
802 return (drive->media == ide_disk) ? 2 : 0;
807 for (i = 0; media[i] != 255; i++) {
808 if (drive->media != media[i])
814 if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
815 printk(KERN_ERR "ide-scsi: %s: Can't allocate a scsi "
816 "structure\n", drive->name);
820 if (ide_register_subdriver(drive, &idescsi_driver,
821 IDE_SUBDRIVER_VERSION)) {
822 printk(KERN_ERR "ide-scsi: %s: Failed to register the "
823 "driver with ide.c\n", drive->name);
829 idescsi_setup(drive, scsi, id);
831 // scan_scsis(HBA, 1, channel, id, lun);
841 MODULE_PARM(ignore, "s");
844 int idescsi_init (void)
846 #ifdef CLASSIC_BUILTINS_METHOD
848 idescsi_scsi_t *scsi;
849 u8 media[] = { TYPE_DISK, /* 0x00 */
850 TYPE_TAPE, /* 0x01 */
851 TYPE_PRINTER, /* 0x02 */
852 TYPE_PROCESSOR, /* 0x03 */
853 TYPE_WORM, /* 0x04 */
855 TYPE_SCANNER, /* 0x06 */
861 if (idescsi_initialized)
863 idescsi_initialized = 1;
864 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
865 idescsi_drives[i] = NULL;
867 for (i = 0; media[i] != 255; i++) {
869 while ((drive = ide_scan_devices(media[i],
870 idescsi_driver.name, NULL, failed++)) != NULL) {
872 /* skip drives we were told to ignore */
873 if (ignore != NULL && strstr(ignore, drive->name)) {
874 printk("ide-scsi: ignoring drive %s\n",
880 if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
881 printk(KERN_ERR "ide-scsi: %s: Can't allocate "
882 "a scsi structure\n", drive->name);
885 if (ide_register_subdriver(drive, &idescsi_driver,
886 IDE_SUBDRIVER_VERSION)) {
887 printk(KERN_ERR "ide-scsi: %s: Failed to "
888 "register the driver with ide.c\n",
894 id < MAX_HWIFS*MAX_DRIVES && idescsi_drives[id];
896 idescsi_setup(drive, scsi, id);
900 #else /* ! CLASSIC_BUILTINS_METHOD */
903 if (idescsi_initialized)
905 idescsi_initialized = 1;
906 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
907 idescsi_drives[i] = NULL;
909 #endif /* CLASSIC_BUILTINS_METHOD */
910 ide_register_module(&idescsi_module);
915 int idescsi_detect (Scsi_Host_Template *host_template)
917 struct Scsi_Host *host;
921 host_template->proc_name = "ide-scsi";
922 host = scsi_register(host_template, 0);
924 printk(KERN_WARNING "%s: host failure!\n", __FUNCTION__);
928 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
929 last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
931 host->max_lun = last_lun + 1;
932 host->can_queue = host->cmd_per_lun * id;
936 int idescsi_release (struct Scsi_Host *host)
941 for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) {
942 drive = idescsi_drives[id];
944 DRIVER(drive)->busy = 0;
949 const char *idescsi_info (struct Scsi_Host *host)
951 return "SCSI host adapter emulation for IDE ATAPI devices";
954 int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
956 ide_drive_t *drive = idescsi_drives[dev->id];
957 idescsi_scsi_t *scsi = drive->driver_data;
959 if (cmd == SG_SET_TRANSFORM) {
961 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
963 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
965 } else if (cmd == SG_GET_TRANSFORM)
966 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
970 static inline struct buffer_head *idescsi_kmalloc_bh (int count)
972 struct buffer_head *bh, *bhp, *first_bh;
974 if ((first_bh = bhp = bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
976 memset(bh, 0, sizeof(struct buffer_head));
977 bh->b_reqnext = NULL;
979 if ((bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
981 memset(bh, 0, sizeof(struct buffer_head));
984 bh->b_reqnext = NULL;
988 idescsi_free_bh(first_bh);
992 static inline int idescsi_set_direction (idescsi_pc_t *pc)
998 clear_bit(PC_WRITING, &pc->flags);
1003 set_bit(PC_WRITING, &pc->flags);
1010 static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc)
1012 struct buffer_head *bh = NULL, *first_bh = NULL;
1013 int segments = pc->scsi_cmd->use_sg;
1014 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
1016 if (!drive->using_dma || !pc->request_transfer || pc->request_transfer & 1023)
1018 if (idescsi_set_direction(pc))
1021 if ((first_bh = bh = idescsi_kmalloc_bh(segments)) == NULL)
1023 #if IDESCSI_DEBUG_LOG
1024 printk("ide-scsi: %s: building DMA table, %d segments, "
1025 "%dkB total\n", drive->name, segments,
1026 pc->request_transfer >> 10);
1027 #endif /* IDESCSI_DEBUG_LOG */
1028 while (segments--) {
1030 bh->b_data = sg->address;
1033 bh->b_page = virt_to_page(sg->address);
1034 bh->b_data = (char *) ((unsigned long) sg->address & ~PAGE_MASK);
1035 } else if (sg->page) {
1036 bh->b_page = sg->page;
1037 bh->b_data = (char *) sg->offset;
1040 bh->b_size = sg->length;
1046 * non-sg requests are guarenteed not to reside in highmem /jens
1048 if ((first_bh = bh = idescsi_kmalloc_bh(1)) == NULL)
1050 #if IDESCSI_DEBUG_LOG
1051 printk("ide-scsi: %s: building DMA table for a single "
1052 "buffer (%dkB)\n", drive->name,
1053 pc->request_transfer >> 10);
1054 #endif /* IDESCSI_DEBUG_LOG */
1055 bh->b_data = pc->scsi_cmd->request_buffer;
1056 bh->b_size = pc->request_transfer;
1061 static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
1063 idescsi_scsi_t *scsi = drive->driver_data;
1065 if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
1066 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
1067 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
1070 int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
1072 ide_drive_t *drive = idescsi_drives[cmd->target];
1073 idescsi_scsi_t *scsi;
1074 struct request *rq = NULL;
1075 idescsi_pc_t *pc = NULL;
1078 printk(KERN_ERR "ide-scsi: drive id %d not present\n",
1082 scsi = drive->driver_data;
1083 pc = kmalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
1084 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
1085 if (rq == NULL || pc == NULL) {
1086 printk(KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
1090 memset(pc->c, 0, 12);
1093 memcpy(pc->c, cmd->cmnd, cmd->cmd_len);
1096 pc->sg = cmd->request_buffer;
1098 pc->buffer = cmd->request_buffer;
1102 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
1105 pc->timeout = jiffies + cmd->timeout_per_command;
1107 if (should_transform(drive, cmd))
1108 set_bit(PC_TRANSFORM, &pc->flags);
1109 idescsi_transform_pc1(drive, pc);
1111 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
1112 printk("ide-scsi: %s: que %lu, cmd = ",
1113 drive->name, cmd->serial_number);
1114 hexdump(cmd->cmnd, cmd->cmd_len);
1115 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
1116 printk("ide-scsi: %s: que %lu, tsl = ",
1117 drive->name, cmd->serial_number);
1122 ide_init_drive_cmd(rq);
1124 rq->bh = idescsi_dma_bh(drive, pc);
1125 rq->cmd = IDESCSI_PC_RQ;
1126 spin_unlock_irq(&io_request_lock);
1127 (void) ide_do_drive_cmd(drive, rq, ide_end);
1128 spin_lock_irq(&io_request_lock);
1133 cmd->result = DID_ERROR << 16;
1138 int idescsi_abort (Scsi_Cmnd *cmd)
1140 return SCSI_ABORT_SNOOZE;
1143 int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags)
1145 return SCSI_RESET_SNOOZE;
1147 #ifdef WORK_IN_PROGRESS
1148 ide_drive_t *drive = idescsi_drives[cmd->target];
1150 /* At this point the state machine is running, that
1151 requires we are especially careful. Ideally we want
1152 to abort commands on timeout only if they hit the
1153 cable but thats harder */
1155 DRIVER(drive)->abort(drive, "scsi reset");
1156 if(HWGROUP(drive)->handler)
1159 /* Ok the state machine is halted but make sure it
1160 doesn't restart too early */
1162 HWGROUP(drive)->busy = 1;
1163 spin_unlock_irq(&io_request_lock);
1165 /* Apply the mallet of re-education firmly to the drive */
1166 ide_do_reset(drive);
1168 /* At this point the reset state machine is running and
1169 its termination will kick off the next command */
1170 spin_lock_irq(&io_request_lock);
1171 return SCSI_RESET_SUCCESS;
1175 int idescsi_bios (Disk *disk, kdev_t dev, int *parm)
1177 ide_drive_t *drive = idescsi_drives[disk->device->id];
1179 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1180 parm[0] = drive->bios_head;
1181 parm[1] = drive->bios_sect;
1182 parm[2] = drive->bios_cyl;
1187 static Scsi_Host_Template idescsi_template = IDESCSI;
1189 static int __init init_idescsi_module(void)
1193 idescsi_template.module = THIS_MODULE;
1194 scsi_register_module(MODULE_SCSI_HA, &idescsi_template);
1198 static void __exit exit_idescsi_module(void)
1201 u8 media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
1204 scsi_unregister_module(MODULE_SCSI_HA, &idescsi_template);
1205 for (i = 0; media[i] != 255; i++) {
1207 while ((drive = ide_scan_devices(media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
1208 if (idescsi_cleanup(drive)) {
1209 printk("%s: exit_idescsi_module() called while still busy\n", drive->name);
1213 ide_unregister_module(&idescsi_module);
1216 module_init(init_idescsi_module);
1217 module_exit(exit_idescsi_module);
1218 MODULE_LICENSE("GPL");