blob: a86c0534cd49f337dbceb8ef2dab3ac2e0115688 [file] [log] [blame]
Melissa Howland31b58082006-09-20 15:59:34 +02001/*
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 Howlandcbea66d2007-08-10 14:32:35 +020020#include <linux/mutex.h>
Melissa Howland31b58082006-09-20 15:59:34 +020021#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 Howland524a2372006-12-28 00:35:25 +010027#define MONWRITE_MAX_DATALEN 4010
Melissa Howland31b58082006-09-20 15:59:34 +020028
29static int mon_max_bufs = 255;
Melissa Howland2d103d52006-10-06 16:38:26 +020030static int mon_buf_count;
Melissa Howland31b58082006-09-20 15:59:34 +020031
32struct mon_buf {
33 struct list_head list;
34 struct monwrite_hdr hdr;
35 int diag_done;
36 char *data;
37};
38
39struct 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 Howlandcbea66d2007-08-10 14:32:35 +020045 struct mutex thread_mutex;
Melissa Howland31b58082006-09-20 15:59:34 +020046};
47
48/*
49 * helper functions
50 */
51
52static 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 Carstens4d284ca2007-02-05 21:18:53 +010072static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
73 struct monwrite_hdr *monhdr)
Melissa Howland31b58082006-09-20 15:59:34 +020074{
75 struct mon_buf *entry, *next;
76
77 list_for_each_entry_safe(entry, next, &monpriv->list, list)
Melissa Howland2c919712006-10-18 18:30:49 +020078 if ((entry->hdr.mon_function == monhdr->mon_function ||
79 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
80 entry->hdr.applid == monhdr->applid &&
Melissa Howland31b58082006-09-20 15:59:34 +020081 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 Howland2c919712006-10-18 18:30:49 +020086
Melissa Howland31b58082006-09-20 15:59:34 +020087 return NULL;
88}
89
90static 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 Howland2c919712006-10-18 18:30:49 +0200100 monbuf = NULL;
101 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
102 monbuf = monwrite_find_hdr(monpriv, monhdr);
Melissa Howland31b58082006-09-20 15:59:34 +0200103 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 Howland2d103d52006-10-06 16:38:26 +0200109 mon_buf_count--;
Melissa Howland31b58082006-09-20 15:59:34 +0200110 kfree(monbuf->data);
111 kfree(monbuf);
112 monbuf = NULL;
113 }
Melissa Howland2c919712006-10-18 18:30:49 +0200114 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
Melissa Howland2d103d52006-10-06 16:38:26 +0200115 if (mon_buf_count >= mon_max_bufs)
Melissa Howland31b58082006-09-20 15:59:34 +0200116 return -ENOSPC;
117 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
118 if (!monbuf)
119 return -ENOMEM;
Melissa Howland715d8542006-10-11 15:31:34 +0200120 monbuf->data = kzalloc(monhdr->datalen,
Melissa Howland31b58082006-09-20 15:59:34 +0200121 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 Howland2c919712006-10-18 18:30:49 +0200128 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
129 mon_buf_count++;
Melissa Howland31b58082006-09-20 15:59:34 +0200130 }
131 monpriv->current_buf = monbuf;
132 return 0;
133}
134
135static 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
175static 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 Howlandcbea66d2007-08-10 14:32:35 +0200184 mutex_init(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200185 filp->private_data = monpriv;
186 return nonseekable_open(inode, filp);
187}
188
189static 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 Howland2d103d52006-10-06 16:38:26 +0200198 mon_buf_count--;
Melissa Howland31b58082006-09-20 15:59:34 +0200199 list_del(&entry->list);
200 kfree(entry->data);
201 kfree(entry);
202 }
203 kfree(monpriv);
204 return 0;
205}
206
207static 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 Howlandcbea66d2007-08-10 14:32:35 +0200215 mutex_lock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200216 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 Howlandcbea66d2007-08-10 14:32:35 +0200254 mutex_unlock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200255 return written;
256
257out_error:
258 monpriv->data_to_read = 0;
259 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
Melissa Howlandcbea66d2007-08-10 14:32:35 +0200260 mutex_unlock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200261 return rc;
262}
263
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800264static const struct file_operations monwrite_fops = {
Melissa Howland31b58082006-09-20 15:59:34 +0200265 .owner = THIS_MODULE,
266 .open = &monwrite_open,
267 .release = &monwrite_close,
268 .write = &monwrite_write,
269};
270
271static 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
281static int __init mon_init(void)
282{
283 if (MACHINE_IS_VM)
284 return misc_register(&mon_dev);
285 else
286 return -ENODEV;
287}
288
289static void __exit mon_exit(void)
290{
291 WARN_ON(misc_deregister(&mon_dev) != 0);
292}
293
294module_init(mon_init);
295module_exit(mon_exit);
296
297module_param_named(max_bufs, mon_max_bufs, int, 0644);
Joe Perchesceb3dfb2008-01-26 14:11:10 +0100298MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
Melissa Howland31b58082006-09-20 15:59:34 +0200299 "that can be active at one time");
300
301MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
302MODULE_DESCRIPTION("Character device driver for writing z/VM "
303 "APPLDATA monitor records.");
304MODULE_LICENSE("GPL");