2 * SCLP control program identification sysfs interface
4 * Copyright IBM Corp. 2001, 2007
5 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
6 * Michael Ernst <mernst@de.ibm.com>
9 #define KMSG_COMPONENT "sclp_cpi"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/stat.h>
15 #include <linux/device.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
18 #include <linux/kmod.h>
19 #include <linux/timer.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/completion.h>
23 #include <linux/export.h>
24 #include <asm/ebcdic.h>
29 #include "sclp_cpi_sys.h"
31 #define CPI_LENGTH_NAME 8
32 #define CPI_LENGTH_LEVEL 16
34 static DEFINE_MUTEX(sclp_cpi_mutex
);
37 struct evbuf_header header
;
40 u8 system_type
[CPI_LENGTH_NAME
];
42 u8 system_name
[CPI_LENGTH_NAME
];
46 u8 sysplex_name
[CPI_LENGTH_NAME
];
48 } __attribute__((packed
));
51 struct sccb_header header
;
52 struct cpi_evbuf cpi_evbuf
;
53 } __attribute__((packed
));
55 static struct sclp_register sclp_cpi_event
= {
56 .send_mask
= EVTYP_CTLPROGIDENT_MASK
,
59 static char system_name
[CPI_LENGTH_NAME
+ 1];
60 static char sysplex_name
[CPI_LENGTH_NAME
+ 1];
61 static char system_type
[CPI_LENGTH_NAME
+ 1];
62 static u64 system_level
;
64 static void set_data(char *field
, char *data
)
66 memset(field
, ' ', CPI_LENGTH_NAME
);
67 memcpy(field
, data
, strlen(data
));
68 sclp_ascebc_str(field
, CPI_LENGTH_NAME
);
71 static void cpi_callback(struct sclp_req
*req
, void *data
)
73 struct completion
*completion
= data
;
78 static struct sclp_req
*cpi_prepare_req(void)
81 struct cpi_sccb
*sccb
;
82 struct cpi_evbuf
*evb
;
84 req
= kzalloc(sizeof(struct sclp_req
), GFP_KERNEL
);
86 return ERR_PTR(-ENOMEM
);
87 sccb
= (struct cpi_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
90 return ERR_PTR(-ENOMEM
);
93 /* setup SCCB for Control-Program Identification */
94 sccb
->header
.length
= sizeof(struct cpi_sccb
);
95 sccb
->cpi_evbuf
.header
.length
= sizeof(struct cpi_evbuf
);
96 sccb
->cpi_evbuf
.header
.type
= 0x0b;
97 evb
= &sccb
->cpi_evbuf
;
100 set_data(evb
->system_type
, system_type
);
102 /* set system name */
103 set_data(evb
->system_name
, system_name
);
105 /* set system level */
106 evb
->system_level
= system_level
;
108 /* set sysplex name */
109 set_data(evb
->sysplex_name
, sysplex_name
);
111 /* prepare request data structure presented to SCLP driver */
112 req
->command
= SCLP_CMDW_WRITE_EVENT_DATA
;
114 req
->status
= SCLP_REQ_FILLED
;
115 req
->callback
= cpi_callback
;
119 static void cpi_free_req(struct sclp_req
*req
)
121 free_page((unsigned long) req
->sccb
);
125 static int cpi_req(void)
127 struct completion completion
;
128 struct sclp_req
*req
;
132 rc
= sclp_register(&sclp_cpi_event
);
135 if (!(sclp_cpi_event
.sclp_receive_mask
& EVTYP_CTLPROGIDENT_MASK
)) {
140 req
= cpi_prepare_req();
146 init_completion(&completion
);
147 req
->callback_data
= &completion
;
149 /* Add request to sclp queue */
150 rc
= sclp_add_request(req
);
154 wait_for_completion(&completion
);
156 if (req
->status
!= SCLP_REQ_DONE
) {
157 pr_warning("request failed (status=0x%02x)\n",
163 response
= ((struct cpi_sccb
*) req
->sccb
)->header
.response_code
;
164 if (response
!= 0x0020) {
165 pr_warning("request failed with response code 0x%x\n",
174 sclp_unregister(&sclp_cpi_event
);
180 static int check_string(const char *attr
, const char *str
)
187 if ((len
> 0) && (str
[len
- 1] == '\n'))
190 if (len
> CPI_LENGTH_NAME
)
193 for (i
= 0; i
< len
; i
++) {
194 if (isalpha(str
[i
]) || isdigit(str
[i
]) ||
195 strchr("$@# ", str
[i
]))
203 static void set_string(char *attr
, const char *value
)
210 if ((len
> 0) && (value
[len
- 1] == '\n'))
213 for (i
= 0; i
< CPI_LENGTH_NAME
; i
++) {
215 attr
[i
] = toupper(value
[i
]);
221 static ssize_t
system_name_show(struct kobject
*kobj
,
222 struct kobj_attribute
*attr
, char *page
)
226 mutex_lock(&sclp_cpi_mutex
);
227 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", system_name
);
228 mutex_unlock(&sclp_cpi_mutex
);
232 static ssize_t
system_name_store(struct kobject
*kobj
,
233 struct kobj_attribute
*attr
,
239 rc
= check_string("system_name", buf
);
243 mutex_lock(&sclp_cpi_mutex
);
244 set_string(system_name
, buf
);
245 mutex_unlock(&sclp_cpi_mutex
);
250 static struct kobj_attribute system_name_attr
=
251 __ATTR(system_name
, 0644, system_name_show
, system_name_store
);
253 static ssize_t
sysplex_name_show(struct kobject
*kobj
,
254 struct kobj_attribute
*attr
, char *page
)
258 mutex_lock(&sclp_cpi_mutex
);
259 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", sysplex_name
);
260 mutex_unlock(&sclp_cpi_mutex
);
264 static ssize_t
sysplex_name_store(struct kobject
*kobj
,
265 struct kobj_attribute
*attr
,
271 rc
= check_string("sysplex_name", buf
);
275 mutex_lock(&sclp_cpi_mutex
);
276 set_string(sysplex_name
, buf
);
277 mutex_unlock(&sclp_cpi_mutex
);
282 static struct kobj_attribute sysplex_name_attr
=
283 __ATTR(sysplex_name
, 0644, sysplex_name_show
, sysplex_name_store
);
285 static ssize_t
system_type_show(struct kobject
*kobj
,
286 struct kobj_attribute
*attr
, char *page
)
290 mutex_lock(&sclp_cpi_mutex
);
291 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", system_type
);
292 mutex_unlock(&sclp_cpi_mutex
);
296 static ssize_t
system_type_store(struct kobject
*kobj
,
297 struct kobj_attribute
*attr
,
303 rc
= check_string("system_type", buf
);
307 mutex_lock(&sclp_cpi_mutex
);
308 set_string(system_type
, buf
);
309 mutex_unlock(&sclp_cpi_mutex
);
314 static struct kobj_attribute system_type_attr
=
315 __ATTR(system_type
, 0644, system_type_show
, system_type_store
);
317 static ssize_t
system_level_show(struct kobject
*kobj
,
318 struct kobj_attribute
*attr
, char *page
)
320 unsigned long long level
;
322 mutex_lock(&sclp_cpi_mutex
);
323 level
= system_level
;
324 mutex_unlock(&sclp_cpi_mutex
);
325 return snprintf(page
, PAGE_SIZE
, "%#018llx\n", level
);
328 static ssize_t
system_level_store(struct kobject
*kobj
,
329 struct kobj_attribute
*attr
,
333 unsigned long long level
;
336 level
= simple_strtoull(buf
, &endp
, 16);
345 mutex_lock(&sclp_cpi_mutex
);
346 system_level
= level
;
347 mutex_unlock(&sclp_cpi_mutex
);
351 static struct kobj_attribute system_level_attr
=
352 __ATTR(system_level
, 0644, system_level_show
, system_level_store
);
354 static ssize_t
set_store(struct kobject
*kobj
,
355 struct kobj_attribute
*attr
,
356 const char *buf
, size_t len
)
360 mutex_lock(&sclp_cpi_mutex
);
362 mutex_unlock(&sclp_cpi_mutex
);
369 static struct kobj_attribute set_attr
= __ATTR(set
, 0200, NULL
, set_store
);
371 static struct attribute
*cpi_attrs
[] = {
372 &system_name_attr
.attr
,
373 &sysplex_name_attr
.attr
,
374 &system_type_attr
.attr
,
375 &system_level_attr
.attr
,
380 static struct attribute_group cpi_attr_group
= {
384 static struct kset
*cpi_kset
;
386 int sclp_cpi_set_data(const char *system
, const char *sysplex
, const char *type
,
391 rc
= check_string("system_name", system
);
394 rc
= check_string("sysplex_name", sysplex
);
397 rc
= check_string("system_type", type
);
401 mutex_lock(&sclp_cpi_mutex
);
402 set_string(system_name
, system
);
403 set_string(sysplex_name
, sysplex
);
404 set_string(system_type
, type
);
405 system_level
= level
;
408 mutex_unlock(&sclp_cpi_mutex
);
412 EXPORT_SYMBOL(sclp_cpi_set_data
);
414 static int __init
cpi_init(void)
418 cpi_kset
= kset_create_and_add("cpi", NULL
, firmware_kobj
);
422 rc
= sysfs_create_group(&cpi_kset
->kobj
, &cpi_attr_group
);
424 kset_unregister(cpi_kset
);
429 __initcall(cpi_init
);