2 * drivers/s390/char/vmlogrdr.c
3 * character device driver for reading z/VM system service records
6 * Copyright IBM Corp. 2004, 2009
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>
14 #define KMSG_COMPONENT "vmlogrdr"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <asm/atomic.h>
25 #include <asm/uaccess.h>
26 #include <asm/cpcmd.h>
27 #include <asm/debug.h>
28 #include <asm/ebcdic.h>
29 #include <net/iucv/iucv.h>
30 #include <linux/kmod.h>
31 #include <linux/cdev.h>
32 #include <linux/device.h>
33 #include <linux/smp_lock.h>
34 #include <linux/string.h>
37 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
38 " Stefan Weinhuber (wein@de.ibm.com)");
39 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
40 "system service records.");
41 MODULE_LICENSE("GPL");
45 * The size of the buffer for iucv data transfer is one page,
46 * but in addition to the data we read from iucv we also
47 * place an integer and some characters into that buffer,
48 * so the maximum size for record data is a little less then
51 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
54 * The elements that are concurrently accessed by bottom halves are
55 * connection_established, iucv_path_severed, local_interrupt_buffer
56 * and receive_ready. The first three can be protected by
57 * priv_lock. receive_ready is atomic, so it can be incremented and
58 * decremented without holding a lock.
59 * The variable dev_in_use needs to be protected by the lock, since
60 * it's a flag used by open to make sure that the device is opened only
61 * by one user at the same time.
63 struct vmlogrdr_priv_t {
64 char system_service[8];
65 char internal_name[8];
66 char recording_name[8];
67 struct iucv_path *path;
68 int connection_established;
69 int iucv_path_severed;
70 struct iucv_message local_interrupt_buffer;
71 atomic_t receive_ready;
74 char * current_position;
76 ulong residual_length;
78 int dev_in_use; /* 1: already opened, 0: not opened*/
80 struct device *device;
81 struct device *class_device;
88 * File operation structure for vmlogrdr devices
90 static int vmlogrdr_open(struct inode *, struct file *);
91 static int vmlogrdr_release(struct inode *, struct file *);
92 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
93 size_t count, loff_t * ppos);
95 static const struct file_operations vmlogrdr_fops = {
97 .open = vmlogrdr_open,
98 .release = vmlogrdr_release,
99 .read = vmlogrdr_read,
104 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
105 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
106 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
107 struct iucv_message *);
110 static struct iucv_handler vmlogrdr_iucv_handler = {
111 .path_complete = vmlogrdr_iucv_path_complete,
112 .path_severed = vmlogrdr_iucv_path_severed,
113 .message_pending = vmlogrdr_iucv_message_pending,
117 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
118 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
121 * pointer to system service private structure
122 * minor number 0 --> logrec
123 * minor number 1 --> account
124 * minor number 2 --> symptom
127 static struct vmlogrdr_priv_t sys_ser[] = {
128 { .system_service = "*LOGREC ",
129 .internal_name = "logrec",
130 .recording_name = "EREP",
133 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
137 { .system_service = "*ACCOUNT",
138 .internal_name = "account",
139 .recording_name = "ACCOUNT",
142 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
146 { .system_service = "*SYMPTOM",
147 .internal_name = "symptom",
148 .recording_name = "SYMPTOM",
151 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
157 #define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
159 static char FENCE[] = {"EOR"};
160 static int vmlogrdr_major = 0;
161 static struct cdev *vmlogrdr_cdev = NULL;
162 static int recording_class_AB;
165 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
167 struct vmlogrdr_priv_t * logptr = path->private;
169 spin_lock(&logptr->priv_lock);
170 logptr->connection_established = 1;
171 spin_unlock(&logptr->priv_lock);
172 wake_up(&conn_wait_queue);
176 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
178 struct vmlogrdr_priv_t * logptr = path->private;
179 u8 reason = (u8) ipuser[8];
181 pr_err("vmlogrdr: connection severed with reason %i\n", reason);
183 iucv_path_sever(path, NULL);
187 spin_lock(&logptr->priv_lock);
188 logptr->connection_established = 0;
189 logptr->iucv_path_severed = 1;
190 spin_unlock(&logptr->priv_lock);
192 wake_up(&conn_wait_queue);
193 /* just in case we're sleeping waiting for a record */
194 wake_up_interruptible(&read_wait_queue);
198 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
199 struct iucv_message *msg)
201 struct vmlogrdr_priv_t * logptr = path->private;
204 * This function is the bottom half so it should be quick.
205 * Copy the external interrupt data into our local eib and increment
208 spin_lock(&logptr->priv_lock);
209 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
210 atomic_inc(&logptr->receive_ready);
211 spin_unlock(&logptr->priv_lock);
212 wake_up_interruptible(&read_wait_queue);
216 static int vmlogrdr_get_recording_class_AB(void)
218 static const char cp_command[] = "QUERY COMMAND RECORDING ";
219 char cp_response[80];
223 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
224 len = strnlen(cp_response,sizeof(cp_response));
226 tail=strnchr(cp_response,len,'=');
230 if (!strncmp("ANY",tail,3))
232 if (!strncmp("NONE",tail,4))
235 * expect comma separated list of classes here, if one of them
236 * is A or B return 1 otherwise 0
238 for (i=tail-cp_response; i<len; i++)
239 if ( cp_response[i]=='A' || cp_response[i]=='B' )
245 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
246 int action, int purge)
250 char cp_response[160];
251 char *onoff, *qid_string;
254 onoff = ((action == 1) ? "ON" : "OFF");
255 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
258 * The recording commands needs to be called with option QID
259 * for guests that have previlege classes A or B.
260 * Purging has to be done as separate step, because recording
261 * can't be switched on as long as records are on the queue.
262 * Doing both at the same time doesn't work.
264 if (purge && (action == 1)) {
265 memset(cp_command, 0x00, sizeof(cp_command));
266 memset(cp_response, 0x00, sizeof(cp_response));
267 snprintf(cp_command, sizeof(cp_command),
268 "RECORDING %s PURGE %s",
269 logptr->recording_name,
271 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
274 memset(cp_command, 0x00, sizeof(cp_command));
275 memset(cp_response, 0x00, sizeof(cp_response));
276 snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
277 logptr->recording_name,
280 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
281 /* The recording command will usually answer with 'Command complete'
282 * on success, but when the specific service was never connected
283 * before then there might be an additional informational message
284 * 'HCPCRC8072I Recording entry not found' before the
285 * 'Command complete'. So I use strstr rather then the strncmp.
287 if (strstr(cp_response,"Command complete"))
292 * If we turn recording off, we have to purge any remaining records
293 * afterwards, as a large number of queued records may impact z/VM
296 if (purge && (action == 0)) {
297 memset(cp_command, 0x00, sizeof(cp_command));
298 memset(cp_response, 0x00, sizeof(cp_response));
299 snprintf(cp_command, sizeof(cp_command),
300 "RECORDING %s PURGE %s",
301 logptr->recording_name,
303 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
310 static int vmlogrdr_open (struct inode *inode, struct file *filp)
313 struct vmlogrdr_priv_t * logptr = NULL;
317 dev_num = iminor(inode);
318 if (dev_num > MAXMINOR)
320 logptr = &sys_ser[dev_num];
323 * only allow for blocking reads to be open
325 if (filp->f_flags & O_NONBLOCK)
328 /* Besure this device hasn't already been opened */
329 spin_lock_bh(&logptr->priv_lock);
330 if (logptr->dev_in_use) {
331 spin_unlock_bh(&logptr->priv_lock);
334 logptr->dev_in_use = 1;
335 logptr->connection_established = 0;
336 logptr->iucv_path_severed = 0;
337 atomic_set(&logptr->receive_ready, 0);
338 logptr->buffer_free = 1;
339 spin_unlock_bh(&logptr->priv_lock);
341 /* set the file options */
342 filp->private_data = logptr;
343 filp->f_op = &vmlogrdr_fops;
345 /* start recording for this service*/
346 if (logptr->autorecording) {
347 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
349 pr_warning("vmlogrdr: failed to start "
350 "recording automatically\n");
353 /* create connection to the system service */
354 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
357 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
358 logptr->system_service, NULL, NULL,
361 pr_err("vmlogrdr: iucv connection to %s "
362 "failed with rc %i \n",
363 logptr->system_service, connect_rc);
367 /* We've issued the connect and now we must wait for a
368 * ConnectionComplete or ConnectinSevered Interrupt
369 * before we can continue to process.
371 wait_event(conn_wait_queue, (logptr->connection_established)
372 || (logptr->iucv_path_severed));
373 if (logptr->iucv_path_severed)
375 nonseekable_open(inode, filp);
379 if (logptr->autorecording)
380 vmlogrdr_recording(logptr,0,logptr->autopurge);
382 kfree(logptr->path); /* kfree(NULL) is ok. */
385 logptr->dev_in_use = 0;
390 static int vmlogrdr_release (struct inode *inode, struct file *filp)
394 struct vmlogrdr_priv_t * logptr = filp->private_data;
396 iucv_path_sever(logptr->path, NULL);
399 if (logptr->autorecording) {
400 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
402 pr_warning("vmlogrdr: failed to stop "
403 "recording automatically\n");
405 logptr->dev_in_use = 0;
411 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
414 /* we need to keep track of two data sizes here:
415 * The number of bytes we need to receive from iucv and
416 * the total number of bytes we actually write into the buffer.
418 int user_data_count, iucv_data_count;
421 if (atomic_read(&priv->receive_ready)) {
422 spin_lock_bh(&priv->priv_lock);
423 if (priv->residual_length){
424 /* receive second half of a record */
425 iucv_data_count = priv->residual_length;
427 buffer = priv->buffer;
429 /* receive a new record:
430 * We need to return the total length of the record
431 * + size of FENCE in the first 4 bytes of the buffer.
433 iucv_data_count = priv->local_interrupt_buffer.length;
434 user_data_count = sizeof(int);
435 temp = (int*)priv->buffer;
436 *temp= iucv_data_count + sizeof(FENCE);
437 buffer = priv->buffer + sizeof(int);
440 * If the record is bigger than our buffer, we receive only
441 * a part of it. We can get the rest later.
443 if (iucv_data_count > NET_BUFFER_SIZE)
444 iucv_data_count = NET_BUFFER_SIZE;
445 rc = iucv_message_receive(priv->path,
446 &priv->local_interrupt_buffer,
447 0, buffer, iucv_data_count,
448 &priv->residual_length);
449 spin_unlock_bh(&priv->priv_lock);
450 /* An rc of 5 indicates that the record was bigger than
451 * the buffer, which is OK for us. A 9 indicates that the
452 * record was purged befor we could receive it.
457 atomic_set(&priv->receive_ready, 0);
462 priv->buffer_free = 0;
463 user_data_count += iucv_data_count;
464 priv->current_position = priv->buffer;
465 if (priv->residual_length == 0){
466 /* the whole record has been captured,
467 * now add the fence */
468 atomic_dec(&priv->receive_ready);
469 buffer = priv->buffer + user_data_count;
470 memcpy(buffer, FENCE, sizeof(FENCE));
471 user_data_count += sizeof(FENCE);
473 priv->remaining = user_data_count;
480 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
481 size_t count, loff_t * ppos)
484 struct vmlogrdr_priv_t * priv = filp->private_data;
486 while (priv->buffer_free) {
487 rc = vmlogrdr_receive_data(priv);
489 rc = wait_event_interruptible(read_wait_queue,
490 atomic_read(&priv->receive_ready));
495 /* copy only up to end of record */
496 if (count > priv->remaining)
497 count = priv->remaining;
499 if (copy_to_user(data, priv->current_position, count))
503 priv->current_position += count;
504 priv->remaining -= count;
506 /* if all data has been transferred, set buffer free */
507 if (priv->remaining == 0)
508 priv->buffer_free = 1;
513 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
514 struct device_attribute *attr,
515 const char * buf, size_t count)
517 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
534 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
535 struct device_attribute *attr,
538 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
539 return sprintf(buf, "%u\n", priv->autopurge);
543 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
544 vmlogrdr_autopurge_store);
547 static ssize_t vmlogrdr_purge_store(struct device * dev,
548 struct device_attribute *attr,
549 const char * buf, size_t count)
553 char cp_response[80];
554 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
559 memset(cp_command, 0x00, sizeof(cp_command));
560 memset(cp_response, 0x00, sizeof(cp_response));
563 * The recording command needs to be called with option QID
564 * for guests that have previlege classes A or B.
565 * Other guests will not recognize the command and we have to
566 * issue the same command without the QID parameter.
569 if (recording_class_AB)
570 snprintf(cp_command, sizeof(cp_command),
571 "RECORDING %s PURGE QID * ",
572 priv->recording_name);
574 snprintf(cp_command, sizeof(cp_command),
575 "RECORDING %s PURGE ",
576 priv->recording_name);
578 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
584 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
587 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
588 struct device_attribute *attr,
589 const char *buf, size_t count)
591 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
596 priv->autorecording=0;
599 priv->autorecording=1;
608 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
609 struct device_attribute *attr,
612 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
613 return sprintf(buf, "%u\n", priv->autorecording);
617 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
618 vmlogrdr_autorecording_store);
621 static ssize_t vmlogrdr_recording_store(struct device * dev,
622 struct device_attribute *attr,
623 const char * buf, size_t count)
625 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
630 ret = vmlogrdr_recording(priv,0,0);
633 ret = vmlogrdr_recording(priv,1,0);
646 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
649 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
653 static const char cp_command[] = "QUERY RECORDING ";
656 cpcmd(cp_command, buf, 4096, NULL);
662 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
665 static struct attribute *vmlogrdr_attrs[] = {
666 &dev_attr_autopurge.attr,
667 &dev_attr_purge.attr,
668 &dev_attr_autorecording.attr,
669 &dev_attr_recording.attr,
673 static int vmlogrdr_pm_prepare(struct device *dev)
676 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
680 spin_lock_bh(&priv->priv_lock);
681 if (priv->dev_in_use)
683 spin_unlock_bh(&priv->priv_lock);
686 pr_err("vmlogrdr: device %s is busy. Refuse to suspend.\n",
692 static const struct dev_pm_ops vmlogrdr_pm_ops = {
693 .prepare = vmlogrdr_pm_prepare,
696 static struct attribute_group vmlogrdr_attr_group = {
697 .attrs = vmlogrdr_attrs,
700 static struct class *vmlogrdr_class;
701 static struct device_driver vmlogrdr_driver = {
704 .pm = &vmlogrdr_pm_ops,
708 static int vmlogrdr_register_driver(void)
712 /* Register with iucv driver */
713 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
717 ret = driver_register(&vmlogrdr_driver);
721 ret = driver_create_file(&vmlogrdr_driver,
722 &driver_attr_recording_status);
726 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
727 if (IS_ERR(vmlogrdr_class)) {
728 ret = PTR_ERR(vmlogrdr_class);
729 vmlogrdr_class = NULL;
735 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
737 driver_unregister(&vmlogrdr_driver);
739 iucv_unregister(&vmlogrdr_iucv_handler, 1);
745 static void vmlogrdr_unregister_driver(void)
747 class_destroy(vmlogrdr_class);
748 vmlogrdr_class = NULL;
749 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
750 driver_unregister(&vmlogrdr_driver);
751 iucv_unregister(&vmlogrdr_iucv_handler, 1);
755 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
760 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
762 dev_set_name(dev, priv->internal_name);
763 dev->bus = &iucv_bus;
764 dev->parent = iucv_root;
765 dev->driver = &vmlogrdr_driver;
766 dev_set_drvdata(dev, priv);
768 * The release function could be called after the
769 * module has been unloaded. It's _only_ task is to
770 * free the struct. Therefore, we specify kfree()
771 * directly here. (Probably a little bit obfuscating
774 dev->release = (void (*)(struct device *))kfree;
777 ret = device_register(dev);
783 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
785 device_unregister(dev);
788 priv->class_device = device_create(vmlogrdr_class, dev,
789 MKDEV(vmlogrdr_major,
791 priv, "%s", dev_name(dev));
792 if (IS_ERR(priv->class_device)) {
793 ret = PTR_ERR(priv->class_device);
794 priv->class_device=NULL;
795 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
796 device_unregister(dev);
804 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
806 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
807 if (priv->device != NULL) {
808 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
809 device_unregister(priv->device);
816 static int vmlogrdr_register_cdev(dev_t dev)
819 vmlogrdr_cdev = cdev_alloc();
820 if (!vmlogrdr_cdev) {
823 vmlogrdr_cdev->owner = THIS_MODULE;
824 vmlogrdr_cdev->ops = &vmlogrdr_fops;
825 vmlogrdr_cdev->dev = dev;
826 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
830 // cleanup: cdev is not fully registered, no cdev_del here!
831 kobject_put(&vmlogrdr_cdev->kobj);
837 static void vmlogrdr_cleanup(void)
842 cdev_del(vmlogrdr_cdev);
845 for (i=0; i < MAXMINOR; ++i ) {
846 vmlogrdr_unregister_device(&sys_ser[i]);
847 free_page((unsigned long)sys_ser[i].buffer);
849 vmlogrdr_unregister_driver();
850 if (vmlogrdr_major) {
851 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
857 static int __init vmlogrdr_init(void)
863 if (! MACHINE_IS_VM) {
864 pr_err("not running under VM, driver not loaded.\n");
868 recording_class_AB = vmlogrdr_get_recording_class_AB();
870 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
873 vmlogrdr_major = MAJOR(dev);
875 rc=vmlogrdr_register_driver();
879 for (i=0; i < MAXMINOR; ++i ) {
880 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
881 if (!sys_ser[i].buffer) {
885 sys_ser[i].current_position = sys_ser[i].buffer;
886 rc=vmlogrdr_register_device(&sys_ser[i]);
893 rc = vmlogrdr_register_cdev(dev);
904 static void __exit vmlogrdr_exit(void)
911 module_init(vmlogrdr_init);
912 module_exit(vmlogrdr_exit);