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