cpcmd.c 2.9 KB

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