1 // SPDX-License-Identifier: GPL-2.0
3 * Character device driver for writing z/VM *MONITOR service records.
5 * Copyright IBM Corp. 2006, 2009
10 #define KMSG_COMPONENT "monwriter"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/miscdevice.h>
20 #include <linux/ctype.h>
21 #include <linux/poll.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <asm/ebcdic.h>
28 #include <asm/appldata.h>
29 #include <asm/monwriter.h>
31 #define MONWRITE_MAX_DATALEN 4010
33 static int mon_max_bufs = 255;
34 static int mon_buf_count;
37 struct list_head list;
38 struct monwrite_hdr hdr;
43 static LIST_HEAD(mon_priv_list);
46 struct list_head priv_list;
47 struct list_head list;
48 struct monwrite_hdr hdr;
51 struct mon_buf *current_buf;
52 struct mutex thread_mutex;
59 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
61 struct appldata_parameter_list *parm_list;
62 struct appldata_product_id *id;
65 id = kmalloc(sizeof(*id), GFP_KERNEL);
66 parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
68 if (!id || !parm_list)
70 memcpy(id->prod_nr, "LNXAPPL", 7);
71 id->prod_fn = myhdr->applid;
72 id->record_nr = myhdr->record_num;
73 id->version_nr = myhdr->version;
74 id->release_nr = myhdr->release;
75 id->mod_lvl = myhdr->mod_level;
76 rc = appldata_asm(parm_list, id, fcn,
77 (void *) buffer, myhdr->datalen);
80 pr_err("Writing monitor data failed with rc=%i\n", rc);
81 rc = (rc == 5) ? -EPERM : -EINVAL;
88 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
89 struct monwrite_hdr *monhdr)
91 struct mon_buf *entry, *next;
93 list_for_each_entry_safe(entry, next, &monpriv->list, list)
94 if ((entry->hdr.mon_function == monhdr->mon_function ||
95 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
96 entry->hdr.applid == monhdr->applid &&
97 entry->hdr.record_num == monhdr->record_num &&
98 entry->hdr.version == monhdr->version &&
99 entry->hdr.release == monhdr->release &&
100 entry->hdr.mod_level == monhdr->mod_level)
106 static int monwrite_new_hdr(struct mon_private *monpriv)
108 struct monwrite_hdr *monhdr = &monpriv->hdr;
109 struct mon_buf *monbuf;
112 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
113 monhdr->mon_function > MONWRITE_START_CONFIG ||
114 monhdr->hdrlen != sizeof(struct monwrite_hdr))
117 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
118 monbuf = monwrite_find_hdr(monpriv, monhdr);
120 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
121 monhdr->datalen = monbuf->hdr.datalen;
122 rc = monwrite_diag(monhdr, monbuf->data,
124 list_del(&monbuf->list);
130 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
131 if (mon_buf_count >= mon_max_bufs)
133 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
136 monbuf->data = kzalloc(monhdr->datalen,
137 GFP_KERNEL | GFP_DMA);
142 monbuf->hdr = *monhdr;
143 list_add_tail(&monbuf->list, &monpriv->list);
144 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
147 monpriv->current_buf = monbuf;
151 static int monwrite_new_data(struct mon_private *monpriv)
153 struct monwrite_hdr *monhdr = &monpriv->hdr;
154 struct mon_buf *monbuf = monpriv->current_buf;
157 switch (monhdr->mon_function) {
158 case MONWRITE_START_INTERVAL:
159 if (!monbuf->diag_done) {
160 rc = monwrite_diag(monhdr, monbuf->data,
161 APPLDATA_START_INTERVAL_REC);
162 monbuf->diag_done = 1;
165 case MONWRITE_START_CONFIG:
166 if (!monbuf->diag_done) {
167 rc = monwrite_diag(monhdr, monbuf->data,
168 APPLDATA_START_CONFIG_REC);
169 monbuf->diag_done = 1;
172 case MONWRITE_GEN_EVENT:
173 rc = monwrite_diag(monhdr, monbuf->data,
174 APPLDATA_GEN_EVENT_REC);
175 list_del(&monpriv->current_buf->list);
176 kfree(monpriv->current_buf->data);
177 kfree(monpriv->current_buf);
178 monpriv->current_buf = NULL;
181 /* monhdr->mon_function is checked in monwrite_new_hdr */
191 static int monwrite_open(struct inode *inode, struct file *filp)
193 struct mon_private *monpriv;
195 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
198 INIT_LIST_HEAD(&monpriv->list);
199 monpriv->hdr_to_read = sizeof(monpriv->hdr);
200 mutex_init(&monpriv->thread_mutex);
201 filp->private_data = monpriv;
202 list_add_tail(&monpriv->priv_list, &mon_priv_list);
203 return nonseekable_open(inode, filp);
206 static int monwrite_close(struct inode *inode, struct file *filp)
208 struct mon_private *monpriv = filp->private_data;
209 struct mon_buf *entry, *next;
211 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
212 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
213 monwrite_diag(&entry->hdr, entry->data,
216 list_del(&entry->list);
220 list_del(&monpriv->priv_list);
225 static ssize_t monwrite_write(struct file *filp, const char __user *data,
226 size_t count, loff_t *ppos)
228 struct mon_private *monpriv = filp->private_data;
233 mutex_lock(&monpriv->thread_mutex);
234 for (written = 0; written < count; ) {
235 if (monpriv->hdr_to_read) {
236 len = min(count - written, monpriv->hdr_to_read);
237 to = (char *) &monpriv->hdr +
238 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
239 if (copy_from_user(to, data + written, len)) {
243 monpriv->hdr_to_read -= len;
245 if (monpriv->hdr_to_read > 0)
247 rc = monwrite_new_hdr(monpriv);
250 monpriv->data_to_read = monpriv->current_buf ?
251 monpriv->current_buf->hdr.datalen : 0;
254 if (monpriv->data_to_read) {
255 len = min(count - written, monpriv->data_to_read);
256 to = monpriv->current_buf->data +
257 monpriv->hdr.datalen - monpriv->data_to_read;
258 if (copy_from_user(to, data + written, len)) {
262 monpriv->data_to_read -= len;
264 if (monpriv->data_to_read > 0)
266 rc = monwrite_new_data(monpriv);
270 monpriv->hdr_to_read = sizeof(monpriv->hdr);
272 mutex_unlock(&monpriv->thread_mutex);
276 monpriv->data_to_read = 0;
277 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
278 mutex_unlock(&monpriv->thread_mutex);
282 static const struct file_operations monwrite_fops = {
283 .owner = THIS_MODULE,
284 .open = &monwrite_open,
285 .release = &monwrite_close,
286 .write = &monwrite_write,
287 .llseek = noop_llseek,
290 static struct miscdevice mon_dev = {
292 .fops = &monwrite_fops,
293 .minor = MISC_DYNAMIC_MINOR,
300 static int monwriter_freeze(struct device *dev)
302 struct mon_private *monpriv;
303 struct mon_buf *monbuf;
305 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
306 list_for_each_entry(monbuf, &monpriv->list, list) {
307 if (monbuf->hdr.mon_function != MONWRITE_GEN_EVENT)
308 monwrite_diag(&monbuf->hdr, monbuf->data,
315 static int monwriter_restore(struct device *dev)
317 struct mon_private *monpriv;
318 struct mon_buf *monbuf;
320 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
321 list_for_each_entry(monbuf, &monpriv->list, list) {
322 if (monbuf->hdr.mon_function == MONWRITE_START_INTERVAL)
323 monwrite_diag(&monbuf->hdr, monbuf->data,
324 APPLDATA_START_INTERVAL_REC);
325 if (monbuf->hdr.mon_function == MONWRITE_START_CONFIG)
326 monwrite_diag(&monbuf->hdr, monbuf->data,
327 APPLDATA_START_CONFIG_REC);
333 static int monwriter_thaw(struct device *dev)
335 return monwriter_restore(dev);
338 static const struct dev_pm_ops monwriter_pm_ops = {
339 .freeze = monwriter_freeze,
340 .thaw = monwriter_thaw,
341 .restore = monwriter_restore,
344 static struct platform_driver monwriter_pdrv = {
347 .pm = &monwriter_pm_ops,
351 static struct platform_device *monwriter_pdev;
357 static int __init mon_init(void)
364 rc = platform_driver_register(&monwriter_pdrv);
368 monwriter_pdev = platform_device_register_simple("monwriter", -1, NULL,
370 if (IS_ERR(monwriter_pdev)) {
371 rc = PTR_ERR(monwriter_pdev);
376 * misc_register() has to be the last action in module_init(), because
377 * file operations will be available right after this.
379 rc = misc_register(&mon_dev);
385 platform_device_unregister(monwriter_pdev);
387 platform_driver_unregister(&monwriter_pdrv);
391 static void __exit mon_exit(void)
393 misc_deregister(&mon_dev);
394 platform_device_unregister(monwriter_pdev);
395 platform_driver_unregister(&monwriter_pdrv);
398 module_init(mon_init);
399 module_exit(mon_exit);
401 module_param_named(max_bufs, mon_max_bufs, int, 0644);
402 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
403 "that can be active at one time");
406 MODULE_DESCRIPTION("Character device driver for writing z/VM "
407 "APPLDATA monitor records.");
408 MODULE_LICENSE("GPL");