blob: ab7e8642cb886cedeeaa63e5aa99be95284fd84c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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>
Jeff Garzik df0ae242005-05-28 07:57:14 -040048#include <linux/delay.h>
Jes Sorensene723ccd2006-03-23 03:00:22 -080049#include <linux/mutex.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070050#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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
64typedef 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 */
Jens Axboec79d88b2007-10-17 13:16:35 +020073 unsigned int sg_cnt; /* Number of entries in sg */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define PC_TIMEDOUT 3 /* command timed out */
87#define PC_DMA_OK 4 /* Use DMA */
88
89/*
90 * SCSI command transformation layer
91 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
93
94/*
95 * Log flags
96 */
97#define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
98
99typedef 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
Jes Sorensene723ccd2006-03-23 03:00:22 -0800111static DEFINE_MUTEX(idescsi_ref_mutex);
Alan Coxaaeab802006-12-06 20:39:33 -0800112static int idescsi_nocd; /* Set by module param to skip cd */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#define ide_scsi_g(disk) \
115 container_of((disk)->private_data, struct ide_scsi_obj, driver)
116
117static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
118{
119 struct ide_scsi_obj *scsi = NULL;
120
Jes Sorensene723ccd2006-03-23 03:00:22 -0800121 mutex_lock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 scsi = ide_scsi_g(disk);
123 if (scsi)
124 scsi_host_get(scsi->host);
Jes Sorensene723ccd2006-03-23 03:00:22 -0800125 mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return scsi;
127}
128
129static void ide_scsi_put(struct ide_scsi_obj *scsi)
130{
Jes Sorensene723ccd2006-03-23 03:00:22 -0800131 mutex_lock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 scsi_host_put(scsi->host);
Jes Sorensene723ccd2006-03-23 03:00:22 -0800133 mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
137{
138 return (idescsi_scsi_t*) (&host[1]);
139}
140
141static 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
156static 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
162static 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 */
171static 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 count = min(pc->sg->length - pc->b_count, bcount);
Jens Axboe45711f12007-10-22 21:19:53 +0200178 if (PageHighMem(sg_page(pc->sg))) {
Andrew Mortona717f772005-10-31 14:08:53 -0800179 unsigned long flags;
180
181 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +0200182 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
Andrew Mortona717f772005-10-31 14:08:53 -0800183 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 {
Jens Axboe45711f12007-10-22 21:19:53 +0200189 buf = sg_virt(pc->sg);
Andrew Mortona717f772005-10-31 14:08:53 -0800190 drive->hwif->atapi_input_bytes(drive,
191 buf + pc->b_count, count);
192 }
193 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (pc->b_count == pc->sg->length) {
Jens Axboec79d88b2007-10-17 13:16:35 +0200195 if (!--pc->sg_cnt)
Jens Axboed274a982007-10-16 11:20:52 +0200196 break;
197 pc->sg = sg_next(pc->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 pc->b_count = 0;
199 }
200 }
Jens Axboed274a982007-10-16 11:20:52 +0200201
202 if (bcount) {
203 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
204 idescsi_discard_data (drive, bcount);
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 count = min(pc->sg->length - pc->b_count, bcount);
Jens Axboe45711f12007-10-22 21:19:53 +0200215 if (PageHighMem(sg_page(pc->sg))) {
Andrew Mortona717f772005-10-31 14:08:53 -0800216 unsigned long flags;
217
218 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +0200219 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
Andrew Mortona717f772005-10-31 14:08:53 -0800220 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 {
Jens Axboe45711f12007-10-22 21:19:53 +0200226 buf = sg_virt(pc->sg);
Andrew Mortona717f772005-10-31 14:08:53 -0800227 drive->hwif->atapi_output_bytes(drive,
228 buf + pc->b_count, count);
229 }
230 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (pc->b_count == pc->sg->length) {
Jens Axboec79d88b2007-10-17 13:16:35 +0200232 if (!--pc->sg_cnt)
Jens Axboed274a982007-10-16 11:20:52 +0200233 break;
234 pc->sg = sg_next(pc->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 pc->b_count = 0;
236 }
237 }
Jens Axboed274a982007-10-16 11:20:52 +0200238
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100245static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static 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 */
Mariusz Kozlowski41ead3c2007-08-01 23:46:45 +0200258 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) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800262 kfree(buf);
263 kfree(rq);
264 kfree(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -ENOMEM;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 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;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200273 rq->cmd_type = REQ_TYPE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 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);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100280 ide_scsi_hex_dump(pc->c, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 rq->rq_disk = scsi->disk;
283 return ide_do_drive_cmd(drive, rq, ide_preempt);
284}
285
286static int idescsi_end_request(ide_drive_t *, int, int);
287
288static ide_startstop_t
289idescsi_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
302static ide_startstop_t
303idescsi_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
316static 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;
Willem Riede32565342005-10-31 00:03:49 +0000323 int errors = rq->errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long flags;
325
Jens Axboe4aff5e22006-08-10 08:44:47 +0200326 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ide_end_request(drive, uptodate, nrsecs);
328 return 0;
329 }
330 ide_end_drive_cmd (drive, 0, 0);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200331 if (blk_sense_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100335 ide_scsi_hex_dump(pc->buffer, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
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;
Willem Riede32565342005-10-31 00:03:49 +0000350 } else if (errors >= ERROR_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 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);
Willem Riede32565342005-10-31 00:03:49 +0000354 } else if (errors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
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
374static inline unsigned long get_timeout(idescsi_pc_t *pc)
375{
376 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
377}
378
379static int idescsi_expiry(ide_drive_t *drive)
380{
Bartlomiej Zolnierkiewiczd1be0a82007-06-16 02:24:44 +0200381 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 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 */
395static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
396{
397 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100398 ide_hwif_t *hwif = drive->hwif;
399 idescsi_pc_t *pc = scsi->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct request *rq = pc->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned int temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100402 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100403 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405#if IDESCSI_DEBUG_LOG
406 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
407#endif /* IDESCSI_DEBUG_LOG */
408
409 if (test_bit(PC_TIMEDOUT, &pc->flags)){
410#if IDESCSI_DEBUG_LOG
411 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
412 pc->scsi_cmd->serial_number, jiffies);
413#endif
414 /* end this request now - scsi should retry it*/
415 idescsi_end_request (drive, 1, 0);
416 return ide_stopped;
417 }
418 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
419#if IDESCSI_DEBUG_LOG
420 printk ("ide-scsi: %s: DMA complete\n", drive->name);
421#endif /* IDESCSI_DEBUG_LOG */
422 pc->actually_transferred=pc->request_transfer;
423 (void) HWIF(drive)->ide_dma_end(drive);
424 }
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100427 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100429 if ((stat & DRQ_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* No more interrupts */
431 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
432 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
Ingo Molnar36e8e572006-08-27 01:23:56 -0700433 local_irq_enable_in_hardirq();
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +0100434 if (stat & ERR_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 rq->errors++;
436 idescsi_end_request (drive, 1, 0);
437 return ide_stopped;
438 }
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100439 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
440 hwif->INB(IDE_BCOUNTL_REG);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100441 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100443 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
445 return ide_do_reset (drive);
446 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100447 if (ireason & IO) {
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100448 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (temp > pc->request_transfer) {
450 if (temp > pc->buffer_size) {
451 printk(KERN_ERR "ide-scsi: The scsi wants to "
452 "send us more data than expected "
453 "- discarding data\n");
454 temp = pc->buffer_size - pc->actually_transferred;
455 if (temp) {
456 clear_bit(PC_WRITING, &pc->flags);
457 if (pc->sg)
458 idescsi_input_buffers(drive, pc, temp);
459 else
460 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100461 printk(KERN_ERR "ide-scsi: transferred"
462 " %d of %d bytes\n",
463 temp, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 pc->actually_transferred += temp;
466 pc->current_position += temp;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100467 idescsi_discard_data(drive, bcount - temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
469 return ide_started;
470 }
471#if IDESCSI_DEBUG_LOG
472 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
473#endif /* IDESCSI_DEBUG_LOG */
474 }
475 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100476 if (ireason & IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 clear_bit(PC_WRITING, &pc->flags);
478 if (pc->sg)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100479 idescsi_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100481 hwif->atapi_input_bytes(drive, pc->current_position,
482 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } else {
484 set_bit(PC_WRITING, &pc->flags);
485 if (pc->sg)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100486 idescsi_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 else
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100488 hwif->atapi_output_bytes(drive, pc->current_position,
489 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100492 pc->actually_transferred += bcount;
493 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* And set the interrupt handler again */
496 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
497 return ide_started;
498}
499
500static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
501{
502 ide_hwif_t *hwif = drive->hwif;
503 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
504 idescsi_pc_t *pc = scsi->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100506 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
509 printk(KERN_ERR "ide-scsi: Strange, packet command "
510 "initiated yet DRQ isn't asserted\n");
511 return startstop;
512 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +0100513 ireason = hwif->INB(IDE_IREASON_REG);
514 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
516 "issuing a packet command\n");
517 return ide_do_reset (drive);
518 }
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700519 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Set the interrupt routine */
521 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
522 /* Send the actual packet */
523 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
524 if (test_bit (PC_DMA_OK, &pc->flags)) {
525 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
526 hwif->dma_start(drive);
527 }
528 return ide_started;
529}
530
531static inline int idescsi_set_direction(idescsi_pc_t *pc)
532{
533 switch (pc->c[0]) {
534 case READ_6: case READ_10: case READ_12:
535 clear_bit(PC_WRITING, &pc->flags);
536 return 0;
537 case WRITE_6: case WRITE_10: case WRITE_12:
538 set_bit(PC_WRITING, &pc->flags);
539 return 0;
540 default:
541 return 1;
542 }
543}
544
545static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
546{
547 ide_hwif_t *hwif = drive->hwif;
548 struct scatterlist *sg, *scsi_sg;
549 int segments;
550
551 if (!pc->request_transfer || pc->request_transfer % 1024)
552 return 1;
553
554 if (idescsi_set_direction(pc))
555 return 1;
556
557 sg = hwif->sg_table;
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200558 scsi_sg = scsi_sglist(pc->scsi_cmd);
559 segments = scsi_sg_count(pc->scsi_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (segments > hwif->sg_max_nents)
562 return 1;
563
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200564 hwif->sg_nents = segments;
565 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return 0;
568}
569
570/*
571 * Issue a packet command
572 */
573static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
574{
575 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
576 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100577 u16 bcount;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100578 u8 dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 scsi->pc=pc; /* Set the current packet command */
581 pc->actually_transferred=0; /* We haven't transferred any data yet */
582 pc->current_position=pc->buffer;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100583 /* Request to transfer the entire buffer at once */
584 bcount = min(pc->request_transfer, 63 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
587 hwif->sg_mapped = 1;
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100588 dma = !hwif->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 hwif->sg_mapped = 0;
590 }
591
592 SELECT_DRIVE(drive);
593 if (IDE_CONTROL_REG)
594 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
595
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100596 hwif->OUTB(dma, IDE_FEATURE_REG);
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +0100597 hwif->OUTB((bcount >> 8) & 0xff, IDE_BCOUNTH_REG);
598 hwif->OUTB(bcount & 0xff, IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Bartlomiej Zolnierkiewicze5f9f5a2008-01-25 22:17:12 +0100600 if (dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 set_bit(PC_DMA_OK, &pc->flags);
602
603 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700604 BUG_ON(HWGROUP(drive)->handler != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ide_set_handler(drive, &idescsi_transfer_pc,
606 get_timeout(pc), idescsi_expiry);
607 /* Issue the packet command */
608 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
609 return ide_started;
610 } else {
611 /* Issue the packet command */
612 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
613 return idescsi_transfer_pc(drive);
614 }
615}
616
617/*
618 * idescsi_do_request is our request handling function.
619 */
620static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
621{
622#if IDESCSI_DEBUG_LOG
Jens Axboecdd60262006-07-28 09:32:07 +0200623 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 -0700624 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
625#endif /* IDESCSI_DEBUG_LOG */
626
Jens Axboe4aff5e22006-08-10 08:44:47 +0200627 if (blk_sense_request(rq) || blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
629 }
630 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
631 idescsi_end_request (drive, 0, 0);
632 return ide_stopped;
633}
634
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200635#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static void idescsi_add_settings(ide_drive_t *drive)
637{
638 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
639
640/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200641 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200643 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
644 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
645 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
646 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
647 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200649#else
650static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653/*
654 * Driver initialization.
655 */
656static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (drive->id && (drive->id->config & 0x0060) == 0x20)
659 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
661#if IDESCSI_DEBUG_LOG
662 set_bit(IDESCSI_LOG_CMD, &scsi->log);
663#endif /* IDESCSI_DEBUG_LOG */
664 idescsi_add_settings(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800667static void ide_scsi_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct Scsi_Host *scsihost = drive->driver_data;
670 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
671 struct gendisk *g = scsi->disk;
672
Matthew Wilcox6fdea8d2007-08-15 12:57:01 -0600673 scsi_remove_host(scsihost);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200674 ide_proc_unregister_driver(drive, scsi->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 ide_unregister_region(g);
677
678 drive->driver_data = NULL;
679 g->private_data = NULL;
680 put_disk(g);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ide_scsi_put(scsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800685static int ide_scsi_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +0200687#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static ide_proc_entry_t idescsi_proc[] = {
689 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
690 { NULL, 0, NULL, NULL }
691};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#endif
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static ide_driver_t idescsi_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200695 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +0100696 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200697 .name = "ide-scsi",
698 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200699 },
Mikael Petterssonb62735d2006-02-01 03:04:45 -0800700 .probe = ide_scsi_probe,
701 .remove = ide_scsi_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 .version = IDESCSI_VERSION,
703 .media = ide_scsi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 .supports_dsc_overlap = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 .do_request = idescsi_do_request,
706 .end_request = idescsi_end_request,
707 .error = idescsi_atapi_error,
708 .abort = idescsi_atapi_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200709#ifdef CONFIG_IDE_PROC_FS
710 .proc = idescsi_proc,
711#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};
713
714static int idescsi_ide_open(struct inode *inode, struct file *filp)
715{
716 struct gendisk *disk = inode->i_bdev->bd_disk;
717 struct ide_scsi_obj *scsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (!(scsi = ide_scsi_get(disk)))
720 return -ENXIO;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723}
724
725static int idescsi_ide_release(struct inode *inode, struct file *filp)
726{
727 struct gendisk *disk = inode->i_bdev->bd_disk;
728 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 ide_scsi_put(scsi);
731
732 return 0;
733}
734
735static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
736 unsigned int cmd, unsigned long arg)
737{
738 struct block_device *bdev = inode->i_bdev;
739 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
740 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
741}
742
743static struct block_device_operations idescsi_ops = {
744 .owner = THIS_MODULE,
745 .open = idescsi_ide_open,
746 .release = idescsi_ide_release,
747 .ioctl = idescsi_ide_ioctl,
748};
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static int idescsi_slave_configure(struct scsi_device * sdp)
751{
752 /* Configure detected device */
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200753 sdp->use_10_for_rw = 1;
754 sdp->use_10_for_ms = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
756 return 0;
757}
758
759static const char *idescsi_info (struct Scsi_Host *host)
760{
761 return "SCSI host adapter emulation for IDE ATAPI devices";
762}
763
764static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
765{
766 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
767
768 if (cmd == SG_SET_TRANSFORM) {
769 if (arg)
770 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
771 else
772 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
773 return 0;
774 } else if (cmd == SG_GET_TRANSFORM)
775 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
776 return -EINVAL;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static int idescsi_queue (struct scsi_cmnd *cmd,
780 void (*done)(struct scsi_cmnd *))
781{
782 struct Scsi_Host *host = cmd->device->host;
783 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
784 ide_drive_t *drive = scsi->drive;
785 struct request *rq = NULL;
786 idescsi_pc_t *pc = NULL;
787
788 if (!drive) {
Jeff Garzik017560f2005-10-24 18:04:36 -0400789 scmd_printk (KERN_ERR, cmd, "drive not present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto abort;
791 }
792 scsi = drive_to_idescsi(drive);
793 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
794 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
795 if (rq == NULL || pc == NULL) {
796 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
797 goto abort;
798 }
799
800 memset (pc->c, 0, 12);
801 pc->flags = 0;
802 pc->rq = rq;
803 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200804 pc->buffer = NULL;
805 pc->sg = scsi_sglist(cmd);
Jens Axboec79d88b2007-10-17 13:16:35 +0200806 pc->sg_cnt = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 pc->b_count = 0;
Boaz Harrosh427d0bd2007-09-18 12:27:43 +0200808 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 pc->scsi_cmd = cmd;
810 pc->done = done;
811 pc->timeout = jiffies + cmd->timeout_per_command;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
814 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100815 ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
817 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
Bartlomiej Zolnierkiewicz69ae6fe2007-12-12 23:31:57 +0100818 ide_scsi_hex_dump(pc->c, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 }
821
822 ide_init_drive_cmd (rq);
823 rq->special = (char *) pc;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200824 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 spin_unlock_irq(host->host_lock);
826 rq->rq_disk = scsi->disk;
827 (void) ide_do_drive_cmd (drive, rq, ide_end);
828 spin_lock_irq(host->host_lock);
829 return 0;
830abort:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800831 kfree (pc);
832 kfree (rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 cmd->result = DID_ERROR << 16;
834 done(cmd);
835 return 0;
836}
837
838static int idescsi_eh_abort (struct scsi_cmnd *cmd)
839{
840 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
841 ide_drive_t *drive = scsi->drive;
842 int busy;
843 int ret = FAILED;
844
845 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
846
847 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
848 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
849
850 if (!drive) {
851 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
852 WARN_ON(1);
853 goto no_drive;
854 }
855
856 /* First give it some more time, how much is "right" is hard to say :-( */
857
858 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
859 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
860 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
861
862 spin_lock_irq(&ide_lock);
863
864 /* If there is no pc running we're done (our interrupt took care of it) */
865 if (!scsi->pc) {
866 ret = SUCCESS;
867 goto ide_unlock;
868 }
869
870 /* It's somewhere in flight. Does ide subsystem agree? */
871 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
872 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
873 /*
874 * FIXME - not sure this condition can ever occur
875 */
876 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
877
Jens Axboe4aff5e22006-08-10 08:44:47 +0200878 if (blk_sense_request(scsi->pc->rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 kfree(scsi->pc->buffer);
880 kfree(scsi->pc->rq);
881 kfree(scsi->pc);
882 scsi->pc = NULL;
883
884 ret = SUCCESS;
885 }
886
887ide_unlock:
888 spin_unlock_irq(&ide_lock);
889no_drive:
890 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
891 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
892
893 return ret;
894}
895
896static int idescsi_eh_reset (struct scsi_cmnd *cmd)
897{
898 struct request *req;
899 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
900 ide_drive_t *drive = scsi->drive;
901 int ready = 0;
902 int ret = SUCCESS;
903
904 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
905
906 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
907 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
908
909 if (!drive) {
910 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
911 WARN_ON(1);
912 return FAILED;
913 }
914
Jeff Garzik df0ae242005-05-28 07:57:14 -0400915 spin_lock_irq(cmd->device->host->host_lock);
916 spin_lock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
919 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
920 spin_unlock(&ide_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -0400921 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return FAILED;
923 }
924
925 /* kill current request */
926 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100927 end_that_request_last(req, 0);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200928 if (blk_sense_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 kfree(scsi->pc->buffer);
930 kfree(scsi->pc);
931 scsi->pc = NULL;
932 kfree(req);
933
934 /* now nuke the drive queue */
935 while ((req = elv_next_request(drive->queue))) {
936 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100937 end_that_request_last(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 HWGROUP(drive)->rq = NULL;
941 HWGROUP(drive)->handler = NULL;
942 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
Jeff Garzik df0ae242005-05-28 07:57:14 -0400943 spin_unlock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 ide_do_reset(drive);
946
947 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
948
949 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 spin_unlock_irq(cmd->device->host->host_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -0400951 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 spin_lock_irq(cmd->device->host->host_lock);
953 } while ( HWGROUP(drive)->handler );
954
955 ready = drive_is_ready(drive);
956 HWGROUP(drive)->busy--;
957 if (!ready) {
958 printk (KERN_ERR "ide-scsi: reset failed!\n");
959 ret = FAILED;
960 }
961
Jeff Garzik df0ae242005-05-28 07:57:14 -0400962 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return ret;
964}
965
966static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
967 sector_t capacity, int *parm)
968{
969 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
970 ide_drive_t *drive = idescsi->drive;
971
972 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
973 parm[0] = drive->bios_head;
974 parm[1] = drive->bios_sect;
975 parm[2] = drive->bios_cyl;
976 }
977 return 0;
978}
979
980static struct scsi_host_template idescsi_template = {
981 .module = THIS_MODULE,
982 .name = "idescsi",
983 .info = idescsi_info,
984 .slave_configure = idescsi_slave_configure,
985 .ioctl = idescsi_ioctl,
986 .queuecommand = idescsi_queue,
987 .eh_abort_handler = idescsi_eh_abort,
988 .eh_host_reset_handler = idescsi_eh_reset,
989 .bios_param = idescsi_bios,
990 .can_queue = 40,
991 .this_id = -1,
992 .sg_tablesize = 256,
993 .cmd_per_lun = 5,
994 .max_sectors = 128,
995 .use_clustering = DISABLE_CLUSTERING,
996 .emulated = 1,
997 .proc_name = "ide-scsi",
998};
999
Mikael Petterssonb62735d2006-02-01 03:04:45 -08001000static int ide_scsi_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 idescsi_scsi_t *idescsi;
1003 struct Scsi_Host *host;
1004 struct gendisk *g;
1005 static int warned;
1006 int err = -ENOMEM;
1007
1008 if (!warned && drive->media == ide_cdrom) {
1009 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1010 warned = 1;
1011 }
1012
Alan Coxaaeab802006-12-06 20:39:33 -08001013 if (idescsi_nocd && drive->media == ide_cdrom)
1014 return -ENODEV;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!strstr("ide-scsi", drive->driver_req) ||
1017 !drive->present ||
1018 drive->media == ide_disk ||
1019 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001020 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 g = alloc_disk(1 << PARTN_BITS);
1023 if (!g)
1024 goto out_host_put;
1025
1026 ide_init_disk(g, drive);
1027
1028 host->max_id = 1;
1029
1030#if IDESCSI_DEBUG_LOG
1031 if (drive->id->last_lun)
1032 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1033#endif
1034 if ((drive->id->last_lun & 0x7) != 7)
1035 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1036 else
1037 host->max_lun = 1;
1038
1039 drive->driver_data = host;
1040 idescsi = scsihost_to_idescsi(host);
1041 idescsi->drive = drive;
1042 idescsi->driver = &idescsi_driver;
1043 idescsi->host = host;
1044 idescsi->disk = g;
1045 g->private_data = &idescsi->driver;
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001046 ide_proc_register_driver(drive, &idescsi_driver);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001047 err = 0;
1048 idescsi_setup(drive, idescsi);
1049 g->fops = &idescsi_ops;
1050 ide_register_region(g);
1051 err = scsi_add_host(host, &drive->gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!err) {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001053 scsi_scan_host(host);
1054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001056 /* fall through on error */
1057 ide_unregister_region(g);
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001058 ide_proc_unregister_driver(drive, &idescsi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 put_disk(g);
1061out_host_put:
1062 scsi_host_put(host);
1063 return err;
1064}
1065
1066static int __init init_idescsi_module(void)
1067{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001068 return driver_register(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071static void __exit exit_idescsi_module(void)
1072{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001073 driver_unregister(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Alan Coxaaeab802006-12-06 20:39:33 -08001076module_param(idescsi_nocd, int, 0600);
1077MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078module_init(init_idescsi_module);
1079module_exit(exit_idescsi_module);
1080MODULE_LICENSE("GPL");