cpcmd.c 2.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *  arch/s390/kernel/cpcmd.c
 *
 *  S390 version
5
 *    Copyright (C) 1999,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
 *               Christian Borntraeger (cborntra@de.ibm.com),
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <asm/ebcdic.h>
#include <asm/cpcmd.h>
#include <asm/system.h>

static DEFINE_SPINLOCK(cpcmd_lock);
21
static char cpcmd_buf[241];
L
Linus Torvalds 已提交
22 23

/*
24 25 26
 * __cpcmd has some restrictions over cpcmd
 *  - the response buffer must reside below 2GB (if any)
 *  - __cpcmd is unlocked and therefore not SMP-safe
L
Linus Torvalds 已提交
27
 */
28
int  __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
L
Linus Torvalds 已提交
29
{
30
	unsigned cmdlen;
31
	int return_code, return_len;
L
Linus Torvalds 已提交
32 33 34

	cmdlen = strlen(cmd);
	BUG_ON(cmdlen > 240);
35
	memcpy(cpcmd_buf, cmd, cmdlen);
L
Linus Torvalds 已提交
36 37 38
	ASCEBC(cpcmd_buf, cmdlen);

	if (response != NULL && rlen > 0) {
39 40 41 42 43
		register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
		register unsigned long reg3 asm ("3") = (addr_t) response;
		register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L;
		register unsigned long reg5 asm ("5") = rlen;

L
Linus Torvalds 已提交
44
		memset(response, 0, rlen);
45
		asm volatile(
46
#ifndef CONFIG_64BIT
47 48 49
			"	diag	%2,%0,0x8\n"
			"	brc	8,1f\n"
			"	ar	%1,%4\n"
50
#else /* CONFIG_64BIT */
51 52 53 54 55
			"	sam31\n"
			"	diag	%2,%0,0x8\n"
			"	sam64\n"
			"	brc	8,1f\n"
			"	agr	%1,%4\n"
56
#endif /* CONFIG_64BIT */
57 58 59 60 61
			"1:\n"
			: "+d" (reg4), "+d" (reg5)
			: "d" (reg2), "d" (reg3), "d" (rlen) : "cc");
		return_code = (int) reg4;
		return_len = (int) reg5;
L
Linus Torvalds 已提交
62 63
                EBCASC(response, rlen);
        } else {
64 65
		register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf;
		register unsigned long reg3 asm ("3") = cmdlen;
66
		return_len = 0;
67
		asm volatile(
68
#ifndef CONFIG_64BIT
69
			"	diag	%1,%0,0x8\n"
70
#else /* CONFIG_64BIT */
71 72 73
			"	sam31\n"
			"	diag	%1,%0,0x8\n"
			"	sam64\n"
74
#endif /* CONFIG_64BIT */
75 76
			: "+d" (reg3) : "d" (reg2) : "cc");
		return_code = (int) reg3;
L
Linus Torvalds 已提交
77
        }
78 79 80
	if (response_code != NULL)
		*response_code = return_code;
	return return_len;
L
Linus Torvalds 已提交
81 82 83 84
}

EXPORT_SYMBOL(__cpcmd);

85
int cpcmd(const char *cmd, char *response, int rlen, int *response_code)
L
Linus Torvalds 已提交
86 87
{
	char *lowbuf;
88
	int len;
89
	unsigned long flags;
90

L
Linus Torvalds 已提交
91
	if ((rlen == 0) || (response == NULL)
92 93
	    || !((unsigned long)response >> 31)) {
		spin_lock_irqsave(&cpcmd_lock, flags);
94
		len = __cpcmd(cmd, response, rlen, response_code);
95 96
		spin_unlock_irqrestore(&cpcmd_lock, flags);
	}
L
Linus Torvalds 已提交
97 98 99 100 101
	else {
		lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA);
		if (!lowbuf) {
			printk(KERN_WARNING
				"cpcmd: could not allocate response buffer\n");
102
			return -ENOMEM;
L
Linus Torvalds 已提交
103
		}
104
		spin_lock_irqsave(&cpcmd_lock, flags);
105
		len = __cpcmd(cmd, lowbuf, rlen, response_code);
106
		spin_unlock_irqrestore(&cpcmd_lock, flags);
L
Linus Torvalds 已提交
107 108 109
		memcpy(response, lowbuf, rlen);
		kfree(lowbuf);
	}
110
	return len;
L
Linus Torvalds 已提交
111 112 113
}

EXPORT_SYMBOL(cpcmd);