blob: f0e4c96afbf803f12c17f296d0fd645a57cb93b4 [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>
13#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/miscdevice.h>
17#include <linux/ctype.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.h>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020020#include <linux/poll.h>
21#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include <asm/ebcdic.h>
24#include <asm/extmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26//#define MON_DEBUG /* Debug messages on/off */
27
28#define MON_NAME "monreader"
29
30#define P_INFO(x...) printk(KERN_INFO MON_NAME " info: " x)
31#define P_ERROR(x...) printk(KERN_ERR MON_NAME " error: " x)
32#define P_WARNING(x...) printk(KERN_WARNING MON_NAME " warning: " x)
33
34#ifdef MON_DEBUG
35#define P_DEBUG(x...) printk(KERN_DEBUG MON_NAME " debug: " x)
36#else
37#define P_DEBUG(x...) do {} while (0)
38#endif
39
40#define MON_COLLECT_SAMPLE 0x80
41#define MON_COLLECT_EVENT 0x40
42#define MON_SERVICE "*MONITOR"
43#define MON_IN_USE 0x01
44#define MON_MSGLIM 255
45
46static char mon_dcss_name[9] = "MONDCSS\0";
47
48struct mon_msg {
49 u32 pos;
50 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080051 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 char msglim_reached;
53 char replied_msglim;
54};
55
56struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080057 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct mon_msg *msg_array[MON_MSGLIM];
59 unsigned int write_index;
60 unsigned int read_index;
61 atomic_t msglim_count;
62 atomic_t read_ready;
63 atomic_t iucv_connected;
64 atomic_t iucv_severed;
65};
66
67static unsigned long mon_in_use = 0;
68
69static unsigned long mon_dcss_start;
70static unsigned long mon_dcss_end;
71
72static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
73static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static u8 user_data_connect[16] = {
76 /* Version code, must be 0x01 for shared mode */
77 0x01,
78 /* what to collect */
79 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
80 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83};
84
85static u8 user_data_sever[16] = {
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88};
89
90
91/******************************************************************************
92 * helper functions *
93 *****************************************************************************/
94/*
95 * Create the 8 bytes EBCDIC DCSS segment name from
96 * an ASCII name, incl. padding
97 */
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +020098static void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int i;
101
102 for (i = 0; i < 8; i++) {
103 if (ascii_name[i] == '\0')
104 break;
105 ebcdic_name[i] = toupper(ascii_name[i]);
106 };
107 for (; i < 8; i++)
108 ebcdic_name[i] = ' ';
109 ASCEBC(ebcdic_name, 8);
110}
111
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800112static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800114 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800117static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800119 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800122static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
125}
126
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800127static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
130}
131
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800132static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
135}
136
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800137static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
140}
141
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200142static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
145 (mon_rec_start(monmsg) < mon_dcss_start) ||
146 (mon_rec_end(monmsg) > mon_dcss_end) ||
147 (mon_mca_type(monmsg, 0) == 0) ||
148 (mon_mca_size(monmsg) % 12 != 0) ||
149 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
150 (mon_mca_end(monmsg) > mon_dcss_end) ||
151 (mon_mca_start(monmsg) < mon_dcss_start) ||
152 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155}
156
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200157static int mon_send_reply(struct mon_msg *monmsg,
158 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int rc;
161
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800162 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
163 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 atomic_dec(&monpriv->msglim_count);
165 if (likely(!monmsg->msglim_reached)) {
166 monmsg->pos = 0;
167 monmsg->mca_offset = 0;
168 monpriv->read_index = (monpriv->read_index + 1) %
169 MON_MSGLIM;
170 atomic_dec(&monpriv->read_ready);
171 } else
172 monmsg->replied_msglim = 1;
173 if (rc) {
174 P_ERROR("read, IUCV reply failed with rc = %i\n\n", rc);
175 return -EIO;
176 }
177 return 0;
178}
179
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200180static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800182 int i;
183
184 for (i = 0; i < MON_MSGLIM; i++)
185 if (monpriv->msg_array[i])
186 kfree(monpriv->msg_array[i]);
187 kfree(monpriv);
188}
189
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200190static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800191{
192 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct mon_private *monpriv;
194
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800195 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200196 if (!monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800199 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 GFP_KERNEL);
201 if (!monpriv->msg_array[i]) {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800202 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return NULL;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 return monpriv;
207}
208
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800209static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 monmsg->mca_offset += 12;
214 monmsg->pos = 0;
215}
216
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200217static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct mon_msg *monmsg;
220
221 if (!atomic_read(&monpriv->read_ready))
222 return NULL;
223 monmsg = monpriv->msg_array[monpriv->read_index];
224 if (unlikely(monmsg->replied_msglim)) {
225 monmsg->replied_msglim = 0;
226 monmsg->msglim_reached = 0;
227 monmsg->pos = 0;
228 monmsg->mca_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 monpriv->read_index = (monpriv->read_index + 1) %
230 MON_MSGLIM;
231 atomic_dec(&monpriv->read_ready);
232 return ERR_PTR(-EOVERFLOW);
233 }
234 return monmsg;
235}
236
237
238/******************************************************************************
239 * IUCV handler *
240 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800241static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800243 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_set(&monpriv->iucv_connected, 1);
246 wake_up(&mon_conn_wait_queue);
247}
248
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800249static void mon_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800251 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800253 P_ERROR("IUCV connection severed with rc = 0x%X\n", ipuser[0]);
254 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 atomic_set(&monpriv->iucv_severed, 1);
256 wake_up(&mon_conn_wait_queue);
257 wake_up_interruptible(&mon_read_wait_queue);
258}
259
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800260static void mon_iucv_message_pending(struct iucv_path *path,
261 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800263 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800265 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
266 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
268 P_WARNING("IUCV message pending, message limit (%i) reached\n",
269 MON_MSGLIM);
270 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
271 }
272 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
273 atomic_inc(&monpriv->read_ready);
274 wake_up_interruptible(&mon_read_wait_queue);
275}
276
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800277static struct iucv_handler monreader_iucv_handler = {
278 .path_complete = mon_iucv_path_complete,
279 .path_severed = mon_iucv_path_severed,
280 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
283/******************************************************************************
284 * file operations *
285 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800286static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800289 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /*
292 * only one user allowed
293 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800294 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800298 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 monpriv = mon_alloc_mem();
300 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800301 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800304 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800306 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
307 if (!monpriv->path)
308 goto out_priv;
309 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
310 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (rc) {
312 P_ERROR("iucv connection to *MONITOR failed with "
313 "IPUSER SEVER code = %i\n", rc);
314 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800315 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 /*
318 * Wait for connection confirmation
319 */
320 wait_event(mon_conn_wait_queue,
321 atomic_read(&monpriv->iucv_connected) ||
322 atomic_read(&monpriv->iucv_severed));
323 if (atomic_read(&monpriv->iucv_severed)) {
324 atomic_set(&monpriv->iucv_severed, 0);
325 atomic_set(&monpriv->iucv_connected, 0);
326 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800327 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 filp->private_data = monpriv;
330 return nonseekable_open(inode, filp);
331
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800332out_path:
333 kfree(monpriv->path);
334out_priv:
335 mon_free_mem(monpriv);
336out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800338out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return rc;
340}
341
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800342static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 int rc, i;
345 struct mon_private *monpriv = filp->private_data;
346
347 /*
348 * Close IUCV connection and unregister
349 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800350 rc = iucv_path_sever(monpriv->path, user_data_sever);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (rc)
352 P_ERROR("close, iucv_sever failed with rc = %i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 atomic_set(&monpriv->iucv_severed, 0);
355 atomic_set(&monpriv->iucv_connected, 0);
356 atomic_set(&monpriv->read_ready, 0);
357 atomic_set(&monpriv->msglim_count, 0);
358 monpriv->write_index = 0;
359 monpriv->read_index = 0;
360
361 for (i = 0; i < MON_MSGLIM; i++)
362 kfree(monpriv->msg_array[i]);
363 kfree(monpriv);
364 clear_bit(MON_IN_USE, &mon_in_use);
365 return 0;
366}
367
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800368static ssize_t mon_read(struct file *filp, char __user *data,
369 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct mon_private *monpriv = filp->private_data;
372 struct mon_msg *monmsg;
373 int ret;
374 u32 mce_start;
375
376 monmsg = mon_next_message(monpriv);
377 if (IS_ERR(monmsg))
378 return PTR_ERR(monmsg);
379
380 if (!monmsg) {
381 if (filp->f_flags & O_NONBLOCK)
382 return -EAGAIN;
383 ret = wait_event_interruptible(mon_read_wait_queue,
384 atomic_read(&monpriv->read_ready) ||
385 atomic_read(&monpriv->iucv_severed));
386 if (ret)
387 return ret;
388 if (unlikely(atomic_read(&monpriv->iucv_severed)))
389 return -EIO;
390 monmsg = monpriv->msg_array[monpriv->read_index];
391 }
392
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200393 if (!monmsg->pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (mon_check_mca(monmsg))
396 goto reply;
397
398 /* read monitor control element (12 bytes) first */
399 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
400 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
401 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
402 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
403 count);
404 if (ret)
405 return -EFAULT;
406 monmsg->pos += count;
407 if (monmsg->pos == mce_start + 12)
408 monmsg->pos = mon_rec_start(monmsg);
409 goto out_copy;
410 }
411
412 /* read records */
413 if (monmsg->pos <= mon_rec_end(monmsg)) {
414 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
415 + 1);
416 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
417 count);
418 if (ret)
419 return -EFAULT;
420 monmsg->pos += count;
421 if (monmsg->pos > mon_rec_end(monmsg))
422 mon_next_mca(monmsg);
423 goto out_copy;
424 }
425reply:
426 ret = mon_send_reply(monmsg, monpriv);
427 return ret;
428
429out_copy:
430 *ppos += count;
431 return count;
432}
433
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800434static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct mon_private *monpriv = filp->private_data;
437
438 poll_wait(filp, &mon_read_wait_queue, p);
439 if (unlikely(atomic_read(&monpriv->iucv_severed)))
440 return POLLERR;
441 if (atomic_read(&monpriv->read_ready))
442 return POLLIN | POLLRDNORM;
443 return 0;
444}
445
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800446static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 .owner = THIS_MODULE,
448 .open = &mon_open,
449 .release = &mon_close,
450 .read = &mon_read,
451 .poll = &mon_poll,
452};
453
454static struct miscdevice mon_dev = {
455 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .fops = &mon_fops,
457 .minor = MISC_DYNAMIC_MINOR,
458};
459
460/******************************************************************************
461 * module init/exit *
462 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800463static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int rc;
466
467 if (!MACHINE_IS_VM) {
468 P_ERROR("not running under z/VM, driver not loaded\n");
469 return -ENODEV;
470 }
471
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800472 /*
473 * Register with IUCV and connect to *MONITOR service
474 */
475 rc = iucv_register(&monreader_iucv_handler, 1);
476 if (rc) {
477 P_ERROR("failed to register with iucv driver\n");
478 return rc;
479 }
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 rc = segment_type(mon_dcss_name);
482 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200483 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800484 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 if (rc != SEG_TYPE_SC) {
487 P_ERROR("segment %s has unsupported type, should be SC\n",
488 mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800489 rc = -EINVAL;
490 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
494 &mon_dcss_start, &mon_dcss_end);
495 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200496 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800497 rc = -EINVAL;
498 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
501
502 rc = misc_register(&mon_dev);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200503 if (rc < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506
507out:
508 segment_unload(mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800509out_iucv:
510 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return rc;
512}
513
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800514static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 segment_unload(mon_dcss_name);
517 WARN_ON(misc_deregister(&mon_dev) != 0);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800518 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return;
520}
521
522
523module_init(mon_init);
524module_exit(mon_exit);
525
526module_param_string(mondcss, mon_dcss_name, 9, 0444);
527MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
528 "service, max. 8 chars. Default is MONDCSS");
529
530MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
531MODULE_DESCRIPTION("Character device driver for reading z/VM "
532 "monitor service records.");
533MODULE_LICENSE("GPL");