Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / s390 / char / monwriter.c
bloba86c0534cd49f337dbceb8ef2dab3ac2e0115688
1 /*
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>
9 */
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>
23 #include <asm/io.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;
32 struct mon_buf {
33 struct list_head list;
34 struct monwrite_hdr hdr;
35 int diag_done;
36 char *data;
39 struct mon_private {
40 struct list_head list;
41 struct monwrite_hdr hdr;
42 size_t hdr_to_read;
43 size_t data_to_read;
44 struct mon_buf *current_buf;
45 struct mutex thread_mutex;
49 * helper functions
52 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
54 struct appldata_product_id id;
55 int rc;
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);
64 if (rc <= 0)
65 return rc;
66 if (rc == 5)
67 return -EPERM;
68 printk("DIAG X'DC' error with return code: %i\n", rc);
69 return -EINVAL;
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)
85 return entry;
87 return NULL;
90 static int monwrite_new_hdr(struct mon_private *monpriv)
92 struct monwrite_hdr *monhdr = &monpriv->hdr;
93 struct mon_buf *monbuf;
94 int rc;
96 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
97 monhdr->mon_function > MONWRITE_START_CONFIG ||
98 monhdr->hdrlen != sizeof(struct monwrite_hdr))
99 return -EINVAL;
100 monbuf = NULL;
101 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
102 monbuf = monwrite_find_hdr(monpriv, monhdr);
103 if (monbuf) {
104 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
105 monhdr->datalen = monbuf->hdr.datalen;
106 rc = monwrite_diag(monhdr, monbuf->data,
107 APPLDATA_STOP_REC);
108 list_del(&monbuf->list);
109 mon_buf_count--;
110 kfree(monbuf->data);
111 kfree(monbuf);
112 monbuf = NULL;
114 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
115 if (mon_buf_count >= mon_max_bufs)
116 return -ENOSPC;
117 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
118 if (!monbuf)
119 return -ENOMEM;
120 monbuf->data = kzalloc(monhdr->datalen,
121 GFP_KERNEL | GFP_DMA);
122 if (!monbuf->data) {
123 kfree(monbuf);
124 return -ENOMEM;
126 monbuf->hdr = *monhdr;
127 list_add_tail(&monbuf->list, &monpriv->list);
128 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
129 mon_buf_count++;
131 monpriv->current_buf = monbuf;
132 return 0;
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;
139 int rc = 0;
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;
148 break;
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;
155 break;
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;
163 break;
164 default:
165 /* monhdr->mon_function is checked in monwrite_new_hdr */
166 BUG();
168 return rc;
172 * file operations
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);
180 if (!monpriv)
181 return -ENOMEM;
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,
197 APPLDATA_STOP_REC);
198 mon_buf_count--;
199 list_del(&entry->list);
200 kfree(entry->data);
201 kfree(entry);
203 kfree(monpriv);
204 return 0;
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;
211 size_t len, written;
212 void *to;
213 int rc;
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)) {
222 rc = -EFAULT;
223 goto out_error;
225 monpriv->hdr_to_read -= len;
226 written += len;
227 if (monpriv->hdr_to_read > 0)
228 continue;
229 rc = monwrite_new_hdr(monpriv);
230 if (rc)
231 goto out_error;
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)) {
241 rc = -EFAULT;
242 goto out_error;
244 monpriv->data_to_read -= len;
245 written += len;
246 if (monpriv->data_to_read > 0)
247 continue;
248 rc = monwrite_new_data(monpriv);
249 if (rc)
250 goto out_error;
252 monpriv->hdr_to_read = sizeof(monpriv->hdr);
254 mutex_unlock(&monpriv->thread_mutex);
255 return written;
257 out_error:
258 monpriv->data_to_read = 0;
259 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
260 mutex_unlock(&monpriv->thread_mutex);
261 return rc;
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 = {
272 .name = "monwriter",
273 .fops = &monwrite_fops,
274 .minor = MISC_DYNAMIC_MINOR,
278 * module init/exit
281 static int __init mon_init(void)
283 if (MACHINE_IS_VM)
284 return misc_register(&mon_dev);
285 else
286 return -ENODEV;
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");