2 * Character device driver for writing z/VM *MONITOR service records.
4 * Copyright IBM Corp. 2006, 2009
6 * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
9 #define KMSG_COMPONENT "monwriter"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/errno.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 <linux/platform_device.h>
23 #include <asm/uaccess.h>
24 #include <asm/ebcdic.h>
26 #include <asm/appldata.h>
27 #include <asm/monwriter.h>
29 #define MONWRITE_MAX_DATALEN 4010
31 static int mon_max_bufs = 255;
32 static int mon_buf_count;
35 struct list_head list;
36 struct monwrite_hdr hdr;
41 static LIST_HEAD(mon_priv_list);
44 struct list_head priv_list;
45 struct list_head list;
46 struct monwrite_hdr hdr;
49 struct mon_buf *current_buf;
50 struct mutex thread_mutex;
57 static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
59 struct appldata_product_id id;
62 strcpy(id.prod_nr, "LNXAPPL");
63 id.prod_fn = myhdr->applid;
64 id.record_nr = myhdr->record_num;
65 id.version_nr = myhdr->version;
66 id.release_nr = myhdr->release;
67 id.mod_lvl = myhdr->mod_level;
68 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
71 pr_err("Writing monitor data failed with rc=%i\n", rc);
77 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
78 struct monwrite_hdr *monhdr)
80 struct mon_buf *entry, *next;
82 list_for_each_entry_safe(entry, next, &monpriv->list, list)
83 if ((entry->hdr.mon_function == monhdr->mon_function ||
84 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
85 entry->hdr.applid == monhdr->applid &&
86 entry->hdr.record_num == monhdr->record_num &&
87 entry->hdr.version == monhdr->version &&
88 entry->hdr.release == monhdr->release &&
89 entry->hdr.mod_level == monhdr->mod_level)
95 static int monwrite_new_hdr(struct mon_private *monpriv)
97 struct monwrite_hdr *monhdr = &monpriv->hdr;
98 struct mon_buf *monbuf;
101 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
102 monhdr->mon_function > MONWRITE_START_CONFIG ||
103 monhdr->hdrlen != sizeof(struct monwrite_hdr))
106 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
107 monbuf = monwrite_find_hdr(monpriv, monhdr);
109 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
110 monhdr->datalen = monbuf->hdr.datalen;
111 rc = monwrite_diag(monhdr, monbuf->data,
113 list_del(&monbuf->list);
119 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
120 if (mon_buf_count >= mon_max_bufs)
122 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
125 monbuf->data = kzalloc(monhdr->datalen,
126 GFP_KERNEL | GFP_DMA);
131 monbuf->hdr = *monhdr;
132 list_add_tail(&monbuf->list, &monpriv->list);
133 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
136 monpriv->current_buf = monbuf;
140 static int monwrite_new_data(struct mon_private *monpriv)
142 struct monwrite_hdr *monhdr = &monpriv->hdr;
143 struct mon_buf *monbuf = monpriv->current_buf;
146 switch (monhdr->mon_function) {
147 case MONWRITE_START_INTERVAL:
148 if (!monbuf->diag_done) {
149 rc = monwrite_diag(monhdr, monbuf->data,
150 APPLDATA_START_INTERVAL_REC);
151 monbuf->diag_done = 1;
154 case MONWRITE_START_CONFIG:
155 if (!monbuf->diag_done) {
156 rc = monwrite_diag(monhdr, monbuf->data,
157 APPLDATA_START_CONFIG_REC);
158 monbuf->diag_done = 1;
161 case MONWRITE_GEN_EVENT:
162 rc = monwrite_diag(monhdr, monbuf->data,
163 APPLDATA_GEN_EVENT_REC);
164 list_del(&monpriv->current_buf->list);
165 kfree(monpriv->current_buf->data);
166 kfree(monpriv->current_buf);
167 monpriv->current_buf = NULL;
170 /* monhdr->mon_function is checked in monwrite_new_hdr */
180 static int monwrite_open(struct inode *inode, struct file *filp)
182 struct mon_private *monpriv;
184 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
187 INIT_LIST_HEAD(&monpriv->list);
188 monpriv->hdr_to_read = sizeof(monpriv->hdr);
189 mutex_init(&monpriv->thread_mutex);
190 filp->private_data = monpriv;
191 list_add_tail(&monpriv->priv_list, &mon_priv_list);
192 return nonseekable_open(inode, filp);
195 static int monwrite_close(struct inode *inode, struct file *filp)
197 struct mon_private *monpriv = filp->private_data;
198 struct mon_buf *entry, *next;
200 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
201 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
202 monwrite_diag(&entry->hdr, entry->data,
205 list_del(&entry->list);
209 list_del(&monpriv->priv_list);
214 static ssize_t monwrite_write(struct file *filp, const char __user *data,
215 size_t count, loff_t *ppos)
217 struct mon_private *monpriv = filp->private_data;
222 mutex_lock(&monpriv->thread_mutex);
223 for (written = 0; written < count; ) {
224 if (monpriv->hdr_to_read) {
225 len = min(count - written, monpriv->hdr_to_read);
226 to = (char *) &monpriv->hdr +
227 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
228 if (copy_from_user(to, data + written, len)) {
232 monpriv->hdr_to_read -= len;
234 if (monpriv->hdr_to_read > 0)
236 rc = monwrite_new_hdr(monpriv);
239 monpriv->data_to_read = monpriv->current_buf ?
240 monpriv->current_buf->hdr.datalen : 0;
243 if (monpriv->data_to_read) {
244 len = min(count - written, monpriv->data_to_read);
245 to = monpriv->current_buf->data +
246 monpriv->hdr.datalen - monpriv->data_to_read;
247 if (copy_from_user(to, data + written, len)) {
251 monpriv->data_to_read -= len;
253 if (monpriv->data_to_read > 0)
255 rc = monwrite_new_data(monpriv);
259 monpriv->hdr_to_read = sizeof(monpriv->hdr);
261 mutex_unlock(&monpriv->thread_mutex);
265 monpriv->data_to_read = 0;
266 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
267 mutex_unlock(&monpriv->thread_mutex);
271 static const struct file_operations monwrite_fops = {
272 .owner = THIS_MODULE,
273 .open = &monwrite_open,
274 .release = &monwrite_close,
275 .write = &monwrite_write,
278 static struct miscdevice mon_dev = {
280 .fops = &monwrite_fops,
281 .minor = MISC_DYNAMIC_MINOR,
288 static int monwriter_freeze(struct device *dev)
290 struct mon_private *monpriv;
291 struct mon_buf *monbuf;
293 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
294 list_for_each_entry(monbuf, &monpriv->list, list) {
295 if (monbuf->hdr.mon_function != MONWRITE_GEN_EVENT)
296 monwrite_diag(&monbuf->hdr, monbuf->data,
303 static int monwriter_restore(struct device *dev)
305 struct mon_private *monpriv;
306 struct mon_buf *monbuf;
308 list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
309 list_for_each_entry(monbuf, &monpriv->list, list) {
310 if (monbuf->hdr.mon_function == MONWRITE_START_INTERVAL)
311 monwrite_diag(&monbuf->hdr, monbuf->data,
312 APPLDATA_START_INTERVAL_REC);
313 if (monbuf->hdr.mon_function == MONWRITE_START_CONFIG)
314 monwrite_diag(&monbuf->hdr, monbuf->data,
315 APPLDATA_START_CONFIG_REC);
321 static int monwriter_thaw(struct device *dev)
323 return monwriter_restore(dev);
326 static const struct dev_pm_ops monwriter_pm_ops = {
327 .freeze = monwriter_freeze,
328 .thaw = monwriter_thaw,
329 .restore = monwriter_restore,
332 static struct platform_driver monwriter_pdrv = {
335 .owner = THIS_MODULE,
336 .pm = &monwriter_pm_ops,
340 static struct platform_device *monwriter_pdev;
346 static int __init mon_init(void)
353 rc = platform_driver_register(&monwriter_pdrv);
357 monwriter_pdev = platform_device_register_simple("monwriter", -1, NULL,
359 if (IS_ERR(monwriter_pdev)) {
360 rc = PTR_ERR(monwriter_pdev);
365 * misc_register() has to be the last action in module_init(), because
366 * file operations will be available right after this.
368 rc = misc_register(&mon_dev);
374 platform_device_unregister(monwriter_pdev);
376 platform_driver_unregister(&monwriter_pdrv);
380 static void __exit mon_exit(void)
382 WARN_ON(misc_deregister(&mon_dev) != 0);
383 platform_device_unregister(monwriter_pdev);
384 platform_driver_unregister(&monwriter_pdrv);
387 module_init(mon_init);
388 module_exit(mon_exit);
390 module_param_named(max_bufs, mon_max_bufs, int, 0644);
391 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
392 "that can be active at one time");
394 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
395 MODULE_DESCRIPTION("Character device driver for writing z/VM "
396 "APPLDATA monitor records.");
397 MODULE_LICENSE("GPL");