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 #define KMSG_COMPONENT "monwriter"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/smp_lock.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/miscdevice.h>
22 #include <linux/ctype.h>
23 #include <linux/poll.h>
24 #include <linux/mutex.h>
25 #include <asm/uaccess.h>
26 #include <asm/ebcdic.h>
28 #include <asm/appldata.h>
29 #include <asm/monwriter.h>
31 #define MONWRITE_MAX_DATALEN 4010
33 static int mon_max_bufs
= 255;
34 static int mon_buf_count
;
37 struct list_head list
;
38 struct monwrite_hdr hdr
;
44 struct list_head list
;
45 struct monwrite_hdr hdr
;
48 struct mon_buf
*current_buf
;
49 struct mutex thread_mutex
;
56 static int monwrite_diag(struct monwrite_hdr
*myhdr
, char *buffer
, int fcn
)
58 struct appldata_product_id id
;
61 strcpy(id
.prod_nr
, "LNXAPPL");
62 id
.prod_fn
= myhdr
->applid
;
63 id
.record_nr
= myhdr
->record_num
;
64 id
.version_nr
= myhdr
->version
;
65 id
.release_nr
= myhdr
->release
;
66 id
.mod_lvl
= myhdr
->mod_level
;
67 rc
= appldata_asm(&id
, fcn
, (void *) buffer
, myhdr
->datalen
);
70 pr_err("Writing monitor data failed with rc=%i\n", rc
);
76 static struct mon_buf
*monwrite_find_hdr(struct mon_private
*monpriv
,
77 struct monwrite_hdr
*monhdr
)
79 struct mon_buf
*entry
, *next
;
81 list_for_each_entry_safe(entry
, next
, &monpriv
->list
, list
)
82 if ((entry
->hdr
.mon_function
== monhdr
->mon_function
||
83 monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) &&
84 entry
->hdr
.applid
== monhdr
->applid
&&
85 entry
->hdr
.record_num
== monhdr
->record_num
&&
86 entry
->hdr
.version
== monhdr
->version
&&
87 entry
->hdr
.release
== monhdr
->release
&&
88 entry
->hdr
.mod_level
== monhdr
->mod_level
)
94 static int monwrite_new_hdr(struct mon_private
*monpriv
)
96 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
97 struct mon_buf
*monbuf
;
100 if (monhdr
->datalen
> MONWRITE_MAX_DATALEN
||
101 monhdr
->mon_function
> MONWRITE_START_CONFIG
||
102 monhdr
->hdrlen
!= sizeof(struct monwrite_hdr
))
105 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
106 monbuf
= monwrite_find_hdr(monpriv
, monhdr
);
108 if (monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) {
109 monhdr
->datalen
= monbuf
->hdr
.datalen
;
110 rc
= monwrite_diag(monhdr
, monbuf
->data
,
112 list_del(&monbuf
->list
);
118 } else if (monhdr
->mon_function
!= MONWRITE_STOP_INTERVAL
) {
119 if (mon_buf_count
>= mon_max_bufs
)
121 monbuf
= kzalloc(sizeof(struct mon_buf
), GFP_KERNEL
);
124 monbuf
->data
= kzalloc(monhdr
->datalen
,
125 GFP_KERNEL
| GFP_DMA
);
130 monbuf
->hdr
= *monhdr
;
131 list_add_tail(&monbuf
->list
, &monpriv
->list
);
132 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
135 monpriv
->current_buf
= monbuf
;
139 static int monwrite_new_data(struct mon_private
*monpriv
)
141 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
142 struct mon_buf
*monbuf
= monpriv
->current_buf
;
145 switch (monhdr
->mon_function
) {
146 case MONWRITE_START_INTERVAL
:
147 if (!monbuf
->diag_done
) {
148 rc
= monwrite_diag(monhdr
, monbuf
->data
,
149 APPLDATA_START_INTERVAL_REC
);
150 monbuf
->diag_done
= 1;
153 case MONWRITE_START_CONFIG
:
154 if (!monbuf
->diag_done
) {
155 rc
= monwrite_diag(monhdr
, monbuf
->data
,
156 APPLDATA_START_CONFIG_REC
);
157 monbuf
->diag_done
= 1;
160 case MONWRITE_GEN_EVENT
:
161 rc
= monwrite_diag(monhdr
, monbuf
->data
,
162 APPLDATA_GEN_EVENT_REC
);
163 list_del(&monpriv
->current_buf
->list
);
164 kfree(monpriv
->current_buf
->data
);
165 kfree(monpriv
->current_buf
);
166 monpriv
->current_buf
= NULL
;
169 /* monhdr->mon_function is checked in monwrite_new_hdr */
179 static int monwrite_open(struct inode
*inode
, struct file
*filp
)
181 struct mon_private
*monpriv
;
183 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
;
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
);
213 static ssize_t
monwrite_write(struct file
*filp
, const char __user
*data
,
214 size_t count
, loff_t
*ppos
)
216 struct mon_private
*monpriv
= filp
->private_data
;
221 mutex_lock(&monpriv
->thread_mutex
);
222 for (written
= 0; written
< count
; ) {
223 if (monpriv
->hdr_to_read
) {
224 len
= min(count
- written
, monpriv
->hdr_to_read
);
225 to
= (char *) &monpriv
->hdr
+
226 sizeof(monpriv
->hdr
) - monpriv
->hdr_to_read
;
227 if (copy_from_user(to
, data
+ written
, len
)) {
231 monpriv
->hdr_to_read
-= len
;
233 if (monpriv
->hdr_to_read
> 0)
235 rc
= monwrite_new_hdr(monpriv
);
238 monpriv
->data_to_read
= monpriv
->current_buf
?
239 monpriv
->current_buf
->hdr
.datalen
: 0;
242 if (monpriv
->data_to_read
) {
243 len
= min(count
- written
, monpriv
->data_to_read
);
244 to
= monpriv
->current_buf
->data
+
245 monpriv
->hdr
.datalen
- monpriv
->data_to_read
;
246 if (copy_from_user(to
, data
+ written
, len
)) {
250 monpriv
->data_to_read
-= len
;
252 if (monpriv
->data_to_read
> 0)
254 rc
= monwrite_new_data(monpriv
);
258 monpriv
->hdr_to_read
= sizeof(monpriv
->hdr
);
260 mutex_unlock(&monpriv
->thread_mutex
);
264 monpriv
->data_to_read
= 0;
265 monpriv
->hdr_to_read
= sizeof(struct monwrite_hdr
);
266 mutex_unlock(&monpriv
->thread_mutex
);
270 static const struct file_operations monwrite_fops
= {
271 .owner
= THIS_MODULE
,
272 .open
= &monwrite_open
,
273 .release
= &monwrite_close
,
274 .write
= &monwrite_write
,
277 static struct miscdevice mon_dev
= {
279 .fops
= &monwrite_fops
,
280 .minor
= MISC_DYNAMIC_MINOR
,
287 static int __init
mon_init(void)
290 return misc_register(&mon_dev
);
295 static void __exit
mon_exit(void)
297 WARN_ON(misc_deregister(&mon_dev
) != 0);
300 module_init(mon_init
);
301 module_exit(mon_exit
);
303 module_param_named(max_bufs
, mon_max_bufs
, int, 0644);
304 MODULE_PARM_DESC(max_bufs
, "Maximum number of sample monitor data buffers "
305 "that can be active at one time");
307 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
308 MODULE_DESCRIPTION("Character device driver for writing z/VM "
309 "APPLDATA monitor records.");
310 MODULE_LICENSE("GPL");