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