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