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;
31 struct list_head list;
32 struct monwrite_hdr hdr;
38 struct list_head list;
39 struct monwrite_hdr hdr;
42 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.applid == monhdr->applid &&
77 entry->hdr.record_num == monhdr->record_num &&
78 entry->hdr.version == monhdr->version &&
79 entry->hdr.release == monhdr->release &&
80 entry->hdr.mod_level == monhdr->mod_level)
85 static int monwrite_new_hdr(struct mon_private *monpriv)
87 struct monwrite_hdr *monhdr = &monpriv->hdr;
88 struct mon_buf *monbuf;
91 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
92 monhdr->mon_function > MONWRITE_START_CONFIG ||
93 monhdr->hdrlen != sizeof(struct monwrite_hdr))
95 monbuf = monwrite_find_hdr(monpriv, monhdr);
97 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
98 monhdr->datalen = monbuf->hdr.datalen;
99 rc = monwrite_diag(monhdr, monbuf->data,
101 list_del(&monbuf->list);
102 monpriv->mon_buf_count--;
108 if (monpriv->mon_buf_count >= mon_max_bufs)
110 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
113 monbuf->data = kzalloc(monbuf->hdr.datalen,
114 GFP_KERNEL | GFP_DMA);
119 monbuf->hdr = *monhdr;
120 list_add_tail(&monbuf->list, &monpriv->list);
121 monpriv->mon_buf_count++;
123 monpriv->current_buf = monbuf;
127 static int monwrite_new_data(struct mon_private *monpriv)
129 struct monwrite_hdr *monhdr = &monpriv->hdr;
130 struct mon_buf *monbuf = monpriv->current_buf;
133 switch (monhdr->mon_function) {
134 case MONWRITE_START_INTERVAL:
135 if (!monbuf->diag_done) {
136 rc = monwrite_diag(monhdr, monbuf->data,
137 APPLDATA_START_INTERVAL_REC);
138 monbuf->diag_done = 1;
141 case MONWRITE_START_CONFIG:
142 if (!monbuf->diag_done) {
143 rc = monwrite_diag(monhdr, monbuf->data,
144 APPLDATA_START_CONFIG_REC);
145 monbuf->diag_done = 1;
148 case MONWRITE_GEN_EVENT:
149 rc = monwrite_diag(monhdr, monbuf->data,
150 APPLDATA_GEN_EVENT_REC);
151 list_del(&monpriv->current_buf->list);
152 kfree(monpriv->current_buf->data);
153 kfree(monpriv->current_buf);
154 monpriv->current_buf = NULL;
157 /* monhdr->mon_function is checked in monwrite_new_hdr */
167 static int monwrite_open(struct inode *inode, struct file *filp)
169 struct mon_private *monpriv;
171 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
174 INIT_LIST_HEAD(&monpriv->list);
175 monpriv->hdr_to_read = sizeof(monpriv->hdr);
176 filp->private_data = monpriv;
177 return nonseekable_open(inode, filp);
180 static int monwrite_close(struct inode *inode, struct file *filp)
182 struct mon_private *monpriv = filp->private_data;
183 struct mon_buf *entry, *next;
185 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
186 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
187 monwrite_diag(&entry->hdr, entry->data,
189 monpriv->mon_buf_count--;
190 list_del(&entry->list);
198 static ssize_t monwrite_write(struct file *filp, const char __user *data,
199 size_t count, loff_t *ppos)
201 struct mon_private *monpriv = filp->private_data;
206 for (written = 0; written < count; ) {
207 if (monpriv->hdr_to_read) {
208 len = min(count - written, monpriv->hdr_to_read);
209 to = (char *) &monpriv->hdr +
210 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
211 if (copy_from_user(to, data + written, len)) {
215 monpriv->hdr_to_read -= len;
217 if (monpriv->hdr_to_read > 0)
219 rc = monwrite_new_hdr(monpriv);
222 monpriv->data_to_read = monpriv->current_buf ?
223 monpriv->current_buf->hdr.datalen : 0;
226 if (monpriv->data_to_read) {
227 len = min(count - written, monpriv->data_to_read);
228 to = monpriv->current_buf->data +
229 monpriv->hdr.datalen - monpriv->data_to_read;
230 if (copy_from_user(to, data + written, len)) {
234 monpriv->data_to_read -= len;
236 if (monpriv->data_to_read > 0)
238 rc = monwrite_new_data(monpriv);
242 monpriv->hdr_to_read = sizeof(monpriv->hdr);
247 monpriv->data_to_read = 0;
248 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
252 static struct file_operations monwrite_fops = {
253 .owner = THIS_MODULE,
254 .open = &monwrite_open,
255 .release = &monwrite_close,
256 .write = &monwrite_write,
259 static struct miscdevice mon_dev = {
261 .fops = &monwrite_fops,
262 .minor = MISC_DYNAMIC_MINOR,
269 static int __init mon_init(void)
272 return misc_register(&mon_dev);
277 static void __exit mon_exit(void)
279 WARN_ON(misc_deregister(&mon_dev) != 0);
282 module_init(mon_init);
283 module_exit(mon_exit);
285 module_param_named(max_bufs, mon_max_bufs, int, 0644);
286 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers"
287 "that can be active at one time");
289 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
290 MODULE_DESCRIPTION("Character device driver for writing z/VM "
291 "APPLDATA monitor records.");
292 MODULE_LICENSE("GPL");