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