scsi_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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>
  16. #include <scsi/scsi.h>
  17. #include <scsi/scsi_device.h>
  18. #include <scsi/scsi_eh.h>
  19. #include <scsi/scsi_host.h>
  20. #include <scsi/scsi_ioctl.h>
  21. #include <scsi/scsi_request.h>
  22. #include <scsi/sg.h>
  23. #include <scsi/scsi_dbg.h>
  24. #include "scsi_logging.h"
  25. #define NORMAL_RETRIES 5
  26. #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
  27. #define MAX_BUF PAGE_SIZE
  28. /**
  29. * ioctl_probe -- return host identification
  30. * @host: host to identify
  31. * @buffer: userspace buffer for identification
  32. *
  33. * Return an identifying string at @buffer, if @buffer is non-NULL, filling
  34. * to the length stored at * (int *) @buffer.
  35. */
  36. static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
  37. {
  38. unsigned int len, slen;
  39. const char *string;
  40. if (buffer) {
  41. if (get_user(len, (unsigned int __user *) buffer))
  42. return -EFAULT;
  43. if (host->hostt->info)
  44. string = host->hostt->info(host);
  45. else
  46. string = host->hostt->name;
  47. if (string) {
  48. slen = strlen(string);
  49. if (len > slen)
  50. len = slen + 1;
  51. if (copy_to_user(buffer, string, len))
  52. return -EFAULT;
  53. }
  54. }
  55. return 1;
  56. }
  57. /*
  58. * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  59. * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
  60. *
  61. * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  62. * input data, if any, not including the command string & counts,
  63. * *((int *)arg + 1) is the output buffer size in bytes.
  64. *
  65. * *(char *) ((int *) arg)[2] the actual command byte.
  66. *
  67. * Note that if more than MAX_BUF bytes are requested to be transferred,
  68. * the ioctl will fail with error EINVAL.
  69. *
  70. * This size *does not* include the initial lengths that were passed.
  71. *
  72. * The SCSI command is read from the memory location immediately after the
  73. * length words, and the input data is right after the command. The SCSI
  74. * routines know the command size based on the opcode decode.
  75. *
  76. * The output area is then filled in starting from the command byte.
  77. */
  78. static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
  79. int timeout, int retries)
  80. {
  81. int result;
  82. struct scsi_sense_hdr sshdr;
  83. SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
  84. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
  85. &sshdr, timeout, retries);
  86. SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", result));
  87. if ((driver_byte(result) & DRIVER_SENSE) &&
  88. (scsi_sense_valid(&sshdr))) {
  89. switch (sshdr.sense_key) {
  90. case ILLEGAL_REQUEST:
  91. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  92. sdev->lockable = 0;
  93. else
  94. printk(KERN_INFO "ioctl_internal_command: "
  95. "ILLEGAL REQUEST asc=0x%x ascq=0x%x\n",
  96. sshdr.asc, sshdr.ascq);
  97. break;
  98. case NOT_READY: /* This happens if there is no disc in drive */
  99. if (sdev->removable && (cmd[0] != TEST_UNIT_READY)) {
  100. printk(KERN_INFO "Device not ready. Make sure"
  101. " there is a disc in the drive.\n");
  102. break;
  103. }
  104. case UNIT_ATTENTION:
  105. if (sdev->removable) {
  106. sdev->changed = 1;
  107. result = 0; /* This is no longer considered an error */
  108. break;
  109. }
  110. default: /* Fall through for non-removable media */
  111. sdev_printk(KERN_INFO, sdev,
  112. "ioctl_internal_command return code = %x\n",
  113. result);
  114. scsi_print_sense_hdr(" ", &sshdr);
  115. break;
  116. }
  117. }
  118. SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
  119. return result;
  120. }
  121. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  122. {
  123. char scsi_cmd[MAX_COMMAND_SIZE];
  124. int ret;
  125. if (!sdev->removable || !sdev->lockable)
  126. return 0;
  127. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  128. scsi_cmd[1] = 0;
  129. scsi_cmd[2] = 0;
  130. scsi_cmd[3] = 0;
  131. scsi_cmd[4] = state;
  132. scsi_cmd[5] = 0;
  133. ret = ioctl_internal_command(sdev, scsi_cmd,
  134. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  135. if (ret == 0)
  136. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  137. return ret;
  138. }
  139. EXPORT_SYMBOL(scsi_set_medium_removal);
  140. /*
  141. * This interface is deprecated - users should use the scsi generic (sg)
  142. * interface instead, as this is a more flexible approach to performing
  143. * generic SCSI commands on a device.
  144. *
  145. * The structure that we are passed should look like:
  146. *
  147. * struct sdata {
  148. * unsigned int inlen; [i] Length of data to be written to device
  149. * unsigned int outlen; [i] Length of data to be read from device
  150. * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
  151. * [o] Data read from device starts here.
  152. * [o] On error, sense buffer starts here.
  153. * unsigned char wdata[y]; [i] Data written to device starts here.
  154. * };
  155. * Notes:
  156. * - The SCSI command length is determined by examining the 1st byte
  157. * of the given command. There is no way to override this.
  158. * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
  159. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  160. * accommodate the sense buffer when an error occurs.
  161. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  162. * old code will not be surprised.
  163. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  164. * a negative return and the Unix error code in 'errno'.
  165. * If the SCSI command succeeds then 0 is returned.
  166. * Positive numbers returned are the compacted SCSI error codes (4
  167. * bytes in one int) where the lowest byte is the SCSI status.
  168. * See the drivers/scsi/scsi.h file for more information on this.
  169. *
  170. */
  171. #define OMAX_SB_LEN 16 /* Old sense buffer length */
  172. int scsi_ioctl_send_command(struct scsi_device *sdev,
  173. struct scsi_ioctl_command __user *sic)
  174. {
  175. char *buf;
  176. unsigned char cmd[MAX_COMMAND_SIZE];
  177. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  178. char __user *cmd_in;
  179. unsigned char opcode;
  180. unsigned int inlen, outlen, cmdlen;
  181. unsigned int needed, buf_needed;
  182. int timeout, retries, result;
  183. int data_direction;
  184. gfp_t gfp_mask = GFP_KERNEL;
  185. if (!sic)
  186. return -EINVAL;
  187. if (sdev->host->unchecked_isa_dma)
  188. gfp_mask |= GFP_DMA;
  189. /*
  190. * Verify that we can read at least this much.
  191. */
  192. if (!access_ok(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
  193. return -EFAULT;
  194. if(__get_user(inlen, &sic->inlen))
  195. return -EFAULT;
  196. if(__get_user(outlen, &sic->outlen))
  197. return -EFAULT;
  198. /*
  199. * We do not transfer more than MAX_BUF with this interface.
  200. * If the user needs to transfer more data than this, they
  201. * should use scsi_generics (sg) instead.
  202. */
  203. if (inlen > MAX_BUF)
  204. return -EINVAL;
  205. if (outlen > MAX_BUF)
  206. return -EINVAL;
  207. cmd_in = sic->data;
  208. if(get_user(opcode, cmd_in))
  209. return -EFAULT;
  210. needed = buf_needed = (inlen > outlen ? inlen : outlen);
  211. if (buf_needed) {
  212. buf_needed = (buf_needed + 511) & ~511;
  213. if (buf_needed > MAX_BUF)
  214. buf_needed = MAX_BUF;
  215. buf = kmalloc(buf_needed, gfp_mask);
  216. if (!buf)
  217. return -ENOMEM;
  218. memset(buf, 0, buf_needed);
  219. if (inlen == 0) {
  220. data_direction = DMA_FROM_DEVICE;
  221. } else if (outlen == 0 ) {
  222. data_direction = DMA_TO_DEVICE;
  223. } else {
  224. /*
  225. * Can this ever happen?
  226. */
  227. data_direction = DMA_BIDIRECTIONAL;
  228. }
  229. } else {
  230. buf = NULL;
  231. data_direction = DMA_NONE;
  232. }
  233. /*
  234. * Obtain the command from the user's address space.
  235. */
  236. cmdlen = COMMAND_SIZE(opcode);
  237. result = -EFAULT;
  238. if (!access_ok(VERIFY_READ, cmd_in, cmdlen + inlen))
  239. goto error;
  240. if(__copy_from_user(cmd, cmd_in, cmdlen))
  241. goto error;
  242. /*
  243. * Obtain the data to be sent to the device (if any).
  244. */
  245. if(inlen && copy_from_user(buf, cmd_in + cmdlen, inlen))
  246. goto error;
  247. switch (opcode) {
  248. case SEND_DIAGNOSTIC:
  249. case FORMAT_UNIT:
  250. timeout = FORMAT_UNIT_TIMEOUT;
  251. retries = 1;
  252. break;
  253. case START_STOP:
  254. timeout = START_STOP_TIMEOUT;
  255. retries = NORMAL_RETRIES;
  256. break;
  257. case MOVE_MEDIUM:
  258. timeout = MOVE_MEDIUM_TIMEOUT;
  259. retries = NORMAL_RETRIES;
  260. break;
  261. case READ_ELEMENT_STATUS:
  262. timeout = READ_ELEMENT_STATUS_TIMEOUT;
  263. retries = NORMAL_RETRIES;
  264. break;
  265. case READ_DEFECT_DATA:
  266. timeout = READ_DEFECT_DATA_TIMEOUT;
  267. retries = 1;
  268. break;
  269. default:
  270. timeout = IOCTL_NORMAL_TIMEOUT;
  271. retries = NORMAL_RETRIES;
  272. break;
  273. }
  274. result = scsi_execute(sdev, cmd, data_direction, buf, needed,
  275. sense, timeout, retries, 0);
  276. /*
  277. * If there was an error condition, pass the info back to the user.
  278. */
  279. if (result) {
  280. int sb_len = sizeof(*sense);
  281. sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
  282. if (copy_to_user(cmd_in, sense, sb_len))
  283. result = -EFAULT;
  284. } else {
  285. if (outlen && copy_to_user(cmd_in, buf, outlen))
  286. result = -EFAULT;
  287. }
  288. error:
  289. kfree(buf);
  290. return result;
  291. }
  292. EXPORT_SYMBOL(scsi_ioctl_send_command);
  293. /*
  294. * The scsi_ioctl_get_pci() function places into arg the value
  295. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  296. * Returns: 0 on success
  297. * -ENXIO if there isn't a PCI device pointer
  298. * (could be because the SCSI driver hasn't been
  299. * updated yet, or because it isn't a SCSI
  300. * device)
  301. * any copy_to_user() error on failure there
  302. */
  303. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  304. {
  305. struct device *dev = scsi_get_device(sdev->host);
  306. if (!dev)
  307. return -ENXIO;
  308. return copy_to_user(arg, dev->bus_id, sizeof(dev->bus_id))? -EFAULT: 0;
  309. }
  310. /*
  311. * the scsi_ioctl() function differs from most ioctls in that it does
  312. * not take a major/minor number as the dev field. Rather, it takes
  313. * a pointer to a scsi_devices[] element, a structure.
  314. */
  315. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  316. {
  317. char scsi_cmd[MAX_COMMAND_SIZE];
  318. /* No idea how this happens.... */
  319. if (!sdev)
  320. return -ENXIO;
  321. /*
  322. * If we are in the middle of error recovery, don't let anyone
  323. * else try and use this device. Also, if error recovery fails, it
  324. * may try and take the device offline, in which case all further
  325. * access to the device is prohibited.
  326. */
  327. if (!scsi_block_when_processing_errors(sdev))
  328. return -ENODEV;
  329. /* Check for deprecated ioctls ... all the ioctls which don't
  330. * follow the new unique numbering scheme are deprecated */
  331. switch (cmd) {
  332. case SCSI_IOCTL_SEND_COMMAND:
  333. case SCSI_IOCTL_TEST_UNIT_READY:
  334. case SCSI_IOCTL_BENCHMARK_COMMAND:
  335. case SCSI_IOCTL_SYNC:
  336. case SCSI_IOCTL_START_UNIT:
  337. case SCSI_IOCTL_STOP_UNIT:
  338. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  339. "ioctl, please convert it to SG_IO\n", current->comm);
  340. break;
  341. default:
  342. break;
  343. }
  344. switch (cmd) {
  345. case SCSI_IOCTL_GET_IDLUN:
  346. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  347. return -EFAULT;
  348. __put_user((sdev->id & 0xff)
  349. + ((sdev->lun & 0xff) << 8)
  350. + ((sdev->channel & 0xff) << 16)
  351. + ((sdev->host->host_no & 0xff) << 24),
  352. &((struct scsi_idlun __user *)arg)->dev_id);
  353. __put_user(sdev->host->unique_id,
  354. &((struct scsi_idlun __user *)arg)->host_unique_id);
  355. return 0;
  356. case SCSI_IOCTL_GET_BUS_NUMBER:
  357. return put_user(sdev->host->host_no, (int __user *)arg);
  358. case SCSI_IOCTL_PROBE_HOST:
  359. return ioctl_probe(sdev->host, arg);
  360. case SCSI_IOCTL_SEND_COMMAND:
  361. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  362. return -EACCES;
  363. return scsi_ioctl_send_command(sdev, arg);
  364. case SCSI_IOCTL_DOORLOCK:
  365. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  366. case SCSI_IOCTL_DOORUNLOCK:
  367. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  368. case SCSI_IOCTL_TEST_UNIT_READY:
  369. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  370. NORMAL_RETRIES);
  371. case SCSI_IOCTL_START_UNIT:
  372. scsi_cmd[0] = START_STOP;
  373. scsi_cmd[1] = 0;
  374. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  375. scsi_cmd[4] = 1;
  376. return ioctl_internal_command(sdev, scsi_cmd,
  377. START_STOP_TIMEOUT, NORMAL_RETRIES);
  378. case SCSI_IOCTL_STOP_UNIT:
  379. scsi_cmd[0] = START_STOP;
  380. scsi_cmd[1] = 0;
  381. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  382. scsi_cmd[4] = 0;
  383. return ioctl_internal_command(sdev, scsi_cmd,
  384. START_STOP_TIMEOUT, NORMAL_RETRIES);
  385. case SCSI_IOCTL_GET_PCI:
  386. return scsi_ioctl_get_pci(sdev, arg);
  387. default:
  388. if (sdev->host->hostt->ioctl)
  389. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  390. }
  391. return -EINVAL;
  392. }
  393. EXPORT_SYMBOL(scsi_ioctl);
  394. /*
  395. * the scsi_nonblock_ioctl() function is designed for ioctls which may
  396. * be executed even if the device is in recovery.
  397. */
  398. int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
  399. void __user *arg, struct file *filp)
  400. {
  401. int val, result;
  402. /* The first set of iocts may be executed even if we're doing
  403. * error processing, as long as the device was opened
  404. * non-blocking */
  405. if (filp && filp->f_flags & O_NONBLOCK) {
  406. if (scsi_host_in_recovery(sdev->host))
  407. return -ENODEV;
  408. } else if (!scsi_block_when_processing_errors(sdev))
  409. return -ENODEV;
  410. switch (cmd) {
  411. case SG_SCSI_RESET:
  412. result = get_user(val, (int __user *)arg);
  413. if (result)
  414. return result;
  415. if (val == SG_SCSI_RESET_NOTHING)
  416. return 0;
  417. switch (val) {
  418. case SG_SCSI_RESET_DEVICE:
  419. val = SCSI_TRY_RESET_DEVICE;
  420. break;
  421. case SG_SCSI_RESET_BUS:
  422. val = SCSI_TRY_RESET_BUS;
  423. break;
  424. case SG_SCSI_RESET_HOST:
  425. val = SCSI_TRY_RESET_HOST;
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  431. return -EACCES;
  432. return (scsi_reset_provider(sdev, val) ==
  433. SUCCESS) ? 0 : -EIO;
  434. }
  435. return -ENODEV;
  436. }
  437. EXPORT_SYMBOL(scsi_nonblockable_ioctl);