cpcmd.c 2.9 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
 *    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>
19
#include <asm/io.h>
L
Linus Torvalds 已提交
20 21

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

/*
25 26 27
 * __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 已提交
28
 */
29
int  __cpcmd(const char *cmd, char *response, int rlen, int *response_code)
L
Linus Torvalds 已提交
30
{
31
	unsigned cmdlen;
32
	int return_code, return_len;
L
Linus Torvalds 已提交
33 34 35

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

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

EXPORT_SYMBOL(__cpcmd);

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

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

EXPORT_SYMBOL(cpcmd);