Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/char/monwriter.c |
| 3 | * |
| 4 | * Character device driver for writing z/VM *MONITOR service records. |
| 5 | * |
| 6 | * Copyright (C) IBM Corp. 2006 |
| 7 | * |
| 8 | * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/miscdevice.h> |
| 18 | #include <linux/ctype.h> |
| 19 | #include <linux/poll.h> |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/ebcdic.h> |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/appldata.h> |
| 25 | #include <asm/monwriter.h> |
| 26 | |
Melissa Howland | 524a237 | 2006-12-28 00:35:25 +0100 | [diff] [blame] | 27 | #define MONWRITE_MAX_DATALEN 4010 |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 28 | |
| 29 | static int mon_max_bufs = 255; |
Melissa Howland | 2d103d5 | 2006-10-06 16:38:26 +0200 | [diff] [blame] | 30 | static int mon_buf_count; |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 31 | |
| 32 | struct mon_buf { |
| 33 | struct list_head list; |
| 34 | struct monwrite_hdr hdr; |
| 35 | int diag_done; |
| 36 | char *data; |
| 37 | }; |
| 38 | |
| 39 | struct mon_private { |
| 40 | struct list_head list; |
| 41 | struct monwrite_hdr hdr; |
| 42 | size_t hdr_to_read; |
| 43 | size_t data_to_read; |
| 44 | struct mon_buf *current_buf; |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 45 | struct mutex thread_mutex; |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * helper functions |
| 50 | */ |
| 51 | |
| 52 | static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn) |
| 53 | { |
| 54 | struct appldata_product_id id; |
| 55 | int rc; |
| 56 | |
| 57 | strcpy(id.prod_nr, "LNXAPPL"); |
| 58 | id.prod_fn = myhdr->applid; |
| 59 | id.record_nr = myhdr->record_num; |
| 60 | id.version_nr = myhdr->version; |
| 61 | id.release_nr = myhdr->release; |
| 62 | id.mod_lvl = myhdr->mod_level; |
| 63 | rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen); |
| 64 | if (rc <= 0) |
| 65 | return rc; |
| 66 | if (rc == 5) |
| 67 | return -EPERM; |
| 68 | printk("DIAG X'DC' error with return code: %i\n", rc); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 72 | static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv, |
| 73 | struct monwrite_hdr *monhdr) |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 74 | { |
| 75 | struct mon_buf *entry, *next; |
| 76 | |
| 77 | list_for_each_entry_safe(entry, next, &monpriv->list, list) |
Melissa Howland | 2c91971 | 2006-10-18 18:30:49 +0200 | [diff] [blame] | 78 | if ((entry->hdr.mon_function == monhdr->mon_function || |
| 79 | monhdr->mon_function == MONWRITE_STOP_INTERVAL) && |
| 80 | entry->hdr.applid == monhdr->applid && |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 81 | entry->hdr.record_num == monhdr->record_num && |
| 82 | entry->hdr.version == monhdr->version && |
| 83 | entry->hdr.release == monhdr->release && |
| 84 | entry->hdr.mod_level == monhdr->mod_level) |
| 85 | return entry; |
Melissa Howland | 2c91971 | 2006-10-18 18:30:49 +0200 | [diff] [blame] | 86 | |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static int monwrite_new_hdr(struct mon_private *monpriv) |
| 91 | { |
| 92 | struct monwrite_hdr *monhdr = &monpriv->hdr; |
| 93 | struct mon_buf *monbuf; |
| 94 | int rc; |
| 95 | |
| 96 | if (monhdr->datalen > MONWRITE_MAX_DATALEN || |
| 97 | monhdr->mon_function > MONWRITE_START_CONFIG || |
| 98 | monhdr->hdrlen != sizeof(struct monwrite_hdr)) |
| 99 | return -EINVAL; |
Melissa Howland | 2c91971 | 2006-10-18 18:30:49 +0200 | [diff] [blame] | 100 | monbuf = NULL; |
| 101 | if (monhdr->mon_function != MONWRITE_GEN_EVENT) |
| 102 | monbuf = monwrite_find_hdr(monpriv, monhdr); |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 103 | if (monbuf) { |
| 104 | if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) { |
| 105 | monhdr->datalen = monbuf->hdr.datalen; |
| 106 | rc = monwrite_diag(monhdr, monbuf->data, |
| 107 | APPLDATA_STOP_REC); |
| 108 | list_del(&monbuf->list); |
Melissa Howland | 2d103d5 | 2006-10-06 16:38:26 +0200 | [diff] [blame] | 109 | mon_buf_count--; |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 110 | kfree(monbuf->data); |
| 111 | kfree(monbuf); |
| 112 | monbuf = NULL; |
| 113 | } |
Melissa Howland | 2c91971 | 2006-10-18 18:30:49 +0200 | [diff] [blame] | 114 | } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) { |
Melissa Howland | 2d103d5 | 2006-10-06 16:38:26 +0200 | [diff] [blame] | 115 | if (mon_buf_count >= mon_max_bufs) |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 116 | return -ENOSPC; |
| 117 | monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL); |
| 118 | if (!monbuf) |
| 119 | return -ENOMEM; |
Melissa Howland | 715d854 | 2006-10-11 15:31:34 +0200 | [diff] [blame] | 120 | monbuf->data = kzalloc(monhdr->datalen, |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 121 | GFP_KERNEL | GFP_DMA); |
| 122 | if (!monbuf->data) { |
| 123 | kfree(monbuf); |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | monbuf->hdr = *monhdr; |
| 127 | list_add_tail(&monbuf->list, &monpriv->list); |
Melissa Howland | 2c91971 | 2006-10-18 18:30:49 +0200 | [diff] [blame] | 128 | if (monhdr->mon_function != MONWRITE_GEN_EVENT) |
| 129 | mon_buf_count++; |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 130 | } |
| 131 | monpriv->current_buf = monbuf; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int monwrite_new_data(struct mon_private *monpriv) |
| 136 | { |
| 137 | struct monwrite_hdr *monhdr = &monpriv->hdr; |
| 138 | struct mon_buf *monbuf = monpriv->current_buf; |
| 139 | int rc = 0; |
| 140 | |
| 141 | switch (monhdr->mon_function) { |
| 142 | case MONWRITE_START_INTERVAL: |
| 143 | if (!monbuf->diag_done) { |
| 144 | rc = monwrite_diag(monhdr, monbuf->data, |
| 145 | APPLDATA_START_INTERVAL_REC); |
| 146 | monbuf->diag_done = 1; |
| 147 | } |
| 148 | break; |
| 149 | case MONWRITE_START_CONFIG: |
| 150 | if (!monbuf->diag_done) { |
| 151 | rc = monwrite_diag(monhdr, monbuf->data, |
| 152 | APPLDATA_START_CONFIG_REC); |
| 153 | monbuf->diag_done = 1; |
| 154 | } |
| 155 | break; |
| 156 | case MONWRITE_GEN_EVENT: |
| 157 | rc = monwrite_diag(monhdr, monbuf->data, |
| 158 | APPLDATA_GEN_EVENT_REC); |
| 159 | list_del(&monpriv->current_buf->list); |
| 160 | kfree(monpriv->current_buf->data); |
| 161 | kfree(monpriv->current_buf); |
| 162 | monpriv->current_buf = NULL; |
| 163 | break; |
| 164 | default: |
| 165 | /* monhdr->mon_function is checked in monwrite_new_hdr */ |
| 166 | BUG(); |
| 167 | } |
| 168 | return rc; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * file operations |
| 173 | */ |
| 174 | |
| 175 | static int monwrite_open(struct inode *inode, struct file *filp) |
| 176 | { |
| 177 | struct mon_private *monpriv; |
| 178 | |
| 179 | monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL); |
| 180 | if (!monpriv) |
| 181 | return -ENOMEM; |
| 182 | INIT_LIST_HEAD(&monpriv->list); |
| 183 | monpriv->hdr_to_read = sizeof(monpriv->hdr); |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 184 | mutex_init(&monpriv->thread_mutex); |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 185 | filp->private_data = monpriv; |
| 186 | return nonseekable_open(inode, filp); |
| 187 | } |
| 188 | |
| 189 | static int monwrite_close(struct inode *inode, struct file *filp) |
| 190 | { |
| 191 | struct mon_private *monpriv = filp->private_data; |
| 192 | struct mon_buf *entry, *next; |
| 193 | |
| 194 | list_for_each_entry_safe(entry, next, &monpriv->list, list) { |
| 195 | if (entry->hdr.mon_function != MONWRITE_GEN_EVENT) |
| 196 | monwrite_diag(&entry->hdr, entry->data, |
| 197 | APPLDATA_STOP_REC); |
Melissa Howland | 2d103d5 | 2006-10-06 16:38:26 +0200 | [diff] [blame] | 198 | mon_buf_count--; |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 199 | list_del(&entry->list); |
| 200 | kfree(entry->data); |
| 201 | kfree(entry); |
| 202 | } |
| 203 | kfree(monpriv); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static ssize_t monwrite_write(struct file *filp, const char __user *data, |
| 208 | size_t count, loff_t *ppos) |
| 209 | { |
| 210 | struct mon_private *monpriv = filp->private_data; |
| 211 | size_t len, written; |
| 212 | void *to; |
| 213 | int rc; |
| 214 | |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 215 | mutex_lock(&monpriv->thread_mutex); |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 216 | for (written = 0; written < count; ) { |
| 217 | if (monpriv->hdr_to_read) { |
| 218 | len = min(count - written, monpriv->hdr_to_read); |
| 219 | to = (char *) &monpriv->hdr + |
| 220 | sizeof(monpriv->hdr) - monpriv->hdr_to_read; |
| 221 | if (copy_from_user(to, data + written, len)) { |
| 222 | rc = -EFAULT; |
| 223 | goto out_error; |
| 224 | } |
| 225 | monpriv->hdr_to_read -= len; |
| 226 | written += len; |
| 227 | if (monpriv->hdr_to_read > 0) |
| 228 | continue; |
| 229 | rc = monwrite_new_hdr(monpriv); |
| 230 | if (rc) |
| 231 | goto out_error; |
| 232 | monpriv->data_to_read = monpriv->current_buf ? |
| 233 | monpriv->current_buf->hdr.datalen : 0; |
| 234 | } |
| 235 | |
| 236 | if (monpriv->data_to_read) { |
| 237 | len = min(count - written, monpriv->data_to_read); |
| 238 | to = monpriv->current_buf->data + |
| 239 | monpriv->hdr.datalen - monpriv->data_to_read; |
| 240 | if (copy_from_user(to, data + written, len)) { |
| 241 | rc = -EFAULT; |
| 242 | goto out_error; |
| 243 | } |
| 244 | monpriv->data_to_read -= len; |
| 245 | written += len; |
| 246 | if (monpriv->data_to_read > 0) |
| 247 | continue; |
| 248 | rc = monwrite_new_data(monpriv); |
| 249 | if (rc) |
| 250 | goto out_error; |
| 251 | } |
| 252 | monpriv->hdr_to_read = sizeof(monpriv->hdr); |
| 253 | } |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 254 | mutex_unlock(&monpriv->thread_mutex); |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 255 | return written; |
| 256 | |
| 257 | out_error: |
| 258 | monpriv->data_to_read = 0; |
| 259 | monpriv->hdr_to_read = sizeof(struct monwrite_hdr); |
Melissa Howland | cbea66d | 2007-08-10 14:32:35 +0200 | [diff] [blame] | 260 | mutex_unlock(&monpriv->thread_mutex); |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 261 | return rc; |
| 262 | } |
| 263 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 264 | static const struct file_operations monwrite_fops = { |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 265 | .owner = THIS_MODULE, |
| 266 | .open = &monwrite_open, |
| 267 | .release = &monwrite_close, |
| 268 | .write = &monwrite_write, |
| 269 | }; |
| 270 | |
| 271 | static struct miscdevice mon_dev = { |
| 272 | .name = "monwriter", |
| 273 | .fops = &monwrite_fops, |
| 274 | .minor = MISC_DYNAMIC_MINOR, |
| 275 | }; |
| 276 | |
| 277 | /* |
| 278 | * module init/exit |
| 279 | */ |
| 280 | |
| 281 | static int __init mon_init(void) |
| 282 | { |
| 283 | if (MACHINE_IS_VM) |
| 284 | return misc_register(&mon_dev); |
| 285 | else |
| 286 | return -ENODEV; |
| 287 | } |
| 288 | |
| 289 | static void __exit mon_exit(void) |
| 290 | { |
| 291 | WARN_ON(misc_deregister(&mon_dev) != 0); |
| 292 | } |
| 293 | |
| 294 | module_init(mon_init); |
| 295 | module_exit(mon_exit); |
| 296 | |
| 297 | module_param_named(max_bufs, mon_max_bufs, int, 0644); |
Joe Perches | ceb3dfb | 2008-01-26 14:11:10 +0100 | [diff] [blame] | 298 | MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers " |
Melissa Howland | 31b5808 | 2006-09-20 15:59:34 +0200 | [diff] [blame] | 299 | "that can be active at one time"); |
| 300 | |
| 301 | MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>"); |
| 302 | MODULE_DESCRIPTION("Character device driver for writing z/VM " |
| 303 | "APPLDATA monitor records."); |
| 304 | MODULE_LICENSE("GPL"); |