scsi_ioctl.c 14 KB

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