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 <linux/slab.h>
24 #include <asm/uaccess.h>
25 #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
;
42 static LIST_HEAD(mon_priv_list
);
45 struct list_head priv_list
;
46 struct list_head list
;
47 struct monwrite_hdr hdr
;
50 struct mon_buf
*current_buf
;
51 struct mutex thread_mutex
;
58 static int monwrite_diag(struct monwrite_hdr
*myhdr
, char *buffer
, int fcn
)
60 struct appldata_product_id id
;
63 strcpy(id
.prod_nr
, "LNXAPPL");
64 id
.prod_fn
= myhdr
->applid
;
65 id
.record_nr
= myhdr
->record_num
;
66 id
.version_nr
= myhdr
->version
;
67 id
.release_nr
= myhdr
->release
;
68 id
.mod_lvl
= myhdr
->mod_level
;
69 rc
= appldata_asm(&id
, fcn
, (void *) buffer
, myhdr
->datalen
);
72 pr_err("Writing monitor data failed with rc=%i\n", rc
);
78 static struct mon_buf
*monwrite_find_hdr(struct mon_private
*monpriv
,
79 struct monwrite_hdr
*monhdr
)
81 struct mon_buf
*entry
, *next
;
83 list_for_each_entry_safe(entry
, next
, &monpriv
->list
, list
)
84 if ((entry
->hdr
.mon_function
== monhdr
->mon_function
||
85 monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) &&
86 entry
->hdr
.applid
== monhdr
->applid
&&
87 entry
->hdr
.record_num
== monhdr
->record_num
&&
88 entry
->hdr
.version
== monhdr
->version
&&
89 entry
->hdr
.release
== monhdr
->release
&&
90 entry
->hdr
.mod_level
== monhdr
->mod_level
)
96 static int monwrite_new_hdr(struct mon_private
*monpriv
)
98 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
99 struct mon_buf
*monbuf
;
102 if (monhdr
->datalen
> MONWRITE_MAX_DATALEN
||
103 monhdr
->mon_function
> MONWRITE_START_CONFIG
||
104 monhdr
->hdrlen
!= sizeof(struct monwrite_hdr
))
107 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
108 monbuf
= monwrite_find_hdr(monpriv
, monhdr
);
110 if (monhdr
->mon_function
== MONWRITE_STOP_INTERVAL
) {
111 monhdr
->datalen
= monbuf
->hdr
.datalen
;
112 rc
= monwrite_diag(monhdr
, monbuf
->data
,
114 list_del(&monbuf
->list
);
120 } else if (monhdr
->mon_function
!= MONWRITE_STOP_INTERVAL
) {
121 if (mon_buf_count
>= mon_max_bufs
)
123 monbuf
= kzalloc(sizeof(struct mon_buf
), GFP_KERNEL
);
126 monbuf
->data
= kzalloc(monhdr
->datalen
,
127 GFP_KERNEL
| GFP_DMA
);
132 monbuf
->hdr
= *monhdr
;
133 list_add_tail(&monbuf
->list
, &monpriv
->list
);
134 if (monhdr
->mon_function
!= MONWRITE_GEN_EVENT
)
137 monpriv
->current_buf
= monbuf
;
141 static int monwrite_new_data(struct mon_private
*monpriv
)
143 struct monwrite_hdr
*monhdr
= &monpriv
->hdr
;
144 struct mon_buf
*monbuf
= monpriv
->current_buf
;
147 switch (monhdr
->mon_function
) {
148 case MONWRITE_START_INTERVAL
:
149 if (!monbuf
->diag_done
) {
150 rc
= monwrite_diag(monhdr
, monbuf
->data
,
151 APPLDATA_START_INTERVAL_REC
);
152 monbuf
->diag_done
= 1;
155 case MONWRITE_START_CONFIG
:
156 if (!monbuf
->diag_done
) {
157 rc
= monwrite_diag(monhdr
, monbuf
->data
,
158 APPLDATA_START_CONFIG_REC
);
159 monbuf
->diag_done
= 1;
162 case MONWRITE_GEN_EVENT
:
163 rc
= monwrite_diag(monhdr
, monbuf
->data
,
164 APPLDATA_GEN_EVENT_REC
);
165 list_del(&monpriv
->current_buf
->list
);
166 kfree(monpriv
->current_buf
->data
);
167 kfree(monpriv
->current_buf
);
168 monpriv
->current_buf
= NULL
;
171 /* monhdr->mon_function is checked in monwrite_new_hdr */
181 static int monwrite_open(struct inode
*inode
, struct file
*filp
)
183 struct mon_private
*monpriv
;
185 monpriv
= kzalloc(sizeof(struct mon_private
), GFP_KERNEL
);
188 INIT_LIST_HEAD(&monpriv
->list
);
189 monpriv
->hdr_to_read
= sizeof(monpriv
->hdr
);
190 mutex_init(&monpriv
->thread_mutex
);
191 filp
->private_data
= monpriv
;
192 list_add_tail(&monpriv
->priv_list
, &mon_priv_list
);
193 return nonseekable_open(inode
, filp
);
196 static int monwrite_close(struct inode
*inode
, struct file
*filp
)
198 struct mon_private
*monpriv
= filp
->private_data
;
199 struct mon_buf
*entry
, *next
;
201 list_for_each_entry_safe(entry
, next
, &monpriv
->list
, list
) {
202 if (entry
->hdr
.mon_function
!= MONWRITE_GEN_EVENT
)
203 monwrite_diag(&entry
->hdr
, entry
->data
,
206 list_del(&entry
->list
);
210 list_del(&monpriv
->priv_list
);
215 static ssize_t
monwrite_write(struct file
*filp
, const char __user
*data
,
216 size_t count
, loff_t
*ppos
)
218 struct mon_private
*monpriv
= filp
->private_data
;
223 mutex_lock(&monpriv
->thread_mutex
);
224 for (written
= 0; written
< count
; ) {
225 if (monpriv
->hdr_to_read
) {
226 len
= min(count
- written
, monpriv
->hdr_to_read
);
227 to
= (char *) &monpriv
->hdr
+
228 sizeof(monpriv
->hdr
) - monpriv
->hdr_to_read
;
229 if (copy_from_user(to
, data
+ written
, len
)) {
233 monpriv
->hdr_to_read
-= len
;
235 if (monpriv
->hdr_to_read
> 0)
237 rc
= monwrite_new_hdr(monpriv
);
240 monpriv
->data_to_read
= monpriv
->current_buf
?
241 monpriv
->current_buf
->hdr
.datalen
: 0;
244 if (monpriv
->data_to_read
) {
245 len
= min(count
- written
, monpriv
->data_to_read
);
246 to
= monpriv
->current_buf
->data
+
247 monpriv
->hdr
.datalen
- monpriv
->data_to_read
;
248 if (copy_from_user(to
, data
+ written
, len
)) {
252 monpriv
->data_to_read
-= len
;
254 if (monpriv
->data_to_read
> 0)
256 rc
= monwrite_new_data(monpriv
);
260 monpriv
->hdr_to_read
= sizeof(monpriv
->hdr
);
262 mutex_unlock(&monpriv
->thread_mutex
);
266 monpriv
->data_to_read
= 0;
267 monpriv
->hdr_to_read
= sizeof(struct monwrite_hdr
);
268 mutex_unlock(&monpriv
->thread_mutex
);
272 static const struct file_operations monwrite_fops
= {
273 .owner
= THIS_MODULE
,
274 .open
= &monwrite_open
,
275 .release
= &monwrite_close
,
276 .write
= &monwrite_write
,
277 .llseek
= noop_llseek
,
280 static struct miscdevice mon_dev
= {
282 .fops
= &monwrite_fops
,
283 .minor
= MISC_DYNAMIC_MINOR
,
290 static int monwriter_freeze(struct device
*dev
)
292 struct mon_private
*monpriv
;
293 struct mon_buf
*monbuf
;
295 list_for_each_entry(monpriv
, &mon_priv_list
, priv_list
) {
296 list_for_each_entry(monbuf
, &monpriv
->list
, list
) {
297 if (monbuf
->hdr
.mon_function
!= MONWRITE_GEN_EVENT
)
298 monwrite_diag(&monbuf
->hdr
, monbuf
->data
,
305 static int monwriter_restore(struct device
*dev
)
307 struct mon_private
*monpriv
;
308 struct mon_buf
*monbuf
;
310 list_for_each_entry(monpriv
, &mon_priv_list
, priv_list
) {
311 list_for_each_entry(monbuf
, &monpriv
->list
, list
) {
312 if (monbuf
->hdr
.mon_function
== MONWRITE_START_INTERVAL
)
313 monwrite_diag(&monbuf
->hdr
, monbuf
->data
,
314 APPLDATA_START_INTERVAL_REC
);
315 if (monbuf
->hdr
.mon_function
== MONWRITE_START_CONFIG
)
316 monwrite_diag(&monbuf
->hdr
, monbuf
->data
,
317 APPLDATA_START_CONFIG_REC
);
323 static int monwriter_thaw(struct device
*dev
)
325 return monwriter_restore(dev
);
328 static const struct dev_pm_ops monwriter_pm_ops
= {
329 .freeze
= monwriter_freeze
,
330 .thaw
= monwriter_thaw
,
331 .restore
= monwriter_restore
,
334 static struct platform_driver monwriter_pdrv
= {
337 .owner
= THIS_MODULE
,
338 .pm
= &monwriter_pm_ops
,
342 static struct platform_device
*monwriter_pdev
;
348 static int __init
mon_init(void)
355 rc
= platform_driver_register(&monwriter_pdrv
);
359 monwriter_pdev
= platform_device_register_simple("monwriter", -1, NULL
,
361 if (IS_ERR(monwriter_pdev
)) {
362 rc
= PTR_ERR(monwriter_pdev
);
367 * misc_register() has to be the last action in module_init(), because
368 * file operations will be available right after this.
370 rc
= misc_register(&mon_dev
);
376 platform_device_unregister(monwriter_pdev
);
378 platform_driver_unregister(&monwriter_pdrv
);
382 static void __exit
mon_exit(void)
384 misc_deregister(&mon_dev
);
385 platform_device_unregister(monwriter_pdev
);
386 platform_driver_unregister(&monwriter_pdrv
);
389 module_init(mon_init
);
390 module_exit(mon_exit
);
392 module_param_named(max_bufs
, mon_max_bufs
, int, 0644);
393 MODULE_PARM_DESC(max_bufs
, "Maximum number of sample monitor data buffers "
394 "that can be active at one time");
396 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
397 MODULE_DESCRIPTION("Character device driver for writing z/VM "
398 "APPLDATA monitor records.");
399 MODULE_LICENSE("GPL");