ACPI: add control method tracing support
[linux-2.6/mini2440.git] / drivers / s390 / char / monreader.c
blob67009bfa093e12c8c0a828e9528f49021d84df20
1 /*
2 * drivers/s390/char/monreader.c
4 * Character device driver for reading z/VM *MONITOR service records.
6 * Copyright 2004 IBM Corporation, IBM Deutschland Entwicklung GmbH.
8 * Author: Gerald Schaefer <geraldsc@de.ibm.com>
9 */
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>
28 //#define MON_DEBUG /* Debug messages on/off */
30 #define MON_NAME "monreader"
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)
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
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
48 static char mon_dcss_name[9] = "MONDCSS\0";
50 struct mon_msg {
51 u32 pos;
52 u32 mca_offset;
53 struct iucv_message msg;
54 char msglim_reached;
55 char replied_msglim;
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;
69 static unsigned long mon_in_use = 0;
71 static unsigned long mon_dcss_start;
72 static unsigned long mon_dcss_end;
74 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
75 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
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,
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,
93 /******************************************************************************
94 * helper functions *
95 *****************************************************************************/
97 * Create the 8 bytes EBCDIC DCSS segment name from
98 * an ASCII name, incl. padding
100 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
102 int i;
104 for (i = 0; i < 8; i++) {
105 if (ascii_name[i] == '\0')
106 break;
107 ebcdic_name[i] = toupper(ascii_name[i]);
109 for (; i < 8; i++)
110 ebcdic_name[i] = ' ';
111 ASCEBC(ebcdic_name, 8);
115 * print appropriate error message for segment_load()/segment_type()
116 * return code
118 static void mon_segment_warn(int rc, char* seg_name)
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;
164 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
166 return *(u32 *) &monmsg->msg.rmmsg;
169 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
171 return *(u32 *) &monmsg->msg.rmmsg[4];
174 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
176 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
179 static inline u32 mon_mca_size(struct mon_msg *monmsg)
181 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
184 static inline u32 mon_rec_start(struct mon_msg *monmsg)
186 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
189 static inline u32 mon_rec_end(struct mon_msg *monmsg)
191 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
194 static int mon_check_mca(struct mon_msg *monmsg)
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)))
206 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
207 return -EINVAL;
209 return 0;
212 static int mon_send_reply(struct mon_msg *monmsg,
213 struct mon_private *monpriv)
215 int rc;
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);
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;
236 return 0;
239 static void mon_free_mem(struct mon_private *monpriv)
241 int i;
243 for (i = 0; i < MON_MSGLIM; i++)
244 if (monpriv->msg_array[i])
245 kfree(monpriv->msg_array[i]);
246 kfree(monpriv);
249 static struct mon_private *mon_alloc_mem(void)
251 int i;
252 struct mon_private *monpriv;
254 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
255 if (!monpriv) {
256 P_ERROR("no memory for monpriv\n");
257 return NULL;
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;
268 return monpriv;
271 static inline void mon_read_debug(struct mon_msg *monmsg,
272 struct mon_private *monpriv)
274 #ifdef MON_DEBUG
275 u8 msg_type[2], mca_type;
276 unsigned long records_len;
278 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
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);
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
301 static inline void mon_next_mca(struct mon_msg *monmsg)
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;
310 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
312 struct mon_msg *monmsg;
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);
328 return monmsg;
332 /******************************************************************************
333 * IUCV handler *
334 *****************************************************************************/
335 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
337 struct mon_private *monpriv = path->private;
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);
347 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
349 struct mon_private *monpriv = path->private;
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);
358 static void mon_iucv_message_pending(struct iucv_path *path,
359 struct iucv_message *msg)
361 struct mon_private *monpriv = path->private;
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;
371 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
372 atomic_inc(&monpriv->read_ready);
373 wake_up_interruptible(&mon_read_wait_queue);
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,
382 /******************************************************************************
383 * file operations *
384 *****************************************************************************/
385 static int mon_open(struct inode *inode, struct file *filp)
387 struct mon_private *monpriv;
388 int rc;
391 * only one user allowed
393 rc = -EBUSY;
394 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
395 goto out;
397 rc = -ENOMEM;
398 monpriv = mon_alloc_mem();
399 if (!monpriv)
400 goto out_use;
403 * Connect to *MONITOR service
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;
417 * Wait for connection confirmation
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;
428 P_INFO("open, established connection to *MONITOR service\n\n");
429 filp->private_data = monpriv;
430 return nonseekable_open(inode, filp);
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;
442 static int mon_close(struct inode *inode, struct file *filp)
444 int rc, i;
445 struct mon_private *monpriv = filp->private_data;
448 * Close IUCV connection and unregister
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");
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;
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;
470 static ssize_t mon_read(struct file *filp, char __user *data,
471 size_t count, loff_t *ppos)
473 struct mon_private *monpriv = filp->private_data;
474 struct mon_msg *monmsg;
475 int ret;
476 u32 mce_start;
478 monmsg = mon_next_message(monpriv);
479 if (IS_ERR(monmsg))
480 return PTR_ERR(monmsg);
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];
495 if (!monmsg->pos) {
496 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
497 mon_read_debug(monmsg, monpriv);
499 if (mon_check_mca(monmsg))
500 goto reply;
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;
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;
529 reply:
530 ret = mon_send_reply(monmsg, monpriv);
531 return ret;
533 out_copy:
534 *ppos += count;
535 return count;
538 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
540 struct mon_private *monpriv = filp->private_data;
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;
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,
558 static struct miscdevice mon_dev = {
559 .name = "monreader",
560 .fops = &mon_fops,
561 .minor = MISC_DYNAMIC_MINOR,
564 /******************************************************************************
565 * module init/exit *
566 *****************************************************************************/
567 static int __init mon_init(void)
569 int rc;
571 if (!MACHINE_IS_VM) {
572 P_ERROR("not running under z/VM, driver not loaded\n");
573 return -ENODEV;
577 * Register with IUCV and connect to *MONITOR service
579 rc = iucv_register(&monreader_iucv_handler, 1);
580 if (rc) {
581 P_ERROR("failed to register with iucv driver\n");
582 return rc;
584 P_INFO("open, registered with IUCV\n");
586 rc = segment_type(mon_dcss_name);
587 if (rc < 0) {
588 mon_segment_warn(rc, mon_dcss_name);
589 goto out_iucv;
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;
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;
605 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
607 rc = misc_register(&mon_dev);
608 if (rc < 0 ) {
609 P_ERROR("misc_register failed, rc = %i\n", rc);
610 goto out;
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;
617 out:
618 segment_unload(mon_dcss_name);
619 out_iucv:
620 iucv_unregister(&monreader_iucv_handler, 1);
621 return rc;
624 static void __exit mon_exit(void)
626 segment_unload(mon_dcss_name);
627 WARN_ON(misc_deregister(&mon_dev) != 0);
628 iucv_unregister(&monreader_iucv_handler, 1);
629 return;
633 module_init(mon_init);
634 module_exit(mon_exit);
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");
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");