]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/ide-scsi.c
Merge iSeries include file move
[karo-tx-linux.git] / drivers / scsi / ide-scsi.c
1 /*
2  * linux/drivers/scsi/ide-scsi.c        Version 0.9             Jul   4, 1999
3  *
4  * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5  */
6
7 /*
8  * Emulation of a SCSI host adapter for IDE ATAPI devices.
9  *
10  * With this driver, one can use the Linux SCSI drivers instead of the
11  * native IDE ATAPI drivers.
12  *
13  * Ver 0.1   Dec  3 96   Initial version.
14  * Ver 0.2   Jan 26 97   Fixed bug in cleanup_module() and added emulation
15  *                        of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
16  *                        to Janos Farkas for pointing this out.
17  *                       Avoid using bitfields in structures for m68k.
18  *                       Added Scatter/Gather and DMA support.
19  * Ver 0.4   Dec  7 97   Add support for ATAPI PD/CD drives.
20  *                       Use variable timeout for each command.
21  * Ver 0.5   Jan  2 98   Fix previous PD/CD support.
22  *                       Allow disabling of SCSI-6 to SCSI-10 transformation.
23  * Ver 0.6   Jan 27 98   Allow disabling of SCSI command translation layer
24  *                        for access through /dev/sg.
25  *                       Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
26  * Ver 0.7   Dec 04 98   Ignore commands where lun != 0 to avoid multiple
27  *                        detection of devices with CONFIG_SCSI_MULTI_LUN
28  * Ver 0.8   Feb 05 99   Optical media need translation too. Reverse 0.7.
29  * Ver 0.9   Jul 04 99   Fix a bug in SG_SET_TRANSFORM.
30  * Ver 0.91  Jun 10 02   Fix "off by one" error in transforms
31  * Ver 0.92  Dec 31 02   Implement new SCSI mid level API
32  */
33
34 #define IDESCSI_VERSION "0.92"
35
36 #include <linux/module.h>
37 #include <linux/config.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/ioport.h>
43 #include <linux/blkdev.h>
44 #include <linux/errno.h>
45 #include <linux/hdreg.h>
46 #include <linux/slab.h>
47 #include <linux/ide.h>
48 #include <linux/scatterlist.h>
49 #include <linux/delay.h>
50
51 #include <asm/io.h>
52 #include <asm/bitops.h>
53 #include <asm/uaccess.h>
54
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_tcq.h>
60 #include <scsi/sg.h>
61
62 #define IDESCSI_DEBUG_LOG               0
63
64 typedef struct idescsi_pc_s {
65         u8 c[12];                               /* Actual packet bytes */
66         int request_transfer;                   /* Bytes to transfer */
67         int actually_transferred;               /* Bytes actually transferred */
68         int buffer_size;                        /* Size of our data buffer */
69         struct request *rq;                     /* The corresponding request */
70         u8 *buffer;                             /* Data buffer */
71         u8 *current_position;                   /* Pointer into the above buffer */
72         struct scatterlist *sg;                 /* Scatter gather table */
73         int b_count;                            /* Bytes transferred from current entry */
74         struct scsi_cmnd *scsi_cmd;             /* SCSI command */
75         void (*done)(struct scsi_cmnd *);       /* Scsi completion routine */
76         unsigned long flags;                    /* Status/Action flags */
77         unsigned long timeout;                  /* Command timeout */
78 } idescsi_pc_t;
79
80 /*
81  *      Packet command status bits.
82  */
83 #define PC_DMA_IN_PROGRESS              0       /* 1 while DMA in progress */
84 #define PC_WRITING                      1       /* Data direction */
85 #define PC_TRANSFORM                    2       /* transform SCSI commands */
86 #define PC_TIMEDOUT                     3       /* command timed out */
87 #define PC_DMA_OK                       4       /* Use DMA */
88
89 /*
90  *      SCSI command transformation layer
91  */
92 #define IDESCSI_TRANSFORM               0       /* Enable/Disable transformation */
93 #define IDESCSI_SG_TRANSFORM            1       /* /dev/sg transformation */
94
95 /*
96  *      Log flags
97  */
98 #define IDESCSI_LOG_CMD                 0       /* Log SCSI commands */
99
100 typedef struct ide_scsi_obj {
101         ide_drive_t             *drive;
102         ide_driver_t            *driver;
103         struct gendisk          *disk;
104         struct Scsi_Host        *host;
105
106         idescsi_pc_t *pc;                       /* Current packet command */
107         unsigned long flags;                    /* Status/Action flags */
108         unsigned long transform;                /* SCSI cmd translation layer */
109         unsigned long log;                      /* log flags */
110 } idescsi_scsi_t;
111
112 static DECLARE_MUTEX(idescsi_ref_sem);
113
114 #define ide_scsi_g(disk) \
115         container_of((disk)->private_data, struct ide_scsi_obj, driver)
116
117 static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
118 {
119         struct ide_scsi_obj *scsi = NULL;
120
121         down(&idescsi_ref_sem);
122         scsi = ide_scsi_g(disk);
123         if (scsi)
124                 scsi_host_get(scsi->host);
125         up(&idescsi_ref_sem);
126         return scsi;
127 }
128
129 static void ide_scsi_put(struct ide_scsi_obj *scsi)
130 {
131         down(&idescsi_ref_sem);
132         scsi_host_put(scsi->host);
133         up(&idescsi_ref_sem);
134 }
135
136 static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
137 {
138         return (idescsi_scsi_t*) (&host[1]);
139 }
140
141 static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
142 {
143         return scsihost_to_idescsi(ide_drive->driver_data);
144 }
145
146 /*
147  *      Per ATAPI device status bits.
148  */
149 #define IDESCSI_DRQ_INTERRUPT           0       /* DRQ interrupt device */
150
151 /*
152  *      ide-scsi requests.
153  */
154 #define IDESCSI_PC_RQ                   90
155
156 static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
157 {
158         while (bcount--)
159                 (void) HWIF(drive)->INB(IDE_DATA_REG);
160 }
161
162 static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
163 {
164         while (bcount--)
165                 HWIF(drive)->OUTB(0, IDE_DATA_REG);
166 }
167
168 /*
169  *      PIO data transfer routines using the scatter gather table.
170  */
171 static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
172 {
173         int count;
174         char *buf;
175
176         while (bcount) {
177                 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
178                         printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
179                         idescsi_discard_data (drive, bcount);
180                         return;
181                 }
182                 count = min(pc->sg->length - pc->b_count, bcount);
183                 buf = kmap_atomic(pc->sg->page, KM_IRQ0);
184                 drive->hwif->atapi_input_bytes(drive,
185                                 buf + pc->b_count + pc->sg->offset, count);
186                 kunmap_atomic(buf, KM_IRQ0);
187                 bcount -= count;
188                 pc->b_count += count;
189                 if (pc->b_count == pc->sg->length) {
190                         pc->sg++;
191                         pc->b_count = 0;
192                 }
193         }
194 }
195
196 static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
197 {
198         int count;
199         char *buf;
200
201         while (bcount) {
202                 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
203                         printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
204                         idescsi_output_zeros (drive, bcount);
205                         return;
206                 }
207                 count = min(pc->sg->length - pc->b_count, bcount);
208                 buf = kmap_atomic(pc->sg->page, KM_IRQ0);
209                 drive->hwif->atapi_output_bytes(drive,
210                                 buf + pc->b_count + pc->sg->offset, count);
211                 kunmap_atomic(buf, KM_IRQ0);
212                 bcount -= count;
213                 pc->b_count += count;
214                 if (pc->b_count == pc->sg->length) {
215                         pc->sg++;
216                         pc->b_count = 0;
217                 }
218         }
219 }
220
221 /*
222  *      Most of the SCSI commands are supported directly by ATAPI devices.
223  *      idescsi_transform_pc handles the few exceptions.
224  */
225 static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
226 {
227         u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
228         char *atapi_buf;
229
230         if (!test_bit(PC_TRANSFORM, &pc->flags))
231                 return;
232         if (drive->media == ide_cdrom || drive->media == ide_optical) {
233                 if (c[0] == READ_6 || c[0] == WRITE_6) {
234                         c[8] = c[4];            c[5] = c[3];            c[4] = c[2];
235                         c[3] = c[1] & 0x1f;     c[2] = 0;               c[1] &= 0xe0;
236                         c[0] += (READ_10 - READ_6);
237                 }
238                 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
239                         unsigned short new_len;
240                         if (!scsi_buf)
241                                 return;
242                         if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
243                                 return;
244                         memset(atapi_buf, 0, pc->buffer_size + 4);
245                         memset (c, 0, 12);
246                         c[0] = sc[0] | 0x40;
247                         c[1] = sc[1];
248                         c[2] = sc[2];
249                         new_len = sc[4] + 4;
250                         c[8] = new_len;
251                         c[7] = new_len >> 8;
252                         c[9] = sc[5];
253                         if (c[0] == MODE_SELECT_10) {
254                                 atapi_buf[1] = scsi_buf[0];     /* Mode data length */
255                                 atapi_buf[2] = scsi_buf[1];     /* Medium type */
256                                 atapi_buf[3] = scsi_buf[2];     /* Device specific parameter */
257                                 atapi_buf[7] = scsi_buf[3];     /* Block descriptor length */
258                                 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
259                         }
260                         pc->buffer = atapi_buf;
261                         pc->request_transfer += 4;
262                         pc->buffer_size += 4;
263                 }
264         }
265 }
266
267 static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
268 {
269         u8 *atapi_buf = pc->buffer;
270         u8 *sc = pc->scsi_cmd->cmnd;
271         u8 *scsi_buf = pc->scsi_cmd->request_buffer;
272
273         if (!test_bit(PC_TRANSFORM, &pc->flags))
274                 return;
275         if (drive->media == ide_cdrom || drive->media == ide_optical) {
276                 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
277                         scsi_buf[0] = atapi_buf[1];             /* Mode data length */
278                         scsi_buf[1] = atapi_buf[2];             /* Medium type */
279                         scsi_buf[2] = atapi_buf[3];             /* Device specific parameter */
280                         scsi_buf[3] = atapi_buf[7];             /* Block descriptor length */
281                         memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
282                 }
283                 if (pc->c[0] == INQUIRY) {
284                         scsi_buf[2] |= 2;                       /* ansi_revision */
285                         scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
286                 }
287         }
288         if (atapi_buf && atapi_buf != scsi_buf)
289                 kfree(atapi_buf);
290 }
291
292 static void hexdump(u8 *x, int len)
293 {
294         int i;
295
296         printk("[ ");
297         for (i = 0; i < len; i++)
298                 printk("%x ", x[i]);
299         printk("]\n");
300 }
301
302 static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
303 {
304         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
305         idescsi_pc_t   *pc;
306         struct request *rq;
307         u8             *buf;
308
309         /* stuff a sense request in front of our current request */
310         pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
311         rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
312         buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
313         if (pc == NULL || rq == NULL || buf == NULL) {
314                 if (pc) kfree(pc);
315                 if (rq) kfree(rq);
316                 if (buf) kfree(buf);
317                 return -ENOMEM;
318         }
319         memset (pc, 0, sizeof (idescsi_pc_t));
320         memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
321         ide_init_drive_cmd(rq);
322         rq->special = (char *) pc;
323         pc->rq = rq;
324         pc->buffer = buf;
325         pc->c[0] = REQUEST_SENSE;
326         pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
327         rq->flags = REQ_SENSE;
328         pc->timeout = jiffies + WAIT_READY;
329         /* NOTE! Save the failed packet command in "rq->buffer" */
330         rq->buffer = (void *) failed_command->special;
331         pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
332         if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
333                 printk ("ide-scsi: %s: queue cmd = ", drive->name);
334                 hexdump(pc->c, 6);
335         }
336         rq->rq_disk = scsi->disk;
337         return ide_do_drive_cmd(drive, rq, ide_preempt);
338 }
339
340 static int idescsi_end_request(ide_drive_t *, int, int);
341
342 static ide_startstop_t
343 idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
344 {
345         if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
346                 /* force an abort */
347                 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
348
349         rq->errors++;
350
351         idescsi_end_request(drive, 0, 0);
352
353         return ide_stopped;
354 }
355
356 static ide_startstop_t
357 idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
358 {
359 #if IDESCSI_DEBUG_LOG
360         printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
361                         ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
362 #endif
363         rq->errors |= ERROR_MAX;
364
365         idescsi_end_request(drive, 0, 0);
366
367         return ide_stopped;
368 }
369
370 static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
371 {
372         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
373         struct request *rq = HWGROUP(drive)->rq;
374         idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
375         int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
376         struct Scsi_Host *host;
377         u8 *scsi_buf;
378         unsigned long flags;
379
380         if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) {
381                 ide_end_request(drive, uptodate, nrsecs);
382                 return 0;
383         }
384         ide_end_drive_cmd (drive, 0, 0);
385         if (rq->flags & REQ_SENSE) {
386                 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
387                 if (log) {
388                         printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
389                         hexdump(pc->buffer,16);
390                 }
391                 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
392                 kfree(pc->buffer);
393                 kfree(pc);
394                 kfree(rq);
395                 pc = opc;
396                 rq = pc->rq;
397                 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
398                                         ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
399         } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
400                 if (log)
401                         printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
402                                         drive->name, pc->scsi_cmd->serial_number);
403                 pc->scsi_cmd->result = DID_TIME_OUT << 16;
404         } else if (rq->errors >= ERROR_MAX) {
405                 pc->scsi_cmd->result = DID_ERROR << 16;
406                 if (log)
407                         printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
408         } else if (rq->errors) {
409                 if (log)
410                         printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
411                 if (!idescsi_check_condition(drive, rq))
412                         /* we started a request sense, so we'll be back, exit for now */
413                         return 0;
414                 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
415         } else {
416                 pc->scsi_cmd->result = DID_OK << 16;
417                 idescsi_transform_pc2 (drive, pc);
418                 if (log) {
419                         printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
420                         if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
421                                 printk(", rst = ");
422                                 scsi_buf = pc->scsi_cmd->request_buffer;
423                                 hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
424                         } else printk("\n");
425                 }
426         }
427         host = pc->scsi_cmd->device->host;
428         spin_lock_irqsave(host->host_lock, flags);
429         pc->done(pc->scsi_cmd);
430         spin_unlock_irqrestore(host->host_lock, flags);
431         kfree(pc);
432         kfree(rq);
433         scsi->pc = NULL;
434         return 0;
435 }
436
437 static inline unsigned long get_timeout(idescsi_pc_t *pc)
438 {
439         return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
440 }
441
442 static int idescsi_expiry(ide_drive_t *drive)
443 {
444         idescsi_scsi_t *scsi = drive->driver_data;
445         idescsi_pc_t   *pc   = scsi->pc;
446
447 #if IDESCSI_DEBUG_LOG
448         printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
449 #endif
450         set_bit(PC_TIMEDOUT, &pc->flags);
451
452         return 0;                                       /* we do not want the ide subsystem to retry */
453 }
454
455 /*
456  *      Our interrupt handler.
457  */
458 static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
459 {
460         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
461         idescsi_pc_t *pc=scsi->pc;
462         struct request *rq = pc->rq;
463         atapi_bcount_t bcount;
464         atapi_status_t status;
465         atapi_ireason_t ireason;
466         atapi_feature_t feature;
467
468         unsigned int temp;
469
470 #if IDESCSI_DEBUG_LOG
471         printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
472 #endif /* IDESCSI_DEBUG_LOG */
473
474         if (test_bit(PC_TIMEDOUT, &pc->flags)){
475 #if IDESCSI_DEBUG_LOG
476                 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet  %lu at %lu\n",
477                                 pc->scsi_cmd->serial_number, jiffies);
478 #endif
479                 /* end this request now - scsi should retry it*/
480                 idescsi_end_request (drive, 1, 0);
481                 return ide_stopped;
482         }
483         if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
484 #if IDESCSI_DEBUG_LOG
485                 printk ("ide-scsi: %s: DMA complete\n", drive->name);
486 #endif /* IDESCSI_DEBUG_LOG */
487                 pc->actually_transferred=pc->request_transfer;
488                 (void) HWIF(drive)->ide_dma_end(drive);
489         }
490
491         feature.all = 0;
492         /* Clear the interrupt */
493         status.all = HWIF(drive)->INB(IDE_STATUS_REG);
494
495         if (!status.b.drq) {
496                 /* No more interrupts */
497                 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
498                         printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
499                 local_irq_enable();
500                 if (status.b.check)
501                         rq->errors++;
502                 idescsi_end_request (drive, 1, 0);
503                 return ide_stopped;
504         }
505         bcount.b.low    = HWIF(drive)->INB(IDE_BCOUNTL_REG);
506         bcount.b.high   = HWIF(drive)->INB(IDE_BCOUNTH_REG);
507         ireason.all     = HWIF(drive)->INB(IDE_IREASON_REG);
508
509         if (ireason.b.cod) {
510                 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
511                 return ide_do_reset (drive);
512         }
513         if (ireason.b.io) {
514                 temp = pc->actually_transferred + bcount.all;
515                 if (temp > pc->request_transfer) {
516                         if (temp > pc->buffer_size) {
517                                 printk(KERN_ERR "ide-scsi: The scsi wants to "
518                                         "send us more data than expected "
519                                         "- discarding data\n");
520                                 temp = pc->buffer_size - pc->actually_transferred;
521                                 if (temp) {
522                                         clear_bit(PC_WRITING, &pc->flags);
523                                         if (pc->sg)
524                                                 idescsi_input_buffers(drive, pc, temp);
525                                         else
526                                                 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
527                                         printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
528                                 }
529                                 pc->actually_transferred += temp;
530                                 pc->current_position += temp;
531                                 idescsi_discard_data(drive, bcount.all - temp);
532                                 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
533                                 return ide_started;
534                         }
535 #if IDESCSI_DEBUG_LOG
536                         printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
537 #endif /* IDESCSI_DEBUG_LOG */
538                 }
539         }
540         if (ireason.b.io) {
541                 clear_bit(PC_WRITING, &pc->flags);
542                 if (pc->sg)
543                         idescsi_input_buffers(drive, pc, bcount.all);
544                 else
545                         HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
546         } else {
547                 set_bit(PC_WRITING, &pc->flags);
548                 if (pc->sg)
549                         idescsi_output_buffers (drive, pc, bcount.all);
550                 else
551                         HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
552         }
553         /* Update the current position */
554         pc->actually_transferred += bcount.all;
555         pc->current_position += bcount.all;
556
557         /* And set the interrupt handler again */
558         ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
559         return ide_started;
560 }
561
562 static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
563 {
564         ide_hwif_t *hwif = drive->hwif;
565         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
566         idescsi_pc_t *pc = scsi->pc;
567         atapi_ireason_t ireason;
568         ide_startstop_t startstop;
569
570         if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
571                 printk(KERN_ERR "ide-scsi: Strange, packet command "
572                         "initiated yet DRQ isn't asserted\n");
573                 return startstop;
574         }
575         ireason.all     = HWIF(drive)->INB(IDE_IREASON_REG);
576         if (!ireason.b.cod || ireason.b.io) {
577                 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
578                                 "issuing a packet command\n");
579                 return ide_do_reset (drive);
580         }
581         if (HWGROUP(drive)->handler != NULL)
582                 BUG();
583         /* Set the interrupt routine */
584         ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
585         /* Send the actual packet */
586         drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
587         if (test_bit (PC_DMA_OK, &pc->flags)) {
588                 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
589                 hwif->dma_start(drive);
590         }
591         return ide_started;
592 }
593
594 static inline int idescsi_set_direction(idescsi_pc_t *pc)
595 {
596         switch (pc->c[0]) {
597                 case READ_6: case READ_10: case READ_12:
598                         clear_bit(PC_WRITING, &pc->flags);
599                         return 0;
600                 case WRITE_6: case WRITE_10: case WRITE_12:
601                         set_bit(PC_WRITING, &pc->flags);
602                         return 0;
603                 default:
604                         return 1;
605         }
606 }
607
608 static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
609 {
610         ide_hwif_t *hwif = drive->hwif;
611         struct scatterlist *sg, *scsi_sg;
612         int segments;
613
614         if (!pc->request_transfer || pc->request_transfer % 1024)
615                 return 1;
616
617         if (idescsi_set_direction(pc))
618                 return 1;
619
620         sg = hwif->sg_table;
621         scsi_sg = pc->scsi_cmd->request_buffer;
622         segments = pc->scsi_cmd->use_sg;
623
624         if (segments > hwif->sg_max_nents)
625                 return 1;
626
627         if (!segments) {
628                 hwif->sg_nents = 1;
629                 sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
630         } else {
631                 hwif->sg_nents = segments;
632                 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
633         }
634
635         return 0;
636 }
637
638 /*
639  *      Issue a packet command
640  */
641 static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
642 {
643         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
644         ide_hwif_t *hwif = drive->hwif;
645         atapi_feature_t feature;
646         atapi_bcount_t bcount;
647
648         scsi->pc=pc;                                                    /* Set the current packet command */
649         pc->actually_transferred=0;                                     /* We haven't transferred any data yet */
650         pc->current_position=pc->buffer;
651         bcount.all = min(pc->request_transfer, 63 * 1024);              /* Request to transfer the entire buffer at once */
652
653         feature.all = 0;
654         if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
655                 hwif->sg_mapped = 1;
656                 feature.b.dma = !hwif->dma_setup(drive);
657                 hwif->sg_mapped = 0;
658         }
659
660         SELECT_DRIVE(drive);
661         if (IDE_CONTROL_REG)
662                 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
663
664         HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
665         HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
666         HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
667
668         if (feature.b.dma)
669                 set_bit(PC_DMA_OK, &pc->flags);
670
671         if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
672                 if (HWGROUP(drive)->handler != NULL)
673                         BUG();
674                 ide_set_handler(drive, &idescsi_transfer_pc,
675                                 get_timeout(pc), idescsi_expiry);
676                 /* Issue the packet command */
677                 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
678                 return ide_started;
679         } else {
680                 /* Issue the packet command */
681                 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
682                 return idescsi_transfer_pc(drive);
683         }
684 }
685
686 /*
687  *      idescsi_do_request is our request handling function.
688  */
689 static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
690 {
691 #if IDESCSI_DEBUG_LOG
692         printk (KERN_INFO "rq_status: %d, dev: %s, cmd: %x, errors: %d\n",rq->rq_status, rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
693         printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
694 #endif /* IDESCSI_DEBUG_LOG */
695
696         if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) {
697                 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
698         }
699         blk_dump_rq_flags(rq, "ide-scsi: unsup command");
700         idescsi_end_request (drive, 0, 0);
701         return ide_stopped;
702 }
703
704 static void idescsi_add_settings(ide_drive_t *drive)
705 {
706         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
707
708 /*
709  *                      drive   setting name    read/write      ioctl   ioctl           data type       min     max     mul_factor      div_factor      data pointer            set function
710  */
711         ide_add_setting(drive,  "bios_cyl",     SETTING_RW,     -1,     -1,             TYPE_INT,       0,      1023,   1,              1,              &drive->bios_cyl,       NULL);
712         ide_add_setting(drive,  "bios_head",    SETTING_RW,     -1,     -1,             TYPE_BYTE,      0,      255,    1,              1,              &drive->bios_head,      NULL);
713         ide_add_setting(drive,  "bios_sect",    SETTING_RW,     -1,     -1,             TYPE_BYTE,      0,      63,     1,              1,              &drive->bios_sect,      NULL);
714         ide_add_setting(drive,  "transform",    SETTING_RW,     -1,     -1,             TYPE_INT,       0,      3,      1,              1,              &scsi->transform,       NULL);
715         ide_add_setting(drive,  "log",          SETTING_RW,     -1,     -1,             TYPE_INT,       0,      1,      1,              1,              &scsi->log,             NULL);
716 }
717
718 /*
719  *      Driver initialization.
720  */
721 static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
722 {
723         if (drive->id && (drive->id->config & 0x0060) == 0x20)
724                 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
725         set_bit(IDESCSI_TRANSFORM, &scsi->transform);
726         clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
727 #if IDESCSI_DEBUG_LOG
728         set_bit(IDESCSI_LOG_CMD, &scsi->log);
729 #endif /* IDESCSI_DEBUG_LOG */
730         idescsi_add_settings(drive);
731 }
732
733 static int ide_scsi_remove(struct device *dev)
734 {
735         ide_drive_t *drive = to_ide_device(dev);
736         struct Scsi_Host *scsihost = drive->driver_data;
737         struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
738         struct gendisk *g = scsi->disk;
739
740         ide_unregister_subdriver(drive, scsi->driver);
741
742         ide_unregister_region(g);
743
744         drive->driver_data = NULL;
745         g->private_data = NULL;
746         put_disk(g);
747
748         scsi_remove_host(scsihost);
749         ide_scsi_put(scsi);
750
751         return 0;
752 }
753
754 static int ide_scsi_probe(struct device *);
755
756 #ifdef CONFIG_PROC_FS
757 static ide_proc_entry_t idescsi_proc[] = {
758         { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
759         { NULL, 0, NULL, NULL }
760 };
761 #else
762 # define idescsi_proc   NULL
763 #endif
764
765 static ide_driver_t idescsi_driver = {
766         .owner                  = THIS_MODULE,
767         .gen_driver = {
768                 .name           = "ide-scsi",
769                 .bus            = &ide_bus_type,
770                 .probe          = ide_scsi_probe,
771                 .remove         = ide_scsi_remove,
772         },
773         .version                = IDESCSI_VERSION,
774         .media                  = ide_scsi,
775         .supports_dsc_overlap   = 0,
776         .proc                   = idescsi_proc,
777         .do_request             = idescsi_do_request,
778         .end_request            = idescsi_end_request,
779         .error                  = idescsi_atapi_error,
780         .abort                  = idescsi_atapi_abort,
781 };
782
783 static int idescsi_ide_open(struct inode *inode, struct file *filp)
784 {
785         struct gendisk *disk = inode->i_bdev->bd_disk;
786         struct ide_scsi_obj *scsi;
787         ide_drive_t *drive;
788
789         if (!(scsi = ide_scsi_get(disk)))
790                 return -ENXIO;
791
792         drive = scsi->drive;
793
794         drive->usage++;
795
796         return 0;
797 }
798
799 static int idescsi_ide_release(struct inode *inode, struct file *filp)
800 {
801         struct gendisk *disk = inode->i_bdev->bd_disk;
802         struct ide_scsi_obj *scsi = ide_scsi_g(disk);
803         ide_drive_t *drive = scsi->drive;
804
805         drive->usage--;
806
807         ide_scsi_put(scsi);
808
809         return 0;
810 }
811
812 static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
813                         unsigned int cmd, unsigned long arg)
814 {
815         struct block_device *bdev = inode->i_bdev;
816         struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
817         return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
818 }
819
820 static struct block_device_operations idescsi_ops = {
821         .owner          = THIS_MODULE,
822         .open           = idescsi_ide_open,
823         .release        = idescsi_ide_release,
824         .ioctl          = idescsi_ide_ioctl,
825 };
826
827 static int idescsi_slave_configure(struct scsi_device * sdp)
828 {
829         /* Configure detected device */
830         scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
831         return 0;
832 }
833
834 static const char *idescsi_info (struct Scsi_Host *host)
835 {
836         return "SCSI host adapter emulation for IDE ATAPI devices";
837 }
838
839 static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
840 {
841         idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
842
843         if (cmd == SG_SET_TRANSFORM) {
844                 if (arg)
845                         set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
846                 else
847                         clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
848                 return 0;
849         } else if (cmd == SG_GET_TRANSFORM)
850                 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
851         return -EINVAL;
852 }
853
854 static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
855 {
856         idescsi_scsi_t *scsi = drive_to_idescsi(drive);
857
858         /* this was a layering violation and we can't support it
859            anymore, sorry. */
860 #if 0
861         struct gendisk *disk = cmd->request->rq_disk;
862
863         if (disk) {
864                 struct Scsi_Device_Template **p = disk->private_data;
865                 if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
866                         return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
867         }
868 #endif
869         return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
870 }
871
872 static int idescsi_queue (struct scsi_cmnd *cmd,
873                 void (*done)(struct scsi_cmnd *))
874 {
875         struct Scsi_Host *host = cmd->device->host;
876         idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
877         ide_drive_t *drive = scsi->drive;
878         struct request *rq = NULL;
879         idescsi_pc_t *pc = NULL;
880
881         if (!drive) {
882                 printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->device->id);
883                 goto abort;
884         }
885         scsi = drive_to_idescsi(drive);
886         pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
887         rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
888         if (rq == NULL || pc == NULL) {
889                 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
890                 goto abort;
891         }
892
893         memset (pc->c, 0, 12);
894         pc->flags = 0;
895         pc->rq = rq;
896         memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
897         if (cmd->use_sg) {
898                 pc->buffer = NULL;
899                 pc->sg = cmd->request_buffer;
900         } else {
901                 pc->buffer = cmd->request_buffer;
902                 pc->sg = NULL;
903         }
904         pc->b_count = 0;
905         pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
906         pc->scsi_cmd = cmd;
907         pc->done = done;
908         pc->timeout = jiffies + cmd->timeout_per_command;
909
910         if (should_transform(drive, cmd))
911                 set_bit(PC_TRANSFORM, &pc->flags);
912         idescsi_transform_pc1 (drive, pc);
913
914         if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
915                 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
916                 hexdump(cmd->cmnd, cmd->cmd_len);
917                 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
918                         printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
919                         hexdump(pc->c, 12);
920                 }
921         }
922
923         ide_init_drive_cmd (rq);
924         rq->special = (char *) pc;
925         rq->flags = REQ_SPECIAL;
926         spin_unlock_irq(host->host_lock);
927         rq->rq_disk = scsi->disk;
928         (void) ide_do_drive_cmd (drive, rq, ide_end);
929         spin_lock_irq(host->host_lock);
930         return 0;
931 abort:
932         if (pc) kfree (pc);
933         if (rq) kfree (rq);
934         cmd->result = DID_ERROR << 16;
935         done(cmd);
936         return 0;
937 }
938
939 static int idescsi_eh_abort (struct scsi_cmnd *cmd)
940 {
941         idescsi_scsi_t *scsi  = scsihost_to_idescsi(cmd->device->host);
942         ide_drive_t    *drive = scsi->drive;
943         int             busy;
944         int             ret   = FAILED;
945
946         /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
947
948         if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
949                 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
950
951         if (!drive) {
952                 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
953                 WARN_ON(1);
954                 goto no_drive;
955         }
956
957         /* First give it some more time, how much is "right" is hard to say :-( */
958
959         busy = ide_wait_not_busy(HWIF(drive), 100);     /* FIXME - uses mdelay which causes latency? */
960         if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
961                 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
962
963         spin_lock_irq(&ide_lock);
964
965         /* If there is no pc running we're done (our interrupt took care of it) */
966         if (!scsi->pc) {
967                 ret = SUCCESS;
968                 goto ide_unlock;
969         }
970
971         /* It's somewhere in flight. Does ide subsystem agree? */
972         if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
973             elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
974                 /*
975                  * FIXME - not sure this condition can ever occur
976                  */
977                 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
978
979                 if (scsi->pc->rq->flags & REQ_SENSE)
980                         kfree(scsi->pc->buffer);
981                 kfree(scsi->pc->rq);
982                 kfree(scsi->pc);
983                 scsi->pc = NULL;
984
985                 ret = SUCCESS;
986         }
987
988 ide_unlock:
989         spin_unlock_irq(&ide_lock);
990 no_drive:
991         if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
992                 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
993
994         return ret;
995 }
996
997 static int idescsi_eh_reset (struct scsi_cmnd *cmd)
998 {
999         struct request *req;
1000         idescsi_scsi_t *scsi  = scsihost_to_idescsi(cmd->device->host);
1001         ide_drive_t    *drive = scsi->drive;
1002         int             ready = 0;
1003         int             ret   = SUCCESS;
1004
1005         /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
1006
1007         if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
1008                 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
1009
1010         if (!drive) {
1011                 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
1012                 WARN_ON(1);
1013                 return FAILED;
1014         }
1015
1016         spin_lock_irq(cmd->device->host->host_lock);
1017         spin_lock(&ide_lock);
1018
1019         if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
1020                 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
1021                 spin_unlock(&ide_lock);
1022                 spin_unlock_irq(cmd->device->host->host_lock);
1023                 return FAILED;
1024         }
1025
1026         /* kill current request */
1027         blkdev_dequeue_request(req);
1028         end_that_request_last(req);
1029         if (req->flags & REQ_SENSE)
1030                 kfree(scsi->pc->buffer);
1031         kfree(scsi->pc);
1032         scsi->pc = NULL;
1033         kfree(req);
1034
1035         /* now nuke the drive queue */
1036         while ((req = elv_next_request(drive->queue))) {
1037                 blkdev_dequeue_request(req);
1038                 end_that_request_last(req);
1039         }
1040
1041         HWGROUP(drive)->rq = NULL;
1042         HWGROUP(drive)->handler = NULL;
1043         HWGROUP(drive)->busy = 1;               /* will set this to zero when ide reset finished */
1044         spin_unlock(&ide_lock);
1045
1046         ide_do_reset(drive);
1047
1048         /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1049
1050         do {
1051                 spin_unlock_irq(cmd->device->host->host_lock);
1052                 msleep(50);
1053                 spin_lock_irq(cmd->device->host->host_lock);
1054         } while ( HWGROUP(drive)->handler );
1055
1056         ready = drive_is_ready(drive);
1057         HWGROUP(drive)->busy--;
1058         if (!ready) {
1059                 printk (KERN_ERR "ide-scsi: reset failed!\n");
1060                 ret = FAILED;
1061         }
1062
1063         spin_unlock_irq(cmd->device->host->host_lock);
1064         return ret;
1065 }
1066
1067 static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
1068                 sector_t capacity, int *parm)
1069 {
1070         idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
1071         ide_drive_t *drive = idescsi->drive;
1072
1073         if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1074                 parm[0] = drive->bios_head;
1075                 parm[1] = drive->bios_sect;
1076                 parm[2] = drive->bios_cyl;
1077         }
1078         return 0;
1079 }
1080
1081 static struct scsi_host_template idescsi_template = {
1082         .module                 = THIS_MODULE,
1083         .name                   = "idescsi",
1084         .info                   = idescsi_info,
1085         .slave_configure        = idescsi_slave_configure,
1086         .ioctl                  = idescsi_ioctl,
1087         .queuecommand           = idescsi_queue,
1088         .eh_abort_handler       = idescsi_eh_abort,
1089         .eh_host_reset_handler  = idescsi_eh_reset,
1090         .bios_param             = idescsi_bios,
1091         .can_queue              = 40,
1092         .this_id                = -1,
1093         .sg_tablesize           = 256,
1094         .cmd_per_lun            = 5,
1095         .max_sectors            = 128,
1096         .use_clustering         = DISABLE_CLUSTERING,
1097         .emulated               = 1,
1098         .proc_name              = "ide-scsi",
1099 };
1100
1101 static int ide_scsi_probe(struct device *dev)
1102 {
1103         ide_drive_t *drive = to_ide_device(dev);
1104         idescsi_scsi_t *idescsi;
1105         struct Scsi_Host *host;
1106         struct gendisk *g;
1107         static int warned;
1108         int err = -ENOMEM;
1109
1110         if (!warned && drive->media == ide_cdrom) {
1111                 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1112                 warned = 1;
1113         }
1114
1115         if (!strstr("ide-scsi", drive->driver_req) ||
1116             !drive->present ||
1117             drive->media == ide_disk ||
1118             !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
1119                 return -ENODEV;
1120
1121         g = alloc_disk(1 << PARTN_BITS);
1122         if (!g)
1123                 goto out_host_put;
1124
1125         ide_init_disk(g, drive);
1126
1127         host->max_id = 1;
1128
1129 #if IDESCSI_DEBUG_LOG
1130         if (drive->id->last_lun)
1131                 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1132 #endif
1133         if ((drive->id->last_lun & 0x7) != 7)
1134                 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1135         else
1136                 host->max_lun = 1;
1137
1138         drive->driver_data = host;
1139         idescsi = scsihost_to_idescsi(host);
1140         idescsi->drive = drive;
1141         idescsi->driver = &idescsi_driver;
1142         idescsi->host = host;
1143         idescsi->disk = g;
1144         g->private_data = &idescsi->driver;
1145         ide_register_subdriver(drive, &idescsi_driver);
1146         err = 0;
1147         idescsi_setup(drive, idescsi);
1148         g->fops = &idescsi_ops;
1149         ide_register_region(g);
1150         err = scsi_add_host(host, &drive->gendev);
1151         if (!err) {
1152                 scsi_scan_host(host);
1153                 return 0;
1154         }
1155         /* fall through on error */
1156         ide_unregister_region(g);
1157         ide_unregister_subdriver(drive, &idescsi_driver);
1158
1159         put_disk(g);
1160 out_host_put:
1161         scsi_host_put(host);
1162         return err;
1163 }
1164
1165 static int __init init_idescsi_module(void)
1166 {
1167         return driver_register(&idescsi_driver.gen_driver);
1168 }
1169
1170 static void __exit exit_idescsi_module(void)
1171 {
1172         driver_unregister(&idescsi_driver.gen_driver);
1173 }
1174
1175 module_init(init_idescsi_module);
1176 module_exit(exit_idescsi_module);
1177 MODULE_LICENSE("GPL");