scsi_ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. * If we are told to probe a host, we will return 0 if the host is not
  30. * present, 1 if the host is present, and will return an identifying
  31. * string at *arg, if arg is non null, filling to the length stored at
  32. * (int *) arg
  33. */
  34. static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
  35. {
  36. unsigned int len, slen;
  37. const char *string;
  38. int temp = host->hostt->present;
  39. if (temp && buffer) {
  40. if (get_user(len, (unsigned int __user *) buffer))
  41. return -EFAULT;
  42. if (host->hostt->info)
  43. string = host->hostt->info(host);
  44. else
  45. string = host->hostt->name;
  46. if (string) {
  47. slen = strlen(string);
  48. if (len > slen)
  49. len = slen + 1;
  50. if (copy_to_user(buffer, string, len))
  51. return -EFAULT;
  52. }
  53. }
  54. return temp;
  55. }
  56. /*
  57. * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  58. * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
  59. *
  60. * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  61. * input data, if any, not including the command string & counts,
  62. * *((int *)arg + 1) is the output buffer size in bytes.
  63. *
  64. * *(char *) ((int *) arg)[2] the actual command byte.
  65. *
  66. * Note that if more than MAX_BUF bytes are requested to be transferred,
  67. * the ioctl will fail with error EINVAL.
  68. *
  69. * This size *does not* include the initial lengths that were passed.
  70. *
  71. * The SCSI command is read from the memory location immediately after the
  72. * length words, and the input data is right after the command. The SCSI
  73. * routines know the command size based on the opcode decode.
  74. *
  75. * The output area is then filled in starting from the command byte.
  76. */
  77. static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
  78. int timeout, int retries)
  79. {
  80. int result;
  81. struct scsi_sense_hdr sshdr;
  82. char sense[SCSI_SENSE_BUFFERSIZE];
  83. SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
  84. memset(sense, 0, sizeof(*sense));
  85. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
  86. sense, timeout, retries);
  87. SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", result));
  88. if ((driver_byte(result) & DRIVER_SENSE) &&
  89. (scsi_normalize_sense(sense, sizeof(*sense), &sshdr))) {
  90. switch (sshdr.sense_key) {
  91. case ILLEGAL_REQUEST:
  92. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  93. sdev->lockable = 0;
  94. else
  95. printk(KERN_INFO "ioctl_internal_command: "
  96. "ILLEGAL REQUEST asc=0x%x ascq=0x%x\n",
  97. sshdr.asc, sshdr.ascq);
  98. break;
  99. case NOT_READY: /* This happens if there is no disc in drive */
  100. if (sdev->removable && (cmd[0] != TEST_UNIT_READY)) {
  101. printk(KERN_INFO "Device not ready. Make sure"
  102. " there is a disc in the drive.\n");
  103. break;
  104. }
  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. printk(KERN_INFO "ioctl_internal_command: <%d %d %d "
  113. "%d> return code = %x\n",
  114. sdev->host->host_no,
  115. sdev->channel,
  116. sdev->id,
  117. sdev->lun,
  118. result);
  119. __scsi_print_sense(" ", sense, sizeof(*sense));
  120. break;
  121. }
  122. }
  123. SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
  124. return result;
  125. }
  126. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  127. {
  128. char scsi_cmd[MAX_COMMAND_SIZE];
  129. int ret;
  130. if (!sdev->removable || !sdev->lockable)
  131. return 0;
  132. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  133. scsi_cmd[1] = 0;
  134. scsi_cmd[2] = 0;
  135. scsi_cmd[3] = 0;
  136. scsi_cmd[4] = state;
  137. scsi_cmd[5] = 0;
  138. ret = ioctl_internal_command(sdev, scsi_cmd,
  139. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  140. if (ret == 0)
  141. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(scsi_set_medium_removal);
  145. /*
  146. * This interface is deprecated - users should use the scsi generic (sg)
  147. * interface instead, as this is a more flexible approach to performing
  148. * generic SCSI commands on a device.
  149. *
  150. * The structure that we are passed should look like:
  151. *
  152. * struct sdata {
  153. * unsigned int inlen; [i] Length of data to be written to device
  154. * unsigned int outlen; [i] Length of data to be read from device
  155. * unsigned char cmd[x]; [i] SCSI command (6 <= x <= 12).
  156. * [o] Data read from device starts here.
  157. * [o] On error, sense buffer starts here.
  158. * unsigned char wdata[y]; [i] Data written to device starts here.
  159. * };
  160. * Notes:
  161. * - The SCSI command length is determined by examining the 1st byte
  162. * of the given command. There is no way to override this.
  163. * - Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
  164. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  165. * accommodate the sense buffer when an error occurs.
  166. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  167. * old code will not be surprised.
  168. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  169. * a negative return and the Unix error code in 'errno'.
  170. * If the SCSI command succeeds then 0 is returned.
  171. * Positive numbers returned are the compacted SCSI error codes (4
  172. * bytes in one int) where the lowest byte is the SCSI status.
  173. * See the drivers/scsi/scsi.h file for more information on this.
  174. *
  175. */
  176. #define OMAX_SB_LEN 16 /* Old sense buffer length */
  177. int scsi_ioctl_send_command(struct scsi_device *sdev,
  178. struct scsi_ioctl_command __user *sic)
  179. {
  180. char *buf;
  181. unsigned char cmd[MAX_COMMAND_SIZE];
  182. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  183. char __user *cmd_in;
  184. unsigned char opcode;
  185. unsigned int inlen, outlen, cmdlen;
  186. unsigned int needed, buf_needed;
  187. int timeout, retries, result;
  188. int data_direction, 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_req(sdev, cmd, data_direction, buf, needed,
  279. sense, timeout, retries);
  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 (sdev->host->shost_state == SHOST_RECOVERY)
  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);