1
2
3
4
5
6
7
8#define KMSG_COMPONENT "sclp_sd"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11#include <linux/completion.h>
12#include <linux/kobject.h>
13#include <linux/list.h>
14#include <linux/printk.h>
15#include <linux/slab.h>
16#include <linux/vmalloc.h>
17#include <linux/async.h>
18#include <linux/export.h>
19#include <linux/mutex.h>
20
21#include <asm/pgalloc.h>
22
23#include "sclp.h"
24
25#define SD_EQ_STORE_DATA 0
26#define SD_EQ_HALT 1
27#define SD_EQ_SIZE 2
28
29#define SD_DI_CONFIG 3
30
31struct sclp_sd_evbuf {
32 struct evbuf_header hdr;
33 u8 eq;
34 u8 di;
35 u8 rflags;
36 u64 :56;
37 u32 id;
38 u16 :16;
39 u8 fmt;
40 u8 status;
41 u64 sat;
42 u64 sa;
43 u32 esize;
44 u32 dsize;
45} __packed;
46
47struct sclp_sd_sccb {
48 struct sccb_header hdr;
49 struct sclp_sd_evbuf evbuf;
50} __packed __aligned(PAGE_SIZE);
51
52
53
54
55
56
57
58struct sclp_sd_data {
59 size_t esize_bytes;
60 size_t dsize_bytes;
61 void *data;
62};
63
64
65
66
67
68
69
70
71struct sclp_sd_listener {
72 struct list_head list;
73 u32 id;
74 struct completion completion;
75 struct sclp_sd_evbuf evbuf;
76};
77
78
79
80
81
82
83
84
85
86struct sclp_sd_file {
87 struct kobject kobj;
88 struct bin_attribute data_attr;
89 struct mutex data_mutex;
90 struct sclp_sd_data data;
91 u8 di;
92};
93#define to_sd_file(x) container_of(x, struct sclp_sd_file, kobj)
94
95static struct kset *sclp_sd_kset;
96static struct sclp_sd_file *config_file;
97
98static LIST_HEAD(sclp_sd_queue);
99static DEFINE_SPINLOCK(sclp_sd_queue_lock);
100
101
102
103
104
105static void sclp_sd_listener_add(struct sclp_sd_listener *listener)
106{
107 spin_lock_irq(&sclp_sd_queue_lock);
108 list_add_tail(&listener->list, &sclp_sd_queue);
109 spin_unlock_irq(&sclp_sd_queue_lock);
110}
111
112
113
114
115
116static void sclp_sd_listener_remove(struct sclp_sd_listener *listener)
117{
118 spin_lock_irq(&sclp_sd_queue_lock);
119 list_del(&listener->list);
120 spin_unlock_irq(&sclp_sd_queue_lock);
121}
122
123
124
125
126
127
128
129
130
131static void sclp_sd_listener_init(struct sclp_sd_listener *listener, u32 id)
132{
133 memset(listener, 0, sizeof(*listener));
134 listener->id = id;
135 init_completion(&listener->completion);
136}
137
138
139
140
141
142
143
144static void sclp_sd_receiver(struct evbuf_header *evbuf_hdr)
145{
146 struct sclp_sd_evbuf *evbuf = (struct sclp_sd_evbuf *) evbuf_hdr;
147 struct sclp_sd_listener *listener;
148 int found = 0;
149
150 pr_debug("received event (id=0x%08x)\n", evbuf->id);
151 spin_lock(&sclp_sd_queue_lock);
152 list_for_each_entry(listener, &sclp_sd_queue, list) {
153 if (listener->id != evbuf->id)
154 continue;
155
156 listener->evbuf = *evbuf;
157 complete(&listener->completion);
158 found = 1;
159 break;
160 }
161 spin_unlock(&sclp_sd_queue_lock);
162
163 if (!found)
164 pr_debug("unsolicited event (id=0x%08x)\n", evbuf->id);
165}
166
167static struct sclp_register sclp_sd_register = {
168 .send_mask = EVTYP_STORE_DATA_MASK,
169 .receive_mask = EVTYP_STORE_DATA_MASK,
170 .receiver_fn = sclp_sd_receiver,
171};
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188static int sclp_sd_sync(unsigned long page, u8 eq, u8 di, u64 sat, u64 sa,
189 u32 *dsize_ptr, u32 *esize_ptr)
190{
191 struct sclp_sd_sccb *sccb = (void *) page;
192 struct sclp_sd_listener listener;
193 struct sclp_sd_evbuf *evbuf;
194 int rc;
195
196 sclp_sd_listener_init(&listener, (u32) (addr_t) sccb);
197 sclp_sd_listener_add(&listener);
198
199
200 memset(sccb, 0, PAGE_SIZE);
201 sccb->hdr.length = sizeof(sccb->hdr) + sizeof(sccb->evbuf);
202 evbuf = &sccb->evbuf;
203 evbuf->hdr.length = sizeof(*evbuf);
204 evbuf->hdr.type = EVTYP_STORE_DATA;
205 evbuf->eq = eq;
206 evbuf->di = di;
207 evbuf->id = listener.id;
208 evbuf->fmt = 1;
209 evbuf->sat = sat;
210 evbuf->sa = sa;
211 if (dsize_ptr)
212 evbuf->dsize = *dsize_ptr;
213
214
215 pr_debug("request (eq=%d, di=%d, id=0x%08x)\n", eq, di, listener.id);
216 rc = sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
217 pr_debug("request done (rc=%d)\n", rc);
218 if (rc)
219 goto out;
220
221
222 if (sccb->hdr.response_code == 0x73f0) {
223 pr_debug("event not supported\n");
224 rc = -EIO;
225 goto out_remove;
226 }
227 if (sccb->hdr.response_code != 0x0020 || !(evbuf->hdr.flags & 0x80)) {
228 rc = -EIO;
229 goto out;
230 }
231 if (!(evbuf->rflags & 0x80)) {
232 rc = wait_for_completion_interruptible(&listener.completion);
233 if (rc)
234 goto out;
235 evbuf = &listener.evbuf;
236 }
237 switch (evbuf->status) {
238 case 0:
239 if (dsize_ptr)
240 *dsize_ptr = evbuf->dsize;
241 if (esize_ptr)
242 *esize_ptr = evbuf->esize;
243 pr_debug("success (dsize=%u, esize=%u)\n", evbuf->dsize,
244 evbuf->esize);
245 break;
246 case 3:
247 rc = -ENOENT;
248 break;
249 default:
250 rc = -EIO;
251 break;
252
253 }
254
255out:
256 if (rc && rc != -ENOENT) {
257
258 pr_warn("Store Data request failed (eq=%d, di=%d, "
259 "response=0x%04x, flags=0x%02x, status=%d, rc=%d)\n",
260 eq, di, sccb->hdr.response_code, evbuf->hdr.flags,
261 evbuf->status, rc);
262 }
263
264out_remove:
265 sclp_sd_listener_remove(&listener);
266
267 return rc;
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284static int sclp_sd_store_data(struct sclp_sd_data *result, u8 di)
285{
286 u32 dsize = 0, esize = 0;
287 unsigned long page, asce = 0;
288 void *data = NULL;
289 int rc;
290
291 page = __get_free_page(GFP_KERNEL | GFP_DMA);
292 if (!page)
293 return -ENOMEM;
294
295
296 rc = sclp_sd_sync(page, SD_EQ_SIZE, di, 0, 0, &dsize, &esize);
297 if (rc)
298 goto out;
299 if (dsize == 0)
300 goto out_result;
301
302
303 data = vzalloc(array_size((size_t)dsize, PAGE_SIZE));
304 if (!data) {
305 rc = -ENOMEM;
306 goto out;
307 }
308
309
310 asce = base_asce_alloc((unsigned long) data, dsize);
311 if (!asce) {
312 vfree(data);
313 rc = -ENOMEM;
314 goto out;
315 }
316
317
318 rc = sclp_sd_sync(page, SD_EQ_STORE_DATA, di, asce, (u64) data, &dsize,
319 &esize);
320 if (rc) {
321
322 if (rc == -ERESTARTSYS)
323 sclp_sd_sync(page, SD_EQ_HALT, di, 0, 0, NULL, NULL);
324 vfree(data);
325 goto out;
326 }
327
328out_result:
329 result->esize_bytes = (size_t) esize * PAGE_SIZE;
330 result->dsize_bytes = (size_t) dsize * PAGE_SIZE;
331 result->data = data;
332
333out:
334 base_asce_free(asce);
335 free_page(page);
336
337 return rc;
338}
339
340
341
342
343
344
345
346static void sclp_sd_data_reset(struct sclp_sd_data *data)
347{
348 vfree(data->data);
349 data->data = NULL;
350 data->dsize_bytes = 0;
351 data->esize_bytes = 0;
352}
353
354
355
356
357
358static void sclp_sd_file_release(struct kobject *kobj)
359{
360 struct sclp_sd_file *sd_file = to_sd_file(kobj);
361
362 sclp_sd_data_reset(&sd_file->data);
363 kfree(sd_file);
364}
365
366
367
368
369
370
371
372
373
374
375
376static int sclp_sd_file_update(struct sclp_sd_file *sd_file)
377{
378 const char *name = kobject_name(&sd_file->kobj);
379 struct sclp_sd_data data;
380 int rc;
381
382 rc = sclp_sd_store_data(&data, sd_file->di);
383 if (rc) {
384 if (rc == -ENOENT) {
385 pr_info("No data is available for the %s data entity\n",
386 name);
387 }
388 return rc;
389 }
390
391 mutex_lock(&sd_file->data_mutex);
392 sclp_sd_data_reset(&sd_file->data);
393 sd_file->data = data;
394 mutex_unlock(&sd_file->data_mutex);
395
396 pr_info("A %zu-byte %s data entity was retrieved\n", data.dsize_bytes,
397 name);
398 kobject_uevent(&sd_file->kobj, KOBJ_CHANGE);
399
400 return 0;
401}
402
403
404
405
406
407static void sclp_sd_file_update_async(void *data, async_cookie_t cookie)
408{
409 struct sclp_sd_file *sd_file = data;
410
411 sclp_sd_file_update(sd_file);
412}
413
414
415
416
417
418
419
420static ssize_t reload_store(struct kobject *kobj, struct kobj_attribute *attr,
421 const char *buf, size_t count)
422{
423 struct sclp_sd_file *sd_file = to_sd_file(kobj);
424
425 sclp_sd_file_update(sd_file);
426
427 return count;
428}
429
430static struct kobj_attribute reload_attr = __ATTR_WO(reload);
431
432static struct attribute *sclp_sd_file_default_attrs[] = {
433 &reload_attr.attr,
434 NULL,
435};
436
437static struct kobj_type sclp_sd_file_ktype = {
438 .sysfs_ops = &kobj_sysfs_ops,
439 .release = sclp_sd_file_release,
440 .default_attrs = sclp_sd_file_default_attrs,
441};
442
443
444
445
446
447
448
449
450
451
452
453
454static ssize_t data_read(struct file *file, struct kobject *kobj,
455 struct bin_attribute *attr, char *buffer,
456 loff_t off, size_t size)
457{
458 struct sclp_sd_file *sd_file = to_sd_file(kobj);
459 size_t data_size;
460 char *data;
461
462 mutex_lock(&sd_file->data_mutex);
463
464 data = sd_file->data.data;
465 data_size = sd_file->data.dsize_bytes;
466 if (!data || off >= data_size) {
467 size = 0;
468 } else {
469 if (off + size > data_size)
470 size = data_size - off;
471 memcpy(buffer, data + off, size);
472 }
473
474 mutex_unlock(&sd_file->data_mutex);
475
476 return size;
477}
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495static __init struct sclp_sd_file *sclp_sd_file_create(const char *name, u8 di)
496{
497 struct sclp_sd_file *sd_file;
498 int rc;
499
500 sd_file = kzalloc(sizeof(*sd_file), GFP_KERNEL);
501 if (!sd_file)
502 return NULL;
503 sd_file->di = di;
504 mutex_init(&sd_file->data_mutex);
505
506
507 sd_file->kobj.kset = sclp_sd_kset;
508 rc = kobject_init_and_add(&sd_file->kobj, &sclp_sd_file_ktype, NULL,
509 "%s", name);
510 if (rc) {
511 kobject_put(&sd_file->kobj);
512 return NULL;
513 }
514
515 sysfs_bin_attr_init(&sd_file->data_attr);
516 sd_file->data_attr.attr.name = "data";
517 sd_file->data_attr.attr.mode = 0444;
518 sd_file->data_attr.read = data_read;
519
520 rc = sysfs_create_bin_file(&sd_file->kobj, &sd_file->data_attr);
521 if (rc) {
522 kobject_put(&sd_file->kobj);
523 return NULL;
524 }
525
526
527
528
529
530 kobject_uevent(&sd_file->kobj, KOBJ_ADD);
531
532
533 async_schedule(sclp_sd_file_update_async, sd_file);
534
535 return sd_file;
536}
537
538
539
540
541static __init int sclp_sd_init(void)
542{
543 int rc;
544
545 rc = sclp_register(&sclp_sd_register);
546 if (rc)
547 return rc;
548
549
550 rc = -ENOMEM;
551 sclp_sd_kset = kset_create_and_add("sclp_sd", NULL, firmware_kobj);
552 if (!sclp_sd_kset)
553 goto err_kset;
554
555 rc = -EINVAL;
556 config_file = sclp_sd_file_create("config", SD_DI_CONFIG);
557 if (!config_file)
558 goto err_config;
559
560 return 0;
561
562err_config:
563 kset_unregister(sclp_sd_kset);
564err_kset:
565 sclp_unregister(&sclp_sd_register);
566
567 return rc;
568}
569device_initcall(sclp_sd_init);
570