1 // SPDX-License-Identifier: GPL-2.0
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 #define KMSG_COMPONENT "sclp_cpi"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/stat.h>
16 #include <linux/device.h>
17 #include <linux/string.h>
18 #include <linux/ctype.h>
19 #include <linux/kmod.h>
20 #include <linux/timer.h>
21 #include <linux/err.h>
22 #include <linux/slab.h>
23 #include <linux/completion.h>
24 #include <linux/export.h>
25 #include <asm/ebcdic.h>
30 #include "sclp_cpi_sys.h"
32 #define CPI_LENGTH_NAME 8
33 #define CPI_LENGTH_LEVEL 16
35 static DEFINE_MUTEX(sclp_cpi_mutex
);
38 struct evbuf_header header
;
41 u8 system_type
[CPI_LENGTH_NAME
];
43 u8 system_name
[CPI_LENGTH_NAME
];
47 u8 sysplex_name
[CPI_LENGTH_NAME
];
49 } __attribute__((packed
));
52 struct sccb_header header
;
53 struct cpi_evbuf cpi_evbuf
;
54 } __attribute__((packed
));
56 static struct sclp_register sclp_cpi_event
= {
57 .send_mask
= EVTYP_CTLPROGIDENT_MASK
,
60 static char system_name
[CPI_LENGTH_NAME
+ 1];
61 static char sysplex_name
[CPI_LENGTH_NAME
+ 1];
62 static char system_type
[CPI_LENGTH_NAME
+ 1];
63 static u64 system_level
;
65 static void set_data(char *field
, char *data
)
67 memset(field
, ' ', CPI_LENGTH_NAME
);
68 memcpy(field
, data
, strlen(data
));
69 sclp_ascebc_str(field
, CPI_LENGTH_NAME
);
72 static void cpi_callback(struct sclp_req
*req
, void *data
)
74 struct completion
*completion
= data
;
79 static struct sclp_req
*cpi_prepare_req(void)
82 struct cpi_sccb
*sccb
;
83 struct cpi_evbuf
*evb
;
85 req
= kzalloc(sizeof(struct sclp_req
), GFP_KERNEL
);
87 return ERR_PTR(-ENOMEM
);
88 sccb
= (struct cpi_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
91 return ERR_PTR(-ENOMEM
);
94 /* setup SCCB for Control-Program Identification */
95 sccb
->header
.length
= sizeof(struct cpi_sccb
);
96 sccb
->cpi_evbuf
.header
.length
= sizeof(struct cpi_evbuf
);
97 sccb
->cpi_evbuf
.header
.type
= EVTYP_CTLPROGIDENT
;
98 evb
= &sccb
->cpi_evbuf
;
100 /* set system type */
101 set_data(evb
->system_type
, system_type
);
103 /* set system name */
104 set_data(evb
->system_name
, system_name
);
106 /* set system level */
107 evb
->system_level
= system_level
;
109 /* set sysplex name */
110 set_data(evb
->sysplex_name
, sysplex_name
);
112 /* prepare request data structure presented to SCLP driver */
113 req
->command
= SCLP_CMDW_WRITE_EVENT_DATA
;
115 req
->status
= SCLP_REQ_FILLED
;
116 req
->callback
= cpi_callback
;
120 static void cpi_free_req(struct sclp_req
*req
)
122 free_page((unsigned long) req
->sccb
);
126 static int cpi_req(void)
128 struct completion completion
;
129 struct sclp_req
*req
;
133 rc
= sclp_register(&sclp_cpi_event
);
136 if (!(sclp_cpi_event
.sclp_receive_mask
& EVTYP_CTLPROGIDENT_MASK
)) {
141 req
= cpi_prepare_req();
147 init_completion(&completion
);
148 req
->callback_data
= &completion
;
150 /* Add request to sclp queue */
151 rc
= sclp_add_request(req
);
155 wait_for_completion(&completion
);
157 if (req
->status
!= SCLP_REQ_DONE
) {
158 pr_warn("request failed (status=0x%02x)\n", req
->status
);
163 response
= ((struct cpi_sccb
*) req
->sccb
)->header
.response_code
;
164 if (response
!= 0x0020) {
165 pr_warn("request failed with response code 0x%x\n", response
);
173 sclp_unregister(&sclp_cpi_event
);
179 static int check_string(const char *attr
, const char *str
)
186 if ((len
> 0) && (str
[len
- 1] == '\n'))
189 if (len
> CPI_LENGTH_NAME
)
192 for (i
= 0; i
< len
; i
++) {
193 if (isalpha(str
[i
]) || isdigit(str
[i
]) ||
194 strchr("$@# ", str
[i
]))
202 static void set_string(char *attr
, const char *value
)
209 if ((len
> 0) && (value
[len
- 1] == '\n'))
212 for (i
= 0; i
< CPI_LENGTH_NAME
; i
++) {
214 attr
[i
] = toupper(value
[i
]);
220 static ssize_t
system_name_show(struct kobject
*kobj
,
221 struct kobj_attribute
*attr
, char *page
)
225 mutex_lock(&sclp_cpi_mutex
);
226 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", system_name
);
227 mutex_unlock(&sclp_cpi_mutex
);
231 static ssize_t
system_name_store(struct kobject
*kobj
,
232 struct kobj_attribute
*attr
,
238 rc
= check_string("system_name", buf
);
242 mutex_lock(&sclp_cpi_mutex
);
243 set_string(system_name
, buf
);
244 mutex_unlock(&sclp_cpi_mutex
);
249 static struct kobj_attribute system_name_attr
=
250 __ATTR(system_name
, 0644, system_name_show
, system_name_store
);
252 static ssize_t
sysplex_name_show(struct kobject
*kobj
,
253 struct kobj_attribute
*attr
, char *page
)
257 mutex_lock(&sclp_cpi_mutex
);
258 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", sysplex_name
);
259 mutex_unlock(&sclp_cpi_mutex
);
263 static ssize_t
sysplex_name_store(struct kobject
*kobj
,
264 struct kobj_attribute
*attr
,
270 rc
= check_string("sysplex_name", buf
);
274 mutex_lock(&sclp_cpi_mutex
);
275 set_string(sysplex_name
, buf
);
276 mutex_unlock(&sclp_cpi_mutex
);
281 static struct kobj_attribute sysplex_name_attr
=
282 __ATTR(sysplex_name
, 0644, sysplex_name_show
, sysplex_name_store
);
284 static ssize_t
system_type_show(struct kobject
*kobj
,
285 struct kobj_attribute
*attr
, char *page
)
289 mutex_lock(&sclp_cpi_mutex
);
290 rc
= snprintf(page
, PAGE_SIZE
, "%s\n", system_type
);
291 mutex_unlock(&sclp_cpi_mutex
);
295 static ssize_t
system_type_store(struct kobject
*kobj
,
296 struct kobj_attribute
*attr
,
302 rc
= check_string("system_type", buf
);
306 mutex_lock(&sclp_cpi_mutex
);
307 set_string(system_type
, buf
);
308 mutex_unlock(&sclp_cpi_mutex
);
313 static struct kobj_attribute system_type_attr
=
314 __ATTR(system_type
, 0644, system_type_show
, system_type_store
);
316 static ssize_t
system_level_show(struct kobject
*kobj
,
317 struct kobj_attribute
*attr
, char *page
)
319 unsigned long long level
;
321 mutex_lock(&sclp_cpi_mutex
);
322 level
= system_level
;
323 mutex_unlock(&sclp_cpi_mutex
);
324 return snprintf(page
, PAGE_SIZE
, "%#018llx\n", level
);
327 static ssize_t
system_level_store(struct kobject
*kobj
,
328 struct kobj_attribute
*attr
,
332 unsigned long long level
;
335 level
= simple_strtoull(buf
, &endp
, 16);
344 mutex_lock(&sclp_cpi_mutex
);
345 system_level
= level
;
346 mutex_unlock(&sclp_cpi_mutex
);
350 static struct kobj_attribute system_level_attr
=
351 __ATTR(system_level
, 0644, system_level_show
, system_level_store
);
353 static ssize_t
set_store(struct kobject
*kobj
,
354 struct kobj_attribute
*attr
,
355 const char *buf
, size_t len
)
359 mutex_lock(&sclp_cpi_mutex
);
361 mutex_unlock(&sclp_cpi_mutex
);
368 static struct kobj_attribute set_attr
= __ATTR(set
, 0200, NULL
, set_store
);
370 static struct attribute
*cpi_attrs
[] = {
371 &system_name_attr
.attr
,
372 &sysplex_name_attr
.attr
,
373 &system_type_attr
.attr
,
374 &system_level_attr
.attr
,
379 static struct attribute_group cpi_attr_group
= {
383 static struct kset
*cpi_kset
;
385 int sclp_cpi_set_data(const char *system
, const char *sysplex
, const char *type
,
390 rc
= check_string("system_name", system
);
393 rc
= check_string("sysplex_name", sysplex
);
396 rc
= check_string("system_type", type
);
400 mutex_lock(&sclp_cpi_mutex
);
401 set_string(system_name
, system
);
402 set_string(sysplex_name
, sysplex
);
403 set_string(system_type
, type
);
404 system_level
= level
;
407 mutex_unlock(&sclp_cpi_mutex
);
411 EXPORT_SYMBOL(sclp_cpi_set_data
);
413 static int __init
cpi_init(void)
417 cpi_kset
= kset_create_and_add("cpi", NULL
, firmware_kobj
);
421 rc
= sysfs_create_group(&cpi_kset
->kobj
, &cpi_attr_group
);
423 kset_unregister(cpi_kset
);
428 __initcall(cpi_init
);