| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * drivers/s390/char/vmlogrdr.c | 
 | 3 |  *	character device driver for reading z/VM system service records | 
 | 4 |  * | 
 | 5 |  * | 
| Stefan Weinhuber | 9f62fa1 | 2009-06-16 10:30:38 +0200 | [diff] [blame] | 6 |  *	Copyright IBM Corp. 2004, 2009 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  *	character device driver for reading z/VM system service records, | 
 | 8 |  *	Version 1.0 | 
 | 9 |  *	Author(s): Xenia Tkatschow <xenia@us.ibm.com> | 
 | 10 |  *		   Stefan Weinhuber <wein@de.ibm.com> | 
 | 11 |  * | 
 | 12 |  */ | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 13 |  | 
 | 14 | #define KMSG_COMPONENT "vmlogrdr" | 
 | 15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | 
 | 16 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> | 
 | 18 | #include <linux/init.h> | 
 | 19 | #include <linux/errno.h> | 
 | 20 | #include <linux/types.h> | 
 | 21 | #include <linux/interrupt.h> | 
 | 22 | #include <linux/spinlock.h> | 
 | 23 | #include <asm/atomic.h> | 
 | 24 | #include <asm/uaccess.h> | 
 | 25 | #include <asm/cpcmd.h> | 
 | 26 | #include <asm/debug.h> | 
 | 27 | #include <asm/ebcdic.h> | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 28 | #include <net/iucv/iucv.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kmod.h> | 
 | 30 | #include <linux/cdev.h> | 
 | 31 | #include <linux/device.h> | 
| Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 32 | #include <linux/smp_lock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/string.h> | 
 | 34 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | MODULE_AUTHOR | 
 | 36 | 	("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n" | 
 | 37 | 	 "                            Stefan Weinhuber (wein@de.ibm.com)"); | 
 | 38 | MODULE_DESCRIPTION ("Character device driver for reading z/VM " | 
 | 39 | 		    "system service records."); | 
 | 40 | MODULE_LICENSE("GPL"); | 
 | 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 |  */ | 
 | 62 | struct vmlogrdr_priv_t { | 
 | 63 | 	char system_service[8]; | 
 | 64 | 	char internal_name[8]; | 
 | 65 | 	char recording_name[8]; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 66 | 	struct iucv_path *path; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	int connection_established; | 
 | 68 | 	int iucv_path_severed; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 69 | 	struct iucv_message local_interrupt_buffer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 	atomic_t receive_ready; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | 	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 Huck | 7f021ce | 2007-10-22 12:52:42 +0200 | [diff] [blame] | 80 | 	struct device  *class_device; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 	int autorecording; | 
 | 82 | 	int autopurge; | 
 | 83 | }; | 
 | 84 |  | 
 | 85 |  | 
 | 86 | /* | 
 | 87 |  * File operation structure for vmlogrdr devices | 
 | 88 |  */ | 
 | 89 | static int vmlogrdr_open(struct inode *, struct file *); | 
 | 90 | static int vmlogrdr_release(struct inode *, struct file *); | 
| Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 91 | static ssize_t vmlogrdr_read (struct file *filp, char __user *data, | 
 | 92 | 			      size_t count, loff_t * ppos); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  | 
| Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 94 | static const struct file_operations vmlogrdr_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | 	.owner   = THIS_MODULE, | 
 | 96 | 	.open    = vmlogrdr_open, | 
 | 97 | 	.release = vmlogrdr_release, | 
 | 98 | 	.read    = vmlogrdr_read, | 
 | 99 | }; | 
 | 100 |  | 
 | 101 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 102 | static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]); | 
 | 103 | static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]); | 
 | 104 | static void vmlogrdr_iucv_message_pending(struct iucv_path *, | 
 | 105 | 					  struct iucv_message *); | 
 | 106 |  | 
 | 107 |  | 
 | 108 | static struct iucv_handler vmlogrdr_iucv_handler = { | 
 | 109 | 	.path_complete	 = vmlogrdr_iucv_path_complete, | 
 | 110 | 	.path_severed	 = vmlogrdr_iucv_path_severed, | 
 | 111 | 	.message_pending = vmlogrdr_iucv_message_pending, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | }; | 
 | 113 |  | 
 | 114 |  | 
| Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 115 | static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue); | 
 | 116 | static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 |  | 
 | 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 |  | 
 | 125 | static 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 Choudhary | cb629a0 | 2007-04-27 16:02:01 +0200 | [diff] [blame] | 131 | 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 	  .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 Choudhary | cb629a0 | 2007-04-27 16:02:01 +0200 | [diff] [blame] | 140 | 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | 	  .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 Choudhary | cb629a0 | 2007-04-27 16:02:01 +0200 | [diff] [blame] | 149 | 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 	  .autorecording  = 1, | 
 | 151 | 	  .autopurge      = 1, | 
 | 152 | 	} | 
 | 153 | }; | 
 | 154 |  | 
 | 155 | #define MAXMINOR  (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t)) | 
 | 156 |  | 
 | 157 | static char FENCE[] = {"EOR"}; | 
 | 158 | static int vmlogrdr_major = 0; | 
 | 159 | static struct cdev  *vmlogrdr_cdev = NULL; | 
 | 160 | static int recording_class_AB; | 
 | 161 |  | 
 | 162 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 163 | static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 165 | 	struct vmlogrdr_priv_t * logptr = path->private; | 
 | 166 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | 	spin_lock(&logptr->priv_lock); | 
 | 168 | 	logptr->connection_established = 1; | 
 | 169 | 	spin_unlock(&logptr->priv_lock); | 
 | 170 | 	wake_up(&conn_wait_queue); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } | 
 | 172 |  | 
 | 173 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 174 | static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 176 | 	struct vmlogrdr_priv_t * logptr = path->private; | 
 | 177 | 	u8 reason = (u8) ipuser[8]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 |  | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 179 | 	pr_err("vmlogrdr: connection severed with reason %i\n", reason); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 181 | 	iucv_path_sever(path, NULL); | 
 | 182 | 	kfree(path); | 
 | 183 | 	logptr->path = NULL; | 
 | 184 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | 	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 Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 196 | static void vmlogrdr_iucv_message_pending(struct iucv_path *path, | 
 | 197 | 					  struct iucv_message *msg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 199 | 	struct vmlogrdr_priv_t * logptr = path->private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 |  | 
 | 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 Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 207 | 	memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | 	atomic_inc(&logptr->receive_ready); | 
 | 209 | 	spin_unlock(&logptr->priv_lock); | 
 | 210 | 	wake_up_interruptible(&read_wait_queue); | 
 | 211 | } | 
 | 212 |  | 
 | 213 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 214 | static int vmlogrdr_get_recording_class_AB(void) | 
 | 215 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | 	char cp_command[]="QUERY COMMAND RECORDING "; | 
 | 217 | 	char cp_response[80]; | 
 | 218 | 	char *tail; | 
 | 219 | 	int len,i; | 
 | 220 |  | 
| Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 221 | 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | 	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 Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 243 | static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr, | 
 | 244 | 			      int action, int purge) | 
 | 245 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 |  | 
 | 247 | 	char cp_command[80]; | 
 | 248 | 	char cp_response[160]; | 
 | 249 | 	char *onoff, *qid_string; | 
 | 250 |  | 
 | 251 | 	memset(cp_command, 0x00, sizeof(cp_command)); | 
 | 252 | 	memset(cp_response, 0x00, sizeof(cp_response)); | 
 | 253 |  | 
 | 254 |         onoff = ((action == 1) ? "ON" : "OFF"); | 
 | 255 | 	qid_string = ((recording_class_AB == 1) ? " QID * " : ""); | 
 | 256 |  | 
 | 257 |         /* | 
 | 258 | 	 * The recording commands needs to be called with option QID | 
 | 259 | 	 * for guests that have previlege classes A or B. | 
 | 260 | 	 * Purging has to be done as separate step, because recording | 
 | 261 | 	 * can't be switched on as long as records are on the queue. | 
 | 262 | 	 * Doing both at the same time doesn't work. | 
 | 263 | 	 */ | 
 | 264 |  | 
 | 265 | 	if (purge) { | 
 | 266 | 		snprintf(cp_command, sizeof(cp_command), | 
 | 267 | 			 "RECORDING %s PURGE %s", | 
 | 268 | 			 logptr->recording_name, | 
 | 269 | 			 qid_string); | 
 | 270 |  | 
| Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 271 | 		cpcmd(cp_command, cp_response, sizeof(cp_response), NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | 	} | 
 | 273 |  | 
 | 274 | 	memset(cp_command, 0x00, sizeof(cp_command)); | 
 | 275 | 	memset(cp_response, 0x00, sizeof(cp_response)); | 
 | 276 | 	snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s", | 
 | 277 | 		logptr->recording_name, | 
 | 278 | 		onoff, | 
 | 279 | 		qid_string); | 
 | 280 |  | 
| Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 281 | 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 	/* The recording command will usually answer with 'Command complete' | 
 | 283 | 	 * on success, but when the specific service was never connected | 
 | 284 | 	 * before then there might be an additional informational message | 
 | 285 | 	 * 'HCPCRC8072I Recording entry not found' before the | 
 | 286 |          * 'Command complete'. So I use strstr rather then the strncmp. | 
 | 287 | 	 */ | 
 | 288 | 	if (strstr(cp_response,"Command complete")) | 
 | 289 | 		return 0; | 
 | 290 | 	else | 
 | 291 | 		return -EIO; | 
 | 292 |  | 
 | 293 | } | 
 | 294 |  | 
 | 295 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 296 | static int vmlogrdr_open (struct inode *inode, struct file *filp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { | 
 | 298 | 	int dev_num = 0; | 
 | 299 | 	struct vmlogrdr_priv_t * logptr = NULL; | 
 | 300 | 	int connect_rc = 0; | 
 | 301 | 	int ret; | 
 | 302 |  | 
 | 303 | 	dev_num = iminor(inode); | 
 | 304 | 	if (dev_num > MAXMINOR) | 
 | 305 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 	logptr = &sys_ser[dev_num]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 |  | 
 | 308 | 	/* | 
 | 309 | 	 * only allow for blocking reads to be open | 
 | 310 | 	 */ | 
 | 311 | 	if (filp->f_flags & O_NONBLOCK) | 
 | 312 | 		return -ENOSYS; | 
 | 313 |  | 
 | 314 | 	/* Besure this device hasn't already been opened */ | 
 | 315 | 	spin_lock_bh(&logptr->priv_lock); | 
 | 316 | 	if (logptr->dev_in_use)	{ | 
 | 317 | 		spin_unlock_bh(&logptr->priv_lock); | 
 | 318 | 		return -EBUSY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 	} | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 320 | 	logptr->dev_in_use = 1; | 
 | 321 | 	logptr->connection_established = 0; | 
 | 322 | 	logptr->iucv_path_severed = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | 	atomic_set(&logptr->receive_ready, 0); | 
 | 324 | 	logptr->buffer_free = 1; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 325 | 	spin_unlock_bh(&logptr->priv_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 |  | 
 | 327 | 	/* set the file options */ | 
 | 328 | 	filp->private_data = logptr; | 
 | 329 | 	filp->f_op = &vmlogrdr_fops; | 
 | 330 |  | 
 | 331 | 	/* start recording for this service*/ | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 332 | 	if (logptr->autorecording) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | 		ret = vmlogrdr_recording(logptr,1,logptr->autopurge); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 334 | 		if (ret) | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 335 | 			pr_warning("vmlogrdr: failed to start " | 
 | 336 | 				   "recording automatically\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | 	} | 
 | 338 |  | 
 | 339 | 	/* create connection to the system service */ | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 340 | 	logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL); | 
 | 341 | 	if (!logptr->path) | 
 | 342 | 		goto out_dev; | 
 | 343 | 	connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler, | 
 | 344 | 				       logptr->system_service, NULL, NULL, | 
 | 345 | 				       logptr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 	if (connect_rc) { | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 347 | 		pr_err("vmlogrdr: iucv connection to %s " | 
 | 348 | 		       "failed with rc %i \n", | 
 | 349 | 		       logptr->system_service, connect_rc); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 350 | 		goto out_path; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | 	} | 
 | 352 |  | 
 | 353 | 	/* We've issued the connect and now we must wait for a | 
 | 354 | 	 * ConnectionComplete or ConnectinSevered Interrupt | 
 | 355 | 	 * before we can continue to process. | 
 | 356 | 	 */ | 
 | 357 | 	wait_event(conn_wait_queue, (logptr->connection_established) | 
 | 358 | 		   || (logptr->iucv_path_severed)); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 359 | 	if (logptr->iucv_path_severed) | 
 | 360 | 		goto out_record; | 
| Martin Schwidefsky | 3b47f9d | 2009-12-07 12:52:23 +0100 | [diff] [blame] | 361 | 	nonseekable_open(inode, filp); | 
 | 362 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 364 | out_record: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 	if (logptr->autorecording) | 
 | 366 | 		vmlogrdr_recording(logptr,0,logptr->autopurge); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 367 | out_path: | 
 | 368 | 	kfree(logptr->path);	/* kfree(NULL) is ok. */ | 
 | 369 | 	logptr->path = NULL; | 
 | 370 | out_dev: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 	logptr->dev_in_use = 0; | 
 | 372 | 	return -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } | 
 | 374 |  | 
 | 375 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 376 | static int vmlogrdr_release (struct inode *inode, struct file *filp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { | 
 | 378 | 	int ret; | 
 | 379 |  | 
 | 380 | 	struct vmlogrdr_priv_t * logptr = filp->private_data; | 
 | 381 |  | 
| Ursula Braun | 66b494a | 2007-04-27 16:01:52 +0200 | [diff] [blame] | 382 | 	iucv_path_sever(logptr->path, NULL); | 
 | 383 | 	kfree(logptr->path); | 
 | 384 | 	logptr->path = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | 	if (logptr->autorecording) { | 
 | 386 | 		ret = vmlogrdr_recording(logptr,0,logptr->autopurge); | 
 | 387 | 		if (ret) | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 388 | 			pr_warning("vmlogrdr: failed to stop " | 
 | 389 | 				   "recording automatically\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | 	} | 
 | 391 | 	logptr->dev_in_use = 0; | 
 | 392 |  | 
 | 393 | 	return 0; | 
 | 394 | } | 
 | 395 |  | 
 | 396 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 397 | static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv) | 
 | 398 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | 	int rc, *temp; | 
 | 400 | 	/* we need to keep track of two data sizes here: | 
 | 401 | 	 * The number of bytes we need to receive from iucv and | 
 | 402 | 	 * the total number of bytes we actually write into the buffer. | 
 | 403 | 	 */ | 
 | 404 | 	int user_data_count, iucv_data_count; | 
 | 405 | 	char * buffer; | 
 | 406 |  | 
 | 407 | 	if (atomic_read(&priv->receive_ready)) { | 
 | 408 | 		spin_lock_bh(&priv->priv_lock); | 
 | 409 | 		if (priv->residual_length){ | 
 | 410 | 			/* receive second half of a record */ | 
 | 411 | 			iucv_data_count = priv->residual_length; | 
 | 412 | 			user_data_count = 0; | 
 | 413 | 			buffer = priv->buffer; | 
 | 414 | 		} else { | 
 | 415 | 			/* receive a new record: | 
 | 416 | 			 * We need to return the total length of the record | 
 | 417 |                          * + size of FENCE in the first 4 bytes of the buffer. | 
 | 418 | 		         */ | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 419 | 			iucv_data_count = priv->local_interrupt_buffer.length; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | 			user_data_count = sizeof(int); | 
 | 421 | 			temp = (int*)priv->buffer; | 
 | 422 | 			*temp= iucv_data_count + sizeof(FENCE); | 
 | 423 | 			buffer = priv->buffer + sizeof(int); | 
 | 424 | 		} | 
 | 425 | 		/* | 
| Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 426 | 		 * If the record is bigger than our buffer, we receive only | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | 		 * a part of it. We can get the rest later. | 
 | 428 | 		 */ | 
 | 429 | 		if (iucv_data_count > NET_BUFFER_SIZE) | 
 | 430 | 			iucv_data_count = NET_BUFFER_SIZE; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 431 | 		rc = iucv_message_receive(priv->path, | 
 | 432 | 					  &priv->local_interrupt_buffer, | 
 | 433 | 					  0, buffer, iucv_data_count, | 
 | 434 | 					  &priv->residual_length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | 		spin_unlock_bh(&priv->priv_lock); | 
| Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 436 | 		/* An rc of 5 indicates that the record was bigger than | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | 		 * the buffer, which is OK for us. A 9 indicates that the | 
 | 438 | 		 * record was purged befor we could receive it. | 
 | 439 | 		 */ | 
 | 440 | 		if (rc == 5) | 
 | 441 | 			rc = 0; | 
 | 442 | 		if (rc == 9) | 
 | 443 | 			atomic_set(&priv->receive_ready, 0); | 
 | 444 | 	} else { | 
 | 445 | 		rc = 1; | 
 | 446 | 	} | 
 | 447 | 	if (!rc) { | 
 | 448 | 		priv->buffer_free = 0; | 
 | 449 |  		user_data_count += iucv_data_count; | 
 | 450 | 		priv->current_position = priv->buffer; | 
 | 451 | 		if (priv->residual_length == 0){ | 
 | 452 | 			/* the whole record has been captured, | 
 | 453 | 			 * now add the fence */ | 
 | 454 | 			atomic_dec(&priv->receive_ready); | 
 | 455 | 			buffer = priv->buffer + user_data_count; | 
 | 456 | 			memcpy(buffer, FENCE, sizeof(FENCE)); | 
 | 457 | 			user_data_count += sizeof(FENCE); | 
 | 458 | 		} | 
 | 459 | 		priv->remaining = user_data_count; | 
 | 460 | 	} | 
 | 461 |  | 
 | 462 | 	return rc; | 
 | 463 | } | 
 | 464 |  | 
 | 465 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 466 | static ssize_t vmlogrdr_read(struct file *filp, char __user *data, | 
 | 467 | 			     size_t count, loff_t * ppos) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { | 
 | 469 | 	int rc; | 
 | 470 | 	struct vmlogrdr_priv_t * priv = filp->private_data; | 
 | 471 |  | 
 | 472 | 	while (priv->buffer_free) { | 
 | 473 | 		rc = vmlogrdr_receive_data(priv); | 
 | 474 | 		if (rc) { | 
 | 475 | 			rc = wait_event_interruptible(read_wait_queue, | 
 | 476 | 					atomic_read(&priv->receive_ready)); | 
 | 477 | 			if (rc) | 
 | 478 | 				return rc; | 
 | 479 | 		} | 
 | 480 | 	} | 
 | 481 | 	/* copy only up to end of record */ | 
 | 482 | 	if (count > priv->remaining) | 
 | 483 | 		count = priv->remaining; | 
 | 484 |  | 
 | 485 | 	if (copy_to_user(data, priv->current_position, count)) | 
 | 486 | 		return -EFAULT; | 
 | 487 |  | 
 | 488 | 	*ppos += count; | 
 | 489 | 	priv->current_position += count; | 
 | 490 | 	priv->remaining -= count; | 
 | 491 |  | 
 | 492 | 	/* if all data has been transferred, set buffer free */ | 
 | 493 | 	if (priv->remaining == 0) | 
 | 494 | 		priv->buffer_free = 1; | 
 | 495 |  | 
 | 496 | 	return count; | 
 | 497 | } | 
 | 498 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 499 | static ssize_t vmlogrdr_autopurge_store(struct device * dev, | 
 | 500 | 					struct device_attribute *attr, | 
 | 501 | 					const char * buf, size_t count) | 
 | 502 | { | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 503 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | 	ssize_t ret = count; | 
 | 505 |  | 
 | 506 | 	switch (buf[0]) { | 
 | 507 | 	case '0': | 
 | 508 | 		priv->autopurge=0; | 
 | 509 | 		break; | 
 | 510 | 	case '1': | 
 | 511 | 		priv->autopurge=1; | 
 | 512 | 		break; | 
 | 513 | 	default: | 
 | 514 | 		ret = -EINVAL; | 
 | 515 | 	} | 
 | 516 | 	return ret; | 
 | 517 | } | 
 | 518 |  | 
 | 519 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 520 | static ssize_t vmlogrdr_autopurge_show(struct device *dev, | 
 | 521 | 				       struct device_attribute *attr, | 
 | 522 | 				       char *buf) | 
 | 523 | { | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 524 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | 	return sprintf(buf, "%u\n", priv->autopurge); | 
 | 526 | } | 
 | 527 |  | 
 | 528 |  | 
 | 529 | static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show, | 
 | 530 | 		   vmlogrdr_autopurge_store); | 
 | 531 |  | 
 | 532 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 533 | static ssize_t vmlogrdr_purge_store(struct device * dev, | 
 | 534 | 				    struct device_attribute *attr, | 
 | 535 | 				    const char * buf, size_t count) | 
 | 536 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 |  | 
 | 538 | 	char cp_command[80]; | 
 | 539 | 	char cp_response[80]; | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 540 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 |  | 
 | 542 | 	if (buf[0] != '1') | 
 | 543 | 		return -EINVAL; | 
 | 544 |  | 
 | 545 | 	memset(cp_command, 0x00, sizeof(cp_command)); | 
 | 546 | 	memset(cp_response, 0x00, sizeof(cp_response)); | 
 | 547 |  | 
 | 548 |         /* | 
 | 549 | 	 * The recording command needs to be called with option QID | 
 | 550 | 	 * for guests that have previlege classes A or B. | 
 | 551 | 	 * Other guests will not recognize the command and we have to | 
 | 552 | 	 * issue the same command without the QID parameter. | 
 | 553 | 	 */ | 
 | 554 |  | 
 | 555 | 	if (recording_class_AB) | 
 | 556 | 		snprintf(cp_command, sizeof(cp_command), | 
 | 557 | 			 "RECORDING %s PURGE QID * ", | 
 | 558 | 			 priv->recording_name); | 
 | 559 | 	else | 
 | 560 | 		snprintf(cp_command, sizeof(cp_command), | 
 | 561 | 			 "RECORDING %s PURGE ", | 
 | 562 | 			 priv->recording_name); | 
 | 563 |  | 
| Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 564 | 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 |  | 
 | 566 | 	return count; | 
 | 567 | } | 
 | 568 |  | 
 | 569 |  | 
 | 570 | static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store); | 
 | 571 |  | 
 | 572 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 573 | static ssize_t vmlogrdr_autorecording_store(struct device *dev, | 
 | 574 | 					    struct device_attribute *attr, | 
 | 575 | 					    const char *buf, size_t count) | 
 | 576 | { | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 577 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | 	ssize_t ret = count; | 
 | 579 |  | 
 | 580 | 	switch (buf[0]) { | 
 | 581 | 	case '0': | 
 | 582 | 		priv->autorecording=0; | 
 | 583 | 		break; | 
 | 584 | 	case '1': | 
 | 585 | 		priv->autorecording=1; | 
 | 586 | 		break; | 
 | 587 | 	default: | 
 | 588 | 		ret = -EINVAL; | 
 | 589 | 	} | 
 | 590 | 	return ret; | 
 | 591 | } | 
 | 592 |  | 
 | 593 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 594 | static ssize_t vmlogrdr_autorecording_show(struct device *dev, | 
 | 595 | 					   struct device_attribute *attr, | 
 | 596 | 					   char *buf) | 
 | 597 | { | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 598 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | 	return sprintf(buf, "%u\n", priv->autorecording); | 
 | 600 | } | 
 | 601 |  | 
 | 602 |  | 
 | 603 | static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show, | 
 | 604 | 		   vmlogrdr_autorecording_store); | 
 | 605 |  | 
 | 606 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 607 | static ssize_t vmlogrdr_recording_store(struct device * dev, | 
 | 608 | 					struct device_attribute *attr, | 
 | 609 | 					const char * buf, size_t count) | 
 | 610 | { | 
| Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 611 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | 	ssize_t ret; | 
 | 613 |  | 
 | 614 | 	switch (buf[0]) { | 
 | 615 | 	case '0': | 
 | 616 | 		ret = vmlogrdr_recording(priv,0,0); | 
 | 617 | 		break; | 
 | 618 | 	case '1': | 
 | 619 | 		ret = vmlogrdr_recording(priv,1,0); | 
 | 620 | 		break; | 
 | 621 | 	default: | 
 | 622 | 		ret = -EINVAL; | 
 | 623 | 	} | 
 | 624 | 	if (ret) | 
 | 625 | 		return ret; | 
 | 626 | 	else | 
 | 627 | 		return count; | 
 | 628 |  | 
 | 629 | } | 
 | 630 |  | 
 | 631 |  | 
 | 632 | static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store); | 
 | 633 |  | 
 | 634 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 635 | static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver, | 
 | 636 | 					      char *buf) | 
 | 637 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
 | 639 | 	char cp_command[] = "QUERY RECORDING "; | 
 | 640 | 	int len; | 
 | 641 |  | 
| Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 642 | 	cpcmd(cp_command, buf, 4096, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | 	len = strlen(buf); | 
 | 644 | 	return len; | 
 | 645 | } | 
 | 646 |  | 
 | 647 |  | 
 | 648 | static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show, | 
 | 649 | 		   NULL); | 
 | 650 |  | 
 | 651 | static struct attribute *vmlogrdr_attrs[] = { | 
 | 652 | 	&dev_attr_autopurge.attr, | 
 | 653 | 	&dev_attr_purge.attr, | 
 | 654 | 	&dev_attr_autorecording.attr, | 
 | 655 | 	&dev_attr_recording.attr, | 
 | 656 | 	NULL, | 
 | 657 | }; | 
 | 658 |  | 
| Stefan Weinhuber | 9f62fa1 | 2009-06-16 10:30:38 +0200 | [diff] [blame] | 659 | static int vmlogrdr_pm_prepare(struct device *dev) | 
 | 660 | { | 
 | 661 | 	int rc; | 
| Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 662 | 	struct vmlogrdr_priv_t *priv = dev_get_drvdata(dev); | 
| Stefan Weinhuber | 9f62fa1 | 2009-06-16 10:30:38 +0200 | [diff] [blame] | 663 |  | 
 | 664 | 	rc = 0; | 
 | 665 | 	if (priv) { | 
 | 666 | 		spin_lock_bh(&priv->priv_lock); | 
 | 667 | 		if (priv->dev_in_use) | 
 | 668 | 			rc = -EBUSY; | 
 | 669 | 		spin_unlock_bh(&priv->priv_lock); | 
 | 670 | 	} | 
 | 671 | 	if (rc) | 
 | 672 | 		pr_err("vmlogrdr: device %s is busy. Refuse to suspend.\n", | 
 | 673 | 		       dev_name(dev)); | 
 | 674 | 	return rc; | 
 | 675 | } | 
 | 676 |  | 
 | 677 |  | 
| Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 678 | static const struct dev_pm_ops vmlogrdr_pm_ops = { | 
| Stefan Weinhuber | 9f62fa1 | 2009-06-16 10:30:38 +0200 | [diff] [blame] | 679 | 	.prepare = vmlogrdr_pm_prepare, | 
 | 680 | }; | 
 | 681 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | static struct attribute_group vmlogrdr_attr_group = { | 
 | 683 | 	.attrs = vmlogrdr_attrs, | 
 | 684 | }; | 
 | 685 |  | 
| gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 686 | static struct class *vmlogrdr_class; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | static struct device_driver vmlogrdr_driver = { | 
 | 688 | 	.name = "vmlogrdr", | 
 | 689 | 	.bus  = &iucv_bus, | 
| Stefan Weinhuber | 9f62fa1 | 2009-06-16 10:30:38 +0200 | [diff] [blame] | 690 | 	.pm = &vmlogrdr_pm_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | }; | 
 | 692 |  | 
 | 693 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 694 | static int vmlogrdr_register_driver(void) | 
 | 695 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | 	int ret; | 
 | 697 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 698 | 	/* Register with iucv driver */ | 
 | 699 | 	ret = iucv_register(&vmlogrdr_iucv_handler, 1); | 
| Martin Schwidefsky | 2f6f252 | 2008-07-14 09:59:37 +0200 | [diff] [blame] | 700 | 	if (ret) | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 701 | 		goto out; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 702 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | 	ret = driver_register(&vmlogrdr_driver); | 
| Martin Schwidefsky | 2f6f252 | 2008-07-14 09:59:37 +0200 | [diff] [blame] | 704 | 	if (ret) | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 705 | 		goto out_iucv; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 |  | 
 | 707 | 	ret = driver_create_file(&vmlogrdr_driver, | 
 | 708 | 				 &driver_attr_recording_status); | 
| Martin Schwidefsky | 2f6f252 | 2008-07-14 09:59:37 +0200 | [diff] [blame] | 709 | 	if (ret) | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 710 | 		goto out_driver; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 |  | 
| gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 712 | 	vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | 	if (IS_ERR(vmlogrdr_class)) { | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 714 | 		ret = PTR_ERR(vmlogrdr_class); | 
 | 715 | 		vmlogrdr_class = NULL; | 
 | 716 | 		goto out_attr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | 	} | 
 | 718 | 	return 0; | 
 | 719 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 720 | out_attr: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | 	driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 722 | out_driver: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | 	driver_unregister(&vmlogrdr_driver); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 724 | out_iucv: | 
 | 725 | 	iucv_unregister(&vmlogrdr_iucv_handler, 1); | 
 | 726 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | 	return ret; | 
 | 728 | } | 
 | 729 |  | 
 | 730 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 731 | static void vmlogrdr_unregister_driver(void) | 
 | 732 | { | 
| gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 733 | 	class_destroy(vmlogrdr_class); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | 	vmlogrdr_class = NULL; | 
 | 735 | 	driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status); | 
 | 736 | 	driver_unregister(&vmlogrdr_driver); | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 737 | 	iucv_unregister(&vmlogrdr_iucv_handler, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } | 
 | 739 |  | 
 | 740 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 741 | static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv) | 
 | 742 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | 	struct device *dev; | 
 | 744 | 	int ret; | 
 | 745 |  | 
| Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 746 | 	dev = kzalloc(sizeof(struct device), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | 	if (dev) { | 
| Cornelia Huck | 1bf5b28 | 2008-10-10 21:33:10 +0200 | [diff] [blame] | 748 | 		dev_set_name(dev, priv->internal_name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | 		dev->bus = &iucv_bus; | 
 | 750 | 		dev->parent = iucv_root; | 
 | 751 | 		dev->driver = &vmlogrdr_driver; | 
| Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 752 | 		dev_set_drvdata(dev, priv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | 		/* | 
 | 754 | 		 * The release function could be called after the | 
 | 755 | 		 * module has been unloaded. It's _only_ task is to | 
 | 756 | 		 * free the struct. Therefore, we specify kfree() | 
 | 757 | 		 * directly here. (Probably a little bit obfuscating | 
 | 758 | 		 * but legitime ...). | 
 | 759 | 		 */ | 
 | 760 | 		dev->release = (void (*)(struct device *))kfree; | 
 | 761 | 	} else | 
 | 762 | 		return -ENOMEM; | 
 | 763 | 	ret = device_register(dev); | 
| Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 764 | 	if (ret) { | 
 | 765 | 		put_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | 		return ret; | 
| Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 767 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 |  | 
 | 769 | 	ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group); | 
 | 770 | 	if (ret) { | 
 | 771 | 		device_unregister(dev); | 
 | 772 | 		return ret; | 
 | 773 | 	} | 
| Greg Kroah-Hartman | ea9e42f | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 774 | 	priv->class_device = device_create(vmlogrdr_class, dev, | 
 | 775 | 					   MKDEV(vmlogrdr_major, | 
 | 776 | 						 priv->minor_num), | 
 | 777 | 					   priv, "%s", dev_name(dev)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | 	if (IS_ERR(priv->class_device)) { | 
 | 779 | 		ret = PTR_ERR(priv->class_device); | 
 | 780 | 		priv->class_device=NULL; | 
 | 781 | 		sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group); | 
 | 782 | 		device_unregister(dev); | 
 | 783 | 		return ret; | 
 | 784 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | 	priv->device = dev; | 
 | 786 | 	return 0; | 
 | 787 | } | 
 | 788 |  | 
 | 789 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 790 | static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv) | 
 | 791 | { | 
| Cornelia Huck | 7f021ce | 2007-10-22 12:52:42 +0200 | [diff] [blame] | 792 | 	device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | 	if (priv->device != NULL) { | 
 | 794 | 		sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group); | 
 | 795 | 		device_unregister(priv->device); | 
 | 796 | 		priv->device=NULL; | 
 | 797 | 	} | 
 | 798 | 	return 0; | 
 | 799 | } | 
 | 800 |  | 
 | 801 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 802 | static int vmlogrdr_register_cdev(dev_t dev) | 
 | 803 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | 	int rc = 0; | 
 | 805 | 	vmlogrdr_cdev = cdev_alloc(); | 
 | 806 | 	if (!vmlogrdr_cdev) { | 
 | 807 | 		return -ENOMEM; | 
 | 808 | 	} | 
 | 809 | 	vmlogrdr_cdev->owner = THIS_MODULE; | 
 | 810 | 	vmlogrdr_cdev->ops = &vmlogrdr_fops; | 
 | 811 | 	vmlogrdr_cdev->dev = dev; | 
 | 812 | 	rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR); | 
 | 813 | 	if (!rc) | 
 | 814 | 		return 0; | 
 | 815 |  | 
 | 816 | 	// cleanup: cdev is not fully registered, no cdev_del here! | 
 | 817 | 	kobject_put(&vmlogrdr_cdev->kobj); | 
 | 818 | 	vmlogrdr_cdev=NULL; | 
 | 819 | 	return rc; | 
 | 820 | } | 
 | 821 |  | 
 | 822 |  | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 823 | static void vmlogrdr_cleanup(void) | 
 | 824 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 |         int i; | 
| Martin Schwidefsky | c9101c5 | 2007-02-08 13:40:41 -0800 | [diff] [blame] | 826 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | 	if (vmlogrdr_cdev) { | 
 | 828 | 		cdev_del(vmlogrdr_cdev); | 
 | 829 | 		vmlogrdr_cdev=NULL; | 
 | 830 | 	} | 
 | 831 | 	for (i=0; i < MAXMINOR; ++i ) { | 
 | 832 | 		vmlogrdr_unregister_device(&sys_ser[i]); | 
 | 833 | 		free_page((unsigned long)sys_ser[i].buffer); | 
 | 834 | 	} | 
 | 835 | 	vmlogrdr_unregister_driver(); | 
 | 836 | 	if (vmlogrdr_major) { | 
 | 837 | 		unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR); | 
 | 838 | 		vmlogrdr_major=0; | 
 | 839 | 	} | 
 | 840 | } | 
 | 841 |  | 
 | 842 |  | 
| Christian Borntraeger | f60d891 | 2007-07-10 11:24:22 +0200 | [diff] [blame] | 843 | static int __init vmlogrdr_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { | 
 | 845 | 	int rc; | 
 | 846 | 	int i; | 
 | 847 | 	dev_t dev; | 
 | 848 |  | 
 | 849 | 	if (! MACHINE_IS_VM) { | 
| Martin Schwidefsky | 5466c2e | 2008-12-25 13:39:52 +0100 | [diff] [blame] | 850 | 		pr_err("not running under VM, driver not loaded.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | 		return -ENODEV; | 
 | 852 | 	} | 
 | 853 |  | 
 | 854 |         recording_class_AB = vmlogrdr_get_recording_class_AB(); | 
 | 855 |  | 
 | 856 | 	rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr"); | 
 | 857 | 	if (rc) | 
 | 858 | 		return rc; | 
 | 859 | 	vmlogrdr_major = MAJOR(dev); | 
 | 860 |  | 
 | 861 | 	rc=vmlogrdr_register_driver(); | 
 | 862 | 	if (rc) | 
 | 863 | 		goto cleanup; | 
 | 864 |  | 
 | 865 | 	for (i=0; i < MAXMINOR; ++i ) { | 
 | 866 | 		sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL); | 
 | 867 | 		if (!sys_ser[i].buffer) { | 
| Marcin Slusarz | 3cb2cea | 2008-05-15 16:52:32 +0200 | [diff] [blame] | 868 | 			rc = -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | 			break; | 
 | 870 | 		} | 
 | 871 | 		sys_ser[i].current_position = sys_ser[i].buffer; | 
 | 872 | 		rc=vmlogrdr_register_device(&sys_ser[i]); | 
 | 873 | 		if (rc) | 
 | 874 | 			break; | 
 | 875 | 	} | 
 | 876 | 	if (rc) | 
 | 877 | 		goto cleanup; | 
 | 878 |  | 
 | 879 | 	rc = vmlogrdr_register_cdev(dev); | 
 | 880 | 	if (rc) | 
 | 881 | 		goto cleanup; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | 	return 0; | 
 | 883 |  | 
 | 884 | cleanup: | 
 | 885 | 	vmlogrdr_cleanup(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | 	return rc; | 
 | 887 | } | 
 | 888 |  | 
 | 889 |  | 
| Christian Borntraeger | f60d891 | 2007-07-10 11:24:22 +0200 | [diff] [blame] | 890 | static void __exit vmlogrdr_exit(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { | 
 | 892 | 	vmlogrdr_cleanup(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | 	return; | 
 | 894 | } | 
 | 895 |  | 
 | 896 |  | 
 | 897 | module_init(vmlogrdr_init); | 
 | 898 | module_exit(vmlogrdr_exit); |