Loading...
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 <linux/io.h>
20#include <asm/diag.h>
21#include <asm/ebcdic.h>
22#include <asm/cpcmd.h>
23#include <asm/asm.h>
24
25static DEFINE_SPINLOCK(cpcmd_lock);
26static char cpcmd_buf[241];
27
28static int diag8_noresponse(int cmdlen)
29{
30 asm volatile(
31 " diag %[rx],%[ry],0x8\n"
32 : [ry] "+&d" (cmdlen)
33 : [rx] "d" (__pa(cpcmd_buf))
34 : "cc");
35 return cmdlen;
36}
37
38static int diag8_response(int cmdlen, char *response, int *rlen)
39{
40 union register_pair rx, ry;
41 int cc;
42
43 rx.even = __pa(cpcmd_buf);
44 rx.odd = __pa(response);
45 ry.even = cmdlen | 0x40000000L;
46 ry.odd = *rlen;
47 asm volatile(
48 " diag %[rx],%[ry],0x8\n"
49 CC_IPM(cc)
50 : CC_OUT(cc, cc), [ry] "+d" (ry.pair)
51 : [rx] "d" (rx.pair)
52 : CC_CLOBBER);
53 if (CC_TRANSFORM(cc))
54 *rlen += ry.odd;
55 else
56 *rlen = ry.odd;
57 return ry.even;
58}
59
60/*
61 * __cpcmd has some restrictions over cpcmd
62 * - __cpcmd is unlocked and therefore not SMP-safe
63 */
64int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
65{
66 int cmdlen;
67 int rc;
68 int response_len;
69
70 cmdlen = strlen(cmd);
71 BUG_ON(cmdlen > 240);
72 memcpy(cpcmd_buf, cmd, cmdlen);
73 ASCEBC(cpcmd_buf, cmdlen);
74
75 diag_stat_inc(DIAG_STAT_X008);
76 if (response) {
77 memset(response, 0, rlen);
78 response_len = rlen;
79 rc = diag8_response(cmdlen, response, &rlen);
80 EBCASC(response, response_len);
81 } else {
82 rc = diag8_noresponse(cmdlen);
83 }
84 if (response_code)
85 *response_code = rc;
86 return rlen;
87}
88EXPORT_SYMBOL(__cpcmd);
89
90int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
91{
92 unsigned long flags;
93 char *lowbuf;
94 int len;
95
96 if (is_vmalloc_or_module_addr(response)) {
97 lowbuf = kmalloc(rlen, GFP_KERNEL);
98 if (!lowbuf) {
99 pr_warn("The cpcmd kernel function failed to allocate a response buffer\n");
100 return -ENOMEM;
101 }
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);
106 kfree(lowbuf);
107 } else {
108 spin_lock_irqsave(&cpcmd_lock, flags);
109 len = __cpcmd(cmd, response, rlen, response_code);
110 spin_unlock_irqrestore(&cpcmd_lock, flags);
111 }
112 return len;
113}
114EXPORT_SYMBOL(cpcmd);
1/*
2 * arch/s390/kernel/cpcmd.c
3 *
4 * S390 version
5 * Copyright IBM Corp. 1999,2007
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Christian Borntraeger (cborntra@de.ibm.com),
8 */
9
10#define KMSG_COMPONENT "cpcmd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/stddef.h>
18#include <linux/string.h>
19#include <asm/ebcdic.h>
20#include <asm/cpcmd.h>
21#include <asm/io.h>
22
23static DEFINE_SPINLOCK(cpcmd_lock);
24static char cpcmd_buf[241];
25
26static 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#ifndef CONFIG_64BIT
33 " diag %1,%0,0x8\n"
34#else /* CONFIG_64BIT */
35 " sam31\n"
36 " diag %1,%0,0x8\n"
37 " sam64\n"
38#endif /* CONFIG_64BIT */
39 : "+d" (reg3) : "d" (reg2) : "cc");
40 return reg3;
41}
42
43static int diag8_response(int cmdlen, char *response, int *rlen)
44{
45 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
46 register unsigned long reg3 asm ("3") = (addr_t) response;
47 register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
48 register unsigned long reg5 asm ("5") = *rlen;
49
50 asm volatile(
51#ifndef CONFIG_64BIT
52 " diag %2,%0,0x8\n"
53 " brc 8,1f\n"
54 " ar %1,%4\n"
55#else /* CONFIG_64BIT */
56 " sam31\n"
57 " diag %2,%0,0x8\n"
58 " sam64\n"
59 " brc 8,1f\n"
60 " agr %1,%4\n"
61#endif /* CONFIG_64BIT */
62 "1:\n"
63 : "+d" (reg4), "+d" (reg5)
64 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
65 *rlen = reg5;
66 return reg4;
67}
68
69/*
70 * __cpcmd has some restrictions over cpcmd
71 * - the response buffer must reside below 2GB (if any)
72 * - __cpcmd is unlocked and therefore not SMP-safe
73 */
74int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
75{
76 int cmdlen;
77 int rc;
78 int response_len;
79
80 cmdlen = strlen(cmd);
81 BUG_ON(cmdlen > 240);
82 memcpy(cpcmd_buf, cmd, cmdlen);
83 ASCEBC(cpcmd_buf, cmdlen);
84
85 if (response) {
86 memset(response, 0, rlen);
87 response_len = rlen;
88 rc = diag8_response(cmdlen, response, &rlen);
89 EBCASC(response, response_len);
90 } else {
91 rc = diag8_noresponse(cmdlen);
92 }
93 if (response_code)
94 *response_code = rc;
95 return rlen;
96}
97EXPORT_SYMBOL(__cpcmd);
98
99int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
100{
101 char *lowbuf;
102 int len;
103 unsigned long flags;
104
105 if ((virt_to_phys(response) != (unsigned long) response) ||
106 (((unsigned long)response + rlen) >> 31)) {
107 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
108 if (!lowbuf) {
109 pr_warning("The cpcmd kernel function failed to "
110 "allocate a response buffer\n");
111 return -ENOMEM;
112 }
113 spin_lock_irqsave(&cpcmd_lock, flags);
114 len = __cpcmd(cmd, lowbuf, rlen, response_code);
115 spin_unlock_irqrestore(&cpcmd_lock, flags);
116 memcpy(response, lowbuf, rlen);
117 kfree(lowbuf);
118 } else {
119 spin_lock_irqsave(&cpcmd_lock, flags);
120 len = __cpcmd(cmd, response, rlen, response_code);
121 spin_unlock_irqrestore(&cpcmd_lock, flags);
122 }
123 return len;
124}
125EXPORT_SYMBOL(cpcmd);