xref: /linux/drivers/s390/char/monwriter.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Character device driver for writing z/VM *MONITOR service records.
4  *
5  * Copyright IBM Corp. 2006, 2009
6  *
7  * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
8  */
9 
10 #define pr_fmt(fmt) "monwriter: " fmt
11 
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/ctype.h>
20 #include <linux/poll.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/io.h>
25 #include <asm/machine.h>
26 #include <asm/ebcdic.h>
27 #include <asm/appldata.h>
28 #include <asm/monwriter.h>
29 
30 #define MONWRITE_MAX_DATALEN	4010
31 
32 static int mon_max_bufs = 255;
33 static int mon_buf_count;
34 
35 struct mon_buf {
36 	struct list_head list;
37 	struct monwrite_hdr hdr;
38 	int diag_done;
39 	char *data;
40 };
41 
42 struct mon_private {
43 	struct list_head list;
44 	struct monwrite_hdr hdr;
45 	size_t hdr_to_read;
46 	size_t data_to_read;
47 	struct mon_buf *current_buf;
48 	struct mutex thread_mutex;
49 };
50 
51 /*
52  * helper functions
53  */
54 
monwrite_diag(struct monwrite_hdr * myhdr,char * buffer,int fcn)55 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
56 {
57 	struct appldata_parameter_list *parm_list;
58 	struct appldata_product_id *id;
59 	int rc;
60 
61 	id = kmalloc_obj(*id);
62 	parm_list = kmalloc_obj(*parm_list);
63 	rc = -ENOMEM;
64 	if (!id || !parm_list)
65 		goto out;
66 	memcpy(id->prod_nr, "LNXAPPL", 7);
67 	id->prod_fn = myhdr->applid;
68 	id->record_nr = myhdr->record_num;
69 	id->version_nr = myhdr->version;
70 	id->release_nr = myhdr->release;
71 	id->mod_lvl = myhdr->mod_level;
72 	rc = appldata_asm(parm_list, id, fcn,
73 			  (void *) buffer, myhdr->datalen);
74 	if (rc <= 0)
75 		goto out;
76 	pr_err("Writing monitor data failed with rc=%i\n", rc);
77 	rc = (rc == 5) ? -EPERM : -EINVAL;
78 out:
79 	kfree(id);
80 	kfree(parm_list);
81 	return rc;
82 }
83 
monwrite_find_hdr(struct mon_private * monpriv,struct monwrite_hdr * monhdr)84 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
85 					 struct monwrite_hdr *monhdr)
86 {
87 	struct mon_buf *entry, *next;
88 
89 	list_for_each_entry_safe(entry, next, &monpriv->list, list)
90 		if ((entry->hdr.mon_function == monhdr->mon_function ||
91 		     monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
92 		    entry->hdr.applid == monhdr->applid &&
93 		    entry->hdr.record_num == monhdr->record_num &&
94 		    entry->hdr.version == monhdr->version &&
95 		    entry->hdr.release == monhdr->release &&
96 		    entry->hdr.mod_level == monhdr->mod_level)
97 			return entry;
98 
99 	return NULL;
100 }
101 
monwrite_new_hdr(struct mon_private * monpriv)102 static int monwrite_new_hdr(struct mon_private *monpriv)
103 {
104 	struct monwrite_hdr *monhdr = &monpriv->hdr;
105 	struct mon_buf *monbuf;
106 	int rc = 0;
107 
108 	if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
109 	    monhdr->mon_function > MONWRITE_START_CONFIG ||
110 	    monhdr->hdrlen != sizeof(struct monwrite_hdr))
111 		return -EINVAL;
112 	monbuf = NULL;
113 	if (monhdr->mon_function != MONWRITE_GEN_EVENT)
114 		monbuf = monwrite_find_hdr(monpriv, monhdr);
115 	if (monbuf) {
116 		if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
117 			monhdr->datalen = monbuf->hdr.datalen;
118 			rc = monwrite_diag(monhdr, monbuf->data,
119 					   APPLDATA_STOP_REC);
120 			list_del(&monbuf->list);
121 			mon_buf_count--;
122 			kfree(monbuf->data);
123 			kfree(monbuf);
124 			monbuf = NULL;
125 		} else if (monbuf->hdr.datalen != monhdr->datalen) {
126 			/* Data with buffer reuse must not change its length */
127 			return -EINVAL;
128 		}
129 	} else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
130 		if (mon_buf_count >= mon_max_bufs)
131 			return -ENOSPC;
132 		monbuf = kzalloc_obj(struct mon_buf);
133 		if (!monbuf)
134 			return -ENOMEM;
135 		monbuf->data = kzalloc(monhdr->datalen,
136 				       GFP_KERNEL | GFP_DMA);
137 		if (!monbuf->data) {
138 			kfree(monbuf);
139 			return -ENOMEM;
140 		}
141 		monbuf->hdr = *monhdr;
142 		list_add_tail(&monbuf->list, &monpriv->list);
143 		if (monhdr->mon_function != MONWRITE_GEN_EVENT)
144 			mon_buf_count++;
145 	}
146 	monpriv->current_buf = monbuf;
147 	return rc;
148 }
149 
monwrite_new_data(struct mon_private * monpriv)150 static int monwrite_new_data(struct mon_private *monpriv)
151 {
152 	struct monwrite_hdr *monhdr = &monpriv->hdr;
153 	struct mon_buf *monbuf = monpriv->current_buf;
154 	int rc = 0;
155 
156 	switch (monhdr->mon_function) {
157 	case MONWRITE_START_INTERVAL:
158 		if (!monbuf->diag_done) {
159 			rc = monwrite_diag(monhdr, monbuf->data,
160 					   APPLDATA_START_INTERVAL_REC);
161 			monbuf->diag_done = 1;
162 		}
163 		break;
164 	case MONWRITE_START_CONFIG:
165 		if (!monbuf->diag_done) {
166 			rc = monwrite_diag(monhdr, monbuf->data,
167 					   APPLDATA_START_CONFIG_REC);
168 			monbuf->diag_done = 1;
169 		}
170 		break;
171 	case MONWRITE_GEN_EVENT:
172 		rc = monwrite_diag(monhdr, monbuf->data,
173 				   APPLDATA_GEN_EVENT_REC);
174 		list_del(&monpriv->current_buf->list);
175 		kfree(monpriv->current_buf->data);
176 		kfree(monpriv->current_buf);
177 		monpriv->current_buf = NULL;
178 		break;
179 	default:
180 		/* monhdr->mon_function is checked in monwrite_new_hdr */
181 		BUG();
182 	}
183 	return rc;
184 }
185 
186 /*
187  * file operations
188  */
189 
monwrite_open(struct inode * inode,struct file * filp)190 static int monwrite_open(struct inode *inode, struct file *filp)
191 {
192 	struct mon_private *monpriv;
193 
194 	monpriv = kzalloc_obj(struct mon_private);
195 	if (!monpriv)
196 		return -ENOMEM;
197 	INIT_LIST_HEAD(&monpriv->list);
198 	monpriv->hdr_to_read = sizeof(monpriv->hdr);
199 	mutex_init(&monpriv->thread_mutex);
200 	filp->private_data = monpriv;
201 	return nonseekable_open(inode, filp);
202 }
203 
monwrite_close(struct inode * inode,struct file * filp)204 static int monwrite_close(struct inode *inode, struct file *filp)
205 {
206 	struct mon_private *monpriv = filp->private_data;
207 	struct mon_buf *entry, *next;
208 
209 	list_for_each_entry_safe(entry, next, &monpriv->list, list) {
210 		if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
211 			monwrite_diag(&entry->hdr, entry->data,
212 				      APPLDATA_STOP_REC);
213 		mon_buf_count--;
214 		list_del(&entry->list);
215 		kfree(entry->data);
216 		kfree(entry);
217 	}
218 	kfree(monpriv);
219 	return 0;
220 }
221 
monwrite_write(struct file * filp,const char __user * data,size_t count,loff_t * ppos)222 static ssize_t monwrite_write(struct file *filp, const char __user *data,
223 			      size_t count, loff_t *ppos)
224 {
225 	struct mon_private *monpriv = filp->private_data;
226 	size_t len, written;
227 	void *to;
228 	int rc;
229 
230 	mutex_lock(&monpriv->thread_mutex);
231 	for (written = 0; written < count; ) {
232 		if (monpriv->hdr_to_read) {
233 			len = min(count - written, monpriv->hdr_to_read);
234 			to = (char *) &monpriv->hdr +
235 				sizeof(monpriv->hdr) - monpriv->hdr_to_read;
236 			if (copy_from_user(to, data + written, len)) {
237 				rc = -EFAULT;
238 				goto out_error;
239 			}
240 			monpriv->hdr_to_read -= len;
241 			written += len;
242 			if (monpriv->hdr_to_read > 0)
243 				continue;
244 			rc = monwrite_new_hdr(monpriv);
245 			if (rc)
246 				goto out_error;
247 			monpriv->data_to_read = monpriv->current_buf ?
248 				monpriv->current_buf->hdr.datalen : 0;
249 		}
250 
251 		if (monpriv->data_to_read) {
252 			len = min(count - written, monpriv->data_to_read);
253 			to = monpriv->current_buf->data +
254 				monpriv->hdr.datalen - monpriv->data_to_read;
255 			if (copy_from_user(to, data + written, len)) {
256 				rc = -EFAULT;
257 				goto out_error;
258 			}
259 			monpriv->data_to_read -= len;
260 			written += len;
261 			if (monpriv->data_to_read > 0)
262 				continue;
263 			rc = monwrite_new_data(monpriv);
264 			if (rc)
265 				goto out_error;
266 		}
267 		monpriv->hdr_to_read = sizeof(monpriv->hdr);
268 	}
269 	mutex_unlock(&monpriv->thread_mutex);
270 	return written;
271 
272 out_error:
273 	monpriv->data_to_read = 0;
274 	monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
275 	mutex_unlock(&monpriv->thread_mutex);
276 	return rc;
277 }
278 
279 static const struct file_operations monwrite_fops = {
280 	.owner	 = THIS_MODULE,
281 	.open	 = &monwrite_open,
282 	.release = &monwrite_close,
283 	.write	 = &monwrite_write,
284 	.llseek  = noop_llseek,
285 };
286 
287 static struct miscdevice mon_dev = {
288 	.name	= "monwriter",
289 	.fops	= &monwrite_fops,
290 	.minor	= MISC_DYNAMIC_MINOR,
291 };
292 
293 /*
294  * module init/exit
295  */
296 
mon_init(void)297 static int __init mon_init(void)
298 {
299 	if (!machine_is_vm())
300 		return -ENODEV;
301 	/*
302 	 * misc_register() has to be the last action in module_init(), because
303 	 * file operations will be available right after this.
304 	 */
305 	return misc_register(&mon_dev);
306 }
307 
mon_exit(void)308 static void __exit mon_exit(void)
309 {
310 	misc_deregister(&mon_dev);
311 }
312 
313 module_init(mon_init);
314 module_exit(mon_exit);
315 
316 module_param_named(max_bufs, mon_max_bufs, int, 0644);
317 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
318 		 "that can be active at one time");
319 
320 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
321 MODULE_DESCRIPTION("Character device driver for writing z/VM "
322 		   "APPLDATA monitor records.");
323 MODULE_LICENSE("GPL");
324