cpcmd.c 3.0 KB

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