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);
114 static inline unsigned long mon_mca_start(struct mon_msg
*monmsg
)
116 return *(u32
*) &monmsg
->msg
.rmmsg
;
119 static inline unsigned long mon_mca_end(struct mon_msg
*monmsg
)
121 return *(u32
*) &monmsg
->msg
.rmmsg
[4];
124 static inline u8
mon_mca_type(struct mon_msg
*monmsg
, u8 index
)
126 return *((u8
*) mon_mca_start(monmsg
) + monmsg
->mca_offset
+ index
);
129 static inline u32
mon_mca_size(struct mon_msg
*monmsg
)
131 return mon_mca_end(monmsg
) - mon_mca_start(monmsg
) + 1;
134 static inline u32
mon_rec_start(struct mon_msg
*monmsg
)
136 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 4));
139 static inline u32
mon_rec_end(struct mon_msg
*monmsg
)
141 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 8));
144 static int mon_check_mca(struct mon_msg
*monmsg
)
146 if ((mon_rec_end(monmsg
) <= mon_rec_start(monmsg
)) ||
147 (mon_rec_start(monmsg
) < mon_dcss_start
) ||
148 (mon_rec_end(monmsg
) > mon_dcss_end
) ||
149 (mon_mca_type(monmsg
, 0) == 0) ||
150 (mon_mca_size(monmsg
) % 12 != 0) ||
151 (mon_mca_end(monmsg
) <= mon_mca_start(monmsg
)) ||
152 (mon_mca_end(monmsg
) > mon_dcss_end
) ||
153 (mon_mca_start(monmsg
) < mon_dcss_start
) ||
154 ((mon_mca_type(monmsg
, 1) == 0) && (mon_mca_type(monmsg
, 2) == 0)))
156 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
162 static int mon_send_reply(struct mon_msg
*monmsg
,
163 struct mon_private
*monpriv
)
167 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
169 monpriv
->path
->pathid
, monmsg
->msg
.id
, monmsg
->msg
.class);
171 rc
= iucv_message_reply(monpriv
->path
, &monmsg
->msg
,
172 IUCV_IPRMDATA
, NULL
, 0);
173 atomic_dec(&monpriv
->msglim_count
);
174 if (likely(!monmsg
->msglim_reached
)) {
176 monmsg
->mca_offset
= 0;
177 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
179 atomic_dec(&monpriv
->read_ready
);
181 monmsg
->replied_msglim
= 1;
183 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc
);
189 static void mon_free_mem(struct mon_private
*monpriv
)
193 for (i
= 0; i
< MON_MSGLIM
; i
++)
194 if (monpriv
->msg_array
[i
])
195 kfree(monpriv
->msg_array
[i
]);
199 static struct mon_private
*mon_alloc_mem(void)
202 struct mon_private
*monpriv
;
204 monpriv
= kzalloc(sizeof(struct mon_private
), GFP_KERNEL
);
206 P_ERROR("no memory for monpriv\n");
209 for (i
= 0; i
< MON_MSGLIM
; i
++) {
210 monpriv
->msg_array
[i
] = kzalloc(sizeof(struct mon_msg
),
212 if (!monpriv
->msg_array
[i
]) {
213 P_ERROR("open, no memory for msg_array\n");
214 mon_free_mem(monpriv
);
221 static inline void mon_read_debug(struct mon_msg
*monmsg
,
222 struct mon_private
*monpriv
)
225 u8 msg_type
[2], mca_type
;
226 unsigned long records_len
;
228 records_len
= mon_rec_end(monmsg
) - mon_rec_start(monmsg
) + 1;
230 memcpy(msg_type
, &monmsg
->msg
.class, 2);
232 mca_type
= mon_mca_type(monmsg
, 0);
233 EBCASC(&mca_type
, 1);
235 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
236 monpriv
->read_index
, monpriv
->write_index
);
237 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
238 monpriv
->path
->pathid
, monmsg
->msg
.id
, monmsg
->msg
.class);
239 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
240 msg_type
[0], msg_type
[1], mca_type
? mca_type
: 'X',
241 mon_mca_type(monmsg
, 1), mon_mca_type(monmsg
, 2));
242 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
243 mon_mca_start(monmsg
), mon_mca_end(monmsg
));
244 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
245 mon_rec_start(monmsg
), mon_rec_end(monmsg
), records_len
);
246 if (mon_mca_size(monmsg
) > 12)
247 P_DEBUG("READ, MORE THAN ONE MCA\n\n");
251 static inline void mon_next_mca(struct mon_msg
*monmsg
)
253 if (likely((mon_mca_size(monmsg
) - monmsg
->mca_offset
) == 12))
255 P_DEBUG("READ, NEXT MCA\n\n");
256 monmsg
->mca_offset
+= 12;
260 static struct mon_msg
*mon_next_message(struct mon_private
*monpriv
)
262 struct mon_msg
*monmsg
;
264 if (!atomic_read(&monpriv
->read_ready
))
266 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
267 if (unlikely(monmsg
->replied_msglim
)) {
268 monmsg
->replied_msglim
= 0;
269 monmsg
->msglim_reached
= 0;
271 monmsg
->mca_offset
= 0;
272 P_WARNING("read, message limit reached\n");
273 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
275 atomic_dec(&monpriv
->read_ready
);
276 return ERR_PTR(-EOVERFLOW
);
282 /******************************************************************************
284 *****************************************************************************/
285 static void mon_iucv_path_complete(struct iucv_path
*path
, u8 ipuser
[16])
287 struct mon_private
*monpriv
= path
->private;
289 P_DEBUG("IUCV connection completed\n");
290 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
291 "0x%02X, Sample = 0x%02X\n",
292 ipuser
[0], ipuser
[1], ipuser
[2]);
293 atomic_set(&monpriv
->iucv_connected
, 1);
294 wake_up(&mon_conn_wait_queue
);
297 static void mon_iucv_path_severed(struct iucv_path
*path
, u8 ipuser
[16])
299 struct mon_private
*monpriv
= path
->private;
301 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser
[0]);
302 iucv_path_sever(path
, NULL
);
303 atomic_set(&monpriv
->iucv_severed
, 1);
304 wake_up(&mon_conn_wait_queue
);
305 wake_up_interruptible(&mon_read_wait_queue
);
308 static void mon_iucv_message_pending(struct iucv_path
*path
,
309 struct iucv_message
*msg
)
311 struct mon_private
*monpriv
= path
->private;
313 P_DEBUG("IUCV message pending\n");
314 memcpy(&monpriv
->msg_array
[monpriv
->write_index
]->msg
,
316 if (atomic_inc_return(&monpriv
->msglim_count
) == MON_MSGLIM
) {
317 P_WARNING("IUCV message pending, message limit (%i) reached\n",
319 monpriv
->msg_array
[monpriv
->write_index
]->msglim_reached
= 1;
321 monpriv
->write_index
= (monpriv
->write_index
+ 1) % MON_MSGLIM
;
322 atomic_inc(&monpriv
->read_ready
);
323 wake_up_interruptible(&mon_read_wait_queue
);
326 static struct iucv_handler monreader_iucv_handler
= {
327 .path_complete
= mon_iucv_path_complete
,
328 .path_severed
= mon_iucv_path_severed
,
329 .message_pending
= mon_iucv_message_pending
,
332 /******************************************************************************
334 *****************************************************************************/
335 static int mon_open(struct inode
*inode
, struct file
*filp
)
337 struct mon_private
*monpriv
;
341 * only one user allowed
344 if (test_and_set_bit(MON_IN_USE
, &mon_in_use
))
348 monpriv
= mon_alloc_mem();
353 * Connect to *MONITOR service
355 monpriv
->path
= iucv_path_alloc(MON_MSGLIM
, IUCV_IPRMDATA
, GFP_KERNEL
);
358 rc
= iucv_path_connect(monpriv
->path
, &monreader_iucv_handler
,
359 MON_SERVICE
, NULL
, user_data_connect
, monpriv
);
361 P_ERROR("iucv connection to *MONITOR failed with "
362 "IPUSER SEVER code = %i\n", rc
);
367 * Wait for connection confirmation
369 wait_event(mon_conn_wait_queue
,
370 atomic_read(&monpriv
->iucv_connected
) ||
371 atomic_read(&monpriv
->iucv_severed
));
372 if (atomic_read(&monpriv
->iucv_severed
)) {
373 atomic_set(&monpriv
->iucv_severed
, 0);
374 atomic_set(&monpriv
->iucv_connected
, 0);
378 P_INFO("open, established connection to *MONITOR service\n\n");
379 filp
->private_data
= monpriv
;
380 return nonseekable_open(inode
, filp
);
383 kfree(monpriv
->path
);
385 mon_free_mem(monpriv
);
387 clear_bit(MON_IN_USE
, &mon_in_use
);
392 static int mon_close(struct inode
*inode
, struct file
*filp
)
395 struct mon_private
*monpriv
= filp
->private_data
;
398 * Close IUCV connection and unregister
400 rc
= iucv_path_sever(monpriv
->path
, user_data_sever
);
402 P_ERROR("close, iucv_sever failed with rc = %i\n", rc
);
404 P_INFO("close, terminated connection to *MONITOR service\n");
406 atomic_set(&monpriv
->iucv_severed
, 0);
407 atomic_set(&monpriv
->iucv_connected
, 0);
408 atomic_set(&monpriv
->read_ready
, 0);
409 atomic_set(&monpriv
->msglim_count
, 0);
410 monpriv
->write_index
= 0;
411 monpriv
->read_index
= 0;
413 for (i
= 0; i
< MON_MSGLIM
; i
++)
414 kfree(monpriv
->msg_array
[i
]);
416 clear_bit(MON_IN_USE
, &mon_in_use
);
420 static ssize_t
mon_read(struct file
*filp
, char __user
*data
,
421 size_t count
, loff_t
*ppos
)
423 struct mon_private
*monpriv
= filp
->private_data
;
424 struct mon_msg
*monmsg
;
428 monmsg
= mon_next_message(monpriv
);
430 return PTR_ERR(monmsg
);
433 if (filp
->f_flags
& O_NONBLOCK
)
435 ret
= wait_event_interruptible(mon_read_wait_queue
,
436 atomic_read(&monpriv
->read_ready
) ||
437 atomic_read(&monpriv
->iucv_severed
));
440 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
442 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
446 monmsg
->pos
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
447 mon_read_debug(monmsg
, monpriv
);
449 if (mon_check_mca(monmsg
))
452 /* read monitor control element (12 bytes) first */
453 mce_start
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
454 if ((monmsg
->pos
>= mce_start
) && (monmsg
->pos
< mce_start
+ 12)) {
455 count
= min(count
, (size_t) mce_start
+ 12 - monmsg
->pos
);
456 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
460 monmsg
->pos
+= count
;
461 if (monmsg
->pos
== mce_start
+ 12)
462 monmsg
->pos
= mon_rec_start(monmsg
);
467 if (monmsg
->pos
<= mon_rec_end(monmsg
)) {
468 count
= min(count
, (size_t) mon_rec_end(monmsg
) - monmsg
->pos
470 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
474 monmsg
->pos
+= count
;
475 if (monmsg
->pos
> mon_rec_end(monmsg
))
476 mon_next_mca(monmsg
);
480 ret
= mon_send_reply(monmsg
, monpriv
);
488 static unsigned int mon_poll(struct file
*filp
, struct poll_table_struct
*p
)
490 struct mon_private
*monpriv
= filp
->private_data
;
492 poll_wait(filp
, &mon_read_wait_queue
, p
);
493 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
495 if (atomic_read(&monpriv
->read_ready
))
496 return POLLIN
| POLLRDNORM
;
500 static const struct file_operations mon_fops
= {
501 .owner
= THIS_MODULE
,
503 .release
= &mon_close
,
508 static struct miscdevice mon_dev
= {
511 .minor
= MISC_DYNAMIC_MINOR
,
514 /******************************************************************************
516 *****************************************************************************/
517 static int __init
mon_init(void)
521 if (!MACHINE_IS_VM
) {
522 P_ERROR("not running under z/VM, driver not loaded\n");
527 * Register with IUCV and connect to *MONITOR service
529 rc
= iucv_register(&monreader_iucv_handler
, 1);
531 P_ERROR("failed to register with iucv driver\n");
534 P_INFO("open, registered with IUCV\n");
536 rc
= segment_type(mon_dcss_name
);
538 segment_warning(rc
, mon_dcss_name
);
541 if (rc
!= SEG_TYPE_SC
) {
542 P_ERROR("segment %s has unsupported type, should be SC\n",
548 rc
= segment_load(mon_dcss_name
, SEGMENT_SHARED
,
549 &mon_dcss_start
, &mon_dcss_end
);
551 segment_warning(rc
, mon_dcss_name
);
555 dcss_mkname(mon_dcss_name
, &user_data_connect
[8]);
557 rc
= misc_register(&mon_dev
);
559 P_ERROR("misc_register failed, rc = %i\n", rc
);
562 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
563 mon_dcss_name
, (void *) mon_dcss_start
, (void *) mon_dcss_end
,
564 mon_dcss_end
- mon_dcss_start
+ 1);
568 segment_unload(mon_dcss_name
);
570 iucv_unregister(&monreader_iucv_handler
, 1);
574 static void __exit
mon_exit(void)
576 segment_unload(mon_dcss_name
);
577 WARN_ON(misc_deregister(&mon_dev
) != 0);
578 iucv_unregister(&monreader_iucv_handler
, 1);
583 module_init(mon_init
);
584 module_exit(mon_exit
);
586 module_param_string(mondcss
, mon_dcss_name
, 9, 0444);
587 MODULE_PARM_DESC(mondcss
, "Name of DCSS segment to be used for *MONITOR "
588 "service, max. 8 chars. Default is MONDCSS");
590 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
591 MODULE_DESCRIPTION("Character device driver for reading z/VM "
592 "monitor service records.");
593 MODULE_LICENSE("GPL");