[S390] call home: fix string length handling
[deliverable/linux.git] / drivers / s390 / char / sclp_async.c
CommitLineData
c114728a
HJP
1/*
2 * Enable Asynchronous Notification via SCLP.
3 *
4 * Copyright IBM Corp. 2009
5 * Author(s): Hans-Joachim Picht <hans@linux.vnet.ibm.com>
6 *
7 */
8
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/stat.h>
13#include <linux/string.h>
14#include <linux/ctype.h>
15#include <linux/kmod.h>
16#include <linux/err.h>
17#include <linux/errno.h>
18#include <linux/proc_fs.h>
19#include <linux/sysctl.h>
20#include <linux/utsname.h>
21#include "sclp.h"
22
23static int callhome_enabled;
24static struct sclp_req *request;
25static struct sclp_async_sccb *sccb;
26static int sclp_async_send_wait(char *message);
27static struct ctl_table_header *callhome_sysctl_header;
28static DEFINE_SPINLOCK(sclp_async_lock);
c114728a
HJP
29#define SCLP_NORMAL_WRITE 0x00
30
31struct async_evbuf {
32 struct evbuf_header header;
33 u64 reserved;
34 u8 rflags;
35 u8 empty;
36 u8 rtype;
37 u8 otype;
38 char comp_id[12];
39 char data[3000]; /* there is still some space left */
40} __attribute__((packed));
41
42struct sclp_async_sccb {
43 struct sccb_header header;
44 struct async_evbuf evbuf;
45} __attribute__((packed));
46
47static struct sclp_register sclp_async_register = {
48 .send_mask = EVTYP_ASYNC_MASK,
49};
50
51static int call_home_on_panic(struct notifier_block *self,
52 unsigned long event, void *data)
53{
e8a79c9e
HC
54 strncat(data, init_utsname()->nodename,
55 sizeof(init_utsname()->nodename));
56 sclp_async_send_wait(data);
57 return NOTIFY_DONE;
c114728a
HJP
58}
59
60static struct notifier_block call_home_panic_nb = {
61 .notifier_call = call_home_on_panic,
62 .priority = INT_MAX,
63};
64
d3acf71f 65static int proc_handler_callhome(struct ctl_table *ctl, int write,
c114728a
HJP
66 void __user *buffer, size_t *count,
67 loff_t *ppos)
68{
69 unsigned long val;
70 int len, rc;
3f0b3c33 71 char buf[3];
c114728a 72
3f0b3c33 73 if (!*count || (*ppos && !write)) {
c114728a
HJP
74 *count = 0;
75 return 0;
76 }
77 if (!write) {
3f0b3c33 78 len = snprintf(buf, sizeof(buf), "%d\n", callhome_enabled);
c114728a
HJP
79 rc = copy_to_user(buffer, buf, sizeof(buf));
80 if (rc != 0)
81 return -EFAULT;
82 } else {
83 len = *count;
84 rc = copy_from_user(buf, buffer, sizeof(buf));
85 if (rc != 0)
86 return -EFAULT;
87 if (strict_strtoul(buf, 0, &val) != 0)
88 return -EINVAL;
89 if (val != 0 && val != 1)
90 return -EINVAL;
91 callhome_enabled = val;
92 }
93 *count = len;
94 *ppos += len;
95 return 0;
96}
97
98static struct ctl_table callhome_table[] = {
99 {
100 .procname = "callhome",
101 .mode = 0644,
d3acf71f 102 .proc_handler = proc_handler_callhome,
c114728a
HJP
103 },
104 { .ctl_name = 0 }
105};
106
107static struct ctl_table kern_dir_table[] = {
108 {
109 .ctl_name = CTL_KERN,
110 .procname = "kernel",
111 .maxlen = 0,
112 .mode = 0555,
113 .child = callhome_table,
114 },
115 { .ctl_name = 0 }
116};
117
118/*
119 * Function used to transfer asynchronous notification
120 * records which waits for send completion
121 */
122static int sclp_async_send_wait(char *message)
123{
124 struct async_evbuf *evb;
125 int rc;
126 unsigned long flags;
127
128 if (!callhome_enabled)
129 return 0;
130 sccb->evbuf.header.type = EVTYP_ASYNC;
131 sccb->evbuf.rtype = 0xA5;
132 sccb->evbuf.otype = 0x00;
133 evb = &sccb->evbuf;
134 request->command = SCLP_CMDW_WRITE_EVENT_DATA;
135 request->sccb = sccb;
136 request->status = SCLP_REQ_FILLED;
137 strncpy(sccb->evbuf.data, message, sizeof(sccb->evbuf.data));
138 /*
139 * Retain Queue
140 * e.g. 5639CC140 500 Red Hat RHEL5 Linux for zSeries (RHEL AS)
141 */
142 strncpy(sccb->evbuf.comp_id, "000000000", sizeof(sccb->evbuf.comp_id));
143 sccb->evbuf.header.length = sizeof(sccb->evbuf);
144 sccb->header.length = sizeof(sccb->evbuf) + sizeof(sccb->header);
145 sccb->header.function_code = SCLP_NORMAL_WRITE;
146 rc = sclp_add_request(request);
147 if (rc)
148 return rc;
149 spin_lock_irqsave(&sclp_async_lock, flags);
150 while (request->status != SCLP_REQ_DONE &&
151 request->status != SCLP_REQ_FAILED) {
152 sclp_sync_wait();
153 }
154 spin_unlock_irqrestore(&sclp_async_lock, flags);
155 if (request->status != SCLP_REQ_DONE)
156 return -EIO;
157 rc = ((struct sclp_async_sccb *)
158 request->sccb)->header.response_code;
159 if (rc != 0x0020)
160 return -EIO;
161 if (evb->header.flags != 0x80)
162 return -EIO;
163 return rc;
164}
165
166static int __init sclp_async_init(void)
167{
168 int rc;
169
170 rc = sclp_register(&sclp_async_register);
171 if (rc)
172 return rc;
4a0fb4c4
HC
173 rc = -EOPNOTSUPP;
174 if (!(sclp_async_register.sclp_receive_mask & EVTYP_ASYNC_MASK))
c114728a 175 goto out_sclp;
c114728a 176 rc = -ENOMEM;
4a0fb4c4
HC
177 callhome_sysctl_header = register_sysctl_table(kern_dir_table);
178 if (!callhome_sysctl_header)
179 goto out_sclp;
c114728a 180 request = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
c114728a 181 sccb = (struct sclp_async_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
4a0fb4c4 182 if (!request || !sccb)
c114728a 183 goto out_mem;
4a0fb4c4
HC
184 rc = atomic_notifier_chain_register(&panic_notifier_list,
185 &call_home_panic_nb);
e8a79c9e
HC
186 if (!rc)
187 goto out;
c114728a
HJP
188out_mem:
189 kfree(request);
190 free_page((unsigned long) sccb);
c114728a
HJP
191 unregister_sysctl_table(callhome_sysctl_header);
192out_sclp:
193 sclp_unregister(&sclp_async_register);
4a0fb4c4 194out:
c114728a 195 return rc;
c114728a
HJP
196}
197module_init(sclp_async_init);
198
199static void __exit sclp_async_exit(void)
200{
201 atomic_notifier_chain_unregister(&panic_notifier_list,
202 &call_home_panic_nb);
203 unregister_sysctl_table(callhome_sysctl_header);
204 sclp_unregister(&sclp_async_register);
205 free_page((unsigned long) sccb);
206 kfree(request);
207}
208module_exit(sclp_async_exit);
209
210MODULE_AUTHOR("Copyright IBM Corp. 2009");
211MODULE_AUTHOR("Hans-Joachim Picht <hans@linux.vnet.ibm.com>");
212MODULE_LICENSE("GPL");
213MODULE_DESCRIPTION("SCLP Asynchronous Notification Records");
This page took 0.051159 seconds and 5 git commands to generate.