blob: 4d71aa8c1a7956928e1354cd82f56a70d145c1e2 [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>
Arnd Bergmanndca67e92008-05-20 19:16:18 +020015#include <linux/smp_lock.h>
Melissa Howland31b58082006-09-20 15:59:34 +020016#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/miscdevice.h>
19#include <linux/ctype.h>
20#include <linux/poll.h>
Melissa Howlandcbea66d2007-08-10 14:32:35 +020021#include <linux/mutex.h>
Melissa Howland31b58082006-09-20 15:59:34 +020022#include <asm/uaccess.h>
23#include <asm/ebcdic.h>
24#include <asm/io.h>
25#include <asm/appldata.h>
26#include <asm/monwriter.h>
27
Melissa Howland524a2372006-12-28 00:35:25 +010028#define MONWRITE_MAX_DATALEN 4010
Melissa Howland31b58082006-09-20 15:59:34 +020029
30static int mon_max_bufs = 255;
Melissa Howland2d103d52006-10-06 16:38:26 +020031static int mon_buf_count;
Melissa Howland31b58082006-09-20 15:59:34 +020032
33struct mon_buf {
34 struct list_head list;
35 struct monwrite_hdr hdr;
36 int diag_done;
37 char *data;
38};
39
40struct mon_private {
41 struct list_head list;
42 struct monwrite_hdr hdr;
43 size_t hdr_to_read;
44 size_t data_to_read;
45 struct mon_buf *current_buf;
Melissa Howlandcbea66d2007-08-10 14:32:35 +020046 struct mutex thread_mutex;
Melissa Howland31b58082006-09-20 15:59:34 +020047};
48
49/*
50 * helper functions
51 */
52
53static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
54{
55 struct appldata_product_id id;
56 int rc;
57
58 strcpy(id.prod_nr, "LNXAPPL");
59 id.prod_fn = myhdr->applid;
60 id.record_nr = myhdr->record_num;
61 id.version_nr = myhdr->version;
62 id.release_nr = myhdr->release;
63 id.mod_lvl = myhdr->mod_level;
64 rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
65 if (rc <= 0)
66 return rc;
67 if (rc == 5)
68 return -EPERM;
69 printk("DIAG X'DC' error with return code: %i\n", rc);
70 return -EINVAL;
71}
72
Heiko Carstens4d284ca2007-02-05 21:18:53 +010073static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
74 struct monwrite_hdr *monhdr)
Melissa Howland31b58082006-09-20 15:59:34 +020075{
76 struct mon_buf *entry, *next;
77
78 list_for_each_entry_safe(entry, next, &monpriv->list, list)
Melissa Howland2c919712006-10-18 18:30:49 +020079 if ((entry->hdr.mon_function == monhdr->mon_function ||
80 monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
81 entry->hdr.applid == monhdr->applid &&
Melissa Howland31b58082006-09-20 15:59:34 +020082 entry->hdr.record_num == monhdr->record_num &&
83 entry->hdr.version == monhdr->version &&
84 entry->hdr.release == monhdr->release &&
85 entry->hdr.mod_level == monhdr->mod_level)
86 return entry;
Melissa Howland2c919712006-10-18 18:30:49 +020087
Melissa Howland31b58082006-09-20 15:59:34 +020088 return NULL;
89}
90
91static int monwrite_new_hdr(struct mon_private *monpriv)
92{
93 struct monwrite_hdr *monhdr = &monpriv->hdr;
94 struct mon_buf *monbuf;
95 int rc;
96
97 if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
98 monhdr->mon_function > MONWRITE_START_CONFIG ||
99 monhdr->hdrlen != sizeof(struct monwrite_hdr))
100 return -EINVAL;
Melissa Howland2c919712006-10-18 18:30:49 +0200101 monbuf = NULL;
102 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
103 monbuf = monwrite_find_hdr(monpriv, monhdr);
Melissa Howland31b58082006-09-20 15:59:34 +0200104 if (monbuf) {
105 if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
106 monhdr->datalen = monbuf->hdr.datalen;
107 rc = monwrite_diag(monhdr, monbuf->data,
108 APPLDATA_STOP_REC);
109 list_del(&monbuf->list);
Melissa Howland2d103d52006-10-06 16:38:26 +0200110 mon_buf_count--;
Melissa Howland31b58082006-09-20 15:59:34 +0200111 kfree(monbuf->data);
112 kfree(monbuf);
113 monbuf = NULL;
114 }
Melissa Howland2c919712006-10-18 18:30:49 +0200115 } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
Melissa Howland2d103d52006-10-06 16:38:26 +0200116 if (mon_buf_count >= mon_max_bufs)
Melissa Howland31b58082006-09-20 15:59:34 +0200117 return -ENOSPC;
118 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
119 if (!monbuf)
120 return -ENOMEM;
Melissa Howland715d8542006-10-11 15:31:34 +0200121 monbuf->data = kzalloc(monhdr->datalen,
Melissa Howland31b58082006-09-20 15:59:34 +0200122 GFP_KERNEL | GFP_DMA);
123 if (!monbuf->data) {
124 kfree(monbuf);
125 return -ENOMEM;
126 }
127 monbuf->hdr = *monhdr;
128 list_add_tail(&monbuf->list, &monpriv->list);
Melissa Howland2c919712006-10-18 18:30:49 +0200129 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
130 mon_buf_count++;
Melissa Howland31b58082006-09-20 15:59:34 +0200131 }
132 monpriv->current_buf = monbuf;
133 return 0;
134}
135
136static int monwrite_new_data(struct mon_private *monpriv)
137{
138 struct monwrite_hdr *monhdr = &monpriv->hdr;
139 struct mon_buf *monbuf = monpriv->current_buf;
140 int rc = 0;
141
142 switch (monhdr->mon_function) {
143 case MONWRITE_START_INTERVAL:
144 if (!monbuf->diag_done) {
145 rc = monwrite_diag(monhdr, monbuf->data,
146 APPLDATA_START_INTERVAL_REC);
147 monbuf->diag_done = 1;
148 }
149 break;
150 case MONWRITE_START_CONFIG:
151 if (!monbuf->diag_done) {
152 rc = monwrite_diag(monhdr, monbuf->data,
153 APPLDATA_START_CONFIG_REC);
154 monbuf->diag_done = 1;
155 }
156 break;
157 case MONWRITE_GEN_EVENT:
158 rc = monwrite_diag(monhdr, monbuf->data,
159 APPLDATA_GEN_EVENT_REC);
160 list_del(&monpriv->current_buf->list);
161 kfree(monpriv->current_buf->data);
162 kfree(monpriv->current_buf);
163 monpriv->current_buf = NULL;
164 break;
165 default:
166 /* monhdr->mon_function is checked in monwrite_new_hdr */
167 BUG();
168 }
169 return rc;
170}
171
172/*
173 * file operations
174 */
175
176static int monwrite_open(struct inode *inode, struct file *filp)
177{
178 struct mon_private *monpriv;
179
180 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
181 if (!monpriv)
182 return -ENOMEM;
Arnd Bergmanndca67e92008-05-20 19:16:18 +0200183 lock_kernel();
Melissa Howland31b58082006-09-20 15:59:34 +0200184 INIT_LIST_HEAD(&monpriv->list);
185 monpriv->hdr_to_read = sizeof(monpriv->hdr);
Melissa Howlandcbea66d2007-08-10 14:32:35 +0200186 mutex_init(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200187 filp->private_data = monpriv;
Arnd Bergmanndca67e92008-05-20 19:16:18 +0200188 unlock_kernel();
Melissa Howland31b58082006-09-20 15:59:34 +0200189 return nonseekable_open(inode, filp);
190}
191
192static int monwrite_close(struct inode *inode, struct file *filp)
193{
194 struct mon_private *monpriv = filp->private_data;
195 struct mon_buf *entry, *next;
196
197 list_for_each_entry_safe(entry, next, &monpriv->list, list) {
198 if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
199 monwrite_diag(&entry->hdr, entry->data,
200 APPLDATA_STOP_REC);
Melissa Howland2d103d52006-10-06 16:38:26 +0200201 mon_buf_count--;
Melissa Howland31b58082006-09-20 15:59:34 +0200202 list_del(&entry->list);
203 kfree(entry->data);
204 kfree(entry);
205 }
206 kfree(monpriv);
207 return 0;
208}
209
210static ssize_t monwrite_write(struct file *filp, const char __user *data,
211 size_t count, loff_t *ppos)
212{
213 struct mon_private *monpriv = filp->private_data;
214 size_t len, written;
215 void *to;
216 int rc;
217
Melissa Howlandcbea66d2007-08-10 14:32:35 +0200218 mutex_lock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200219 for (written = 0; written < count; ) {
220 if (monpriv->hdr_to_read) {
221 len = min(count - written, monpriv->hdr_to_read);
222 to = (char *) &monpriv->hdr +
223 sizeof(monpriv->hdr) - monpriv->hdr_to_read;
224 if (copy_from_user(to, data + written, len)) {
225 rc = -EFAULT;
226 goto out_error;
227 }
228 monpriv->hdr_to_read -= len;
229 written += len;
230 if (monpriv->hdr_to_read > 0)
231 continue;
232 rc = monwrite_new_hdr(monpriv);
233 if (rc)
234 goto out_error;
235 monpriv->data_to_read = monpriv->current_buf ?
236 monpriv->current_buf->hdr.datalen : 0;
237 }
238
239 if (monpriv->data_to_read) {
240 len = min(count - written, monpriv->data_to_read);
241 to = monpriv->current_buf->data +
242 monpriv->hdr.datalen - monpriv->data_to_read;
243 if (copy_from_user(to, data + written, len)) {
244 rc = -EFAULT;
245 goto out_error;
246 }
247 monpriv->data_to_read -= len;
248 written += len;
249 if (monpriv->data_to_read > 0)
250 continue;
251 rc = monwrite_new_data(monpriv);
252 if (rc)
253 goto out_error;
254 }
255 monpriv->hdr_to_read = sizeof(monpriv->hdr);
256 }
Melissa Howlandcbea66d2007-08-10 14:32:35 +0200257 mutex_unlock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200258 return written;
259
260out_error:
261 monpriv->data_to_read = 0;
262 monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
Melissa Howlandcbea66d2007-08-10 14:32:35 +0200263 mutex_unlock(&monpriv->thread_mutex);
Melissa Howland31b58082006-09-20 15:59:34 +0200264 return rc;
265}
266
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800267static const struct file_operations monwrite_fops = {
Melissa Howland31b58082006-09-20 15:59:34 +0200268 .owner = THIS_MODULE,
269 .open = &monwrite_open,
270 .release = &monwrite_close,
271 .write = &monwrite_write,
272};
273
274static struct miscdevice mon_dev = {
275 .name = "monwriter",
276 .fops = &monwrite_fops,
277 .minor = MISC_DYNAMIC_MINOR,
278};
279
280/*
281 * module init/exit
282 */
283
284static int __init mon_init(void)
285{
286 if (MACHINE_IS_VM)
287 return misc_register(&mon_dev);
288 else
289 return -ENODEV;
290}
291
292static void __exit mon_exit(void)
293{
294 WARN_ON(misc_deregister(&mon_dev) != 0);
295}
296
297module_init(mon_init);
298module_exit(mon_exit);
299
300module_param_named(max_bufs, mon_max_bufs, int, 0644);
Joe Perchesceb3dfb2008-01-26 14:11:10 +0100301MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
Melissa Howland31b58082006-09-20 15:59:34 +0200302 "that can be active at one time");
303
304MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
305MODULE_DESCRIPTION("Character device driver for writing z/VM "
306 "APPLDATA monitor records.");
307MODULE_LICENSE("GPL");