MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / scsi_ioctl.c
blobfda438ddc28ea97369a08aa603f547bc35a7733d
1 /*
2 * Changes:
3 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
4 * - get rid of some verify_areas and use __copy*user and __get/put_user
5 * for the ones that remain
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 <asm/uaccess.h>
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_eh.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_ioctl.h>
22 #include <scsi/scsi_request.h>
24 #include "scsi_logging.h"
26 #define NORMAL_RETRIES 5
27 #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
28 #define FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
29 #define START_STOP_TIMEOUT (60 * HZ)
30 #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
31 #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
32 #define READ_DEFECT_DATA_TIMEOUT (60 * HZ ) /* ZIP-250 on parallel port takes as long! */
34 #define MAX_BUF PAGE_SIZE
37 * If we are told to probe a host, we will return 0 if the host is not
38 * present, 1 if the host is present, and will return an identifying
39 * string at *arg, if arg is non null, filling to the length stored at
40 * (int *) arg
43 static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
45 unsigned int len, slen;
46 const char *string;
47 int temp = host->hostt->present;
49 if (temp && buffer) {
50 if (get_user(len, (unsigned int __user *) buffer))
51 return -EFAULT;
53 if (host->hostt->info)
54 string = host->hostt->info(host);
55 else
56 string = host->hostt->name;
57 if (string) {
58 slen = strlen(string);
59 if (len > slen)
60 len = slen + 1;
61 if (copy_to_user(buffer, string, len))
62 return -EFAULT;
65 return temp;
70 * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
71 * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
73 * dev is the SCSI device struct ptr, *(int *) arg is the length of the
74 * input data, if any, not including the command string & counts,
75 * *((int *)arg + 1) is the output buffer size in bytes.
77 * *(char *) ((int *) arg)[2] the actual command byte.
79 * Note that if more than MAX_BUF bytes are requested to be transferred,
80 * the ioctl will fail with error EINVAL.
82 * This size *does not* include the initial lengths that were passed.
84 * The SCSI command is read from the memory location immediately after the
85 * length words, and the input data is right after the command. The SCSI
86 * routines know the command size based on the opcode decode.
88 * The output area is then filled in starting from the command byte.
91 static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
92 int timeout, int retries)
94 struct scsi_request *sreq;
95 int result;
97 SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
99 sreq = scsi_allocate_request(sdev, GFP_KERNEL);
100 if (!sreq) {
101 printk("SCSI internal ioctl failed, no memory\n");
102 return -ENOMEM;
105 sreq->sr_data_direction = DMA_NONE;
106 scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
108 SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", sreq->sr_result));
110 if (driver_byte(sreq->sr_result)) {
111 switch (sreq->sr_sense_buffer[2] & 0xf) {
112 case ILLEGAL_REQUEST:
113 if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
114 sdev->lockable = 0;
115 else
116 printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
117 break;
118 case NOT_READY: /* This happens if there is no disc in drive */
119 if (sdev->removable && (cmd[0] != TEST_UNIT_READY)) {
120 printk(KERN_INFO "Device not ready. Make sure there is a disc in the drive.\n");
121 break;
123 case UNIT_ATTENTION:
124 if (sdev->removable) {
125 sdev->changed = 1;
126 sreq->sr_result = 0; /* This is no longer considered an error */
127 break;
129 default: /* Fall through for non-removable media */
130 #if 0 //johnson remove 2008-01-10
131 printk("SCSI error: host %d id %d lun %d return code = %x\n",
132 sdev->host->host_no,
133 sdev->id,
134 sdev->lun,
135 sreq->sr_result);
136 printk("\tSense class %x, sense error %x, extended sense %x\n",
137 sense_class(sreq->sr_sense_buffer[0]),
138 sense_error(sreq->sr_sense_buffer[0]),
139 sreq->sr_sense_buffer[2] & 0xf);
140 #endif
144 result = sreq->sr_result;
145 SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
146 scsi_release_request(sreq);
147 return result;
150 int scsi_set_medium_removal(struct scsi_device *sdev, char state)
152 char scsi_cmd[MAX_COMMAND_SIZE];
153 int ret;
155 if (!sdev->removable || !sdev->lockable)
156 return 0;
158 scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
159 scsi_cmd[1] = 0;
160 scsi_cmd[2] = 0;
161 scsi_cmd[3] = 0;
162 scsi_cmd[4] = state;
163 scsi_cmd[5] = 0;
165 ret = ioctl_internal_command(sdev, scsi_cmd,
166 IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
167 if (ret == 0)
168 sdev->locked = (state == SCSI_REMOVAL_PREVENT);
169 return ret;
173 * This interface is deprecated - users should use the scsi generic (sg)
174 * interface instead, as this is a more flexible approach to performing
175 * generic SCSI commands on a device.
177 * The structure that we are passed should look like:
179 * struct sdata {
180 * unsigned int inlen; [i] Length of data to be written to device
181 * unsigned int outlen; [i] Length of data to be read from device
182 * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
183 * [o] Data read from device starts here.
184 * [o] On error, sense buffer starts here.
185 * unsigned char wdata[y]; [i] Data written to device starts here.
186 * };
187 * Notes:
188 * - The SCSI command length is determined by examining the 1st byte
189 * of the given command. There is no way to override this.
190 * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
191 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
192 * accommodate the sense buffer when an error occurs.
193 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
194 * old code will not be surprised.
195 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
196 * a negative return and the Unix error code in 'errno'.
197 * If the SCSI command succeeds then 0 is returned.
198 * Positive numbers returned are the compacted SCSI error codes (4
199 * bytes in one int) where the lowest byte is the SCSI status.
200 * See the drivers/scsi/scsi.h file for more information on this.
203 #define OMAX_SB_LEN 16 /* Old sense buffer length */
205 int scsi_ioctl_send_command(struct scsi_device *sdev,
206 struct scsi_ioctl_command __user *sic)
208 char *buf;
209 unsigned char cmd[MAX_COMMAND_SIZE];
210 char __user *cmd_in;
211 struct scsi_request *sreq;
212 unsigned char opcode;
213 unsigned int inlen, outlen, cmdlen;
214 unsigned int needed, buf_needed;
215 int timeout, retries, result;
216 int data_direction, gfp_mask = GFP_KERNEL;
218 if (!sic)
219 return -EINVAL;
221 if (sdev->host->unchecked_isa_dma)
222 gfp_mask |= GFP_DMA;
225 * Verify that we can read at least this much.
227 if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
228 return -EFAULT;
230 if(__get_user(inlen, &sic->inlen))
231 return -EFAULT;
233 if(__get_user(outlen, &sic->outlen))
234 return -EFAULT;
237 * We do not transfer more than MAX_BUF with this interface.
238 * If the user needs to transfer more data than this, they
239 * should use scsi_generics (sg) instead.
241 if (inlen > MAX_BUF)
242 return -EINVAL;
243 if (outlen > MAX_BUF)
244 return -EINVAL;
246 cmd_in = sic->data;
247 if(get_user(opcode, cmd_in))
248 return -EFAULT;
250 needed = buf_needed = (inlen > outlen ? inlen : outlen);
251 if (buf_needed) {
252 buf_needed = (buf_needed + 511) & ~511;
253 if (buf_needed > MAX_BUF)
254 buf_needed = MAX_BUF;
255 buf = kmalloc(buf_needed, gfp_mask);
256 if (!buf)
257 return -ENOMEM;
258 memset(buf, 0, buf_needed);
259 if (inlen == 0) {
260 data_direction = DMA_FROM_DEVICE;
261 } else if (outlen == 0 ) {
262 data_direction = DMA_TO_DEVICE;
263 } else {
265 * Can this ever happen?
267 data_direction = DMA_BIDIRECTIONAL;
270 } else {
271 buf = NULL;
272 data_direction = DMA_NONE;
276 * Obtain the command from the user's address space.
278 cmdlen = COMMAND_SIZE(opcode);
280 result = -EFAULT;
282 if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
283 goto error;
285 if(__copy_from_user(cmd, cmd_in, cmdlen))
286 goto error;
289 * Obtain the data to be sent to the device (if any).
292 if(copy_from_user(buf, cmd_in + cmdlen, inlen))
293 goto error;
295 switch (opcode) {
296 case SEND_DIAGNOSTIC:
297 case FORMAT_UNIT:
298 timeout = FORMAT_UNIT_TIMEOUT;
299 retries = 1;
300 break;
301 case START_STOP:
302 timeout = START_STOP_TIMEOUT;
303 retries = NORMAL_RETRIES;
304 break;
305 case MOVE_MEDIUM:
306 timeout = MOVE_MEDIUM_TIMEOUT;
307 retries = NORMAL_RETRIES;
308 break;
309 case READ_ELEMENT_STATUS:
310 timeout = READ_ELEMENT_STATUS_TIMEOUT;
311 retries = NORMAL_RETRIES;
312 break;
313 case READ_DEFECT_DATA:
314 timeout = READ_DEFECT_DATA_TIMEOUT;
315 retries = 1;
316 break;
317 default:
318 timeout = IOCTL_NORMAL_TIMEOUT;
319 retries = NORMAL_RETRIES;
320 break;
323 sreq = scsi_allocate_request(sdev, GFP_KERNEL);
324 if (!sreq) {
325 result = -EINTR;
326 goto error;
329 sreq->sr_data_direction = data_direction;
330 scsi_wait_req(sreq, cmd, buf, needed, timeout, retries);
333 * If there was an error condition, pass the info back to the user.
335 result = sreq->sr_result;
336 if (result) {
337 int sb_len = sizeof(sreq->sr_sense_buffer);
339 sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
340 if (copy_to_user(cmd_in, sreq->sr_sense_buffer, sb_len))
341 result = -EFAULT;
342 } else {
343 if (copy_to_user(cmd_in, buf, outlen))
344 result = -EFAULT;
347 scsi_release_request(sreq);
348 error:
349 kfree(buf);
350 return result;
354 * The scsi_ioctl_get_pci() function places into arg the value
355 * pci_dev::slot_name (8 characters) for the PCI device (if any).
356 * Returns: 0 on success
357 * -ENXIO if there isn't a PCI device pointer
358 * (could be because the SCSI driver hasn't been
359 * updated yet, or because it isn't a SCSI
360 * device)
361 * any copy_to_user() error on failure there
363 static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
365 struct device *dev = scsi_get_device(sdev->host);
367 if (!dev)
368 return -ENXIO;
369 return copy_to_user(arg, dev->bus_id, sizeof(dev->bus_id))? -EFAULT: 0;
374 * the scsi_ioctl() function differs from most ioctls in that it does
375 * not take a major/minor number as the dev field. Rather, it takes
376 * a pointer to a scsi_devices[] element, a structure.
378 int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
380 char scsi_cmd[MAX_COMMAND_SIZE];
382 /* No idea how this happens.... */
383 if (!sdev)
384 return -ENXIO;
387 * If we are in the middle of error recovery, don't let anyone
388 * else try and use this device. Also, if error recovery fails, it
389 * may try and take the device offline, in which case all further
390 * access to the device is prohibited.
392 if (!scsi_block_when_processing_errors(sdev))
393 return -ENODEV;
395 /* Check for deprecated ioctls ... all the ioctls which don't
396 * follow the new unique numbering scheme are deprecated */
397 switch (cmd) {
398 case SCSI_IOCTL_SEND_COMMAND:
399 case SCSI_IOCTL_TEST_UNIT_READY:
400 case SCSI_IOCTL_BENCHMARK_COMMAND:
401 case SCSI_IOCTL_SYNC:
402 case SCSI_IOCTL_START_UNIT:
403 case SCSI_IOCTL_STOP_UNIT:
404 printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
405 break;
406 default:
407 break;
410 switch (cmd) {
411 case SCSI_IOCTL_GET_IDLUN:
412 if (verify_area(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
413 return -EFAULT;
415 __put_user((sdev->id & 0xff)
416 + ((sdev->lun & 0xff) << 8)
417 + ((sdev->channel & 0xff) << 16)
418 + ((sdev->host->host_no & 0xff) << 24),
419 &((struct scsi_idlun __user *)arg)->dev_id);
420 __put_user(sdev->host->unique_id,
421 &((struct scsi_idlun __user *)arg)->host_unique_id);
422 return 0;
423 case SCSI_IOCTL_GET_BUS_NUMBER:
424 return put_user(sdev->host->host_no, (int __user *)arg);
425 case SCSI_IOCTL_PROBE_HOST:
426 return ioctl_probe(sdev->host, arg);
427 case SCSI_IOCTL_SEND_COMMAND:
428 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
429 return -EACCES;
430 return scsi_ioctl_send_command(sdev, arg);
431 case SCSI_IOCTL_DOORLOCK:
432 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
433 case SCSI_IOCTL_DOORUNLOCK:
434 return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
435 case SCSI_IOCTL_TEST_UNIT_READY:
436 return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
437 NORMAL_RETRIES);
438 case SCSI_IOCTL_START_UNIT:
439 scsi_cmd[0] = START_STOP;
440 scsi_cmd[1] = 0;
441 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
442 scsi_cmd[4] = 1;
443 return ioctl_internal_command(sdev, scsi_cmd,
444 START_STOP_TIMEOUT, NORMAL_RETRIES);
445 case SCSI_IOCTL_STOP_UNIT:
446 scsi_cmd[0] = START_STOP;
447 scsi_cmd[1] = 0;
448 scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
449 scsi_cmd[4] = 0;
450 return ioctl_internal_command(sdev, scsi_cmd,
451 START_STOP_TIMEOUT, NORMAL_RETRIES);
452 case SCSI_IOCTL_GET_PCI:
453 return scsi_ioctl_get_pci(sdev, arg);
454 default:
455 if (sdev->host->hostt->ioctl)
456 return sdev->host->hostt->ioctl(sdev, cmd, arg);
458 return -EINVAL;