kbuild: Fix instrumentation removal breakage on avr32
[wrt350n-kernel.git] / drivers / s390 / char / sclp_cpi_sys.c
blob41617032afdced7e737a7f37060370ebb684bfe0
1 /*
2 * drivers/s390/char/sclp_cpi_sys.c
3 * SCLP control program identification sysfs interface
5 * Copyright IBM Corp. 2001, 2007
6 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
7 * Michael Ernst <mernst@de.ibm.com>
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/stat.h>
13 #include <linux/device.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16 #include <linux/kmod.h>
17 #include <linux/timer.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/completion.h>
21 #include <asm/ebcdic.h>
22 #include <asm/sclp.h>
23 #include "sclp.h"
24 #include "sclp_rw.h"
25 #include "sclp_cpi_sys.h"
27 #define CPI_LENGTH_NAME 8
28 #define CPI_LENGTH_LEVEL 16
30 struct cpi_evbuf {
31 struct evbuf_header header;
32 u8 id_format;
33 u8 reserved0;
34 u8 system_type[CPI_LENGTH_NAME];
35 u64 reserved1;
36 u8 system_name[CPI_LENGTH_NAME];
37 u64 reserved2;
38 u64 system_level;
39 u64 reserved3;
40 u8 sysplex_name[CPI_LENGTH_NAME];
41 u8 reserved4[16];
42 } __attribute__((packed));
44 struct cpi_sccb {
45 struct sccb_header header;
46 struct cpi_evbuf cpi_evbuf;
47 } __attribute__((packed));
49 static struct sclp_register sclp_cpi_event = {
50 .send_mask = EVTYP_CTLPROGIDENT_MASK,
53 static char system_name[CPI_LENGTH_NAME + 1];
54 static char sysplex_name[CPI_LENGTH_NAME + 1];
55 static char system_type[CPI_LENGTH_NAME + 1];
56 static u64 system_level;
58 static void set_data(char *field, char *data)
60 memset(field, ' ', CPI_LENGTH_NAME);
61 memcpy(field, data, strlen(data));
62 sclp_ascebc_str(field, CPI_LENGTH_NAME);
65 static void cpi_callback(struct sclp_req *req, void *data)
67 struct completion *completion = data;
69 complete(completion);
72 static struct sclp_req *cpi_prepare_req(void)
74 struct sclp_req *req;
75 struct cpi_sccb *sccb;
76 struct cpi_evbuf *evb;
78 req = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
79 if (!req)
80 return ERR_PTR(-ENOMEM);
81 sccb = (struct cpi_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
82 if (!sccb) {
83 kfree(req);
84 return ERR_PTR(-ENOMEM);
87 /* setup SCCB for Control-Program Identification */
88 sccb->header.length = sizeof(struct cpi_sccb);
89 sccb->cpi_evbuf.header.length = sizeof(struct cpi_evbuf);
90 sccb->cpi_evbuf.header.type = 0x0b;
91 evb = &sccb->cpi_evbuf;
93 /* set system type */
94 set_data(evb->system_type, system_type);
96 /* set system name */
97 set_data(evb->system_name, system_name);
99 /* set sytem level */
100 evb->system_level = system_level;
102 /* set sysplex name */
103 set_data(evb->sysplex_name, sysplex_name);
105 /* prepare request data structure presented to SCLP driver */
106 req->command = SCLP_CMDW_WRITE_EVENT_DATA;
107 req->sccb = sccb;
108 req->status = SCLP_REQ_FILLED;
109 req->callback = cpi_callback;
110 return req;
113 static void cpi_free_req(struct sclp_req *req)
115 free_page((unsigned long) req->sccb);
116 kfree(req);
119 static int cpi_req(void)
121 struct completion completion;
122 struct sclp_req *req;
123 int rc;
124 int response;
126 rc = sclp_register(&sclp_cpi_event);
127 if (rc) {
128 printk(KERN_WARNING "cpi: could not register "
129 "to hardware console.\n");
130 goto out;
132 if (!(sclp_cpi_event.sclp_send_mask & EVTYP_CTLPROGIDENT_MASK)) {
133 printk(KERN_WARNING "cpi: no control program "
134 "identification support\n");
135 rc = -EOPNOTSUPP;
136 goto out_unregister;
139 req = cpi_prepare_req();
140 if (IS_ERR(req)) {
141 printk(KERN_WARNING "cpi: could not allocate request\n");
142 rc = PTR_ERR(req);
143 goto out_unregister;
146 init_completion(&completion);
147 req->callback_data = &completion;
149 /* Add request to sclp queue */
150 rc = sclp_add_request(req);
151 if (rc) {
152 printk(KERN_WARNING "cpi: could not start request\n");
153 goto out_free_req;
156 wait_for_completion(&completion);
158 if (req->status != SCLP_REQ_DONE) {
159 printk(KERN_WARNING "cpi: request failed (status=0x%02x)\n",
160 req->status);
161 rc = -EIO;
162 goto out_free_req;
165 response = ((struct cpi_sccb *) req->sccb)->header.response_code;
166 if (response != 0x0020) {
167 printk(KERN_WARNING "cpi: failed with "
168 "response code 0x%x\n", response);
169 rc = -EIO;
172 out_free_req:
173 cpi_free_req(req);
175 out_unregister:
176 sclp_unregister(&sclp_cpi_event);
178 out:
179 return rc;
182 static int check_string(const char *attr, const char *str)
184 size_t len;
185 size_t i;
187 len = strlen(str);
189 if ((len > 0) && (str[len - 1] == '\n'))
190 len--;
192 if (len > CPI_LENGTH_NAME)
193 return -EINVAL;
195 for (i = 0; i < len ; i++) {
196 if (isalpha(str[i]) || isdigit(str[i]) ||
197 strchr("$@# ", str[i]))
198 continue;
199 return -EINVAL;
202 return 0;
205 static void set_string(char *attr, const char *value)
207 size_t len;
208 size_t i;
210 len = strlen(value);
212 if ((len > 0) && (value[len - 1] == '\n'))
213 len--;
215 for (i = 0; i < CPI_LENGTH_NAME; i++) {
216 if (i < len)
217 attr[i] = toupper(value[i]);
218 else
219 attr[i] = ' ';
223 static ssize_t system_name_show(struct kobject *kobj,
224 struct kobj_attribute *attr, char *page)
226 return snprintf(page, PAGE_SIZE, "%s\n", system_name);
229 static ssize_t system_name_store(struct kobject *kobj,
230 struct kobj_attribute *attr,
231 const char *buf,
232 size_t len)
234 int rc;
236 rc = check_string("system_name", buf);
237 if (rc)
238 return rc;
240 set_string(system_name, buf);
242 return len;
245 static struct kobj_attribute system_name_attr =
246 __ATTR(system_name, 0644, system_name_show, system_name_store);
248 static ssize_t sysplex_name_show(struct kobject *kobj,
249 struct kobj_attribute *attr, char *page)
251 return snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
254 static ssize_t sysplex_name_store(struct kobject *kobj,
255 struct kobj_attribute *attr,
256 const char *buf,
257 size_t len)
259 int rc;
261 rc = check_string("sysplex_name", buf);
262 if (rc)
263 return rc;
265 set_string(sysplex_name, buf);
267 return len;
270 static struct kobj_attribute sysplex_name_attr =
271 __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
273 static ssize_t system_type_show(struct kobject *kobj,
274 struct kobj_attribute *attr, char *page)
276 return snprintf(page, PAGE_SIZE, "%s\n", system_type);
279 static ssize_t system_type_store(struct kobject *kobj,
280 struct kobj_attribute *attr,
281 const char *buf,
282 size_t len)
284 int rc;
286 rc = check_string("system_type", buf);
287 if (rc)
288 return rc;
290 set_string(system_type, buf);
292 return len;
295 static struct kobj_attribute system_type_attr =
296 __ATTR(system_type, 0644, system_type_show, system_type_store);
298 static ssize_t system_level_show(struct kobject *kobj,
299 struct kobj_attribute *attr, char *page)
301 unsigned long long level = system_level;
303 return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
306 static ssize_t system_level_store(struct kobject *kobj,
307 struct kobj_attribute *attr,
308 const char *buf,
309 size_t len)
311 unsigned long long level;
312 char *endp;
314 level = simple_strtoull(buf, &endp, 16);
316 if (endp == buf)
317 return -EINVAL;
318 if (*endp == '\n')
319 endp++;
320 if (*endp)
321 return -EINVAL;
323 system_level = level;
325 return len;
328 static struct kobj_attribute system_level_attr =
329 __ATTR(system_level, 0644, system_level_show, system_level_store);
331 static ssize_t set_store(struct kobject *kobj,
332 struct kobj_attribute *attr,
333 const char *buf, size_t len)
335 int rc;
337 rc = cpi_req();
338 if (rc)
339 return rc;
341 return len;
344 static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
346 static struct attribute *cpi_attrs[] = {
347 &system_name_attr.attr,
348 &sysplex_name_attr.attr,
349 &system_type_attr.attr,
350 &system_level_attr.attr,
351 &set_attr.attr,
352 NULL,
355 static struct attribute_group cpi_attr_group = {
356 .attrs = cpi_attrs,
359 static struct kset *cpi_kset;
361 int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
362 const u64 level)
364 int rc;
366 rc = check_string("system_name", system);
367 if (rc)
368 return rc;
369 rc = check_string("sysplex_name", sysplex);
370 if (rc)
371 return rc;
372 rc = check_string("system_type", type);
373 if (rc)
374 return rc;
376 set_string(system_name, system);
377 set_string(sysplex_name, sysplex);
378 set_string(system_type, type);
379 system_level = level;
381 return cpi_req();
383 EXPORT_SYMBOL(sclp_cpi_set_data);
385 static int __init cpi_init(void)
387 int rc;
389 cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
390 if (!cpi_kset)
391 return -ENOMEM;
393 rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
394 if (rc)
395 kset_unregister(cpi_kset);
397 return rc;
400 __initcall(cpi_init);