Merge branch 'for-linus' of git://git.alsa-project.org/alsa-kernel
[linux-2.6] / drivers / s390 / char / monreader.c
1 /*
2  * drivers/s390/char/monreader.c
3  *
4  * Character device driver for reading z/VM *MONITOR service records.
5  *
6  *   Copyright IBM Corp. 2004, 2008
7  *   Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
8  */
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/miscdevice.h>
17 #include <linux/ctype.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <linux/poll.h>
21 #include <net/iucv/iucv.h>
22 #include <asm/uaccess.h>
23 #include <asm/ebcdic.h>
24 #include <asm/extmem.h>
25
26 //#define MON_DEBUG                     /* Debug messages on/off */
27
28 #define MON_NAME "monreader"
29
30 #define P_INFO(x...)    printk(KERN_INFO MON_NAME " info: " x)
31 #define P_ERROR(x...)   printk(KERN_ERR MON_NAME " error: " x)
32 #define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
33
34 #ifdef MON_DEBUG
35 #define P_DEBUG(x...)   printk(KERN_DEBUG MON_NAME " debug: " x)
36 #else
37 #define P_DEBUG(x...)   do {} while (0)
38 #endif
39
40 #define MON_COLLECT_SAMPLE 0x80
41 #define MON_COLLECT_EVENT  0x40
42 #define MON_SERVICE        "*MONITOR"
43 #define MON_IN_USE         0x01
44 #define MON_MSGLIM         255
45
46 static char mon_dcss_name[9] = "MONDCSS\0";
47
48 struct mon_msg {
49         u32 pos;
50         u32 mca_offset;
51         struct iucv_message msg;
52         char msglim_reached;
53         char replied_msglim;
54 };
55
56 struct mon_private {
57         struct iucv_path *path;
58         struct mon_msg *msg_array[MON_MSGLIM];
59         unsigned int   write_index;
60         unsigned int   read_index;
61         atomic_t msglim_count;
62         atomic_t read_ready;
63         atomic_t iucv_connected;
64         atomic_t iucv_severed;
65 };
66
67 static unsigned long mon_in_use = 0;
68
69 static unsigned long mon_dcss_start;
70 static unsigned long mon_dcss_end;
71
72 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
73 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
74
75 static u8 user_data_connect[16] = {
76         /* Version code, must be 0x01 for shared mode */
77         0x01,
78         /* what to collect */
79         MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
80         /* DCSS name in EBCDIC, 8 bytes padded with blanks */
81         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82         0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 };
84
85 static u8 user_data_sever[16] = {
86         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 };
89
90
91 /******************************************************************************
92  *                             helper functions                               *
93  *****************************************************************************/
94 /*
95  * Create the 8 bytes EBCDIC DCSS segment name from
96  * an ASCII name, incl. padding
97  */
98 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
99 {
100         int i;
101
102         for (i = 0; i < 8; i++) {
103                 if (ascii_name[i] == '\0')
104                         break;
105                 ebcdic_name[i] = toupper(ascii_name[i]);
106         };
107         for (; i < 8; i++)
108                 ebcdic_name[i] = ' ';
109         ASCEBC(ebcdic_name, 8);
110 }
111
112 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
113 {
114         return *(u32 *) &monmsg->msg.rmmsg;
115 }
116
117 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
118 {
119         return *(u32 *) &monmsg->msg.rmmsg[4];
120 }
121
122 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
123 {
124         return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
125 }
126
127 static inline u32 mon_mca_size(struct mon_msg *monmsg)
128 {
129         return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
130 }
131
132 static inline u32 mon_rec_start(struct mon_msg *monmsg)
133 {
134         return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
135 }
136
137 static inline u32 mon_rec_end(struct mon_msg *monmsg)
138 {
139         return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
140 }
141
142 static int mon_check_mca(struct mon_msg *monmsg)
143 {
144         if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
145             (mon_rec_start(monmsg) < mon_dcss_start) ||
146             (mon_rec_end(monmsg) > mon_dcss_end) ||
147             (mon_mca_type(monmsg, 0) == 0) ||
148             (mon_mca_size(monmsg) % 12 != 0) ||
149             (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
150             (mon_mca_end(monmsg) > mon_dcss_end) ||
151             (mon_mca_start(monmsg) < mon_dcss_start) ||
152             ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
153                 return -EINVAL;
154         return 0;
155 }
156
157 static int mon_send_reply(struct mon_msg *monmsg,
158                           struct mon_private *monpriv)
159 {
160         int rc;
161
162         rc = iucv_message_reply(monpriv->path, &monmsg->msg,
163                                 IUCV_IPRMDATA, NULL, 0);
164         atomic_dec(&monpriv->msglim_count);
165         if (likely(!monmsg->msglim_reached)) {
166                 monmsg->pos = 0;
167                 monmsg->mca_offset = 0;
168                 monpriv->read_index = (monpriv->read_index + 1) %
169                                       MON_MSGLIM;
170                 atomic_dec(&monpriv->read_ready);
171         } else
172                 monmsg->replied_msglim = 1;
173         if (rc) {
174                 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
175                 return -EIO;
176         }
177         return 0;
178 }
179
180 static void mon_free_mem(struct mon_private *monpriv)
181 {
182         int i;
183
184         for (i = 0; i < MON_MSGLIM; i++)
185                 if (monpriv->msg_array[i])
186                         kfree(monpriv->msg_array[i]);
187         kfree(monpriv);
188 }
189
190 static struct mon_private *mon_alloc_mem(void)
191 {
192         int i;
193         struct mon_private *monpriv;
194
195         monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
196         if (!monpriv)
197                 return NULL;
198         for (i = 0; i < MON_MSGLIM; i++) {
199                 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
200                                                     GFP_KERNEL);
201                 if (!monpriv->msg_array[i]) {
202                         mon_free_mem(monpriv);
203                         return NULL;
204                 }
205         }
206         return monpriv;
207 }
208
209 static inline void mon_next_mca(struct mon_msg *monmsg)
210 {
211         if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
212                 return;
213         monmsg->mca_offset += 12;
214         monmsg->pos = 0;
215 }
216
217 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
218 {
219         struct mon_msg *monmsg;
220
221         if (!atomic_read(&monpriv->read_ready))
222                 return NULL;
223         monmsg = monpriv->msg_array[monpriv->read_index];
224         if (unlikely(monmsg->replied_msglim)) {
225                 monmsg->replied_msglim = 0;
226                 monmsg->msglim_reached = 0;
227                 monmsg->pos = 0;
228                 monmsg->mca_offset = 0;
229                 monpriv->read_index = (monpriv->read_index + 1) %
230                                       MON_MSGLIM;
231                 atomic_dec(&monpriv->read_ready);
232                 return ERR_PTR(-EOVERFLOW);
233         }
234         return monmsg;
235 }
236
237
238 /******************************************************************************
239  *                               IUCV handler                                 *
240  *****************************************************************************/
241 static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
242 {
243         struct mon_private *monpriv = path->private;
244
245         atomic_set(&monpriv->iucv_connected, 1);
246         wake_up(&mon_conn_wait_queue);
247 }
248
249 static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
250 {
251         struct mon_private *monpriv = path->private;
252
253         P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
254         iucv_path_sever(path, NULL);
255         atomic_set(&monpriv->iucv_severed, 1);
256         wake_up(&mon_conn_wait_queue);
257         wake_up_interruptible(&mon_read_wait_queue);
258 }
259
260 static void mon_iucv_message_pending(struct iucv_path *path,
261                                      struct iucv_message *msg)
262 {
263         struct mon_private *monpriv = path->private;
264
265         memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
266                msg, sizeof(*msg));
267         if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
268                 P_WARNING("IUCV message pending, message limit (%i) reached\n",
269                           MON_MSGLIM);
270                 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
271         }
272         monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
273         atomic_inc(&monpriv->read_ready);
274         wake_up_interruptible(&mon_read_wait_queue);
275 }
276
277 static struct iucv_handler monreader_iucv_handler = {
278         .path_complete   = mon_iucv_path_complete,
279         .path_severed    = mon_iucv_path_severed,
280         .message_pending = mon_iucv_message_pending,
281 };
282
283 /******************************************************************************
284  *                               file operations                              *
285  *****************************************************************************/
286 static int mon_open(struct inode *inode, struct file *filp)
287 {
288         struct mon_private *monpriv;
289         int rc;
290
291         /*
292          * only one user allowed
293          */
294         rc = -EBUSY;
295         if (test_and_set_bit(MON_IN_USE, &mon_in_use))
296                 goto out;
297
298         rc = -ENOMEM;
299         monpriv = mon_alloc_mem();
300         if (!monpriv)
301                 goto out_use;
302
303         /*
304          * Connect to *MONITOR service
305          */
306         monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
307         if (!monpriv->path)
308                 goto out_priv;
309         rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
310                                MON_SERVICE, NULL, user_data_connect, monpriv);
311         if (rc) {
312                 P_ERROR("iucv connection to *MONITOR failed with "
313                         "IPUSER SEVER code = %i\n", rc);
314                 rc = -EIO;
315                 goto out_path;
316         }
317         /*
318          * Wait for connection confirmation
319          */
320         wait_event(mon_conn_wait_queue,
321                    atomic_read(&monpriv->iucv_connected) ||
322                    atomic_read(&monpriv->iucv_severed));
323         if (atomic_read(&monpriv->iucv_severed)) {
324                 atomic_set(&monpriv->iucv_severed, 0);
325                 atomic_set(&monpriv->iucv_connected, 0);
326                 rc = -EIO;
327                 goto out_path;
328         }
329         filp->private_data = monpriv;
330         return nonseekable_open(inode, filp);
331
332 out_path:
333         kfree(monpriv->path);
334 out_priv:
335         mon_free_mem(monpriv);
336 out_use:
337         clear_bit(MON_IN_USE, &mon_in_use);
338 out:
339         return rc;
340 }
341
342 static int mon_close(struct inode *inode, struct file *filp)
343 {
344         int rc, i;
345         struct mon_private *monpriv = filp->private_data;
346
347         /*
348          * Close IUCV connection and unregister
349          */
350         rc = iucv_path_sever(monpriv->path, user_data_sever);
351         if (rc)
352                 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
353
354         atomic_set(&monpriv->iucv_severed, 0);
355         atomic_set(&monpriv->iucv_connected, 0);
356         atomic_set(&monpriv->read_ready, 0);
357         atomic_set(&monpriv->msglim_count, 0);
358         monpriv->write_index  = 0;
359         monpriv->read_index   = 0;
360
361         for (i = 0; i < MON_MSGLIM; i++)
362                 kfree(monpriv->msg_array[i]);
363         kfree(monpriv);
364         clear_bit(MON_IN_USE, &mon_in_use);
365         return 0;
366 }
367
368 static ssize_t mon_read(struct file *filp, char __user *data,
369                         size_t count, loff_t *ppos)
370 {
371         struct mon_private *monpriv = filp->private_data;
372         struct mon_msg *monmsg;
373         int ret;
374         u32 mce_start;
375
376         monmsg = mon_next_message(monpriv);
377         if (IS_ERR(monmsg))
378                 return PTR_ERR(monmsg);
379
380         if (!monmsg) {
381                 if (filp->f_flags & O_NONBLOCK)
382                         return -EAGAIN;
383                 ret = wait_event_interruptible(mon_read_wait_queue,
384                                         atomic_read(&monpriv->read_ready) ||
385                                         atomic_read(&monpriv->iucv_severed));
386                 if (ret)
387                         return ret;
388                 if (unlikely(atomic_read(&monpriv->iucv_severed)))
389                         return -EIO;
390                 monmsg = monpriv->msg_array[monpriv->read_index];
391         }
392
393         if (!monmsg->pos)
394                 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
395         if (mon_check_mca(monmsg))
396                 goto reply;
397
398         /* read monitor control element (12 bytes) first */
399         mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
400         if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
401                 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
402                 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
403                                    count);
404                 if (ret)
405                         return -EFAULT;
406                 monmsg->pos += count;
407                 if (monmsg->pos == mce_start + 12)
408                         monmsg->pos = mon_rec_start(monmsg);
409                 goto out_copy;
410         }
411
412         /* read records */
413         if (monmsg->pos <= mon_rec_end(monmsg)) {
414                 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
415                                             + 1);
416                 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
417                                    count);
418                 if (ret)
419                         return -EFAULT;
420                 monmsg->pos += count;
421                 if (monmsg->pos > mon_rec_end(monmsg))
422                         mon_next_mca(monmsg);
423                 goto out_copy;
424         }
425 reply:
426         ret = mon_send_reply(monmsg, monpriv);
427         return ret;
428
429 out_copy:
430         *ppos += count;
431         return count;
432 }
433
434 static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
435 {
436         struct mon_private *monpriv = filp->private_data;
437
438         poll_wait(filp, &mon_read_wait_queue, p);
439         if (unlikely(atomic_read(&monpriv->iucv_severed)))
440                 return POLLERR;
441         if (atomic_read(&monpriv->read_ready))
442                 return POLLIN | POLLRDNORM;
443         return 0;
444 }
445
446 static const struct file_operations mon_fops = {
447         .owner   = THIS_MODULE,
448         .open    = &mon_open,
449         .release = &mon_close,
450         .read    = &mon_read,
451         .poll    = &mon_poll,
452 };
453
454 static struct miscdevice mon_dev = {
455         .name       = "monreader",
456         .fops       = &mon_fops,
457         .minor      = MISC_DYNAMIC_MINOR,
458 };
459
460 /******************************************************************************
461  *                              module init/exit                              *
462  *****************************************************************************/
463 static int __init mon_init(void)
464 {
465         int rc;
466
467         if (!MACHINE_IS_VM) {
468                 P_ERROR("not running under z/VM, driver not loaded\n");
469                 return -ENODEV;
470         }
471
472         /*
473          * Register with IUCV and connect to *MONITOR service
474          */
475         rc = iucv_register(&monreader_iucv_handler, 1);
476         if (rc) {
477                 P_ERROR("failed to register with iucv driver\n");
478                 return rc;
479         }
480
481         rc = segment_type(mon_dcss_name);
482         if (rc < 0) {
483                 segment_warning(rc, mon_dcss_name);
484                 goto out_iucv;
485         }
486         if (rc != SEG_TYPE_SC) {
487                 P_ERROR("segment %s has unsupported type, should be SC\n",
488                         mon_dcss_name);
489                 rc = -EINVAL;
490                 goto out_iucv;
491         }
492
493         rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
494                           &mon_dcss_start, &mon_dcss_end);
495         if (rc < 0) {
496                 segment_warning(rc, mon_dcss_name);
497                 rc = -EINVAL;
498                 goto out_iucv;
499         }
500         dcss_mkname(mon_dcss_name, &user_data_connect[8]);
501
502         rc = misc_register(&mon_dev);
503         if (rc < 0 )
504                 goto out;
505         return 0;
506
507 out:
508         segment_unload(mon_dcss_name);
509 out_iucv:
510         iucv_unregister(&monreader_iucv_handler, 1);
511         return rc;
512 }
513
514 static void __exit mon_exit(void)
515 {
516         segment_unload(mon_dcss_name);
517         WARN_ON(misc_deregister(&mon_dev) != 0);
518         iucv_unregister(&monreader_iucv_handler, 1);
519         return;
520 }
521
522
523 module_init(mon_init);
524 module_exit(mon_exit);
525
526 module_param_string(mondcss, mon_dcss_name, 9, 0444);
527 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
528                  "service, max. 8 chars. Default is MONDCSS");
529
530 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
531 MODULE_DESCRIPTION("Character device driver for reading z/VM "
532                    "monitor service records.");
533 MODULE_LICENSE("GPL");