Import 2.3.18pre1
[davej-history.git] / drivers / scsi / ide-scsi.c
blobb89f39c8fd241bd08d182c7c5b8d0d2f9444cdc2
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 typedefstruct 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 typedefstruct{
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 voididescsi_discard_data(ide_drive_t *drive,unsigned int bcount)
118 while(bcount--)
119 IN_BYTE(IDE_DATA_REG);
122 static voididescsi_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 voididescsi_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 voididescsi_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 staticinlinevoididescsi_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 staticinlinevoididescsi_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 staticinlinevoididescsi_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 voidhexdump(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 voididescsi_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 }elseprintk("\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 staticinlineunsigned longget_timeout(idescsi_pc_t *pc)
304 returnIDE_MAX(WAIT_CMD, pc->timeout - jiffies);
308 * Our interrupt handler.
310 static voididescsi_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;
342 bcount =IN_BYTE(IDE_BCOUNTH_REG) <<8|IN_BYTE(IDE_BCOUNTL_REG);
343 ireason =IN_BYTE(IDE_IREASON_REG);
345 if(ireason & IDESCSI_IREASON_COD) {
346 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
347 ide_do_reset(drive);
348 return;
350 if(ireason & IDESCSI_IREASON_IO) {
351 temp = pc->actually_transferred + bcount;
352 if( temp > pc->request_transfer) {
353 if(temp > pc->buffer_size) {
354 printk(KERN_ERR "ide-scsi: The scsi wants to send us more data than expected - discarding data\n");
355 temp = pc->buffer_size - pc->actually_transferred;
356 if(temp) {
357 clear_bit(PC_WRITING, &pc->flags);
358 if(pc->sg)
359 idescsi_input_buffers(drive, pc, temp);
360 else
361 atapi_input_bytes(drive, pc->current_position, temp);
362 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount);
364 pc->actually_transferred += temp;
365 pc->current_position += temp;
366 idescsi_discard_data(drive,bcount - temp);
367 ide_set_handler(drive, &idescsi_pc_intr,get_timeout(pc));
368 return;
370 #if IDESCSI_DEBUG_LOG
371 printk(KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
372 #endif/* IDESCSI_DEBUG_LOG */
375 if(ireason & IDESCSI_IREASON_IO) {
376 clear_bit(PC_WRITING, &pc->flags);
377 if(pc->sg)
378 idescsi_input_buffers(drive, pc, bcount);
379 else
380 atapi_input_bytes(drive,pc->current_position,bcount);
381 }else{
382 set_bit(PC_WRITING, &pc->flags);
383 if(pc->sg)
384 idescsi_output_buffers(drive, pc, bcount);
385 else
386 atapi_output_bytes(drive,pc->current_position,bcount);
388 pc->actually_transferred+=bcount;/* Update the current position */
389 pc->current_position+=bcount;
391 ide_set_handler(drive, &idescsi_pc_intr,get_timeout(pc));/* And set the interrupt handler again */
394 static voididescsi_transfer_pc(ide_drive_t *drive)
396 idescsi_scsi_t *scsi = drive->driver_data;
397 idescsi_pc_t *pc = scsi->pc;
398 byte ireason;
400 if(ide_wait_stat(drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
401 printk(KERN_ERR "ide-scsi: Strange, packet command initiated yet DRQ isn't asserted\n");
402 return;
404 ireason =IN_BYTE(IDE_IREASON_REG);
405 if((ireason & (IDESCSI_IREASON_IO | IDESCSI_IREASON_COD)) != IDESCSI_IREASON_COD) {
406 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while issuing a packet command\n");
407 ide_do_reset(drive);
408 return;
410 ide_set_handler(drive, &idescsi_pc_intr,get_timeout(pc));/* Set the interrupt routine */
411 atapi_output_bytes(drive, scsi->pc->c,12);/* Send the actual packet */
415 * Issue a packet command
417 static voididescsi_issue_pc(ide_drive_t *drive, idescsi_pc_t *pc)
419 idescsi_scsi_t *scsi = drive->driver_data;
420 int bcount;
421 struct request *rq = pc->rq;
422 int dma_ok =0;
424 scsi->pc=pc;/* Set the current packet command */
425 pc->actually_transferred=0;/* We haven't transferred any data yet */
426 pc->current_position=pc->buffer;
427 bcount =IDE_MIN(pc->request_transfer,63*1024);/* Request to transfer the entire buffer at once */
429 if(drive->using_dma && rq->bh)
430 dma_ok=!HWIF(drive)->dmaproc(test_bit(PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
432 if(IDE_CONTROL_REG)
433 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
434 OUT_BYTE(dma_ok,IDE_FEATURE_REG);
435 OUT_BYTE(bcount >>8,IDE_BCOUNTH_REG);
436 OUT_BYTE(bcount &0xff,IDE_BCOUNTL_REG);
437 OUT_BYTE(drive->select.all,IDE_SELECT_REG);
439 if(dma_ok) {
440 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
441 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
443 if(test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
444 ide_set_handler(drive, &idescsi_transfer_pc,get_timeout(pc));
445 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);/* Issue the packet command */
446 }else{
447 OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
448 idescsi_transfer_pc(drive);
453 * idescsi_do_request is our request handling function.
455 static voididescsi_do_request(ide_drive_t *drive,struct request *rq,unsigned long block)
457 #if IDESCSI_DEBUG_LOG
458 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);
459 printk(KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
460 #endif/* IDESCSI_DEBUG_LOG */
462 if(rq->cmd == IDESCSI_PC_RQ) {
463 idescsi_issue_pc(drive, (idescsi_pc_t *) rq->buffer);
464 return;
466 printk(KERN_ERR "ide-scsi: %s: unsupported command in request queue (%x)\n", drive->name, rq->cmd);
467 idescsi_end_request(0,HWGROUP(drive));
470 static intidescsi_open(struct inode *inode,struct file *filp, ide_drive_t *drive)
472 MOD_INC_USE_COUNT;
473 return0;
476 static voididescsi_ide_release(struct inode *inode,struct file *filp, ide_drive_t *drive)
478 MOD_DEC_USE_COUNT;
481 static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
482 static int idescsi_initialized =0;
484 static voididescsi_add_settings(ide_drive_t *drive)
486 idescsi_scsi_t *scsi = drive->driver_data;
489 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
491 ide_add_setting(drive,"bios_cyl", SETTING_RW, -1, -1, TYPE_SHORT,0,1023,1,1, &drive->bios_cyl, NULL);
492 ide_add_setting(drive,"bios_head", SETTING_RW, -1, -1, TYPE_BYTE,0,255,1,1, &drive->bios_head, NULL);
493 ide_add_setting(drive,"bios_sect", SETTING_RW, -1, -1, TYPE_BYTE,0,63,1,1, &drive->bios_sect, NULL);
494 ide_add_setting(drive,"transform", SETTING_RW, -1, -1, TYPE_INT,0,3,1,1, &scsi->transform, NULL);
495 ide_add_setting(drive,"log", SETTING_RW, -1, -1, TYPE_INT,0,1,1,1, &scsi->log, NULL);
499 * Driver initialization.
501 static voididescsi_setup(ide_drive_t *drive, idescsi_scsi_t *scsi,int id)
503 DRIVER(drive)->busy++;
504 idescsi_drives[id] = drive;
505 drive->driver_data = scsi;
506 drive->ready_stat =0;
507 memset(scsi,0,sizeof(idescsi_scsi_t));
508 scsi->drive = drive;
509 if(drive->id && (drive->id->config &0x0060) ==0x20)
510 set_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags);
511 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
512 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
513 #if IDESCSI_DEBUG_LOG
514 set_bit(IDESCSI_LOG_CMD, &scsi->log);
515 #endif/* IDESCSI_DEBUG_LOG */
516 idescsi_add_settings(drive);
519 static intidescsi_cleanup(ide_drive_t *drive)
521 idescsi_scsi_t *scsi = drive->driver_data;
523 if(ide_unregister_subdriver(drive))
524 return1;
525 drive->driver_data = NULL;
526 kfree(scsi);
527 return0;
531 * IDE subdriver functions, registered with ide.c
533 static ide_driver_t idescsi_driver = {
534 "ide-scsi",/* name */
535 IDESCSI_VERSION,/* version */
536 ide_scsi,/* media */
537 0,/* busy */
538 1,/* supports_dma */
539 0,/* supports_dsc_overlap */
540 idescsi_cleanup,/* cleanup */
541 idescsi_do_request,/* do_request */
542 idescsi_end_request,/* end_request */
543 NULL,/* ioctl */
544 idescsi_open,/* open */
545 idescsi_ide_release,/* release */
546 NULL,/* media_change */
547 NULL,/* pre_reset */
548 NULL,/* capacity */
549 NULL,/* special */
550 NULL /* proc */
553 intidescsi_init(void);
554 static ide_module_t idescsi_module = {
555 IDE_DRIVER_MODULE,
556 idescsi_init,
557 &idescsi_driver,
558 NULL
561 static struct proc_dir_entry idescsi_proc_dir = {PROC_SCSI_IDESCSI,8,"ide-scsi", S_IFDIR | S_IRUGO | S_IXUGO,2};
564 * idescsi_init will register the driver for each scsi.
566 intidescsi_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 return0;
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 return0;
601 intidescsi_detect(Scsi_Host_Template *host_template)
603 struct Scsi_Host *host;
604 int id;
606 host_template->proc_dir = &idescsi_proc_dir;
607 host =scsi_register(host_template,0);
608 for(id =0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++);
609 host->max_id = id;
610 host->can_queue = host->cmd_per_lun * id;
611 return1;
614 intidescsi_release(struct Scsi_Host *host)
616 ide_drive_t *drive;
617 int id;
619 for(id =0; id < MAX_HWIFS * MAX_DRIVES; id++) {
620 drive = idescsi_drives[id];
621 if(drive)
622 DRIVER(drive)->busy--;
624 return0;
627 const char*idescsi_info(struct Scsi_Host *host)
629 return"SCSI host adapter emulation for IDE ATAPI devices";
632 intidescsi_ioctl(Scsi_Device *dev,int cmd,void*arg)
634 ide_drive_t *drive = idescsi_drives[dev->id];
635 idescsi_scsi_t *scsi = drive->driver_data;
636 int enable = (int) arg;
638 if(cmd == SG_SET_TRANSFORM) {
639 if(enable)
640 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
641 else
642 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
643 return0;
644 }else if(cmd == SG_GET_TRANSFORM)
645 returnput_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int*) arg);
646 return-EINVAL;
649 staticinlinestruct buffer_head *idescsi_kmalloc_bh(int count)
651 struct buffer_head *bh, *bhp, *first_bh;
653 if((first_bh = bhp = bh =kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
654 goto abort;
655 memset(bh,0,sizeof(struct buffer_head));
656 bh->b_reqnext = NULL;
657 while(--count) {
658 if((bh =kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
659 goto abort;
660 memset(bh,0,sizeof(struct buffer_head));
661 bhp->b_reqnext = bh;
662 bhp = bh;
663 bh->b_reqnext = NULL;
665 return first_bh;
666 abort:
667 idescsi_free_bh(first_bh);
668 return NULL;
671 staticinlineintidescsi_set_direction(idescsi_pc_t *pc)
673 switch(pc->c[0]) {
674 case READ_6:case READ_10:case READ_12:
675 clear_bit(PC_WRITING, &pc->flags);
676 return0;
677 case WRITE_6:case WRITE_10:case WRITE_12:
678 set_bit(PC_WRITING, &pc->flags);
679 return0;
680 default:
681 return1;
685 staticinlinestruct buffer_head *idescsi_dma_bh(ide_drive_t *drive, idescsi_pc_t *pc)
687 struct buffer_head *bh = NULL, *first_bh = NULL;
688 int segments = pc->scsi_cmd->use_sg;
689 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
691 if(!drive->using_dma || !pc->request_transfer || pc->request_transfer %1024)
692 return NULL;
693 if(idescsi_set_direction(pc))
694 return NULL;
695 if(segments) {
696 if((first_bh = bh =idescsi_kmalloc_bh(segments)) == NULL)
697 return NULL;
698 #if IDESCSI_DEBUG_LOG
699 printk("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive->name, segments, pc->request_transfer >>10);
700 #endif/* IDESCSI_DEBUG_LOG */
701 while(segments--) {
702 bh->b_data = sg->address;
703 bh->b_size = sg->length;
704 bh = bh->b_reqnext;
705 sg++;
707 }else{
708 if((first_bh = bh =idescsi_kmalloc_bh(1)) == NULL)
709 return NULL;
710 #if IDESCSI_DEBUG_LOG
711 printk("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive->name, pc->request_transfer >>10);
712 #endif/* IDESCSI_DEBUG_LOG */
713 bh->b_data = pc->scsi_cmd->request_buffer;
714 bh->b_size = pc->request_transfer;
716 return first_bh;
719 staticinlineintshould_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
721 idescsi_scsi_t *scsi = drive->driver_data;
723 if(MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
724 returntest_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
725 returntest_bit(IDESCSI_TRANSFORM, &scsi->transform);
728 intidescsi_queue(Scsi_Cmnd *cmd,void(*done)(Scsi_Cmnd *))
730 ide_drive_t *drive = idescsi_drives[cmd->target];
731 idescsi_scsi_t *scsi;
732 struct request *rq = NULL;
733 idescsi_pc_t *pc = NULL;
735 if(!drive) {
736 printk(KERN_ERR "ide-scsi: drive id %d not present\n", cmd->target);
737 goto abort;
739 scsi = drive->driver_data;
740 pc =kmalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
741 rq =kmalloc(sizeof(struct request), GFP_ATOMIC);
742 if(rq == NULL || pc == NULL) {
743 printk(KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
744 goto abort;
747 memset(pc->c,0,12);
748 pc->flags =0;
749 pc->rq = rq;
750 memcpy(pc->c, cmd->cmnd, cmd->cmd_len);
751 if(cmd->use_sg) {
752 pc->buffer = NULL;
753 pc->sg = cmd->request_buffer;
754 }else{
755 pc->buffer = cmd->request_buffer;
756 pc->sg = NULL;
758 pc->b_count =0;
759 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
760 pc->scsi_cmd = cmd;
761 pc->done = done;
762 pc->timeout = jiffies + cmd->timeout_per_command;
764 if(should_transform(drive, cmd))
765 set_bit(PC_TRANSFORM, &pc->flags);
766 idescsi_transform_pc1(drive, pc);
768 if(test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
769 printk("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
770 hexdump(cmd->cmnd, cmd->cmd_len);
771 if(memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
772 printk("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
773 hexdump(pc->c,12);
777 ide_init_drive_cmd(rq);
778 rq->buffer = (char*) pc;
779 rq->bh =idescsi_dma_bh(drive, pc);
780 rq->cmd = IDESCSI_PC_RQ;
781 spin_unlock(&io_request_lock);
782 (void)ide_do_drive_cmd(drive, rq, ide_end);
783 spin_lock_irq(&io_request_lock);
784 return0;
785 abort:
786 if(pc)kfree(pc);
787 if(rq)kfree(rq);
788 cmd->result = DID_ERROR <<16;
789 done(cmd);
790 return0;
793 intidescsi_abort(Scsi_Cmnd *cmd)
795 return SCSI_ABORT_SNOOZE;
798 intidescsi_reset(Scsi_Cmnd *cmd,unsigned int resetflags)
800 return SCSI_RESET_SUCCESS;
803 intidescsi_bios(Disk *disk, kdev_t dev,int*parm)
805 ide_drive_t *drive = idescsi_drives[disk->device->id];
807 if(drive->bios_cyl && drive->bios_head && drive->bios_sect) {
808 parm[0] = drive->bios_head;
809 parm[1] = drive->bios_sect;
810 parm[2] = drive->bios_cyl;
812 return0;
815 #ifdef MODULE
816 Scsi_Host_Template idescsi_template = IDESCSI;
818 intinit_module(void)
820 idescsi_init();
821 idescsi_template.module = &__this_module;
822 scsi_register_module(MODULE_SCSI_HA, &idescsi_template);
823 return0;
826 voidcleanup_module(void)
828 ide_drive_t *drive;
829 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD,255};
830 int i, failed;
832 scsi_unregister_module(MODULE_SCSI_HA, &idescsi_template);
833 for(i =0; media[i] !=255; i++) {
834 failed =0;
835 while((drive =ide_scan_devices(media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
836 if(idescsi_cleanup(drive)) {
837 printk("%s: cleanup_module() called while still busy\n", drive->name);
838 failed++;
841 ide_unregister_module(&idescsi_module);
843 #endif/* MODULE */
close