1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright IBM Corp. 1999, 2007
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
6 * Christian Borntraeger (cborntra@de.ibm.com),
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)
30 " diag %[rx],%[ry],0x8\n"
32 : [rx] "d" ((addr_t) cpcmd_buf)
37 static int diag8_response(int cmdlen, char *response, int *rlen)
39 union register_pair rx, ry;
42 rx.even = (addr_t) cpcmd_buf;
43 rx.odd = (addr_t) response;
44 ry.even = cmdlen | 0x40000000L;
47 " diag %[rx],%[ry],0x8\n"
50 : [cc] "=&d" (cc), [ry] "+&d" (ry.pair)
61 * __cpcmd has some restrictions over cpcmd
62 * - __cpcmd is unlocked and therefore not SMP-safe
64 int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
72 memcpy(cpcmd_buf, cmd, cmdlen);
73 ASCEBC(cpcmd_buf, cmdlen);
75 diag_stat_inc(DIAG_STAT_X008);
77 memset(response, 0, rlen);
79 rc = diag8_response(cmdlen, response, &rlen);
80 EBCASC(response, response_len);
82 rc = diag8_noresponse(cmdlen);
88 EXPORT_SYMBOL(__cpcmd);
90 int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
96 if (is_vmalloc_or_module_addr(response)) {
97 lowbuf = kmalloc(rlen, GFP_KERNEL);
99 pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
102 spin_lock_irqsave(&cpcmd_lock, flags);
103 len = __cpcmd(cmd, lowbuf, rlen, response_code);
104 spin_unlock_irqrestore(&cpcmd_lock, flags);
105 memcpy(response, lowbuf, rlen);
108 spin_lock_irqsave(&cpcmd_lock, flags);
109 len = __cpcmd(cmd, response, rlen, response_code);
110 spin_unlock_irqrestore(&cpcmd_lock, flags);
114 EXPORT_SYMBOL(cpcmd);