Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / drivers / s390 / char / monwriter.c
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/smp_lock.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/ctype.h>
20 #include <linux/poll.h>
21 #include <linux/mutex.h>
22 #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
28 #define MONWRITE_MAX_DATALEN    4010
29
30 static int mon_max_bufs = 255;
31 static int mon_buf_count;
32
33 struct mon_buf {
34         struct list_head list;
35         struct monwrite_hdr hdr;
36         int diag_done;
37         char *data;
38 };
39
40 struct 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;
46         struct mutex thread_mutex;
47 };
48
49 /*
50  * helper functions
51  */
52
53 static 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
73 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
74                                          struct monwrite_hdr *monhdr)
75 {
76         struct mon_buf *entry, *next;
77
78         list_for_each_entry_safe(entry, next, &monpriv->list, list)
79                 if ((entry->hdr.mon_function == monhdr->mon_function ||
80                      monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
81                     entry->hdr.applid == monhdr->applid &&
82                     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;
87
88         return NULL;
89 }
90
91 static 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;
101         monbuf = NULL;
102         if (monhdr->mon_function != MONWRITE_GEN_EVENT)
103                 monbuf = monwrite_find_hdr(monpriv, monhdr);
104         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);
110                         mon_buf_count--;
111                         kfree(monbuf->data);
112                         kfree(monbuf);
113                         monbuf = NULL;
114                 }
115         } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
116                 if (mon_buf_count >= mon_max_bufs)
117                         return -ENOSPC;
118                 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
119                 if (!monbuf)
120                         return -ENOMEM;
121                 monbuf->data = kzalloc(monhdr->datalen,
122                                        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);
129                 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
130                         mon_buf_count++;
131         }
132         monpriv->current_buf = monbuf;
133         return 0;
134 }
135
136 static 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
176 static 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;
183         lock_kernel();
184         INIT_LIST_HEAD(&monpriv->list);
185         monpriv->hdr_to_read = sizeof(monpriv->hdr);
186         mutex_init(&monpriv->thread_mutex);
187         filp->private_data = monpriv;
188         unlock_kernel();
189         return nonseekable_open(inode, filp);
190 }
191
192 static 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);
201                 mon_buf_count--;
202                 list_del(&entry->list);
203                 kfree(entry->data);
204                 kfree(entry);
205         }
206         kfree(monpriv);
207         return 0;
208 }
209
210 static 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
218         mutex_lock(&monpriv->thread_mutex);
219         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         }
257         mutex_unlock(&monpriv->thread_mutex);
258         return written;
259
260 out_error:
261         monpriv->data_to_read = 0;
262         monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
263         mutex_unlock(&monpriv->thread_mutex);
264         return rc;
265 }
266
267 static const struct file_operations monwrite_fops = {
268         .owner   = THIS_MODULE,
269         .open    = &monwrite_open,
270         .release = &monwrite_close,
271         .write   = &monwrite_write,
272 };
273
274 static 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
284 static int __init mon_init(void)
285 {
286         if (MACHINE_IS_VM)
287                 return misc_register(&mon_dev);
288         else
289                 return -ENODEV;
290 }
291
292 static void __exit mon_exit(void)
293 {
294         WARN_ON(misc_deregister(&mon_dev) != 0);
295 }
296
297 module_init(mon_init);
298 module_exit(mon_exit);
299
300 module_param_named(max_bufs, mon_max_bufs, int, 0644);
301 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
302                  "that can be active at one time");
303
304 MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
305 MODULE_DESCRIPTION("Character device driver for writing z/VM "
306                    "APPLDATA monitor records.");
307 MODULE_LICENSE("GPL");