cpcmd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * arch/s390/kernel/cpcmd.c
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 1999,2007
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Christian Borntraeger (cborntra@de.ibm.com),
  8. */
  9. #define KMSG_COMPONENT "cpcmd"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/stddef.h>
  16. #include <linux/string.h>
  17. #include <asm/ebcdic.h>
  18. #include <asm/cpcmd.h>
  19. #include <asm/system.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. #ifndef CONFIG_64BIT
  29. " diag %1,%0,0x8\n"
  30. #else /* CONFIG_64BIT */
  31. " sam31\n"
  32. " diag %1,%0,0x8\n"
  33. " sam64\n"
  34. #endif /* CONFIG_64BIT */
  35. : "+d" (reg3) : "d" (reg2) : "cc");
  36. return reg3;
  37. }
  38. static int diag8_response(int cmdlen, char *response, int *rlen)
  39. {
  40. register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
  41. register unsigned long reg3 asm ("3") = (addr_t) response;
  42. register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
  43. register unsigned long reg5 asm ("5") = *rlen;
  44. asm volatile(
  45. #ifndef CONFIG_64BIT
  46. " diag %2,%0,0x8\n"
  47. " brc 8,1f\n"
  48. " ar %1,%4\n"
  49. #else /* CONFIG_64BIT */
  50. " sam31\n"
  51. " diag %2,%0,0x8\n"
  52. " sam64\n"
  53. " brc 8,1f\n"
  54. " agr %1,%4\n"
  55. #endif /* CONFIG_64BIT */
  56. "1:\n"
  57. : "+d" (reg4), "+d" (reg5)
  58. : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
  59. *rlen = reg5;
  60. return reg4;
  61. }
  62. /*
  63. * __cpcmd has some restrictions over cpcmd
  64. * - the response buffer must reside below 2GB (if any)
  65. * - __cpcmd is unlocked and therefore not SMP-safe
  66. */
  67. int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  68. {
  69. int cmdlen;
  70. int rc;
  71. int response_len;
  72. cmdlen = strlen(cmd);
  73. BUG_ON(cmdlen > 240);
  74. memcpy(cpcmd_buf, cmd, cmdlen);
  75. ASCEBC(cpcmd_buf, cmdlen);
  76. if (response) {
  77. memset(response, 0, rlen);
  78. response_len = rlen;
  79. rc = diag8_response(cmdlen, response, &rlen);
  80. EBCASC(response, response_len);
  81. } else {
  82. rc = diag8_noresponse(cmdlen);
  83. }
  84. if (response_code)
  85. *response_code = rc;
  86. return rlen;
  87. }
  88. EXPORT_SYMBOL(__cpcmd);
  89. int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
  90. {
  91. char *lowbuf;
  92. int len;
  93. unsigned long flags;
  94. if ((virt_to_phys(response) != (unsigned long) response) ||
  95. (((unsigned long)response + rlen) >> 31)) {
  96. lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
  97. if (!lowbuf) {
  98. pr_warning("The cpcmd kernel function failed to "
  99. "allocate a response buffer\n");
  100. return -ENOMEM;
  101. }
  102. spin_lock_irqsave(&cpcmd_lock, flags);
  103. len = __cpcmd(cmd, lowbuf, rlen, response_code);
  104. spin_unlock_irqrestore(&cpcmd_lock, flags);
  105. memcpy(response, lowbuf, rlen);
  106. kfree(lowbuf);
  107. } else {
  108. spin_lock_irqsave(&cpcmd_lock, flags);
  109. len = __cpcmd(cmd, response, rlen, response_code);
  110. spin_unlock_irqrestore(&cpcmd_lock, flags);
  111. }
  112. return len;
  113. }
  114. EXPORT_SYMBOL(cpcmd);