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