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.
32 #define IDESCSI_VERSION "0.9"
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
39 #include <linux/ioport.h>
40 #include <linux/blkdev.h>
41 #include <linux/errno.h>
42 #include <linux/hdreg.h>
43 #include <linux/malloc.h>
44 #include <linux/ide.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
56 #define IDESCSI_DEBUG_LOG 0
58 typedef struct idescsi_pc_s
{
59 u8 c
[12]; /* Actual packet bytes */
60 int request_transfer
; /* Bytes to transfer */
61 int actually_transferred
; /* Bytes actually transferred */
62 int buffer_size
; /* Size of our data buffer */
63 struct request
*rq
; /* The corresponding request */
64 byte
*buffer
; /* Data buffer */
65 byte
*current_position
; /* Pointer into the above buffer */
66 struct scatterlist
*sg
; /* Scatter gather table */
67 int b_count
; /* Bytes transferred from current entry */
68 Scsi_Cmnd
*scsi_cmd
; /* SCSI command */
69 void (*done
)(Scsi_Cmnd
*); /* Scsi completion routine */
70 unsigned long flags
; /* Status/Action flags */
71 unsigned long timeout
; /* Command timeout */
75 * Packet command status bits.
77 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
78 #define PC_WRITING 1 /* Data direction */
79 #define PC_TRANSFORM 2 /* transform SCSI commands */
82 * SCSI command transformation layer
84 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
85 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
90 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
94 idescsi_pc_t
*pc
; /* Current packet command */
95 unsigned long flags
; /* Status/Action flags */
96 unsigned long transform
; /* SCSI cmd translation layer */
97 unsigned long log
; /* log flags */
101 * Per ATAPI device status bits.
103 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
108 #define IDESCSI_PC_RQ 90
111 * Bits of the interrupt reason register.
113 #define IDESCSI_IREASON_COD 0x1 /* Information transferred is command */
114 #define IDESCSI_IREASON_IO 0x2 /* The device requests us to read */
116 static void idescsi_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
119 IN_BYTE (IDE_DATA_REG
);
122 static void idescsi_output_zeros (ide_drive_t
*drive
, unsigned int bcount
)
125 OUT_BYTE (0, IDE_DATA_REG
);
129 * PIO data transfer routines using the scatter gather table.
131 static void idescsi_input_buffers (ide_drive_t
*drive
, idescsi_pc_t
*pc
, unsigned int bcount
)
136 if (pc
->sg
- (struct scatterlist
*) pc
->scsi_cmd
->request_buffer
> pc
->scsi_cmd
->use_sg
) {
137 printk (KERN_ERR
"ide-scsi: scatter gather table too small, discarding data\n");
138 idescsi_discard_data (drive
, bcount
);
141 count
= IDE_MIN (pc
->sg
->length
- pc
->b_count
, bcount
);
142 atapi_input_bytes (drive
, pc
->sg
->address
+ pc
->b_count
, count
);
143 bcount
-= count
; pc
->b_count
+= count
;
144 if (pc
->b_count
== pc
->sg
->length
) {
151 static void idescsi_output_buffers (ide_drive_t
*drive
, idescsi_pc_t
*pc
, unsigned int bcount
)
156 if (pc
->sg
- (struct scatterlist
*) pc
->scsi_cmd
->request_buffer
> pc
->scsi_cmd
->use_sg
) {
157 printk (KERN_ERR
"ide-scsi: scatter gather table too small, padding with zeros\n");
158 idescsi_output_zeros (drive
, bcount
);
161 count
= IDE_MIN (pc
->sg
->length
- pc
->b_count
, bcount
);
162 atapi_output_bytes (drive
, pc
->sg
->address
+ pc
->b_count
, count
);
163 bcount
-= count
; pc
->b_count
+= count
;
164 if (pc
->b_count
== pc
->sg
->length
) {
172 * Most of the SCSI commands are supported directly by ATAPI devices.
173 * idescsi_transform_pc handles the few exceptions.
175 static inline void idescsi_transform_pc1 (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
177 u8
*c
= pc
->c
, *scsi_buf
= pc
->buffer
, *sc
= pc
->scsi_cmd
->cmnd
;
180 if (!test_bit(PC_TRANSFORM
, &pc
->flags
))
182 if (drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
) {
183 if (c
[0] == READ_6
|| c
[0] == WRITE_6
) {
184 c
[8] = c
[4]; c
[5] = c
[3]; c
[4] = c
[2];
185 c
[3] = c
[1] & 0x1f; c
[2] = 0; c
[1] &= 0xe0;
186 c
[0] += (READ_10
- READ_6
);
188 if (c
[0] == MODE_SENSE
|| c
[0] == MODE_SELECT
) {
191 if ((atapi_buf
= kmalloc(pc
->buffer_size
+ 4, GFP_ATOMIC
)) == NULL
)
193 memset(atapi_buf
, 0, pc
->buffer_size
+ 4);
195 c
[0] = sc
[0] | 0x40; c
[1] = sc
[1]; c
[2] = sc
[2];
196 c
[8] = sc
[4] + 4; c
[9] = sc
[5];
198 c
[7] = sc
[4] + 4 - 255;
199 if (c
[0] == MODE_SELECT_10
) {
200 atapi_buf
[1] = scsi_buf
[0]; /* Mode data length */
201 atapi_buf
[2] = scsi_buf
[1]; /* Medium type */
202 atapi_buf
[3] = scsi_buf
[2]; /* Device specific parameter */
203 atapi_buf
[7] = scsi_buf
[3]; /* Block descriptor length */
204 memcpy(atapi_buf
+ 8, scsi_buf
+ 4, pc
->buffer_size
- 4);
206 pc
->buffer
= atapi_buf
;
207 pc
->request_transfer
+= 4;
208 pc
->buffer_size
+= 4;
213 static inline void idescsi_transform_pc2 (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
215 u8
*atapi_buf
= pc
->buffer
;
216 u8
*sc
= pc
->scsi_cmd
->cmnd
;
217 u8
*scsi_buf
= pc
->scsi_cmd
->request_buffer
;
219 if (!test_bit(PC_TRANSFORM
, &pc
->flags
))
221 if (drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
) {
222 if (pc
->c
[0] == MODE_SENSE_10
&& sc
[0] == MODE_SENSE
) {
223 scsi_buf
[0] = atapi_buf
[1]; /* Mode data length */
224 scsi_buf
[1] = atapi_buf
[2]; /* Medium type */
225 scsi_buf
[2] = atapi_buf
[3]; /* Device specific parameter */
226 scsi_buf
[3] = atapi_buf
[7]; /* Block descriptor length */
227 memcpy(scsi_buf
+ 4, atapi_buf
+ 8, pc
->request_transfer
- 8);
229 if (pc
->c
[0] == INQUIRY
) {
230 scsi_buf
[2] |= 2; /* ansi_revision */
231 scsi_buf
[3] = (scsi_buf
[3] & 0xf0) | 2; /* response data format */
234 if (atapi_buf
&& atapi_buf
!= scsi_buf
)
238 static inline void idescsi_free_bh (struct buffer_head
*bh
)
240 struct buffer_head
*bhp
;
249 static void hexdump(u8
*x
, int len
)
254 for (i
= 0; i
< len
; i
++)
259 static void idescsi_end_request (byte uptodate
, ide_hwgroup_t
*hwgroup
)
261 ide_drive_t
*drive
= hwgroup
->drive
;
262 idescsi_scsi_t
*scsi
= drive
->driver_data
;
263 struct request
*rq
= hwgroup
->rq
;
264 idescsi_pc_t
*pc
= (idescsi_pc_t
*) rq
->buffer
;
265 int log
= test_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
269 if (rq
->cmd
!= IDESCSI_PC_RQ
) {
270 ide_end_request (uptodate
, hwgroup
);
273 ide_end_drive_cmd (drive
, 0, 0);
274 if (rq
->errors
>= ERROR_MAX
) {
275 pc
->scsi_cmd
->result
= DID_ERROR
<< 16;
277 printk ("ide-scsi: %s: I/O error for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
278 } else if (rq
->errors
) {
279 pc
->scsi_cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
281 printk ("ide-scsi: %s: check condition for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
283 pc
->scsi_cmd
->result
= DID_OK
<< 16;
284 idescsi_transform_pc2 (drive
, pc
);
286 printk ("ide-scsi: %s: suc %lu", drive
->name
, pc
->scsi_cmd
->serial_number
);
287 if (!test_bit(PC_WRITING
, &pc
->flags
) && pc
->actually_transferred
&& pc
->actually_transferred
<= 1024 && pc
->buffer
) {
289 scsi_buf
= pc
->scsi_cmd
->request_buffer
;
290 hexdump(scsi_buf
, IDE_MIN(16, pc
->scsi_cmd
->request_bufflen
));
294 spin_lock_irqsave(&io_request_lock
,flags
);
295 pc
->done(pc
->scsi_cmd
);
296 spin_unlock_irqrestore(&io_request_lock
,flags
);
297 idescsi_free_bh (rq
->bh
);
298 kfree(pc
); kfree(rq
);
302 static inline unsigned long get_timeout(idescsi_pc_t
*pc
)
304 return IDE_MAX(WAIT_CMD
, pc
->timeout
- jiffies
);
308 * Our interrupt handler.
310 static void idescsi_pc_intr (ide_drive_t
*drive
)
312 idescsi_scsi_t
*scsi
= drive
->driver_data
;
313 byte status
, ireason
;
315 idescsi_pc_t
*pc
=scsi
->pc
;
316 struct request
*rq
= pc
->rq
;
319 #if IDESCSI_DEBUG_LOG
320 printk (KERN_INFO
"ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
321 #endif /* IDESCSI_DEBUG_LOG */
323 if (test_and_clear_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
324 #if IDESCSI_DEBUG_LOG
325 printk ("ide-scsi: %s: DMA complete\n", drive
->name
);
326 #endif /* IDESCSI_DEBUG_LOG */
327 pc
->actually_transferred
=pc
->request_transfer
;
328 (void) (HWIF(drive
)->dmaproc(ide_dma_end
, drive
));
331 status
= GET_STAT(); /* Clear the interrupt */
333 if ((status
& DRQ_STAT
) == 0) { /* No more interrupts */
334 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
335 printk (KERN_INFO
"Packet command completed, %d bytes transferred\n", pc
->actually_transferred
);
337 if (status
& ERR_STAT
)
339 idescsi_end_request (1, HWGROUP(drive
));
342 bcount
= IN_BYTE (IDE_BCOUNTH_REG
) << 8 | IN_BYTE (IDE_BCOUNTL_REG
);
343 ireason
= IN_BYTE (IDE_IREASON_REG
);
345 if (ireason
& IDESCSI_IREASON_COD
) {
346 printk (KERN_ERR
"ide-scsi: CoD != 0 in idescsi_pc_intr\n");
347 ide_do_reset (drive
);
350 if (ireason
& IDESCSI_IREASON_IO
) {
351 temp
= pc
->actually_transferred
+ bcount
;
352 if ( temp
> pc
->request_transfer
) {
353 if (temp
> pc
->buffer_size
) {
354 printk (KERN_ERR
"ide-scsi: The scsi wants to send us more data than expected - discarding data\n");
355 temp
= pc
->buffer_size
- pc
->actually_transferred
;
357 clear_bit(PC_WRITING
, &pc
->flags
);
359 idescsi_input_buffers(drive
, pc
, temp
);
361 atapi_input_bytes(drive
, pc
->current_position
, temp
);
362 printk(KERN_ERR
"ide-scsi: transferred %d of %d bytes\n", temp
, bcount
);
364 pc
->actually_transferred
+= temp
;
365 pc
->current_position
+= temp
;
366 idescsi_discard_data (drive
,bcount
- temp
);
367 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
));
370 #if IDESCSI_DEBUG_LOG
371 printk (KERN_NOTICE
"ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
372 #endif /* IDESCSI_DEBUG_LOG */
375 if (ireason
& IDESCSI_IREASON_IO
) {
376 clear_bit(PC_WRITING
, &pc
->flags
);
378 idescsi_input_buffers (drive
, pc
, bcount
);
380 atapi_input_bytes (drive
,pc
->current_position
,bcount
);
382 set_bit(PC_WRITING
, &pc
->flags
);
384 idescsi_output_buffers (drive
, pc
, bcount
);
386 atapi_output_bytes (drive
,pc
->current_position
,bcount
);
388 pc
->actually_transferred
+=bcount
; /* Update the current position */
389 pc
->current_position
+=bcount
;
391 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
)); /* And set the interrupt handler again */
394 static void idescsi_transfer_pc (ide_drive_t
*drive
)
396 idescsi_scsi_t
*scsi
= drive
->driver_data
;
397 idescsi_pc_t
*pc
= scsi
->pc
;
400 if (ide_wait_stat (drive
,DRQ_STAT
,BUSY_STAT
,WAIT_READY
)) {
401 printk (KERN_ERR
"ide-scsi: Strange, packet command initiated yet DRQ isn't asserted\n");
404 ireason
= IN_BYTE (IDE_IREASON_REG
);
405 if ((ireason
& (IDESCSI_IREASON_IO
| IDESCSI_IREASON_COD
)) != IDESCSI_IREASON_COD
) {
406 printk (KERN_ERR
"ide-scsi: (IO,CoD) != (0,1) while issuing a packet command\n");
407 ide_do_reset (drive
);
410 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
)); /* Set the interrupt routine */
411 atapi_output_bytes (drive
, scsi
->pc
->c
, 12); /* Send the actual packet */
415 * Issue a packet command
417 static void idescsi_issue_pc (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
419 idescsi_scsi_t
*scsi
= drive
->driver_data
;
421 struct request
*rq
= pc
->rq
;
424 scsi
->pc
=pc
; /* Set the current packet command */
425 pc
->actually_transferred
=0; /* We haven't transferred any data yet */
426 pc
->current_position
=pc
->buffer
;
427 bcount
= IDE_MIN (pc
->request_transfer
, 63 * 1024); /* Request to transfer the entire buffer at once */
429 if (drive
->using_dma
&& rq
->bh
)
430 dma_ok
=!HWIF(drive
)->dmaproc(test_bit (PC_WRITING
, &pc
->flags
) ? ide_dma_write
: ide_dma_read
, drive
);
432 OUT_BYTE (drive
->ctl
,IDE_CONTROL_REG
);
433 OUT_BYTE (dma_ok
,IDE_FEATURE_REG
);
434 OUT_BYTE (bcount
>> 8,IDE_BCOUNTH_REG
);
435 OUT_BYTE (bcount
& 0xff,IDE_BCOUNTL_REG
);
436 OUT_BYTE (drive
->select
.all
,IDE_SELECT_REG
);
439 set_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
);
440 (void) (HWIF(drive
)->dmaproc(ide_dma_begin
, drive
));
442 if (test_bit (IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
)) {
443 ide_set_handler (drive
, &idescsi_transfer_pc
, get_timeout(pc
));
444 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
); /* Issue the packet command */
446 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
);
447 idescsi_transfer_pc (drive
);
452 * idescsi_do_request is our request handling function.
454 static void idescsi_do_request (ide_drive_t
*drive
, struct request
*rq
, unsigned long block
)
456 #if IDESCSI_DEBUG_LOG
457 printk (KERN_INFO
"rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq
->rq_status
,(unsigned int) rq
->rq_dev
,rq
->cmd
,rq
->errors
);
458 printk (KERN_INFO
"sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq
->sector
,rq
->nr_sectors
,rq
->current_nr_sectors
);
459 #endif /* IDESCSI_DEBUG_LOG */
461 if (rq
->cmd
== IDESCSI_PC_RQ
) {
462 idescsi_issue_pc (drive
, (idescsi_pc_t
*) rq
->buffer
);
465 printk (KERN_ERR
"ide-scsi: %s: unsupported command in request queue (%x)\n", drive
->name
, rq
->cmd
);
466 idescsi_end_request (0,HWGROUP (drive
));
469 static int idescsi_open (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
475 static void idescsi_ide_release (struct inode
*inode
, struct file
*filp
, ide_drive_t
*drive
)
480 static ide_drive_t
*idescsi_drives
[MAX_HWIFS
* MAX_DRIVES
];
481 static int idescsi_initialized
= 0;
483 static void idescsi_add_settings(ide_drive_t
*drive
)
485 idescsi_scsi_t
*scsi
= drive
->driver_data
;
488 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
490 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, -1, -1, TYPE_SHORT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
491 ide_add_setting(drive
, "bios_head", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
492 ide_add_setting(drive
, "bios_sect", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
493 ide_add_setting(drive
, "transform", SETTING_RW
, -1, -1, TYPE_INT
, 0, 3, 1, 1, &scsi
->transform
, NULL
);
494 ide_add_setting(drive
, "log", SETTING_RW
, -1, -1, TYPE_INT
, 0, 1, 1, 1, &scsi
->log
, NULL
);
498 * Driver initialization.
500 static void idescsi_setup (ide_drive_t
*drive
, idescsi_scsi_t
*scsi
, int id
)
502 DRIVER(drive
)->busy
++;
503 idescsi_drives
[id
] = drive
;
504 drive
->driver_data
= scsi
;
505 drive
->ready_stat
= 0;
506 memset (scsi
, 0, sizeof (idescsi_scsi_t
));
508 if (drive
->id
&& (drive
->id
->config
& 0x0060) == 0x20)
509 set_bit (IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
);
510 set_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
511 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
512 #if IDESCSI_DEBUG_LOG
513 set_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
514 #endif /* IDESCSI_DEBUG_LOG */
515 idescsi_add_settings(drive
);
518 static int idescsi_cleanup (ide_drive_t
*drive
)
520 idescsi_scsi_t
*scsi
= drive
->driver_data
;
522 if (ide_unregister_subdriver (drive
))
524 drive
->driver_data
= NULL
;
530 * IDE subdriver functions, registered with ide.c
532 static ide_driver_t idescsi_driver
= {
533 "ide-scsi", /* name */
534 IDESCSI_VERSION
, /* version */
535 ide_scsi
, /* media */
537 1, /* supports_dma */
538 0, /* supports_dsc_overlap */
539 idescsi_cleanup
, /* cleanup */
540 idescsi_do_request
, /* do_request */
541 idescsi_end_request
, /* end_request */
543 idescsi_open
, /* open */
544 idescsi_ide_release
, /* release */
545 NULL
, /* media_change */
546 NULL
, /* pre_reset */
552 int idescsi_init (void);
553 static ide_module_t idescsi_module
= {
560 static struct proc_dir_entry idescsi_proc_dir
= {PROC_SCSI_IDESCSI
, 8, "ide-scsi", S_IFDIR
| S_IRUGO
| S_IXUGO
, 2};
563 * idescsi_init will register the driver for each scsi.
565 int idescsi_init (void)
568 idescsi_scsi_t
*scsi
;
569 byte media
[] = {TYPE_DISK
, TYPE_TAPE
, TYPE_PROCESSOR
, TYPE_WORM
, TYPE_ROM
, TYPE_SCANNER
, TYPE_MOD
, 255};
572 if (idescsi_initialized
)
574 idescsi_initialized
= 1;
575 for (i
= 0; i
< MAX_HWIFS
* MAX_DRIVES
; i
++)
576 idescsi_drives
[i
] = NULL
;
578 for (i
= 0; media
[i
] != 255; i
++) {
580 while ((drive
= ide_scan_devices (media
[i
], idescsi_driver
.name
, NULL
, failed
++)) != NULL
) {
581 if ((scsi
= (idescsi_scsi_t
*) kmalloc (sizeof (idescsi_scsi_t
), GFP_KERNEL
)) == NULL
) {
582 printk (KERN_ERR
"ide-scsi: %s: Can't allocate a scsi structure\n", drive
->name
);
585 if (ide_register_subdriver (drive
, &idescsi_driver
, IDE_SUBDRIVER_VERSION
)) {
586 printk (KERN_ERR
"ide-scsi: %s: Failed to register the driver with ide.c\n", drive
->name
);
590 for (id
= 0; id
< MAX_HWIFS
* MAX_DRIVES
&& idescsi_drives
[id
]; id
++);
591 idescsi_setup (drive
, scsi
, id
);
595 ide_register_module(&idescsi_module
);
600 int idescsi_detect (Scsi_Host_Template
*host_template
)
602 struct Scsi_Host
*host
;
605 host_template
->proc_dir
= &idescsi_proc_dir
;
606 host
= scsi_register(host_template
, 0);
607 for (id
= 0; id
< MAX_HWIFS
* MAX_DRIVES
&& idescsi_drives
[id
]; id
++);
609 host
->can_queue
= host
->cmd_per_lun
* id
;
613 int idescsi_release (struct Scsi_Host
*host
)
618 for (id
= 0; id
< MAX_HWIFS
* MAX_DRIVES
; id
++) {
619 drive
= idescsi_drives
[id
];
621 DRIVER(drive
)->busy
--;
626 const char *idescsi_info (struct Scsi_Host
*host
)
628 return "SCSI host adapter emulation for IDE ATAPI devices";
631 int idescsi_ioctl (Scsi_Device
*dev
, int cmd
, void *arg
)
633 ide_drive_t
*drive
= idescsi_drives
[dev
->id
];
634 idescsi_scsi_t
*scsi
= drive
->driver_data
;
635 int enable
= (int) arg
;
637 if (cmd
== SG_SET_TRANSFORM
) {
639 set_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
641 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
643 } else if (cmd
== SG_GET_TRANSFORM
)
644 return put_user(test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
), (int *) arg
);
648 static inline struct buffer_head
*idescsi_kmalloc_bh (int count
)
650 struct buffer_head
*bh
, *bhp
, *first_bh
;
652 if ((first_bh
= bhp
= bh
= kmalloc (sizeof(struct buffer_head
), GFP_ATOMIC
)) == NULL
)
654 memset (bh
, 0, sizeof (struct buffer_head
));
655 bh
->b_reqnext
= NULL
;
657 if ((bh
= kmalloc (sizeof(struct buffer_head
), GFP_ATOMIC
)) == NULL
)
659 memset (bh
, 0, sizeof (struct buffer_head
));
662 bh
->b_reqnext
= NULL
;
666 idescsi_free_bh (first_bh
);
670 static inline int idescsi_set_direction (idescsi_pc_t
*pc
)
673 case READ_6
: case READ_10
: case READ_12
:
674 clear_bit (PC_WRITING
, &pc
->flags
);
676 case WRITE_6
: case WRITE_10
: case WRITE_12
:
677 set_bit (PC_WRITING
, &pc
->flags
);
684 static inline struct buffer_head
*idescsi_dma_bh (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
686 struct buffer_head
*bh
= NULL
, *first_bh
= NULL
;
687 int segments
= pc
->scsi_cmd
->use_sg
;
688 struct scatterlist
*sg
= pc
->scsi_cmd
->request_buffer
;
690 if (!drive
->using_dma
|| !pc
->request_transfer
|| pc
->request_transfer
% 1024)
692 if (idescsi_set_direction(pc
))
695 if ((first_bh
= bh
= idescsi_kmalloc_bh (segments
)) == NULL
)
697 #if IDESCSI_DEBUG_LOG
698 printk ("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive
->name
, segments
, pc
->request_transfer
>> 10);
699 #endif /* IDESCSI_DEBUG_LOG */
701 bh
->b_data
= sg
->address
;
702 bh
->b_size
= sg
->length
;
707 if ((first_bh
= bh
= idescsi_kmalloc_bh (1)) == NULL
)
709 #if IDESCSI_DEBUG_LOG
710 printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive
->name
, pc
->request_transfer
>> 10);
711 #endif /* IDESCSI_DEBUG_LOG */
712 bh
->b_data
= pc
->scsi_cmd
->request_buffer
;
713 bh
->b_size
= pc
->request_transfer
;
718 static inline int should_transform(ide_drive_t
*drive
, Scsi_Cmnd
*cmd
)
720 idescsi_scsi_t
*scsi
= drive
->driver_data
;
722 if (MAJOR(cmd
->request
.rq_dev
) == SCSI_GENERIC_MAJOR
)
723 return test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
724 return test_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
727 int idescsi_queue (Scsi_Cmnd
*cmd
, void (*done
)(Scsi_Cmnd
*))
729 ide_drive_t
*drive
= idescsi_drives
[cmd
->target
];
730 idescsi_scsi_t
*scsi
;
731 struct request
*rq
= NULL
;
732 idescsi_pc_t
*pc
= NULL
;
735 printk (KERN_ERR
"ide-scsi: drive id %d not present\n", cmd
->target
);
738 scsi
= drive
->driver_data
;
739 pc
= kmalloc (sizeof (idescsi_pc_t
), GFP_ATOMIC
);
740 rq
= kmalloc (sizeof (struct request
), GFP_ATOMIC
);
741 if (rq
== NULL
|| pc
== NULL
) {
742 printk (KERN_ERR
"ide-scsi: %s: out of memory\n", drive
->name
);
746 memset (pc
->c
, 0, 12);
749 memcpy (pc
->c
, cmd
->cmnd
, cmd
->cmd_len
);
752 pc
->sg
= cmd
->request_buffer
;
754 pc
->buffer
= cmd
->request_buffer
;
758 pc
->request_transfer
= pc
->buffer_size
= cmd
->request_bufflen
;
761 pc
->timeout
= jiffies
+ cmd
->timeout_per_command
;
763 if (should_transform(drive
, cmd
))
764 set_bit(PC_TRANSFORM
, &pc
->flags
);
765 idescsi_transform_pc1 (drive
, pc
);
767 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
)) {
768 printk ("ide-scsi: %s: que %lu, cmd = ", drive
->name
, cmd
->serial_number
);
769 hexdump(cmd
->cmnd
, cmd
->cmd_len
);
770 if (memcmp(pc
->c
, cmd
->cmnd
, cmd
->cmd_len
)) {
771 printk ("ide-scsi: %s: que %lu, tsl = ", drive
->name
, cmd
->serial_number
);
776 ide_init_drive_cmd (rq
);
777 rq
->buffer
= (char *) pc
;
778 rq
->bh
= idescsi_dma_bh (drive
, pc
);
779 rq
->cmd
= IDESCSI_PC_RQ
;
780 spin_unlock(&io_request_lock
);
781 (void) ide_do_drive_cmd (drive
, rq
, ide_end
);
782 spin_lock_irq(&io_request_lock
);
787 cmd
->result
= DID_ERROR
<< 16;
792 int idescsi_abort (Scsi_Cmnd
*cmd
)
794 return SCSI_ABORT_SNOOZE
;
797 int idescsi_reset (Scsi_Cmnd
*cmd
, unsigned int resetflags
)
799 return SCSI_RESET_SUCCESS
;
802 int idescsi_bios (Disk
*disk
, kdev_t dev
, int *parm
)
804 ide_drive_t
*drive
= idescsi_drives
[disk
->device
->id
];
806 if (drive
->bios_cyl
&& drive
->bios_head
&& drive
->bios_sect
) {
807 parm
[0] = drive
->bios_head
;
808 parm
[1] = drive
->bios_sect
;
809 parm
[2] = drive
->bios_cyl
;
815 Scsi_Host_Template idescsi_template
= IDESCSI
;
817 int init_module (void)
820 idescsi_template
.module
= &__this_module
;
821 scsi_register_module (MODULE_SCSI_HA
, &idescsi_template
);
825 void cleanup_module (void)
828 byte media
[] = {TYPE_DISK
, TYPE_TAPE
, TYPE_PROCESSOR
, TYPE_WORM
, TYPE_ROM
, TYPE_SCANNER
, TYPE_MOD
, 255};
831 scsi_unregister_module (MODULE_SCSI_HA
, &idescsi_template
);
832 for (i
= 0; media
[i
] != 255; i
++) {
834 while ((drive
= ide_scan_devices (media
[i
], idescsi_driver
.name
, &idescsi_driver
, failed
)) != NULL
)
835 if (idescsi_cleanup (drive
)) {
836 printk ("%s: cleanup_module() called while still busy\n", drive
->name
);
840 ide_unregister_module(&idescsi_module
);