]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/s390/char/vmlogrdr.c
drivers/s390: cdev lock_kernel() pushdown
[linux-3.10.git] / drivers / s390 / char / vmlogrdr.c
1 /*
2  * drivers/s390/char/vmlogrdr.c
3  *      character device driver for reading z/VM system service records
4  *
5  *
6  *      Copyright 2004 IBM Corporation
7  *      character device driver for reading z/VM system service records,
8  *      Version 1.0
9  *      Author(s): Xenia Tkatschow <xenia@us.ibm.com>
10  *                 Stefan Weinhuber <wein@de.ibm.com>
11  *
12  */
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>
30
31
32
33 MODULE_AUTHOR
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");
39
40
41 /*
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
46  * one page.
47  */
48 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
49
50 /*
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.
59  */
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;
69         int minor_num;
70         char * buffer;
71         char * current_position;
72         int remaining;
73         ulong residual_length;
74         int buffer_free;
75         int dev_in_use; /* 1: already opened, 0: not opened*/
76         spinlock_t priv_lock;
77         struct device  *device;
78         struct device  *class_device;
79         int autorecording;
80         int autopurge;
81 };
82
83
84 /*
85  * File operation structure for vmlogrdr devices
86  */
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);
91
92 static const struct file_operations vmlogrdr_fops = {
93         .owner   = THIS_MODULE,
94         .open    = vmlogrdr_open,
95         .release = vmlogrdr_release,
96         .read    = vmlogrdr_read,
97 };
98
99
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 *);
104
105
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,
110 };
111
112
113 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
114 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
115
116 /*
117  * pointer to system service private structure
118  * minor number 0 --> logrec
119  * minor number 1 --> account
120  * minor number 2 --> symptom
121  */
122
123 static struct vmlogrdr_priv_t sys_ser[] = {
124         { .system_service = "*LOGREC ",
125           .internal_name  = "logrec",
126           .recording_name = "EREP",
127           .minor_num      = 0,
128           .buffer_free    = 1,
129           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
130           .autorecording  = 1,
131           .autopurge      = 1,
132         },
133         { .system_service = "*ACCOUNT",
134           .internal_name  = "account",
135           .recording_name = "ACCOUNT",
136           .minor_num      = 1,
137           .buffer_free    = 1,
138           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
139           .autorecording  = 1,
140           .autopurge      = 1,
141         },
142         { .system_service = "*SYMPTOM",
143           .internal_name  = "symptom",
144           .recording_name = "SYMPTOM",
145           .minor_num      = 2,
146           .buffer_free    = 1,
147           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
148           .autorecording  = 1,
149           .autopurge      = 1,
150         }
151 };
152
153 #define MAXMINOR  (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
154
155 static char FENCE[] = {"EOR"};
156 static int vmlogrdr_major = 0;
157 static struct cdev  *vmlogrdr_cdev = NULL;
158 static int recording_class_AB;
159
160
161 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
162 {
163         struct vmlogrdr_priv_t * logptr = path->private;
164
165         spin_lock(&logptr->priv_lock);
166         logptr->connection_established = 1;
167         spin_unlock(&logptr->priv_lock);
168         wake_up(&conn_wait_queue);
169 }
170
171
172 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
173 {
174         struct vmlogrdr_priv_t * logptr = path->private;
175         u8 reason = (u8) ipuser[8];
176
177         printk (KERN_ERR "vmlogrdr: connection severed with"
178                 " reason %i\n", reason);
179
180         iucv_path_sever(path, NULL);
181         kfree(path);
182         logptr->path = NULL;
183
184         spin_lock(&logptr->priv_lock);
185         logptr->connection_established = 0;
186         logptr->iucv_path_severed = 1;
187         spin_unlock(&logptr->priv_lock);
188
189         wake_up(&conn_wait_queue);
190         /* just in case we're sleeping waiting for a record */
191         wake_up_interruptible(&read_wait_queue);
192 }
193
194
195 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
196                                           struct iucv_message *msg)
197 {
198         struct vmlogrdr_priv_t * logptr = path->private;
199
200         /*
201          * This function is the bottom half so it should be quick.
202          * Copy the external interrupt data into our local eib and increment
203          * the usage count
204          */
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);
210 }
211
212
213 static int vmlogrdr_get_recording_class_AB(void)
214 {
215         char cp_command[]="QUERY COMMAND RECORDING ";
216         char cp_response[80];
217         char *tail;
218         int len,i;
219
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));
224         // now the parsing
225         tail=strnchr(cp_response,len,'=');
226         if (!tail)
227                 return 0;
228         tail++;
229         if (!strncmp("ANY",tail,3))
230                 return 1;
231         if (!strncmp("NONE",tail,4))
232                 return 0;
233         /*
234          * expect comma separated list of classes here, if one of them
235          * is A or B return 1 otherwise 0
236          */
237         for (i=tail-cp_response; i<len; i++)
238                 if ( cp_response[i]=='A' || cp_response[i]=='B' )
239                         return 1;
240         return 0;
241 }
242
243
244 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
245                               int action, int purge)
246 {
247
248         char cp_command[80];
249         char cp_response[160];
250         char *onoff, *qid_string;
251
252         memset(cp_command, 0x00, sizeof(cp_command));
253         memset(cp_response, 0x00, sizeof(cp_response));
254
255         onoff = ((action == 1) ? "ON" : "OFF");
256         qid_string = ((recording_class_AB == 1) ? " QID * " : "");
257
258         /*
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.
264          */
265
266         if (purge) {
267                 snprintf(cp_command, sizeof(cp_command),
268                          "RECORDING %s PURGE %s",
269                          logptr->recording_name,
270                          qid_string);
271
272                 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n",
273                         cp_command);
274                 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
275                 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
276                         cp_response);
277         }
278
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,
283                 onoff,
284                 qid_string);
285
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",
289                 cp_response);
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.
295          */
296         if (strstr(cp_response,"Command complete"))
297                 return 0;
298         else
299                 return -EIO;
300
301 }
302
303
304 static int vmlogrdr_open (struct inode *inode, struct file *filp)
305 {
306         int dev_num = 0;
307         struct vmlogrdr_priv_t * logptr = NULL;
308         int connect_rc = 0;
309         int ret;
310
311         dev_num = iminor(inode);
312         if (dev_num > MAXMINOR)
313                 return -ENODEV;
314         logptr = &sys_ser[dev_num];
315
316         /*
317          * only allow for blocking reads to be open
318          */
319         if (filp->f_flags & O_NONBLOCK)
320                 return -ENOSYS;
321
322         /* Besure this device hasn't already been opened */
323         lock_kernel();
324         spin_lock_bh(&logptr->priv_lock);
325         if (logptr->dev_in_use) {
326                 spin_unlock_bh(&logptr->priv_lock);
327                 unlock_kernel();
328                 return -EBUSY;
329         }
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);
336
337         /* set the file options */
338         filp->private_data = logptr;
339         filp->f_op = &vmlogrdr_fops;
340
341         /* start recording for this service*/
342         if (logptr->autorecording) {
343                 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
344                 if (ret)
345                         printk (KERN_WARNING "vmlogrdr: failed to start "
346                                 "recording automatically\n");
347         }
348
349         /* create connection to the system service */
350         logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
351         if (!logptr->path)
352                 goto out_dev;
353         connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
354                                        logptr->system_service, NULL, NULL,
355                                        logptr);
356         if (connect_rc) {
357                 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
358                         "failed with rc %i \n", logptr->system_service,
359                         connect_rc);
360                 goto out_path;
361         }
362
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.
366          */
367         wait_event(conn_wait_queue, (logptr->connection_established)
368                    || (logptr->iucv_path_severed));
369         if (logptr->iucv_path_severed)
370                 goto out_record;
371         ret = nonseekable_open(inode, filp);
372         unlock_kernel();
373         return ret;
374
375 out_record:
376         if (logptr->autorecording)
377                 vmlogrdr_recording(logptr,0,logptr->autopurge);
378 out_path:
379         kfree(logptr->path);    /* kfree(NULL) is ok. */
380         logptr->path = NULL;
381 out_dev:
382         logptr->dev_in_use = 0;
383         unlock_kernel();
384         return -EIO;
385 }
386
387
388 static int vmlogrdr_release (struct inode *inode, struct file *filp)
389 {
390         int ret;
391
392         struct vmlogrdr_priv_t * logptr = filp->private_data;
393
394         iucv_path_sever(logptr->path, NULL);
395         kfree(logptr->path);
396         logptr->path = NULL;
397         if (logptr->autorecording) {
398                 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
399                 if (ret)
400                         printk (KERN_WARNING "vmlogrdr: failed to stop "
401                                 "recording automatically\n");
402         }
403         logptr->dev_in_use = 0;
404
405         return 0;
406 }
407
408
409 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
410 {
411         int rc, *temp;
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.
415          */
416         int user_data_count, iucv_data_count;
417         char * buffer;
418
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;
424                         user_data_count = 0;
425                         buffer = priv->buffer;
426                 } else {
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.
430                          */
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);
436                 }
437                 /*
438                  * If the record is bigger then our buffer, we receive only
439                  * a part of it. We can get the rest later.
440                  */
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.
451                  */
452                 if (rc == 5)
453                         rc = 0;
454                 if (rc == 9)
455                         atomic_set(&priv->receive_ready, 0);
456         } else {
457                 rc = 1;
458         }
459         if (!rc) {
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);
470                 }
471                 priv->remaining = user_data_count;
472         }
473
474         return rc;
475 }
476
477
478 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
479                              size_t count, loff_t * ppos)
480 {
481         int rc;
482         struct vmlogrdr_priv_t * priv = filp->private_data;
483
484         while (priv->buffer_free) {
485                 rc = vmlogrdr_receive_data(priv);
486                 if (rc) {
487                         rc = wait_event_interruptible(read_wait_queue,
488                                         atomic_read(&priv->receive_ready));
489                         if (rc)
490                                 return rc;
491                 }
492         }
493         /* copy only up to end of record */
494         if (count > priv->remaining)
495                 count = priv->remaining;
496
497         if (copy_to_user(data, priv->current_position, count))
498                 return -EFAULT;
499
500         *ppos += count;
501         priv->current_position += count;
502         priv->remaining -= count;
503
504         /* if all data has been transferred, set buffer free */
505         if (priv->remaining == 0)
506                 priv->buffer_free = 1;
507
508         return count;
509 }
510
511 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
512                                         struct device_attribute *attr,
513                                         const char * buf, size_t count)
514 {
515         struct vmlogrdr_priv_t *priv = dev->driver_data;
516         ssize_t ret = count;
517
518         switch (buf[0]) {
519         case '0':
520                 priv->autopurge=0;
521                 break;
522         case '1':
523                 priv->autopurge=1;
524                 break;
525         default:
526                 ret = -EINVAL;
527         }
528         return ret;
529 }
530
531
532 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
533                                        struct device_attribute *attr,
534                                        char *buf)
535 {
536         struct vmlogrdr_priv_t *priv = dev->driver_data;
537         return sprintf(buf, "%u\n", priv->autopurge);
538 }
539
540
541 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
542                    vmlogrdr_autopurge_store);
543
544
545 static ssize_t vmlogrdr_purge_store(struct device * dev,
546                                     struct device_attribute *attr,
547                                     const char * buf, size_t count)
548 {
549
550         char cp_command[80];
551         char cp_response[80];
552         struct vmlogrdr_priv_t *priv = dev->driver_data;
553
554         if (buf[0] != '1')
555                 return -EINVAL;
556
557         memset(cp_command, 0x00, sizeof(cp_command));
558         memset(cp_response, 0x00, sizeof(cp_response));
559
560         /*
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.
565          */
566
567         if (recording_class_AB)
568                 snprintf(cp_command, sizeof(cp_command),
569                          "RECORDING %s PURGE QID * ",
570                          priv->recording_name);
571         else
572                 snprintf(cp_command, sizeof(cp_command),
573                          "RECORDING %s PURGE ",
574                          priv->recording_name);
575
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",
579                 cp_response);
580
581         return count;
582 }
583
584
585 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
586
587
588 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
589                                             struct device_attribute *attr,
590                                             const char *buf, size_t count)
591 {
592         struct vmlogrdr_priv_t *priv = dev->driver_data;
593         ssize_t ret = count;
594
595         switch (buf[0]) {
596         case '0':
597                 priv->autorecording=0;
598                 break;
599         case '1':
600                 priv->autorecording=1;
601                 break;
602         default:
603                 ret = -EINVAL;
604         }
605         return ret;
606 }
607
608
609 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
610                                            struct device_attribute *attr,
611                                            char *buf)
612 {
613         struct vmlogrdr_priv_t *priv = dev->driver_data;
614         return sprintf(buf, "%u\n", priv->autorecording);
615 }
616
617
618 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
619                    vmlogrdr_autorecording_store);
620
621
622 static ssize_t vmlogrdr_recording_store(struct device * dev,
623                                         struct device_attribute *attr,
624                                         const char * buf, size_t count)
625 {
626         struct vmlogrdr_priv_t *priv = dev->driver_data;
627         ssize_t ret;
628
629         switch (buf[0]) {
630         case '0':
631                 ret = vmlogrdr_recording(priv,0,0);
632                 break;
633         case '1':
634                 ret = vmlogrdr_recording(priv,1,0);
635                 break;
636         default:
637                 ret = -EINVAL;
638         }
639         if (ret)
640                 return ret;
641         else
642                 return count;
643
644 }
645
646
647 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
648
649
650 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
651                                               char *buf)
652 {
653
654         char cp_command[] = "QUERY RECORDING ";
655         int len;
656
657         cpcmd(cp_command, buf, 4096, NULL);
658         len = strlen(buf);
659         return len;
660 }
661
662
663 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
664                    NULL);
665
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,
671         NULL,
672 };
673
674 static struct attribute_group vmlogrdr_attr_group = {
675         .attrs = vmlogrdr_attrs,
676 };
677
678 static struct class *vmlogrdr_class;
679 static struct device_driver vmlogrdr_driver = {
680         .name = "vmlogrdr",
681         .bus  = &iucv_bus,
682 };
683
684
685 static int vmlogrdr_register_driver(void)
686 {
687         int ret;
688
689         /* Register with iucv driver */
690         ret = iucv_register(&vmlogrdr_iucv_handler, 1);
691         if (ret) {
692                 printk (KERN_ERR "vmlogrdr: failed to register with "
693                         "iucv driver\n");
694                 goto out;
695         }
696
697         ret = driver_register(&vmlogrdr_driver);
698         if (ret) {
699                 printk(KERN_ERR "vmlogrdr: failed to register driver.\n");
700                 goto out_iucv;
701         }
702
703         ret = driver_create_file(&vmlogrdr_driver,
704                                  &driver_attr_recording_status);
705         if (ret) {
706                 printk(KERN_ERR "vmlogrdr: failed to add driver attribute.\n");
707                 goto out_driver;
708         }
709
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;
715                 goto out_attr;
716         }
717         return 0;
718
719 out_attr:
720         driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
721 out_driver:
722         driver_unregister(&vmlogrdr_driver);
723 out_iucv:
724         iucv_unregister(&vmlogrdr_iucv_handler, 1);
725 out:
726         return ret;
727 }
728
729
730 static void vmlogrdr_unregister_driver(void)
731 {
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);
737 }
738
739
740 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
741 {
742         struct device *dev;
743         int ret;
744
745         dev = kzalloc(sizeof(struct device), GFP_KERNEL);
746         if (dev) {
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;
752                 /*
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
757                  * but legitime ...).
758                  */
759                 dev->release = (void (*)(struct device *))kfree;
760         } else
761                 return -ENOMEM;
762         ret = device_register(dev);
763         if (ret)
764                 return ret;
765
766         ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
767         if (ret) {
768                 device_unregister(dev);
769                 return ret;
770         }
771         priv->class_device = device_create(vmlogrdr_class, dev,
772                                            MKDEV(vmlogrdr_major,
773                                                  priv->minor_num),
774                                            "%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);
780                 return ret;
781         }
782         dev->driver_data = priv;
783         priv->device = dev;
784         return 0;
785 }
786
787
788 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
789 {
790         device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
791         if (priv->device != NULL) {
792                 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
793                 device_unregister(priv->device);
794                 priv->device=NULL;
795         }
796         return 0;
797 }
798
799
800 static int vmlogrdr_register_cdev(dev_t dev)
801 {
802         int rc = 0;
803         vmlogrdr_cdev = cdev_alloc();
804         if (!vmlogrdr_cdev) {
805                 return -ENOMEM;
806         }
807         vmlogrdr_cdev->owner = THIS_MODULE;
808         vmlogrdr_cdev->ops = &vmlogrdr_fops;
809         vmlogrdr_cdev->dev = dev;
810         rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
811         if (!rc)
812                 return 0;
813
814         // cleanup: cdev is not fully registered, no cdev_del here!
815         kobject_put(&vmlogrdr_cdev->kobj);
816         vmlogrdr_cdev=NULL;
817         return rc;
818 }
819
820
821 static void vmlogrdr_cleanup(void)
822 {
823         int i;
824
825         if (vmlogrdr_cdev) {
826                 cdev_del(vmlogrdr_cdev);
827                 vmlogrdr_cdev=NULL;
828         }
829         for (i=0; i < MAXMINOR; ++i ) {
830                 vmlogrdr_unregister_device(&sys_ser[i]);
831                 free_page((unsigned long)sys_ser[i].buffer);
832         }
833         vmlogrdr_unregister_driver();
834         if (vmlogrdr_major) {
835                 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
836                 vmlogrdr_major=0;
837         }
838 }
839
840
841 static int __init vmlogrdr_init(void)
842 {
843         int rc;
844         int i;
845         dev_t dev;
846
847         if (! MACHINE_IS_VM) {
848                 printk (KERN_ERR "vmlogrdr: not running under VM, "
849                                 "driver not loaded.\n");
850                 return -ENODEV;
851         }
852
853         recording_class_AB = vmlogrdr_get_recording_class_AB();
854
855         rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
856         if (rc)
857                 return rc;
858         vmlogrdr_major = MAJOR(dev);
859
860         rc=vmlogrdr_register_driver();
861         if (rc)
862                 goto cleanup;
863
864         for (i=0; i < MAXMINOR; ++i ) {
865                 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
866                 if (!sys_ser[i].buffer) {
867                         rc = -ENOMEM;
868                         break;
869                 }
870                 sys_ser[i].current_position = sys_ser[i].buffer;
871                 rc=vmlogrdr_register_device(&sys_ser[i]);
872                 if (rc)
873                         break;
874         }
875         if (rc)
876                 goto cleanup;
877
878         rc = vmlogrdr_register_cdev(dev);
879         if (rc)
880                 goto cleanup;
881         printk (KERN_INFO "vmlogrdr: driver loaded\n");
882         return 0;
883
884 cleanup:
885         vmlogrdr_cleanup();
886         printk (KERN_ERR "vmlogrdr: driver not loaded.\n");
887         return rc;
888 }
889
890
891 static void __exit vmlogrdr_exit(void)
892 {
893         vmlogrdr_cleanup();
894         printk (KERN_INFO "vmlogrdr: driver unloaded\n");
895         return;
896 }
897
898
899 module_init(vmlogrdr_init);
900 module_exit(vmlogrdr_exit);