2 * drivers/s390/char/vmlogrdr.c
3 * character device driver for reading z/VM system service records
6 * Copyright 2004 IBM Corporation
7 * character device driver for reading z/VM system service records,
9 * Author(s): Xenia Tkatschow <xenia@us.ibm.com>
10 * Stefan Weinhuber <wein@de.ibm.com>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/interrupt.h>
18 #include <linux/spinlock.h>
19 #include <asm/atomic.h>
20 #include <asm/uaccess.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/ebcdic.h>
24 #include <net/iucv/iucv.h>
25 #include <linux/kmod.h>
26 #include <linux/cdev.h>
27 #include <linux/device.h>
28 #include <linux/string.h>
33 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
34 " Stefan Weinhuber (wein@de.ibm.com)");
35 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
36 "system service records.");
37 MODULE_LICENSE("GPL");
41 * The size of the buffer for iucv data transfer is one page,
42 * but in addition to the data we read from iucv we also
43 * place an integer and some characters into that buffer,
44 * so the maximum size for record data is a little less then
47 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
50 * The elements that are concurrently accessed by bottom halves are
51 * connection_established, iucv_path_severed, local_interrupt_buffer
52 * and receive_ready. The first three can be protected by
53 * priv_lock. receive_ready is atomic, so it can be incremented and
54 * decremented without holding a lock.
55 * The variable dev_in_use needs to be protected by the lock, since
56 * it's a flag used by open to make sure that the device is opened only
57 * by one user at the same time.
59 struct vmlogrdr_priv_t {
60 char system_service[8];
61 char internal_name[8];
62 char recording_name[8];
63 struct iucv_path *path;
64 int connection_established;
65 int iucv_path_severed;
66 struct iucv_message local_interrupt_buffer;
67 atomic_t receive_ready;
70 char * current_position;
72 ulong residual_length;
74 int dev_in_use; /* 1: already opened, 0: not opened*/
76 struct device *device;
77 struct device *class_device;
84 * File operation structure for vmlogrdr devices
86 static int vmlogrdr_open(struct inode *, struct file *);
87 static int vmlogrdr_release(struct inode *, struct file *);
88 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
89 size_t count, loff_t * ppos);
91 static const struct file_operations vmlogrdr_fops = {
93 .open = vmlogrdr_open,
94 .release = vmlogrdr_release,
95 .read = vmlogrdr_read,
99 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
100 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
101 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
102 struct iucv_message *);
105 static struct iucv_handler vmlogrdr_iucv_handler = {
106 .path_complete = vmlogrdr_iucv_path_complete,
107 .path_severed = vmlogrdr_iucv_path_severed,
108 .message_pending = vmlogrdr_iucv_message_pending,
112 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
113 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
116 * pointer to system service private structure
117 * minor number 0 --> logrec
118 * minor number 1 --> account
119 * minor number 2 --> symptom
122 static struct vmlogrdr_priv_t sys_ser[] = {
123 { .system_service = "*LOGREC ",
124 .internal_name = "logrec",
125 .recording_name = "EREP",
128 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
132 { .system_service = "*ACCOUNT",
133 .internal_name = "account",
134 .recording_name = "ACCOUNT",
137 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
141 { .system_service = "*SYMPTOM",
142 .internal_name = "symptom",
143 .recording_name = "SYMPTOM",
146 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
152 #define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
154 static char FENCE[] = {"EOR"};
155 static int vmlogrdr_major = 0;
156 static struct cdev *vmlogrdr_cdev = NULL;
157 static int recording_class_AB;
160 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
162 struct vmlogrdr_priv_t * logptr = path->private;
164 spin_lock(&logptr->priv_lock);
165 logptr->connection_established = 1;
166 spin_unlock(&logptr->priv_lock);
167 wake_up(&conn_wait_queue);
171 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
173 struct vmlogrdr_priv_t * logptr = path->private;
174 u8 reason = (u8) ipuser[8];
176 printk (KERN_ERR "vmlogrdr: connection severed with"
177 " reason %i\n", reason);
179 iucv_path_sever(path, NULL);
183 spin_lock(&logptr->priv_lock);
184 logptr->connection_established = 0;
185 logptr->iucv_path_severed = 1;
186 spin_unlock(&logptr->priv_lock);
188 wake_up(&conn_wait_queue);
189 /* just in case we're sleeping waiting for a record */
190 wake_up_interruptible(&read_wait_queue);
194 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
195 struct iucv_message *msg)
197 struct vmlogrdr_priv_t * logptr = path->private;
200 * This function is the bottom half so it should be quick.
201 * Copy the external interrupt data into our local eib and increment
204 spin_lock(&logptr->priv_lock);
205 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
206 atomic_inc(&logptr->receive_ready);
207 spin_unlock(&logptr->priv_lock);
208 wake_up_interruptible(&read_wait_queue);
212 static int vmlogrdr_get_recording_class_AB(void)
214 char cp_command[]="QUERY COMMAND RECORDING ";
215 char cp_response[80];
219 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
220 len = strnlen(cp_response,sizeof(cp_response));
222 tail=strnchr(cp_response,len,'=');
226 if (!strncmp("ANY",tail,3))
228 if (!strncmp("NONE",tail,4))
231 * expect comma separated list of classes here, if one of them
232 * is A or B return 1 otherwise 0
234 for (i=tail-cp_response; i<len; i++)
235 if ( cp_response[i]=='A' || cp_response[i]=='B' )
241 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
242 int action, int purge)
246 char cp_response[160];
247 char *onoff, *qid_string;
249 memset(cp_command, 0x00, sizeof(cp_command));
250 memset(cp_response, 0x00, sizeof(cp_response));
252 onoff = ((action == 1) ? "ON" : "OFF");
253 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
256 * The recording commands needs to be called with option QID
257 * for guests that have previlege classes A or B.
258 * Purging has to be done as separate step, because recording
259 * can't be switched on as long as records are on the queue.
260 * Doing both at the same time doesn't work.
264 snprintf(cp_command, sizeof(cp_command),
265 "RECORDING %s PURGE %s",
266 logptr->recording_name,
269 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
272 memset(cp_command, 0x00, sizeof(cp_command));
273 memset(cp_response, 0x00, sizeof(cp_response));
274 snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
275 logptr->recording_name,
279 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
280 /* The recording command will usually answer with 'Command complete'
281 * on success, but when the specific service was never connected
282 * before then there might be an additional informational message
283 * 'HCPCRC8072I Recording entry not found' before the
284 * 'Command complete'. So I use strstr rather then the strncmp.
286 if (strstr(cp_response,"Command complete"))
294 static int vmlogrdr_open (struct inode *inode, struct file *filp)
297 struct vmlogrdr_priv_t * logptr = NULL;
301 dev_num = iminor(inode);
302 if (dev_num > MAXMINOR)
304 logptr = &sys_ser[dev_num];
307 * only allow for blocking reads to be open
309 if (filp->f_flags & O_NONBLOCK)
312 /* Besure this device hasn't already been opened */
313 spin_lock_bh(&logptr->priv_lock);
314 if (logptr->dev_in_use) {
315 spin_unlock_bh(&logptr->priv_lock);
318 logptr->dev_in_use = 1;
319 logptr->connection_established = 0;
320 logptr->iucv_path_severed = 0;
321 atomic_set(&logptr->receive_ready, 0);
322 logptr->buffer_free = 1;
323 spin_unlock_bh(&logptr->priv_lock);
325 /* set the file options */
326 filp->private_data = logptr;
327 filp->f_op = &vmlogrdr_fops;
329 /* start recording for this service*/
330 if (logptr->autorecording) {
331 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
333 printk (KERN_WARNING "vmlogrdr: failed to start "
334 "recording automatically\n");
337 /* create connection to the system service */
338 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
341 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
342 logptr->system_service, NULL, NULL,
345 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
346 "failed with rc %i \n", logptr->system_service,
351 /* We've issued the connect and now we must wait for a
352 * ConnectionComplete or ConnectinSevered Interrupt
353 * before we can continue to process.
355 wait_event(conn_wait_queue, (logptr->connection_established)
356 || (logptr->iucv_path_severed));
357 if (logptr->iucv_path_severed)
359 return nonseekable_open(inode, filp);
362 if (logptr->autorecording)
363 vmlogrdr_recording(logptr,0,logptr->autopurge);
365 kfree(logptr->path); /* kfree(NULL) is ok. */
368 logptr->dev_in_use = 0;
373 static int vmlogrdr_release (struct inode *inode, struct file *filp)
377 struct vmlogrdr_priv_t * logptr = filp->private_data;
379 iucv_path_sever(logptr->path, NULL);
382 if (logptr->autorecording) {
383 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
385 printk (KERN_WARNING "vmlogrdr: failed to stop "
386 "recording automatically\n");
388 logptr->dev_in_use = 0;
394 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
397 /* we need to keep track of two data sizes here:
398 * The number of bytes we need to receive from iucv and
399 * the total number of bytes we actually write into the buffer.
401 int user_data_count, iucv_data_count;
404 if (atomic_read(&priv->receive_ready)) {
405 spin_lock_bh(&priv->priv_lock);
406 if (priv->residual_length){
407 /* receive second half of a record */
408 iucv_data_count = priv->residual_length;
410 buffer = priv->buffer;
412 /* receive a new record:
413 * We need to return the total length of the record
414 * + size of FENCE in the first 4 bytes of the buffer.
416 iucv_data_count = priv->local_interrupt_buffer.length;
417 user_data_count = sizeof(int);
418 temp = (int*)priv->buffer;
419 *temp= iucv_data_count + sizeof(FENCE);
420 buffer = priv->buffer + sizeof(int);
423 * If the record is bigger then our buffer, we receive only
424 * a part of it. We can get the rest later.
426 if (iucv_data_count > NET_BUFFER_SIZE)
427 iucv_data_count = NET_BUFFER_SIZE;
428 rc = iucv_message_receive(priv->path,
429 &priv->local_interrupt_buffer,
430 0, buffer, iucv_data_count,
431 &priv->residual_length);
432 spin_unlock_bh(&priv->priv_lock);
433 /* An rc of 5 indicates that the record was bigger then
434 * the buffer, which is OK for us. A 9 indicates that the
435 * record was purged befor we could receive it.
440 atomic_set(&priv->receive_ready, 0);
445 priv->buffer_free = 0;
446 user_data_count += iucv_data_count;
447 priv->current_position = priv->buffer;
448 if (priv->residual_length == 0){
449 /* the whole record has been captured,
450 * now add the fence */
451 atomic_dec(&priv->receive_ready);
452 buffer = priv->buffer + user_data_count;
453 memcpy(buffer, FENCE, sizeof(FENCE));
454 user_data_count += sizeof(FENCE);
456 priv->remaining = user_data_count;
463 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
464 size_t count, loff_t * ppos)
467 struct vmlogrdr_priv_t * priv = filp->private_data;
469 while (priv->buffer_free) {
470 rc = vmlogrdr_receive_data(priv);
472 rc = wait_event_interruptible(read_wait_queue,
473 atomic_read(&priv->receive_ready));
478 /* copy only up to end of record */
479 if (count > priv->remaining)
480 count = priv->remaining;
482 if (copy_to_user(data, priv->current_position, count))
486 priv->current_position += count;
487 priv->remaining -= count;
489 /* if all data has been transferred, set buffer free */
490 if (priv->remaining == 0)
491 priv->buffer_free = 1;
496 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
497 struct device_attribute *attr,
498 const char * buf, size_t count)
500 struct vmlogrdr_priv_t *priv = dev->driver_data;
517 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
518 struct device_attribute *attr,
521 struct vmlogrdr_priv_t *priv = dev->driver_data;
522 return sprintf(buf, "%u\n", priv->autopurge);
526 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
527 vmlogrdr_autopurge_store);
530 static ssize_t vmlogrdr_purge_store(struct device * dev,
531 struct device_attribute *attr,
532 const char * buf, size_t count)
536 char cp_response[80];
537 struct vmlogrdr_priv_t *priv = dev->driver_data;
542 memset(cp_command, 0x00, sizeof(cp_command));
543 memset(cp_response, 0x00, sizeof(cp_response));
546 * The recording command needs to be called with option QID
547 * for guests that have previlege classes A or B.
548 * Other guests will not recognize the command and we have to
549 * issue the same command without the QID parameter.
552 if (recording_class_AB)
553 snprintf(cp_command, sizeof(cp_command),
554 "RECORDING %s PURGE QID * ",
555 priv->recording_name);
557 snprintf(cp_command, sizeof(cp_command),
558 "RECORDING %s PURGE ",
559 priv->recording_name);
561 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
567 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
570 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct vmlogrdr_priv_t *priv = dev->driver_data;
579 priv->autorecording=0;
582 priv->autorecording=1;
591 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
592 struct device_attribute *attr,
595 struct vmlogrdr_priv_t *priv = dev->driver_data;
596 return sprintf(buf, "%u\n", priv->autorecording);
600 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
601 vmlogrdr_autorecording_store);
604 static ssize_t vmlogrdr_recording_store(struct device * dev,
605 struct device_attribute *attr,
606 const char * buf, size_t count)
608 struct vmlogrdr_priv_t *priv = dev->driver_data;
613 ret = vmlogrdr_recording(priv,0,0);
616 ret = vmlogrdr_recording(priv,1,0);
629 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
632 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
636 char cp_command[] = "QUERY RECORDING ";
639 cpcmd(cp_command, buf, 4096, NULL);
645 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
648 static struct attribute *vmlogrdr_attrs[] = {
649 &dev_attr_autopurge.attr,
650 &dev_attr_purge.attr,
651 &dev_attr_autorecording.attr,
652 &dev_attr_recording.attr,
656 static struct attribute_group vmlogrdr_attr_group = {
657 .attrs = vmlogrdr_attrs,
660 static struct class *vmlogrdr_class;
661 static struct device_driver vmlogrdr_driver = {
667 static int vmlogrdr_register_driver(void)
671 /* Register with iucv driver */
672 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
676 ret = driver_register(&vmlogrdr_driver);
680 ret = driver_create_file(&vmlogrdr_driver,
681 &driver_attr_recording_status);
685 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
686 if (IS_ERR(vmlogrdr_class)) {
687 ret = PTR_ERR(vmlogrdr_class);
688 vmlogrdr_class = NULL;
694 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
696 driver_unregister(&vmlogrdr_driver);
698 iucv_unregister(&vmlogrdr_iucv_handler, 1);
704 static void vmlogrdr_unregister_driver(void)
706 class_destroy(vmlogrdr_class);
707 vmlogrdr_class = NULL;
708 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
709 driver_unregister(&vmlogrdr_driver);
710 iucv_unregister(&vmlogrdr_iucv_handler, 1);
714 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
719 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
721 snprintf(dev->bus_id, BUS_ID_SIZE, "%s",
722 priv->internal_name);
723 dev->bus = &iucv_bus;
724 dev->parent = iucv_root;
725 dev->driver = &vmlogrdr_driver;
727 * The release function could be called after the
728 * module has been unloaded. It's _only_ task is to
729 * free the struct. Therefore, we specify kfree()
730 * directly here. (Probably a little bit obfuscating
733 dev->release = (void (*)(struct device *))kfree;
736 ret = device_register(dev);
740 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
742 device_unregister(dev);
745 priv->class_device = device_create_drvdata(vmlogrdr_class, dev,
746 MKDEV(vmlogrdr_major,
748 priv, "%s", dev->bus_id);
749 if (IS_ERR(priv->class_device)) {
750 ret = PTR_ERR(priv->class_device);
751 priv->class_device=NULL;
752 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
753 device_unregister(dev);
761 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
763 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
764 if (priv->device != NULL) {
765 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
766 device_unregister(priv->device);
773 static int vmlogrdr_register_cdev(dev_t dev)
776 vmlogrdr_cdev = cdev_alloc();
777 if (!vmlogrdr_cdev) {
780 vmlogrdr_cdev->owner = THIS_MODULE;
781 vmlogrdr_cdev->ops = &vmlogrdr_fops;
782 vmlogrdr_cdev->dev = dev;
783 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
787 // cleanup: cdev is not fully registered, no cdev_del here!
788 kobject_put(&vmlogrdr_cdev->kobj);
794 static void vmlogrdr_cleanup(void)
799 cdev_del(vmlogrdr_cdev);
802 for (i=0; i < MAXMINOR; ++i ) {
803 vmlogrdr_unregister_device(&sys_ser[i]);
804 free_page((unsigned long)sys_ser[i].buffer);
806 vmlogrdr_unregister_driver();
807 if (vmlogrdr_major) {
808 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
814 static int __init vmlogrdr_init(void)
820 if (! MACHINE_IS_VM) {
821 printk (KERN_ERR "vmlogrdr: not running under VM, "
822 "driver not loaded.\n");
826 recording_class_AB = vmlogrdr_get_recording_class_AB();
828 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
831 vmlogrdr_major = MAJOR(dev);
833 rc=vmlogrdr_register_driver();
837 for (i=0; i < MAXMINOR; ++i ) {
838 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
839 if (!sys_ser[i].buffer) {
843 sys_ser[i].current_position = sys_ser[i].buffer;
844 rc=vmlogrdr_register_device(&sys_ser[i]);
851 rc = vmlogrdr_register_cdev(dev);
862 static void __exit vmlogrdr_exit(void)
869 module_init(vmlogrdr_init);
870 module_exit(vmlogrdr_exit);