blob: 005f330ed92689a0de2faaf7e3108879e4a213a9 [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
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"
26
27#define CPI_LENGTH_NAME 8
28#define CPI_LENGTH_LEVEL 16
29
Michael Ernstb1c02d92008-07-14 09:57:24 +020030static DEFINE_MUTEX(sclp_cpi_mutex);
31
Michael Ernstc05ffc42008-01-26 14:10:55 +010032struct cpi_evbuf {
33 struct evbuf_header header;
34 u8 id_format;
35 u8 reserved0;
36 u8 system_type[CPI_LENGTH_NAME];
37 u64 reserved1;
38 u8 system_name[CPI_LENGTH_NAME];
39 u64 reserved2;
40 u64 system_level;
41 u64 reserved3;
42 u8 sysplex_name[CPI_LENGTH_NAME];
43 u8 reserved4[16];
44} __attribute__((packed));
45
46struct cpi_sccb {
47 struct sccb_header header;
48 struct cpi_evbuf cpi_evbuf;
49} __attribute__((packed));
50
51static struct sclp_register sclp_cpi_event = {
52 .send_mask = EVTYP_CTLPROGIDENT_MASK,
53};
54
55static char system_name[CPI_LENGTH_NAME + 1];
56static char sysplex_name[CPI_LENGTH_NAME + 1];
57static char system_type[CPI_LENGTH_NAME + 1];
58static u64 system_level;
59
60static void set_data(char *field, char *data)
61{
62 memset(field, ' ', CPI_LENGTH_NAME);
63 memcpy(field, data, strlen(data));
64 sclp_ascebc_str(field, CPI_LENGTH_NAME);
65}
66
67static void cpi_callback(struct sclp_req *req, void *data)
68{
69 struct completion *completion = data;
70
71 complete(completion);
72}
73
74static struct sclp_req *cpi_prepare_req(void)
75{
76 struct sclp_req *req;
77 struct cpi_sccb *sccb;
78 struct cpi_evbuf *evb;
79
80 req = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
81 if (!req)
82 return ERR_PTR(-ENOMEM);
83 sccb = (struct cpi_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
84 if (!sccb) {
85 kfree(req);
86 return ERR_PTR(-ENOMEM);
87 }
88
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;
94
95 /* set system type */
96 set_data(evb->system_type, system_type);
97
98 /* set system name */
99 set_data(evb->system_name, system_name);
100
101 /* set sytem level */
102 evb->system_level = system_level;
103
104 /* set sysplex name */
105 set_data(evb->sysplex_name, sysplex_name);
106
107 /* prepare request data structure presented to SCLP driver */
108 req->command = SCLP_CMDW_WRITE_EVENT_DATA;
109 req->sccb = sccb;
110 req->status = SCLP_REQ_FILLED;
111 req->callback = cpi_callback;
112 return req;
113}
114
115static void cpi_free_req(struct sclp_req *req)
116{
117 free_page((unsigned long) req->sccb);
118 kfree(req);
119}
120
121static int cpi_req(void)
122{
123 struct completion completion;
124 struct sclp_req *req;
125 int rc;
126 int response;
127
128 rc = sclp_register(&sclp_cpi_event);
129 if (rc) {
130 printk(KERN_WARNING "cpi: could not register "
131 "to hardware console.\n");
132 goto out;
133 }
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100134 if (!(sclp_cpi_event.sclp_receive_mask & EVTYP_CTLPROGIDENT_MASK)) {
Michael Ernstc05ffc42008-01-26 14:10:55 +0100135 printk(KERN_WARNING "cpi: no control program "
136 "identification support\n");
137 rc = -EOPNOTSUPP;
138 goto out_unregister;
139 }
140
141 req = cpi_prepare_req();
142 if (IS_ERR(req)) {
143 printk(KERN_WARNING "cpi: could not allocate request\n");
144 rc = PTR_ERR(req);
145 goto out_unregister;
146 }
147
148 init_completion(&completion);
149 req->callback_data = &completion;
150
151 /* Add request to sclp queue */
152 rc = sclp_add_request(req);
153 if (rc) {
154 printk(KERN_WARNING "cpi: could not start request\n");
155 goto out_free_req;
156 }
157
158 wait_for_completion(&completion);
159
160 if (req->status != SCLP_REQ_DONE) {
161 printk(KERN_WARNING "cpi: request failed (status=0x%02x)\n",
162 req->status);
163 rc = -EIO;
164 goto out_free_req;
165 }
166
167 response = ((struct cpi_sccb *) req->sccb)->header.response_code;
168 if (response != 0x0020) {
169 printk(KERN_WARNING "cpi: failed with "
170 "response code 0x%x\n", response);
171 rc = -EIO;
172 }
173
174out_free_req:
175 cpi_free_req(req);
176
177out_unregister:
178 sclp_unregister(&sclp_cpi_event);
179
180out:
181 return rc;
182}
183
184static int check_string(const char *attr, const char *str)
185{
186 size_t len;
187 size_t i;
188
189 len = strlen(str);
190
191 if ((len > 0) && (str[len - 1] == '\n'))
192 len--;
193
194 if (len > CPI_LENGTH_NAME)
195 return -EINVAL;
196
197 for (i = 0; i < len ; i++) {
198 if (isalpha(str[i]) || isdigit(str[i]) ||
199 strchr("$@# ", str[i]))
200 continue;
201 return -EINVAL;
202 }
203
204 return 0;
205}
206
207static void set_string(char *attr, const char *value)
208{
209 size_t len;
210 size_t i;
211
212 len = strlen(value);
213
214 if ((len > 0) && (value[len - 1] == '\n'))
215 len--;
216
217 for (i = 0; i < CPI_LENGTH_NAME; i++) {
218 if (i < len)
219 attr[i] = toupper(value[i]);
220 else
221 attr[i] = ' ';
222 }
223}
224
225static ssize_t system_name_show(struct kobject *kobj,
226 struct kobj_attribute *attr, char *page)
227{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200228 int rc;
229
230 mutex_lock(&sclp_cpi_mutex);
231 rc = snprintf(page, PAGE_SIZE, "%s\n", system_name);
232 mutex_unlock(&sclp_cpi_mutex);
233 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100234}
235
236static ssize_t system_name_store(struct kobject *kobj,
237 struct kobj_attribute *attr,
238 const char *buf,
239 size_t len)
240{
241 int rc;
242
243 rc = check_string("system_name", buf);
244 if (rc)
245 return rc;
246
Michael Ernstb1c02d92008-07-14 09:57:24 +0200247 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100248 set_string(system_name, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200249 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100250
251 return len;
252}
253
254static struct kobj_attribute system_name_attr =
255 __ATTR(system_name, 0644, system_name_show, system_name_store);
256
257static ssize_t sysplex_name_show(struct kobject *kobj,
258 struct kobj_attribute *attr, char *page)
259{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200260 int rc;
261
262 mutex_lock(&sclp_cpi_mutex);
263 rc = snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
264 mutex_unlock(&sclp_cpi_mutex);
265 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100266}
267
268static ssize_t sysplex_name_store(struct kobject *kobj,
269 struct kobj_attribute *attr,
270 const char *buf,
271 size_t len)
272{
273 int rc;
274
275 rc = check_string("sysplex_name", buf);
276 if (rc)
277 return rc;
278
Michael Ernstb1c02d92008-07-14 09:57:24 +0200279 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100280 set_string(sysplex_name, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200281 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100282
283 return len;
284}
285
286static struct kobj_attribute sysplex_name_attr =
287 __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
288
289static ssize_t system_type_show(struct kobject *kobj,
290 struct kobj_attribute *attr, char *page)
291{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200292 int rc;
293
294 mutex_lock(&sclp_cpi_mutex);
295 rc = snprintf(page, PAGE_SIZE, "%s\n", system_type);
296 mutex_unlock(&sclp_cpi_mutex);
297 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100298}
299
300static ssize_t system_type_store(struct kobject *kobj,
301 struct kobj_attribute *attr,
302 const char *buf,
303 size_t len)
304{
305 int rc;
306
307 rc = check_string("system_type", buf);
308 if (rc)
309 return rc;
310
Michael Ernstb1c02d92008-07-14 09:57:24 +0200311 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100312 set_string(system_type, buf);
Michael Ernstb1c02d92008-07-14 09:57:24 +0200313 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100314
315 return len;
316}
317
318static struct kobj_attribute system_type_attr =
319 __ATTR(system_type, 0644, system_type_show, system_type_store);
320
321static ssize_t system_level_show(struct kobject *kobj,
322 struct kobj_attribute *attr, char *page)
323{
Michael Ernstb1c02d92008-07-14 09:57:24 +0200324 unsigned long long level;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100325
Michael Ernstb1c02d92008-07-14 09:57:24 +0200326 mutex_lock(&sclp_cpi_mutex);
327 level = system_level;
328 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100329 return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
330}
331
332static ssize_t system_level_store(struct kobject *kobj,
333 struct kobj_attribute *attr,
334 const char *buf,
335 size_t len)
336{
337 unsigned long long level;
338 char *endp;
339
340 level = simple_strtoull(buf, &endp, 16);
341
342 if (endp == buf)
343 return -EINVAL;
344 if (*endp == '\n')
345 endp++;
346 if (*endp)
347 return -EINVAL;
348
Michael Ernstb1c02d92008-07-14 09:57:24 +0200349 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100350 system_level = level;
Michael Ernstb1c02d92008-07-14 09:57:24 +0200351 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100352 return len;
353}
354
355static struct kobj_attribute system_level_attr =
356 __ATTR(system_level, 0644, system_level_show, system_level_store);
357
358static ssize_t set_store(struct kobject *kobj,
359 struct kobj_attribute *attr,
360 const char *buf, size_t len)
361{
362 int rc;
363
Michael Ernstb1c02d92008-07-14 09:57:24 +0200364 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100365 rc = cpi_req();
Michael Ernstb1c02d92008-07-14 09:57:24 +0200366 mutex_unlock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100367 if (rc)
368 return rc;
369
370 return len;
371}
372
373static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
374
375static struct attribute *cpi_attrs[] = {
376 &system_name_attr.attr,
377 &sysplex_name_attr.attr,
378 &system_type_attr.attr,
379 &system_level_attr.attr,
380 &set_attr.attr,
381 NULL,
382};
383
384static struct attribute_group cpi_attr_group = {
385 .attrs = cpi_attrs,
386};
387
388static struct kset *cpi_kset;
389
390int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
391 const u64 level)
392{
393 int rc;
394
395 rc = check_string("system_name", system);
396 if (rc)
397 return rc;
398 rc = check_string("sysplex_name", sysplex);
399 if (rc)
400 return rc;
401 rc = check_string("system_type", type);
402 if (rc)
403 return rc;
404
Michael Ernstb1c02d92008-07-14 09:57:24 +0200405 mutex_lock(&sclp_cpi_mutex);
Michael Ernstc05ffc42008-01-26 14:10:55 +0100406 set_string(system_name, system);
407 set_string(sysplex_name, sysplex);
408 set_string(system_type, type);
409 system_level = level;
410
Michael Ernstb1c02d92008-07-14 09:57:24 +0200411 rc = cpi_req();
412 mutex_unlock(&sclp_cpi_mutex);
413
414 return rc;
Michael Ernstc05ffc42008-01-26 14:10:55 +0100415}
416EXPORT_SYMBOL(sclp_cpi_set_data);
417
418static int __init cpi_init(void)
419{
420 int rc;
421
422 cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
423 if (!cpi_kset)
424 return -ENOMEM;
425
426 rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
427 if (rc)
428 kset_unregister(cpi_kset);
429
430 return rc;
431}
432
433__initcall(cpi_init);