Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / scsi / ide-scsi.c
blob5d403105525d29761439fff33298a9f4ad88849d
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 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 returnide_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 returnide_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 SELECT_DRIVE(HWIF(drive), drive);
434 if(IDE_CONTROL_REG)
435 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
436 OUT_BYTE(dma_ok,IDE_FEATURE_REG);
437 OUT_BYTE(bcount >>8,IDE_BCOUNTH_REG);
438 OUT_BYTE(bcount &0xff,IDE_BCOUNTL_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 returnidescsi_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 returnidescsi_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 intidescsi_open(struct inode *inode,struct file *filp, ide_drive_t *drive)
474 MOD_INC_USE_COUNT;
475 return0;
478 static voididescsi_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 voididescsi_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 voididescsi_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 intidescsi_cleanup(ide_drive_t *drive)
523 idescsi_scsi_t *scsi = drive->driver_data;
525 if(ide_unregister_subdriver(drive))
526 return1;
527 drive->driver_data = NULL;
528 kfree(scsi);
529 return0;
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,/* revalidate */
550 NULL,/* pre_reset */
551 NULL,/* capacity */
552 NULL,/* special */
553 NULL /* proc */
556 intidescsi_init(void);
557 static ide_module_t idescsi_module = {
558 IDE_DRIVER_MODULE,
559 idescsi_init,
560 &idescsi_driver,
561 NULL
565 * idescsi_init will register the driver for each scsi.
567 intidescsi_init(void)
569 ide_drive_t *drive;
570 idescsi_scsi_t *scsi;
571 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD,255};
572 int i, failed, id;
574 if(idescsi_initialized)
575 return0;
576 idescsi_initialized =1;
577 for(i =0; i < MAX_HWIFS * MAX_DRIVES; i++)
578 idescsi_drives[i] = NULL;
579 MOD_INC_USE_COUNT;
580 for(i =0; media[i] !=255; i++) {
581 failed =0;
582 while((drive =ide_scan_devices(media[i], idescsi_driver.name, NULL, failed++)) != NULL) {
584 if((scsi = (idescsi_scsi_t *)kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
585 printk(KERN_ERR "ide-scsi: %s: Can't allocate a scsi structure\n", drive->name);
586 continue;
588 if(ide_register_subdriver(drive, &idescsi_driver, IDE_SUBDRIVER_VERSION)) {
589 printk(KERN_ERR "ide-scsi: %s: Failed to register the driver with ide.c\n", drive->name);
590 kfree(scsi);
591 continue;
593 for(id =0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++);
594 idescsi_setup(drive, scsi, id);
595 failed--;
598 ide_register_module(&idescsi_module);
599 MOD_DEC_USE_COUNT;
600 return0;
603 intidescsi_detect(Scsi_Host_Template *host_template)
605 struct Scsi_Host *host;
606 int id;
607 int last_lun =0;
609 host_template->proc_name ="ide-scsi";
610 host =scsi_register(host_template,0);
611 if(host == NULL)
612 return0;
614 for(id =0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
615 last_lun =IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
616 host->max_id = id;
617 host->max_lun = last_lun +1;
618 host->can_queue = host->cmd_per_lun * id;
619 return1;
622 intidescsi_release(struct Scsi_Host *host)
624 ide_drive_t *drive;
625 int id;
627 for(id =0; id < MAX_HWIFS * MAX_DRIVES; id++) {
628 drive = idescsi_drives[id];
629 if(drive)
630 DRIVER(drive)->busy--;
632 return0;
635 const char*idescsi_info(struct Scsi_Host *host)
637 return"SCSI host adapter emulation for IDE ATAPI devices";
640 intidescsi_ioctl(Scsi_Device *dev,int cmd,void*arg)
642 ide_drive_t *drive = idescsi_drives[dev->id];
643 idescsi_scsi_t *scsi = drive->driver_data;
645 if(cmd == SG_SET_TRANSFORM) {
646 if(arg)
647 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
648 else
649 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
650 return0;
651 }else if(cmd == SG_GET_TRANSFORM)
652 returnput_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int*) arg);
653 return-EINVAL;
656 staticinlinestruct buffer_head *idescsi_kmalloc_bh(int count)
658 struct buffer_head *bh, *bhp, *first_bh;
660 if((first_bh = bhp = bh =kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
661 goto abort;
662 memset(bh,0,sizeof(struct buffer_head));
663 bh->b_reqnext = NULL;
664 while(--count) {
665 if((bh =kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
666 goto abort;
667 memset(bh,0,sizeof(struct buffer_head));
668 bhp->b_reqnext = bh;
669 bhp = bh;
670 bh->b_reqnext = NULL;
672 return first_bh;
673 abort:
674 idescsi_free_bh(first_bh);
675 return NULL;
678 staticinlineintidescsi_set_direction(idescsi_pc_t *pc)
680 switch(pc->c[0]) {
681 case READ_6:case READ_10:case READ_12:
682 clear_bit(PC_WRITING, &pc->flags);
683 return0;
684 case WRITE_6:case WRITE_10:case WRITE_12:
685 set_bit(PC_WRITING, &pc->flags);
686 return0;
687 default:
688 return1;
692 staticinlinestruct buffer_head *idescsi_dma_bh(ide_drive_t *drive, idescsi_pc_t *pc)
694 struct buffer_head *bh = NULL, *first_bh = NULL;
695 int segments = pc->scsi_cmd->use_sg;
696 struct scatterlist *sg = pc->scsi_cmd->request_buffer;
698 if(!drive->using_dma || !pc->request_transfer || pc->request_transfer %1024)
699 return NULL;
700 if(idescsi_set_direction(pc))
701 return NULL;
702 if(segments) {
703 if((first_bh = bh =idescsi_kmalloc_bh(segments)) == NULL)
704 return NULL;
705 #if IDESCSI_DEBUG_LOG
706 printk("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive->name, segments, pc->request_transfer >>10);
707 #endif/* IDESCSI_DEBUG_LOG */
708 while(segments--) {
709 bh->b_data = sg->address;
710 bh->b_size = sg->length;
711 bh = bh->b_reqnext;
712 sg++;
714 }else{
715 if((first_bh = bh =idescsi_kmalloc_bh(1)) == NULL)
716 return NULL;
717 #if IDESCSI_DEBUG_LOG
718 printk("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive->name, pc->request_transfer >>10);
719 #endif/* IDESCSI_DEBUG_LOG */
720 bh->b_data = pc->scsi_cmd->request_buffer;
721 bh->b_size = pc->request_transfer;
723 return first_bh;
726 staticinlineintshould_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
728 idescsi_scsi_t *scsi = drive->driver_data;
730 if(MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
731 returntest_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
732 returntest_bit(IDESCSI_TRANSFORM, &scsi->transform);
735 intidescsi_queue(Scsi_Cmnd *cmd,void(*done)(Scsi_Cmnd *))
737 ide_drive_t *drive = idescsi_drives[cmd->target];
738 idescsi_scsi_t *scsi;
739 struct request *rq = NULL;
740 idescsi_pc_t *pc = NULL;
742 if(!drive) {
743 printk(KERN_ERR "ide-scsi: drive id %d not present\n", cmd->target);
744 goto abort;
746 scsi = drive->driver_data;
747 pc =kmalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
748 rq =kmalloc(sizeof(struct request), GFP_ATOMIC);
749 if(rq == NULL || pc == NULL) {
750 printk(KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
751 goto abort;
754 memset(pc->c,0,12);
755 pc->flags =0;
756 pc->rq = rq;
757 memcpy(pc->c, cmd->cmnd, cmd->cmd_len);
758 if(cmd->use_sg) {
759 pc->buffer = NULL;
760 pc->sg = cmd->request_buffer;
761 }else{
762 pc->buffer = cmd->request_buffer;
763 pc->sg = NULL;
765 pc->b_count =0;
766 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
767 pc->scsi_cmd = cmd;
768 pc->done = done;
769 pc->timeout = jiffies + cmd->timeout_per_command;
771 if(should_transform(drive, cmd))
772 set_bit(PC_TRANSFORM, &pc->flags);
773 idescsi_transform_pc1(drive, pc);
775 if(test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
776 printk("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
777 hexdump(cmd->cmnd, cmd->cmd_len);
778 if(memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
779 printk("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
780 hexdump(pc->c,12);
784 ide_init_drive_cmd(rq);
785 rq->buffer = (char*) pc;
786 rq->bh =idescsi_dma_bh(drive, pc);
787 rq->cmd = IDESCSI_PC_RQ;
788 spin_unlock(&io_request_lock);
789 (void)ide_do_drive_cmd(drive, rq, ide_end);
790 spin_lock_irq(&io_request_lock);
791 return0;
792 abort:
793 if(pc)kfree(pc);
794 if(rq)kfree(rq);
795 cmd->result = DID_ERROR <<16;
796 done(cmd);
797 return0;
800 intidescsi_abort(Scsi_Cmnd *cmd)
802 return SCSI_ABORT_SNOOZE;
805 intidescsi_reset(Scsi_Cmnd *cmd,unsigned int resetflags)
807 return SCSI_RESET_SUCCESS;
810 intidescsi_bios(Disk *disk, kdev_t dev,int*parm)
812 ide_drive_t *drive = idescsi_drives[disk->device->id];
814 if(drive->bios_cyl && drive->bios_head && drive->bios_sect) {
815 parm[0] = drive->bios_head;
816 parm[1] = drive->bios_sect;
817 parm[2] = drive->bios_cyl;
819 return0;
822 static Scsi_Host_Template idescsi_template = IDESCSI;
824 static int __init init_idescsi_module(void)
826 idescsi_init();
827 idescsi_template.module = THIS_MODULE;
828 scsi_register_module(MODULE_SCSI_HA, &idescsi_template);
829 return0;
832 static void __exit exit_idescsi_module(void)
834 ide_drive_t *drive;
835 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD,255};
836 int i, failed;
838 scsi_unregister_module(MODULE_SCSI_HA, &idescsi_template);
839 for(i =0; media[i] !=255; i++) {
840 failed =0;
841 while((drive =ide_scan_devices(media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
842 if(idescsi_cleanup(drive)) {
843 printk("%s: exit_idescsi_module() called while still busy\n", drive->name);
844 failed++;
847 ide_unregister_module(&idescsi_module);
850 module_init(init_idescsi_module);
851 module_exit(exit_idescsi_module);
close