added mtd driver
[linux-2.4.git] / drivers / scsi / ide-scsi.c
1 /*
2  * linux/drivers/scsi/ide-scsi.c        Version 0.94    Sept 09, 2003
3  *
4  * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5  * Copyright (C) 2001 - 2002 Andre Hedrick <andre@linux-ide.org>
6  */
7
8 /*
9  * Emulation of a SCSI host adapter for IDE ATAPI devices.
10  *
11  * With this driver, one can use the Linux SCSI drivers instead of the
12  * native IDE ATAPI drivers.
13  *
14  * Ver 0.1   Dec  3 96   Initial version.
15  * Ver 0.2   Jan 26 97   Fixed bug in cleanup_module() and added emulation
16  *                        of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
17  *                        to Janos Farkas for pointing this out.
18  *                       Avoid using bitfields in structures for m68k.
19  *                       Added Scatter/Gather and DMA support.
20  * Ver 0.4   Dec  7 97   Add support for ATAPI PD/CD drives.
21  *                       Use variable timeout for each command.
22  * Ver 0.5   Jan  2 98   Fix previous PD/CD support.
23  *                       Allow disabling of SCSI-6 to SCSI-10 transformation.
24  * Ver 0.6   Jan 27 98   Allow disabling of SCSI command translation layer
25  *                        for access through /dev/sg.
26  *                       Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
27  * Ver 0.7   Dec 04 98   Ignore commands where lun != 0 to avoid multiple
28  *                        detection of devices with CONFIG_SCSI_MULTI_LUN
29  * Ver 0.8   Feb 05 99   Optical media need translation too. Reverse 0.7.
30  * Ver 0.9   Jul 04 99   Fix a bug in SG_SET_TRANSFORM.
31  * Ver 0.91  Jan 06 02   Added 'ignore' parameter when ide-scsi is a module
32  *                        so that use of scsi emulation can be made independent
33  *                        of load order when other IDE drivers are modules.
34  *                        Chris Ebenezer <chriseb@pobox.com>
35  * Ver 0.92  Mar 21 02   Include DevFs support
36  *                        Borsenkow Andrej <Andrej.Borsenkow@mow.siemens.ru>
37  * Ver 0.93  Jun 10 02   Fix "off by one" error in transforms
38  * Ver 0.94  Sep 09 03   Added transform for reading ATAPI tape drive block
39  *                        limits (ATAPI tapes report block limits in mode
40  *                        page 0x2A, not by "read block limits" command)
41  */
42
43 #define IDESCSI_VERSION "0.94"
44
45 #include <linux/module.h>
46 #include <linux/config.h>
47 #include <linux/types.h>
48 #include <linux/string.h>
49 #include <linux/kernel.h>
50 #include <linux/mm.h>
51 #include <linux/ioport.h>
52 #include <linux/blkdev.h>
53 #include <linux/errno.h>
54 #include <linux/hdreg.h>
55 #include <linux/slab.h>
56 #include <linux/ide.h>
57
58 #include <asm/io.h>
59 #include <asm/bitops.h>
60 #include <asm/uaccess.h>
61
62 #include "scsi.h"
63 #include "hosts.h"
64 #include "sd.h"
65 #include "ide-scsi.h"
66 #include <scsi/sg.h>
67
68 #define IDESCSI_DEBUG_LOG       0
69
70 typedef struct idescsi_pc_s {
71         u8 c[12];                       /* Actual packet bytes */
72         int request_transfer;           /* Bytes to transfer */
73         int actually_transferred;       /* Bytes actually transferred */
74         int buffer_size;                /* Size of our data buffer */
75         struct request *rq;             /* The corresponding request */
76         u8 *buffer;                     /* Data buffer */
77         u8 *current_position;           /* Pointer into the above buffer */
78         struct scatterlist *sg;         /* Scatter gather table */
79         int b_count;                    /* Bytes transferred from current entry */
80         Scsi_Cmnd *scsi_cmd;            /* SCSI command */
81         void (*done)(Scsi_Cmnd *);      /* Scsi completion routine */
82         unsigned long flags;            /* Status/Action flags */
83         unsigned long timeout;          /* Command timeout */
84 } idescsi_pc_t;
85
86 /*
87  *      Packet command status bits.
88  */
89 #define PC_DMA_IN_PROGRESS      0       /* 1 while DMA in progress */
90 #define PC_WRITING              1       /* Data direction */
91 #define PC_TRANSFORM            2       /* transform SCSI commands */
92 #define PC_DMA_OK               4       /* Use DMA */
93
94 /*
95  *      SCSI command transformation layer
96  */
97 #define IDESCSI_TRANSFORM       0       /* Enable/Disable transformation */
98 #define IDESCSI_SG_TRANSFORM    1       /* /dev/sg transformation */
99
100 /*
101  *      Log flags
102  */
103 #define IDESCSI_LOG_CMD         0       /* Log SCSI commands */
104
105 #define IDESCSI_DEVFS
106
107 typedef struct {
108         ide_drive_t *drive;
109         idescsi_pc_t *pc;               /* Current packet command */
110         unsigned long flags;            /* Status/Action flags */
111         unsigned long transform;        /* SCSI cmd translation layer */
112         unsigned long log;              /* log flags */
113         int id;                         /* id */
114 #ifdef IDESCSI_DEVFS
115         devfs_handle_t de;              /* pointer to IDE device */
116 #endif /* IDESCSI_DEVFS */
117 } idescsi_scsi_t;
118
119 /*
120  *      Per ATAPI device status bits.
121  */
122 #define IDESCSI_DRQ_INTERRUPT   0       /* DRQ interrupt device */
123
124 /*
125  *      ide-scsi requests.
126  */
127 #define IDESCSI_PC_RQ           90
128
129 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
130 {
131         while (bcount--)
132                 (void) HWIF(drive)->INB(IDE_DATA_REG);
133 }
134
135 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
136 {
137         while (bcount--)
138                 HWIF(drive)->OUTB(0, IDE_DATA_REG);
139 }
140
141 /*
142  *      PIO data transfer routines using the scatter gather table.
143  */
144 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
145 {
146         int count;
147
148         while (bcount) {
149                 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
150                         printk(KERN_ERR "ide-scsi: scatter gather "
151                                 "table too small, discarding data\n");
152                         idescsi_discard_data(drive, bcount);
153                         return;
154                 }
155                 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
156                 HWIF(drive)->atapi_input_bytes(drive, pc->sg->address + pc->b_count, count);
157                 bcount -= count;
158                 pc->b_count += count;
159                 if (pc->b_count == pc->sg->length) {
160                         pc->sg++;
161                         pc->b_count = 0;
162                 }
163         }
164 }
165
166 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
167 {
168         int count;
169
170         while (bcount) {
171                 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
172                         printk(KERN_ERR "ide-scsi: scatter gather table "
173                                 "too small, padding with zeros\n");
174                         idescsi_output_zeros(drive, bcount);
175                         return;
176                 }
177                 count = IDE_MIN(pc->sg->length - pc->b_count, bcount);
178                 HWIF(drive)->atapi_output_bytes(drive, pc->sg->address + pc->b_count, count);
179                 bcount -= count;
180                 pc->b_count += count;
181                 if (pc->b_count == pc->sg->length) {
182                         pc->sg++;
183                         pc->b_count = 0;
184                 }
185         }
186 }
187
188 /*
189  *      Most of the SCSI commands are supported directly by ATAPI devices.
190  *      idescsi_transform_pc handles the few exceptions.
191  */
192 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
193 {
194         u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
195         char *atapi_buf;
196
197         if (!test_bit(PC_TRANSFORM, &pc->flags))
198                 return;
199         if (drive->media == ide_cdrom || drive->media == ide_optical) {
200                 if (c[0] == READ_6 || c[0] == WRITE_6) {
201                         c[8] = c[4];
202                         c[5] = c[3];
203                         c[4] = c[2];
204                         c[3] = c[1] & 0x1f;
205                         c[2] = 0;
206                         c[1] &= 0xe0;
207                         c[0] += (READ_10 - READ_6);
208                 }
209                 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
210                         unsigned short new_len;
211                         if (!scsi_buf)
212                                 return;
213                         if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
214                                 return;
215                         memset(atapi_buf, 0, pc->buffer_size + 4);
216                         memset (c, 0, 12);
217                         c[0] = sc[0] | 0x40;
218                         c[1] = sc[1];
219                         c[2] = sc[2];
220                         new_len = sc[4] + 4;
221                         c[8] = new_len;
222                         c[7] = new_len >> 8;
223                         c[9] = sc[5];
224                         if (c[0] == MODE_SELECT_10) {
225                                 /* Mode data length */
226                                 atapi_buf[1] = scsi_buf[0];
227                                 /* Medium type */
228                                 atapi_buf[2] = scsi_buf[1];
229                                 /* Device specific parameter */
230                                 atapi_buf[3] = scsi_buf[2];
231                                 /* Block descriptor length */
232                                 atapi_buf[7] = scsi_buf[3];
233                                 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
234                         }
235                         pc->buffer = atapi_buf;
236                         pc->request_transfer += 4;
237                         pc->buffer_size += 4;
238                 }
239         }
240         if (drive->media == ide_tape) {
241                 if (sc[0] == READ_BLOCK_LIMITS) {       /* IDE tapes have blk lmts in mode page 0x2a */
242                         if (!scsi_buf)
243                                 return;
244                         /* buffer size should be 6 for READ_BLOCK_LIMITS    */
245                         /* we need 12 bytes (4 for header + 8 for mode page */
246                         if ((atapi_buf = kmalloc(12, GFP_ATOMIC)) == NULL)
247                                 return;
248                         memset(atapi_buf, 0, 12);
249                         memset (c, 0, 12);
250                         c[0] = MODE_SENSE;
251                         c[1] = 8;       /* no block descriptors     */
252                         c[2] = 0x2A;    /* mode page 0x2A           */
253                         c[4] = 12;      /* buffer length 12 decimal */
254                         pc->buffer = atapi_buf;
255                         pc->request_transfer = 12;
256                         pc->buffer_size = 12;
257                 }
258         }
259 }
260
261 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
262 {
263         u8 *atapi_buf = pc->buffer;
264         u8 *sc = pc->scsi_cmd->cmnd;
265         u8 *scsi_buf = pc->scsi_cmd->request_buffer;
266
267         if (!test_bit(PC_TRANSFORM, &pc->flags))
268                 return;
269         if (drive->media == ide_cdrom || drive->media == ide_optical) {
270                 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
271                         /* Mode data length */
272                         scsi_buf[0] = atapi_buf[1];
273                         /* Medium type */
274                         scsi_buf[1] = atapi_buf[2];
275                         /* Device specific parameter */
276                         scsi_buf[2] = atapi_buf[3];
277                         /* Block descriptor length */
278                         scsi_buf[3] = atapi_buf[7];
279                         memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
280                 }
281                 if (pc->c[0] == INQUIRY) {
282                         /* ansi_revision */
283                         scsi_buf[2] |= 2;
284                         /* response data format */
285                         scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
286                 }
287         }       
288         if (drive->media == ide_tape) {
289                 if (sc[0] == READ_BLOCK_LIMITS) {
290                         memset(scsi_buf, 0, pc->scsi_cmd->request_bufflen);
291                         /* granularity of 9 (always 9 for ide tapes) */
292                         scsi_buf[0] = 9;
293                         /* block length of 1024 bytes supported? */
294                         scsi_buf[2] = (atapi_buf[11] & 0x04) ? 4 : 2;
295                         /* block length of 512 bytes supported?  */
296                         scsi_buf[4] = (atapi_buf[11] & 0x02) ? 2 : 4;
297                 }
298         }
299
300         if (atapi_buf && atapi_buf != scsi_buf)
301                 kfree(atapi_buf);
302 }
303
304 static inline void idescsi_free_bh (struct buffer_head *bh)
305 {
306         struct buffer_head *bhp;
307
308         while (bh) {
309                 bhp = bh;
310                 bh = bh->b_reqnext;
311                 kfree (bhp);
312         }
313 }
314
315 static void hexdump(u8 *x, int len)
316 {
317         int i;
318
319         printk("[ ");
320         for (i = 0; i < len; i++)
321                 printk("%x ", x[i]);
322         printk("]\n");
323 }
324
325 static int idescsi_do_end_request (ide_drive_t *drive, int uptodate)
326 {
327         struct request *rq;
328         unsigned long flags;
329         int ret = 1;
330
331         spin_lock_irqsave(&io_request_lock, flags);
332         rq = HWGROUP(drive)->rq;
333
334         /*
335          * decide whether to reenable DMA -- 3 is a random magic for now,
336          * if we DMA timeout more than 3 times, just stay in PIO
337          */
338         if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
339                 drive->state = 0;
340                 HWGROUP(drive)->hwif->ide_dma_on(drive);
341         }
342
343         if (!end_that_request_first(rq, uptodate, drive->name)) {
344                 add_blkdev_randomness(MAJOR(rq->rq_dev));
345                 blkdev_dequeue_request(rq);
346                 HWGROUP(drive)->rq = NULL;
347                 end_that_request_last(rq);
348                 ret = 0;
349         }
350         spin_unlock_irqrestore(&io_request_lock, flags);
351         return ret;
352 }
353
354 static int idescsi_end_request (ide_drive_t *drive, int uptodate)
355 {
356         idescsi_scsi_t *scsi = drive->driver_data;
357         struct request *rq = HWGROUP(drive)->rq;
358         idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
359         int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
360         u8 *scsi_buf;
361         unsigned long flags;
362
363         if (rq->cmd != IDESCSI_PC_RQ) {
364                 idescsi_do_end_request(drive, uptodate);
365                 return 0;
366         }
367         ide_end_drive_cmd(drive, 0, 0);
368         if (rq->errors >= ERROR_MAX) {
369                 pc->scsi_cmd->result = DID_ERROR << 16;
370                 if (log)
371                         printk("ide-scsi: %s: I/O error for %lu\n",
372                                 drive->name, pc->scsi_cmd->serial_number);
373         } else if (rq->errors) {
374                 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
375                 if (log)
376                         printk("ide-scsi: %s: check condition for %lu\n",
377                                 drive->name, pc->scsi_cmd->serial_number);
378         } else {
379                 pc->scsi_cmd->result = DID_OK << 16;
380                 idescsi_transform_pc2(drive, pc);
381                 if (log) {
382                         printk("ide-scsi: %s: suc %lu", drive->name,
383                                 pc->scsi_cmd->serial_number);
384                         if (!test_bit(PC_WRITING, &pc->flags) &&
385                             pc->actually_transferred &&
386                             pc->actually_transferred <= 1024 &&
387                             pc->buffer) {
388                                 printk(", rst = ");
389                                 scsi_buf = pc->scsi_cmd->request_buffer;
390                                 hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen));
391                         } else printk("\n");
392                 }
393         }
394         spin_lock_irqsave(&io_request_lock, flags);     
395         pc->done(pc->scsi_cmd);
396         spin_unlock_irqrestore(&io_request_lock, flags);
397         idescsi_free_bh(rq->bh);
398         kfree(pc);
399         kfree(rq);
400         scsi->pc = NULL;
401         return 0;
402 }
403
404 static inline unsigned long get_timeout(idescsi_pc_t *pc)
405 {
406         return IDE_MAX(WAIT_CMD, pc->timeout - jiffies);
407 }
408
409 /*
410  *      Our interrupt handler.
411  */
412 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
413 {
414         idescsi_scsi_t *scsi = drive->driver_data;
415         idescsi_pc_t *pc = scsi->pc;
416         struct request *rq = pc->rq;
417         atapi_bcount_t bcount;
418         atapi_status_t status;
419         atapi_ireason_t ireason;
420         atapi_feature_t feature;
421         unsigned int temp;
422
423 #if IDESCSI_DEBUG_LOG
424         printk(KERN_INFO "ide-scsi: Reached idescsi_pc_intr "
425                 "interrupt handler\n");
426 #endif /* IDESCSI_DEBUG_LOG */
427
428         if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
429 #if IDESCSI_DEBUG_LOG
430                 printk("ide-scsi: %s: DMA complete\n", drive->name);
431 #endif /* IDESCSI_DEBUG_LOG */
432                 pc->actually_transferred = pc->request_transfer;
433                 (void) (HWIF(drive)->ide_dma_end(drive));
434         }
435
436         feature.all = 0;
437         /* Clear the interrupt */
438         status.all = HWIF(drive)->INB(IDE_STATUS_REG);
439
440         if (!status.b.drq) {
441                 /* No more interrupts */
442                 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
443                         printk(KERN_INFO "Packet command completed, %d "
444                                 "bytes transferred\n",
445                                 pc->actually_transferred);
446                 local_irq_enable();
447                 if (status.b.check)
448                         rq->errors++;
449                 idescsi_end_request(drive, 1);
450                 return ide_stopped;
451         }
452
453         bcount.b.low    = HWIF(drive)->INB(IDE_BCOUNTL_REG);
454         bcount.b.high   = HWIF(drive)->INB(IDE_BCOUNTH_REG);
455         ireason.all     = HWIF(drive)->INB(IDE_IREASON_REG);
456
457         if (ireason.b.cod) {
458                 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
459                 return ide_do_reset(drive);
460         }
461         if (ireason.b.io) {
462                 temp = pc->actually_transferred + bcount.all;
463                 if (temp > pc->request_transfer) {
464                         if (temp > pc->buffer_size) {
465                                 printk(KERN_ERR "ide-scsi: The scsi wants to "
466                                         "send us more data than expected "
467                                         "- discarding data\n");
468                                 printk(KERN_ERR "ide-scsi: [");
469                                 hexdump(pc->c, 12);
470                                 printk("]\n");
471                                 printk(KERN_ERR "ide-scsi: expected %d got %d limit %d\n",
472                                         pc->request_transfer, temp, pc->buffer_size);
473                                 temp = pc->buffer_size - pc->actually_transferred;
474                                 if (temp) {
475                                         clear_bit(PC_WRITING, &pc->flags);
476                                         if (pc->sg)
477                                                 idescsi_input_buffers(drive, pc, temp);
478                                         else
479                                                 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, temp);
480                                         printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
481                                 }
482                                 pc->actually_transferred += temp;
483                                 pc->current_position += temp;
484                                 idescsi_discard_data(drive, bcount.all - temp);
485                                 if (HWGROUP(drive)->handler != NULL)
486                                         BUG();
487                                 ide_set_handler(drive,
488                                                 &idescsi_pc_intr,
489                                                 get_timeout(pc),
490                                                 NULL);
491                                 return ide_started;
492                         }
493 #if IDESCSI_DEBUG_LOG
494                         printk(KERN_NOTICE "ide-scsi: The scsi wants to send "
495                                 "us more data than expected - "
496                                 "allowing transfer\n");
497 #endif /* IDESCSI_DEBUG_LOG */
498                 }
499         }
500         if (ireason.b.io) {
501                 clear_bit(PC_WRITING, &pc->flags);
502                 if (pc->sg)
503                         idescsi_input_buffers(drive, pc, bcount.all);
504                 else
505                         HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
506         } else {
507                 set_bit(PC_WRITING, &pc->flags);
508                 if (pc->sg)
509                         idescsi_output_buffers(drive, pc, bcount.all);
510                 else
511                         HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
512         }
513         /* Update the current position */
514         pc->actually_transferred += bcount.all;
515         pc->current_position += bcount.all;
516
517         if (HWGROUP(drive)->handler != NULL)
518                 BUG();
519         /* And set the interrupt handler again */
520         ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
521         return ide_started;
522 }
523
524 static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive)
525 {
526         idescsi_scsi_t *scsi = drive->driver_data;
527         idescsi_pc_t *pc = scsi->pc;
528         atapi_ireason_t ireason;
529         ide_startstop_t startstop;
530
531         if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
532                 printk(KERN_ERR "ide-scsi: Strange, packet command "
533                         "initiated yet DRQ isn't asserted\n");
534                 return startstop;
535         }
536
537         ireason.all     = HWIF(drive)->INB(IDE_IREASON_REG);
538
539         if (!ireason.b.cod || ireason.b.io) {
540                 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
541                                 "issuing a packet command\n");
542                 return ide_do_reset(drive);
543         }
544
545         if (HWGROUP(drive)->handler != NULL)
546                 BUG();
547         /* Set the interrupt routine */
548         ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL);
549         /* Send the actual packet */
550         HWIF(drive)->atapi_output_bytes(drive, scsi->pc->c, 12);
551         if (test_bit (PC_DMA_OK, &pc->flags)) {
552                 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
553                 (void) (HWIF(drive)->ide_dma_begin(drive));
554         }
555         return ide_started;
556 }
557
558 /*
559  *      Issue a packet command
560  */
561 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
562 {
563         idescsi_scsi_t *scsi = drive->driver_data;
564         atapi_feature_t feature;
565         atapi_bcount_t bcount;
566         struct request *rq = pc->rq;
567
568         feature.all = 0;
569
570         /* Set the current packet command */
571         scsi->pc = pc;
572         /* We haven't transferred any data yet */
573         pc->actually_transferred = 0;
574         pc->current_position = pc->buffer;
575         /* Request to transfer the entire buffer at once */
576         bcount.all = IDE_MIN(pc->request_transfer, 63 * 1024);
577
578
579         if (drive->using_dma && rq->bh) {
580                 if (test_bit(PC_WRITING, &pc->flags))
581                         feature.b.dma = !HWIF(drive)->ide_dma_write(drive);
582                 else
583                         feature.b.dma = !HWIF(drive)->ide_dma_read(drive);
584         }
585
586         SELECT_DRIVE(drive);
587         if (IDE_CONTROL_REG)
588                 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
589         HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
590         HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
591         HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
592
593         if (feature.b.dma) {
594                 set_bit(PC_DMA_OK, &pc->flags);
595         }
596         if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
597                 if (HWGROUP(drive)->handler != NULL)
598                         BUG();
599                 ide_set_handler(drive,
600                                 &idescsi_transfer_pc,
601                                 get_timeout(pc),
602                                 NULL);
603                 /* Issue the packet command */
604                 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
605                 return ide_started;
606         } else {
607                 /* Issue the packet command */
608                 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
609                 return idescsi_transfer_pc(drive);
610         }
611 }
612
613 /*
614  *      idescsi_do_request is our request handling function.
615  */
616 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
617 {
618 #if IDESCSI_DEBUG_LOG
619         printk(KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",
620                 rq->rq_status, (unsigned int) rq->rq_dev, rq->cmd, rq->errors);
621         printk(KERN_INFO "sector: %ld, nr_sectors: %ld, "
622                 "current_nr_sectors: %ld\n", rq->sector,
623                 rq->nr_sectors, rq->current_nr_sectors);
624 #endif /* IDESCSI_DEBUG_LOG */
625
626         if (rq->cmd == IDESCSI_PC_RQ) {
627                 return idescsi_issue_pc(drive, rq->special);
628         }
629         printk(KERN_ERR "ide-scsi: %s: unsupported command in request "
630                 "queue (%x)\n", drive->name, rq->cmd);
631         idescsi_end_request(drive, 0);
632         return ide_stopped;
633 }
634
635 static int idescsi_do_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
636 {
637         /* need to figure out how to parse scsi-atapi media type */
638
639         return -EINVAL;
640 }
641
642 static int idescsi_ide_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
643 {
644         MOD_INC_USE_COUNT;
645         return 0;
646 }
647
648 static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
649 {
650         MOD_DEC_USE_COUNT;
651 }
652
653 static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES];
654 static int idescsi_initialized = 0;
655 static int drive_count = 0;
656
657 static void idescsi_add_settings(ide_drive_t *drive)
658 {
659         idescsi_scsi_t *scsi = drive->driver_data;
660
661 /*
662  *                      drive   setting name    read/write      ioctl   ioctl           data type       min     max     mul_factor      div_factor      data pointer            set function
663  */
664         ide_add_setting(drive,  "bios_cyl",     SETTING_RW,     -1,     -1,             TYPE_INT,       0,      1023,   1,              1,              &drive->bios_cyl,       NULL);
665         ide_add_setting(drive,  "bios_head",    SETTING_RW,     -1,     -1,             TYPE_BYTE,      0,      255,    1,              1,              &drive->bios_head,      NULL);
666         ide_add_setting(drive,  "bios_sect",    SETTING_RW,     -1,     -1,             TYPE_BYTE,      0,      63,     1,              1,              &drive->bios_sect,      NULL);
667         ide_add_setting(drive,  "transform",    SETTING_RW,     -1,     -1,             TYPE_INT,       0,      3,      1,              1,              &scsi->transform,       NULL);
668         ide_add_setting(drive,  "log",          SETTING_RW,     -1,     -1,             TYPE_INT,       0,      1,      1,              1,              &scsi->log,             NULL);
669 }
670
671 /*
672  *      Driver initialization.
673  */
674 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id)
675 {
676         int minor = (drive->select.b.unit) << PARTN_BITS;
677
678         DRIVER(drive)->busy++;
679         idescsi_drives[id] = drive;
680         drive->driver_data = scsi;
681         drive->ready_stat = 0;
682         memset(scsi, 0, sizeof(idescsi_scsi_t));
683         scsi->drive = drive;
684         scsi->id = id;
685         if (drive->id && (drive->id->config & 0x0060) == 0x20)
686                 set_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags);
687         set_bit(IDESCSI_TRANSFORM, &scsi->transform);
688         clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
689 #if IDESCSI_DEBUG_LOG
690         set_bit(IDESCSI_LOG_CMD, &scsi->log);
691 #endif /* IDESCSI_DEBUG_LOG */
692         idescsi_add_settings(drive);
693 #ifdef IDESCSI_DEVFS
694         scsi->de = devfs_register(drive->de, "generic", DEVFS_FL_DEFAULT,
695                                         HWIF(drive)->major, minor,
696                                         S_IFBLK | S_IRUSR | S_IWUSR,
697                                         ide_fops, NULL);
698 #endif /* IDESCSI_DEVFS */
699         drive_count++;
700         DRIVER(drive)->busy--;
701 }
702
703 static int idescsi_cleanup (ide_drive_t *drive)
704 {
705         idescsi_scsi_t *scsi = drive->driver_data;
706
707         if (ide_unregister_subdriver(drive)) {
708                 printk("%s: %s: failed to unregister! \n",
709                         __FUNCTION__, drive->name);
710                 printk("%s: usage %d, busy %d, driver %p, Dbusy %d\n",
711                         drive->name, drive->usage, drive->busy,
712                         drive->driver, DRIVER(drive)->busy);
713                 return 1;
714         }
715         idescsi_drives[scsi->id] = NULL;
716 #ifdef IDESCSI_DEVFS
717         if (scsi->de)
718                 devfs_unregister(scsi->de);
719 #endif /* IDESCSI_DEVFS */
720         drive->driver_data = NULL;
721         kfree(scsi);
722         drive_count--;
723         return 0;
724 }
725
726 int idescsi_init(void);
727 int idescsi_attach(ide_drive_t *drive);
728
729 /*
730  *      IDE subdriver functions, registered with ide.c
731  */
732 static ide_driver_t idescsi_driver = {
733         name:                   "ide-scsi",
734         version:                IDESCSI_VERSION,
735         media:                  ide_scsi,
736         busy:                   0,
737 #ifdef CONFIG_IDEDMA_ONLYDISK
738         supports_dma:           0,
739 #else
740         supports_dma:           1,
741 #endif
742         supports_dsc_overlap:   0,
743         cleanup:                idescsi_cleanup,
744         standby:                NULL,
745         suspend:                NULL,
746         resume:                 NULL,
747         flushcache:             NULL,
748         do_request:             idescsi_do_request,
749         end_request:            idescsi_end_request,
750         sense:                  NULL,
751         error:                  NULL,
752         ioctl:                  idescsi_do_ioctl,
753         open:                   idescsi_ide_open,
754         release:                idescsi_ide_release,
755         media_change:           NULL,
756         revalidate:             NULL,
757         pre_reset:              NULL,
758         capacity:               NULL,
759         special:                NULL,
760         proc:                   NULL,
761         init:                   idescsi_init,
762         attach:                 idescsi_attach,
763         ata_prebuilder:         NULL,
764         atapi_prebuilder:       NULL,
765 };
766
767 static ide_module_t idescsi_module = {
768         IDE_DRIVER_MODULE,
769         idescsi_init,
770         &idescsi_driver,
771         NULL
772 };
773
774 int idescsi_attach (ide_drive_t *drive)
775 {
776         idescsi_scsi_t *scsi;
777         u8 media[] = {  TYPE_DISK,              /* 0x00 */
778                         TYPE_TAPE,              /* 0x01 */
779                         TYPE_PRINTER,           /* 0x02 */
780                         TYPE_PROCESSOR,         /* 0x03 */
781                         TYPE_WORM,              /* 0x04 */
782                         TYPE_ROM,               /* 0x05 */
783                         TYPE_SCANNER,           /* 0x06 */
784                         TYPE_MOD,               /* 0x07 */
785                         255};
786         int i = 0, ret = 0, id = 0;
787 //      int id = 2 * HWIF(drive)->index + drive->select.b.unit;
788 //      int id = drive_count + 1;
789
790         for (id = 0; id < MAX_HWIFS*MAX_DRIVES; id++)
791                 if (idescsi_drives[id] == NULL)
792                         break;
793
794         printk("%s: id = %d\n", drive->name, id);
795
796         if ((!idescsi_initialized) || (drive->media == ide_disk)) {
797                 printk(KERN_ERR "ide-scsi: (%sinitialized) %s: "
798                                 "media-type (%ssupported)\n",
799                         (idescsi_initialized) ? "" : "! ",
800                         drive->name,
801                         (drive->media == ide_disk) ? "! " : "");
802                 return (drive->media == ide_disk) ? 2 : 0;
803         }
804
805         MOD_INC_USE_COUNT;
806
807         for (i = 0; media[i] != 255; i++) {
808                 if (drive->media != media[i])
809                         continue;
810                 else
811                         break;
812         }
813
814         if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
815                 printk(KERN_ERR "ide-scsi: %s: Can't allocate a scsi "
816                         "structure\n", drive->name);
817                 ret = 1;
818                 goto bye_game_over;
819         }
820         if (ide_register_subdriver(drive, &idescsi_driver,
821                         IDE_SUBDRIVER_VERSION)) {
822                 printk(KERN_ERR "ide-scsi: %s: Failed to register the "
823                         "driver with ide.c\n", drive->name);
824                 kfree(scsi);
825                 ret = 1;
826                 goto bye_game_over;
827         }
828
829         idescsi_setup(drive, scsi, id);
830
831 //      scan_scsis(HBA, 1, channel, id, lun);
832 bye_game_over:
833         MOD_DEC_USE_COUNT;
834         return ret;
835 }
836
837 #ifdef MODULE
838 /* options */
839 char *ignore = NULL;
840
841 MODULE_PARM(ignore, "s");
842 #endif
843
844 int idescsi_init (void)
845 {
846 #ifdef CLASSIC_BUILTINS_METHOD
847         ide_drive_t *drive;
848         idescsi_scsi_t *scsi;
849         u8 media[] = {  TYPE_DISK,              /* 0x00 */
850                         TYPE_TAPE,              /* 0x01 */
851                         TYPE_PRINTER,           /* 0x02 */
852                         TYPE_PROCESSOR,         /* 0x03 */
853                         TYPE_WORM,              /* 0x04 */
854                         TYPE_ROM,               /* 0x05 */
855                         TYPE_SCANNER,           /* 0x06 */
856                         TYPE_MOD,               /* 0x07 */
857                         255};
858
859         int i, failed, id;
860
861         if (idescsi_initialized)
862                 return 0;
863         idescsi_initialized = 1;
864         for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
865                 idescsi_drives[i] = NULL;
866         MOD_INC_USE_COUNT;
867         for (i = 0; media[i] != 255; i++) {
868                 failed = 0;
869                 while ((drive = ide_scan_devices(media[i],
870                                 idescsi_driver.name, NULL, failed++)) != NULL) {
871 #ifdef MODULE
872                         /* skip drives we were told to ignore */
873                         if (ignore != NULL && strstr(ignore, drive->name)) {
874                                 printk("ide-scsi: ignoring drive %s\n",
875                                         drive->name);
876                                 continue;
877                         }
878 #endif
879
880                 if ((scsi = (idescsi_scsi_t *) kmalloc(sizeof(idescsi_scsi_t), GFP_KERNEL)) == NULL) {
881                                 printk(KERN_ERR "ide-scsi: %s: Can't allocate "
882                                         "a scsi structure\n", drive->name);
883                                 continue;
884                         }
885                         if (ide_register_subdriver(drive, &idescsi_driver,
886                                         IDE_SUBDRIVER_VERSION)) {
887                                 printk(KERN_ERR "ide-scsi: %s: Failed to "
888                                         "register the driver with ide.c\n",
889                                         drive->name);
890                                 kfree(scsi);
891                                 continue;
892                         }
893                         for (id = 0;
894                                 id < MAX_HWIFS*MAX_DRIVES && idescsi_drives[id];
895                                         id++);
896                                 idescsi_setup(drive, scsi, id);
897                         failed--;
898                 }
899         }
900 #else /* ! CLASSIC_BUILTINS_METHOD */
901         int i;
902
903         if (idescsi_initialized)
904                 return 0;
905         idescsi_initialized = 1;
906         for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++)
907                 idescsi_drives[i] = NULL;
908         MOD_INC_USE_COUNT;
909 #endif /* CLASSIC_BUILTINS_METHOD */
910         ide_register_module(&idescsi_module);
911         MOD_DEC_USE_COUNT;
912         return 0;
913 }
914
915 int idescsi_detect (Scsi_Host_Template *host_template)
916 {
917         struct Scsi_Host *host;
918         int id;
919         int last_lun = 0;
920
921         host_template->proc_name = "ide-scsi";
922         host = scsi_register(host_template, 0);
923         if (host == NULL) {
924                 printk(KERN_WARNING "%s: host failure!\n", __FUNCTION__);
925                 return 0;
926         }
927
928         for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++)
929                 last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun);
930         host->max_id = id;
931         host->max_lun = last_lun + 1;
932         host->can_queue = host->cmd_per_lun * id;
933         return 1;
934 }
935
936 int idescsi_release (struct Scsi_Host *host)
937 {
938         ide_drive_t *drive;
939         int id;
940
941         for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) {
942                 drive = idescsi_drives[id];
943                 if (drive)
944                         DRIVER(drive)->busy = 0;
945         }
946         return 0;
947 }
948
949 const char *idescsi_info (struct Scsi_Host *host)
950 {
951         return "SCSI host adapter emulation for IDE ATAPI devices";
952 }
953
954 int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg)
955 {
956         ide_drive_t *drive = idescsi_drives[dev->id];
957         idescsi_scsi_t *scsi = drive->driver_data;
958
959         if (cmd == SG_SET_TRANSFORM) {
960                 if (arg)
961                         set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
962                 else
963                         clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
964                 return 0;
965         } else if (cmd == SG_GET_TRANSFORM)
966                 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg);
967         return -EINVAL;
968 }
969
970 static inline struct buffer_head *idescsi_kmalloc_bh (int count)
971 {
972         struct buffer_head *bh, *bhp, *first_bh;
973
974         if ((first_bh = bhp = bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
975                 goto abort;
976         memset(bh, 0, sizeof(struct buffer_head));
977         bh->b_reqnext = NULL;
978         while (--count) {
979                 if ((bh = kmalloc(sizeof(struct buffer_head), GFP_ATOMIC)) == NULL)
980                         goto abort;
981                 memset(bh, 0, sizeof(struct buffer_head));
982                 bhp->b_reqnext = bh;
983                 bhp = bh;
984                 bh->b_reqnext = NULL;
985         }
986         return first_bh;
987 abort:
988         idescsi_free_bh(first_bh);
989         return NULL;
990 }
991
992 static inline int idescsi_set_direction (idescsi_pc_t *pc)
993 {
994         switch (pc->c[0]) {
995                 case READ_6:
996                 case READ_10:
997                 case READ_12:
998                         clear_bit(PC_WRITING, &pc->flags);
999                         return 0;
1000                 case WRITE_6:
1001                 case WRITE_10:
1002                 case WRITE_12:
1003                         set_bit(PC_WRITING, &pc->flags);
1004                         return 0;
1005                 default:
1006                         return 1;
1007         }
1008 }
1009
1010 static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc)
1011 {
1012         struct buffer_head *bh = NULL, *first_bh = NULL;
1013         int segments = pc->scsi_cmd->use_sg;
1014         struct scatterlist *sg = pc->scsi_cmd->request_buffer;
1015
1016         if (!drive->using_dma || !pc->request_transfer || pc->request_transfer & 1023)
1017                 return NULL;
1018         if (idescsi_set_direction(pc))
1019                 return NULL;
1020         if (segments) {
1021                 if ((first_bh = bh = idescsi_kmalloc_bh(segments)) == NULL)
1022                         return NULL;
1023 #if IDESCSI_DEBUG_LOG
1024                 printk("ide-scsi: %s: building DMA table, %d segments, "
1025                         "%dkB total\n", drive->name, segments,
1026                         pc->request_transfer >> 10);
1027 #endif /* IDESCSI_DEBUG_LOG */
1028                 while (segments--) {
1029 #if 1
1030                         bh->b_data = sg->address;
1031 #else
1032                         if (sg->address) {
1033                                 bh->b_page = virt_to_page(sg->address);
1034                                 bh->b_data = (char *) ((unsigned long) sg->address & ~PAGE_MASK);
1035                         } else if (sg->page) {
1036                                 bh->b_page = sg->page;
1037                                 bh->b_data = (char *) sg->offset;
1038                         }
1039 #endif
1040                         bh->b_size = sg->length;
1041                         bh = bh->b_reqnext;
1042                         sg++;
1043                 }
1044         } else {
1045                 /*
1046                  * non-sg requests are guarenteed not to reside in highmem /jens
1047                  */
1048                 if ((first_bh = bh = idescsi_kmalloc_bh(1)) == NULL)
1049                         return NULL;
1050 #if IDESCSI_DEBUG_LOG
1051                 printk("ide-scsi: %s: building DMA table for a single "
1052                         "buffer (%dkB)\n", drive->name,
1053                         pc->request_transfer >> 10);
1054 #endif /* IDESCSI_DEBUG_LOG */
1055                 bh->b_data = pc->scsi_cmd->request_buffer;
1056                 bh->b_size = pc->request_transfer;
1057         }
1058         return first_bh;
1059 }
1060
1061 static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd)
1062 {
1063         idescsi_scsi_t *scsi = drive->driver_data;
1064
1065         if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR)
1066                 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
1067         return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
1068 }
1069
1070 int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
1071 {
1072         ide_drive_t *drive = idescsi_drives[cmd->target];
1073         idescsi_scsi_t *scsi;
1074         struct request *rq = NULL;
1075         idescsi_pc_t *pc = NULL;
1076
1077         if (!drive) {
1078                 printk(KERN_ERR "ide-scsi: drive id %d not present\n",
1079                         cmd->target);
1080                 goto abort;
1081         }
1082         scsi = drive->driver_data;
1083         pc = kmalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
1084         rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
1085         if (rq == NULL || pc == NULL) {
1086                 printk(KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
1087                 goto abort;
1088         }
1089
1090         memset(pc->c, 0, 12);
1091         pc->flags = 0;
1092         pc->rq = rq;
1093         memcpy(pc->c, cmd->cmnd, cmd->cmd_len);
1094         if (cmd->use_sg) {
1095                 pc->buffer = NULL;
1096                 pc->sg = cmd->request_buffer;
1097         } else {
1098                 pc->buffer = cmd->request_buffer;
1099                 pc->sg = NULL;
1100         }
1101         pc->b_count = 0;
1102         pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
1103         pc->scsi_cmd = cmd;
1104         pc->done = done;
1105         pc->timeout = jiffies + cmd->timeout_per_command;
1106
1107         if (should_transform(drive, cmd))
1108                 set_bit(PC_TRANSFORM, &pc->flags);
1109         idescsi_transform_pc1(drive, pc);
1110
1111         if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
1112                 printk("ide-scsi: %s: que %lu, cmd = ",
1113                         drive->name, cmd->serial_number);
1114                 hexdump(cmd->cmnd, cmd->cmd_len);
1115                 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
1116                         printk("ide-scsi: %s: que %lu, tsl = ",
1117                                 drive->name, cmd->serial_number);
1118                         hexdump(pc->c, 12);
1119                 }
1120         }
1121
1122         ide_init_drive_cmd(rq);
1123         rq->special = pc;
1124         rq->bh = idescsi_dma_bh(drive, pc);
1125         rq->cmd = IDESCSI_PC_RQ;
1126         spin_unlock_irq(&io_request_lock);
1127         (void) ide_do_drive_cmd(drive, rq, ide_end);
1128         spin_lock_irq(&io_request_lock);
1129         return 0;
1130 abort:
1131         if (pc) kfree(pc);
1132         if (rq) kfree(rq);
1133         cmd->result = DID_ERROR << 16;
1134         done(cmd);
1135         return 0;
1136 }
1137
1138 int idescsi_abort (Scsi_Cmnd *cmd)
1139 {
1140         return SCSI_ABORT_SNOOZE;
1141 }
1142
1143 int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags)
1144 {
1145         return SCSI_RESET_SNOOZE;
1146
1147 #ifdef WORK_IN_PROGRESS
1148         ide_drive_t *drive      = idescsi_drives[cmd->target];
1149
1150         /* At this point the state machine is running, that
1151            requires we are especially careful. Ideally we want
1152            to abort commands on timeout only if they hit the
1153            cable but thats harder */
1154
1155         DRIVER(drive)->abort(drive, "scsi reset");
1156         if(HWGROUP(drive)->handler)
1157                 BUG();
1158         
1159         /* Ok the state machine is halted but make sure it
1160            doesn't restart too early */ 
1161            
1162         HWGROUP(drive)->busy = 1;
1163         spin_unlock_irq(&io_request_lock);
1164         
1165         /* Apply the mallet of re-education firmly to the drive */
1166         ide_do_reset(drive);
1167
1168         /* At this point the reset state machine is running and
1169            its termination will kick off the next command */    
1170         spin_lock_irq(&io_request_lock);
1171         return SCSI_RESET_SUCCESS;
1172 #endif  
1173 }
1174
1175 int idescsi_bios (Disk *disk, kdev_t dev, int *parm)
1176 {
1177         ide_drive_t *drive = idescsi_drives[disk->device->id];
1178
1179         if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1180                 parm[0] = drive->bios_head;
1181                 parm[1] = drive->bios_sect;
1182                 parm[2] = drive->bios_cyl;
1183         }
1184         return 0;
1185 }
1186
1187 static Scsi_Host_Template idescsi_template = IDESCSI;
1188
1189 static int __init init_idescsi_module(void)
1190 {
1191         drive_count = 0;
1192         idescsi_init();
1193         idescsi_template.module = THIS_MODULE;
1194         scsi_register_module(MODULE_SCSI_HA, &idescsi_template);
1195         return 0;
1196 }
1197
1198 static void __exit exit_idescsi_module(void)
1199 {
1200         ide_drive_t *drive;
1201         u8 media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255};
1202         int i, failed;
1203
1204         scsi_unregister_module(MODULE_SCSI_HA, &idescsi_template);
1205         for (i = 0; media[i] != 255; i++) {
1206                 failed = 0;
1207                 while ((drive = ide_scan_devices(media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL)
1208                         if (idescsi_cleanup(drive)) {
1209                                 printk("%s: exit_idescsi_module() called while still busy\n", drive->name);
1210                                 failed++;
1211                         }
1212         }
1213         ide_unregister_module(&idescsi_module);
1214 }
1215
1216 module_init(init_idescsi_module);
1217 module_exit(exit_idescsi_module);
1218 MODULE_LICENSE("GPL");