2 * drivers/s390/char/monwriter.c
4 * Character device driver for writing z/VM *MONITOR service records.
6 * Copyright (C) IBM Corp. 2006
8 * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/ctype.h>
19 #include <linux/poll.h>
20 #include <asm/uaccess.h>
21 #include <asm/ebcdic.h>
23 #include <asm/appldata.h>
24 #include <asm/monwriter.h>
26 #define MONWRITE_MAX_DATALEN 4024
28 static int mon_max_bufs = 255;
29 static int mon_buf_count;
32 struct list_head list;
33 struct monwrite_hdr hdr;
39 struct list_head list;
40 struct monwrite_hdr hdr;
43 struct mon_buf *current_buf;
50 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
52 struct appldata_product_id id;
55 strcpy(id.prod_nr, "LNXAPPL");
56 id.prod_fn = myhdr->applid;
57 id.record_nr = myhdr->record_num;
58 id.version_nr = myhdr->version;
59 id.release_nr = myhdr->release;
60 id.mod_lvl = myhdr->mod_level;
61 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
66 printk("DIAG X'DC' error with return code: %i\n", rc);
70 static inline struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
71 struct monwrite_hdr *monhdr)
73 struct mon_buf *entry, *next;
75 list_for_each_entry_safe(entry, next, &monpriv->list, list)
76 if ((entry->hdr.mon_function == monhdr->mon_function ||
77 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
78 entry->hdr.applid == monhdr->applid &&
79 entry->hdr.record_num == monhdr->record_num &&
80 entry->hdr.version == monhdr->version &&
81 entry->hdr.release == monhdr->release &&
82 entry->hdr.mod_level == monhdr->mod_level)
88 static int monwrite_new_hdr(struct mon_private *monpriv)
90 struct monwrite_hdr *monhdr = &monpriv->hdr;
91 struct mon_buf *monbuf;
94 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
95 monhdr->mon_function > MONWRITE_START_CONFIG ||
96 monhdr->hdrlen != sizeof(struct monwrite_hdr))
99 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
100 monbuf = monwrite_find_hdr(monpriv, monhdr);
102 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
103 monhdr->datalen = monbuf->hdr.datalen;
104 rc = monwrite_diag(monhdr, monbuf->data,
106 list_del(&monbuf->list);
112 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
113 if (mon_buf_count >= mon_max_bufs)
115 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
118 monbuf->data = kzalloc(monhdr->datalen,
119 GFP_KERNEL | GFP_DMA);
124 monbuf->hdr = *monhdr;
125 list_add_tail(&monbuf->list, &monpriv->list);
126 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
129 monpriv->current_buf = monbuf;
133 static int monwrite_new_data(struct mon_private *monpriv)
135 struct monwrite_hdr *monhdr = &monpriv->hdr;
136 struct mon_buf *monbuf = monpriv->current_buf;
139 switch (monhdr->mon_function) {
140 case MONWRITE_START_INTERVAL:
141 if (!monbuf->diag_done) {
142 rc = monwrite_diag(monhdr, monbuf->data,
143 APPLDATA_START_INTERVAL_REC);
144 monbuf->diag_done = 1;
147 case MONWRITE_START_CONFIG:
148 if (!monbuf->diag_done) {
149 rc = monwrite_diag(monhdr, monbuf->data,
150 APPLDATA_START_CONFIG_REC);
151 monbuf->diag_done = 1;
154 case MONWRITE_GEN_EVENT:
155 rc = monwrite_diag(monhdr, monbuf->data,
156 APPLDATA_GEN_EVENT_REC);
157 list_del(&monpriv->current_buf->list);
158 kfree(monpriv->current_buf->data);
159 kfree(monpriv->current_buf);
160 monpriv->current_buf = NULL;
163 /* monhdr->mon_function is checked in monwrite_new_hdr */
173 static int monwrite_open(struct inode *inode, struct file *filp)
175 struct mon_private *monpriv;
177 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
180 INIT_LIST_HEAD(&monpriv->list);
181 monpriv->hdr_to_read = sizeof(monpriv->hdr);
182 filp->private_data = monpriv;
183 return nonseekable_open(inode, filp);
186 static int monwrite_close(struct inode *inode, struct file *filp)
188 struct mon_private *monpriv = filp->private_data;
189 struct mon_buf *entry, *next;
191 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
192 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
193 monwrite_diag(&entry->hdr, entry->data,
196 list_del(&entry->list);
204 static ssize_t monwrite_write(struct file *filp, const char __user *data,
205 size_t count, loff_t *ppos)
207 struct mon_private *monpriv = filp->private_data;
212 for (written = 0; written < count; ) {
213 if (monpriv->hdr_to_read) {
214 len = min(count - written, monpriv->hdr_to_read);
215 to = (char *) &monpriv->hdr +
216 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
217 if (copy_from_user(to, data + written, len)) {
221 monpriv->hdr_to_read -= len;
223 if (monpriv->hdr_to_read > 0)
225 rc = monwrite_new_hdr(monpriv);
228 monpriv->data_to_read = monpriv->current_buf ?
229 monpriv->current_buf->hdr.datalen : 0;
232 if (monpriv->data_to_read) {
233 len = min(count - written, monpriv->data_to_read);
234 to = monpriv->current_buf->data +
235 monpriv->hdr.datalen - monpriv->data_to_read;
236 if (copy_from_user(to, data + written, len)) {
240 monpriv->data_to_read -= len;
242 if (monpriv->data_to_read > 0)
244 rc = monwrite_new_data(monpriv);
248 monpriv->hdr_to_read = sizeof(monpriv->hdr);
253 monpriv->data_to_read = 0;
254 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
258 static struct file_operations monwrite_fops = {
259 .owner = THIS_MODULE,
260 .open = &monwrite_open,
261 .release = &monwrite_close,
262 .write = &monwrite_write,
265 static struct miscdevice mon_dev = {
267 .fops = &monwrite_fops,
268 .minor = MISC_DYNAMIC_MINOR,
275 static int __init mon_init(void)
278 return misc_register(&mon_dev);
283 static void __exit mon_exit(void)
285 WARN_ON(misc_deregister(&mon_dev) != 0);
288 module_init(mon_init);
289 module_exit(mon_exit);
291 module_param_named(max_bufs, mon_max_bufs, int, 0644);
292 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers"
293 "that can be active at one time");
295 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
296 MODULE_DESCRIPTION("Character device driver for writing z/VM "
297 "APPLDATA monitor records.");
298 MODULE_LICENSE("GPL");