blob: e9d70aedbaa2c8a7d45200627c2a70667d1074ac [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 *
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +02006 * Copyright IBM Corp. 2004, 2009
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 */
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +010013
14#define KMSG_COMPONENT "vmlogrdr"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/interrupt.h>
23#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/cpcmd.h>
27#include <asm/debug.h>
28#include <asm/ebcdic.h>
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080029#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/kmod.h>
31#include <linux/cdev.h>
32#include <linux/device.h>
33#include <linux/string.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_AUTHOR
36 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
37 " Stefan Weinhuber (wein@de.ibm.com)");
38MODULE_DESCRIPTION ("Character device driver for reading z/VM "
39 "system service records.");
40MODULE_LICENSE("GPL");
41
42
43/*
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
48 * one page.
49 */
50#define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
51
52/*
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.
61 */
62struct vmlogrdr_priv_t {
63 char system_service[8];
64 char internal_name[8];
65 char recording_name[8];
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080066 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int connection_established;
68 int iucv_path_severed;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080069 struct iucv_message local_interrupt_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 atomic_t receive_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int minor_num;
72 char * buffer;
73 char * current_position;
74 int remaining;
75 ulong residual_length;
76 int buffer_free;
77 int dev_in_use; /* 1: already opened, 0: not opened*/
78 spinlock_t priv_lock;
79 struct device *device;
Cornelia Huck7f021ce2007-10-22 12:52:42 +020080 struct device *class_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int autorecording;
82 int autopurge;
83};
84
85
86/*
87 * File operation structure for vmlogrdr devices
88 */
89static int vmlogrdr_open(struct inode *, struct file *);
90static int vmlogrdr_release(struct inode *, struct file *);
Heiko Carstensd2c993d2006-07-12 16:41:55 +020091static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
92 size_t count, loff_t * ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080094static const struct file_operations vmlogrdr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .owner = THIS_MODULE,
96 .open = vmlogrdr_open,
97 .release = vmlogrdr_release,
98 .read = vmlogrdr_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +020099 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800103static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
104static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
105static void vmlogrdr_iucv_message_pending(struct iucv_path *,
106 struct iucv_message *);
107
108
109static struct iucv_handler vmlogrdr_iucv_handler = {
110 .path_complete = vmlogrdr_iucv_path_complete,
111 .path_severed = vmlogrdr_iucv_path_severed,
112 .message_pending = vmlogrdr_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100116static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
117static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/*
120 * pointer to system service private structure
121 * minor number 0 --> logrec
122 * minor number 1 --> account
123 * minor number 2 --> symptom
124 */
125
126static struct vmlogrdr_priv_t sys_ser[] = {
127 { .system_service = "*LOGREC ",
128 .internal_name = "logrec",
129 .recording_name = "EREP",
130 .minor_num = 0,
131 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200132 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .autorecording = 1,
134 .autopurge = 1,
135 },
136 { .system_service = "*ACCOUNT",
137 .internal_name = "account",
138 .recording_name = "ACCOUNT",
139 .minor_num = 1,
140 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200141 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .autorecording = 1,
143 .autopurge = 1,
144 },
145 { .system_service = "*SYMPTOM",
146 .internal_name = "symptom",
147 .recording_name = "SYMPTOM",
148 .minor_num = 2,
149 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200150 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .autorecording = 1,
152 .autopurge = 1,
153 }
154};
155
156#define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
157
158static char FENCE[] = {"EOR"};
159static int vmlogrdr_major = 0;
160static struct cdev *vmlogrdr_cdev = NULL;
161static int recording_class_AB;
162
163
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800164static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800166 struct vmlogrdr_priv_t * logptr = path->private;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 spin_lock(&logptr->priv_lock);
169 logptr->connection_established = 1;
170 spin_unlock(&logptr->priv_lock);
171 wake_up(&conn_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800175static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800177 struct vmlogrdr_priv_t * logptr = path->private;
178 u8 reason = (u8) ipuser[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100180 pr_err("vmlogrdr: connection severed with reason %i\n", reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800182 iucv_path_sever(path, NULL);
183 kfree(path);
184 logptr->path = NULL;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_lock(&logptr->priv_lock);
187 logptr->connection_established = 0;
188 logptr->iucv_path_severed = 1;
189 spin_unlock(&logptr->priv_lock);
190
191 wake_up(&conn_wait_queue);
192 /* just in case we're sleeping waiting for a record */
193 wake_up_interruptible(&read_wait_queue);
194}
195
196
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800197static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
198 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800200 struct vmlogrdr_priv_t * logptr = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /*
203 * This function is the bottom half so it should be quick.
204 * Copy the external interrupt data into our local eib and increment
205 * the usage count
206 */
207 spin_lock(&logptr->priv_lock);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800208 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 atomic_inc(&logptr->receive_ready);
210 spin_unlock(&logptr->priv_lock);
211 wake_up_interruptible(&read_wait_queue);
212}
213
214
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800215static int vmlogrdr_get_recording_class_AB(void)
216{
Joe Perchesbf2106a2010-10-25 16:10:20 +0200217 static const char cp_command[] = "QUERY COMMAND RECORDING ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 char cp_response[80];
219 char *tail;
220 int len,i;
221
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700222 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 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
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800244static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
245 int action, int purge)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 char cp_command[80];
249 char cp_response[160];
250 char *onoff, *qid_string;
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100251 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100253 onoff = ((action == 1) ? "ON" : "OFF");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
255
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100256 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * The recording commands needs to be called with option QID
258 * for guests that have previlege classes A or B.
259 * Purging has to be done as separate step, because recording
260 * can't be switched on as long as records are on the queue.
261 * Doing both at the same time doesn't work.
262 */
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100263 if (purge && (action == 1)) {
264 memset(cp_command, 0x00, sizeof(cp_command));
265 memset(cp_response, 0x00, sizeof(cp_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 snprintf(cp_command, sizeof(cp_command),
267 "RECORDING %s PURGE %s",
268 logptr->recording_name,
269 qid_string);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700270 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 memset(cp_command, 0x00, sizeof(cp_command));
274 memset(cp_response, 0x00, sizeof(cp_response));
275 snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
276 logptr->recording_name,
277 onoff,
278 qid_string);
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
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100284 * 'Command complete'. So I use strstr rather then the strncmp.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286 if (strstr(cp_response,"Command complete"))
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100287 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 else
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100289 rc = -EIO;
290 /*
291 * If we turn recording off, we have to purge any remaining records
292 * afterwards, as a large number of queued records may impact z/VM
293 * performance.
294 */
295 if (purge && (action == 0)) {
296 memset(cp_command, 0x00, sizeof(cp_command));
297 memset(cp_response, 0x00, sizeof(cp_response));
298 snprintf(cp_command, sizeof(cp_command),
299 "RECORDING %s PURGE %s",
300 logptr->recording_name,
301 qid_string);
302 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100305 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800309static int vmlogrdr_open (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int dev_num = 0;
312 struct vmlogrdr_priv_t * logptr = NULL;
313 int connect_rc = 0;
314 int ret;
315
316 dev_num = iminor(inode);
317 if (dev_num > MAXMINOR)
318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 logptr = &sys_ser[dev_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /*
322 * only allow for blocking reads to be open
323 */
324 if (filp->f_flags & O_NONBLOCK)
325 return -ENOSYS;
326
327 /* Besure this device hasn't already been opened */
328 spin_lock_bh(&logptr->priv_lock);
329 if (logptr->dev_in_use) {
330 spin_unlock_bh(&logptr->priv_lock);
331 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800333 logptr->dev_in_use = 1;
334 logptr->connection_established = 0;
335 logptr->iucv_path_severed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 atomic_set(&logptr->receive_ready, 0);
337 logptr->buffer_free = 1;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800338 spin_unlock_bh(&logptr->priv_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* set the file options */
341 filp->private_data = logptr;
342 filp->f_op = &vmlogrdr_fops;
343
344 /* start recording for this service*/
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800345 if (logptr->autorecording) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800347 if (ret)
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100348 pr_warning("vmlogrdr: failed to start "
349 "recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 /* create connection to the system service */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800353 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
354 if (!logptr->path)
355 goto out_dev;
356 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
357 logptr->system_service, NULL, NULL,
358 logptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (connect_rc) {
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100360 pr_err("vmlogrdr: iucv connection to %s "
361 "failed with rc %i \n",
362 logptr->system_service, connect_rc);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800363 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 /* We've issued the connect and now we must wait for a
367 * ConnectionComplete or ConnectinSevered Interrupt
368 * before we can continue to process.
369 */
370 wait_event(conn_wait_queue, (logptr->connection_established)
371 || (logptr->iucv_path_severed));
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800372 if (logptr->iucv_path_severed)
373 goto out_record;
Martin Schwidefsky3b47f9d2009-12-07 12:52:23 +0100374 nonseekable_open(inode, filp);
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800377out_record:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (logptr->autorecording)
379 vmlogrdr_recording(logptr,0,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800380out_path:
381 kfree(logptr->path); /* kfree(NULL) is ok. */
382 logptr->path = NULL;
383out_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 logptr->dev_in_use = 0;
385 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800389static int vmlogrdr_release (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int ret;
392
393 struct vmlogrdr_priv_t * logptr = filp->private_data;
394
Ursula Braun66b494a2007-04-27 16:01:52 +0200395 iucv_path_sever(logptr->path, NULL);
396 kfree(logptr->path);
397 logptr->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (logptr->autorecording) {
399 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
400 if (ret)
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100401 pr_warning("vmlogrdr: failed to stop "
402 "recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 logptr->dev_in_use = 0;
405
406 return 0;
407}
408
409
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800410static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
411{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int rc, *temp;
413 /* we need to keep track of two data sizes here:
414 * The number of bytes we need to receive from iucv and
415 * the total number of bytes we actually write into the buffer.
416 */
417 int user_data_count, iucv_data_count;
418 char * buffer;
419
420 if (atomic_read(&priv->receive_ready)) {
421 spin_lock_bh(&priv->priv_lock);
422 if (priv->residual_length){
423 /* receive second half of a record */
424 iucv_data_count = priv->residual_length;
425 user_data_count = 0;
426 buffer = priv->buffer;
427 } else {
428 /* receive a new record:
429 * We need to return the total length of the record
430 * + size of FENCE in the first 4 bytes of the buffer.
431 */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800432 iucv_data_count = priv->local_interrupt_buffer.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 user_data_count = sizeof(int);
434 temp = (int*)priv->buffer;
435 *temp= iucv_data_count + sizeof(FENCE);
436 buffer = priv->buffer + sizeof(int);
437 }
438 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200439 * If the record is bigger than our buffer, we receive only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * a part of it. We can get the rest later.
441 */
442 if (iucv_data_count > NET_BUFFER_SIZE)
443 iucv_data_count = NET_BUFFER_SIZE;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800444 rc = iucv_message_receive(priv->path,
445 &priv->local_interrupt_buffer,
446 0, buffer, iucv_data_count,
447 &priv->residual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_unlock_bh(&priv->priv_lock);
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200449 /* An rc of 5 indicates that the record was bigger than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * the buffer, which is OK for us. A 9 indicates that the
451 * record was purged befor we could receive it.
452 */
453 if (rc == 5)
454 rc = 0;
455 if (rc == 9)
456 atomic_set(&priv->receive_ready, 0);
457 } else {
458 rc = 1;
459 }
460 if (!rc) {
461 priv->buffer_free = 0;
462 user_data_count += iucv_data_count;
463 priv->current_position = priv->buffer;
464 if (priv->residual_length == 0){
465 /* the whole record has been captured,
466 * now add the fence */
467 atomic_dec(&priv->receive_ready);
468 buffer = priv->buffer + user_data_count;
469 memcpy(buffer, FENCE, sizeof(FENCE));
470 user_data_count += sizeof(FENCE);
471 }
472 priv->remaining = user_data_count;
473 }
474
475 return rc;
476}
477
478
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800479static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
480 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int rc;
483 struct vmlogrdr_priv_t * priv = filp->private_data;
484
485 while (priv->buffer_free) {
486 rc = vmlogrdr_receive_data(priv);
487 if (rc) {
488 rc = wait_event_interruptible(read_wait_queue,
489 atomic_read(&priv->receive_ready));
490 if (rc)
491 return rc;
492 }
493 }
494 /* copy only up to end of record */
495 if (count > priv->remaining)
496 count = priv->remaining;
497
498 if (copy_to_user(data, priv->current_position, count))
499 return -EFAULT;
500
501 *ppos += count;
502 priv->current_position += count;
503 priv->remaining -= count;
504
505 /* if all data has been transferred, set buffer free */
506 if (priv->remaining == 0)
507 priv->buffer_free = 1;
508
509 return count;
510}
511
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800512static ssize_t vmlogrdr_autopurge_store(struct device * dev,
513 struct device_attribute *attr,
514 const char * buf, size_t count)
515{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700516 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ssize_t ret = count;
518
519 switch (buf[0]) {
520 case '0':
521 priv->autopurge=0;
522 break;
523 case '1':
524 priv->autopurge=1;
525 break;
526 default:
527 ret = -EINVAL;
528 }
529 return ret;
530}
531
532
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800533static ssize_t vmlogrdr_autopurge_show(struct device *dev,
534 struct device_attribute *attr,
535 char *buf)
536{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700537 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return sprintf(buf, "%u\n", priv->autopurge);
539}
540
541
542static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
543 vmlogrdr_autopurge_store);
544
545
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800546static ssize_t vmlogrdr_purge_store(struct device * dev,
547 struct device_attribute *attr,
548 const char * buf, size_t count)
549{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 char cp_command[80];
552 char cp_response[80];
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700553 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (buf[0] != '1')
556 return -EINVAL;
557
558 memset(cp_command, 0x00, sizeof(cp_command));
559 memset(cp_response, 0x00, sizeof(cp_response));
560
561 /*
562 * The recording command needs to be called with option QID
563 * for guests that have previlege classes A or B.
564 * Other guests will not recognize the command and we have to
565 * issue the same command without the QID parameter.
566 */
567
568 if (recording_class_AB)
569 snprintf(cp_command, sizeof(cp_command),
570 "RECORDING %s PURGE QID * ",
571 priv->recording_name);
572 else
573 snprintf(cp_command, sizeof(cp_command),
574 "RECORDING %s PURGE ",
575 priv->recording_name);
576
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700577 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return count;
580}
581
582
583static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
584
585
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800586static ssize_t vmlogrdr_autorecording_store(struct device *dev,
587 struct device_attribute *attr,
588 const char *buf, size_t count)
589{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700590 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ssize_t ret = count;
592
593 switch (buf[0]) {
594 case '0':
595 priv->autorecording=0;
596 break;
597 case '1':
598 priv->autorecording=1;
599 break;
600 default:
601 ret = -EINVAL;
602 }
603 return ret;
604}
605
606
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800607static ssize_t vmlogrdr_autorecording_show(struct device *dev,
608 struct device_attribute *attr,
609 char *buf)
610{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700611 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return sprintf(buf, "%u\n", priv->autorecording);
613}
614
615
616static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
617 vmlogrdr_autorecording_store);
618
619
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800620static ssize_t vmlogrdr_recording_store(struct device * dev,
621 struct device_attribute *attr,
622 const char * buf, size_t count)
623{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700624 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ssize_t ret;
626
627 switch (buf[0]) {
628 case '0':
629 ret = vmlogrdr_recording(priv,0,0);
630 break;
631 case '1':
632 ret = vmlogrdr_recording(priv,1,0);
633 break;
634 default:
635 ret = -EINVAL;
636 }
637 if (ret)
638 return ret;
639 else
640 return count;
641
642}
643
644
645static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
646
647
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800648static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
649 char *buf)
650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Joe Perchesbf2106a2010-10-25 16:10:20 +0200652 static const char cp_command[] = "QUERY RECORDING ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int len;
654
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700655 cpcmd(cp_command, buf, 4096, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 len = strlen(buf);
657 return len;
658}
659
660
661static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
662 NULL);
663
664static struct attribute *vmlogrdr_attrs[] = {
665 &dev_attr_autopurge.attr,
666 &dev_attr_purge.attr,
667 &dev_attr_autorecording.attr,
668 &dev_attr_recording.attr,
669 NULL,
670};
Sebastian Ott76e03772012-06-04 19:32:13 +0200671static struct attribute_group vmlogrdr_attr_group = {
672 .attrs = vmlogrdr_attrs,
673};
674static const struct attribute_group *vmlogrdr_attr_groups[] = {
675 &vmlogrdr_attr_group,
676 NULL,
677};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200679static int vmlogrdr_pm_prepare(struct device *dev)
680{
681 int rc;
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200682 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200683
684 rc = 0;
685 if (priv) {
686 spin_lock_bh(&priv->priv_lock);
687 if (priv->dev_in_use)
688 rc = -EBUSY;
689 spin_unlock_bh(&priv->priv_lock);
690 }
691 if (rc)
692 pr_err("vmlogrdr: device %s is busy. Refuse to suspend.\n",
693 dev_name(dev));
694 return rc;
695}
696
697
Alexey Dobriyan47145212009-12-14 18:00:08 -0800698static const struct dev_pm_ops vmlogrdr_pm_ops = {
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200699 .prepare = vmlogrdr_pm_prepare,
700};
701
gregkh@suse.de56b22932005-03-23 10:01:41 -0800702static struct class *vmlogrdr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static struct device_driver vmlogrdr_driver = {
704 .name = "vmlogrdr",
705 .bus = &iucv_bus,
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200706 .pm = &vmlogrdr_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707};
708
709
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800710static int vmlogrdr_register_driver(void)
711{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int ret;
713
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800714 /* Register with iucv driver */
715 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200716 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800717 goto out;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ret = driver_register(&vmlogrdr_driver);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200720 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800721 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 ret = driver_create_file(&vmlogrdr_driver,
724 &driver_attr_recording_status);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200725 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800726 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
gregkh@suse.de56b22932005-03-23 10:01:41 -0800728 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (IS_ERR(vmlogrdr_class)) {
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800730 ret = PTR_ERR(vmlogrdr_class);
731 vmlogrdr_class = NULL;
732 goto out_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 return 0;
735
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800736out_attr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800738out_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800740out_iucv:
741 iucv_unregister(&vmlogrdr_iucv_handler, 1);
742out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
746
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800747static void vmlogrdr_unregister_driver(void)
748{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800749 class_destroy(vmlogrdr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 vmlogrdr_class = NULL;
751 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
752 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800753 iucv_unregister(&vmlogrdr_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800757static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
758{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct device *dev;
760 int ret;
761
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800762 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (dev) {
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200764 dev_set_name(dev, priv->internal_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dev->bus = &iucv_bus;
766 dev->parent = iucv_root;
767 dev->driver = &vmlogrdr_driver;
Sebastian Ott76e03772012-06-04 19:32:13 +0200768 dev->groups = vmlogrdr_attr_groups;
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200769 dev_set_drvdata(dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
771 * The release function could be called after the
772 * module has been unloaded. It's _only_ task is to
773 * free the struct. Therefore, we specify kfree()
774 * directly here. (Probably a little bit obfuscating
775 * but legitime ...).
776 */
777 dev->release = (void (*)(struct device *))kfree;
778 } else
779 return -ENOMEM;
780 ret = device_register(dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200781 if (ret) {
782 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return ret;
Sebastian Ottc6304932009-09-11 10:28:38 +0200784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -0700786 priv->class_device = device_create(vmlogrdr_class, dev,
787 MKDEV(vmlogrdr_major,
788 priv->minor_num),
789 priv, "%s", dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (IS_ERR(priv->class_device)) {
791 ret = PTR_ERR(priv->class_device);
792 priv->class_device=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 device_unregister(dev);
794 return ret;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 priv->device = dev;
797 return 0;
798}
799
800
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800801static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
802{
Cornelia Huck7f021ce2007-10-22 12:52:42 +0200803 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (priv->device != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 device_unregister(priv->device);
806 priv->device=NULL;
807 }
808 return 0;
809}
810
811
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800812static int vmlogrdr_register_cdev(dev_t dev)
813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int rc = 0;
815 vmlogrdr_cdev = cdev_alloc();
816 if (!vmlogrdr_cdev) {
817 return -ENOMEM;
818 }
819 vmlogrdr_cdev->owner = THIS_MODULE;
820 vmlogrdr_cdev->ops = &vmlogrdr_fops;
821 vmlogrdr_cdev->dev = dev;
822 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
823 if (!rc)
824 return 0;
825
826 // cleanup: cdev is not fully registered, no cdev_del here!
827 kobject_put(&vmlogrdr_cdev->kobj);
828 vmlogrdr_cdev=NULL;
829 return rc;
830}
831
832
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800833static void vmlogrdr_cleanup(void)
834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int i;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (vmlogrdr_cdev) {
838 cdev_del(vmlogrdr_cdev);
839 vmlogrdr_cdev=NULL;
840 }
841 for (i=0; i < MAXMINOR; ++i ) {
842 vmlogrdr_unregister_device(&sys_ser[i]);
843 free_page((unsigned long)sys_ser[i].buffer);
844 }
845 vmlogrdr_unregister_driver();
846 if (vmlogrdr_major) {
847 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
848 vmlogrdr_major=0;
849 }
850}
851
852
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200853static int __init vmlogrdr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 int rc;
856 int i;
857 dev_t dev;
858
859 if (! MACHINE_IS_VM) {
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100860 pr_err("not running under VM, driver not loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENODEV;
862 }
863
864 recording_class_AB = vmlogrdr_get_recording_class_AB();
865
866 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
867 if (rc)
868 return rc;
869 vmlogrdr_major = MAJOR(dev);
870
871 rc=vmlogrdr_register_driver();
872 if (rc)
873 goto cleanup;
874
875 for (i=0; i < MAXMINOR; ++i ) {
876 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
877 if (!sys_ser[i].buffer) {
Marcin Slusarz3cb2cea2008-05-15 16:52:32 +0200878 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 }
881 sys_ser[i].current_position = sys_ser[i].buffer;
882 rc=vmlogrdr_register_device(&sys_ser[i]);
883 if (rc)
884 break;
885 }
886 if (rc)
887 goto cleanup;
888
889 rc = vmlogrdr_register_cdev(dev);
890 if (rc)
891 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return 0;
893
894cleanup:
895 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return rc;
897}
898
899
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200900static void __exit vmlogrdr_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return;
904}
905
906
907module_init(vmlogrdr_init);
908module_exit(vmlogrdr_exit);