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>
48 #include <linux/scatterlist.h>
49 #include <linux/delay.h>
52 #include <asm/bitops.h>
53 #include <asm/uaccess.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_tcq.h>
62 #define IDESCSI_DEBUG_LOG 0
64 typedef struct idescsi_pc_s
{
65 u8 c
[12]; /* Actual packet bytes */
66 int request_transfer
; /* Bytes to transfer */
67 int actually_transferred
; /* Bytes actually transferred */
68 int buffer_size
; /* Size of our data buffer */
69 struct request
*rq
; /* The corresponding request */
70 u8
*buffer
; /* Data buffer */
71 u8
*current_position
; /* Pointer into the above buffer */
72 struct scatterlist
*sg
; /* Scatter gather table */
73 int b_count
; /* Bytes transferred from current entry */
74 struct scsi_cmnd
*scsi_cmd
; /* SCSI command */
75 void (*done
)(struct scsi_cmnd
*); /* Scsi completion routine */
76 unsigned long flags
; /* Status/Action flags */
77 unsigned long timeout
; /* Command timeout */
81 * Packet command status bits.
83 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
84 #define PC_WRITING 1 /* Data direction */
85 #define PC_TRANSFORM 2 /* transform SCSI commands */
86 #define PC_TIMEDOUT 3 /* command timed out */
87 #define PC_DMA_OK 4 /* Use DMA */
90 * SCSI command transformation layer
92 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
93 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
98 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
100 typedef struct ide_scsi_obj
{
102 ide_driver_t
*driver
;
103 struct gendisk
*disk
;
104 struct Scsi_Host
*host
;
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 static DECLARE_MUTEX(idescsi_ref_sem
);
114 #define ide_scsi_g(disk) \
115 container_of((disk)->private_data, struct ide_scsi_obj, driver)
117 static struct ide_scsi_obj
*ide_scsi_get(struct gendisk
*disk
)
119 struct ide_scsi_obj
*scsi
= NULL
;
121 down(&idescsi_ref_sem
);
122 scsi
= ide_scsi_g(disk
);
124 scsi_host_get(scsi
->host
);
125 up(&idescsi_ref_sem
);
129 static void ide_scsi_put(struct ide_scsi_obj
*scsi
)
131 down(&idescsi_ref_sem
);
132 scsi_host_put(scsi
->host
);
133 up(&idescsi_ref_sem
);
136 static inline idescsi_scsi_t
*scsihost_to_idescsi(struct Scsi_Host
*host
)
138 return (idescsi_scsi_t
*) (&host
[1]);
141 static inline idescsi_scsi_t
*drive_to_idescsi(ide_drive_t
*ide_drive
)
143 return scsihost_to_idescsi(ide_drive
->driver_data
);
147 * Per ATAPI device status bits.
149 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
154 #define IDESCSI_PC_RQ 90
156 static void idescsi_discard_data (ide_drive_t
*drive
, unsigned int bcount
)
159 (void) HWIF(drive
)->INB(IDE_DATA_REG
);
162 static void idescsi_output_zeros (ide_drive_t
*drive
, unsigned int bcount
)
165 HWIF(drive
)->OUTB(0, IDE_DATA_REG
);
169 * PIO data transfer routines using the scatter gather table.
171 static void idescsi_input_buffers (ide_drive_t
*drive
, idescsi_pc_t
*pc
, unsigned int bcount
)
177 if (pc
->sg
- (struct scatterlist
*) pc
->scsi_cmd
->request_buffer
> pc
->scsi_cmd
->use_sg
) {
178 printk (KERN_ERR
"ide-scsi: scatter gather table too small, discarding data\n");
179 idescsi_discard_data (drive
, bcount
);
182 count
= min(pc
->sg
->length
- pc
->b_count
, bcount
);
183 if (PageHighMem(pc
->sg
->page
)) {
186 local_irq_save(flags
);
187 buf
= kmap_atomic(pc
->sg
->page
, KM_IRQ0
) + pc
->sg
->offset
;
188 drive
->hwif
->atapi_input_bytes(drive
, buf
+ pc
->b_count
, count
);
189 kunmap_atomic(buf
- pc
->sg
->offset
, KM_IRQ0
);
190 local_irq_restore(flags
);
192 buf
= page_address(pc
->sg
->page
) + pc
->sg
->offset
;
193 drive
->hwif
->atapi_input_bytes(drive
, buf
+ pc
->b_count
, count
);
195 bcount
-= count
; pc
->b_count
+= count
;
196 if (pc
->b_count
== pc
->sg
->length
) {
203 static void idescsi_output_buffers (ide_drive_t
*drive
, idescsi_pc_t
*pc
, unsigned int bcount
)
209 if (pc
->sg
- (struct scatterlist
*) pc
->scsi_cmd
->request_buffer
> pc
->scsi_cmd
->use_sg
) {
210 printk (KERN_ERR
"ide-scsi: scatter gather table too small, padding with zeros\n");
211 idescsi_output_zeros (drive
, bcount
);
214 count
= min(pc
->sg
->length
- pc
->b_count
, bcount
);
215 if (PageHighMem(pc
->sg
->page
)) {
218 local_irq_save(flags
);
219 buf
= kmap_atomic(pc
->sg
->page
, KM_IRQ0
) + pc
->sg
->offset
;
220 drive
->hwif
->atapi_output_bytes(drive
, buf
+ pc
->b_count
, count
);
221 kunmap_atomic(buf
- pc
->sg
->offset
, KM_IRQ0
);
222 local_irq_restore(flags
);
224 buf
= page_address(pc
->sg
->page
) + pc
->sg
->offset
;
225 drive
->hwif
->atapi_output_bytes(drive
, buf
+ pc
->b_count
, count
);
227 bcount
-= count
; pc
->b_count
+= count
;
228 if (pc
->b_count
== pc
->sg
->length
) {
236 * Most of the SCSI commands are supported directly by ATAPI devices.
237 * idescsi_transform_pc handles the few exceptions.
239 static inline void idescsi_transform_pc1 (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
241 u8
*c
= pc
->c
, *scsi_buf
= pc
->buffer
, *sc
= pc
->scsi_cmd
->cmnd
;
244 if (!test_bit(PC_TRANSFORM
, &pc
->flags
))
246 if (drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
) {
247 if (c
[0] == READ_6
|| c
[0] == WRITE_6
) {
248 c
[8] = c
[4]; c
[5] = c
[3]; c
[4] = c
[2];
249 c
[3] = c
[1] & 0x1f; c
[2] = 0; c
[1] &= 0xe0;
250 c
[0] += (READ_10
- READ_6
);
252 if (c
[0] == MODE_SENSE
|| c
[0] == MODE_SELECT
) {
253 unsigned short new_len
;
256 if ((atapi_buf
= kmalloc(pc
->buffer_size
+ 4, GFP_ATOMIC
)) == NULL
)
258 memset(atapi_buf
, 0, pc
->buffer_size
+ 4);
267 if (c
[0] == MODE_SELECT_10
) {
268 atapi_buf
[1] = scsi_buf
[0]; /* Mode data length */
269 atapi_buf
[2] = scsi_buf
[1]; /* Medium type */
270 atapi_buf
[3] = scsi_buf
[2]; /* Device specific parameter */
271 atapi_buf
[7] = scsi_buf
[3]; /* Block descriptor length */
272 memcpy(atapi_buf
+ 8, scsi_buf
+ 4, pc
->buffer_size
- 4);
274 pc
->buffer
= atapi_buf
;
275 pc
->request_transfer
+= 4;
276 pc
->buffer_size
+= 4;
281 static inline void idescsi_transform_pc2 (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
283 u8
*atapi_buf
= pc
->buffer
;
284 u8
*sc
= pc
->scsi_cmd
->cmnd
;
285 u8
*scsi_buf
= pc
->scsi_cmd
->request_buffer
;
287 if (!test_bit(PC_TRANSFORM
, &pc
->flags
))
289 if (drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
) {
290 if (pc
->c
[0] == MODE_SENSE_10
&& sc
[0] == MODE_SENSE
) {
291 scsi_buf
[0] = atapi_buf
[1]; /* Mode data length */
292 scsi_buf
[1] = atapi_buf
[2]; /* Medium type */
293 scsi_buf
[2] = atapi_buf
[3]; /* Device specific parameter */
294 scsi_buf
[3] = atapi_buf
[7]; /* Block descriptor length */
295 memcpy(scsi_buf
+ 4, atapi_buf
+ 8, pc
->request_transfer
- 8);
297 if (pc
->c
[0] == INQUIRY
) {
298 scsi_buf
[2] |= 2; /* ansi_revision */
299 scsi_buf
[3] = (scsi_buf
[3] & 0xf0) | 2; /* response data format */
302 if (atapi_buf
&& atapi_buf
!= scsi_buf
)
306 static void hexdump(u8
*x
, int len
)
311 for (i
= 0; i
< len
; i
++)
316 static int idescsi_check_condition(ide_drive_t
*drive
, struct request
*failed_command
)
318 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
323 /* stuff a sense request in front of our current request */
324 pc
= kmalloc (sizeof (idescsi_pc_t
), GFP_ATOMIC
);
325 rq
= kmalloc (sizeof (struct request
), GFP_ATOMIC
);
326 buf
= kmalloc(SCSI_SENSE_BUFFERSIZE
, GFP_ATOMIC
);
327 if (pc
== NULL
|| rq
== NULL
|| buf
== NULL
) {
333 memset (pc
, 0, sizeof (idescsi_pc_t
));
334 memset (buf
, 0, SCSI_SENSE_BUFFERSIZE
);
335 ide_init_drive_cmd(rq
);
336 rq
->special
= (char *) pc
;
339 pc
->c
[0] = REQUEST_SENSE
;
340 pc
->c
[4] = pc
->request_transfer
= pc
->buffer_size
= SCSI_SENSE_BUFFERSIZE
;
341 rq
->flags
= REQ_SENSE
;
342 pc
->timeout
= jiffies
+ WAIT_READY
;
343 /* NOTE! Save the failed packet command in "rq->buffer" */
344 rq
->buffer
= (void *) failed_command
->special
;
345 pc
->scsi_cmd
= ((idescsi_pc_t
*) failed_command
->special
)->scsi_cmd
;
346 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
)) {
347 printk ("ide-scsi: %s: queue cmd = ", drive
->name
);
350 rq
->rq_disk
= scsi
->disk
;
351 return ide_do_drive_cmd(drive
, rq
, ide_preempt
);
354 static int idescsi_end_request(ide_drive_t
*, int, int);
356 static ide_startstop_t
357 idescsi_atapi_error(ide_drive_t
*drive
, struct request
*rq
, u8 stat
, u8 err
)
359 if (HWIF(drive
)->INB(IDE_STATUS_REG
) & (BUSY_STAT
|DRQ_STAT
))
361 HWIF(drive
)->OUTB(WIN_IDLEIMMEDIATE
,IDE_COMMAND_REG
);
365 idescsi_end_request(drive
, 0, 0);
370 static ide_startstop_t
371 idescsi_atapi_abort(ide_drive_t
*drive
, struct request
*rq
)
373 #if IDESCSI_DEBUG_LOG
374 printk(KERN_WARNING
"idescsi_atapi_abort called for %lu\n",
375 ((idescsi_pc_t
*) rq
->special
)->scsi_cmd
->serial_number
);
377 rq
->errors
|= ERROR_MAX
;
379 idescsi_end_request(drive
, 0, 0);
384 static int idescsi_end_request (ide_drive_t
*drive
, int uptodate
, int nrsecs
)
386 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
387 struct request
*rq
= HWGROUP(drive
)->rq
;
388 idescsi_pc_t
*pc
= (idescsi_pc_t
*) rq
->special
;
389 int log
= test_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
390 struct Scsi_Host
*host
;
394 if (!(rq
->flags
& (REQ_SPECIAL
|REQ_SENSE
))) {
395 ide_end_request(drive
, uptodate
, nrsecs
);
398 ide_end_drive_cmd (drive
, 0, 0);
399 if (rq
->flags
& REQ_SENSE
) {
400 idescsi_pc_t
*opc
= (idescsi_pc_t
*) rq
->buffer
;
402 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive
->name
, opc
->scsi_cmd
->serial_number
);
403 hexdump(pc
->buffer
,16);
405 memcpy((void *) opc
->scsi_cmd
->sense_buffer
, pc
->buffer
, SCSI_SENSE_BUFFERSIZE
);
411 pc
->scsi_cmd
->result
= (CHECK_CONDITION
<< 1) |
412 ((test_bit(PC_TIMEDOUT
, &pc
->flags
)?DID_TIME_OUT
:DID_OK
) << 16);
413 } else if (test_bit(PC_TIMEDOUT
, &pc
->flags
)) {
415 printk (KERN_WARNING
"ide-scsi: %s: timed out for %lu\n",
416 drive
->name
, pc
->scsi_cmd
->serial_number
);
417 pc
->scsi_cmd
->result
= DID_TIME_OUT
<< 16;
418 } else if (rq
->errors
>= ERROR_MAX
) {
419 pc
->scsi_cmd
->result
= DID_ERROR
<< 16;
421 printk ("ide-scsi: %s: I/O error for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
422 } else if (rq
->errors
) {
424 printk ("ide-scsi: %s: check condition for %lu\n", drive
->name
, pc
->scsi_cmd
->serial_number
);
425 if (!idescsi_check_condition(drive
, rq
))
426 /* we started a request sense, so we'll be back, exit for now */
428 pc
->scsi_cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
430 pc
->scsi_cmd
->result
= DID_OK
<< 16;
431 idescsi_transform_pc2 (drive
, pc
);
433 printk ("ide-scsi: %s: suc %lu", drive
->name
, pc
->scsi_cmd
->serial_number
);
434 if (!test_bit(PC_WRITING
, &pc
->flags
) && pc
->actually_transferred
&& pc
->actually_transferred
<= 1024 && pc
->buffer
) {
436 scsi_buf
= pc
->scsi_cmd
->request_buffer
;
437 hexdump(scsi_buf
, min_t(unsigned, 16, pc
->scsi_cmd
->request_bufflen
));
441 host
= pc
->scsi_cmd
->device
->host
;
442 spin_lock_irqsave(host
->host_lock
, flags
);
443 pc
->done(pc
->scsi_cmd
);
444 spin_unlock_irqrestore(host
->host_lock
, flags
);
451 static inline unsigned long get_timeout(idescsi_pc_t
*pc
)
453 return max_t(unsigned long, WAIT_CMD
, pc
->timeout
- jiffies
);
456 static int idescsi_expiry(ide_drive_t
*drive
)
458 idescsi_scsi_t
*scsi
= drive
->driver_data
;
459 idescsi_pc_t
*pc
= scsi
->pc
;
461 #if IDESCSI_DEBUG_LOG
462 printk(KERN_WARNING
"idescsi_expiry called for %lu at %lu\n", pc
->scsi_cmd
->serial_number
, jiffies
);
464 set_bit(PC_TIMEDOUT
, &pc
->flags
);
466 return 0; /* we do not want the ide subsystem to retry */
470 * Our interrupt handler.
472 static ide_startstop_t
idescsi_pc_intr (ide_drive_t
*drive
)
474 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
475 idescsi_pc_t
*pc
=scsi
->pc
;
476 struct request
*rq
= pc
->rq
;
477 atapi_bcount_t bcount
;
478 atapi_status_t status
;
479 atapi_ireason_t ireason
;
480 atapi_feature_t feature
;
484 #if IDESCSI_DEBUG_LOG
485 printk (KERN_INFO
"ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
486 #endif /* IDESCSI_DEBUG_LOG */
488 if (test_bit(PC_TIMEDOUT
, &pc
->flags
)){
489 #if IDESCSI_DEBUG_LOG
490 printk(KERN_WARNING
"idescsi_pc_intr: got timed out packet %lu at %lu\n",
491 pc
->scsi_cmd
->serial_number
, jiffies
);
493 /* end this request now - scsi should retry it*/
494 idescsi_end_request (drive
, 1, 0);
497 if (test_and_clear_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
)) {
498 #if IDESCSI_DEBUG_LOG
499 printk ("ide-scsi: %s: DMA complete\n", drive
->name
);
500 #endif /* IDESCSI_DEBUG_LOG */
501 pc
->actually_transferred
=pc
->request_transfer
;
502 (void) HWIF(drive
)->ide_dma_end(drive
);
506 /* Clear the interrupt */
507 status
.all
= HWIF(drive
)->INB(IDE_STATUS_REG
);
510 /* No more interrupts */
511 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
512 printk (KERN_INFO
"Packet command completed, %d bytes transferred\n", pc
->actually_transferred
);
516 idescsi_end_request (drive
, 1, 0);
519 bcount
.b
.low
= HWIF(drive
)->INB(IDE_BCOUNTL_REG
);
520 bcount
.b
.high
= HWIF(drive
)->INB(IDE_BCOUNTH_REG
);
521 ireason
.all
= HWIF(drive
)->INB(IDE_IREASON_REG
);
524 printk(KERN_ERR
"ide-scsi: CoD != 0 in idescsi_pc_intr\n");
525 return ide_do_reset (drive
);
528 temp
= pc
->actually_transferred
+ bcount
.all
;
529 if (temp
> pc
->request_transfer
) {
530 if (temp
> pc
->buffer_size
) {
531 printk(KERN_ERR
"ide-scsi: The scsi wants to "
532 "send us more data than expected "
533 "- discarding data\n");
534 temp
= pc
->buffer_size
- pc
->actually_transferred
;
536 clear_bit(PC_WRITING
, &pc
->flags
);
538 idescsi_input_buffers(drive
, pc
, temp
);
540 drive
->hwif
->atapi_input_bytes(drive
, pc
->current_position
, temp
);
541 printk(KERN_ERR
"ide-scsi: transferred %d of %d bytes\n", temp
, bcount
.all
);
543 pc
->actually_transferred
+= temp
;
544 pc
->current_position
+= temp
;
545 idescsi_discard_data(drive
, bcount
.all
- temp
);
546 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
549 #if IDESCSI_DEBUG_LOG
550 printk (KERN_NOTICE
"ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
551 #endif /* IDESCSI_DEBUG_LOG */
555 clear_bit(PC_WRITING
, &pc
->flags
);
557 idescsi_input_buffers(drive
, pc
, bcount
.all
);
559 HWIF(drive
)->atapi_input_bytes(drive
, pc
->current_position
, bcount
.all
);
561 set_bit(PC_WRITING
, &pc
->flags
);
563 idescsi_output_buffers (drive
, pc
, bcount
.all
);
565 HWIF(drive
)->atapi_output_bytes(drive
, pc
->current_position
, bcount
.all
);
567 /* Update the current position */
568 pc
->actually_transferred
+= bcount
.all
;
569 pc
->current_position
+= bcount
.all
;
571 /* And set the interrupt handler again */
572 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
576 static ide_startstop_t
idescsi_transfer_pc(ide_drive_t
*drive
)
578 ide_hwif_t
*hwif
= drive
->hwif
;
579 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
580 idescsi_pc_t
*pc
= scsi
->pc
;
581 atapi_ireason_t ireason
;
582 ide_startstop_t startstop
;
584 if (ide_wait_stat(&startstop
,drive
,DRQ_STAT
,BUSY_STAT
,WAIT_READY
)) {
585 printk(KERN_ERR
"ide-scsi: Strange, packet command "
586 "initiated yet DRQ isn't asserted\n");
589 ireason
.all
= HWIF(drive
)->INB(IDE_IREASON_REG
);
590 if (!ireason
.b
.cod
|| ireason
.b
.io
) {
591 printk(KERN_ERR
"ide-scsi: (IO,CoD) != (0,1) while "
592 "issuing a packet command\n");
593 return ide_do_reset (drive
);
595 if (HWGROUP(drive
)->handler
!= NULL
)
597 /* Set the interrupt routine */
598 ide_set_handler(drive
, &idescsi_pc_intr
, get_timeout(pc
), idescsi_expiry
);
599 /* Send the actual packet */
600 drive
->hwif
->atapi_output_bytes(drive
, scsi
->pc
->c
, 12);
601 if (test_bit (PC_DMA_OK
, &pc
->flags
)) {
602 set_bit (PC_DMA_IN_PROGRESS
, &pc
->flags
);
603 hwif
->dma_start(drive
);
608 static inline int idescsi_set_direction(idescsi_pc_t
*pc
)
611 case READ_6
: case READ_10
: case READ_12
:
612 clear_bit(PC_WRITING
, &pc
->flags
);
614 case WRITE_6
: case WRITE_10
: case WRITE_12
:
615 set_bit(PC_WRITING
, &pc
->flags
);
622 static int idescsi_map_sg(ide_drive_t
*drive
, idescsi_pc_t
*pc
)
624 ide_hwif_t
*hwif
= drive
->hwif
;
625 struct scatterlist
*sg
, *scsi_sg
;
628 if (!pc
->request_transfer
|| pc
->request_transfer
% 1024)
631 if (idescsi_set_direction(pc
))
635 scsi_sg
= pc
->scsi_cmd
->request_buffer
;
636 segments
= pc
->scsi_cmd
->use_sg
;
638 if (segments
> hwif
->sg_max_nents
)
643 sg_init_one(sg
, pc
->scsi_cmd
->request_buffer
, pc
->request_transfer
);
645 hwif
->sg_nents
= segments
;
646 memcpy(sg
, scsi_sg
, sizeof(*sg
) * segments
);
653 * Issue a packet command
655 static ide_startstop_t
idescsi_issue_pc (ide_drive_t
*drive
, idescsi_pc_t
*pc
)
657 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
658 ide_hwif_t
*hwif
= drive
->hwif
;
659 atapi_feature_t feature
;
660 atapi_bcount_t bcount
;
662 scsi
->pc
=pc
; /* Set the current packet command */
663 pc
->actually_transferred
=0; /* We haven't transferred any data yet */
664 pc
->current_position
=pc
->buffer
;
665 bcount
.all
= min(pc
->request_transfer
, 63 * 1024); /* Request to transfer the entire buffer at once */
668 if (drive
->using_dma
&& !idescsi_map_sg(drive
, pc
)) {
670 feature
.b
.dma
= !hwif
->dma_setup(drive
);
676 HWIF(drive
)->OUTB(drive
->ctl
, IDE_CONTROL_REG
);
678 HWIF(drive
)->OUTB(feature
.all
, IDE_FEATURE_REG
);
679 HWIF(drive
)->OUTB(bcount
.b
.high
, IDE_BCOUNTH_REG
);
680 HWIF(drive
)->OUTB(bcount
.b
.low
, IDE_BCOUNTL_REG
);
683 set_bit(PC_DMA_OK
, &pc
->flags
);
685 if (test_bit(IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
)) {
686 if (HWGROUP(drive
)->handler
!= NULL
)
688 ide_set_handler(drive
, &idescsi_transfer_pc
,
689 get_timeout(pc
), idescsi_expiry
);
690 /* Issue the packet command */
691 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
694 /* Issue the packet command */
695 HWIF(drive
)->OUTB(WIN_PACKETCMD
, IDE_COMMAND_REG
);
696 return idescsi_transfer_pc(drive
);
701 * idescsi_do_request is our request handling function.
703 static ide_startstop_t
idescsi_do_request (ide_drive_t
*drive
, struct request
*rq
, sector_t block
)
705 #if IDESCSI_DEBUG_LOG
706 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
);
707 printk (KERN_INFO
"sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq
->sector
,rq
->nr_sectors
,rq
->current_nr_sectors
);
708 #endif /* IDESCSI_DEBUG_LOG */
710 if (rq
->flags
& (REQ_SPECIAL
|REQ_SENSE
)) {
711 return idescsi_issue_pc (drive
, (idescsi_pc_t
*) rq
->special
);
713 blk_dump_rq_flags(rq
, "ide-scsi: unsup command");
714 idescsi_end_request (drive
, 0, 0);
718 static void idescsi_add_settings(ide_drive_t
*drive
)
720 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
723 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
725 ide_add_setting(drive
, "bios_cyl", SETTING_RW
, -1, -1, TYPE_INT
, 0, 1023, 1, 1, &drive
->bios_cyl
, NULL
);
726 ide_add_setting(drive
, "bios_head", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 255, 1, 1, &drive
->bios_head
, NULL
);
727 ide_add_setting(drive
, "bios_sect", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 63, 1, 1, &drive
->bios_sect
, NULL
);
728 ide_add_setting(drive
, "transform", SETTING_RW
, -1, -1, TYPE_INT
, 0, 3, 1, 1, &scsi
->transform
, NULL
);
729 ide_add_setting(drive
, "log", SETTING_RW
, -1, -1, TYPE_INT
, 0, 1, 1, 1, &scsi
->log
, NULL
);
733 * Driver initialization.
735 static void idescsi_setup (ide_drive_t
*drive
, idescsi_scsi_t
*scsi
)
737 if (drive
->id
&& (drive
->id
->config
& 0x0060) == 0x20)
738 set_bit (IDESCSI_DRQ_INTERRUPT
, &scsi
->flags
);
739 set_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
740 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
741 #if IDESCSI_DEBUG_LOG
742 set_bit(IDESCSI_LOG_CMD
, &scsi
->log
);
743 #endif /* IDESCSI_DEBUG_LOG */
744 idescsi_add_settings(drive
);
747 static int ide_scsi_remove(struct device
*dev
)
749 ide_drive_t
*drive
= to_ide_device(dev
);
750 struct Scsi_Host
*scsihost
= drive
->driver_data
;
751 struct ide_scsi_obj
*scsi
= scsihost_to_idescsi(scsihost
);
752 struct gendisk
*g
= scsi
->disk
;
754 ide_unregister_subdriver(drive
, scsi
->driver
);
756 ide_unregister_region(g
);
758 drive
->driver_data
= NULL
;
759 g
->private_data
= NULL
;
762 scsi_remove_host(scsihost
);
768 static int ide_scsi_probe(struct device
*);
770 #ifdef CONFIG_PROC_FS
771 static ide_proc_entry_t idescsi_proc
[] = {
772 { "capacity", S_IFREG
|S_IRUGO
, proc_ide_read_capacity
, NULL
},
773 { NULL
, 0, NULL
, NULL
}
776 # define idescsi_proc NULL
779 static ide_driver_t idescsi_driver
= {
780 .owner
= THIS_MODULE
,
783 .bus
= &ide_bus_type
,
784 .probe
= ide_scsi_probe
,
785 .remove
= ide_scsi_remove
,
787 .version
= IDESCSI_VERSION
,
789 .supports_dsc_overlap
= 0,
790 .proc
= idescsi_proc
,
791 .do_request
= idescsi_do_request
,
792 .end_request
= idescsi_end_request
,
793 .error
= idescsi_atapi_error
,
794 .abort
= idescsi_atapi_abort
,
797 static int idescsi_ide_open(struct inode
*inode
, struct file
*filp
)
799 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
800 struct ide_scsi_obj
*scsi
;
803 if (!(scsi
= ide_scsi_get(disk
)))
813 static int idescsi_ide_release(struct inode
*inode
, struct file
*filp
)
815 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
816 struct ide_scsi_obj
*scsi
= ide_scsi_g(disk
);
817 ide_drive_t
*drive
= scsi
->drive
;
826 static int idescsi_ide_ioctl(struct inode
*inode
, struct file
*file
,
827 unsigned int cmd
, unsigned long arg
)
829 struct block_device
*bdev
= inode
->i_bdev
;
830 struct ide_scsi_obj
*scsi
= ide_scsi_g(bdev
->bd_disk
);
831 return generic_ide_ioctl(scsi
->drive
, file
, bdev
, cmd
, arg
);
834 static struct block_device_operations idescsi_ops
= {
835 .owner
= THIS_MODULE
,
836 .open
= idescsi_ide_open
,
837 .release
= idescsi_ide_release
,
838 .ioctl
= idescsi_ide_ioctl
,
841 static int idescsi_slave_configure(struct scsi_device
* sdp
)
843 /* Configure detected device */
844 scsi_adjust_queue_depth(sdp
, MSG_SIMPLE_TAG
, sdp
->host
->cmd_per_lun
);
848 static const char *idescsi_info (struct Scsi_Host
*host
)
850 return "SCSI host adapter emulation for IDE ATAPI devices";
853 static int idescsi_ioctl (struct scsi_device
*dev
, int cmd
, void __user
*arg
)
855 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(dev
->host
);
857 if (cmd
== SG_SET_TRANSFORM
) {
859 set_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
861 clear_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
863 } else if (cmd
== SG_GET_TRANSFORM
)
864 return put_user(test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
), (int __user
*) arg
);
868 static inline int should_transform(ide_drive_t
*drive
, struct scsi_cmnd
*cmd
)
870 idescsi_scsi_t
*scsi
= drive_to_idescsi(drive
);
872 /* this was a layering violation and we can't support it
875 struct gendisk
*disk
= cmd
->request
->rq_disk
;
878 struct Scsi_Device_Template
**p
= disk
->private_data
;
879 if (strcmp((*p
)->scsi_driverfs_driver
.name
, "sg") == 0)
880 return test_bit(IDESCSI_SG_TRANSFORM
, &scsi
->transform
);
883 return test_bit(IDESCSI_TRANSFORM
, &scsi
->transform
);
886 static int idescsi_queue (struct scsi_cmnd
*cmd
,
887 void (*done
)(struct scsi_cmnd
*))
889 struct Scsi_Host
*host
= cmd
->device
->host
;
890 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(host
);
891 ide_drive_t
*drive
= scsi
->drive
;
892 struct request
*rq
= NULL
;
893 idescsi_pc_t
*pc
= NULL
;
896 printk (KERN_ERR
"ide-scsi: drive id %d not present\n", cmd
->device
->id
);
899 scsi
= drive_to_idescsi(drive
);
900 pc
= kmalloc (sizeof (idescsi_pc_t
), GFP_ATOMIC
);
901 rq
= kmalloc (sizeof (struct request
), GFP_ATOMIC
);
902 if (rq
== NULL
|| pc
== NULL
) {
903 printk (KERN_ERR
"ide-scsi: %s: out of memory\n", drive
->name
);
907 memset (pc
->c
, 0, 12);
910 memcpy (pc
->c
, cmd
->cmnd
, cmd
->cmd_len
);
913 pc
->sg
= cmd
->request_buffer
;
915 pc
->buffer
= cmd
->request_buffer
;
919 pc
->request_transfer
= pc
->buffer_size
= cmd
->request_bufflen
;
922 pc
->timeout
= jiffies
+ cmd
->timeout_per_command
;
924 if (should_transform(drive
, cmd
))
925 set_bit(PC_TRANSFORM
, &pc
->flags
);
926 idescsi_transform_pc1 (drive
, pc
);
928 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
)) {
929 printk ("ide-scsi: %s: que %lu, cmd = ", drive
->name
, cmd
->serial_number
);
930 hexdump(cmd
->cmnd
, cmd
->cmd_len
);
931 if (memcmp(pc
->c
, cmd
->cmnd
, cmd
->cmd_len
)) {
932 printk ("ide-scsi: %s: que %lu, tsl = ", drive
->name
, cmd
->serial_number
);
937 ide_init_drive_cmd (rq
);
938 rq
->special
= (char *) pc
;
939 rq
->flags
= REQ_SPECIAL
;
940 spin_unlock_irq(host
->host_lock
);
941 rq
->rq_disk
= scsi
->disk
;
942 (void) ide_do_drive_cmd (drive
, rq
, ide_end
);
943 spin_lock_irq(host
->host_lock
);
948 cmd
->result
= DID_ERROR
<< 16;
953 static int idescsi_eh_abort (struct scsi_cmnd
*cmd
)
955 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(cmd
->device
->host
);
956 ide_drive_t
*drive
= scsi
->drive
;
960 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
962 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
963 printk (KERN_WARNING
"ide-scsi: abort called for %lu\n", cmd
->serial_number
);
966 printk (KERN_WARNING
"ide-scsi: Drive not set in idescsi_eh_abort\n");
971 /* First give it some more time, how much is "right" is hard to say :-( */
973 busy
= ide_wait_not_busy(HWIF(drive
), 100); /* FIXME - uses mdelay which causes latency? */
974 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
975 printk (KERN_WARNING
"ide-scsi: drive did%s become ready\n", busy
?" not":"");
977 spin_lock_irq(&ide_lock
);
979 /* If there is no pc running we're done (our interrupt took care of it) */
985 /* It's somewhere in flight. Does ide subsystem agree? */
986 if (scsi
->pc
->scsi_cmd
->serial_number
== cmd
->serial_number
&& !busy
&&
987 elv_queue_empty(drive
->queue
) && HWGROUP(drive
)->rq
!= scsi
->pc
->rq
) {
989 * FIXME - not sure this condition can ever occur
991 printk (KERN_ERR
"ide-scsi: cmd aborted!\n");
993 if (scsi
->pc
->rq
->flags
& REQ_SENSE
)
994 kfree(scsi
->pc
->buffer
);
1003 spin_unlock_irq(&ide_lock
);
1005 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
1006 printk (KERN_WARNING
"ide-scsi: abort returns %s\n", ret
== SUCCESS
?"success":"failed");
1011 static int idescsi_eh_reset (struct scsi_cmnd
*cmd
)
1013 struct request
*req
;
1014 idescsi_scsi_t
*scsi
= scsihost_to_idescsi(cmd
->device
->host
);
1015 ide_drive_t
*drive
= scsi
->drive
;
1019 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
1021 if (test_bit(IDESCSI_LOG_CMD
, &scsi
->log
))
1022 printk (KERN_WARNING
"ide-scsi: reset called for %lu\n", cmd
->serial_number
);
1025 printk (KERN_WARNING
"ide-scsi: Drive not set in idescsi_eh_reset\n");
1030 spin_lock_irq(cmd
->device
->host
->host_lock
);
1031 spin_lock(&ide_lock
);
1033 if (!scsi
->pc
|| (req
= scsi
->pc
->rq
) != HWGROUP(drive
)->rq
|| !HWGROUP(drive
)->handler
) {
1034 printk (KERN_WARNING
"ide-scsi: No active request in idescsi_eh_reset\n");
1035 spin_unlock(&ide_lock
);
1036 spin_unlock_irq(cmd
->device
->host
->host_lock
);
1040 /* kill current request */
1041 blkdev_dequeue_request(req
);
1042 end_that_request_last(req
);
1043 if (req
->flags
& REQ_SENSE
)
1044 kfree(scsi
->pc
->buffer
);
1049 /* now nuke the drive queue */
1050 while ((req
= elv_next_request(drive
->queue
))) {
1051 blkdev_dequeue_request(req
);
1052 end_that_request_last(req
);
1055 HWGROUP(drive
)->rq
= NULL
;
1056 HWGROUP(drive
)->handler
= NULL
;
1057 HWGROUP(drive
)->busy
= 1; /* will set this to zero when ide reset finished */
1058 spin_unlock(&ide_lock
);
1060 ide_do_reset(drive
);
1062 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1065 spin_unlock_irq(cmd
->device
->host
->host_lock
);
1067 spin_lock_irq(cmd
->device
->host
->host_lock
);
1068 } while ( HWGROUP(drive
)->handler
);
1070 ready
= drive_is_ready(drive
);
1071 HWGROUP(drive
)->busy
--;
1073 printk (KERN_ERR
"ide-scsi: reset failed!\n");
1077 spin_unlock_irq(cmd
->device
->host
->host_lock
);
1081 static int idescsi_bios(struct scsi_device
*sdev
, struct block_device
*bdev
,
1082 sector_t capacity
, int *parm
)
1084 idescsi_scsi_t
*idescsi
= scsihost_to_idescsi(sdev
->host
);
1085 ide_drive_t
*drive
= idescsi
->drive
;
1087 if (drive
->bios_cyl
&& drive
->bios_head
&& drive
->bios_sect
) {
1088 parm
[0] = drive
->bios_head
;
1089 parm
[1] = drive
->bios_sect
;
1090 parm
[2] = drive
->bios_cyl
;
1095 static struct scsi_host_template idescsi_template
= {
1096 .module
= THIS_MODULE
,
1098 .info
= idescsi_info
,
1099 .slave_configure
= idescsi_slave_configure
,
1100 .ioctl
= idescsi_ioctl
,
1101 .queuecommand
= idescsi_queue
,
1102 .eh_abort_handler
= idescsi_eh_abort
,
1103 .eh_host_reset_handler
= idescsi_eh_reset
,
1104 .bios_param
= idescsi_bios
,
1107 .sg_tablesize
= 256,
1110 .use_clustering
= DISABLE_CLUSTERING
,
1112 .proc_name
= "ide-scsi",
1115 static int ide_scsi_probe(struct device
*dev
)
1117 ide_drive_t
*drive
= to_ide_device(dev
);
1118 idescsi_scsi_t
*idescsi
;
1119 struct Scsi_Host
*host
;
1124 if (!warned
&& drive
->media
== ide_cdrom
) {
1125 printk(KERN_WARNING
"ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1129 if (!strstr("ide-scsi", drive
->driver_req
) ||
1131 drive
->media
== ide_disk
||
1132 !(host
= scsi_host_alloc(&idescsi_template
,sizeof(idescsi_scsi_t
))))
1135 g
= alloc_disk(1 << PARTN_BITS
);
1139 ide_init_disk(g
, drive
);
1143 #if IDESCSI_DEBUG_LOG
1144 if (drive
->id
->last_lun
)
1145 printk(KERN_NOTICE
"%s: id->last_lun=%u\n", drive
->name
, drive
->id
->last_lun
);
1147 if ((drive
->id
->last_lun
& 0x7) != 7)
1148 host
->max_lun
= (drive
->id
->last_lun
& 0x7) + 1;
1152 drive
->driver_data
= host
;
1153 idescsi
= scsihost_to_idescsi(host
);
1154 idescsi
->drive
= drive
;
1155 idescsi
->driver
= &idescsi_driver
;
1156 idescsi
->host
= host
;
1158 g
->private_data
= &idescsi
->driver
;
1159 ide_register_subdriver(drive
, &idescsi_driver
);
1161 idescsi_setup(drive
, idescsi
);
1162 g
->fops
= &idescsi_ops
;
1163 ide_register_region(g
);
1164 err
= scsi_add_host(host
, &drive
->gendev
);
1166 scsi_scan_host(host
);
1169 /* fall through on error */
1170 ide_unregister_region(g
);
1171 ide_unregister_subdriver(drive
, &idescsi_driver
);
1175 scsi_host_put(host
);
1179 static int __init
init_idescsi_module(void)
1181 return driver_register(&idescsi_driver
.gen_driver
);
1184 static void __exit
exit_idescsi_module(void)
1186 driver_unregister(&idescsi_driver
.gen_driver
);
1189 module_init(init_idescsi_module
);
1190 module_exit(exit_idescsi_module
);
1191 MODULE_LICENSE("GPL");