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