scsi_ioctl.c 14 KB

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