1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Changes:
4*4882a593Smuzhiyun * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
5*4882a593Smuzhiyun * - get rid of some verify_areas and use __copy*user and __get/put_user
6*4882a593Smuzhiyun * for the ones that remain
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/blkdev.h>
10*4882a593Smuzhiyun #include <linux/interrupt.h>
11*4882a593Smuzhiyun #include <linux/errno.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/mm.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/uaccess.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <scsi/scsi.h>
19*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
20*4882a593Smuzhiyun #include <scsi/scsi_device.h>
21*4882a593Smuzhiyun #include <scsi/scsi_eh.h>
22*4882a593Smuzhiyun #include <scsi/scsi_host.h>
23*4882a593Smuzhiyun #include <scsi/scsi_ioctl.h>
24*4882a593Smuzhiyun #include <scsi/sg.h>
25*4882a593Smuzhiyun #include <scsi/scsi_dbg.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "scsi_logging.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define NORMAL_RETRIES 5
30*4882a593Smuzhiyun #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define MAX_BUF PAGE_SIZE
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun * ioctl_probe -- return host identification
36*4882a593Smuzhiyun * @host: host to identify
37*4882a593Smuzhiyun * @buffer: userspace buffer for identification
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * Return an identifying string at @buffer, if @buffer is non-NULL, filling
40*4882a593Smuzhiyun * to the length stored at * (int *) @buffer.
41*4882a593Smuzhiyun */
ioctl_probe(struct Scsi_Host * host,void __user * buffer)42*4882a593Smuzhiyun static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun unsigned int len, slen;
45*4882a593Smuzhiyun const char *string;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (buffer) {
48*4882a593Smuzhiyun if (get_user(len, (unsigned int __user *) buffer))
49*4882a593Smuzhiyun return -EFAULT;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (host->hostt->info)
52*4882a593Smuzhiyun string = host->hostt->info(host);
53*4882a593Smuzhiyun else
54*4882a593Smuzhiyun string = host->hostt->name;
55*4882a593Smuzhiyun if (string) {
56*4882a593Smuzhiyun slen = strlen(string);
57*4882a593Smuzhiyun if (len > slen)
58*4882a593Smuzhiyun len = slen + 1;
59*4882a593Smuzhiyun if (copy_to_user(buffer, string, len))
60*4882a593Smuzhiyun return -EFAULT;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun return 1;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
69*4882a593Smuzhiyun * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * dev is the SCSI device struct ptr, *(int *) arg is the length of the
72*4882a593Smuzhiyun * input data, if any, not including the command string & counts,
73*4882a593Smuzhiyun * *((int *)arg + 1) is the output buffer size in bytes.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * *(char *) ((int *) arg)[2] the actual command byte.
76*4882a593Smuzhiyun *
77*4882a593Smuzhiyun * Note that if more than MAX_BUF bytes are requested to be transferred,
78*4882a593Smuzhiyun * the ioctl will fail with error EINVAL.
79*4882a593Smuzhiyun *
80*4882a593Smuzhiyun * This size *does not* include the initial lengths that were passed.
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * The SCSI command is read from the memory location immediately after the
83*4882a593Smuzhiyun * length words, and the input data is right after the command. The SCSI
84*4882a593Smuzhiyun * routines know the command size based on the opcode decode.
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * The output area is then filled in starting from the command byte.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun
ioctl_internal_command(struct scsi_device * sdev,char * cmd,int timeout,int retries)89*4882a593Smuzhiyun static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
90*4882a593Smuzhiyun int timeout, int retries)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun int result;
93*4882a593Smuzhiyun struct scsi_sense_hdr sshdr;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun SCSI_LOG_IOCTL(1, sdev_printk(KERN_INFO, sdev,
96*4882a593Smuzhiyun "Trying ioctl with scsi command %d\n", *cmd));
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
99*4882a593Smuzhiyun &sshdr, timeout, retries, NULL);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
102*4882a593Smuzhiyun "Ioctl returned 0x%x\n", result));
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (driver_byte(result) == DRIVER_SENSE &&
105*4882a593Smuzhiyun scsi_sense_valid(&sshdr)) {
106*4882a593Smuzhiyun switch (sshdr.sense_key) {
107*4882a593Smuzhiyun case ILLEGAL_REQUEST:
108*4882a593Smuzhiyun if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
109*4882a593Smuzhiyun sdev->lockable = 0;
110*4882a593Smuzhiyun else
111*4882a593Smuzhiyun sdev_printk(KERN_INFO, sdev,
112*4882a593Smuzhiyun "ioctl_internal_command: "
113*4882a593Smuzhiyun "ILLEGAL REQUEST "
114*4882a593Smuzhiyun "asc=0x%x ascq=0x%x\n",
115*4882a593Smuzhiyun sshdr.asc, sshdr.ascq);
116*4882a593Smuzhiyun break;
117*4882a593Smuzhiyun case NOT_READY: /* This happens if there is no disc in drive */
118*4882a593Smuzhiyun if (sdev->removable)
119*4882a593Smuzhiyun break;
120*4882a593Smuzhiyun fallthrough;
121*4882a593Smuzhiyun case UNIT_ATTENTION:
122*4882a593Smuzhiyun if (sdev->removable) {
123*4882a593Smuzhiyun sdev->changed = 1;
124*4882a593Smuzhiyun result = 0; /* This is no longer considered an error */
125*4882a593Smuzhiyun break;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun fallthrough; /* for non-removable media */
128*4882a593Smuzhiyun default:
129*4882a593Smuzhiyun sdev_printk(KERN_INFO, sdev,
130*4882a593Smuzhiyun "ioctl_internal_command return code = %x\n",
131*4882a593Smuzhiyun result);
132*4882a593Smuzhiyun scsi_print_sense_hdr(sdev, NULL, &sshdr);
133*4882a593Smuzhiyun break;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
138*4882a593Smuzhiyun "IOCTL Releasing command\n"));
139*4882a593Smuzhiyun return result;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
scsi_set_medium_removal(struct scsi_device * sdev,char state)142*4882a593Smuzhiyun int scsi_set_medium_removal(struct scsi_device *sdev, char state)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun char scsi_cmd[MAX_COMMAND_SIZE];
145*4882a593Smuzhiyun int ret;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (!sdev->removable || !sdev->lockable)
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
151*4882a593Smuzhiyun scsi_cmd[1] = 0;
152*4882a593Smuzhiyun scsi_cmd[2] = 0;
153*4882a593Smuzhiyun scsi_cmd[3] = 0;
154*4882a593Smuzhiyun scsi_cmd[4] = state;
155*4882a593Smuzhiyun scsi_cmd[5] = 0;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun ret = ioctl_internal_command(sdev, scsi_cmd,
158*4882a593Smuzhiyun IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
159*4882a593Smuzhiyun if (ret == 0)
160*4882a593Smuzhiyun sdev->locked = (state == SCSI_REMOVAL_PREVENT);
161*4882a593Smuzhiyun return ret;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun EXPORT_SYMBOL(scsi_set_medium_removal);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * The scsi_ioctl_get_pci() function places into arg the value
167*4882a593Smuzhiyun * pci_dev::slot_name (8 characters) for the PCI device (if any).
168*4882a593Smuzhiyun * Returns: 0 on success
169*4882a593Smuzhiyun * -ENXIO if there isn't a PCI device pointer
170*4882a593Smuzhiyun * (could be because the SCSI driver hasn't been
171*4882a593Smuzhiyun * updated yet, or because it isn't a SCSI
172*4882a593Smuzhiyun * device)
173*4882a593Smuzhiyun * any copy_to_user() error on failure there
174*4882a593Smuzhiyun */
scsi_ioctl_get_pci(struct scsi_device * sdev,void __user * arg)175*4882a593Smuzhiyun static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun struct device *dev = scsi_get_device(sdev->host);
178*4882a593Smuzhiyun const char *name;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (!dev)
181*4882a593Smuzhiyun return -ENXIO;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun name = dev_name(dev);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* compatibility with old ioctl which only returned
186*4882a593Smuzhiyun * 20 characters */
187*4882a593Smuzhiyun return copy_to_user(arg, name, min(strlen(name), (size_t)20))
188*4882a593Smuzhiyun ? -EFAULT: 0;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun
scsi_ioctl_common(struct scsi_device * sdev,int cmd,void __user * arg)192*4882a593Smuzhiyun static int scsi_ioctl_common(struct scsi_device *sdev, int cmd, void __user *arg)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun char scsi_cmd[MAX_COMMAND_SIZE];
195*4882a593Smuzhiyun struct scsi_sense_hdr sense_hdr;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /* Check for deprecated ioctls ... all the ioctls which don't
198*4882a593Smuzhiyun * follow the new unique numbering scheme are deprecated */
199*4882a593Smuzhiyun switch (cmd) {
200*4882a593Smuzhiyun case SCSI_IOCTL_SEND_COMMAND:
201*4882a593Smuzhiyun case SCSI_IOCTL_TEST_UNIT_READY:
202*4882a593Smuzhiyun case SCSI_IOCTL_BENCHMARK_COMMAND:
203*4882a593Smuzhiyun case SCSI_IOCTL_SYNC:
204*4882a593Smuzhiyun case SCSI_IOCTL_START_UNIT:
205*4882a593Smuzhiyun case SCSI_IOCTL_STOP_UNIT:
206*4882a593Smuzhiyun printk(KERN_WARNING "program %s is using a deprecated SCSI "
207*4882a593Smuzhiyun "ioctl, please convert it to SG_IO\n", current->comm);
208*4882a593Smuzhiyun break;
209*4882a593Smuzhiyun default:
210*4882a593Smuzhiyun break;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun switch (cmd) {
214*4882a593Smuzhiyun case SCSI_IOCTL_GET_IDLUN: {
215*4882a593Smuzhiyun struct scsi_idlun v = {
216*4882a593Smuzhiyun .dev_id = (sdev->id & 0xff)
217*4882a593Smuzhiyun + ((sdev->lun & 0xff) << 8)
218*4882a593Smuzhiyun + ((sdev->channel & 0xff) << 16)
219*4882a593Smuzhiyun + ((sdev->host->host_no & 0xff) << 24),
220*4882a593Smuzhiyun .host_unique_id = sdev->host->unique_id
221*4882a593Smuzhiyun };
222*4882a593Smuzhiyun if (copy_to_user(arg, &v, sizeof(struct scsi_idlun)))
223*4882a593Smuzhiyun return -EFAULT;
224*4882a593Smuzhiyun return 0;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun case SCSI_IOCTL_GET_BUS_NUMBER:
227*4882a593Smuzhiyun return put_user(sdev->host->host_no, (int __user *)arg);
228*4882a593Smuzhiyun case SCSI_IOCTL_PROBE_HOST:
229*4882a593Smuzhiyun return ioctl_probe(sdev->host, arg);
230*4882a593Smuzhiyun case SCSI_IOCTL_SEND_COMMAND:
231*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
232*4882a593Smuzhiyun return -EACCES;
233*4882a593Smuzhiyun return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
234*4882a593Smuzhiyun case SCSI_IOCTL_DOORLOCK:
235*4882a593Smuzhiyun return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
236*4882a593Smuzhiyun case SCSI_IOCTL_DOORUNLOCK:
237*4882a593Smuzhiyun return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
238*4882a593Smuzhiyun case SCSI_IOCTL_TEST_UNIT_READY:
239*4882a593Smuzhiyun return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
240*4882a593Smuzhiyun NORMAL_RETRIES, &sense_hdr);
241*4882a593Smuzhiyun case SCSI_IOCTL_START_UNIT:
242*4882a593Smuzhiyun scsi_cmd[0] = START_STOP;
243*4882a593Smuzhiyun scsi_cmd[1] = 0;
244*4882a593Smuzhiyun scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
245*4882a593Smuzhiyun scsi_cmd[4] = 1;
246*4882a593Smuzhiyun return ioctl_internal_command(sdev, scsi_cmd,
247*4882a593Smuzhiyun START_STOP_TIMEOUT, NORMAL_RETRIES);
248*4882a593Smuzhiyun case SCSI_IOCTL_STOP_UNIT:
249*4882a593Smuzhiyun scsi_cmd[0] = START_STOP;
250*4882a593Smuzhiyun scsi_cmd[1] = 0;
251*4882a593Smuzhiyun scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
252*4882a593Smuzhiyun scsi_cmd[4] = 0;
253*4882a593Smuzhiyun return ioctl_internal_command(sdev, scsi_cmd,
254*4882a593Smuzhiyun START_STOP_TIMEOUT, NORMAL_RETRIES);
255*4882a593Smuzhiyun case SCSI_IOCTL_GET_PCI:
256*4882a593Smuzhiyun return scsi_ioctl_get_pci(sdev, arg);
257*4882a593Smuzhiyun case SG_SCSI_RESET:
258*4882a593Smuzhiyun return scsi_ioctl_reset(sdev, arg);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun return -ENOIOCTLCMD;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /**
264*4882a593Smuzhiyun * scsi_ioctl - Dispatch ioctl to scsi device
265*4882a593Smuzhiyun * @sdev: scsi device receiving ioctl
266*4882a593Smuzhiyun * @cmd: which ioctl is it
267*4882a593Smuzhiyun * @arg: data associated with ioctl
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * Description: The scsi_ioctl() function differs from most ioctls in that it
270*4882a593Smuzhiyun * does not take a major/minor number as the dev field. Rather, it takes
271*4882a593Smuzhiyun * a pointer to a &struct scsi_device.
272*4882a593Smuzhiyun */
scsi_ioctl(struct scsi_device * sdev,int cmd,void __user * arg)273*4882a593Smuzhiyun int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun int ret = scsi_ioctl_common(sdev, cmd, arg);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (ret != -ENOIOCTLCMD)
278*4882a593Smuzhiyun return ret;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (sdev->host->hostt->ioctl)
281*4882a593Smuzhiyun return sdev->host->hostt->ioctl(sdev, cmd, arg);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun return -EINVAL;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun EXPORT_SYMBOL(scsi_ioctl);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
scsi_compat_ioctl(struct scsi_device * sdev,int cmd,void __user * arg)288*4882a593Smuzhiyun int scsi_compat_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun int ret = scsi_ioctl_common(sdev, cmd, arg);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun if (ret != -ENOIOCTLCMD)
293*4882a593Smuzhiyun return ret;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun if (sdev->host->hostt->compat_ioctl)
296*4882a593Smuzhiyun return sdev->host->hostt->compat_ioctl(sdev, cmd, arg);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun return ret;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun EXPORT_SYMBOL(scsi_compat_ioctl);
301*4882a593Smuzhiyun #endif
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * We can process a reset even when a device isn't fully operable.
305*4882a593Smuzhiyun */
scsi_ioctl_block_when_processing_errors(struct scsi_device * sdev,int cmd,bool ndelay)306*4882a593Smuzhiyun int scsi_ioctl_block_when_processing_errors(struct scsi_device *sdev, int cmd,
307*4882a593Smuzhiyun bool ndelay)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun if (cmd == SG_SCSI_RESET && ndelay) {
310*4882a593Smuzhiyun if (scsi_host_in_recovery(sdev->host))
311*4882a593Smuzhiyun return -EAGAIN;
312*4882a593Smuzhiyun } else {
313*4882a593Smuzhiyun if (!scsi_block_when_processing_errors(sdev))
314*4882a593Smuzhiyun return -ENODEV;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(scsi_ioctl_block_when_processing_errors);
320