Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/s390/kernel/cpcmd.c |
| 3 | * |
| 4 | * S390 version |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 5 | * Copyright (C) 1999,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation |
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> |
| 19 | |
| 20 | static DEFINE_SPINLOCK(cpcmd_lock); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 21 | static char cpcmd_buf[241]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * the caller of __cpcmd has to ensure that the response buffer is below 2 GB |
| 25 | */ |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 26 | int __cpcmd(const char *cmd, char *response, int rlen, int *response_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | const int mask = 0x40000000L; |
| 29 | unsigned long flags; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 30 | int return_code; |
| 31 | int return_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | int cmdlen; |
| 33 | |
| 34 | spin_lock_irqsave(&cpcmd_lock, flags); |
| 35 | cmdlen = strlen(cmd); |
| 36 | BUG_ON(cmdlen > 240); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 37 | memcpy(cpcmd_buf, cmd, cmdlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | ASCEBC(cpcmd_buf, cmdlen); |
| 39 | |
| 40 | if (response != NULL && rlen > 0) { |
| 41 | memset(response, 0, rlen); |
| 42 | #ifndef CONFIG_ARCH_S390X |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 43 | asm volatile ( "lra 2,0(%2)\n" |
| 44 | "lr 4,%3\n" |
| 45 | "o 4,%6\n" |
| 46 | "lra 3,0(%4)\n" |
| 47 | "lr 5,%5\n" |
| 48 | "diag 2,4,0x8\n" |
Al Viro | 0cbdff4 | 2005-08-23 22:47:32 +0100 | [diff] [blame] | 49 | "brc 8, 1f\n" |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 50 | "ar 5, %5\n" |
Al Viro | 0cbdff4 | 2005-08-23 22:47:32 +0100 | [diff] [blame] | 51 | "1: \n" |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 52 | "lr %0,4\n" |
| 53 | "lr %1,5\n" |
| 54 | : "=d" (return_code), "=d" (return_len) |
| 55 | : "a" (cpcmd_buf), "d" (cmdlen), |
| 56 | "a" (response), "d" (rlen), "m" (mask) |
| 57 | : "cc", "2", "3", "4", "5" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #else /* CONFIG_ARCH_S390X */ |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 59 | asm volatile ( "lrag 2,0(%2)\n" |
| 60 | "lgr 4,%3\n" |
| 61 | "o 4,%6\n" |
| 62 | "lrag 3,0(%4)\n" |
| 63 | "lgr 5,%5\n" |
| 64 | "sam31\n" |
| 65 | "diag 2,4,0x8\n" |
| 66 | "sam64\n" |
Al Viro | 0cbdff4 | 2005-08-23 22:47:32 +0100 | [diff] [blame] | 67 | "brc 8, 1f\n" |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 68 | "agr 5, %5\n" |
Al Viro | 0cbdff4 | 2005-08-23 22:47:32 +0100 | [diff] [blame] | 69 | "1: \n" |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 70 | "lgr %0,4\n" |
| 71 | "lgr %1,5\n" |
| 72 | : "=d" (return_code), "=d" (return_len) |
| 73 | : "a" (cpcmd_buf), "d" (cmdlen), |
| 74 | "a" (response), "d" (rlen), "m" (mask) |
| 75 | : "cc", "2", "3", "4", "5" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #endif /* CONFIG_ARCH_S390X */ |
| 77 | EBCASC(response, rlen); |
| 78 | } else { |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 79 | return_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #ifndef CONFIG_ARCH_S390X |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 81 | asm volatile ( "lra 2,0(%1)\n" |
| 82 | "lr 3,%2\n" |
| 83 | "diag 2,3,0x8\n" |
| 84 | "lr %0,3\n" |
| 85 | : "=d" (return_code) |
| 86 | : "a" (cpcmd_buf), "d" (cmdlen) |
| 87 | : "2", "3" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | #else /* CONFIG_ARCH_S390X */ |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 89 | asm volatile ( "lrag 2,0(%1)\n" |
| 90 | "lgr 3,%2\n" |
| 91 | "sam31\n" |
| 92 | "diag 2,3,0x8\n" |
| 93 | "sam64\n" |
| 94 | "lgr %0,3\n" |
| 95 | : "=d" (return_code) |
| 96 | : "a" (cpcmd_buf), "d" (cmdlen) |
| 97 | : "2", "3" ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #endif /* CONFIG_ARCH_S390X */ |
| 99 | } |
| 100 | spin_unlock_irqrestore(&cpcmd_lock, flags); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 101 | if (response_code != NULL) |
| 102 | *response_code = return_code; |
| 103 | return return_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | EXPORT_SYMBOL(__cpcmd); |
| 107 | |
| 108 | #ifdef CONFIG_ARCH_S390X |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 109 | int cpcmd(const char *cmd, char *response, int rlen, int *response_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | char *lowbuf; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 112 | int len; |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if ((rlen == 0) || (response == NULL) |
| 115 | || !((unsigned long)response >> 31)) |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 116 | len = __cpcmd(cmd, response, rlen, response_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | else { |
| 118 | lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA); |
| 119 | if (!lowbuf) { |
| 120 | printk(KERN_WARNING |
| 121 | "cpcmd: could not allocate response buffer\n"); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 122 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 124 | len = __cpcmd(cmd, lowbuf, rlen, response_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | memcpy(response, lowbuf, rlen); |
| 126 | kfree(lowbuf); |
| 127 | } |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 128 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | EXPORT_SYMBOL(cpcmd); |
| 132 | #endif /* CONFIG_ARCH_S390X */ |