ide-scsi.c 28.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
 * Copyright (C) 2004-2005  Bartlomiej Zolnierkiewicz
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 */

/*
 * Emulation of a SCSI host adapter for IDE ATAPI devices.
 *
 * With this driver, one can use the Linux SCSI drivers instead of the
 * native IDE ATAPI drivers.
 *
 * Ver 0.1   Dec  3 96   Initial version.
 * Ver 0.2   Jan 26 97   Fixed bug in cleanup_module() and added emulation
 *                        of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
 *                        to Janos Farkas for pointing this out.
 *                       Avoid using bitfields in structures for m68k.
 *                       Added Scatter/Gather and DMA support.
 * Ver 0.4   Dec  7 97   Add support for ATAPI PD/CD drives.
 *                       Use variable timeout for each command.
 * Ver 0.5   Jan  2 98   Fix previous PD/CD support.
 *                       Allow disabling of SCSI-6 to SCSI-10 transformation.
 * Ver 0.6   Jan 27 98   Allow disabling of SCSI command translation layer
 *                        for access through /dev/sg.
 *                       Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
 * Ver 0.7   Dec 04 98   Ignore commands where lun != 0 to avoid multiple
 *                        detection of devices with CONFIG_SCSI_MULTI_LUN
 * Ver 0.8   Feb 05 99   Optical media need translation too. Reverse 0.7.
 * Ver 0.9   Jul 04 99   Fix a bug in SG_SET_TRANSFORM.
 * Ver 0.91  Jun 10 02   Fix "off by one" error in transforms
 * Ver 0.92  Dec 31 02   Implement new SCSI mid level API
 */

#define IDESCSI_VERSION "0.92"

#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/slab.h>
#include <linux/ide.h>
#include <linux/scatterlist.h>
47
#include <linux/delay.h>
48
#include <linux/mutex.h>
J
Jiri Slaby 已提交
49
#include <linux/bitops.h>
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

#include <asm/io.h>
#include <asm/uaccess.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>
#include <scsi/sg.h>

#define IDESCSI_DEBUG_LOG		0

/*
 *	SCSI command transformation layer
 */
#define IDESCSI_SG_TRANSFORM		1	/* /dev/sg transformation */

/*
 *	Log flags
 */
#define IDESCSI_LOG_CMD			0	/* Log SCSI commands */

typedef struct ide_scsi_obj {
	ide_drive_t		*drive;
	ide_driver_t		*driver;
	struct gendisk		*disk;
	struct Scsi_Host	*host;

79
	struct ide_atapi_pc *pc;		/* Current packet command */
L
Linus Torvalds 已提交
80 81 82 83 84
	unsigned long flags;			/* Status/Action flags */
	unsigned long transform;		/* SCSI cmd translation layer */
	unsigned long log;			/* log flags */
} idescsi_scsi_t;

85
static DEFINE_MUTEX(idescsi_ref_mutex);
86 87
/* Set by module param to skip cd */
static int idescsi_nocd;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95

#define ide_scsi_g(disk) \
	container_of((disk)->private_data, struct ide_scsi_obj, driver)

static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
{
	struct ide_scsi_obj *scsi = NULL;

96
	mutex_lock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
97 98 99
	scsi = ide_scsi_g(disk);
	if (scsi)
		scsi_host_get(scsi->host);
100
	mutex_unlock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
101 102 103 104 105
	return scsi;
}

static void ide_scsi_put(struct ide_scsi_obj *scsi)
{
106
	mutex_lock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
107
	scsi_host_put(scsi->host);
108
	mutex_unlock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
{
	return (idescsi_scsi_t*) (&host[1]);
}

static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
{
	return scsihost_to_idescsi(ide_drive->driver_data);
}

/*
 *	Per ATAPI device status bits.
 */
#define IDESCSI_DRQ_INTERRUPT		0	/* DRQ interrupt device */

/*
 *	ide-scsi requests.
 */
#define IDESCSI_PC_RQ			90

/*
 *	PIO data transfer routines using the scatter gather table.
 */
134 135
static void idescsi_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
		unsigned int bcount)
L
Linus Torvalds 已提交
136
{
137
	ide_hwif_t *hwif = drive->hwif;
L
Linus Torvalds 已提交
138 139 140 141 142
	int count;
	char *buf;

	while (bcount) {
		count = min(pc->sg->length - pc->b_count, bcount);
J
Jens Axboe 已提交
143
		if (PageHighMem(sg_page(pc->sg))) {
144 145 146
			unsigned long flags;

			local_irq_save(flags);
J
Jens Axboe 已提交
147
			buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
148
					pc->sg->offset;
149
			hwif->input_data(drive, NULL, buf + pc->b_count, count);
150 151 152
			kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
			local_irq_restore(flags);
		} else {
J
Jens Axboe 已提交
153
			buf = sg_virt(pc->sg);
154
			hwif->input_data(drive, NULL, buf + pc->b_count, count);
155 156
		}
		bcount -= count; pc->b_count += count;
L
Linus Torvalds 已提交
157
		if (pc->b_count == pc->sg->length) {
158
			if (!--pc->sg_cnt)
J
Jens Axboe 已提交
159 160
				break;
			pc->sg = sg_next(pc->sg);
L
Linus Torvalds 已提交
161 162 163
			pc->b_count = 0;
		}
	}
J
Jens Axboe 已提交
164 165 166

	if (bcount) {
		printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
167
		ide_atapi_discard_data(drive, bcount);
J
Jens Axboe 已提交
168
	}
L
Linus Torvalds 已提交
169 170
}

171 172
static void idescsi_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
		unsigned int bcount)
L
Linus Torvalds 已提交
173
{
174
	ide_hwif_t *hwif = drive->hwif;
L
Linus Torvalds 已提交
175 176 177 178 179
	int count;
	char *buf;

	while (bcount) {
		count = min(pc->sg->length - pc->b_count, bcount);
J
Jens Axboe 已提交
180
		if (PageHighMem(sg_page(pc->sg))) {
181 182 183
			unsigned long flags;

			local_irq_save(flags);
J
Jens Axboe 已提交
184
			buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
185
						pc->sg->offset;
186
			hwif->output_data(drive, NULL, buf + pc->b_count, count);
187 188 189
			kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
			local_irq_restore(flags);
		} else {
J
Jens Axboe 已提交
190
			buf = sg_virt(pc->sg);
191
			hwif->output_data(drive, NULL, buf + pc->b_count, count);
192 193
		}
		bcount -= count; pc->b_count += count;
L
Linus Torvalds 已提交
194
		if (pc->b_count == pc->sg->length) {
195
			if (!--pc->sg_cnt)
J
Jens Axboe 已提交
196 197
				break;
			pc->sg = sg_next(pc->sg);
L
Linus Torvalds 已提交
198 199 200
			pc->b_count = 0;
		}
	}
J
Jens Axboe 已提交
201 202 203

	if (bcount) {
		printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
204
		ide_atapi_write_zeros(drive, bcount);
J
Jens Axboe 已提交
205
	}
L
Linus Torvalds 已提交
206 207
}

208 209 210 211 212
static void ide_scsi_hex_dump(u8 *data, int len)
{
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
}

213 214
static int idescsi_check_condition(ide_drive_t *drive,
		struct request *failed_cmd)
L
Linus Torvalds 已提交
215 216
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
217
	struct ide_atapi_pc   *pc;
L
Linus Torvalds 已提交
218 219 220 221
	struct request *rq;
	u8             *buf;

	/* stuff a sense request in front of our current request */
222
	pc = kzalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
223 224 225
	rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
	buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
	if (!pc || !rq || !buf) {
J
Jesper Juhl 已提交
226 227 228
		kfree(buf);
		kfree(rq);
		kfree(pc);
L
Linus Torvalds 已提交
229 230 231 232 233
		return -ENOMEM;
	}
	ide_init_drive_cmd(rq);
	rq->special = (char *) pc;
	pc->rq = rq;
234
	pc->buf = buf;
L
Linus Torvalds 已提交
235
	pc->c[0] = REQUEST_SENSE;
236
	pc->c[4] = pc->req_xfer = pc->buf_size = SCSI_SENSE_BUFFERSIZE;
237
	rq->cmd_type = REQ_TYPE_SENSE;
L
Linus Torvalds 已提交
238 239
	pc->timeout = jiffies + WAIT_READY;
	/* NOTE! Save the failed packet command in "rq->buffer" */
240 241
	rq->buffer = (void *) failed_cmd->special;
	pc->scsi_cmd = ((struct ide_atapi_pc *) failed_cmd->special)->scsi_cmd;
L
Linus Torvalds 已提交
242 243
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: queue cmd = ", drive->name);
244
		ide_scsi_hex_dump(pc->c, 6);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254
	}
	rq->rq_disk = scsi->disk;
	return ide_do_drive_cmd(drive, rq, ide_preempt);
}

static int idescsi_end_request(ide_drive_t *, int, int);

static ide_startstop_t
idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
{
255 256
	ide_hwif_t *hwif = drive->hwif;

257
	if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
L
Linus Torvalds 已提交
258
		/* force an abort */
259
		hwif->OUTB(WIN_IDLEIMMEDIATE, hwif->io_ports.command_addr);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272

	rq->errors++;

	idescsi_end_request(drive, 0, 0);

	return ide_stopped;
}

static ide_startstop_t
idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
{
#if IDESCSI_DEBUG_LOG
	printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
273
		((struct ide_atapi_pc *) rq->special)->scsi_cmd->serial_number);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285
#endif
	rq->errors |= ERROR_MAX;

	idescsi_end_request(drive, 0, 0);

	return ide_stopped;
}

static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
	struct request *rq = HWGROUP(drive)->rq;
286
	struct ide_atapi_pc *pc = (struct ide_atapi_pc *) rq->special;
L
Linus Torvalds 已提交
287 288
	int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
	struct Scsi_Host *host;
289
	int errors = rq->errors;
L
Linus Torvalds 已提交
290 291
	unsigned long flags;

292
	if (!blk_special_request(rq) && !blk_sense_request(rq)) {
L
Linus Torvalds 已提交
293 294 295 296
		ide_end_request(drive, uptodate, nrsecs);
		return 0;
	}
	ide_end_drive_cmd (drive, 0, 0);
297
	if (blk_sense_request(rq)) {
298
		struct ide_atapi_pc *opc = (struct ide_atapi_pc *) rq->buffer;
L
Linus Torvalds 已提交
299 300
		if (log) {
			printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
301
			ide_scsi_hex_dump(pc->buf, 16);
L
Linus Torvalds 已提交
302
		}
303 304 305
		memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buf,
			SCSI_SENSE_BUFFERSIZE);
		kfree(pc->buf);
L
Linus Torvalds 已提交
306 307 308 309 310
		kfree(pc);
		kfree(rq);
		pc = opc;
		rq = pc->rq;
		pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
311 312 313 314
				(((pc->flags & PC_FLAG_TIMEDOUT) ?
				  DID_TIME_OUT :
				  DID_OK) << 16);
	} else if (pc->flags & PC_FLAG_TIMEDOUT) {
L
Linus Torvalds 已提交
315 316 317 318
		if (log)
			printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
					drive->name, pc->scsi_cmd->serial_number);
		pc->scsi_cmd->result = DID_TIME_OUT << 16;
319
	} else if (errors >= ERROR_MAX) {
L
Linus Torvalds 已提交
320 321 322
		pc->scsi_cmd->result = DID_ERROR << 16;
		if (log)
			printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
323
	} else if (errors) {
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		if (log)
			printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
		if (!idescsi_check_condition(drive, rq))
			/* we started a request sense, so we'll be back, exit for now */
			return 0;
		pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
	} else {
		pc->scsi_cmd->result = DID_OK << 16;
	}
	host = pc->scsi_cmd->device->host;
	spin_lock_irqsave(host->host_lock, flags);
	pc->done(pc->scsi_cmd);
	spin_unlock_irqrestore(host->host_lock, flags);
	kfree(pc);
	kfree(rq);
	scsi->pc = NULL;
	return 0;
}

343
static inline unsigned long get_timeout(struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
344 345 346 347 348 349
{
	return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
}

static int idescsi_expiry(ide_drive_t *drive)
{
350
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
351
	struct ide_atapi_pc   *pc   = scsi->pc;
L
Linus Torvalds 已提交
352 353 354 355

#if IDESCSI_DEBUG_LOG
	printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
#endif
356
	pc->flags |= PC_FLAG_TIMEDOUT;
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366

	return 0;					/* we do not want the ide subsystem to retry */
}

/*
 *	Our interrupt handler.
 */
static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
367
	ide_hwif_t *hwif = drive->hwif;
368
	struct ide_atapi_pc *pc = scsi->pc;
L
Linus Torvalds 已提交
369 370
	struct request *rq = pc->rq;
	unsigned int temp;
371
	u16 bcount;
372
	u8 stat, ireason;
L
Linus Torvalds 已提交
373 374 375 376 377

#if IDESCSI_DEBUG_LOG
	printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
#endif /* IDESCSI_DEBUG_LOG */

378
	if (pc->flags & PC_FLAG_TIMEDOUT) {
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386
#if IDESCSI_DEBUG_LOG
		printk(KERN_WARNING "idescsi_pc_intr: got timed out packet  %lu at %lu\n",
				pc->scsi_cmd->serial_number, jiffies);
#endif
		/* end this request now - scsi should retry it*/
		idescsi_end_request (drive, 1, 0);
		return ide_stopped;
	}
387 388
	if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
		pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
L
Linus Torvalds 已提交
389 390 391
#if IDESCSI_DEBUG_LOG
		printk ("ide-scsi: %s: DMA complete\n", drive->name);
#endif /* IDESCSI_DEBUG_LOG */
392
		pc->xferred = pc->req_xfer;
393
		(void)hwif->dma_ops->dma_end(drive);
L
Linus Torvalds 已提交
394 395 396
	}

	/* Clear the interrupt */
397
	stat = ide_read_status(drive);
L
Linus Torvalds 已提交
398

399
	if ((stat & DRQ_STAT) == 0) {
L
Linus Torvalds 已提交
400 401
		/* No more interrupts */
		if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
402 403
			printk(KERN_INFO "Packet command completed, %d bytes"
					" transferred\n", pc->xferred);
404
		local_irq_enable_in_hardirq();
405
		if (stat & ERR_STAT)
L
Linus Torvalds 已提交
406 407 408 409
			rq->errors++;
		idescsi_end_request (drive, 1, 0);
		return ide_stopped;
	}
410 411 412
	bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
		  hwif->INB(hwif->io_ports.lbam_addr);
	ireason = hwif->INB(hwif->io_ports.nsect_addr);
L
Linus Torvalds 已提交
413

414
	if (ireason & CD) {
L
Linus Torvalds 已提交
415 416 417
		printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
		return ide_do_reset (drive);
	}
418
	if (ireason & IO) {
419 420 421
		temp = pc->xferred + bcount;
		if (temp > pc->req_xfer) {
			if (temp > pc->buf_size) {
L
Linus Torvalds 已提交
422 423 424
				printk(KERN_ERR "ide-scsi: The scsi wants to "
					"send us more data than expected "
					"- discarding data\n");
425
				temp = pc->buf_size - pc->xferred;
L
Linus Torvalds 已提交
426
				if (temp) {
427
					pc->flags &= ~PC_FLAG_WRITING;
L
Linus Torvalds 已提交
428
					if (pc->sg)
429 430
						idescsi_input_buffers(drive, pc,
									temp);
L
Linus Torvalds 已提交
431
					else
432 433
						hwif->input_data(drive, NULL,
							pc->cur_pos, temp);
434 435 436
					printk(KERN_ERR "ide-scsi: transferred"
							" %d of %d bytes\n",
							temp, bcount);
L
Linus Torvalds 已提交
437
				}
438 439
				pc->xferred += temp;
				pc->cur_pos += temp;
440
				ide_atapi_discard_data(drive, bcount - temp);
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448
				ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
				return ide_started;
			}
#if IDESCSI_DEBUG_LOG
			printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
#endif /* IDESCSI_DEBUG_LOG */
		}
	}
449
	if (ireason & IO) {
450
		pc->flags &= ~PC_FLAG_WRITING;
L
Linus Torvalds 已提交
451
		if (pc->sg)
452
			idescsi_input_buffers(drive, pc, bcount);
L
Linus Torvalds 已提交
453
		else
454
			hwif->input_data(drive, NULL, pc->cur_pos, bcount);
L
Linus Torvalds 已提交
455
	} else {
456
		pc->flags |= PC_FLAG_WRITING;
L
Linus Torvalds 已提交
457
		if (pc->sg)
458
			idescsi_output_buffers(drive, pc, bcount);
L
Linus Torvalds 已提交
459
		else
460
			hwif->output_data(drive, NULL, pc->cur_pos, bcount);
L
Linus Torvalds 已提交
461 462
	}
	/* Update the current position */
463 464
	pc->xferred += bcount;
	pc->cur_pos += bcount;
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474

	/* And set the interrupt handler again */
	ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
	return ide_started;
}

static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
{
	ide_hwif_t *hwif = drive->hwif;
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
475
	struct ide_atapi_pc *pc = scsi->pc;
L
Linus Torvalds 已提交
476
	ide_startstop_t startstop;
477
	u8 ireason;
L
Linus Torvalds 已提交
478 479 480 481 482 483

	if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
		printk(KERN_ERR "ide-scsi: Strange, packet command "
			"initiated yet DRQ isn't asserted\n");
		return startstop;
	}
484
	ireason = hwif->INB(hwif->io_ports.nsect_addr);
485
	if ((ireason & CD) == 0 || (ireason & IO)) {
L
Linus Torvalds 已提交
486 487 488 489
		printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
				"issuing a packet command\n");
		return ide_do_reset (drive);
	}
E
Eric Sesterhenn 已提交
490
	BUG_ON(HWGROUP(drive)->handler != NULL);
L
Linus Torvalds 已提交
491 492
	/* Set the interrupt routine */
	ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
493

L
Linus Torvalds 已提交
494
	/* Send the actual packet */
495 496
	hwif->output_data(drive, NULL, scsi->pc->c, 12);

497 498
	if (pc->flags & PC_FLAG_DMA_OK) {
		pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
499
		hwif->dma_ops->dma_start(drive);
L
Linus Torvalds 已提交
500 501 502 503
	}
	return ide_started;
}

504
static inline int idescsi_set_direction(struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
505 506 507
{
	switch (pc->c[0]) {
		case READ_6: case READ_10: case READ_12:
508
			pc->flags &= ~PC_FLAG_WRITING;
L
Linus Torvalds 已提交
509 510
			return 0;
		case WRITE_6: case WRITE_10: case WRITE_12:
511
			pc->flags |= PC_FLAG_WRITING;
L
Linus Torvalds 已提交
512 513 514 515 516 517
			return 0;
		default:
			return 1;
	}
}

518
static int idescsi_map_sg(ide_drive_t *drive, struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
519 520 521 522 523
{
	ide_hwif_t *hwif = drive->hwif;
	struct scatterlist *sg, *scsi_sg;
	int segments;

524
	if (!pc->req_xfer || pc->req_xfer % 1024)
L
Linus Torvalds 已提交
525 526 527 528 529 530
		return 1;

	if (idescsi_set_direction(pc))
		return 1;

	sg = hwif->sg_table;
531 532
	scsi_sg = scsi_sglist(pc->scsi_cmd);
	segments = scsi_sg_count(pc->scsi_cmd);
L
Linus Torvalds 已提交
533 534 535 536

	if (segments > hwif->sg_max_nents)
		return 1;

537 538
	hwif->sg_nents = segments;
	memcpy(sg, scsi_sg, sizeof(*sg) * segments);
L
Linus Torvalds 已提交
539 540 541 542

	return 0;
}

543 544
static ide_startstop_t idescsi_issue_pc(ide_drive_t *drive,
		struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
545 546 547
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
	ide_hwif_t *hwif = drive->hwif;
548
	u16 bcount;
549
	u8 dma = 0;
L
Linus Torvalds 已提交
550

551 552 553 554 555
	/* Set the current packet command */
	scsi->pc = pc;
	/* We haven't transferred any data yet */
	pc->xferred = 0;
	pc->cur_pos = pc->buf;
556
	/* Request to transfer the entire buffer at once */
557
	bcount = min(pc->req_xfer, 63 * 1024);
L
Linus Torvalds 已提交
558 559 560

	if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
		hwif->sg_mapped = 1;
561
		dma = !hwif->dma_ops->dma_setup(drive);
L
Linus Torvalds 已提交
562 563 564
		hwif->sg_mapped = 0;
	}

565
	ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK, bcount, dma);
L
Linus Torvalds 已提交
566

567
	if (dma)
568
		pc->flags |= PC_FLAG_DMA_OK;
L
Linus Torvalds 已提交
569 570

	if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
571 572
		ide_execute_command(drive, WIN_PACKETCMD, &idescsi_transfer_pc,
				    get_timeout(pc), idescsi_expiry);
L
Linus Torvalds 已提交
573 574 575
		return ide_started;
	} else {
		/* Issue the packet command */
576
		hwif->OUTB(WIN_PACKETCMD, hwif->io_ports.command_addr);
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585 586
		return idescsi_transfer_pc(drive);
	}
}

/*
 *	idescsi_do_request is our request handling function.
 */
static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
{
#if IDESCSI_DEBUG_LOG
587
	printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
L
Linus Torvalds 已提交
588 589 590
	printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
#endif /* IDESCSI_DEBUG_LOG */

591
	if (blk_sense_request(rq) || blk_special_request(rq)) {
592 593
		return idescsi_issue_pc(drive,
				(struct ide_atapi_pc *) rq->special);
L
Linus Torvalds 已提交
594 595 596 597 598 599
	}
	blk_dump_rq_flags(rq, "ide-scsi: unsup command");
	idescsi_end_request (drive, 0, 0);
	return ide_stopped;
}

600
#ifdef CONFIG_IDE_PROC_FS
L
Linus Torvalds 已提交
601 602 603 604 605
static void idescsi_add_settings(ide_drive_t *drive)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);

/*
606
 *			drive	setting name	read/write	data type	min	max	mul_factor	div_factor	data pointer		set function
L
Linus Torvalds 已提交
607
 */
608 609 610 611 612
	ide_add_setting(drive,	"bios_cyl",	SETTING_RW,	TYPE_INT,	0,	1023,	1,		1,		&drive->bios_cyl,	NULL);
	ide_add_setting(drive,	"bios_head",	SETTING_RW,	TYPE_BYTE,	0,	255,	1,		1,		&drive->bios_head,	NULL);
	ide_add_setting(drive,	"bios_sect",	SETTING_RW,	TYPE_BYTE,	0,	63,	1,		1,		&drive->bios_sect,	NULL);
	ide_add_setting(drive,	"transform",	SETTING_RW,	TYPE_INT,	0,	3,	1,		1,		&scsi->transform,	NULL);
	ide_add_setting(drive,	"log",		SETTING_RW,	TYPE_INT,	0,	1,	1,		1,		&scsi->log,		NULL);
L
Linus Torvalds 已提交
613
}
614 615 616
#else
static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
#endif
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

/*
 *	Driver initialization.
 */
static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
{
	if (drive->id && (drive->id->config & 0x0060) == 0x20)
		set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
	clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
#if IDESCSI_DEBUG_LOG
	set_bit(IDESCSI_LOG_CMD, &scsi->log);
#endif /* IDESCSI_DEBUG_LOG */
	idescsi_add_settings(drive);
}

632
static void ide_scsi_remove(ide_drive_t *drive)
L
Linus Torvalds 已提交
633 634 635 636 637
{
	struct Scsi_Host *scsihost = drive->driver_data;
	struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
	struct gendisk *g = scsi->disk;

638
	scsi_remove_host(scsihost);
639
	ide_proc_unregister_driver(drive, scsi->driver);
L
Linus Torvalds 已提交
640 641 642 643 644 645 646 647 648 649

	ide_unregister_region(g);

	drive->driver_data = NULL;
	g->private_data = NULL;
	put_disk(g);

	ide_scsi_put(scsi);
}

650
static int ide_scsi_probe(ide_drive_t *);
L
Linus Torvalds 已提交
651

652
#ifdef CONFIG_IDE_PROC_FS
L
Linus Torvalds 已提交
653 654 655 656 657 658 659
static ide_proc_entry_t idescsi_proc[] = {
	{ "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
	{ NULL, 0, NULL, NULL }
};
#endif

static ide_driver_t idescsi_driver = {
660
	.gen_driver = {
661
		.owner		= THIS_MODULE,
662 663 664
		.name		= "ide-scsi",
		.bus		= &ide_bus_type,
	},
665 666
	.probe			= ide_scsi_probe,
	.remove			= ide_scsi_remove,
L
Linus Torvalds 已提交
667 668 669 670 671 672 673
	.version		= IDESCSI_VERSION,
	.media			= ide_scsi,
	.supports_dsc_overlap	= 0,
	.do_request		= idescsi_do_request,
	.end_request		= idescsi_end_request,
	.error                  = idescsi_atapi_error,
	.abort                  = idescsi_atapi_abort,
674 675 676
#ifdef CONFIG_IDE_PROC_FS
	.proc			= idescsi_proc,
#endif
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
};

static int idescsi_ide_open(struct inode *inode, struct file *filp)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct ide_scsi_obj *scsi;

	if (!(scsi = ide_scsi_get(disk)))
		return -ENXIO;

	return 0;
}

static int idescsi_ide_release(struct inode *inode, struct file *filp)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct ide_scsi_obj *scsi = ide_scsi_g(disk);

	ide_scsi_put(scsi);

	return 0;
}

static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
			unsigned int cmd, unsigned long arg)
{
	struct block_device *bdev = inode->i_bdev;
	struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
	return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
}

static struct block_device_operations idescsi_ops = {
	.owner		= THIS_MODULE,
	.open		= idescsi_ide_open,
	.release	= idescsi_ide_release,
	.ioctl		= idescsi_ide_ioctl,
};

static int idescsi_slave_configure(struct scsi_device * sdp)
{
	/* Configure detected device */
718 719
	sdp->use_10_for_rw = 1;
	sdp->use_10_for_ms = 1;
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
	return 0;
}

static const char *idescsi_info (struct Scsi_Host *host)
{
	return "SCSI host adapter emulation for IDE ATAPI devices";
}

static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
{
	idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);

	if (cmd == SG_SET_TRANSFORM) {
		if (arg)
			set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
		else
			clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
		return 0;
	} else if (cmd == SG_GET_TRANSFORM)
		return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
	return -EINVAL;
}

static int idescsi_queue (struct scsi_cmnd *cmd,
		void (*done)(struct scsi_cmnd *))
{
	struct Scsi_Host *host = cmd->device->host;
	idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
	ide_drive_t *drive = scsi->drive;
	struct request *rq = NULL;
751
	struct ide_atapi_pc *pc = NULL;
L
Linus Torvalds 已提交
752 753

	if (!drive) {
J
Jeff Garzik 已提交
754
		scmd_printk (KERN_ERR, cmd, "drive not present\n");
L
Linus Torvalds 已提交
755 756 757
		goto abort;
	}
	scsi = drive_to_idescsi(drive);
758 759
	pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
	rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
L
Linus Torvalds 已提交
760 761 762 763 764 765 766 767 768
	if (rq == NULL || pc == NULL) {
		printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
		goto abort;
	}

	memset (pc->c, 0, 12);
	pc->flags = 0;
	pc->rq = rq;
	memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
769
	pc->buf = NULL;
770
	pc->sg = scsi_sglist(cmd);
771
	pc->sg_cnt = scsi_sg_count(cmd);
L
Linus Torvalds 已提交
772
	pc->b_count = 0;
773
	pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
L
Linus Torvalds 已提交
774 775 776 777 778 779
	pc->scsi_cmd = cmd;
	pc->done = done;
	pc->timeout = jiffies + cmd->timeout_per_command;

	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
780
		ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
L
Linus Torvalds 已提交
781 782
		if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
			printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
783
			ide_scsi_hex_dump(pc->c, 12);
L
Linus Torvalds 已提交
784 785 786 787 788
		}
	}

	ide_init_drive_cmd (rq);
	rq->special = (char *) pc;
789
	rq->cmd_type = REQ_TYPE_SPECIAL;
L
Linus Torvalds 已提交
790 791 792 793 794 795
	spin_unlock_irq(host->host_lock);
	rq->rq_disk = scsi->disk;
	(void) ide_do_drive_cmd (drive, rq, ide_end);
	spin_lock_irq(host->host_lock);
	return 0;
abort:
J
Jesper Juhl 已提交
796 797
	kfree (pc);
	kfree (rq);
L
Linus Torvalds 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	cmd->result = DID_ERROR << 16;
	done(cmd);
	return 0;
}

static int idescsi_eh_abort (struct scsi_cmnd *cmd)
{
	idescsi_scsi_t *scsi  = scsihost_to_idescsi(cmd->device->host);
	ide_drive_t    *drive = scsi->drive;
	int		busy;
	int             ret   = FAILED;

	/* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */

	if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);

	if (!drive) {
		printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
		WARN_ON(1);
		goto no_drive;
	}

	/* First give it some more time, how much is "right" is hard to say :-( */

	busy = ide_wait_not_busy(HWIF(drive), 100);	/* FIXME - uses mdelay which causes latency? */
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");

	spin_lock_irq(&ide_lock);

	/* If there is no pc running we're done (our interrupt took care of it) */
	if (!scsi->pc) {
		ret = SUCCESS;
		goto ide_unlock;
	}

	/* It's somewhere in flight. Does ide subsystem agree? */
	if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
	    elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
		/*
		 * FIXME - not sure this condition can ever occur
		 */
		printk (KERN_ERR "ide-scsi: cmd aborted!\n");

843
		if (blk_sense_request(scsi->pc->rq))
844
			kfree(scsi->pc->buf);
L
Linus Torvalds 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		kfree(scsi->pc->rq);
		kfree(scsi->pc);
		scsi->pc = NULL;

		ret = SUCCESS;
	}

ide_unlock:
	spin_unlock_irq(&ide_lock);
no_drive:
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");

	return ret;
}

static int idescsi_eh_reset (struct scsi_cmnd *cmd)
{
	struct request *req;
	idescsi_scsi_t *scsi  = scsihost_to_idescsi(cmd->device->host);
	ide_drive_t    *drive = scsi->drive;
	int             ready = 0;
	int             ret   = SUCCESS;

	/* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */

	if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);

	if (!drive) {
		printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
		WARN_ON(1);
		return FAILED;
	}

880 881
	spin_lock_irq(cmd->device->host->host_lock);
	spin_lock(&ide_lock);
L
Linus Torvalds 已提交
882 883 884 885

	if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
		printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
		spin_unlock(&ide_lock);
886
		spin_unlock_irq(cmd->device->host->host_lock);
L
Linus Torvalds 已提交
887 888 889 890
		return FAILED;
	}

	/* kill current request */
891 892
	if (__blk_end_request(req, -EIO, 0))
		BUG();
893
	if (blk_sense_request(req))
894
		kfree(scsi->pc->buf);
L
Linus Torvalds 已提交
895 896 897 898 899 900
	kfree(scsi->pc);
	scsi->pc = NULL;
	kfree(req);

	/* now nuke the drive queue */
	while ((req = elv_next_request(drive->queue))) {
901 902
		if (__blk_end_request(req, -EIO, 0))
			BUG();
L
Linus Torvalds 已提交
903 904 905 906 907
	}

	HWGROUP(drive)->rq = NULL;
	HWGROUP(drive)->handler = NULL;
	HWGROUP(drive)->busy = 1;		/* will set this to zero when ide reset finished */
908
	spin_unlock(&ide_lock);
L
Linus Torvalds 已提交
909 910 911 912 913 914 915

	ide_do_reset(drive);

	/* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */

	do {
		spin_unlock_irq(cmd->device->host->host_lock);
916
		msleep(50);
L
Linus Torvalds 已提交
917 918 919 920 921 922 923 924 925 926
		spin_lock_irq(cmd->device->host->host_lock);
	} while ( HWGROUP(drive)->handler );

	ready = drive_is_ready(drive);
	HWGROUP(drive)->busy--;
	if (!ready) {
		printk (KERN_ERR "ide-scsi: reset failed!\n");
		ret = FAILED;
	}

927
	spin_unlock_irq(cmd->device->host->host_lock);
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	return ret;
}

static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
		sector_t capacity, int *parm)
{
	idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
	ide_drive_t *drive = idescsi->drive;

	if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
		parm[0] = drive->bios_head;
		parm[1] = drive->bios_sect;
		parm[2] = drive->bios_cyl;
	}
	return 0;
}

static struct scsi_host_template idescsi_template = {
	.module			= THIS_MODULE,
	.name			= "idescsi",
	.info			= idescsi_info,
	.slave_configure        = idescsi_slave_configure,
	.ioctl			= idescsi_ioctl,
	.queuecommand		= idescsi_queue,
	.eh_abort_handler	= idescsi_eh_abort,
	.eh_host_reset_handler  = idescsi_eh_reset,
	.bios_param		= idescsi_bios,
	.can_queue		= 40,
	.this_id		= -1,
	.sg_tablesize		= 256,
	.cmd_per_lun		= 5,
	.max_sectors		= 128,
	.use_clustering		= DISABLE_CLUSTERING,
	.emulated		= 1,
	.proc_name		= "ide-scsi",
};

965
static int ide_scsi_probe(ide_drive_t *drive)
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973 974 975 976 977
{
	idescsi_scsi_t *idescsi;
	struct Scsi_Host *host;
	struct gendisk *g;
	static int warned;
	int err = -ENOMEM;

	if (!warned && drive->media == ide_cdrom) {
		printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
		warned = 1;
	}

978 979 980
	if (idescsi_nocd && drive->media == ide_cdrom)
		return -ENODEV;

L
Linus Torvalds 已提交
981 982 983 984
	if (!strstr("ide-scsi", drive->driver_req) ||
	    !drive->present ||
	    drive->media == ide_disk ||
	    !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
985
		return -ENODEV;
L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

	g = alloc_disk(1 << PARTN_BITS);
	if (!g)
		goto out_host_put;

	ide_init_disk(g, drive);

	host->max_id = 1;

#if IDESCSI_DEBUG_LOG
	if (drive->id->last_lun)
		printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
#endif
	if ((drive->id->last_lun & 0x7) != 7)
		host->max_lun = (drive->id->last_lun & 0x7) + 1;
	else
		host->max_lun = 1;

	drive->driver_data = host;
	idescsi = scsihost_to_idescsi(host);
	idescsi->drive = drive;
	idescsi->driver = &idescsi_driver;
	idescsi->host = host;
	idescsi->disk = g;
	g->private_data = &idescsi->driver;
1011
	ide_proc_register_driver(drive, &idescsi_driver);
1012 1013 1014 1015 1016
	err = 0;
	idescsi_setup(drive, idescsi);
	g->fops = &idescsi_ops;
	ide_register_region(g);
	err = scsi_add_host(host, &drive->gendev);
L
Linus Torvalds 已提交
1017
	if (!err) {
1018 1019
		scsi_scan_host(host);
		return 0;
L
Linus Torvalds 已提交
1020
	}
1021 1022
	/* fall through on error */
	ide_unregister_region(g);
1023
	ide_proc_unregister_driver(drive, &idescsi_driver);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032

	put_disk(g);
out_host_put:
	scsi_host_put(host);
	return err;
}

static int __init init_idescsi_module(void)
{
1033
	return driver_register(&idescsi_driver.gen_driver);
L
Linus Torvalds 已提交
1034 1035 1036 1037
}

static void __exit exit_idescsi_module(void)
{
1038
	driver_unregister(&idescsi_driver.gen_driver);
L
Linus Torvalds 已提交
1039 1040
}

1041 1042
module_param(idescsi_nocd, int, 0600);
MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
L
Linus Torvalds 已提交
1043 1044 1045
module_init(init_idescsi_module);
module_exit(exit_idescsi_module);
MODULE_LICENSE("GPL");