hypfs_sprp.c 3.2 KB

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