blob: 25d2d88e82ada07444f870e02e7d9b57cc3f7514 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz3bb46632008-02-01 23:09:28 +01002 * ATAPI CD-ROM driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov>
5 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
6 * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
Bartlomiej Zolnierkiewicz14fa91c2009-03-31 20:15:16 +02007 * Copyright (C) 2005, 2007-2009 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * May be copied or modified under the terms of the GNU General Public
10 * License. See linux/COPYING for more information.
11 *
Mauro Carvalho Chehab8ea61882019-06-12 14:52:40 -030012 * See Documentation/cdrom/ide-cd.rst for usage information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * Suggestions are welcome. Patches that work are more welcome though. ;-)
Bartlomiej Zolnierkiewicz116e6902009-03-31 20:14:59 +020015 *
16 * Documentation:
17 * Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
Bartlomiej Zolnierkiewicz03553352008-02-01 23:09:18 +010019 * For historical changelog please see:
20 * Documentation/ide/ChangeLog.ide-cd.1994-2004
21 */
22
Borislav Petkovfc8323f2008-10-13 21:39:48 +020023#define DRV_NAME "ide-cd"
24#define PFX DRV_NAME ": "
25
Bartlomiej Zolnierkiewicz3bb46632008-02-01 23:09:28 +010026#define IDECD_VERSION "5.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Arnd Bergmann64cbfa92019-11-28 15:55:17 +010028#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/types.h>
31#include <linux/kernel.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010032#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/timer.h>
Alexey Dobriyan6d703a82009-09-01 17:52:57 -070035#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/interrupt.h>
38#include <linux/errno.h>
39#include <linux/cdrom.h>
40#include <linux/ide.h>
41#include <linux/completion.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080042#include <linux/mutex.h>
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +010043#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020045/* For SCSI -> ATAPI command conversion */
46#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Borislav Petkov9aba4682008-04-26 22:25:15 +020048#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/byteorder.h>
Borislav Petkov9aba4682008-04-26 22:25:15 +020050#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/unaligned.h>
52
53#include "ide-cd.h"
54
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020055static DEFINE_MUTEX(ide_cd_mutex);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080056static DEFINE_MUTEX(idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010058static void ide_cd_release(struct device *);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct cdrom_info *ide_cd_get(struct gendisk *disk)
61{
62 struct cdrom_info *cd = NULL;
63
Arjan van de Vencf8b8972006-03-23 03:00:45 -080064 mutex_lock(&idecd_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +020065 cd = ide_drv_g(disk, cdrom_info);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020066 if (cd) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020067 if (ide_device_get(cd->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020068 cd = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020069 else
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010070 get_device(&cd->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020071
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020072 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -080073 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return cd;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void ide_cd_put(struct cdrom_info *cd)
78{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020079 ide_drive_t *drive = cd->drive;
80
Arjan van de Vencf8b8972006-03-23 03:00:45 -080081 mutex_lock(&idecd_ref_mutex);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010082 put_device(&cd->dev);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020083 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080084 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Generic packet command support and error handling routines.
89 */
90
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020091/* Mark that we've seen a media change and invalidate our internal buffers. */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +020092static void cdrom_saw_media_change(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +020094 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Borislav Petkov570f89e2008-07-23 19:56:02 +020095 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Borislav Petkov239f7e22009-04-23 08:30:29 +020098static int cdrom_log_sense(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Borislav Petkov239f7e22009-04-23 08:30:29 +0200100 struct request_sense *sense = &drive->sense_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int log = 0;
102
Christoph Hellwige8064022016-10-20 15:12:13 +0200103 if (!sense || !rq || (rq->rq_flags & RQF_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105
Borislav Petkov239f7e22009-04-23 08:30:29 +0200106 ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 switch (sense->sense_key) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200109 case NO_SENSE:
110 case RECOVERED_ERROR:
111 break;
112 case NOT_READY:
113 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200114 * don't care about tray state messages for e.g. capacity
115 * commands or in-progress or becoming ready
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200116 */
117 if (sense->asc == 0x3a || sense->asc == 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200119 log = 1;
120 break;
121 case ILLEGAL_REQUEST:
122 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200123 * don't log START_STOP unit with LoEj set, since we cannot
124 * reliably check if drive can auto-close
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200125 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100126 if (scsi_req(rq)->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200128 log = 1;
129 break;
130 case UNIT_ATTENTION:
131 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200132 * Make good and sure we've seen this potential media change.
133 * Some drives (i.e. Creative) fail to present the correct sense
134 * key in the error register.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200135 */
136 cdrom_saw_media_change(drive);
137 break;
138 default:
139 log = 1;
140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 return log;
143}
144
Borislav Petkove5e076a2008-04-26 22:25:15 +0200145static void cdrom_analyze_sense_data(ide_drive_t *drive,
Borislav Petkov239f7e22009-04-23 08:30:29 +0200146 struct request *failed_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Borislav Petkov239f7e22009-04-23 08:30:29 +0200148 struct request_sense *sense = &drive->sense_data;
149 struct cdrom_info *info = drive->driver_data;
Alan Coxdbe217a2006-06-25 05:47:44 -0700150 unsigned long sector;
151 unsigned long bio_sectors;
Alan Coxdbe217a2006-06-25 05:47:44 -0700152
Borislav Petkov088b1b82009-01-02 13:34:47 +0100153 ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x",
154 sense->error_code, sense->sense_key);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200155
156 if (failed_command)
Borislav Petkov088b1b82009-01-02 13:34:47 +0100157 ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x",
158 failed_command->cmd[0]);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200159
Borislav Petkov239f7e22009-04-23 08:30:29 +0200160 if (!cdrom_log_sense(drive, failed_command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return;
162
163 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200164 * If a read toc is executed for a CD-R or CD-RW medium where the first
165 * toc has not been recorded yet, it will fail with 05/24/00 (which is a
166 * confusing error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100168 if (failed_command && scsi_req(failed_command)->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (sense->sense_key == 0x05 && sense->asc == 0x24)
170 return;
171
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200172 /* current error */
173 if (sense->error_code == 0x70) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200174 switch (sense->sense_key) {
Alan Coxdbe217a2006-06-25 05:47:44 -0700175 case MEDIUM_ERROR:
176 case VOLUME_OVERFLOW:
177 case ILLEGAL_REQUEST:
178 if (!sense->valid)
179 break;
180 if (failed_command == NULL ||
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100181 blk_rq_is_passthrough(failed_command))
Alan Coxdbe217a2006-06-25 05:47:44 -0700182 break;
183 sector = (sense->information[0] << 24) |
184 (sense->information[1] << 16) |
185 (sense->information[2] << 8) |
186 (sense->information[3]);
187
Martin K. Petersene1defc42009-05-22 17:17:49 -0400188 if (queue_logical_block_size(drive->queue) == 2048)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200189 /* device sector size is 2K */
190 sector <<= 2;
Roel Kluineee49292008-04-28 23:44:43 +0200191
192 bio_sectors = max(bio_sectors(failed_command->bio), 4U);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200193 sector &= ~(bio_sectors - 1);
Alan Coxdbe217a2006-06-25 05:47:44 -0700194
Bartlomiej Zolnierkiewiczd3dd7102009-02-25 20:28:23 +0100195 /*
196 * The SCSI specification allows for the value
197 * returned by READ CAPACITY to be up to 75 2K
198 * sectors past the last readable block.
199 * Therefore, if we hit a medium error within the
200 * last 75 2K sectors, we decrease the saved size
201 * value.
202 */
Alan Coxdbe217a2006-06-25 05:47:44 -0700203 if (sector < get_capacity(info->disk) &&
Borislav Petkove5e076a2008-04-26 22:25:15 +0200204 drive->probed_capacity - sector < 4 * 75)
Alan Coxdbe217a2006-06-25 05:47:44 -0700205 set_capacity(info->disk, sector);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200206 }
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +0100209 ide_cd_log_error(drive->name, failed_command, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200212static void ide_cd_complete_failed_rq(ide_drive_t *drive, struct request *rq)
213{
214 /*
Christoph Hellwig22ce0a72018-11-10 09:30:49 +0100215 * For ATA_PRIV_SENSE, "ide_req(rq)->special" points to the original
Tejun Heo02e7cf82009-04-19 07:00:42 +0900216 * failed request. Also, the sense data should be read
217 * directly from rq which might be different from the original
218 * sense buffer if it got copied during mapping.
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200219 */
Christoph Hellwig22ce0a72018-11-10 09:30:49 +0100220 struct request *failed = ide_req(rq)->special;
Tejun Heo02e7cf82009-04-19 07:00:42 +0900221 void *sense = bio_data(rq->bio);
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200222
223 if (failed) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100224 /*
225 * Sense is always read into drive->sense_data, copy back to the
226 * original request.
227 */
228 memcpy(scsi_req(failed)->sense, sense, 18);
229 scsi_req(failed)->sense_len = scsi_req(rq)->sense_len;
Borislav Petkov239f7e22009-04-23 08:30:29 +0200230 cdrom_analyze_sense_data(drive, failed);
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200231
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200232 if (ide_end_rq(drive, failed, BLK_STS_IOERR, blk_rq_bytes(failed)))
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200233 BUG();
234 } else
Borislav Petkov239f7e22009-04-23 08:30:29 +0200235 cdrom_analyze_sense_data(drive, NULL);
Bartlomiej Zolnierkiewicz299c4852009-03-31 20:15:01 +0200236}
237
Borislav Petkov805ec582009-04-08 14:12:52 +0200238
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200239/*
Borislav Petkov805ec582009-04-08 14:12:52 +0200240 * Allow the drive 5 seconds to recover; some devices will return NOT_READY
241 * while flushing data from cache.
242 *
243 * returns: 0 failed (write timeout expired)
244 * 1 success
245 */
246static int ide_cd_breathe(ide_drive_t *drive, struct request *rq)
247{
248
249 struct cdrom_info *info = drive->driver_data;
250
Christoph Hellwig17d53632017-04-20 16:03:01 +0200251 if (!scsi_req(rq)->result)
Borislav Petkov805ec582009-04-08 14:12:52 +0200252 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
253
Christoph Hellwig17d53632017-04-20 16:03:01 +0200254 scsi_req(rq)->result = 1;
Borislav Petkov805ec582009-04-08 14:12:52 +0200255
256 if (time_after(jiffies, info->write_timeout))
257 return 0;
258 else {
Borislav Petkov805ec582009-04-08 14:12:52 +0200259 /*
Jens Axboe0a41e902010-04-16 21:11:21 +0200260 * take a breather
Borislav Petkov805ec582009-04-08 14:12:52 +0200261 */
Jens Axboe60033522018-10-26 09:53:52 -0600262 blk_mq_requeue_request(rq, false);
263 blk_mq_delay_kick_requeue_list(drive->queue, 1);
Borislav Petkov805ec582009-04-08 14:12:52 +0200264 return 1;
265 }
266}
267
Jens Axboe60033522018-10-26 09:53:52 -0600268static void ide_cd_free_sense(ide_drive_t *drive)
269{
270 if (!drive->sense_rq)
271 return;
272
273 blk_mq_free_request(drive->sense_rq);
274 drive->sense_rq = NULL;
275 drive->sense_rq_armed = false;
276}
277
Borislav Petkov805ec582009-04-08 14:12:52 +0200278/**
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200279 * Returns:
280 * 0: if the request should be continued.
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200281 * 1: if the request will be going through error recovery.
282 * 2: if the request should be ended.
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200283 */
Borislav Petkov8e59bfd2009-04-08 14:12:51 +0200284static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200286 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100287 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200288 int err, sense_key, do_end_request = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200290 /* get the IDE error register */
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100291 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 sense_key = err >> 4;
293
Borislav Petkov98036ab2009-04-08 14:12:53 +0200294 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, rq->cmd_type: 0x%x, err: 0x%x, "
295 "stat 0x%x",
296 rq->cmd[0], rq->cmd_type, err, stat);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200297
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100298 if (ata_sense_request(rq)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200299 /*
300 * We got an error trying to get sense info from the drive
301 * (probably while trying to recover from a former error).
302 * Just give up.
303 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200304 rq->rq_flags |= RQF_FAILED;
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200305 return 2;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200308 /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */
Christoph Hellwig17d53632017-04-20 16:03:01 +0200309 if (blk_rq_is_scsi(rq) && !scsi_req(rq)->result)
310 scsi_req(rq)->result = SAM_STAT_CHECK_CONDITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200312 if (blk_noretry_request(rq))
313 do_end_request = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200315 switch (sense_key) {
316 case NOT_READY:
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100317 if (req_op(rq) == REQ_OP_WRITE) {
Borislav Petkov3c8a48e2009-04-08 14:13:03 +0200318 if (ide_cd_breathe(drive, rq))
319 return 1;
320 } else {
Borislav Petkove5e076a2008-04-26 22:25:15 +0200321 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100323 if (!blk_rq_is_passthrough(rq) &&
Christoph Hellwige8064022016-10-20 15:12:13 +0200324 !(rq->rq_flags & RQF_QUIET))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200325 printk(KERN_ERR PFX "%s: tray open\n",
326 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200328 do_end_request = 1;
329 break;
330 case UNIT_ATTENTION:
331 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100333 if (blk_rq_is_passthrough(rq))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200334 return 0;
335
336 /*
337 * Arrange to retry the request but be sure to give up if we've
338 * retried too many times.
339 */
Christoph Hellwig17d53632017-04-20 16:03:01 +0200340 if (++scsi_req(rq)->result > ERROR_MAX)
Bartlomiej Zolnierkiewicz1920c482009-04-08 14:12:54 +0200341 do_end_request = 1;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200342 break;
343 case ILLEGAL_REQUEST:
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200344 /*
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200345 * Don't print error message for this condition -- SFF8090i
346 * indicates that 5/24/00 is the correct response to a request
347 * to close the tray if the drive doesn't have that capability.
348 *
349 * cdrom_log_sense() knows this!
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200350 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100351 if (scsi_req(rq)->cmd[0] == GPCMD_START_STOP_UNIT)
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200352 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500353 fallthrough;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200354 case DATA_PROTECT:
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100355 /*
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200356 * No point in retrying after an illegal request or data
357 * protect error.
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100358 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200359 if (!(rq->rq_flags & RQF_QUIET))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200360 ide_dump_status(drive, "command error", stat);
361 do_end_request = 1;
362 break;
363 case MEDIUM_ERROR:
364 /*
365 * No point in re-trying a zillion times on a bad sector.
366 * If we got here the error is not correctable.
367 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200368 if (!(rq->rq_flags & RQF_QUIET))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200369 ide_dump_status(drive, "media error "
370 "(bad sector)", stat);
371 do_end_request = 1;
372 break;
373 case BLANK_CHECK:
374 /* disk appears blank? */
Christoph Hellwige8064022016-10-20 15:12:13 +0200375 if (!(rq->rq_flags & RQF_QUIET))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200376 ide_dump_status(drive, "media error (blank)",
377 stat);
378 do_end_request = 1;
379 break;
380 default:
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100381 if (blk_rq_is_passthrough(rq))
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200382 break;
383 if (err & ~ATA_ABORTED) {
384 /* go to the default handler for other errors */
385 ide_error(drive, "cdrom_decode_status", stat);
386 return 1;
Christoph Hellwig17d53632017-04-20 16:03:01 +0200387 } else if (++scsi_req(rq)->result > ERROR_MAX)
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200388 /* we've racked up too many retries, abort */
389 do_end_request = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100392 if (blk_rq_is_passthrough(rq)) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200393 rq->rq_flags |= RQF_FAILED;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200394 do_end_request = 1;
395 }
396
397 /*
398 * End a request through request sense analysis when we have sense data.
399 * We need this in order to perform end of media processing.
400 */
401 if (do_end_request)
402 goto end_request;
403
404 /* if we got a CHECK_CONDITION status, queue a request sense command */
405 if (stat & ATA_ERR)
Tejun Heo02e7cf82009-04-19 07:00:42 +0900406 return ide_queue_sense_rq(drive, NULL) ? 2 : 1;
Bartlomiej Zolnierkiewicz674f0ea2009-04-08 14:12:54 +0200407 return 1;
408
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100409end_request:
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200410 if (stat & ATA_ERR) {
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100411 hwif->rq = NULL;
Tejun Heo02e7cf82009-04-19 07:00:42 +0900412 return ide_queue_sense_rq(drive, rq) ? 2 : 1;
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100413 } else
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200414 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200417static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100418{
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200419 struct request *rq = cmd->rq;
420
Borislav Petkov088b1b82009-01-02 13:34:47 +0100421 ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200422
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100423 /*
424 * Some of the trailing request sense fields are optional,
425 * and some drives don't send them. Sigh.
426 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100427 if (scsi_req(rq)->cmd[0] == GPCMD_REQUEST_SENSE &&
Tejun Heo59a4f6f2009-04-19 07:00:41 +0900428 cmd->nleft > 0 && cmd->nleft <= 5)
429 cmd->nleft = 0;
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100430}
431
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200432int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
433 int write, void *buffer, unsigned *bufflen,
Kees Cooke7d07482018-08-02 15:22:13 -0600434 struct scsi_sense_hdr *sshdr, int timeout,
Christoph Hellwige8064022016-10-20 15:12:13 +0200435 req_flags_t rq_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200437 struct cdrom_info *info = drive->driver_data;
Kees Cook7a6873be2018-07-31 12:51:51 -0700438 struct scsi_sense_hdr local_sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int retries = 10;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100440 bool failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Borislav Petkov088b1b82009-01-02 13:34:47 +0100442 ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, "
Christoph Hellwige8064022016-10-20 15:12:13 +0200443 "rq_flags: 0x%x",
444 cmd[0], write, timeout, rq_flags);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200445
Kees Cook7a6873be2018-07-31 12:51:51 -0700446 if (!sshdr)
447 sshdr = &local_sshdr;
448
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200449 /* start of retry loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 do {
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200451 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int error;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100453 bool delay = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100455 rq = blk_get_request(drive->queue,
Christoph Hellwigff005a02018-05-09 09:54:05 +0200456 write ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100457 memcpy(scsi_req(rq)->cmd, cmd, BLK_MAX_CDB);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100458 ide_req(rq)->type = ATA_PRIV_PC;
Christoph Hellwige8064022016-10-20 15:12:13 +0200459 rq->rq_flags |= rq_flags;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200460 rq->timeout = timeout;
461 if (buffer) {
Tejun Heo02e7cf82009-04-19 07:00:42 +0900462 error = blk_rq_map_kern(drive->queue, rq, buffer,
463 *bufflen, GFP_NOIO);
464 if (error) {
465 blk_put_request(rq);
466 return error;
467 }
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200468 }
469
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200470 blk_execute_rq(drive->queue, info->disk, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200471 error = scsi_req(rq)->result ? -EIO : 0;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200472
473 if (buffer)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100474 *bufflen = scsi_req(rq)->resid_len;
Kees Cook7a6873be2018-07-31 12:51:51 -0700475 scsi_normalize_sense(scsi_req(rq)->sense,
476 scsi_req(rq)->sense_len, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200478 /*
479 * FIXME: we should probably abort/retry or something in case of
480 * failure.
481 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100482 failed = (rq->rq_flags & RQF_FAILED) != 0;
483 if (failed) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200484 /*
485 * The request failed. Retry if it was due to a unit
486 * attention status (usually means media was changed).
487 */
Kees Cook7a6873be2018-07-31 12:51:51 -0700488 if (sshdr->sense_key == UNIT_ATTENTION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 cdrom_saw_media_change(drive);
Kees Cook7a6873be2018-07-31 12:51:51 -0700490 else if (sshdr->sense_key == NOT_READY &&
491 sshdr->asc == 4 && sshdr->ascq != 4) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200492 /*
493 * The drive is in the process of loading
494 * a disk. Retry, but wait a little to give
495 * the drive time to complete the load.
496 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100497 delay = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200499 /* otherwise, don't retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 retries = 0;
501 }
502 --retries;
503 }
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100504 blk_put_request(rq);
505 if (delay)
506 ssleep(2);
507 } while (failed && retries >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200509 /* return an error if the command failed */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100510 return failed ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Borislav Petkov11071282010-07-05 21:23:52 -0700513/*
514 * returns true if rq has been completed
515 */
516static bool ide_cd_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200517{
518 unsigned int nr_bytes = cmd->nbytes - cmd->nleft;
519
520 if (cmd->tf_flags & IDE_TFLAG_WRITE)
521 nr_bytes -= cmd->last_xfer_len;
522
Borislav Petkov11071282010-07-05 21:23:52 -0700523 if (nr_bytes > 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200524 ide_complete_rq(drive, BLK_STS_OK, nr_bytes);
Borislav Petkov11071282010-07-05 21:23:52 -0700525 return true;
526 }
527
528 return false;
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200529}
530
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100531/* standard prep_rq that builds 10 byte cmds */
532static bool ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
Jens Axboe60033522018-10-26 09:53:52 -0600533{
534 int hard_sect = queue_logical_block_size(q);
535 long block = (long)blk_rq_pos(rq) / (hard_sect >> 9);
536 unsigned long blocks = blk_rq_sectors(rq) / (hard_sect >> 9);
537 struct scsi_request *req = scsi_req(rq);
538
539 if (rq_data_dir(rq) == READ)
540 req->cmd[0] = GPCMD_READ_10;
541 else
542 req->cmd[0] = GPCMD_WRITE_10;
543
544 /*
545 * fill in lba
546 */
547 req->cmd[2] = (block >> 24) & 0xff;
548 req->cmd[3] = (block >> 16) & 0xff;
549 req->cmd[4] = (block >> 8) & 0xff;
550 req->cmd[5] = block & 0xff;
551
552 /*
553 * and transfer length
554 */
555 req->cmd[7] = (blocks >> 8) & 0xff;
556 req->cmd[8] = blocks & 0xff;
557 req->cmd_len = 10;
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100558 return true;
Jens Axboe60033522018-10-26 09:53:52 -0600559}
560
561/*
562 * Most of the SCSI commands are supported directly by ATAPI devices.
563 * This transform handles the few exceptions.
564 */
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100565static bool ide_cdrom_prep_pc(struct request *rq)
Jens Axboe60033522018-10-26 09:53:52 -0600566{
567 u8 *c = scsi_req(rq)->cmd;
568
569 /* transform 6-byte read/write commands to the 10-byte version */
570 if (c[0] == READ_6 || c[0] == WRITE_6) {
571 c[8] = c[4];
572 c[5] = c[3];
573 c[4] = c[2];
574 c[3] = c[1] & 0x1f;
575 c[2] = 0;
576 c[1] &= 0xe0;
577 c[0] += (READ_10 - READ_6);
578 scsi_req(rq)->cmd_len = 10;
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100579 return true;
Jens Axboe60033522018-10-26 09:53:52 -0600580 }
581
582 /*
583 * it's silly to pretend we understand 6-byte sense commands, just
584 * reject with ILLEGAL_REQUEST and the caller should take the
585 * appropriate action
586 */
587 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
588 scsi_req(rq)->result = ILLEGAL_REQUEST;
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100589 return false;
Jens Axboe60033522018-10-26 09:53:52 -0600590 }
591
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100592 return true;
Jens Axboe60033522018-10-26 09:53:52 -0600593}
594
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100595static bool ide_cdrom_prep_rq(ide_drive_t *drive, struct request *rq)
Jens Axboe60033522018-10-26 09:53:52 -0600596{
597 if (!blk_rq_is_passthrough(rq)) {
598 scsi_req_init(scsi_req(rq));
599
600 return ide_cdrom_prep_fs(drive->queue, rq);
601 } else if (blk_rq_is_scsi(rq))
602 return ide_cdrom_prep_pc(rq);
603
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100604 return true;
Jens Axboe60033522018-10-26 09:53:52 -0600605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
608{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200609 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200610 struct ide_cmd *cmd = &hwif->cmd;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100611 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100612 ide_expiry_t *expiry = NULL;
Borislav Petkov8e59bfd2009-04-08 14:12:51 +0200613 int dma_error = 0, dma, thislen, uptodate = 0;
Tejun Heo34b7d2c2009-05-07 22:24:43 +0900614 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0;
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100615 int sense = ata_sense_request(rq);
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100616 unsigned int timeout;
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200617 u16 len;
Borislav Petkov8e59bfd2009-04-08 14:12:51 +0200618 u8 ireason, stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Borislav Petkov98036ab2009-04-08 14:12:53 +0200620 ide_debug_log(IDE_DBG_PC, "cmd: 0x%x, write: 0x%x", rq->cmd[0], write);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200621
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200622 /* check for errors */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200623 dma = drive->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (dma) {
Borislav Petkov12469ac2008-10-13 21:39:49 +0200625 drive->dma = 0;
Bartlomiej Zolnierkiewicz88b41322009-03-31 20:15:22 +0200626 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200627 dma_error = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewiczf094d4d82009-03-31 20:15:24 +0200628 ide_dma_unmap_sg(drive, cmd);
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100629 if (dma_error) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200630 printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100631 write ? "write" : "read");
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100632 ide_dma_off(drive);
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
Borislav Petkov8e59bfd2009-04-08 14:12:51 +0200636 /* check status */
637 stat = hwif->tp_ops->read_status(hwif);
638
639 if (!OK_STAT(stat, 0, BAD_R_STAT)) {
640 rc = cdrom_decode_status(drive, stat);
641 if (rc) {
642 if (rc == 2)
643 goto out_end;
644 return ide_stopped;
645 }
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200648 /* using dma, transfer is complete now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (dma) {
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100650 if (dma_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return ide_error(drive, "dma error", stat);
Bartlomiej Zolnierkiewicz4a3d8cf2009-03-31 20:15:15 +0200652 uptodate = 1;
Bartlomiej Zolnierkiewicz984c5e52009-03-31 20:15:03 +0200653 goto out_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200656 ide_read_bcount_and_ireason(drive, &len, &ireason);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100657
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100658 thislen = !blk_rq_is_passthrough(rq) ? len : cmd->nleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (thislen > len)
660 thislen = len;
661
Borislav Petkov088b1b82009-01-02 13:34:47 +0100662 ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
663 stat, thislen);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200664
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200665 /* If DRQ is clear, the command has completed. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200666 if ((stat & ATA_DRQ) == 0) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100667 switch (req_op(rq)) {
668 default:
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100669 /*
670 * If we're not done reading/writing, complain.
671 * Otherwise, complete the command normally.
672 */
673 uptodate = 1;
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200674 if (cmd->nleft > 0) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200675 printk(KERN_ERR PFX "%s: %s: data underrun "
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200676 "(%u bytes)\n", drive->name, __func__,
677 cmd->nleft);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100678 if (!write)
Christoph Hellwige8064022016-10-20 15:12:13 +0200679 rq->rq_flags |= RQF_FAILED;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100680 uptodate = 0;
681 }
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100682 goto out_end;
683 case REQ_OP_DRV_IN:
684 case REQ_OP_DRV_OUT:
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200685 ide_cd_request_sense_fixup(drive, cmd);
Borislav Petkov9c72ebe2009-06-26 11:22:37 -0700686
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200687 uptodate = cmd->nleft ? 0 : 1;
Borislav Petkov9c72ebe2009-06-26 11:22:37 -0700688
689 /*
690 * suck out the remaining bytes from the drive in an
691 * attempt to complete the data xfer. (see BZ#13399)
692 */
693 if (!(stat & ATA_ERR) && !uptodate && thislen) {
694 ide_pio_bytes(drive, cmd, write, thislen);
695 uptodate = cmd->nleft ? 0 : 1;
696 }
697
698 if (!uptodate)
Christoph Hellwige8064022016-10-20 15:12:13 +0200699 rq->rq_flags |= RQF_FAILED;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100700 goto out_end;
701 case REQ_OP_SCSI_IN:
702 case REQ_OP_SCSI_OUT:
703 goto out_end;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100704 }
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -0500705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Borislav Petkov103f7032009-04-26 10:39:07 +0200707 rc = ide_check_ireason(drive, rq, len, ireason, write);
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200708 if (rc)
709 goto out_end;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100710
Bartlomiej Zolnierkiewicz06a449e2009-03-31 20:15:13 +0200711 cmd->last_xfer_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Borislav Petkov088b1b82009-01-02 13:34:47 +0100713 ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
714 "ireason: 0x%x",
715 rq->cmd_type, ireason);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200716
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200717 /* transfer data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 while (thislen > 0) {
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200719 int blen = min_t(int, thislen, cmd->nleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Bartlomiej Zolnierkiewicz14fa91c2009-03-31 20:15:16 +0200721 if (cmd->nleft == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200724 ide_pio_bytes(drive, cmd, write, blen);
725 cmd->last_xfer_len += blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 thislen -= blen;
728 len -= blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200730 if (sense && write == 0)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100731 scsi_req(rq)->sense_len += blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200734 /* pad, if necessary */
Bartlomiej Zolnierkiewicz14fa91c2009-03-31 20:15:16 +0200735 if (len > 0) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100736 if (blk_rq_is_passthrough(rq) || write == 0)
Bartlomiej Zolnierkiewicz14fa91c2009-03-31 20:15:16 +0200737 ide_pad_transfer(drive, write, len);
738 else {
739 printk(KERN_ERR PFX "%s: confused, missing data\n",
740 drive->name);
741 blk_dump_rq_flags(rq, "cdrom_newpc_intr");
742 }
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100745 switch (req_op(rq)) {
746 case REQ_OP_SCSI_IN:
747 case REQ_OP_SCSI_OUT:
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100748 timeout = rq->timeout;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100749 break;
750 case REQ_OP_DRV_IN:
751 case REQ_OP_DRV_OUT:
752 expiry = ide_cd_expiry;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500753 fallthrough;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100754 default:
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100755 timeout = ATAPI_WAIT_PC;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100756 break;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100757 }
758
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100759 hwif->expiry = expiry;
760 ide_set_handler(drive, cdrom_newpc_intr, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return ide_started;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100762
Bartlomiej Zolnierkiewicz984c5e52009-03-31 20:15:03 +0200763out_end:
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100764 if (blk_rq_is_scsi(rq) && rc == 0) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100765 scsi_req(rq)->resid_len = 0;
Jens Axboe60033522018-10-26 09:53:52 -0600766 blk_mq_end_request(rq, BLK_STS_OK);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100767 hwif->rq = NULL;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100768 } else {
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200769 if (sense && uptodate)
770 ide_cd_complete_failed_rq(drive, rq);
771
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100772 if (!blk_rq_is_passthrough(rq)) {
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200773 if (cmd->nleft == 0)
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200774 uptodate = 1;
775 } else {
Christoph Hellwig17d53632017-04-20 16:03:01 +0200776 if (uptodate <= 0 && scsi_req(rq)->result == 0)
777 scsi_req(rq)->result = -EIO;
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200778 }
779
Rainer Weikusat39c58f32009-06-18 17:04:00 +0200780 if (uptodate == 0 && rq->bio)
Borislav Petkov11071282010-07-05 21:23:52 -0700781 if (ide_cd_error_cmd(drive, cmd))
782 return ide_stopped;
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200783
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200784 /* make sure it's fully ended */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100785 if (blk_rq_is_passthrough(rq)) {
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100786 scsi_req(rq)->resid_len -= cmd->nbytes - cmd->nleft;
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200787 if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100788 scsi_req(rq)->resid_len += cmd->last_xfer_len;
Bartlomiej Zolnierkiewiczc7ec8992009-03-31 20:15:15 +0200789 }
790
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200791 ide_complete_rq(drive, uptodate ? BLK_STS_OK : BLK_STS_IOERR, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200792
Bartlomiej Zolnierkiewicz8a116972009-03-31 20:15:03 +0200793 if (sense && rc == 2)
794 ide_error(drive, "request sense failure", stat);
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100795 }
Jens Axboe60033522018-10-26 09:53:52 -0600796
797 ide_cd_free_sense(drive);
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100798 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100801static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100803 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200804 struct request_queue *q = drive->queue;
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100805 int write = rq_data_dir(rq) == WRITE;
806 unsigned short sectors_per_frame =
Bart Van Assche233bde22018-03-14 15:48:06 -0700807 queue_logical_block_size(q) >> SECTOR_SHIFT;
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100808
Bartlomiej Zolnierkiewicz8652b312009-03-31 20:15:14 +0200809 ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, "
Borislav Petkov088b1b82009-01-02 13:34:47 +0100810 "secs_per_frame: %u",
Bartlomiej Zolnierkiewicz8652b312009-03-31 20:15:14 +0200811 rq->cmd[0], rq->cmd_flags, sectors_per_frame);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200812
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100813 if (write) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200814 /* disk has become write protected */
Bartlomiej Zolnierkiewicze0458cc2009-03-31 20:15:02 +0200815 if (get_disk_ro(cd->disk))
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100816 return ide_stopped;
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100817 } else {
818 /*
819 * We may be retrying this request after an error. Fix up any
820 * weirdness which might be present in the request packet.
821 */
Christoph Hellwig535ac5d2018-11-09 14:42:35 +0100822 ide_cdrom_prep_rq(drive, rq);
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200825 /* fs requests *must* be hardware frame aligned */
Tejun Heo9780e2dd2009-05-07 22:24:40 +0900826 if ((blk_rq_sectors(rq) & (sectors_per_frame - 1)) ||
827 (blk_rq_pos(rq) & (sectors_per_frame - 1)))
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200828 return ide_stopped;
829
830 /* use DMA, if possible */
831 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +0100833 if (write)
834 cd->devinfo.media_written = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Bartlomiej Zolnierkiewicz8652b312009-03-31 20:15:14 +0200836 rq->timeout = ATAPI_WAIT_PC;
837
Borislav Petkovb6ca4402008-07-16 20:33:46 +0200838 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Borislav Petkovb6ca4402008-07-16 20:33:46 +0200841static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Borislav Petkov088b1b82009-01-02 13:34:47 +0100844 ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
845 rq->cmd[0], rq->cmd_type);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200846
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100847 if (blk_rq_is_scsi(rq))
Christoph Hellwige8064022016-10-20 15:12:13 +0200848 rq->rq_flags |= RQF_QUIET;
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +0100849 else
Christoph Hellwige8064022016-10-20 15:12:13 +0200850 rq->rq_flags &= ~RQF_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Borislav Petkov12469ac2008-10-13 21:39:49 +0200852 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200854 /* sg request */
Tejun Heo02e7cf82009-04-19 07:00:42 +0900855 if (rq->bio) {
FUJITA Tomonorie5318b52008-07-16 20:33:35 +0200856 struct request_queue *q = drive->queue;
Tejun Heo02e7cf82009-04-19 07:00:42 +0900857 char *buf = bio_data(rq->bio);
FUJITA Tomonorie5318b52008-07-16 20:33:35 +0200858 unsigned int alignment;
FUJITA Tomonorie5318b52008-07-16 20:33:35 +0200859
Borislav Petkov12469ac2008-10-13 21:39:49 +0200860 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /*
863 * check if dma is safe
Linus Torvalds5d9e4ea2005-05-27 07:36:17 -0700864 *
865 * NOTE! The "len" and "addr" checks should possibly have
866 * separate masks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
FUJITA Tomonorie5318b52008-07-16 20:33:35 +0200868 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
Borislav Petkov9bd27cb2008-11-02 21:40:07 +0100869 if ((unsigned long)buf & alignment
Tejun Heo34b7d2c2009-05-07 22:24:43 +0900870 || blk_rq_bytes(rq) & q->dma_pad_mask
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +0200871 || object_is_on_stack(buf))
Borislav Petkov12469ac2008-10-13 21:39:49 +0200872 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Borislav Petkov99384ae2008-07-16 20:33:45 +0200876static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
Borislav Petkove5e076a2008-04-26 22:25:15 +0200877 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100879 struct ide_cmd cmd;
Connor Hansen3307d0d2011-06-11 15:06:48 -0700880 int uptodate = 0;
881 unsigned int nsectors;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100882
Borislav Petkov088b1b82009-01-02 13:34:47 +0100883 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
884 rq->cmd[0], (unsigned long long)block);
885
886 if (drive->debug_mask & IDE_DBG_RQ)
887 blk_dump_rq_flags(rq, "ide_cd_do_request");
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200888
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100889 switch (req_op(rq)) {
890 default:
Bartlomiej Zolnierkiewicz8652b312009-03-31 20:15:14 +0200891 if (cdrom_start_rw(drive, rq) == ide_stopped)
Bartlomiej Zolnierkiewicze0458cc2009-03-31 20:15:02 +0200892 goto out_end;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200893 break;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100894 case REQ_OP_SCSI_IN:
895 case REQ_OP_SCSI_OUT:
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100896 handle_pc:
Borislav Petkov7fcebda2008-07-16 20:33:46 +0200897 if (!rq->timeout)
898 rq->timeout = ATAPI_WAIT_PC;
Borislav Petkovb6ca4402008-07-16 20:33:46 +0200899 cdrom_do_block_pc(drive, rq);
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200900 break;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100901 case REQ_OP_DRV_IN:
902 case REQ_OP_DRV_OUT:
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100903 switch (ide_req(rq)->type) {
904 case ATA_PRIV_MISC:
905 /* right now this can only be a reset... */
906 uptodate = 1;
907 goto out_end;
908 case ATA_PRIV_SENSE:
909 case ATA_PRIV_PC:
910 goto handle_pc;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100911 default:
912 BUG();
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100913 }
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200914 }
Borislav Petkovb6ca4402008-07-16 20:33:46 +0200915
Borislav Petkovc457ce82009-04-19 07:00:42 +0900916 /* prepare sense request for this command */
917 ide_prep_sense(drive, rq);
918
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100919 memset(&cmd, 0, sizeof(cmd));
920
921 if (rq_data_dir(rq))
922 cmd.tf_flags |= IDE_TFLAG_WRITE;
923
924 cmd.rq = rq;
925
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100926 if (!blk_rq_is_passthrough(rq) || blk_rq_bytes(rq)) {
Tejun Heo34b7d2c2009-05-07 22:24:43 +0900927 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicza08915b2009-03-31 20:15:13 +0200928 ide_map_sg(drive, &cmd);
929 }
930
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100931 return ide_issue_pc(drive, &cmd);
Bartlomiej Zolnierkiewicze0458cc2009-03-31 20:15:02 +0200932out_end:
Tejun Heo5b936292009-05-07 22:24:38 +0900933 nsectors = blk_rq_sectors(rq);
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200934
935 if (nsectors == 0)
936 nsectors = 1;
937
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200938 ide_complete_rq(drive, uptodate ? BLK_STS_OK : BLK_STS_IOERR, nsectors << 9);
Bartlomiej Zolnierkiewiczf63174e2009-03-31 20:15:04 +0200939
Bartlomiej Zolnierkiewicze0458cc2009-03-31 20:15:02 +0200940 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200943/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * Ioctl handling.
945 *
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200946 * Routines which queue packet commands take as a final argument a pointer to a
947 * request_sense struct. If execution of the command results in an error with a
948 * CHECK CONDITION status, this structure will be filled with the results of the
949 * subsequent request sense command. The pointer can also be NULL, in which case
950 * no sense information is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Borislav Petkove5e076a2008-04-26 22:25:15 +0200952static void msf_from_bcd(struct atapi_msf *msf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Adrian Bunkfc99856a2008-08-18 21:40:04 +0200954 msf->minute = bcd2bin(msf->minute);
955 msf->second = bcd2bin(msf->second);
956 msf->frame = bcd2bin(msf->frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Kees Cooke7d07482018-08-02 15:22:13 -0600959int cdrom_check_status(ide_drive_t *drive, struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct cdrom_info *info = drive->driver_data;
Borislav Petkovfbc3edf2017-11-07 17:37:24 +0100962 struct cdrom_device_info *cdi;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200963 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Borislav Petkov088b1b82009-01-02 13:34:47 +0100965 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200966
Borislav Petkovfbc3edf2017-11-07 17:37:24 +0100967 if (!info)
968 return -EIO;
969
970 cdi = &info->devinfo;
971
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200972 memset(cmd, 0, BLK_MAX_CDB);
973 cmd[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +0100975 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200976 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
977 * instead of supporting the LOAD_UNLOAD opcode.
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +0100978 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200979 cmd[7] = cdi->sanyo_slot % 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Kees Cooke7d07482018-08-02 15:22:13 -0600981 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sshdr, 0, RQF_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
Kees Cook54f8a7a2018-07-31 12:51:46 -0700985 unsigned long *sectors_per_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct {
Harvey Harrison141d3b22008-07-23 19:56:02 +0200988 __be32 lba;
989 __be32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 } capbuf;
991
992 int stat;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200993 unsigned char cmd[BLK_MAX_CDB];
994 unsigned len = sizeof(capbuf);
Petr Tesarik938bb032008-08-05 18:17:02 +0200995 u32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Borislav Petkov088b1b82009-01-02 13:34:47 +0100997 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200998
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200999 memset(cmd, 0, BLK_MAX_CDB);
1000 cmd[0] = GPCMD_READ_CDVD_CAPACITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Kees Cook54f8a7a2018-07-31 12:51:46 -07001002 stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, NULL, 0,
Christoph Hellwige8064022016-10-20 15:12:13 +02001003 RQF_QUIET);
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001004 if (stat)
1005 return stat;
1006
1007 /*
David S. Milleraf054ed2009-06-23 16:01:06 -07001008 * Sanity check the given block size, in so far as making
1009 * sure the sectors_per_frame we give to the caller won't
1010 * end up being bogus.
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001011 */
Petr Tesarik938bb032008-08-05 18:17:02 +02001012 blocklen = be32_to_cpu(capbuf.blocklen);
Bart Van Assche233bde22018-03-14 15:48:06 -07001013 blocklen = (blocklen >> SECTOR_SHIFT) << SECTOR_SHIFT;
Petr Tesarik938bb032008-08-05 18:17:02 +02001014 switch (blocklen) {
1015 case 512:
1016 case 1024:
1017 case 2048:
1018 case 4096:
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001019 break;
1020 default:
Frans Popd9ae6242009-06-23 16:02:58 -07001021 printk_once(KERN_ERR PFX "%s: weird block size %u; "
1022 "setting default block size to 2048\n",
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001023 drive->name, blocklen);
Petr Tesarik938bb032008-08-05 18:17:02 +02001024 blocklen = 2048;
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001028 *capacity = 1 + be32_to_cpu(capbuf.lba);
Bart Van Assche233bde22018-03-14 15:48:06 -07001029 *sectors_per_frame = blocklen >> SECTOR_SHIFT;
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001030
Borislav Petkov088b1b82009-01-02 13:34:47 +01001031 ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
1032 *capacity, *sectors_per_frame);
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001033
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001037static int ide_cdrom_read_tocentry(ide_drive_t *drive, int trackno,
1038 int msf_flag, int format, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001040 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Borislav Petkov088b1b82009-01-02 13:34:47 +01001042 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001043
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001044 memset(cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001046 cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1047 cmd[6] = trackno;
1048 cmd[7] = (buflen >> 8);
1049 cmd[8] = (buflen & 0xff);
1050 cmd[9] = (format << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (msf_flag)
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001053 cmd[1] = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Kees Cook54f8a7a2018-07-31 12:51:46 -07001055 return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, NULL, 0, RQF_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/* Try to read the entire TOC for the disk into our internal buffer. */
Kees Cook54f8a7a2018-07-31 12:51:46 -07001059int ide_cd_read_toc(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int stat, ntracks, i;
1062 struct cdrom_info *info = drive->driver_data;
1063 struct cdrom_device_info *cdi = &info->devinfo;
1064 struct atapi_toc *toc = info->toc;
1065 struct {
1066 struct atapi_toc_header hdr;
1067 struct atapi_toc_entry ent;
1068 } ms_tmp;
1069 long last_written;
1070 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1071
Borislav Petkov088b1b82009-01-02 13:34:47 +01001072 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (toc == NULL) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001075 /* try to allocate space */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001076 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (toc == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001078 printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001079 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -ENOMEM;
1081 }
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001082 info->toc = toc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001085 /*
1086 * Check to see if the existing data is still valid. If it is,
1087 * just return.
1088 */
Kees Cook54f8a7a2018-07-31 12:51:46 -07001089 (void) cdrom_check_status(drive, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Borislav Petkov570f89e2008-07-23 19:56:02 +02001091 if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
1093
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001094 /* try to get the total cdrom capacity and sector size */
Kees Cook54f8a7a2018-07-31 12:51:46 -07001095 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (stat)
1097 toc->capacity = 0x1fffff;
1098
1099 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001100 /* save a private copy of the TOC capacity for error handling */
Alan Coxdbe217a2006-06-25 05:47:44 -07001101 drive->probed_capacity = toc->capacity * sectors_per_frame;
1102
Martin K. Petersene1defc42009-05-22 17:17:49 -04001103 blk_queue_logical_block_size(drive->queue,
Bart Van Assche233bde22018-03-14 15:48:06 -07001104 sectors_per_frame << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001106 /* first read just the header, so we know how long the TOC is */
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001107 stat = ide_cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
Kees Cook54f8a7a2018-07-31 12:51:46 -07001108 sizeof(struct atapi_toc_header));
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001109 if (stat)
1110 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Borislav Petkov570f89e2008-07-23 19:56:02 +02001112 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001113 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1114 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1118 if (ntracks <= 0)
1119 return -EIO;
1120 if (ntracks > MAX_TRACKS)
1121 ntracks = MAX_TRACKS;
1122
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001123 /* now read the whole schmeer */
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001124 stat = ide_cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 (char *)&toc->hdr,
1126 sizeof(struct atapi_toc_header) +
1127 (ntracks + 1) *
Kees Cook54f8a7a2018-07-31 12:51:46 -07001128 sizeof(struct atapi_toc_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (stat && toc->hdr.first_track > 1) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001131 /*
1132 * Cds with CDI tracks only don't have any TOC entries, despite
1133 * of this the returned values are
1134 * first_track == last_track = number of CDI tracks + 1,
1135 * so that this case is indistinguishable from the same layout
1136 * plus an additional audio track. If we get an error for the
1137 * regular case, we assume a CDI without additional audio
1138 * tracks. In this case the readable TOC is empty (CDI tracks
1139 * are not included) and only holds the Leadout entry.
1140 *
1141 * Heiko Eißfeldt.
1142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ntracks = 0;
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001144 stat = ide_cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 (char *)&toc->hdr,
1146 sizeof(struct atapi_toc_header) +
1147 (ntracks + 1) *
Kees Cook54f8a7a2018-07-31 12:51:46 -07001148 sizeof(struct atapi_toc_entry));
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001149 if (stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return stat;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001151
Borislav Petkov570f89e2008-07-23 19:56:02 +02001152 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001153 toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
1154 toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001155 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 toc->hdr.first_track = CDROM_LEADOUT;
1157 toc->hdr.last_track = CDROM_LEADOUT;
1158 }
1159 }
1160
1161 if (stat)
1162 return stat;
1163
Borislav Petkov7eb43fd2008-02-11 00:32:13 +01001164 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Borislav Petkov570f89e2008-07-23 19:56:02 +02001166 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001167 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1168 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001171 for (i = 0; i <= ntracks; i++) {
Borislav Petkov570f89e2008-07-23 19:56:02 +02001172 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1173 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001174 toc->ent[i].track = bcd2bin(toc->ent[i].track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 msf_from_bcd(&toc->ent[i].addr.msf);
1176 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001177 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
1178 toc->ent[i].addr.msf.second,
1179 toc->ent[i].addr.msf.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (toc->hdr.first_track != CDROM_LEADOUT) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001183 /* read the multisession information */
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001184 stat = ide_cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
Kees Cook54f8a7a2018-07-31 12:51:46 -07001185 sizeof(ms_tmp));
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001186 if (stat)
1187 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1190 } else {
Borislav Petkove5e076a2008-04-26 22:25:15 +02001191 ms_tmp.hdr.last_track = CDROM_LEADOUT;
1192 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1194 }
1195
Borislav Petkov570f89e2008-07-23 19:56:02 +02001196 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001197 /* re-read multisession information using MSF format */
Christoph Hellwig068f8d92020-04-25 09:57:01 +02001198 stat = ide_cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
Kees Cook54f8a7a2018-07-31 12:51:46 -07001199 sizeof(ms_tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (stat)
1201 return stat;
1202
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001203 msf_from_bcd(&ms_tmp.ent.addr.msf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001205 ms_tmp.ent.addr.msf.second,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 ms_tmp.ent.addr.msf.frame);
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1210
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001211 /* now try to get the total cdrom capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 stat = cdrom_get_last_written(cdi, &last_written);
1213 if (!stat && (last_written > toc->capacity)) {
1214 toc->capacity = last_written;
1215 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Alan Coxdbe217a2006-06-25 05:47:44 -07001216 drive->probed_capacity = toc->capacity * sectors_per_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
1219 /* Remember that we've read this stuff. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001220 drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 return 0;
1223}
1224
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001225int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001226{
1227 struct cdrom_info *info = drive->driver_data;
1228 struct cdrom_device_info *cdi = &info->devinfo;
1229 struct packet_command cgc;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001230 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001231
Borislav Petkov088b1b82009-01-02 13:34:47 +01001232 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001233
Borislav Petkov570f89e2008-07-23 19:56:02 +02001234 if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001235 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001236
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001237 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001238 do {
1239 /* we seem to get stat=0x01,err=0x00 the first time (??) */
Eric Piel9235e682005-06-23 00:10:29 -07001240 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1241 if (!stat)
1242 break;
1243 } while (--attempts);
1244 return stat;
1245}
1246
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001247void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001248{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001249 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001250 u16 curspeed, maxspeed;
1251
Borislav Petkov088b1b82009-01-02 13:34:47 +01001252 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001253
Borislav Petkov570f89e2008-07-23 19:56:02 +02001254 if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001255 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
1256 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001257 } else {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001258 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
1259 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001260 }
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001261
Borislav Petkov088b1b82009-01-02 13:34:47 +01001262 ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
1263 curspeed, maxspeed);
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001264
Julia Lawall72db37b2009-08-02 13:19:05 -07001265 cd->current_speed = DIV_ROUND_CLOSEST(curspeed, 176);
1266 cd->max_speed = DIV_ROUND_CLOSEST(maxspeed, 176);
Eric Piel9235e682005-06-23 00:10:29 -07001267}
1268
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001269#define IDE_CD_CAPABILITIES \
1270 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1271 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1272 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1273 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1274 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Kees Cook853fe1b2017-02-13 16:25:26 -08001276static const struct cdrom_device_ops ide_cdrom_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 .open = ide_cdrom_open_real,
1278 .release = ide_cdrom_release_real,
1279 .drive_status = ide_cdrom_drive_status,
Tejun Heo5b03a1b2011-03-09 19:54:27 +01001280 .check_events = ide_cdrom_check_events_real,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 .tray_move = ide_cdrom_tray_move,
1282 .lock_door = ide_cdrom_lock_door,
1283 .select_speed = ide_cdrom_select_speed,
1284 .get_last_session = ide_cdrom_get_last_session,
1285 .get_mcn = ide_cdrom_get_mcn,
1286 .reset = ide_cdrom_reset,
1287 .audio_ioctl = ide_cdrom_audio_ioctl,
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001288 .capability = IDE_CD_CAPABILITIES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 .generic_packet = ide_cdrom_packet,
1290};
1291
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001292static int ide_cdrom_register(ide_drive_t *drive, int nslots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct cdrom_info *info = drive->driver_data;
1295 struct cdrom_device_info *devinfo = &info->devinfo;
1296
Borislav Petkov088b1b82009-01-02 13:34:47 +01001297 ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 devinfo->ops = &ide_cdrom_dops;
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001300 devinfo->speed = info->current_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 devinfo->capacity = nslots;
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001302 devinfo->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 strcpy(devinfo->name, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Borislav Petkov570f89e2008-07-23 19:56:02 +02001305 if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
Bartlomiej Zolnierkiewicz3cbd8142007-12-24 15:23:43 +01001306 devinfo->mask |= CDC_SELECT_SPEED;
1307
Christoph Hellwiga711d912020-04-25 09:57:00 +02001308 return register_cdrom(info->disk, devinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Borislav Petkove5e076a2008-04-26 22:25:15 +02001311static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001313 struct cdrom_info *cd = drive->driver_data;
1314 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001315 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1316 mechtype_t mechtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int nslots = 1;
1318
Borislav Petkov088b1b82009-01-02 13:34:47 +01001319 ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
1320 drive->media, drive->atapi_flags);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001321
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001322 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1323 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1324 CDC_MO_DRIVE | CDC_RAM);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (drive->media == ide_optical) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001327 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001328 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001329 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return nslots;
1331 }
1332
Borislav Petkov570f89e2008-07-23 19:56:02 +02001333 if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
1334 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001335 cdi->mask &= ~CDC_PLAY_AUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return nslots;
1337 }
1338
1339 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001340 * We have to cheat a little here. the packet will eventually be queued
1341 * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
1342 * Since this device hasn't been registered with the Uniform layer yet,
1343 * it can't do this. Same goes for cdi->ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001345 cdi->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 cdi->ops = &ide_cdrom_dops;
1347
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001348 if (ide_cdrom_get_capabilities(drive, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001351 if ((buf[8 + 6] & 0x01) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001352 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001353 if (buf[8 + 6] & 0x08)
Borislav Petkov570f89e2008-07-23 19:56:02 +02001354 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001355 if (buf[8 + 3] & 0x01)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001356 cdi->mask &= ~CDC_CD_R;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001357 if (buf[8 + 3] & 0x02)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001358 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001359 if (buf[8 + 2] & 0x38)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001360 cdi->mask &= ~CDC_DVD;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001361 if (buf[8 + 3] & 0x20)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001362 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001363 if (buf[8 + 3] & 0x10)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001364 cdi->mask &= ~CDC_DVD_R;
Borislav Petkov570f89e2008-07-23 19:56:02 +02001365 if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001366 cdi->mask &= ~CDC_PLAY_AUDIO;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001367
1368 mechtype = buf[8 + 6] >> 5;
Borislav Petkovf20f2582008-10-05 18:23:27 +02001369 if (mechtype == mechtype_caddy ||
1370 mechtype == mechtype_popup ||
1371 (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001372 cdi->mask |= CDC_CLOSE_TRAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (cdi->sanyo_slot > 0) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001375 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 nslots = 3;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001377 } else if (mechtype == mechtype_individual_changer ||
1378 mechtype == mechtype_cartridge_changer) {
Bartlomiej Zolnierkiewicz2609d062008-02-01 23:09:19 +01001379 nslots = cdrom_number_of_slots(cdi);
1380 if (nslots > 1)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001381 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001384 ide_cdrom_update_speed(drive, buf);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001385
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001386 printk(KERN_INFO PFX "%s: ATAPI", drive->name);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001387
1388 /* don't print speed if the drive reported 0 */
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001389 if (cd->max_speed)
1390 printk(KERN_CONT " %dX", cd->max_speed);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001391
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001392 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001394 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1395 printk(KERN_CONT " DVD%s%s",
1396 (cdi->mask & CDC_DVD_R) ? "" : "-R",
Borislav Petkov419a5b62008-10-17 18:09:14 +02001397 (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001399 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1400 printk(KERN_CONT " CD%s%s",
1401 (cdi->mask & CDC_CD_R) ? "" : "-R",
1402 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001404 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1405 printk(KERN_CONT " changer w/%d slots", nslots);
1406 else
1407 printk(KERN_CONT " drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001409 printk(KERN_CONT ", %dkB Cache\n",
1410 be16_to_cpup((__be16 *)&buf[8 + 12]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return nslots;
1413}
1414
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001415struct cd_list_entry {
1416 const char *id_model;
1417 const char *id_firmware;
1418 unsigned int cd_flags;
1419};
1420
Borislav Petkov5e657a92008-04-26 22:25:15 +02001421#ifdef CONFIG_IDE_PROC_FS
1422static sector_t ide_cdrom_capacity(ide_drive_t *drive)
1423{
1424 unsigned long capacity, sectors_per_frame;
1425
Kees Cook54f8a7a2018-07-31 12:51:46 -07001426 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame))
Borislav Petkov5e657a92008-04-26 22:25:15 +02001427 return 0;
1428
1429 return capacity * sectors_per_frame;
1430}
1431
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001432static int idecd_capacity_proc_show(struct seq_file *m, void *v)
Borislav Petkov5e657a92008-04-26 22:25:15 +02001433{
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001434 ide_drive_t *drive = m->private;
Borislav Petkov5e657a92008-04-26 22:25:15 +02001435
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001436 seq_printf(m, "%llu\n", (long long)ide_cdrom_capacity(drive));
1437 return 0;
Borislav Petkov5e657a92008-04-26 22:25:15 +02001438}
1439
1440static ide_proc_entry_t idecd_proc[] = {
Christoph Hellwigec7d9c92018-04-13 21:25:54 +02001441 { "capacity", S_IFREG|S_IRUGO, idecd_capacity_proc_show },
Alexey Dobriyan6d703a82009-09-01 17:52:57 -07001442 {}
Borislav Petkov5e657a92008-04-26 22:25:15 +02001443};
1444
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001445static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
1446{
1447 return idecd_proc;
1448}
1449
1450static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
1451{
Bartlomiej Zolnierkiewicz519d6802008-12-29 20:27:38 +01001452 return NULL;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001453}
Borislav Petkov5e657a92008-04-26 22:25:15 +02001454#endif
1455
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001456static const struct cd_list_entry ide_cd_quirks_list[] = {
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001457 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001458 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001459 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001460 { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
1461 IDE_AFLAG_PRE_ATAPI12, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001462 /* Vertos 300, some versions of this drive like to talk BCD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001463 { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001464 /* Vertos 600 ESD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001465 { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001466 /*
1467 * Sanyo 3 CD changer uses a non-standard command for CD changing
1468 * (by default standard ATAPI support for CD changers is used).
1469 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001470 { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
1471 { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
1472 { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001473 /* Stingray 8X CD-ROM. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001474 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001475 /*
1476 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1477 * mode sense page capabilities size, but older drives break.
1478 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001479 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
1480 { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001481 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001482 { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001483 /*
1484 * Some drives used by Apple don't advertise audio play
1485 * but they do support reading TOC & audio datas.
1486 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001487 { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1488 { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1489 { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1490 { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1491 { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Bodo Eggertf3e85ee2008-10-05 18:23:28 +02001492 { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Borislav Petkovf20f2582008-10-05 18:23:27 +02001493 { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Márton Németh64c2eae2008-10-23 23:22:09 +02001494 { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001495 { NULL, NULL, 0 }
1496};
1497
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001498static unsigned int ide_cd_flags(u16 *id)
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001499{
1500 const struct cd_list_entry *cle = ide_cd_quirks_list;
1501
1502 while (cle->id_model) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001503 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001504 (cle->id_firmware == NULL ||
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001505 strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001506 return cle->cd_flags;
1507 cle++;
1508 }
1509
1510 return 0;
1511}
1512
Borislav Petkove5e076a2008-04-26 22:25:15 +02001513static int ide_cdrom_setup(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001515 struct cdrom_info *cd = drive->driver_data;
1516 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicze698ea82009-03-31 20:15:16 +02001517 struct request_queue *q = drive->queue;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001518 u16 *id = drive->id;
1519 char *fw_rev = (char *)&id[ATA_ID_FW_REV];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int nslots;
1521
Borislav Petkov088b1b82009-01-02 13:34:47 +01001522 ide_debug_log(IDE_DBG_PROBE, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001523
Christoph Hellwig535ac5d2018-11-09 14:42:35 +01001524 drive->prep_rq = ide_cdrom_prep_rq;
Bartlomiej Zolnierkiewicze698ea82009-03-31 20:15:16 +02001525 blk_queue_dma_alignment(q, 31);
1526 blk_queue_update_dma_pad(q, 15);
1527
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +02001528 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
1529 drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Borislav Petkov570f89e2008-07-23 19:56:02 +02001531 if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001532 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02001533 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
1534 IDE_AFLAG_TOCADDR_AS_BCD);
1535 else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001536 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02001537 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
1538 else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001539 /* 3 => use CD in slot 0 */
1540 cdi->sanyo_slot = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001542 nslots = ide_cdrom_probe_capabilities(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Martin K. Petersene1defc42009-05-22 17:17:49 -04001544 blk_queue_logical_block_size(q, CD_FRAMESIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (ide_cdrom_register(drive, nslots)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001547 printk(KERN_ERR PFX "%s: %s failed to register device with the"
Borislav Petkov83c85652008-04-26 22:25:15 +02001548 " cdrom driver.\n", drive->name, __func__);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001549 cd->devinfo.handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return 1;
1551 }
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001552
1553 ide_proc_register_driver(drive, cd->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
1556
Russell King4031bbe2006-01-06 11:41:00 +00001557static void ide_cd_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct cdrom_info *info = drive->driver_data;
1560
Borislav Petkov088b1b82009-01-02 13:34:47 +01001561 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001562
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001563 ide_proc_unregister_driver(drive, info->driver);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001564 device_del(&info->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 del_gendisk(info->disk);
1566
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001567 mutex_lock(&idecd_ref_mutex);
1568 put_device(&info->dev);
1569 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001572static void ide_cd_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001574 struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct cdrom_device_info *devinfo = &info->devinfo;
1576 ide_drive_t *drive = info->drive;
1577 struct gendisk *g = info->disk;
1578
Borislav Petkov088b1b82009-01-02 13:34:47 +01001579 ide_debug_log(IDE_DBG_FUNC, "enter");
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001580
Jesper Juhl6044ec82005-11-07 01:01:32 -08001581 kfree(info->toc);
Akinobu Mita0a0c4112008-03-26 12:09:02 +01001582 if (devinfo->handle == drive)
1583 unregister_cdrom(devinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 drive->driver_data = NULL;
Jens Axboe60033522018-10-26 09:53:52 -06001585 drive->prep_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 g->private_data = NULL;
1587 put_disk(g);
1588 kfree(info);
1589}
1590
Russell King4031bbe2006-01-06 11:41:00 +00001591static int ide_cd_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +01001593static struct ide_driver ide_cdrom_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001594 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001595 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001596 .name = "ide-cdrom",
1597 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001598 },
Russell King4031bbe2006-01-06 11:41:00 +00001599 .probe = ide_cd_probe,
1600 .remove = ide_cd_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 .version = IDECD_VERSION,
Borislav Petkov99384ae2008-07-16 20:33:45 +02001602 .do_request = ide_cd_do_request,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001603#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001604 .proc_entries = ide_cd_proc_entries,
1605 .proc_devsets = ide_cd_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607};
1608
Al Viro488ca602008-03-02 10:26:23 -05001609static int idecd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001611 struct cdrom_info *info;
1612 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Christoph Hellwiga367e442020-09-08 16:53:41 +02001614 if (bdev_check_media_change(bdev)) {
1615 info = ide_drv_g(bdev->bd_disk, cdrom_info);
1616
1617 ide_cd_read_toc(info->drive);
1618 }
Maurizio Lombardi2bbea6e2018-03-09 13:59:06 +01001619
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001620 mutex_lock(&ide_cd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001621 info = ide_cd_get(bdev->bd_disk);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001622 if (!info)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001623 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Al Viro488ca602008-03-02 10:26:23 -05001625 rc = cdrom_open(&info->devinfo, bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (rc < 0)
1627 ide_cd_put(info);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001628out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001629 mutex_unlock(&ide_cd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return rc;
1631}
1632
Al Virodb2a1442013-05-05 21:52:57 -04001633static void idecd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001635 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001637 mutex_lock(&ide_cd_mutex);
Al Viro488ca602008-03-02 10:26:23 -05001638 cdrom_release(&info->devinfo, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 ide_cd_put(info);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001641 mutex_unlock(&ide_cd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001644static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1645{
1646 struct packet_command cgc;
1647 char buffer[16];
1648 int stat;
1649 char spindown;
1650
1651 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
1652 return -EFAULT;
1653
1654 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1655
1656 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1657 if (stat)
1658 return stat;
1659
1660 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1661 return cdrom_mode_select(cdi, &cgc);
1662}
1663
1664static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1665{
1666 struct packet_command cgc;
1667 char buffer[16];
1668 int stat;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001669 char spindown;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001670
1671 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1672
1673 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1674 if (stat)
1675 return stat;
1676
1677 spindown = buffer[11] & 0x0f;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001678 if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001679 return -EFAULT;
1680 return 0;
1681}
1682
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001683static int idecd_locked_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 unsigned int cmd, unsigned long arg)
1685{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001686 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int err;
1688
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001689 switch (cmd) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001690 case CDROMSETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001691 return idecd_set_spindown(&info->devinfo, arg);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001692 case CDROMGETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001693 return idecd_get_spindown(&info->devinfo, arg);
1694 default:
1695 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001696 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001697
Al Viro1bddd9e2008-09-02 17:19:43 -04001698 err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (err == -EINVAL)
Al Viro488ca602008-03-02 10:26:23 -05001700 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 return err;
1703}
1704
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001705static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
1706 unsigned int cmd, unsigned long arg)
1707{
1708 int ret;
1709
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001710 mutex_lock(&ide_cd_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001711 ret = idecd_locked_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001712 mutex_unlock(&ide_cd_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001713
1714 return ret;
1715}
1716
Arnd Bergmann64cbfa92019-11-28 15:55:17 +01001717static int idecd_locked_compat_ioctl(struct block_device *bdev, fmode_t mode,
1718 unsigned int cmd, unsigned long arg)
1719{
1720 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
1721 void __user *argp = compat_ptr(arg);
1722 int err;
1723
1724 switch (cmd) {
1725 case CDROMSETSPINDOWN:
1726 return idecd_set_spindown(&info->devinfo, (unsigned long)argp);
1727 case CDROMGETSPINDOWN:
1728 return idecd_get_spindown(&info->devinfo, (unsigned long)argp);
1729 default:
1730 break;
1731 }
1732
Arnd Bergmann1df23c62019-11-28 17:14:59 +01001733 err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
1734 if (err == -EINVAL)
1735 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd,
1736 (unsigned long)argp);
1737
1738 return err;
Arnd Bergmann64cbfa92019-11-28 15:55:17 +01001739}
1740
1741static int idecd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1742 unsigned int cmd, unsigned long arg)
1743{
1744 int ret;
1745
1746 mutex_lock(&ide_cd_mutex);
1747 ret = idecd_locked_compat_ioctl(bdev, mode, cmd, arg);
1748 mutex_unlock(&ide_cd_mutex);
1749
1750 return ret;
1751}
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001752
Tejun Heo5b03a1b2011-03-09 19:54:27 +01001753static unsigned int idecd_check_events(struct gendisk *disk,
1754 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001756 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Tejun Heo5b03a1b2011-03-09 19:54:27 +01001757 return cdrom_check_events(&info->devinfo, clearing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001760static const struct block_device_operations idecd_ops = {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001761 .owner = THIS_MODULE,
Al Viro488ca602008-03-02 10:26:23 -05001762 .open = idecd_open,
1763 .release = idecd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001764 .ioctl = idecd_ioctl,
Arnd Bergmann1df23c62019-11-28 17:14:59 +01001765 .compat_ioctl = IS_ENABLED(CONFIG_COMPAT) ?
1766 idecd_compat_ioctl : NULL,
Tejun Heo5b03a1b2011-03-09 19:54:27 +01001767 .check_events = idecd_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768};
1769
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001770/* module options */
Borislav Petkov35d9b172008-10-13 21:39:49 +02001771static unsigned long debug_mask;
1772module_param(debug_mask, ulong, 0644);
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
1775
Russell King4031bbe2006-01-06 11:41:00 +00001776static int ide_cd_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 struct cdrom_info *info;
1779 struct gendisk *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Borislav Petkov088b1b82009-01-02 13:34:47 +01001781 ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
1782 drive->driver_req, drive->media);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (!strstr("ide-cdrom", drive->driver_req))
1785 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (drive->media != ide_cdrom && drive->media != ide_optical)
1788 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02001789
Borislav Petkov35d9b172008-10-13 21:39:49 +02001790 drive->debug_mask = debug_mask;
Borislav Petkovd6251d442009-01-06 17:20:58 +01001791 drive->irq_handler = cdrom_newpc_intr;
Borislav Petkov35d9b172008-10-13 21:39:49 +02001792
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08001793 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (info == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001795 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001796 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto failed;
1798 }
1799
1800 g = alloc_disk(1 << PARTN_BITS);
1801 if (!g)
1802 goto out_free_cd;
1803
1804 ide_init_disk(g, drive);
1805
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001806 info->dev.parent = &drive->gendev;
1807 info->dev.release = ide_cd_release;
Kees Cook02aa2a32013-07-03 15:04:56 -07001808 dev_set_name(&info->dev, "%s", dev_name(&drive->gendev));
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001809
1810 if (device_register(&info->dev))
1811 goto out_free_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 info->drive = drive;
1814 info->driver = &ide_cdrom_driver;
1815 info->disk = g;
1816
1817 g->private_data = &info->driver;
1818
1819 drive->driver_data = info;
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 g->minors = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
1823 if (ide_cdrom_setup(drive)) {
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001824 put_device(&info->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto failed;
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Kees Cook54f8a7a2018-07-31 12:51:46 -07001828 ide_cd_read_toc(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 g->fops = &idecd_ops;
Tejun Heod4dc2102011-04-21 20:54:46 +02001830 g->flags |= GENHD_FL_REMOVABLE | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
Martin Wilck3c12c8e2019-03-27 14:51:03 +01001831 g->events = DISK_EVENT_MEDIA_CHANGE;
Hannes Reineckefef912b2018-09-28 08:17:19 +02001832 device_add_disk(&drive->gendev, g, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +01001835out_free_disk:
1836 put_disk(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837out_free_cd:
1838 kfree(info);
1839failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001840 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843static void __exit ide_cdrom_exit(void)
1844{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001845 driver_unregister(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01001847
1848static int __init ide_cdrom_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001850 printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001851 return driver_register(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Kay Sievers263756e2005-12-12 18:03:44 +01001854MODULE_ALIAS("ide:*m-cdrom*");
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +01001855MODULE_ALIAS("ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856module_init(ide_cdrom_init);
1857module_exit(ide_cdrom_exit);
1858MODULE_LICENSE("GPL");