2 * drivers/s390/char/monreader.c
4 * Character device driver for reading z/VM *MONITOR service records.
6 * Copyright 2004 IBM Corporation, IBM Deutschland Entwicklung GmbH.
8 * Author: Gerald Schaefer <geraldsc@de.ibm.com>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/ctype.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <asm/uaccess.h>
22 #include <asm/ebcdic.h>
23 #include <asm/extmem.h>
24 #include <linux/poll.h>
25 #include <net/iucv/iucv.h>
28 //#define MON_DEBUG /* Debug messages on/off */
30 #define MON_NAME "monreader"
32 #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
33 #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
34 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
37 #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
39 #define P_DEBUG(x...) do {} while (0)
42 #define MON_COLLECT_SAMPLE 0x80
43 #define MON_COLLECT_EVENT 0x40
44 #define MON_SERVICE "*MONITOR"
45 #define MON_IN_USE 0x01
46 #define MON_MSGLIM 255
48 static char mon_dcss_name[9] = "MONDCSS\0";
53 struct iucv_message msg;
59 struct iucv_path *path;
60 struct mon_msg *msg_array[MON_MSGLIM];
61 unsigned int write_index;
62 unsigned int read_index;
63 atomic_t msglim_count;
65 atomic_t iucv_connected;
66 atomic_t iucv_severed;
69 static unsigned long mon_in_use = 0;
71 static unsigned long mon_dcss_start;
72 static unsigned long mon_dcss_end;
74 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
75 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
77 static u8 user_data_connect[16] = {
78 /* Version code, must be 0x01 for shared mode */
81 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
82 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 static u8 user_data_sever[16] = {
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93 /******************************************************************************
95 *****************************************************************************/
97 * Create the 8 bytes EBCDIC DCSS segment name from
98 * an ASCII name, incl. padding
100 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
104 for (i = 0; i < 8; i++) {
105 if (ascii_name[i] == '\0')
107 ebcdic_name[i] = toupper(ascii_name[i]);
110 ebcdic_name[i] = ' ';
111 ASCEBC(ebcdic_name, 8);
115 * print appropriate error message for segment_load()/segment_type()
118 static void mon_segment_warn(int rc, char* seg_name)
122 P_WARNING("cannot load/query segment %s, does not exist\n",
126 P_WARNING("cannot load/query segment %s, not running on VM\n",
130 P_WARNING("cannot load/query segment %s, hardware error\n",
134 P_WARNING("cannot load/query segment %s, is a multi-part "
135 "segment\n", seg_name);
138 P_WARNING("cannot load/query segment %s, overlaps with "
139 "storage\n", seg_name);
142 P_WARNING("cannot load/query segment %s, overlaps with "
143 "already loaded dcss\n", seg_name);
146 P_WARNING("cannot load/query segment %s, already loaded in "
147 "incompatible mode\n", seg_name);
150 P_WARNING("cannot load/query segment %s, out of memory\n",
154 P_WARNING("cannot load/query segment %s, exceeds kernel "
155 "mapping range\n", seg_name);
158 P_WARNING("cannot load/query segment %s, return value %i\n",
164 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
166 return *(u32 *) &monmsg->msg.rmmsg;
169 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
171 return *(u32 *) &monmsg->msg.rmmsg[4];
174 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
176 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
179 static inline u32 mon_mca_size(struct mon_msg *monmsg)
181 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
184 static inline u32 mon_rec_start(struct mon_msg *monmsg)
186 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
189 static inline u32 mon_rec_end(struct mon_msg *monmsg)
191 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
194 static int mon_check_mca(struct mon_msg *monmsg)
196 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
197 (mon_rec_start(monmsg) < mon_dcss_start) ||
198 (mon_rec_end(monmsg) > mon_dcss_end) ||
199 (mon_mca_type(monmsg, 0) == 0) ||
200 (mon_mca_size(monmsg) % 12 != 0) ||
201 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
202 (mon_mca_end(monmsg) > mon_dcss_end) ||
203 (mon_mca_start(monmsg) < mon_dcss_start) ||
204 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
206 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
212 static int mon_send_reply(struct mon_msg *monmsg,
213 struct mon_private *monpriv)
217 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
219 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
221 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
222 IUCV_IPRMDATA, NULL, 0);
223 atomic_dec(&monpriv->msglim_count);
224 if (likely(!monmsg->msglim_reached)) {
226 monmsg->mca_offset = 0;
227 monpriv->read_index = (monpriv->read_index + 1) %
229 atomic_dec(&monpriv->read_ready);
231 monmsg->replied_msglim = 1;
233 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
239 static void mon_free_mem(struct mon_private *monpriv)
243 for (i = 0; i < MON_MSGLIM; i++)
244 if (monpriv->msg_array[i])
245 kfree(monpriv->msg_array[i]);
249 static struct mon_private *mon_alloc_mem(void)
252 struct mon_private *monpriv;
254 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
256 P_ERROR("no memory for monpriv\n");
259 for (i = 0; i < MON_MSGLIM; i++) {
260 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
262 if (!monpriv->msg_array[i]) {
263 P_ERROR("open, no memory for msg_array\n");
264 mon_free_mem(monpriv);
271 static inline void mon_read_debug(struct mon_msg *monmsg,
272 struct mon_private *monpriv)
275 u8 msg_type[2], mca_type;
276 unsigned long records_len;
278 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
280 memcpy(msg_type, &monmsg->msg.class, 2);
282 mca_type = mon_mca_type(monmsg, 0);
283 EBCASC(&mca_type, 1);
285 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
286 monpriv->read_index, monpriv->write_index);
287 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
288 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
289 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
290 msg_type[0], msg_type[1], mca_type ? mca_type : 'X',
291 mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2));
292 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
293 mon_mca_start(monmsg), mon_mca_end(monmsg));
294 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
295 mon_rec_start(monmsg), mon_rec_end(monmsg), records_len);
296 if (mon_mca_size(monmsg) > 12)
297 P_DEBUG("READ, MORE THAN ONE MCA\n\n");
301 static inline void mon_next_mca(struct mon_msg *monmsg)
303 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
305 P_DEBUG("READ, NEXT MCA\n\n");
306 monmsg->mca_offset += 12;
310 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
312 struct mon_msg *monmsg;
314 if (!atomic_read(&monpriv->read_ready))
316 monmsg = monpriv->msg_array[monpriv->read_index];
317 if (unlikely(monmsg->replied_msglim)) {
318 monmsg->replied_msglim = 0;
319 monmsg->msglim_reached = 0;
321 monmsg->mca_offset = 0;
322 P_WARNING("read, message limit reached\n");
323 monpriv->read_index = (monpriv->read_index + 1) %
325 atomic_dec(&monpriv->read_ready);
326 return ERR_PTR(-EOVERFLOW);
332 /******************************************************************************
334 *****************************************************************************/
335 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
337 struct mon_private *monpriv = path->private;
339 P_DEBUG("IUCV connection completed\n");
340 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
341 "0x%02X, Sample = 0x%02X\n",
342 ipuser[0], ipuser[1], ipuser[2]);
343 atomic_set(&monpriv->iucv_connected, 1);
344 wake_up(&mon_conn_wait_queue);
347 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
349 struct mon_private *monpriv = path->private;
351 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
352 iucv_path_sever(path, NULL);
353 atomic_set(&monpriv->iucv_severed, 1);
354 wake_up(&mon_conn_wait_queue);
355 wake_up_interruptible(&mon_read_wait_queue);
358 static void mon_iucv_message_pending(struct iucv_path *path,
359 struct iucv_message *msg)
361 struct mon_private *monpriv = path->private;
363 P_DEBUG("IUCV message pending\n");
364 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
366 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
367 P_WARNING("IUCV message pending, message limit (%i) reached\n",
369 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
371 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
372 atomic_inc(&monpriv->read_ready);
373 wake_up_interruptible(&mon_read_wait_queue);
376 static struct iucv_handler monreader_iucv_handler = {
377 .path_complete = mon_iucv_path_complete,
378 .path_severed = mon_iucv_path_severed,
379 .message_pending = mon_iucv_message_pending,
382 /******************************************************************************
384 *****************************************************************************/
385 static int mon_open(struct inode *inode, struct file *filp)
387 struct mon_private *monpriv;
391 * only one user allowed
394 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
398 monpriv = mon_alloc_mem();
403 * Connect to *MONITOR service
405 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
408 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
409 MON_SERVICE, NULL, user_data_connect, monpriv);
411 P_ERROR("iucv connection to *MONITOR failed with "
412 "IPUSER SEVER code = %i\n", rc);
417 * Wait for connection confirmation
419 wait_event(mon_conn_wait_queue,
420 atomic_read(&monpriv->iucv_connected) ||
421 atomic_read(&monpriv->iucv_severed));
422 if (atomic_read(&monpriv->iucv_severed)) {
423 atomic_set(&monpriv->iucv_severed, 0);
424 atomic_set(&monpriv->iucv_connected, 0);
428 P_INFO("open, established connection to *MONITOR service\n\n");
429 filp->private_data = monpriv;
430 return nonseekable_open(inode, filp);
433 kfree(monpriv->path);
435 mon_free_mem(monpriv);
437 clear_bit(MON_IN_USE, &mon_in_use);
442 static int mon_close(struct inode *inode, struct file *filp)
445 struct mon_private *monpriv = filp->private_data;
448 * Close IUCV connection and unregister
450 rc = iucv_path_sever(monpriv->path, user_data_sever);
452 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
454 P_INFO("close, terminated connection to *MONITOR service\n");
456 atomic_set(&monpriv->iucv_severed, 0);
457 atomic_set(&monpriv->iucv_connected, 0);
458 atomic_set(&monpriv->read_ready, 0);
459 atomic_set(&monpriv->msglim_count, 0);
460 monpriv->write_index = 0;
461 monpriv->read_index = 0;
463 for (i = 0; i < MON_MSGLIM; i++)
464 kfree(monpriv->msg_array[i]);
466 clear_bit(MON_IN_USE, &mon_in_use);
470 static ssize_t mon_read(struct file *filp, char __user *data,
471 size_t count, loff_t *ppos)
473 struct mon_private *monpriv = filp->private_data;
474 struct mon_msg *monmsg;
478 monmsg = mon_next_message(monpriv);
480 return PTR_ERR(monmsg);
483 if (filp->f_flags & O_NONBLOCK)
485 ret = wait_event_interruptible(mon_read_wait_queue,
486 atomic_read(&monpriv->read_ready) ||
487 atomic_read(&monpriv->iucv_severed));
490 if (unlikely(atomic_read(&monpriv->iucv_severed)))
492 monmsg = monpriv->msg_array[monpriv->read_index];
496 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
497 mon_read_debug(monmsg, monpriv);
499 if (mon_check_mca(monmsg))
502 /* read monitor control element (12 bytes) first */
503 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
504 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
505 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
506 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
510 monmsg->pos += count;
511 if (monmsg->pos == mce_start + 12)
512 monmsg->pos = mon_rec_start(monmsg);
517 if (monmsg->pos <= mon_rec_end(monmsg)) {
518 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
520 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
524 monmsg->pos += count;
525 if (monmsg->pos > mon_rec_end(monmsg))
526 mon_next_mca(monmsg);
530 ret = mon_send_reply(monmsg, monpriv);
538 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
540 struct mon_private *monpriv = filp->private_data;
542 poll_wait(filp, &mon_read_wait_queue, p);
543 if (unlikely(atomic_read(&monpriv->iucv_severed)))
545 if (atomic_read(&monpriv->read_ready))
546 return POLLIN | POLLRDNORM;
550 static const struct file_operations mon_fops = {
551 .owner = THIS_MODULE,
553 .release = &mon_close,
558 static struct miscdevice mon_dev = {
561 .minor = MISC_DYNAMIC_MINOR,
564 /******************************************************************************
566 *****************************************************************************/
567 static int __init mon_init(void)
571 if (!MACHINE_IS_VM) {
572 P_ERROR("not running under z/VM, driver not loaded\n");
577 * Register with IUCV and connect to *MONITOR service
579 rc = iucv_register(&monreader_iucv_handler, 1);
581 P_ERROR("failed to register with iucv driver\n");
584 P_INFO("open, registered with IUCV\n");
586 rc = segment_type(mon_dcss_name);
588 mon_segment_warn(rc, mon_dcss_name);
591 if (rc != SEG_TYPE_SC) {
592 P_ERROR("segment %s has unsupported type, should be SC\n",
598 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
599 &mon_dcss_start, &mon_dcss_end);
601 mon_segment_warn(rc, mon_dcss_name);
605 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
607 rc = misc_register(&mon_dev);
609 P_ERROR("misc_register failed, rc = %i\n", rc);
612 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
613 mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end,
614 mon_dcss_end - mon_dcss_start + 1);
618 segment_unload(mon_dcss_name);
620 iucv_unregister(&monreader_iucv_handler, 1);
624 static void __exit mon_exit(void)
626 segment_unload(mon_dcss_name);
627 WARN_ON(misc_deregister(&mon_dev) != 0);
628 iucv_unregister(&monreader_iucv_handler, 1);
633 module_init(mon_init);
634 module_exit(mon_exit);
636 module_param_string(mondcss, mon_dcss_name, 9, 0444);
637 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
638 "service, max. 8 chars. Default is MONDCSS");
640 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
641 MODULE_DESCRIPTION("Character device driver for reading z/VM "
642 "monitor service records.");
643 MODULE_LICENSE("GPL");