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