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