blob: 1e1f50655bbfb8f01a4896dd830a09ff23f98f99 [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 Schwidefsky9b0c4552007-05-10 15:45:44 +0200100static 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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800114static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800116 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800119static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800121 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800124static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
127}
128
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800129static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
132}
133
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800134static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
137}
138
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800139static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
142}
143
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200144static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
147 (mon_rec_start(monmsg) < mon_dcss_start) ||
148 (mon_rec_end(monmsg) > mon_dcss_end) ||
149 (mon_mca_type(monmsg, 0) == 0) ||
150 (mon_mca_size(monmsg) % 12 != 0) ||
151 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
152 (mon_mca_end(monmsg) > mon_dcss_end) ||
153 (mon_mca_start(monmsg) < mon_dcss_start) ||
154 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
155 {
156 P_DEBUG("READ, IGNORED INVALID MCA\n\n");
157 return -EINVAL;
158 }
159 return 0;
160}
161
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200162static int mon_send_reply(struct mon_msg *monmsg,
163 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int rc;
166
167 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
168 "0x%08X\n\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800169 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
170
171 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
172 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 atomic_dec(&monpriv->msglim_count);
174 if (likely(!monmsg->msglim_reached)) {
175 monmsg->pos = 0;
176 monmsg->mca_offset = 0;
177 monpriv->read_index = (monpriv->read_index + 1) %
178 MON_MSGLIM;
179 atomic_dec(&monpriv->read_ready);
180 } else
181 monmsg->replied_msglim = 1;
182 if (rc) {
183 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
184 return -EIO;
185 }
186 return 0;
187}
188
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200189static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800191 int i;
192
193 for (i = 0; i < MON_MSGLIM; i++)
194 if (monpriv->msg_array[i])
195 kfree(monpriv->msg_array[i]);
196 kfree(monpriv);
197}
198
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200199static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800200{
201 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct mon_private *monpriv;
203
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800204 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!monpriv) {
206 P_ERROR("no memory for monpriv\n");
207 return NULL;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800210 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 GFP_KERNEL);
212 if (!monpriv->msg_array[i]) {
213 P_ERROR("open, no memory for msg_array\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800214 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 return monpriv;
219}
220
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800221static inline void mon_read_debug(struct mon_msg *monmsg,
222 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224#ifdef MON_DEBUG
225 u8 msg_type[2], mca_type;
226 unsigned long records_len;
227
228 records_len = mon_rec_end(monmsg) - mon_rec_start(monmsg) + 1;
229
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800230 memcpy(msg_type, &monmsg->msg.class, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 EBCASC(msg_type, 2);
232 mca_type = mon_mca_type(monmsg, 0);
233 EBCASC(&mca_type, 1);
234
235 P_DEBUG("read, mon_read_index = %i, mon_write_index = %i\n",
236 monpriv->read_index, monpriv->write_index);
237 P_DEBUG("read, pathid = 0x%04X, msgid = 0x%08X, trgcls = 0x%08X\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800238 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 P_DEBUG("read, msg_type = '%c%c', mca_type = '%c' / 0x%X / 0x%X\n",
240 msg_type[0], msg_type[1], mca_type ? mca_type : 'X',
241 mon_mca_type(monmsg, 1), mon_mca_type(monmsg, 2));
242 P_DEBUG("read, MCA: start = 0x%lX, end = 0x%lX\n",
243 mon_mca_start(monmsg), mon_mca_end(monmsg));
244 P_DEBUG("read, REC: start = 0x%X, end = 0x%X, len = %lu\n\n",
245 mon_rec_start(monmsg), mon_rec_end(monmsg), records_len);
246 if (mon_mca_size(monmsg) > 12)
247 P_DEBUG("READ, MORE THAN ONE MCA\n\n");
248#endif
249}
250
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800251static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
254 return;
255 P_DEBUG("READ, NEXT MCA\n\n");
256 monmsg->mca_offset += 12;
257 monmsg->pos = 0;
258}
259
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200260static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct mon_msg *monmsg;
263
264 if (!atomic_read(&monpriv->read_ready))
265 return NULL;
266 monmsg = monpriv->msg_array[monpriv->read_index];
267 if (unlikely(monmsg->replied_msglim)) {
268 monmsg->replied_msglim = 0;
269 monmsg->msglim_reached = 0;
270 monmsg->pos = 0;
271 monmsg->mca_offset = 0;
272 P_WARNING("read, message limit reached\n");
273 monpriv->read_index = (monpriv->read_index + 1) %
274 MON_MSGLIM;
275 atomic_dec(&monpriv->read_ready);
276 return ERR_PTR(-EOVERFLOW);
277 }
278 return monmsg;
279}
280
281
282/******************************************************************************
283 * IUCV handler *
284 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800285static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800287 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 P_DEBUG("IUCV connection completed\n");
290 P_DEBUG("IUCV ACCEPT (from *MONITOR): Version = 0x%02X, Event = "
291 "0x%02X, Sample = 0x%02X\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800292 ipuser[0], ipuser[1], ipuser[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 atomic_set(&monpriv->iucv_connected, 1);
294 wake_up(&mon_conn_wait_queue);
295}
296
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800297static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800299 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800301 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
302 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 atomic_set(&monpriv->iucv_severed, 1);
304 wake_up(&mon_conn_wait_queue);
305 wake_up_interruptible(&mon_read_wait_queue);
306}
307
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800308static void mon_iucv_message_pending(struct iucv_path *path,
309 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800311 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 P_DEBUG("IUCV message pending\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800314 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
315 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
317 P_WARNING("IUCV message pending, message limit (%i) reached\n",
318 MON_MSGLIM);
319 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
320 }
321 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
322 atomic_inc(&monpriv->read_ready);
323 wake_up_interruptible(&mon_read_wait_queue);
324}
325
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800326static struct iucv_handler monreader_iucv_handler = {
327 .path_complete = mon_iucv_path_complete,
328 .path_severed = mon_iucv_path_severed,
329 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332/******************************************************************************
333 * file operations *
334 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800335static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800338 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /*
341 * only one user allowed
342 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800343 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800347 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 monpriv = mon_alloc_mem();
349 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800350 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800353 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800355 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
356 if (!monpriv->path)
357 goto out_priv;
358 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
359 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (rc) {
361 P_ERROR("iucv connection to *MONITOR failed with "
362 "IPUSER SEVER code = %i\n", rc);
363 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800364 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 /*
367 * Wait for connection confirmation
368 */
369 wait_event(mon_conn_wait_queue,
370 atomic_read(&monpriv->iucv_connected) ||
371 atomic_read(&monpriv->iucv_severed));
372 if (atomic_read(&monpriv->iucv_severed)) {
373 atomic_set(&monpriv->iucv_severed, 0);
374 atomic_set(&monpriv->iucv_connected, 0);
375 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800376 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 P_INFO("open, established connection to *MONITOR service\n\n");
379 filp->private_data = monpriv;
380 return nonseekable_open(inode, filp);
381
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800382out_path:
383 kfree(monpriv->path);
384out_priv:
385 mon_free_mem(monpriv);
386out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800388out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return rc;
390}
391
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800392static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 int rc, i;
395 struct mon_private *monpriv = filp->private_data;
396
397 /*
398 * Close IUCV connection and unregister
399 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800400 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (rc)
402 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
403 else
404 P_INFO("close, terminated connection to *MONITOR service\n");
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 atomic_set(&monpriv->iucv_severed, 0);
407 atomic_set(&monpriv->iucv_connected, 0);
408 atomic_set(&monpriv->read_ready, 0);
409 atomic_set(&monpriv->msglim_count, 0);
410 monpriv->write_index = 0;
411 monpriv->read_index = 0;
412
413 for (i = 0; i < MON_MSGLIM; i++)
414 kfree(monpriv->msg_array[i]);
415 kfree(monpriv);
416 clear_bit(MON_IN_USE, &mon_in_use);
417 return 0;
418}
419
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800420static ssize_t mon_read(struct file *filp, char __user *data,
421 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct mon_private *monpriv = filp->private_data;
424 struct mon_msg *monmsg;
425 int ret;
426 u32 mce_start;
427
428 monmsg = mon_next_message(monpriv);
429 if (IS_ERR(monmsg))
430 return PTR_ERR(monmsg);
431
432 if (!monmsg) {
433 if (filp->f_flags & O_NONBLOCK)
434 return -EAGAIN;
435 ret = wait_event_interruptible(mon_read_wait_queue,
436 atomic_read(&monpriv->read_ready) ||
437 atomic_read(&monpriv->iucv_severed));
438 if (ret)
439 return ret;
440 if (unlikely(atomic_read(&monpriv->iucv_severed)))
441 return -EIO;
442 monmsg = monpriv->msg_array[monpriv->read_index];
443 }
444
445 if (!monmsg->pos) {
446 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
447 mon_read_debug(monmsg, monpriv);
448 }
449 if (mon_check_mca(monmsg))
450 goto reply;
451
452 /* read monitor control element (12 bytes) first */
453 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
454 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
455 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
456 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
457 count);
458 if (ret)
459 return -EFAULT;
460 monmsg->pos += count;
461 if (monmsg->pos == mce_start + 12)
462 monmsg->pos = mon_rec_start(monmsg);
463 goto out_copy;
464 }
465
466 /* read records */
467 if (monmsg->pos <= mon_rec_end(monmsg)) {
468 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
469 + 1);
470 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
471 count);
472 if (ret)
473 return -EFAULT;
474 monmsg->pos += count;
475 if (monmsg->pos > mon_rec_end(monmsg))
476 mon_next_mca(monmsg);
477 goto out_copy;
478 }
479reply:
480 ret = mon_send_reply(monmsg, monpriv);
481 return ret;
482
483out_copy:
484 *ppos += count;
485 return count;
486}
487
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800488static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct mon_private *monpriv = filp->private_data;
491
492 poll_wait(filp, &mon_read_wait_queue, p);
493 if (unlikely(atomic_read(&monpriv->iucv_severed)))
494 return POLLERR;
495 if (atomic_read(&monpriv->read_ready))
496 return POLLIN | POLLRDNORM;
497 return 0;
498}
499
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800500static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .owner = THIS_MODULE,
502 .open = &mon_open,
503 .release = &mon_close,
504 .read = &mon_read,
505 .poll = &mon_poll,
506};
507
508static struct miscdevice mon_dev = {
509 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .fops = &mon_fops,
511 .minor = MISC_DYNAMIC_MINOR,
512};
513
514/******************************************************************************
515 * module init/exit *
516 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800517static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 int rc;
520
521 if (!MACHINE_IS_VM) {
522 P_ERROR("not running under z/VM, driver not loaded\n");
523 return -ENODEV;
524 }
525
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800526 /*
527 * Register with IUCV and connect to *MONITOR service
528 */
529 rc = iucv_register(&monreader_iucv_handler, 1);
530 if (rc) {
531 P_ERROR("failed to register with iucv driver\n");
532 return rc;
533 }
534 P_INFO("open, registered with IUCV\n");
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 rc = segment_type(mon_dcss_name);
537 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200538 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800539 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 if (rc != SEG_TYPE_SC) {
542 P_ERROR("segment %s has unsupported type, should be SC\n",
543 mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800544 rc = -EINVAL;
545 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
548 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
549 &mon_dcss_start, &mon_dcss_end);
550 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200551 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800552 rc = -EINVAL;
553 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
556
557 rc = misc_register(&mon_dev);
558 if (rc < 0 ) {
559 P_ERROR("misc_register failed, rc = %i\n", rc);
560 goto out;
561 }
562 P_INFO("Loaded segment %s from %p to %p, size = %lu Byte\n",
563 mon_dcss_name, (void *) mon_dcss_start, (void *) mon_dcss_end,
564 mon_dcss_end - mon_dcss_start + 1);
565 return 0;
566
567out:
568 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800569out_iucv:
570 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return rc;
572}
573
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800574static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 segment_unload(mon_dcss_name);
577 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800578 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return;
580}
581
582
583module_init(mon_init);
584module_exit(mon_exit);
585
586module_param_string(mondcss, mon_dcss_name, 9, 0444);
587MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
588 "service, max. 8 chars. Default is MONDCSS");
589
590MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
591MODULE_DESCRIPTION("Character device driver for reading z/VM "
592 "monitor service records.");
593MODULE_LICENSE("GPL");