// SPDX-License-Identifier: GPL-2.0-only
/*
* Changes:
* Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
* - get rid of some verify_areas and use __copy*user and __get/put_user
* for the ones that remain
*/
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/cdrom.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>
#include <scsi/scsi_dbg.h>
#include "scsi_logging.h"
#define NORMAL_RETRIES 5
#define IOCTL_NORMAL_TIMEOUT (10 * HZ)
#define MAX_BUF PAGE_SIZE
/**
* ioctl_probe -- return host identification
* @host: host to identify
* @buffer: userspace buffer for identification
*
* Return:
* * if successful, %1 and an identifying string at @buffer, if @buffer
* is non-NULL, filling to the length stored at * (int *) @buffer.
* * <0 error code on failure.
*/
static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
{
unsigned int len, slen;
const char *string;
if (buffer) {
if (get_user(len, (unsigned int __user *) buffer))
return -EFAULT;
if (host->hostt->info)
string = host->hostt->info(host);
else
string = host->hostt->name;
if (string) {
slen = strlen(string);
if (len > slen)
len = slen + 1;
if (copy_to_user(buffer, string, len))
return -EFAULT;
}
}
return 1;
}
static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
int timeout, int retries)
{
int result;
struct scsi_sense_hdr sshdr;
const struct scsi_exec_args exec_args = {
.sshdr = &sshdr,
};
SCSI_LOG_IOCTL(1, sdev_printk(KERN_INFO, sdev,
"Trying ioctl with scsi command %d\n", *cmd));
result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, NULL, 0, timeout,
retries, &exec_args);
SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
"Ioctl returned 0x%x\n", result));
if (result < 0)
goto out;
if (scsi_sense_valid(&sshdr)) {
switch (sshdr.sense_key) {
case ILLEGAL_REQUEST:
if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
sdev->lockable = 0;
else
sdev_printk(KERN_INFO, sdev,
"ioctl_internal_command: "
"ILLEGAL REQUEST "
"asc=0x%x ascq=0x%x\n",
sshdr.asc, sshdr.ascq);
break;
case NOT_READY: /* This happens if there is no disc in drive */
if (sdev->removable)
break;
fallthrough;
case UNIT_ATTENTION:
if (sdev->removable) {
sdev->changed = 1;
result = 0; /* This is no longer considered an error */
break;
}
fallthrough; /* for non-removable media */
default:
sdev_printk(KERN_INFO, sdev,
"ioctl_internal_command return code = %x\n",
result);
scsi_print_sense_hdr(sdev, NULL, &sshdr);
break;
}
}
out:
SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
"IOCTL Releasing command\n"));
return result;
}
/**
* scsi_set_medium_removal() - send command to allow or prevent medium removal
* @sdev: target scsi device
* @state: removal state to set (prevent or allow)
*
* Returns:
* * %0 if @sdev is not removable or not lockable or successful.
* * non-%0 is a SCSI result code if > 0 or kernel error code if < 0.
* * Sets @sdev->locked to the new state on success.
*/
int scsi_set_medium_removal(struct scsi_device *sdev, char state)
{
char scsi_cmd[MAX_COMMAND_SIZE];
int ret;
if (!sdev->removable || !sdev->lockable)
return 0;
scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
scsi_cmd[1] = 0;
scsi_cmd[2] = 0;
scsi_cmd[3] = 0;
scsi_cmd[4] = state;
scsi_cmd[5] = 0;
ret = ioctl_internal_command(sdev, scsi_cmd,
IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
if (ret == 0)
sdev->locked = (state == SCSI_REMOVAL_PREVENT);
return ret;
}
EXPORT_SYMBOL(scsi_set_medium_removal);
/*
* The scsi_ioctl_get_pci() function places into arg the value
* pci_dev::slot_name (8 characters) for the PCI device (if any).
* Returns: 0 on success
* -ENXIO if there isn't a PCI device pointer
* (could be because the