blob: dc4710e64e0c61134aa1766bf755aecfc7425541 [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>
Arnd Bergmann6ce46a42008-05-20 19:16:17 +020014#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080026#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
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
49static char mon_dcss_name[9] = "MONDCSS\0";
50
51struct mon_msg {
52 u32 pos;
53 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080054 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char msglim_reached;
56 char replied_msglim;
57};
58
59struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080060 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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
70static unsigned long mon_in_use = 0;
71
72static unsigned long mon_dcss_start;
73static unsigned long mon_dcss_end;
74
75static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
76static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static 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
88static 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 */
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200101static void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800115static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800117 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800120static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800122 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800125static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
128}
129
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800130static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
133}
134
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800135static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
138}
139
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800140static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
143}
144
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200145static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
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
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200163static int mon_send_reply(struct mon_msg *monmsg,
164 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int rc;
167
168 P_DEBUG("read, REPLY: pathid = 0x%04X, msgid = 0x%08X, trgcls = "
169 "0x%08X\n\n",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800170 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
171
172 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
173 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 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
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200190static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800192 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
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200200static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800201{
202 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct mon_private *monpriv;
204
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800205 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!monpriv) {
207 P_ERROR("no memory for monpriv\n");
208 return NULL;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800211 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 GFP_KERNEL);
213 if (!monpriv->msg_array[i]) {
214 P_ERROR("open, no memory for msg_array\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800215 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return NULL;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 return monpriv;
220}
221
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800222static inline void mon_read_debug(struct mon_msg *monmsg,
223 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800231 memcpy(msg_type, &monmsg->msg.class, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 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",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800239 monpriv->path->pathid, monmsg->msg.id, monmsg->msg.class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800252static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
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
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200261static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
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 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800286static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800288 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
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",
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800293 ipuser[0], ipuser[1], ipuser[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 atomic_set(&monpriv->iucv_connected, 1);
295 wake_up(&mon_conn_wait_queue);
296}
297
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800298static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800300 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800302 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
303 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 atomic_set(&monpriv->iucv_severed, 1);
305 wake_up(&mon_conn_wait_queue);
306 wake_up_interruptible(&mon_read_wait_queue);
307}
308
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800309static void mon_iucv_message_pending(struct iucv_path *path,
310 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800312 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 P_DEBUG("IUCV message pending\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800315 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
316 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800327static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
333/******************************************************************************
334 * file operations *
335 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800336static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800339 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * only one user allowed
343 */
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200344 lock_kernel();
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800345 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800349 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 monpriv = mon_alloc_mem();
351 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800352 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800355 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800357 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (rc) {
363 P_ERROR("iucv connection to *MONITOR failed with "
364 "IPUSER SEVER code = %i\n", rc);
365 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800366 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
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;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800378 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 P_INFO("open, established connection to *MONITOR service\n\n");
381 filp->private_data = monpriv;
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200382 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return nonseekable_open(inode, filp);
384
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800385out_path:
386 kfree(monpriv->path);
387out_priv:
388 mon_free_mem(monpriv);
389out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800391out:
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200392 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return rc;
394}
395
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800396static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 int rc, i;
399 struct mon_private *monpriv = filp->private_data;
400
401 /*
402 * Close IUCV connection and unregister
403 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800404 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800424static ssize_t mon_read(struct file *filp, char __user *data,
425 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
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 }
483reply:
484 ret = mon_send_reply(monmsg, monpriv);
485 return ret;
486
487out_copy:
488 *ppos += count;
489 return count;
490}
491
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800492static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
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
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800504static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .owner = THIS_MODULE,
506 .open = &mon_open,
507 .release = &mon_close,
508 .read = &mon_read,
509 .poll = &mon_poll,
510};
511
512static struct miscdevice mon_dev = {
513 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .fops = &mon_fops,
515 .minor = MISC_DYNAMIC_MINOR,
516};
517
518/******************************************************************************
519 * module init/exit *
520 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800521static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
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
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800530 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rc = segment_type(mon_dcss_name);
541 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200542 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800543 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 if (rc != SEG_TYPE_SC) {
546 P_ERROR("segment %s has unsupported type, should be SC\n",
547 mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800548 rc = -EINVAL;
549 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
552 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
553 &mon_dcss_start, &mon_dcss_end);
554 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200555 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800556 rc = -EINVAL;
557 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
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
571out:
572 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800573out_iucv:
574 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return rc;
576}
577
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800578static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 segment_unload(mon_dcss_name);
581 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800582 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return;
584}
585
586
587module_init(mon_init);
588module_exit(mon_exit);
589
590module_param_string(mondcss, mon_dcss_name, 9, 0444);
591MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
592 "service, max. 8 chars. Default is MONDCSS");
593
594MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
595MODULE_DESCRIPTION("Character device driver for reading z/VM "
596 "monitor service records.");
597MODULE_LICENSE("GPL");