1 /* 2 * drivers/s390/char/monreader.c 3 * 4 * Character device driver for reading z/VM *MONITOR service records. 5 * 6 * Copyright 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/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 struct iucv_message msg; 54 char msglim_reached; 55 char replied_msglim; 56 }; 57 58 struct mon_private { 59 struct iucv_path *path; 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 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 */ 100 static void dcss_mkname(char *ascii_name, char *ebcdic_name) 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 */ 118 static void mon_segment_warn(int rc, char* seg_name) 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 164 static inline unsigned long mon_mca_start(struct mon_msg *monmsg) 165 { 166 return *(u32 *) &monmsg->msg.rmmsg; 167 } 168 169 static inline unsigned long mon_mca_end(struct mon_msg *monmsg) 170 { 171 return *(u32 *) &monmsg->msg.rmmsg[4]; 172 } 173 174 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) 175 { 176 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); 177 } 178 179 static inline u32 mon_mca_size(struct mon_msg *monmsg) 180 { 181 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; 182 } 183 184 static inline u32 mon_rec_start(struct mon_msg *monmsg) 185 { 186 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); 187 } 188 189 static inline u32 mon_rec_end(struct mon_msg *monmsg) 190 { 191 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); 192 } 193 194 static int mon_check_mca(struct mon_msg *monmsg) 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 212 static int mon_send_reply(struct mon_msg *monmsg, 213 struct mon_private *monpriv) 214 { 215 int rc; 216 217 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = " 218 "0x%08X\n\n", 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); 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 239 static void mon_free_mem(struct mon_private *monpriv) 240 { 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 struct mon_private *mon_alloc_mem(void) 250 { 251 int i; 252 struct mon_private *monpriv; 253 254 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); 255 if (!monpriv) { 256 P_ERROR("no memory for monpriv\n"); 257 return NULL; 258 } 259 for (i = 0; i < MON_MSGLIM; i++) { 260 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), 261 GFP_KERNEL); 262 if (!monpriv->msg_array[i]) { 263 P_ERROR("open, no memory for msg_array\n"); 264 mon_free_mem(monpriv); 265 return NULL; 266 } 267 } 268 return monpriv; 269 } 270 271 static inline void mon_read_debug(struct mon_msg *monmsg, 272 struct mon_private *monpriv) 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 280 memcpy(msg_type, &monmsg->msg.class, 2); 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", 288 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class); 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 301 static inline void mon_next_mca(struct mon_msg *monmsg) 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 310 static struct mon_msg *mon_next_message(struct mon_private *monpriv) 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 *****************************************************************************/ 335 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16]) 336 { 337 struct mon_private *monpriv = path->private; 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", 342 ipuser[0], ipuser[1], ipuser[2]); 343 atomic_set(&monpriv->iucv_connected, 1); 344 wake_up(&mon_conn_wait_queue); 345 } 346 347 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) 348 { 349 struct mon_private *monpriv = path->private; 350 351 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]); 352 iucv_path_sever(path, NULL); 353 atomic_set(&monpriv->iucv_severed, 1); 354 wake_up(&mon_conn_wait_queue); 355 wake_up_interruptible(&mon_read_wait_queue); 356 } 357 358 static void mon_iucv_message_pending(struct iucv_path *path, 359 struct iucv_message *msg) 360 { 361 struct mon_private *monpriv = path->private; 362 363 P_DEBUG("IUCV message pending\n"); 364 memcpy(&monpriv->msg_array[monpriv->write_index]->msg, 365 msg, sizeof(*msg)); 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 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, 380 }; 381 382 /****************************************************************************** 383 * file operations * 384 *****************************************************************************/ 385 static int mon_open(struct inode *inode, struct file *filp) 386 { 387 struct mon_private *monpriv; 388 int rc; 389 390 /* 391 * only one user allowed 392 */ 393 rc = -EBUSY; 394 if (test_and_set_bit(MON_IN_USE, &mon_in_use)) 395 goto out; 396 397 rc = -ENOMEM; 398 monpriv = mon_alloc_mem(); 399 if (!monpriv) 400 goto out_use; 401 402 /* 403 * Connect to *MONITOR service 404 */ 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); 410 if (rc) { 411 P_ERROR("iucv connection to *MONITOR failed with " 412 "IPUSER SEVER code = %i\n", rc); 413 rc = -EIO; 414 goto out_path; 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; 426 goto out_path; 427 } 428 P_INFO("open, established connection to *MONITOR service\n\n"); 429 filp->private_data = monpriv; 430 return nonseekable_open(inode, filp); 431 432 out_path: 433 kfree(monpriv->path); 434 out_priv: 435 mon_free_mem(monpriv); 436 out_use: 437 clear_bit(MON_IN_USE, &mon_in_use); 438 out: 439 return rc; 440 } 441 442 static int mon_close(struct inode *inode, struct file *filp) 443 { 444 int rc, i; 445 struct mon_private *monpriv = filp->private_data; 446 447 /* 448 * Close IUCV connection and unregister 449 */ 450 rc = iucv_path_sever(monpriv->path, user_data_sever); 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 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 470 static ssize_t mon_read(struct file *filp, char __user *data, 471 size_t count, loff_t *ppos) 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 538 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p) 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 const 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", 560 .fops = &mon_fops, 561 .minor = MISC_DYNAMIC_MINOR, 562 }; 563 564 /****************************************************************************** 565 * module init/exit * 566 *****************************************************************************/ 567 static int __init mon_init(void) 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 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 586 rc = segment_type(mon_dcss_name); 587 if (rc < 0) { 588 mon_segment_warn(rc, mon_dcss_name); 589 goto out_iucv; 590 } 591 if (rc != SEG_TYPE_SC) { 592 P_ERROR("segment %s has unsupported type, should be SC\n", 593 mon_dcss_name); 594 rc = -EINVAL; 595 goto out_iucv; 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); 602 rc = -EINVAL; 603 goto out_iucv; 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); 619 out_iucv: 620 iucv_unregister(&monreader_iucv_handler, 1); 621 return rc; 622 } 623 624 static void __exit mon_exit(void) 625 { 626 segment_unload(mon_dcss_name); 627 WARN_ON(misc_deregister(&mon_dev) != 0); 628 iucv_unregister(&monreader_iucv_handler, 1); 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"); 644