scsi_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 = kzalloc(buf_needed, gfp_mask);
  216. if (!buf)
  217. return -ENOMEM;
  218. if (inlen == 0) {
  219. data_direction = DMA_FROM_DEVICE;
  220. } else if (outlen == 0 ) {
  221. data_direction = DMA_TO_DEVICE;
  222. } else {
  223. /*
  224. * Can this ever happen?
  225. */
  226. data_direction = DMA_BIDIRECTIONAL;
  227. }
  228. } else {
  229. buf = NULL;
  230. data_direction = DMA_NONE;
  231. }
  232. /*
  233. * Obtain the command from the user's address space.
  234. */
  235. cmdlen = COMMAND_SIZE(opcode);
  236. result = -EFAULT;
  237. if (!access_ok(VERIFY_READ, cmd_in, cmdlen + inlen))
  238. goto error;
  239. if(__copy_from_user(cmd, cmd_in, cmdlen))
  240. goto error;
  241. /*
  242. * Obtain the data to be sent to the device (if any).
  243. */
  244. if(inlen && copy_from_user(buf, cmd_in + cmdlen, inlen))
  245. goto error;
  246. switch (opcode) {
  247. case SEND_DIAGNOSTIC:
  248. case FORMAT_UNIT:
  249. timeout = FORMAT_UNIT_TIMEOUT;
  250. retries = 1;
  251. break;
  252. case START_STOP:
  253. timeout = START_STOP_TIMEOUT;
  254. retries = NORMAL_RETRIES;
  255. break;
  256. case MOVE_MEDIUM:
  257. timeout = MOVE_MEDIUM_TIMEOUT;
  258. retries = NORMAL_RETRIES;
  259. break;
  260. case READ_ELEMENT_STATUS:
  261. timeout = READ_ELEMENT_STATUS_TIMEOUT;
  262. retries = NORMAL_RETRIES;
  263. break;
  264. case READ_DEFECT_DATA:
  265. timeout = READ_DEFECT_DATA_TIMEOUT;
  266. retries = 1;
  267. break;
  268. default:
  269. timeout = IOCTL_NORMAL_TIMEOUT;
  270. retries = NORMAL_RETRIES;
  271. break;
  272. }
  273. result = scsi_execute(sdev, cmd, data_direction, buf, needed,
  274. sense, timeout, retries, 0);
  275. /*
  276. * If there was an error condition, pass the info back to the user.
  277. */
  278. if (result) {
  279. int sb_len = sizeof(*sense);
  280. sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
  281. if (copy_to_user(cmd_in, sense, sb_len))
  282. result = -EFAULT;
  283. } else {
  284. if (outlen && copy_to_user(cmd_in, buf, outlen))
  285. result = -EFAULT;
  286. }
  287. error:
  288. kfree(buf);
  289. return result;
  290. }
  291. EXPORT_SYMBOL(scsi_ioctl_send_command);
  292. /*
  293. * The scsi_ioctl_get_pci() function places into arg the value
  294. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  295. * Returns: 0 on success
  296. * -ENXIO if there isn't a PCI device pointer
  297. * (could be because the SCSI driver hasn't been
  298. * updated yet, or because it isn't a SCSI
  299. * device)
  300. * any copy_to_user() error on failure there
  301. */
  302. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  303. {
  304. struct device *dev = scsi_get_device(sdev->host);
  305. if (!dev)
  306. return -ENXIO;
  307. return copy_to_user(arg, dev->bus_id, sizeof(dev->bus_id))? -EFAULT: 0;
  308. }
  309. /*
  310. * the scsi_ioctl() function differs from most ioctls in that it does
  311. * not take a major/minor number as the dev field. Rather, it takes
  312. * a pointer to a scsi_devices[] element, a structure.
  313. */
  314. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  315. {
  316. char scsi_cmd[MAX_COMMAND_SIZE];
  317. /* No idea how this happens.... */
  318. if (!sdev)
  319. return -ENXIO;
  320. /*
  321. * If we are in the middle of error recovery, don't let anyone
  322. * else try and use this device. Also, if error recovery fails, it
  323. * may try and take the device offline, in which case all further
  324. * access to the device is prohibited.
  325. */
  326. if (!scsi_block_when_processing_errors(sdev))
  327. return -ENODEV;
  328. /* Check for deprecated ioctls ... all the ioctls which don't
  329. * follow the new unique numbering scheme are deprecated */
  330. switch (cmd) {
  331. case SCSI_IOCTL_SEND_COMMAND:
  332. case SCSI_IOCTL_TEST_UNIT_READY:
  333. case SCSI_IOCTL_BENCHMARK_COMMAND:
  334. case SCSI_IOCTL_SYNC:
  335. case SCSI_IOCTL_START_UNIT:
  336. case SCSI_IOCTL_STOP_UNIT:
  337. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  338. "ioctl, please convert it to SG_IO\n", current->comm);
  339. break;
  340. default:
  341. break;
  342. }
  343. switch (cmd) {
  344. case SCSI_IOCTL_GET_IDLUN:
  345. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  346. return -EFAULT;
  347. __put_user((sdev->id & 0xff)
  348. + ((sdev->lun & 0xff) << 8)
  349. + ((sdev->channel & 0xff) << 16)
  350. + ((sdev->host->host_no & 0xff) << 24),
  351. &((struct scsi_idlun __user *)arg)->dev_id);
  352. __put_user(sdev->host->unique_id,
  353. &((struct scsi_idlun __user *)arg)->host_unique_id);
  354. return 0;
  355. case SCSI_IOCTL_GET_BUS_NUMBER:
  356. return put_user(sdev->host->host_no, (int __user *)arg);
  357. case SCSI_IOCTL_PROBE_HOST:
  358. return ioctl_probe(sdev->host, arg);
  359. case SCSI_IOCTL_SEND_COMMAND:
  360. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  361. return -EACCES;
  362. return scsi_ioctl_send_command(sdev, arg);
  363. case SCSI_IOCTL_DOORLOCK:
  364. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  365. case SCSI_IOCTL_DOORUNLOCK:
  366. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  367. case SCSI_IOCTL_TEST_UNIT_READY:
  368. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  369. NORMAL_RETRIES);
  370. case SCSI_IOCTL_START_UNIT:
  371. scsi_cmd[0] = START_STOP;
  372. scsi_cmd[1] = 0;
  373. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  374. scsi_cmd[4] = 1;
  375. return ioctl_internal_command(sdev, scsi_cmd,
  376. START_STOP_TIMEOUT, NORMAL_RETRIES);
  377. case SCSI_IOCTL_STOP_UNIT:
  378. scsi_cmd[0] = START_STOP;
  379. scsi_cmd[1] = 0;
  380. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  381. scsi_cmd[4] = 0;
  382. return ioctl_internal_command(sdev, scsi_cmd,
  383. START_STOP_TIMEOUT, NORMAL_RETRIES);
  384. case SCSI_IOCTL_GET_PCI:
  385. return scsi_ioctl_get_pci(sdev, arg);
  386. default:
  387. if (sdev->host->hostt->ioctl)
  388. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  389. }
  390. return -EINVAL;
  391. }
  392. EXPORT_SYMBOL(scsi_ioctl);
  393. /*
  394. * the scsi_nonblock_ioctl() function is designed for ioctls which may
  395. * be executed even if the device is in recovery.
  396. */
  397. int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
  398. void __user *arg, struct file *filp)
  399. {
  400. int val, result;
  401. /* The first set of iocts may be executed even if we're doing
  402. * error processing, as long as the device was opened
  403. * non-blocking */
  404. if (filp && filp->f_flags & O_NONBLOCK) {
  405. if (scsi_host_in_recovery(sdev->host))
  406. return -ENODEV;
  407. } else if (!scsi_block_when_processing_errors(sdev))
  408. return -ENODEV;
  409. switch (cmd) {
  410. case SG_SCSI_RESET:
  411. result = get_user(val, (int __user *)arg);
  412. if (result)
  413. return result;
  414. if (val == SG_SCSI_RESET_NOTHING)
  415. return 0;
  416. switch (val) {
  417. case SG_SCSI_RESET_DEVICE:
  418. val = SCSI_TRY_RESET_DEVICE;
  419. break;
  420. case SG_SCSI_RESET_BUS:
  421. val = SCSI_TRY_RESET_BUS;
  422. break;
  423. case SG_SCSI_RESET_HOST:
  424. val = SCSI_TRY_RESET_HOST;
  425. break;
  426. default:
  427. return -EINVAL;
  428. }
  429. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  430. return -EACCES;
  431. return (scsi_reset_provider(sdev, val) ==
  432. SUCCESS) ? 0 : -EIO;
  433. }
  434. return -ENODEV;
  435. }
  436. EXPORT_SYMBOL(scsi_nonblockable_ioctl);