Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Character device driver for reading z/VM *MONITOR service records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright IBM Corp. 2004, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define KMSG_COMPONENT "monreader"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <net/iucv/iucv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/ebcdic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <asm/extmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define MON_COLLECT_SAMPLE 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MON_COLLECT_EVENT  0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define MON_SERVICE	   "*MONITOR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define MON_IN_USE	   0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define MON_MSGLIM	   255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static char mon_dcss_name[9] = "MONDCSS\0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct mon_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u32 pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u32 mca_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct iucv_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	char msglim_reached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	char replied_msglim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) struct mon_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct iucv_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct mon_msg *msg_array[MON_MSGLIM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned int   write_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	unsigned int   read_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	atomic_t msglim_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	atomic_t read_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	atomic_t iucv_connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	atomic_t iucv_severed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static unsigned long mon_in_use = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static unsigned long mon_dcss_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static unsigned long mon_dcss_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static u8 user_data_connect[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* Version code, must be 0x01 for shared mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* what to collect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* DCSS name in EBCDIC, 8 bytes padded with blanks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static u8 user_data_sever[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static struct device *monreader_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *                             helper functions                               *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * Create the 8 bytes EBCDIC DCSS segment name from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * an ASCII name, incl. padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static void dcss_mkname(char *ascii_name, char *ebcdic_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		if (ascii_name[i] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		ebcdic_name[i] = toupper(ascii_name[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	for (; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ebcdic_name[i] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ASCEBC(ebcdic_name, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return *(u32 *) &monmsg->msg.rmmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return *(u32 *) &monmsg->msg.rmmsg[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline u32 mon_mca_size(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline u32 mon_rec_start(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static inline u32 mon_rec_end(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int mon_check_mca(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	    (mon_rec_start(monmsg) < mon_dcss_start) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	    (mon_rec_end(monmsg) > mon_dcss_end) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	    (mon_mca_type(monmsg, 0) == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	    (mon_mca_size(monmsg) % 12 != 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	    (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	    (mon_mca_end(monmsg) > mon_dcss_end) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	    (mon_mca_start(monmsg) < mon_dcss_start) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	    ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int mon_send_reply(struct mon_msg *monmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			  struct mon_private *monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	rc = iucv_message_reply(monpriv->path, &monmsg->msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				IUCV_IPRMDATA, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	atomic_dec(&monpriv->msglim_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (likely(!monmsg->msglim_reached)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		monmsg->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		monmsg->mca_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		monpriv->read_index = (monpriv->read_index + 1) %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				      MON_MSGLIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		atomic_dec(&monpriv->read_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		monmsg->replied_msglim = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		pr_err("Reading monitor data failed with rc=%i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void mon_free_mem(struct mon_private *monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	for (i = 0; i < MON_MSGLIM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		kfree(monpriv->msg_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	kfree(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static struct mon_private *mon_alloc_mem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct mon_private *monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (!monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	for (i = 0; i < MON_MSGLIM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 						    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (!monpriv->msg_array[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			mon_free_mem(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline void mon_next_mca(struct mon_msg *monmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	monmsg->mca_offset += 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	monmsg->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static struct mon_msg *mon_next_message(struct mon_private *monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct mon_msg *monmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (!atomic_read(&monpriv->read_ready))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	monmsg = monpriv->msg_array[monpriv->read_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (unlikely(monmsg->replied_msglim)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		monmsg->replied_msglim = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		monmsg->msglim_reached = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		monmsg->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		monmsg->mca_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		monpriv->read_index = (monpriv->read_index + 1) %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				      MON_MSGLIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		atomic_dec(&monpriv->read_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return ERR_PTR(-EOVERFLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return monmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  *                               IUCV handler                                 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct mon_private *monpriv = path->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	atomic_set(&monpriv->iucv_connected, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	wake_up(&mon_conn_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct mon_private *monpriv = path->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	       ipuser[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	iucv_path_sever(path, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	atomic_set(&monpriv->iucv_severed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	wake_up(&mon_conn_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	wake_up_interruptible(&mon_read_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void mon_iucv_message_pending(struct iucv_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				     struct iucv_message *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct mon_private *monpriv = path->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	       msg, sizeof(*msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		pr_warn("The read queue for monitor data is full\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	atomic_inc(&monpriv->read_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	wake_up_interruptible(&mon_read_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static struct iucv_handler monreader_iucv_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.path_complete	 = mon_iucv_path_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.path_severed	 = mon_iucv_path_severed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.message_pending = mon_iucv_message_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  *                               file operations                              *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  *****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int mon_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct mon_private *monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * only one user allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (test_and_set_bit(MON_IN_USE, &mon_in_use))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	monpriv = mon_alloc_mem();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		goto out_use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * Connect to *MONITOR service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (!monpriv->path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto out_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			       MON_SERVICE, NULL, user_data_connect, monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		pr_err("Connecting to the z/VM *MONITOR system service "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		       "failed with rc=%i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		goto out_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * Wait for connection confirmation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	wait_event(mon_conn_wait_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		   atomic_read(&monpriv->iucv_connected) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		   atomic_read(&monpriv->iucv_severed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (atomic_read(&monpriv->iucv_severed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		atomic_set(&monpriv->iucv_severed, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		atomic_set(&monpriv->iucv_connected, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		goto out_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	filp->private_data = monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	dev_set_drvdata(monreader_device, monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return nonseekable_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) out_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	iucv_path_free(monpriv->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) out_priv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	mon_free_mem(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) out_use:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	clear_bit(MON_IN_USE, &mon_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static int mon_close(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct mon_private *monpriv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 * Close IUCV connection and unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (monpriv->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		rc = iucv_path_sever(monpriv->path, user_data_sever);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		iucv_path_free(monpriv->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	atomic_set(&monpriv->iucv_severed, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	atomic_set(&monpriv->iucv_connected, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	atomic_set(&monpriv->read_ready, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	atomic_set(&monpriv->msglim_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	monpriv->write_index  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	monpriv->read_index   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	dev_set_drvdata(monreader_device, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	for (i = 0; i < MON_MSGLIM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		kfree(monpriv->msg_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	kfree(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	clear_bit(MON_IN_USE, &mon_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static ssize_t mon_read(struct file *filp, char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct mon_private *monpriv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct mon_msg *monmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	u32 mce_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	monmsg = mon_next_message(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (IS_ERR(monmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return PTR_ERR(monmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (!monmsg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		if (filp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		ret = wait_event_interruptible(mon_read_wait_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 					atomic_read(&monpriv->read_ready) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 					atomic_read(&monpriv->iucv_severed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		if (unlikely(atomic_read(&monpriv->iucv_severed)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		monmsg = monpriv->msg_array[monpriv->read_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (!monmsg->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (mon_check_mca(monmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		goto reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	/* read monitor control element (12 bytes) first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		count = min(count, (size_t) mce_start + 12 - monmsg->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 				   count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		monmsg->pos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (monmsg->pos == mce_start + 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			monmsg->pos = mon_rec_start(monmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		goto out_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* read records */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (monmsg->pos <= mon_rec_end(monmsg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 					    + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				   count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		monmsg->pos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (monmsg->pos > mon_rec_end(monmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			mon_next_mca(monmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto out_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) reply:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	ret = mon_send_reply(monmsg, monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) out_copy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	*ppos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	struct mon_private *monpriv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	poll_wait(filp, &mon_read_wait_queue, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (unlikely(atomic_read(&monpriv->iucv_severed)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (atomic_read(&monpriv->read_ready))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static const struct file_operations mon_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	.owner   = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	.open    = &mon_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	.release = &mon_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	.read    = &mon_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	.poll    = &mon_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	.llseek  = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static struct miscdevice mon_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	.name       = "monreader",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	.fops       = &mon_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	.minor      = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  *				suspend / resume			      *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  *****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static int monreader_freeze(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct mon_private *monpriv = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (!monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (monpriv->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		rc = iucv_path_sever(monpriv->path, user_data_sever);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 				rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		iucv_path_free(monpriv->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	atomic_set(&monpriv->iucv_severed, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	atomic_set(&monpriv->iucv_connected, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	atomic_set(&monpriv->read_ready, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	atomic_set(&monpriv->msglim_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	monpriv->write_index  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	monpriv->read_index   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	monpriv->path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int monreader_thaw(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	struct mon_private *monpriv = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (!monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (!monpriv->path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			       MON_SERVICE, NULL, user_data_connect, monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		pr_err("Connecting to the z/VM *MONITOR system service "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		       "failed with rc=%i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		goto out_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	wait_event(mon_conn_wait_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		   atomic_read(&monpriv->iucv_connected) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		   atomic_read(&monpriv->iucv_severed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (atomic_read(&monpriv->iucv_severed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		goto out_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) out_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	iucv_path_free(monpriv->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	monpriv->path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	atomic_set(&monpriv->iucv_severed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int monreader_restore(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	segment_unload(mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			  &mon_dcss_start, &mon_dcss_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		segment_warning(rc, mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		panic("fatal monreader resume error: no monitor dcss\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	return monreader_thaw(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static const struct dev_pm_ops monreader_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	.freeze  = monreader_freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	.thaw	 = monreader_thaw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	.restore = monreader_restore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static struct device_driver monreader_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	.name = "monreader",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	.bus  = &iucv_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	.pm   = &monreader_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  *                              module init/exit                              *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  *****************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static int __init mon_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	if (!MACHINE_IS_VM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		pr_err("The z/VM *MONITOR record device driver cannot be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		       "loaded without z/VM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	 * Register with IUCV and connect to *MONITOR service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	rc = iucv_register(&monreader_iucv_handler, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		pr_err("The z/VM *MONITOR record device driver failed to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		       "register with IUCV\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	rc = driver_register(&monreader_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		goto out_iucv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	if (!monreader_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		goto out_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	dev_set_name(monreader_device, "monreader-dev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	monreader_device->bus = &iucv_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	monreader_device->parent = iucv_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	monreader_device->driver = &monreader_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	monreader_device->release = (void (*)(struct device *))kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	rc = device_register(monreader_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		put_device(monreader_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		goto out_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	rc = segment_type(mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		segment_warning(rc, mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		goto out_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (rc != SEG_TYPE_SC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		pr_err("The specified *MONITOR DCSS %s does not have the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		       "required type SC\n", mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		goto out_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			  &mon_dcss_start, &mon_dcss_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		segment_warning(rc, mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		goto out_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	dcss_mkname(mon_dcss_name, &user_data_connect[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	 * misc_register() has to be the last action in module_init(), because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	 * file operations will be available right after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	rc = misc_register(&mon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (rc < 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	segment_unload(mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) out_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	device_unregister(monreader_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) out_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	driver_unregister(&monreader_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) out_iucv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	iucv_unregister(&monreader_iucv_handler, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static void __exit mon_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	segment_unload(mon_dcss_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	misc_deregister(&mon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	device_unregister(monreader_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	driver_unregister(&monreader_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	iucv_unregister(&monreader_iucv_handler, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) module_init(mon_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) module_exit(mon_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) module_param_string(mondcss, mon_dcss_name, 9, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		 "service, max. 8 chars. Default is MONDCSS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) MODULE_DESCRIPTION("Character device driver for reading z/VM "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		   "monitor service records.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) MODULE_LICENSE("GPL");