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