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