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