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/smp_lock.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 <asm/uaccess.h>
23 #include <asm/ebcdic.h>
24 #include <asm/extmem.h>
25 #include <linux/poll.h>
26 #include <net/iucv/iucv.h>
29 //#define MON_DEBUG /* Debug messages on/off */
31 #define MON_NAME "monreader"
33 #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
34 #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
35 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
38 #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
40 #define P_DEBUG(x...) do {} while (0)
43 #define MON_COLLECT_SAMPLE 0x80
44 #define MON_COLLECT_EVENT 0x40
45 #define MON_SERVICE "*MONITOR"
46 #define MON_IN_USE 0x01
47 #define MON_MSGLIM 255
49 static char mon_dcss_name[9] = "MONDCSS\0";
54 struct iucv_message msg;
60 struct iucv_path *path;
61 struct mon_msg *msg_array[MON_MSGLIM];
62 unsigned int write_index;
63 unsigned int read_index;
64 atomic_t msglim_count;
66 atomic_t iucv_connected;
67 atomic_t iucv_severed;
70 static unsigned long mon_in_use = 0;
72 static unsigned long mon_dcss_start;
73 static unsigned long mon_dcss_end;
75 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
76 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
78 static u8 user_data_connect[16] = {
79 /* Version code, must be 0x01 for shared mode */
82 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
83 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 static u8 user_data_sever[16] = {
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
94 /******************************************************************************
96 *****************************************************************************/
98 * Create the 8 bytes EBCDIC DCSS segment name from
99 * an ASCII name, incl. padding
101 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
105 for (i = 0; i < 8; i++) {
106 if (ascii_name[i] == '\0')
108 ebcdic_name[i] = toupper(ascii_name[i]);
111 ebcdic_name[i] = ' ';
112 ASCEBC(ebcdic_name, 8);
115 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
117 return *(u32 *) &monmsg->msg.rmmsg;
120 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
122 return *(u32 *) &monmsg->msg.rmmsg[4];
125 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
127 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
130 static inline u32 mon_mca_size(struct mon_msg *monmsg)
132 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
135 static inline u32 mon_rec_start(struct mon_msg *monmsg)
137 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
140 static inline u32 mon_rec_end(struct mon_msg *monmsg)
142 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
145 static int mon_check_mca(struct mon_msg *monmsg)
147 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
148 (mon_rec_start(monmsg) < mon_dcss_start) ||
149 (mon_rec_end(monmsg) > mon_dcss_end) ||
150 (mon_mca_type(monmsg, 0) == 0) ||
151 (mon_mca_size(monmsg) % 12 != 0) ||
152 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
153 (mon_mca_end(monmsg) > mon_dcss_end) ||
154 (mon_mca_start(monmsg) < mon_dcss_start) ||
155 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
157 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
163 static int mon_send_reply(struct mon_msg *monmsg,
164 struct mon_private *monpriv)
168 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
170 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
172 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
173 IUCV_IPRMDATA, NULL, 0);
174 atomic_dec(&monpriv->msglim_count);
175 if (likely(!monmsg->msglim_reached)) {
177 monmsg->mca_offset = 0;
178 monpriv->read_index = (monpriv->read_index + 1) %
180 atomic_dec(&monpriv->read_ready);
182 monmsg->replied_msglim = 1;
184 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
190 static void mon_free_mem(struct mon_private *monpriv)
194 for (i = 0; i < MON_MSGLIM; i++)
195 if (monpriv->msg_array[i])
196 kfree(monpriv->msg_array[i]);
200 static struct mon_private *mon_alloc_mem(void)
203 struct mon_private *monpriv;
205 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
207 P_ERROR("no memory for monpriv\n");
210 for (i = 0; i < MON_MSGLIM; i++) {
211 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
213 if (!monpriv->msg_array[i]) {
214 P_ERROR("open, no memory for msg_array\n");
215 mon_free_mem(monpriv);
222 static inline void mon_read_debug(struct mon_msg *monmsg,
223 struct mon_private *monpriv)
226 u8 msg_type[2], mca_type;
227 unsigned long records_len;
229 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
231 memcpy(msg_type, &monmsg->msg.class, 2);
233 mca_type = mon_mca_type(monmsg, 0);
234 EBCASC(&mca_type, 1);
236 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
237 monpriv->read_index, monpriv->write_index);
238 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
239 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
240 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
241 msg_type[0], msg_type[1], mca_type ? mca_type : 'X',
242 mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2));
243 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
244 mon_mca_start(monmsg), mon_mca_end(monmsg));
245 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
246 mon_rec_start(monmsg), mon_rec_end(monmsg), records_len);
247 if (mon_mca_size(monmsg) > 12)
248 P_DEBUG("READ, MORE THAN ONE MCA\n\n");
252 static inline void mon_next_mca(struct mon_msg *monmsg)
254 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
256 P_DEBUG("READ, NEXT MCA\n\n");
257 monmsg->mca_offset += 12;
261 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
263 struct mon_msg *monmsg;
265 if (!atomic_read(&monpriv->read_ready))
267 monmsg = monpriv->msg_array[monpriv->read_index];
268 if (unlikely(monmsg->replied_msglim)) {
269 monmsg->replied_msglim = 0;
270 monmsg->msglim_reached = 0;
272 monmsg->mca_offset = 0;
273 P_WARNING("read, message limit reached\n");
274 monpriv->read_index = (monpriv->read_index + 1) %
276 atomic_dec(&monpriv->read_ready);
277 return ERR_PTR(-EOVERFLOW);
283 /******************************************************************************
285 *****************************************************************************/
286 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
288 struct mon_private *monpriv = path->private;
290 P_DEBUG("IUCV connection completed\n");
291 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
292 "0x%02X, Sample = 0x%02X\n",
293 ipuser[0], ipuser[1], ipuser[2]);
294 atomic_set(&monpriv->iucv_connected, 1);
295 wake_up(&mon_conn_wait_queue);
298 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
300 struct mon_private *monpriv = path->private;
302 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
303 iucv_path_sever(path, NULL);
304 atomic_set(&monpriv->iucv_severed, 1);
305 wake_up(&mon_conn_wait_queue);
306 wake_up_interruptible(&mon_read_wait_queue);
309 static void mon_iucv_message_pending(struct iucv_path *path,
310 struct iucv_message *msg)
312 struct mon_private *monpriv = path->private;
314 P_DEBUG("IUCV message pending\n");
315 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
317 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
318 P_WARNING("IUCV message pending, message limit (%i) reached\n",
320 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
322 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
323 atomic_inc(&monpriv->read_ready);
324 wake_up_interruptible(&mon_read_wait_queue);
327 static struct iucv_handler monreader_iucv_handler = {
328 .path_complete = mon_iucv_path_complete,
329 .path_severed = mon_iucv_path_severed,
330 .message_pending = mon_iucv_message_pending,
333 /******************************************************************************
335 *****************************************************************************/
336 static int mon_open(struct inode *inode, struct file *filp)
338 struct mon_private *monpriv;
342 * only one user allowed
346 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
350 monpriv = mon_alloc_mem();
355 * Connect to *MONITOR service
357 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
360 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
361 MON_SERVICE, NULL, user_data_connect, monpriv);
363 P_ERROR("iucv connection to *MONITOR failed with "
364 "IPUSER SEVER code = %i\n", rc);
369 * Wait for connection confirmation
371 wait_event(mon_conn_wait_queue,
372 atomic_read(&monpriv->iucv_connected) ||
373 atomic_read(&monpriv->iucv_severed));
374 if (atomic_read(&monpriv->iucv_severed)) {
375 atomic_set(&monpriv->iucv_severed, 0);
376 atomic_set(&monpriv->iucv_connected, 0);
380 P_INFO("open, established connection to *MONITOR service\n\n");
381 filp->private_data = monpriv;
383 return nonseekable_open(inode, filp);
386 kfree(monpriv->path);
388 mon_free_mem(monpriv);
390 clear_bit(MON_IN_USE, &mon_in_use);
396 static int mon_close(struct inode *inode, struct file *filp)
399 struct mon_private *monpriv = filp->private_data;
402 * Close IUCV connection and unregister
404 rc = iucv_path_sever(monpriv->path, user_data_sever);
406 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
408 P_INFO("close, terminated connection to *MONITOR service\n");
410 atomic_set(&monpriv->iucv_severed, 0);
411 atomic_set(&monpriv->iucv_connected, 0);
412 atomic_set(&monpriv->read_ready, 0);
413 atomic_set(&monpriv->msglim_count, 0);
414 monpriv->write_index = 0;
415 monpriv->read_index = 0;
417 for (i = 0; i < MON_MSGLIM; i++)
418 kfree(monpriv->msg_array[i]);
420 clear_bit(MON_IN_USE, &mon_in_use);
424 static ssize_t mon_read(struct file *filp, char __user *data,
425 size_t count, loff_t *ppos)
427 struct mon_private *monpriv = filp->private_data;
428 struct mon_msg *monmsg;
432 monmsg = mon_next_message(monpriv);
434 return PTR_ERR(monmsg);
437 if (filp->f_flags & O_NONBLOCK)
439 ret = wait_event_interruptible(mon_read_wait_queue,
440 atomic_read(&monpriv->read_ready) ||
441 atomic_read(&monpriv->iucv_severed));
444 if (unlikely(atomic_read(&monpriv->iucv_severed)))
446 monmsg = monpriv->msg_array[monpriv->read_index];
450 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
451 mon_read_debug(monmsg, monpriv);
453 if (mon_check_mca(monmsg))
456 /* read monitor control element (12 bytes) first */
457 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
458 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
459 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
460 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
464 monmsg->pos += count;
465 if (monmsg->pos == mce_start + 12)
466 monmsg->pos = mon_rec_start(monmsg);
471 if (monmsg->pos <= mon_rec_end(monmsg)) {
472 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
474 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
478 monmsg->pos += count;
479 if (monmsg->pos > mon_rec_end(monmsg))
480 mon_next_mca(monmsg);
484 ret = mon_send_reply(monmsg, monpriv);
492 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
494 struct mon_private *monpriv = filp->private_data;
496 poll_wait(filp, &mon_read_wait_queue, p);
497 if (unlikely(atomic_read(&monpriv->iucv_severed)))
499 if (atomic_read(&monpriv->read_ready))
500 return POLLIN | POLLRDNORM;
504 static const struct file_operations mon_fops = {
505 .owner = THIS_MODULE,
507 .release = &mon_close,
512 static struct miscdevice mon_dev = {
515 .minor = MISC_DYNAMIC_MINOR,
518 /******************************************************************************
520 *****************************************************************************/
521 static int __init mon_init(void)
525 if (!MACHINE_IS_VM) {
526 P_ERROR("not running under z/VM, driver not loaded\n");
531 * Register with IUCV and connect to *MONITOR service
533 rc = iucv_register(&monreader_iucv_handler, 1);
535 P_ERROR("failed to register with iucv driver\n");
538 P_INFO("open, registered with IUCV\n");
540 rc = segment_type(mon_dcss_name);
542 segment_warning(rc, mon_dcss_name);
545 if (rc != SEG_TYPE_SC) {
546 P_ERROR("segment %s has unsupported type, should be SC\n",
552 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
553 &mon_dcss_start, &mon_dcss_end);
555 segment_warning(rc, mon_dcss_name);
559 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
561 rc = misc_register(&mon_dev);
563 P_ERROR("misc_register failed, rc = %i\n", rc);
566 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
567 mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end,
568 mon_dcss_end - mon_dcss_start + 1);
572 segment_unload(mon_dcss_name);
574 iucv_unregister(&monreader_iucv_handler, 1);
578 static void __exit mon_exit(void)
580 segment_unload(mon_dcss_name);
581 WARN_ON(misc_deregister(&mon_dev) != 0);
582 iucv_unregister(&monreader_iucv_handler, 1);
587 module_init(mon_init);
588 module_exit(mon_exit);
590 module_param_string(mondcss, mon_dcss_name, 9, 0444);
591 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
592 "service, max. 8 chars. Default is MONDCSS");
594 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
595 MODULE_DESCRIPTION("Character device driver for reading z/VM "
596 "monitor service records.");
597 MODULE_LICENSE("GPL");