Greg Kroah-Hartman | 6f05e69 | 2017-11-14 18:38:03 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Character device driver for reading z/VM *MONITOR service records. |
| 4 | * |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2004, 2009 |
| 6 | * |
| 7 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 10 | #define KMSG_COMPONENT "monreader" |
| 11 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 23 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 25 | #include <net/iucv/iucv.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/ebcdic.h> |
| 28 | #include <asm/extmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 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 |
| 36 | |
| 37 | static char mon_dcss_name[9] = "MONDCSS\0"; |
| 38 | |
| 39 | struct mon_msg { |
| 40 | u32 pos; |
| 41 | u32 mca_offset; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 42 | struct iucv_message msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | char msglim_reached; |
| 44 | char replied_msglim; |
| 45 | }; |
| 46 | |
| 47 | struct mon_private { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 48 | struct iucv_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct mon_msg *msg_array[MON_MSGLIM]; |
| 50 | unsigned int write_index; |
| 51 | unsigned int read_index; |
| 52 | atomic_t msglim_count; |
| 53 | atomic_t read_ready; |
| 54 | atomic_t iucv_connected; |
| 55 | atomic_t iucv_severed; |
| 56 | }; |
| 57 | |
| 58 | static unsigned long mon_in_use = 0; |
| 59 | |
| 60 | static unsigned long mon_dcss_start; |
| 61 | static unsigned long mon_dcss_end; |
| 62 | |
| 63 | static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue); |
| 64 | static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue); |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static u8 user_data_connect[16] = { |
| 67 | /* Version code, must be 0x01 for shared mode */ |
| 68 | 0x01, |
| 69 | /* what to collect */ |
| 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, |
| 74 | }; |
| 75 | |
| 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, |
| 79 | }; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /****************************************************************************** |
| 82 | * helper functions * |
| 83 | *****************************************************************************/ |
| 84 | /* |
| 85 | * Create the 8 bytes EBCDIC DCSS segment name from |
| 86 | * an ASCII name, incl. padding |
| 87 | */ |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 88 | static void dcss_mkname(char *ascii_name, char *ebcdic_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < 8; i++) { |
| 93 | if (ascii_name[i] == '\0') |
| 94 | break; |
| 95 | ebcdic_name[i] = toupper(ascii_name[i]); |
Peter Senna Tschudin | 3b97487 | 2015-08-04 17:11:15 +0200 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | for (; i < 8; i++) |
| 98 | ebcdic_name[i] = ' '; |
| 99 | ASCEBC(ebcdic_name, 8); |
| 100 | } |
| 101 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 102 | static inline unsigned long mon_mca_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 104 | return *(u32 *) &monmsg->msg.rmmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 107 | static inline unsigned long mon_mca_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 109 | return *(u32 *) &monmsg->msg.rmmsg[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 112 | static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); |
| 115 | } |
| 116 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 117 | static inline u32 mon_mca_size(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; |
| 120 | } |
| 121 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 122 | static inline u32 mon_rec_start(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); |
| 125 | } |
| 126 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 127 | static inline u32 mon_rec_end(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); |
| 130 | } |
| 131 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 132 | static int mon_check_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 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))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 147 | static int mon_send_reply(struct mon_msg *monmsg, |
| 148 | struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int rc; |
| 151 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 152 | rc = iucv_message_reply(monpriv->path, &monmsg->msg, |
| 153 | IUCV_IPRMDATA, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | atomic_dec(&monpriv->msglim_count); |
| 155 | if (likely(!monmsg->msglim_reached)) { |
| 156 | monmsg->pos = 0; |
| 157 | monmsg->mca_offset = 0; |
| 158 | monpriv->read_index = (monpriv->read_index + 1) % |
| 159 | MON_MSGLIM; |
| 160 | atomic_dec(&monpriv->read_ready); |
| 161 | } else |
| 162 | monmsg->replied_msglim = 1; |
| 163 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 164 | pr_err("Reading monitor data failed with rc=%i\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return -EIO; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 170 | static void mon_free_mem(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 172 | int i; |
| 173 | |
| 174 | for (i = 0; i < MON_MSGLIM; i++) |
Syam Sidhardhan | a75a282 | 2013-03-07 01:33:55 +0530 | [diff] [blame] | 175 | kfree(monpriv->msg_array[i]); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 176 | kfree(monpriv); |
| 177 | } |
| 178 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 179 | static struct mon_private *mon_alloc_mem(void) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 180 | { |
| 181 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | struct mon_private *monpriv; |
| 183 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 184 | monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 185 | if (!monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | for (i = 0; i < MON_MSGLIM; i++) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 188 | monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | GFP_KERNEL); |
| 190 | if (!monpriv->msg_array[i]) { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 191 | mon_free_mem(monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | return monpriv; |
| 196 | } |
| 197 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 198 | static inline void mon_next_mca(struct mon_msg *monmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12)) |
| 201 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | monmsg->mca_offset += 12; |
| 203 | monmsg->pos = 0; |
| 204 | } |
| 205 | |
Martin Schwidefsky | 9b0c455 | 2007-05-10 15:45:44 +0200 | [diff] [blame] | 206 | static struct mon_msg *mon_next_message(struct mon_private *monpriv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | struct mon_msg *monmsg; |
| 209 | |
| 210 | if (!atomic_read(&monpriv->read_ready)) |
| 211 | return NULL; |
| 212 | monmsg = monpriv->msg_array[monpriv->read_index]; |
| 213 | if (unlikely(monmsg->replied_msglim)) { |
| 214 | monmsg->replied_msglim = 0; |
| 215 | monmsg->msglim_reached = 0; |
| 216 | monmsg->pos = 0; |
| 217 | monmsg->mca_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | monpriv->read_index = (monpriv->read_index + 1) % |
| 219 | MON_MSGLIM; |
| 220 | atomic_dec(&monpriv->read_ready); |
| 221 | return ERR_PTR(-EOVERFLOW); |
| 222 | } |
| 223 | return monmsg; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /****************************************************************************** |
| 228 | * IUCV handler * |
| 229 | *****************************************************************************/ |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 230 | static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 232 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | atomic_set(&monpriv->iucv_connected, 1); |
| 235 | wake_up(&mon_conn_wait_queue); |
| 236 | } |
| 237 | |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 238 | static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 240 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 242 | pr_err("z/VM *MONITOR system service disconnected with rc=%i\n", |
| 243 | ipuser[0]); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 244 | iucv_path_sever(path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | atomic_set(&monpriv->iucv_severed, 1); |
| 246 | wake_up(&mon_conn_wait_queue); |
| 247 | wake_up_interruptible(&mon_read_wait_queue); |
| 248 | } |
| 249 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 250 | static void mon_iucv_message_pending(struct iucv_path *path, |
| 251 | struct iucv_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 253 | struct mon_private *monpriv = path->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 255 | memcpy(&monpriv->msg_array[monpriv->write_index]->msg, |
| 256 | msg, sizeof(*msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) { |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 258 | pr_warn("The read queue for monitor data is full\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | monpriv->msg_array[monpriv->write_index]->msglim_reached = 1; |
| 260 | } |
| 261 | monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM; |
| 262 | atomic_inc(&monpriv->read_ready); |
| 263 | wake_up_interruptible(&mon_read_wait_queue); |
| 264 | } |
| 265 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | /****************************************************************************** |
| 273 | * file operations * |
| 274 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 275 | static int mon_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | struct mon_private *monpriv; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 278 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * only one user allowed |
| 282 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 283 | rc = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (test_and_set_bit(MON_IN_USE, &mon_in_use)) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 285 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 287 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | monpriv = mon_alloc_mem(); |
| 289 | if (!monpriv) |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 290 | goto out_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | /* |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 293 | * Connect to *MONITOR service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | */ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 295 | monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); |
| 296 | if (!monpriv->path) |
| 297 | goto out_priv; |
| 298 | rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, |
| 299 | MON_SERVICE, NULL, user_data_connect, monpriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 301 | pr_err("Connecting to the z/VM *MONITOR system service " |
| 302 | "failed with rc=%i\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 304 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | /* |
| 307 | * Wait for connection confirmation |
| 308 | */ |
| 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); |
| 315 | rc = -EIO; |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 316 | goto out_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | filp->private_data = monpriv; |
| 319 | return nonseekable_open(inode, filp); |
| 320 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 321 | out_path: |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 322 | iucv_path_free(monpriv->path); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 323 | out_priv: |
| 324 | mon_free_mem(monpriv); |
| 325 | out_use: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | clear_bit(MON_IN_USE, &mon_in_use); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 327 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return rc; |
| 329 | } |
| 330 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 331 | static int mon_close(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | int rc, i; |
| 334 | struct mon_private *monpriv = filp->private_data; |
| 335 | |
| 336 | /* |
| 337 | * Close IUCV connection and unregister |
| 338 | */ |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 339 | if (monpriv->path) { |
| 340 | rc = iucv_path_sever(monpriv->path, user_data_sever); |
| 341 | if (rc) |
Joe Perches | baebc70 | 2016-03-03 20:49:57 -0800 | [diff] [blame] | 342 | pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n", |
| 343 | rc); |
Gerald Schaefer | 2b1e3e5 | 2009-06-16 10:30:50 +0200 | [diff] [blame] | 344 | iucv_path_free(monpriv->path); |
| 345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 353 | |
| 354 | for (i = 0; i < MON_MSGLIM; i++) |
| 355 | kfree(monpriv->msg_array[i]); |
| 356 | kfree(monpriv); |
| 357 | clear_bit(MON_IN_USE, &mon_in_use); |
| 358 | return 0; |
| 359 | } |
| 360 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 361 | static ssize_t mon_read(struct file *filp, char __user *data, |
| 362 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | struct mon_private *monpriv = filp->private_data; |
| 365 | struct mon_msg *monmsg; |
| 366 | int ret; |
| 367 | u32 mce_start; |
| 368 | |
| 369 | monmsg = mon_next_message(monpriv); |
| 370 | if (IS_ERR(monmsg)) |
| 371 | return PTR_ERR(monmsg); |
| 372 | |
| 373 | if (!monmsg) { |
| 374 | if (filp->f_flags & O_NONBLOCK) |
| 375 | return -EAGAIN; |
| 376 | ret = wait_event_interruptible(mon_read_wait_queue, |
| 377 | atomic_read(&monpriv->read_ready) || |
| 378 | atomic_read(&monpriv->iucv_severed)); |
| 379 | if (ret) |
| 380 | return ret; |
| 381 | if (unlikely(atomic_read(&monpriv->iucv_severed))) |
| 382 | return -EIO; |
| 383 | monmsg = monpriv->msg_array[monpriv->read_index]; |
| 384 | } |
| 385 | |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 386 | if (!monmsg->pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | if (mon_check_mca(monmsg)) |
| 389 | goto reply; |
| 390 | |
| 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, |
| 396 | count); |
| 397 | if (ret) |
| 398 | return -EFAULT; |
| 399 | monmsg->pos += count; |
| 400 | if (monmsg->pos == mce_start + 12) |
| 401 | monmsg->pos = mon_rec_start(monmsg); |
| 402 | goto out_copy; |
| 403 | } |
| 404 | |
| 405 | /* read records */ |
| 406 | if (monmsg->pos <= mon_rec_end(monmsg)) { |
| 407 | count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos |
| 408 | + 1); |
| 409 | ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, |
| 410 | count); |
| 411 | if (ret) |
| 412 | return -EFAULT; |
| 413 | monmsg->pos += count; |
| 414 | if (monmsg->pos > mon_rec_end(monmsg)) |
| 415 | mon_next_mca(monmsg); |
| 416 | goto out_copy; |
| 417 | } |
| 418 | reply: |
| 419 | ret = mon_send_reply(monmsg, monpriv); |
| 420 | return ret; |
| 421 | |
| 422 | out_copy: |
| 423 | *ppos += count; |
| 424 | return count; |
| 425 | } |
| 426 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 427 | static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
| 429 | struct mon_private *monpriv = filp->private_data; |
| 430 | |
| 431 | poll_wait(filp, &mon_read_wait_queue, p); |
| 432 | if (unlikely(atomic_read(&monpriv->iucv_severed))) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 433 | return EPOLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (atomic_read(&monpriv->read_ready)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 435 | return EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 439 | static const struct file_operations mon_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | .owner = THIS_MODULE, |
| 441 | .open = &mon_open, |
| 442 | .release = &mon_close, |
| 443 | .read = &mon_read, |
| 444 | .poll = &mon_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 445 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | static struct miscdevice mon_dev = { |
| 449 | .name = "monreader", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | .fops = &mon_fops, |
| 451 | .minor = MISC_DYNAMIC_MINOR, |
| 452 | }; |
| 453 | |
| 454 | /****************************************************************************** |
| 455 | * module init/exit * |
| 456 | *****************************************************************************/ |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 457 | static int __init mon_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | int rc; |
| 460 | |
| 461 | if (!MACHINE_IS_VM) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 462 | pr_err("The z/VM *MONITOR record device driver cannot be " |
| 463 | "loaded without z/VM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return -ENODEV; |
| 465 | } |
| 466 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 467 | /* |
| 468 | * Register with IUCV and connect to *MONITOR service |
| 469 | */ |
| 470 | rc = iucv_register(&monreader_iucv_handler, 1); |
| 471 | if (rc) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 472 | pr_err("The z/VM *MONITOR record device driver failed to " |
| 473 | "register with IUCV\n"); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 474 | return rc; |
| 475 | } |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | rc = segment_type(mon_dcss_name); |
| 478 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 479 | segment_warning(rc, mon_dcss_name); |
Peter Oberparleiter | 8e1eaf4 | 2021-06-09 16:59:29 +0200 | [diff] [blame] | 480 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | if (rc != SEG_TYPE_SC) { |
Gerald Schaefer | a4f5a29 | 2008-12-25 13:39:42 +0100 | [diff] [blame] | 483 | pr_err("The specified *MONITOR DCSS %s does not have the " |
| 484 | "required type SC\n", mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 485 | rc = -EINVAL; |
Peter Oberparleiter | 8e1eaf4 | 2021-06-09 16:59:29 +0200 | [diff] [blame] | 486 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | rc = segment_load(mon_dcss_name, SEGMENT_SHARED, |
| 490 | &mon_dcss_start, &mon_dcss_end); |
| 491 | if (rc < 0) { |
Martin Schwidefsky | ca68305 | 2008-04-17 07:46:31 +0200 | [diff] [blame] | 492 | segment_warning(rc, mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 493 | rc = -EINVAL; |
Peter Oberparleiter | 8e1eaf4 | 2021-06-09 16:59:29 +0200 | [diff] [blame] | 494 | goto out_iucv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | dcss_mkname(mon_dcss_name, &user_data_connect[8]); |
| 497 | |
Gerald Schaefer | 1963403 | 2009-12-07 12:52:17 +0100 | [diff] [blame] | 498 | /* |
| 499 | * misc_register() has to be the last action in module_init(), because |
| 500 | * file operations will be available right after this. |
| 501 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | rc = misc_register(&mon_dev); |
Gerald Schaefer | 2ca5b6e | 2008-07-14 09:59:35 +0200 | [diff] [blame] | 503 | if (rc < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | |
| 507 | out: |
| 508 | segment_unload(mon_dcss_name); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 509 | out_iucv: |
| 510 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return rc; |
| 512 | } |
| 513 | |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 514 | static void __exit mon_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
| 516 | segment_unload(mon_dcss_name); |
Akinobu Mita | 547415d5 | 2010-08-09 17:20:35 -0700 | [diff] [blame] | 517 | misc_deregister(&mon_dev); |
Martin Schwidefsky | c667aac | 2007-02-08 13:38:11 -0800 | [diff] [blame] | 518 | iucv_unregister(&monreader_iucv_handler, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | module_init(mon_init); |
| 524 | module_exit(mon_exit); |
| 525 | |
| 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"); |
| 529 | |
| 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"); |