scsi_ioctl.c 14 KB

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