cpcmd.c 3.0 KB

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