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