blob: 3c3b3502c4d4137e108d1f415ce30d01f35b842c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
3 * Copyright (C) 2004-2005 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
Jeff Garzik df0ae242005-05-28 07:57:14 -040047#include <linux/delay.h>
Jes Sorensene723ccd2006-03-23 03:00:22 -080048#include <linux/mutex.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070049#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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
63typedef 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 */
Jens Axboec79d88b2007-10-17 13:16:35 +020072 unsigned int sg_cnt; /* Number of entries in sg */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int b_count; /* Bytes transferred from current entry */
74 struct scsi_cmnd *scsi_cmd; /* SCSI command */
75 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
76 unsigned long flags; /* Status/Action flags */
77 unsigned long timeout; /* Command timeout */
78} idescsi_pc_t;
79
80/*
81 * Packet command status bits.
82 */
83#define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
84#define PC_WRITING 1 /* Data direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define PC_TIMEDOUT 3 /* command timed out */
86#define PC_DMA_OK 4 /* Use DMA */
87
88/*
89 * SCSI command transformation layer
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
92
93/*
94 * Log flags
95 */
96#define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
97
98typedef struct ide_scsi_obj {
99 ide_drive_t *drive;
100 ide_driver_t *driver;
101 struct gendisk *disk;
102 struct Scsi_Host *host;
103
104 idescsi_pc_t *pc; /* Current packet command */
105 unsigned long flags; /* Status/Action flags */
106 unsigned long transform; /* SCSI cmd translation layer */
107 unsigned long log; /* log flags */
108} idescsi_scsi_t;
109
Jes Sorensene723ccd2006-03-23 03:00:22 -0800110static DEFINE_MUTEX(idescsi_ref_mutex);
Alan Coxaaeab802006-12-06 20:39:33 -0800111static int idescsi_nocd; /* Set by module param to skip cd */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#define ide_scsi_g(disk) \
114 container_of((disk)->private_data, struct ide_scsi_obj, driver)
115
116static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
117{
118 struct ide_scsi_obj *scsi = NULL;
119
Jes Sorensene723ccd2006-03-23 03:00:22 -0800120 mutex_lock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 scsi = ide_scsi_g(disk);
122 if (scsi)
123 scsi_host_get(scsi->host);
Jes Sorensene723ccd2006-03-23 03:00:22 -0800124 mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return scsi;
126}
127
128static void ide_scsi_put(struct ide_scsi_obj *scsi)
129{
Jes Sorensene723ccd2006-03-23 03:00:22 -0800130 mutex_lock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 scsi_host_put(scsi->host);
Jes Sorensene723ccd2006-03-23 03:00:22 -0800132 mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
136{
137 return (idescsi_scsi_t*) (&host[1]);
138}
139
140static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
141{
142 return scsihost_to_idescsi(ide_drive->driver_data);
143}
144
145/*
146 * Per ATAPI device status bits.
147 */
148#define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
149
150/*
151 * ide-scsi requests.
152 */
153#define IDESCSI_PC_RQ 90
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * PIO data transfer routines using the scatter gather table.
157 */
158static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
159{
160 int count;
161 char *buf;
162
163 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 count = min(pc->sg->length - pc->b_count, bcount);
Jens Axboe45711f12007-10-22 21:19:53 +0200165 if (PageHighMem(sg_page(pc->sg))) {
Andrew Mortona717f772005-10-31 14:08:53 -0800166 unsigned long flags;
167
168 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +0200169 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
Andrew Mortona717f772005-10-31 14:08:53 -0800170 pc->sg->offset;
171 drive->hwif->atapi_input_bytes(drive,
172 buf + pc->b_count, count);
173 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
174 local_irq_restore(flags);
175 } else {
Jens Axboe45711f12007-10-22 21:19:53 +0200176 buf = sg_virt(pc->sg);
Andrew Mortona717f772005-10-31 14:08:53 -0800177 drive->hwif->atapi_input_bytes(drive,
178 buf + pc->b_count, count);
179 }
180 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (pc->b_count == pc->sg->length) {
Jens Axboec79d88b2007-10-17 13:16:35 +0200182 if (!--pc->sg_cnt)
Jens Axboed274a982007-10-16 11:20:52 +0200183 break;
184 pc->sg = sg_next(pc->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pc->b_count = 0;
186 }
187 }
Jens Axboed274a982007-10-16 11:20:52 +0200188
189 if (bcount) {
190 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200191 ide_atapi_discard_data(drive, bcount);
Jens Axboed274a982007-10-16 11:20:52 +0200192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
196{
197 int count;
198 char *buf;
199
200 while (bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 count = min(pc->sg->length - pc->b_count, bcount);
Jens Axboe45711f12007-10-22 21:19:53 +0200202 if (PageHighMem(sg_page(pc->sg))) {
Andrew Mortona717f772005-10-31 14:08:53 -0800203 unsigned long flags;
204
205 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +0200206 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
Andrew Mortona717f772005-10-31 14:08:53 -0800207 pc->sg->offset;
208 drive->hwif->atapi_output_bytes(drive,
209 buf + pc->b_count, count);
210 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
211 local_irq_restore(flags);
212 } else {
Jens Axboe45711f12007-10-22 21:19:53 +0200213 buf = sg_virt(pc->sg);
Andrew Mortona717f772005-10-31 14:08:53 -0800214 drive->hwif->atapi_output_bytes(drive,
215 buf + pc->b_count, count);
216 }
217 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (pc->b_count == pc->sg->length) {
Jens Axboec79d88b2007-10-17 13:16:35 +0200219 if (!--pc->sg_cnt)
Jens Axboed274a982007-10-16 11:20:52 +0200220 break;
221 pc->sg = sg_next(pc->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 pc->b_count = 0;
223 }
224 }
Jens Axboed274a982007-10-16 11:20:52 +0200225
226 if (bcount) {
227 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200228 ide_atapi_write_zeros(drive, bcount);
Jens Axboed274a982007-10-16 11:20:52 +0200229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100232static void ide_scsi_hex_dump(u8 *data, int len)
233{
234 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
238{
239 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
240 idescsi_pc_t *pc;
241 struct request *rq;
242 u8 *buf;
243
244 /* stuff a sense request in front of our current request */
Mariusz Kozlowski41ead3c2007-08-01 23:46:45 +0200245 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
246 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
247 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
248 if (!pc || !rq || !buf) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800249 kfree(buf);
250 kfree(rq);
251 kfree(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -ENOMEM;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ide_init_drive_cmd(rq);
255 rq->special = (char *) pc;
256 pc->rq = rq;
257 pc->buffer = buf;
258 pc->c[0] = REQUEST_SENSE;
259 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200260 rq->cmd_type = REQ_TYPE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 pc->timeout = jiffies + WAIT_READY;
262 /* NOTE! Save the failed packet command in "rq->buffer" */
263 rq->buffer = (void *) failed_command->special;
264 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
265 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
266 printk ("ide-scsi: %s: queue cmd = ", drive->name);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100267 ide_scsi_hex_dump(pc->c, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 rq->rq_disk = scsi->disk;
270 return ide_do_drive_cmd(drive, rq, ide_preempt);
271}
272
273static int idescsi_end_request(ide_drive_t *, int, int);
274
275static ide_startstop_t
276idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
277{
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100278 if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* force an abort */
280 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
281
282 rq->errors++;
283
284 idescsi_end_request(drive, 0, 0);
285
286 return ide_stopped;
287}
288
289static ide_startstop_t
290idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
291{
292#if IDESCSI_DEBUG_LOG
293 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
294 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
295#endif
296 rq->errors |= ERROR_MAX;
297
298 idescsi_end_request(drive, 0, 0);
299
300 return ide_stopped;
301}
302
303static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
304{
305 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
306 struct request *rq = HWGROUP(drive)->rq;
307 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
308 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
309 struct Scsi_Host *host;
Willem Riede32565342005-10-31 00:03:49 +0000310 int errors = rq->errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned long flags;
312
Jens Axboe4aff5e22006-08-10 08:44:47 +0200313 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ide_end_request(drive, uptodate, nrsecs);
315 return 0;
316 }
317 ide_end_drive_cmd (drive, 0, 0);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200318 if (blk_sense_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
320 if (log) {
321 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100322 ide_scsi_hex_dump(pc->buffer, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
325 kfree(pc->buffer);
326 kfree(pc);
327 kfree(rq);
328 pc = opc;
329 rq = pc->rq;
330 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
331 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
332 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
333 if (log)
334 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
335 drive->name, pc->scsi_cmd->serial_number);
336 pc->scsi_cmd->result = DID_TIME_OUT << 16;
Willem Riede32565342005-10-31 00:03:49 +0000337 } else if (errors >= ERROR_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 pc->scsi_cmd->result = DID_ERROR << 16;
339 if (log)
340 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
Willem Riede32565342005-10-31 00:03:49 +0000341 } else if (errors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (log)
343 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
344 if (!idescsi_check_condition(drive, rq))
345 /* we started a request sense, so we'll be back, exit for now */
346 return 0;
347 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
348 } else {
349 pc->scsi_cmd->result = DID_OK << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 host = pc->scsi_cmd->device->host;
352 spin_lock_irqsave(host->host_lock, flags);
353 pc->done(pc->scsi_cmd);
354 spin_unlock_irqrestore(host->host_lock, flags);
355 kfree(pc);
356 kfree(rq);
357 scsi->pc = NULL;
358 return 0;
359}
360
361static inline unsigned long get_timeout(idescsi_pc_t *pc)
362{
363 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
364}
365
366static int idescsi_expiry(ide_drive_t *drive)
367{
Bartlomiej Zolnierkiewiczd1be0a82007-06-16 02:24:44 +0200368 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 idescsi_pc_t *pc = scsi->pc;
370
371#if IDESCSI_DEBUG_LOG
372 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
373#endif
374 set_bit(PC_TIMEDOUT, &pc->flags);
375
376 return 0; /* we do not want the ide subsystem to retry */
377}
378
379/*
380 * Our interrupt handler.
381 */
382static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
383{
384 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100385 ide_hwif_t *hwif = drive->hwif;
386 idescsi_pc_t *pc = scsi->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct request *rq = pc->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100389 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100390 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392#if IDESCSI_DEBUG_LOG
393 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
394#endif /* IDESCSI_DEBUG_LOG */
395
396 if (test_bit(PC_TIMEDOUT, &pc->flags)){
397#if IDESCSI_DEBUG_LOG
398 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
399 pc->scsi_cmd->serial_number, jiffies);
400#endif
401 /* end this request now - scsi should retry it*/
402 idescsi_end_request (drive, 1, 0);
403 return ide_stopped;
404 }
405 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
406#if IDESCSI_DEBUG_LOG
407 printk ("ide-scsi: %s: DMA complete\n", drive->name);
408#endif /* IDESCSI_DEBUG_LOG */
409 pc->actually_transferred=pc->request_transfer;
410 (void) HWIF(drive)->ide_dma_end(drive);
411 }
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Clear the interrupt */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100414 stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100416 if ((stat & DRQ_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* No more interrupts */
418 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
419 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
Ingo Molnar36e8e572006-08-27 01:23:56 -0700420 local_irq_enable_in_hardirq();
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100421 if (stat & ERR_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 rq->errors++;
423 idescsi_end_request (drive, 1, 0);
424 return ide_stopped;
425 }
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100426 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
427 hwif->INB(IDE_BCOUNTL_REG);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100428 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100430 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
432 return ide_do_reset (drive);
433 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100434 if (ireason & IO) {
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100435 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (temp > pc->request_transfer) {
437 if (temp > pc->buffer_size) {
438 printk(KERN_ERR "ide-scsi: The scsi wants to "
439 "send us more data than expected "
440 "- discarding data\n");
441 temp = pc->buffer_size - pc->actually_transferred;
442 if (temp) {
443 clear_bit(PC_WRITING, &pc->flags);
444 if (pc->sg)
445 idescsi_input_buffers(drive, pc, temp);
446 else
447 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100448 printk(KERN_ERR "ide-scsi: transferred"
449 " %d of %d bytes\n",
450 temp, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 pc->actually_transferred += temp;
453 pc->current_position += temp;
Bartlomiej Zolnierkiewicz7616c0a2008-04-18 00:46:26 +0200454 ide_atapi_discard_data(drive, bcount - temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
456 return ide_started;
457 }
458#if IDESCSI_DEBUG_LOG
459 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
460#endif /* IDESCSI_DEBUG_LOG */
461 }
462 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100463 if (ireason & IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 clear_bit(PC_WRITING, &pc->flags);
465 if (pc->sg)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100466 idescsi_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100468 hwif->atapi_input_bytes(drive, pc->current_position,
469 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 set_bit(PC_WRITING, &pc->flags);
472 if (pc->sg)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100473 idescsi_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100475 hwif->atapi_output_bytes(drive, pc->current_position,
476 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100479 pc->actually_transferred += bcount;
480 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* And set the interrupt handler again */
483 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
484 return ide_started;
485}
486
487static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
488{
489 ide_hwif_t *hwif = drive->hwif;
490 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
491 idescsi_pc_t *pc = scsi->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100493 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
496 printk(KERN_ERR "ide-scsi: Strange, packet command "
497 "initiated yet DRQ isn't asserted\n");
498 return startstop;
499 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100500 ireason = hwif->INB(IDE_IREASON_REG);
501 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
503 "issuing a packet command\n");
504 return ide_do_reset (drive);
505 }
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700506 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Set the interrupt routine */
508 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
509 /* Send the actual packet */
510 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
511 if (test_bit (PC_DMA_OK, &pc->flags)) {
512 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
513 hwif->dma_start(drive);
514 }
515 return ide_started;
516}
517
518static inline int idescsi_set_direction(idescsi_pc_t *pc)
519{
520 switch (pc->c[0]) {
521 case READ_6: case READ_10: case READ_12:
522 clear_bit(PC_WRITING, &pc->flags);
523 return 0;
524 case WRITE_6: case WRITE_10: case WRITE_12:
525 set_bit(PC_WRITING, &pc->flags);
526 return 0;
527 default:
528 return 1;
529 }
530}
531
532static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
533{
534 ide_hwif_t *hwif = drive->hwif;
535 struct scatterlist *sg, *scsi_sg;
536 int segments;
537
538 if (!pc->request_transfer || pc->request_transfer % 1024)
539 return 1;
540
541 if (idescsi_set_direction(pc))
542 return 1;
543
544 sg = hwif->sg_table;
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200545 scsi_sg = scsi_sglist(pc->scsi_cmd);
546 segments = scsi_sg_count(pc->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (segments > hwif->sg_max_nents)
549 return 1;
550
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200551 hwif->sg_nents = segments;
552 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return 0;
555}
556
557/*
558 * Issue a packet command
559 */
560static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
561{
562 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
563 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100564 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100565 u8 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 scsi->pc=pc; /* Set the current packet command */
568 pc->actually_transferred=0; /* We haven't transferred any data yet */
569 pc->current_position=pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100570 /* Request to transfer the entire buffer at once */
571 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
574 hwif->sg_mapped = 1;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100575 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 hwif->sg_mapped = 0;
577 }
578
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100579 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK, bcount, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100581 if (dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 set_bit(PC_DMA_OK, &pc->flags);
583
584 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
Bartlomiej Zolnierkiewicz2d6027f2008-02-02 19:56:44 +0100585 ide_execute_command(drive, WIN_PACKETCMD, &idescsi_transfer_pc,
586 get_timeout(pc), idescsi_expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return ide_started;
588 } else {
589 /* Issue the packet command */
590 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
591 return idescsi_transfer_pc(drive);
592 }
593}
594
595/*
596 * idescsi_do_request is our request handling function.
597 */
598static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
599{
600#if IDESCSI_DEBUG_LOG
Jens Axboecdd60262006-07-28 09:32:07 +0200601 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
603#endif /* IDESCSI_DEBUG_LOG */
604
Jens Axboe4aff5e22006-08-10 08:44:47 +0200605 if (blk_sense_request(rq) || blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
607 }
608 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
609 idescsi_end_request (drive, 0, 0);
610 return ide_stopped;
611}
612
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200613#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void idescsi_add_settings(ide_drive_t *drive)
615{
616 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
617
618/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200619 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200621 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
622 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
623 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
624 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
625 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200627#else
628static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/*
632 * Driver initialization.
633 */
634static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
635{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (drive->id && (drive->id->config & 0x0060) == 0x20)
637 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
639#if IDESCSI_DEBUG_LOG
640 set_bit(IDESCSI_LOG_CMD, &scsi->log);
641#endif /* IDESCSI_DEBUG_LOG */
642 idescsi_add_settings(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800645static void ide_scsi_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct Scsi_Host *scsihost = drive->driver_data;
648 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
649 struct gendisk *g = scsi->disk;
650
Matthew Wilcox6fdea8d2007-08-15 12:57:01 -0600651 scsi_remove_host(scsihost);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200652 ide_proc_unregister_driver(drive, scsi->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 ide_unregister_region(g);
655
656 drive->driver_data = NULL;
657 g->private_data = NULL;
658 put_disk(g);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ide_scsi_put(scsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800663static int ide_scsi_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200665#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static ide_proc_entry_t idescsi_proc[] = {
667 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
668 { NULL, 0, NULL, NULL }
669};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static ide_driver_t idescsi_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200673 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +0100674 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200675 .name = "ide-scsi",
676 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200677 },
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800678 .probe = ide_scsi_probe,
679 .remove = ide_scsi_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 .version = IDESCSI_VERSION,
681 .media = ide_scsi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .do_request = idescsi_do_request,
684 .end_request = idescsi_end_request,
685 .error = idescsi_atapi_error,
686 .abort = idescsi_atapi_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200687#ifdef CONFIG_IDE_PROC_FS
688 .proc = idescsi_proc,
689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690};
691
692static int idescsi_ide_open(struct inode *inode, struct file *filp)
693{
694 struct gendisk *disk = inode->i_bdev->bd_disk;
695 struct ide_scsi_obj *scsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (!(scsi = ide_scsi_get(disk)))
698 return -ENXIO;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
703static int idescsi_ide_release(struct inode *inode, struct file *filp)
704{
705 struct gendisk *disk = inode->i_bdev->bd_disk;
706 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 ide_scsi_put(scsi);
709
710 return 0;
711}
712
713static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
714 unsigned int cmd, unsigned long arg)
715{
716 struct block_device *bdev = inode->i_bdev;
717 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
718 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
719}
720
721static struct block_device_operations idescsi_ops = {
722 .owner = THIS_MODULE,
723 .open = idescsi_ide_open,
724 .release = idescsi_ide_release,
725 .ioctl = idescsi_ide_ioctl,
726};
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static int idescsi_slave_configure(struct scsi_device * sdp)
729{
730 /* Configure detected device */
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200731 sdp->use_10_for_rw = 1;
732 sdp->use_10_for_ms = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
734 return 0;
735}
736
737static const char *idescsi_info (struct Scsi_Host *host)
738{
739 return "SCSI host adapter emulation for IDE ATAPI devices";
740}
741
742static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
743{
744 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
745
746 if (cmd == SG_SET_TRANSFORM) {
747 if (arg)
748 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
749 else
750 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
751 return 0;
752 } else if (cmd == SG_GET_TRANSFORM)
753 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
754 return -EINVAL;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static int idescsi_queue (struct scsi_cmnd *cmd,
758 void (*done)(struct scsi_cmnd *))
759{
760 struct Scsi_Host *host = cmd->device->host;
761 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
762 ide_drive_t *drive = scsi->drive;
763 struct request *rq = NULL;
764 idescsi_pc_t *pc = NULL;
765
766 if (!drive) {
Jeff Garzik017560f2005-10-24 18:04:36 -0400767 scmd_printk (KERN_ERR, cmd, "drive not present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto abort;
769 }
770 scsi = drive_to_idescsi(drive);
771 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
772 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
773 if (rq == NULL || pc == NULL) {
774 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
775 goto abort;
776 }
777
778 memset (pc->c, 0, 12);
779 pc->flags = 0;
780 pc->rq = rq;
781 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200782 pc->buffer = NULL;
783 pc->sg = scsi_sglist(cmd);
Jens Axboec79d88b2007-10-17 13:16:35 +0200784 pc->sg_cnt = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 pc->b_count = 0;
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200786 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 pc->scsi_cmd = cmd;
788 pc->done = done;
789 pc->timeout = jiffies + cmd->timeout_per_command;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
792 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100793 ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
795 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100796 ide_scsi_hex_dump(pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
799
800 ide_init_drive_cmd (rq);
801 rq->special = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200802 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_unlock_irq(host->host_lock);
804 rq->rq_disk = scsi->disk;
805 (void) ide_do_drive_cmd (drive, rq, ide_end);
806 spin_lock_irq(host->host_lock);
807 return 0;
808abort:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800809 kfree (pc);
810 kfree (rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 cmd->result = DID_ERROR << 16;
812 done(cmd);
813 return 0;
814}
815
816static int idescsi_eh_abort (struct scsi_cmnd *cmd)
817{
818 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
819 ide_drive_t *drive = scsi->drive;
820 int busy;
821 int ret = FAILED;
822
823 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
824
825 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
826 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
827
828 if (!drive) {
829 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
830 WARN_ON(1);
831 goto no_drive;
832 }
833
834 /* First give it some more time, how much is "right" is hard to say :-( */
835
836 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
837 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
838 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
839
840 spin_lock_irq(&ide_lock);
841
842 /* If there is no pc running we're done (our interrupt took care of it) */
843 if (!scsi->pc) {
844 ret = SUCCESS;
845 goto ide_unlock;
846 }
847
848 /* It's somewhere in flight. Does ide subsystem agree? */
849 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
850 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
851 /*
852 * FIXME - not sure this condition can ever occur
853 */
854 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
855
Jens Axboe4aff5e22006-08-10 08:44:47 +0200856 if (blk_sense_request(scsi->pc->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 kfree(scsi->pc->buffer);
858 kfree(scsi->pc->rq);
859 kfree(scsi->pc);
860 scsi->pc = NULL;
861
862 ret = SUCCESS;
863 }
864
865ide_unlock:
866 spin_unlock_irq(&ide_lock);
867no_drive:
868 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
869 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
870
871 return ret;
872}
873
874static int idescsi_eh_reset (struct scsi_cmnd *cmd)
875{
876 struct request *req;
877 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
878 ide_drive_t *drive = scsi->drive;
879 int ready = 0;
880 int ret = SUCCESS;
881
882 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
883
884 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
885 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
886
887 if (!drive) {
888 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
889 WARN_ON(1);
890 return FAILED;
891 }
892
Jeff Garzik df0ae242005-05-28 07:57:14 -0400893 spin_lock_irq(cmd->device->host->host_lock);
894 spin_lock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
897 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
898 spin_unlock(&ide_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -0400899 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return FAILED;
901 }
902
903 /* kill current request */
Kiyoshi Ueda5a330e32007-12-11 17:49:29 -0500904 if (__blk_end_request(req, -EIO, 0))
905 BUG();
Jens Axboe4aff5e22006-08-10 08:44:47 +0200906 if (blk_sense_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 kfree(scsi->pc->buffer);
908 kfree(scsi->pc);
909 scsi->pc = NULL;
910 kfree(req);
911
912 /* now nuke the drive queue */
913 while ((req = elv_next_request(drive->queue))) {
Kiyoshi Ueda5a330e32007-12-11 17:49:29 -0500914 if (__blk_end_request(req, -EIO, 0))
915 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 HWGROUP(drive)->rq = NULL;
919 HWGROUP(drive)->handler = NULL;
920 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
Jeff Garzik df0ae242005-05-28 07:57:14 -0400921 spin_unlock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 ide_do_reset(drive);
924
925 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
926
927 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 spin_unlock_irq(cmd->device->host->host_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -0400929 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 spin_lock_irq(cmd->device->host->host_lock);
931 } while ( HWGROUP(drive)->handler );
932
933 ready = drive_is_ready(drive);
934 HWGROUP(drive)->busy--;
935 if (!ready) {
936 printk (KERN_ERR "ide-scsi: reset failed!\n");
937 ret = FAILED;
938 }
939
Jeff Garzik df0ae242005-05-28 07:57:14 -0400940 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return ret;
942}
943
944static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
945 sector_t capacity, int *parm)
946{
947 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
948 ide_drive_t *drive = idescsi->drive;
949
950 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
951 parm[0] = drive->bios_head;
952 parm[1] = drive->bios_sect;
953 parm[2] = drive->bios_cyl;
954 }
955 return 0;
956}
957
958static struct scsi_host_template idescsi_template = {
959 .module = THIS_MODULE,
960 .name = "idescsi",
961 .info = idescsi_info,
962 .slave_configure = idescsi_slave_configure,
963 .ioctl = idescsi_ioctl,
964 .queuecommand = idescsi_queue,
965 .eh_abort_handler = idescsi_eh_abort,
966 .eh_host_reset_handler = idescsi_eh_reset,
967 .bios_param = idescsi_bios,
968 .can_queue = 40,
969 .this_id = -1,
970 .sg_tablesize = 256,
971 .cmd_per_lun = 5,
972 .max_sectors = 128,
973 .use_clustering = DISABLE_CLUSTERING,
974 .emulated = 1,
975 .proc_name = "ide-scsi",
976};
977
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800978static int ide_scsi_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 idescsi_scsi_t *idescsi;
981 struct Scsi_Host *host;
982 struct gendisk *g;
983 static int warned;
984 int err = -ENOMEM;
985
986 if (!warned && drive->media == ide_cdrom) {
987 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
988 warned = 1;
989 }
990
Alan Coxaaeab802006-12-06 20:39:33 -0800991 if (idescsi_nocd && drive->media == ide_cdrom)
992 return -ENODEV;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!strstr("ide-scsi", drive->driver_req) ||
995 !drive->present ||
996 drive->media == ide_disk ||
997 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200998 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 g = alloc_disk(1 << PARTN_BITS);
1001 if (!g)
1002 goto out_host_put;
1003
1004 ide_init_disk(g, drive);
1005
1006 host->max_id = 1;
1007
1008#if IDESCSI_DEBUG_LOG
1009 if (drive->id->last_lun)
1010 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1011#endif
1012 if ((drive->id->last_lun & 0x7) != 7)
1013 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1014 else
1015 host->max_lun = 1;
1016
1017 drive->driver_data = host;
1018 idescsi = scsihost_to_idescsi(host);
1019 idescsi->drive = drive;
1020 idescsi->driver = &idescsi_driver;
1021 idescsi->host = host;
1022 idescsi->disk = g;
1023 g->private_data = &idescsi->driver;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001024 ide_proc_register_driver(drive, &idescsi_driver);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001025 err = 0;
1026 idescsi_setup(drive, idescsi);
1027 g->fops = &idescsi_ops;
1028 ide_register_region(g);
1029 err = scsi_add_host(host, &drive->gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!err) {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001031 scsi_scan_host(host);
1032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001034 /* fall through on error */
1035 ide_unregister_region(g);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001036 ide_proc_unregister_driver(drive, &idescsi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 put_disk(g);
1039out_host_put:
1040 scsi_host_put(host);
1041 return err;
1042}
1043
1044static int __init init_idescsi_module(void)
1045{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001046 return driver_register(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049static void __exit exit_idescsi_module(void)
1050{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001051 driver_unregister(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Alan Coxaaeab802006-12-06 20:39:33 -08001054module_param(idescsi_nocd, int, 0600);
1055MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056module_init(init_idescsi_module);
1057module_exit(exit_idescsi_module);
1058MODULE_LICENSE("GPL");