Driver core: remove subsys_set_kset
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / ide-scsi.c
blob1cc01acc28089060b6aa39f677e630b06c3a83f9
1 /*
2 * linux/drivers/scsi/ide-scsi.c Version 0.9 Jul 4, 1999
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 */
7 /*
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/types.h>
38 #include <linux/string.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/ioport.h>
42 #include <linux/blkdev.h>
43 #include <linux/errno.h>
44 #include <linux/hdreg.h>
45 #include <linux/slab.h>
46 #include <linux/ide.h>
47 #include <linux/scatterlist.h>
48 #include <linux/delay.h>
49 #include <linux/mutex.h>
51 #include <asm/io.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>
60 #include <scsi/sg.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 */
78 } idescsi_pc_t;
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 */
96 * Log flags
98 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
100 typedef struct ide_scsi_obj {
101 ide_drive_t *drive;
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 */
110 } idescsi_scsi_t;
112 static DEFINE_MUTEX(idescsi_ref_mutex);
113 static int idescsi_nocd; /* Set by module param to skip cd */
115 #define ide_scsi_g(disk) \
116 container_of((disk)->private_data, struct ide_scsi_obj, driver)
118 static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
120 struct ide_scsi_obj *scsi = NULL;
122 mutex_lock(&idescsi_ref_mutex);
123 scsi = ide_scsi_g(disk);
124 if (scsi)
125 scsi_host_get(scsi->host);
126 mutex_unlock(&idescsi_ref_mutex);
127 return scsi;
130 static void ide_scsi_put(struct ide_scsi_obj *scsi)
132 mutex_lock(&idescsi_ref_mutex);
133 scsi_host_put(scsi->host);
134 mutex_unlock(&idescsi_ref_mutex);
137 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
139 return (idescsi_scsi_t*) (&host[1]);
142 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
144 return scsihost_to_idescsi(ide_drive->driver_data);
148 * Per ATAPI device status bits.
150 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
153 * ide-scsi requests.
155 #define IDESCSI_PC_RQ 90
157 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
159 while (bcount--)
160 (void) HWIF(drive)->INB(IDE_DATA_REG);
163 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
165 while (bcount--)
166 HWIF(drive)->OUTB(0, IDE_DATA_REG);
170 * PIO data transfer routines using the scatter gather table.
172 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
174 int count;
175 char *buf;
177 while (bcount) {
178 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
179 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
180 idescsi_discard_data (drive, bcount);
181 return;
183 count = min(pc->sg->length - pc->b_count, bcount);
184 if (PageHighMem(pc->sg->page)) {
185 unsigned long flags;
187 local_irq_save(flags);
188 buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
189 pc->sg->offset;
190 drive->hwif->atapi_input_bytes(drive,
191 buf + pc->b_count, count);
192 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
193 local_irq_restore(flags);
194 } else {
195 buf = page_address(pc->sg->page) + pc->sg->offset;
196 drive->hwif->atapi_input_bytes(drive,
197 buf + pc->b_count, count);
199 bcount -= count; pc->b_count += count;
200 if (pc->b_count == pc->sg->length) {
201 pc->sg++;
202 pc->b_count = 0;
207 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
209 int count;
210 char *buf;
212 while (bcount) {
213 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
214 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
215 idescsi_output_zeros (drive, bcount);
216 return;
218 count = min(pc->sg->length - pc->b_count, bcount);
219 if (PageHighMem(pc->sg->page)) {
220 unsigned long flags;
222 local_irq_save(flags);
223 buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
224 pc->sg->offset;
225 drive->hwif->atapi_output_bytes(drive,
226 buf + pc->b_count, count);
227 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
228 local_irq_restore(flags);
229 } else {
230 buf = page_address(pc->sg->page) + pc->sg->offset;
231 drive->hwif->atapi_output_bytes(drive,
232 buf + pc->b_count, count);
234 bcount -= count; pc->b_count += count;
235 if (pc->b_count == pc->sg->length) {
236 pc->sg++;
237 pc->b_count = 0;
243 * Most of the SCSI commands are supported directly by ATAPI devices.
244 * idescsi_transform_pc handles the few exceptions.
246 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
248 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
249 char *atapi_buf;
251 if (!test_bit(PC_TRANSFORM, &pc->flags))
252 return;
253 if (drive->media == ide_cdrom || drive->media == ide_optical) {
254 if (c[0] == READ_6 || c[0] == WRITE_6) {
255 c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
256 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
257 c[0] += (READ_10 - READ_6);
259 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
260 unsigned short new_len;
261 if (!scsi_buf)
262 return;
263 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
264 return;
265 memset(atapi_buf, 0, pc->buffer_size + 4);
266 memset (c, 0, 12);
267 c[0] = sc[0] | 0x40;
268 c[1] = sc[1];
269 c[2] = sc[2];
270 new_len = sc[4] + 4;
271 c[8] = new_len;
272 c[7] = new_len >> 8;
273 c[9] = sc[5];
274 if (c[0] == MODE_SELECT_10) {
275 atapi_buf[1] = scsi_buf[0]; /* Mode data length */
276 atapi_buf[2] = scsi_buf[1]; /* Medium type */
277 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
278 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
279 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
281 pc->buffer = atapi_buf;
282 pc->request_transfer += 4;
283 pc->buffer_size += 4;
288 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
290 u8 *atapi_buf = pc->buffer;
291 u8 *sc = pc->scsi_cmd->cmnd;
292 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
294 if (!test_bit(PC_TRANSFORM, &pc->flags))
295 return;
296 if (drive->media == ide_cdrom || drive->media == ide_optical) {
297 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
298 scsi_buf[0] = atapi_buf[1]; /* Mode data length */
299 scsi_buf[1] = atapi_buf[2]; /* Medium type */
300 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
301 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
302 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
304 if (pc->c[0] == INQUIRY) {
305 scsi_buf[2] |= 2; /* ansi_revision */
306 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
309 if (atapi_buf && atapi_buf != scsi_buf)
310 kfree(atapi_buf);
313 static void hexdump(u8 *x, int len)
315 int i;
317 printk("[ ");
318 for (i = 0; i < len; i++)
319 printk("%x ", x[i]);
320 printk("]\n");
323 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
325 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
326 idescsi_pc_t *pc;
327 struct request *rq;
328 u8 *buf;
330 /* stuff a sense request in front of our current request */
331 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
332 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
333 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
334 if (!pc || !rq || !buf) {
335 kfree(buf);
336 kfree(rq);
337 kfree(pc);
338 return -ENOMEM;
340 ide_init_drive_cmd(rq);
341 rq->special = (char *) pc;
342 pc->rq = rq;
343 pc->buffer = buf;
344 pc->c[0] = REQUEST_SENSE;
345 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
346 rq->cmd_type = REQ_TYPE_SENSE;
347 pc->timeout = jiffies + WAIT_READY;
348 /* NOTE! Save the failed packet command in "rq->buffer" */
349 rq->buffer = (void *) failed_command->special;
350 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
351 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
352 printk ("ide-scsi: %s: queue cmd = ", drive->name);
353 hexdump(pc->c, 6);
355 rq->rq_disk = scsi->disk;
356 return ide_do_drive_cmd(drive, rq, ide_preempt);
359 static int idescsi_end_request(ide_drive_t *, int, int);
361 static ide_startstop_t
362 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
364 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
365 /* force an abort */
366 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
368 rq->errors++;
370 idescsi_end_request(drive, 0, 0);
372 return ide_stopped;
375 static ide_startstop_t
376 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
378 #if IDESCSI_DEBUG_LOG
379 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
380 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
381 #endif
382 rq->errors |= ERROR_MAX;
384 idescsi_end_request(drive, 0, 0);
386 return ide_stopped;
389 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
391 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
392 struct request *rq = HWGROUP(drive)->rq;
393 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
394 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
395 struct Scsi_Host *host;
396 u8 *scsi_buf;
397 int errors = rq->errors;
398 unsigned long flags;
400 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
401 ide_end_request(drive, uptodate, nrsecs);
402 return 0;
404 ide_end_drive_cmd (drive, 0, 0);
405 if (blk_sense_request(rq)) {
406 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
407 if (log) {
408 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
409 hexdump(pc->buffer,16);
411 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
412 kfree(pc->buffer);
413 kfree(pc);
414 kfree(rq);
415 pc = opc;
416 rq = pc->rq;
417 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
418 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
419 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
420 if (log)
421 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
422 drive->name, pc->scsi_cmd->serial_number);
423 pc->scsi_cmd->result = DID_TIME_OUT << 16;
424 } else if (errors >= ERROR_MAX) {
425 pc->scsi_cmd->result = DID_ERROR << 16;
426 if (log)
427 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
428 } else if (errors) {
429 if (log)
430 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
431 if (!idescsi_check_condition(drive, rq))
432 /* we started a request sense, so we'll be back, exit for now */
433 return 0;
434 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
435 } else {
436 pc->scsi_cmd->result = DID_OK << 16;
437 idescsi_transform_pc2 (drive, pc);
438 if (log) {
439 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
440 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
441 printk(", rst = ");
442 scsi_buf = pc->scsi_cmd->request_buffer;
443 hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
444 } else printk("\n");
447 host = pc->scsi_cmd->device->host;
448 spin_lock_irqsave(host->host_lock, flags);
449 pc->done(pc->scsi_cmd);
450 spin_unlock_irqrestore(host->host_lock, flags);
451 kfree(pc);
452 kfree(rq);
453 scsi->pc = NULL;
454 return 0;
457 static inline unsigned long get_timeout(idescsi_pc_t *pc)
459 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
462 static int idescsi_expiry(ide_drive_t *drive)
464 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
465 idescsi_pc_t *pc = scsi->pc;
467 #if IDESCSI_DEBUG_LOG
468 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
469 #endif
470 set_bit(PC_TIMEDOUT, &pc->flags);
472 return 0; /* we do not want the ide subsystem to retry */
476 * Our interrupt handler.
478 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
480 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
481 idescsi_pc_t *pc=scsi->pc;
482 struct request *rq = pc->rq;
483 atapi_bcount_t bcount;
484 atapi_status_t status;
485 atapi_ireason_t ireason;
486 atapi_feature_t feature;
488 unsigned int temp;
490 #if IDESCSI_DEBUG_LOG
491 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
492 #endif /* IDESCSI_DEBUG_LOG */
494 if (test_bit(PC_TIMEDOUT, &pc->flags)){
495 #if IDESCSI_DEBUG_LOG
496 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
497 pc->scsi_cmd->serial_number, jiffies);
498 #endif
499 /* end this request now - scsi should retry it*/
500 idescsi_end_request (drive, 1, 0);
501 return ide_stopped;
503 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
504 #if IDESCSI_DEBUG_LOG
505 printk ("ide-scsi: %s: DMA complete\n", drive->name);
506 #endif /* IDESCSI_DEBUG_LOG */
507 pc->actually_transferred=pc->request_transfer;
508 (void) HWIF(drive)->ide_dma_end(drive);
511 feature.all = 0;
512 /* Clear the interrupt */
513 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
515 if (!status.b.drq) {
516 /* No more interrupts */
517 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
518 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
519 local_irq_enable_in_hardirq();
520 if (status.b.check)
521 rq->errors++;
522 idescsi_end_request (drive, 1, 0);
523 return ide_stopped;
525 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
526 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
527 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
529 if (ireason.b.cod) {
530 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
531 return ide_do_reset (drive);
533 if (ireason.b.io) {
534 temp = pc->actually_transferred + bcount.all;
535 if (temp > pc->request_transfer) {
536 if (temp > pc->buffer_size) {
537 printk(KERN_ERR "ide-scsi: The scsi wants to "
538 "send us more data than expected "
539 "- discarding data\n");
540 temp = pc->buffer_size - pc->actually_transferred;
541 if (temp) {
542 clear_bit(PC_WRITING, &pc->flags);
543 if (pc->sg)
544 idescsi_input_buffers(drive, pc, temp);
545 else
546 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
547 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
549 pc->actually_transferred += temp;
550 pc->current_position += temp;
551 idescsi_discard_data(drive, bcount.all - temp);
552 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
553 return ide_started;
555 #if IDESCSI_DEBUG_LOG
556 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
557 #endif /* IDESCSI_DEBUG_LOG */
560 if (ireason.b.io) {
561 clear_bit(PC_WRITING, &pc->flags);
562 if (pc->sg)
563 idescsi_input_buffers(drive, pc, bcount.all);
564 else
565 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
566 } else {
567 set_bit(PC_WRITING, &pc->flags);
568 if (pc->sg)
569 idescsi_output_buffers (drive, pc, bcount.all);
570 else
571 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
573 /* Update the current position */
574 pc->actually_transferred += bcount.all;
575 pc->current_position += bcount.all;
577 /* And set the interrupt handler again */
578 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
579 return ide_started;
582 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
584 ide_hwif_t *hwif = drive->hwif;
585 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
586 idescsi_pc_t *pc = scsi->pc;
587 atapi_ireason_t ireason;
588 ide_startstop_t startstop;
590 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
591 printk(KERN_ERR "ide-scsi: Strange, packet command "
592 "initiated yet DRQ isn't asserted\n");
593 return startstop;
595 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
596 if (!ireason.b.cod || ireason.b.io) {
597 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
598 "issuing a packet command\n");
599 return ide_do_reset (drive);
601 BUG_ON(HWGROUP(drive)->handler != NULL);
602 /* Set the interrupt routine */
603 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
604 /* Send the actual packet */
605 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
606 if (test_bit (PC_DMA_OK, &pc->flags)) {
607 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
608 hwif->dma_start(drive);
610 return ide_started;
613 static inline int idescsi_set_direction(idescsi_pc_t *pc)
615 switch (pc->c[0]) {
616 case READ_6: case READ_10: case READ_12:
617 clear_bit(PC_WRITING, &pc->flags);
618 return 0;
619 case WRITE_6: case WRITE_10: case WRITE_12:
620 set_bit(PC_WRITING, &pc->flags);
621 return 0;
622 default:
623 return 1;
627 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
629 ide_hwif_t *hwif = drive->hwif;
630 struct scatterlist *sg, *scsi_sg;
631 int segments;
633 if (!pc->request_transfer || pc->request_transfer % 1024)
634 return 1;
636 if (idescsi_set_direction(pc))
637 return 1;
639 sg = hwif->sg_table;
640 scsi_sg = pc->scsi_cmd->request_buffer;
641 segments = pc->scsi_cmd->use_sg;
643 if (segments > hwif->sg_max_nents)
644 return 1;
646 if (!segments) {
647 hwif->sg_nents = 1;
648 sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
649 } else {
650 hwif->sg_nents = segments;
651 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
654 return 0;
658 * Issue a packet command
660 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
662 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
663 ide_hwif_t *hwif = drive->hwif;
664 atapi_feature_t feature;
665 atapi_bcount_t bcount;
667 scsi->pc=pc; /* Set the current packet command */
668 pc->actually_transferred=0; /* We haven't transferred any data yet */
669 pc->current_position=pc->buffer;
670 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
672 feature.all = 0;
673 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
674 hwif->sg_mapped = 1;
675 feature.b.dma = !hwif->dma_setup(drive);
676 hwif->sg_mapped = 0;
679 SELECT_DRIVE(drive);
680 if (IDE_CONTROL_REG)
681 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
683 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
684 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
685 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
687 if (feature.b.dma)
688 set_bit(PC_DMA_OK, &pc->flags);
690 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
691 BUG_ON(HWGROUP(drive)->handler != NULL);
692 ide_set_handler(drive, &idescsi_transfer_pc,
693 get_timeout(pc), idescsi_expiry);
694 /* Issue the packet command */
695 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
696 return ide_started;
697 } else {
698 /* Issue the packet command */
699 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
700 return idescsi_transfer_pc(drive);
705 * idescsi_do_request is our request handling function.
707 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
709 #if IDESCSI_DEBUG_LOG
710 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
711 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
712 #endif /* IDESCSI_DEBUG_LOG */
714 if (blk_sense_request(rq) || blk_special_request(rq)) {
715 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
717 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
718 idescsi_end_request (drive, 0, 0);
719 return ide_stopped;
722 #ifdef CONFIG_IDE_PROC_FS
723 static void idescsi_add_settings(ide_drive_t *drive)
725 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
728 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
730 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
731 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
732 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
733 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
734 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
736 #else
737 static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
738 #endif
741 * Driver initialization.
743 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
745 if (drive->id && (drive->id->config & 0x0060) == 0x20)
746 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
747 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
748 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
749 #if IDESCSI_DEBUG_LOG
750 set_bit(IDESCSI_LOG_CMD, &scsi->log);
751 #endif /* IDESCSI_DEBUG_LOG */
752 idescsi_add_settings(drive);
755 static void ide_scsi_remove(ide_drive_t *drive)
757 struct Scsi_Host *scsihost = drive->driver_data;
758 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
759 struct gendisk *g = scsi->disk;
761 ide_proc_unregister_driver(drive, scsi->driver);
763 ide_unregister_region(g);
765 drive->driver_data = NULL;
766 g->private_data = NULL;
767 put_disk(g);
769 scsi_remove_host(scsihost);
770 ide_scsi_put(scsi);
773 static int ide_scsi_probe(ide_drive_t *);
775 #ifdef CONFIG_IDE_PROC_FS
776 static ide_proc_entry_t idescsi_proc[] = {
777 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
778 { NULL, 0, NULL, NULL }
780 #endif
782 static ide_driver_t idescsi_driver = {
783 .gen_driver = {
784 .owner = THIS_MODULE,
785 .name = "ide-scsi",
786 .bus = &ide_bus_type,
788 .probe = ide_scsi_probe,
789 .remove = ide_scsi_remove,
790 .version = IDESCSI_VERSION,
791 .media = ide_scsi,
792 .supports_dsc_overlap = 0,
793 .do_request = idescsi_do_request,
794 .end_request = idescsi_end_request,
795 .error = idescsi_atapi_error,
796 .abort = idescsi_atapi_abort,
797 #ifdef CONFIG_IDE_PROC_FS
798 .proc = idescsi_proc,
799 #endif
802 static int idescsi_ide_open(struct inode *inode, struct file *filp)
804 struct gendisk *disk = inode->i_bdev->bd_disk;
805 struct ide_scsi_obj *scsi;
807 if (!(scsi = ide_scsi_get(disk)))
808 return -ENXIO;
810 return 0;
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);
818 ide_scsi_put(scsi);
820 return 0;
823 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
824 unsigned int cmd, unsigned long arg)
826 struct block_device *bdev = inode->i_bdev;
827 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
828 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
831 static struct block_device_operations idescsi_ops = {
832 .owner = THIS_MODULE,
833 .open = idescsi_ide_open,
834 .release = idescsi_ide_release,
835 .ioctl = idescsi_ide_ioctl,
838 static int idescsi_slave_configure(struct scsi_device * sdp)
840 /* Configure detected device */
841 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
842 return 0;
845 static const char *idescsi_info (struct Scsi_Host *host)
847 return "SCSI host adapter emulation for IDE ATAPI devices";
850 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
852 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
854 if (cmd == SG_SET_TRANSFORM) {
855 if (arg)
856 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
857 else
858 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
859 return 0;
860 } else if (cmd == SG_GET_TRANSFORM)
861 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
862 return -EINVAL;
865 static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
867 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
869 /* this was a layering violation and we can't support it
870 anymore, sorry. */
871 #if 0
872 struct gendisk *disk = cmd->request->rq_disk;
874 if (disk) {
875 struct struct scsi_device_Template **p = disk->private_data;
876 if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
877 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
879 #endif
880 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
883 static int idescsi_queue (struct scsi_cmnd *cmd,
884 void (*done)(struct scsi_cmnd *))
886 struct Scsi_Host *host = cmd->device->host;
887 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
888 ide_drive_t *drive = scsi->drive;
889 struct request *rq = NULL;
890 idescsi_pc_t *pc = NULL;
892 if (!drive) {
893 scmd_printk (KERN_ERR, cmd, "drive not present\n");
894 goto abort;
896 scsi = drive_to_idescsi(drive);
897 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
898 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
899 if (rq == NULL || pc == NULL) {
900 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
901 goto abort;
904 memset (pc->c, 0, 12);
905 pc->flags = 0;
906 pc->rq = rq;
907 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
908 if (cmd->use_sg) {
909 pc->buffer = NULL;
910 pc->sg = cmd->request_buffer;
911 } else {
912 pc->buffer = cmd->request_buffer;
913 pc->sg = NULL;
915 pc->b_count = 0;
916 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
917 pc->scsi_cmd = cmd;
918 pc->done = done;
919 pc->timeout = jiffies + cmd->timeout_per_command;
921 if (should_transform(drive, cmd))
922 set_bit(PC_TRANSFORM, &pc->flags);
923 idescsi_transform_pc1 (drive, pc);
925 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
926 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
927 hexdump(cmd->cmnd, cmd->cmd_len);
928 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
929 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
930 hexdump(pc->c, 12);
934 ide_init_drive_cmd (rq);
935 rq->special = (char *) pc;
936 rq->cmd_type = REQ_TYPE_SPECIAL;
937 spin_unlock_irq(host->host_lock);
938 rq->rq_disk = scsi->disk;
939 (void) ide_do_drive_cmd (drive, rq, ide_end);
940 spin_lock_irq(host->host_lock);
941 return 0;
942 abort:
943 kfree (pc);
944 kfree (rq);
945 cmd->result = DID_ERROR << 16;
946 done(cmd);
947 return 0;
950 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
952 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
953 ide_drive_t *drive = scsi->drive;
954 int busy;
955 int ret = FAILED;
957 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
959 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
960 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
962 if (!drive) {
963 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
964 WARN_ON(1);
965 goto no_drive;
968 /* First give it some more time, how much is "right" is hard to say :-( */
970 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
971 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
972 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
974 spin_lock_irq(&ide_lock);
976 /* If there is no pc running we're done (our interrupt took care of it) */
977 if (!scsi->pc) {
978 ret = SUCCESS;
979 goto ide_unlock;
982 /* It's somewhere in flight. Does ide subsystem agree? */
983 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
984 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
986 * FIXME - not sure this condition can ever occur
988 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
990 if (blk_sense_request(scsi->pc->rq))
991 kfree(scsi->pc->buffer);
992 kfree(scsi->pc->rq);
993 kfree(scsi->pc);
994 scsi->pc = NULL;
996 ret = SUCCESS;
999 ide_unlock:
1000 spin_unlock_irq(&ide_lock);
1001 no_drive:
1002 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
1003 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
1005 return ret;
1008 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
1010 struct request *req;
1011 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
1012 ide_drive_t *drive = scsi->drive;
1013 int ready = 0;
1014 int ret = SUCCESS;
1016 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
1018 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
1019 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
1021 if (!drive) {
1022 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
1023 WARN_ON(1);
1024 return FAILED;
1027 spin_lock_irq(cmd->device->host->host_lock);
1028 spin_lock(&ide_lock);
1030 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
1031 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
1032 spin_unlock(&ide_lock);
1033 spin_unlock_irq(cmd->device->host->host_lock);
1034 return FAILED;
1037 /* kill current request */
1038 blkdev_dequeue_request(req);
1039 end_that_request_last(req, 0);
1040 if (blk_sense_request(req))
1041 kfree(scsi->pc->buffer);
1042 kfree(scsi->pc);
1043 scsi->pc = NULL;
1044 kfree(req);
1046 /* now nuke the drive queue */
1047 while ((req = elv_next_request(drive->queue))) {
1048 blkdev_dequeue_request(req);
1049 end_that_request_last(req, 0);
1052 HWGROUP(drive)->rq = NULL;
1053 HWGROUP(drive)->handler = NULL;
1054 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
1055 spin_unlock(&ide_lock);
1057 ide_do_reset(drive);
1059 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1061 do {
1062 spin_unlock_irq(cmd->device->host->host_lock);
1063 msleep(50);
1064 spin_lock_irq(cmd->device->host->host_lock);
1065 } while ( HWGROUP(drive)->handler );
1067 ready = drive_is_ready(drive);
1068 HWGROUP(drive)->busy--;
1069 if (!ready) {
1070 printk (KERN_ERR "ide-scsi: reset failed!\n");
1071 ret = FAILED;
1074 spin_unlock_irq(cmd->device->host->host_lock);
1075 return ret;
1078 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
1079 sector_t capacity, int *parm)
1081 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
1082 ide_drive_t *drive = idescsi->drive;
1084 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1085 parm[0] = drive->bios_head;
1086 parm[1] = drive->bios_sect;
1087 parm[2] = drive->bios_cyl;
1089 return 0;
1092 static struct scsi_host_template idescsi_template = {
1093 .module = THIS_MODULE,
1094 .name = "idescsi",
1095 .info = idescsi_info,
1096 .slave_configure = idescsi_slave_configure,
1097 .ioctl = idescsi_ioctl,
1098 .queuecommand = idescsi_queue,
1099 .eh_abort_handler = idescsi_eh_abort,
1100 .eh_host_reset_handler = idescsi_eh_reset,
1101 .bios_param = idescsi_bios,
1102 .can_queue = 40,
1103 .this_id = -1,
1104 .sg_tablesize = 256,
1105 .cmd_per_lun = 5,
1106 .max_sectors = 128,
1107 .use_clustering = DISABLE_CLUSTERING,
1108 .emulated = 1,
1109 .proc_name = "ide-scsi",
1112 static int ide_scsi_probe(ide_drive_t *drive)
1114 idescsi_scsi_t *idescsi;
1115 struct Scsi_Host *host;
1116 struct gendisk *g;
1117 static int warned;
1118 int err = -ENOMEM;
1120 if (!warned && drive->media == ide_cdrom) {
1121 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1122 warned = 1;
1125 if (idescsi_nocd && drive->media == ide_cdrom)
1126 return -ENODEV;
1128 if (!strstr("ide-scsi", drive->driver_req) ||
1129 !drive->present ||
1130 drive->media == ide_disk ||
1131 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1132 return -ENODEV;
1134 g = alloc_disk(1 << PARTN_BITS);
1135 if (!g)
1136 goto out_host_put;
1138 ide_init_disk(g, drive);
1140 host->max_id = 1;
1142 #if IDESCSI_DEBUG_LOG
1143 if (drive->id->last_lun)
1144 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1145 #endif
1146 if ((drive->id->last_lun & 0x7) != 7)
1147 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1148 else
1149 host->max_lun = 1;
1151 drive->driver_data = host;
1152 idescsi = scsihost_to_idescsi(host);
1153 idescsi->drive = drive;
1154 idescsi->driver = &idescsi_driver;
1155 idescsi->host = host;
1156 idescsi->disk = g;
1157 g->private_data = &idescsi->driver;
1158 ide_proc_register_driver(drive, &idescsi_driver);
1159 err = 0;
1160 idescsi_setup(drive, idescsi);
1161 g->fops = &idescsi_ops;
1162 ide_register_region(g);
1163 err = scsi_add_host(host, &drive->gendev);
1164 if (!err) {
1165 scsi_scan_host(host);
1166 return 0;
1168 /* fall through on error */
1169 ide_unregister_region(g);
1170 ide_proc_unregister_driver(drive, &idescsi_driver);
1172 put_disk(g);
1173 out_host_put:
1174 scsi_host_put(host);
1175 return err;
1178 static int __init init_idescsi_module(void)
1180 return driver_register(&idescsi_driver.gen_driver);
1183 static void __exit exit_idescsi_module(void)
1185 driver_unregister(&idescsi_driver.gen_driver);
1188 module_param(idescsi_nocd, int, 0600);
1189 MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
1190 module_init(init_idescsi_module);
1191 module_exit(exit_idescsi_module);
1192 MODULE_LICENSE("GPL");