blob: d8c1131e081568927e7f4460aa4febd912c869bf [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
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/stddef.h>
15#include <linux/string.h>
16#include <asm/ebcdic.h>
17#include <asm/cpcmd.h>
18#include <asm/system.h>
Christian Borntraegerbda35632007-02-05 21:16:54 +010019#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static DEFINE_SPINLOCK(cpcmd_lock);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070022static char cpcmd_buf[241];
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Heiko Carstensa004fb02007-10-12 16:11:46 +020024static int diag8_noresponse(int cmdlen)
25{
26 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
27 register unsigned long reg3 asm ("3") = cmdlen;
28
29 asm volatile(
30#ifndef CONFIG_64BIT
31 " diag %1,%0,0x8\n"
32#else /* CONFIG_64BIT */
33 " sam31\n"
34 " diag %1,%0,0x8\n"
35 " sam64\n"
36#endif /* CONFIG_64BIT */
37 : "+d" (reg3) : "d" (reg2) : "cc");
38 return reg3;
39}
40
41static int diag8_response(int cmdlen, char *response, int *rlen)
42{
43 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
44 register unsigned long reg3 asm ("3") = (addr_t) response;
45 register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
46 register unsigned long reg5 asm ("5") = *rlen;
47
48 asm volatile(
49#ifndef CONFIG_64BIT
50 " diag %2,%0,0x8\n"
51 " brc 8,1f\n"
52 " ar %1,%4\n"
53#else /* CONFIG_64BIT */
54 " sam31\n"
55 " diag %2,%0,0x8\n"
56 " sam64\n"
57 " brc 8,1f\n"
58 " agr %1,%4\n"
59#endif /* CONFIG_64BIT */
60 "1:\n"
61 : "+d" (reg4), "+d" (reg5)
62 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc");
63 *rlen = reg5;
64 return reg4;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Heiko Carstens740b5702006-12-04 15:40:30 +010068 * __cpcmd has some restrictions over cpcmd
69 * - the response buffer must reside below 2GB (if any)
70 * - __cpcmd is unlocked and therefore not SMP-safe
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
Christian Borntraeger6b979de2005-06-25 14:55:32 -070072int __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Heiko Carstensa004fb02007-10-12 16:11:46 +020074 int cmdlen;
75 int rc;
76 int response_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 cmdlen = strlen(cmd);
79 BUG_ON(cmdlen > 240);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070080 memcpy(cpcmd_buf, cmd, cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 ASCEBC(cpcmd_buf, cmdlen);
82
Heiko Carstensa004fb02007-10-12 16:11:46 +020083 if (response) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 memset(response, 0, rlen);
Heiko Carstensa004fb02007-10-12 16:11:46 +020085 response_len = rlen;
86 rc = diag8_response(cmdlen, response, &rlen);
87 EBCASC(response, response_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } else {
Heiko Carstensa004fb02007-10-12 16:11:46 +020089 rc = diag8_noresponse(cmdlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Heiko Carstensa004fb02007-10-12 16:11:46 +020091 if (response_code)
92 *response_code = rc;
93 return rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(__cpcmd);
96
Christian Borntraeger6b979de2005-06-25 14:55:32 -070097int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 char *lowbuf;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700100 int len;
Heiko Carstens740b5702006-12-04 15:40:30 +0100101 unsigned long flags;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700102
Christian Borntraegerbda35632007-02-05 21:16:54 +0100103 if ((virt_to_phys(response) != (unsigned long) response) ||
104 (((unsigned long)response + rlen) >> 31)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
106 if (!lowbuf) {
107 printk(KERN_WARNING
108 "cpcmd: could not allocate response buffer\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Heiko Carstens740b5702006-12-04 15:40:30 +0100111 spin_lock_irqsave(&cpcmd_lock, flags);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700112 len = __cpcmd(cmd, lowbuf, rlen, response_code);
Heiko Carstens740b5702006-12-04 15:40:30 +0100113 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 memcpy(response, lowbuf, rlen);
115 kfree(lowbuf);
Christian Borntraegerbda35632007-02-05 21:16:54 +0100116 } else {
117 spin_lock_irqsave(&cpcmd_lock, flags);
118 len = __cpcmd(cmd, response, rlen, response_code);
119 spin_unlock_irqrestore(&cpcmd_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700121 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123EXPORT_SYMBOL(cpcmd);