blob: 3a1a958fb5f2843a2132e1a487af739d06417915 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/monreader.c
3 *
4 * Character device driver for reading z/VM *MONITOR service records.
5 *
Martin Schwidefskyc667aac2007-02-08 13:38:11 -08006 * Copyright 2004 IBM Corporation, IBM Deutschland Entwicklung GmbH.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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/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>
21#include <asm/uaccess.h>
22#include <asm/ebcdic.h>
23#include <asm/extmem.h>
24#include <linux/poll.h>
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080025#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27
28//#define MON_DEBUG /* Debug messages on/off */
29
30#define MON_NAME "monreader"
31
32#define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
33#define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
34#define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
35
36#ifdef MON_DEBUG
37#define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
38#else
39#define P_DEBUG(x...) do {} while (0)
40#endif
41
42#define MON_COLLECT_SAMPLE 0x80
43#define MON_COLLECT_EVENT 0x40
44#define MON_SERVICE "*MONITOR"
45#define MON_IN_USE 0x01
46#define MON_MSGLIM 255
47
48static char mon_dcss_name[9] = "MONDCSS\0";
49
50struct mon_msg {
51 u32 pos;
52 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080053 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 char msglim_reached;
55 char replied_msglim;
56};
57
58struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080059 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct mon_msg *msg_array[MON_MSGLIM];
61 unsigned int write_index;
62 unsigned int read_index;
63 atomic_t msglim_count;
64 atomic_t read_ready;
65 atomic_t iucv_connected;
66 atomic_t iucv_severed;
67};
68
69static unsigned long mon_in_use = 0;
70
71static unsigned long mon_dcss_start;
72static unsigned long mon_dcss_end;
73
74static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
75static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static u8 user_data_connect[16] = {
78 /* Version code, must be 0x01 for shared mode */
79 0x01,
80 /* what to collect */
81 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
82 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85};
86
87static u8 user_data_sever[16] = {
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90};
91
92
93/******************************************************************************
94 * helper functions *
95 *****************************************************************************/
96/*
97 * Create the 8 bytes EBCDIC DCSS segment name from
98 * an ASCII name, incl. padding
99 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800100static inline void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 int i;
103
104 for (i = 0; i < 8; i++) {
105 if (ascii_name[i] == '\0')
106 break;
107 ebcdic_name[i] = toupper(ascii_name[i]);
108 };
109 for (; i < 8; i++)
110 ebcdic_name[i] = ' ';
111 ASCEBC(ebcdic_name, 8);
112}
113
114/*
115 * print appropriate error message for segment_load()/segment_type()
116 * return code
117 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800118static void mon_segment_warn(int rc, char* seg_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 switch (rc) {
121 case -ENOENT:
122 P_WARNING("cannot load/query segment %s, does not exist\n",
123 seg_name);
124 break;
125 case -ENOSYS:
126 P_WARNING("cannot load/query segment %s, not running on VM\n",
127 seg_name);
128 break;
129 case -EIO:
130 P_WARNING("cannot load/query segment %s, hardware error\n",
131 seg_name);
132 break;
133 case -ENOTSUPP:
134 P_WARNING("cannot load/query segment %s, is a multi-part "
135 "segment\n", seg_name);
136 break;
137 case -ENOSPC:
138 P_WARNING("cannot load/query segment %s, overlaps with "
139 "storage\n", seg_name);
140 break;
141 case -EBUSY:
142 P_WARNING("cannot load/query segment %s, overlaps with "
143 "already loaded dcss\n", seg_name);
144 break;
145 case -EPERM:
146 P_WARNING("cannot load/query segment %s, already loaded in "
147 "incompatible mode\n", seg_name);
148 break;
149 case -ENOMEM:
150 P_WARNING("cannot load/query segment %s, out of memory\n",
151 seg_name);
152 break;
153 case -ERANGE:
154 P_WARNING("cannot load/query segment %s, exceeds kernel "
155 "mapping range\n", seg_name);
156 break;
157 default:
158 P_WARNING("cannot load/query segment %s, return value %i\n",
159 seg_name, rc);
160 break;
161 }
162}
163
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800164static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800166 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800169static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800171 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800174static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
177}
178
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800179static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
182}
183
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800184static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
187}
188
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800189static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
192}
193
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800194static inline int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
197 (mon_rec_start(monmsg) < mon_dcss_start) ||
198 (mon_rec_end(monmsg) > mon_dcss_end) ||
199 (mon_mca_type(monmsg, 0) == 0) ||
200 (mon_mca_size(monmsg) % 12 != 0) ||
201 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
202 (mon_mca_end(monmsg) > mon_dcss_end) ||
203 (mon_mca_start(monmsg) < mon_dcss_start) ||
204 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
205 {
206 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
207 return -EINVAL;
208 }
209 return 0;
210}
211
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800212static inline int mon_send_reply(struct mon_msg *monmsg,
213 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int rc;
216
217 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
218 "0x%08X\n\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800219 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
220
221 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
222 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 atomic_dec(&monpriv->msglim_count);
224 if (likely(!monmsg->msglim_reached)) {
225 monmsg->pos = 0;
226 monmsg->mca_offset = 0;
227 monpriv->read_index = (monpriv->read_index + 1) %
228 MON_MSGLIM;
229 atomic_dec(&monpriv->read_ready);
230 } else
231 monmsg->replied_msglim = 1;
232 if (rc) {
233 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
234 return -EIO;
235 }
236 return 0;
237}
238
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800239static inline void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800241 int i;
242
243 for (i = 0; i < MON_MSGLIM; i++)
244 if (monpriv->msg_array[i])
245 kfree(monpriv->msg_array[i]);
246 kfree(monpriv);
247}
248
249static inline struct mon_private *mon_alloc_mem(void)
250{
251 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct mon_private *monpriv;
253
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800254 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!monpriv) {
256 P_ERROR("no memory for monpriv\n");
257 return NULL;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800260 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 GFP_KERNEL);
262 if (!monpriv->msg_array[i]) {
263 P_ERROR("open, no memory for msg_array\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800264 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return NULL;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 return monpriv;
269}
270
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800271static inline void mon_read_debug(struct mon_msg *monmsg,
272 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274#ifdef MON_DEBUG
275 u8 msg_type[2], mca_type;
276 unsigned long records_len;
277
278 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
279
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800280 memcpy(msg_type, &monmsg->msg.class, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 EBCASC(msg_type, 2);
282 mca_type = mon_mca_type(monmsg, 0);
283 EBCASC(&mca_type, 1);
284
285 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
286 monpriv->read_index, monpriv->write_index);
287 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800288 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
290 msg_type[0], msg_type[1], mca_type ? mca_type : 'X',
291 mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2));
292 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
293 mon_mca_start(monmsg), mon_mca_end(monmsg));
294 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
295 mon_rec_start(monmsg), mon_rec_end(monmsg), records_len);
296 if (mon_mca_size(monmsg) > 12)
297 P_DEBUG("READ, MORE THAN ONE MCA\n\n");
298#endif
299}
300
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800301static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
304 return;
305 P_DEBUG("READ, NEXT MCA\n\n");
306 monmsg->mca_offset += 12;
307 monmsg->pos = 0;
308}
309
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800310static inline struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct mon_msg *monmsg;
313
314 if (!atomic_read(&monpriv->read_ready))
315 return NULL;
316 monmsg = monpriv->msg_array[monpriv->read_index];
317 if (unlikely(monmsg->replied_msglim)) {
318 monmsg->replied_msglim = 0;
319 monmsg->msglim_reached = 0;
320 monmsg->pos = 0;
321 monmsg->mca_offset = 0;
322 P_WARNING("read, message limit reached\n");
323 monpriv->read_index = (monpriv->read_index + 1) %
324 MON_MSGLIM;
325 atomic_dec(&monpriv->read_ready);
326 return ERR_PTR(-EOVERFLOW);
327 }
328 return monmsg;
329}
330
331
332/******************************************************************************
333 * IUCV handler *
334 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800335static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800337 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 P_DEBUG("IUCV connection completed\n");
340 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
341 "0x%02X, Sample = 0x%02X\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800342 ipuser[0], ipuser[1], ipuser[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 atomic_set(&monpriv->iucv_connected, 1);
344 wake_up(&mon_conn_wait_queue);
345}
346
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800347static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800349 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800351 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
352 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 atomic_set(&monpriv->iucv_severed, 1);
354 wake_up(&mon_conn_wait_queue);
355 wake_up_interruptible(&mon_read_wait_queue);
356}
357
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800358static void mon_iucv_message_pending(struct iucv_path *path,
359 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800361 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 P_DEBUG("IUCV message pending\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800364 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
365 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
367 P_WARNING("IUCV message pending, message limit (%i) reached\n",
368 MON_MSGLIM);
369 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
370 }
371 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
372 atomic_inc(&monpriv->read_ready);
373 wake_up_interruptible(&mon_read_wait_queue);
374}
375
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800376static struct iucv_handler monreader_iucv_handler = {
377 .path_complete = mon_iucv_path_complete,
378 .path_severed = mon_iucv_path_severed,
379 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382/******************************************************************************
383 * file operations *
384 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800385static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800388 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * only one user allowed
392 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800393 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800397 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 monpriv = mon_alloc_mem();
399 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800400 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800403 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800405 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
406 if (!monpriv->path)
407 goto out_priv;
408 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
409 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (rc) {
411 P_ERROR("iucv connection to *MONITOR failed with "
412 "IPUSER SEVER code = %i\n", rc);
413 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800414 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 /*
417 * Wait for connection confirmation
418 */
419 wait_event(mon_conn_wait_queue,
420 atomic_read(&monpriv->iucv_connected) ||
421 atomic_read(&monpriv->iucv_severed));
422 if (atomic_read(&monpriv->iucv_severed)) {
423 atomic_set(&monpriv->iucv_severed, 0);
424 atomic_set(&monpriv->iucv_connected, 0);
425 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800426 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 P_INFO("open, established connection to *MONITOR service\n\n");
429 filp->private_data = monpriv;
430 return nonseekable_open(inode, filp);
431
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800432out_path:
433 kfree(monpriv->path);
434out_priv:
435 mon_free_mem(monpriv);
436out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return rc;
440}
441
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800442static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 int rc, i;
445 struct mon_private *monpriv = filp->private_data;
446
447 /*
448 * Close IUCV connection and unregister
449 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800450 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (rc)
452 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
453 else
454 P_INFO("close, terminated connection to *MONITOR service\n");
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 atomic_set(&monpriv->iucv_severed, 0);
457 atomic_set(&monpriv->iucv_connected, 0);
458 atomic_set(&monpriv->read_ready, 0);
459 atomic_set(&monpriv->msglim_count, 0);
460 monpriv->write_index = 0;
461 monpriv->read_index = 0;
462
463 for (i = 0; i < MON_MSGLIM; i++)
464 kfree(monpriv->msg_array[i]);
465 kfree(monpriv);
466 clear_bit(MON_IN_USE, &mon_in_use);
467 return 0;
468}
469
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800470static ssize_t mon_read(struct file *filp, char __user *data,
471 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct mon_private *monpriv = filp->private_data;
474 struct mon_msg *monmsg;
475 int ret;
476 u32 mce_start;
477
478 monmsg = mon_next_message(monpriv);
479 if (IS_ERR(monmsg))
480 return PTR_ERR(monmsg);
481
482 if (!monmsg) {
483 if (filp->f_flags & O_NONBLOCK)
484 return -EAGAIN;
485 ret = wait_event_interruptible(mon_read_wait_queue,
486 atomic_read(&monpriv->read_ready) ||
487 atomic_read(&monpriv->iucv_severed));
488 if (ret)
489 return ret;
490 if (unlikely(atomic_read(&monpriv->iucv_severed)))
491 return -EIO;
492 monmsg = monpriv->msg_array[monpriv->read_index];
493 }
494
495 if (!monmsg->pos) {
496 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
497 mon_read_debug(monmsg, monpriv);
498 }
499 if (mon_check_mca(monmsg))
500 goto reply;
501
502 /* read monitor control element (12 bytes) first */
503 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
504 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
505 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
506 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
507 count);
508 if (ret)
509 return -EFAULT;
510 monmsg->pos += count;
511 if (monmsg->pos == mce_start + 12)
512 monmsg->pos = mon_rec_start(monmsg);
513 goto out_copy;
514 }
515
516 /* read records */
517 if (monmsg->pos <= mon_rec_end(monmsg)) {
518 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
519 + 1);
520 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
521 count);
522 if (ret)
523 return -EFAULT;
524 monmsg->pos += count;
525 if (monmsg->pos > mon_rec_end(monmsg))
526 mon_next_mca(monmsg);
527 goto out_copy;
528 }
529reply:
530 ret = mon_send_reply(monmsg, monpriv);
531 return ret;
532
533out_copy:
534 *ppos += count;
535 return count;
536}
537
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800538static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct mon_private *monpriv = filp->private_data;
541
542 poll_wait(filp, &mon_read_wait_queue, p);
543 if (unlikely(atomic_read(&monpriv->iucv_severed)))
544 return POLLERR;
545 if (atomic_read(&monpriv->read_ready))
546 return POLLIN | POLLRDNORM;
547 return 0;
548}
549
550static struct file_operations mon_fops = {
551 .owner = THIS_MODULE,
552 .open = &mon_open,
553 .release = &mon_close,
554 .read = &mon_read,
555 .poll = &mon_poll,
556};
557
558static struct miscdevice mon_dev = {
559 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .fops = &mon_fops,
561 .minor = MISC_DYNAMIC_MINOR,
562};
563
564/******************************************************************************
565 * module init/exit *
566 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800567static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int rc;
570
571 if (!MACHINE_IS_VM) {
572 P_ERROR("not running under z/VM, driver not loaded\n");
573 return -ENODEV;
574 }
575
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800576 /*
577 * Register with IUCV and connect to *MONITOR service
578 */
579 rc = iucv_register(&monreader_iucv_handler, 1);
580 if (rc) {
581 P_ERROR("failed to register with iucv driver\n");
582 return rc;
583 }
584 P_INFO("open, registered with IUCV\n");
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 rc = segment_type(mon_dcss_name);
587 if (rc < 0) {
588 mon_segment_warn(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800589 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 if (rc != SEG_TYPE_SC) {
592 P_ERROR("segment %s has unsupported type, should be SC\n",
593 mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800594 rc = -EINVAL;
595 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
599 &mon_dcss_start, &mon_dcss_end);
600 if (rc < 0) {
601 mon_segment_warn(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800602 rc = -EINVAL;
603 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
606
607 rc = misc_register(&mon_dev);
608 if (rc < 0 ) {
609 P_ERROR("misc_register failed, rc = %i\n", rc);
610 goto out;
611 }
612 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
613 mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end,
614 mon_dcss_end - mon_dcss_start + 1);
615 return 0;
616
617out:
618 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800619out_iucv:
620 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return rc;
622}
623
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800624static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 segment_unload(mon_dcss_name);
627 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800628 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return;
630}
631
632
633module_init(mon_init);
634module_exit(mon_exit);
635
636module_param_string(mondcss, mon_dcss_name, 9, 0444);
637MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
638 "service, max. 8 chars. Default is MONDCSS");
639
640MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
641MODULE_DESCRIPTION("Character device driver for reading z/VM "
642 "monitor service records.");
643MODULE_LICENSE("GPL");