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