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/smp_lock.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 <asm/uaccess.h>
23 #include <asm/ebcdic.h>
25 #include <asm/appldata.h>
26 #include <asm/monwriter.h>
28 #define MONWRITE_MAX_DATALEN 4010
30 static int mon_max_bufs = 255;
31 static int mon_buf_count;
34 struct list_head list;
35 struct monwrite_hdr hdr;
41 struct list_head list;
42 struct monwrite_hdr hdr;
45 struct mon_buf *current_buf;
46 struct mutex thread_mutex;
53 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
55 struct appldata_product_id id;
58 strcpy(id.prod_nr, "LNXAPPL");
59 id.prod_fn = myhdr->applid;
60 id.record_nr = myhdr->record_num;
61 id.version_nr = myhdr->version;
62 id.release_nr = myhdr->release;
63 id.mod_lvl = myhdr->mod_level;
64 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
69 printk("DIAG X'DC' error with return code: %i\n", rc);
73 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
74 struct monwrite_hdr *monhdr)
76 struct mon_buf *entry, *next;
78 list_for_each_entry_safe(entry, next, &monpriv->list, list)
79 if ((entry->hdr.mon_function == monhdr->mon_function ||
80 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
81 entry->hdr.applid == monhdr->applid &&
82 entry->hdr.record_num == monhdr->record_num &&
83 entry->hdr.version == monhdr->version &&
84 entry->hdr.release == monhdr->release &&
85 entry->hdr.mod_level == monhdr->mod_level)
91 static int monwrite_new_hdr(struct mon_private *monpriv)
93 struct monwrite_hdr *monhdr = &monpriv->hdr;
94 struct mon_buf *monbuf;
97 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
98 monhdr->mon_function > MONWRITE_START_CONFIG ||
99 monhdr->hdrlen != sizeof(struct monwrite_hdr))
102 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
103 monbuf = monwrite_find_hdr(monpriv, monhdr);
105 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
106 monhdr->datalen = monbuf->hdr.datalen;
107 rc = monwrite_diag(monhdr, monbuf->data,
109 list_del(&monbuf->list);
115 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
116 if (mon_buf_count >= mon_max_bufs)
118 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
121 monbuf->data = kzalloc(monhdr->datalen,
122 GFP_KERNEL | GFP_DMA);
127 monbuf->hdr = *monhdr;
128 list_add_tail(&monbuf->list, &monpriv->list);
129 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
132 monpriv->current_buf = monbuf;
136 static int monwrite_new_data(struct mon_private *monpriv)
138 struct monwrite_hdr *monhdr = &monpriv->hdr;
139 struct mon_buf *monbuf = monpriv->current_buf;
142 switch (monhdr->mon_function) {
143 case MONWRITE_START_INTERVAL:
144 if (!monbuf->diag_done) {
145 rc = monwrite_diag(monhdr, monbuf->data,
146 APPLDATA_START_INTERVAL_REC);
147 monbuf->diag_done = 1;
150 case MONWRITE_START_CONFIG:
151 if (!monbuf->diag_done) {
152 rc = monwrite_diag(monhdr, monbuf->data,
153 APPLDATA_START_CONFIG_REC);
154 monbuf->diag_done = 1;
157 case MONWRITE_GEN_EVENT:
158 rc = monwrite_diag(monhdr, monbuf->data,
159 APPLDATA_GEN_EVENT_REC);
160 list_del(&monpriv->current_buf->list);
161 kfree(monpriv->current_buf->data);
162 kfree(monpriv->current_buf);
163 monpriv->current_buf = NULL;
166 /* monhdr->mon_function is checked in monwrite_new_hdr */
176 static int monwrite_open(struct inode *inode, struct file *filp)
178 struct mon_private *monpriv;
180 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
184 INIT_LIST_HEAD(&monpriv->list);
185 monpriv->hdr_to_read = sizeof(monpriv->hdr);
186 mutex_init(&monpriv->thread_mutex);
187 filp->private_data = monpriv;
189 return nonseekable_open(inode, filp);
192 static int monwrite_close(struct inode *inode, struct file *filp)
194 struct mon_private *monpriv = filp->private_data;
195 struct mon_buf *entry, *next;
197 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
198 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
199 monwrite_diag(&entry->hdr, entry->data,
202 list_del(&entry->list);
210 static ssize_t monwrite_write(struct file *filp, const char __user *data,
211 size_t count, loff_t *ppos)
213 struct mon_private *monpriv = filp->private_data;
218 mutex_lock(&monpriv->thread_mutex);
219 for (written = 0; written < count; ) {
220 if (monpriv->hdr_to_read) {
221 len = min(count - written, monpriv->hdr_to_read);
222 to = (char *) &monpriv->hdr +
223 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
224 if (copy_from_user(to, data + written, len)) {
228 monpriv->hdr_to_read -= len;
230 if (monpriv->hdr_to_read > 0)
232 rc = monwrite_new_hdr(monpriv);
235 monpriv->data_to_read = monpriv->current_buf ?
236 monpriv->current_buf->hdr.datalen : 0;
239 if (monpriv->data_to_read) {
240 len = min(count - written, monpriv->data_to_read);
241 to = monpriv->current_buf->data +
242 monpriv->hdr.datalen - monpriv->data_to_read;
243 if (copy_from_user(to, data + written, len)) {
247 monpriv->data_to_read -= len;
249 if (monpriv->data_to_read > 0)
251 rc = monwrite_new_data(monpriv);
255 monpriv->hdr_to_read = sizeof(monpriv->hdr);
257 mutex_unlock(&monpriv->thread_mutex);
261 monpriv->data_to_read = 0;
262 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
263 mutex_unlock(&monpriv->thread_mutex);
267 static const struct file_operations monwrite_fops = {
268 .owner = THIS_MODULE,
269 .open = &monwrite_open,
270 .release = &monwrite_close,
271 .write = &monwrite_write,
274 static struct miscdevice mon_dev = {
276 .fops = &monwrite_fops,
277 .minor = MISC_DYNAMIC_MINOR,
284 static int __init mon_init(void)
287 return misc_register(&mon_dev);
292 static void __exit mon_exit(void)
294 WARN_ON(misc_deregister(&mon_dev) != 0);
297 module_init(mon_init);
298 module_exit(mon_exit);
300 module_param_named(max_bufs, mon_max_bufs, int, 0644);
301 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
302 "that can be active at one time");
304 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
305 MODULE_DESCRIPTION("Character device driver for writing z/VM "
306 "APPLDATA monitor records.");
307 MODULE_LICENSE("GPL");