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