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