1 // SPDX-License-Identifier: GPL-2.0
3 * Character device driver for writing z/VM *MONITOR service records.
5 * Copyright IBM Corp. 2006, 2009
7 * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
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_product_id id;
64 strncpy(id.prod_nr, "LNXAPPL", 7);
65 id.prod_fn = myhdr->applid;
66 id.record_nr = myhdr->record_num;
67 id.version_nr = myhdr->version;
68 id.release_nr = myhdr->release;
69 id.mod_lvl = myhdr->mod_level;
70 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
73 pr_err("Writing monitor data failed with rc=%i\n", rc);
79 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
80 struct monwrite_hdr *monhdr)
82 struct mon_buf *entry, *next;
84 list_for_each_entry_safe(entry, next, &monpriv->list, list)
85 if ((entry->hdr.mon_function == monhdr->mon_function ||
86 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
87 entry->hdr.applid == monhdr->applid &&
88 entry->hdr.record_num == monhdr->record_num &&
89 entry->hdr.version == monhdr->version &&
90 entry->hdr.release == monhdr->release &&
91 entry->hdr.mod_level == monhdr->mod_level)
97 static int monwrite_new_hdr(struct mon_private *monpriv)
99 struct monwrite_hdr *monhdr = &monpriv->hdr;
100 struct mon_buf *monbuf;
103 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
104 monhdr->mon_function > MONWRITE_START_CONFIG ||
105 monhdr->hdrlen != sizeof(struct monwrite_hdr))
108 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
109 monbuf = monwrite_find_hdr(monpriv, monhdr);
111 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
112 monhdr->datalen = monbuf->hdr.datalen;
113 rc = monwrite_diag(monhdr, monbuf->data,
115 list_del(&monbuf->list);
121 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
122 if (mon_buf_count >= mon_max_bufs)
124 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
127 monbuf->data = kzalloc(monhdr->datalen,
128 GFP_KERNEL | GFP_DMA);
133 monbuf->hdr = *monhdr;
134 list_add_tail(&monbuf->list, &monpriv->list);
135 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
138 monpriv->current_buf = monbuf;
142 static int monwrite_new_data(struct mon_private *monpriv)
144 struct monwrite_hdr *monhdr = &monpriv->hdr;
145 struct mon_buf *monbuf = monpriv->current_buf;
148 switch (monhdr->mon_function) {
149 case MONWRITE_START_INTERVAL:
150 if (!monbuf->diag_done) {
151 rc = monwrite_diag(monhdr, monbuf->data,
152 APPLDATA_START_INTERVAL_REC);
153 monbuf->diag_done = 1;
156 case MONWRITE_START_CONFIG:
157 if (!monbuf->diag_done) {
158 rc = monwrite_diag(monhdr, monbuf->data,
159 APPLDATA_START_CONFIG_REC);
160 monbuf->diag_done = 1;
163 case MONWRITE_GEN_EVENT:
164 rc = monwrite_diag(monhdr, monbuf->data,
165 APPLDATA_GEN_EVENT_REC);
166 list_del(&monpriv->current_buf->list);
167 kfree(monpriv->current_buf->data);
168 kfree(monpriv->current_buf);
169 monpriv->current_buf = NULL;
172 /* monhdr->mon_function is checked in monwrite_new_hdr */
182 static int monwrite_open(struct inode *inode, struct file *filp)
184 struct mon_private *monpriv;
186 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
189 INIT_LIST_HEAD(&monpriv->list);
190 monpriv->hdr_to_read = sizeof(monpriv->hdr);
191 mutex_init(&monpriv->thread_mutex);
192 filp->private_data = monpriv;
193 list_add_tail(&monpriv->priv_list, &mon_priv_list);
194 return nonseekable_open(inode, filp);
197 static int monwrite_close(struct inode *inode, struct file *filp)
199 struct mon_private *monpriv = filp->private_data;
200 struct mon_buf *entry, *next;
202 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
203 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
204 monwrite_diag(&entry->hdr, entry->data,
207 list_del(&entry->list);
211 list_del(&monpriv->priv_list);
216 static ssize_t monwrite_write(struct file *filp, const char __user *data,
217 size_t count, loff_t *ppos)
219 struct mon_private *monpriv = filp->private_data;
224 mutex_lock(&monpriv->thread_mutex);
225 for (written = 0; written < count; ) {
226 if (monpriv->hdr_to_read) {
227 len = min(count - written, monpriv->hdr_to_read);
228 to = (char *) &monpriv->hdr +
229 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
230 if (copy_from_user(to, data + written, len)) {
234 monpriv->hdr_to_read -= len;
236 if (monpriv->hdr_to_read > 0)
238 rc = monwrite_new_hdr(monpriv);
241 monpriv->data_to_read = monpriv->current_buf ?
242 monpriv->current_buf->hdr.datalen : 0;
245 if (monpriv->data_to_read) {
246 len = min(count - written, monpriv->data_to_read);
247 to = monpriv->current_buf->data +
248 monpriv->hdr.datalen - monpriv->data_to_read;
249 if (copy_from_user(to, data + written, len)) {
253 monpriv->data_to_read -= len;
255 if (monpriv->data_to_read > 0)
257 rc = monwrite_new_data(monpriv);
261 monpriv->hdr_to_read = sizeof(monpriv->hdr);
263 mutex_unlock(&monpriv->thread_mutex);
267 monpriv->data_to_read = 0;
268 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
269 mutex_unlock(&monpriv->thread_mutex);
273 static const struct file_operations monwrite_fops = {
274 .owner = THIS_MODULE,
275 .open = &monwrite_open,
276 .release = &monwrite_close,
277 .write = &monwrite_write,
278 .llseek = noop_llseek,
281 static struct miscdevice mon_dev = {
283 .fops = &monwrite_fops,
284 .minor = MISC_DYNAMIC_MINOR,
291 static int monwriter_freeze(struct device *dev)
293 struct mon_private *monpriv;
294 struct mon_buf *monbuf;
296 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
297 list_for_each_entry(monbuf, &monpriv->list, list) {
298 if (monbuf->hdr.mon_function != MONWRITE_GEN_EVENT)
299 monwrite_diag(&monbuf->hdr, monbuf->data,
306 static int monwriter_restore(struct device *dev)
308 struct mon_private *monpriv;
309 struct mon_buf *monbuf;
311 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
312 list_for_each_entry(monbuf, &monpriv->list, list) {
313 if (monbuf->hdr.mon_function == MONWRITE_START_INTERVAL)
314 monwrite_diag(&monbuf->hdr, monbuf->data,
315 APPLDATA_START_INTERVAL_REC);
316 if (monbuf->hdr.mon_function == MONWRITE_START_CONFIG)
317 monwrite_diag(&monbuf->hdr, monbuf->data,
318 APPLDATA_START_CONFIG_REC);
324 static int monwriter_thaw(struct device *dev)
326 return monwriter_restore(dev);
329 static const struct dev_pm_ops monwriter_pm_ops = {
330 .freeze = monwriter_freeze,
331 .thaw = monwriter_thaw,
332 .restore = monwriter_restore,
335 static struct platform_driver monwriter_pdrv = {
338 .pm = &monwriter_pm_ops,
342 static struct platform_device *monwriter_pdev;
348 static int __init mon_init(void)
355 rc = platform_driver_register(&monwriter_pdrv);
359 monwriter_pdev = platform_device_register_simple("monwriter", -1, NULL,
361 if (IS_ERR(monwriter_pdev)) {
362 rc = PTR_ERR(monwriter_pdev);
367 * misc_register() has to be the last action in module_init(), because
368 * file operations will be available right after this.
370 rc = misc_register(&mon_dev);
376 platform_device_unregister(monwriter_pdev);
378 platform_driver_unregister(&monwriter_pdrv);
382 static void __exit mon_exit(void)
384 misc_deregister(&mon_dev);
385 platform_device_unregister(monwriter_pdev);
386 platform_driver_unregister(&monwriter_pdrv);
389 module_init(mon_init);
390 module_exit(mon_exit);
392 module_param_named(max_bufs, mon_max_bufs, int, 0644);
393 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
394 "that can be active at one time");
396 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
397 MODULE_DESCRIPTION("Character device driver for writing z/VM "
398 "APPLDATA monitor records.");
399 MODULE_LICENSE("GPL");