1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright IBM Corp. 1999, 2007
9 #define KMSG_COMPONENT "cpcmd"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
20 #include <asm/ebcdic.h>
21 #include <asm/cpcmd.h>
24 static DEFINE_SPINLOCK(cpcmd_lock);
25 static char cpcmd_buf[241];
27 static int diag8_noresponse(int cmdlen)
29 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
30 register unsigned long reg3 asm ("3") = cmdlen;
34 : "+d" (reg3) : "d" (reg2) : "cc");
38 static int diag8_response(int cmdlen, char *response, int *rlen)
40 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
41 register unsigned long reg3 asm ("3") = (addr_t) response;
42 register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
43 register unsigned long reg5 asm ("5") = *rlen;
50 : "+d" (reg4), "+d" (reg5)
51 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
57 * __cpcmd has some restrictions over cpcmd
58 * - __cpcmd is unlocked and therefore not SMP-safe
60 int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
68 memcpy(cpcmd_buf, cmd, cmdlen);
69 ASCEBC(cpcmd_buf, cmdlen);
71 diag_stat_inc(DIAG_STAT_X008);
73 memset(response, 0, rlen);
75 rc = diag8_response(cmdlen, response, &rlen);
76 EBCASC(response, response_len);
78 rc = diag8_noresponse(cmdlen);
84 EXPORT_SYMBOL(__cpcmd);
86 int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
92 if (is_vmalloc_or_module_addr(response)) {
93 lowbuf = kmalloc(rlen, GFP_KERNEL);
95 pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
98 spin_lock_irqsave(&cpcmd_lock, flags);
99 len = __cpcmd(cmd, lowbuf, rlen, response_code);
100 spin_unlock_irqrestore(&cpcmd_lock, flags);
101 memcpy(response, lowbuf, rlen);
104 spin_lock_irqsave(&cpcmd_lock, flags);
105 len = __cpcmd(cmd, response, rlen, response_code);
106 spin_unlock_irqrestore(&cpcmd_lock, flags);
110 EXPORT_SYMBOL(cpcmd);