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 <linux/mutex.h>
21 #include <asm/uaccess.h>
22 #include <asm/ebcdic.h>
24 #include <asm/appldata.h>
25 #include <asm/monwriter.h>
27 #define MONWRITE_MAX_DATALEN 4010
29 static int mon_max_bufs = 255;
30 static int mon_buf_count;
33 struct list_head list;
34 struct monwrite_hdr hdr;
40 struct list_head list;
41 struct monwrite_hdr hdr;
44 struct mon_buf *current_buf;
45 struct mutex thread_mutex;
52 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
54 struct appldata_product_id id;
57 strcpy(id.prod_nr, "LNXAPPL");
58 id.prod_fn = myhdr->applid;
59 id.record_nr = myhdr->record_num;
60 id.version_nr = myhdr->version;
61 id.release_nr = myhdr->release;
62 id.mod_lvl = myhdr->mod_level;
63 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
68 printk("DIAG X'DC' error with return code: %i\n", rc);
72 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
73 struct monwrite_hdr *monhdr)
75 struct mon_buf *entry, *next;
77 list_for_each_entry_safe(entry, next, &monpriv->list, list)
78 if ((entry->hdr.mon_function == monhdr->mon_function ||
79 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
80 entry->hdr.applid == monhdr->applid &&
81 entry->hdr.record_num == monhdr->record_num &&
82 entry->hdr.version == monhdr->version &&
83 entry->hdr.release == monhdr->release &&
84 entry->hdr.mod_level == monhdr->mod_level)
90 static int monwrite_new_hdr(struct mon_private *monpriv)
92 struct monwrite_hdr *monhdr = &monpriv->hdr;
93 struct mon_buf *monbuf;
96 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
97 monhdr->mon_function > MONWRITE_START_CONFIG ||
98 monhdr->hdrlen != sizeof(struct monwrite_hdr))
101 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
102 monbuf = monwrite_find_hdr(monpriv, monhdr);
104 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
105 monhdr->datalen = monbuf->hdr.datalen;
106 rc = monwrite_diag(monhdr, monbuf->data,
108 list_del(&monbuf->list);
114 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
115 if (mon_buf_count >= mon_max_bufs)
117 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
120 monbuf->data = kzalloc(monhdr->datalen,
121 GFP_KERNEL | GFP_DMA);
126 monbuf->hdr = *monhdr;
127 list_add_tail(&monbuf->list, &monpriv->list);
128 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
131 monpriv->current_buf = monbuf;
135 static int monwrite_new_data(struct mon_private *monpriv)
137 struct monwrite_hdr *monhdr = &monpriv->hdr;
138 struct mon_buf *monbuf = monpriv->current_buf;
141 switch (monhdr->mon_function) {
142 case MONWRITE_START_INTERVAL:
143 if (!monbuf->diag_done) {
144 rc = monwrite_diag(monhdr, monbuf->data,
145 APPLDATA_START_INTERVAL_REC);
146 monbuf->diag_done = 1;
149 case MONWRITE_START_CONFIG:
150 if (!monbuf->diag_done) {
151 rc = monwrite_diag(monhdr, monbuf->data,
152 APPLDATA_START_CONFIG_REC);
153 monbuf->diag_done = 1;
156 case MONWRITE_GEN_EVENT:
157 rc = monwrite_diag(monhdr, monbuf->data,
158 APPLDATA_GEN_EVENT_REC);
159 list_del(&monpriv->current_buf->list);
160 kfree(monpriv->current_buf->data);
161 kfree(monpriv->current_buf);
162 monpriv->current_buf = NULL;
165 /* monhdr->mon_function is checked in monwrite_new_hdr */
175 static int monwrite_open(struct inode *inode, struct file *filp)
177 struct mon_private *monpriv;
179 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
182 INIT_LIST_HEAD(&monpriv->list);
183 monpriv->hdr_to_read = sizeof(monpriv->hdr);
184 mutex_init(&monpriv->thread_mutex);
185 filp->private_data = monpriv;
186 return nonseekable_open(inode, filp);
189 static int monwrite_close(struct inode *inode, struct file *filp)
191 struct mon_private *monpriv = filp->private_data;
192 struct mon_buf *entry, *next;
194 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
195 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
196 monwrite_diag(&entry->hdr, entry->data,
199 list_del(&entry->list);
207 static ssize_t monwrite_write(struct file *filp, const char __user *data,
208 size_t count, loff_t *ppos)
210 struct mon_private *monpriv = filp->private_data;
215 mutex_lock(&monpriv->thread_mutex);
216 for (written = 0; written < count; ) {
217 if (monpriv->hdr_to_read) {
218 len = min(count - written, monpriv->hdr_to_read);
219 to = (char *) &monpriv->hdr +
220 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
221 if (copy_from_user(to, data + written, len)) {
225 monpriv->hdr_to_read -= len;
227 if (monpriv->hdr_to_read > 0)
229 rc = monwrite_new_hdr(monpriv);
232 monpriv->data_to_read = monpriv->current_buf ?
233 monpriv->current_buf->hdr.datalen : 0;
236 if (monpriv->data_to_read) {
237 len = min(count - written, monpriv->data_to_read);
238 to = monpriv->current_buf->data +
239 monpriv->hdr.datalen - monpriv->data_to_read;
240 if (copy_from_user(to, data + written, len)) {
244 monpriv->data_to_read -= len;
246 if (monpriv->data_to_read > 0)
248 rc = monwrite_new_data(monpriv);
252 monpriv->hdr_to_read = sizeof(monpriv->hdr);
254 mutex_unlock(&monpriv->thread_mutex);
258 monpriv->data_to_read = 0;
259 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
260 mutex_unlock(&monpriv->thread_mutex);
264 static const struct file_operations monwrite_fops = {
265 .owner = THIS_MODULE,
266 .open = &monwrite_open,
267 .release = &monwrite_close,
268 .write = &monwrite_write,
271 static struct miscdevice mon_dev = {
273 .fops = &monwrite_fops,
274 .minor = MISC_DYNAMIC_MINOR,
281 static int __init mon_init(void)
284 return misc_register(&mon_dev);
289 static void __exit mon_exit(void)
291 WARN_ON(misc_deregister(&mon_dev) != 0);
294 module_init(mon_init);
295 module_exit(mon_exit);
297 module_param_named(max_bufs, mon_max_bufs, int, 0644);
298 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
299 "that can be active at one time");
301 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
302 MODULE_DESCRIPTION("Character device driver for writing z/VM "
303 "APPLDATA monitor records.");
304 MODULE_LICENSE("GPL");