xref: /openbmc/linux/arch/s390/kernel/cpcmd.c (revision 6aa7de05)
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/export.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/stddef.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <asm/diag.h>
19 #include <asm/ebcdic.h>
20 #include <asm/cpcmd.h>
21 #include <asm/io.h>
22 
23 static DEFINE_SPINLOCK(cpcmd_lock);
24 static char cpcmd_buf[241];
25 
26 static int diag8_noresponse(int cmdlen)
27 {
28 	register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
29 	register unsigned long reg3 asm ("3") = cmdlen;
30 
31 	asm volatile(
32 		"	diag	%1,%0,0x8\n"
33 		: "+d" (reg3) : "d" (reg2) : "cc");
34 	return reg3;
35 }
36 
37 static int diag8_response(int cmdlen, char *response, int *rlen)
38 {
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 	asm volatile(
45 		"	diag	%2,%0,0x8\n"
46 		"	brc	8,1f\n"
47 		"	agr	%1,%4\n"
48 		"1:\n"
49 		: "+d" (reg4), "+d" (reg5)
50 		: "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
51 	*rlen = reg5;
52 	return reg4;
53 }
54 
55 /*
56  * __cpcmd has some restrictions over cpcmd
57  *  - __cpcmd is unlocked and therefore not SMP-safe
58  */
59 int  __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
60 {
61 	int cmdlen;
62 	int rc;
63 	int response_len;
64 
65 	cmdlen = strlen(cmd);
66 	BUG_ON(cmdlen > 240);
67 	memcpy(cpcmd_buf, cmd, cmdlen);
68 	ASCEBC(cpcmd_buf, cmdlen);
69 
70 	diag_stat_inc(DIAG_STAT_X008);
71 	if (response) {
72 		memset(response, 0, rlen);
73 		response_len = rlen;
74 		rc = diag8_response(cmdlen, response, &rlen);
75 		EBCASC(response, response_len);
76         } else {
77 		rc = diag8_noresponse(cmdlen);
78         }
79 	if (response_code)
80 		*response_code = rc;
81 	return rlen;
82 }
83 EXPORT_SYMBOL(__cpcmd);
84 
85 int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
86 {
87 	unsigned long flags;
88 	char *lowbuf;
89 	int len;
90 
91 	if (is_vmalloc_or_module_addr(response)) {
92 		lowbuf = kmalloc(rlen, GFP_KERNEL);
93 		if (!lowbuf) {
94 			pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
95 			return -ENOMEM;
96 		}
97 		spin_lock_irqsave(&cpcmd_lock, flags);
98 		len = __cpcmd(cmd, lowbuf, rlen, response_code);
99 		spin_unlock_irqrestore(&cpcmd_lock, flags);
100 		memcpy(response, lowbuf, rlen);
101 		kfree(lowbuf);
102 	} else {
103 		spin_lock_irqsave(&cpcmd_lock, flags);
104 		len = __cpcmd(cmd, response, rlen, response_code);
105 		spin_unlock_irqrestore(&cpcmd_lock, flags);
106 	}
107 	return len;
108 }
109 EXPORT_SYMBOL(cpcmd);
110