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> |
| 37 | #include <linux/config.h> |
| 38 | #include <linux/types.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/mm.h> |
| 42 | #include <linux/ioport.h> |
| 43 | #include <linux/blkdev.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/hdreg.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/ide.h> |
| 48 | #include <linux/scatterlist.h> |
| 49 | |
| 50 | #include <asm/io.h> |
| 51 | #include <asm/bitops.h> |
| 52 | #include <asm/uaccess.h> |
| 53 | |
| 54 | #include <scsi/scsi.h> |
| 55 | #include <scsi/scsi_cmnd.h> |
| 56 | #include <scsi/scsi_device.h> |
| 57 | #include <scsi/scsi_host.h> |
| 58 | #include <scsi/scsi_tcq.h> |
| 59 | #include <scsi/sg.h> |
| 60 | |
| 61 | #define IDESCSI_DEBUG_LOG 0 |
| 62 | |
| 63 | typedef struct idescsi_pc_s { |
| 64 | u8 c[12]; /* Actual packet bytes */ |
| 65 | int request_transfer; /* Bytes to transfer */ |
| 66 | int actually_transferred; /* Bytes actually transferred */ |
| 67 | int buffer_size; /* Size of our data buffer */ |
| 68 | struct request *rq; /* The corresponding request */ |
| 69 | u8 *buffer; /* Data buffer */ |
| 70 | u8 *current_position; /* Pointer into the above buffer */ |
| 71 | struct scatterlist *sg; /* Scatter gather table */ |
| 72 | int b_count; /* Bytes transferred from current entry */ |
| 73 | struct scsi_cmnd *scsi_cmd; /* SCSI command */ |
| 74 | void (*done)(struct scsi_cmnd *); /* Scsi completion routine */ |
| 75 | unsigned long flags; /* Status/Action flags */ |
| 76 | unsigned long timeout; /* Command timeout */ |
| 77 | } idescsi_pc_t; |
| 78 | |
| 79 | /* |
| 80 | * Packet command status bits. |
| 81 | */ |
| 82 | #define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */ |
| 83 | #define PC_WRITING 1 /* Data direction */ |
| 84 | #define PC_TRANSFORM 2 /* transform SCSI commands */ |
| 85 | #define PC_TIMEDOUT 3 /* command timed out */ |
| 86 | #define PC_DMA_OK 4 /* Use DMA */ |
| 87 | |
| 88 | /* |
| 89 | * SCSI command transformation layer |
| 90 | */ |
| 91 | #define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */ |
| 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 | |
| 111 | static DECLARE_MUTEX(idescsi_ref_sem); |
| 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 | |
| 120 | down(&idescsi_ref_sem); |
| 121 | scsi = ide_scsi_g(disk); |
| 122 | if (scsi) |
| 123 | scsi_host_get(scsi->host); |
| 124 | up(&idescsi_ref_sem); |
| 125 | return scsi; |
| 126 | } |
| 127 | |
| 128 | static void ide_scsi_put(struct ide_scsi_obj *scsi) |
| 129 | { |
| 130 | down(&idescsi_ref_sem); |
| 131 | scsi_host_put(scsi->host); |
| 132 | up(&idescsi_ref_sem); |
| 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) { |
| 176 | if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) { |
| 177 | printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n"); |
| 178 | idescsi_discard_data (drive, bcount); |
| 179 | return; |
| 180 | } |
| 181 | count = min(pc->sg->length - pc->b_count, bcount); |
| 182 | buf = page_address(pc->sg->page) + pc->sg->offset; |
| 183 | drive->hwif->atapi_input_bytes(drive, buf + pc->b_count, count); |
| 184 | bcount -= count; pc->b_count += count; |
| 185 | if (pc->b_count == pc->sg->length) { |
| 186 | pc->sg++; |
| 187 | pc->b_count = 0; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount) |
| 193 | { |
| 194 | int count; |
| 195 | char *buf; |
| 196 | |
| 197 | while (bcount) { |
| 198 | if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) { |
| 199 | printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n"); |
| 200 | idescsi_output_zeros (drive, bcount); |
| 201 | return; |
| 202 | } |
| 203 | count = min(pc->sg->length - pc->b_count, bcount); |
| 204 | buf = page_address(pc->sg->page) + pc->sg->offset; |
| 205 | drive->hwif->atapi_output_bytes(drive, buf + pc->b_count, count); |
| 206 | bcount -= count; pc->b_count += count; |
| 207 | if (pc->b_count == pc->sg->length) { |
| 208 | pc->sg++; |
| 209 | pc->b_count = 0; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Most of the SCSI commands are supported directly by ATAPI devices. |
| 216 | * idescsi_transform_pc handles the few exceptions. |
| 217 | */ |
| 218 | static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc) |
| 219 | { |
| 220 | u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd; |
| 221 | char *atapi_buf; |
| 222 | |
| 223 | if (!test_bit(PC_TRANSFORM, &pc->flags)) |
| 224 | return; |
| 225 | if (drive->media == ide_cdrom || drive->media == ide_optical) { |
| 226 | if (c[0] == READ_6 || c[0] == WRITE_6) { |
| 227 | c[8] = c[4]; c[5] = c[3]; c[4] = c[2]; |
| 228 | c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0; |
| 229 | c[0] += (READ_10 - READ_6); |
| 230 | } |
| 231 | if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) { |
| 232 | unsigned short new_len; |
| 233 | if (!scsi_buf) |
| 234 | return; |
| 235 | if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL) |
| 236 | return; |
| 237 | memset(atapi_buf, 0, pc->buffer_size + 4); |
| 238 | memset (c, 0, 12); |
| 239 | c[0] = sc[0] | 0x40; |
| 240 | c[1] = sc[1]; |
| 241 | c[2] = sc[2]; |
| 242 | new_len = sc[4] + 4; |
| 243 | c[8] = new_len; |
| 244 | c[7] = new_len >> 8; |
| 245 | c[9] = sc[5]; |
| 246 | if (c[0] == MODE_SELECT_10) { |
| 247 | atapi_buf[1] = scsi_buf[0]; /* Mode data length */ |
| 248 | atapi_buf[2] = scsi_buf[1]; /* Medium type */ |
| 249 | atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */ |
| 250 | atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */ |
| 251 | memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4); |
| 252 | } |
| 253 | pc->buffer = atapi_buf; |
| 254 | pc->request_transfer += 4; |
| 255 | pc->buffer_size += 4; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc) |
| 261 | { |
| 262 | u8 *atapi_buf = pc->buffer; |
| 263 | u8 *sc = pc->scsi_cmd->cmnd; |
| 264 | u8 *scsi_buf = pc->scsi_cmd->request_buffer; |
| 265 | |
| 266 | if (!test_bit(PC_TRANSFORM, &pc->flags)) |
| 267 | return; |
| 268 | if (drive->media == ide_cdrom || drive->media == ide_optical) { |
| 269 | if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) { |
| 270 | scsi_buf[0] = atapi_buf[1]; /* Mode data length */ |
| 271 | scsi_buf[1] = atapi_buf[2]; /* Medium type */ |
| 272 | scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */ |
| 273 | scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */ |
| 274 | memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8); |
| 275 | } |
| 276 | if (pc->c[0] == INQUIRY) { |
| 277 | scsi_buf[2] |= 2; /* ansi_revision */ |
| 278 | scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */ |
| 279 | } |
| 280 | } |
| 281 | if (atapi_buf && atapi_buf != scsi_buf) |
| 282 | kfree(atapi_buf); |
| 283 | } |
| 284 | |
| 285 | static void hexdump(u8 *x, int len) |
| 286 | { |
| 287 | int i; |
| 288 | |
| 289 | printk("[ "); |
| 290 | for (i = 0; i < len; i++) |
| 291 | printk("%x ", x[i]); |
| 292 | printk("]\n"); |
| 293 | } |
| 294 | |
| 295 | static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command) |
| 296 | { |
| 297 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 298 | idescsi_pc_t *pc; |
| 299 | struct request *rq; |
| 300 | u8 *buf; |
| 301 | |
| 302 | /* stuff a sense request in front of our current request */ |
| 303 | pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC); |
| 304 | rq = kmalloc (sizeof (struct request), GFP_ATOMIC); |
| 305 | buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC); |
| 306 | if (pc == NULL || rq == NULL || buf == NULL) { |
| 307 | if (pc) kfree(pc); |
| 308 | if (rq) kfree(rq); |
| 309 | if (buf) kfree(buf); |
| 310 | return -ENOMEM; |
| 311 | } |
| 312 | memset (pc, 0, sizeof (idescsi_pc_t)); |
| 313 | memset (buf, 0, SCSI_SENSE_BUFFERSIZE); |
| 314 | ide_init_drive_cmd(rq); |
| 315 | rq->special = (char *) pc; |
| 316 | pc->rq = rq; |
| 317 | pc->buffer = buf; |
| 318 | pc->c[0] = REQUEST_SENSE; |
| 319 | pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE; |
| 320 | rq->flags = REQ_SENSE; |
| 321 | pc->timeout = jiffies + WAIT_READY; |
| 322 | /* NOTE! Save the failed packet command in "rq->buffer" */ |
| 323 | rq->buffer = (void *) failed_command->special; |
| 324 | pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd; |
| 325 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) { |
| 326 | printk ("ide-scsi: %s: queue cmd = ", drive->name); |
| 327 | hexdump(pc->c, 6); |
| 328 | } |
| 329 | rq->rq_disk = scsi->disk; |
| 330 | return ide_do_drive_cmd(drive, rq, ide_preempt); |
| 331 | } |
| 332 | |
| 333 | static int idescsi_end_request(ide_drive_t *, int, int); |
| 334 | |
| 335 | static ide_startstop_t |
| 336 | idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err) |
| 337 | { |
| 338 | if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) |
| 339 | /* force an abort */ |
| 340 | HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG); |
| 341 | |
| 342 | rq->errors++; |
| 343 | |
| 344 | idescsi_end_request(drive, 0, 0); |
| 345 | |
| 346 | return ide_stopped; |
| 347 | } |
| 348 | |
| 349 | static ide_startstop_t |
| 350 | idescsi_atapi_abort(ide_drive_t *drive, struct request *rq) |
| 351 | { |
| 352 | #if IDESCSI_DEBUG_LOG |
| 353 | printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n", |
| 354 | ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number); |
| 355 | #endif |
| 356 | rq->errors |= ERROR_MAX; |
| 357 | |
| 358 | idescsi_end_request(drive, 0, 0); |
| 359 | |
| 360 | return ide_stopped; |
| 361 | } |
| 362 | |
| 363 | static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs) |
| 364 | { |
| 365 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 366 | struct request *rq = HWGROUP(drive)->rq; |
| 367 | idescsi_pc_t *pc = (idescsi_pc_t *) rq->special; |
| 368 | int log = test_bit(IDESCSI_LOG_CMD, &scsi->log); |
| 369 | struct Scsi_Host *host; |
| 370 | u8 *scsi_buf; |
| 371 | unsigned long flags; |
| 372 | |
| 373 | if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) { |
| 374 | ide_end_request(drive, uptodate, nrsecs); |
| 375 | return 0; |
| 376 | } |
| 377 | ide_end_drive_cmd (drive, 0, 0); |
| 378 | if (rq->flags & REQ_SENSE) { |
| 379 | idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer; |
| 380 | if (log) { |
| 381 | printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number); |
| 382 | hexdump(pc->buffer,16); |
| 383 | } |
| 384 | memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE); |
| 385 | kfree(pc->buffer); |
| 386 | kfree(pc); |
| 387 | kfree(rq); |
| 388 | pc = opc; |
| 389 | rq = pc->rq; |
| 390 | pc->scsi_cmd->result = (CHECK_CONDITION << 1) | |
| 391 | ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16); |
| 392 | } else if (test_bit(PC_TIMEDOUT, &pc->flags)) { |
| 393 | if (log) |
| 394 | printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n", |
| 395 | drive->name, pc->scsi_cmd->serial_number); |
| 396 | pc->scsi_cmd->result = DID_TIME_OUT << 16; |
| 397 | } else if (rq->errors >= ERROR_MAX) { |
| 398 | pc->scsi_cmd->result = DID_ERROR << 16; |
| 399 | if (log) |
| 400 | printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number); |
| 401 | } else if (rq->errors) { |
| 402 | if (log) |
| 403 | printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number); |
| 404 | if (!idescsi_check_condition(drive, rq)) |
| 405 | /* we started a request sense, so we'll be back, exit for now */ |
| 406 | return 0; |
| 407 | pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16); |
| 408 | } else { |
| 409 | pc->scsi_cmd->result = DID_OK << 16; |
| 410 | idescsi_transform_pc2 (drive, pc); |
| 411 | if (log) { |
| 412 | printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number); |
| 413 | if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) { |
| 414 | printk(", rst = "); |
| 415 | scsi_buf = pc->scsi_cmd->request_buffer; |
| 416 | hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen)); |
| 417 | } else printk("\n"); |
| 418 | } |
| 419 | } |
| 420 | host = pc->scsi_cmd->device->host; |
| 421 | spin_lock_irqsave(host->host_lock, flags); |
| 422 | pc->done(pc->scsi_cmd); |
| 423 | spin_unlock_irqrestore(host->host_lock, flags); |
| 424 | kfree(pc); |
| 425 | kfree(rq); |
| 426 | scsi->pc = NULL; |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static inline unsigned long get_timeout(idescsi_pc_t *pc) |
| 431 | { |
| 432 | return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies); |
| 433 | } |
| 434 | |
| 435 | static int idescsi_expiry(ide_drive_t *drive) |
| 436 | { |
| 437 | idescsi_scsi_t *scsi = drive->driver_data; |
| 438 | idescsi_pc_t *pc = scsi->pc; |
| 439 | |
| 440 | #if IDESCSI_DEBUG_LOG |
| 441 | printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies); |
| 442 | #endif |
| 443 | set_bit(PC_TIMEDOUT, &pc->flags); |
| 444 | |
| 445 | return 0; /* we do not want the ide subsystem to retry */ |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Our interrupt handler. |
| 450 | */ |
| 451 | static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive) |
| 452 | { |
| 453 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 454 | idescsi_pc_t *pc=scsi->pc; |
| 455 | struct request *rq = pc->rq; |
| 456 | atapi_bcount_t bcount; |
| 457 | atapi_status_t status; |
| 458 | atapi_ireason_t ireason; |
| 459 | atapi_feature_t feature; |
| 460 | |
| 461 | unsigned int temp; |
| 462 | |
| 463 | #if IDESCSI_DEBUG_LOG |
| 464 | printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n"); |
| 465 | #endif /* IDESCSI_DEBUG_LOG */ |
| 466 | |
| 467 | if (test_bit(PC_TIMEDOUT, &pc->flags)){ |
| 468 | #if IDESCSI_DEBUG_LOG |
| 469 | printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n", |
| 470 | pc->scsi_cmd->serial_number, jiffies); |
| 471 | #endif |
| 472 | /* end this request now - scsi should retry it*/ |
| 473 | idescsi_end_request (drive, 1, 0); |
| 474 | return ide_stopped; |
| 475 | } |
| 476 | if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) { |
| 477 | #if IDESCSI_DEBUG_LOG |
| 478 | printk ("ide-scsi: %s: DMA complete\n", drive->name); |
| 479 | #endif /* IDESCSI_DEBUG_LOG */ |
| 480 | pc->actually_transferred=pc->request_transfer; |
| 481 | (void) HWIF(drive)->ide_dma_end(drive); |
| 482 | } |
| 483 | |
| 484 | feature.all = 0; |
| 485 | /* Clear the interrupt */ |
| 486 | status.all = HWIF(drive)->INB(IDE_STATUS_REG); |
| 487 | |
| 488 | if (!status.b.drq) { |
| 489 | /* No more interrupts */ |
| 490 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) |
| 491 | printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred); |
| 492 | local_irq_enable(); |
| 493 | if (status.b.check) |
| 494 | rq->errors++; |
| 495 | idescsi_end_request (drive, 1, 0); |
| 496 | return ide_stopped; |
| 497 | } |
| 498 | bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG); |
| 499 | bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG); |
| 500 | ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); |
| 501 | |
| 502 | if (ireason.b.cod) { |
| 503 | printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n"); |
| 504 | return ide_do_reset (drive); |
| 505 | } |
| 506 | if (ireason.b.io) { |
| 507 | temp = pc->actually_transferred + bcount.all; |
| 508 | if (temp > pc->request_transfer) { |
| 509 | if (temp > pc->buffer_size) { |
| 510 | printk(KERN_ERR "ide-scsi: The scsi wants to " |
| 511 | "send us more data than expected " |
| 512 | "- discarding data\n"); |
| 513 | temp = pc->buffer_size - pc->actually_transferred; |
| 514 | if (temp) { |
| 515 | clear_bit(PC_WRITING, &pc->flags); |
| 516 | if (pc->sg) |
| 517 | idescsi_input_buffers(drive, pc, temp); |
| 518 | else |
| 519 | drive->hwif->atapi_input_bytes(drive, pc->current_position, temp); |
| 520 | printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all); |
| 521 | } |
| 522 | pc->actually_transferred += temp; |
| 523 | pc->current_position += temp; |
| 524 | idescsi_discard_data(drive, bcount.all - temp); |
| 525 | ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry); |
| 526 | return ide_started; |
| 527 | } |
| 528 | #if IDESCSI_DEBUG_LOG |
| 529 | printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n"); |
| 530 | #endif /* IDESCSI_DEBUG_LOG */ |
| 531 | } |
| 532 | } |
| 533 | if (ireason.b.io) { |
| 534 | clear_bit(PC_WRITING, &pc->flags); |
| 535 | if (pc->sg) |
| 536 | idescsi_input_buffers(drive, pc, bcount.all); |
| 537 | else |
| 538 | HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all); |
| 539 | } else { |
| 540 | set_bit(PC_WRITING, &pc->flags); |
| 541 | if (pc->sg) |
| 542 | idescsi_output_buffers (drive, pc, bcount.all); |
| 543 | else |
| 544 | HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all); |
| 545 | } |
| 546 | /* Update the current position */ |
| 547 | pc->actually_transferred += bcount.all; |
| 548 | pc->current_position += bcount.all; |
| 549 | |
| 550 | /* And set the interrupt handler again */ |
| 551 | ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry); |
| 552 | return ide_started; |
| 553 | } |
| 554 | |
| 555 | static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive) |
| 556 | { |
| 557 | ide_hwif_t *hwif = drive->hwif; |
| 558 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 559 | idescsi_pc_t *pc = scsi->pc; |
| 560 | atapi_ireason_t ireason; |
| 561 | ide_startstop_t startstop; |
| 562 | |
| 563 | if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) { |
| 564 | printk(KERN_ERR "ide-scsi: Strange, packet command " |
| 565 | "initiated yet DRQ isn't asserted\n"); |
| 566 | return startstop; |
| 567 | } |
| 568 | ireason.all = HWIF(drive)->INB(IDE_IREASON_REG); |
| 569 | if (!ireason.b.cod || ireason.b.io) { |
| 570 | printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while " |
| 571 | "issuing a packet command\n"); |
| 572 | return ide_do_reset (drive); |
| 573 | } |
| 574 | if (HWGROUP(drive)->handler != NULL) |
| 575 | BUG(); |
| 576 | /* Set the interrupt routine */ |
| 577 | ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry); |
| 578 | /* Send the actual packet */ |
| 579 | drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12); |
| 580 | if (test_bit (PC_DMA_OK, &pc->flags)) { |
| 581 | set_bit (PC_DMA_IN_PROGRESS, &pc->flags); |
| 582 | hwif->dma_start(drive); |
| 583 | } |
| 584 | return ide_started; |
| 585 | } |
| 586 | |
| 587 | static inline int idescsi_set_direction(idescsi_pc_t *pc) |
| 588 | { |
| 589 | switch (pc->c[0]) { |
| 590 | case READ_6: case READ_10: case READ_12: |
| 591 | clear_bit(PC_WRITING, &pc->flags); |
| 592 | return 0; |
| 593 | case WRITE_6: case WRITE_10: case WRITE_12: |
| 594 | set_bit(PC_WRITING, &pc->flags); |
| 595 | return 0; |
| 596 | default: |
| 597 | return 1; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc) |
| 602 | { |
| 603 | ide_hwif_t *hwif = drive->hwif; |
| 604 | struct scatterlist *sg, *scsi_sg; |
| 605 | int segments; |
| 606 | |
| 607 | if (!pc->request_transfer || pc->request_transfer % 1024) |
| 608 | return 1; |
| 609 | |
| 610 | if (idescsi_set_direction(pc)) |
| 611 | return 1; |
| 612 | |
| 613 | sg = hwif->sg_table; |
| 614 | scsi_sg = pc->scsi_cmd->request_buffer; |
| 615 | segments = pc->scsi_cmd->use_sg; |
| 616 | |
| 617 | if (segments > hwif->sg_max_nents) |
| 618 | return 1; |
| 619 | |
| 620 | if (!segments) { |
| 621 | hwif->sg_nents = 1; |
| 622 | sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer); |
| 623 | } else { |
| 624 | hwif->sg_nents = segments; |
| 625 | memcpy(sg, scsi_sg, sizeof(*sg) * segments); |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Issue a packet command |
| 633 | */ |
| 634 | static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc) |
| 635 | { |
| 636 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 637 | ide_hwif_t *hwif = drive->hwif; |
| 638 | atapi_feature_t feature; |
| 639 | atapi_bcount_t bcount; |
| 640 | |
| 641 | scsi->pc=pc; /* Set the current packet command */ |
| 642 | pc->actually_transferred=0; /* We haven't transferred any data yet */ |
| 643 | pc->current_position=pc->buffer; |
| 644 | bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */ |
| 645 | |
| 646 | feature.all = 0; |
| 647 | if (drive->using_dma && !idescsi_map_sg(drive, pc)) { |
| 648 | hwif->sg_mapped = 1; |
| 649 | feature.b.dma = !hwif->dma_setup(drive); |
| 650 | hwif->sg_mapped = 0; |
| 651 | } |
| 652 | |
| 653 | SELECT_DRIVE(drive); |
| 654 | if (IDE_CONTROL_REG) |
| 655 | HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG); |
| 656 | |
| 657 | HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG); |
| 658 | HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG); |
| 659 | HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG); |
| 660 | |
| 661 | if (feature.b.dma) |
| 662 | set_bit(PC_DMA_OK, &pc->flags); |
| 663 | |
| 664 | if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) { |
| 665 | if (HWGROUP(drive)->handler != NULL) |
| 666 | BUG(); |
| 667 | ide_set_handler(drive, &idescsi_transfer_pc, |
| 668 | get_timeout(pc), idescsi_expiry); |
| 669 | /* Issue the packet command */ |
| 670 | HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG); |
| 671 | return ide_started; |
| 672 | } else { |
| 673 | /* Issue the packet command */ |
| 674 | HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG); |
| 675 | return idescsi_transfer_pc(drive); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * idescsi_do_request is our request handling function. |
| 681 | */ |
| 682 | static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block) |
| 683 | { |
| 684 | #if IDESCSI_DEBUG_LOG |
| 685 | printk (KERN_INFO "rq_status: %d, dev: %s, cmd: %x, errors: %d\n",rq->rq_status, rq->rq_disk->disk_name,rq->cmd[0],rq->errors); |
| 686 | printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors); |
| 687 | #endif /* IDESCSI_DEBUG_LOG */ |
| 688 | |
| 689 | if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) { |
| 690 | return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special); |
| 691 | } |
| 692 | blk_dump_rq_flags(rq, "ide-scsi: unsup command"); |
| 693 | idescsi_end_request (drive, 0, 0); |
| 694 | return ide_stopped; |
| 695 | } |
| 696 | |
| 697 | static void idescsi_add_settings(ide_drive_t *drive) |
| 698 | { |
| 699 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 700 | |
| 701 | /* |
| 702 | * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function |
| 703 | */ |
| 704 | ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL); |
| 705 | ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL); |
| 706 | ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL); |
| 707 | ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL); |
| 708 | ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL); |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Driver initialization. |
| 713 | */ |
| 714 | static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi) |
| 715 | { |
| 716 | DRIVER(drive)->busy++; |
| 717 | if (drive->id && (drive->id->config & 0x0060) == 0x20) |
| 718 | set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags); |
| 719 | set_bit(IDESCSI_TRANSFORM, &scsi->transform); |
| 720 | clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); |
| 721 | #if IDESCSI_DEBUG_LOG |
| 722 | set_bit(IDESCSI_LOG_CMD, &scsi->log); |
| 723 | #endif /* IDESCSI_DEBUG_LOG */ |
| 724 | idescsi_add_settings(drive); |
| 725 | DRIVER(drive)->busy--; |
| 726 | } |
| 727 | |
| 728 | static int idescsi_cleanup (ide_drive_t *drive) |
| 729 | { |
| 730 | struct Scsi_Host *scsihost = drive->driver_data; |
| 731 | struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost); |
| 732 | struct gendisk *g = scsi->disk; |
| 733 | |
| 734 | if (ide_unregister_subdriver(drive)) |
| 735 | return 1; |
| 736 | |
| 737 | ide_unregister_region(g); |
| 738 | |
| 739 | drive->driver_data = NULL; |
| 740 | g->private_data = NULL; |
| 741 | put_disk(g); |
| 742 | |
| 743 | scsi_remove_host(scsihost); |
| 744 | ide_scsi_put(scsi); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int idescsi_attach(ide_drive_t *drive); |
| 750 | |
| 751 | #ifdef CONFIG_PROC_FS |
| 752 | static ide_proc_entry_t idescsi_proc[] = { |
| 753 | { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, |
| 754 | { NULL, 0, NULL, NULL } |
| 755 | }; |
| 756 | #else |
| 757 | # define idescsi_proc NULL |
| 758 | #endif |
| 759 | |
| 760 | /* |
| 761 | * IDE subdriver functions, registered with ide.c |
| 762 | */ |
| 763 | static ide_driver_t idescsi_driver = { |
| 764 | .owner = THIS_MODULE, |
| 765 | .name = "ide-scsi", |
| 766 | .version = IDESCSI_VERSION, |
| 767 | .media = ide_scsi, |
| 768 | .busy = 0, |
| 769 | .supports_dsc_overlap = 0, |
| 770 | .proc = idescsi_proc, |
| 771 | .attach = idescsi_attach, |
| 772 | .cleanup = idescsi_cleanup, |
| 773 | .do_request = idescsi_do_request, |
| 774 | .end_request = idescsi_end_request, |
| 775 | .error = idescsi_atapi_error, |
| 776 | .abort = idescsi_atapi_abort, |
| 777 | .drives = LIST_HEAD_INIT(idescsi_driver.drives), |
| 778 | }; |
| 779 | |
| 780 | static int idescsi_ide_open(struct inode *inode, struct file *filp) |
| 781 | { |
| 782 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 783 | struct ide_scsi_obj *scsi; |
| 784 | ide_drive_t *drive; |
| 785 | |
| 786 | if (!(scsi = ide_scsi_get(disk))) |
| 787 | return -ENXIO; |
| 788 | |
| 789 | drive = scsi->drive; |
| 790 | |
| 791 | drive->usage++; |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int idescsi_ide_release(struct inode *inode, struct file *filp) |
| 797 | { |
| 798 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 799 | struct ide_scsi_obj *scsi = ide_scsi_g(disk); |
| 800 | ide_drive_t *drive = scsi->drive; |
| 801 | |
| 802 | drive->usage--; |
| 803 | |
| 804 | ide_scsi_put(scsi); |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | static int idescsi_ide_ioctl(struct inode *inode, struct file *file, |
| 810 | unsigned int cmd, unsigned long arg) |
| 811 | { |
| 812 | struct block_device *bdev = inode->i_bdev; |
| 813 | struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk); |
| 814 | return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg); |
| 815 | } |
| 816 | |
| 817 | static struct block_device_operations idescsi_ops = { |
| 818 | .owner = THIS_MODULE, |
| 819 | .open = idescsi_ide_open, |
| 820 | .release = idescsi_ide_release, |
| 821 | .ioctl = idescsi_ide_ioctl, |
| 822 | }; |
| 823 | |
| 824 | static int idescsi_attach(ide_drive_t *drive); |
| 825 | |
| 826 | static int idescsi_slave_configure(struct scsi_device * sdp) |
| 827 | { |
| 828 | /* Configure detected device */ |
| 829 | scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun); |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static const char *idescsi_info (struct Scsi_Host *host) |
| 834 | { |
| 835 | return "SCSI host adapter emulation for IDE ATAPI devices"; |
| 836 | } |
| 837 | |
| 838 | static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg) |
| 839 | { |
| 840 | idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host); |
| 841 | |
| 842 | if (cmd == SG_SET_TRANSFORM) { |
| 843 | if (arg) |
| 844 | set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); |
| 845 | else |
| 846 | clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); |
| 847 | return 0; |
| 848 | } else if (cmd == SG_GET_TRANSFORM) |
| 849 | return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg); |
| 850 | return -EINVAL; |
| 851 | } |
| 852 | |
| 853 | static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd) |
| 854 | { |
| 855 | idescsi_scsi_t *scsi = drive_to_idescsi(drive); |
| 856 | |
| 857 | /* this was a layering violation and we can't support it |
| 858 | anymore, sorry. */ |
| 859 | #if 0 |
| 860 | struct gendisk *disk = cmd->request->rq_disk; |
| 861 | |
| 862 | if (disk) { |
| 863 | struct Scsi_Device_Template **p = disk->private_data; |
| 864 | if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0) |
| 865 | return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); |
| 866 | } |
| 867 | #endif |
| 868 | return test_bit(IDESCSI_TRANSFORM, &scsi->transform); |
| 869 | } |
| 870 | |
| 871 | static int idescsi_queue (struct scsi_cmnd *cmd, |
| 872 | void (*done)(struct scsi_cmnd *)) |
| 873 | { |
| 874 | struct Scsi_Host *host = cmd->device->host; |
| 875 | idescsi_scsi_t *scsi = scsihost_to_idescsi(host); |
| 876 | ide_drive_t *drive = scsi->drive; |
| 877 | struct request *rq = NULL; |
| 878 | idescsi_pc_t *pc = NULL; |
| 879 | |
| 880 | if (!drive) { |
| 881 | printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->device->id); |
| 882 | goto abort; |
| 883 | } |
| 884 | scsi = drive_to_idescsi(drive); |
| 885 | pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC); |
| 886 | rq = kmalloc (sizeof (struct request), GFP_ATOMIC); |
| 887 | if (rq == NULL || pc == NULL) { |
| 888 | printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name); |
| 889 | goto abort; |
| 890 | } |
| 891 | |
| 892 | memset (pc->c, 0, 12); |
| 893 | pc->flags = 0; |
| 894 | pc->rq = rq; |
| 895 | memcpy (pc->c, cmd->cmnd, cmd->cmd_len); |
| 896 | if (cmd->use_sg) { |
| 897 | pc->buffer = NULL; |
| 898 | pc->sg = cmd->request_buffer; |
| 899 | } else { |
| 900 | pc->buffer = cmd->request_buffer; |
| 901 | pc->sg = NULL; |
| 902 | } |
| 903 | pc->b_count = 0; |
| 904 | pc->request_transfer = pc->buffer_size = cmd->request_bufflen; |
| 905 | pc->scsi_cmd = cmd; |
| 906 | pc->done = done; |
| 907 | pc->timeout = jiffies + cmd->timeout_per_command; |
| 908 | |
| 909 | if (should_transform(drive, cmd)) |
| 910 | set_bit(PC_TRANSFORM, &pc->flags); |
| 911 | idescsi_transform_pc1 (drive, pc); |
| 912 | |
| 913 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) { |
| 914 | printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number); |
| 915 | hexdump(cmd->cmnd, cmd->cmd_len); |
| 916 | if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) { |
| 917 | printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number); |
| 918 | hexdump(pc->c, 12); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | ide_init_drive_cmd (rq); |
| 923 | rq->special = (char *) pc; |
| 924 | rq->flags = REQ_SPECIAL; |
| 925 | spin_unlock_irq(host->host_lock); |
| 926 | rq->rq_disk = scsi->disk; |
| 927 | (void) ide_do_drive_cmd (drive, rq, ide_end); |
| 928 | spin_lock_irq(host->host_lock); |
| 929 | return 0; |
| 930 | abort: |
| 931 | if (pc) kfree (pc); |
| 932 | if (rq) kfree (rq); |
| 933 | cmd->result = DID_ERROR << 16; |
| 934 | done(cmd); |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static int idescsi_eh_abort (struct scsi_cmnd *cmd) |
| 939 | { |
| 940 | idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host); |
| 941 | ide_drive_t *drive = scsi->drive; |
| 942 | int busy; |
| 943 | int ret = FAILED; |
| 944 | |
| 945 | /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */ |
| 946 | |
| 947 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) |
| 948 | printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number); |
| 949 | |
| 950 | if (!drive) { |
| 951 | printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n"); |
| 952 | WARN_ON(1); |
| 953 | goto no_drive; |
| 954 | } |
| 955 | |
| 956 | /* First give it some more time, how much is "right" is hard to say :-( */ |
| 957 | |
| 958 | busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */ |
| 959 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) |
| 960 | printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":""); |
| 961 | |
| 962 | spin_lock_irq(&ide_lock); |
| 963 | |
| 964 | /* If there is no pc running we're done (our interrupt took care of it) */ |
| 965 | if (!scsi->pc) { |
| 966 | ret = SUCCESS; |
| 967 | goto ide_unlock; |
| 968 | } |
| 969 | |
| 970 | /* It's somewhere in flight. Does ide subsystem agree? */ |
| 971 | if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy && |
| 972 | elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) { |
| 973 | /* |
| 974 | * FIXME - not sure this condition can ever occur |
| 975 | */ |
| 976 | printk (KERN_ERR "ide-scsi: cmd aborted!\n"); |
| 977 | |
| 978 | if (scsi->pc->rq->flags & REQ_SENSE) |
| 979 | kfree(scsi->pc->buffer); |
| 980 | kfree(scsi->pc->rq); |
| 981 | kfree(scsi->pc); |
| 982 | scsi->pc = NULL; |
| 983 | |
| 984 | ret = SUCCESS; |
| 985 | } |
| 986 | |
| 987 | ide_unlock: |
| 988 | spin_unlock_irq(&ide_lock); |
| 989 | no_drive: |
| 990 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) |
| 991 | printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed"); |
| 992 | |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | static int idescsi_eh_reset (struct scsi_cmnd *cmd) |
| 997 | { |
| 998 | struct request *req; |
| 999 | idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host); |
| 1000 | ide_drive_t *drive = scsi->drive; |
| 1001 | int ready = 0; |
| 1002 | int ret = SUCCESS; |
| 1003 | |
| 1004 | /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */ |
| 1005 | |
| 1006 | if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) |
| 1007 | printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number); |
| 1008 | |
| 1009 | if (!drive) { |
| 1010 | printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n"); |
| 1011 | WARN_ON(1); |
| 1012 | return FAILED; |
| 1013 | } |
| 1014 | |
| 1015 | spin_lock_irq(&ide_lock); |
| 1016 | |
| 1017 | if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) { |
| 1018 | printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n"); |
| 1019 | spin_unlock(&ide_lock); |
| 1020 | return FAILED; |
| 1021 | } |
| 1022 | |
| 1023 | /* kill current request */ |
| 1024 | blkdev_dequeue_request(req); |
| 1025 | end_that_request_last(req); |
| 1026 | if (req->flags & REQ_SENSE) |
| 1027 | kfree(scsi->pc->buffer); |
| 1028 | kfree(scsi->pc); |
| 1029 | scsi->pc = NULL; |
| 1030 | kfree(req); |
| 1031 | |
| 1032 | /* now nuke the drive queue */ |
| 1033 | while ((req = elv_next_request(drive->queue))) { |
| 1034 | blkdev_dequeue_request(req); |
| 1035 | end_that_request_last(req); |
| 1036 | } |
| 1037 | |
| 1038 | HWGROUP(drive)->rq = NULL; |
| 1039 | HWGROUP(drive)->handler = NULL; |
| 1040 | HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */ |
| 1041 | spin_unlock_irq(&ide_lock); |
| 1042 | |
| 1043 | ide_do_reset(drive); |
| 1044 | |
| 1045 | /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */ |
| 1046 | |
| 1047 | do { |
| 1048 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1049 | spin_unlock_irq(cmd->device->host->host_lock); |
| 1050 | schedule_timeout(HZ/20); |
| 1051 | spin_lock_irq(cmd->device->host->host_lock); |
| 1052 | } while ( HWGROUP(drive)->handler ); |
| 1053 | |
| 1054 | ready = drive_is_ready(drive); |
| 1055 | HWGROUP(drive)->busy--; |
| 1056 | if (!ready) { |
| 1057 | printk (KERN_ERR "ide-scsi: reset failed!\n"); |
| 1058 | ret = FAILED; |
| 1059 | } |
| 1060 | |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev, |
| 1065 | sector_t capacity, int *parm) |
| 1066 | { |
| 1067 | idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host); |
| 1068 | ide_drive_t *drive = idescsi->drive; |
| 1069 | |
| 1070 | if (drive->bios_cyl && drive->bios_head && drive->bios_sect) { |
| 1071 | parm[0] = drive->bios_head; |
| 1072 | parm[1] = drive->bios_sect; |
| 1073 | parm[2] = drive->bios_cyl; |
| 1074 | } |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static struct scsi_host_template idescsi_template = { |
| 1079 | .module = THIS_MODULE, |
| 1080 | .name = "idescsi", |
| 1081 | .info = idescsi_info, |
| 1082 | .slave_configure = idescsi_slave_configure, |
| 1083 | .ioctl = idescsi_ioctl, |
| 1084 | .queuecommand = idescsi_queue, |
| 1085 | .eh_abort_handler = idescsi_eh_abort, |
| 1086 | .eh_host_reset_handler = idescsi_eh_reset, |
| 1087 | .bios_param = idescsi_bios, |
| 1088 | .can_queue = 40, |
| 1089 | .this_id = -1, |
| 1090 | .sg_tablesize = 256, |
| 1091 | .cmd_per_lun = 5, |
| 1092 | .max_sectors = 128, |
| 1093 | .use_clustering = DISABLE_CLUSTERING, |
| 1094 | .emulated = 1, |
| 1095 | .proc_name = "ide-scsi", |
| 1096 | }; |
| 1097 | |
| 1098 | static int idescsi_attach(ide_drive_t *drive) |
| 1099 | { |
| 1100 | idescsi_scsi_t *idescsi; |
| 1101 | struct Scsi_Host *host; |
| 1102 | struct gendisk *g; |
| 1103 | static int warned; |
| 1104 | int err = -ENOMEM; |
| 1105 | |
| 1106 | if (!warned && drive->media == ide_cdrom) { |
| 1107 | printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n"); |
| 1108 | warned = 1; |
| 1109 | } |
| 1110 | |
| 1111 | if (!strstr("ide-scsi", drive->driver_req) || |
| 1112 | !drive->present || |
| 1113 | drive->media == ide_disk || |
| 1114 | !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t)))) |
| 1115 | return 1; |
| 1116 | |
| 1117 | g = alloc_disk(1 << PARTN_BITS); |
| 1118 | if (!g) |
| 1119 | goto out_host_put; |
| 1120 | |
| 1121 | ide_init_disk(g, drive); |
| 1122 | |
| 1123 | host->max_id = 1; |
| 1124 | |
| 1125 | #if IDESCSI_DEBUG_LOG |
| 1126 | if (drive->id->last_lun) |
| 1127 | printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun); |
| 1128 | #endif |
| 1129 | if ((drive->id->last_lun & 0x7) != 7) |
| 1130 | host->max_lun = (drive->id->last_lun & 0x7) + 1; |
| 1131 | else |
| 1132 | host->max_lun = 1; |
| 1133 | |
| 1134 | drive->driver_data = host; |
| 1135 | idescsi = scsihost_to_idescsi(host); |
| 1136 | idescsi->drive = drive; |
| 1137 | idescsi->driver = &idescsi_driver; |
| 1138 | idescsi->host = host; |
| 1139 | idescsi->disk = g; |
| 1140 | g->private_data = &idescsi->driver; |
| 1141 | err = ide_register_subdriver(drive, &idescsi_driver); |
| 1142 | if (!err) { |
| 1143 | idescsi_setup (drive, idescsi); |
| 1144 | g->fops = &idescsi_ops; |
| 1145 | ide_register_region(g); |
| 1146 | err = scsi_add_host(host, &drive->gendev); |
| 1147 | if (!err) { |
| 1148 | scsi_scan_host(host); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | /* fall through on error */ |
| 1152 | ide_unregister_region(g); |
| 1153 | ide_unregister_subdriver(drive); |
| 1154 | } |
| 1155 | |
| 1156 | put_disk(g); |
| 1157 | out_host_put: |
| 1158 | scsi_host_put(host); |
| 1159 | return err; |
| 1160 | } |
| 1161 | |
| 1162 | static int __init init_idescsi_module(void) |
| 1163 | { |
| 1164 | return ide_register_driver(&idescsi_driver); |
| 1165 | } |
| 1166 | |
| 1167 | static void __exit exit_idescsi_module(void) |
| 1168 | { |
| 1169 | ide_unregister_driver(&idescsi_driver); |
| 1170 | } |
| 1171 | |
| 1172 | module_init(init_idescsi_module); |
| 1173 | module_exit(exit_idescsi_module); |
| 1174 | MODULE_LICENSE("GPL"); |