blob: 4a51e3f096894d0151326bfac6ecef7b5c1c73d8 [file] [log] [blame]
Michael Ernstc05ffc42008-01-26 14:10:55 +01001/*
2 * drivers/s390/char/sclp_cpi_sys.c
3 * SCLP control program identification sysfs interface
4 *
5 * Copyright IBM Corp. 2001, 2007
6 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
7 * Michael Ernst <mernst@de.ibm.com>
8 */
9
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +010010#define KMSG_COMPONENT "sclp_cpi"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Michael Ernstc05ffc42008-01-26 14:10:55 +010013#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 <asm/ebcdic.h>
25#include <asm/sclp.h>
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +010026
Michael Ernstc05ffc42008-01-26 14:10:55 +010027#include "sclp.h"
28#include "sclp_rw.h"
29#include "sclp_cpi_sys.h"
30
31#define CPI_LENGTH_NAME 8
32#define CPI_LENGTH_LEVEL 16
33
Michael Ernstb1c02d92008-07-14 09:57:24 +020034static DEFINE_MUTEX(sclp_cpi_mutex);
35
Michael Ernstc05ffc42008-01-26 14:10:55 +010036struct cpi_evbuf {
37 struct evbuf_header header;
38 u8 id_format;
39 u8 reserved0;
40 u8 system_type[CPI_LENGTH_NAME];
41 u64 reserved1;
42 u8 system_name[CPI_LENGTH_NAME];
43 u64 reserved2;
44 u64 system_level;
45 u64 reserved3;
46 u8 sysplex_name[CPI_LENGTH_NAME];
47 u8 reserved4[16];
48} __attribute__((packed));
49
50struct cpi_sccb {
51 struct sccb_header header;
52 struct cpi_evbuf cpi_evbuf;
53} __attribute__((packed));
54
55static struct sclp_register sclp_cpi_event = {
56 .send_mask = EVTYP_CTLPROGIDENT_MASK,
57};
58
59static char system_name[CPI_LENGTH_NAME + 1];
60static char sysplex_name[CPI_LENGTH_NAME + 1];
61static char system_type[CPI_LENGTH_NAME + 1];
62static u64 system_level;
63
64static void set_data(char *field, char *data)
65{
66 memset(field, ' ', CPI_LENGTH_NAME);
67 memcpy(field, data, strlen(data));
68 sclp_ascebc_str(field, CPI_LENGTH_NAME);
69}
70
71static void cpi_callback(struct sclp_req *req, void *data)
72{
73 struct completion *completion = data;
74
75 complete(completion);
76}
77
78static struct sclp_req *cpi_prepare_req(void)
79{
80 struct sclp_req *req;
81 struct cpi_sccb *sccb;
82 struct cpi_evbuf *evb;
83
84 req = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
85 if (!req)
86 return ERR_PTR(-ENOMEM);
87 sccb = (struct cpi_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
88 if (!sccb) {
89 kfree(req);
90 return ERR_PTR(-ENOMEM);
91 }
92
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;
98
99 /* set system type */
100 set_data(evb->system_type, system_type);
101
102 /* set system name */
103 set_data(evb->system_name, system_name);
104
Thomas Weber88393162010-03-16 11:47:56 +0100105 /* set system level */
Michael Ernstc05ffc42008-01-26 14:10:55 +0100106 evb->system_level = system_level;
107
108 /* set sysplex name */
109 set_data(evb->sysplex_name, sysplex_name);
110
111 /* prepare request data structure presented to SCLP driver */
112 req->command = SCLP_CMDW_WRITE_EVENT_DATA;
113 req->sccb = sccb;
114 req->status = SCLP_REQ_FILLED;
115 req->callback = cpi_callback;
116 return req;
117}
118
119static void cpi_free_req(struct sclp_req *req)
120{
121 free_page((unsigned long) req->sccb);
122 kfree(req);
123}
124
125static int cpi_req(void)
126{
127 struct completion completion;
128 struct sclp_req *req;
129 int rc;
130 int response;
131
132 rc = sclp_register(&sclp_cpi_event);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200133 if (rc)
Michael Ernstc05ffc42008-01-26 14:10:55 +0100134 goto out;
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100135 if (!(sclp_cpi_event.sclp_receive_mask & EVTYP_CTLPROGIDENT_MASK)) {
Michael Ernstc05ffc42008-01-26 14:10:55 +0100136 rc = -EOPNOTSUPP;
137 goto out_unregister;
138 }
139
140 req = cpi_prepare_req();
141 if (IS_ERR(req)) {
Michael Ernstc05ffc42008-01-26 14:10:55 +0100142 rc = PTR_ERR(req);
143 goto out_unregister;
144 }
145
146 init_completion(&completion);
147 req->callback_data = &completion;
148
149 /* Add request to sclp queue */
150 rc = sclp_add_request(req);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200151 if (rc)
Michael Ernstc05ffc42008-01-26 14:10:55 +0100152 goto out_free_req;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100153
154 wait_for_completion(&completion);
155
156 if (req->status != SCLP_REQ_DONE) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100157 pr_warning("request failed (status=0x%02x)\n",
158 req->status);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100159 rc = -EIO;
160 goto out_free_req;
161 }
162
163 response = ((struct cpi_sccb *) req->sccb)->header.response_code;
164 if (response != 0x0020) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100165 pr_warning("request failed with response code 0x%x\n",
166 response);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100167 rc = -EIO;
168 }
169
170out_free_req:
171 cpi_free_req(req);
172
173out_unregister:
174 sclp_unregister(&sclp_cpi_event);
175
176out:
177 return rc;
178}
179
180static int check_string(const char *attr, const char *str)
181{
182 size_t len;
183 size_t i;
184
185 len = strlen(str);
186
187 if ((len > 0) && (str[len - 1] == '\n'))
188 len--;
189
190 if (len > CPI_LENGTH_NAME)
191 return -EINVAL;
192
193 for (i = 0; i < len ; i++) {
194 if (isalpha(str[i]) || isdigit(str[i]) ||
195 strchr("$@# ", str[i]))
196 continue;
197 return -EINVAL;
198 }
199
200 return 0;
201}
202
203static void set_string(char *attr, const char *value)
204{
205 size_t len;
206 size_t i;
207
208 len = strlen(value);
209
210 if ((len > 0) && (value[len - 1] == '\n'))
211 len--;
212
213 for (i = 0; i < CPI_LENGTH_NAME; i++) {
214 if (i < len)
215 attr[i] = toupper(value[i]);
216 else
217 attr[i] = ' ';
218 }
219}
220
221static ssize_t system_name_show(struct kobject *kobj,
222 struct kobj_attribute *attr, char *page)
223{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200224 int rc;
225
226 mutex_lock(&sclp_cpi_mutex);
227 rc = snprintf(page, PAGE_SIZE, "%s\n", system_name);
228 mutex_unlock(&sclp_cpi_mutex);
229 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100230}
231
232static ssize_t system_name_store(struct kobject *kobj,
233 struct kobj_attribute *attr,
234 const char *buf,
235 size_t len)
236{
237 int rc;
238
239 rc = check_string("system_name", buf);
240 if (rc)
241 return rc;
242
Michael Ernstb1c02d92008-07-14 09:57:24 +0200243 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100244 set_string(system_name, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200245 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100246
247 return len;
248}
249
250static struct kobj_attribute system_name_attr =
251 __ATTR(system_name, 0644, system_name_show, system_name_store);
252
253static ssize_t sysplex_name_show(struct kobject *kobj,
254 struct kobj_attribute *attr, char *page)
255{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200256 int rc;
257
258 mutex_lock(&sclp_cpi_mutex);
259 rc = snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
260 mutex_unlock(&sclp_cpi_mutex);
261 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100262}
263
264static ssize_t sysplex_name_store(struct kobject *kobj,
265 struct kobj_attribute *attr,
266 const char *buf,
267 size_t len)
268{
269 int rc;
270
271 rc = check_string("sysplex_name", buf);
272 if (rc)
273 return rc;
274
Michael Ernstb1c02d92008-07-14 09:57:24 +0200275 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100276 set_string(sysplex_name, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200277 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100278
279 return len;
280}
281
282static struct kobj_attribute sysplex_name_attr =
283 __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
284
285static ssize_t system_type_show(struct kobject *kobj,
286 struct kobj_attribute *attr, char *page)
287{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200288 int rc;
289
290 mutex_lock(&sclp_cpi_mutex);
291 rc = snprintf(page, PAGE_SIZE, "%s\n", system_type);
292 mutex_unlock(&sclp_cpi_mutex);
293 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100294}
295
296static ssize_t system_type_store(struct kobject *kobj,
297 struct kobj_attribute *attr,
298 const char *buf,
299 size_t len)
300{
301 int rc;
302
303 rc = check_string("system_type", buf);
304 if (rc)
305 return rc;
306
Michael Ernstb1c02d92008-07-14 09:57:24 +0200307 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100308 set_string(system_type, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200309 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100310
311 return len;
312}
313
314static struct kobj_attribute system_type_attr =
315 __ATTR(system_type, 0644, system_type_show, system_type_store);
316
317static ssize_t system_level_show(struct kobject *kobj,
318 struct kobj_attribute *attr, char *page)
319{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200320 unsigned long long level;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100321
Michael Ernstb1c02d92008-07-14 09:57:24 +0200322 mutex_lock(&sclp_cpi_mutex);
323 level = system_level;
324 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100325 return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
326}
327
328static ssize_t system_level_store(struct kobject *kobj,
329 struct kobj_attribute *attr,
330 const char *buf,
331 size_t len)
332{
333 unsigned long long level;
334 char *endp;
335
336 level = simple_strtoull(buf, &endp, 16);
337
338 if (endp == buf)
339 return -EINVAL;
340 if (*endp == '\n')
341 endp++;
342 if (*endp)
343 return -EINVAL;
344
Michael Ernstb1c02d92008-07-14 09:57:24 +0200345 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100346 system_level = level;
Michael Ernstb1c02d92008-07-14 09:57:24 +0200347 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100348 return len;
349}
350
351static struct kobj_attribute system_level_attr =
352 __ATTR(system_level, 0644, system_level_show, system_level_store);
353
354static ssize_t set_store(struct kobject *kobj,
355 struct kobj_attribute *attr,
356 const char *buf, size_t len)
357{
358 int rc;
359
Michael Ernstb1c02d92008-07-14 09:57:24 +0200360 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100361 rc = cpi_req();
Michael Ernstb1c02d92008-07-14 09:57:24 +0200362 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100363 if (rc)
364 return rc;
365
366 return len;
367}
368
369static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
370
371static struct attribute *cpi_attrs[] = {
372 &system_name_attr.attr,
373 &sysplex_name_attr.attr,
374 &system_type_attr.attr,
375 &system_level_attr.attr,
376 &set_attr.attr,
377 NULL,
378};
379
380static struct attribute_group cpi_attr_group = {
381 .attrs = cpi_attrs,
382};
383
384static struct kset *cpi_kset;
385
386int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
387 const u64 level)
388{
389 int rc;
390
391 rc = check_string("system_name", system);
392 if (rc)
393 return rc;
394 rc = check_string("sysplex_name", sysplex);
395 if (rc)
396 return rc;
397 rc = check_string("system_type", type);
398 if (rc)
399 return rc;
400
Michael Ernstb1c02d92008-07-14 09:57:24 +0200401 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100402 set_string(system_name, system);
403 set_string(sysplex_name, sysplex);
404 set_string(system_type, type);
405 system_level = level;
406
Michael Ernstb1c02d92008-07-14 09:57:24 +0200407 rc = cpi_req();
408 mutex_unlock(&sclp_cpi_mutex);
409
410 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100411}
412EXPORT_SYMBOL(sclp_cpi_set_data);
413
414static int __init cpi_init(void)
415{
416 int rc;
417
418 cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
419 if (!cpi_kset)
420 return -ENOMEM;
421
422 rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
423 if (rc)
424 kset_unregister(cpi_kset);
425
426 return rc;
427}
428
429__initcall(cpi_init);