blob: 3e8b8816f309937c6ac5b2af395c346f98e19bb2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/cpcmd.c
3 *
4 * S390 version
Heiko Carstensa004fb02007-10-12 16:11:46 +02005 * Copyright IBM Corp. 1999,2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 * Christian Borntraeger (cborntra@de.ibm.com),
8 */
9
Christian Borntraeger2f526e52008-12-25 13:39:34 +010010#define KMSG_COMPONENT "cpcmd"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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/system.h>
Christian Borntraegerbda35632007-02-05 21:16:54 +010022#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static DEFINE_SPINLOCK(cpcmd_lock);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070025static char cpcmd_buf[241];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Heiko Carstensa004fb02007-10-12 16:11:46 +020027static 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(
33#ifndef CONFIG_64BIT
34 " diag %1,%0,0x8\n"
35#else /* CONFIG_64BIT */
36 " sam31\n"
37 " diag %1,%0,0x8\n"
38 " sam64\n"
39#endif /* CONFIG_64BIT */
40 : "+d" (reg3) : "d" (reg2) : "cc");
41 return reg3;
42}
43
44static int diag8_response(int cmdlen, char *response, int *rlen)
45{
46 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
47 register unsigned long reg3 asm ("3") = (addr_t) response;
48 register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
49 register unsigned long reg5 asm ("5") = *rlen;
50
51 asm volatile(
52#ifndef CONFIG_64BIT
53 " diag %2,%0,0x8\n"
54 " brc 8,1f\n"
55 " ar %1,%4\n"
56#else /* CONFIG_64BIT */
57 " sam31\n"
58 " diag %2,%0,0x8\n"
59 " sam64\n"
60 " brc 8,1f\n"
61 " agr %1,%4\n"
62#endif /* CONFIG_64BIT */
63 "1:\n"
64 : "+d" (reg4), "+d" (reg5)
65 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
66 *rlen = reg5;
67 return reg4;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Heiko Carstens740b5702006-12-04 15:40:30 +010071 * __cpcmd has some restrictions over cpcmd
72 * - the response buffer must reside below 2GB (if any)
73 * - __cpcmd is unlocked and therefore not SMP-safe
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Christian Borntraeger6b979de2005-06-25 14:55:32 -070075int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Heiko Carstensa004fb02007-10-12 16:11:46 +020077 int cmdlen;
78 int rc;
79 int response_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 cmdlen = strlen(cmd);
82 BUG_ON(cmdlen > 240);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070083 memcpy(cpcmd_buf, cmd, cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 ASCEBC(cpcmd_buf, cmdlen);
85
Heiko Carstensa004fb02007-10-12 16:11:46 +020086 if (response) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 memset(response, 0, rlen);
Heiko Carstensa004fb02007-10-12 16:11:46 +020088 response_len = rlen;
89 rc = diag8_response(cmdlen, response, &rlen);
90 EBCASC(response, response_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 } else {
Heiko Carstensa004fb02007-10-12 16:11:46 +020092 rc = diag8_noresponse(cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Heiko Carstensa004fb02007-10-12 16:11:46 +020094 if (response_code)
95 *response_code = rc;
96 return rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(__cpcmd);
99
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700100int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 char *lowbuf;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700103 int len;
Heiko Carstens740b5702006-12-04 15:40:30 +0100104 unsigned long flags;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700105
Christian Borntraegerbda35632007-02-05 21:16:54 +0100106 if ((virt_to_phys(response) != (unsigned long) response) ||
107 (((unsigned long)response + rlen) >> 31)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
109 if (!lowbuf) {
Christian Borntraeger2f526e52008-12-25 13:39:34 +0100110 pr_warning("The cpcmd kernel function failed to "
111 "allocate a response buffer\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Heiko Carstens740b5702006-12-04 15:40:30 +0100114 spin_lock_irqsave(&cpcmd_lock, flags);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700115 len = __cpcmd(cmd, lowbuf, rlen, response_code);
Heiko Carstens740b5702006-12-04 15:40:30 +0100116 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 memcpy(response, lowbuf, rlen);
118 kfree(lowbuf);
Christian Borntraegerbda35632007-02-05 21:16:54 +0100119 } else {
120 spin_lock_irqsave(&cpcmd_lock, flags);
121 len = __cpcmd(cmd, response, rlen, response_code);
122 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700124 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126EXPORT_SYMBOL(cpcmd);