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