[S390] Cleanup 3215 printk messages.
[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/types.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/ctype.h>
19 #include <linux/poll.h>
20 #include <linux/mutex.h>
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
27 #define MONWRITE_MAX_DATALEN    4010
28
29 static int mon_max_bufs = 255;
30 static int mon_buf_count;
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;
45         struct mutex thread_mutex;
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
72 static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
73                                          struct monwrite_hdr *monhdr)
74 {
75         struct mon_buf *entry, *next;
76
77         list_for_each_entry_safe(entry, next, &monpriv->list, list)
78                 if ((entry->hdr.mon_function == monhdr->mon_function ||
79                      monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
80                     entry->hdr.applid == monhdr->applid &&
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;
86
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;
100         monbuf = NULL;
101         if (monhdr->mon_function != MONWRITE_GEN_EVENT)
102                 monbuf = monwrite_find_hdr(monpriv, monhdr);
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);
109                         mon_buf_count--;
110                         kfree(monbuf->data);
111                         kfree(monbuf);
112                         monbuf = NULL;
113                 }
114         } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
115                 if (mon_buf_count >= mon_max_bufs)
116                         return -ENOSPC;
117                 monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
118                 if (!monbuf)
119                         return -ENOMEM;
120                 monbuf->data = kzalloc(monhdr->datalen,
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);
128                 if (monhdr->mon_function != MONWRITE_GEN_EVENT)
129                         mon_buf_count++;
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);
184         mutex_init(&monpriv->thread_mutex);
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);
198                 mon_buf_count--;
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
215         mutex_lock(&monpriv->thread_mutex);
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         }
254         mutex_unlock(&monpriv->thread_mutex);
255         return written;
256
257 out_error:
258         monpriv->data_to_read = 0;
259         monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
260         mutex_unlock(&monpriv->thread_mutex);
261         return rc;
262 }
263
264 static const struct file_operations monwrite_fops = {
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);
298 MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
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");