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