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/smp_lock.h>
29 #include <linux/string.h>
34 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
35 " Stefan Weinhuber (wein@de.ibm.com)");
36 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
37 "system service records.");
38 MODULE_LICENSE("GPL");
42 * The size of the buffer for iucv data transfer is one page,
43 * but in addition to the data we read from iucv we also
44 * place an integer and some characters into that buffer,
45 * so the maximum size for record data is a little less then
48 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
51 * The elements that are concurrently accessed by bottom halves are
52 * connection_established, iucv_path_severed, local_interrupt_buffer
53 * and receive_ready. The first three can be protected by
54 * priv_lock. receive_ready is atomic, so it can be incremented and
55 * decremented without holding a lock.
56 * The variable dev_in_use needs to be protected by the lock, since
57 * it's a flag used by open to make sure that the device is opened only
58 * by one user at the same time.
60 struct vmlogrdr_priv_t {
61 char system_service[8];
62 char internal_name[8];
63 char recording_name[8];
64 struct iucv_path *path;
65 int connection_established;
66 int iucv_path_severed;
67 struct iucv_message local_interrupt_buffer;
68 atomic_t receive_ready;
71 char * current_position;
73 ulong residual_length;
75 int dev_in_use; /* 1: already opened, 0: not opened*/
77 struct device *device;
78 struct device *class_device;
85 * File operation structure for vmlogrdr devices
87 static int vmlogrdr_open(struct inode *, struct file *);
88 static int vmlogrdr_release(struct inode *, struct file *);
89 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
90 size_t count, loff_t * ppos);
92 static const struct file_operations vmlogrdr_fops = {
94 .open = vmlogrdr_open,
95 .release = vmlogrdr_release,
96 .read = vmlogrdr_read,
100 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
101 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
102 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
103 struct iucv_message *);
106 static struct iucv_handler vmlogrdr_iucv_handler = {
107 .path_complete = vmlogrdr_iucv_path_complete,
108 .path_severed = vmlogrdr_iucv_path_severed,
109 .message_pending = vmlogrdr_iucv_message_pending,
113 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
114 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
117 * pointer to system service private structure
118 * minor number 0 --> logrec
119 * minor number 1 --> account
120 * minor number 2 --> symptom
123 static struct vmlogrdr_priv_t sys_ser[] = {
124 { .system_service = "*LOGREC ",
125 .internal_name = "logrec",
126 .recording_name = "EREP",
129 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
133 { .system_service = "*ACCOUNT",
134 .internal_name = "account",
135 .recording_name = "ACCOUNT",
138 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
142 { .system_service = "*SYMPTOM",
143 .internal_name = "symptom",
144 .recording_name = "SYMPTOM",
147 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
153 #define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
155 static char FENCE[] = {"EOR"};
156 static int vmlogrdr_major = 0;
157 static struct cdev *vmlogrdr_cdev = NULL;
158 static int recording_class_AB;
161 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
163 struct vmlogrdr_priv_t * logptr = path->private;
165 spin_lock(&logptr->priv_lock);
166 logptr->connection_established = 1;
167 spin_unlock(&logptr->priv_lock);
168 wake_up(&conn_wait_queue);
172 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
174 struct vmlogrdr_priv_t * logptr = path->private;
175 u8 reason = (u8) ipuser[8];
177 printk (KERN_ERR "vmlogrdr: connection severed with"
178 " reason %i\n", reason);
180 iucv_path_sever(path, NULL);
184 spin_lock(&logptr->priv_lock);
185 logptr->connection_established = 0;
186 logptr->iucv_path_severed = 1;
187 spin_unlock(&logptr->priv_lock);
189 wake_up(&conn_wait_queue);
190 /* just in case we're sleeping waiting for a record */
191 wake_up_interruptible(&read_wait_queue);
195 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
196 struct iucv_message *msg)
198 struct vmlogrdr_priv_t * logptr = path->private;
201 * This function is the bottom half so it should be quick.
202 * Copy the external interrupt data into our local eib and increment
205 spin_lock(&logptr->priv_lock);
206 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
207 atomic_inc(&logptr->receive_ready);
208 spin_unlock(&logptr->priv_lock);
209 wake_up_interruptible(&read_wait_queue);
213 static int vmlogrdr_get_recording_class_AB(void)
215 char cp_command[]="QUERY COMMAND RECORDING ";
216 char cp_response[80];
220 printk (KERN_DEBUG "vmlogrdr: query command: %s\n", cp_command);
221 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
222 printk (KERN_DEBUG "vmlogrdr: response: %s", cp_response);
223 len = strnlen(cp_response,sizeof(cp_response));
225 tail=strnchr(cp_response,len,'=');
229 if (!strncmp("ANY",tail,3))
231 if (!strncmp("NONE",tail,4))
234 * expect comma separated list of classes here, if one of them
235 * is A or B return 1 otherwise 0
237 for (i=tail-cp_response; i<len; i++)
238 if ( cp_response[i]=='A' || cp_response[i]=='B' )
244 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
245 int action, int purge)
249 char cp_response[160];
250 char *onoff, *qid_string;
252 memset(cp_command, 0x00, sizeof(cp_command));
253 memset(cp_response, 0x00, sizeof(cp_response));
255 onoff = ((action == 1) ? "ON" : "OFF");
256 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
259 * The recording commands needs to be called with option QID
260 * for guests that have previlege classes A or B.
261 * Purging has to be done as separate step, because recording
262 * can't be switched on as long as records are on the queue.
263 * Doing both at the same time doesn't work.
267 snprintf(cp_command, sizeof(cp_command),
268 "RECORDING %s PURGE %s",
269 logptr->recording_name,
272 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n",
274 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
275 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
279 memset(cp_command, 0x00, sizeof(cp_command));
280 memset(cp_response, 0x00, sizeof(cp_response));
281 snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
282 logptr->recording_name,
286 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
287 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
288 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
290 /* The recording command will usually answer with 'Command complete'
291 * on success, but when the specific service was never connected
292 * before then there might be an additional informational message
293 * 'HCPCRC8072I Recording entry not found' before the
294 * 'Command complete'. So I use strstr rather then the strncmp.
296 if (strstr(cp_response,"Command complete"))
304 static int vmlogrdr_open (struct inode *inode, struct file *filp)
307 struct vmlogrdr_priv_t * logptr = NULL;
311 dev_num = iminor(inode);
312 if (dev_num > MAXMINOR)
314 logptr = &sys_ser[dev_num];
317 * only allow for blocking reads to be open
319 if (filp->f_flags & O_NONBLOCK)
322 /* Besure this device hasn't already been opened */
324 spin_lock_bh(&logptr->priv_lock);
325 if (logptr->dev_in_use) {
326 spin_unlock_bh(&logptr->priv_lock);
330 logptr->dev_in_use = 1;
331 logptr->connection_established = 0;
332 logptr->iucv_path_severed = 0;
333 atomic_set(&logptr->receive_ready, 0);
334 logptr->buffer_free = 1;
335 spin_unlock_bh(&logptr->priv_lock);
337 /* set the file options */
338 filp->private_data = logptr;
339 filp->f_op = &vmlogrdr_fops;
341 /* start recording for this service*/
342 if (logptr->autorecording) {
343 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
345 printk (KERN_WARNING "vmlogrdr: failed to start "
346 "recording automatically\n");
349 /* create connection to the system service */
350 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
353 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
354 logptr->system_service, NULL, NULL,
357 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
358 "failed with rc %i \n", logptr->system_service,
363 /* We've issued the connect and now we must wait for a
364 * ConnectionComplete or ConnectinSevered Interrupt
365 * before we can continue to process.
367 wait_event(conn_wait_queue, (logptr->connection_established)
368 || (logptr->iucv_path_severed));
369 if (logptr->iucv_path_severed)
371 ret = nonseekable_open(inode, filp);
376 if (logptr->autorecording)
377 vmlogrdr_recording(logptr,0,logptr->autopurge);
379 kfree(logptr->path); /* kfree(NULL) is ok. */
382 logptr->dev_in_use = 0;
388 static int vmlogrdr_release (struct inode *inode, struct file *filp)
392 struct vmlogrdr_priv_t * logptr = filp->private_data;
394 iucv_path_sever(logptr->path, NULL);
397 if (logptr->autorecording) {
398 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
400 printk (KERN_WARNING "vmlogrdr: failed to stop "
401 "recording automatically\n");
403 logptr->dev_in_use = 0;
409 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
412 /* we need to keep track of two data sizes here:
413 * The number of bytes we need to receive from iucv and
414 * the total number of bytes we actually write into the buffer.
416 int user_data_count, iucv_data_count;
419 if (atomic_read(&priv->receive_ready)) {
420 spin_lock_bh(&priv->priv_lock);
421 if (priv->residual_length){
422 /* receive second half of a record */
423 iucv_data_count = priv->residual_length;
425 buffer = priv->buffer;
427 /* receive a new record:
428 * We need to return the total length of the record
429 * + size of FENCE in the first 4 bytes of the buffer.
431 iucv_data_count = priv->local_interrupt_buffer.length;
432 user_data_count = sizeof(int);
433 temp = (int*)priv->buffer;
434 *temp= iucv_data_count + sizeof(FENCE);
435 buffer = priv->buffer + sizeof(int);
438 * If the record is bigger then our buffer, we receive only
439 * a part of it. We can get the rest later.
441 if (iucv_data_count > NET_BUFFER_SIZE)
442 iucv_data_count = NET_BUFFER_SIZE;
443 rc = iucv_message_receive(priv->path,
444 &priv->local_interrupt_buffer,
445 0, buffer, iucv_data_count,
446 &priv->residual_length);
447 spin_unlock_bh(&priv->priv_lock);
448 /* An rc of 5 indicates that the record was bigger then
449 * the buffer, which is OK for us. A 9 indicates that the
450 * record was purged befor we could receive it.
455 atomic_set(&priv->receive_ready, 0);
460 priv->buffer_free = 0;
461 user_data_count += iucv_data_count;
462 priv->current_position = priv->buffer;
463 if (priv->residual_length == 0){
464 /* the whole record has been captured,
465 * now add the fence */
466 atomic_dec(&priv->receive_ready);
467 buffer = priv->buffer + user_data_count;
468 memcpy(buffer, FENCE, sizeof(FENCE));
469 user_data_count += sizeof(FENCE);
471 priv->remaining = user_data_count;
478 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
479 size_t count, loff_t * ppos)
482 struct vmlogrdr_priv_t * priv = filp->private_data;
484 while (priv->buffer_free) {
485 rc = vmlogrdr_receive_data(priv);
487 rc = wait_event_interruptible(read_wait_queue,
488 atomic_read(&priv->receive_ready));
493 /* copy only up to end of record */
494 if (count > priv->remaining)
495 count = priv->remaining;
497 if (copy_to_user(data, priv->current_position, count))
501 priv->current_position += count;
502 priv->remaining -= count;
504 /* if all data has been transferred, set buffer free */
505 if (priv->remaining == 0)
506 priv->buffer_free = 1;
511 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
512 struct device_attribute *attr,
513 const char * buf, size_t count)
515 struct vmlogrdr_priv_t *priv = dev->driver_data;
532 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
533 struct device_attribute *attr,
536 struct vmlogrdr_priv_t *priv = dev->driver_data;
537 return sprintf(buf, "%u\n", priv->autopurge);
541 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
542 vmlogrdr_autopurge_store);
545 static ssize_t vmlogrdr_purge_store(struct device * dev,
546 struct device_attribute *attr,
547 const char * buf, size_t count)
551 char cp_response[80];
552 struct vmlogrdr_priv_t *priv = dev->driver_data;
557 memset(cp_command, 0x00, sizeof(cp_command));
558 memset(cp_response, 0x00, sizeof(cp_response));
561 * The recording command needs to be called with option QID
562 * for guests that have previlege classes A or B.
563 * Other guests will not recognize the command and we have to
564 * issue the same command without the QID parameter.
567 if (recording_class_AB)
568 snprintf(cp_command, sizeof(cp_command),
569 "RECORDING %s PURGE QID * ",
570 priv->recording_name);
572 snprintf(cp_command, sizeof(cp_command),
573 "RECORDING %s PURGE ",
574 priv->recording_name);
576 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
577 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
578 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
585 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
588 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
589 struct device_attribute *attr,
590 const char *buf, size_t count)
592 struct vmlogrdr_priv_t *priv = dev->driver_data;
597 priv->autorecording=0;
600 priv->autorecording=1;
609 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
610 struct device_attribute *attr,
613 struct vmlogrdr_priv_t *priv = dev->driver_data;
614 return sprintf(buf, "%u\n", priv->autorecording);
618 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
619 vmlogrdr_autorecording_store);
622 static ssize_t vmlogrdr_recording_store(struct device * dev,
623 struct device_attribute *attr,
624 const char * buf, size_t count)
626 struct vmlogrdr_priv_t *priv = dev->driver_data;
631 ret = vmlogrdr_recording(priv,0,0);
634 ret = vmlogrdr_recording(priv,1,0);
647 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
650 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
654 char cp_command[] = "QUERY RECORDING ";
657 cpcmd(cp_command, buf, 4096, NULL);
663 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
666 static struct attribute *vmlogrdr_attrs[] = {
667 &dev_attr_autopurge.attr,
668 &dev_attr_purge.attr,
669 &dev_attr_autorecording.attr,
670 &dev_attr_recording.attr,
674 static struct attribute_group vmlogrdr_attr_group = {
675 .attrs = vmlogrdr_attrs,
678 static struct class *vmlogrdr_class;
679 static struct device_driver vmlogrdr_driver = {
685 static int vmlogrdr_register_driver(void)
689 /* Register with iucv driver */
690 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
692 printk (KERN_ERR "vmlogrdr: failed to register with "
697 ret = driver_register(&vmlogrdr_driver);
699 printk(KERN_ERR "vmlogrdr: failed to register driver.\n");
703 ret = driver_create_file(&vmlogrdr_driver,
704 &driver_attr_recording_status);
706 printk(KERN_ERR "vmlogrdr: failed to add driver attribute.\n");
710 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
711 if (IS_ERR(vmlogrdr_class)) {
712 printk(KERN_ERR "vmlogrdr: failed to create class.\n");
713 ret = PTR_ERR(vmlogrdr_class);
714 vmlogrdr_class = NULL;
720 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
722 driver_unregister(&vmlogrdr_driver);
724 iucv_unregister(&vmlogrdr_iucv_handler, 1);
730 static void vmlogrdr_unregister_driver(void)
732 class_destroy(vmlogrdr_class);
733 vmlogrdr_class = NULL;
734 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
735 driver_unregister(&vmlogrdr_driver);
736 iucv_unregister(&vmlogrdr_iucv_handler, 1);
740 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
745 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
747 snprintf(dev->bus_id, BUS_ID_SIZE, "%s",
748 priv->internal_name);
749 dev->bus = &iucv_bus;
750 dev->parent = iucv_root;
751 dev->driver = &vmlogrdr_driver;
753 * The release function could be called after the
754 * module has been unloaded. It's _only_ task is to
755 * free the struct. Therefore, we specify kfree()
756 * directly here. (Probably a little bit obfuscating
759 dev->release = (void (*)(struct device *))kfree;
762 ret = device_register(dev);
766 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
768 device_unregister(dev);
771 priv->class_device = device_create_drvdata(vmlogrdr_class, dev,
772 MKDEV(vmlogrdr_major,
774 priv, "%s", dev->bus_id);
775 if (IS_ERR(priv->class_device)) {
776 ret = PTR_ERR(priv->class_device);
777 priv->class_device=NULL;
778 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
779 device_unregister(dev);
787 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
789 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
790 if (priv->device != NULL) {
791 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
792 device_unregister(priv->device);
799 static int vmlogrdr_register_cdev(dev_t dev)
802 vmlogrdr_cdev = cdev_alloc();
803 if (!vmlogrdr_cdev) {
806 vmlogrdr_cdev->owner = THIS_MODULE;
807 vmlogrdr_cdev->ops = &vmlogrdr_fops;
808 vmlogrdr_cdev->dev = dev;
809 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
813 // cleanup: cdev is not fully registered, no cdev_del here!
814 kobject_put(&vmlogrdr_cdev->kobj);
820 static void vmlogrdr_cleanup(void)
825 cdev_del(vmlogrdr_cdev);
828 for (i=0; i < MAXMINOR; ++i ) {
829 vmlogrdr_unregister_device(&sys_ser[i]);
830 free_page((unsigned long)sys_ser[i].buffer);
832 vmlogrdr_unregister_driver();
833 if (vmlogrdr_major) {
834 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
840 static int __init vmlogrdr_init(void)
846 if (! MACHINE_IS_VM) {
847 printk (KERN_ERR "vmlogrdr: not running under VM, "
848 "driver not loaded.\n");
852 recording_class_AB = vmlogrdr_get_recording_class_AB();
854 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
857 vmlogrdr_major = MAJOR(dev);
859 rc=vmlogrdr_register_driver();
863 for (i=0; i < MAXMINOR; ++i ) {
864 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
865 if (!sys_ser[i].buffer) {
869 sys_ser[i].current_position = sys_ser[i].buffer;
870 rc=vmlogrdr_register_device(&sys_ser[i]);
877 rc = vmlogrdr_register_cdev(dev);
880 printk (KERN_INFO "vmlogrdr: driver loaded\n");
885 printk (KERN_ERR "vmlogrdr: driver not loaded.\n");
890 static void __exit vmlogrdr_exit(void)
893 printk (KERN_INFO "vmlogrdr: driver unloaded\n");
898 module_init(vmlogrdr_init);
899 module_exit(vmlogrdr_exit);