blob: a246bc73ae6430b5761a47ccc90305c7564da9e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/vmlogrdr.c
3 * character device driver for reading z/VM system service records
4 *
5 *
Martin Schwidefskyc9101c52007-02-08 13:40:41 -08006 * Copyright 2004 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080024#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kmod.h>
26#include <linux/cdev.h>
27#include <linux/device.h>
28#include <linux/string.h>
29
30
31
32MODULE_AUTHOR
33 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
34 " Stefan Weinhuber (wein@de.ibm.com)");
35MODULE_DESCRIPTION ("Character device driver for reading z/VM "
36 "system service records.");
37MODULE_LICENSE("GPL");
38
39
40/*
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
45 * one page.
46 */
47#define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
48
49/*
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.
58 */
59struct vmlogrdr_priv_t {
60 char system_service[8];
61 char internal_name[8];
62 char recording_name[8];
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080063 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int connection_established;
65 int iucv_path_severed;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080066 struct iucv_message local_interrupt_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 atomic_t receive_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int minor_num;
69 char * buffer;
70 char * current_position;
71 int remaining;
72 ulong residual_length;
73 int buffer_free;
74 int dev_in_use; /* 1: already opened, 0: not opened*/
75 spinlock_t priv_lock;
76 struct device *device;
Cornelia Huck7f021ce2007-10-22 12:52:42 +020077 struct device *class_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int autorecording;
79 int autopurge;
80};
81
82
83/*
84 * File operation structure for vmlogrdr devices
85 */
86static int vmlogrdr_open(struct inode *, struct file *);
87static int vmlogrdr_release(struct inode *, struct file *);
Heiko Carstensd2c993d2006-07-12 16:41:55 +020088static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
89 size_t count, loff_t * ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080091static const struct file_operations vmlogrdr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .owner = THIS_MODULE,
93 .open = vmlogrdr_open,
94 .release = vmlogrdr_release,
95 .read = vmlogrdr_read,
96};
97
98
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080099static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
100static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
101static void vmlogrdr_iucv_message_pending(struct iucv_path *,
102 struct iucv_message *);
103
104
105static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100112static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
113static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * pointer to system service private structure
117 * minor number 0 --> logrec
118 * minor number 1 --> account
119 * minor number 2 --> symptom
120 */
121
122static struct vmlogrdr_priv_t sys_ser[] = {
123 { .system_service = "*LOGREC ",
124 .internal_name = "logrec",
125 .recording_name = "EREP",
126 .minor_num = 0,
127 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200128 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .autorecording = 1,
130 .autopurge = 1,
131 },
132 { .system_service = "*ACCOUNT",
133 .internal_name = "account",
134 .recording_name = "ACCOUNT",
135 .minor_num = 1,
136 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200137 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .autorecording = 1,
139 .autopurge = 1,
140 },
141 { .system_service = "*SYMPTOM",
142 .internal_name = "symptom",
143 .recording_name = "SYMPTOM",
144 .minor_num = 2,
145 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200146 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .autorecording = 1,
148 .autopurge = 1,
149 }
150};
151
152#define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
153
154static char FENCE[] = {"EOR"};
155static int vmlogrdr_major = 0;
156static struct cdev *vmlogrdr_cdev = NULL;
157static int recording_class_AB;
158
159
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800160static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800162 struct vmlogrdr_priv_t * logptr = path->private;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 spin_lock(&logptr->priv_lock);
165 logptr->connection_established = 1;
166 spin_unlock(&logptr->priv_lock);
167 wake_up(&conn_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800171static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800173 struct vmlogrdr_priv_t * logptr = path->private;
174 u8 reason = (u8) ipuser[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 printk (KERN_ERR "vmlogrdr: connection severed with"
177 " reason %i\n", reason);
178
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800179 iucv_path_sever(path, NULL);
180 kfree(path);
181 logptr->path = NULL;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_lock(&logptr->priv_lock);
184 logptr->connection_established = 0;
185 logptr->iucv_path_severed = 1;
186 spin_unlock(&logptr->priv_lock);
187
188 wake_up(&conn_wait_queue);
189 /* just in case we're sleeping waiting for a record */
190 wake_up_interruptible(&read_wait_queue);
191}
192
193
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800194static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
195 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800197 struct vmlogrdr_priv_t * logptr = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * This function is the bottom half so it should be quick.
201 * Copy the external interrupt data into our local eib and increment
202 * the usage count
203 */
204 spin_lock(&logptr->priv_lock);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800205 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 atomic_inc(&logptr->receive_ready);
207 spin_unlock(&logptr->priv_lock);
208 wake_up_interruptible(&read_wait_queue);
209}
210
211
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800212static int vmlogrdr_get_recording_class_AB(void)
213{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 char cp_command[]="QUERY COMMAND RECORDING ";
215 char cp_response[80];
216 char *tail;
217 int len,i;
218
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700219 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 len = strnlen(cp_response,sizeof(cp_response));
221 // now the parsing
222 tail=strnchr(cp_response,len,'=');
223 if (!tail)
224 return 0;
225 tail++;
226 if (!strncmp("ANY",tail,3))
227 return 1;
228 if (!strncmp("NONE",tail,4))
229 return 0;
230 /*
231 * expect comma separated list of classes here, if one of them
232 * is A or B return 1 otherwise 0
233 */
234 for (i=tail-cp_response; i<len; i++)
235 if ( cp_response[i]=='A' || cp_response[i]=='B' )
236 return 1;
237 return 0;
238}
239
240
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800241static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
242 int action, int purge)
243{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 char cp_command[80];
246 char cp_response[160];
247 char *onoff, *qid_string;
248
249 memset(cp_command, 0x00, sizeof(cp_command));
250 memset(cp_response, 0x00, sizeof(cp_response));
251
252 onoff = ((action == 1) ? "ON" : "OFF");
253 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
254
255 /*
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.
261 */
262
263 if (purge) {
264 snprintf(cp_command, sizeof(cp_command),
265 "RECORDING %s PURGE %s",
266 logptr->recording_name,
267 qid_string);
268
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700269 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
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,
276 onoff,
277 qid_string);
278
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700279 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* 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.
285 */
286 if (strstr(cp_response,"Command complete"))
287 return 0;
288 else
289 return -EIO;
290
291}
292
293
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800294static int vmlogrdr_open (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int dev_num = 0;
297 struct vmlogrdr_priv_t * logptr = NULL;
298 int connect_rc = 0;
299 int ret;
300
301 dev_num = iminor(inode);
302 if (dev_num > MAXMINOR)
303 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 logptr = &sys_ser[dev_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * only allow for blocking reads to be open
308 */
309 if (filp->f_flags & O_NONBLOCK)
310 return -ENOSYS;
311
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);
316 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800318 logptr->dev_in_use = 1;
319 logptr->connection_established = 0;
320 logptr->iucv_path_severed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 atomic_set(&logptr->receive_ready, 0);
322 logptr->buffer_free = 1;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800323 spin_unlock_bh(&logptr->priv_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* set the file options */
326 filp->private_data = logptr;
327 filp->f_op = &vmlogrdr_fops;
328
329 /* start recording for this service*/
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800330 if (logptr->autorecording) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800332 if (ret)
333 printk (KERN_WARNING "vmlogrdr: failed to start "
334 "recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 /* create connection to the system service */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800338 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
339 if (!logptr->path)
340 goto out_dev;
341 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
342 logptr->system_service, NULL, NULL,
343 logptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (connect_rc) {
345 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
346 "failed with rc %i \n", logptr->system_service,
347 connect_rc);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800348 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
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.
354 */
355 wait_event(conn_wait_queue, (logptr->connection_established)
356 || (logptr->iucv_path_severed));
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800357 if (logptr->iucv_path_severed)
358 goto out_record;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return nonseekable_open(inode, filp);
360
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800361out_record:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (logptr->autorecording)
363 vmlogrdr_recording(logptr,0,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800364out_path:
365 kfree(logptr->path); /* kfree(NULL) is ok. */
366 logptr->path = NULL;
367out_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 logptr->dev_in_use = 0;
369 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800373static int vmlogrdr_release (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int ret;
376
377 struct vmlogrdr_priv_t * logptr = filp->private_data;
378
Ursula Braun66b494a2007-04-27 16:01:52 +0200379 iucv_path_sever(logptr->path, NULL);
380 kfree(logptr->path);
381 logptr->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (logptr->autorecording) {
383 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
384 if (ret)
385 printk (KERN_WARNING "vmlogrdr: failed to stop "
386 "recording automatically\n");
387 }
388 logptr->dev_in_use = 0;
389
390 return 0;
391}
392
393
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800394static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int rc, *temp;
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.
400 */
401 int user_data_count, iucv_data_count;
402 char * buffer;
403
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;
409 user_data_count = 0;
410 buffer = priv->buffer;
411 } else {
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.
415 */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800416 iucv_data_count = priv->local_interrupt_buffer.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 user_data_count = sizeof(int);
418 temp = (int*)priv->buffer;
419 *temp= iucv_data_count + sizeof(FENCE);
420 buffer = priv->buffer + sizeof(int);
421 }
422 /*
423 * If the record is bigger then our buffer, we receive only
424 * a part of it. We can get the rest later.
425 */
426 if (iucv_data_count > NET_BUFFER_SIZE)
427 iucv_data_count = NET_BUFFER_SIZE;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800428 rc = iucv_message_receive(priv->path,
429 &priv->local_interrupt_buffer,
430 0, buffer, iucv_data_count,
431 &priv->residual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 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.
436 */
437 if (rc == 5)
438 rc = 0;
439 if (rc == 9)
440 atomic_set(&priv->receive_ready, 0);
441 } else {
442 rc = 1;
443 }
444 if (!rc) {
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);
455 }
456 priv->remaining = user_data_count;
457 }
458
459 return rc;
460}
461
462
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800463static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
464 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 int rc;
467 struct vmlogrdr_priv_t * priv = filp->private_data;
468
469 while (priv->buffer_free) {
470 rc = vmlogrdr_receive_data(priv);
471 if (rc) {
472 rc = wait_event_interruptible(read_wait_queue,
473 atomic_read(&priv->receive_ready));
474 if (rc)
475 return rc;
476 }
477 }
478 /* copy only up to end of record */
479 if (count > priv->remaining)
480 count = priv->remaining;
481
482 if (copy_to_user(data, priv->current_position, count))
483 return -EFAULT;
484
485 *ppos += count;
486 priv->current_position += count;
487 priv->remaining -= count;
488
489 /* if all data has been transferred, set buffer free */
490 if (priv->remaining == 0)
491 priv->buffer_free = 1;
492
493 return count;
494}
495
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800496static ssize_t vmlogrdr_autopurge_store(struct device * dev,
497 struct device_attribute *attr,
498 const char * buf, size_t count)
499{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct vmlogrdr_priv_t *priv = dev->driver_data;
501 ssize_t ret = count;
502
503 switch (buf[0]) {
504 case '0':
505 priv->autopurge=0;
506 break;
507 case '1':
508 priv->autopurge=1;
509 break;
510 default:
511 ret = -EINVAL;
512 }
513 return ret;
514}
515
516
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800517static ssize_t vmlogrdr_autopurge_show(struct device *dev,
518 struct device_attribute *attr,
519 char *buf)
520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct vmlogrdr_priv_t *priv = dev->driver_data;
522 return sprintf(buf, "%u\n", priv->autopurge);
523}
524
525
526static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
527 vmlogrdr_autopurge_store);
528
529
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800530static ssize_t vmlogrdr_purge_store(struct device * dev,
531 struct device_attribute *attr,
532 const char * buf, size_t count)
533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 char cp_command[80];
536 char cp_response[80];
537 struct vmlogrdr_priv_t *priv = dev->driver_data;
538
539 if (buf[0] != '1')
540 return -EINVAL;
541
542 memset(cp_command, 0x00, sizeof(cp_command));
543 memset(cp_response, 0x00, sizeof(cp_response));
544
545 /*
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.
550 */
551
552 if (recording_class_AB)
553 snprintf(cp_command, sizeof(cp_command),
554 "RECORDING %s PURGE QID * ",
555 priv->recording_name);
556 else
557 snprintf(cp_command, sizeof(cp_command),
558 "RECORDING %s PURGE ",
559 priv->recording_name);
560
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700561 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return count;
564}
565
566
567static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
568
569
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800570static ssize_t vmlogrdr_autorecording_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
573{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct vmlogrdr_priv_t *priv = dev->driver_data;
575 ssize_t ret = count;
576
577 switch (buf[0]) {
578 case '0':
579 priv->autorecording=0;
580 break;
581 case '1':
582 priv->autorecording=1;
583 break;
584 default:
585 ret = -EINVAL;
586 }
587 return ret;
588}
589
590
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800591static ssize_t vmlogrdr_autorecording_show(struct device *dev,
592 struct device_attribute *attr,
593 char *buf)
594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct vmlogrdr_priv_t *priv = dev->driver_data;
596 return sprintf(buf, "%u\n", priv->autorecording);
597}
598
599
600static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
601 vmlogrdr_autorecording_store);
602
603
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800604static ssize_t vmlogrdr_recording_store(struct device * dev,
605 struct device_attribute *attr,
606 const char * buf, size_t count)
607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct vmlogrdr_priv_t *priv = dev->driver_data;
609 ssize_t ret;
610
611 switch (buf[0]) {
612 case '0':
613 ret = vmlogrdr_recording(priv,0,0);
614 break;
615 case '1':
616 ret = vmlogrdr_recording(priv,1,0);
617 break;
618 default:
619 ret = -EINVAL;
620 }
621 if (ret)
622 return ret;
623 else
624 return count;
625
626}
627
628
629static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
630
631
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800632static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
633 char *buf)
634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 char cp_command[] = "QUERY RECORDING ";
637 int len;
638
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700639 cpcmd(cp_command, buf, 4096, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 len = strlen(buf);
641 return len;
642}
643
644
645static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
646 NULL);
647
648static struct attribute *vmlogrdr_attrs[] = {
649 &dev_attr_autopurge.attr,
650 &dev_attr_purge.attr,
651 &dev_attr_autorecording.attr,
652 &dev_attr_recording.attr,
653 NULL,
654};
655
656static struct attribute_group vmlogrdr_attr_group = {
657 .attrs = vmlogrdr_attrs,
658};
659
gregkh@suse.de56b22932005-03-23 10:01:41 -0800660static struct class *vmlogrdr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static struct device_driver vmlogrdr_driver = {
662 .name = "vmlogrdr",
663 .bus = &iucv_bus,
664};
665
666
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800667static int vmlogrdr_register_driver(void)
668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int ret;
670
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800671 /* Register with iucv driver */
672 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200673 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800674 goto out;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ret = driver_register(&vmlogrdr_driver);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200677 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800678 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ret = driver_create_file(&vmlogrdr_driver,
681 &driver_attr_recording_status);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200682 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800683 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
gregkh@suse.de56b22932005-03-23 10:01:41 -0800685 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (IS_ERR(vmlogrdr_class)) {
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800687 ret = PTR_ERR(vmlogrdr_class);
688 vmlogrdr_class = NULL;
689 goto out_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 return 0;
692
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800693out_attr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800695out_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800697out_iucv:
698 iucv_unregister(&vmlogrdr_iucv_handler, 1);
699out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return ret;
701}
702
703
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800704static void vmlogrdr_unregister_driver(void)
705{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800706 class_destroy(vmlogrdr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 vmlogrdr_class = NULL;
708 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
709 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800710 iucv_unregister(&vmlogrdr_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800714static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
715{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct device *dev;
717 int ret;
718
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800719 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 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;
726 /*
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
731 * but legitime ...).
732 */
733 dev->release = (void (*)(struct device *))kfree;
734 } else
735 return -ENOMEM;
736 ret = device_register(dev);
737 if (ret)
738 return ret;
739
740 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
741 if (ret) {
742 device_unregister(dev);
743 return ret;
744 }
Greg Kroah-Hartmanc5fb9202008-05-16 17:55:12 -0700745 priv->class_device = device_create_drvdata(vmlogrdr_class, dev,
746 MKDEV(vmlogrdr_major,
747 priv->minor_num),
748 priv, "%s", dev->bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 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);
754 return ret;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 priv->device = dev;
757 return 0;
758}
759
760
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800761static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
762{
Cornelia Huck7f021ce2007-10-22 12:52:42 +0200763 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (priv->device != NULL) {
765 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
766 device_unregister(priv->device);
767 priv->device=NULL;
768 }
769 return 0;
770}
771
772
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800773static int vmlogrdr_register_cdev(dev_t dev)
774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int rc = 0;
776 vmlogrdr_cdev = cdev_alloc();
777 if (!vmlogrdr_cdev) {
778 return -ENOMEM;
779 }
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);
784 if (!rc)
785 return 0;
786
787 // cleanup: cdev is not fully registered, no cdev_del here!
788 kobject_put(&vmlogrdr_cdev->kobj);
789 vmlogrdr_cdev=NULL;
790 return rc;
791}
792
793
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800794static void vmlogrdr_cleanup(void)
795{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int i;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (vmlogrdr_cdev) {
799 cdev_del(vmlogrdr_cdev);
800 vmlogrdr_cdev=NULL;
801 }
802 for (i=0; i < MAXMINOR; ++i ) {
803 vmlogrdr_unregister_device(&sys_ser[i]);
804 free_page((unsigned long)sys_ser[i].buffer);
805 }
806 vmlogrdr_unregister_driver();
807 if (vmlogrdr_major) {
808 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
809 vmlogrdr_major=0;
810 }
811}
812
813
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200814static int __init vmlogrdr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int rc;
817 int i;
818 dev_t dev;
819
820 if (! MACHINE_IS_VM) {
821 printk (KERN_ERR "vmlogrdr: not running under VM, "
822 "driver not loaded.\n");
823 return -ENODEV;
824 }
825
826 recording_class_AB = vmlogrdr_get_recording_class_AB();
827
828 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
829 if (rc)
830 return rc;
831 vmlogrdr_major = MAJOR(dev);
832
833 rc=vmlogrdr_register_driver();
834 if (rc)
835 goto cleanup;
836
837 for (i=0; i < MAXMINOR; ++i ) {
838 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
839 if (!sys_ser[i].buffer) {
Marcin Slusarz3cb2cea2008-05-15 16:52:32 +0200840 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842 }
843 sys_ser[i].current_position = sys_ser[i].buffer;
844 rc=vmlogrdr_register_device(&sys_ser[i]);
845 if (rc)
846 break;
847 }
848 if (rc)
849 goto cleanup;
850
851 rc = vmlogrdr_register_cdev(dev);
852 if (rc)
853 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
855
856cleanup:
857 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return rc;
859}
860
861
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200862static void __exit vmlogrdr_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return;
866}
867
868
869module_init(vmlogrdr_init);
870module_exit(vmlogrdr_exit);