1 // SPDX-License-Identifier: GPL-2.0
3 * Character device driver for reading z/VM *MONITOR service records.
5 * Copyright IBM Corp. 2004, 2009
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/errno.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/miscdevice.h>
20 #include <linux/ctype.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <net/iucv/iucv.h>
26 #include <linux/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,
81 /******************************************************************************
83 *****************************************************************************/
85 * Create the 8 bytes EBCDIC DCSS segment name from
86 * an ASCII name, incl. padding
88 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
92 for (i = 0; i < 8; i++) {
93 if (ascii_name[i] == '\0')
95 ebcdic_name[i] = toupper(ascii_name[i]);
99 ASCEBC(ebcdic_name, 8);
102 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
104 return *(u32 *) &monmsg->msg.rmmsg;
107 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
109 return *(u32 *) &monmsg->msg.rmmsg[4];
112 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
114 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
117 static inline u32 mon_mca_size(struct mon_msg *monmsg)
119 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
122 static inline u32 mon_rec_start(struct mon_msg *monmsg)
124 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
127 static inline u32 mon_rec_end(struct mon_msg *monmsg)
129 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
132 static int mon_check_mca(struct mon_msg *monmsg)
134 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
135 (mon_rec_start(monmsg) < mon_dcss_start) ||
136 (mon_rec_end(monmsg) > mon_dcss_end) ||
137 (mon_mca_type(monmsg, 0) == 0) ||
138 (mon_mca_size(monmsg) % 12 != 0) ||
139 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
140 (mon_mca_end(monmsg) > mon_dcss_end) ||
141 (mon_mca_start(monmsg) < mon_dcss_start) ||
142 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
147 static int mon_send_reply(struct mon_msg *monmsg,
148 struct mon_private *monpriv)
152 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
153 IUCV_IPRMDATA, NULL, 0);
154 atomic_dec(&monpriv->msglim_count);
155 if (likely(!monmsg->msglim_reached)) {
157 monmsg->mca_offset = 0;
158 monpriv->read_index = (monpriv->read_index + 1) %
160 atomic_dec(&monpriv->read_ready);
162 monmsg->replied_msglim = 1;
164 pr_err("Reading monitor data failed with rc=%i\n", rc);
170 static void mon_free_mem(struct mon_private *monpriv)
174 for (i = 0; i < MON_MSGLIM; i++)
175 kfree(monpriv->msg_array[i]);
179 static struct mon_private *mon_alloc_mem(void)
182 struct mon_private *monpriv;
184 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
187 for (i = 0; i < MON_MSGLIM; i++) {
188 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
190 if (!monpriv->msg_array[i]) {
191 mon_free_mem(monpriv);
198 static inline void mon_next_mca(struct mon_msg *monmsg)
200 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
202 monmsg->mca_offset += 12;
206 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
208 struct mon_msg *monmsg;
210 if (!atomic_read(&monpriv->read_ready))
212 monmsg = monpriv->msg_array[monpriv->read_index];
213 if (unlikely(monmsg->replied_msglim)) {
214 monmsg->replied_msglim = 0;
215 monmsg->msglim_reached = 0;
217 monmsg->mca_offset = 0;
218 monpriv->read_index = (monpriv->read_index + 1) %
220 atomic_dec(&monpriv->read_ready);
221 return ERR_PTR(-EOVERFLOW);
227 /******************************************************************************
229 *****************************************************************************/
230 static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
232 struct mon_private *monpriv = path->private;
234 atomic_set(&monpriv->iucv_connected, 1);
235 wake_up(&mon_conn_wait_queue);
238 static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
240 struct mon_private *monpriv = path->private;
242 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
244 iucv_path_sever(path, NULL);
245 atomic_set(&monpriv->iucv_severed, 1);
246 wake_up(&mon_conn_wait_queue);
247 wake_up_interruptible(&mon_read_wait_queue);
250 static void mon_iucv_message_pending(struct iucv_path *path,
251 struct iucv_message *msg)
253 struct mon_private *monpriv = path->private;
255 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
257 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
258 pr_warn("The read queue for monitor data is full\n");
259 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
261 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
262 atomic_inc(&monpriv->read_ready);
263 wake_up_interruptible(&mon_read_wait_queue);
266 static struct iucv_handler monreader_iucv_handler = {
267 .path_complete = mon_iucv_path_complete,
268 .path_severed = mon_iucv_path_severed,
269 .message_pending = mon_iucv_message_pending,
272 /******************************************************************************
274 *****************************************************************************/
275 static int mon_open(struct inode *inode, struct file *filp)
277 struct mon_private *monpriv;
281 * only one user allowed
284 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
288 monpriv = mon_alloc_mem();
293 * Connect to *MONITOR service
295 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
298 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
299 MON_SERVICE, NULL, user_data_connect, monpriv);
301 pr_err("Connecting to the z/VM *MONITOR system service "
302 "failed with rc=%i\n", rc);
307 * Wait for connection confirmation
309 wait_event(mon_conn_wait_queue,
310 atomic_read(&monpriv->iucv_connected) ||
311 atomic_read(&monpriv->iucv_severed));
312 if (atomic_read(&monpriv->iucv_severed)) {
313 atomic_set(&monpriv->iucv_severed, 0);
314 atomic_set(&monpriv->iucv_connected, 0);
318 filp->private_data = monpriv;
319 return nonseekable_open(inode, filp);
322 iucv_path_free(monpriv->path);
324 mon_free_mem(monpriv);
326 clear_bit(MON_IN_USE, &mon_in_use);
331 static int mon_close(struct inode *inode, struct file *filp)
334 struct mon_private *monpriv = filp->private_data;
337 * Close IUCV connection and unregister
340 rc = iucv_path_sever(monpriv->path, user_data_sever);
342 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
344 iucv_path_free(monpriv->path);
347 atomic_set(&monpriv->iucv_severed, 0);
348 atomic_set(&monpriv->iucv_connected, 0);
349 atomic_set(&monpriv->read_ready, 0);
350 atomic_set(&monpriv->msglim_count, 0);
351 monpriv->write_index = 0;
352 monpriv->read_index = 0;
354 for (i = 0; i < MON_MSGLIM; i++)
355 kfree(monpriv->msg_array[i]);
357 clear_bit(MON_IN_USE, &mon_in_use);
361 static ssize_t mon_read(struct file *filp, char __user *data,
362 size_t count, loff_t *ppos)
364 struct mon_private *monpriv = filp->private_data;
365 struct mon_msg *monmsg;
369 monmsg = mon_next_message(monpriv);
371 return PTR_ERR(monmsg);
374 if (filp->f_flags & O_NONBLOCK)
376 ret = wait_event_interruptible(mon_read_wait_queue,
377 atomic_read(&monpriv->read_ready) ||
378 atomic_read(&monpriv->iucv_severed));
381 if (unlikely(atomic_read(&monpriv->iucv_severed)))
383 monmsg = monpriv->msg_array[monpriv->read_index];
387 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
388 if (mon_check_mca(monmsg))
391 /* read monitor control element (12 bytes) first */
392 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
393 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
394 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
395 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
399 monmsg->pos += count;
400 if (monmsg->pos == mce_start + 12)
401 monmsg->pos = mon_rec_start(monmsg);
406 if (monmsg->pos <= mon_rec_end(monmsg)) {
407 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
409 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
413 monmsg->pos += count;
414 if (monmsg->pos > mon_rec_end(monmsg))
415 mon_next_mca(monmsg);
419 ret = mon_send_reply(monmsg, monpriv);
427 static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p)
429 struct mon_private *monpriv = filp->private_data;
431 poll_wait(filp, &mon_read_wait_queue, p);
432 if (unlikely(atomic_read(&monpriv->iucv_severed)))
434 if (atomic_read(&monpriv->read_ready))
435 return EPOLLIN | EPOLLRDNORM;
439 static const struct file_operations mon_fops = {
440 .owner = THIS_MODULE,
442 .release = &mon_close,
445 .llseek = noop_llseek,
448 static struct miscdevice mon_dev = {
451 .minor = MISC_DYNAMIC_MINOR,
454 /******************************************************************************
456 *****************************************************************************/
457 static int __init mon_init(void)
461 if (!MACHINE_IS_VM) {
462 pr_err("The z/VM *MONITOR record device driver cannot be "
463 "loaded without z/VM\n");
468 * Register with IUCV and connect to *MONITOR service
470 rc = iucv_register(&monreader_iucv_handler, 1);
472 pr_err("The z/VM *MONITOR record device driver failed to "
473 "register with IUCV\n");
477 rc = segment_type(mon_dcss_name);
479 segment_warning(rc, mon_dcss_name);
482 if (rc != SEG_TYPE_SC) {
483 pr_err("The specified *MONITOR DCSS %s does not have the "
484 "required type SC\n", mon_dcss_name);
489 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
490 &mon_dcss_start, &mon_dcss_end);
492 segment_warning(rc, mon_dcss_name);
496 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
499 * misc_register() has to be the last action in module_init(), because
500 * file operations will be available right after this.
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 misc_deregister(&mon_dev);
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");
531 MODULE_DESCRIPTION("Character device driver for reading z/VM "
532 "monitor service records.");
533 MODULE_LICENSE("GPL");