1 // SPDX-License-Identifier: GPL-2.0
3 * Character device driver for writing z/VM *MONITOR service records.
5 * Copyright IBM Corp. 2006, 2009
7 * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
10 #define KMSG_COMPONENT "monwriter"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/miscdevice.h>
20 #include <linux/ctype.h>
21 #include <linux/poll.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
26 #include <asm/ebcdic.h>
27 #include <asm/appldata.h>
28 #include <asm/monwriter.h>
30 #define MONWRITE_MAX_DATALEN 4010
32 static int mon_max_bufs
= 255;
33 static int mon_buf_count
;
36 struct list_head list
;
37 struct monwrite_hdr hdr
;
43 struct list_head list
;
44 struct monwrite_hdr hdr
;
47 struct mon_buf
*current_buf
;
48 struct mutex thread_mutex
;
55 static int monwrite_diag(struct monwrite_hdr
*myhdr
, char *buffer
, int fcn
)
57 struct appldata_parameter_list
*parm_list
;
58 struct appldata_product_id
*id
;
61 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
62 parm_list
= kmalloc(sizeof(*parm_list
), GFP_KERNEL
);
64 if (!id
|| !parm_list
)
66 memcpy(id
->prod_nr
, "LNXAPPL", 7);
67 id
->prod_fn
= myhdr
->applid
;
68 id
->record_nr
= myhdr
->record_num
;
69 id
->version_nr
= myhdr
->version
;
70 id
->release_nr
= myhdr
->release
;
71 id
->mod_lvl
= myhdr
->mod_level
;
72 rc
= appldata_asm(parm_list
, id
, fcn
,
73 (void *) buffer
, myhdr
->datalen
);
76 pr_err("Writing monitor data failed with rc=%i\n", rc
);
77 rc
= (rc
== 5) ? -EPERM
: -EINVAL
;
84 static struct mon_buf
*monwrite_find_hdr(struct mon_private
*monpriv
,
85 struct monwrite_hdr
*monhdr
)
87 struct mon_buf
*entry
, *next
;
89 list_for_each_entry_safe(entry
, next
, &monpriv
->list
, list
)
90 if ((entry
->hdr
.mon_function
== monhdr
->mon_function
||
91 monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) &&
92 entry
->hdr
.applid
== monhdr
->applid
&&
93 entry
->hdr
.record_num
== monhdr
->record_num
&&
94 entry
->hdr
.version
== monhdr
->version
&&
95 entry
->hdr
.release
== monhdr
->release
&&
96 entry
->hdr
.mod_level
== monhdr
->mod_level
)
102 static int monwrite_new_hdr(struct mon_private
*monpriv
)
104 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
105 struct mon_buf
*monbuf
;
108 if (monhdr
->datalen
> MONWRITE_MAX_DATALEN
||
109 monhdr
->mon_function
> MONWRITE_START_CONFIG
||
110 monhdr
->hdrlen
!= sizeof(struct monwrite_hdr
))
113 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
114 monbuf
= monwrite_find_hdr(monpriv
, monhdr
);
116 if (monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) {
117 monhdr
->datalen
= monbuf
->hdr
.datalen
;
118 rc
= monwrite_diag(monhdr
, monbuf
->data
,
120 list_del(&monbuf
->list
);
126 } else if (monhdr
->mon_function
!= MONWRITE_STOP_INTERVAL
) {
127 if (mon_buf_count
>= mon_max_bufs
)
129 monbuf
= kzalloc(sizeof(struct mon_buf
), GFP_KERNEL
);
132 monbuf
->data
= kzalloc(monhdr
->datalen
,
133 GFP_KERNEL
| GFP_DMA
);
138 monbuf
->hdr
= *monhdr
;
139 list_add_tail(&monbuf
->list
, &monpriv
->list
);
140 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
143 monpriv
->current_buf
= monbuf
;
147 static int monwrite_new_data(struct mon_private
*monpriv
)
149 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
150 struct mon_buf
*monbuf
= monpriv
->current_buf
;
153 switch (monhdr
->mon_function
) {
154 case MONWRITE_START_INTERVAL
:
155 if (!monbuf
->diag_done
) {
156 rc
= monwrite_diag(monhdr
, monbuf
->data
,
157 APPLDATA_START_INTERVAL_REC
);
158 monbuf
->diag_done
= 1;
161 case MONWRITE_START_CONFIG
:
162 if (!monbuf
->diag_done
) {
163 rc
= monwrite_diag(monhdr
, monbuf
->data
,
164 APPLDATA_START_CONFIG_REC
);
165 monbuf
->diag_done
= 1;
168 case MONWRITE_GEN_EVENT
:
169 rc
= monwrite_diag(monhdr
, monbuf
->data
,
170 APPLDATA_GEN_EVENT_REC
);
171 list_del(&monpriv
->current_buf
->list
);
172 kfree(monpriv
->current_buf
->data
);
173 kfree(monpriv
->current_buf
);
174 monpriv
->current_buf
= NULL
;
177 /* monhdr->mon_function is checked in monwrite_new_hdr */
187 static int monwrite_open(struct inode
*inode
, struct file
*filp
)
189 struct mon_private
*monpriv
;
191 monpriv
= kzalloc(sizeof(struct mon_private
), GFP_KERNEL
);
194 INIT_LIST_HEAD(&monpriv
->list
);
195 monpriv
->hdr_to_read
= sizeof(monpriv
->hdr
);
196 mutex_init(&monpriv
->thread_mutex
);
197 filp
->private_data
= monpriv
;
198 return nonseekable_open(inode
, filp
);
201 static int monwrite_close(struct inode
*inode
, struct file
*filp
)
203 struct mon_private
*monpriv
= filp
->private_data
;
204 struct mon_buf
*entry
, *next
;
206 list_for_each_entry_safe(entry
, next
, &monpriv
->list
, list
) {
207 if (entry
->hdr
.mon_function
!= MONWRITE_GEN_EVENT
)
208 monwrite_diag(&entry
->hdr
, entry
->data
,
211 list_del(&entry
->list
);
219 static ssize_t
monwrite_write(struct file
*filp
, const char __user
*data
,
220 size_t count
, loff_t
*ppos
)
222 struct mon_private
*monpriv
= filp
->private_data
;
227 mutex_lock(&monpriv
->thread_mutex
);
228 for (written
= 0; written
< count
; ) {
229 if (monpriv
->hdr_to_read
) {
230 len
= min(count
- written
, monpriv
->hdr_to_read
);
231 to
= (char *) &monpriv
->hdr
+
232 sizeof(monpriv
->hdr
) - monpriv
->hdr_to_read
;
233 if (copy_from_user(to
, data
+ written
, len
)) {
237 monpriv
->hdr_to_read
-= len
;
239 if (monpriv
->hdr_to_read
> 0)
241 rc
= monwrite_new_hdr(monpriv
);
244 monpriv
->data_to_read
= monpriv
->current_buf
?
245 monpriv
->current_buf
->hdr
.datalen
: 0;
248 if (monpriv
->data_to_read
) {
249 len
= min(count
- written
, monpriv
->data_to_read
);
250 to
= monpriv
->current_buf
->data
+
251 monpriv
->hdr
.datalen
- monpriv
->data_to_read
;
252 if (copy_from_user(to
, data
+ written
, len
)) {
256 monpriv
->data_to_read
-= len
;
258 if (monpriv
->data_to_read
> 0)
260 rc
= monwrite_new_data(monpriv
);
264 monpriv
->hdr_to_read
= sizeof(monpriv
->hdr
);
266 mutex_unlock(&monpriv
->thread_mutex
);
270 monpriv
->data_to_read
= 0;
271 monpriv
->hdr_to_read
= sizeof(struct monwrite_hdr
);
272 mutex_unlock(&monpriv
->thread_mutex
);
276 static const struct file_operations monwrite_fops
= {
277 .owner
= THIS_MODULE
,
278 .open
= &monwrite_open
,
279 .release
= &monwrite_close
,
280 .write
= &monwrite_write
,
281 .llseek
= noop_llseek
,
284 static struct miscdevice mon_dev
= {
286 .fops
= &monwrite_fops
,
287 .minor
= MISC_DYNAMIC_MINOR
,
294 static int __init
mon_init(void)
299 * misc_register() has to be the last action in module_init(), because
300 * file operations will be available right after this.
302 return misc_register(&mon_dev
);
305 static void __exit
mon_exit(void)
307 misc_deregister(&mon_dev
);
310 module_init(mon_init
);
311 module_exit(mon_exit
);
313 module_param_named(max_bufs
, mon_max_bufs
, int, 0644);
314 MODULE_PARM_DESC(max_bufs
, "Maximum number of sample monitor data buffers "
315 "that can be active at one time");
317 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
318 MODULE_DESCRIPTION("Character device driver for writing z/VM "
319 "APPLDATA monitor records.");
320 MODULE_LICENSE("GPL");