cpcmd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999, 2007
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  6. * Christian Borntraeger (cborntra@de.ibm.com),
  7. */
  8. #define KMSG_COMPONENT "cpcmd"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/stddef.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <asm/diag.h>
  18. #include <asm/ebcdic.h>
  19. #include <asm/cpcmd.h>
  20. #include <asm/io.h>
  21. static DEFINE_SPINLOCK(cpcmd_lock);
  22. static char cpcmd_buf[241];
  23. static int diag8_noresponse(int cmdlen)
  24. {
  25. register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
  26. register unsigned long reg3 asm ("3") = cmdlen;
  27. asm volatile(
  28. " diag %1,%0,0x8\n"
  29. : "+d" (reg3) : "d" (reg2) : "cc");
  30. return reg3;
  31. }
  32. static int diag8_response(int cmdlen, char *response, int *rlen)
  33. {
  34. unsigned long _cmdlen = cmdlen | 0x40000000L;
  35. unsigned long _rlen = *rlen;
  36. register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
  37. register unsigned long reg3 asm ("3") = (addr_t) response;
  38. register unsigned long reg4 asm ("4") = _cmdlen;
  39. register unsigned long reg5 asm ("5") = _rlen;
  40. asm volatile(
  41. " diag %2,%0,0x8\n"
  42. " brc 8,1f\n"
  43. " agr %1,%4\n"
  44. "1:\n"
  45. : "+d" (reg4), "+d" (reg5)
  46. : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
  47. *rlen = reg5;
  48. return reg4;
  49. }
  50. /*
  51. * __cpcmd has some restrictions over cpcmd
  52. * - __cpcmd is unlocked and therefore not SMP-safe
  53. */
  54. int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  55. {
  56. int cmdlen;
  57. int rc;
  58. int response_len;
  59. cmdlen = strlen(cmd);
  60. BUG_ON(cmdlen > 240);
  61. memcpy(cpcmd_buf, cmd, cmdlen);
  62. ASCEBC(cpcmd_buf, cmdlen);
  63. diag_stat_inc(DIAG_STAT_X008);
  64. if (response) {
  65. memset(response, 0, rlen);
  66. response_len = rlen;
  67. rc = diag8_response(cmdlen, response, &rlen);
  68. EBCASC(response, response_len);
  69. } else {
  70. rc = diag8_noresponse(cmdlen);
  71. }
  72. if (response_code)
  73. *response_code = rc;
  74. return rlen;
  75. }
  76. EXPORT_SYMBOL(__cpcmd);
  77. int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  78. {
  79. unsigned long flags;
  80. char *lowbuf;
  81. int len;
  82. if (is_vmalloc_or_module_addr(response)) {
  83. lowbuf = kmalloc(rlen, GFP_KERNEL);
  84. if (!lowbuf) {
  85. pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
  86. return -ENOMEM;
  87. }
  88. spin_lock_irqsave(&cpcmd_lock, flags);
  89. len = __cpcmd(cmd, lowbuf, rlen, response_code);
  90. spin_unlock_irqrestore(&cpcmd_lock, flags);
  91. memcpy(response, lowbuf, rlen);
  92. kfree(lowbuf);
  93. } else {
  94. spin_lock_irqsave(&cpcmd_lock, flags);
  95. len = __cpcmd(cmd, response, rlen, response_code);
  96. spin_unlock_irqrestore(&cpcmd_lock, flags);
  97. }
  98. return len;
  99. }
  100. EXPORT_SYMBOL(cpcmd);