1/* 2 * S390 version 3 * Copyright IBM Corp. 1999, 2007 4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com), 5 * Christian Borntraeger (cborntra@de.ibm.com), 6 */ 7 8#define KMSG_COMPONENT "cpcmd" 9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 10 11#include <linux/kernel.h> 12#include <linux/module.h> 13#include <linux/slab.h> 14#include <linux/spinlock.h> 15#include <linux/stddef.h> 16#include <linux/string.h> 17#include <asm/ebcdic.h> 18#include <asm/cpcmd.h> 19#include <asm/io.h> 20 21static DEFINE_SPINLOCK(cpcmd_lock); 22static char cpcmd_buf[241]; 23 24static 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 " sam31\n" 31 " diag %1,%0,0x8\n" 32 " sam64\n" 33 : "+d" (reg3) : "d" (reg2) : "cc"); 34 return reg3; 35} 36 37static int diag8_response(int cmdlen, char *response, int *rlen) 38{ 39 register unsigned long reg2 asm ("2") = (addr_t) cpcmd_buf; 40 register unsigned long reg3 asm ("3") = (addr_t) response; 41 register unsigned long reg4 asm ("4") = cmdlen | 0x40000000L; 42 register unsigned long reg5 asm ("5") = *rlen; 43 44 asm volatile( 45 " sam31\n" 46 " diag %2,%0,0x8\n" 47 " sam64\n" 48 " brc 8,1f\n" 49 " agr %1,%4\n" 50 "1:\n" 51 : "+d" (reg4), "+d" (reg5) 52 : "d" (reg2), "d" (reg3), "d" (*rlen) : "cc"); 53 *rlen = reg5; 54 return reg4; 55} 56 57/* 58 * __cpcmd has some restrictions over cpcmd 59 * - the response buffer must reside below 2GB (if any) 60 * - __cpcmd is unlocked and therefore not SMP-safe 61 */ 62int __cpcmd(const char *cmd, char *response, int rlen, int *response_code) 63{ 64 int cmdlen; 65 int rc; 66 int response_len; 67 68 cmdlen = strlen(cmd); 69 BUG_ON(cmdlen > 240); 70 memcpy(cpcmd_buf, cmd, cmdlen); 71 ASCEBC(cpcmd_buf, cmdlen); 72 73 if (response) { 74 memset(response, 0, rlen); 75 response_len = rlen; 76 rc = diag8_response(cmdlen, response, &rlen); 77 EBCASC(response, response_len); 78 } else { 79 rc = diag8_noresponse(cmdlen); 80 } 81 if (response_code) 82 *response_code = rc; 83 return rlen; 84} 85EXPORT_SYMBOL(__cpcmd); 86 87int cpcmd(const char *cmd, char *response, int rlen, int *response_code) 88{ 89 char *lowbuf; 90 int len; 91 unsigned long flags; 92 93 if ((virt_to_phys(response) != (unsigned long) response) || 94 (((unsigned long)response + rlen) >> 31)) { 95 lowbuf = kmalloc(rlen, GFP_KERNEL | GFP_DMA); 96 if (!lowbuf) { 97 pr_warning("The cpcmd kernel function failed to " 98 "allocate a response buffer\n"); 99 return -ENOMEM; 100 } 101 spin_lock_irqsave(&cpcmd_lock, flags); 102 len = __cpcmd(cmd, lowbuf, rlen, response_code); 103 spin_unlock_irqrestore(&cpcmd_lock, flags); 104 memcpy(response, lowbuf, rlen); 105 kfree(lowbuf); 106 } else { 107 spin_lock_irqsave(&cpcmd_lock, flags); 108 len = __cpcmd(cmd, response, rlen, response_code); 109 spin_unlock_irqrestore(&cpcmd_lock, flags); 110 } 111 return len; 112} 113EXPORT_SYMBOL(cpcmd); 114