ide-scsi.c 22.1 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
 */

/*
 * 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/slab.h>
#include <linux/ide.h>
#include <linux/scatterlist.h>
46
#include <linux/delay.h>
47
#include <linux/mutex.h>
J
Jiri Slaby 已提交
48
#include <linux/bitops.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61

#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

62 63 64 65 66 67 68
#if IDESCSI_DEBUG_LOG
#define debug_log(fmt, args...) \
	printk(KERN_INFO "ide-scsi: " fmt, ## args)
#else
#define debug_log(fmt, args...) do {} while (0)
#endif

L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/*
 *	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;

	unsigned long transform;		/* SCSI cmd translation layer */
	unsigned long log;			/* log flags */
} idescsi_scsi_t;

89
static DEFINE_MUTEX(idescsi_ref_mutex);
90 91
/* Set by module param to skip cd */
static int idescsi_nocd;
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99

#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;

100
	mutex_lock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
101
	scsi = ide_scsi_g(disk);
102
	if (scsi) {
103
		if (ide_device_get(scsi->drive))
104
			scsi = NULL;
105 106
		else
			scsi_host_get(scsi->host);
107
	}
108
	mutex_unlock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
109 110 111 112 113
	return scsi;
}

static void ide_scsi_put(struct ide_scsi_obj *scsi)
{
114 115
	ide_drive_t *drive = scsi->drive;

116
	mutex_lock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
117
	scsi_host_put(scsi->host);
118
	ide_device_put(drive);
119
	mutex_unlock(&idescsi_ref_mutex);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131
}

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);
}

132 133 134 135 136
static void ide_scsi_hex_dump(u8 *data, int len)
{
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
}

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

139
static void ide_scsi_callback(ide_drive_t *drive, int dsc)
140 141
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
142
	struct ide_atapi_pc *pc = drive->pc;
143 144 145 146 147 148 149 150 151 152 153 154

	if (pc->flags & PC_FLAG_TIMEDOUT)
		debug_log("%s: got timed out packet %lu at %lu\n", __func__,
			  pc->scsi_cmd->serial_number, jiffies);
		/* end this request now - scsi should retry it*/
	else if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk(KERN_INFO "Packet command completed, %d bytes"
				 " transferred\n", pc->xferred);

	idescsi_end_request(drive, 1, 0);
}

155 156
static int idescsi_check_condition(ide_drive_t *drive,
		struct request *failed_cmd)
L
Linus Torvalds 已提交
157 158
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
159
	struct ide_atapi_pc   *pc;
L
Linus Torvalds 已提交
160 161 162 163
	struct request *rq;
	u8             *buf;

	/* stuff a sense request in front of our current request */
164
	pc = kzalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
165
	rq = blk_get_request(drive->queue, READ, GFP_ATOMIC);
166 167
	buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
	if (!pc || !rq || !buf) {
J
Jesper Juhl 已提交
168
		kfree(buf);
169 170
		if (rq)
			blk_put_request(rq);
J
Jesper Juhl 已提交
171
		kfree(pc);
L
Linus Torvalds 已提交
172 173 174 175
		return -ENOMEM;
	}
	rq->special = (char *) pc;
	pc->rq = rq;
176
	pc->buf = buf;
L
Linus Torvalds 已提交
177
	pc->c[0] = REQUEST_SENSE;
178
	pc->c[4] = pc->req_xfer = pc->buf_size = SCSI_SENSE_BUFFERSIZE;
179
	rq->cmd_type = REQ_TYPE_SENSE;
180
	rq->cmd_flags |= REQ_PREEMPT;
L
Linus Torvalds 已提交
181 182
	pc->timeout = jiffies + WAIT_READY;
	/* NOTE! Save the failed packet command in "rq->buffer" */
183 184
	rq->buffer = (void *) failed_cmd->special;
	pc->scsi_cmd = ((struct ide_atapi_pc *) failed_cmd->special)->scsi_cmd;
L
Linus Torvalds 已提交
185 186
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: queue cmd = ", drive->name);
187
		ide_scsi_hex_dump(pc->c, 6);
L
Linus Torvalds 已提交
188 189
	}
	rq->rq_disk = scsi->disk;
190
	rq->ref_count++;
191
	memcpy(rq->cmd, pc->c, 12);
192 193
	ide_do_drive_cmd(drive, rq);
	return 0;
L
Linus Torvalds 已提交
194 195 196 197 198
}

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

201
	if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
L
Linus Torvalds 已提交
202
		/* force an abort */
203
		hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215

	rq->errors++;

	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;
216
	struct ide_atapi_pc *pc = (struct ide_atapi_pc *) rq->special;
L
Linus Torvalds 已提交
217 218
	int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
	struct Scsi_Host *host;
219
	int errors = rq->errors;
L
Linus Torvalds 已提交
220 221
	unsigned long flags;

222
	if (!blk_special_request(rq) && !blk_sense_request(rq)) {
L
Linus Torvalds 已提交
223 224 225 226
		ide_end_request(drive, uptodate, nrsecs);
		return 0;
	}
	ide_end_drive_cmd (drive, 0, 0);
227
	if (blk_sense_request(rq)) {
228
		struct ide_atapi_pc *opc = (struct ide_atapi_pc *) rq->buffer;
L
Linus Torvalds 已提交
229 230
		if (log) {
			printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
231
			ide_scsi_hex_dump(pc->buf, 16);
L
Linus Torvalds 已提交
232
		}
233 234 235
		memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buf,
			SCSI_SENSE_BUFFERSIZE);
		kfree(pc->buf);
L
Linus Torvalds 已提交
236
		kfree(pc);
237
		blk_put_request(rq);
L
Linus Torvalds 已提交
238 239 240
		pc = opc;
		rq = pc->rq;
		pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
241 242 243 244
				(((pc->flags & PC_FLAG_TIMEDOUT) ?
				  DID_TIME_OUT :
				  DID_OK) << 16);
	} else if (pc->flags & PC_FLAG_TIMEDOUT) {
L
Linus Torvalds 已提交
245 246 247 248
		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;
249
	} else if (errors >= ERROR_MAX) {
L
Linus Torvalds 已提交
250 251 252
		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);
253
	} else if (errors) {
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
		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);
268
	blk_put_request(rq);
269
	drive->pc = NULL;
L
Linus Torvalds 已提交
270 271 272
	return 0;
}

273
static inline int idescsi_set_direction(struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
274 275 276
{
	switch (pc->c[0]) {
		case READ_6: case READ_10: case READ_12:
277
			pc->flags &= ~PC_FLAG_WRITING;
L
Linus Torvalds 已提交
278 279
			return 0;
		case WRITE_6: case WRITE_10: case WRITE_12:
280
			pc->flags |= PC_FLAG_WRITING;
L
Linus Torvalds 已提交
281 282 283 284 285 286
			return 0;
		default:
			return 1;
	}
}

287
static int idescsi_map_sg(ide_drive_t *drive, struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
288 289 290 291 292
{
	ide_hwif_t *hwif = drive->hwif;
	struct scatterlist *sg, *scsi_sg;
	int segments;

293
	if (!pc->req_xfer || pc->req_xfer % 1024)
L
Linus Torvalds 已提交
294 295 296 297 298 299
		return 1;

	if (idescsi_set_direction(pc))
		return 1;

	sg = hwif->sg_table;
300 301
	scsi_sg = scsi_sglist(pc->scsi_cmd);
	segments = scsi_sg_count(pc->scsi_cmd);
L
Linus Torvalds 已提交
302 303 304 305

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

306 307
	hwif->sg_nents = segments;
	memcpy(sg, scsi_sg, sizeof(*sg) * segments);
L
Linus Torvalds 已提交
308 309 310 311

	return 0;
}

312 313
static ide_startstop_t idescsi_issue_pc(ide_drive_t *drive,
		struct ide_atapi_pc *pc)
L
Linus Torvalds 已提交
314
{
315
	/* Set the current packet command */
316
	drive->pc = pc;
L
Linus Torvalds 已提交
317

318
	return ide_issue_pc(drive, ide_scsi_get_timeout(pc), ide_scsi_expiry);
L
Linus Torvalds 已提交
319 320 321 322 323 324 325
}

/*
 *	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)
{
326 327 328 329
	debug_log("dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,
		  rq->cmd[0], rq->errors);
	debug_log("sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
		  rq->sector, rq->nr_sectors, rq->current_nr_sectors);
L
Linus Torvalds 已提交
330

331
	if (blk_sense_request(rq) || blk_special_request(rq)) {
332 333
		struct ide_atapi_pc *pc = (struct ide_atapi_pc *)rq->special;

B
Bartlomiej Zolnierkiewicz 已提交
334 335
		if ((drive->dev_flags & IDE_DFLAG_USING_DMA) &&
		    idescsi_map_sg(drive, pc) == 0)
336 337 338
			pc->flags |= PC_FLAG_DMA_OK;

		return idescsi_issue_pc(drive, pc);
L
Linus Torvalds 已提交
339 340 341 342 343 344
	}
	blk_dump_rq_flags(rq, "ide-scsi: unsup command");
	idescsi_end_request (drive, 0, 0);
	return ide_stopped;
}

345
#ifdef CONFIG_IDE_PROC_FS
346 347 348 349 350
static ide_proc_entry_t idescsi_proc[] = {
	{ "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
	{ NULL, 0, NULL, NULL }
};

351 352 353 354 355 356
#define ide_scsi_devset_get(name, field) \
static int get_##name(ide_drive_t *drive) \
{ \
	idescsi_scsi_t *scsi = drive_to_idescsi(drive); \
	return scsi->field; \
}
L
Linus Torvalds 已提交
357

358 359 360 361 362 363
#define ide_scsi_devset_set(name, field) \
static int set_##name(ide_drive_t *drive, int arg) \
{ \
	idescsi_scsi_t *scsi = drive_to_idescsi(drive); \
	scsi->field = arg; \
	return 0; \
L
Linus Torvalds 已提交
364
}
365

366
#define ide_scsi_devset_rw_field(_name, _field) \
367 368
ide_scsi_devset_get(_name, _field); \
ide_scsi_devset_set(_name, _field); \
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name);

ide_devset_rw_field(bios_cyl, bios_cyl);
ide_devset_rw_field(bios_head, bios_head);
ide_devset_rw_field(bios_sect, bios_sect);

ide_scsi_devset_rw_field(transform, transform);
ide_scsi_devset_rw_field(log, log);

static const struct ide_proc_devset idescsi_settings[] = {
	IDE_PROC_DEVSET(bios_cyl,  0, 1023),
	IDE_PROC_DEVSET(bios_head, 0,  255),
	IDE_PROC_DEVSET(bios_sect, 0,	63),
	IDE_PROC_DEVSET(log,	   0,	 1),
	IDE_PROC_DEVSET(transform, 0,	 3),
	{ 0 },
385
};
386 387 388 389 390 391 392 393 394 395

static ide_proc_entry_t *ide_scsi_proc_entries(ide_drive_t *drive)
{
	return idescsi_proc;
}

static const struct ide_proc_devset *ide_scsi_proc_devsets(ide_drive_t *drive)
{
	return idescsi_settings;
}
396
#endif
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406

/*
 *	Driver initialization.
 */
static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
{
	clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
#if IDESCSI_DEBUG_LOG
	set_bit(IDESCSI_LOG_CMD, &scsi->log);
#endif /* IDESCSI_DEBUG_LOG */
407

408 409 410
	drive->pc_callback	 = ide_scsi_callback;
	drive->pc_update_buffers = NULL;
	drive->pc_io_buffers	 = ide_io_buffers;
411

412
	ide_proc_register_driver(drive, scsi->driver);
L
Linus Torvalds 已提交
413 414
}

415
static void ide_scsi_remove(ide_drive_t *drive)
L
Linus Torvalds 已提交
416 417 418 419 420
{
	struct Scsi_Host *scsihost = drive->driver_data;
	struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
	struct gendisk *g = scsi->disk;

421
	scsi_remove_host(scsihost);
422
	ide_proc_unregister_driver(drive, scsi->driver);
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430

	ide_unregister_region(g);

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

	ide_scsi_put(scsi);
431

B
Bartlomiej Zolnierkiewicz 已提交
432
	drive->dev_flags &= ~IDE_DFLAG_SCSI;
L
Linus Torvalds 已提交
433 434
}

435
static int ide_scsi_probe(ide_drive_t *);
L
Linus Torvalds 已提交
436 437

static ide_driver_t idescsi_driver = {
438
	.gen_driver = {
439
		.owner		= THIS_MODULE,
440 441 442
		.name		= "ide-scsi",
		.bus		= &ide_bus_type,
	},
443 444
	.probe			= ide_scsi_probe,
	.remove			= ide_scsi_remove,
L
Linus Torvalds 已提交
445 446 447 448
	.version		= IDESCSI_VERSION,
	.do_request		= idescsi_do_request,
	.end_request		= idescsi_end_request,
	.error                  = idescsi_atapi_error,
449
#ifdef CONFIG_IDE_PROC_FS
450 451
	.proc_entries		= ide_scsi_proc_entries,
	.proc_devsets		= ide_scsi_proc_devsets,
452
#endif
L
Linus Torvalds 已提交
453 454
};

A
Al Viro 已提交
455
static int idescsi_ide_open(struct block_device *bdev, fmode_t mode)
L
Linus Torvalds 已提交
456
{
A
Al Viro 已提交
457
	struct ide_scsi_obj *scsi = ide_scsi_get(bdev->bd_disk);
L
Linus Torvalds 已提交
458

A
Al Viro 已提交
459
	if (!scsi)
L
Linus Torvalds 已提交
460 461 462 463 464
		return -ENXIO;

	return 0;
}

A
Al Viro 已提交
465
static int idescsi_ide_release(struct gendisk *disk, fmode_t mode)
L
Linus Torvalds 已提交
466
{
A
Al Viro 已提交
467
	ide_scsi_put(ide_scsi_g(disk));
L
Linus Torvalds 已提交
468 469 470
	return 0;
}

A
Al Viro 已提交
471
static int idescsi_ide_ioctl(struct block_device *bdev, fmode_t mode,
L
Linus Torvalds 已提交
472 473 474
			unsigned int cmd, unsigned long arg)
{
	struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
475
	return generic_ide_ioctl(scsi->drive, bdev, cmd, arg);
L
Linus Torvalds 已提交
476 477 478 479
}

static struct block_device_operations idescsi_ops = {
	.owner		= THIS_MODULE,
A
Al Viro 已提交
480 481 482
	.open		= idescsi_ide_open,
	.release	= idescsi_ide_release,
	.locked_ioctl	= idescsi_ide_ioctl,
L
Linus Torvalds 已提交
483 484 485 486 487
};

static int idescsi_slave_configure(struct scsi_device * sdp)
{
	/* Configure detected device */
488 489
	sdp->use_10_for_rw = 1;
	sdp->use_10_for_ms = 1;
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	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;
521
	struct ide_atapi_pc *pc = NULL;
522
	int write = cmd->sc_data_direction == DMA_TO_DEVICE;
L
Linus Torvalds 已提交
523 524

	if (!drive) {
J
Jeff Garzik 已提交
525
		scmd_printk (KERN_ERR, cmd, "drive not present\n");
L
Linus Torvalds 已提交
526 527 528
		goto abort;
	}
	scsi = drive_to_idescsi(drive);
529
	pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
530
	rq = blk_get_request(drive->queue, write, GFP_ATOMIC);
L
Linus Torvalds 已提交
531 532 533 534 535 536 537
	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;
538 539
	if (cmd->sc_data_direction == DMA_TO_DEVICE)
		pc->flags |= PC_FLAG_WRITING;
L
Linus Torvalds 已提交
540 541
	pc->rq = rq;
	memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
542
	pc->buf = NULL;
543
	pc->sg = scsi_sglist(cmd);
544
	pc->sg_cnt = scsi_sg_count(cmd);
L
Linus Torvalds 已提交
545
	pc->b_count = 0;
546
	pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
L
Linus Torvalds 已提交
547 548
	pc->scsi_cmd = cmd;
	pc->done = done;
J
Jens Axboe 已提交
549
	pc->timeout = jiffies + cmd->request->timeout;
L
Linus Torvalds 已提交
550 551 552

	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
553
		ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
L
Linus Torvalds 已提交
554 555
		if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
			printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
556
			ide_scsi_hex_dump(pc->c, 12);
L
Linus Torvalds 已提交
557 558 559 560
		}
	}

	rq->special = (char *) pc;
561
	rq->cmd_type = REQ_TYPE_SPECIAL;
L
Linus Torvalds 已提交
562
	spin_unlock_irq(host->host_lock);
563
	rq->ref_count++;
564
	memcpy(rq->cmd, pc->c, 12);
565
	blk_execute_rq_nowait(drive->queue, scsi->disk, rq, 0, NULL);
L
Linus Torvalds 已提交
566 567 568
	spin_lock_irq(host->host_lock);
	return 0;
abort:
J
Jesper Juhl 已提交
569
	kfree (pc);
570 571
	if (rq)
		blk_put_request(rq);
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580
	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;
581 582
	ide_hwif_t     *hwif;
	ide_hwgroup_t  *hwgroup;
L
Linus Torvalds 已提交
583 584 585
	int		busy;
	int             ret   = FAILED;

586 587
	struct ide_atapi_pc *pc;

L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595 596 597 598
	/* 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;
	}

599 600
	hwif = drive->hwif;
	hwgroup = hwif->hwgroup;
L
Linus Torvalds 已提交
601

602 603 604
	/* First give it some more time, how much is "right" is hard to say :-(
	   FIXME - uses mdelay which causes latency? */
	busy = ide_wait_not_busy(hwif, 100);
L
Linus Torvalds 已提交
605 606 607
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
		printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");

608
	spin_lock_irq(&hwgroup->lock);
L
Linus Torvalds 已提交
609 610

	/* If there is no pc running we're done (our interrupt took care of it) */
611 612
	pc = drive->pc;
	if (pc == NULL) {
L
Linus Torvalds 已提交
613 614 615 616 617
		ret = SUCCESS;
		goto ide_unlock;
	}

	/* It's somewhere in flight. Does ide subsystem agree? */
618 619
	if (pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
	    elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != pc->rq) {
L
Linus Torvalds 已提交
620 621 622 623 624
		/*
		 * FIXME - not sure this condition can ever occur
		 */
		printk (KERN_ERR "ide-scsi: cmd aborted!\n");

625 626
		if (blk_sense_request(pc->rq))
			kfree(pc->buf);
627
		/* we need to call blk_put_request twice. */
628 629 630 631
		blk_put_request(pc->rq);
		blk_put_request(pc->rq);
		kfree(pc);
		drive->pc = NULL;
L
Linus Torvalds 已提交
632 633 634 635 636

		ret = SUCCESS;
	}

ide_unlock:
637
	spin_unlock_irq(&hwgroup->lock);
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649
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;
650
	ide_hwgroup_t  *hwgroup;
L
Linus Torvalds 已提交
651 652 653
	int             ready = 0;
	int             ret   = SUCCESS;

654 655
	struct ide_atapi_pc *pc;

L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666
	/* 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;
	}

667 668
	hwgroup = drive->hwif->hwgroup;

669
	spin_lock_irq(cmd->device->host->host_lock);
670
	spin_lock(&hwgroup->lock);
L
Linus Torvalds 已提交
671

672
	pc = drive->pc;
673 674
	if (pc)
		req = pc->rq;
675

676
	if (pc == NULL || req != hwgroup->rq || hwgroup->handler == NULL) {
L
Linus Torvalds 已提交
677
		printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
678
		spin_unlock(&hwgroup->lock);
679
		spin_unlock_irq(cmd->device->host->host_lock);
L
Linus Torvalds 已提交
680 681 682 683
		return FAILED;
	}

	/* kill current request */
684 685
	if (__blk_end_request(req, -EIO, 0))
		BUG();
686
	if (blk_sense_request(req))
687 688 689
		kfree(pc->buf);
	kfree(pc);
	drive->pc = NULL;
690
	blk_put_request(req);
L
Linus Torvalds 已提交
691 692 693

	/* now nuke the drive queue */
	while ((req = elv_next_request(drive->queue))) {
694 695
		if (__blk_end_request(req, -EIO, 0))
			BUG();
L
Linus Torvalds 已提交
696 697
	}

698 699 700 701
	hwgroup->rq = NULL;
	hwgroup->handler = NULL;
	hwgroup->busy = 1; /* will set this to zero when ide reset finished */
	spin_unlock(&hwgroup->lock);
L
Linus Torvalds 已提交
702 703 704 705 706 707 708

	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);
709
		msleep(50);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717 718 719
		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;
	}

720
	spin_unlock_irq(cmd->device->host->host_lock);
L
Linus Torvalds 已提交
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 751 752 753 754 755 756 757
	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",
};

758
static int ide_scsi_probe(ide_drive_t *drive)
L
Linus Torvalds 已提交
759 760 761 762 763 764
{
	idescsi_scsi_t *idescsi;
	struct Scsi_Host *host;
	struct gendisk *g;
	static int warned;
	int err = -ENOMEM;
765
	u16 last_lun;
L
Linus Torvalds 已提交
766 767 768 769 770 771

	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;
	}

772 773 774
	if (idescsi_nocd && drive->media == ide_cdrom)
		return -ENODEV;

L
Linus Torvalds 已提交
775 776 777
	if (!strstr("ide-scsi", drive->driver_req) ||
	    drive->media == ide_disk ||
	    !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
778
		return -ENODEV;
L
Linus Torvalds 已提交
779

B
Bartlomiej Zolnierkiewicz 已提交
780
	drive->dev_flags |= IDE_DFLAG_SCSI;
781

L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789
	g = alloc_disk(1 << PARTN_BITS);
	if (!g)
		goto out_host_put;

	ide_init_disk(g, drive);

	host->max_id = 1;

790 791 792
	last_lun = drive->id[ATA_ID_LAST_LUN];
	if (last_lun)
		debug_log("%s: last_lun=%u\n", drive->name, last_lun);
793

794 795
	if ((last_lun & 7) != 7)
		host->max_lun = (last_lun & 7) + 1;
L
Linus Torvalds 已提交
796 797 798 799 800 801 802 803 804 805
	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;
806 807 808 809 810
	err = 0;
	idescsi_setup(drive, idescsi);
	g->fops = &idescsi_ops;
	ide_register_region(g);
	err = scsi_add_host(host, &drive->gendev);
L
Linus Torvalds 已提交
811
	if (!err) {
812 813
		scsi_scan_host(host);
		return 0;
L
Linus Torvalds 已提交
814
	}
815 816
	/* fall through on error */
	ide_unregister_region(g);
817
	ide_proc_unregister_driver(drive, &idescsi_driver);
L
Linus Torvalds 已提交
818 819 820

	put_disk(g);
out_host_put:
B
Bartlomiej Zolnierkiewicz 已提交
821
	drive->dev_flags &= ~IDE_DFLAG_SCSI;
L
Linus Torvalds 已提交
822 823 824 825 826 827
	scsi_host_put(host);
	return err;
}

static int __init init_idescsi_module(void)
{
828
	return driver_register(&idescsi_driver.gen_driver);
L
Linus Torvalds 已提交
829 830 831 832
}

static void __exit exit_idescsi_module(void)
{
833
	driver_unregister(&idescsi_driver.gen_driver);
L
Linus Torvalds 已提交
834 835
}

836 837
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 已提交
838 839 840
module_init(init_idescsi_module);
module_exit(exit_idescsi_module);
MODULE_LICENSE("GPL");