2 * drivers/s390/char/monreader.c
4 * Character device driver for reading z/VM *MONITOR service records.
6 * Copyright IBM Corp. 2004, 2008
7 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/smp_lock.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 <linux/poll.h>
22 #include <net/iucv/iucv.h>
23 #include <asm/uaccess.h>
24 #include <asm/ebcdic.h>
25 #include <asm/extmem.h>
27 //#define MON_DEBUG /* Debug messages on/off */
29 #define MON_NAME "monreader"
31 #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
32 #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
33 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
36 #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
38 #define P_DEBUG(x...) do {} while (0)
41 #define MON_COLLECT_SAMPLE 0x80
42 #define MON_COLLECT_EVENT 0x40
43 #define MON_SERVICE "*MONITOR"
44 #define MON_IN_USE 0x01
45 #define MON_MSGLIM 255
47 static char mon_dcss_name[9] = "MONDCSS\0";
52 struct iucv_message msg;
58 struct iucv_path *path;
59 struct mon_msg *msg_array[MON_MSGLIM];
60 unsigned int write_index;
61 unsigned int read_index;
62 atomic_t msglim_count;
64 atomic_t iucv_connected;
65 atomic_t iucv_severed;
68 static unsigned long mon_in_use = 0;
70 static unsigned long mon_dcss_start;
71 static unsigned long mon_dcss_end;
73 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
74 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
76 static u8 user_data_connect[16] = {
77 /* Version code, must be 0x01 for shared mode */
80 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
81 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 static u8 user_data_sever[16] = {
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92 /******************************************************************************
94 *****************************************************************************/
96 * Create the 8 bytes EBCDIC DCSS segment name from
97 * an ASCII name, incl. padding
99 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
103 for (i = 0; i < 8; i++) {
104 if (ascii_name[i] == '\0')
106 ebcdic_name[i] = toupper(ascii_name[i]);
109 ebcdic_name[i] = ' ';
110 ASCEBC(ebcdic_name, 8);
113 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
115 return *(u32 *) &monmsg->msg.rmmsg;
118 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
120 return *(u32 *) &monmsg->msg.rmmsg[4];
123 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
125 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
128 static inline u32 mon_mca_size(struct mon_msg *monmsg)
130 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
133 static inline u32 mon_rec_start(struct mon_msg *monmsg)
135 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
138 static inline u32 mon_rec_end(struct mon_msg *monmsg)
140 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
143 static int mon_check_mca(struct mon_msg *monmsg)
145 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
146 (mon_rec_start(monmsg) < mon_dcss_start) ||
147 (mon_rec_end(monmsg) > mon_dcss_end) ||
148 (mon_mca_type(monmsg, 0) == 0) ||
149 (mon_mca_size(monmsg) % 12 != 0) ||
150 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
151 (mon_mca_end(monmsg) > mon_dcss_end) ||
152 (mon_mca_start(monmsg) < mon_dcss_start) ||
153 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
158 static int mon_send_reply(struct mon_msg *monmsg,
159 struct mon_private *monpriv)
163 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
164 IUCV_IPRMDATA, NULL, 0);
165 atomic_dec(&monpriv->msglim_count);
166 if (likely(!monmsg->msglim_reached)) {
168 monmsg->mca_offset = 0;
169 monpriv->read_index = (monpriv->read_index + 1) %
171 atomic_dec(&monpriv->read_ready);
173 monmsg->replied_msglim = 1;
175 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
181 static void mon_free_mem(struct mon_private *monpriv)
185 for (i = 0; i < MON_MSGLIM; i++)
186 if (monpriv->msg_array[i])
187 kfree(monpriv->msg_array[i]);
191 static struct mon_private *mon_alloc_mem(void)
194 struct mon_private *monpriv;
196 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
199 for (i = 0; i < MON_MSGLIM; i++) {
200 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
202 if (!monpriv->msg_array[i]) {
203 mon_free_mem(monpriv);
210 static inline void mon_next_mca(struct mon_msg *monmsg)
212 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
214 monmsg->mca_offset += 12;
218 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
220 struct mon_msg *monmsg;
222 if (!atomic_read(&monpriv->read_ready))
224 monmsg = monpriv->msg_array[monpriv->read_index];
225 if (unlikely(monmsg->replied_msglim)) {
226 monmsg->replied_msglim = 0;
227 monmsg->msglim_reached = 0;
229 monmsg->mca_offset = 0;
230 monpriv->read_index = (monpriv->read_index + 1) %
232 atomic_dec(&monpriv->read_ready);
233 return ERR_PTR(-EOVERFLOW);
239 /******************************************************************************
241 *****************************************************************************/
242 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
244 struct mon_private *monpriv = path->private;
246 atomic_set(&monpriv->iucv_connected, 1);
247 wake_up(&mon_conn_wait_queue);
250 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
252 struct mon_private *monpriv = path->private;
254 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
255 iucv_path_sever(path, NULL);
256 atomic_set(&monpriv->iucv_severed, 1);
257 wake_up(&mon_conn_wait_queue);
258 wake_up_interruptible(&mon_read_wait_queue);
261 static void mon_iucv_message_pending(struct iucv_path *path,
262 struct iucv_message *msg)
264 struct mon_private *monpriv = path->private;
266 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
268 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
269 P_WARNING("IUCV message pending, message limit (%i) reached\n",
271 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
273 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
274 atomic_inc(&monpriv->read_ready);
275 wake_up_interruptible(&mon_read_wait_queue);
278 static struct iucv_handler monreader_iucv_handler = {
279 .path_complete = mon_iucv_path_complete,
280 .path_severed = mon_iucv_path_severed,
281 .message_pending = mon_iucv_message_pending,
284 /******************************************************************************
286 *****************************************************************************/
287 static int mon_open(struct inode *inode, struct file *filp)
289 struct mon_private *monpriv;
293 * only one user allowed
297 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
301 monpriv = mon_alloc_mem();
306 * Connect to *MONITOR service
308 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
311 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
312 MON_SERVICE, NULL, user_data_connect, monpriv);
314 P_ERROR("iucv connection to *MONITOR failed with "
315 "IPUSER SEVER code = %i\n", rc);
320 * Wait for connection confirmation
322 wait_event(mon_conn_wait_queue,
323 atomic_read(&monpriv->iucv_connected) ||
324 atomic_read(&monpriv->iucv_severed));
325 if (atomic_read(&monpriv->iucv_severed)) {
326 atomic_set(&monpriv->iucv_severed, 0);
327 atomic_set(&monpriv->iucv_connected, 0);
331 filp->private_data = monpriv;
333 return nonseekable_open(inode, filp);
336 kfree(monpriv->path);
338 mon_free_mem(monpriv);
340 clear_bit(MON_IN_USE, &mon_in_use);
346 static int mon_close(struct inode *inode, struct file *filp)
349 struct mon_private *monpriv = filp->private_data;
352 * Close IUCV connection and unregister
354 rc = iucv_path_sever(monpriv->path, user_data_sever);
356 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
358 atomic_set(&monpriv->iucv_severed, 0);
359 atomic_set(&monpriv->iucv_connected, 0);
360 atomic_set(&monpriv->read_ready, 0);
361 atomic_set(&monpriv->msglim_count, 0);
362 monpriv->write_index = 0;
363 monpriv->read_index = 0;
365 for (i = 0; i < MON_MSGLIM; i++)
366 kfree(monpriv->msg_array[i]);
368 clear_bit(MON_IN_USE, &mon_in_use);
372 static ssize_t mon_read(struct file *filp, char __user *data,
373 size_t count, loff_t *ppos)
375 struct mon_private *monpriv = filp->private_data;
376 struct mon_msg *monmsg;
380 monmsg = mon_next_message(monpriv);
382 return PTR_ERR(monmsg);
385 if (filp->f_flags & O_NONBLOCK)
387 ret = wait_event_interruptible(mon_read_wait_queue,
388 atomic_read(&monpriv->read_ready) ||
389 atomic_read(&monpriv->iucv_severed));
392 if (unlikely(atomic_read(&monpriv->iucv_severed)))
394 monmsg = monpriv->msg_array[monpriv->read_index];
398 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
399 if (mon_check_mca(monmsg))
402 /* read monitor control element (12 bytes) first */
403 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
404 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
405 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
406 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
410 monmsg->pos += count;
411 if (monmsg->pos == mce_start + 12)
412 monmsg->pos = mon_rec_start(monmsg);
417 if (monmsg->pos <= mon_rec_end(monmsg)) {
418 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
420 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
424 monmsg->pos += count;
425 if (monmsg->pos > mon_rec_end(monmsg))
426 mon_next_mca(monmsg);
430 ret = mon_send_reply(monmsg, monpriv);
438 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
440 struct mon_private *monpriv = filp->private_data;
442 poll_wait(filp, &mon_read_wait_queue, p);
443 if (unlikely(atomic_read(&monpriv->iucv_severed)))
445 if (atomic_read(&monpriv->read_ready))
446 return POLLIN | POLLRDNORM;
450 static const struct file_operations mon_fops = {
451 .owner = THIS_MODULE,
453 .release = &mon_close,
458 static struct miscdevice mon_dev = {
461 .minor = MISC_DYNAMIC_MINOR,
464 /******************************************************************************
466 *****************************************************************************/
467 static int __init mon_init(void)
471 if (!MACHINE_IS_VM) {
472 P_ERROR("not running under z/VM, driver not loaded\n");
477 * Register with IUCV and connect to *MONITOR service
479 rc = iucv_register(&monreader_iucv_handler, 1);
481 P_ERROR("failed to register with iucv driver\n");
485 rc = segment_type(mon_dcss_name);
487 segment_warning(rc, mon_dcss_name);
490 if (rc != SEG_TYPE_SC) {
491 P_ERROR("segment %s has unsupported type, should be SC\n",
497 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
498 &mon_dcss_start, &mon_dcss_end);
500 segment_warning(rc, mon_dcss_name);
504 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
506 rc = misc_register(&mon_dev);
512 segment_unload(mon_dcss_name);
514 iucv_unregister(&monreader_iucv_handler, 1);
518 static void __exit mon_exit(void)
520 segment_unload(mon_dcss_name);
521 WARN_ON(misc_deregister(&mon_dev) != 0);
522 iucv_unregister(&monreader_iucv_handler, 1);
527 module_init(mon_init);
528 module_exit(mon_exit);
530 module_param_string(mondcss, mon_dcss_name, 9, 0444);
531 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
532 "service, max. 8 chars. Default is MONDCSS");
534 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
535 MODULE_DESCRIPTION("Character device driver for reading z/VM "
536 "monitor service records.");
537 MODULE_LICENSE("GPL");