blob: 97e63cf469443ab594e231a288b856c0cc5d9562 [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 *
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +02006 * Copyright IBM Corp. 2004, 2008
7 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Gerald Schaefera4f5a292008-12-25 13:39:42 +010010#define KMSG_COMPONENT "monreader"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/init.h>
Arnd Bergmann6ce46a42008-05-20 19:16:17 +020016#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/miscdevice.h>
21#include <linux/ctype.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020024#include <linux/poll.h>
25#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
27#include <asm/ebcdic.h>
28#include <asm/extmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define MON_COLLECT_SAMPLE 0x80
32#define MON_COLLECT_EVENT 0x40
33#define MON_SERVICE "*MONITOR"
34#define MON_IN_USE 0x01
35#define MON_MSGLIM 255
36
37static char mon_dcss_name[9] = "MONDCSS\0";
38
39struct mon_msg {
40 u32 pos;
41 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080042 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 char msglim_reached;
44 char replied_msglim;
45};
46
47struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080048 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct mon_msg *msg_array[MON_MSGLIM];
50 unsigned int write_index;
51 unsigned int read_index;
52 atomic_t msglim_count;
53 atomic_t read_ready;
54 atomic_t iucv_connected;
55 atomic_t iucv_severed;
56};
57
58static unsigned long mon_in_use = 0;
59
60static unsigned long mon_dcss_start;
61static unsigned long mon_dcss_end;
62
63static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
64static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static u8 user_data_connect[16] = {
67 /* Version code, must be 0x01 for shared mode */
68 0x01,
69 /* what to collect */
70 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
71 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74};
75
76static u8 user_data_sever[16] = {
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79};
80
81
82/******************************************************************************
83 * helper functions *
84 *****************************************************************************/
85/*
86 * Create the 8 bytes EBCDIC DCSS segment name from
87 * an ASCII name, incl. padding
88 */
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +020089static void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int i;
92
93 for (i = 0; i < 8; i++) {
94 if (ascii_name[i] == '\0')
95 break;
96 ebcdic_name[i] = toupper(ascii_name[i]);
97 };
98 for (; i < 8; i++)
99 ebcdic_name[i] = ' ';
100 ASCEBC(ebcdic_name, 8);
101}
102
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800103static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800105 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800108static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800110 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800113static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
116}
117
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800118static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
121}
122
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800123static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
126}
127
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800128static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
131}
132
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200133static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
136 (mon_rec_start(monmsg) < mon_dcss_start) ||
137 (mon_rec_end(monmsg) > mon_dcss_end) ||
138 (mon_mca_type(monmsg, 0) == 0) ||
139 (mon_mca_size(monmsg) % 12 != 0) ||
140 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
141 (mon_mca_end(monmsg) > mon_dcss_end) ||
142 (mon_mca_start(monmsg) < mon_dcss_start) ||
143 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200148static int mon_send_reply(struct mon_msg *monmsg,
149 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int rc;
152
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800153 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
154 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 atomic_dec(&monpriv->msglim_count);
156 if (likely(!monmsg->msglim_reached)) {
157 monmsg->pos = 0;
158 monmsg->mca_offset = 0;
159 monpriv->read_index = (monpriv->read_index + 1) %
160 MON_MSGLIM;
161 atomic_dec(&monpriv->read_ready);
162 } else
163 monmsg->replied_msglim = 1;
164 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100165 pr_err("Reading monitor data failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -EIO;
167 }
168 return 0;
169}
170
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200171static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800173 int i;
174
175 for (i = 0; i < MON_MSGLIM; i++)
176 if (monpriv->msg_array[i])
177 kfree(monpriv->msg_array[i]);
178 kfree(monpriv);
179}
180
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200181static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800182{
183 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct mon_private *monpriv;
185
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800186 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200187 if (!monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800190 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 GFP_KERNEL);
192 if (!monpriv->msg_array[i]) {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800193 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return NULL;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 return monpriv;
198}
199
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800200static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 monmsg->mca_offset += 12;
205 monmsg->pos = 0;
206}
207
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200208static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct mon_msg *monmsg;
211
212 if (!atomic_read(&monpriv->read_ready))
213 return NULL;
214 monmsg = monpriv->msg_array[monpriv->read_index];
215 if (unlikely(monmsg->replied_msglim)) {
216 monmsg->replied_msglim = 0;
217 monmsg->msglim_reached = 0;
218 monmsg->pos = 0;
219 monmsg->mca_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 monpriv->read_index = (monpriv->read_index + 1) %
221 MON_MSGLIM;
222 atomic_dec(&monpriv->read_ready);
223 return ERR_PTR(-EOVERFLOW);
224 }
225 return monmsg;
226}
227
228
229/******************************************************************************
230 * IUCV handler *
231 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800232static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800234 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 atomic_set(&monpriv->iucv_connected, 1);
237 wake_up(&mon_conn_wait_queue);
238}
239
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800240static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800242 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100244 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
245 ipuser[0]);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800246 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 atomic_set(&monpriv->iucv_severed, 1);
248 wake_up(&mon_conn_wait_queue);
249 wake_up_interruptible(&mon_read_wait_queue);
250}
251
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800252static void mon_iucv_message_pending(struct iucv_path *path,
253 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800255 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800257 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
258 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100260 pr_warning("The read queue for monitor data is full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
262 }
263 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
264 atomic_inc(&monpriv->read_ready);
265 wake_up_interruptible(&mon_read_wait_queue);
266}
267
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800268static struct iucv_handler monreader_iucv_handler = {
269 .path_complete = mon_iucv_path_complete,
270 .path_severed = mon_iucv_path_severed,
271 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
274/******************************************************************************
275 * file operations *
276 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800277static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800280 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /*
283 * only one user allowed
284 */
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200285 lock_kernel();
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800286 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800290 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 monpriv = mon_alloc_mem();
292 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800293 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800296 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800298 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
299 if (!monpriv->path)
300 goto out_priv;
301 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
302 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100304 pr_err("Connecting to the z/VM *MONITOR system service "
305 "failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800307 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 /*
310 * Wait for connection confirmation
311 */
312 wait_event(mon_conn_wait_queue,
313 atomic_read(&monpriv->iucv_connected) ||
314 atomic_read(&monpriv->iucv_severed));
315 if (atomic_read(&monpriv->iucv_severed)) {
316 atomic_set(&monpriv->iucv_severed, 0);
317 atomic_set(&monpriv->iucv_connected, 0);
318 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800319 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 filp->private_data = monpriv;
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200322 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return nonseekable_open(inode, filp);
324
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800325out_path:
326 kfree(monpriv->path);
327out_priv:
328 mon_free_mem(monpriv);
329out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800331out:
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200332 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return rc;
334}
335
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800336static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 int rc, i;
339 struct mon_private *monpriv = filp->private_data;
340
341 /*
342 * Close IUCV connection and unregister
343 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800344 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (rc)
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100346 pr_warning("Disconnecting the z/VM *MONITOR system service "
347 "failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 atomic_set(&monpriv->iucv_severed, 0);
350 atomic_set(&monpriv->iucv_connected, 0);
351 atomic_set(&monpriv->read_ready, 0);
352 atomic_set(&monpriv->msglim_count, 0);
353 monpriv->write_index = 0;
354 monpriv->read_index = 0;
355
356 for (i = 0; i < MON_MSGLIM; i++)
357 kfree(monpriv->msg_array[i]);
358 kfree(monpriv);
359 clear_bit(MON_IN_USE, &mon_in_use);
360 return 0;
361}
362
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800363static ssize_t mon_read(struct file *filp, char __user *data,
364 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct mon_private *monpriv = filp->private_data;
367 struct mon_msg *monmsg;
368 int ret;
369 u32 mce_start;
370
371 monmsg = mon_next_message(monpriv);
372 if (IS_ERR(monmsg))
373 return PTR_ERR(monmsg);
374
375 if (!monmsg) {
376 if (filp->f_flags & O_NONBLOCK)
377 return -EAGAIN;
378 ret = wait_event_interruptible(mon_read_wait_queue,
379 atomic_read(&monpriv->read_ready) ||
380 atomic_read(&monpriv->iucv_severed));
381 if (ret)
382 return ret;
383 if (unlikely(atomic_read(&monpriv->iucv_severed)))
384 return -EIO;
385 monmsg = monpriv->msg_array[monpriv->read_index];
386 }
387
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200388 if (!monmsg->pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (mon_check_mca(monmsg))
391 goto reply;
392
393 /* read monitor control element (12 bytes) first */
394 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
395 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
396 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
397 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
398 count);
399 if (ret)
400 return -EFAULT;
401 monmsg->pos += count;
402 if (monmsg->pos == mce_start + 12)
403 monmsg->pos = mon_rec_start(monmsg);
404 goto out_copy;
405 }
406
407 /* read records */
408 if (monmsg->pos <= mon_rec_end(monmsg)) {
409 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
410 + 1);
411 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
412 count);
413 if (ret)
414 return -EFAULT;
415 monmsg->pos += count;
416 if (monmsg->pos > mon_rec_end(monmsg))
417 mon_next_mca(monmsg);
418 goto out_copy;
419 }
420reply:
421 ret = mon_send_reply(monmsg, monpriv);
422 return ret;
423
424out_copy:
425 *ppos += count;
426 return count;
427}
428
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800429static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct mon_private *monpriv = filp->private_data;
432
433 poll_wait(filp, &mon_read_wait_queue, p);
434 if (unlikely(atomic_read(&monpriv->iucv_severed)))
435 return POLLERR;
436 if (atomic_read(&monpriv->read_ready))
437 return POLLIN | POLLRDNORM;
438 return 0;
439}
440
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800441static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .owner = THIS_MODULE,
443 .open = &mon_open,
444 .release = &mon_close,
445 .read = &mon_read,
446 .poll = &mon_poll,
447};
448
449static struct miscdevice mon_dev = {
450 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .fops = &mon_fops,
452 .minor = MISC_DYNAMIC_MINOR,
453};
454
455/******************************************************************************
456 * module init/exit *
457 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800458static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int rc;
461
462 if (!MACHINE_IS_VM) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100463 pr_err("The z/VM *MONITOR record device driver cannot be "
464 "loaded without z/VM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -ENODEV;
466 }
467
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800468 /*
469 * Register with IUCV and connect to *MONITOR service
470 */
471 rc = iucv_register(&monreader_iucv_handler, 1);
472 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100473 pr_err("The z/VM *MONITOR record device driver failed to "
474 "register with IUCV\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800475 return rc;
476 }
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 rc = segment_type(mon_dcss_name);
479 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200480 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800481 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 if (rc != SEG_TYPE_SC) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100484 pr_err("The specified *MONITOR DCSS %s does not have the "
485 "required type SC\n", mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800486 rc = -EINVAL;
487 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
491 &mon_dcss_start, &mon_dcss_end);
492 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200493 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800494 rc = -EINVAL;
495 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
498
499 rc = misc_register(&mon_dev);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200500 if (rc < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503
504out:
505 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800506out_iucv:
507 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return rc;
509}
510
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800511static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 segment_unload(mon_dcss_name);
514 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800515 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return;
517}
518
519
520module_init(mon_init);
521module_exit(mon_exit);
522
523module_param_string(mondcss, mon_dcss_name, 9, 0444);
524MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
525 "service, max. 8 chars. Default is MONDCSS");
526
527MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
528MODULE_DESCRIPTION("Character device driver for reading z/VM "
529 "monitor service records.");
530MODULE_LICENSE("GPL");