1
2
3
4
5
6
7#include <linux/module.h>
8#include <linux/blkdev.h>
9#include <linux/interrupt.h>
10#include <linux/errno.h>
11#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/string.h>
15#include <linux/uaccess.h>
16
17#include <scsi/scsi.h>
18#include <scsi/scsi_cmnd.h>
19#include <scsi/scsi_device.h>
20#include <scsi/scsi_eh.h>
21#include <scsi/scsi_host.h>
22#include <scsi/scsi_ioctl.h>
23#include <scsi/sg.h>
24#include <scsi/scsi_dbg.h>
25
26#include "scsi_logging.h"
27
28#define NORMAL_RETRIES 5
29#define IOCTL_NORMAL_TIMEOUT (10 * HZ)
30
31#define MAX_BUF PAGE_SIZE
32
33
34
35
36
37
38
39
40
41static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
42{
43 unsigned int len, slen;
44 const char *string;
45
46 if (buffer) {
47 if (get_user(len, (unsigned int __user *) buffer))
48 return -EFAULT;
49
50 if (host->hostt->info)
51 string = host->hostt->info(host);
52 else
53 string = host->hostt->name;
54 if (string) {
55 slen = strlen(string);
56 if (len > slen)
57 len = slen + 1;
58 if (copy_to_user(buffer, string, len))
59 return -EFAULT;
60 }
61 }
62 return 1;
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
89 int timeout, int retries)
90{
91 int result;
92 struct scsi_sense_hdr sshdr;
93
94 SCSI_LOG_IOCTL(1, sdev_printk(KERN_INFO, sdev,
95 "Trying ioctl with scsi command %d\n", *cmd));
96
97 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
98 &sshdr, timeout, retries, NULL);
99
100 SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
101 "Ioctl returned 0x%x\n", result));
102
103 if ((driver_byte(result) & DRIVER_SENSE) &&
104 (scsi_sense_valid(&sshdr))) {
105 switch (sshdr.sense_key) {
106 case ILLEGAL_REQUEST:
107 if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
108 sdev->lockable = 0;
109 else
110 sdev_printk(KERN_INFO, sdev,
111 "ioctl_internal_command: "
112 "ILLEGAL REQUEST "
113 "asc=0x%x ascq=0x%x\n",
114 sshdr.asc, sshdr.ascq);
115 break;
116 case NOT_READY:
117 if (sdev->removable)
118 break;
119 case UNIT_ATTENTION:
120 if (sdev->removable) {
121 sdev->changed = 1;
122 result = 0;
123 break;
124 }
125 default:
126 sdev_printk(KERN_INFO, sdev,
127 "ioctl_internal_command return code = %x\n",
128 result);
129 scsi_print_sense_hdr(sdev, NULL, &sshdr);
130 break;
131 }
132 }
133
134 SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
135 "IOCTL Releasing command\n"));
136 return result;
137}
138
139int scsi_set_medium_removal(struct scsi_device *sdev, char state)
140{
141 char scsi_cmd[MAX_COMMAND_SIZE];
142 int ret;
143
144 if (!sdev->removable || !sdev->lockable)
145 return 0;
146
147 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
148 scsi_cmd[1] = 0;
149 scsi_cmd[2] = 0;
150 scsi_cmd[3] = 0;
151 scsi_cmd[4] = state;
152 scsi_cmd[5] = 0;
153
154 ret = ioctl_internal_command(sdev, scsi_cmd,
155 IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
156 if (ret == 0)
157 sdev->locked = (state == SCSI_REMOVAL_PREVENT);
158 return ret;
159}
160EXPORT_SYMBOL(scsi_set_medium_removal);
161
162
163
164
165
166
167
168
169
170
171
172static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
173{
174 struct device *dev = scsi_get_device(sdev->host);
175 const char *name;
176
177 if (!dev)
178 return -ENXIO;
179
180 name = dev_name(dev);
181
182
183
184 return copy_to_user(arg, name, min(strlen(name), (size_t)20))
185 ? -EFAULT: 0;
186}
187
188
189
190
191
192
193
194
195
196
197
198
199int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
200{
201 char scsi_cmd[MAX_COMMAND_SIZE];
202 struct scsi_sense_hdr sense_hdr;
203
204
205
206 switch (cmd) {
207 case SCSI_IOCTL_SEND_COMMAND:
208 case SCSI_IOCTL_TEST_UNIT_READY:
209 case SCSI_IOCTL_BENCHMARK_COMMAND:
210 case SCSI_IOCTL_SYNC:
211 case SCSI_IOCTL_START_UNIT:
212 case SCSI_IOCTL_STOP_UNIT:
213 printk(KERN_WARNING "program %s is using a deprecated SCSI "
214 "ioctl, please convert it to SG_IO\n", current->comm);
215 break;
216 default:
217 break;
218 }
219
220 switch (cmd) {
221 case SCSI_IOCTL_GET_IDLUN:
222 if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
223 return -EFAULT;
224
225 __put_user((sdev->id & 0xff)
226 + ((sdev->lun & 0xff) << 8)
227 + ((sdev->channel & 0xff) << 16)
228 + ((sdev->host->host_no & 0xff) << 24),
229 &((struct scsi_idlun __user *)arg)->dev_id);
230 __put_user(sdev->host->unique_id,
231 &((struct scsi_idlun __user *)arg)->host_unique_id);
232 return 0;
233 case SCSI_IOCTL_GET_BUS_NUMBER:
234 return put_user(sdev->host->host_no, (int __user *)arg);
235 case SCSI_IOCTL_PROBE_HOST:
236 return ioctl_probe(sdev->host, arg);
237 case SCSI_IOCTL_SEND_COMMAND:
238 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
239 return -EACCES;
240 return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
241 case SCSI_IOCTL_DOORLOCK:
242 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
243 case SCSI_IOCTL_DOORUNLOCK:
244 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
245 case SCSI_IOCTL_TEST_UNIT_READY:
246 return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
247 NORMAL_RETRIES, &sense_hdr);
248 case SCSI_IOCTL_START_UNIT:
249 scsi_cmd[0] = START_STOP;
250 scsi_cmd[1] = 0;
251 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
252 scsi_cmd[4] = 1;
253 return ioctl_internal_command(sdev, scsi_cmd,
254 START_STOP_TIMEOUT, NORMAL_RETRIES);
255 case SCSI_IOCTL_STOP_UNIT:
256 scsi_cmd[0] = START_STOP;
257 scsi_cmd[1] = 0;
258 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
259 scsi_cmd[4] = 0;
260 return ioctl_internal_command(sdev, scsi_cmd,
261 START_STOP_TIMEOUT, NORMAL_RETRIES);
262 case SCSI_IOCTL_GET_PCI:
263 return scsi_ioctl_get_pci(sdev, arg);
264 case SG_SCSI_RESET:
265 return scsi_ioctl_reset(sdev, arg);
266 default:
267 if (sdev->host->hostt->ioctl)
268 return sdev->host->hostt->ioctl(sdev, cmd, arg);
269 }
270 return -EINVAL;
271}
272EXPORT_SYMBOL(scsi_ioctl);
273
274
275
276
277int scsi_ioctl_block_when_processing_errors(struct scsi_device *sdev, int cmd,
278 bool ndelay)
279{
280 if (cmd == SG_SCSI_RESET && ndelay) {
281 if (scsi_host_in_recovery(sdev->host))
282 return -EAGAIN;
283 } else {
284 if (!scsi_block_when_processing_errors(sdev))
285 return -ENODEV;
286 }
287
288 return 0;
289}
290EXPORT_SYMBOL_GPL(scsi_ioctl_block_when_processing_errors);
291