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