// SPDX-License-Identifier: GPL-2.0-only/* * sr.c Copyright (C) 1992 David Giller * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale * * adapted from: * sd.c Copyright (C) 1992 Drew Eckhardt * Linux scsi disk driver by * Drew Eckhardt <drew@colorado.edu> * * Modified by Eric Youngdale ericy@andante.org to * add scatter-gather, multiple outstanding request, and other * enhancements. * * Modified by Eric Youngdale eric@andante.org to support loadable * low-level scsi drivers. * * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to * provide auto-eject. * * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the * generic cdrom interface * * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() * interface, capabilities probe additions, ioctl cleanups, etc. * * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs * * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM * transparently and lose the GHOST hack * * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> * check resource allocation in sr_init and some cleanups */ #include <linux/module.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/bio.h>#include <linux/compat.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/cdrom.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/major.h>#include <linux/blkdev.h>#include <linux/blk-pm.h>#include <linux/mutex.h>#include <linux/slab.h>#include <linux/pm_runtime.h>#include <linux/uaccess.h> #include <linux/unaligned.h> #include <scsi/scsi.h>#include <scsi/scsi_dbg.h>#include <scsi/scsi_device.h>#include <scsi/scsi_driver.h>#include <scsi/scsi_cmnd.h>#include <scsi/scsi_eh.h>#include <scsi/scsi_host.h>#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ #include "scsi_logging.h"#include "sr.h" MODULE_DESCRIPTION("SCSI cdrom (sr) driver");MODULE_LICENSE("GPL");MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM); #define SR_DISKS 256 #define SR_CAPABILITIES \ (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \ CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ CDC_MRW|CDC_MRW_W|CDC_RAM) static int sr_probe(struct scsi_device *);static void sr_remove(struct scsi_device *);static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);static int sr_done(struct scsi_cmnd *);static int sr_runtime_suspend(struct device *dev); static const struct dev_pm_ops sr_pm_ops = { .runtime_suspend = sr_runtime_suspend,}; static struct scsi_driver sr_template = { .probe = sr_probe, .remove = sr_remove, .gendrv = { .name = "sr", .pm = &sr_pm_ops, }, .init_command = sr_init_command, .done = sr_done,}; static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];static DEFINE_SPINLOCK(sr_index_lock); static struct lock_class_key sr_bio_compl_lkclass; static int sr_open(struct cdrom_device_info *, int);static void sr_release(struct cdrom_device_info *); static int get_sectorsize(struct scsi_cd *);static int get_capabilities(struct scsi_cd *); static unsigned int sr_check_events(struct cdrom_device_info *cdi, unsigned int clearing, int slot);static int sr_packet(struct cdrom_device_info *, struct packet_command *);static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf, u32 lba, u32 nr, u8 *last_sense); static const struct cdrom_device_ops sr_dops = { .open = sr_open, .release = sr_release, .drive_status = sr_drive_status, .check_events = sr_check_events, .tray_move = sr_tray_move, .lock_door = sr_lock_door, .select_speed = sr_select_speed, .get_last_session = sr_get_last_session, .get_mcn = sr_get_mcn, .reset = sr_reset, .audio_ioctl = sr_audio_ioctl, .generic_packet = sr_packet, .read_cdda_bpc = sr_read_cdda_bpc, .capability = SR_CAPABILITIES,}; static inline struct scsi_cd *scsi_cd(struct gendisk *disk){ return disk->private_data;} static int sr_runtime_suspend(struct device *dev){ struct scsi_cd *cd = dev_get_drvdata(dev); if (!cd) /* E.g.: runtime suspend following sr_remove() */ return 0; if (cd->media_present) return -EBUSY; else return 0;} static unsigned int sr_get_events(struct scsi_device *sdev){ u8 buf[8]; u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION, 1, /* polled */ 0, 0, /* reserved */ 1 << 4, /* notification class: media */ 0, 0, /* reserved */ 0, sizeof(buf), /* allocation length */ 0, /* control */ }; struct event_header *eh = (void *)buf; struct media_event_desc *med = (void *)(buf + 4); struct scsi_sense_hdr sshdr; const struct scsi_exec_args exec_args = { .sshdr = &sshdr, }; int result; result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buf, sizeof(buf), SR_TIMEOUT, MAX_RETRIES, &exec_args); if (result > 0 && scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION) return DISK_EVENT_MEDIA_CHANGE; if (result || be16_to_cpu(eh->data_len) < sizeof(*med)) return 0; if (eh->nea || eh->notification_class != 0x4) return 0; if (med->media_event_code == 1) return DISK_EVENT_EJECT_REQUEST; else if (med->media_event_code == 2) return DISK_EVENT_MEDIA_CHANGE; else if (med->media_event_code == 3) return DISK_EVENT_MEDIA_CHANGE; return 0;} /* * This function checks to see if the media has been changed or eject * button has been pressed. It is possible that we have already * sensed a change, or the drive may have sensed one and not yet * reported it. The past events are accumulated in sdev->changed and * returned together with the current state. */static unsigned int sr_check_events(struct cdrom_device_info *cdi, unsigned int clearing, int slot){ struct scsi_cd *cd = cdi->handle; bool last_present; struct scsi_sense_hdr sshdr; unsigned int events; int ret; /* no changer support */ if (CDSL_CURRENT != slot) return 0; events = sr_get_events(cd->device); cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE; /* * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree * for several times in a row. We rely on TUR only for this likely * broken device, to prevent generating incorrect media changed * events for every open(). */ if (cd->ignore_get_event) { events &= ~DISK_EVENT_MEDIA_CHANGE; goto do_tur; } /* * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE * is being cleared. Note that there are devices which hang * if asked to execute TUR repeatedly. */ if (cd->device->changed) { events |= DISK_EVENT_MEDIA_CHANGE; cd->device->changed = 0; cd->tur_changed = true; } if (!(clearing & DISK_EVENT_MEDIA_CHANGE)) return events;do_tur: /* let's see whether the media is there with TUR */ last_present = cd->media_present; ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); /* * Media is considered to be present if TUR succeeds or fails with * sense data indicating something other than media-not-present * (ASC 0x3a). */ cd->media_present = scsi_status_is_good(ret) || (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a); if (last_present != cd->media_present) cd->device->changed = 1; if (cd->device->changed) { events |= DISK_EVENT_MEDIA_CHANGE; cd->device->changed = 0; cd->tur_changed = true; } if (cd->ignore_get_event) return events; /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */ if (!cd->tur_changed) { if (cd->get_event_changed) { if (cd->tur_mismatch++ > 8) { sr_printk(KERN_WARNING, cd, "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n"); cd->ignore_get_event = true; } } else { cd->tur_mismatch = 0; } } cd->tur_changed = false; cd->get_event_changed = false; return events;} /* * sr_done is the interrupt routine for the device driver. * * It will be notified on the end of a SCSI read / write, and will take one * of several actions based on success or failure. */static int sr_done(struct scsi_cmnd *SCpnt){ int result = SCpnt->result; int this_count = scsi_bufflen(SCpnt); int good_bytes = (result == 0 ? this_count : 0); int block_sectors = 0; long error_sector; struct request *rq = scsi_cmd_to_rq(SCpnt); struct scsi_cd *cd = scsi_cd(rq->q->disk); #ifdef DEBUG scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);#endif /* * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial * success. Since this is a relatively rare error condition, no * care is taken to avoid unnecessary additional work such as * memcpy's that could be avoided. */ if (scsi_status_is_check_condition(result) && (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ switch (SCpnt->sense_buffer[2]) { case MEDIUM_ERROR: case VOLUME_OVERFLOW: case ILLEGAL_REQUEST: if (!(SCpnt->sense_buffer[0] & 0x90)) break; error_sector = get_unaligned_be32(&SCpnt->sense_buffer[3]); if (rq->bio != NULL) block_sectors = bio_sectors(rq->bio); if (block_sectors < 4) block_sectors = 4; if (cd->device->sector_size == 2048) error_sector <<= 2; error_sector &= ~(block_sectors - 1); good_bytes = (error_sector - blk_rq_pos(rq)) << 9; if (good_bytes < 0 || good_bytes >= this_count) good_bytes = 0; /* * The SCSI specification allows for the value * returned by READ CAPACITY to be up to 75 2K * sectors past the last readable block. * Therefore, if we hit a medium error within the * last 75 2K sectors, we decrease the saved size * value. */ if (error_sector < get_capacity(cd->disk) && cd->capacity - error_sector < 4 * 75) set_capacity(cd->disk, error_sector); break; case RECOVERED_ERROR: good_bytes = this_count; break; default: break; } } return good_bytes;} static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt){ int block = 0, this_count, s_size; struct scsi_cd *cd; struct request *rq = scsi_cmd_to_rq(SCpnt); blk_status_t ret; ret = scsi_alloc_sgtables(SCpnt); if (ret != BLK_STS_OK) return ret; cd = scsi_cd(rq->q->disk); SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, "Doing sr request, block = %d\n", block)); if (!cd->device || !scsi_device_online(cd->device)) { SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "Finishing %u sectors\n", blk_rq_sectors(rq))); SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "Retry with 0x%p\n", SCpnt)); goto out; } if (cd->device->changed) { /* * quietly refuse to do anything to a changed disc until the * changed bit has been reset */ goto out; } s_size = cd->device->sector_size; if (s_size != 512 && s_size != 1024 && s_size != 2048) { scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); goto out; } switch (req_op(rq)) { case REQ_OP_WRITE: if (get_disk_ro(cd->disk)) goto out; SCpnt->cmnd[0] = WRITE_10; cd->cdi.media_written = 1; break; case REQ_OP_READ: SCpnt->cmnd[0] = READ_10; break; default: blk_dump_rq_flags(rq, "Unknown sr command"); goto out; } { struct scatterlist *sg; int i, size = 0, sg_count = scsi_sg_count(SCpnt); scsi_for_each_sg(SCpnt, sg, sg_count, i) size += sg->length; if (size != scsi_bufflen(SCpnt)) { scmd_printk(KERN_ERR, SCpnt, "mismatch count %d, bytes %d\n", size, scsi_bufflen(SCpnt)); if (scsi_bufflen(SCpnt) > size) SCpnt->sdb.length = size; } } /* * request doesn't start on hw block boundary, add scatter pads */ if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) || (scsi_bufflen(SCpnt) % s_size)) { scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); goto out; } this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9); SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "%s %d/%u 512 byte blocks.\n", (rq_data_dir(rq) == WRITE) ? "writing" : "reading", this_count, blk_rq_sectors(rq))); SCpnt->cmnd[1] = 0; block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9); if (this_count > 0xffff) { this_count = 0xffff; SCpnt->sdb.length = this_count * s_size; } put_unaligned_be32(block, &SCpnt->cmnd[2]); SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; put_unaligned_be16(this_count, &SCpnt->cmnd[7]); /* * We shouldn't disconnect in the middle of a sector, so with a dumb * host adapter, it's safe to assume that we can at least transfer * this many bytes between each connect / disconnect. */ SCpnt->transfersize = cd->device->sector_size; SCpnt->underflow = this_count << 9; SCpnt->allowed = MAX_RETRIES; SCpnt->cmd_len = 10; /* * This indicates that the command is ready from our end to be queued. */ return BLK_STS_OK; out: scsi_free_sgtables(SCpnt); return BLK_STS_IOERR;} static int sr_revalidate_disk(struct scsi_cd *cd){ struct request_queue *q = cd->device->request_queue; struct scsi_sense_hdr sshdr; struct queue_limits lim; int sector_size; /* if the unit is not ready, nothing more to do */ if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr)) return 0; sr_cd_check(&cd->cdi); sector_size = get_sectorsize(cd); lim = queue_limits_start_update(q); lim.logical_block_size = sector_size; lim.features |= BLK_FEAT_ROTATIONAL; return queue_limits_commit_update_frozen(q, &lim);} static int sr_block_open(struct gendisk *disk, blk_mode_t mode){ struct scsi_cd *cd = scsi_cd(disk); struct scsi_device *sdev = cd->device; int ret; Lines 1–500 of 1011. Load more
// SPDX-License-Identifier: GPL-2.0-only /* * sr.c Copyright (C) 1992 David Giller * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale * * adapted from: * sd.c Copyright (C) 1992 Drew Eckhardt * Linux scsi disk driver by * Drew Eckhardt <drew@colorado.edu> * * Modified by Eric Youngdale ericy@andante.org to * add scatter-gather, multiple outstanding request, and other * enhancements. * * Modified by Eric Youngdale eric@andante.org to support loadable * low-level scsi drivers. * * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to * provide auto-eject. * * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the * generic cdrom interface * * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() * interface, capabilities probe additions, ioctl cleanups, etc. * * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs * * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM * transparently and lose the GHOST hack * * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> * check resource allocation in sr_init and some cleanups */ #include <linux/module.h> #include <linux/fs.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/bio.h> #include <linux/compat.h> #include <linux/string.h> #include <linux/errno.h> #include <linux/cdrom.h> #include <linux/interrupt.h> #include <linux/init.h> #include <linux/major.h> #include <linux/blkdev.h> #include <linux/blk-pm.h> #include <linux/mutex.h> #include <linux/slab.h> #include <linux/pm_runtime.h> #include <linux/uaccess.h> #include <linux/unaligned.h> #include <scsi/scsi.h> #include <scsi/scsi_dbg.h> #include <scsi/scsi_device.h> #include <scsi/scsi_driver.h> #include <scsi/scsi_cmnd.h> #include <scsi/scsi_eh.h> #include <scsi/scsi_host.h> #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ #include "scsi_logging.h" #include "sr.h" MODULE_DESCRIPTION("SCSI cdrom (sr) driver"); MODULE_LICENSE("GPL"); MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR); MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM); MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM); #define SR_DISKS 256 #define SR_CAPABILITIES \ (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \ CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ CDC_MRW|CDC_MRW_W|CDC_RAM) static int sr_probe(struct scsi_device *); static void sr_remove(struct scsi_device *); static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt); static int sr_done(struct scsi_cmnd *); static int sr_runtime_suspend(struct device *dev); static const struct dev_pm_ops sr_pm_ops = { .runtime_suspend = sr_runtime_suspend, }; static struct scsi_driver sr_template = { .probe = sr_probe, .remove = sr_remove, .gendrv = { .name = "sr", .pm = &sr_pm_ops, }, .init_command = sr_init_command, .done = sr_done, }; static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; static DEFINE_SPINLOCK(sr_index_lock); static struct lock_class_key sr_bio_compl_lkclass; static int sr_open(struct cdrom_device_info *, int); static void sr_release(struct cdrom_device_info *); static int get_sectorsize(struct scsi_cd *); static int get_capabilities(struct scsi_cd *); static unsigned int sr_check_events(struct cdrom_device_info *cdi, unsigned int clearing, int slot); static int sr_packet(struct cdrom_device_info *, struct packet_command *); static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf, u32 lba, u32 nr, u8 *last_sense); static const struct cdrom_device_ops sr_dops = { .open = sr_open, .release = sr_release, .drive_status = sr_drive_status, .check_events = sr_check_events, .tray_move = sr_tray_move, .lock_door = sr_lock_door, .select_speed = sr_select_speed, .get_last_session = sr_get_last_session, .get_mcn = sr_get_mcn, .reset = sr_reset, .audio_ioctl = sr_audio_ioctl, .generic_packet = sr_packet, .read_cdda_bpc = sr_read_cdda_bpc, .capability = SR_CAPABILITIES, }; static inline struct scsi_cd *scsi_cd(struct gendisk *disk) { return disk->private_data; } static int sr_runtime_suspend(struct device *dev) { struct scsi_cd *cd = dev_get_drvdata(dev); if (!cd) /* E.g.: runtime suspend following sr_remove() */ return 0; if (cd->media_present) return -EBUSY; else return 0; } static unsigned int sr_get_events(struct scsi_device *sdev) { u8 buf[8]; u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION, 1, /* polled */ 0, 0, /* reserved */ 1 << 4, /* notification class: media */ 0, 0, /* reserved */ 0, sizeof(buf), /* allocation length */ 0, /* control */ }; struct event_header *eh = (void *)buf; struct media_event_desc *med = (void *)(buf + 4); struct scsi_sense_hdr sshdr; const struct scsi_exec_args exec_args = { .sshdr = &sshdr, }; int result; result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buf, sizeof(buf), SR_TIMEOUT, MAX_RETRIES, &exec_args); if (result > 0 && scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION) return DISK_EVENT_MEDIA_CHANGE; if (result || be16_to_cpu(eh->data_len) < sizeof(*med)) return 0; if (eh->nea || eh->notification_class != 0x4) return 0; if (med->media_event_code == 1) return DISK_EVENT_EJECT_REQUEST; else if (med->media_event_code == 2) return DISK_EVENT_MEDIA_CHANGE; else if (med->media_event_code == 3) return DISK_EVENT_MEDIA_CHANGE; return 0; } /* * This function checks to see if the media has been changed or eject * button has been pressed. It is possible that we have already * sensed a change, or the drive may have sensed one and not yet * reported it. The past events are accumulated in sdev->changed and * returned together with the current state. */ static unsigned int sr_check_events(struct cdrom_device_info *cdi, unsigned int clearing, int slot) { struct scsi_cd *cd = cdi->handle; bool last_present; struct scsi_sense_hdr sshdr; unsigned int events; int ret; /* no changer support */ if (CDSL_CURRENT != slot) return 0; events = sr_get_events(cd->device); cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE; /* * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree * for several times in a row. We rely on TUR only for this likely * broken device, to prevent generating incorrect media changed * events for every open(). */ if (cd->ignore_get_event) { events &= ~DISK_EVENT_MEDIA_CHANGE; goto do_tur; } /* * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE * is being cleared. Note that there are devices which hang * if asked to execute TUR repeatedly. */ if (cd->device->changed) { events |= DISK_EVENT_MEDIA_CHANGE; cd->device->changed = 0; cd->tur_changed = true; } if (!(clearing & DISK_EVENT_MEDIA_CHANGE)) return events; do_tur: /* let's see whether the media is there with TUR */ last_present = cd->media_present; ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); /* * Media is considered to be present if TUR succeeds or fails with * sense data indicating something other than media-not-present * (ASC 0x3a). */ cd->media_present = scsi_status_is_good(ret) || (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a); if (last_present != cd->media_present) cd->device->changed = 1; if (cd->device->changed) { events |= DISK_EVENT_MEDIA_CHANGE; cd->device->changed = 0; cd->tur_changed = true; } if (cd->ignore_get_event) return events; /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */ if (!cd->tur_changed) { if (cd->get_event_changed) { if (cd->tur_mismatch++ > 8) { sr_printk(KERN_WARNING, cd, "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n"); cd->ignore_get_event = true; } } else { cd->tur_mismatch = 0; } } cd->tur_changed = false; cd->get_event_changed = false; return events; } /* * sr_done is the interrupt routine for the device driver. * * It will be notified on the end of a SCSI read / write, and will take one * of several actions based on success or failure. */ static int sr_done(struct scsi_cmnd *SCpnt) { int result = SCpnt->result; int this_count = scsi_bufflen(SCpnt); int good_bytes = (result == 0 ? this_count : 0); int block_sectors = 0; long error_sector; struct request *rq = scsi_cmd_to_rq(SCpnt); struct scsi_cd *cd = scsi_cd(rq->q->disk); #ifdef DEBUG scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result); #endif /* * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial * success. Since this is a relatively rare error condition, no * care is taken to avoid unnecessary additional work such as * memcpy's that could be avoided. */ if (scsi_status_is_check_condition(result) && (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ switch (SCpnt->sense_buffer[2]) { case MEDIUM_ERROR: case VOLUME_OVERFLOW: case ILLEGAL_REQUEST: if (!(SCpnt->sense_buffer[0] & 0x90)) break; error_sector = get_unaligned_be32(&SCpnt->sense_buffer[3]); if (rq->bio != NULL) block_sectors = bio_sectors(rq->bio); if (block_sectors < 4) block_sectors = 4; if (cd->device->sector_size == 2048) error_sector <<= 2; error_sector &= ~(block_sectors - 1); good_bytes = (error_sector - blk_rq_pos(rq)) << 9; if (good_bytes < 0 || good_bytes >= this_count) good_bytes = 0; /* * The SCSI specification allows for the value * returned by READ CAPACITY to be up to 75 2K * sectors past the last readable block. * Therefore, if we hit a medium error within the * last 75 2K sectors, we decrease the saved size * value. */ if (error_sector < get_capacity(cd->disk) && cd->capacity - error_sector < 4 * 75) set_capacity(cd->disk, error_sector); break; case RECOVERED_ERROR: good_bytes = this_count; break; default: break; } } return good_bytes; } static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt) { int block = 0, this_count, s_size; struct scsi_cd *cd; struct request *rq = scsi_cmd_to_rq(SCpnt); blk_status_t ret; ret = scsi_alloc_sgtables(SCpnt); if (ret != BLK_STS_OK) return ret; cd = scsi_cd(rq->q->disk); SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, "Doing sr request, block = %d\n", block)); if (!cd->device || !scsi_device_online(cd->device)) { SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "Finishing %u sectors\n", blk_rq_sectors(rq))); SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "Retry with 0x%p\n", SCpnt)); goto out; } if (cd->device->changed) { /* * quietly refuse to do anything to a changed disc until the * changed bit has been reset */ goto out; } s_size = cd->device->sector_size; if (s_size != 512 && s_size != 1024 && s_size != 2048) { scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); goto out; } switch (req_op(rq)) { case REQ_OP_WRITE: if (get_disk_ro(cd->disk)) goto out; SCpnt->cmnd[0] = WRITE_10; cd->cdi.media_written = 1; break; case REQ_OP_READ: SCpnt->cmnd[0] = READ_10; break; default: blk_dump_rq_flags(rq, "Unknown sr command"); goto out; } { struct scatterlist *sg; int i, size = 0, sg_count = scsi_sg_count(SCpnt); scsi_for_each_sg(SCpnt, sg, sg_count, i) size += sg->length; if (size != scsi_bufflen(SCpnt)) { scmd_printk(KERN_ERR, SCpnt, "mismatch count %d, bytes %d\n", size, scsi_bufflen(SCpnt)); if (scsi_bufflen(SCpnt) > size) SCpnt->sdb.length = size; } } /* * request doesn't start on hw block boundary, add scatter pads */ if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) || (scsi_bufflen(SCpnt) % s_size)) { scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); goto out; } this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9); SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "%s %d/%u 512 byte blocks.\n", (rq_data_dir(rq) == WRITE) ? "writing" : "reading", this_count, blk_rq_sectors(rq))); SCpnt->cmnd[1] = 0; block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9); if (this_count > 0xffff) { this_count = 0xffff; SCpnt->sdb.length = this_count * s_size; } put_unaligned_be32(block, &SCpnt->cmnd[2]); SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; put_unaligned_be16(this_count, &SCpnt->cmnd[7]); /* * We shouldn't disconnect in the middle of a sector, so with a dumb * host adapter, it's safe to assume that we can at least transfer * this many bytes between each connect / disconnect. */ SCpnt->transfersize = cd->device->sector_size; SCpnt->underflow = this_count << 9; SCpnt->allowed = MAX_RETRIES; SCpnt->cmd_len = 10; /* * This indicates that the command is ready from our end to be queued. */ return BLK_STS_OK; out: scsi_free_sgtables(SCpnt); return BLK_STS_IOERR; } static int sr_revalidate_disk(struct scsi_cd *cd) { struct request_queue *q = cd->device->request_queue; struct scsi_sense_hdr sshdr; struct queue_limits lim; int sector_size; /* if the unit is not ready, nothing more to do */ if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr)) return 0; sr_cd_check(&cd->cdi); sector_size = get_sectorsize(cd); lim = queue_limits_start_update(q); lim.logical_block_size = sector_size; lim.features |= BLK_FEAT_ROTATIONAL; return queue_limits_commit_update_frozen(q, &lim); } static int sr_block_open(struct gendisk *disk, blk_mode_t mode) { struct scsi_cd *cd = scsi_cd(disk); struct scsi_device *sdev = cd->device; int ret;