blob: e3dd886e1b3218e6b59ec29af4426879b6279570 [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>
Christian Borntraegerbda35632007-02-05 21:16:54 +010021#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static DEFINE_SPINLOCK(cpcmd_lock);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070024static char cpcmd_buf[241];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Heiko Carstensa004fb02007-10-12 16:11:46 +020026static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Heiko Carstens740b5702006-12-04 15:40:30 +010070 * __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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Christian Borntraeger6b979de2005-06-25 14:55:32 -070074int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Heiko Carstensa004fb02007-10-12 16:11:46 +020076 int cmdlen;
77 int rc;
78 int response_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 cmdlen = strlen(cmd);
81 BUG_ON(cmdlen > 240);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070082 memcpy(cpcmd_buf, cmd, cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ASCEBC(cpcmd_buf, cmdlen);
84
Heiko Carstensa004fb02007-10-12 16:11:46 +020085 if (response) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 memset(response, 0, rlen);
Heiko Carstensa004fb02007-10-12 16:11:46 +020087 response_len = rlen;
88 rc = diag8_response(cmdlen, response, &rlen);
89 EBCASC(response, response_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 } else {
Heiko Carstensa004fb02007-10-12 16:11:46 +020091 rc = diag8_noresponse(cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Heiko Carstensa004fb02007-10-12 16:11:46 +020093 if (response_code)
94 *response_code = rc;
95 return rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097EXPORT_SYMBOL(__cpcmd);
98
Christian Borntraeger6b979de2005-06-25 14:55:32 -070099int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 char *lowbuf;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700102 int len;
Heiko Carstens740b5702006-12-04 15:40:30 +0100103 unsigned long flags;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700104
Christian Borntraegerbda35632007-02-05 21:16:54 +0100105 if ((virt_to_phys(response) != (unsigned long) response) ||
106 (((unsigned long)response + rlen) >> 31)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
108 if (!lowbuf) {
Christian Borntraeger2f526e52008-12-25 13:39:34 +0100109 pr_warning("The cpcmd kernel function failed to "
110 "allocate a response buffer\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700111 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Heiko Carstens740b5702006-12-04 15:40:30 +0100113 spin_lock_irqsave(&cpcmd_lock, flags);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700114 len = __cpcmd(cmd, lowbuf, rlen, response_code);
Heiko Carstens740b5702006-12-04 15:40:30 +0100115 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 memcpy(response, lowbuf, rlen);
117 kfree(lowbuf);
Christian Borntraegerbda35632007-02-05 21:16:54 +0100118 } else {
119 spin_lock_irqsave(&cpcmd_lock, flags);
120 len = __cpcmd(cmd, response, rlen, response_code);
121 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700123 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125EXPORT_SYMBOL(cpcmd);