2 * Character device driver for reading z/VM *MONITOR service records.
4 * Copyright IBM Corp. 2004, 2009
6 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
9 #define KMSG_COMPONENT "monreader"
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/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
24 #include <linux/slab.h>
25 #include <net/iucv/iucv.h>
26 #include <asm/uaccess.h>
27 #include <asm/ebcdic.h>
28 #include <asm/extmem.h>
31 #define MON_COLLECT_SAMPLE 0x80
32 #define MON_COLLECT_EVENT 0x40
33 #define MON_SERVICE "*MONITOR"
34 #define MON_IN_USE 0x01
35 #define MON_MSGLIM 255
37 static char mon_dcss_name
[9] = "MONDCSS\0";
42 struct iucv_message msg
;
48 struct iucv_path
*path
;
49 struct mon_msg
*msg_array
[MON_MSGLIM
];
50 unsigned int write_index
;
51 unsigned int read_index
;
52 atomic_t msglim_count
;
54 atomic_t iucv_connected
;
55 atomic_t iucv_severed
;
58 static unsigned long mon_in_use
= 0;
60 static unsigned long mon_dcss_start
;
61 static unsigned long mon_dcss_end
;
63 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue
);
64 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue
);
66 static u8 user_data_connect
[16] = {
67 /* Version code, must be 0x01 for shared mode */
70 MON_COLLECT_SAMPLE
| MON_COLLECT_EVENT
,
71 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 static u8 user_data_sever
[16] = {
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
81 static struct device
*monreader_device
;
83 /******************************************************************************
85 *****************************************************************************/
87 * Create the 8 bytes EBCDIC DCSS segment name from
88 * an ASCII name, incl. padding
90 static void dcss_mkname(char *ascii_name
, char *ebcdic_name
)
94 for (i
= 0; i
< 8; i
++) {
95 if (ascii_name
[i
] == '\0')
97 ebcdic_name
[i
] = toupper(ascii_name
[i
]);
100 ebcdic_name
[i
] = ' ';
101 ASCEBC(ebcdic_name
, 8);
104 static inline unsigned long mon_mca_start(struct mon_msg
*monmsg
)
106 return *(u32
*) &monmsg
->msg
.rmmsg
;
109 static inline unsigned long mon_mca_end(struct mon_msg
*monmsg
)
111 return *(u32
*) &monmsg
->msg
.rmmsg
[4];
114 static inline u8
mon_mca_type(struct mon_msg
*monmsg
, u8 index
)
116 return *((u8
*) mon_mca_start(monmsg
) + monmsg
->mca_offset
+ index
);
119 static inline u32
mon_mca_size(struct mon_msg
*monmsg
)
121 return mon_mca_end(monmsg
) - mon_mca_start(monmsg
) + 1;
124 static inline u32
mon_rec_start(struct mon_msg
*monmsg
)
126 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 4));
129 static inline u32
mon_rec_end(struct mon_msg
*monmsg
)
131 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 8));
134 static int mon_check_mca(struct mon_msg
*monmsg
)
136 if ((mon_rec_end(monmsg
) <= mon_rec_start(monmsg
)) ||
137 (mon_rec_start(monmsg
) < mon_dcss_start
) ||
138 (mon_rec_end(monmsg
) > mon_dcss_end
) ||
139 (mon_mca_type(monmsg
, 0) == 0) ||
140 (mon_mca_size(monmsg
) % 12 != 0) ||
141 (mon_mca_end(monmsg
) <= mon_mca_start(monmsg
)) ||
142 (mon_mca_end(monmsg
) > mon_dcss_end
) ||
143 (mon_mca_start(monmsg
) < mon_dcss_start
) ||
144 ((mon_mca_type(monmsg
, 1) == 0) && (mon_mca_type(monmsg
, 2) == 0)))
149 static int mon_send_reply(struct mon_msg
*monmsg
,
150 struct mon_private
*monpriv
)
154 rc
= iucv_message_reply(monpriv
->path
, &monmsg
->msg
,
155 IUCV_IPRMDATA
, NULL
, 0);
156 atomic_dec(&monpriv
->msglim_count
);
157 if (likely(!monmsg
->msglim_reached
)) {
159 monmsg
->mca_offset
= 0;
160 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
162 atomic_dec(&monpriv
->read_ready
);
164 monmsg
->replied_msglim
= 1;
166 pr_err("Reading monitor data failed with rc=%i\n", rc
);
172 static void mon_free_mem(struct mon_private
*monpriv
)
176 for (i
= 0; i
< MON_MSGLIM
; i
++)
177 kfree(monpriv
->msg_array
[i
]);
181 static struct mon_private
*mon_alloc_mem(void)
184 struct mon_private
*monpriv
;
186 monpriv
= kzalloc(sizeof(struct mon_private
), GFP_KERNEL
);
189 for (i
= 0; i
< MON_MSGLIM
; i
++) {
190 monpriv
->msg_array
[i
] = kzalloc(sizeof(struct mon_msg
),
192 if (!monpriv
->msg_array
[i
]) {
193 mon_free_mem(monpriv
);
200 static inline void mon_next_mca(struct mon_msg
*monmsg
)
202 if (likely((mon_mca_size(monmsg
) - monmsg
->mca_offset
) == 12))
204 monmsg
->mca_offset
+= 12;
208 static struct mon_msg
*mon_next_message(struct mon_private
*monpriv
)
210 struct mon_msg
*monmsg
;
212 if (!atomic_read(&monpriv
->read_ready
))
214 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
215 if (unlikely(monmsg
->replied_msglim
)) {
216 monmsg
->replied_msglim
= 0;
217 monmsg
->msglim_reached
= 0;
219 monmsg
->mca_offset
= 0;
220 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
222 atomic_dec(&monpriv
->read_ready
);
223 return ERR_PTR(-EOVERFLOW
);
229 /******************************************************************************
231 *****************************************************************************/
232 static void mon_iucv_path_complete(struct iucv_path
*path
, u8
*ipuser
)
234 struct mon_private
*monpriv
= path
->private;
236 atomic_set(&monpriv
->iucv_connected
, 1);
237 wake_up(&mon_conn_wait_queue
);
240 static void mon_iucv_path_severed(struct iucv_path
*path
, u8
*ipuser
)
242 struct mon_private
*monpriv
= path
->private;
244 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
246 iucv_path_sever(path
, NULL
);
247 atomic_set(&monpriv
->iucv_severed
, 1);
248 wake_up(&mon_conn_wait_queue
);
249 wake_up_interruptible(&mon_read_wait_queue
);
252 static void mon_iucv_message_pending(struct iucv_path
*path
,
253 struct iucv_message
*msg
)
255 struct mon_private
*monpriv
= path
->private;
257 memcpy(&monpriv
->msg_array
[monpriv
->write_index
]->msg
,
259 if (atomic_inc_return(&monpriv
->msglim_count
) == MON_MSGLIM
) {
260 pr_warn("The read queue for monitor data is full\n");
261 monpriv
->msg_array
[monpriv
->write_index
]->msglim_reached
= 1;
263 monpriv
->write_index
= (monpriv
->write_index
+ 1) % MON_MSGLIM
;
264 atomic_inc(&monpriv
->read_ready
);
265 wake_up_interruptible(&mon_read_wait_queue
);
268 static struct iucv_handler monreader_iucv_handler
= {
269 .path_complete
= mon_iucv_path_complete
,
270 .path_severed
= mon_iucv_path_severed
,
271 .message_pending
= mon_iucv_message_pending
,
274 /******************************************************************************
276 *****************************************************************************/
277 static int mon_open(struct inode
*inode
, struct file
*filp
)
279 struct mon_private
*monpriv
;
283 * only one user allowed
286 if (test_and_set_bit(MON_IN_USE
, &mon_in_use
))
290 monpriv
= mon_alloc_mem();
295 * Connect to *MONITOR service
297 monpriv
->path
= iucv_path_alloc(MON_MSGLIM
, IUCV_IPRMDATA
, GFP_KERNEL
);
300 rc
= iucv_path_connect(monpriv
->path
, &monreader_iucv_handler
,
301 MON_SERVICE
, NULL
, user_data_connect
, monpriv
);
303 pr_err("Connecting to the z/VM *MONITOR system service "
304 "failed with rc=%i\n", rc
);
309 * Wait for connection confirmation
311 wait_event(mon_conn_wait_queue
,
312 atomic_read(&monpriv
->iucv_connected
) ||
313 atomic_read(&monpriv
->iucv_severed
));
314 if (atomic_read(&monpriv
->iucv_severed
)) {
315 atomic_set(&monpriv
->iucv_severed
, 0);
316 atomic_set(&monpriv
->iucv_connected
, 0);
320 filp
->private_data
= monpriv
;
321 dev_set_drvdata(monreader_device
, monpriv
);
322 return nonseekable_open(inode
, filp
);
325 iucv_path_free(monpriv
->path
);
327 mon_free_mem(monpriv
);
329 clear_bit(MON_IN_USE
, &mon_in_use
);
334 static int mon_close(struct inode
*inode
, struct file
*filp
)
337 struct mon_private
*monpriv
= filp
->private_data
;
340 * Close IUCV connection and unregister
343 rc
= iucv_path_sever(monpriv
->path
, user_data_sever
);
345 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
347 iucv_path_free(monpriv
->path
);
350 atomic_set(&monpriv
->iucv_severed
, 0);
351 atomic_set(&monpriv
->iucv_connected
, 0);
352 atomic_set(&monpriv
->read_ready
, 0);
353 atomic_set(&monpriv
->msglim_count
, 0);
354 monpriv
->write_index
= 0;
355 monpriv
->read_index
= 0;
356 dev_set_drvdata(monreader_device
, NULL
);
358 for (i
= 0; i
< MON_MSGLIM
; i
++)
359 kfree(monpriv
->msg_array
[i
]);
361 clear_bit(MON_IN_USE
, &mon_in_use
);
365 static ssize_t
mon_read(struct file
*filp
, char __user
*data
,
366 size_t count
, loff_t
*ppos
)
368 struct mon_private
*monpriv
= filp
->private_data
;
369 struct mon_msg
*monmsg
;
373 monmsg
= mon_next_message(monpriv
);
375 return PTR_ERR(monmsg
);
378 if (filp
->f_flags
& O_NONBLOCK
)
380 ret
= wait_event_interruptible(mon_read_wait_queue
,
381 atomic_read(&monpriv
->read_ready
) ||
382 atomic_read(&monpriv
->iucv_severed
));
385 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
387 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
391 monmsg
->pos
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
392 if (mon_check_mca(monmsg
))
395 /* read monitor control element (12 bytes) first */
396 mce_start
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
397 if ((monmsg
->pos
>= mce_start
) && (monmsg
->pos
< mce_start
+ 12)) {
398 count
= min(count
, (size_t) mce_start
+ 12 - monmsg
->pos
);
399 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
403 monmsg
->pos
+= count
;
404 if (monmsg
->pos
== mce_start
+ 12)
405 monmsg
->pos
= mon_rec_start(monmsg
);
410 if (monmsg
->pos
<= mon_rec_end(monmsg
)) {
411 count
= min(count
, (size_t) mon_rec_end(monmsg
) - monmsg
->pos
413 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
417 monmsg
->pos
+= count
;
418 if (monmsg
->pos
> mon_rec_end(monmsg
))
419 mon_next_mca(monmsg
);
423 ret
= mon_send_reply(monmsg
, monpriv
);
431 static unsigned int mon_poll(struct file
*filp
, struct poll_table_struct
*p
)
433 struct mon_private
*monpriv
= filp
->private_data
;
435 poll_wait(filp
, &mon_read_wait_queue
, p
);
436 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
438 if (atomic_read(&monpriv
->read_ready
))
439 return POLLIN
| POLLRDNORM
;
443 static const struct file_operations mon_fops
= {
444 .owner
= THIS_MODULE
,
446 .release
= &mon_close
,
449 .llseek
= noop_llseek
,
452 static struct miscdevice mon_dev
= {
455 .minor
= MISC_DYNAMIC_MINOR
,
459 /******************************************************************************
461 *****************************************************************************/
462 static int monreader_freeze(struct device
*dev
)
464 struct mon_private
*monpriv
= dev_get_drvdata(dev
);
470 rc
= iucv_path_sever(monpriv
->path
, user_data_sever
);
472 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
474 iucv_path_free(monpriv
->path
);
476 atomic_set(&monpriv
->iucv_severed
, 0);
477 atomic_set(&monpriv
->iucv_connected
, 0);
478 atomic_set(&monpriv
->read_ready
, 0);
479 atomic_set(&monpriv
->msglim_count
, 0);
480 monpriv
->write_index
= 0;
481 monpriv
->read_index
= 0;
482 monpriv
->path
= NULL
;
486 static int monreader_thaw(struct device
*dev
)
488 struct mon_private
*monpriv
= dev_get_drvdata(dev
);
494 monpriv
->path
= iucv_path_alloc(MON_MSGLIM
, IUCV_IPRMDATA
, GFP_KERNEL
);
497 rc
= iucv_path_connect(monpriv
->path
, &monreader_iucv_handler
,
498 MON_SERVICE
, NULL
, user_data_connect
, monpriv
);
500 pr_err("Connecting to the z/VM *MONITOR system service "
501 "failed with rc=%i\n", rc
);
504 wait_event(mon_conn_wait_queue
,
505 atomic_read(&monpriv
->iucv_connected
) ||
506 atomic_read(&monpriv
->iucv_severed
));
507 if (atomic_read(&monpriv
->iucv_severed
))
512 iucv_path_free(monpriv
->path
);
513 monpriv
->path
= NULL
;
515 atomic_set(&monpriv
->iucv_severed
, 1);
519 static int monreader_restore(struct device
*dev
)
523 segment_unload(mon_dcss_name
);
524 rc
= segment_load(mon_dcss_name
, SEGMENT_SHARED
,
525 &mon_dcss_start
, &mon_dcss_end
);
527 segment_warning(rc
, mon_dcss_name
);
528 panic("fatal monreader resume error: no monitor dcss\n");
530 return monreader_thaw(dev
);
533 static const struct dev_pm_ops monreader_pm_ops
= {
534 .freeze
= monreader_freeze
,
535 .thaw
= monreader_thaw
,
536 .restore
= monreader_restore
,
539 static struct device_driver monreader_driver
= {
542 .pm
= &monreader_pm_ops
,
546 /******************************************************************************
548 *****************************************************************************/
549 static int __init
mon_init(void)
553 if (!MACHINE_IS_VM
) {
554 pr_err("The z/VM *MONITOR record device driver cannot be "
555 "loaded without z/VM\n");
560 * Register with IUCV and connect to *MONITOR service
562 rc
= iucv_register(&monreader_iucv_handler
, 1);
564 pr_err("The z/VM *MONITOR record device driver failed to "
565 "register with IUCV\n");
569 rc
= driver_register(&monreader_driver
);
572 monreader_device
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
573 if (!monreader_device
) {
578 dev_set_name(monreader_device
, "monreader-dev");
579 monreader_device
->bus
= &iucv_bus
;
580 monreader_device
->parent
= iucv_root
;
581 monreader_device
->driver
= &monreader_driver
;
582 monreader_device
->release
= (void (*)(struct device
*))kfree
;
583 rc
= device_register(monreader_device
);
585 put_device(monreader_device
);
589 rc
= segment_type(mon_dcss_name
);
591 segment_warning(rc
, mon_dcss_name
);
594 if (rc
!= SEG_TYPE_SC
) {
595 pr_err("The specified *MONITOR DCSS %s does not have the "
596 "required type SC\n", mon_dcss_name
);
601 rc
= segment_load(mon_dcss_name
, SEGMENT_SHARED
,
602 &mon_dcss_start
, &mon_dcss_end
);
604 segment_warning(rc
, mon_dcss_name
);
608 dcss_mkname(mon_dcss_name
, &user_data_connect
[8]);
611 * misc_register() has to be the last action in module_init(), because
612 * file operations will be available right after this.
614 rc
= misc_register(&mon_dev
);
620 segment_unload(mon_dcss_name
);
622 device_unregister(monreader_device
);
624 driver_unregister(&monreader_driver
);
626 iucv_unregister(&monreader_iucv_handler
, 1);
630 static void __exit
mon_exit(void)
632 segment_unload(mon_dcss_name
);
633 misc_deregister(&mon_dev
);
634 device_unregister(monreader_device
);
635 driver_unregister(&monreader_driver
);
636 iucv_unregister(&monreader_iucv_handler
, 1);
641 module_init(mon_init
);
642 module_exit(mon_exit
);
644 module_param_string(mondcss
, mon_dcss_name
, 9, 0444);
645 MODULE_PARM_DESC(mondcss
, "Name of DCSS segment to be used for *MONITOR "
646 "service, max. 8 chars. Default is MONDCSS");
648 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
649 MODULE_DESCRIPTION("Character device driver for reading z/VM "
650 "monitor service records.");
651 MODULE_LICENSE("GPL");