Import 2.3.36
[davej-history.git] / drivers / scsi / ide-scsi.c
blob6554ee2f0046b18dc7c1fe9209a9e2cea9636f39
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.
32 #define IDESCSI_VERSION "0.9"
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/ioport.h>
40 #include <linux/blkdev.h>
41 #include <linux/errno.h>
42 #include <linux/hdreg.h>
43 #include <linux/malloc.h>
44 #include <linux/ide.h>
46 #include <asm/io.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
50 #include "scsi.h"
51 #include "hosts.h"
52 #include "sd.h"
53 #include "ide-scsi.h"
54 #include <scsi/sg.h>
56 #define IDESCSI_DEBUG_LOG 0
58 typedef struct idescsi_pc_s {
59 u8 c[12]; /* Actual packet bytes */
60 int request_transfer; /* Bytes to transfer */
61 int actually_transferred; /* Bytes actually transferred */
62 int buffer_size; /* Size of our data buffer */
63 struct request *rq; /* The corresponding request */
64 byte *buffer; /* Data buffer */
65 byte *current_position; /* Pointer into the above buffer */
66 struct scatterlist *sg; /* Scatter gather table */
67 int b_count; /* Bytes transferred from current entry */
68 Scsi_Cmnd *scsi_cmd; /* SCSI command */
69 void (*done)(Scsi_Cmnd *); /* Scsi completion routine */
70 unsigned long flags; /* Status/Action flags */
71 unsigned long timeout; /* Command timeout */
72 } idescsi_pc_t;
75 * Packet command status bits.
77 #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
78 #define PC_WRITING 1 /* Data direction */
79 #define PC_TRANSFORM 2 /* transform SCSI commands */
82 * SCSI command transformation layer
84 #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
85 #define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
88 * Log flags
90 #define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
92 typedef struct {
93 ide_drive_t *drive;
94 idescsi_pc_t *pc; /* Current packet command */
95 unsigned long flags; /* Status/Action flags */
96 unsigned long transform; /* SCSI cmd translation layer */
97 unsigned long log; /* log flags */
98 } idescsi_scsi_t;
101 * Per ATAPI device status bits.
103 #define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
106 * ide-scsi requests.
108 #define IDESCSI_PC_RQ 90
111 * Bits of the interrupt reason register.
113 #define IDESCSI_IREASON_COD 0x1 /* Information transferred is command */
114 #define IDESCSI_IREASON_IO 0x2 /* The device requests us to read */
116 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
118 while (bcount--)
119 IN_BYTE (IDE_DATA_REG);
122 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
124 while (bcount--)
125 OUT_BYTE (0, IDE_DATA_REG);
129 * PIO data transfer routines using the scatter gather table.
131 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
133 int count;
135 while (bcount) {
136 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
137 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
138 idescsi_discard_data (drive, bcount);
139 return;
141 count = IDE_MIN (pc->sg->length - pc->b_count, bcount);
142 atapi_input_bytes (drive, pc->sg->address + pc->b_count, count);
143 bcount -= count; pc->b_count += count;
144 if (pc->b_count == pc->sg->length) {
145 pc->sg++;
146 pc->b_count = 0;
151 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
153 int count;
155 while (bcount) {
156 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
157 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
158 idescsi_output_zeros (drive, bcount);
159 return;
161 count = IDE_MIN (pc->sg->length - pc->b_count, bcount);
162 atapi_output_bytes (drive, pc->sg->address + pc->b_count, count);
163 bcount -= count; pc->b_count += count;
164 if (pc->b_count == pc->sg->length) {
165 pc->sg++;
166 pc->b_count = 0;
172 * Most of the SCSI commands are supported directly by ATAPI devices.
173 * idescsi_transform_pc handles the few exceptions.
175 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
177 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
178 char *atapi_buf;
180 if (!test_bit(PC_TRANSFORM, &pc->flags))
181 return;
182 if (drive->media == ide_cdrom || drive->media == ide_optical) {
183 if (c[0] == READ_6 || c[0] == WRITE_6) {
184 c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
185 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
186 c[0] += (READ_10 - READ_6);
188 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
189 if (!scsi_buf)
190 return;
191 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
192 return;
193 memset(atapi_buf, 0, pc->buffer_size + 4);
194 memset (c, 0, 12);
195 c[0] = sc[0] | 0x40; c[1] = sc[1]; c[2] = sc[2];
196 c[8] = sc[4] + 4; c[9] = sc[5];
197 if (sc[4] + 4 > 255)
198 c[7] = sc[4] + 4 - 255;
199 if (c[0] == MODE_SELECT_10) {
200 atapi_buf[1] = scsi_buf[0]; /* Mode data length */
201 atapi_buf[2] = scsi_buf[1]; /* Medium type */
202 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
203 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
204 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
206 pc->buffer = atapi_buf;
207 pc->request_transfer += 4;
208 pc->buffer_size += 4;
213 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
215 u8 *atapi_buf = pc->buffer;
216 u8 *sc = pc->scsi_cmd->cmnd;
217 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
219 if (!test_bit(PC_TRANSFORM, &pc->flags))
220 return;
221 if (drive->media == ide_cdrom || drive->media == ide_optical) {
222 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
223 scsi_buf[0] = atapi_buf[1]; /* Mode data length */
224 scsi_buf[1] = atapi_buf[2]; /* Medium type */
225 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
226 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
227 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
229 if (pc->c[0] == INQUIRY) {
230 scsi_buf[2] |= 2; /* ansi_revision */
231 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
234 if (atapi_buf && atapi_buf != scsi_buf)
235 kfree(atapi_buf);
238 static inline void idescsi_free_bh (struct buffer_head *bh)
240 struct buffer_head *bhp;
242 while (bh) {
243 bhp = bh;
244 bh = bh->b_reqnext;
245 kfree (bhp);
249 static void hexdump(u8 *x, int len)
251 int i;
253 printk("[ ");
254 for (i = 0; i < len; i++)
255 printk("%x ", x[i]);
256 printk("]\n");
259 static void idescsi_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
261 ide_drive_t *drive = hwgroup->drive;
262 idescsi_scsi_t *scsi = drive->driver_data;
263 struct request *rq = hwgroup->rq;
264 idescsi_pc_t *pc = (idescsi_pc_t *) rq->buffer;
265 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
266 u8 *scsi_buf;
267 unsigned long flags;
269 if (rq->cmd != IDESCSI_PC_RQ) {
270 ide_end_request (uptodate, hwgroup);
271 return;
273 ide_end_drive_cmd (drive, 0, 0);
274 if (rq->errors >= ERROR_MAX) {
275 pc->scsi_cmd->result = DID_ERROR << 16;
276 if (log)
277 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
278 } else if (rq->errors) {
279 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
280 if (log)
281 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
282 } else {
283 pc->scsi_cmd->result = DID_OK << 16;
284 idescsi_transform_pc2 (drive, pc);
285 if (log) {
286 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
287 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
288 printk(", rst = ");
289 scsi_buf = pc->scsi_cmd->request_buffer;
290 hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen));
291 } else printk("\n");
294 spin_lock_irqsave(&io_request_lock,flags);
295 pc->done(pc->scsi_cmd);
296 spin_unlock_irqrestore(&io_request_lock,flags);
297 idescsi_free_bh (rq->bh);
298 kfree(pc); kfree(rq);
299 scsi->pc = NULL;
302 static inline unsigned long get_timeout(idescsi_pc_t *pc)
304 return IDE_MAX(WAIT_CMD, pc->timeout - jiffies);
308 * Our interrupt handler.
310 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
312 idescsi_scsi_t *scsi = drive->driver_data;
313 byte status, ireason;
314 int bcount;
315 idescsi_pc_t *pc=scsi->pc;
316 struct request *rq = pc->rq;
317 unsigned int temp;
319 #if IDESCSI_DEBUG_LOG
320 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
321 #endif /* IDESCSI_DEBUG_LOG */
323 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
324 #if IDESCSI_DEBUG_LOG
325 printk ("ide-scsi: %s: DMA complete\n", drive->name);
326 #endif /* IDESCSI_DEBUG_LOG */
327 pc->actually_transferred=pc->request_transfer;
328 (void) (HWIF(drive)->dmaproc(ide_dma_end, drive));
331 status = GET_STAT(); /* Clear the interrupt */
333 if ((status & DRQ_STAT) == 0) { /* No more interrupts */
334 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
335 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
336 ide__sti();
337 if (status & ERR_STAT)
338 rq->errors++;
339 idescsi_end_request (1, HWGROUP(drive));
340 return ide_stopped;
342 bcount = IN_BYTE (IDE_BCOUNTH_REG) << 8 | IN_BYTE (IDE_BCOUNTL_REG);
343 ireason = IN_BYTE (IDE_IREASON_REG);
345 if (ireason & IDESCSI_IREASON_COD) {
346 printk (KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
347 return ide_do_reset (drive);
349 if (ireason & IDESCSI_IREASON_IO) {
350 temp = pc->actually_transferred + bcount;
351 if ( temp > pc->request_transfer) {
352 if (temp > pc->buffer_size) {
353 printk (KERN_ERR "ide-scsi: The scsi wants to send us more data than expected - discarding data\n");
354 temp = pc->buffer_size - pc->actually_transferred;
355 if (temp) {
356 clear_bit(PC_WRITING, &pc->flags);
357 if (pc->sg)
358 idescsi_input_buffers(drive, pc, temp);
359 else
360 atapi_input_bytes(drive, pc->current_position, temp);
361 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount);
363 pc->actually_transferred += temp;
364 pc->current_position += temp;
365 idescsi_discard_data (drive,bcount - temp);
366 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
367 return ide_started;
369 #if IDESCSI_DEBUG_LOG
370 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
371 #endif /* IDESCSI_DEBUG_LOG */
374 if (ireason & IDESCSI_IREASON_IO) {
375 clear_bit(PC_WRITING, &pc->flags);
376 if (pc->sg)
377 idescsi_input_buffers (drive, pc, bcount);
378 else
379 atapi_input_bytes (drive,pc->current_position,bcount);
380 } else {
381 set_bit(PC_WRITING, &pc->flags);
382 if (pc->sg)
383 idescsi_output_buffers (drive, pc, bcount);
384 else
385 atapi_output_bytes (drive,pc->current_position,bcount);
387 pc->actually_transferred+=bcount; /* Update the current position */
388 pc->current_position+=bcount;
390 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* And set the interrupt handler again */
391 return ide_started;
394 static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive)
396 idescsi_scsi_t *scsi = drive->driver_data;
397 idescsi_pc_t *pc = scsi->pc;
398 byte ireason;
399 ide_startstop_t startstop;
401 if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
402 printk (KERN_ERR "ide-scsi: Strange, packet command initiated yet DRQ isn't asserted\n");
403 return startstop;
405 ireason = IN_BYTE (IDE_IREASON_REG);
406 if ((ireason & (IDESCSI_IREASON_IO | IDESCSI_IREASON_COD)) != IDESCSI_IREASON_COD) {
407 printk (KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while issuing a packet command\n");
408 return ide_do_reset (drive);
410 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* Set the interrupt routine */
411 atapi_output_bytes (drive, scsi->pc->c, 12); /* Send the actual packet */
412 return ide_started;
416 * Issue a packet command
418 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
420 idescsi_scsi_t *scsi = drive->driver_data;
421 int bcount;
422 struct request *rq = pc->rq;
423 int dma_ok = 0;
425 scsi->pc=pc; /* Set the current packet command */
426 pc->actually_transferred=0; /* We haven't transferred any data yet */
427 pc->current_position=pc->buffer;
428 bcount = IDE_MIN (pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
430 if (drive->using_dma && rq->bh)
431 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
433 if (IDE_CONTROL_REG)
434 OUT_BYTE (drive->ctl,IDE_CONTROL_REG);
435 OUT_BYTE (dma_ok,IDE_FEATURE_REG);
436 OUT_BYTE (bcount >> 8,IDE_BCOUNTH_REG);
437 OUT_BYTE (bcount & 0xff,IDE_BCOUNTL_REG);
438 OUT_BYTE (drive->select.all,IDE_SELECT_REG);
440 if (dma_ok) {
441 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
442 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
444 if (test_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
445 ide_set_handler (drive, &idescsi_transfer_pc, get_timeout(pc), NULL);
446 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* Issue the packet command */
447 return ide_started;
448 } else {
449 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
450 return idescsi_transfer_pc (drive);
455 * idescsi_do_request is our request handling function.
457 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
459 #if IDESCSI_DEBUG_LOG
460 printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
461 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
462 #endif /* IDESCSI_DEBUG_LOG */
464 if (rq->cmd == IDESCSI_PC_RQ) {
465 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->buffer);
467 printk (KERN_ERR "ide-scsi: %s: unsupported command in request queue (%x)\n", drive->name, rq->cmd);
468 idescsi_end_request (0,HWGROUP (drive));
469 return ide_stopped;
472 static int idescsi_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
474 MOD_INC_USE_COUNT;
475 return 0;
478 static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
480 MOD_DEC_USE_COUNT;
483 static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
484 static int idescsi_initialized = 0;
486 static void idescsi_add_settings(ide_drive_t *drive)
488 idescsi_scsi_t *scsi = drive->driver_data;
491 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
493 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
494 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
495 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
496 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
497 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
501 * Driver initialization.
503 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id)
505 DRIVER(drive)->busy++;
506 idescsi_drives[id] = drive;
507 drive->driver_data = scsi;
508 drive->ready_stat = 0;
509 memset (scsi, 0, sizeof (idescsi_scsi_t));
510 scsi->drive = drive;
511 if (drive->id && (drive->id->config & 0x0060) == 0x20)
512 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
513 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
514 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
515 #if IDESCSI_DEBUG_LOG
516 set_bit(IDESCSI_LOG_CMD, &scsi->log);
517 #endif /* IDESCSI_DEBUG_LOG */
518 idescsi_add_settings(drive);
521 static int idescsi_cleanup (ide_drive_t *drive)
523 idescsi_scsi_t *scsi = drive->driver_data;
525 if (ide_unregister_subdriver (drive))
526 return 1;
527 drive->driver_data = NULL;
528 kfree (scsi);
529 return 0;
533 * IDE subdriver functions, registered with ide.c
535 static ide_driver_t idescsi_driver = {
536 "ide-scsi", /* name */
537 IDESCSI_VERSION, /* version */
538 ide_scsi, /* media */
539 0, /* busy */
540 1, /* supports_dma */
541 0, /* supports_dsc_overlap */
542 idescsi_cleanup, /* cleanup */
543 idescsi_do_request, /* do_request */
544 idescsi_end_request, /* end_request */
545 NULL, /* ioctl */
546 idescsi_open, /* open */
547 idescsi_ide_release, /* release */
548 NULL, /* media_change */
549 NULL, /* pre_reset */
550 NULL, /* capacity */
551 NULL, /* special */
552 NULL /* proc */
555 int idescsi_init (void);
556 static ide_module_t idescsi_module = {
557 IDE_DRIVER_MODULE,
558 idescsi_init,
559 &idescsi_driver,
560 NULL
564 * idescsi_init will register the driver for each scsi.
566 int idescsi_init (void)
568 ide_drive_t *drive;
569 idescsi_scsi_t *scsi;
570 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
571 int i, failed, id;
573 if (idescsi_initialized)
574 return 0;
575 idescsi_initialized = 1;
576 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
577 idescsi_drives[i] = NULL;
578 MOD_INC_USE_COUNT;
579 for (i = 0; media[i] != 255; i++) {
580 failed = 0;
581 while ((drive = ide_scan_devices (media[i], idescsi_driver.name, NULL, failed++)) != NULL) {
582 if ((scsi = (idescsi_scsi_t *) kmalloc (sizeof (idescsi_scsi_t), GFP_KERNEL)) == NULL) {
583 printk (KERN_ERR "ide-scsi: %s: Can't allocate a scsi structure\n", drive->name);
584 continue;
586 if (ide_register_subdriver (drive, &idescsi_driver, IDE_SUBDRIVER_VERSION)) {
587 printk (KERN_ERR "ide-scsi: %s: Failed to register the driver with ide.c\n", drive->name);
588 kfree (scsi);
589 continue;
591 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++);
592 idescsi_setup (drive, scsi, id);
593 failed--;
596 ide_register_module(&idescsi_module);
597 MOD_DEC_USE_COUNT;
598 return 0;
601 int idescsi_detect (Scsi_Host_Template *host_template)
603 struct Scsi_Host *host;
604 int id;
605 int last_lun = 0;
607 host_template->proc_name = "ide-scsi";
608 host = scsi_register(host_template, 0);
609 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
610 last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
611 host->max_id = id;
612 host->max_lun = last_lun + 1;
613 host->can_queue = host->cmd_per_lun * id;
614 return 1;
617 int idescsi_release (struct Scsi_Host *host)
619 ide_drive_t *drive;
620 int id;
622 for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) {
623 drive = idescsi_drives[id];
624 if (drive)
625 DRIVER(drive)->busy--;
627 return 0;
630 const char *idescsi_info (struct Scsi_Host *host)
632 return "SCSI host adapter emulation for IDE ATAPI devices";
635 int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
637 ide_drive_t *drive = idescsi_drives[dev->id];
638 idescsi_scsi_t *scsi = drive->driver_data;
640 if (cmd == SG_SET_TRANSFORM) {
641 if (arg)
642 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
643 else
644 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
645 return 0;
646 } else if (cmd == SG_GET_TRANSFORM)
647 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
648 return -EINVAL;
651 static inline struct buffer_head *idescsi_kmalloc_bh (int count)
653 struct buffer_head *bh, *bhp, *first_bh;
655 if ((first_bh = bhp = bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
656 goto abort;
657 memset (bh, 0, sizeof (struct buffer_head));
658 bh->b_reqnext = NULL;
659 while (--count) {
660 if ((bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
661 goto abort;
662 memset (bh, 0, sizeof (struct buffer_head));
663 bhp->b_reqnext = bh;
664 bhp = bh;
665 bh->b_reqnext = NULL;
667 return first_bh;
668 abort:
669 idescsi_free_bh (first_bh);
670 return NULL;
673 static inline int idescsi_set_direction (idescsi_pc_t *pc)
675 switch (pc->c[0]) {
676 case READ_6: case READ_10: case READ_12:
677 clear_bit (PC_WRITING, &pc->flags);
678 return 0;
679 case WRITE_6: case WRITE_10: case WRITE_12:
680 set_bit (PC_WRITING, &pc->flags);
681 return 0;
682 default:
683 return 1;
687 static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc)
689 struct buffer_head *bh = NULL, *first_bh = NULL;
690 int segments = pc->scsi_cmd->use_sg;
691 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
693 if (!drive->using_dma || !pc->request_transfer || pc->request_transfer % 1024)
694 return NULL;
695 if (idescsi_set_direction(pc))
696 return NULL;
697 if (segments) {
698 if ((first_bh = bh = idescsi_kmalloc_bh (segments)) == NULL)
699 return NULL;
700 #if IDESCSI_DEBUG_LOG
701 printk ("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive->name, segments, pc->request_transfer >> 10);
702 #endif /* IDESCSI_DEBUG_LOG */
703 while (segments--) {
704 bh->b_data = sg->address;
705 bh->b_size = sg->length;
706 bh = bh->b_reqnext;
707 sg++;
709 } else {
710 if ((first_bh = bh = idescsi_kmalloc_bh (1)) == NULL)
711 return NULL;
712 #if IDESCSI_DEBUG_LOG
713 printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive->name, pc->request_transfer >> 10);
714 #endif /* IDESCSI_DEBUG_LOG */
715 bh->b_data = pc->scsi_cmd->request_buffer;
716 bh->b_size = pc->request_transfer;
718 return first_bh;
721 static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
723 idescsi_scsi_t *scsi = drive->driver_data;
725 if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
726 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
727 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
730 int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
732 ide_drive_t *drive = idescsi_drives[cmd->target];
733 idescsi_scsi_t *scsi;
734 struct request *rq = NULL;
735 idescsi_pc_t *pc = NULL;
737 if (!drive) {
738 printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->target);
739 goto abort;
741 scsi = drive->driver_data;
742 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
743 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
744 if (rq == NULL || pc == NULL) {
745 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
746 goto abort;
749 memset (pc->c, 0, 12);
750 pc->flags = 0;
751 pc->rq = rq;
752 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
753 if (cmd->use_sg) {
754 pc->buffer = NULL;
755 pc->sg = cmd->request_buffer;
756 } else {
757 pc->buffer = cmd->request_buffer;
758 pc->sg = NULL;
760 pc->b_count = 0;
761 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
762 pc->scsi_cmd = cmd;
763 pc->done = done;
764 pc->timeout = jiffies + cmd->timeout_per_command;
766 if (should_transform(drive, cmd))
767 set_bit(PC_TRANSFORM, &pc->flags);
768 idescsi_transform_pc1 (drive, pc);
770 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
771 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
772 hexdump(cmd->cmnd, cmd->cmd_len);
773 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
774 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
775 hexdump(pc->c, 12);
779 ide_init_drive_cmd (rq);
780 rq->buffer = (char *) pc;
781 rq->bh = idescsi_dma_bh (drive, pc);
782 rq->cmd = IDESCSI_PC_RQ;
783 spin_unlock(&io_request_lock);
784 (void) ide_do_drive_cmd (drive, rq, ide_end);
785 spin_lock_irq(&io_request_lock);
786 return 0;
787 abort:
788 if (pc) kfree (pc);
789 if (rq) kfree (rq);
790 cmd->result = DID_ERROR << 16;
791 done(cmd);
792 return 0;
795 int idescsi_abort (Scsi_Cmnd *cmd)
797 return SCSI_ABORT_SNOOZE;
800 int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags)
802 return SCSI_RESET_SUCCESS;
805 int idescsi_bios (Disk *disk, kdev_t dev, int *parm)
807 ide_drive_t *drive = idescsi_drives[disk->device->id];
809 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
810 parm[0] = drive->bios_head;
811 parm[1] = drive->bios_sect;
812 parm[2] = drive->bios_cyl;
814 return 0;
817 #ifdef MODULE
818 Scsi_Host_Template idescsi_template = IDESCSI;
820 int init_module (void)
822 idescsi_init ();
823 idescsi_template.module = &__this_module;
824 scsi_register_module (MODULE_SCSI_HA, &idescsi_template);
825 return 0;
828 void cleanup_module (void)
830 ide_drive_t *drive;
831 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
832 int i, failed;
834 scsi_unregister_module (MODULE_SCSI_HA, &idescsi_template);
835 for (i = 0; media[i] != 255; i++) {
836 failed = 0;
837 while ((drive = ide_scan_devices (media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
838 if (idescsi_cleanup (drive)) {
839 printk ("%s: cleanup_module() called while still busy\n", drive->name);
840 failed++;
843 ide_unregister_module(&idescsi_module);
845 #endif /* MODULE */