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