blob: 19c25427f27fdd702864153fe64f71abb2a175b0 [file] [log] [blame]
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020015#include <linux/ctype.h>
16#include <linux/kmod.h>
17#include <linux/err.h>
18#include <linux/errno.h>
19#include <linux/proc_fs.h>
20#include <linux/sysctl.h>
21#include <linux/utsname.h>
22#include "sclp.h"
23
24static int callhome_enabled;
25static struct sclp_req *request;
26static struct sclp_async_sccb *sccb;
27static int sclp_async_send_wait(char *message);
28static struct ctl_table_header *callhome_sysctl_header;
29static DEFINE_SPINLOCK(sclp_async_lock);
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020030#define SCLP_NORMAL_WRITE 0x00
31
32struct async_evbuf {
33 struct evbuf_header header;
34 u64 reserved;
35 u8 rflags;
36 u8 empty;
37 u8 rtype;
38 u8 otype;
39 char comp_id[12];
40 char data[3000]; /* there is still some space left */
41} __attribute__((packed));
42
43struct sclp_async_sccb {
44 struct sccb_header header;
45 struct async_evbuf evbuf;
46} __attribute__((packed));
47
48static struct sclp_register sclp_async_register = {
49 .send_mask = EVTYP_ASYNC_MASK,
50};
51
52static int call_home_on_panic(struct notifier_block *self,
53 unsigned long event, void *data)
54{
Heiko Carstense8a79c92009-10-29 15:04:11 +010055 strncat(data, init_utsname()->nodename,
56 sizeof(init_utsname()->nodename));
57 sclp_async_send_wait(data);
58 return NOTIFY_DONE;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020059}
60
61static struct notifier_block call_home_panic_nb = {
62 .notifier_call = call_home_on_panic,
63 .priority = INT_MAX,
64};
65
Heiko Carstensd3acf712009-10-14 12:43:49 +020066static int proc_handler_callhome(struct ctl_table *ctl, int write,
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020067 void __user *buffer, size_t *count,
68 loff_t *ppos)
69{
70 unsigned long val;
71 int len, rc;
Sebastian Ott3f0b3c32009-10-29 15:04:07 +010072 char buf[3];
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020073
Sebastian Ott3f0b3c32009-10-29 15:04:07 +010074 if (!*count || (*ppos && !write)) {
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020075 *count = 0;
76 return 0;
77 }
78 if (!write) {
Sebastian Ott3f0b3c32009-10-29 15:04:07 +010079 len = snprintf(buf, sizeof(buf), "%d\n", callhome_enabled);
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020080 rc = copy_to_user(buffer, buf, sizeof(buf));
81 if (rc != 0)
82 return -EFAULT;
83 } else {
84 len = *count;
Peter Huewed5f4d112011-08-03 16:44:29 +020085 rc = kstrtoul_from_user(buffer, len, 0, &val);
86 if (rc)
87 return rc;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +020088 if (val != 0 && val != 1)
89 return -EINVAL;
90 callhome_enabled = val;
91 }
92 *count = len;
93 *ppos += len;
94 return 0;
95}
96
97static struct ctl_table callhome_table[] = {
98 {
99 .procname = "callhome",
100 .mode = 0644,
Heiko Carstensd3acf712009-10-14 12:43:49 +0200101 .proc_handler = proc_handler_callhome,
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200102 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700103 {}
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200104};
105
106static struct ctl_table kern_dir_table[] = {
107 {
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200108 .procname = "kernel",
109 .maxlen = 0,
110 .mode = 0555,
111 .child = callhome_table,
112 },
Eric W. Biedermanb05fd352009-04-03 04:36:34 -0700113 {}
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200114};
115
116/*
117 * Function used to transfer asynchronous notification
118 * records which waits for send completion
119 */
120static int sclp_async_send_wait(char *message)
121{
122 struct async_evbuf *evb;
123 int rc;
124 unsigned long flags;
125
126 if (!callhome_enabled)
127 return 0;
128 sccb->evbuf.header.type = EVTYP_ASYNC;
129 sccb->evbuf.rtype = 0xA5;
130 sccb->evbuf.otype = 0x00;
131 evb = &sccb->evbuf;
132 request->command = SCLP_CMDW_WRITE_EVENT_DATA;
133 request->sccb = sccb;
134 request->status = SCLP_REQ_FILLED;
135 strncpy(sccb->evbuf.data, message, sizeof(sccb->evbuf.data));
136 /*
137 * Retain Queue
138 * e.g. 5639CC140 500 Red Hat RHEL5 Linux for zSeries (RHEL AS)
139 */
Martin Schwidefskyb3e06dc2014-11-24 13:05:05 +0100140 strncpy(sccb->evbuf.comp_id, CONFIG_SCLP_ASYNC_ID,
141 sizeof(sccb->evbuf.comp_id));
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200142 sccb->evbuf.header.length = sizeof(sccb->evbuf);
143 sccb->header.length = sizeof(sccb->evbuf) + sizeof(sccb->header);
144 sccb->header.function_code = SCLP_NORMAL_WRITE;
145 rc = sclp_add_request(request);
146 if (rc)
147 return rc;
148 spin_lock_irqsave(&sclp_async_lock, flags);
149 while (request->status != SCLP_REQ_DONE &&
150 request->status != SCLP_REQ_FAILED) {
151 sclp_sync_wait();
152 }
153 spin_unlock_irqrestore(&sclp_async_lock, flags);
154 if (request->status != SCLP_REQ_DONE)
155 return -EIO;
156 rc = ((struct sclp_async_sccb *)
157 request->sccb)->header.response_code;
158 if (rc != 0x0020)
159 return -EIO;
160 if (evb->header.flags != 0x80)
161 return -EIO;
162 return rc;
163}
164
165static int __init sclp_async_init(void)
166{
167 int rc;
168
169 rc = sclp_register(&sclp_async_register);
170 if (rc)
171 return rc;
Heiko Carstens4a0fb4c2009-10-29 15:04:10 +0100172 rc = -EOPNOTSUPP;
173 if (!(sclp_async_register.sclp_receive_mask & EVTYP_ASYNC_MASK))
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200174 goto out_sclp;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200175 rc = -ENOMEM;
Heiko Carstens4a0fb4c2009-10-29 15:04:10 +0100176 callhome_sysctl_header = register_sysctl_table(kern_dir_table);
177 if (!callhome_sysctl_header)
178 goto out_sclp;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200179 request = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200180 sccb = (struct sclp_async_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Heiko Carstens4a0fb4c2009-10-29 15:04:10 +0100181 if (!request || !sccb)
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200182 goto out_mem;
Heiko Carstens4a0fb4c2009-10-29 15:04:10 +0100183 rc = atomic_notifier_chain_register(&panic_notifier_list,
184 &call_home_panic_nb);
Heiko Carstense8a79c92009-10-29 15:04:11 +0100185 if (!rc)
186 goto out;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200187out_mem:
188 kfree(request);
189 free_page((unsigned long) sccb);
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200190 unregister_sysctl_table(callhome_sysctl_header);
191out_sclp:
192 sclp_unregister(&sclp_async_register);
Heiko Carstens4a0fb4c2009-10-29 15:04:10 +0100193out:
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200194 return rc;
Hans-Joachim Pichtc114728a2009-09-11 10:28:47 +0200195}
196module_init(sclp_async_init);
197
198static void __exit sclp_async_exit(void)
199{
200 atomic_notifier_chain_unregister(&panic_notifier_list,
201 &call_home_panic_nb);
202 unregister_sysctl_table(callhome_sysctl_header);
203 sclp_unregister(&sclp_async_register);
204 free_page((unsigned long) sccb);
205 kfree(request);
206}
207module_exit(sclp_async_exit);
208
209MODULE_AUTHOR("Copyright IBM Corp. 2009");
210MODULE_AUTHOR("Hans-Joachim Picht <hans@linux.vnet.ibm.com>");
211MODULE_LICENSE("GPL");
212MODULE_DESCRIPTION("SCLP Asynchronous Notification Records");