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>
54 #include <scsi/scsi_host.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 /* retry only "normal" I/O: */
322 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
| REQ_DRIVE_TASKFILE
)) {
324 ide_end_drive_cmd(drive
, stat
, err
);
328 if (HWIF(drive
)->INB(IDE_STATUS_REG
) & (BUSY_STAT
|DRQ_STAT
))
330 HWIF(drive
)->OUTB(WIN_IDLEIMMEDIATE
,IDE_COMMAND_REG
);
333 DRIVER(drive
)->end_request(drive
, 0, 0);
337 ide_startstop_t
idescsi_atapi_abort (ide_drive_t
*drive
, const char *msg
)
341 if (drive
== NULL
|| (rq
= HWGROUP(drive
)->rq
) == NULL
)
344 /* retry only "normal" I/O: */
345 if (rq
->flags
& (REQ_DRIVE_CMD
| REQ_DRIVE_TASK
| REQ_DRIVE_TASKFILE
)) {
347 ide_end_drive_cmd(drive
, BUSY_STAT
, 0);
351 #if IDESCSI_DEBUG_LOG
352 printk(KERN_WARNING
"idescsi_atapi_abort called for %lu\n",
353 ((idescsi_pc_t
*) rq
->special
)->scsi_cmd
->serial_number
);
355 rq
->errors
|= ERROR_MAX
;
356 DRIVER(drive
)->end_request(drive
, 0, 0);
360 static int idescsi_end_request (ide_drive_t
*drive
, int uptodate
, int nrsecs
)
362 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
363 struct request
*rq
= HWGROUP(drive
)->rq
;
364 idescsi_pc_t
*pc
= (idescsi_pc_t
*) rq
->special
;
365 int log
= test_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
366 struct Scsi_Host
*host
;
370 if (!(rq
->flags
& (REQ_SPECIAL
|REQ_SENSE
))) {
371 ide_end_request(drive
, uptodate
, nrsecs
);
374 ide_end_drive_cmd (drive
, 0, 0);
375 if (rq
->flags
& REQ_SENSE
) {
376 idescsi_pc_t
*opc
= (idescsi_pc_t
*) rq
->buffer
;
378 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive
->name
, opc
->scsi_cmd
->serial_number
);
379 hexdump(pc
->buffer
,16);
381 memcpy((void *) opc
->scsi_cmd
->sense_buffer
, pc
->buffer
, SCSI_SENSE_BUFFERSIZE
);
387 pc
->scsi_cmd
->result
= (CHECK_CONDITION
<< 1) |
388 ((test_bit(PC_TIMEDOUT
, &pc
->flags
)?DID_TIME_OUT
:DID_OK
) << 16);
389 } else if (test_bit(PC_TIMEDOUT
, &pc
->flags
)) {
391 printk (KERN_WARNING
"ide-scsi: %s: timed out for %lu\n",
392 drive
->name
, pc
->scsi_cmd
->serial_number
);
393 pc
->scsi_cmd
->result
= DID_TIME_OUT
<< 16;
394 } else if (rq
->errors
>= ERROR_MAX
) {
395 pc
->scsi_cmd
->result
= DID_ERROR
<< 16;
397 printk ("ide-scsi: %s: I/O error for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
398 } else if (rq
->errors
) {
400 printk ("ide-scsi: %s: check condition for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
401 if (!idescsi_check_condition(drive
, rq
))
402 /* we started a request sense, so we'll be back, exit for now */
404 pc
->scsi_cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
406 pc
->scsi_cmd
->result
= DID_OK
<< 16;
407 idescsi_transform_pc2 (drive
, pc
);
409 printk ("ide-scsi: %s: suc %lu", drive
->name
, pc
->scsi_cmd
->serial_number
);
410 if (!test_bit(PC_WRITING
, &pc
->flags
) && pc
->actually_transferred
&& pc
->actually_transferred
<= 1024 && pc
->buffer
) {
412 scsi_buf
= pc
->scsi_cmd
->request_buffer
;
413 hexdump(scsi_buf
, min_t(unsigned, 16, pc
->scsi_cmd
->request_bufflen
));
417 host
= pc
->scsi_cmd
->device
->host
;
418 spin_lock_irqsave(host
->host_lock
, flags
);
419 pc
->done(pc
->scsi_cmd
);
420 spin_unlock_irqrestore(host
->host_lock
, flags
);
421 idescsi_free_bio(rq
->bio
);
428 static inline unsigned long get_timeout(idescsi_pc_t
*pc
)
430 return max_t(unsigned long, WAIT_CMD
, pc
->timeout
- jiffies
);
433 static int idescsi_expiry(ide_drive_t
*drive
)
435 idescsi_scsi_t
*scsi
= drive
->driver_data
;
436 idescsi_pc_t
*pc
= scsi
->pc
;
438 #if IDESCSI_DEBUG_LOG
439 printk(KERN_WARNING
"idescsi_expiry called for %lu at %lu\n", pc
->scsi_cmd
->serial_number
, jiffies
);
441 set_bit(PC_TIMEDOUT
, &pc
->flags
);
443 return 0; /* we do not want the ide subsystem to retry */
447 * Our interrupt handler.
449 static ide_startstop_t
idescsi_pc_intr (ide_drive_t
*drive
)
451 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
452 idescsi_pc_t
*pc
=scsi
->pc
;
453 struct request
*rq
= pc
->rq
;
454 atapi_bcount_t bcount
;
455 atapi_status_t status
;
456 atapi_ireason_t ireason
;
457 atapi_feature_t feature
;
461 #if IDESCSI_DEBUG_LOG
462 printk (KERN_INFO
"ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
463 #endif /* IDESCSI_DEBUG_LOG */
465 if (test_bit(PC_TIMEDOUT
, &pc
->flags
)){
466 #if IDESCSI_DEBUG_LOG
467 printk(KERN_WARNING
"idescsi_pc_intr: got timed out packet %lu at %lu\n",
468 pc
->scsi_cmd
->serial_number
, jiffies
);
470 /* end this request now - scsi should retry it*/
471 idescsi_end_request (drive
, 1, 0);
474 if (test_and_clear_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
475 #if IDESCSI_DEBUG_LOG
476 printk ("ide-scsi: %s: DMA complete\n", drive
->name
);
477 #endif /* IDESCSI_DEBUG_LOG */
478 pc
->actually_transferred
=pc
->request_transfer
;
479 (void) HWIF(drive
)->ide_dma_end(drive
);
483 /* Clear the interrupt */
484 status
.all
= HWIF(drive
)->INB(IDE_STATUS_REG
);
487 /* No more interrupts */
488 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
489 printk (KERN_INFO
"Packet command completed, %d bytes transferred\n", pc
->actually_transferred
);
493 idescsi_end_request (drive
, 1, 0);
496 bcount
.b
.low
= HWIF(drive
)->INB(IDE_BCOUNTL_REG
);
497 bcount
.b
.high
= HWIF(drive
)->INB(IDE_BCOUNTH_REG
);
498 ireason
.all
= HWIF(drive
)->INB(IDE_IREASON_REG
);
501 printk(KERN_ERR
"ide-scsi: CoD != 0 in idescsi_pc_intr\n");
502 return ide_do_reset (drive
);
505 temp
= pc
->actually_transferred
+ bcount
.all
;
506 if (temp
> pc
->request_transfer
) {
507 if (temp
> pc
->buffer_size
) {
508 printk(KERN_ERR
"ide-scsi: The scsi wants to "
509 "send us more data than expected "
510 "- discarding data\n");
511 temp
= pc
->buffer_size
- pc
->actually_transferred
;
513 clear_bit(PC_WRITING
, &pc
->flags
);
515 idescsi_input_buffers(drive
, pc
, temp
);
517 atapi_input_bytes(drive
, pc
->current_position
, temp
);
518 printk(KERN_ERR
"ide-scsi: transferred %d of %d bytes\n", temp
, bcount
.all
);
520 pc
->actually_transferred
+= temp
;
521 pc
->current_position
+= temp
;
522 idescsi_discard_data(drive
, bcount
.all
- temp
);
523 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
526 #if IDESCSI_DEBUG_LOG
527 printk (KERN_NOTICE
"ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
528 #endif /* IDESCSI_DEBUG_LOG */
532 clear_bit(PC_WRITING
, &pc
->flags
);
534 idescsi_input_buffers(drive
, pc
, bcount
.all
);
536 HWIF(drive
)->atapi_input_bytes(drive
, pc
->current_position
, bcount
.all
);
538 set_bit(PC_WRITING
, &pc
->flags
);
540 idescsi_output_buffers (drive
, pc
, bcount
.all
);
542 HWIF(drive
)->atapi_output_bytes(drive
, pc
->current_position
, bcount
.all
);
544 /* Update the current position */
545 pc
->actually_transferred
+= bcount
.all
;
546 pc
->current_position
+= bcount
.all
;
548 /* And set the interrupt handler again */
549 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
553 static ide_startstop_t
idescsi_transfer_pc(ide_drive_t
*drive
)
555 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
556 idescsi_pc_t
*pc
= scsi
->pc
;
557 atapi_ireason_t ireason
;
558 ide_startstop_t startstop
;
560 if (ide_wait_stat(&startstop
,drive
,DRQ_STAT
,BUSY_STAT
,WAIT_READY
)) {
561 printk(KERN_ERR
"ide-scsi: Strange, packet command "
562 "initiated yet DRQ isn't asserted\n");
565 ireason
.all
= HWIF(drive
)->INB(IDE_IREASON_REG
);
566 if (!ireason
.b
.cod
|| ireason
.b
.io
) {
567 printk(KERN_ERR
"ide-scsi: (IO,CoD) != (0,1) while "
568 "issuing a packet command\n");
569 return ide_do_reset (drive
);
571 if (HWGROUP(drive
)->handler
!= NULL
)
573 /* Set the interrupt routine */
574 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
575 /* Send the actual packet */
576 atapi_output_bytes(drive
, scsi
->pc
->c
, 12);
577 if (test_bit (PC_DMA_OK
, &pc
->flags
)) {
578 set_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
);
579 (void) (HWIF(drive
)->ide_dma_begin(drive
));
585 * Issue a packet command
587 static ide_startstop_t
idescsi_issue_pc (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
589 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
590 atapi_feature_t feature
;
591 atapi_bcount_t bcount
;
592 struct request
*rq
= pc
->rq
;
594 scsi
->pc
=pc
; /* Set the current packet command */
595 pc
->actually_transferred
=0; /* We haven't transferred any data yet */
596 pc
->current_position
=pc
->buffer
;
597 bcount
.all
= min(pc
->request_transfer
, 63 * 1024); /* Request to transfer the entire buffer at once */
600 if (drive
->using_dma
&& rq
->bio
) {
601 if (test_bit(PC_WRITING
, &pc
->flags
))
602 feature
.b
.dma
= !HWIF(drive
)->ide_dma_write(drive
);
604 feature
.b
.dma
= !HWIF(drive
)->ide_dma_read(drive
);
609 HWIF(drive
)->OUTB(drive
->ctl
, IDE_CONTROL_REG
);
611 HWIF(drive
)->OUTB(feature
.all
, IDE_FEATURE_REG
);
612 HWIF(drive
)->OUTB(bcount
.b
.high
, IDE_BCOUNTH_REG
);
613 HWIF(drive
)->OUTB(bcount
.b
.low
, IDE_BCOUNTL_REG
);
616 set_bit(PC_DMA_OK
, &pc
->flags
);
618 if (test_bit(IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
)) {
619 if (HWGROUP(drive
)->handler
!= NULL
)
621 ide_set_handler(drive
, &idescsi_transfer_pc
,
622 get_timeout(pc
), idescsi_expiry
);
623 /* Issue the packet command */
624 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
627 /* Issue the packet command */
628 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
629 return idescsi_transfer_pc(drive
);
634 * idescsi_do_request is our request handling function.
636 static ide_startstop_t
idescsi_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block
)
638 #if IDESCSI_DEBUG_LOG
639 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
);
640 printk (KERN_INFO
"sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq
->sector
,rq
->nr_sectors
,rq
->current_nr_sectors
);
641 #endif /* IDESCSI_DEBUG_LOG */
643 if (rq
->flags
& (REQ_SPECIAL
|REQ_SENSE
)) {
644 return idescsi_issue_pc (drive
, (idescsi_pc_t
*) rq
->special
);
646 blk_dump_rq_flags(rq
, "ide-scsi: unsup command");
647 idescsi_end_request (drive
, 0, 0);
651 static void idescsi_add_settings(ide_drive_t
*drive
)
653 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
656 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
658 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, -1, -1, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
659 ide_add_setting(drive
, "bios_head", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
660 ide_add_setting(drive
, "bios_sect", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
661 ide_add_setting(drive
, "transform", SETTING_RW
, -1, -1, TYPE_INT
, 0, 3, 1, 1, &scsi
->transform
, NULL
);
662 ide_add_setting(drive
, "log", SETTING_RW
, -1, -1, TYPE_INT
, 0, 1, 1, 1, &scsi
->log
, NULL
);
666 * Driver initialization.
668 static void idescsi_setup (ide_drive_t
*drive
, idescsi_scsi_t
*scsi
)
670 DRIVER(drive
)->busy
++;
671 drive
->ready_stat
= 0;
672 if (drive
->id
&& (drive
->id
->config
& 0x0060) == 0x20)
673 set_bit (IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
);
674 set_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
675 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
676 #if IDESCSI_DEBUG_LOG
677 set_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
678 #endif /* IDESCSI_DEBUG_LOG */
679 idescsi_add_settings(drive
);
680 DRIVER(drive
)->busy
--;
683 static int idescsi_cleanup (ide_drive_t
*drive
)
685 struct Scsi_Host
*scsihost
= drive
->driver_data
;
687 if (ide_unregister_subdriver(drive
))
690 /* FIXME?: Are these two statements necessary? */
691 drive
->driver_data
= NULL
;
692 drive
->disk
->fops
= ide_fops
;
694 scsi_remove_host(scsihost
);
695 scsi_host_put(scsihost
);
699 static int idescsi_attach(ide_drive_t
*drive
);
702 * IDE subdriver functions, registered with ide.c
704 static ide_driver_t idescsi_driver
= {
705 .owner
= THIS_MODULE
,
707 .version
= IDESCSI_VERSION
,
710 .supports_dsc_overlap
= 0,
711 .attach
= idescsi_attach
,
712 .cleanup
= idescsi_cleanup
,
713 .do_request
= idescsi_do_request
,
714 .end_request
= idescsi_end_request
,
715 .error
= idescsi_atapi_error
,
716 .abort
= idescsi_atapi_abort
,
717 .drives
= LIST_HEAD_INIT(idescsi_driver
.drives
),
720 static int idescsi_ide_open(struct inode
*inode
, struct file
*filp
)
722 ide_drive_t
*drive
= inode
->i_bdev
->bd_disk
->private_data
;
727 static int idescsi_ide_release(struct inode
*inode
, struct file
*filp
)
729 ide_drive_t
*drive
= inode
->i_bdev
->bd_disk
->private_data
;
734 static int idescsi_ide_ioctl(struct inode
*inode
, struct file
*file
,
735 unsigned int cmd
, unsigned long arg
)
737 struct block_device
*bdev
= inode
->i_bdev
;
738 return generic_ide_ioctl(file
, bdev
, cmd
, arg
);
741 static struct block_device_operations idescsi_ops
= {
742 .owner
= THIS_MODULE
,
743 .open
= idescsi_ide_open
,
744 .release
= idescsi_ide_release
,
745 .ioctl
= idescsi_ide_ioctl
,
748 static int idescsi_attach(ide_drive_t
*drive
);
750 static int idescsi_slave_configure(Scsi_Device
* sdp
)
752 /* Configure detected device */
753 scsi_adjust_queue_depth(sdp
, MSG_SIMPLE_TAG
, sdp
->host
->cmd_per_lun
);
757 static const char *idescsi_info (struct Scsi_Host
*host
)
759 return "SCSI host adapter emulation for IDE ATAPI devices";
762 static int idescsi_ioctl (Scsi_Device
*dev
, int cmd
, void __user
*arg
)
764 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(dev
->host
);
766 if (cmd
== SG_SET_TRANSFORM
) {
768 set_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
770 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
772 } else if (cmd
== SG_GET_TRANSFORM
)
773 return put_user(test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
), (int __user
*) arg
);
777 static inline struct bio
*idescsi_kmalloc_bio (int count
)
779 struct bio
*bh
, *bhp
, *first_bh
;
781 if ((first_bh
= bhp
= bh
= bio_alloc(GFP_ATOMIC
, 1)) == NULL
)
786 if ((bh
= bio_alloc(GFP_ATOMIC
, 1)) == NULL
)
796 idescsi_free_bio (first_bh
);
800 static inline int idescsi_set_direction (idescsi_pc_t
*pc
)
803 case READ_6
: case READ_10
: case READ_12
:
804 clear_bit (PC_WRITING
, &pc
->flags
);
806 case WRITE_6
: case WRITE_10
: case WRITE_12
:
807 set_bit (PC_WRITING
, &pc
->flags
);
814 static inline struct bio
*idescsi_dma_bio(ide_drive_t
*drive
, idescsi_pc_t
*pc
)
816 struct bio
*bh
= NULL
, *first_bh
= NULL
;
817 int segments
= pc
->scsi_cmd
->use_sg
;
818 struct scatterlist
*sg
= pc
->scsi_cmd
->request_buffer
;
820 if (!drive
->using_dma
|| !pc
->request_transfer
|| pc
->request_transfer
% 1024)
822 if (idescsi_set_direction(pc
))
825 if ((first_bh
= bh
= idescsi_kmalloc_bio (segments
)) == NULL
)
827 #if IDESCSI_DEBUG_LOG
828 printk ("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive
->name
, segments
, pc
->request_transfer
>> 10);
829 #endif /* IDESCSI_DEBUG_LOG */
831 bh
->bi_io_vec
[0].bv_page
= sg
->page
;
832 bh
->bi_io_vec
[0].bv_len
= sg
->length
;
833 bh
->bi_io_vec
[0].bv_offset
= sg
->offset
;
834 bh
->bi_size
= sg
->length
;
839 if ((first_bh
= bh
= idescsi_kmalloc_bio (1)) == NULL
)
841 #if IDESCSI_DEBUG_LOG
842 printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive
->name
, pc
->request_transfer
>> 10);
843 #endif /* IDESCSI_DEBUG_LOG */
844 bh
->bi_io_vec
[0].bv_page
= virt_to_page(pc
->scsi_cmd
->request_buffer
);
845 bh
->bi_io_vec
[0].bv_offset
= offset_in_page(pc
->scsi_cmd
->request_buffer
);
846 bh
->bi_io_vec
[0].bv_len
= pc
->request_transfer
;
847 bh
->bi_size
= pc
->request_transfer
;
852 static inline int should_transform(ide_drive_t
*drive
, Scsi_Cmnd
*cmd
)
854 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
856 /* this was a layering violation and we can't support it
859 struct gendisk
*disk
= cmd
->request
->rq_disk
;
862 struct Scsi_Device_Template
**p
= disk
->private_data
;
863 if (strcmp((*p
)->scsi_driverfs_driver
.name
, "sg") == 0)
864 return test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
867 return test_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
870 static int idescsi_queue (Scsi_Cmnd
*cmd
, void (*done
)(Scsi_Cmnd
*))
872 struct Scsi_Host
*host
= cmd
->device
->host
;
873 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(host
);
874 ide_drive_t
*drive
= scsi
->drive
;
875 struct request
*rq
= NULL
;
876 idescsi_pc_t
*pc
= NULL
;
879 printk (KERN_ERR
"ide-scsi: drive id %d not present\n", cmd
->device
->id
);
882 scsi
= drive_to_idescsi(drive
);
883 pc
= kmalloc (sizeof (idescsi_pc_t
), GFP_ATOMIC
);
884 rq
= kmalloc (sizeof (struct request
), GFP_ATOMIC
);
885 if (rq
== NULL
|| pc
== NULL
) {
886 printk (KERN_ERR
"ide-scsi: %s: out of memory\n", drive
->name
);
890 memset (pc
->c
, 0, 12);
893 memcpy (pc
->c
, cmd
->cmnd
, cmd
->cmd_len
);
896 pc
->sg
= cmd
->request_buffer
;
898 pc
->buffer
= cmd
->request_buffer
;
902 pc
->request_transfer
= pc
->buffer_size
= cmd
->request_bufflen
;
905 pc
->timeout
= jiffies
+ cmd
->timeout_per_command
;
907 if (should_transform(drive
, cmd
))
908 set_bit(PC_TRANSFORM
, &pc
->flags
);
909 idescsi_transform_pc1 (drive
, pc
);
911 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
)) {
912 printk ("ide-scsi: %s: que %lu, cmd = ", drive
->name
, cmd
->serial_number
);
913 hexdump(cmd
->cmnd
, cmd
->cmd_len
);
914 if (memcmp(pc
->c
, cmd
->cmnd
, cmd
->cmd_len
)) {
915 printk ("ide-scsi: %s: que %lu, tsl = ", drive
->name
, cmd
->serial_number
);
920 ide_init_drive_cmd (rq
);
921 rq
->special
= (char *) pc
;
922 rq
->bio
= idescsi_dma_bio (drive
, pc
);
923 rq
->flags
= REQ_SPECIAL
;
924 spin_unlock_irq(host
->host_lock
);
925 (void) ide_do_drive_cmd (drive
, rq
, ide_end
);
926 spin_lock_irq(host
->host_lock
);
931 cmd
->result
= DID_ERROR
<< 16;
936 static int idescsi_eh_abort (Scsi_Cmnd
*cmd
)
938 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(cmd
->device
->host
);
939 ide_drive_t
*drive
= scsi
->drive
;
943 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
945 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
946 printk (KERN_WARNING
"ide-scsi: abort called for %lu\n", cmd
->serial_number
);
949 printk (KERN_WARNING
"ide-scsi: Drive not set in idescsi_eh_abort\n");
954 /* First give it some more time, how much is "right" is hard to say :-( */
956 busy
= ide_wait_not_busy(HWIF(drive
), 100); /* FIXME - uses mdelay which causes latency? */
957 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
958 printk (KERN_WARNING
"ide-scsi: drive did%s become ready\n", busy
?" not":"");
960 spin_lock_irq(&ide_lock
);
962 /* If there is no pc running we're done (our interrupt took care of it) */
968 /* It's somewhere in flight. Does ide subsystem agree? */
969 if (scsi
->pc
->scsi_cmd
->serial_number
== cmd
->serial_number
&& !busy
&&
970 elv_queue_empty(drive
->queue
) && HWGROUP(drive
)->rq
!= scsi
->pc
->rq
) {
972 * FIXME - not sure this condition can ever occur
974 printk (KERN_ERR
"ide-scsi: cmd aborted!\n");
976 idescsi_free_bio(scsi
->pc
->rq
->bio
);
977 if (scsi
->pc
->rq
->flags
& REQ_SENSE
)
978 kfree(scsi
->pc
->buffer
);
987 spin_unlock_irq(&ide_lock
);
989 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
990 printk (KERN_WARNING
"ide-scsi: abort returns %s\n", ret
== SUCCESS
?"success":"failed");
995 static int idescsi_eh_reset (Scsi_Cmnd
*cmd
)
998 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(cmd
->device
->host
);
999 ide_drive_t
*drive
= scsi
->drive
;
1003 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
1005 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
1006 printk (KERN_WARNING
"ide-scsi: reset called for %lu\n", cmd
->serial_number
);
1009 printk (KERN_WARNING
"ide-scsi: Drive not set in idescsi_eh_reset\n");
1014 spin_lock_irq(&ide_lock
);
1016 if (!scsi
->pc
|| (req
= scsi
->pc
->rq
) != HWGROUP(drive
)->rq
|| !HWGROUP(drive
)->handler
) {
1017 printk (KERN_WARNING
"ide-scsi: No active request in idescsi_eh_reset\n");
1018 spin_unlock(&ide_lock
);
1022 /* kill current request */
1023 blkdev_dequeue_request(req
);
1024 end_that_request_last(req
);
1025 idescsi_free_bio(req
->bio
);
1026 if (req
->flags
& REQ_SENSE
)
1027 kfree(scsi
->pc
->buffer
);
1032 /* now nuke the drive queue */
1033 while ((req
= elv_next_request(drive
->queue
))) {
1034 blkdev_dequeue_request(req
);
1035 end_that_request_last(req
);
1038 HWGROUP(drive
)->rq
= NULL
;
1039 HWGROUP(drive
)->handler
= NULL
;
1040 HWGROUP(drive
)->busy
= 1; /* will set this to zero when ide reset finished */
1041 spin_unlock_irq(&ide_lock
);
1043 ide_do_reset(drive
);
1045 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1048 set_current_state(TASK_UNINTERRUPTIBLE
);
1049 spin_unlock_irq(cmd
->device
->host
->host_lock
);
1050 schedule_timeout(HZ
/20);
1051 spin_lock_irq(cmd
->device
->host
->host_lock
);
1052 } while ( HWGROUP(drive
)->handler
);
1054 ready
= drive_is_ready(drive
);
1055 HWGROUP(drive
)->busy
--;
1057 printk (KERN_ERR
"ide-scsi: reset failed!\n");
1064 static int idescsi_bios(struct scsi_device
*sdev
, struct block_device
*bdev
,
1065 sector_t capacity
, int *parm
)
1067 idescsi_scsi_t
*idescsi
= scsihost_to_idescsi(sdev
->host
);
1068 ide_drive_t
*drive
= idescsi
->drive
;
1070 if (drive
->bios_cyl
&& drive
->bios_head
&& drive
->bios_sect
) {
1071 parm
[0] = drive
->bios_head
;
1072 parm
[1] = drive
->bios_sect
;
1073 parm
[2] = drive
->bios_cyl
;
1078 static Scsi_Host_Template idescsi_template
= {
1079 .module
= THIS_MODULE
,
1081 .info
= idescsi_info
,
1082 .slave_configure
= idescsi_slave_configure
,
1083 .ioctl
= idescsi_ioctl
,
1084 .queuecommand
= idescsi_queue
,
1085 .eh_abort_handler
= idescsi_eh_abort
,
1086 .eh_host_reset_handler
= idescsi_eh_reset
,
1087 .bios_param
= idescsi_bios
,
1090 .sg_tablesize
= 256,
1093 .use_clustering
= DISABLE_CLUSTERING
,
1095 .proc_name
= "ide-scsi",
1098 static int idescsi_attach(ide_drive_t
*drive
)
1100 idescsi_scsi_t
*idescsi
;
1101 struct Scsi_Host
*host
;
1105 if (!warned
&& drive
->media
== ide_cdrom
) {
1106 printk(KERN_WARNING
"ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1110 if (!strstr("ide-scsi", drive
->driver_req
) ||
1112 drive
->media
== ide_disk
||
1113 !(host
= scsi_host_alloc(&idescsi_template
,sizeof(idescsi_scsi_t
))))
1118 #if IDESCSI_DEBUG_LOG
1119 if (drive
->id
->last_lun
)
1120 printk(KERN_NOTICE
"%s: id->last_lun=%u\n", drive
->name
, drive
->id
->last_lun
);
1122 if ((drive
->id
->last_lun
& 0x7) != 7)
1123 host
->max_lun
= (drive
->id
->last_lun
& 0x7) + 1;
1127 drive
->driver_data
= host
;
1128 idescsi
= scsihost_to_idescsi(host
);
1129 idescsi
->drive
= drive
;
1130 err
= ide_register_subdriver(drive
, &idescsi_driver
);
1132 idescsi_setup (drive
, idescsi
);
1133 drive
->disk
->fops
= &idescsi_ops
;
1134 err
= scsi_add_host(host
, &drive
->gendev
);
1136 scsi_scan_host(host
);
1139 /* fall through on error */
1140 ide_unregister_subdriver(drive
);
1143 scsi_host_put(host
);
1147 static int __init
init_idescsi_module(void)
1149 return ide_register_driver(&idescsi_driver
);
1152 static void __exit
exit_idescsi_module(void)
1154 ide_unregister_driver(&idescsi_driver
);
1157 module_init(init_idescsi_module
);
1158 module_exit(exit_idescsi_module
);
1159 MODULE_LICENSE("GPL");