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 static inline unsigned long mon_mca_start(struct mon_msg *monmsg) 115 { 116 return *(u32 *) &monmsg->msg.rmmsg; 117 } 118 119 static inline unsigned long mon_mca_end(struct mon_msg *monmsg) 120 { 121 return *(u32 *) &monmsg->msg.rmmsg[4]; 122 } 123 124 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) 125 { 126 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); 127 } 128 129 static inline u32 mon_mca_size(struct mon_msg *monmsg) 130 { 131 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; 132 } 133 134 static inline u32 mon_rec_start(struct mon_msg *monmsg) 135 { 136 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); 137 } 138 139 static inline u32 mon_rec_end(struct mon_msg *monmsg) 140 { 141 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); 142 } 143 144 static int mon_check_mca(struct mon_msg *monmsg) 145 { 146 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) || 147 (mon_rec_start(monmsg) < mon_dcss_start) || 148 (mon_rec_end(monmsg) > mon_dcss_end) || 149 (mon_mca_type(monmsg, 0) == 0) || 150 (mon_mca_size(monmsg) % 12 != 0) || 151 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) || 152 (mon_mca_end(monmsg) > mon_dcss_end) || 153 (mon_mca_start(monmsg) < mon_dcss_start) || 154 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0))) 155 { 156 P_DEBUG("READ, IGNORED INVALID MCA\n\n"); 157 return -EINVAL; 158 } 159 return 0; 160 } 161 162 static int mon_send_reply(struct mon_msg *monmsg, 163 struct mon_private *monpriv) 164 { 165 int rc; 166 167 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = " 168 "0x%08X\n\n", 169 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class); 170 171 rc = iucv_message_reply(monpriv->path, &monmsg->msg, 172 IUCV_IPRMDATA, NULL, 0); 173 atomic_dec(&monpriv->msglim_count); 174 if (likely(!monmsg->msglim_reached)) { 175 monmsg->pos = 0; 176 monmsg->mca_offset = 0; 177 monpriv->read_index = (monpriv->read_index + 1) % 178 MON_MSGLIM; 179 atomic_dec(&monpriv->read_ready); 180 } else 181 monmsg->replied_msglim = 1; 182 if (rc) { 183 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc); 184 return -EIO; 185 } 186 return 0; 187 } 188 189 static void mon_free_mem(struct mon_private *monpriv) 190 { 191 int i; 192 193 for (i = 0; i < MON_MSGLIM; i++) 194 if (monpriv->msg_array[i]) 195 kfree(monpriv->msg_array[i]); 196 kfree(monpriv); 197 } 198 199 static struct mon_private *mon_alloc_mem(void) 200 { 201 int i; 202 struct mon_private *monpriv; 203 204 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); 205 if (!monpriv) { 206 P_ERROR("no memory for monpriv\n"); 207 return NULL; 208 } 209 for (i = 0; i < MON_MSGLIM; i++) { 210 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), 211 GFP_KERNEL); 212 if (!monpriv->msg_array[i]) { 213 P_ERROR("open, no memory for msg_array\n"); 214 mon_free_mem(monpriv); 215 return NULL; 216 } 217 } 218 return monpriv; 219 } 220 221 static inline void mon_read_debug(struct mon_msg *monmsg, 222 struct mon_private *monpriv) 223 { 224 #ifdef MON_DEBUG 225 u8 msg_type[2], mca_type; 226 unsigned long records_len; 227 228 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1; 229 230 memcpy(msg_type, &monmsg->msg.class, 2); 231 EBCASC(msg_type, 2); 232 mca_type = mon_mca_type(monmsg, 0); 233 EBCASC(&mca_type, 1); 234 235 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n", 236 monpriv->read_index, monpriv->write_index); 237 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n", 238 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class); 239 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n", 240 msg_type[0], msg_type[1], mca_type ? mca_type : 'X', 241 mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2)); 242 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n", 243 mon_mca_start(monmsg), mon_mca_end(monmsg)); 244 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n", 245 mon_rec_start(monmsg), mon_rec_end(monmsg), records_len); 246 if (mon_mca_size(monmsg) > 12) 247 P_DEBUG("READ, MORE THAN ONE MCA\n\n"); 248 #endif 249 } 250 251 static inline void mon_next_mca(struct mon_msg *monmsg) 252 { 253 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12)) 254 return; 255 P_DEBUG("READ, NEXT MCA\n\n"); 256 monmsg->mca_offset += 12; 257 monmsg->pos = 0; 258 } 259 260 static struct mon_msg *mon_next_message(struct mon_private *monpriv) 261 { 262 struct mon_msg *monmsg; 263 264 if (!atomic_read(&monpriv->read_ready)) 265 return NULL; 266 monmsg = monpriv->msg_array[monpriv->read_index]; 267 if (unlikely(monmsg->replied_msglim)) { 268 monmsg->replied_msglim = 0; 269 monmsg->msglim_reached = 0; 270 monmsg->pos = 0; 271 monmsg->mca_offset = 0; 272 P_WARNING("read, message limit reached\n"); 273 monpriv->read_index = (monpriv->read_index + 1) % 274 MON_MSGLIM; 275 atomic_dec(&monpriv->read_ready); 276 return ERR_PTR(-EOVERFLOW); 277 } 278 return monmsg; 279 } 280 281 282 /****************************************************************************** 283 * IUCV handler * 284 *****************************************************************************/ 285 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16]) 286 { 287 struct mon_private *monpriv = path->private; 288 289 P_DEBUG("IUCV connection completed\n"); 290 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = " 291 "0x%02X, Sample = 0x%02X\n", 292 ipuser[0], ipuser[1], ipuser[2]); 293 atomic_set(&monpriv->iucv_connected, 1); 294 wake_up(&mon_conn_wait_queue); 295 } 296 297 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) 298 { 299 struct mon_private *monpriv = path->private; 300 301 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]); 302 iucv_path_sever(path, NULL); 303 atomic_set(&monpriv->iucv_severed, 1); 304 wake_up(&mon_conn_wait_queue); 305 wake_up_interruptible(&mon_read_wait_queue); 306 } 307 308 static void mon_iucv_message_pending(struct iucv_path *path, 309 struct iucv_message *msg) 310 { 311 struct mon_private *monpriv = path->private; 312 313 P_DEBUG("IUCV message pending\n"); 314 memcpy(&monpriv->msg_array[monpriv->write_index]->msg, 315 msg, sizeof(*msg)); 316 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) { 317 P_WARNING("IUCV message pending, message limit (%i) reached\n", 318 MON_MSGLIM); 319 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1; 320 } 321 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM; 322 atomic_inc(&monpriv->read_ready); 323 wake_up_interruptible(&mon_read_wait_queue); 324 } 325 326 static struct iucv_handler monreader_iucv_handler = { 327 .path_complete = mon_iucv_path_complete, 328 .path_severed = mon_iucv_path_severed, 329 .message_pending = mon_iucv_message_pending, 330 }; 331 332 /****************************************************************************** 333 * file operations * 334 *****************************************************************************/ 335 static int mon_open(struct inode *inode, struct file *filp) 336 { 337 struct mon_private *monpriv; 338 int rc; 339 340 /* 341 * only one user allowed 342 */ 343 rc = -EBUSY; 344 if (test_and_set_bit(MON_IN_USE, &mon_in_use)) 345 goto out; 346 347 rc = -ENOMEM; 348 monpriv = mon_alloc_mem(); 349 if (!monpriv) 350 goto out_use; 351 352 /* 353 * Connect to *MONITOR service 354 */ 355 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); 356 if (!monpriv->path) 357 goto out_priv; 358 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, 359 MON_SERVICE, NULL, user_data_connect, monpriv); 360 if (rc) { 361 P_ERROR("iucv connection to *MONITOR failed with " 362 "IPUSER SEVER code = %i\n", rc); 363 rc = -EIO; 364 goto out_path; 365 } 366 /* 367 * Wait for connection confirmation 368 */ 369 wait_event(mon_conn_wait_queue, 370 atomic_read(&monpriv->iucv_connected) || 371 atomic_read(&monpriv->iucv_severed)); 372 if (atomic_read(&monpriv->iucv_severed)) { 373 atomic_set(&monpriv->iucv_severed, 0); 374 atomic_set(&monpriv->iucv_connected, 0); 375 rc = -EIO; 376 goto out_path; 377 } 378 P_INFO("open, established connection to *MONITOR service\n\n"); 379 filp->private_data = monpriv; 380 return nonseekable_open(inode, filp); 381 382 out_path: 383 kfree(monpriv->path); 384 out_priv: 385 mon_free_mem(monpriv); 386 out_use: 387 clear_bit(MON_IN_USE, &mon_in_use); 388 out: 389 return rc; 390 } 391 392 static int mon_close(struct inode *inode, struct file *filp) 393 { 394 int rc, i; 395 struct mon_private *monpriv = filp->private_data; 396 397 /* 398 * Close IUCV connection and unregister 399 */ 400 rc = iucv_path_sever(monpriv->path, user_data_sever); 401 if (rc) 402 P_ERROR("close, iucv_sever failed with rc = %i\n", rc); 403 else 404 P_INFO("close, terminated connection to *MONITOR service\n"); 405 406 atomic_set(&monpriv->iucv_severed, 0); 407 atomic_set(&monpriv->iucv_connected, 0); 408 atomic_set(&monpriv->read_ready, 0); 409 atomic_set(&monpriv->msglim_count, 0); 410 monpriv->write_index = 0; 411 monpriv->read_index = 0; 412 413 for (i = 0; i < MON_MSGLIM; i++) 414 kfree(monpriv->msg_array[i]); 415 kfree(monpriv); 416 clear_bit(MON_IN_USE, &mon_in_use); 417 return 0; 418 } 419 420 static ssize_t mon_read(struct file *filp, char __user *data, 421 size_t count, loff_t *ppos) 422 { 423 struct mon_private *monpriv = filp->private_data; 424 struct mon_msg *monmsg; 425 int ret; 426 u32 mce_start; 427 428 monmsg = mon_next_message(monpriv); 429 if (IS_ERR(monmsg)) 430 return PTR_ERR(monmsg); 431 432 if (!monmsg) { 433 if (filp->f_flags & O_NONBLOCK) 434 return -EAGAIN; 435 ret = wait_event_interruptible(mon_read_wait_queue, 436 atomic_read(&monpriv->read_ready) || 437 atomic_read(&monpriv->iucv_severed)); 438 if (ret) 439 return ret; 440 if (unlikely(atomic_read(&monpriv->iucv_severed))) 441 return -EIO; 442 monmsg = monpriv->msg_array[monpriv->read_index]; 443 } 444 445 if (!monmsg->pos) { 446 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset; 447 mon_read_debug(monmsg, monpriv); 448 } 449 if (mon_check_mca(monmsg)) 450 goto reply; 451 452 /* read monitor control element (12 bytes) first */ 453 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset; 454 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) { 455 count = min(count, (size_t) mce_start + 12 - monmsg->pos); 456 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, 457 count); 458 if (ret) 459 return -EFAULT; 460 monmsg->pos += count; 461 if (monmsg->pos == mce_start + 12) 462 monmsg->pos = mon_rec_start(monmsg); 463 goto out_copy; 464 } 465 466 /* read records */ 467 if (monmsg->pos <= mon_rec_end(monmsg)) { 468 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos 469 + 1); 470 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, 471 count); 472 if (ret) 473 return -EFAULT; 474 monmsg->pos += count; 475 if (monmsg->pos > mon_rec_end(monmsg)) 476 mon_next_mca(monmsg); 477 goto out_copy; 478 } 479 reply: 480 ret = mon_send_reply(monmsg, monpriv); 481 return ret; 482 483 out_copy: 484 *ppos += count; 485 return count; 486 } 487 488 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p) 489 { 490 struct mon_private *monpriv = filp->private_data; 491 492 poll_wait(filp, &mon_read_wait_queue, p); 493 if (unlikely(atomic_read(&monpriv->iucv_severed))) 494 return POLLERR; 495 if (atomic_read(&monpriv->read_ready)) 496 return POLLIN | POLLRDNORM; 497 return 0; 498 } 499 500 static const struct file_operations mon_fops = { 501 .owner = THIS_MODULE, 502 .open = &mon_open, 503 .release = &mon_close, 504 .read = &mon_read, 505 .poll = &mon_poll, 506 }; 507 508 static struct miscdevice mon_dev = { 509 .name = "monreader", 510 .fops = &mon_fops, 511 .minor = MISC_DYNAMIC_MINOR, 512 }; 513 514 /****************************************************************************** 515 * module init/exit * 516 *****************************************************************************/ 517 static int __init mon_init(void) 518 { 519 int rc; 520 521 if (!MACHINE_IS_VM) { 522 P_ERROR("not running under z/VM, driver not loaded\n"); 523 return -ENODEV; 524 } 525 526 /* 527 * Register with IUCV and connect to *MONITOR service 528 */ 529 rc = iucv_register(&monreader_iucv_handler, 1); 530 if (rc) { 531 P_ERROR("failed to register with iucv driver\n"); 532 return rc; 533 } 534 P_INFO("open, registered with IUCV\n"); 535 536 rc = segment_type(mon_dcss_name); 537 if (rc < 0) { 538 segment_warning(rc, mon_dcss_name); 539 goto out_iucv; 540 } 541 if (rc != SEG_TYPE_SC) { 542 P_ERROR("segment %s has unsupported type, should be SC\n", 543 mon_dcss_name); 544 rc = -EINVAL; 545 goto out_iucv; 546 } 547 548 rc = segment_load(mon_dcss_name, SEGMENT_SHARED, 549 &mon_dcss_start, &mon_dcss_end); 550 if (rc < 0) { 551 segment_warning(rc, mon_dcss_name); 552 rc = -EINVAL; 553 goto out_iucv; 554 } 555 dcss_mkname(mon_dcss_name, &user_data_connect[8]); 556 557 rc = misc_register(&mon_dev); 558 if (rc < 0 ) { 559 P_ERROR("misc_register failed, rc = %i\n", rc); 560 goto out; 561 } 562 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n", 563 mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end, 564 mon_dcss_end - mon_dcss_start + 1); 565 return 0; 566 567 out: 568 segment_unload(mon_dcss_name); 569 out_iucv: 570 iucv_unregister(&monreader_iucv_handler, 1); 571 return rc; 572 } 573 574 static void __exit mon_exit(void) 575 { 576 segment_unload(mon_dcss_name); 577 WARN_ON(misc_deregister(&mon_dev) != 0); 578 iucv_unregister(&monreader_iucv_handler, 1); 579 return; 580 } 581 582 583 module_init(mon_init); 584 module_exit(mon_exit); 585 586 module_param_string(mondcss, mon_dcss_name, 9, 0444); 587 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR " 588 "service, max. 8 chars. Default is MONDCSS"); 589 590 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>"); 591 MODULE_DESCRIPTION("Character device driver for reading z/VM " 592 "monitor service records."); 593 MODULE_LICENSE("GPL"); 594