blob: 62559dc0169f8c9f32a4677e946e0e88880ae17f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * character device driver for reading z/VM system service records
3 *
4 *
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +02005 * Copyright IBM Corp. 2004, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * character device driver for reading z/VM system service records,
7 * Version 1.0
8 * Author(s): Xenia Tkatschow <xenia@us.ibm.com>
9 * Stefan Weinhuber <wein@de.ibm.com>
10 *
11 */
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +010012
13#define KMSG_COMPONENT "vmlogrdr"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/interrupt.h>
22#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/cpcmd.h>
26#include <asm/debug.h>
27#include <asm/ebcdic.h>
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080028#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kmod.h>
30#include <linux/cdev.h>
31#include <linux/device.h>
32#include <linux/string.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034MODULE_AUTHOR
35 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
36 " Stefan Weinhuber (wein@de.ibm.com)");
37MODULE_DESCRIPTION ("Character device driver for reading z/VM "
38 "system service records.");
39MODULE_LICENSE("GPL");
40
41
42/*
43 * The size of the buffer for iucv data transfer is one page,
44 * but in addition to the data we read from iucv we also
45 * place an integer and some characters into that buffer,
46 * so the maximum size for record data is a little less then
47 * one page.
48 */
49#define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
50
51/*
52 * The elements that are concurrently accessed by bottom halves are
53 * connection_established, iucv_path_severed, local_interrupt_buffer
54 * and receive_ready. The first three can be protected by
55 * priv_lock. receive_ready is atomic, so it can be incremented and
56 * decremented without holding a lock.
57 * The variable dev_in_use needs to be protected by the lock, since
58 * it's a flag used by open to make sure that the device is opened only
59 * by one user at the same time.
60 */
61struct vmlogrdr_priv_t {
62 char system_service[8];
63 char internal_name[8];
64 char recording_name[8];
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080065 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int connection_established;
67 int iucv_path_severed;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080068 struct iucv_message local_interrupt_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 atomic_t receive_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int minor_num;
71 char * buffer;
72 char * current_position;
73 int remaining;
74 ulong residual_length;
75 int buffer_free;
76 int dev_in_use; /* 1: already opened, 0: not opened*/
77 spinlock_t priv_lock;
78 struct device *device;
Cornelia Huck7f021ce2007-10-22 12:52:42 +020079 struct device *class_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int autorecording;
81 int autopurge;
82};
83
84
85/*
86 * File operation structure for vmlogrdr devices
87 */
88static int vmlogrdr_open(struct inode *, struct file *);
89static int vmlogrdr_release(struct inode *, struct file *);
Heiko Carstensd2c993d2006-07-12 16:41:55 +020090static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
91 size_t count, loff_t * ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080093static const struct file_operations vmlogrdr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .owner = THIS_MODULE,
95 .open = vmlogrdr_open,
96 .release = vmlogrdr_release,
97 .read = vmlogrdr_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +020098 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101
Ursula Braun91e60eb2015-09-18 16:06:52 +0200102static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 *ipuser);
103static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 *ipuser);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800104static void vmlogrdr_iucv_message_pending(struct iucv_path *,
105 struct iucv_message *);
106
107
108static struct iucv_handler vmlogrdr_iucv_handler = {
109 .path_complete = vmlogrdr_iucv_path_complete,
110 .path_severed = vmlogrdr_iucv_path_severed,
111 .message_pending = vmlogrdr_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100115static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
116static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * pointer to system service private structure
120 * minor number 0 --> logrec
121 * minor number 1 --> account
122 * minor number 2 --> symptom
123 */
124
125static struct vmlogrdr_priv_t sys_ser[] = {
126 { .system_service = "*LOGREC ",
127 .internal_name = "logrec",
128 .recording_name = "EREP",
129 .minor_num = 0,
130 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200131 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .autorecording = 1,
133 .autopurge = 1,
134 },
135 { .system_service = "*ACCOUNT",
136 .internal_name = "account",
137 .recording_name = "ACCOUNT",
138 .minor_num = 1,
139 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200140 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .autorecording = 1,
142 .autopurge = 1,
143 },
144 { .system_service = "*SYMPTOM",
145 .internal_name = "symptom",
146 .recording_name = "SYMPTOM",
147 .minor_num = 2,
148 .buffer_free = 1,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200149 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .autorecording = 1,
151 .autopurge = 1,
152 }
153};
154
155#define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
156
157static char FENCE[] = {"EOR"};
158static int vmlogrdr_major = 0;
159static struct cdev *vmlogrdr_cdev = NULL;
160static int recording_class_AB;
161
162
Ursula Braun91e60eb2015-09-18 16:06:52 +0200163static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800165 struct vmlogrdr_priv_t * logptr = path->private;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 spin_lock(&logptr->priv_lock);
168 logptr->connection_established = 1;
169 spin_unlock(&logptr->priv_lock);
170 wake_up(&conn_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173
Ursula Braun91e60eb2015-09-18 16:06:52 +0200174static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800176 struct vmlogrdr_priv_t * logptr = path->private;
177 u8 reason = (u8) ipuser[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100179 pr_err("vmlogrdr: connection severed with reason %i\n", reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800181 iucv_path_sever(path, NULL);
182 kfree(path);
183 logptr->path = NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_lock(&logptr->priv_lock);
186 logptr->connection_established = 0;
187 logptr->iucv_path_severed = 1;
188 spin_unlock(&logptr->priv_lock);
189
190 wake_up(&conn_wait_queue);
191 /* just in case we're sleeping waiting for a record */
192 wake_up_interruptible(&read_wait_queue);
193}
194
195
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800196static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
197 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800199 struct vmlogrdr_priv_t * logptr = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /*
202 * This function is the bottom half so it should be quick.
203 * Copy the external interrupt data into our local eib and increment
204 * the usage count
205 */
206 spin_lock(&logptr->priv_lock);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800207 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 atomic_inc(&logptr->receive_ready);
209 spin_unlock(&logptr->priv_lock);
210 wake_up_interruptible(&read_wait_queue);
211}
212
213
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800214static int vmlogrdr_get_recording_class_AB(void)
215{
Joe Perchesbf2106a2010-10-25 16:10:20 +0200216 static const char cp_command[] = "QUERY COMMAND RECORDING ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 char cp_response[80];
218 char *tail;
219 int len,i;
220
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700221 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 len = strnlen(cp_response,sizeof(cp_response));
223 // now the parsing
224 tail=strnchr(cp_response,len,'=');
225 if (!tail)
226 return 0;
227 tail++;
228 if (!strncmp("ANY",tail,3))
229 return 1;
230 if (!strncmp("NONE",tail,4))
231 return 0;
232 /*
233 * expect comma separated list of classes here, if one of them
234 * is A or B return 1 otherwise 0
235 */
236 for (i=tail-cp_response; i<len; i++)
237 if ( cp_response[i]=='A' || cp_response[i]=='B' )
238 return 1;
239 return 0;
240}
241
242
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800243static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
244 int action, int purge)
245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 char cp_command[80];
248 char cp_response[160];
249 char *onoff, *qid_string;
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100250 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100252 onoff = ((action == 1) ? "ON" : "OFF");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
254
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100255 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * 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 */
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100262 if (purge && (action == 1)) {
263 memset(cp_command, 0x00, sizeof(cp_command));
264 memset(cp_response, 0x00, sizeof(cp_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 snprintf(cp_command, sizeof(cp_command),
266 "RECORDING %s PURGE %s",
267 logptr->recording_name,
268 qid_string);
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);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700278 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* The recording command will usually answer with 'Command complete'
280 * on success, but when the specific service was never connected
281 * before then there might be an additional informational message
282 * 'HCPCRC8072I Recording entry not found' before the
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100283 * 'Command complete'. So I use strstr rather then the strncmp.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285 if (strstr(cp_response,"Command complete"))
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100286 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 else
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100288 rc = -EIO;
289 /*
290 * If we turn recording off, we have to purge any remaining records
291 * afterwards, as a large number of queued records may impact z/VM
292 * performance.
293 */
294 if (purge && (action == 0)) {
295 memset(cp_command, 0x00, sizeof(cp_command));
296 memset(cp_response, 0x00, sizeof(cp_response));
297 snprintf(cp_command, sizeof(cp_command),
298 "RECORDING %s PURGE %s",
299 logptr->recording_name,
300 qid_string);
301 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Stefan Weinhuberca768b62010-11-10 10:05:54 +0100304 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800308static int vmlogrdr_open (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 int dev_num = 0;
311 struct vmlogrdr_priv_t * logptr = NULL;
312 int connect_rc = 0;
313 int ret;
314
315 dev_num = iminor(inode);
Heiko Carstens9784bd42013-10-14 14:26:08 +0200316 if (dev_num >= MAXMINOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 logptr = &sys_ser[dev_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * only allow for blocking reads to be open
322 */
323 if (filp->f_flags & O_NONBLOCK)
Heiko Carstens745e9672012-09-06 17:35:00 +0200324 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Besure this device hasn't already been opened */
327 spin_lock_bh(&logptr->priv_lock);
328 if (logptr->dev_in_use) {
329 spin_unlock_bh(&logptr->priv_lock);
330 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800332 logptr->dev_in_use = 1;
333 logptr->connection_established = 0;
334 logptr->iucv_path_severed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 atomic_set(&logptr->receive_ready, 0);
336 logptr->buffer_free = 1;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800337 spin_unlock_bh(&logptr->priv_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* set the file options */
340 filp->private_data = logptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* start recording for this service*/
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800343 if (logptr->autorecording) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800345 if (ret)
Joe Perchesbaebc702016-03-03 20:49:57 -0800346 pr_warn("vmlogrdr: failed to start recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* create connection to the system service */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800350 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (connect_rc) {
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100357 pr_err("vmlogrdr: iucv connection to %s "
358 "failed with rc %i \n",
359 logptr->system_service, connect_rc);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800360 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
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));
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800369 if (logptr->iucv_path_severed)
370 goto out_record;
Martin Schwidefsky3b47f9d2009-12-07 12:52:23 +0100371 nonseekable_open(inode, filp);
372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800374out_record:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (logptr->autorecording)
376 vmlogrdr_recording(logptr,0,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800377out_path:
378 kfree(logptr->path); /* kfree(NULL) is ok. */
379 logptr->path = NULL;
380out_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 logptr->dev_in_use = 0;
382 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800386static int vmlogrdr_release (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 int ret;
389
390 struct vmlogrdr_priv_t * logptr = filp->private_data;
391
Ursula Braun66b494a2007-04-27 16:01:52 +0200392 iucv_path_sever(logptr->path, NULL);
393 kfree(logptr->path);
394 logptr->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (logptr->autorecording) {
396 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
397 if (ret)
Joe Perchesbaebc702016-03-03 20:49:57 -0800398 pr_warn("vmlogrdr: failed to stop recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 logptr->dev_in_use = 0;
401
402 return 0;
403}
404
405
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800406static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int rc, *temp;
409 /* we need to keep track of two data sizes here:
410 * The number of bytes we need to receive from iucv and
411 * the total number of bytes we actually write into the buffer.
412 */
413 int user_data_count, iucv_data_count;
414 char * buffer;
415
416 if (atomic_read(&priv->receive_ready)) {
417 spin_lock_bh(&priv->priv_lock);
418 if (priv->residual_length){
419 /* receive second half of a record */
420 iucv_data_count = priv->residual_length;
421 user_data_count = 0;
422 buffer = priv->buffer;
423 } else {
424 /* receive a new record:
425 * We need to return the total length of the record
426 * + size of FENCE in the first 4 bytes of the buffer.
427 */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800428 iucv_data_count = priv->local_interrupt_buffer.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 user_data_count = sizeof(int);
430 temp = (int*)priv->buffer;
431 *temp= iucv_data_count + sizeof(FENCE);
432 buffer = priv->buffer + sizeof(int);
433 }
434 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200435 * If the record is bigger than our buffer, we receive only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * a part of it. We can get the rest later.
437 */
438 if (iucv_data_count > NET_BUFFER_SIZE)
439 iucv_data_count = NET_BUFFER_SIZE;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800440 rc = iucv_message_receive(priv->path,
441 &priv->local_interrupt_buffer,
442 0, buffer, iucv_data_count,
443 &priv->residual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock_bh(&priv->priv_lock);
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200445 /* An rc of 5 indicates that the record was bigger than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * the buffer, which is OK for us. A 9 indicates that the
447 * record was purged befor we could receive it.
448 */
449 if (rc == 5)
450 rc = 0;
451 if (rc == 9)
452 atomic_set(&priv->receive_ready, 0);
453 } else {
454 rc = 1;
455 }
456 if (!rc) {
457 priv->buffer_free = 0;
458 user_data_count += iucv_data_count;
459 priv->current_position = priv->buffer;
460 if (priv->residual_length == 0){
461 /* the whole record has been captured,
462 * now add the fence */
463 atomic_dec(&priv->receive_ready);
464 buffer = priv->buffer + user_data_count;
465 memcpy(buffer, FENCE, sizeof(FENCE));
466 user_data_count += sizeof(FENCE);
467 }
468 priv->remaining = user_data_count;
469 }
470
471 return rc;
472}
473
474
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800475static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
476 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 int rc;
479 struct vmlogrdr_priv_t * priv = filp->private_data;
480
481 while (priv->buffer_free) {
482 rc = vmlogrdr_receive_data(priv);
483 if (rc) {
484 rc = wait_event_interruptible(read_wait_queue,
485 atomic_read(&priv->receive_ready));
486 if (rc)
487 return rc;
488 }
489 }
490 /* copy only up to end of record */
491 if (count > priv->remaining)
492 count = priv->remaining;
493
494 if (copy_to_user(data, priv->current_position, count))
495 return -EFAULT;
496
497 *ppos += count;
498 priv->current_position += count;
499 priv->remaining -= count;
500
501 /* if all data has been transferred, set buffer free */
502 if (priv->remaining == 0)
503 priv->buffer_free = 1;
504
505 return count;
506}
507
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800508static ssize_t vmlogrdr_autopurge_store(struct device * dev,
509 struct device_attribute *attr,
510 const char * buf, size_t count)
511{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700512 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ssize_t ret = count;
514
515 switch (buf[0]) {
516 case '0':
517 priv->autopurge=0;
518 break;
519 case '1':
520 priv->autopurge=1;
521 break;
522 default:
523 ret = -EINVAL;
524 }
525 return ret;
526}
527
528
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800529static ssize_t vmlogrdr_autopurge_show(struct device *dev,
530 struct device_attribute *attr,
531 char *buf)
532{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700533 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return sprintf(buf, "%u\n", priv->autopurge);
535}
536
537
538static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
539 vmlogrdr_autopurge_store);
540
541
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800542static ssize_t vmlogrdr_purge_store(struct device * dev,
543 struct device_attribute *attr,
544 const char * buf, size_t count)
545{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 char cp_command[80];
548 char cp_response[80];
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700549 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (buf[0] != '1')
552 return -EINVAL;
553
554 memset(cp_command, 0x00, sizeof(cp_command));
555 memset(cp_response, 0x00, sizeof(cp_response));
556
557 /*
558 * The recording command needs to be called with option QID
559 * for guests that have previlege classes A or B.
560 * Other guests will not recognize the command and we have to
561 * issue the same command without the QID parameter.
562 */
563
564 if (recording_class_AB)
565 snprintf(cp_command, sizeof(cp_command),
566 "RECORDING %s PURGE QID * ",
567 priv->recording_name);
568 else
569 snprintf(cp_command, sizeof(cp_command),
570 "RECORDING %s PURGE ",
571 priv->recording_name);
572
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700573 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return count;
576}
577
578
579static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
580
581
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800582static ssize_t vmlogrdr_autorecording_store(struct device *dev,
583 struct device_attribute *attr,
584 const char *buf, size_t count)
585{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700586 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ssize_t ret = count;
588
589 switch (buf[0]) {
590 case '0':
591 priv->autorecording=0;
592 break;
593 case '1':
594 priv->autorecording=1;
595 break;
596 default:
597 ret = -EINVAL;
598 }
599 return ret;
600}
601
602
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800603static ssize_t vmlogrdr_autorecording_show(struct device *dev,
604 struct device_attribute *attr,
605 char *buf)
606{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700607 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return sprintf(buf, "%u\n", priv->autorecording);
609}
610
611
612static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
613 vmlogrdr_autorecording_store);
614
615
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800616static ssize_t vmlogrdr_recording_store(struct device * dev,
617 struct device_attribute *attr,
618 const char * buf, size_t count)
619{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700620 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ssize_t ret;
622
623 switch (buf[0]) {
624 case '0':
625 ret = vmlogrdr_recording(priv,0,0);
626 break;
627 case '1':
628 ret = vmlogrdr_recording(priv,1,0);
629 break;
630 default:
631 ret = -EINVAL;
632 }
633 if (ret)
634 return ret;
635 else
636 return count;
637
638}
639
640
641static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
642
643
Greg Kroah-Hartman36369562017-06-09 11:03:13 +0200644static ssize_t recording_status_show(struct device_driver *driver, char *buf)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800645{
Joe Perchesbf2106a2010-10-25 16:10:20 +0200646 static const char cp_command[] = "QUERY RECORDING ";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int len;
648
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700649 cpcmd(cp_command, buf, 4096, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 len = strlen(buf);
651 return len;
652}
Greg Kroah-Hartman36369562017-06-09 11:03:13 +0200653static DRIVER_ATTR_RO(recording_status);
Sebastian Ott72f6e3a2012-06-04 19:33:09 +0200654static struct attribute *vmlogrdr_drv_attrs[] = {
655 &driver_attr_recording_status.attr,
656 NULL,
657};
658static struct attribute_group vmlogrdr_drv_attr_group = {
659 .attrs = vmlogrdr_drv_attrs,
660};
661static const struct attribute_group *vmlogrdr_drv_attr_groups[] = {
662 &vmlogrdr_drv_attr_group,
663 NULL,
664};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666static 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};
Sebastian Ott76e03772012-06-04 19:32:13 +0200673static struct attribute_group vmlogrdr_attr_group = {
674 .attrs = vmlogrdr_attrs,
675};
676static const struct attribute_group *vmlogrdr_attr_groups[] = {
677 &vmlogrdr_attr_group,
678 NULL,
679};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200681static int vmlogrdr_pm_prepare(struct device *dev)
682{
683 int rc;
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200684 struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev);
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200685
686 rc = 0;
687 if (priv) {
688 spin_lock_bh(&priv->priv_lock);
689 if (priv->dev_in_use)
690 rc = -EBUSY;
691 spin_unlock_bh(&priv->priv_lock);
692 }
693 if (rc)
694 pr_err("vmlogrdr: device %s is busy. Refuse to suspend.\n",
695 dev_name(dev));
696 return rc;
697}
698
699
Alexey Dobriyan47145212009-12-14 18:00:08 -0800700static const struct dev_pm_ops vmlogrdr_pm_ops = {
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200701 .prepare = vmlogrdr_pm_prepare,
702};
703
gregkh@suse.de56b22932005-03-23 10:01:41 -0800704static struct class *vmlogrdr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static struct device_driver vmlogrdr_driver = {
706 .name = "vmlogrdr",
707 .bus = &iucv_bus,
Stefan Weinhuber9f62fa12009-06-16 10:30:38 +0200708 .pm = &vmlogrdr_pm_ops,
Sebastian Ott72f6e3a2012-06-04 19:33:09 +0200709 .groups = vmlogrdr_drv_attr_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710};
711
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800712static int vmlogrdr_register_driver(void)
713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int ret;
715
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800716 /* Register with iucv driver */
717 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200718 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800719 goto out;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ret = driver_register(&vmlogrdr_driver);
Martin Schwidefsky2f6f2522008-07-14 09:59:37 +0200722 if (ret)
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800723 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
gregkh@suse.de56b22932005-03-23 10:01:41 -0800725 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (IS_ERR(vmlogrdr_class)) {
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800727 ret = PTR_ERR(vmlogrdr_class);
728 vmlogrdr_class = NULL;
Sebastian Ott72f6e3a2012-06-04 19:33:09 +0200729 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 return 0;
732
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800733out_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800735out_iucv:
736 iucv_unregister(&vmlogrdr_iucv_handler, 1);
737out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return ret;
739}
740
741
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800742static void vmlogrdr_unregister_driver(void)
743{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800744 class_destroy(vmlogrdr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 vmlogrdr_class = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800747 iucv_unregister(&vmlogrdr_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800751static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct device *dev;
754 int ret;
755
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800756 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (dev) {
Kees Cookef283688f2014-06-10 10:46:20 -0700758 dev_set_name(dev, "%s", priv->internal_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dev->bus = &iucv_bus;
760 dev->parent = iucv_root;
761 dev->driver = &vmlogrdr_driver;
Sebastian Ott76e03772012-06-04 19:32:13 +0200762 dev->groups = vmlogrdr_attr_groups;
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200763 dev_set_drvdata(dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * The release function could be called after the
766 * module has been unloaded. It's _only_ task is to
767 * free the struct. Therefore, we specify kfree()
768 * directly here. (Probably a little bit obfuscating
769 * but legitime ...).
770 */
771 dev->release = (void (*)(struct device *))kfree;
772 } else
773 return -ENOMEM;
774 ret = device_register(dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200775 if (ret) {
776 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return ret;
Sebastian Ottc6304932009-09-11 10:28:38 +0200778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -0700780 priv->class_device = device_create(vmlogrdr_class, dev,
781 MKDEV(vmlogrdr_major,
782 priv->minor_num),
783 priv, "%s", dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (IS_ERR(priv->class_device)) {
785 ret = PTR_ERR(priv->class_device);
786 priv->class_device=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 device_unregister(dev);
788 return ret;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 priv->device = dev;
791 return 0;
792}
793
794
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800795static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
796{
Cornelia Huck7f021ce2007-10-22 12:52:42 +0200797 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (priv->device != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 device_unregister(priv->device);
800 priv->device=NULL;
801 }
802 return 0;
803}
804
805
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800806static int vmlogrdr_register_cdev(dev_t dev)
807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int rc = 0;
809 vmlogrdr_cdev = cdev_alloc();
810 if (!vmlogrdr_cdev) {
811 return -ENOMEM;
812 }
813 vmlogrdr_cdev->owner = THIS_MODULE;
814 vmlogrdr_cdev->ops = &vmlogrdr_fops;
Jean Delvareb08e19d2017-09-18 11:13:13 +0200815 rc = cdev_add(vmlogrdr_cdev, dev, MAXMINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!rc)
817 return 0;
818
819 // cleanup: cdev is not fully registered, no cdev_del here!
820 kobject_put(&vmlogrdr_cdev->kobj);
821 vmlogrdr_cdev=NULL;
822 return rc;
823}
824
825
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800826static void vmlogrdr_cleanup(void)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int i;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (vmlogrdr_cdev) {
831 cdev_del(vmlogrdr_cdev);
832 vmlogrdr_cdev=NULL;
833 }
834 for (i=0; i < MAXMINOR; ++i ) {
835 vmlogrdr_unregister_device(&sys_ser[i]);
836 free_page((unsigned long)sys_ser[i].buffer);
837 }
838 vmlogrdr_unregister_driver();
839 if (vmlogrdr_major) {
840 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
841 vmlogrdr_major=0;
842 }
843}
844
845
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200846static int __init vmlogrdr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int rc;
849 int i;
850 dev_t dev;
851
852 if (! MACHINE_IS_VM) {
Martin Schwidefsky5466c2e2008-12-25 13:39:52 +0100853 pr_err("not running under VM, driver not loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -ENODEV;
855 }
856
857 recording_class_AB = vmlogrdr_get_recording_class_AB();
858
859 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
860 if (rc)
861 return rc;
862 vmlogrdr_major = MAJOR(dev);
863
864 rc=vmlogrdr_register_driver();
865 if (rc)
866 goto cleanup;
867
868 for (i=0; i < MAXMINOR; ++i ) {
Gerald Schaefer5457e032016-11-21 12:13:58 +0100869 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!sys_ser[i].buffer) {
Marcin Slusarz3cb2cea2008-05-15 16:52:32 +0200871 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 }
874 sys_ser[i].current_position = sys_ser[i].buffer;
875 rc=vmlogrdr_register_device(&sys_ser[i]);
876 if (rc)
877 break;
878 }
879 if (rc)
880 goto cleanup;
881
882 rc = vmlogrdr_register_cdev(dev);
883 if (rc)
884 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
886
887cleanup:
888 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return rc;
890}
891
892
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200893static void __exit vmlogrdr_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 vmlogrdr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return;
897}
898
899
900module_init(vmlogrdr_init);
901module_exit(vmlogrdr_exit);