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>
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>
25 #include "sclp_cpi_sys.h"
27 #define CPI_LENGTH_NAME 8
28 #define CPI_LENGTH_LEVEL 16
30 static DEFINE_MUTEX(sclp_cpi_mutex);
33 struct evbuf_header header;
36 u8 system_type[CPI_LENGTH_NAME];
38 u8 system_name[CPI_LENGTH_NAME];
42 u8 sysplex_name[CPI_LENGTH_NAME];
44 } __attribute__((packed));
47 struct sccb_header header;
48 struct cpi_evbuf cpi_evbuf;
49 } __attribute__((packed));
51 static struct sclp_register sclp_cpi_event = {
52 .send_mask = EVTYP_CTLPROGIDENT_MASK,
55 static char system_name[CPI_LENGTH_NAME + 1];
56 static char sysplex_name[CPI_LENGTH_NAME + 1];
57 static char system_type[CPI_LENGTH_NAME + 1];
58 static u64 system_level;
60 static void set_data(char *field, char *data)
62 memset(field, ' ', CPI_LENGTH_NAME);
63 memcpy(field, data, strlen(data));
64 sclp_ascebc_str(field, CPI_LENGTH_NAME);
67 static void cpi_callback(struct sclp_req *req, void *data)
69 struct completion *completion = data;
74 static struct sclp_req *cpi_prepare_req(void)
77 struct cpi_sccb *sccb;
78 struct cpi_evbuf *evb;
80 req = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
82 return ERR_PTR(-ENOMEM);
83 sccb = (struct cpi_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
86 return ERR_PTR(-ENOMEM);
89 /* setup SCCB for Control-Program Identification */
90 sccb->header.length = sizeof(struct cpi_sccb);
91 sccb->cpi_evbuf.header.length = sizeof(struct cpi_evbuf);
92 sccb->cpi_evbuf.header.type = 0x0b;
93 evb = &sccb->cpi_evbuf;
96 set_data(evb->system_type, system_type);
99 set_data(evb->system_name, system_name);
101 /* set sytem level */
102 evb->system_level = system_level;
104 /* set sysplex name */
105 set_data(evb->sysplex_name, sysplex_name);
107 /* prepare request data structure presented to SCLP driver */
108 req->command = SCLP_CMDW_WRITE_EVENT_DATA;
110 req->status = SCLP_REQ_FILLED;
111 req->callback = cpi_callback;
115 static void cpi_free_req(struct sclp_req *req)
117 free_page((unsigned long) req->sccb);
121 static int cpi_req(void)
123 struct completion completion;
124 struct sclp_req *req;
128 rc = sclp_register(&sclp_cpi_event);
131 if (!(sclp_cpi_event.sclp_receive_mask & EVTYP_CTLPROGIDENT_MASK)) {
136 req = cpi_prepare_req();
142 init_completion(&completion);
143 req->callback_data = &completion;
145 /* Add request to sclp queue */
146 rc = sclp_add_request(req);
150 wait_for_completion(&completion);
152 if (req->status != SCLP_REQ_DONE) {
153 printk(KERN_WARNING "cpi: request failed (status=0x%02x)\n",
159 response = ((struct cpi_sccb *) req->sccb)->header.response_code;
160 if (response != 0x0020) {
161 printk(KERN_WARNING "cpi: failed with "
162 "response code 0x%x\n", response);
170 sclp_unregister(&sclp_cpi_event);
176 static int check_string(const char *attr, const char *str)
183 if ((len > 0) && (str[len - 1] == '\n'))
186 if (len > CPI_LENGTH_NAME)
189 for (i = 0; i < len ; i++) {
190 if (isalpha(str[i]) || isdigit(str[i]) ||
191 strchr("$@# ", str[i]))
199 static void set_string(char *attr, const char *value)
206 if ((len > 0) && (value[len - 1] == '\n'))
209 for (i = 0; i < CPI_LENGTH_NAME; i++) {
211 attr[i] = toupper(value[i]);
217 static ssize_t system_name_show(struct kobject *kobj,
218 struct kobj_attribute *attr, char *page)
222 mutex_lock(&sclp_cpi_mutex);
223 rc = snprintf(page, PAGE_SIZE, "%s\n", system_name);
224 mutex_unlock(&sclp_cpi_mutex);
228 static ssize_t system_name_store(struct kobject *kobj,
229 struct kobj_attribute *attr,
235 rc = check_string("system_name", buf);
239 mutex_lock(&sclp_cpi_mutex);
240 set_string(system_name, buf);
241 mutex_unlock(&sclp_cpi_mutex);
246 static struct kobj_attribute system_name_attr =
247 __ATTR(system_name, 0644, system_name_show, system_name_store);
249 static ssize_t sysplex_name_show(struct kobject *kobj,
250 struct kobj_attribute *attr, char *page)
254 mutex_lock(&sclp_cpi_mutex);
255 rc = snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
256 mutex_unlock(&sclp_cpi_mutex);
260 static ssize_t sysplex_name_store(struct kobject *kobj,
261 struct kobj_attribute *attr,
267 rc = check_string("sysplex_name", buf);
271 mutex_lock(&sclp_cpi_mutex);
272 set_string(sysplex_name, buf);
273 mutex_unlock(&sclp_cpi_mutex);
278 static struct kobj_attribute sysplex_name_attr =
279 __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
281 static ssize_t system_type_show(struct kobject *kobj,
282 struct kobj_attribute *attr, char *page)
286 mutex_lock(&sclp_cpi_mutex);
287 rc = snprintf(page, PAGE_SIZE, "%s\n", system_type);
288 mutex_unlock(&sclp_cpi_mutex);
292 static ssize_t system_type_store(struct kobject *kobj,
293 struct kobj_attribute *attr,
299 rc = check_string("system_type", buf);
303 mutex_lock(&sclp_cpi_mutex);
304 set_string(system_type, buf);
305 mutex_unlock(&sclp_cpi_mutex);
310 static struct kobj_attribute system_type_attr =
311 __ATTR(system_type, 0644, system_type_show, system_type_store);
313 static ssize_t system_level_show(struct kobject *kobj,
314 struct kobj_attribute *attr, char *page)
316 unsigned long long level;
318 mutex_lock(&sclp_cpi_mutex);
319 level = system_level;
320 mutex_unlock(&sclp_cpi_mutex);
321 return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
324 static ssize_t system_level_store(struct kobject *kobj,
325 struct kobj_attribute *attr,
329 unsigned long long level;
332 level = simple_strtoull(buf, &endp, 16);
341 mutex_lock(&sclp_cpi_mutex);
342 system_level = level;
343 mutex_unlock(&sclp_cpi_mutex);
347 static struct kobj_attribute system_level_attr =
348 __ATTR(system_level, 0644, system_level_show, system_level_store);
350 static ssize_t set_store(struct kobject *kobj,
351 struct kobj_attribute *attr,
352 const char *buf, size_t len)
356 mutex_lock(&sclp_cpi_mutex);
358 mutex_unlock(&sclp_cpi_mutex);
365 static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
367 static struct attribute *cpi_attrs[] = {
368 &system_name_attr.attr,
369 &sysplex_name_attr.attr,
370 &system_type_attr.attr,
371 &system_level_attr.attr,
376 static struct attribute_group cpi_attr_group = {
380 static struct kset *cpi_kset;
382 int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
387 rc = check_string("system_name", system);
390 rc = check_string("sysplex_name", sysplex);
393 rc = check_string("system_type", type);
397 mutex_lock(&sclp_cpi_mutex);
398 set_string(system_name, system);
399 set_string(sysplex_name, sysplex);
400 set_string(system_type, type);
401 system_level = level;
404 mutex_unlock(&sclp_cpi_mutex);
408 EXPORT_SYMBOL(sclp_cpi_set_data);
410 static int __init cpi_init(void)
414 cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
418 rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
420 kset_unregister(cpi_kset);
425 __initcall(cpi_init);