ide-ioctls.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IDE ioctls handling.
  4. */
  5. #include <linux/compat.h>
  6. #include <linux/export.h>
  7. #include <linux/hdreg.h>
  8. #include <linux/ide.h>
  9. #include <linux/slab.h>
  10. static int put_user_long(long val, unsigned long arg)
  11. {
  12. if (in_compat_syscall())
  13. return put_user(val, (compat_long_t __user *)compat_ptr(arg));
  14. return put_user(val, (long __user *)arg);
  15. }
  16. static const struct ide_ioctl_devset ide_ioctl_settings[] = {
  17. { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
  18. { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
  19. { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
  20. { HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
  21. { -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
  22. { 0 }
  23. };
  24. int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
  25. unsigned int cmd, unsigned long arg,
  26. const struct ide_ioctl_devset *s)
  27. {
  28. const struct ide_devset *ds;
  29. int err = -EOPNOTSUPP;
  30. for (; (ds = s->setting); s++) {
  31. if (ds->get && s->get_ioctl == cmd)
  32. goto read_val;
  33. else if (ds->set && s->set_ioctl == cmd)
  34. goto set_val;
  35. }
  36. return err;
  37. read_val:
  38. mutex_lock(&ide_setting_mtx);
  39. err = ds->get(drive);
  40. mutex_unlock(&ide_setting_mtx);
  41. return err >= 0 ? put_user_long(err, arg) : err;
  42. set_val:
  43. if (bdev_is_partition(bdev))
  44. err = -EINVAL;
  45. else {
  46. if (!capable(CAP_SYS_ADMIN))
  47. err = -EACCES;
  48. else {
  49. mutex_lock(&ide_setting_mtx);
  50. err = ide_devset_execute(drive, ds, arg);
  51. mutex_unlock(&ide_setting_mtx);
  52. }
  53. }
  54. return err;
  55. }
  56. EXPORT_SYMBOL_GPL(ide_setting_ioctl);
  57. static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
  58. void __user *argp)
  59. {
  60. u16 *id = NULL;
  61. int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
  62. int rc = 0;
  63. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  64. rc = -ENOMSG;
  65. goto out;
  66. }
  67. /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
  68. id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
  69. if (id == NULL) {
  70. rc = -ENOMEM;
  71. goto out;
  72. }
  73. memcpy(id, drive->id, size);
  74. ata_id_to_hd_driveid(id);
  75. if (copy_to_user(argp, id, size))
  76. rc = -EFAULT;
  77. kfree(id);
  78. out:
  79. return rc;
  80. }
  81. static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  82. {
  83. return put_user_long((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
  84. << IDE_NICE_DSC_OVERLAP) |
  85. (!!(drive->dev_flags & IDE_DFLAG_NICE1)
  86. << IDE_NICE_1), arg);
  87. }
  88. static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  89. {
  90. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  91. return -EPERM;
  92. if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
  93. (drive->media != ide_tape))
  94. return -EPERM;
  95. if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
  96. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  97. else
  98. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  99. if ((arg >> IDE_NICE_1) & 1)
  100. drive->dev_flags |= IDE_DFLAG_NICE1;
  101. else
  102. drive->dev_flags &= ~IDE_DFLAG_NICE1;
  103. return 0;
  104. }
  105. static int ide_cmd_ioctl(ide_drive_t *drive, void __user *argp)
  106. {
  107. u8 *buf = NULL;
  108. int bufsize = 0, err = 0;
  109. u8 args[4], xfer_rate = 0;
  110. struct ide_cmd cmd;
  111. struct ide_taskfile *tf = &cmd.tf;
  112. if (NULL == argp) {
  113. struct request *rq;
  114. rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
  115. ide_req(rq)->type = ATA_PRIV_TASKFILE;
  116. blk_execute_rq(drive->queue, NULL, rq, 0);
  117. err = scsi_req(rq)->result ? -EIO : 0;
  118. blk_put_request(rq);
  119. return err;
  120. }
  121. if (copy_from_user(args, argp, 4))
  122. return -EFAULT;
  123. memset(&cmd, 0, sizeof(cmd));
  124. tf->feature = args[2];
  125. if (args[0] == ATA_CMD_SMART) {
  126. tf->nsect = args[3];
  127. tf->lbal = args[1];
  128. tf->lbam = ATA_SMART_LBAM_PASS;
  129. tf->lbah = ATA_SMART_LBAH_PASS;
  130. cmd.valid.out.tf = IDE_VALID_OUT_TF;
  131. cmd.valid.in.tf = IDE_VALID_NSECT;
  132. } else {
  133. tf->nsect = args[1];
  134. cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
  135. cmd.valid.in.tf = IDE_VALID_NSECT;
  136. }
  137. tf->command = args[0];
  138. cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
  139. if (args[3]) {
  140. cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
  141. bufsize = SECTOR_SIZE * args[3];
  142. buf = kzalloc(bufsize, GFP_KERNEL);
  143. if (buf == NULL)
  144. return -ENOMEM;
  145. }
  146. if (tf->command == ATA_CMD_SET_FEATURES &&
  147. tf->feature == SETFEATURES_XFER &&
  148. tf->nsect >= XFER_SW_DMA_0) {
  149. xfer_rate = ide_find_dma_mode(drive, tf->nsect);
  150. if (xfer_rate != tf->nsect) {
  151. err = -EINVAL;
  152. goto abort;
  153. }
  154. cmd.tf_flags |= IDE_TFLAG_SET_XFER;
  155. }
  156. err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
  157. args[0] = tf->status;
  158. args[1] = tf->error;
  159. args[2] = tf->nsect;
  160. abort:
  161. if (copy_to_user(argp, &args, 4))
  162. err = -EFAULT;
  163. if (buf) {
  164. if (copy_to_user((argp + 4), buf, bufsize))
  165. err = -EFAULT;
  166. kfree(buf);
  167. }
  168. return err;
  169. }
  170. static int ide_task_ioctl(ide_drive_t *drive, void __user *p)
  171. {
  172. int err = 0;
  173. u8 args[7];
  174. struct ide_cmd cmd;
  175. if (copy_from_user(args, p, 7))
  176. return -EFAULT;
  177. memset(&cmd, 0, sizeof(cmd));
  178. memcpy(&cmd.tf.feature, &args[1], 6);
  179. cmd.tf.command = args[0];
  180. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  181. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  182. err = ide_no_data_taskfile(drive, &cmd);
  183. args[0] = cmd.tf.command;
  184. memcpy(&args[1], &cmd.tf.feature, 6);
  185. if (copy_to_user(p, args, 7))
  186. err = -EFAULT;
  187. return err;
  188. }
  189. static int generic_drive_reset(ide_drive_t *drive)
  190. {
  191. struct request *rq;
  192. int ret = 0;
  193. rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
  194. ide_req(rq)->type = ATA_PRIV_MISC;
  195. scsi_req(rq)->cmd_len = 1;
  196. scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
  197. blk_execute_rq(drive->queue, NULL, rq, 1);
  198. ret = scsi_req(rq)->result;
  199. blk_put_request(rq);
  200. return ret;
  201. }
  202. int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
  203. unsigned int cmd, unsigned long arg)
  204. {
  205. int err;
  206. void __user *argp = (void __user *)arg;
  207. if (in_compat_syscall())
  208. argp = compat_ptr(arg);
  209. err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
  210. if (err != -EOPNOTSUPP)
  211. return err;
  212. switch (cmd) {
  213. case HDIO_OBSOLETE_IDENTITY:
  214. case HDIO_GET_IDENTITY:
  215. if (bdev_is_partition(bdev))
  216. return -EINVAL;
  217. return ide_get_identity_ioctl(drive, cmd, argp);
  218. case HDIO_GET_NICE:
  219. return ide_get_nice_ioctl(drive, arg);
  220. case HDIO_SET_NICE:
  221. if (!capable(CAP_SYS_ADMIN))
  222. return -EACCES;
  223. return ide_set_nice_ioctl(drive, arg);
  224. #ifdef CONFIG_IDE_TASK_IOCTL
  225. case HDIO_DRIVE_TASKFILE:
  226. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  227. return -EACCES;
  228. /* missing compat handler for HDIO_DRIVE_TASKFILE */
  229. if (in_compat_syscall())
  230. return -ENOTTY;
  231. if (drive->media == ide_disk)
  232. return ide_taskfile_ioctl(drive, arg);
  233. return -ENOMSG;
  234. #endif
  235. case HDIO_DRIVE_CMD:
  236. if (!capable(CAP_SYS_RAWIO))
  237. return -EACCES;
  238. return ide_cmd_ioctl(drive, argp);
  239. case HDIO_DRIVE_TASK:
  240. if (!capable(CAP_SYS_RAWIO))
  241. return -EACCES;
  242. return ide_task_ioctl(drive, argp);
  243. case HDIO_DRIVE_RESET:
  244. if (!capable(CAP_SYS_ADMIN))
  245. return -EACCES;
  246. return generic_drive_reset(drive);
  247. case HDIO_GET_BUSSTATE:
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -EACCES;
  250. if (put_user_long(BUSSTATE_ON, arg))
  251. return -EFAULT;
  252. return 0;
  253. case HDIO_SET_BUSSTATE:
  254. if (!capable(CAP_SYS_ADMIN))
  255. return -EACCES;
  256. return -EOPNOTSUPP;
  257. default:
  258. return -EINVAL;
  259. }
  260. }
  261. EXPORT_SYMBOL(generic_ide_ioctl);