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 #define KMSG_COMPONENT "monreader"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/smp_lock.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/ctype.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/poll.h>
25 #include <net/iucv/iucv.h>
26 #include <asm/uaccess.h>
27 #include <asm/ebcdic.h>
28 #include <asm/extmem.h>
31 #define MON_COLLECT_SAMPLE 0x80
32 #define MON_COLLECT_EVENT 0x40
33 #define MON_SERVICE "*MONITOR"
34 #define MON_IN_USE 0x01
35 #define MON_MSGLIM 255
37 static char mon_dcss_name
[9] = "MONDCSS\0";
42 struct iucv_message msg
;
48 struct iucv_path
*path
;
49 struct mon_msg
*msg_array
[MON_MSGLIM
];
50 unsigned int write_index
;
51 unsigned int read_index
;
52 atomic_t msglim_count
;
54 atomic_t iucv_connected
;
55 atomic_t iucv_severed
;
58 static unsigned long mon_in_use
= 0;
60 static unsigned long mon_dcss_start
;
61 static unsigned long mon_dcss_end
;
63 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue
);
64 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue
);
66 static u8 user_data_connect
[16] = {
67 /* Version code, must be 0x01 for shared mode */
70 MON_COLLECT_SAMPLE
| MON_COLLECT_EVENT
,
71 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 static u8 user_data_sever
[16] = {
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 /******************************************************************************
84 *****************************************************************************/
86 * Create the 8 bytes EBCDIC DCSS segment name from
87 * an ASCII name, incl. padding
89 static void dcss_mkname(char *ascii_name
, char *ebcdic_name
)
93 for (i
= 0; i
< 8; i
++) {
94 if (ascii_name
[i
] == '\0')
96 ebcdic_name
[i
] = toupper(ascii_name
[i
]);
100 ASCEBC(ebcdic_name
, 8);
103 static inline unsigned long mon_mca_start(struct mon_msg
*monmsg
)
105 return *(u32
*) &monmsg
->msg
.rmmsg
;
108 static inline unsigned long mon_mca_end(struct mon_msg
*monmsg
)
110 return *(u32
*) &monmsg
->msg
.rmmsg
[4];
113 static inline u8
mon_mca_type(struct mon_msg
*monmsg
, u8 index
)
115 return *((u8
*) mon_mca_start(monmsg
) + monmsg
->mca_offset
+ index
);
118 static inline u32
mon_mca_size(struct mon_msg
*monmsg
)
120 return mon_mca_end(monmsg
) - mon_mca_start(monmsg
) + 1;
123 static inline u32
mon_rec_start(struct mon_msg
*monmsg
)
125 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 4));
128 static inline u32
mon_rec_end(struct mon_msg
*monmsg
)
130 return *((u32
*) (mon_mca_start(monmsg
) + monmsg
->mca_offset
+ 8));
133 static int mon_check_mca(struct mon_msg
*monmsg
)
135 if ((mon_rec_end(monmsg
) <= mon_rec_start(monmsg
)) ||
136 (mon_rec_start(monmsg
) < mon_dcss_start
) ||
137 (mon_rec_end(monmsg
) > mon_dcss_end
) ||
138 (mon_mca_type(monmsg
, 0) == 0) ||
139 (mon_mca_size(monmsg
) % 12 != 0) ||
140 (mon_mca_end(monmsg
) <= mon_mca_start(monmsg
)) ||
141 (mon_mca_end(monmsg
) > mon_dcss_end
) ||
142 (mon_mca_start(monmsg
) < mon_dcss_start
) ||
143 ((mon_mca_type(monmsg
, 1) == 0) && (mon_mca_type(monmsg
, 2) == 0)))
148 static int mon_send_reply(struct mon_msg
*monmsg
,
149 struct mon_private
*monpriv
)
153 rc
= iucv_message_reply(monpriv
->path
, &monmsg
->msg
,
154 IUCV_IPRMDATA
, NULL
, 0);
155 atomic_dec(&monpriv
->msglim_count
);
156 if (likely(!monmsg
->msglim_reached
)) {
158 monmsg
->mca_offset
= 0;
159 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
161 atomic_dec(&monpriv
->read_ready
);
163 monmsg
->replied_msglim
= 1;
165 pr_err("Reading monitor data failed with rc=%i\n", rc
);
171 static void mon_free_mem(struct mon_private
*monpriv
)
175 for (i
= 0; i
< MON_MSGLIM
; i
++)
176 if (monpriv
->msg_array
[i
])
177 kfree(monpriv
->msg_array
[i
]);
181 static struct mon_private
*mon_alloc_mem(void)
184 struct mon_private
*monpriv
;
186 monpriv
= kzalloc(sizeof(struct mon_private
), GFP_KERNEL
);
189 for (i
= 0; i
< MON_MSGLIM
; i
++) {
190 monpriv
->msg_array
[i
] = kzalloc(sizeof(struct mon_msg
),
192 if (!monpriv
->msg_array
[i
]) {
193 mon_free_mem(monpriv
);
200 static inline void mon_next_mca(struct mon_msg
*monmsg
)
202 if (likely((mon_mca_size(monmsg
) - monmsg
->mca_offset
) == 12))
204 monmsg
->mca_offset
+= 12;
208 static struct mon_msg
*mon_next_message(struct mon_private
*monpriv
)
210 struct mon_msg
*monmsg
;
212 if (!atomic_read(&monpriv
->read_ready
))
214 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
215 if (unlikely(monmsg
->replied_msglim
)) {
216 monmsg
->replied_msglim
= 0;
217 monmsg
->msglim_reached
= 0;
219 monmsg
->mca_offset
= 0;
220 monpriv
->read_index
= (monpriv
->read_index
+ 1) %
222 atomic_dec(&monpriv
->read_ready
);
223 return ERR_PTR(-EOVERFLOW
);
229 /******************************************************************************
231 *****************************************************************************/
232 static void mon_iucv_path_complete(struct iucv_path
*path
, u8 ipuser
[16])
234 struct mon_private
*monpriv
= path
->private;
236 atomic_set(&monpriv
->iucv_connected
, 1);
237 wake_up(&mon_conn_wait_queue
);
240 static void mon_iucv_path_severed(struct iucv_path
*path
, u8 ipuser
[16])
242 struct mon_private
*monpriv
= path
->private;
244 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
246 iucv_path_sever(path
, NULL
);
247 atomic_set(&monpriv
->iucv_severed
, 1);
248 wake_up(&mon_conn_wait_queue
);
249 wake_up_interruptible(&mon_read_wait_queue
);
252 static void mon_iucv_message_pending(struct iucv_path
*path
,
253 struct iucv_message
*msg
)
255 struct mon_private
*monpriv
= path
->private;
257 memcpy(&monpriv
->msg_array
[monpriv
->write_index
]->msg
,
259 if (atomic_inc_return(&monpriv
->msglim_count
) == MON_MSGLIM
) {
260 pr_warning("The read queue for monitor data is full\n");
261 monpriv
->msg_array
[monpriv
->write_index
]->msglim_reached
= 1;
263 monpriv
->write_index
= (monpriv
->write_index
+ 1) % MON_MSGLIM
;
264 atomic_inc(&monpriv
->read_ready
);
265 wake_up_interruptible(&mon_read_wait_queue
);
268 static struct iucv_handler monreader_iucv_handler
= {
269 .path_complete
= mon_iucv_path_complete
,
270 .path_severed
= mon_iucv_path_severed
,
271 .message_pending
= mon_iucv_message_pending
,
274 /******************************************************************************
276 *****************************************************************************/
277 static int mon_open(struct inode
*inode
, struct file
*filp
)
279 struct mon_private
*monpriv
;
283 * only one user allowed
287 if (test_and_set_bit(MON_IN_USE
, &mon_in_use
))
291 monpriv
= mon_alloc_mem();
296 * Connect to *MONITOR service
298 monpriv
->path
= iucv_path_alloc(MON_MSGLIM
, IUCV_IPRMDATA
, GFP_KERNEL
);
301 rc
= iucv_path_connect(monpriv
->path
, &monreader_iucv_handler
,
302 MON_SERVICE
, NULL
, user_data_connect
, monpriv
);
304 pr_err("Connecting to the z/VM *MONITOR system service "
305 "failed with rc=%i\n", rc
);
310 * Wait for connection confirmation
312 wait_event(mon_conn_wait_queue
,
313 atomic_read(&monpriv
->iucv_connected
) ||
314 atomic_read(&monpriv
->iucv_severed
));
315 if (atomic_read(&monpriv
->iucv_severed
)) {
316 atomic_set(&monpriv
->iucv_severed
, 0);
317 atomic_set(&monpriv
->iucv_connected
, 0);
321 filp
->private_data
= monpriv
;
323 return nonseekable_open(inode
, filp
);
326 kfree(monpriv
->path
);
328 mon_free_mem(monpriv
);
330 clear_bit(MON_IN_USE
, &mon_in_use
);
336 static int mon_close(struct inode
*inode
, struct file
*filp
)
339 struct mon_private
*monpriv
= filp
->private_data
;
342 * Close IUCV connection and unregister
344 rc
= iucv_path_sever(monpriv
->path
, user_data_sever
);
346 pr_warning("Disconnecting the z/VM *MONITOR system service "
347 "failed with rc=%i\n", rc
);
349 atomic_set(&monpriv
->iucv_severed
, 0);
350 atomic_set(&monpriv
->iucv_connected
, 0);
351 atomic_set(&monpriv
->read_ready
, 0);
352 atomic_set(&monpriv
->msglim_count
, 0);
353 monpriv
->write_index
= 0;
354 monpriv
->read_index
= 0;
356 for (i
= 0; i
< MON_MSGLIM
; i
++)
357 kfree(monpriv
->msg_array
[i
]);
359 clear_bit(MON_IN_USE
, &mon_in_use
);
363 static ssize_t
mon_read(struct file
*filp
, char __user
*data
,
364 size_t count
, loff_t
*ppos
)
366 struct mon_private
*monpriv
= filp
->private_data
;
367 struct mon_msg
*monmsg
;
371 monmsg
= mon_next_message(monpriv
);
373 return PTR_ERR(monmsg
);
376 if (filp
->f_flags
& O_NONBLOCK
)
378 ret
= wait_event_interruptible(mon_read_wait_queue
,
379 atomic_read(&monpriv
->read_ready
) ||
380 atomic_read(&monpriv
->iucv_severed
));
383 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
385 monmsg
= monpriv
->msg_array
[monpriv
->read_index
];
389 monmsg
->pos
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
390 if (mon_check_mca(monmsg
))
393 /* read monitor control element (12 bytes) first */
394 mce_start
= mon_mca_start(monmsg
) + monmsg
->mca_offset
;
395 if ((monmsg
->pos
>= mce_start
) && (monmsg
->pos
< mce_start
+ 12)) {
396 count
= min(count
, (size_t) mce_start
+ 12 - monmsg
->pos
);
397 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
401 monmsg
->pos
+= count
;
402 if (monmsg
->pos
== mce_start
+ 12)
403 monmsg
->pos
= mon_rec_start(monmsg
);
408 if (monmsg
->pos
<= mon_rec_end(monmsg
)) {
409 count
= min(count
, (size_t) mon_rec_end(monmsg
) - monmsg
->pos
411 ret
= copy_to_user(data
, (void *) (unsigned long) monmsg
->pos
,
415 monmsg
->pos
+= count
;
416 if (monmsg
->pos
> mon_rec_end(monmsg
))
417 mon_next_mca(monmsg
);
421 ret
= mon_send_reply(monmsg
, monpriv
);
429 static unsigned int mon_poll(struct file
*filp
, struct poll_table_struct
*p
)
431 struct mon_private
*monpriv
= filp
->private_data
;
433 poll_wait(filp
, &mon_read_wait_queue
, p
);
434 if (unlikely(atomic_read(&monpriv
->iucv_severed
)))
436 if (atomic_read(&monpriv
->read_ready
))
437 return POLLIN
| POLLRDNORM
;
441 static const struct file_operations mon_fops
= {
442 .owner
= THIS_MODULE
,
444 .release
= &mon_close
,
449 static struct miscdevice mon_dev
= {
452 .minor
= MISC_DYNAMIC_MINOR
,
455 /******************************************************************************
457 *****************************************************************************/
458 static int __init
mon_init(void)
462 if (!MACHINE_IS_VM
) {
463 pr_err("The z/VM *MONITOR record device driver cannot be "
464 "loaded without z/VM\n");
469 * Register with IUCV and connect to *MONITOR service
471 rc
= iucv_register(&monreader_iucv_handler
, 1);
473 pr_err("The z/VM *MONITOR record device driver failed to "
474 "register with IUCV\n");
478 rc
= segment_type(mon_dcss_name
);
480 segment_warning(rc
, mon_dcss_name
);
483 if (rc
!= SEG_TYPE_SC
) {
484 pr_err("The specified *MONITOR DCSS %s does not have the "
485 "required type SC\n", mon_dcss_name
);
490 rc
= segment_load(mon_dcss_name
, SEGMENT_SHARED
,
491 &mon_dcss_start
, &mon_dcss_end
);
493 segment_warning(rc
, mon_dcss_name
);
497 dcss_mkname(mon_dcss_name
, &user_data_connect
[8]);
499 rc
= misc_register(&mon_dev
);
505 segment_unload(mon_dcss_name
);
507 iucv_unregister(&monreader_iucv_handler
, 1);
511 static void __exit
mon_exit(void)
513 segment_unload(mon_dcss_name
);
514 WARN_ON(misc_deregister(&mon_dev
) != 0);
515 iucv_unregister(&monreader_iucv_handler
, 1);
520 module_init(mon_init
);
521 module_exit(mon_exit
);
523 module_param_string(mondcss
, mon_dcss_name
, 9, 0444);
524 MODULE_PARM_DESC(mondcss
, "Name of DCSS segment to be used for *MONITOR "
525 "service, max. 8 chars. Default is MONDCSS");
527 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
528 MODULE_DESCRIPTION("Character device driver for reading z/VM "
529 "monitor service records.");
530 MODULE_LICENSE("GPL");