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