cpcmd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
  35. register unsigned long reg3 asm ("3") = (addr_t) response;
  36. register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
  37. register unsigned long reg5 asm ("5") = *rlen;
  38. asm volatile(
  39. " diag %2,%0,0x8\n"
  40. " brc 8,1f\n"
  41. " agr %1,%4\n"
  42. "1:\n"
  43. : "+d" (reg4), "+d" (reg5)
  44. : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
  45. *rlen = reg5;
  46. return reg4;
  47. }
  48. /*
  49. * __cpcmd has some restrictions over cpcmd
  50. * - __cpcmd is unlocked and therefore not SMP-safe
  51. */
  52. int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  53. {
  54. int cmdlen;
  55. int rc;
  56. int response_len;
  57. cmdlen = strlen(cmd);
  58. BUG_ON(cmdlen > 240);
  59. memcpy(cpcmd_buf, cmd, cmdlen);
  60. ASCEBC(cpcmd_buf, cmdlen);
  61. diag_stat_inc(DIAG_STAT_X008);
  62. if (response) {
  63. memset(response, 0, rlen);
  64. response_len = rlen;
  65. rc = diag8_response(cmdlen, response, &rlen);
  66. EBCASC(response, response_len);
  67. } else {
  68. rc = diag8_noresponse(cmdlen);
  69. }
  70. if (response_code)
  71. *response_code = rc;
  72. return rlen;
  73. }
  74. EXPORT_SYMBOL(__cpcmd);
  75. int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  76. {
  77. unsigned long flags;
  78. char *lowbuf;
  79. int len;
  80. if (is_vmalloc_or_module_addr(response)) {
  81. lowbuf = kmalloc(rlen, GFP_KERNEL);
  82. if (!lowbuf) {
  83. pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
  84. return -ENOMEM;
  85. }
  86. spin_lock_irqsave(&cpcmd_lock, flags);
  87. len = __cpcmd(cmd, lowbuf, rlen, response_code);
  88. spin_unlock_irqrestore(&cpcmd_lock, flags);
  89. memcpy(response, lowbuf, rlen);
  90. kfree(lowbuf);
  91. } else {
  92. spin_lock_irqsave(&cpcmd_lock, flags);
  93. len = __cpcmd(cmd, response, rlen, response_code);
  94. spin_unlock_irqrestore(&cpcmd_lock, flags);
  95. }
  96. return len;
  97. }
  98. EXPORT_SYMBOL(cpcmd);