ide-cd.c 60.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * ATAPI CD-ROM driver.
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
4
5
6
7
 * Copyright (C) 1994-1996   Scott Snyder <snyder@fnald0.fnal.gov>
 * Copyright (C) 1996-1998   Erik Andersen <andersee@debian.org>
 * Copyright (C) 1998-2000   Jens Axboe <axboe@suse.de>
 * Copyright (C) 2005, 2007  Bartlomiej Zolnierkiewicz
Linus Torvalds's avatar
Linus Torvalds committed
8
9
10
11
12
13
14
15
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See linux/COPYING for more information.
 *
 * See Documentation/cdrom/ide-cd for usage information.
 *
 * Suggestions are welcome. Patches that work are more welcome though. ;-)
 * For those wishing to work on this driver, please be sure you download
16
17
 * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
 * (SFF-8020i rev 2.6) standards. These documents can be obtained by
Linus Torvalds's avatar
Linus Torvalds committed
18
19
20
21
 * anonymous ftp from:
 * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
 * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
 *
22
23
24
25
 * For historical changelog please see:
 *	Documentation/ide/ChangeLog.ide-cd.1994-2004
 */

26
27
28
#define DRV_NAME "ide-cd"
#define PFX DRV_NAME ": "

29
#define IDECD_VERSION "5.00"
Linus Torvalds's avatar
Linus Torvalds committed
30
31
32
33
34
35
36
37
38
39
40
41

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/cdrom.h>
#include <linux/ide.h>
#include <linux/completion.h>
42
#include <linux/mutex.h>
43
#include <linux/bcd.h>
Linus Torvalds's avatar
Linus Torvalds committed
44

Borislav Petkov's avatar
Borislav Petkov committed
45
46
/* For SCSI -> ATAPI command conversion */
#include <scsi/scsi.h>
Linus Torvalds's avatar
Linus Torvalds committed
47

48
49
#include <linux/irq.h>
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
50
#include <asm/byteorder.h>
51
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
#include <asm/unaligned.h>

#include "ide-cd.h"

56
57
58
59
60
61
62
63
#define IDECD_DEBUG_LOG		1

#if IDECD_DEBUG_LOG
#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, args)
#else
#define ide_debug_log(lvl, fmt, args...) do {} while (0)
#endif

64
static DEFINE_MUTEX(idecd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
65

66
67
static void ide_cd_release(struct kref *);

Linus Torvalds's avatar
Linus Torvalds committed
68
69
70
71
static struct cdrom_info *ide_cd_get(struct gendisk *disk)
{
	struct cdrom_info *cd = NULL;

72
	mutex_lock(&idecd_ref_mutex);
Borislav Petkov's avatar
Borislav Petkov committed
73
	cd = ide_drv_g(disk, cdrom_info);
74
	if (cd) {
75
		if (ide_device_get(cd->drive))
76
			cd = NULL;
77
78
79
		else
			kref_get(&cd->kref);

80
	}
81
	mutex_unlock(&idecd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
82
83
84
85
86
	return cd;
}

static void ide_cd_put(struct cdrom_info *cd)
{
87
88
	ide_drive_t *drive = cd->drive;

89
	mutex_lock(&idecd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
90
	kref_put(&cd->kref, ide_cd_release);
91
	ide_device_put(drive);
92
	mutex_unlock(&idecd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
93
94
}

Borislav Petkov's avatar
Borislav Petkov committed
95
/*
Linus Torvalds's avatar
Linus Torvalds committed
96
97
98
 * Generic packet command support and error handling routines.
 */

Borislav Petkov's avatar
Borislav Petkov committed
99
/* Mark that we've seen a media change and invalidate our internal buffers. */
100
static void cdrom_saw_media_change(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
101
{
102
	drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
103
	drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
Linus Torvalds's avatar
Linus Torvalds committed
104
105
106
107
108
109
110
}

static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
			   struct request_sense *sense)
{
	int log = 0;

111
112
113
	ide_debug_log(IDE_DBG_SENSE, "Call %s, sense_key: 0x%x\n", __func__,
		      sense->sense_key);

114
	if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
Linus Torvalds's avatar
Linus Torvalds committed
115
116
117
		return 0;

	switch (sense->sense_key) {
118
119
120
121
122
	case NO_SENSE:
	case RECOVERED_ERROR:
		break;
	case NOT_READY:
		/*
Borislav Petkov's avatar
Borislav Petkov committed
123
124
		 * don't care about tray state messages for e.g. capacity
		 * commands or in-progress or becoming ready
125
126
		 */
		if (sense->asc == 0x3a || sense->asc == 0x04)
Linus Torvalds's avatar
Linus Torvalds committed
127
			break;
128
129
130
131
		log = 1;
		break;
	case ILLEGAL_REQUEST:
		/*
Borislav Petkov's avatar
Borislav Petkov committed
132
133
		 * don't log START_STOP unit with LoEj set, since we cannot
		 * reliably check if drive can auto-close
134
135
		 */
		if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
Linus Torvalds's avatar
Linus Torvalds committed
136
			break;
137
138
139
140
		log = 1;
		break;
	case UNIT_ATTENTION:
		/*
Borislav Petkov's avatar
Borislav Petkov committed
141
142
143
		 * Make good and sure we've seen this potential media change.
		 * Some drives (i.e. Creative) fail to present the correct sense
		 * key in the error register.
144
145
146
147
148
149
		 */
		cdrom_saw_media_change(drive);
		break;
	default:
		log = 1;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
150
151
152
153
	}
	return log;
}

154
static void cdrom_analyze_sense_data(ide_drive_t *drive,
Linus Torvalds's avatar
Linus Torvalds committed
155
156
157
			      struct request *failed_command,
			      struct request_sense *sense)
{
158
159
160
161
	unsigned long sector;
	unsigned long bio_sectors;
	struct cdrom_info *info = drive->driver_data;

162
163
164
165
166
167
168
169
	ide_debug_log(IDE_DBG_SENSE, "Call %s, error_code: 0x%x, "
			"sense_key: 0x%x\n", __func__, sense->error_code,
			sense->sense_key);

	if (failed_command)
		ide_debug_log(IDE_DBG_SENSE, "%s: failed cmd: 0x%x\n",
				__func__, failed_command->cmd[0]);

Linus Torvalds's avatar
Linus Torvalds committed
170
171
172
173
	if (!cdrom_log_sense(drive, failed_command, sense))
		return;

	/*
Borislav Petkov's avatar
Borislav Petkov committed
174
175
176
	 * If a read toc is executed for a CD-R or CD-RW medium where the first
	 * toc has not been recorded yet, it will fail with 05/24/00 (which is a
	 * confusing error)
Linus Torvalds's avatar
Linus Torvalds committed
177
178
179
180
181
	 */
	if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
		if (sense->sense_key == 0x05 && sense->asc == 0x24)
			return;

Borislav Petkov's avatar
Borislav Petkov committed
182
183
	/* current error */
	if (sense->error_code == 0x70) {
184
		switch (sense->sense_key) {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		case MEDIUM_ERROR:
		case VOLUME_OVERFLOW:
		case ILLEGAL_REQUEST:
			if (!sense->valid)
				break;
			if (failed_command == NULL ||
					!blk_fs_request(failed_command))
				break;
			sector = (sense->information[0] << 24) |
				 (sense->information[1] << 16) |
				 (sense->information[2] <<  8) |
				 (sense->information[3]);

			if (drive->queue->hardsect_size == 2048)
Borislav Petkov's avatar
Borislav Petkov committed
199
200
				/* device sector size is 2K */
				sector <<= 2;
201
202

			bio_sectors = max(bio_sectors(failed_command->bio), 4U);
203
			sector &= ~(bio_sectors - 1);
204
205

			if (sector < get_capacity(info->disk) &&
206
			    drive->probed_capacity - sector < 4 * 75)
207
				set_capacity(info->disk, sector);
208
209
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
210

211
	ide_cd_log_error(drive->name, failed_command, sense);
Linus Torvalds's avatar
Linus Torvalds committed
212
213
214
215
216
217
218
219
}

static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
				      struct request *failed_command)
{
	struct cdrom_info *info		= drive->driver_data;
	struct request *rq		= &info->request_sense_request;

220
221
	ide_debug_log(IDE_DBG_SENSE, "Call %s\n", __func__);

Linus Torvalds's avatar
Linus Torvalds committed
222
223
224
225
	if (sense == NULL)
		sense = &info->sense_data;

	/* stuff the sense request in front of our current request */
226
227
228
	blk_rq_init(NULL, rq);
	rq->cmd_type = REQ_TYPE_ATA_PC;
	rq->rq_disk = info->disk;
Linus Torvalds's avatar
Linus Torvalds committed
229
230
231

	rq->data = sense;
	rq->cmd[0] = GPCMD_REQUEST_SENSE;
232
233
	rq->cmd[4] = 18;
	rq->data_len = 18;
Linus Torvalds's avatar
Linus Torvalds committed
234

235
	rq->cmd_type = REQ_TYPE_SENSE;
236
	rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds's avatar
Linus Torvalds committed
237
238
239
240

	/* NOTE! Save the failed command in "rq->buffer" */
	rq->buffer = (void *) failed_command;

241
242
243
244
	if (failed_command)
		ide_debug_log(IDE_DBG_SENSE, "failed_cmd: 0x%x\n",
			      failed_command->cmd[0]);

245
	ide_do_drive_cmd(drive, rq);
Linus Torvalds's avatar
Linus Torvalds committed
246
247
}

248
static void cdrom_end_request(ide_drive_t *drive, int uptodate)
Linus Torvalds's avatar
Linus Torvalds committed
249
250
251
252
{
	struct request *rq = HWGROUP(drive)->rq;
	int nsectors = rq->hard_cur_sectors;

253
254
255
256
	ide_debug_log(IDE_DBG_FUNC, "Call %s, cmd: 0x%x, uptodate: 0x%x, "
		      "nsectors: %d\n", __func__, rq->cmd[0], uptodate,
		      nsectors);

257
	if (blk_sense_request(rq) && uptodate) {
Linus Torvalds's avatar
Linus Torvalds committed
258
		/*
259
260
		 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
		 * failed request
Linus Torvalds's avatar
Linus Torvalds committed
261
262
263
264
265
266
267
268
269
270
271
		 */
		struct request *failed = (struct request *) rq->buffer;
		struct cdrom_info *info = drive->driver_data;
		void *sense = &info->sense_data;
		unsigned long flags;

		if (failed) {
			if (failed->sense) {
				sense = failed->sense;
				failed->sense_len = rq->sense_len;
			}
272
			cdrom_analyze_sense_data(drive, failed, sense);
Linus Torvalds's avatar
Linus Torvalds committed
273
			/*
Borislav Petkov's avatar
Borislav Petkov committed
274
			 * now end the failed request
Linus Torvalds's avatar
Linus Torvalds committed
275
			 */
276
277
278
279
280
281
			if (blk_fs_request(failed)) {
				if (ide_end_dequeued_request(drive, failed, 0,
						failed->hard_nr_sectors))
					BUG();
			} else {
				spin_lock_irqsave(&ide_lock, flags);
282
283
284
				if (__blk_end_request(failed, -EIO,
						      failed->data_len))
					BUG();
285
286
287
288
				spin_unlock_irqrestore(&ide_lock, flags);
			}
		} else
			cdrom_analyze_sense_data(drive, NULL, sense);
Linus Torvalds's avatar
Linus Torvalds committed
289
290
291
292
293
294
295
296
297
298
	}

	if (!rq->current_nr_sectors && blk_fs_request(rq))
		uptodate = 1;
	/* make sure it's fully ended */
	if (blk_pc_request(rq))
		nsectors = (rq->data_len + 511) >> 9;
	if (!nsectors)
		nsectors = 1;

299
300
301
	ide_debug_log(IDE_DBG_FUNC, "Exit %s, uptodate: 0x%x, nsectors: %d\n",
		      __func__, uptodate, nsectors);

Linus Torvalds's avatar
Linus Torvalds committed
302
303
304
	ide_end_request(drive, uptodate, nsectors);
}

305
static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 st)
306
{
307
	if (st & 0x80)
308
		return;
309
	ide_dump_status(drive, msg, st);
310
311
}

Borislav Petkov's avatar
Borislav Petkov committed
312
313
314
315
316
/*
 * Returns:
 * 0: if the request should be continued.
 * 1: if the request was ended.
 */
Linus Torvalds's avatar
Linus Torvalds committed
317
318
static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
{
319
320
	ide_hwif_t *hwif = drive->hwif;
	struct request *rq = hwif->hwgroup->rq;
Linus Torvalds's avatar
Linus Torvalds committed
321
	int stat, err, sense_key;
322

Borislav Petkov's avatar
Borislav Petkov committed
323
	/* check for errors */
324
	stat = hwif->tp_ops->read_status(hwif);
325

Linus Torvalds's avatar
Linus Torvalds committed
326
327
328
329
330
331
	if (stat_ret)
		*stat_ret = stat;

	if (OK_STAT(stat, good_stat, BAD_R_STAT))
		return 0;

Borislav Petkov's avatar
Borislav Petkov committed
332
	/* get the IDE error register */
333
	err = ide_read_error(drive);
Linus Torvalds's avatar
Linus Torvalds committed
334
335
336
	sense_key = err >> 4;

	if (rq == NULL) {
337
		printk(KERN_ERR PFX "%s: missing rq in %s\n",
338
				drive->name, __func__);
Linus Torvalds's avatar
Linus Torvalds committed
339
340
341
		return 1;
	}

342
	ide_debug_log(IDE_DBG_RQ, "%s: stat: 0x%x, good_stat: 0x%x, "
343
344
		      "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x, err: 0x%x\n",
		      __func__, stat, good_stat, rq->cmd[0], rq->cmd_type, err);
345

346
	if (blk_sense_request(rq)) {
Borislav Petkov's avatar
Borislav Petkov committed
347
348
349
350
351
		/*
		 * We got an error trying to get sense info from the drive
		 * (probably while trying to recover from a former error).
		 * Just give up.
		 */
352
		rq->cmd_flags |= REQ_FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
353
354
355
356
		cdrom_end_request(drive, 0);
		ide_error(drive, "request sense failure", stat);
		return 1;

357
	} else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
Linus Torvalds's avatar
Linus Torvalds committed
358
359
360
361
362
363
		/* All other functions, except for READ. */

		/*
		 * if we have an error, pass back CHECK_CONDITION as the
		 * scsi status byte
		 */
364
		if (blk_pc_request(rq) && !rq->errors)
Linus Torvalds's avatar
Linus Torvalds committed
365
366
			rq->errors = SAM_STAT_CHECK_CONDITION;

Borislav Petkov's avatar
Borislav Petkov committed
367
		/* check for tray open */
Linus Torvalds's avatar
Linus Torvalds committed
368
		if (sense_key == NOT_READY) {
369
			cdrom_saw_media_change(drive);
Linus Torvalds's avatar
Linus Torvalds committed
370
		} else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov's avatar
Borislav Petkov committed
371
			/* check for media change */
372
			cdrom_saw_media_change(drive);
Linus Torvalds's avatar
Linus Torvalds committed
373
			return 0;
374
375
376
377
378
379
380
381
382
		} else if (sense_key == ILLEGAL_REQUEST &&
			   rq->cmd[0] == GPCMD_START_STOP_UNIT) {
			/*
			 * Don't print error message for this condition--
			 * SFF8090i indicates that 5/24/00 is the correct
			 * response to a request to close the tray if the
			 * drive doesn't have that capability.
			 * cdrom_log_sense() knows this!
			 */
383
		} else if (!(rq->cmd_flags & REQ_QUIET)) {
Borislav Petkov's avatar
Borislav Petkov committed
384
			/* otherwise, print an error */
Linus Torvalds's avatar
Linus Torvalds committed
385
386
			ide_dump_status(drive, "packet command error", stat);
		}
387

388
		rq->cmd_flags |= REQ_FAILED;
Linus Torvalds's avatar
Linus Torvalds committed
389
390
391
392
393
394

		/*
		 * instead of playing games with moving completions around,
		 * remove failed request completely and end it when the
		 * request sense has completed
		 */
395
		goto end_request;
Linus Torvalds's avatar
Linus Torvalds committed
396
397
398
399

	} else if (blk_fs_request(rq)) {
		int do_end_request = 0;

Borislav Petkov's avatar
Borislav Petkov committed
400
		/* handle errors from READ and WRITE requests */
Linus Torvalds's avatar
Linus Torvalds committed
401
402
403
404
405

		if (blk_noretry_request(rq))
			do_end_request = 1;

		if (sense_key == NOT_READY) {
Borislav Petkov's avatar
Borislav Petkov committed
406
			/* tray open */
Linus Torvalds's avatar
Linus Torvalds committed
407
			if (rq_data_dir(rq) == READ) {
408
				cdrom_saw_media_change(drive);
Linus Torvalds's avatar
Linus Torvalds committed
409

Borislav Petkov's avatar
Borislav Petkov committed
410
				/* fail the request */
411
412
				printk(KERN_ERR PFX "%s: tray open\n",
						drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
413
414
415
416
				do_end_request = 1;
			} else {
				struct cdrom_info *info = drive->driver_data;

Borislav Petkov's avatar
Borislav Petkov committed
417
418
				/*
				 * Allow the drive 5 seconds to recover, some
Linus Torvalds's avatar
Linus Torvalds committed
419
				 * devices will return this error while flushing
Borislav Petkov's avatar
Borislav Petkov committed
420
421
				 * data from cache.
				 */
Linus Torvalds's avatar
Linus Torvalds committed
422
				if (!rq->errors)
423
424
					info->write_timeout = jiffies +
							ATAPI_WAIT_WRITE_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
425
426
427
428
429
430
431
				rq->errors = 1;
				if (time_after(jiffies, info->write_timeout))
					do_end_request = 1;
				else {
					unsigned long flags;

					/*
Borislav Petkov's avatar
Borislav Petkov committed
432
433
					 * take a breather relying on the unplug
					 * timer to kick us again
Linus Torvalds's avatar
Linus Torvalds committed
434
435
436
					 */
					spin_lock_irqsave(&ide_lock, flags);
					blk_plug_device(drive->queue);
437
438
					spin_unlock_irqrestore(&ide_lock,
								flags);
Linus Torvalds's avatar
Linus Torvalds committed
439
440
441
442
					return 1;
				}
			}
		} else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov's avatar
Borislav Petkov committed
443
			/* media change */
444
			cdrom_saw_media_change(drive);
Linus Torvalds's avatar
Linus Torvalds committed
445

446
			/*
Borislav Petkov's avatar
Borislav Petkov committed
447
448
			 * Arrange to retry the request but be sure to give up
			 * if we've retried too many times.
449
			 */
Linus Torvalds's avatar
Linus Torvalds committed
450
451
452
453
			if (++rq->errors > ERROR_MAX)
				do_end_request = 1;
		} else if (sense_key == ILLEGAL_REQUEST ||
			   sense_key == DATA_PROTECT) {
454
			/*
Borislav Petkov's avatar
Borislav Petkov committed
455
456
			 * No point in retrying after an illegal request or data
			 * protect error.
457
458
			 */
			ide_dump_status_no_sense(drive, "command error", stat);
Linus Torvalds's avatar
Linus Torvalds committed
459
460
			do_end_request = 1;
		} else if (sense_key == MEDIUM_ERROR) {
461
462
			/*
			 * No point in re-trying a zillion times on a bad
Borislav Petkov's avatar
Borislav Petkov committed
463
			 * sector. If we got here the error is not correctable.
464
			 */
465
466
467
			ide_dump_status_no_sense(drive,
						 "media error (bad sector)",
						 stat);
Linus Torvalds's avatar
Linus Torvalds committed
468
469
			do_end_request = 1;
		} else if (sense_key == BLANK_CHECK) {
Borislav Petkov's avatar
Borislav Petkov committed
470
			/* disk appears blank ?? */
471
472
			ide_dump_status_no_sense(drive, "media error (blank)",
						 stat);
Linus Torvalds's avatar
Linus Torvalds committed
473
			do_end_request = 1;
474
		} else if ((err & ~ATA_ABORTED) != 0) {
Borislav Petkov's avatar
Borislav Petkov committed
475
			/* go to the default handler for other errors */
Linus Torvalds's avatar
Linus Torvalds committed
476
477
478
			ide_error(drive, "cdrom_decode_status", stat);
			return 1;
		} else if ((++rq->errors > ERROR_MAX)) {
Borislav Petkov's avatar
Borislav Petkov committed
479
			/* we've racked up too many retries, abort */
Linus Torvalds's avatar
Linus Torvalds committed
480
481
482
			do_end_request = 1;
		}

Borislav Petkov's avatar
Borislav Petkov committed
483
484
485
486
487
		/*
		 * End a request through request sense analysis when we have
		 * sense data. We need this in order to perform end of media
		 * processing.
		 */
488
489
		if (do_end_request)
			goto end_request;
490

491
		/*
Borislav Petkov's avatar
Borislav Petkov committed
492
493
		 * If we got a CHECK_CONDITION status, queue
		 * a request sense command.
494
		 */
495
		if (stat & ATA_ERR)
496
			cdrom_queue_request_sense(drive, NULL, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
497
	} else {
498
		blk_dump_rq_flags(rq, PFX "bad rq");
Linus Torvalds's avatar
Linus Torvalds committed
499
500
501
		cdrom_end_request(drive, 0);
	}

Borislav Petkov's avatar
Borislav Petkov committed
502
	/* retry, or handle the next request */
Linus Torvalds's avatar
Linus Torvalds committed
503
	return 1;
504
505

end_request:
506
	if (stat & ATA_ERR) {
507
508
509
510
511
512
513
514
515
516
517
518
		unsigned long flags;

		spin_lock_irqsave(&ide_lock, flags);
		blkdev_dequeue_request(rq);
		HWGROUP(drive)->rq = NULL;
		spin_unlock_irqrestore(&ide_lock, flags);

		cdrom_queue_request_sense(drive, rq->sense, rq);
	} else
		cdrom_end_request(drive, 0);

	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
519
520
521
522
523
524
525
}

static int cdrom_timer_expiry(ide_drive_t *drive)
{
	struct request *rq = HWGROUP(drive)->rq;
	unsigned long wait = 0;

526
527
528
	ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd[0]: 0x%x\n", __func__,
		      rq->cmd[0]);

Linus Torvalds's avatar
Linus Torvalds committed
529
	/*
Borislav Petkov's avatar
Borislav Petkov committed
530
531
532
533
	 * Some commands are *slow* and normally take a long time to complete.
	 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
	 * commands/drives support that. Let ide_timer_expiry keep polling us
	 * for these.
Linus Torvalds's avatar
Linus Torvalds committed
534
535
	 */
	switch (rq->cmd[0]) {
536
537
538
539
540
541
542
543
544
	case GPCMD_BLANK:
	case GPCMD_FORMAT_UNIT:
	case GPCMD_RESERVE_RZONE_TRACK:
	case GPCMD_CLOSE_TRACK:
	case GPCMD_FLUSH_CACHE:
		wait = ATAPI_WAIT_PC;
		break;
	default:
		if (!(rq->cmd_flags & REQ_QUIET))
545
			printk(KERN_INFO PFX "cmd 0x%x timed out\n",
546
					 rq->cmd[0]);
547
548
		wait = 0;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
549
550
551
552
	}
	return wait;
}

Borislav Petkov's avatar
Borislav Petkov committed
553
554
555
556
557
558
559
560
/*
 * Set up the device registers for transferring a packet command on DEV,
 * expecting to later transfer XFERLEN bytes.  HANDLER is the routine
 * which actually transfers the command to the drive.  If this is a
 * drq_interrupt device, this routine will arrange for HANDLER to be
 * called when the interrupt from the drive arrives.  Otherwise, HANDLER
 * will be called immediately after the drive is prepared for the transfer.
 */
Linus Torvalds's avatar
Linus Torvalds committed
561
562
563
564
565
566
static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
						  int xferlen,
						  ide_handler_t *handler)
{
	ide_hwif_t *hwif = drive->hwif;

567
568
	ide_debug_log(IDE_DBG_PC, "Call %s, xferlen: %d\n", __func__, xferlen);

569
	/* FIXME: for Virtual DMA we must check harder */
570
571
	if (drive->dma)
		drive->dma = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds's avatar
Linus Torvalds committed
572

Borislav Petkov's avatar
Borislav Petkov committed
573
	/* set up the controller registers */
574
	ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL,
575
			   xferlen, drive->dma);
576

577
	if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
578
		/* waiting for CDB interrupt, not DMA yet. */
579
		if (drive->dma)
580
581
			drive->waiting_for_dma = 0;

Linus Torvalds's avatar
Linus Torvalds committed
582
		/* packet command */
583
		ide_execute_command(drive, ATA_CMD_PACKET, handler,
584
				    ATAPI_WAIT_PC, cdrom_timer_expiry);
Linus Torvalds's avatar
Linus Torvalds committed
585
586
		return ide_started;
	} else {
587
		ide_execute_pkt_cmd(drive);
Linus Torvalds's avatar
Linus Torvalds committed
588
589
590
591
592

		return (*handler) (drive);
	}
}

Borislav Petkov's avatar
Borislav Petkov committed
593
594
595
596
597
598
/*
 * Send a packet command to DRIVE described by CMD_BUF and CMD_LEN. The device
 * registers must have already been prepared by cdrom_start_packet_command.
 * HANDLER is the interrupt handler to call when the command completes or
 * there's data ready.
 */
Linus Torvalds's avatar
Linus Torvalds committed
599
#define ATAPI_MIN_CDB_BYTES 12
600
static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *drive,
Linus Torvalds's avatar
Linus Torvalds committed
601
602
603
604
605
606
607
					  struct request *rq,
					  ide_handler_t *handler)
{
	ide_hwif_t *hwif = drive->hwif;
	int cmd_len;
	ide_startstop_t startstop;

608
609
	ide_debug_log(IDE_DBG_PC, "Call %s\n", __func__);

610
	if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov's avatar
Borislav Petkov committed
611
612
613
614
		/*
		 * Here we should have been called after receiving an interrupt
		 * from the device.  DRQ should how be set.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
615

Borislav Petkov's avatar
Borislav Petkov committed
616
		/* check for errors */
617
		if (cdrom_decode_status(drive, ATA_DRQ, NULL))
Linus Torvalds's avatar
Linus Torvalds committed
618
			return ide_stopped;
619

Borislav Petkov's avatar
Borislav Petkov committed
620
		/* ok, next interrupt will be DMA interrupt */
621
		if (drive->dma)
622
			drive->waiting_for_dma = 1;
Linus Torvalds's avatar
Linus Torvalds committed
623
	} else {
Borislav Petkov's avatar
Borislav Petkov committed
624
		/* otherwise, we must wait for DRQ to get set */
625
626
		if (ide_wait_stat(&startstop, drive, ATA_DRQ,
				  ATA_BUSY, WAIT_READY))
Linus Torvalds's avatar
Linus Torvalds committed
627
628
629
			return startstop;
	}

Borislav Petkov's avatar
Borislav Petkov committed
630
	/* arm the interrupt handler */
Linus Torvalds's avatar
Linus Torvalds committed
631
632
633
634
635
636
637
	ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);

	/* ATAPI commands get padded out to 12 bytes minimum */
	cmd_len = COMMAND_SIZE(rq->cmd[0]);
	if (cmd_len < ATAPI_MIN_CDB_BYTES)
		cmd_len = ATAPI_MIN_CDB_BYTES;

Borislav Petkov's avatar
Borislav Petkov committed
638
	/* send the command to the device */
639
	hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
Linus Torvalds's avatar
Linus Torvalds committed
640

Borislav Petkov's avatar
Borislav Petkov committed
641
	/* start the DMA if need be */
642
	if (drive->dma)
643
		hwif->dma_ops->dma_start(drive);
Linus Torvalds's avatar
Linus Torvalds committed
644
645
646
647
648
649
650
651
652

	return ide_started;
}

/*
 * Check the contents of the interrupt reason register from the cdrom
 * and attempt to recover if there are problems.  Returns  0 if everything's
 * ok; nonzero if the request has been terminated.
 */
653
654
static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
				int len, int ireason, int rw)
Linus Torvalds's avatar
Linus Torvalds committed
655
{
656
657
	ide_hwif_t *hwif = drive->hwif;

658
659
660
	ide_debug_log(IDE_DBG_FUNC, "Call %s, ireason: 0x%x, rw: 0x%x\n",
		      __func__, ireason, rw);

661
662
663
664
665
	/*
	 * ireason == 0: the drive wants to receive data from us
	 * ireason == 2: the drive is expecting to transfer data to us
	 */
	if (ireason == (!rw << 1))
Linus Torvalds's avatar
Linus Torvalds committed
666
		return 0;
667
	else if (ireason == (rw << 1)) {
668

Borislav Petkov's avatar
Borislav Petkov committed
669
		/* whoops... */
670
		printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
671
				drive->name, __func__);
Linus Torvalds's avatar
Linus Torvalds committed
672

673
		ide_pad_transfer(drive, rw, len);
674
	} else  if (rw == 0 && ireason == 1) {
Borislav Petkov's avatar
Borislav Petkov committed
675
676
677
		/*
		 * Some drives (ASUS) seem to tell us that status info is
		 * available.  Just get it and ignore.
Linus Torvalds's avatar
Linus Torvalds committed
678
		 */
679
		(void)hwif->tp_ops->read_status(hwif);
Linus Torvalds's avatar
Linus Torvalds committed
680
681
		return 0;
	} else {
Borislav Petkov's avatar
Borislav Petkov committed
682
		/* drive wants a command packet, or invalid ireason... */
683
		printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
684
				drive->name, __func__, ireason);
Linus Torvalds's avatar
Linus Torvalds committed
685
686
	}

687
688
689
	if (rq->cmd_type == REQ_TYPE_ATA_PC)
		rq->cmd_flags |= REQ_FAILED;

Linus Torvalds's avatar
Linus Torvalds committed
690
691
692
693
	cdrom_end_request(drive, 0);
	return -1;
}

694
695
696
697
698
699
/*
 * Assume that the drive will always provide data in multiples of at least
 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
 */
static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
{
700
701
	ide_debug_log(IDE_DBG_FUNC, "Call %s, len: %d\n", __func__, len);

702
703
704
	if ((len % SECTOR_SIZE) == 0)
		return 0;

705
706
	printk(KERN_ERR PFX "%s: %s: Bad transfer size %d\n", drive->name,
			__func__, len);
707

708
	if (drive->atapi_flags & IDE_AFLAG_LIMIT_NFRAMES)
709
710
		printk(KERN_ERR PFX "This drive is not supported by this "
				"version of the driver\n");
711
	else {
712
		printk(KERN_ERR PFX "Trying to limit transfer sizes\n");
713
		drive->atapi_flags |= IDE_AFLAG_LIMIT_NFRAMES;
714
715
716
717
718
	}

	return 1;
}

719
static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
720

721
722
static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
						 struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
723
{
724
725
726
	ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd_flags: 0x%x\n", __func__,
		      rq->cmd_flags);

727
728
729
730
	if (rq_data_dir(rq) == READ) {
		unsigned short sectors_per_frame =
			queue_hardsect_size(drive->queue) >> SECTOR_BITS;
		int nskip = rq->sector & (sectors_per_frame - 1);
Linus Torvalds's avatar
Linus Torvalds committed
731

732
733
734
735
736
737
738
739
740
741
742
		/*
		 * If the requested sector doesn't start on a frame boundary,
		 * we must adjust the start of the transfer so that it does,
		 * and remember to skip the first few sectors.
		 *
		 * If the rq->current_nr_sectors field is larger than the size
		 * of the buffer, it will mean that we're to skip a number of
		 * sectors equal to the amount by which rq->current_nr_sectors
		 * is larger than the buffer size.
		 */
		if (nskip > 0) {
Borislav Petkov's avatar
Borislav Petkov committed
743
			/* sanity check... */
744
745
			if (rq->current_nr_sectors !=
			    bio_cur_sectors(rq->bio)) {
746
				printk(KERN_ERR PFX "%s: %s: buffer botch (%u)\n",
747
						drive->name, __func__,
748
749
750
751
752
						rq->current_nr_sectors);
				cdrom_end_request(drive, 0);
				return ide_stopped;
			}
			rq->current_nr_sectors += nskip;
Linus Torvalds's avatar
Linus Torvalds committed
753
754
		}
	}
755

Borislav Petkov's avatar
Borislav Petkov committed
756
	/* set up the command */
Linus Torvalds's avatar
Linus Torvalds committed
757
758
	rq->timeout = ATAPI_WAIT_PC;

759
760
761
762
763
764
765
766
767
768
769
770
771
	return ide_started;
}

/*
 * Routine to send a read/write packet command to the drive. This is usually
 * called directly from cdrom_start_{read,write}(). However, for drq_interrupt
 * devices, it is called from an interrupt when the drive is ready to accept
 * the command.
 */
static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
{
	struct request *rq = drive->hwif->hwgroup->rq;

Borislav Petkov's avatar
Borislav Petkov committed
772
	/* send the command to the drive and return */
773
	return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
Linus Torvalds's avatar
Linus Torvalds committed
774
775
776
777
778
779
}

#define IDECD_SEEK_THRESHOLD	(1000)			/* 1000 blocks */
#define IDECD_SEEK_TIMER	(5 * WAIT_MIN_SLEEP)	/* 100 ms */
#define IDECD_SEEK_TIMEOUT	(2 * WAIT_CMD)		/* 20 sec */

780
static ide_startstop_t cdrom_seek_intr(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783
784
785
{
	struct cdrom_info *info = drive->driver_data;
	int stat;
	static int retry = 10;

786
787
	ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);

Linus Torvalds's avatar
Linus Torvalds committed
788
789
	if (cdrom_decode_status(drive, 0, &stat))
		return ide_stopped;
790

791
	drive->atapi_flags |= IDE_AFLAG_SEEKING;
Linus Torvalds's avatar
Linus Torvalds committed
792
793

	if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
Borislav Petkov's avatar
Borislav Petkov committed
794
		if (--retry == 0)
795
			drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds's avatar
Linus Torvalds committed
796
797
798
799
	}
	return ide_stopped;
}

800
static void ide_cd_prepare_seek_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
801
802
803
{
	sector_t frame = rq->sector;

804
805
	ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);

Linus Torvalds's avatar
Linus Torvalds committed
806
807
	sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);

808
	memset(rq->cmd, 0, BLK_MAX_CDB);
Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
812
	rq->cmd[0] = GPCMD_SEEK;
	put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);

	rq->timeout = ATAPI_WAIT_PC;
813
814
815
816
817
818
}

static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive)
{
	struct request *rq = drive->hwif->hwgroup->rq;

Linus Torvalds's avatar
Linus Torvalds committed
819
820
821
	return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
}

822
/*
Borislav Petkov's avatar
Borislav Petkov committed
823
824
 * Fix up a possibly partially-processed request so that we can start it over
 * entirely, or even put it back on the request queue.
825
 */
826
static void ide_cd_restore_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
827
{
828
829
830

	ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);

Linus Torvalds's avatar
Linus Torvalds committed
831
	if (rq->buffer != bio_data(rq->bio)) {
832
833
		sector_t n =
			(rq->buffer - (char *)bio_data(rq->bio)) / SECTOR_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
837
838

		rq->buffer = bio_data(rq->bio);
		rq->nr_sectors += n;
		rq->sector -= n;
	}
839
840
	rq->current_nr_sectors = bio_cur_sectors(rq->bio);
	rq->hard_cur_sectors = rq->current_nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
841
842
843
844
845
	rq->hard_nr_sectors = rq->nr_sectors;
	rq->hard_sector = rq->sector;
	rq->q->prep_rq_fn(rq->q, rq);
}

846
static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct request *rq)
847
{
848
849
	ide_debug_log(IDE_DBG_FUNC, "Call %s, rq->cmd[0]: 0x%x\n",
		      __func__, rq->cmd[0]);
850

851
852
853
854
855
856
857
858
859
860
861
862
	/*
	 * Some of the trailing request sense fields are optional,
	 * and some drives don't send them.  Sigh.
	 */
	if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
	    rq->data_len > 0 && rq->data_len <= 5)
		while (rq->data_len > 0) {
			*(u8 *)rq->data++ = 0;
			--rq->data_len;
		}
}

863
864
865
866
int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
		    int write, void *buffer, unsigned *bufflen,
		    struct request_sense *sense, int timeout,
		    unsigned int cmd_flags)
Linus Torvalds's avatar
Linus Torvalds committed
867
{
868
869
	struct cdrom_info *info = drive->driver_data;
	struct request_sense local_sense;
Linus Torvalds's avatar
Linus Torvalds committed
870
	int retries = 10;
871
	unsigned int flags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
872

873
874
	if (!sense)
		sense = &local_sense;
Linus Torvalds's avatar
Linus Torvalds committed
875

876
	ide_debug_log(IDE_DBG_PC, "Call %s, cmd[0]: 0x%x, write: 0x%x, "
877
878
879
		      "timeout: %d, cmd_flags: 0x%x\n", __func__, cmd[0], write,
		      timeout, cmd_flags);

Borislav Petkov's avatar
Borislav Petkov committed
880
	/* start of retry loop */
Linus Torvalds's avatar
Linus Torvalds committed
881
	do {
882
		struct request *rq;
Linus Torvalds's avatar
Linus Torvalds committed
883
884
		int error;

885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
		rq = blk_get_request(drive->queue, write, __GFP_WAIT);

		memcpy(rq->cmd, cmd, BLK_MAX_CDB);
		rq->cmd_type = REQ_TYPE_ATA_PC;
		rq->sense = sense;
		rq->cmd_flags |= cmd_flags;
		rq->timeout = timeout;
		if (buffer) {
			rq->data = buffer;
			rq->data_len = *bufflen;
		}

		error = blk_execute_rq(drive->queue, info->disk, rq, 0);

		if (buffer)
			*bufflen = rq->data_len;

		flags = rq->cmd_flags;
		blk_put_request(rq);
Linus Torvalds's avatar
Linus Torvalds committed
904

Borislav Petkov's avatar
Borislav Petkov committed
905
906
907
908
		/*
		 * FIXME: we should probably abort/retry or something in case of
		 * failure.
		 */
909
		if (flags & REQ_FAILED) {
Borislav Petkov's avatar
Borislav Petkov committed
910
911
912
913
			/*
			 * The request failed.  Retry if it was due to a unit
			 * attention status (usually means media was changed).
			 */
914
			struct request_sense *reqbuf = sense;
Linus Torvalds's avatar
Linus Torvalds committed
915
916
917
918
919

			if (reqbuf->sense_key == UNIT_ATTENTION)
				cdrom_saw_media_change(drive);
			else if (reqbuf->sense_key == NOT_READY &&
				 reqbuf->asc == 4 && reqbuf->ascq != 4) {
Borislav Petkov's avatar
Borislav Petkov committed
920
921
922
923
924
				/*
				 * The drive is in the process of loading
				 * a disk.  Retry, but wait a little to give
				 * the drive time to complete the load.
				 */
Linus Torvalds's avatar
Linus Torvalds committed
925
926
				ssleep(2);
			} else {
Borislav Petkov's avatar
Borislav Petkov committed
927
				/* otherwise, don't retry */
Linus Torvalds's avatar
Linus Torvalds committed
928
929
930
931
932
				retries = 0;
			}
			--retries;
		}

Borislav Petkov's avatar
Borislav Petkov committed
933
		/* end of retry loop */
934
	} while ((flags & REQ_FAILED) && retries >= 0);
Linus Torvalds's avatar
Linus Torvalds committed
935

Borislav Petkov's avatar
Borislav Petkov committed
936
	/* return an error if the command failed */
937
	return (flags & REQ_FAILED) ? -EIO : 0;
Linus Torvalds's avatar
Linus Torvalds committed
938
939
}

940
/*
Borislav Petkov's avatar
Borislav Petkov committed
941
942
943
 * Called from blk_end_request_callback() after the data of the request is
 * completed and before the request itself is completed. By returning value '1',
 * blk_end_request_callback() returns immediately without completing it.
944
945
946
947
948
949
 */
static int cdrom_newpc_intr_dummy_cb(struct request *rq)
{
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
950
951
static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
{
952
	ide_hwif_t *hwif = drive->hwif;
Linus Torvalds's avatar
Linus Torvalds committed
953
954
	struct request *rq = HWGROUP(drive)->rq;
	xfer_func_t *xferfunc;
955
	ide_expiry_t *expiry = NULL;
956
	int dma_error = 0, dma, stat, thislen, uptodate = 0;
957
958
	int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
	unsigned int timeout;
959
960
	u16 len;
	u8 ireason;
Linus Torvalds's avatar
Linus Torvalds committed
961

962
963
964
	ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x\n",
		      __func__, rq->cmd[0], write);

Borislav Petkov's avatar
Borislav Petkov committed
965
	/* check for errors */
966
	dma = drive->dma;
Linus Torvalds's avatar
Linus Torvalds committed
967
	if (dma) {
968
		drive->dma = 0;
969
		dma_error = hwif->dma_ops->dma_end(drive);
970
		if (dma_error) {
971
			printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
972
					write ? "write" : "read");
973
974
			ide_dma_off(drive);
		}
Linus Torvalds's avatar
Linus Torvalds committed
975
976
977
978
979
	}

	if (cdrom_decode_status(drive, 0, &stat))
		return ide_stopped;

Borislav Petkov's avatar
Borislav Petkov committed
980
	/* using dma, transfer is complete now */
Linus Torvalds's avatar
Linus Torvalds committed
981
	if (dma) {
982
		if (dma_error)
Linus Torvalds's avatar
Linus Torvalds committed
983
			return ide_error(drive, "dma error", stat);
984
985
986
987
		if (blk_fs_request(rq)) {
			ide_end_request(drive, 1, rq->nr_sectors);
			return ide_stopped;
		}
988
		goto end_request;
Linus Torvalds's avatar
Linus Torvalds committed
989
990
	}

991
	ide_read_bcount_and_ireason(drive, &len, &ireason);
992
993

	thislen = blk_fs_request(rq) ? len : rq->data_len;
Linus Torvalds's avatar
Linus Torvalds committed
994
995
996
	if (thislen > len)
		thislen = len;

997
998
999
	ide_debug_log(IDE_DBG_PC, "%s: DRQ: stat: 0x%x, thislen: %d\n",
		      __func__, stat, thislen);

Borislav Petkov's avatar
Borislav Petkov committed
1000
	/* If DRQ is clear, the command has completed. */
1001
	if ((stat & ATA_DRQ) == 0) {
1002
1003
1004
1005
1006
1007
1008
		if (blk_fs_request(rq)) {
			/*
			 * If we're not done reading/writing, complain.
			 * Otherwise, complete the command normally.
			 */
			uptodate = 1;
			if (rq->current_nr_sectors > 0) {
1009
				printk(KERN_ERR PFX "%s: %s: data underrun "
1010
						"(%d blocks)\n",
1011
						drive->name, __func__,
1012
1013
1014
1015
1016
1017
1018
1019
						rq->current_nr_sectors);
				if (!write)
					rq->cmd_flags |= REQ_FAILED;
				uptodate = 0;
			}
			cdrom_end_request(drive, uptodate);
			return ide_stopped;
		} else if (!blk_pc_request(rq)) {
1020
			ide_cd_request_sense_fixup(drive, rq);
Borislav Petkov's avatar
Borislav Petkov committed
1021
			/* complain if we still have data left to transfer */
1022
1023
1024
			uptodate = rq->data_len ? 0 : 1;
		}
		goto end_request;
1025
	}
Linus Torvalds's avatar
Linus Torvalds committed
1026

Borislav Petkov's avatar
Borislav Petkov committed
1027
	/* check which way to transfer data */
1028
1029
	if (ide_cd_check_ireason(drive, rq, len, ireason, write))
		return ide_stopped;
1030

1031
1032
	if (blk_fs_request(rq)) {
		if (write == 0) {
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
			int nskip;

			if (ide_cd_check_transfer_size(drive, len)) {
				cdrom_end_request(drive, 0);
				return ide_stopped;
			}

			/*
			 * First, figure out if we need to bit-bucket
			 * any of the leading sectors.
			 */
			nskip = min_t(int, rq->current_nr_sectors
					   - bio_cur_sectors(rq->bio),
					   thislen >> 9);
			if (nskip > 0) {
1048
				ide_pad_transfer(drive, write, nskip << 9);
1049
1050
1051
1052
				rq->current_nr_sectors -= nskip;
				thislen -= (nskip << 9);
			}
		}
1053
	}
Linus Torvalds's avatar
Linus Torvalds committed
1054

1055
1056
	if (ireason == 0) {
		write = 1;
1057
		xferfunc = hwif->tp_ops->output_data;
1058
	} else {
1059
		write = 0;
1060
		xferfunc = hwif->tp_ops->input_data;
Linus Torvalds's avatar
Linus Torvalds committed
1061
1062
	}

1063
1064
1065
	ide_debug_log(IDE_DBG_PC, "%s: data transfer, rq->cmd_type: 0x%x, "
		      "ireason: 0x%x\n", __func__, rq->cmd_type, ireason);

Borislav Petkov's avatar
Borislav Petkov committed
1066
	/* transfer data */
Linus Torvalds's avatar
Linus Torvalds committed
1067
	while (thislen > 0) {
1068
		u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
1069
		int blen = rq->data_len;
Linus Torvalds's avatar
Linus Torvalds committed
1070

Borislav Petkov's avatar
Borislav Petkov committed
1071
		/* bio backed? */
Linus Torvalds's avatar
Linus Torvalds committed
1072
		if (rq->bio) {
1073
1074
1075
1076
1077
1078
1079