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