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 */ 31 32#define IDESCSI_VERSION "0.9" 33 34#include <linux/module.h> 35#include <linux/types.h> 36#include <linux/string.h> 37#include <linux/kernel.h> 38#include <linux/mm.h> 39#include <linux/ioport.h> 40#include <linux/blkdev.h> 41#include <linux/errno.h> 42#include <linux/hdreg.h> 43#include <linux/slab.h> 44#include <linux/ide.h> 45 46#include <asm/io.h> 47#include <asm/bitops.h> 48#include <asm/uaccess.h> 49 50#include "scsi.h" 51#include "hosts.h" 52#include "sd.h" 53#include "ide-scsi.h" 54#include <scsi/sg.h> 55 56#define IDESCSI_DEBUG_LOG 0 57 58typedef struct idescsi_pc_s { 59 u8 c[12]; /* Actual packet bytes */ 60 int request_transfer; /* Bytes to transfer */ 61 int actually_transferred; /* Bytes actually transferred */ 62 int buffer_size; /* Size of our data buffer */ 63 struct request *rq; /* The corresponding request */ 64 byte *buffer; /* Data buffer */ 65 byte *current_position; /* Pointer into the above buffer */ 66 struct scatterlist *sg; /* Scatter gather table */ 67 int b_count; /* Bytes transferred from current entry */ 68 Scsi_Cmnd *scsi_cmd; /* SCSI command */ 69 void (*done)(Scsi_Cmnd *); /* Scsi completion routine */ 70 unsigned long flags; /* Status/Action flags */ 71 unsigned long timeout; /* Command timeout */ 72} idescsi_pc_t; 73 74/* 75 * Packet command status bits. 76 */ 77#define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */ 78#define PC_WRITING 1 /* Data direction */ 79#define PC_TRANSFORM 2 /* transform SCSI commands */ 80 81/* 82 * SCSI command transformation layer 83 */ 84#define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */ 85#define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */ 86 87/* 88 * Log flags 89 */ 90#define IDESCSI_LOG_CMD 0 /* Log SCSI commands */ 91 92typedef struct { 93 ide_drive_t *drive; 94 idescsi_pc_t *pc; /* Current packet command */ 95 unsigned long flags; /* Status/Action flags */ 96 unsigned long transform; /* SCSI cmd translation layer */ 97 unsigned long log; /* log flags */ 98} idescsi_scsi_t; 99 100/* 101 * Per ATAPI device status bits. 102 */ 103#define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */ 104 105/* 106 * ide-scsi requests. 107 */ 108#define IDESCSI_PC_RQ 90 109 110/* 111 * Bits of the interrupt reason register. 112 */ 113#define IDESCSI_IREASON_COD 0x1 /* Information transferred is command */ 114#define IDESCSI_IREASON_IO 0x2 /* The device requests us to read */ 115 116static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount) 117{ 118 while (bcount--) 119 IN_BYTE (IDE_DATA_REG); 120} 121 122static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount) 123{ 124 while (bcount--) 125 OUT_BYTE (0, IDE_DATA_REG); 126} 127 128/* 129 * PIO data transfer routines using the scatter gather table. 130 */ 131static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount) 132{ 133 int count; 134 135 while (bcount) { 136 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) { 137 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n"); 138 idescsi_discard_data (drive, bcount); 139 return; 140 } 141 count = IDE_MIN (pc->sg->length - pc->b_count, bcount); 142 atapi_input_bytes (drive, pc->sg->address + pc->b_count, count); 143 bcount -= count; pc->b_count += count; 144 if (pc->b_count == pc->sg->length) { 145 pc->sg++; 146 pc->b_count = 0; 147 } 148 } 149} 150 151static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount) 152{ 153 int count; 154 155 while (bcount) { 156 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) { 157 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n"); 158 idescsi_output_zeros (drive, bcount); 159 return; 160 } 161 count = IDE_MIN (pc->sg->length - pc->b_count, bcount); 162 atapi_output_bytes (drive, pc->sg->address + pc->b_count, count); 163 bcount -= count; pc->b_count += count; 164 if (pc->b_count == pc->sg->length) { 165 pc->sg++; 166 pc->b_count = 0; 167 } 168 } 169} 170 171/* 172 * Most of the SCSI commands are supported directly by ATAPI devices. 173 * idescsi_transform_pc handles the few exceptions. 174 */ 175static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc) 176{ 177 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd; 178 char *atapi_buf; 179 180 if (!test_bit(PC_TRANSFORM, &pc->flags)) 181 return; 182 if (drive->media == ide_cdrom || drive->media == ide_optical) { 183 if (c[0] == READ_6 || c[0] == WRITE_6) { 184 c[8] = c[4]; c[5] = c[3]; c[4] = c[2]; 185 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0; 186 c[0] += (READ_10 - READ_6); 187 } 188 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) { 189 if (!scsi_buf) 190 return; 191 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL) 192 return; 193 memset(atapi_buf, 0, pc->buffer_size + 4); 194 memset (c, 0, 12); 195 c[0] = sc[0] | 0x40; c[1] = sc[1]; c[2] = sc[2]; 196 c[8] = sc[4] + 4; c[9] = sc[5]; 197 if (sc[4] + 4 > 255) 198 c[7] = sc[4] + 4 - 255; 199 if (c[0] == MODE_SELECT_10) { 200 atapi_buf[1] = scsi_buf[0]; /* Mode data length */ 201 atapi_buf[2] = scsi_buf[1]; /* Medium type */ 202 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */ 203 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */ 204 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4); 205 } 206 pc->buffer = atapi_buf; 207 pc->request_transfer += 4; 208 pc->buffer_size += 4; 209 } 210 } 211} 212 213static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc) 214{ 215 u8 *atapi_buf = pc->buffer; 216 u8 *sc = pc->scsi_cmd->cmnd; 217 u8 *scsi_buf = pc->scsi_cmd->request_buffer; 218 219 if (!test_bit(PC_TRANSFORM, &pc->flags)) 220 return; 221 if (drive->media == ide_cdrom || drive->media == ide_optical) { 222 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) { 223 scsi_buf[0] = atapi_buf[1]; /* Mode data length */ 224 scsi_buf[1] = atapi_buf[2]; /* Medium type */ 225 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */ 226 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */ 227 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8); 228 } 229 if (pc->c[0] == INQUIRY) { 230 scsi_buf[2] |= 2; /* ansi_revision */ 231 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */ 232 } 233 } 234 if (atapi_buf && atapi_buf != scsi_buf) 235 kfree(atapi_buf); 236} 237 238static inline void idescsi_free_bh (struct buffer_head *bh) 239{ 240 struct buffer_head *bhp; 241 242 while (bh) { 243 bhp = bh; 244 bh = bh->b_reqnext; 245 kfree (bhp); 246 } 247} 248 249static void hexdump(u8 *x, int len) 250{ 251 int i; 252 253 printk("[ "); 254 for (i = 0; i < len; i++) 255 printk("%x ", x[i]); 256 printk("]\n"); 257} 258 259static void idescsi_end_request (byte uptodate, ide_hwgroup_t *hwgroup) 260{ 261 ide_drive_t *drive = hwgroup->drive; 262 idescsi_scsi_t *scsi = drive->driver_data; 263 struct request *rq = hwgroup->rq; 264 idescsi_pc_t *pc = (idescsi_pc_t *) rq->buffer; 265 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log); 266 u8 *scsi_buf; 267 unsigned long flags; 268 269 if (rq->cmd != IDESCSI_PC_RQ) { 270 ide_end_request (uptodate, hwgroup); 271 return; 272 } 273 ide_end_drive_cmd (drive, 0, 0); 274 if (rq->errors >= ERROR_MAX) { 275 pc->scsi_cmd->result = DID_ERROR << 16; 276 if (log) 277 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number); 278 } else if (rq->errors) { 279 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16); 280 if (log) 281 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number); 282 } else { 283 pc->scsi_cmd->result = DID_OK << 16; 284 idescsi_transform_pc2 (drive, pc); 285 if (log) { 286 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number); 287 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) { 288 printk(", rst = "); 289 scsi_buf = pc->scsi_cmd->request_buffer; 290 hexdump(scsi_buf, IDE_MIN(16, pc->scsi_cmd->request_bufflen)); 291 } else printk("\n"); 292 } 293 } 294 spin_lock_irqsave(&io_request_lock,flags); 295 pc->done(pc->scsi_cmd); 296 spin_unlock_irqrestore(&io_request_lock,flags); 297 idescsi_free_bh (rq->bh); 298 kfree(pc); kfree(rq); 299 scsi->pc = NULL; 300} 301 302static inline unsigned long get_timeout(idescsi_pc_t *pc) 303{ 304 return IDE_MAX(WAIT_CMD, pc->timeout - jiffies); 305} 306 307/* 308 * Our interrupt handler. 309 */ 310static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive) 311{ 312 idescsi_scsi_t *scsi = drive->driver_data; 313 byte status, ireason; 314 int bcount; 315 idescsi_pc_t *pc=scsi->pc; 316 struct request *rq = pc->rq; 317 unsigned int temp; 318 319#if IDESCSI_DEBUG_LOG 320 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n"); 321#endif /* IDESCSI_DEBUG_LOG */ 322 323 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) { 324#if IDESCSI_DEBUG_LOG 325 printk ("ide-scsi: %s: DMA complete\n", drive->name); 326#endif /* IDESCSI_DEBUG_LOG */ 327 pc->actually_transferred=pc->request_transfer; 328 (void) (HWIF(drive)->dmaproc(ide_dma_end, drive)); 329 } 330 331 status = GET_STAT(); /* Clear the interrupt */ 332 333 if ((status & DRQ_STAT) == 0) { /* No more interrupts */ 334 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) 335 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred); 336 ide__sti(); 337 if (status & ERR_STAT) 338 rq->errors++; 339 idescsi_end_request (1, HWGROUP(drive)); 340 return ide_stopped; 341 } 342 bcount = IN_BYTE (IDE_BCOUNTH_REG) << 8 | IN_BYTE (IDE_BCOUNTL_REG); 343 ireason = IN_BYTE (IDE_IREASON_REG); 344 345 if (ireason & IDESCSI_IREASON_COD) { 346 printk (KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n"); 347 return ide_do_reset (drive); 348 } 349 if (ireason & IDESCSI_IREASON_IO) { 350 temp = pc->actually_transferred + bcount; 351 if ( temp > pc->request_transfer) { 352 if (temp > pc->buffer_size) { 353 printk (KERN_ERR "ide-scsi: The scsi wants to send us more data than expected - discarding data\n"); 354 temp = pc->buffer_size - pc->actually_transferred; 355 if (temp) { 356 clear_bit(PC_WRITING, &pc->flags); 357 if (pc->sg) 358 idescsi_input_buffers(drive, pc, temp); 359 else 360 atapi_input_bytes(drive, pc->current_position, temp); 361 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount); 362 } 363 pc->actually_transferred += temp; 364 pc->current_position += temp; 365 idescsi_discard_data (drive,bcount - temp); 366 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); 367 return ide_started; 368 } 369#if IDESCSI_DEBUG_LOG 370 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n"); 371#endif /* IDESCSI_DEBUG_LOG */ 372 } 373 } 374 if (ireason & IDESCSI_IREASON_IO) { 375 clear_bit(PC_WRITING, &pc->flags); 376 if (pc->sg) 377 idescsi_input_buffers (drive, pc, bcount); 378 else 379 atapi_input_bytes (drive,pc->current_position,bcount); 380 } else { 381 set_bit(PC_WRITING, &pc->flags); 382 if (pc->sg) 383 idescsi_output_buffers (drive, pc, bcount); 384 else 385 atapi_output_bytes (drive,pc->current_position,bcount); 386 } 387 pc->actually_transferred+=bcount; /* Update the current position */ 388 pc->current_position+=bcount; 389 390 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* And set the interrupt handler again */ 391 return ide_started; 392} 393 394static ide_startstop_t idescsi_transfer_pc (ide_drive_t *drive) 395{ 396 idescsi_scsi_t *scsi = drive->driver_data; 397 idescsi_pc_t *pc = scsi->pc; 398 byte ireason; 399 ide_startstop_t startstop; 400 401 if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) { 402 printk (KERN_ERR "ide-scsi: Strange, packet command initiated yet DRQ isn't asserted\n"); 403 return startstop; 404 } 405 ireason = IN_BYTE (IDE_IREASON_REG); 406 if ((ireason & (IDESCSI_IREASON_IO | IDESCSI_IREASON_COD)) != IDESCSI_IREASON_COD) { 407 printk (KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while issuing a packet command\n"); 408 return ide_do_reset (drive); 409 } 410 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), NULL); /* Set the interrupt routine */ 411 atapi_output_bytes (drive, scsi->pc->c, 12); /* Send the actual packet */ 412 return ide_started; 413} 414 415/* 416 * Issue a packet command 417 */ 418static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc) 419{ 420 idescsi_scsi_t *scsi = drive->driver_data; 421 int bcount; 422 struct request *rq = pc->rq; 423 int dma_ok = 0; 424 425 scsi->pc=pc; /* Set the current packet command */ 426 pc->actually_transferred=0; /* We haven't transferred any data yet */ 427 pc->current_position=pc->buffer; 428 bcount = IDE_MIN (pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */ 429 430 if (drive->using_dma && rq->bh) 431 dma_ok=!HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive); 432 433 SELECT_DRIVE(HWIF(drive), drive); 434 if (IDE_CONTROL_REG) 435 OUT_BYTE (drive->ctl,IDE_CONTROL_REG); 436 OUT_BYTE (dma_ok,IDE_FEATURE_REG); 437 OUT_BYTE (bcount >> 8,IDE_BCOUNTH_REG); 438 OUT_BYTE (bcount & 0xff,IDE_BCOUNTL_REG); 439 440 if (dma_ok) { 441 set_bit (PC_DMA_IN_PROGRESS, &pc->flags); 442 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive)); 443 } 444 if (test_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags)) { 445 ide_set_handler (drive, &idescsi_transfer_pc, get_timeout(pc), NULL); 446 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* Issue the packet command */ 447 return ide_started; 448 } else { 449 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); 450 return idescsi_transfer_pc (drive); 451 } 452} 453 454/* 455 * idescsi_do_request is our request handling function. 456 */ 457static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, unsigned long block) 458{ 459#if IDESCSI_DEBUG_LOG 460 printk (KERN_INFO "rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors); 461 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors); 462#endif /* IDESCSI_DEBUG_LOG */ 463 464 if (rq->cmd == IDESCSI_PC_RQ) { 465 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->buffer); 466 } 467 printk (KERN_ERR "ide-scsi: %s: unsupported command in request queue (%x)\n", drive->name, rq->cmd); 468 idescsi_end_request (0,HWGROUP (drive)); 469 return ide_stopped; 470} 471 472static int idescsi_open (struct inode *inode, struct file *filp, ide_drive_t *drive) 473{ 474 MOD_INC_USE_COUNT; 475 return 0; 476} 477 478static void idescsi_ide_release (struct inode *inode, struct file *filp, ide_drive_t *drive) 479{ 480 MOD_DEC_USE_COUNT; 481} 482 483static ide_drive_t *idescsi_drives[MAX_HWIFS * MAX_DRIVES]; 484static int idescsi_initialized = 0; 485 486static void idescsi_add_settings(ide_drive_t *drive) 487{ 488 idescsi_scsi_t *scsi = drive->driver_data; 489 490/* 491 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function 492 */ 493 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL); 494 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL); 495 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL); 496 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL); 497 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL); 498} 499 500/* 501 * Driver initialization. 502 */ 503static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi, int id) 504{ 505 DRIVER(drive)->busy++; 506 idescsi_drives[id] = drive; 507 drive->driver_data = scsi; 508 drive->ready_stat = 0; 509 memset (scsi, 0, sizeof (idescsi_scsi_t)); 510 scsi->drive = drive; 511 if (drive->id && (drive->id->config & 0x0060) == 0x20) 512 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags); 513 set_bit(IDESCSI_TRANSFORM, &scsi->transform); 514 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); 515#if IDESCSI_DEBUG_LOG 516 set_bit(IDESCSI_LOG_CMD, &scsi->log); 517#endif /* IDESCSI_DEBUG_LOG */ 518 idescsi_add_settings(drive); 519} 520 521static int idescsi_cleanup (ide_drive_t *drive) 522{ 523 idescsi_scsi_t *scsi = drive->driver_data; 524 525 if (ide_unregister_subdriver (drive)) 526 return 1; 527 drive->driver_data = NULL; 528 kfree (scsi); 529 return 0; 530} 531 532int idescsi_reinit(ide_drive_t *drive); 533 534/* 535 * IDE subdriver functions, registered with ide.c 536 */ 537static ide_driver_t idescsi_driver = { 538 name: "ide-scsi", 539 version: IDESCSI_VERSION, 540 media: ide_scsi, 541 busy: 0, 542 supports_dma: 1, 543 supports_dsc_overlap: 0, 544 cleanup: idescsi_cleanup, 545 standby: NULL, 546 flushcache: NULL, 547 do_request: idescsi_do_request, 548 end_request: idescsi_end_request, 549 ioctl: NULL, 550 open: idescsi_open, 551 release: idescsi_ide_release, 552 media_change: NULL, 553 revalidate: NULL, 554 pre_reset: NULL, 555 capacity: NULL, 556 special: NULL, 557 proc: NULL, 558 reinit: idescsi_reinit, 559 ata_prebuilder: NULL, 560 atapi_prebuilder: NULL, 561}; 562 563int idescsi_init (void); 564static ide_module_t idescsi_module = { 565 IDE_DRIVER_MODULE, 566 idescsi_init, 567 &idescsi_driver, 568 NULL 569}; 570 571int idescsi_reinit (ide_drive_t *drive) 572{ 573 return 0; 574} 575 576/* 577 * idescsi_init will register the driver for each scsi. 578 */ 579int idescsi_init (void) 580{ 581 ide_drive_t *drive; 582 idescsi_scsi_t *scsi; 583 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255}; 584 int i, failed, id; 585 586 if (idescsi_initialized) 587 return 0; 588 idescsi_initialized = 1; 589 for (i = 0; i < MAX_HWIFS * MAX_DRIVES; i++) 590 idescsi_drives[i] = NULL; 591 MOD_INC_USE_COUNT; 592 for (i = 0; media[i] != 255; i++) { 593 failed = 0; 594 while ((drive = ide_scan_devices (media[i], idescsi_driver.name, NULL, failed++)) != NULL) { 595 596 if ((scsi = (idescsi_scsi_t *) kmalloc (sizeof (idescsi_scsi_t), GFP_KERNEL)) == NULL) { 597 printk (KERN_ERR "ide-scsi: %s: Can't allocate a scsi structure\n", drive->name); 598 continue; 599 } 600 if (ide_register_subdriver (drive, &idescsi_driver, IDE_SUBDRIVER_VERSION)) { 601 printk (KERN_ERR "ide-scsi: %s: Failed to register the driver with ide.c\n", drive->name); 602 kfree (scsi); 603 continue; 604 } 605 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++); 606 idescsi_setup (drive, scsi, id); 607 failed--; 608 } 609 } 610 ide_register_module(&idescsi_module); 611 MOD_DEC_USE_COUNT; 612 return 0; 613} 614 615int idescsi_detect (Scsi_Host_Template *host_template) 616{ 617 struct Scsi_Host *host; 618 int id; 619 int last_lun = 0; 620 621 host_template->proc_name = "ide-scsi"; 622 host = scsi_register(host_template, 0); 623 if(host == NULL) 624 return 0; 625 626 for (id = 0; id < MAX_HWIFS * MAX_DRIVES && idescsi_drives[id]; id++) 627 last_lun = IDE_MAX(last_lun, idescsi_drives[id]->last_lun); 628 host->max_id = id; 629 host->max_lun = last_lun + 1; 630 host->can_queue = host->cmd_per_lun * id; 631 return 1; 632} 633 634int idescsi_release (struct Scsi_Host *host) 635{ 636 ide_drive_t *drive; 637 int id; 638 639 for (id = 0; id < MAX_HWIFS * MAX_DRIVES; id++) { 640 drive = idescsi_drives[id]; 641 if (drive) 642 DRIVER(drive)->busy--; 643 } 644 return 0; 645} 646 647const char *idescsi_info (struct Scsi_Host *host) 648{ 649 return "SCSI host adapter emulation for IDE ATAPI devices"; 650} 651 652int idescsi_ioctl (Scsi_Device *dev, int cmd, void *arg) 653{ 654 ide_drive_t *drive = idescsi_drives[dev->id]; 655 idescsi_scsi_t *scsi = drive->driver_data; 656 657 if (cmd == SG_SET_TRANSFORM) { 658 if (arg) 659 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); 660 else 661 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); 662 return 0; 663 } else if (cmd == SG_GET_TRANSFORM) 664 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int *) arg); 665 return -EINVAL; 666} 667 668static inline struct buffer_head *idescsi_kmalloc_bh (int count) 669{ 670 struct buffer_head *bh, *bhp, *first_bh; 671 672 if ((first_bh = bhp = bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL) 673 goto abort; 674 memset (bh, 0, sizeof (struct buffer_head)); 675 bh->b_reqnext = NULL; 676 while (--count) { 677 if ((bh = kmalloc (sizeof(struct buffer_head), GFP_ATOMIC)) == NULL) 678 goto abort; 679 memset (bh, 0, sizeof (struct buffer_head)); 680 bhp->b_reqnext = bh; 681 bhp = bh; 682 bh->b_reqnext = NULL; 683 } 684 return first_bh; 685abort: 686 idescsi_free_bh (first_bh); 687 return NULL; 688} 689 690static inline int idescsi_set_direction (idescsi_pc_t *pc) 691{ 692 switch (pc->c[0]) { 693 case READ_6: case READ_10: case READ_12: 694 clear_bit (PC_WRITING, &pc->flags); 695 return 0; 696 case WRITE_6: case WRITE_10: case WRITE_12: 697 set_bit (PC_WRITING, &pc->flags); 698 return 0; 699 default: 700 return 1; 701 } 702} 703 704static inline struct buffer_head *idescsi_dma_bh (ide_drive_t *drive, idescsi_pc_t *pc) 705{ 706 struct buffer_head *bh = NULL, *first_bh = NULL; 707 int segments = pc->scsi_cmd->use_sg; 708 struct scatterlist *sg = pc->scsi_cmd->request_buffer; 709 710 if (!drive->using_dma || !pc->request_transfer || pc->request_transfer & 1023) 711 return NULL; 712 if (idescsi_set_direction(pc)) 713 return NULL; 714 if (segments) { 715 if ((first_bh = bh = idescsi_kmalloc_bh (segments)) == NULL) 716 return NULL; 717#if IDESCSI_DEBUG_LOG 718 printk ("ide-scsi: %s: building DMA table, %d segments, %dkB total\n", drive->name, segments, pc->request_transfer >> 10); 719#endif /* IDESCSI_DEBUG_LOG */ 720 while (segments--) { 721 if (sg->address) { 722 bh->b_page = virt_to_page(sg->address); 723 bh->b_data = (char *) ((unsigned long) sg->address & ~PAGE_MASK); 724 } else if (sg->page) { 725 bh->b_page = sg->page; 726 bh->b_data = (char *) sg->offset; 727 } 728 729 bh->b_size = sg->length; 730 bh = bh->b_reqnext; 731 sg++; 732 } 733 } else { 734 /* 735 * non-sg requests are guarenteed not to reside in highmem /jens 736 */ 737 if ((first_bh = bh = idescsi_kmalloc_bh (1)) == NULL) 738 return NULL; 739#if IDESCSI_DEBUG_LOG 740 printk ("ide-scsi: %s: building DMA table for a single buffer (%dkB)\n", drive->name, pc->request_transfer >> 10); 741#endif /* IDESCSI_DEBUG_LOG */ 742 bh->b_data = pc->scsi_cmd->request_buffer; 743 bh->b_size = pc->request_transfer; 744 } 745 return first_bh; 746} 747 748static inline int should_transform(ide_drive_t *drive, Scsi_Cmnd *cmd) 749{ 750 idescsi_scsi_t *scsi = drive->driver_data; 751 752 if (MAJOR(cmd->request.rq_dev) == SCSI_GENERIC_MAJOR) 753 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform); 754 return test_bit(IDESCSI_TRANSFORM, &scsi->transform); 755} 756 757int idescsi_queue (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) 758{ 759 ide_drive_t *drive = idescsi_drives[cmd->target]; 760 idescsi_scsi_t *scsi; 761 struct request *rq = NULL; 762 idescsi_pc_t *pc = NULL; 763 764 if (!drive) { 765 printk (KERN_ERR "ide-scsi: drive id %d not present\n", cmd->target); 766 goto abort; 767 } 768 scsi = drive->driver_data; 769 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC); 770 rq = kmalloc (sizeof (struct request), GFP_ATOMIC); 771 if (rq == NULL || pc == NULL) { 772 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name); 773 goto abort; 774 } 775 776 memset (pc->c, 0, 12); 777 pc->flags = 0; 778 pc->rq = rq; 779 memcpy (pc->c, cmd->cmnd, cmd->cmd_len); 780 if (cmd->use_sg) { 781 pc->buffer = NULL; 782 pc->sg = cmd->request_buffer; 783 } else { 784 pc->buffer = cmd->request_buffer; 785 pc->sg = NULL; 786 } 787 pc->b_count = 0; 788 pc->request_transfer = pc->buffer_size = cmd->request_bufflen; 789 pc->scsi_cmd = cmd; 790 pc->done = done; 791 pc->timeout = jiffies + cmd->timeout_per_command; 792 793 if (should_transform(drive, cmd)) 794 set_bit(PC_TRANSFORM, &pc->flags); 795 idescsi_transform_pc1 (drive, pc); 796 797 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) { 798 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number); 799 hexdump(cmd->cmnd, cmd->cmd_len); 800 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) { 801 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number); 802 hexdump(pc->c, 12); 803 } 804 } 805 806 ide_init_drive_cmd (rq); 807 rq->buffer = (char *) pc; 808 rq->bh = idescsi_dma_bh (drive, pc); 809 rq->cmd = IDESCSI_PC_RQ; 810 spin_unlock_irq(&io_request_lock); 811 (void) ide_do_drive_cmd (drive, rq, ide_end); 812 spin_lock_irq(&io_request_lock); 813 return 0; 814abort: 815 if (pc) kfree (pc); 816 if (rq) kfree (rq); 817 cmd->result = DID_ERROR << 16; 818 done(cmd); 819 return 0; 820} 821 822int idescsi_abort (Scsi_Cmnd *cmd) 823{ 824 return SCSI_ABORT_SNOOZE; 825} 826 827int idescsi_reset (Scsi_Cmnd *cmd, unsigned int resetflags) 828{ 829 return SCSI_RESET_SUCCESS; 830} 831 832int idescsi_bios (Disk *disk, kdev_t dev, int *parm) 833{ 834 ide_drive_t *drive = idescsi_drives[disk->device->id]; 835 836 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) { 837 parm[0] = drive->bios_head; 838 parm[1] = drive->bios_sect; 839 parm[2] = drive->bios_cyl; 840 } 841 return 0; 842} 843 844static Scsi_Host_Template idescsi_template = IDESCSI; 845 846static int __init init_idescsi_module(void) 847{ 848 idescsi_init(); 849 idescsi_template.module = THIS_MODULE; 850 scsi_register_module (MODULE_SCSI_HA, &idescsi_template); 851 return 0; 852} 853 854static void __exit exit_idescsi_module(void) 855{ 856 ide_drive_t *drive; 857 byte media[] = {TYPE_DISK, TYPE_TAPE, TYPE_PROCESSOR, TYPE_WORM, TYPE_ROM, TYPE_SCANNER, TYPE_MOD, 255}; 858 int i, failed; 859 860 scsi_unregister_module (MODULE_SCSI_HA, &idescsi_template); 861 for (i = 0; media[i] != 255; i++) { 862 failed = 0; 863 while ((drive = ide_scan_devices (media[i], idescsi_driver.name, &idescsi_driver, failed)) != NULL) 864 if (idescsi_cleanup (drive)) { 865 printk ("%s: exit_idescsi_module() called while still busy\n", drive->name); 866 failed++; 867 } 868 } 869 ide_unregister_module(&idescsi_module); 870} 871 872module_init(init_idescsi_module); 873module_exit(exit_idescsi_module); 874MODULE_LICENSE("GPL"); 875