hypfs_sprp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hypervisor filesystem for Linux on s390.
  4. * Set Partition-Resource Parameter interface.
  5. *
  6. * Copyright IBM Corp. 2013
  7. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #include <linux/compat.h>
  10. #include <linux/errno.h>
  11. #include <linux/gfp.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/compat.h>
  16. #include <asm/diag.h>
  17. #include <asm/sclp.h>
  18. #include "hypfs.h"
  19. #define DIAG304_SET_WEIGHTS 0
  20. #define DIAG304_QUERY_PRP 1
  21. #define DIAG304_SET_CAPPING 2
  22. #define DIAG304_CMD_MAX 2
  23. static inline unsigned long __hypfs_sprp_diag304(void *data, unsigned long cmd)
  24. {
  25. register unsigned long _data asm("2") = (unsigned long) data;
  26. register unsigned long _rc asm("3");
  27. register unsigned long _cmd asm("4") = cmd;
  28. asm volatile("diag %1,%2,0x304\n"
  29. : "=d" (_rc) : "d" (_data), "d" (_cmd) : "memory");
  30. return _rc;
  31. }
  32. static unsigned long hypfs_sprp_diag304(void *data, unsigned long cmd)
  33. {
  34. diag_stat_inc(DIAG_STAT_X304);
  35. return __hypfs_sprp_diag304(data, cmd);
  36. }
  37. static void hypfs_sprp_free(const void *data)
  38. {
  39. free_page((unsigned long) data);
  40. }
  41. static int hypfs_sprp_create(void **data_ptr, void **free_ptr, size_t *size)
  42. {
  43. unsigned long rc;
  44. void *data;
  45. data = (void *) get_zeroed_page(GFP_KERNEL);
  46. if (!data)
  47. return -ENOMEM;
  48. rc = hypfs_sprp_diag304(data, DIAG304_QUERY_PRP);
  49. if (rc != 1) {
  50. *data_ptr = *free_ptr = NULL;
  51. *size = 0;
  52. free_page((unsigned long) data);
  53. return -EIO;
  54. }
  55. *data_ptr = *free_ptr = data;
  56. *size = PAGE_SIZE;
  57. return 0;
  58. }
  59. static int __hypfs_sprp_ioctl(void __user *user_area)
  60. {
  61. struct hypfs_diag304 diag304;
  62. unsigned long cmd;
  63. void __user *udata;
  64. void *data;
  65. int rc;
  66. if (copy_from_user(&diag304, user_area, sizeof(diag304)))
  67. return -EFAULT;
  68. if ((diag304.args[0] >> 8) != 0 || diag304.args[1] > DIAG304_CMD_MAX)
  69. return -EINVAL;
  70. data = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  71. if (!data)
  72. return -ENOMEM;
  73. udata = (void __user *)(unsigned long) diag304.data;
  74. if (diag304.args[1] == DIAG304_SET_WEIGHTS ||
  75. diag304.args[1] == DIAG304_SET_CAPPING)
  76. if (copy_from_user(data, udata, PAGE_SIZE)) {
  77. rc = -EFAULT;
  78. goto out;
  79. }
  80. cmd = *(unsigned long *) &diag304.args[0];
  81. diag304.rc = hypfs_sprp_diag304(data, cmd);
  82. if (diag304.args[1] == DIAG304_QUERY_PRP)
  83. if (copy_to_user(udata, data, PAGE_SIZE)) {
  84. rc = -EFAULT;
  85. goto out;
  86. }
  87. rc = copy_to_user(user_area, &diag304, sizeof(diag304)) ? -EFAULT : 0;
  88. out:
  89. free_page((unsigned long) data);
  90. return rc;
  91. }
  92. static long hypfs_sprp_ioctl(struct file *file, unsigned int cmd,
  93. unsigned long arg)
  94. {
  95. void __user *argp;
  96. if (!capable(CAP_SYS_ADMIN))
  97. return -EACCES;
  98. if (is_compat_task())
  99. argp = compat_ptr(arg);
  100. else
  101. argp = (void __user *) arg;
  102. switch (cmd) {
  103. case HYPFS_DIAG304:
  104. return __hypfs_sprp_ioctl(argp);
  105. default: /* unknown ioctl number */
  106. return -ENOTTY;
  107. }
  108. return 0;
  109. }
  110. static struct hypfs_dbfs_file hypfs_sprp_file = {
  111. .name = "diag_304",
  112. .data_create = hypfs_sprp_create,
  113. .data_free = hypfs_sprp_free,
  114. .unlocked_ioctl = hypfs_sprp_ioctl,
  115. };
  116. int hypfs_sprp_init(void)
  117. {
  118. if (!sclp.has_sprp)
  119. return 0;
  120. return hypfs_dbfs_create_file(&hypfs_sprp_file);
  121. }
  122. void hypfs_sprp_exit(void)
  123. {
  124. if (!sclp.has_sprp)
  125. return;
  126. hypfs_dbfs_remove_file(&hypfs_sprp_file);
  127. }