blob: 35fd8dfcaaa6d22d2083304ccdd9ebb4137d3af7 [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
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/init.h>
Arnd Bergmann6ce46a42008-05-20 19:16:17 +020013#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020021#include <linux/poll.h>
22#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/ebcdic.h>
25#include <asm/extmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27//#define MON_DEBUG /* Debug messages on/off */
28
29#define MON_NAME "monreader"
30
31#define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
32#define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
33#define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
34
35#ifdef MON_DEBUG
36#define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
37#else
38#define P_DEBUG(x...) do {} while (0)
39#endif
40
41#define MON_COLLECT_SAMPLE 0x80
42#define MON_COLLECT_EVENT 0x40
43#define MON_SERVICE "*MONITOR"
44#define MON_IN_USE 0x01
45#define MON_MSGLIM 255
46
47static char mon_dcss_name[9] = "MONDCSS\0";
48
49struct mon_msg {
50 u32 pos;
51 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080052 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 char msglim_reached;
54 char replied_msglim;
55};
56
57struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080058 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct mon_msg *msg_array[MON_MSGLIM];
60 unsigned int write_index;
61 unsigned int read_index;
62 atomic_t msglim_count;
63 atomic_t read_ready;
64 atomic_t iucv_connected;
65 atomic_t iucv_severed;
66};
67
68static unsigned long mon_in_use = 0;
69
70static unsigned long mon_dcss_start;
71static unsigned long mon_dcss_end;
72
73static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
74static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static u8 user_data_connect[16] = {
77 /* Version code, must be 0x01 for shared mode */
78 0x01,
79 /* what to collect */
80 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
81 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84};
85
86static u8 user_data_sever[16] = {
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89};
90
91
92/******************************************************************************
93 * helper functions *
94 *****************************************************************************/
95/*
96 * Create the 8 bytes EBCDIC DCSS segment name from
97 * an ASCII name, incl. padding
98 */
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +020099static void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int i;
102
103 for (i = 0; i < 8; i++) {
104 if (ascii_name[i] == '\0')
105 break;
106 ebcdic_name[i] = toupper(ascii_name[i]);
107 };
108 for (; i < 8; i++)
109 ebcdic_name[i] = ' ';
110 ASCEBC(ebcdic_name, 8);
111}
112
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800113static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800115 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800118static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800120 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800123static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
126}
127
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800128static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
131}
132
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800133static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
136}
137
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800138static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
141}
142
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200143static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
146 (mon_rec_start(monmsg) < mon_dcss_start) ||
147 (mon_rec_end(monmsg) > mon_dcss_end) ||
148 (mon_mca_type(monmsg, 0) == 0) ||
149 (mon_mca_size(monmsg) % 12 != 0) ||
150 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
151 (mon_mca_end(monmsg) > mon_dcss_end) ||
152 (mon_mca_start(monmsg) < mon_dcss_start) ||
153 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200158static int mon_send_reply(struct mon_msg *monmsg,
159 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int rc;
162
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800163 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
164 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 atomic_dec(&monpriv->msglim_count);
166 if (likely(!monmsg->msglim_reached)) {
167 monmsg->pos = 0;
168 monmsg->mca_offset = 0;
169 monpriv->read_index = (monpriv->read_index + 1) %
170 MON_MSGLIM;
171 atomic_dec(&monpriv->read_ready);
172 } else
173 monmsg->replied_msglim = 1;
174 if (rc) {
175 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
176 return -EIO;
177 }
178 return 0;
179}
180
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200181static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800183 int i;
184
185 for (i = 0; i < MON_MSGLIM; i++)
186 if (monpriv->msg_array[i])
187 kfree(monpriv->msg_array[i]);
188 kfree(monpriv);
189}
190
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200191static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800192{
193 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct mon_private *monpriv;
195
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800196 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200197 if (!monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800200 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 GFP_KERNEL);
202 if (!monpriv->msg_array[i]) {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800203 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return NULL;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 return monpriv;
208}
209
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800210static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
213 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 monmsg->mca_offset += 12;
215 monmsg->pos = 0;
216}
217
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200218static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct mon_msg *monmsg;
221
222 if (!atomic_read(&monpriv->read_ready))
223 return NULL;
224 monmsg = monpriv->msg_array[monpriv->read_index];
225 if (unlikely(monmsg->replied_msglim)) {
226 monmsg->replied_msglim = 0;
227 monmsg->msglim_reached = 0;
228 monmsg->pos = 0;
229 monmsg->mca_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 monpriv->read_index = (monpriv->read_index + 1) %
231 MON_MSGLIM;
232 atomic_dec(&monpriv->read_ready);
233 return ERR_PTR(-EOVERFLOW);
234 }
235 return monmsg;
236}
237
238
239/******************************************************************************
240 * IUCV handler *
241 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800242static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800244 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 atomic_set(&monpriv->iucv_connected, 1);
247 wake_up(&mon_conn_wait_queue);
248}
249
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800250static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800252 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800254 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
255 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 atomic_set(&monpriv->iucv_severed, 1);
257 wake_up(&mon_conn_wait_queue);
258 wake_up_interruptible(&mon_read_wait_queue);
259}
260
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800261static void mon_iucv_message_pending(struct iucv_path *path,
262 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800264 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800266 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
267 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
269 P_WARNING("IUCV message pending, message limit (%i) reached\n",
270 MON_MSGLIM);
271 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
272 }
273 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
274 atomic_inc(&monpriv->read_ready);
275 wake_up_interruptible(&mon_read_wait_queue);
276}
277
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800278static struct iucv_handler monreader_iucv_handler = {
279 .path_complete = mon_iucv_path_complete,
280 .path_severed = mon_iucv_path_severed,
281 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284/******************************************************************************
285 * file operations *
286 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800287static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800290 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * only one user allowed
294 */
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200295 lock_kernel();
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800296 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800300 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 monpriv = mon_alloc_mem();
302 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800303 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800306 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800308 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
309 if (!monpriv->path)
310 goto out_priv;
311 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
312 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (rc) {
314 P_ERROR("iucv connection to *MONITOR failed with "
315 "IPUSER SEVER code = %i\n", rc);
316 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800317 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 /*
320 * Wait for connection confirmation
321 */
322 wait_event(mon_conn_wait_queue,
323 atomic_read(&monpriv->iucv_connected) ||
324 atomic_read(&monpriv->iucv_severed));
325 if (atomic_read(&monpriv->iucv_severed)) {
326 atomic_set(&monpriv->iucv_severed, 0);
327 atomic_set(&monpriv->iucv_connected, 0);
328 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800329 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 filp->private_data = monpriv;
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200332 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return nonseekable_open(inode, filp);
334
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800335out_path:
336 kfree(monpriv->path);
337out_priv:
338 mon_free_mem(monpriv);
339out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800341out:
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200342 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return rc;
344}
345
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800346static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int rc, i;
349 struct mon_private *monpriv = filp->private_data;
350
351 /*
352 * Close IUCV connection and unregister
353 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800354 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (rc)
356 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 atomic_set(&monpriv->iucv_severed, 0);
359 atomic_set(&monpriv->iucv_connected, 0);
360 atomic_set(&monpriv->read_ready, 0);
361 atomic_set(&monpriv->msglim_count, 0);
362 monpriv->write_index = 0;
363 monpriv->read_index = 0;
364
365 for (i = 0; i < MON_MSGLIM; i++)
366 kfree(monpriv->msg_array[i]);
367 kfree(monpriv);
368 clear_bit(MON_IN_USE, &mon_in_use);
369 return 0;
370}
371
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800372static ssize_t mon_read(struct file *filp, char __user *data,
373 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct mon_private *monpriv = filp->private_data;
376 struct mon_msg *monmsg;
377 int ret;
378 u32 mce_start;
379
380 monmsg = mon_next_message(monpriv);
381 if (IS_ERR(monmsg))
382 return PTR_ERR(monmsg);
383
384 if (!monmsg) {
385 if (filp->f_flags & O_NONBLOCK)
386 return -EAGAIN;
387 ret = wait_event_interruptible(mon_read_wait_queue,
388 atomic_read(&monpriv->read_ready) ||
389 atomic_read(&monpriv->iucv_severed));
390 if (ret)
391 return ret;
392 if (unlikely(atomic_read(&monpriv->iucv_severed)))
393 return -EIO;
394 monmsg = monpriv->msg_array[monpriv->read_index];
395 }
396
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200397 if (!monmsg->pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (mon_check_mca(monmsg))
400 goto reply;
401
402 /* read monitor control element (12 bytes) first */
403 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
404 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
405 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
406 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
407 count);
408 if (ret)
409 return -EFAULT;
410 monmsg->pos += count;
411 if (monmsg->pos == mce_start + 12)
412 monmsg->pos = mon_rec_start(monmsg);
413 goto out_copy;
414 }
415
416 /* read records */
417 if (monmsg->pos <= mon_rec_end(monmsg)) {
418 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
419 + 1);
420 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
421 count);
422 if (ret)
423 return -EFAULT;
424 monmsg->pos += count;
425 if (monmsg->pos > mon_rec_end(monmsg))
426 mon_next_mca(monmsg);
427 goto out_copy;
428 }
429reply:
430 ret = mon_send_reply(monmsg, monpriv);
431 return ret;
432
433out_copy:
434 *ppos += count;
435 return count;
436}
437
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800438static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct mon_private *monpriv = filp->private_data;
441
442 poll_wait(filp, &mon_read_wait_queue, p);
443 if (unlikely(atomic_read(&monpriv->iucv_severed)))
444 return POLLERR;
445 if (atomic_read(&monpriv->read_ready))
446 return POLLIN | POLLRDNORM;
447 return 0;
448}
449
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800450static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .owner = THIS_MODULE,
452 .open = &mon_open,
453 .release = &mon_close,
454 .read = &mon_read,
455 .poll = &mon_poll,
456};
457
458static struct miscdevice mon_dev = {
459 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .fops = &mon_fops,
461 .minor = MISC_DYNAMIC_MINOR,
462};
463
464/******************************************************************************
465 * module init/exit *
466 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800467static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int rc;
470
471 if (!MACHINE_IS_VM) {
472 P_ERROR("not running under z/VM, driver not loaded\n");
473 return -ENODEV;
474 }
475
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800476 /*
477 * Register with IUCV and connect to *MONITOR service
478 */
479 rc = iucv_register(&monreader_iucv_handler, 1);
480 if (rc) {
481 P_ERROR("failed to register with iucv driver\n");
482 return rc;
483 }
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 rc = segment_type(mon_dcss_name);
486 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200487 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800488 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 if (rc != SEG_TYPE_SC) {
491 P_ERROR("segment %s has unsupported type, should be SC\n",
492 mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800493 rc = -EINVAL;
494 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
498 &mon_dcss_start, &mon_dcss_end);
499 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200500 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800501 rc = -EINVAL;
502 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
505
506 rc = misc_register(&mon_dev);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200507 if (rc < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510
511out:
512 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800513out_iucv:
514 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return rc;
516}
517
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800518static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 segment_unload(mon_dcss_name);
521 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800522 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return;
524}
525
526
527module_init(mon_init);
528module_exit(mon_exit);
529
530module_param_string(mondcss, mon_dcss_name, 9, 0444);
531MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
532 "service, max. 8 chars. Default is MONDCSS");
533
534MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
535MODULE_DESCRIPTION("Character device driver for reading z/VM "
536 "monitor service records.");
537MODULE_LICENSE("GPL");