ide-scsi.c 27.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
3
 * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
 * Copyright (C) 2004-2005  Bartlomiej Zolnierkiewicz
Linus Torvalds's avatar
Linus Torvalds committed
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>
Jiri Slaby's avatar
Jiri Slaby committed
49
#include <linux/bitops.h>
Linus Torvalds's avatar
Linus Torvalds committed
50
51
52
53
54
55
56
57
58
59
60
61
62

#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

63
64
65
66
67
68
69
#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

Linus Torvalds's avatar
Linus Torvalds committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 *	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;

86
	struct ide_atapi_pc *pc;		/* Current packet command */
Linus Torvalds's avatar
Linus Torvalds committed
87
88
89
90
91
	unsigned long flags;			/* Status/Action flags */
	unsigned long transform;		/* SCSI cmd translation layer */
	unsigned long log;			/* log flags */
} idescsi_scsi_t;

92
static DEFINE_MUTEX(idescsi_ref_mutex);
93
94
/* Set by module param to skip cd */
static int idescsi_nocd;
Linus Torvalds's avatar
Linus Torvalds committed
95
96
97
98
99
100
101
102

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

103
	mutex_lock(&idescsi_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
104
105
106
	scsi = ide_scsi_g(disk);
	if (scsi)
		scsi_host_get(scsi->host);
107
	mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
108
109
110
111
112
	return scsi;
}

static void ide_scsi_put(struct ide_scsi_obj *scsi)
{
113
	mutex_lock(&idescsi_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
114
	scsi_host_put(scsi->host);
115
	mutex_unlock(&idescsi_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}

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

/*
139
 *	PIO data transfer routine using the scatter gather table.
Linus Torvalds's avatar
Linus Torvalds committed
140
 */
141
142
static void ide_scsi_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
				unsigned int bcount, int write)
Linus Torvalds's avatar
Linus Torvalds committed
143
{
144
	ide_hwif_t *hwif = drive->hwif;
145
	xfer_func_t *xf = write ? hwif->output_data : hwif->input_data;
Linus Torvalds's avatar
Linus Torvalds committed
146
147
148
149
150
	char *buf;
	int count;

	while (bcount) {
		count = min(pc->sg->length - pc->b_count, bcount);
151
		if (PageHighMem(sg_page(pc->sg))) {
152
153
154
			unsigned long flags;

			local_irq_save(flags);
155
			buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
156
157
					  pc->sg->offset;
			xf(drive, NULL, buf + pc->b_count, count);
158
159
160
			kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
			local_irq_restore(flags);
		} else {
161
			buf = sg_virt(pc->sg);
162
			xf(drive, NULL, buf + pc->b_count, count);
163
164
		}
		bcount -= count; pc->b_count += count;
Linus Torvalds's avatar
Linus Torvalds committed
165
		if (pc->b_count == pc->sg->length) {
166
			if (!--pc->sg_cnt)
Jens Axboe's avatar
Jens Axboe committed
167
168
				break;
			pc->sg = sg_next(pc->sg);
Linus Torvalds's avatar
Linus Torvalds committed
169
170
171
			pc->b_count = 0;
		}
	}
Jens Axboe's avatar
Jens Axboe committed
172
173

	if (bcount) {
174
175
176
177
		printk(KERN_ERR "%s: scatter gather table too small, %s\n",
				drive->name, write ? "padding with zeros"
						   : "discarding data");
		ide_pad_transfer(drive, write, bcount);
Jens Axboe's avatar
Jens Axboe committed
178
	}
Linus Torvalds's avatar
Linus Torvalds committed
179
180
}

181
182
183
184
185
static void ide_scsi_hex_dump(u8 *data, int len)
{
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, data, len, 0);
}

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
static int idescsi_end_request(ide_drive_t *, int, int);

static void ide_scsi_callback(ide_drive_t *drive)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
	struct ide_atapi_pc *pc = scsi->pc;

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

204
205
static int idescsi_check_condition(ide_drive_t *drive,
		struct request *failed_cmd)
Linus Torvalds's avatar
Linus Torvalds committed
206
207
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
208
	struct ide_atapi_pc   *pc;
Linus Torvalds's avatar
Linus Torvalds committed
209
210
211
212
	struct request *rq;
	u8             *buf;

	/* stuff a sense request in front of our current request */
213
	pc = kzalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
214
215
216
	rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
	buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
	if (!pc || !rq || !buf) {
217
218
219
		kfree(buf);
		kfree(rq);
		kfree(pc);
Linus Torvalds's avatar
Linus Torvalds committed
220
221
		return -ENOMEM;
	}
222
	blk_rq_init(NULL, rq);
Linus Torvalds's avatar
Linus Torvalds committed
223
224
	rq->special = (char *) pc;
	pc->rq = rq;
225
	pc->buf = buf;
Linus Torvalds's avatar
Linus Torvalds committed
226
	pc->c[0] = REQUEST_SENSE;
227
	pc->c[4] = pc->req_xfer = pc->buf_size = SCSI_SENSE_BUFFERSIZE;
228
	rq->cmd_type = REQ_TYPE_SENSE;
229
	rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds's avatar
Linus Torvalds committed
230
	pc->timeout = jiffies + WAIT_READY;
231
	pc->callback = ide_scsi_callback;
Linus Torvalds's avatar
Linus Torvalds committed
232
	/* NOTE! Save the failed packet command in "rq->buffer" */
233
234
	rq->buffer = (void *) failed_cmd->special;
	pc->scsi_cmd = ((struct ide_atapi_pc *) failed_cmd->special)->scsi_cmd;
Linus Torvalds's avatar
Linus Torvalds committed
235
236
	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: queue cmd = ", drive->name);
237
		ide_scsi_hex_dump(pc->c, 6);
Linus Torvalds's avatar
Linus Torvalds committed
238
239
	}
	rq->rq_disk = scsi->disk;
240
241
	ide_do_drive_cmd(drive, rq);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
242
243
244
245
246
}

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

249
	if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
Linus Torvalds's avatar
Linus Torvalds committed
250
		/* force an abort */
251
		hwif->OUTBSYNC(hwif, WIN_IDLEIMMEDIATE,
252
			       hwif->io_ports.command_addr);
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
259
260
261
262
263

	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)
{
264
	debug_log("%s called for %lu\n", __func__,
265
		((struct ide_atapi_pc *) rq->special)->scsi_cmd->serial_number);
266

Linus Torvalds's avatar
Linus Torvalds committed
267
268
269
270
271
272
273
274
275
276
277
	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;
278
	struct ide_atapi_pc *pc = (struct ide_atapi_pc *) rq->special;
Linus Torvalds's avatar
Linus Torvalds committed
279
280
	int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
	struct Scsi_Host *host;
281
	int errors = rq->errors;
Linus Torvalds's avatar
Linus Torvalds committed
282
283
	unsigned long flags;

284
	if (!blk_special_request(rq) && !blk_sense_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
285
286
287
288
		ide_end_request(drive, uptodate, nrsecs);
		return 0;
	}
	ide_end_drive_cmd (drive, 0, 0);
289
	if (blk_sense_request(rq)) {
290
		struct ide_atapi_pc *opc = (struct ide_atapi_pc *) rq->buffer;
Linus Torvalds's avatar
Linus Torvalds committed
291
292
		if (log) {
			printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
293
			ide_scsi_hex_dump(pc->buf, 16);
Linus Torvalds's avatar
Linus Torvalds committed
294
		}
295
296
297
		memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buf,
			SCSI_SENSE_BUFFERSIZE);
		kfree(pc->buf);
Linus Torvalds's avatar
Linus Torvalds committed
298
299
300
301
302
		kfree(pc);
		kfree(rq);
		pc = opc;
		rq = pc->rq;
		pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
303
304
305
306
				(((pc->flags & PC_FLAG_TIMEDOUT) ?
				  DID_TIME_OUT :
				  DID_OK) << 16);
	} else if (pc->flags & PC_FLAG_TIMEDOUT) {
Linus Torvalds's avatar
Linus Torvalds committed
307
308
309
310
		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;
311
	} else if (errors >= ERROR_MAX) {
Linus Torvalds's avatar
Linus Torvalds committed
312
313
314
		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);
315
	} else if (errors) {
Linus Torvalds's avatar
Linus Torvalds committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
		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;
}

335
static inline unsigned long get_timeout(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
336
337
338
339
340
341
{
	return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
}

static int idescsi_expiry(ide_drive_t *drive)
{
342
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
343
	struct ide_atapi_pc   *pc   = scsi->pc;
Linus Torvalds's avatar
Linus Torvalds committed
344

345
346
347
	debug_log("%s called for %lu at %lu\n", __func__,
		  pc->scsi_cmd->serial_number, jiffies);

348
	pc->flags |= PC_FLAG_TIMEDOUT;
Linus Torvalds's avatar
Linus Torvalds committed
349
350
351
352
353
354
355
356
357
358

	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);
359
	ide_hwif_t *hwif = drive->hwif;
360
	struct ide_atapi_pc *pc = scsi->pc;
Linus Torvalds's avatar
Linus Torvalds committed
361
	struct request *rq = pc->rq;
362
	xfer_func_t *xferfunc;
Linus Torvalds's avatar
Linus Torvalds committed
363
	unsigned int temp;
364
	u16 bcount;
365
	u8 stat, ireason;
Linus Torvalds's avatar
Linus Torvalds committed
366

367
	debug_log("Enter %s - interrupt handler\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
368

369
	if (pc->flags & PC_FLAG_TIMEDOUT) {
370
		pc->callback(drive);
Linus Torvalds's avatar
Linus Torvalds committed
371
372
		return ide_stopped;
	}
373
374
375
376

	/* Clear the interrupt */
	stat = ide_read_status(drive);

377
	if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
378
379
380
381
		if (hwif->dma_ops->dma_end(drive))
			pc->flags |= PC_FLAG_DMA_ERROR;
		else
			pc->xferred = pc->req_xfer;
382
		debug_log("%s: DMA finished\n", drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
383
384
	}

385
	if ((stat & DRQ_STAT) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
386
		/* No more interrupts */
387
388
		debug_log("Packet command completed, %d bytes transferred\n",
			  pc->xferred);
389
		pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
390
		local_irq_enable_in_hardirq();
391
392
393
394
		if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
			/* Error detected */
			debug_log("%s: I/O error\n", drive->name);

Linus Torvalds's avatar
Linus Torvalds committed
395
			rq->errors++;
396
		}
397
		pc->callback(drive);
Linus Torvalds's avatar
Linus Torvalds committed
398
399
		return ide_stopped;
	}
400
401
402
403
404
405
406
	if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
		pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
		printk(KERN_ERR "%s: The device wants to issue more interrupts "
				"in DMA mode\n", drive->name);
		ide_dma_off(drive);
		return ide_do_reset(drive);
	}
407
408
409
	bcount = (hwif->INB(hwif->io_ports.lbah_addr) << 8) |
		  hwif->INB(hwif->io_ports.lbam_addr);
	ireason = hwif->INB(hwif->io_ports.nsect_addr);
Linus Torvalds's avatar
Linus Torvalds committed
410

411
	if (ireason & CD) {
412
		printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
Linus Torvalds's avatar
Linus Torvalds committed
413
414
		return ide_do_reset (drive);
	}
415
416
417
418
419
420
421
422
423
	if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
		/* Hopefully, we will never get here */
		printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
				"to %s!\n", drive->name,
				(ireason & IO) ? "Write" : "Read",
				(ireason & IO) ? "Read" : "Write");
		return ide_do_reset(drive);
	}
	if (!(pc->flags & PC_FLAG_WRITING)) {
424
425
426
		temp = pc->xferred + bcount;
		if (temp > pc->req_xfer) {
			if (temp > pc->buf_size) {
427
428
429
430
				printk(KERN_ERR "%s: The device wants to send "
						"us more data than expected - "
						"discarding data\n",
						drive->name);
431
				temp = pc->buf_size - pc->xferred;
Linus Torvalds's avatar
Linus Torvalds committed
432
433
				if (temp) {
					if (pc->sg)
434
435
						ide_scsi_io_buffers(drive, pc,
								    temp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
436
					else
437
438
						hwif->input_data(drive, NULL,
							pc->cur_pos, temp);
439
440
441
					printk(KERN_ERR "%s: transferred %d of "
							"%d bytes\n",
							drive->name,
442
							temp, bcount);
Linus Torvalds's avatar
Linus Torvalds committed
443
				}
444
445
				pc->xferred += temp;
				pc->cur_pos += temp;
446
				ide_pad_transfer(drive, 0, bcount - temp);
Linus Torvalds's avatar
Linus Torvalds committed
447
448
449
				ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
				return ide_started;
			}
450
			debug_log("The device wants to send us more data than "
451
				  "expected - allowing transfer\n");
Linus Torvalds's avatar
Linus Torvalds committed
452
		}
453
454
455
456
457
458
459
460
461
462
		xferfunc = hwif->input_data;
	} else
		xferfunc = hwif->output_data;

	if (pc->sg)
		ide_scsi_io_buffers(drive, pc, bcount,
				    !!(pc->flags & PC_FLAG_WRITING));
	else
		xferfunc(drive, NULL, pc->cur_pos, bcount);

Linus Torvalds's avatar
Linus Torvalds committed
463
	/* Update the current position */
464
465
	pc->xferred += bcount;
	pc->cur_pos += bcount;
Linus Torvalds's avatar
Linus Torvalds committed
466

467
468
469
	debug_log("[cmd %x] transferred %d bytes on that intr.\n",
		  pc->c[0], bcount);

Linus Torvalds's avatar
Linus Torvalds committed
470
471
472
473
474
475
476
477
	/* 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)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);
478

479
480
	return ide_transfer_pc(drive, scsi->pc, idescsi_pc_intr,
			       get_timeout(scsi->pc), idescsi_expiry);
Linus Torvalds's avatar
Linus Torvalds committed
481
482
}

483
static inline int idescsi_set_direction(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
484
485
486
{
	switch (pc->c[0]) {
		case READ_6: case READ_10: case READ_12:
487
			pc->flags &= ~PC_FLAG_WRITING;
Linus Torvalds's avatar
Linus Torvalds committed
488
489
			return 0;
		case WRITE_6: case WRITE_10: case WRITE_12:
490
			pc->flags |= PC_FLAG_WRITING;
Linus Torvalds's avatar
Linus Torvalds committed
491
492
493
494
495
496
			return 0;
		default:
			return 1;
	}
}

497
static int idescsi_map_sg(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
498
499
500
501
502
{
	ide_hwif_t *hwif = drive->hwif;
	struct scatterlist *sg, *scsi_sg;
	int segments;

503
	if (!pc->req_xfer || pc->req_xfer % 1024)
Linus Torvalds's avatar
Linus Torvalds committed
504
505
506
507
508
509
		return 1;

	if (idescsi_set_direction(pc))
		return 1;

	sg = hwif->sg_table;
510
511
	scsi_sg = scsi_sglist(pc->scsi_cmd);
	segments = scsi_sg_count(pc->scsi_cmd);
Linus Torvalds's avatar
Linus Torvalds committed
512
513
514
515

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

516
517
	hwif->sg_nents = segments;
	memcpy(sg, scsi_sg, sizeof(*sg) * segments);
Linus Torvalds's avatar
Linus Torvalds committed
518
519
520
521

	return 0;
}

522
523
static ide_startstop_t idescsi_issue_pc(ide_drive_t *drive,
		struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
524
525
526
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);

527
528
	/* Set the current packet command */
	scsi->pc = pc;
Linus Torvalds's avatar
Linus Torvalds committed
529

530
531
	return ide_issue_pc(drive, pc, idescsi_transfer_pc,
			    get_timeout(pc), idescsi_expiry);
Linus Torvalds's avatar
Linus Torvalds committed
532
533
534
535
536
537
538
}

/*
 *	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)
{
539
540
541
542
	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);
Linus Torvalds's avatar
Linus Torvalds committed
543

544
	if (blk_sense_request(rq) || blk_special_request(rq)) {
545
		struct ide_atapi_pc *pc = (struct ide_atapi_pc *)rq->special;
546
547
548
549
		idescsi_scsi_t *scsi = drive_to_idescsi(drive);

		if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags))
			pc->flags |= PC_FLAG_DRQ_INTERRUPT;
550
551
552
553
554

		if (drive->using_dma && !idescsi_map_sg(drive, pc))
			pc->flags |= PC_FLAG_DMA_OK;

		return idescsi_issue_pc(drive, pc);
Linus Torvalds's avatar
Linus Torvalds committed
555
556
557
558
559
560
	}
	blk_dump_rq_flags(rq, "ide-scsi: unsup command");
	idescsi_end_request (drive, 0, 0);
	return ide_stopped;
}

561
#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds's avatar
Linus Torvalds committed
562
563
564
565
566
static void idescsi_add_settings(ide_drive_t *drive)
{
	idescsi_scsi_t *scsi = drive_to_idescsi(drive);

/*
567
 *			drive	setting name	read/write	data type	min	max	mul_factor	div_factor	data pointer		set function
Linus Torvalds's avatar
Linus Torvalds committed
568
 */
569
570
571
572
573
	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);
Linus Torvalds's avatar
Linus Torvalds committed
574
}
575
576
577
#else
static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
#endif
Linus Torvalds's avatar
Linus Torvalds committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

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

593
static void ide_scsi_remove(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
594
595
596
597
598
{
	struct Scsi_Host *scsihost = drive->driver_data;
	struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
	struct gendisk *g = scsi->disk;

599
	scsi_remove_host(scsihost);
600
	ide_proc_unregister_driver(drive, scsi->driver);
Linus Torvalds's avatar
Linus Torvalds committed
601
602
603
604
605
606
607
608

	ide_unregister_region(g);

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

	ide_scsi_put(scsi);
609
610

	drive->scsi = 0;
Linus Torvalds's avatar
Linus Torvalds committed
611
612
}

613
static int ide_scsi_probe(ide_drive_t *);
Linus Torvalds's avatar
Linus Torvalds committed
614

615
#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds's avatar
Linus Torvalds committed
616
617
618
619
620
621
622
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 = {
623
	.gen_driver = {
624
		.owner		= THIS_MODULE,
625
626
627
		.name		= "ide-scsi",
		.bus		= &ide_bus_type,
	},
628
629
	.probe			= ide_scsi_probe,
	.remove			= ide_scsi_remove,
Linus Torvalds's avatar
Linus Torvalds committed
630
631
632
633
634
635
636
	.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,
637
638
639
#ifdef CONFIG_IDE_PROC_FS
	.proc			= idescsi_proc,
#endif
Linus Torvalds's avatar
Linus Torvalds committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
};

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 */
681
682
	sdp->use_10_for_rw = 1;
	sdp->use_10_for_ms = 1;
Linus Torvalds's avatar
Linus Torvalds committed
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
	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;
714
	struct ide_atapi_pc *pc = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
715
716

	if (!drive) {
717
		scmd_printk (KERN_ERR, cmd, "drive not present\n");
Linus Torvalds's avatar
Linus Torvalds committed
718
719
720
		goto abort;
	}
	scsi = drive_to_idescsi(drive);
721
722
	pc = kmalloc(sizeof(struct ide_atapi_pc), GFP_ATOMIC);
	rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
723
724
725
726
727
728
729
	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;
730
731
	if (cmd->sc_data_direction == DMA_TO_DEVICE)
		pc->flags |= PC_FLAG_WRITING;
Linus Torvalds's avatar
Linus Torvalds committed
732
733
	pc->rq = rq;
	memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
734
	pc->buf = NULL;
735
	pc->sg = scsi_sglist(cmd);
736
	pc->sg_cnt = scsi_sg_count(cmd);
Linus Torvalds's avatar
Linus Torvalds committed
737
	pc->b_count = 0;
738
	pc->req_xfer = pc->buf_size = scsi_bufflen(cmd);
Linus Torvalds's avatar
Linus Torvalds committed
739
740
741
	pc->scsi_cmd = cmd;
	pc->done = done;
	pc->timeout = jiffies + cmd->timeout_per_command;
742
	pc->callback = ide_scsi_callback;
Linus Torvalds's avatar
Linus Torvalds committed
743
744
745

	if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
		printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
746
		ide_scsi_hex_dump(cmd->cmnd, cmd->cmd_len);
Linus Torvalds's avatar
Linus Torvalds committed
747
748
		if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
			printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
749
			ide_scsi_hex_dump(pc->c, 12);
Linus Torvalds's avatar
Linus Torvalds committed
750
751
752
		}
	}

753
	blk_rq_init(NULL, rq);
Linus Torvalds's avatar
Linus Torvalds committed
754
	rq->special = (char *) pc;
755
	rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds's avatar
Linus Torvalds committed
756
	spin_unlock_irq(host->host_lock);
757
	blk_execute_rq_nowait(drive->queue, scsi->disk, rq, 0, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
758
759
760
	spin_lock_irq(host->host_lock);
	return 0;
abort:
761
762
	kfree (pc);
	kfree (rq);
Linus Torvalds's avatar
Linus Torvalds committed
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
	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");

808
		if (blk_sense_request(scsi->pc->rq))
809
			kfree(scsi->pc->buf);
Linus Torvalds's avatar
Linus Torvalds committed
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
843
844
		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;
	}

845
846
	spin_lock_irq(cmd->device->host->host_lock);
	spin_lock(&ide_lock);
Linus Torvalds's avatar
Linus Torvalds committed
847
848
849
850

	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);
851
		spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds's avatar
Linus Torvalds committed
852
853
854
855
		return FAILED;
	}

	/* kill current request */
856
857
	if (__blk_end_request(req, -EIO, 0))
		BUG();
858
	if (blk_sense_request(req))
859
		kfree(scsi->pc->buf);
Linus Torvalds's avatar
Linus Torvalds committed
860
861
862
863
864
865
	kfree(scsi->pc);
	scsi->pc = NULL;
	kfree(req);

	/* now nuke the drive queue */
	while ((req = elv_next_request(drive->queue))) {
866
867
		if (__blk_end_request(req, -EIO, 0))
			BUG();
Linus Torvalds's avatar
Linus Torvalds committed
868
869
870
871
872
	}

	HWGROUP(drive)->rq = NULL;
	HWGROUP(drive)->handler = NULL;
	HWGROUP(drive)->busy = 1;		/* will set this to zero when ide reset finished */
873
	spin_unlock(&ide_lock);
Linus Torvalds's avatar
Linus Torvalds committed
874
875
876
877
878
879
880

	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);
881
		msleep(50);
Linus Torvalds's avatar
Linus Torvalds committed
882
883
884
885
886
887
888
889
890
891
		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;
	}

892
	spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds's avatar
Linus Torvalds committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
	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",
};

930
static int ide_scsi_probe(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
931
932
933
934
935
936
937
938
939
940
941
942
{
	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;
	}

943
944
945
	if (idescsi_nocd && drive->media == ide_cdrom)
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
946
947
948
949
	if (!strstr("ide-scsi", drive->driver_req) ||
	    !drive->present ||
	    drive->media == ide_disk ||
	    !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
950
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
951

952
953
	drive->scsi = 1;

Linus Torvalds's avatar
Linus Torvalds committed
954
955
956
957
958
959
960
961
962
	g = alloc_disk(1 << PARTN_BITS);
	if (!g)
		goto out_host_put;

	ide_init_disk(g, drive);

	host->max_id = 1;

	if (drive->id->last_lun)
963
964
965
		debug_log("%s: id->last_lun=%u\n", drive->name,
			  drive->id->last_lun);

Linus Torvalds's avatar
Linus Torvalds committed
966
967
968
969
970
971
972
973
974
975
976
977
	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;
978
	ide_proc_register_driver(drive, &idescsi_driver);
979
980
981
982
983
	err = 0;
	idescsi_setup(drive, idescsi);
	g->fops = &idescsi_ops;
	ide_register_region(g);
	err = scsi_add_host(host, &drive->gendev);
Linus Torvalds's avatar
Linus Torvalds committed
984
	if (!err) {
985
986
		scsi_scan_host(host);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
987
	}
988
989
	/* fall through on error */
	ide_unregister_region(g);
990
	ide_proc_unregister_driver(drive, &idescsi_driver);
Linus Torvalds's avatar
Linus Torvalds committed
991
992
993

	put_disk(g);
out_host_put:
994
	drive->scsi = 0;
Linus Torvalds's avatar
Linus Torvalds committed
995
996
997
998
999
1000
	scsi_host_put(host);
	return err;
}

static int __init init_idescsi_module(void)
{