xref: /openbmc/linux/drivers/s390/char/sclp_ocf.c (revision 8730046c)
1 /*
2  *    SCLP OCF communication parameters sysfs interface
3  *
4  *    Copyright IBM Corp. 2011
5  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6  */
7 
8 #define KMSG_COMPONENT "sclp_ocf"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/stat.h>
14 #include <linux/device.h>
15 #include <linux/string.h>
16 #include <linux/ctype.h>
17 #include <linux/kmod.h>
18 #include <linux/timer.h>
19 #include <linux/err.h>
20 #include <asm/ebcdic.h>
21 #include <asm/sclp.h>
22 
23 #include "sclp.h"
24 
25 #define OCF_LENGTH_HMC_NETWORK 8UL
26 #define OCF_LENGTH_CPC_NAME 8UL
27 
28 static char hmc_network[OCF_LENGTH_HMC_NETWORK + 1];
29 static char cpc_name[OCF_LENGTH_CPC_NAME]; /* in EBCDIC */
30 
31 static DEFINE_SPINLOCK(sclp_ocf_lock);
32 static struct work_struct sclp_ocf_change_work;
33 
34 static struct kset *ocf_kset;
35 
36 static void sclp_ocf_change_notify(struct work_struct *work)
37 {
38 	kobject_uevent(&ocf_kset->kobj, KOBJ_CHANGE);
39 }
40 
41 /* Handler for OCF event. Look for the CPC image name. */
42 static void sclp_ocf_handler(struct evbuf_header *evbuf)
43 {
44 	struct gds_vector *v;
45 	struct gds_subvector *sv, *netid, *cpc;
46 	size_t size;
47 
48 	/* Find the 0x9f00 block. */
49 	v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
50 				 0x9f00);
51 	if (!v)
52 		return;
53 	/* Find the 0x9f22 block inside the 0x9f00 block. */
54 	v = sclp_find_gds_vector(v + 1, (void *) v + v->length, 0x9f22);
55 	if (!v)
56 		return;
57 	/* Find the 0x81 block inside the 0x9f22 block. */
58 	sv = sclp_find_gds_subvector(v + 1, (void *) v + v->length, 0x81);
59 	if (!sv)
60 		return;
61 	/* Find the 0x01 block inside the 0x81 block. */
62 	netid = sclp_find_gds_subvector(sv + 1, (void *) sv + sv->length, 1);
63 	/* Find the 0x02 block inside the 0x81 block. */
64 	cpc = sclp_find_gds_subvector(sv + 1, (void *) sv + sv->length, 2);
65 	/* Copy network name and cpc name. */
66 	spin_lock(&sclp_ocf_lock);
67 	if (netid) {
68 		size = min(OCF_LENGTH_HMC_NETWORK, (size_t) netid->length);
69 		memcpy(hmc_network, netid + 1, size);
70 		EBCASC(hmc_network, size);
71 		hmc_network[size] = 0;
72 	}
73 	if (cpc) {
74 		size = min(OCF_LENGTH_CPC_NAME, (size_t) cpc->length);
75 		memset(cpc_name, 0, OCF_LENGTH_CPC_NAME);
76 		memcpy(cpc_name, cpc + 1, size);
77 	}
78 	spin_unlock(&sclp_ocf_lock);
79 	schedule_work(&sclp_ocf_change_work);
80 }
81 
82 static struct sclp_register sclp_ocf_event = {
83 	.receive_mask = EVTYP_OCF_MASK,
84 	.receiver_fn = sclp_ocf_handler,
85 };
86 
87 void sclp_ocf_cpc_name_copy(char *dst)
88 {
89 	spin_lock_irq(&sclp_ocf_lock);
90 	memcpy(dst, cpc_name, OCF_LENGTH_CPC_NAME);
91 	spin_unlock_irq(&sclp_ocf_lock);
92 }
93 EXPORT_SYMBOL(sclp_ocf_cpc_name_copy);
94 
95 static ssize_t cpc_name_show(struct kobject *kobj,
96 			     struct kobj_attribute *attr, char *page)
97 {
98 	char name[OCF_LENGTH_CPC_NAME + 1];
99 
100 	sclp_ocf_cpc_name_copy(name);
101 	name[OCF_LENGTH_CPC_NAME] = 0;
102 	EBCASC(name, OCF_LENGTH_CPC_NAME);
103 	return snprintf(page, PAGE_SIZE, "%s\n", name);
104 }
105 
106 static struct kobj_attribute cpc_name_attr =
107 	__ATTR(cpc_name, 0444, cpc_name_show, NULL);
108 
109 static ssize_t hmc_network_show(struct kobject *kobj,
110 				struct kobj_attribute *attr, char *page)
111 {
112 	int rc;
113 
114 	spin_lock_irq(&sclp_ocf_lock);
115 	rc = snprintf(page, PAGE_SIZE, "%s\n", hmc_network);
116 	spin_unlock_irq(&sclp_ocf_lock);
117 	return rc;
118 }
119 
120 static struct kobj_attribute hmc_network_attr =
121 	__ATTR(hmc_network, 0444, hmc_network_show, NULL);
122 
123 static struct attribute *ocf_attrs[] = {
124 	&cpc_name_attr.attr,
125 	&hmc_network_attr.attr,
126 	NULL,
127 };
128 
129 static struct attribute_group ocf_attr_group = {
130 	.attrs = ocf_attrs,
131 };
132 
133 static int __init ocf_init(void)
134 {
135 	int rc;
136 
137 	INIT_WORK(&sclp_ocf_change_work, sclp_ocf_change_notify);
138 	ocf_kset = kset_create_and_add("ocf", NULL, firmware_kobj);
139 	if (!ocf_kset)
140 		return -ENOMEM;
141 
142 	rc = sysfs_create_group(&ocf_kset->kobj, &ocf_attr_group);
143 	if (rc) {
144 		kset_unregister(ocf_kset);
145 		return rc;
146 	}
147 
148 	return sclp_register(&sclp_ocf_event);
149 }
150 
151 device_initcall(ocf_init);
152