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/errno.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/miscdevice.h>
17 #include <linux/ctype.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <linux/poll.h>
21 #include <net/iucv/iucv.h>
22 #include <asm/uaccess.h>
23 #include <asm/ebcdic.h>
24 #include <asm/extmem.h>
26 //#define MON_DEBUG /* Debug messages on/off */
28 #define MON_NAME "monreader"
30 #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
31 #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
32 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
35 #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
37 #define P_DEBUG(x...) do {} while (0)
40 #define MON_COLLECT_SAMPLE 0x80
41 #define MON_COLLECT_EVENT 0x40
42 #define MON_SERVICE "*MONITOR"
43 #define MON_IN_USE 0x01
44 #define MON_MSGLIM 255
46 static char mon_dcss_name[9] = "MONDCSS\0";
51 struct iucv_message msg;
57 struct iucv_path *path;
58 struct mon_msg *msg_array[MON_MSGLIM];
59 unsigned int write_index;
60 unsigned int read_index;
61 atomic_t msglim_count;
63 atomic_t iucv_connected;
64 atomic_t iucv_severed;
67 static unsigned long mon_in_use = 0;
69 static unsigned long mon_dcss_start;
70 static unsigned long mon_dcss_end;
72 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
73 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
75 static u8 user_data_connect[16] = {
76 /* Version code, must be 0x01 for shared mode */
79 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
80 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 static u8 user_data_sever[16] = {
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91 /******************************************************************************
93 *****************************************************************************/
95 * Create the 8 bytes EBCDIC DCSS segment name from
96 * an ASCII name, incl. padding
98 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
102 for (i = 0; i < 8; i++) {
103 if (ascii_name[i] == '\0')
105 ebcdic_name[i] = toupper(ascii_name[i]);
108 ebcdic_name[i] = ' ';
109 ASCEBC(ebcdic_name, 8);
112 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
114 return *(u32 *) &monmsg->msg.rmmsg;
117 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
119 return *(u32 *) &monmsg->msg.rmmsg[4];
122 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
124 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
127 static inline u32 mon_mca_size(struct mon_msg *monmsg)
129 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
132 static inline u32 mon_rec_start(struct mon_msg *monmsg)
134 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
137 static inline u32 mon_rec_end(struct mon_msg *monmsg)
139 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
142 static int mon_check_mca(struct mon_msg *monmsg)
144 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
145 (mon_rec_start(monmsg) < mon_dcss_start) ||
146 (mon_rec_end(monmsg) > mon_dcss_end) ||
147 (mon_mca_type(monmsg, 0) == 0) ||
148 (mon_mca_size(monmsg) % 12 != 0) ||
149 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
150 (mon_mca_end(monmsg) > mon_dcss_end) ||
151 (mon_mca_start(monmsg) < mon_dcss_start) ||
152 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
157 static int mon_send_reply(struct mon_msg *monmsg,
158 struct mon_private *monpriv)
162 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
163 IUCV_IPRMDATA, NULL, 0);
164 atomic_dec(&monpriv->msglim_count);
165 if (likely(!monmsg->msglim_reached)) {
167 monmsg->mca_offset = 0;
168 monpriv->read_index = (monpriv->read_index + 1) %
170 atomic_dec(&monpriv->read_ready);
172 monmsg->replied_msglim = 1;
174 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
180 static void mon_free_mem(struct mon_private *monpriv)
184 for (i = 0; i < MON_MSGLIM; i++)
185 if (monpriv->msg_array[i])
186 kfree(monpriv->msg_array[i]);
190 static struct mon_private *mon_alloc_mem(void)
193 struct mon_private *monpriv;
195 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
198 for (i = 0; i < MON_MSGLIM; i++) {
199 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
201 if (!monpriv->msg_array[i]) {
202 mon_free_mem(monpriv);
209 static inline void mon_next_mca(struct mon_msg *monmsg)
211 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
213 monmsg->mca_offset += 12;
217 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
219 struct mon_msg *monmsg;
221 if (!atomic_read(&monpriv->read_ready))
223 monmsg = monpriv->msg_array[monpriv->read_index];
224 if (unlikely(monmsg->replied_msglim)) {
225 monmsg->replied_msglim = 0;
226 monmsg->msglim_reached = 0;
228 monmsg->mca_offset = 0;
229 monpriv->read_index = (monpriv->read_index + 1) %
231 atomic_dec(&monpriv->read_ready);
232 return ERR_PTR(-EOVERFLOW);
238 /******************************************************************************
240 *****************************************************************************/
241 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
243 struct mon_private *monpriv = path->private;
245 atomic_set(&monpriv->iucv_connected, 1);
246 wake_up(&mon_conn_wait_queue);
249 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
251 struct mon_private *monpriv = path->private;
253 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
254 iucv_path_sever(path, NULL);
255 atomic_set(&monpriv->iucv_severed, 1);
256 wake_up(&mon_conn_wait_queue);
257 wake_up_interruptible(&mon_read_wait_queue);
260 static void mon_iucv_message_pending(struct iucv_path *path,
261 struct iucv_message *msg)
263 struct mon_private *monpriv = path->private;
265 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
267 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
268 P_WARNING("IUCV message pending, message limit (%i) reached\n",
270 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
272 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
273 atomic_inc(&monpriv->read_ready);
274 wake_up_interruptible(&mon_read_wait_queue);
277 static struct iucv_handler monreader_iucv_handler = {
278 .path_complete = mon_iucv_path_complete,
279 .path_severed = mon_iucv_path_severed,
280 .message_pending = mon_iucv_message_pending,
283 /******************************************************************************
285 *****************************************************************************/
286 static int mon_open(struct inode *inode, struct file *filp)
288 struct mon_private *monpriv;
292 * only one user allowed
295 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
299 monpriv = mon_alloc_mem();
304 * Connect to *MONITOR service
306 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
309 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
310 MON_SERVICE, NULL, user_data_connect, monpriv);
312 P_ERROR("iucv connection to *MONITOR failed with "
313 "IPUSER SEVER code = %i\n", rc);
318 * Wait for connection confirmation
320 wait_event(mon_conn_wait_queue,
321 atomic_read(&monpriv->iucv_connected) ||
322 atomic_read(&monpriv->iucv_severed));
323 if (atomic_read(&monpriv->iucv_severed)) {
324 atomic_set(&monpriv->iucv_severed, 0);
325 atomic_set(&monpriv->iucv_connected, 0);
329 filp->private_data = monpriv;
330 return nonseekable_open(inode, filp);
333 kfree(monpriv->path);
335 mon_free_mem(monpriv);
337 clear_bit(MON_IN_USE, &mon_in_use);
342 static int mon_close(struct inode *inode, struct file *filp)
345 struct mon_private *monpriv = filp->private_data;
348 * Close IUCV connection and unregister
350 rc = iucv_path_sever(monpriv->path, user_data_sever);
352 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
354 atomic_set(&monpriv->iucv_severed, 0);
355 atomic_set(&monpriv->iucv_connected, 0);
356 atomic_set(&monpriv->read_ready, 0);
357 atomic_set(&monpriv->msglim_count, 0);
358 monpriv->write_index = 0;
359 monpriv->read_index = 0;
361 for (i = 0; i < MON_MSGLIM; i++)
362 kfree(monpriv->msg_array[i]);
364 clear_bit(MON_IN_USE, &mon_in_use);
368 static ssize_t mon_read(struct file *filp, char __user *data,
369 size_t count, loff_t *ppos)
371 struct mon_private *monpriv = filp->private_data;
372 struct mon_msg *monmsg;
376 monmsg = mon_next_message(monpriv);
378 return PTR_ERR(monmsg);
381 if (filp->f_flags & O_NONBLOCK)
383 ret = wait_event_interruptible(mon_read_wait_queue,
384 atomic_read(&monpriv->read_ready) ||
385 atomic_read(&monpriv->iucv_severed));
388 if (unlikely(atomic_read(&monpriv->iucv_severed)))
390 monmsg = monpriv->msg_array[monpriv->read_index];
394 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
395 if (mon_check_mca(monmsg))
398 /* read monitor control element (12 bytes) first */
399 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
400 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
401 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
402 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
406 monmsg->pos += count;
407 if (monmsg->pos == mce_start + 12)
408 monmsg->pos = mon_rec_start(monmsg);
413 if (monmsg->pos <= mon_rec_end(monmsg)) {
414 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
416 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
420 monmsg->pos += count;
421 if (monmsg->pos > mon_rec_end(monmsg))
422 mon_next_mca(monmsg);
426 ret = mon_send_reply(monmsg, monpriv);
434 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
436 struct mon_private *monpriv = filp->private_data;
438 poll_wait(filp, &mon_read_wait_queue, p);
439 if (unlikely(atomic_read(&monpriv->iucv_severed)))
441 if (atomic_read(&monpriv->read_ready))
442 return POLLIN | POLLRDNORM;
446 static const struct file_operations mon_fops = {
447 .owner = THIS_MODULE,
449 .release = &mon_close,
454 static struct miscdevice mon_dev = {
457 .minor = MISC_DYNAMIC_MINOR,
460 /******************************************************************************
462 *****************************************************************************/
463 static int __init mon_init(void)
467 if (!MACHINE_IS_VM) {
468 P_ERROR("not running under z/VM, driver not loaded\n");
473 * Register with IUCV and connect to *MONITOR service
475 rc = iucv_register(&monreader_iucv_handler, 1);
477 P_ERROR("failed to register with iucv driver\n");
481 rc = segment_type(mon_dcss_name);
483 segment_warning(rc, mon_dcss_name);
486 if (rc != SEG_TYPE_SC) {
487 P_ERROR("segment %s has unsupported type, should be SC\n",
493 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
494 &mon_dcss_start, &mon_dcss_end);
496 segment_warning(rc, mon_dcss_name);
500 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
502 rc = misc_register(&mon_dev);
508 segment_unload(mon_dcss_name);
510 iucv_unregister(&monreader_iucv_handler, 1);
514 static void __exit mon_exit(void)
516 segment_unload(mon_dcss_name);
517 WARN_ON(misc_deregister(&mon_dev) != 0);
518 iucv_unregister(&monreader_iucv_handler, 1);
523 module_init(mon_init);
524 module_exit(mon_exit);
526 module_param_string(mondcss, mon_dcss_name, 9, 0444);
527 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
528 "service, max. 8 chars. Default is MONDCSS");
530 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
531 MODULE_DESCRIPTION("Character device driver for reading z/VM "
532 "monitor service records.");
533 MODULE_LICENSE("GPL");