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>
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/errno.h>
20 #include <linux/types.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <asm/atomic.h>
24 #include <asm/uaccess.h>
25 #include <asm/cpcmd.h>
26 #include <asm/debug.h>
27 #include <asm/ebcdic.h>
28 #include <net/iucv/iucv.h>
29 #include <linux/kmod.h>
30 #include <linux/cdev.h>
31 #include <linux/device.h>
32 #include <linux/smp_lock.h>
33 #include <linux/string.h>
36 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
37 " Stefan Weinhuber (wein@de.ibm.com)");
38 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
39 "system service records.");
40 MODULE_LICENSE("GPL");
44 * The size of the buffer for iucv data transfer is one page,
45 * but in addition to the data we read from iucv we also
46 * place an integer and some characters into that buffer,
47 * so the maximum size for record data is a little less then
50 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
53 * The elements that are concurrently accessed by bottom halves are
54 * connection_established, iucv_path_severed, local_interrupt_buffer
55 * and receive_ready. The first three can be protected by
56 * priv_lock. receive_ready is atomic, so it can be incremented and
57 * decremented without holding a lock.
58 * The variable dev_in_use needs to be protected by the lock, since
59 * it's a flag used by open to make sure that the device is opened only
60 * by one user at the same time.
62 struct vmlogrdr_priv_t {
63 char system_service[8];
64 char internal_name[8];
65 char recording_name[8];
66 struct iucv_path *path;
67 int connection_established;
68 int iucv_path_severed;
69 struct iucv_message local_interrupt_buffer;
70 atomic_t receive_ready;
73 char * current_position;
75 ulong residual_length;
77 int dev_in_use; /* 1: already opened, 0: not opened*/
79 struct device *device;
80 struct device *class_device;
87 * File operation structure for vmlogrdr devices
89 static int vmlogrdr_open(struct inode *, struct file *);
90 static int vmlogrdr_release(struct inode *, struct file *);
91 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
92 size_t count, loff_t * ppos);
94 static const struct file_operations vmlogrdr_fops = {
96 .open = vmlogrdr_open,
97 .release = vmlogrdr_release,
98 .read = vmlogrdr_read,
102 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
103 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
104 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
105 struct iucv_message *);
108 static struct iucv_handler vmlogrdr_iucv_handler = {
109 .path_complete = vmlogrdr_iucv_path_complete,
110 .path_severed = vmlogrdr_iucv_path_severed,
111 .message_pending = vmlogrdr_iucv_message_pending,
115 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
116 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
119 * pointer to system service private structure
120 * minor number 0 --> logrec
121 * minor number 1 --> account
122 * minor number 2 --> symptom
125 static struct vmlogrdr_priv_t sys_ser[] = {
126 { .system_service = "*LOGREC ",
127 .internal_name = "logrec",
128 .recording_name = "EREP",
131 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
135 { .system_service = "*ACCOUNT",
136 .internal_name = "account",
137 .recording_name = "ACCOUNT",
140 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
144 { .system_service = "*SYMPTOM",
145 .internal_name = "symptom",
146 .recording_name = "SYMPTOM",
149 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
155 #define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
157 static char FENCE[] = {"EOR"};
158 static int vmlogrdr_major = 0;
159 static struct cdev *vmlogrdr_cdev = NULL;
160 static int recording_class_AB;
163 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
165 struct vmlogrdr_priv_t * logptr = path->private;
167 spin_lock(&logptr->priv_lock);
168 logptr->connection_established = 1;
169 spin_unlock(&logptr->priv_lock);
170 wake_up(&conn_wait_queue);
174 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
176 struct vmlogrdr_priv_t * logptr = path->private;
177 u8 reason = (u8) ipuser[8];
179 pr_err("vmlogrdr: connection severed with reason %i\n", reason);
181 iucv_path_sever(path, NULL);
185 spin_lock(&logptr->priv_lock);
186 logptr->connection_established = 0;
187 logptr->iucv_path_severed = 1;
188 spin_unlock(&logptr->priv_lock);
190 wake_up(&conn_wait_queue);
191 /* just in case we're sleeping waiting for a record */
192 wake_up_interruptible(&read_wait_queue);
196 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
197 struct iucv_message *msg)
199 struct vmlogrdr_priv_t * logptr = path->private;
202 * This function is the bottom half so it should be quick.
203 * Copy the external interrupt data into our local eib and increment
206 spin_lock(&logptr->priv_lock);
207 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
208 atomic_inc(&logptr->receive_ready);
209 spin_unlock(&logptr->priv_lock);
210 wake_up_interruptible(&read_wait_queue);
214 static int vmlogrdr_get_recording_class_AB(void)
216 char cp_command[]="QUERY COMMAND RECORDING ";
217 char cp_response[80];
221 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
222 len = strnlen(cp_response,sizeof(cp_response));
224 tail=strnchr(cp_response,len,'=');
228 if (!strncmp("ANY",tail,3))
230 if (!strncmp("NONE",tail,4))
233 * expect comma separated list of classes here, if one of them
234 * is A or B return 1 otherwise 0
236 for (i=tail-cp_response; i<len; i++)
237 if ( cp_response[i]=='A' || cp_response[i]=='B' )
243 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
244 int action, int purge)
248 char cp_response[160];
249 char *onoff, *qid_string;
251 memset(cp_command, 0x00, sizeof(cp_command));
252 memset(cp_response, 0x00, sizeof(cp_response));
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.
266 snprintf(cp_command, sizeof(cp_command),
267 "RECORDING %s PURGE %s",
268 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,
281 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
282 /* The recording command will usually answer with 'Command complete'
283 * on success, but when the specific service was never connected
284 * before then there might be an additional informational message
285 * 'HCPCRC8072I Recording entry not found' before the
286 * 'Command complete'. So I use strstr rather then the strncmp.
288 if (strstr(cp_response,"Command complete"))
296 static int vmlogrdr_open (struct inode *inode, struct file *filp)
299 struct vmlogrdr_priv_t * logptr = NULL;
303 dev_num = iminor(inode);
304 if (dev_num > MAXMINOR)
306 logptr = &sys_ser[dev_num];
309 * only allow for blocking reads to be open
311 if (filp->f_flags & O_NONBLOCK)
314 /* Besure this device hasn't already been opened */
316 spin_lock_bh(&logptr->priv_lock);
317 if (logptr->dev_in_use) {
318 spin_unlock_bh(&logptr->priv_lock);
322 logptr->dev_in_use = 1;
323 logptr->connection_established = 0;
324 logptr->iucv_path_severed = 0;
325 atomic_set(&logptr->receive_ready, 0);
326 logptr->buffer_free = 1;
327 spin_unlock_bh(&logptr->priv_lock);
329 /* set the file options */
330 filp->private_data = logptr;
331 filp->f_op = &vmlogrdr_fops;
333 /* start recording for this service*/
334 if (logptr->autorecording) {
335 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
337 pr_warning("vmlogrdr: failed to start "
338 "recording automatically\n");
341 /* create connection to the system service */
342 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
345 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
346 logptr->system_service, NULL, NULL,
349 pr_err("vmlogrdr: iucv connection to %s "
350 "failed with rc %i \n",
351 logptr->system_service, connect_rc);
355 /* We've issued the connect and now we must wait for a
356 * ConnectionComplete or ConnectinSevered Interrupt
357 * before we can continue to process.
359 wait_event(conn_wait_queue, (logptr->connection_established)
360 || (logptr->iucv_path_severed));
361 if (logptr->iucv_path_severed)
363 ret = nonseekable_open(inode, filp);
368 if (logptr->autorecording)
369 vmlogrdr_recording(logptr,0,logptr->autopurge);
371 kfree(logptr->path); /* kfree(NULL) is ok. */
374 logptr->dev_in_use = 0;
380 static int vmlogrdr_release (struct inode *inode, struct file *filp)
384 struct vmlogrdr_priv_t * logptr = filp->private_data;
386 iucv_path_sever(logptr->path, NULL);
389 if (logptr->autorecording) {
390 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
392 pr_warning("vmlogrdr: failed to stop "
393 "recording automatically\n");
395 logptr->dev_in_use = 0;
401 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
404 /* we need to keep track of two data sizes here:
405 * The number of bytes we need to receive from iucv and
406 * the total number of bytes we actually write into the buffer.
408 int user_data_count, iucv_data_count;
411 if (atomic_read(&priv->receive_ready)) {
412 spin_lock_bh(&priv->priv_lock);
413 if (priv->residual_length){
414 /* receive second half of a record */
415 iucv_data_count = priv->residual_length;
417 buffer = priv->buffer;
419 /* receive a new record:
420 * We need to return the total length of the record
421 * + size of FENCE in the first 4 bytes of the buffer.
423 iucv_data_count = priv->local_interrupt_buffer.length;
424 user_data_count = sizeof(int);
425 temp = (int*)priv->buffer;
426 *temp= iucv_data_count + sizeof(FENCE);
427 buffer = priv->buffer + sizeof(int);
430 * If the record is bigger than our buffer, we receive only
431 * a part of it. We can get the rest later.
433 if (iucv_data_count > NET_BUFFER_SIZE)
434 iucv_data_count = NET_BUFFER_SIZE;
435 rc = iucv_message_receive(priv->path,
436 &priv->local_interrupt_buffer,
437 0, buffer, iucv_data_count,
438 &priv->residual_length);
439 spin_unlock_bh(&priv->priv_lock);
440 /* An rc of 5 indicates that the record was bigger than
441 * the buffer, which is OK for us. A 9 indicates that the
442 * record was purged befor we could receive it.
447 atomic_set(&priv->receive_ready, 0);
452 priv->buffer_free = 0;
453 user_data_count += iucv_data_count;
454 priv->current_position = priv->buffer;
455 if (priv->residual_length == 0){
456 /* the whole record has been captured,
457 * now add the fence */
458 atomic_dec(&priv->receive_ready);
459 buffer = priv->buffer + user_data_count;
460 memcpy(buffer, FENCE, sizeof(FENCE));
461 user_data_count += sizeof(FENCE);
463 priv->remaining = user_data_count;
470 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
471 size_t count, loff_t * ppos)
474 struct vmlogrdr_priv_t * priv = filp->private_data;
476 while (priv->buffer_free) {
477 rc = vmlogrdr_receive_data(priv);
479 rc = wait_event_interruptible(read_wait_queue,
480 atomic_read(&priv->receive_ready));
485 /* copy only up to end of record */
486 if (count > priv->remaining)
487 count = priv->remaining;
489 if (copy_to_user(data, priv->current_position, count))
493 priv->current_position += count;
494 priv->remaining -= count;
496 /* if all data has been transferred, set buffer free */
497 if (priv->remaining == 0)
498 priv->buffer_free = 1;
503 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
504 struct device_attribute *attr,
505 const char * buf, size_t count)
507 struct vmlogrdr_priv_t *priv = dev->driver_data;
524 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
525 struct device_attribute *attr,
528 struct vmlogrdr_priv_t *priv = dev->driver_data;
529 return sprintf(buf, "%u\n", priv->autopurge);
533 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
534 vmlogrdr_autopurge_store);
537 static ssize_t vmlogrdr_purge_store(struct device * dev,
538 struct device_attribute *attr,
539 const char * buf, size_t count)
543 char cp_response[80];
544 struct vmlogrdr_priv_t *priv = dev->driver_data;
549 memset(cp_command, 0x00, sizeof(cp_command));
550 memset(cp_response, 0x00, sizeof(cp_response));
553 * The recording command needs to be called with option QID
554 * for guests that have previlege classes A or B.
555 * Other guests will not recognize the command and we have to
556 * issue the same command without the QID parameter.
559 if (recording_class_AB)
560 snprintf(cp_command, sizeof(cp_command),
561 "RECORDING %s PURGE QID * ",
562 priv->recording_name);
564 snprintf(cp_command, sizeof(cp_command),
565 "RECORDING %s PURGE ",
566 priv->recording_name);
568 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
574 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
577 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
578 struct device_attribute *attr,
579 const char *buf, size_t count)
581 struct vmlogrdr_priv_t *priv = dev->driver_data;
586 priv->autorecording=0;
589 priv->autorecording=1;
598 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
599 struct device_attribute *attr,
602 struct vmlogrdr_priv_t *priv = dev->driver_data;
603 return sprintf(buf, "%u\n", priv->autorecording);
607 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
608 vmlogrdr_autorecording_store);
611 static ssize_t vmlogrdr_recording_store(struct device * dev,
612 struct device_attribute *attr,
613 const char * buf, size_t count)
615 struct vmlogrdr_priv_t *priv = dev->driver_data;
620 ret = vmlogrdr_recording(priv,0,0);
623 ret = vmlogrdr_recording(priv,1,0);
636 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
639 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
643 char cp_command[] = "QUERY RECORDING ";
646 cpcmd(cp_command, buf, 4096, NULL);
652 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
655 static struct attribute *vmlogrdr_attrs[] = {
656 &dev_attr_autopurge.attr,
657 &dev_attr_purge.attr,
658 &dev_attr_autorecording.attr,
659 &dev_attr_recording.attr,
663 static struct attribute_group vmlogrdr_attr_group = {
664 .attrs = vmlogrdr_attrs,
667 static struct class *vmlogrdr_class;
668 static struct device_driver vmlogrdr_driver = {
674 static int vmlogrdr_register_driver(void)
678 /* Register with iucv driver */
679 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
683 ret = driver_register(&vmlogrdr_driver);
687 ret = driver_create_file(&vmlogrdr_driver,
688 &driver_attr_recording_status);
692 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
693 if (IS_ERR(vmlogrdr_class)) {
694 ret = PTR_ERR(vmlogrdr_class);
695 vmlogrdr_class = NULL;
701 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
703 driver_unregister(&vmlogrdr_driver);
705 iucv_unregister(&vmlogrdr_iucv_handler, 1);
711 static void vmlogrdr_unregister_driver(void)
713 class_destroy(vmlogrdr_class);
714 vmlogrdr_class = NULL;
715 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
716 driver_unregister(&vmlogrdr_driver);
717 iucv_unregister(&vmlogrdr_iucv_handler, 1);
721 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
726 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
728 dev_set_name(dev, priv->internal_name);
729 dev->bus = &iucv_bus;
730 dev->parent = iucv_root;
731 dev->driver = &vmlogrdr_driver;
733 * The release function could be called after the
734 * module has been unloaded. It's _only_ task is to
735 * free the struct. Therefore, we specify kfree()
736 * directly here. (Probably a little bit obfuscating
739 dev->release = (void (*)(struct device *))kfree;
742 ret = device_register(dev);
746 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
748 device_unregister(dev);
751 priv->class_device = device_create(vmlogrdr_class, dev,
752 MKDEV(vmlogrdr_major,
754 priv, "%s", dev_name(dev));
755 if (IS_ERR(priv->class_device)) {
756 ret = PTR_ERR(priv->class_device);
757 priv->class_device=NULL;
758 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
759 device_unregister(dev);
767 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
769 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
770 if (priv->device != NULL) {
771 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
772 device_unregister(priv->device);
779 static int vmlogrdr_register_cdev(dev_t dev)
782 vmlogrdr_cdev = cdev_alloc();
783 if (!vmlogrdr_cdev) {
786 vmlogrdr_cdev->owner = THIS_MODULE;
787 vmlogrdr_cdev->ops = &vmlogrdr_fops;
788 vmlogrdr_cdev->dev = dev;
789 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
793 // cleanup: cdev is not fully registered, no cdev_del here!
794 kobject_put(&vmlogrdr_cdev->kobj);
800 static void vmlogrdr_cleanup(void)
805 cdev_del(vmlogrdr_cdev);
808 for (i=0; i < MAXMINOR; ++i ) {
809 vmlogrdr_unregister_device(&sys_ser[i]);
810 free_page((unsigned long)sys_ser[i].buffer);
812 vmlogrdr_unregister_driver();
813 if (vmlogrdr_major) {
814 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
820 static int __init vmlogrdr_init(void)
826 if (! MACHINE_IS_VM) {
827 pr_err("not running under VM, driver not loaded.\n");
831 recording_class_AB = vmlogrdr_get_recording_class_AB();
833 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
836 vmlogrdr_major = MAJOR(dev);
838 rc=vmlogrdr_register_driver();
842 for (i=0; i < MAXMINOR; ++i ) {
843 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
844 if (!sys_ser[i].buffer) {
848 sys_ser[i].current_position = sys_ser[i].buffer;
849 rc=vmlogrdr_register_device(&sys_ser[i]);
856 rc = vmlogrdr_register_cdev(dev);
867 static void __exit vmlogrdr_exit(void)
874 module_init(vmlogrdr_init);
875 module_exit(vmlogrdr_exit);