scsi_ioctl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_cmnd.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/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, sdev_printk(KERN_INFO, sdev,
  84. "Trying ioctl with scsi command %d\n", *cmd));
  85. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
  86. &sshdr, timeout, retries, NULL);
  87. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  88. "Ioctl returned 0x%x\n", result));
  89. if ((driver_byte(result) & DRIVER_SENSE) &&
  90. (scsi_sense_valid(&sshdr))) {
  91. switch (sshdr.sense_key) {
  92. case ILLEGAL_REQUEST:
  93. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  94. sdev->lockable = 0;
  95. else
  96. sdev_printk(KERN_INFO, sdev,
  97. "ioctl_internal_command: "
  98. "ILLEGAL REQUEST "
  99. "asc=0x%x ascq=0x%x\n",
  100. sshdr.asc, sshdr.ascq);
  101. break;
  102. case NOT_READY: /* This happens if there is no disc in drive */
  103. if (sdev->removable)
  104. break;
  105. case UNIT_ATTENTION:
  106. if (sdev->removable) {
  107. sdev->changed = 1;
  108. result = 0; /* This is no longer considered an error */
  109. break;
  110. }
  111. default: /* Fall through for non-removable media */
  112. sdev_printk(KERN_INFO, sdev,
  113. "ioctl_internal_command return code = %x\n",
  114. result);
  115. scsi_print_sense_hdr(sdev, NULL, &sshdr);
  116. break;
  117. }
  118. }
  119. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  120. "IOCTL Releasing command\n"));
  121. return result;
  122. }
  123. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  124. {
  125. char scsi_cmd[MAX_COMMAND_SIZE];
  126. int ret;
  127. if (!sdev->removable || !sdev->lockable)
  128. return 0;
  129. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  130. scsi_cmd[1] = 0;
  131. scsi_cmd[2] = 0;
  132. scsi_cmd[3] = 0;
  133. scsi_cmd[4] = state;
  134. scsi_cmd[5] = 0;
  135. ret = ioctl_internal_command(sdev, scsi_cmd,
  136. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  137. if (ret == 0)
  138. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(scsi_set_medium_removal);
  142. /*
  143. * The scsi_ioctl_get_pci() function places into arg the value
  144. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  145. * Returns: 0 on success
  146. * -ENXIO if there isn't a PCI device pointer
  147. * (could be because the SCSI driver hasn't been
  148. * updated yet, or because it isn't a SCSI
  149. * device)
  150. * any copy_to_user() error on failure there
  151. */
  152. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  153. {
  154. struct device *dev = scsi_get_device(sdev->host);
  155. const char *name;
  156. if (!dev)
  157. return -ENXIO;
  158. name = dev_name(dev);
  159. /* compatibility with old ioctl which only returned
  160. * 20 characters */
  161. return copy_to_user(arg, name, min(strlen(name), (size_t)20))
  162. ? -EFAULT: 0;
  163. }
  164. /**
  165. * scsi_ioctl - Dispatch ioctl to scsi device
  166. * @sdev: scsi device receiving ioctl
  167. * @cmd: which ioctl is it
  168. * @arg: data associated with ioctl
  169. *
  170. * Description: The scsi_ioctl() function differs from most ioctls in that it
  171. * does not take a major/minor number as the dev field. Rather, it takes
  172. * a pointer to a &struct scsi_device.
  173. */
  174. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  175. {
  176. char scsi_cmd[MAX_COMMAND_SIZE];
  177. /* Check for deprecated ioctls ... all the ioctls which don't
  178. * follow the new unique numbering scheme are deprecated */
  179. switch (cmd) {
  180. case SCSI_IOCTL_SEND_COMMAND:
  181. case SCSI_IOCTL_TEST_UNIT_READY:
  182. case SCSI_IOCTL_BENCHMARK_COMMAND:
  183. case SCSI_IOCTL_SYNC:
  184. case SCSI_IOCTL_START_UNIT:
  185. case SCSI_IOCTL_STOP_UNIT:
  186. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  187. "ioctl, please convert it to SG_IO\n", current->comm);
  188. break;
  189. default:
  190. break;
  191. }
  192. switch (cmd) {
  193. case SCSI_IOCTL_GET_IDLUN:
  194. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  195. return -EFAULT;
  196. __put_user((sdev->id & 0xff)
  197. + ((sdev->lun & 0xff) << 8)
  198. + ((sdev->channel & 0xff) << 16)
  199. + ((sdev->host->host_no & 0xff) << 24),
  200. &((struct scsi_idlun __user *)arg)->dev_id);
  201. __put_user(sdev->host->unique_id,
  202. &((struct scsi_idlun __user *)arg)->host_unique_id);
  203. return 0;
  204. case SCSI_IOCTL_GET_BUS_NUMBER:
  205. return put_user(sdev->host->host_no, (int __user *)arg);
  206. case SCSI_IOCTL_PROBE_HOST:
  207. return ioctl_probe(sdev->host, arg);
  208. case SCSI_IOCTL_SEND_COMMAND:
  209. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  210. return -EACCES;
  211. return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
  212. case SCSI_IOCTL_DOORLOCK:
  213. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  214. case SCSI_IOCTL_DOORUNLOCK:
  215. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  216. case SCSI_IOCTL_TEST_UNIT_READY:
  217. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  218. NORMAL_RETRIES, NULL);
  219. case SCSI_IOCTL_START_UNIT:
  220. scsi_cmd[0] = START_STOP;
  221. scsi_cmd[1] = 0;
  222. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  223. scsi_cmd[4] = 1;
  224. return ioctl_internal_command(sdev, scsi_cmd,
  225. START_STOP_TIMEOUT, NORMAL_RETRIES);
  226. case SCSI_IOCTL_STOP_UNIT:
  227. scsi_cmd[0] = START_STOP;
  228. scsi_cmd[1] = 0;
  229. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  230. scsi_cmd[4] = 0;
  231. return ioctl_internal_command(sdev, scsi_cmd,
  232. START_STOP_TIMEOUT, NORMAL_RETRIES);
  233. case SCSI_IOCTL_GET_PCI:
  234. return scsi_ioctl_get_pci(sdev, arg);
  235. case SG_SCSI_RESET:
  236. return scsi_ioctl_reset(sdev, arg);
  237. default:
  238. if (sdev->host->hostt->ioctl)
  239. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  240. }
  241. return -EINVAL;
  242. }
  243. EXPORT_SYMBOL(scsi_ioctl);
  244. /*
  245. * We can process a reset even when a device isn't fully operable.
  246. */
  247. int scsi_ioctl_block_when_processing_errors(struct scsi_device *sdev, int cmd,
  248. bool ndelay)
  249. {
  250. if (cmd == SG_SCSI_RESET && ndelay) {
  251. if (scsi_host_in_recovery(sdev->host))
  252. return -EAGAIN;
  253. } else {
  254. if (!scsi_block_when_processing_errors(sdev))
  255. return -ENODEV;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(scsi_ioctl_block_when_processing_errors);