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