1 /*
2 * S390 version
3 * Copyright IBM Corp. 1999, 2007
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
5 * Christian Borntraeger (cborntra@de.ibm.com),
6 */
7
8 #define KMSG_COMPONENT "cpcmd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
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/diag.h>
18 #include <asm/ebcdic.h>
19 #include <asm/cpcmd.h>
20 #include <asm/io.h>
21
22 static DEFINE_SPINLOCK(cpcmd_lock);
23 static char cpcmd_buf[241];
24
diag8_noresponse(int cmdlen)25 static int diag8_noresponse(int cmdlen)
26 {
27 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
28 register unsigned long reg3 asm ("3") = cmdlen;
29
30 asm volatile(
31 " sam31\n"
32 " diag %1,%0,0x8\n"
33 " sam64\n"
34 : "+d" (reg3) : "d" (reg2) : "cc");
35 return reg3;
36 }
37
diag8_response(int cmdlen,char * response,int * rlen)38 static int diag8_response(int cmdlen, char *response, int *rlen)
39 {
40 unsigned long _cmdlen = cmdlen | 0x40000000L;
41 unsigned long _rlen = *rlen;
42 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
43 register unsigned long reg3 asm ("3") = (addr_t) response;
44 register unsigned long reg4 asm ("4") = _cmdlen;
45 register unsigned long reg5 asm ("5") = _rlen;
46
47 asm volatile(
48 " sam31\n"
49 " diag %2,%0,0x8\n"
50 " sam64\n"
51 " brc 8,1f\n"
52 " agr %1,%4\n"
53 "1:\n"
54 : "+d" (reg4), "+d" (reg5)
55 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
56 *rlen = reg5;
57 return reg4;
58 }
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 */
__cpcmd(const char * cmd,char * response,int rlen,int * response_code)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
71 cmdlen = strlen(cmd);
72 BUG_ON(cmdlen > 240);
73 memcpy(cpcmd_buf, cmd, cmdlen);
74 ASCEBC(cpcmd_buf, cmdlen);
75
76 diag_stat_inc(DIAG_STAT_X008);
77 if (response) {
78 memset(response, 0, rlen);
79 response_len = rlen;
80 rc = diag8_response(cmdlen, response, &rlen);
81 EBCASC(response, response_len);
82 } else {
83 rc = diag8_noresponse(cmdlen);
84 }
85 if (response_code)
86 *response_code = rc;
87 return rlen;
88 }
89 EXPORT_SYMBOL(__cpcmd);
90
cpcmd(const char * cmd,char * response,int rlen,int * response_code)91 int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
92 {
93 char *lowbuf;
94 int len;
95 unsigned long flags;
96
97 if ((virt_to_phys(response) != (unsigned long) response) ||
98 (((unsigned long)response + rlen) >> 31)) {
99 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
100 if (!lowbuf) {
101 pr_warning("The cpcmd kernel function failed to "
102 "allocate a response buffer\n");
103 return -ENOMEM;
104 }
105 spin_lock_irqsave(&cpcmd_lock, flags);
106 len = __cpcmd(cmd, lowbuf, rlen, response_code);
107 spin_unlock_irqrestore(&cpcmd_lock, flags);
108 memcpy(response, lowbuf, rlen);
109 kfree(lowbuf);
110 } else {
111 spin_lock_irqsave(&cpcmd_lock, flags);
112 len = __cpcmd(cmd, response, rlen, response_code);
113 spin_unlock_irqrestore(&cpcmd_lock, flags);
114 }
115 return len;
116 }
117 EXPORT_SYMBOL(cpcmd);
118