xref: /linux/drivers/s390/char/sclp_config.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007
4  */
5 
6 #define pr_fmt(fmt) "sclp_config: " fmt
7 
8 #include <linux/init.h>
9 #include <linux/errno.h>
10 #include <linux/cpu.h>
11 #include <linux/device.h>
12 #include <linux/workqueue.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <asm/smp.h>
16 
17 #include "sclp.h"
18 
19 struct conf_mgm_data {
20 	u8 reserved;
21 	u8 ev_qualifier;
22 } __attribute__((packed));
23 
24 #define OFB_DATA_MAX 64
25 
26 struct sclp_ofb_evbuf {
27 	struct evbuf_header header;
28 	struct conf_mgm_data cm_data;
29 	char ev_data[OFB_DATA_MAX];
30 } __packed;
31 
32 struct sclp_ofb_sccb {
33 	struct sccb_header header;
34 	struct sclp_ofb_evbuf ofb_evbuf;
35 } __packed;
36 
37 #define EV_QUAL_CPU_CHANGE	1
38 #define EV_QUAL_CAP_CHANGE	3
39 #define EV_QUAL_OPEN4BUSINESS	5
40 
41 static struct work_struct sclp_cpu_capability_work;
42 static struct work_struct sclp_cpu_change_work;
43 
44 static void sclp_cpu_capability_notify(struct work_struct *work)
45 {
46 	int cpu;
47 	struct device *dev;
48 
49 	s390_update_cpu_mhz();
50 	pr_info("CPU capability may have changed\n");
51 	cpus_read_lock();
52 	for_each_online_cpu(cpu) {
53 		dev = get_cpu_device(cpu);
54 		kobject_uevent(&dev->kobj, KOBJ_CHANGE);
55 	}
56 	cpus_read_unlock();
57 }
58 
59 static void __ref sclp_cpu_change_notify(struct work_struct *work)
60 {
61 	lock_device_hotplug();
62 	smp_rescan_cpus(false);
63 	unlock_device_hotplug();
64 }
65 
66 static void sclp_conf_receiver_fn(struct evbuf_header *evbuf)
67 {
68 	struct conf_mgm_data *cdata;
69 
70 	cdata = (struct conf_mgm_data *)(evbuf + 1);
71 	switch (cdata->ev_qualifier) {
72 	case EV_QUAL_CPU_CHANGE:
73 		schedule_work(&sclp_cpu_change_work);
74 		break;
75 	case EV_QUAL_CAP_CHANGE:
76 		schedule_work(&sclp_cpu_capability_work);
77 		break;
78 	}
79 }
80 
81 static struct sclp_register sclp_conf_register =
82 {
83 	.send_mask    = EVTYP_CONFMGMDATA_MASK,
84 	.receive_mask = EVTYP_CONFMGMDATA_MASK,
85 	.receiver_fn  = sclp_conf_receiver_fn,
86 };
87 
88 static int sclp_ofb_send_req(char *ev_data, size_t len)
89 {
90 	static DEFINE_MUTEX(send_mutex);
91 	struct sclp_ofb_sccb *sccb;
92 	int rc, response;
93 
94 	if (len > OFB_DATA_MAX)
95 		return -EINVAL;
96 	sccb = (struct sclp_ofb_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
97 	if (!sccb)
98 		return -ENOMEM;
99 	/* Setup SCCB for Control-Program Identification */
100 	sccb->header.length = sizeof(struct sclp_ofb_sccb);
101 	sccb->ofb_evbuf.header.length = sizeof(struct sclp_ofb_evbuf);
102 	sccb->ofb_evbuf.header.type = EVTYP_CONFMGMDATA;
103 	sccb->ofb_evbuf.cm_data.ev_qualifier = EV_QUAL_OPEN4BUSINESS;
104 	memcpy(sccb->ofb_evbuf.ev_data, ev_data, len);
105 
106 	if (!(sclp_conf_register.sclp_receive_mask & EVTYP_CONFMGMDATA_MASK))
107 		pr_warn("SCLP receiver did not register to receive "
108 			"Configuration Management Data Events.\n");
109 
110 	mutex_lock(&send_mutex);
111 	rc = sclp_sync_request(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
112 	mutex_unlock(&send_mutex);
113 	if (rc)
114 		goto out;
115 	response = sccb->header.response_code;
116 	if (response != 0x0020) {
117 		pr_err("Open for Business request failed with response code "
118 		       "0x%04x\n", response);
119 		rc = -EIO;
120 	}
121 out:
122 	free_page((unsigned long)sccb);
123 	return rc;
124 }
125 
126 static ssize_t sysfs_ofb_data_write(struct file *filp, struct kobject *kobj,
127 				    const struct bin_attribute *bin_attr,
128 				    char *buf, loff_t off, size_t count)
129 {
130 	int rc;
131 
132 	rc = sclp_ofb_send_req(buf, count);
133 	return rc ?: count;
134 }
135 
136 static const struct bin_attribute ofb_bin_attr = {
137 	.attr = {
138 		.name = "event_data",
139 		.mode = S_IWUSR,
140 	},
141 	.write = sysfs_ofb_data_write,
142 };
143 
144 static int __init sclp_ofb_setup(void)
145 {
146 	struct kset *ofb_kset;
147 	int rc;
148 
149 	ofb_kset = kset_create_and_add("ofb", NULL, firmware_kobj);
150 	if (!ofb_kset)
151 		return -ENOMEM;
152 	rc = sysfs_create_bin_file(&ofb_kset->kobj, &ofb_bin_attr);
153 	if (rc) {
154 		kset_unregister(ofb_kset);
155 		return rc;
156 	}
157 	return 0;
158 }
159 
160 static int __init sclp_conf_init(void)
161 {
162 	int rc;
163 
164 	INIT_WORK(&sclp_cpu_capability_work, sclp_cpu_capability_notify);
165 	INIT_WORK(&sclp_cpu_change_work, sclp_cpu_change_notify);
166 	rc = sclp_register(&sclp_conf_register);
167 	if (rc)
168 		return rc;
169 	return sclp_ofb_setup();
170 }
171 
172 __initcall(sclp_conf_init);
173