hypfs_sprp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/sclp.h>
  16. #include "hypfs.h"
  17. #define DIAG304_SET_WEIGHTS 0
  18. #define DIAG304_QUERY_PRP 1
  19. #define DIAG304_SET_CAPPING 2
  20. #define DIAG304_CMD_MAX 2
  21. static unsigned long hypfs_sprp_diag304(void *data, unsigned long cmd)
  22. {
  23. register unsigned long _data asm("2") = (unsigned long) data;
  24. register unsigned long _rc asm("3");
  25. register unsigned long _cmd asm("4") = cmd;
  26. asm volatile("diag %1,%2,0x304\n"
  27. : "=d" (_rc) : "d" (_data), "d" (_cmd) : "memory");
  28. return _rc;
  29. }
  30. static void hypfs_sprp_free(const void *data)
  31. {
  32. free_page((unsigned long) data);
  33. }
  34. static int hypfs_sprp_create(void **data_ptr, void **free_ptr, size_t *size)
  35. {
  36. unsigned long rc;
  37. void *data;
  38. data = (void *) get_zeroed_page(GFP_KERNEL);
  39. if (!data)
  40. return -ENOMEM;
  41. rc = hypfs_sprp_diag304(data, DIAG304_QUERY_PRP);
  42. if (rc != 1) {
  43. *data_ptr = *free_ptr = NULL;
  44. *size = 0;
  45. free_page((unsigned long) data);
  46. return -EIO;
  47. }
  48. *data_ptr = *free_ptr = data;
  49. *size = PAGE_SIZE;
  50. return 0;
  51. }
  52. static int __hypfs_sprp_ioctl(void __user *user_area)
  53. {
  54. struct hypfs_diag304 diag304;
  55. unsigned long cmd;
  56. void __user *udata;
  57. void *data;
  58. int rc;
  59. if (copy_from_user(&diag304, user_area, sizeof(diag304)))
  60. return -EFAULT;
  61. if ((diag304.args[0] >> 8) != 0 || diag304.args[1] > DIAG304_CMD_MAX)
  62. return -EINVAL;
  63. data = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  64. if (!data)
  65. return -ENOMEM;
  66. udata = (void __user *)(unsigned long) diag304.data;
  67. if (diag304.args[1] == DIAG304_SET_WEIGHTS ||
  68. diag304.args[1] == DIAG304_SET_CAPPING)
  69. if (copy_from_user(data, udata, PAGE_SIZE)) {
  70. rc = -EFAULT;
  71. goto out;
  72. }
  73. cmd = *(unsigned long *) &diag304.args[0];
  74. diag304.rc = hypfs_sprp_diag304(data, cmd);
  75. if (diag304.args[1] == DIAG304_QUERY_PRP)
  76. if (copy_to_user(udata, data, PAGE_SIZE)) {
  77. rc = -EFAULT;
  78. goto out;
  79. }
  80. rc = copy_to_user(user_area, &diag304, sizeof(diag304)) ? -EFAULT : 0;
  81. out:
  82. free_page((unsigned long) data);
  83. return rc;
  84. }
  85. static long hypfs_sprp_ioctl(struct file *file, unsigned int cmd,
  86. unsigned long arg)
  87. {
  88. void __user *argp;
  89. if (!capable(CAP_SYS_ADMIN))
  90. return -EACCES;
  91. if (is_compat_task())
  92. argp = compat_ptr(arg);
  93. else
  94. argp = (void __user *) arg;
  95. switch (cmd) {
  96. case HYPFS_DIAG304:
  97. return __hypfs_sprp_ioctl(argp);
  98. default: /* unknown ioctl number */
  99. return -ENOTTY;
  100. }
  101. return 0;
  102. }
  103. static struct hypfs_dbfs_file hypfs_sprp_file = {
  104. .name = "diag_304",
  105. .data_create = hypfs_sprp_create,
  106. .data_free = hypfs_sprp_free,
  107. .unlocked_ioctl = hypfs_sprp_ioctl,
  108. };
  109. int hypfs_sprp_init(void)
  110. {
  111. if (!sclp_has_sprp())
  112. return 0;
  113. return hypfs_dbfs_create_file(&hypfs_sprp_file);
  114. }
  115. void hypfs_sprp_exit(void)
  116. {
  117. if (!sclp_has_sprp())
  118. return;
  119. hypfs_dbfs_remove_file(&hypfs_sprp_file);
  120. }