xref: /linux/drivers/scsi/sr.c (revision 49d33b6245d91a2ca20755cd4edf86dd0e49649e)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  sr.c Copyright (C) 1992 David Giller
41da177e4SLinus Torvalds  *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  adapted from:
71da177e4SLinus Torvalds  *      sd.c Copyright (C) 1992 Drew Eckhardt
81da177e4SLinus Torvalds  *      Linux scsi disk driver by
91da177e4SLinus Torvalds  *              Drew Eckhardt <drew@colorado.edu>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Modified by Eric Youngdale ericy@andante.org to
121da177e4SLinus Torvalds  *	add scatter-gather, multiple outstanding request, and other
131da177e4SLinus Torvalds  *	enhancements.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *      Modified by Eric Youngdale eric@andante.org to support loadable
161da177e4SLinus Torvalds  *      low-level scsi drivers.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
191da177e4SLinus Torvalds  *      provide auto-eject.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
221da177e4SLinus Torvalds  *      generic cdrom interface
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
251da177e4SLinus Torvalds  *      interface, capabilities probe additions, ioctl cleanups, etc.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
301da177e4SLinus Torvalds  *	transparently and lose the GHOST hack
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
331da177e4SLinus Torvalds  *	check resource allocation in sr_init and some cleanups
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <linux/module.h>
371da177e4SLinus Torvalds #include <linux/fs.h>
381da177e4SLinus Torvalds #include <linux/kernel.h>
391da177e4SLinus Torvalds #include <linux/mm.h>
401da177e4SLinus Torvalds #include <linux/bio.h>
41d320a955SArnd Bergmann #include <linux/compat.h>
421da177e4SLinus Torvalds #include <linux/string.h>
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/cdrom.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/init.h>
47b81e0c23SChristoph Hellwig #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/blkdev.h>
49bca6b067SBart Van Assche #include <linux/blk-pm.h>
500b950672SArjan van de Ven #include <linux/mutex.h>
515a0e3ad6STejun Heo #include <linux/slab.h>
526c7f1e2fSAaron Lu #include <linux/pm_runtime.h>
537c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
541da177e4SLinus Torvalds 
55655da8e5SBart Van Assche #include <asm/unaligned.h>
56655da8e5SBart Van Assche 
571da177e4SLinus Torvalds #include <scsi/scsi.h>
581da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
591da177e4SLinus Torvalds #include <scsi/scsi_device.h>
601da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
61820732b5SJames Bottomley #include <scsi/scsi_cmnd.h>
621da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
631da177e4SLinus Torvalds #include <scsi/scsi_host.h>
641da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include "scsi_logging.h"
671da177e4SLinus Torvalds #include "sr.h"
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 
70f018fa55SRene Herman MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
71f018fa55SRene Herman MODULE_LICENSE("GPL");
72f018fa55SRene Herman MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
73d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
74d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
75f018fa55SRene Herman 
761da177e4SLinus Torvalds #define SR_DISKS	256
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define SR_CAPABILITIES \
791da177e4SLinus Torvalds 	(CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
801da177e4SLinus Torvalds 	 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
816a2900b6SChristoph Hellwig 	 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
821da177e4SLinus Torvalds 	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
831da177e4SLinus Torvalds 	 CDC_MRW|CDC_MRW_W|CDC_RAM)
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int sr_probe(struct device *);
861da177e4SLinus Torvalds static int sr_remove(struct device *);
87159b2cbfSChristoph Hellwig static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
887b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *);
896c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev);
906c7f1e2fSAaron Lu 
91a816b4c6SJulia Lawall static const struct dev_pm_ops sr_pm_ops = {
926c7f1e2fSAaron Lu 	.runtime_suspend	= sr_runtime_suspend,
936c7f1e2fSAaron Lu };
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct scsi_driver sr_template = {
961da177e4SLinus Torvalds 	.gendrv = {
971da177e4SLinus Torvalds 		.name   	= "sr",
983af6b352SChristoph Hellwig 		.owner		= THIS_MODULE,
991da177e4SLinus Torvalds 		.probe		= sr_probe,
1001da177e4SLinus Torvalds 		.remove		= sr_remove,
1016c7f1e2fSAaron Lu 		.pm		= &sr_pm_ops,
1021da177e4SLinus Torvalds 	},
103a1b73fc1SChristoph Hellwig 	.init_command		= sr_init_command,
1047b3d9545SLinus Torvalds 	.done			= sr_done,
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
1081da177e4SLinus Torvalds static DEFINE_SPINLOCK(sr_index_lock);
1091da177e4SLinus Torvalds 
1109c2b9dbaSChristoph Hellwig static struct lock_class_key sr_bio_compl_lkclass;
1119c2b9dbaSChristoph Hellwig 
1121da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *, int);
1131da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *);
116ebc95c79SEnze Li static int get_capabilities(struct scsi_cd *);
1171da177e4SLinus Torvalds 
11893aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
11993aae17aSTejun Heo 				    unsigned int clearing, int slot);
1201da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *, struct packet_command *);
121ba51bdafSChristoph Hellwig static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
122ba51bdafSChristoph Hellwig 		u32 lba, u32 nr, u8 *last_sense);
1231da177e4SLinus Torvalds 
124853fe1bfSKees Cook static const struct cdrom_device_ops sr_dops = {
1251da177e4SLinus Torvalds 	.open			= sr_open,
1261da177e4SLinus Torvalds 	.release	 	= sr_release,
1271da177e4SLinus Torvalds 	.drive_status	 	= sr_drive_status,
12893aae17aSTejun Heo 	.check_events		= sr_check_events,
1291da177e4SLinus Torvalds 	.tray_move		= sr_tray_move,
1301da177e4SLinus Torvalds 	.lock_door		= sr_lock_door,
1311da177e4SLinus Torvalds 	.select_speed		= sr_select_speed,
1321da177e4SLinus Torvalds 	.get_last_session	= sr_get_last_session,
1331da177e4SLinus Torvalds 	.get_mcn		= sr_get_mcn,
1341da177e4SLinus Torvalds 	.reset			= sr_reset,
1351da177e4SLinus Torvalds 	.audio_ioctl		= sr_audio_ioctl,
1361da177e4SLinus Torvalds 	.generic_packet		= sr_packet,
137ba51bdafSChristoph Hellwig 	.read_cdda_bpc		= sr_read_cdda_bpc,
138ba51bdafSChristoph Hellwig 	.capability		= SR_CAPABILITIES,
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
1421da177e4SLinus Torvalds {
143e7f76552SChristoph Hellwig 	return disk->private_data;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1466c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev)
1476c7f1e2fSAaron Lu {
1486c7f1e2fSAaron Lu 	struct scsi_cd *cd = dev_get_drvdata(dev);
1496c7f1e2fSAaron Lu 
15013b43891SAlan Stern 	if (!cd)	/* E.g.: runtime suspend following sr_remove() */
15113b43891SAlan Stern 		return 0;
15213b43891SAlan Stern 
1536c7f1e2fSAaron Lu 	if (cd->media_present)
1546c7f1e2fSAaron Lu 		return -EBUSY;
1556c7f1e2fSAaron Lu 	else
1566c7f1e2fSAaron Lu 		return 0;
1576c7f1e2fSAaron Lu }
1586c7f1e2fSAaron Lu 
15993aae17aSTejun Heo static unsigned int sr_get_events(struct scsi_device *sdev)
1601da177e4SLinus Torvalds {
16193aae17aSTejun Heo 	u8 buf[8];
16293aae17aSTejun Heo 	u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
16393aae17aSTejun Heo 		     1,			/* polled */
16493aae17aSTejun Heo 		     0, 0,		/* reserved */
16593aae17aSTejun Heo 		     1 << 4,		/* notification class: media */
16693aae17aSTejun Heo 		     0, 0,		/* reserved */
16793aae17aSTejun Heo 		     0, sizeof(buf),	/* allocation length */
16893aae17aSTejun Heo 		     0,			/* control */
16993aae17aSTejun Heo 	};
17093aae17aSTejun Heo 	struct event_header *eh = (void *)buf;
17193aae17aSTejun Heo 	struct media_event_desc *med = (void *)(buf + 4);
17293aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
173*49d33b62SMike Christie 	const struct scsi_exec_args exec_args = {
174*49d33b62SMike Christie 		.sshdr = &sshdr,
175*49d33b62SMike Christie 	};
17693aae17aSTejun Heo 	int result;
1771da177e4SLinus Torvalds 
178*49d33b62SMike Christie 	result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buf, sizeof(buf),
179*49d33b62SMike Christie 				  SR_TIMEOUT, MAX_RETRIES, &exec_args);
18093aae17aSTejun Heo 	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
18193aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
18293aae17aSTejun Heo 
18393aae17aSTejun Heo 	if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
18493aae17aSTejun Heo 		return 0;
18593aae17aSTejun Heo 
18693aae17aSTejun Heo 	if (eh->nea || eh->notification_class != 0x4)
18793aae17aSTejun Heo 		return 0;
18893aae17aSTejun Heo 
18993aae17aSTejun Heo 	if (med->media_event_code == 1)
19093aae17aSTejun Heo 		return DISK_EVENT_EJECT_REQUEST;
19193aae17aSTejun Heo 	else if (med->media_event_code == 2)
19293aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
1937dd753caSManYi Li 	else if (med->media_event_code == 3)
1945c04243aSLi Manyi 		return DISK_EVENT_MEDIA_CHANGE;
19593aae17aSTejun Heo 	return 0;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
19893aae17aSTejun Heo /*
19993aae17aSTejun Heo  * This function checks to see if the media has been changed or eject
20093aae17aSTejun Heo  * button has been pressed.  It is possible that we have already
20193aae17aSTejun Heo  * sensed a change, or the drive may have sensed one and not yet
20293aae17aSTejun Heo  * reported it.  The past events are accumulated in sdev->changed and
20393aae17aSTejun Heo  * returned together with the current state.
20493aae17aSTejun Heo  */
20593aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
20693aae17aSTejun Heo 				    unsigned int clearing, int slot)
20793aae17aSTejun Heo {
20893aae17aSTejun Heo 	struct scsi_cd *cd = cdi->handle;
20993aae17aSTejun Heo 	bool last_present;
21093aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
21193aae17aSTejun Heo 	unsigned int events;
21293aae17aSTejun Heo 	int ret;
21393aae17aSTejun Heo 
21493aae17aSTejun Heo 	/* no changer support */
21593aae17aSTejun Heo 	if (CDSL_CURRENT != slot)
21693aae17aSTejun Heo 		return 0;
21793aae17aSTejun Heo 
21893aae17aSTejun Heo 	events = sr_get_events(cd->device);
21979b9677dSKay Sievers 	cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
22079b9677dSKay Sievers 
22179b9677dSKay Sievers 	/*
22279b9677dSKay Sievers 	 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
22379b9677dSKay Sievers 	 * for several times in a row.  We rely on TUR only for this likely
22479b9677dSKay Sievers 	 * broken device, to prevent generating incorrect media changed
22579b9677dSKay Sievers 	 * events for every open().
22679b9677dSKay Sievers 	 */
22779b9677dSKay Sievers 	if (cd->ignore_get_event) {
22879b9677dSKay Sievers 		events &= ~DISK_EVENT_MEDIA_CHANGE;
22979b9677dSKay Sievers 		goto do_tur;
23079b9677dSKay Sievers 	}
23179b9677dSKay Sievers 
23293aae17aSTejun Heo 	/*
23393aae17aSTejun Heo 	 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
23493aae17aSTejun Heo 	 * is being cleared.  Note that there are devices which hang
23593aae17aSTejun Heo 	 * if asked to execute TUR repeatedly.
23693aae17aSTejun Heo 	 */
23779b9677dSKay Sievers 	if (cd->device->changed) {
23879b9677dSKay Sievers 		events |= DISK_EVENT_MEDIA_CHANGE;
23979b9677dSKay Sievers 		cd->device->changed = 0;
24079b9677dSKay Sievers 		cd->tur_changed = true;
24179b9677dSKay Sievers 	}
24293aae17aSTejun Heo 
24379b9677dSKay Sievers 	if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
24479b9677dSKay Sievers 		return events;
24579b9677dSKay Sievers do_tur:
24693aae17aSTejun Heo 	/* let's see whether the media is there with TUR */
24793aae17aSTejun Heo 	last_present = cd->media_present;
24893aae17aSTejun Heo 	ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
24993aae17aSTejun Heo 
250638428ecSTejun Heo 	/*
251638428ecSTejun Heo 	 * Media is considered to be present if TUR succeeds or fails with
252638428ecSTejun Heo 	 * sense data indicating something other than media-not-present
253638428ecSTejun Heo 	 * (ASC 0x3a).
254638428ecSTejun Heo 	 */
25593aae17aSTejun Heo 	cd->media_present = scsi_status_is_good(ret) ||
25693aae17aSTejun Heo 		(scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
2571da177e4SLinus Torvalds 
25893aae17aSTejun Heo 	if (last_present != cd->media_present)
25979b9677dSKay Sievers 		cd->device->changed = 1;
26079b9677dSKay Sievers 
26193aae17aSTejun Heo 	if (cd->device->changed) {
26293aae17aSTejun Heo 		events |= DISK_EVENT_MEDIA_CHANGE;
2631da177e4SLinus Torvalds 		cd->device->changed = 0;
26479b9677dSKay Sievers 		cd->tur_changed = true;
2651da177e4SLinus Torvalds 	}
266285e9670SKay Sievers 
26779b9677dSKay Sievers 	if (cd->ignore_get_event)
26879b9677dSKay Sievers 		return events;
26979b9677dSKay Sievers 
27079b9677dSKay Sievers 	/* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
27179b9677dSKay Sievers 	if (!cd->tur_changed) {
27279b9677dSKay Sievers 		if (cd->get_event_changed) {
27379b9677dSKay Sievers 			if (cd->tur_mismatch++ > 8) {
27496eefad2SHannes Reinecke 				sr_printk(KERN_WARNING, cd,
27579b9677dSKay Sievers 					  "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
27679b9677dSKay Sievers 				cd->ignore_get_event = true;
27779b9677dSKay Sievers 			}
27879b9677dSKay Sievers 		} else {
27979b9677dSKay Sievers 			cd->tur_mismatch = 0;
28079b9677dSKay Sievers 		}
28179b9677dSKay Sievers 	}
28279b9677dSKay Sievers 	cd->tur_changed = false;
28379b9677dSKay Sievers 	cd->get_event_changed = false;
28479b9677dSKay Sievers 
28593aae17aSTejun Heo 	return events;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /*
2897b3d9545SLinus Torvalds  * sr_done is the interrupt routine for the device driver.
2901da177e4SLinus Torvalds  *
2917b3d9545SLinus Torvalds  * It will be notified on the end of a SCSI read / write, and will take one
2921da177e4SLinus Torvalds  * of several actions based on success or failure.
2931da177e4SLinus Torvalds  */
2947b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *SCpnt)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	int result = SCpnt->result;
29730b0c37bSBoaz Harrosh 	int this_count = scsi_bufflen(SCpnt);
2981da177e4SLinus Torvalds 	int good_bytes = (result == 0 ? this_count : 0);
2991da177e4SLinus Torvalds 	int block_sectors = 0;
3001da177e4SLinus Torvalds 	long error_sector;
301c4deb5b5SBart Van Assche 	struct request *rq = scsi_cmd_to_rq(SCpnt);
302f3fa33acSChristoph Hellwig 	struct scsi_cd *cd = scsi_cd(rq->q->disk);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds #ifdef DEBUG
30596eefad2SHannes Reinecke 	scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
3061da177e4SLinus Torvalds #endif
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/*
3091da177e4SLinus Torvalds 	 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
3101da177e4SLinus Torvalds 	 * success.  Since this is a relatively rare error condition, no
3111da177e4SLinus Torvalds 	 * care is taken to avoid unnecessary additional work such as
3121da177e4SLinus Torvalds 	 * memcpy's that could be avoided.
3131da177e4SLinus Torvalds 	 */
3143d45cefcSHannes Reinecke 	if (scsi_status_is_check_condition(result) &&
3151da177e4SLinus Torvalds 	    (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
3161da177e4SLinus Torvalds 		switch (SCpnt->sense_buffer[2]) {
3171da177e4SLinus Torvalds 		case MEDIUM_ERROR:
3181da177e4SLinus Torvalds 		case VOLUME_OVERFLOW:
3191da177e4SLinus Torvalds 		case ILLEGAL_REQUEST:
3201da177e4SLinus Torvalds 			if (!(SCpnt->sense_buffer[0] & 0x90))
3211da177e4SLinus Torvalds 				break;
322655da8e5SBart Van Assche 			error_sector =
323655da8e5SBart Van Assche 				get_unaligned_be32(&SCpnt->sense_buffer[3]);
324c4deb5b5SBart Van Assche 			if (rq->bio != NULL)
325c4deb5b5SBart Van Assche 				block_sectors = bio_sectors(rq->bio);
3261da177e4SLinus Torvalds 			if (block_sectors < 4)
3271da177e4SLinus Torvalds 				block_sectors = 4;
3281da177e4SLinus Torvalds 			if (cd->device->sector_size == 2048)
3291da177e4SLinus Torvalds 				error_sector <<= 2;
3301da177e4SLinus Torvalds 			error_sector &= ~(block_sectors - 1);
331c4deb5b5SBart Van Assche 			good_bytes = (error_sector - blk_rq_pos(rq)) << 9;
3321da177e4SLinus Torvalds 			if (good_bytes < 0 || good_bytes >= this_count)
3331da177e4SLinus Torvalds 				good_bytes = 0;
3341da177e4SLinus Torvalds 			/*
3351da177e4SLinus Torvalds 			 * The SCSI specification allows for the value
3361da177e4SLinus Torvalds 			 * returned by READ CAPACITY to be up to 75 2K
3371da177e4SLinus Torvalds 			 * sectors past the last readable block.
3381da177e4SLinus Torvalds 			 * Therefore, if we hit a medium error within the
3391da177e4SLinus Torvalds 			 * last 75 2K sectors, we decrease the saved size
3401da177e4SLinus Torvalds 			 * value.
3411da177e4SLinus Torvalds 			 */
3421da177e4SLinus Torvalds 			if (error_sector < get_capacity(cd->disk) &&
3431da177e4SLinus Torvalds 			    cd->capacity - error_sector < 4 * 75)
3441da177e4SLinus Torvalds 				set_capacity(cd->disk, error_sector);
3451da177e4SLinus Torvalds 			break;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 		case RECOVERED_ERROR:
3481da177e4SLinus Torvalds 			good_bytes = this_count;
3491da177e4SLinus Torvalds 			break;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		default:
3521da177e4SLinus Torvalds 			break;
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3567b3d9545SLinus Torvalds 	return good_bytes;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
359159b2cbfSChristoph Hellwig static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
3601da177e4SLinus Torvalds {
361242f9dcbSJens Axboe 	int block = 0, this_count, s_size;
3627f9a6bc4SJames Bottomley 	struct scsi_cd *cd;
363c4deb5b5SBart Van Assche 	struct request *rq = scsi_cmd_to_rq(SCpnt);
364159b2cbfSChristoph Hellwig 	blk_status_t ret;
3657f9a6bc4SJames Bottomley 
3667007e9ddSChristoph Hellwig 	ret = scsi_alloc_sgtables(SCpnt);
367159b2cbfSChristoph Hellwig 	if (ret != BLK_STS_OK)
3687007e9ddSChristoph Hellwig 		return ret;
369f3fa33acSChristoph Hellwig 	cd = scsi_cd(rq->q->disk);
3707f9a6bc4SJames Bottomley 
37196eefad2SHannes Reinecke 	SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
37296eefad2SHannes Reinecke 		"Doing sr request, block = %d\n", block));
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	if (!cd->device || !scsi_device_online(cd->device)) {
37596eefad2SHannes Reinecke 		SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
37696eefad2SHannes Reinecke 			"Finishing %u sectors\n", blk_rq_sectors(rq)));
37796eefad2SHannes Reinecke 		SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
37896eefad2SHannes Reinecke 			"Retry with 0x%p\n", SCpnt));
3797f9a6bc4SJames Bottomley 		goto out;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (cd->device->changed) {
3831da177e4SLinus Torvalds 		/*
3841da177e4SLinus Torvalds 		 * quietly refuse to do anything to a changed disc until the
3851da177e4SLinus Torvalds 		 * changed bit has been reset
3861da177e4SLinus Torvalds 		 */
3877f9a6bc4SJames Bottomley 		goto out;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	s_size = cd->device->sector_size;
3911da177e4SLinus Torvalds 	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3923bf743e7SJeff Garzik 		scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
3937f9a6bc4SJames Bottomley 		goto out;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
396aebf526bSChristoph Hellwig 	switch (req_op(rq)) {
397aebf526bSChristoph Hellwig 	case REQ_OP_WRITE:
398fd2eb903SChristoph Hellwig 		if (!cd->writeable)
3997f9a6bc4SJames Bottomley 			goto out;
4001da177e4SLinus Torvalds 		SCpnt->cmnd[0] = WRITE_10;
4011da177e4SLinus Torvalds 		cd->cdi.media_written = 1;
402aebf526bSChristoph Hellwig 		break;
403aebf526bSChristoph Hellwig 	case REQ_OP_READ:
4041da177e4SLinus Torvalds 		SCpnt->cmnd[0] = READ_10;
405aebf526bSChristoph Hellwig 		break;
406aebf526bSChristoph Hellwig 	default:
4077f9a6bc4SJames Bottomley 		blk_dump_rq_flags(rq, "Unknown sr command");
4087f9a6bc4SJames Bottomley 		goto out;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	{
41230b0c37bSBoaz Harrosh 		struct scatterlist *sg;
41330b0c37bSBoaz Harrosh 		int i, size = 0, sg_count = scsi_sg_count(SCpnt);
4141da177e4SLinus Torvalds 
41530b0c37bSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i)
41630b0c37bSBoaz Harrosh 			size += sg->length;
41730b0c37bSBoaz Harrosh 
41830b0c37bSBoaz Harrosh 		if (size != scsi_bufflen(SCpnt)) {
4193bf743e7SJeff Garzik 			scmd_printk(KERN_ERR, SCpnt,
4209ccfc756SJames Bottomley 				"mismatch count %d, bytes %d\n",
42130b0c37bSBoaz Harrosh 				size, scsi_bufflen(SCpnt));
42230b0c37bSBoaz Harrosh 			if (scsi_bufflen(SCpnt) > size)
42330b0c37bSBoaz Harrosh 				SCpnt->sdb.length = size;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/*
4281da177e4SLinus Torvalds 	 * request doesn't start on hw block boundary, add scatter pads
4291da177e4SLinus Torvalds 	 */
43083096ebfSTejun Heo 	if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
43130b0c37bSBoaz Harrosh 	    (scsi_bufflen(SCpnt) % s_size)) {
4323bf743e7SJeff Garzik 		scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
4337f9a6bc4SJames Bottomley 		goto out;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
43630b0c37bSBoaz Harrosh 	this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 
43996eefad2SHannes Reinecke 	SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
44096eefad2SHannes Reinecke 					"%s %d/%u 512 byte blocks.\n",
4417f9a6bc4SJames Bottomley 					(rq_data_dir(rq) == WRITE) ?
4421da177e4SLinus Torvalds 					"writing" : "reading",
44383096ebfSTejun Heo 					this_count, blk_rq_sectors(rq)));
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	SCpnt->cmnd[1] = 0;
44683096ebfSTejun Heo 	block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	if (this_count > 0xffff) {
4491da177e4SLinus Torvalds 		this_count = 0xffff;
45030b0c37bSBoaz Harrosh 		SCpnt->sdb.length = this_count * s_size;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
453655da8e5SBart Van Assche 	put_unaligned_be32(block, &SCpnt->cmnd[2]);
4541da177e4SLinus Torvalds 	SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
455655da8e5SBart Van Assche 	put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/*
4581da177e4SLinus Torvalds 	 * We shouldn't disconnect in the middle of a sector, so with a dumb
4591da177e4SLinus Torvalds 	 * host adapter, it's safe to assume that we can at least transfer
4601da177e4SLinus Torvalds 	 * this many bytes between each connect / disconnect.
4611da177e4SLinus Torvalds 	 */
4621da177e4SLinus Torvalds 	SCpnt->transfersize = cd->device->sector_size;
4631da177e4SLinus Torvalds 	SCpnt->underflow = this_count << 9;
4641da177e4SLinus Torvalds 	SCpnt->allowed = MAX_RETRIES;
4659120ac54SChristoph Hellwig 	SCpnt->cmd_len = 10;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/*
4687007e9ddSChristoph Hellwig 	 * This indicates that the command is ready from our end to be queued.
4691da177e4SLinus Torvalds 	 */
4707007e9ddSChristoph Hellwig 	return BLK_STS_OK;
4717f9a6bc4SJames Bottomley  out:
4727007e9ddSChristoph Hellwig 	scsi_free_sgtables(SCpnt);
4737007e9ddSChristoph Hellwig 	return BLK_STS_IOERR;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
47638a2b557SChristoph Hellwig static void sr_revalidate_disk(struct scsi_cd *cd)
47738a2b557SChristoph Hellwig {
47838a2b557SChristoph Hellwig 	struct scsi_sense_hdr sshdr;
47938a2b557SChristoph Hellwig 
48038a2b557SChristoph Hellwig 	/* if the unit is not ready, nothing more to do */
48138a2b557SChristoph Hellwig 	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
48238a2b557SChristoph Hellwig 		return;
48338a2b557SChristoph Hellwig 	sr_cd_check(&cd->cdi);
48438a2b557SChristoph Hellwig 	get_sectorsize(cd);
48538a2b557SChristoph Hellwig }
48638a2b557SChristoph Hellwig 
48740cc51beSAl Viro static int sr_block_open(struct block_device *bdev, fmode_t mode)
4881da177e4SLinus Torvalds {
48979d45f57SLukas Bulwahn 	struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
49001d0c698SChristoph Hellwig 	struct scsi_device *sdev = cd->device;
49179d45f57SLukas Bulwahn 	int ret;
4921da177e4SLinus Torvalds 
49301d0c698SChristoph Hellwig 	if (scsi_device_get(cd->device))
49401d0c698SChristoph Hellwig 		return -ENXIO;
4951214fd7bSBart Van Assche 
4961214fd7bSBart Van Assche 	scsi_autopm_get_device(sdev);
497afd35c4fSChristoph Hellwig 	if (bdev_check_media_change(bdev))
49838a2b557SChristoph Hellwig 		sr_revalidate_disk(cd);
4992bbea6e1SMaurizio Lombardi 
50051a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
50140cc51beSAl Viro 	ret = cdrom_open(&cd->cdi, bdev, mode);
50251a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
5031214fd7bSBart Van Assche 
5041214fd7bSBart Van Assche 	scsi_autopm_put_device(sdev);
50540cc51beSAl Viro 	if (ret)
50601d0c698SChristoph Hellwig 		scsi_device_put(cd->device);
5071da177e4SLinus Torvalds 	return ret;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
510db2a144bSAl Viro static void sr_block_release(struct gendisk *disk, fmode_t mode)
5111da177e4SLinus Torvalds {
51240cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(disk);
51372655c0eSBart Van Assche 
51451a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
51540cc51beSAl Viro 	cdrom_release(&cd->cdi, mode);
51651a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
51772655c0eSBart Van Assche 
51801d0c698SChristoph Hellwig 	scsi_device_put(cd->device);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
52140cc51beSAl Viro static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
5221da177e4SLinus Torvalds 			  unsigned long arg)
5231da177e4SLinus Torvalds {
524a30e3441SChristoph Hellwig 	struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
5251da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
5266a2900b6SChristoph Hellwig 	void __user *argp = (void __user *)arg;
5276a2900b6SChristoph Hellwig 	int ret;
5281da177e4SLinus Torvalds 
5294f07bfc5SChristoph Hellwig 	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
5304f07bfc5SChristoph Hellwig 		return -ENOIOCTLCMD;
5314f07bfc5SChristoph Hellwig 
53251a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
5338a6cfeb6SArnd Bergmann 
534906d15fbSChristoph Hellwig 	ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
535906d15fbSChristoph Hellwig 			(mode & FMODE_NDELAY) != 0);
536906d15fbSChristoph Hellwig 	if (ret)
537906d15fbSChristoph Hellwig 		goto out;
538906d15fbSChristoph Hellwig 
5391214fd7bSBart Van Assche 	scsi_autopm_get_device(sdev);
5401214fd7bSBart Van Assche 
541bc5519c1SKevin Groeneveld 	if (cmd != CDROMCLOSETRAY && cmd != CDROMEJECT) {
54240cc51beSAl Viro 		ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
5436397256bSTejun Heo 		if (ret != -ENOSYS)
5441214fd7bSBart Van Assche 			goto put;
545558e3fbeSChristoph Hellwig 	}
546a30e3441SChristoph Hellwig 	ret = scsi_ioctl(sdev, mode, cmd, argp);
5478a6cfeb6SArnd Bergmann 
5481214fd7bSBart Van Assche put:
5491214fd7bSBart Van Assche 	scsi_autopm_put_device(sdev);
5508a6cfeb6SArnd Bergmann out:
55151a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
5526a2900b6SChristoph Hellwig 	return ret;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
55593aae17aSTejun Heo static unsigned int sr_block_check_events(struct gendisk *disk,
55693aae17aSTejun Heo 					  unsigned int clearing)
5571da177e4SLinus Torvalds {
55801d0c698SChristoph Hellwig 	struct scsi_cd *cd = disk->private_data;
5596c7f1e2fSAaron Lu 
56001d0c698SChristoph Hellwig 	if (atomic_read(&cd->device->disk_events_disable_depth))
5616627b38fSAaron Lu 		return 0;
56201d0c698SChristoph Hellwig 	return cdrom_check_events(&cd->cdi, clearing);
56301d0c698SChristoph Hellwig }
5646c7f1e2fSAaron Lu 
56501d0c698SChristoph Hellwig static void sr_free_disk(struct gendisk *disk)
56601d0c698SChristoph Hellwig {
56701d0c698SChristoph Hellwig 	struct scsi_cd *cd = disk->private_data;
5682d097c50SJens Axboe 
56901d0c698SChristoph Hellwig 	spin_lock(&sr_index_lock);
57001d0c698SChristoph Hellwig 	clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
57101d0c698SChristoph Hellwig 	spin_unlock(&sr_index_lock);
57201d0c698SChristoph Hellwig 
57301d0c698SChristoph Hellwig 	unregister_cdrom(&cd->cdi);
57401d0c698SChristoph Hellwig 	mutex_destroy(&cd->lock);
57501d0c698SChristoph Hellwig 	kfree(cd);
57693aae17aSTejun Heo }
57793aae17aSTejun Heo 
57883d5cde4SAlexey Dobriyan static const struct block_device_operations sr_bdops =
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
58140cc51beSAl Viro 	.open		= sr_block_open,
58240cc51beSAl Viro 	.release	= sr_block_release,
5838a6cfeb6SArnd Bergmann 	.ioctl		= sr_block_ioctl,
584558e3fbeSChristoph Hellwig 	.compat_ioctl	= blkdev_compat_ptr_ioctl,
58593aae17aSTejun Heo 	.check_events	= sr_block_check_events,
58601d0c698SChristoph Hellwig 	.free_disk	= sr_free_disk,
5871da177e4SLinus Torvalds };
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *cdi, int purpose)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
5921da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
5931da177e4SLinus Torvalds 	int retval;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	/*
5961da177e4SLinus Torvalds 	 * If the device is in error recovery, wait until it is done.
5971da177e4SLinus Torvalds 	 * If the device is offline, then disallow any access to it.
5981da177e4SLinus Torvalds 	 */
5991da177e4SLinus Torvalds 	retval = -ENXIO;
6001da177e4SLinus Torvalds 	if (!scsi_block_when_processing_errors(sdev))
6011da177e4SLinus Torvalds 		goto error_out;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	return 0;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds error_out:
6061da177e4SLinus Torvalds 	return retval;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *cdi)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static int sr_probe(struct device *dev)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6161da177e4SLinus Torvalds 	struct gendisk *disk;
6171da177e4SLinus Torvalds 	struct scsi_cd *cd;
6181da177e4SLinus Torvalds 	int minor, error;
6191da177e4SLinus Torvalds 
6206fe8c1dbSSubhash Jadavani 	scsi_autopm_get_device(sdev);
6211da177e4SLinus Torvalds 	error = -ENODEV;
6221da177e4SLinus Torvalds 	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
6231da177e4SLinus Torvalds 		goto fail;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	error = -ENOMEM;
62624669f75SJes Sorensen 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
6271da177e4SLinus Torvalds 	if (!cd)
6281da177e4SLinus Torvalds 		goto fail;
6291da177e4SLinus Torvalds 
6306f8191fdSChristoph Hellwig 	disk = blk_mq_alloc_disk_for_queue(sdev->request_queue,
6314a1fa41dSChristoph Hellwig 					   &sr_bio_compl_lkclass);
6321da177e4SLinus Torvalds 	if (!disk)
6331da177e4SLinus Torvalds 		goto fail_free;
63451a85881SMerlijn Wajer 	mutex_init(&cd->lock);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
6371da177e4SLinus Torvalds 	minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
6381da177e4SLinus Torvalds 	if (minor == SR_DISKS) {
6391da177e4SLinus Torvalds 		spin_unlock(&sr_index_lock);
6401da177e4SLinus Torvalds 		error = -EBUSY;
6411da177e4SLinus Torvalds 		goto fail_put;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	__set_bit(minor, sr_index_bits);
6441da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	disk->major = SCSI_CDROM_MAJOR;
6471da177e4SLinus Torvalds 	disk->first_minor = minor;
648a58bd768SChristoph Hellwig 	disk->minors = 1;
6491da177e4SLinus Torvalds 	sprintf(disk->disk_name, "sr%d", minor);
6501da177e4SLinus Torvalds 	disk->fops = &sr_bdops;
651a4561f9fSChristoph Hellwig 	disk->flags |= GENHD_FL_REMOVABLE | GENHD_FL_NO_PART;
65293aae17aSTejun Heo 	disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
6531545e0b4SChristoph Hellwig 	disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT |
6541545e0b4SChristoph Hellwig 				DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE;
6551da177e4SLinus Torvalds 
656242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
657242f9dcbSJens Axboe 
6581da177e4SLinus Torvalds 	cd->device = sdev;
6591da177e4SLinus Torvalds 	cd->disk = disk;
6601da177e4SLinus Torvalds 	cd->capacity = 0x1fffff;
6611da177e4SLinus Torvalds 	cd->device->changed = 1;	/* force recheck CD type */
66293aae17aSTejun Heo 	cd->media_present = 1;
6631da177e4SLinus Torvalds 	cd->use = 1;
6641da177e4SLinus Torvalds 	cd->readcd_known = 0;
6651da177e4SLinus Torvalds 	cd->readcd_cdda = 0;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	cd->cdi.ops = &sr_dops;
6681da177e4SLinus Torvalds 	cd->cdi.handle = cd;
6691da177e4SLinus Torvalds 	cd->cdi.mask = 0;
6701da177e4SLinus Torvalds 	cd->cdi.capacity = 1;
6711da177e4SLinus Torvalds 	sprintf(cd->cdi.name, "sr%d", minor);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	sdev->sector_size = 2048;	/* A guess, just in case */
6741da177e4SLinus Torvalds 
675ebc95c79SEnze Li 	error = -ENOMEM;
676ebc95c79SEnze Li 	if (get_capabilities(cd))
677ebc95c79SEnze Li 		goto fail_minor;
6781da177e4SLinus Torvalds 	sr_vendor_init(cd);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	set_capacity(disk, cd->capacity);
681e7f76552SChristoph Hellwig 	disk->private_data = cd;
6821da177e4SLinus Torvalds 
683a711d91cSChristoph Hellwig 	if (register_cdrom(disk, &cd->cdi))
6846555781bSSimon Arlott 		goto fail_minor;
6851da177e4SLinus Torvalds 
6866627b38fSAaron Lu 	/*
6876627b38fSAaron Lu 	 * Initialize block layer runtime PM stuffs before the
6886627b38fSAaron Lu 	 * periodic event checking request gets started in add_disk.
6896627b38fSAaron Lu 	 */
6906627b38fSAaron Lu 	blk_pm_runtime_init(sdev->request_queue, dev);
6916627b38fSAaron Lu 
6921da177e4SLinus Torvalds 	dev_set_drvdata(dev, cd);
69338a2b557SChristoph Hellwig 	sr_revalidate_disk(cd);
694e9d658c2SLuis Chamberlain 
695e9d658c2SLuis Chamberlain 	error = device_add_disk(&sdev->sdev_gendev, disk, NULL);
69601d0c698SChristoph Hellwig 	if (error)
69701d0c698SChristoph Hellwig 		goto unregister_cdrom;
6981da177e4SLinus Torvalds 
6999ccfc756SJames Bottomley 	sdev_printk(KERN_DEBUG, sdev,
7009ccfc756SJames Bottomley 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
7016c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
7026c7f1e2fSAaron Lu 
7031da177e4SLinus Torvalds 	return 0;
7041da177e4SLinus Torvalds 
70501d0c698SChristoph Hellwig unregister_cdrom:
70601d0c698SChristoph Hellwig 	unregister_cdrom(&cd->cdi);
7076555781bSSimon Arlott fail_minor:
7086555781bSSimon Arlott 	spin_lock(&sr_index_lock);
7096555781bSSimon Arlott 	clear_bit(minor, sr_index_bits);
7106555781bSSimon Arlott 	spin_unlock(&sr_index_lock);
7111da177e4SLinus Torvalds fail_put:
7121da177e4SLinus Torvalds 	put_disk(disk);
713a247e07fSSimon Arlott 	mutex_destroy(&cd->lock);
7141da177e4SLinus Torvalds fail_free:
7151da177e4SLinus Torvalds 	kfree(cd);
7161da177e4SLinus Torvalds fail:
7176fe8c1dbSSubhash Jadavani 	scsi_autopm_put_device(sdev);
7181da177e4SLinus Torvalds 	return error;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *cd)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	unsigned char cmd[10];
72562858dacSFUJITA Tomonori 	unsigned char buffer[8];
7261da177e4SLinus Torvalds 	int the_result, retries = 3;
7271da177e4SLinus Torvalds 	int sector_size;
728165125e1SJens Axboe 	struct request_queue *queue;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	do {
7311da177e4SLinus Torvalds 		cmd[0] = READ_CAPACITY;
7321da177e4SLinus Torvalds 		memset((void *) &cmd[1], 0, 9);
73362858dacSFUJITA Tomonori 		memset(buffer, 0, sizeof(buffer));
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 		/* Do the command and wait.. */
736*49d33b62SMike Christie 		the_result = scsi_execute_cmd(cd->device, cmd, REQ_OP_DRV_IN,
737*49d33b62SMike Christie 					      buffer, sizeof(buffer),
738f4f4e47eSFUJITA Tomonori 					      SR_TIMEOUT, MAX_RETRIES, NULL);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 		retries--;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	} while (the_result && retries);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	if (the_result) {
7461da177e4SLinus Torvalds 		cd->capacity = 0x1fffff;
7471da177e4SLinus Torvalds 		sector_size = 2048;	/* A guess, just in case */
7481da177e4SLinus Torvalds 	} else {
7495915136dSTejun Heo 		long last_written;
7505915136dSTejun Heo 
751655da8e5SBart Van Assche 		cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
7525915136dSTejun Heo 		/*
7535915136dSTejun Heo 		 * READ_CAPACITY doesn't return the correct size on
7545915136dSTejun Heo 		 * certain UDF media.  If last_written is larger, use
7555915136dSTejun Heo 		 * it instead.
7565915136dSTejun Heo 		 *
7575915136dSTejun Heo 		 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
7585915136dSTejun Heo 		 */
7595915136dSTejun Heo 		if (!cdrom_get_last_written(&cd->cdi, &last_written))
7605915136dSTejun Heo 			cd->capacity = max_t(long, cd->capacity, last_written);
7615915136dSTejun Heo 
762655da8e5SBart Van Assche 		sector_size = get_unaligned_be32(&buffer[4]);
7631da177e4SLinus Torvalds 		switch (sector_size) {
7641da177e4SLinus Torvalds 			/*
7651da177e4SLinus Torvalds 			 * HP 4020i CD-Recorder reports 2340 byte sectors
7661da177e4SLinus Torvalds 			 * Philips CD-Writers report 2352 byte sectors
7671da177e4SLinus Torvalds 			 *
7681da177e4SLinus Torvalds 			 * Use 2k sectors for them..
7691da177e4SLinus Torvalds 			 */
7701da177e4SLinus Torvalds 		case 0:
7711da177e4SLinus Torvalds 		case 2340:
7721da177e4SLinus Torvalds 		case 2352:
7731da177e4SLinus Torvalds 			sector_size = 2048;
774df561f66SGustavo A. R. Silva 			fallthrough;
7751da177e4SLinus Torvalds 		case 2048:
7761da177e4SLinus Torvalds 			cd->capacity *= 4;
777df561f66SGustavo A. R. Silva 			fallthrough;
7781da177e4SLinus Torvalds 		case 512:
7791da177e4SLinus Torvalds 			break;
7801da177e4SLinus Torvalds 		default:
78196eefad2SHannes Reinecke 			sr_printk(KERN_INFO, cd,
78296eefad2SHannes Reinecke 				  "unsupported sector size %d.", sector_size);
7831da177e4SLinus Torvalds 			cd->capacity = 0;
7841da177e4SLinus Torvalds 		}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		cd->device->sector_size = sector_size;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 		/*
7891da177e4SLinus Torvalds 		 * Add this so that we have the ability to correctly gauge
7901da177e4SLinus Torvalds 		 * what the device is capable of.
7911da177e4SLinus Torvalds 		 */
7921da177e4SLinus Torvalds 		set_capacity(cd->disk, cd->capacity);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	queue = cd->device->request_queue;
796e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, sector_size);
7971da177e4SLinus Torvalds 
79862858dacSFUJITA Tomonori 	return;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
801ebc95c79SEnze Li static int get_capabilities(struct scsi_cd *cd)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	unsigned char *buffer;
8041da177e4SLinus Torvalds 	struct scsi_mode_data data;
805820732b5SJames Bottomley 	struct scsi_sense_hdr sshdr;
806a00a7862SMartin K. Petersen 	unsigned int ms_len = 128;
80738582a62SJames Bottomley 	int rc, n;
8081da177e4SLinus Torvalds 
8090ad78200SArjan van de Ven 	static const char *loadmech[] =
8101da177e4SLinus Torvalds 	{
8111da177e4SLinus Torvalds 		"caddy",
8121da177e4SLinus Torvalds 		"tray",
8131da177e4SLinus Torvalds 		"pop-up",
8141da177e4SLinus Torvalds 		"",
8151da177e4SLinus Torvalds 		"changer",
8161da177e4SLinus Torvalds 		"cartridge changer",
8171da177e4SLinus Torvalds 		"",
8181da177e4SLinus Torvalds 		""
8191da177e4SLinus Torvalds 	};
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	/* allocate transfer buffer */
823d94d9496SChristoph Hellwig 	buffer = kmalloc(512, GFP_KERNEL);
8241da177e4SLinus Torvalds 	if (!buffer) {
82596eefad2SHannes Reinecke 		sr_printk(KERN_ERR, cd, "out of memory.\n");
826ebc95c79SEnze Li 		return -ENOMEM;
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 
82938582a62SJames Bottomley 	/* eat unit attentions */
8309f8a2c23STejun Heo 	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	/* ask for mode page 0x2a */
833a00a7862SMartin K. Petersen 	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
8341cf72699SJames Bottomley 			     SR_TIMEOUT, 3, &data, NULL);
8351da177e4SLinus Torvalds 
8368793613dSHannes Reinecke 	if (rc < 0 || data.length > ms_len ||
837a00a7862SMartin K. Petersen 	    data.header_length + data.block_descriptor_length > data.length) {
8381da177e4SLinus Torvalds 		/* failed, drive doesn't have capabilities mode page */
8391da177e4SLinus Torvalds 		cd->cdi.speed = 1;
8401da177e4SLinus Torvalds 		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
8411da177e4SLinus Torvalds 				 CDC_DVD | CDC_DVD_RAM |
842bcc1e382SChuck Ebbert 				 CDC_SELECT_DISC | CDC_SELECT_SPEED |
843bcc1e382SChuck Ebbert 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
8441da177e4SLinus Torvalds 		kfree(buffer);
84596eefad2SHannes Reinecke 		sr_printk(KERN_INFO, cd, "scsi-1 drive");
846ebc95c79SEnze Li 		return 0;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	n = data.header_length + data.block_descriptor_length;
850655da8e5SBart Van Assche 	cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
8511da177e4SLinus Torvalds 	cd->readcd_known = 1;
8521da177e4SLinus Torvalds 	cd->readcd_cdda = buffer[n + 5] & 0x01;
8531da177e4SLinus Torvalds 	/* print some capability bits */
85496eefad2SHannes Reinecke 	sr_printk(KERN_INFO, cd,
85596eefad2SHannes Reinecke 		  "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
856655da8e5SBart Van Assche 		  get_unaligned_be16(&buffer[n + 14]) / 176,
8571da177e4SLinus Torvalds 		  cd->cdi.speed,
8581da177e4SLinus Torvalds 		  buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
8591da177e4SLinus Torvalds 		  buffer[n + 3] & 0x20 ? "dvd-ram " : "",
8601da177e4SLinus Torvalds 		  buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
8611da177e4SLinus Torvalds 		  buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */
8621da177e4SLinus Torvalds 		  buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
8631da177e4SLinus Torvalds 		  loadmech[buffer[n + 6] >> 5]);
8641da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == 0)
8651da177e4SLinus Torvalds 		/* caddy drives can't close tray... */
8661da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY;
8671da177e4SLinus Torvalds 	if ((buffer[n + 2] & 0x8) == 0)
8681da177e4SLinus Torvalds 		/* not a DVD drive */
8691da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD;
8701da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x20) == 0)
8711da177e4SLinus Torvalds 		/* can't write DVD-RAM media */
8721da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_RAM;
8731da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x10) == 0)
8741da177e4SLinus Torvalds 		/* can't write DVD-R media */
8751da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_R;
8761da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x2) == 0)
8771da177e4SLinus Torvalds 		/* can't write CD-RW media */
8781da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_RW;
8791da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x1) == 0)
8801da177e4SLinus Torvalds 		/* can't write CD-R media */
8811da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_R;
8821da177e4SLinus Torvalds 	if ((buffer[n + 6] & 0x8) == 0)
8831da177e4SLinus Torvalds 		/* can't eject */
8841da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_OPEN_TRAY;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
8871da177e4SLinus Torvalds 	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
8881da177e4SLinus Torvalds 		cd->cdi.capacity =
8891da177e4SLinus Torvalds 		    cdrom_number_of_slots(&cd->cdi);
8901da177e4SLinus Torvalds 	if (cd->cdi.capacity <= 1)
8911da177e4SLinus Torvalds 		/* not a changer */
8921da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_SELECT_DISC;
8931da177e4SLinus Torvalds 	/*else    I don't think it can close its tray
8941da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY; */
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/*
8971da177e4SLinus Torvalds 	 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
8981da177e4SLinus Torvalds 	 */
8991da177e4SLinus Torvalds 	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
9001da177e4SLinus Torvalds 			(CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
901fd2eb903SChristoph Hellwig 		cd->writeable = 1;
9021da177e4SLinus Torvalds 	}
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	kfree(buffer);
905ebc95c79SEnze Li 	return 0;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * sr_packet() is the entry point for the generic commands generated
9101da177e4SLinus Torvalds  * by the Uniform CD-ROM layer.
9111da177e4SLinus Torvalds  */
9121da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *cdi,
9131da177e4SLinus Torvalds 		struct packet_command *cgc)
9141da177e4SLinus Torvalds {
9158e04d805SHans de Goede 	struct scsi_cd *cd = cdi->handle;
9168e04d805SHans de Goede 	struct scsi_device *sdev = cd->device;
9178e04d805SHans de Goede 
9188e04d805SHans de Goede 	if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
9198e04d805SHans de Goede 		return -EDRIVE_CANT_DO_THIS;
9208e04d805SHans de Goede 
9211da177e4SLinus Torvalds 	if (cgc->timeout <= 0)
9221da177e4SLinus Torvalds 		cgc->timeout = IOCTL_TIMEOUT;
9231da177e4SLinus Torvalds 
9248e04d805SHans de Goede 	sr_do_ioctl(cd, cgc);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	return cgc->stat;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
929ba51bdafSChristoph Hellwig static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
930ba51bdafSChristoph Hellwig 		u32 lba, u32 nr, u8 *last_sense)
931ba51bdafSChristoph Hellwig {
932ba51bdafSChristoph Hellwig 	struct gendisk *disk = cdi->disk;
933ba51bdafSChristoph Hellwig 	u32 len = nr * CD_FRAMESIZE_RAW;
934ce70fd9aSChristoph Hellwig 	struct scsi_cmnd *scmd;
935ba51bdafSChristoph Hellwig 	struct request *rq;
936ba51bdafSChristoph Hellwig 	struct bio *bio;
937ba51bdafSChristoph Hellwig 	int ret;
938ba51bdafSChristoph Hellwig 
93968ec3b81SChristoph Hellwig 	rq = scsi_alloc_request(disk->queue, REQ_OP_DRV_IN, 0);
940ba51bdafSChristoph Hellwig 	if (IS_ERR(rq))
941ba51bdafSChristoph Hellwig 		return PTR_ERR(rq);
942ce70fd9aSChristoph Hellwig 	scmd = blk_mq_rq_to_pdu(rq);
943ba51bdafSChristoph Hellwig 
944ba51bdafSChristoph Hellwig 	ret = blk_rq_map_user(disk->queue, rq, NULL, ubuf, len, GFP_KERNEL);
945ba51bdafSChristoph Hellwig 	if (ret)
946ba51bdafSChristoph Hellwig 		goto out_put_request;
947ba51bdafSChristoph Hellwig 
948ce70fd9aSChristoph Hellwig 	scmd->cmnd[0] = GPCMD_READ_CD;
949ce70fd9aSChristoph Hellwig 	scmd->cmnd[1] = 1 << 2;
950ce70fd9aSChristoph Hellwig 	scmd->cmnd[2] = (lba >> 24) & 0xff;
951ce70fd9aSChristoph Hellwig 	scmd->cmnd[3] = (lba >> 16) & 0xff;
952ce70fd9aSChristoph Hellwig 	scmd->cmnd[4] = (lba >>  8) & 0xff;
953ce70fd9aSChristoph Hellwig 	scmd->cmnd[5] = lba & 0xff;
954ce70fd9aSChristoph Hellwig 	scmd->cmnd[6] = (nr >> 16) & 0xff;
955ce70fd9aSChristoph Hellwig 	scmd->cmnd[7] = (nr >>  8) & 0xff;
956ce70fd9aSChristoph Hellwig 	scmd->cmnd[8] = nr & 0xff;
957ce70fd9aSChristoph Hellwig 	scmd->cmnd[9] = 0xf8;
958ce70fd9aSChristoph Hellwig 	scmd->cmd_len = 12;
959ba51bdafSChristoph Hellwig 	rq->timeout = 60 * HZ;
960ba51bdafSChristoph Hellwig 	bio = rq->bio;
961ba51bdafSChristoph Hellwig 
962b84ba30bSChristoph Hellwig 	blk_execute_rq(rq, false);
963dbb4c84dSChristoph Hellwig 	if (scmd->result) {
964ba51bdafSChristoph Hellwig 		struct scsi_sense_hdr sshdr;
965ba51bdafSChristoph Hellwig 
9665b794f98SChristoph Hellwig 		scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len,
967ba51bdafSChristoph Hellwig 				     &sshdr);
968ba51bdafSChristoph Hellwig 		*last_sense = sshdr.sense_key;
969ba51bdafSChristoph Hellwig 		ret = -EIO;
970ba51bdafSChristoph Hellwig 	}
971ba51bdafSChristoph Hellwig 
972ba51bdafSChristoph Hellwig 	if (blk_rq_unmap_user(bio))
973ba51bdafSChristoph Hellwig 		ret = -EFAULT;
974ba51bdafSChristoph Hellwig out_put_request:
9750bf6d96cSChristoph Hellwig 	blk_mq_free_request(rq);
976ba51bdafSChristoph Hellwig 	return ret;
977ba51bdafSChristoph Hellwig }
978ba51bdafSChristoph Hellwig 
9791da177e4SLinus Torvalds static int sr_remove(struct device *dev)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct scsi_cd *cd = dev_get_drvdata(dev);
9821da177e4SLinus Torvalds 
9836c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
9846c7f1e2fSAaron Lu 
9851da177e4SLinus Torvalds 	del_gendisk(cd->disk);
98601d0c698SChristoph Hellwig 	put_disk(cd->disk);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	return 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static int __init init_sr(void)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	int rc;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
9961da177e4SLinus Torvalds 	if (rc)
9971da177e4SLinus Torvalds 		return rc;
998da3962feSAkinobu Mita 	rc = scsi_register_driver(&sr_template.gendrv);
999da3962feSAkinobu Mita 	if (rc)
1000da3962feSAkinobu Mita 		unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1001da3962feSAkinobu Mita 
1002da3962feSAkinobu Mita 	return rc;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds static void __exit exit_sr(void)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	scsi_unregister_driver(&sr_template.gendrv);
10081da177e4SLinus Torvalds 	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds module_init(init_sr);
10121da177e4SLinus Torvalds module_exit(exit_sr);
10131da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1014