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