1 /* 2 * drivers/s390/char/monreader.c 3 * 4 * Character device driver for reading z/VM *MONITOR service records. 5 * 6 * Copyright IBM Corp. 2004, 2008 7 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/init.h> 13 #include <linux/smp_lock.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 <linux/poll.h> 22 #include <net/iucv/iucv.h> 23 #include <asm/uaccess.h> 24 #include <asm/ebcdic.h> 25 #include <asm/extmem.h> 26 27 //#define MON_DEBUG /* Debug messages on/off */ 28 29 #define MON_NAME "monreader" 30 31 #define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x) 32 #define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x) 33 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x) 34 35 #ifdef MON_DEBUG 36 #define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x) 37 #else 38 #define P_DEBUG(x...) do {} while (0) 39 #endif 40 41 #define MON_COLLECT_SAMPLE 0x80 42 #define MON_COLLECT_EVENT 0x40 43 #define MON_SERVICE "*MONITOR" 44 #define MON_IN_USE 0x01 45 #define MON_MSGLIM 255 46 47 static char mon_dcss_name[9] = "MONDCSS\0"; 48 49 struct mon_msg { 50 u32 pos; 51 u32 mca_offset; 52 struct iucv_message msg; 53 char msglim_reached; 54 char replied_msglim; 55 }; 56 57 struct mon_private { 58 struct iucv_path *path; 59 struct mon_msg *msg_array[MON_MSGLIM]; 60 unsigned int write_index; 61 unsigned int read_index; 62 atomic_t msglim_count; 63 atomic_t read_ready; 64 atomic_t iucv_connected; 65 atomic_t iucv_severed; 66 }; 67 68 static unsigned long mon_in_use = 0; 69 70 static unsigned long mon_dcss_start; 71 static unsigned long mon_dcss_end; 72 73 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue); 74 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue); 75 76 static u8 user_data_connect[16] = { 77 /* Version code, must be 0x01 for shared mode */ 78 0x01, 79 /* what to collect */ 80 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT, 81 /* DCSS name in EBCDIC, 8 bytes padded with blanks */ 82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 84 }; 85 86 static u8 user_data_sever[16] = { 87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 89 }; 90 91 92 /****************************************************************************** 93 * helper functions * 94 *****************************************************************************/ 95 /* 96 * Create the 8 bytes EBCDIC DCSS segment name from 97 * an ASCII name, incl. padding 98 */ 99 static void dcss_mkname(char *ascii_name, char *ebcdic_name) 100 { 101 int i; 102 103 for (i = 0; i < 8; i++) { 104 if (ascii_name[i] == '\0') 105 break; 106 ebcdic_name[i] = toupper(ascii_name[i]); 107 }; 108 for (; i < 8; i++) 109 ebcdic_name[i] = ' '; 110 ASCEBC(ebcdic_name, 8); 111 } 112 113 static inline unsigned long mon_mca_start(struct mon_msg *monmsg) 114 { 115 return *(u32 *) &monmsg->msg.rmmsg; 116 } 117 118 static inline unsigned long mon_mca_end(struct mon_msg *monmsg) 119 { 120 return *(u32 *) &monmsg->msg.rmmsg[4]; 121 } 122 123 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index) 124 { 125 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index); 126 } 127 128 static inline u32 mon_mca_size(struct mon_msg *monmsg) 129 { 130 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1; 131 } 132 133 static inline u32 mon_rec_start(struct mon_msg *monmsg) 134 { 135 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4)); 136 } 137 138 static inline u32 mon_rec_end(struct mon_msg *monmsg) 139 { 140 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8)); 141 } 142 143 static int mon_check_mca(struct mon_msg *monmsg) 144 { 145 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) || 146 (mon_rec_start(monmsg) < mon_dcss_start) || 147 (mon_rec_end(monmsg) > mon_dcss_end) || 148 (mon_mca_type(monmsg, 0) == 0) || 149 (mon_mca_size(monmsg) % 12 != 0) || 150 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) || 151 (mon_mca_end(monmsg) > mon_dcss_end) || 152 (mon_mca_start(monmsg) < mon_dcss_start) || 153 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0))) 154 return -EINVAL; 155 return 0; 156 } 157 158 static int mon_send_reply(struct mon_msg *monmsg, 159 struct mon_private *monpriv) 160 { 161 int rc; 162 163 rc = iucv_message_reply(monpriv->path, &monmsg->msg, 164 IUCV_IPRMDATA, NULL, 0); 165 atomic_dec(&monpriv->msglim_count); 166 if (likely(!monmsg->msglim_reached)) { 167 monmsg->pos = 0; 168 monmsg->mca_offset = 0; 169 monpriv->read_index = (monpriv->read_index + 1) % 170 MON_MSGLIM; 171 atomic_dec(&monpriv->read_ready); 172 } else 173 monmsg->replied_msglim = 1; 174 if (rc) { 175 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc); 176 return -EIO; 177 } 178 return 0; 179 } 180 181 static void mon_free_mem(struct mon_private *monpriv) 182 { 183 int i; 184 185 for (i = 0; i < MON_MSGLIM; i++) 186 if (monpriv->msg_array[i]) 187 kfree(monpriv->msg_array[i]); 188 kfree(monpriv); 189 } 190 191 static struct mon_private *mon_alloc_mem(void) 192 { 193 int i; 194 struct mon_private *monpriv; 195 196 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); 197 if (!monpriv) 198 return NULL; 199 for (i = 0; i < MON_MSGLIM; i++) { 200 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg), 201 GFP_KERNEL); 202 if (!monpriv->msg_array[i]) { 203 mon_free_mem(monpriv); 204 return NULL; 205 } 206 } 207 return monpriv; 208 } 209 210 static inline void mon_next_mca(struct mon_msg *monmsg) 211 { 212 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12)) 213 return; 214 monmsg->mca_offset += 12; 215 monmsg->pos = 0; 216 } 217 218 static struct mon_msg *mon_next_message(struct mon_private *monpriv) 219 { 220 struct mon_msg *monmsg; 221 222 if (!atomic_read(&monpriv->read_ready)) 223 return NULL; 224 monmsg = monpriv->msg_array[monpriv->read_index]; 225 if (unlikely(monmsg->replied_msglim)) { 226 monmsg->replied_msglim = 0; 227 monmsg->msglim_reached = 0; 228 monmsg->pos = 0; 229 monmsg->mca_offset = 0; 230 monpriv->read_index = (monpriv->read_index + 1) % 231 MON_MSGLIM; 232 atomic_dec(&monpriv->read_ready); 233 return ERR_PTR(-EOVERFLOW); 234 } 235 return monmsg; 236 } 237 238 239 /****************************************************************************** 240 * IUCV handler * 241 *****************************************************************************/ 242 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16]) 243 { 244 struct mon_private *monpriv = path->private; 245 246 atomic_set(&monpriv->iucv_connected, 1); 247 wake_up(&mon_conn_wait_queue); 248 } 249 250 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) 251 { 252 struct mon_private *monpriv = path->private; 253 254 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]); 255 iucv_path_sever(path, NULL); 256 atomic_set(&monpriv->iucv_severed, 1); 257 wake_up(&mon_conn_wait_queue); 258 wake_up_interruptible(&mon_read_wait_queue); 259 } 260 261 static void mon_iucv_message_pending(struct iucv_path *path, 262 struct iucv_message *msg) 263 { 264 struct mon_private *monpriv = path->private; 265 266 memcpy(&monpriv->msg_array[monpriv->write_index]->msg, 267 msg, sizeof(*msg)); 268 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) { 269 P_WARNING("IUCV message pending, message limit (%i) reached\n", 270 MON_MSGLIM); 271 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1; 272 } 273 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM; 274 atomic_inc(&monpriv->read_ready); 275 wake_up_interruptible(&mon_read_wait_queue); 276 } 277 278 static struct iucv_handler monreader_iucv_handler = { 279 .path_complete = mon_iucv_path_complete, 280 .path_severed = mon_iucv_path_severed, 281 .message_pending = mon_iucv_message_pending, 282 }; 283 284 /****************************************************************************** 285 * file operations * 286 *****************************************************************************/ 287 static int mon_open(struct inode *inode, struct file *filp) 288 { 289 struct mon_private *monpriv; 290 int rc; 291 292 /* 293 * only one user allowed 294 */ 295 lock_kernel(); 296 rc = -EBUSY; 297 if (test_and_set_bit(MON_IN_USE, &mon_in_use)) 298 goto out; 299 300 rc = -ENOMEM; 301 monpriv = mon_alloc_mem(); 302 if (!monpriv) 303 goto out_use; 304 305 /* 306 * Connect to *MONITOR service 307 */ 308 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL); 309 if (!monpriv->path) 310 goto out_priv; 311 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler, 312 MON_SERVICE, NULL, user_data_connect, monpriv); 313 if (rc) { 314 P_ERROR("iucv connection to *MONITOR failed with " 315 "IPUSER SEVER code = %i\n", rc); 316 rc = -EIO; 317 goto out_path; 318 } 319 /* 320 * Wait for connection confirmation 321 */ 322 wait_event(mon_conn_wait_queue, 323 atomic_read(&monpriv->iucv_connected) || 324 atomic_read(&monpriv->iucv_severed)); 325 if (atomic_read(&monpriv->iucv_severed)) { 326 atomic_set(&monpriv->iucv_severed, 0); 327 atomic_set(&monpriv->iucv_connected, 0); 328 rc = -EIO; 329 goto out_path; 330 } 331 filp->private_data = monpriv; 332 unlock_kernel(); 333 return nonseekable_open(inode, filp); 334 335 out_path: 336 kfree(monpriv->path); 337 out_priv: 338 mon_free_mem(monpriv); 339 out_use: 340 clear_bit(MON_IN_USE, &mon_in_use); 341 out: 342 unlock_kernel(); 343 return rc; 344 } 345 346 static int mon_close(struct inode *inode, struct file *filp) 347 { 348 int rc, i; 349 struct mon_private *monpriv = filp->private_data; 350 351 /* 352 * Close IUCV connection and unregister 353 */ 354 rc = iucv_path_sever(monpriv->path, user_data_sever); 355 if (rc) 356 P_ERROR("close, iucv_sever failed with rc = %i\n", rc); 357 358 atomic_set(&monpriv->iucv_severed, 0); 359 atomic_set(&monpriv->iucv_connected, 0); 360 atomic_set(&monpriv->read_ready, 0); 361 atomic_set(&monpriv->msglim_count, 0); 362 monpriv->write_index = 0; 363 monpriv->read_index = 0; 364 365 for (i = 0; i < MON_MSGLIM; i++) 366 kfree(monpriv->msg_array[i]); 367 kfree(monpriv); 368 clear_bit(MON_IN_USE, &mon_in_use); 369 return 0; 370 } 371 372 static ssize_t mon_read(struct file *filp, char __user *data, 373 size_t count, loff_t *ppos) 374 { 375 struct mon_private *monpriv = filp->private_data; 376 struct mon_msg *monmsg; 377 int ret; 378 u32 mce_start; 379 380 monmsg = mon_next_message(monpriv); 381 if (IS_ERR(monmsg)) 382 return PTR_ERR(monmsg); 383 384 if (!monmsg) { 385 if (filp->f_flags & O_NONBLOCK) 386 return -EAGAIN; 387 ret = wait_event_interruptible(mon_read_wait_queue, 388 atomic_read(&monpriv->read_ready) || 389 atomic_read(&monpriv->iucv_severed)); 390 if (ret) 391 return ret; 392 if (unlikely(atomic_read(&monpriv->iucv_severed))) 393 return -EIO; 394 monmsg = monpriv->msg_array[monpriv->read_index]; 395 } 396 397 if (!monmsg->pos) 398 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset; 399 if (mon_check_mca(monmsg)) 400 goto reply; 401 402 /* read monitor control element (12 bytes) first */ 403 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset; 404 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) { 405 count = min(count, (size_t) mce_start + 12 - monmsg->pos); 406 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, 407 count); 408 if (ret) 409 return -EFAULT; 410 monmsg->pos += count; 411 if (monmsg->pos == mce_start + 12) 412 monmsg->pos = mon_rec_start(monmsg); 413 goto out_copy; 414 } 415 416 /* read records */ 417 if (monmsg->pos <= mon_rec_end(monmsg)) { 418 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos 419 + 1); 420 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos, 421 count); 422 if (ret) 423 return -EFAULT; 424 monmsg->pos += count; 425 if (monmsg->pos > mon_rec_end(monmsg)) 426 mon_next_mca(monmsg); 427 goto out_copy; 428 } 429 reply: 430 ret = mon_send_reply(monmsg, monpriv); 431 return ret; 432 433 out_copy: 434 *ppos += count; 435 return count; 436 } 437 438 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p) 439 { 440 struct mon_private *monpriv = filp->private_data; 441 442 poll_wait(filp, &mon_read_wait_queue, p); 443 if (unlikely(atomic_read(&monpriv->iucv_severed))) 444 return POLLERR; 445 if (atomic_read(&monpriv->read_ready)) 446 return POLLIN | POLLRDNORM; 447 return 0; 448 } 449 450 static const struct file_operations mon_fops = { 451 .owner = THIS_MODULE, 452 .open = &mon_open, 453 .release = &mon_close, 454 .read = &mon_read, 455 .poll = &mon_poll, 456 }; 457 458 static struct miscdevice mon_dev = { 459 .name = "monreader", 460 .fops = &mon_fops, 461 .minor = MISC_DYNAMIC_MINOR, 462 }; 463 464 /****************************************************************************** 465 * module init/exit * 466 *****************************************************************************/ 467 static int __init mon_init(void) 468 { 469 int rc; 470 471 if (!MACHINE_IS_VM) { 472 P_ERROR("not running under z/VM, driver not loaded\n"); 473 return -ENODEV; 474 } 475 476 /* 477 * Register with IUCV and connect to *MONITOR service 478 */ 479 rc = iucv_register(&monreader_iucv_handler, 1); 480 if (rc) { 481 P_ERROR("failed to register with iucv driver\n"); 482 return rc; 483 } 484 485 rc = segment_type(mon_dcss_name); 486 if (rc < 0) { 487 segment_warning(rc, mon_dcss_name); 488 goto out_iucv; 489 } 490 if (rc != SEG_TYPE_SC) { 491 P_ERROR("segment %s has unsupported type, should be SC\n", 492 mon_dcss_name); 493 rc = -EINVAL; 494 goto out_iucv; 495 } 496 497 rc = segment_load(mon_dcss_name, SEGMENT_SHARED, 498 &mon_dcss_start, &mon_dcss_end); 499 if (rc < 0) { 500 segment_warning(rc, mon_dcss_name); 501 rc = -EINVAL; 502 goto out_iucv; 503 } 504 dcss_mkname(mon_dcss_name, &user_data_connect[8]); 505 506 rc = misc_register(&mon_dev); 507 if (rc < 0 ) 508 goto out; 509 return 0; 510 511 out: 512 segment_unload(mon_dcss_name); 513 out_iucv: 514 iucv_unregister(&monreader_iucv_handler, 1); 515 return rc; 516 } 517 518 static void __exit mon_exit(void) 519 { 520 segment_unload(mon_dcss_name); 521 WARN_ON(misc_deregister(&mon_dev) != 0); 522 iucv_unregister(&monreader_iucv_handler, 1); 523 return; 524 } 525 526 527 module_init(mon_init); 528 module_exit(mon_exit); 529 530 module_param_string(mondcss, mon_dcss_name, 9, 0444); 531 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR " 532 "service, max. 8 chars. Default is MONDCSS"); 533 534 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>"); 535 MODULE_DESCRIPTION("Character device driver for reading z/VM " 536 "monitor service records."); 537 MODULE_LICENSE("GPL"); 538