blob: 33e96484d54fd1bf5d28aeb369751eed9dbef539 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Character device driver for reading z/VM *MONITOR service records.
3 *
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +02004 * Copyright IBM Corp. 2004, 2009
5 *
6 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Gerald Schaefera4f5a292008-12-25 13:39:42 +01009#define KMSG_COMPONENT "monreader"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/init.h>
15#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>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020022#include <linux/poll.h>
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +020023#include <linux/device.h>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020024#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/ebcdic.h>
27#include <asm/extmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define MON_COLLECT_SAMPLE 0x80
31#define MON_COLLECT_EVENT 0x40
32#define MON_SERVICE "*MONITOR"
33#define MON_IN_USE 0x01
34#define MON_MSGLIM 255
35
36static char mon_dcss_name[9] = "MONDCSS\0";
37
38struct mon_msg {
39 u32 pos;
40 u32 mca_offset;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080041 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 char msglim_reached;
43 char replied_msglim;
44};
45
46struct mon_private {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -080047 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct mon_msg *msg_array[MON_MSGLIM];
49 unsigned int write_index;
50 unsigned int read_index;
51 atomic_t msglim_count;
52 atomic_t read_ready;
53 atomic_t iucv_connected;
54 atomic_t iucv_severed;
55};
56
57static unsigned long mon_in_use = 0;
58
59static unsigned long mon_dcss_start;
60static unsigned long mon_dcss_end;
61
62static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
63static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static u8 user_data_connect[16] = {
66 /* Version code, must be 0x01 for shared mode */
67 0x01,
68 /* what to collect */
69 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
70 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73};
74
75static u8 user_data_sever[16] = {
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78};
79
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +020080static struct device *monreader_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
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 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800285 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800289 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 monpriv = mon_alloc_mem();
291 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800292 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800295 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800297 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
298 if (!monpriv->path)
299 goto out_priv;
300 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
301 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100303 pr_err("Connecting to the z/VM *MONITOR system service "
304 "failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800306 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 /*
309 * Wait for connection confirmation
310 */
311 wait_event(mon_conn_wait_queue,
312 atomic_read(&monpriv->iucv_connected) ||
313 atomic_read(&monpriv->iucv_severed));
314 if (atomic_read(&monpriv->iucv_severed)) {
315 atomic_set(&monpriv->iucv_severed, 0);
316 atomic_set(&monpriv->iucv_connected, 0);
317 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800318 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 filp->private_data = monpriv;
Heiko Carstens99357742009-07-07 16:37:04 +0200321 dev_set_drvdata(monreader_device, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return nonseekable_open(inode, filp);
323
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800324out_path:
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200325 iucv_path_free(monpriv->path);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800326out_priv:
327 mon_free_mem(monpriv);
328out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800330out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return rc;
332}
333
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800334static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int rc, i;
337 struct mon_private *monpriv = filp->private_data;
338
339 /*
340 * Close IUCV connection and unregister
341 */
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200342 if (monpriv->path) {
343 rc = iucv_path_sever(monpriv->path, user_data_sever);
344 if (rc)
345 pr_warning("Disconnecting the z/VM *MONITOR system "
346 "service failed with rc=%i\n", rc);
347 iucv_path_free(monpriv->path);
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 atomic_set(&monpriv->iucv_severed, 0);
351 atomic_set(&monpriv->iucv_connected, 0);
352 atomic_set(&monpriv->read_ready, 0);
353 atomic_set(&monpriv->msglim_count, 0);
354 monpriv->write_index = 0;
355 monpriv->read_index = 0;
Gerald Schaeferccaf6552009-11-13 15:43:51 +0100356 dev_set_drvdata(monreader_device, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 for (i = 0; i < MON_MSGLIM; i++)
359 kfree(monpriv->msg_array[i]);
360 kfree(monpriv);
361 clear_bit(MON_IN_USE, &mon_in_use);
362 return 0;
363}
364
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800365static ssize_t mon_read(struct file *filp, char __user *data,
366 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct mon_private *monpriv = filp->private_data;
369 struct mon_msg *monmsg;
370 int ret;
371 u32 mce_start;
372
373 monmsg = mon_next_message(monpriv);
374 if (IS_ERR(monmsg))
375 return PTR_ERR(monmsg);
376
377 if (!monmsg) {
378 if (filp->f_flags & O_NONBLOCK)
379 return -EAGAIN;
380 ret = wait_event_interruptible(mon_read_wait_queue,
381 atomic_read(&monpriv->read_ready) ||
382 atomic_read(&monpriv->iucv_severed));
383 if (ret)
384 return ret;
385 if (unlikely(atomic_read(&monpriv->iucv_severed)))
386 return -EIO;
387 monmsg = monpriv->msg_array[monpriv->read_index];
388 }
389
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200390 if (!monmsg->pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (mon_check_mca(monmsg))
393 goto reply;
394
395 /* read monitor control element (12 bytes) first */
396 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
397 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
398 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
399 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
400 count);
401 if (ret)
402 return -EFAULT;
403 monmsg->pos += count;
404 if (monmsg->pos == mce_start + 12)
405 monmsg->pos = mon_rec_start(monmsg);
406 goto out_copy;
407 }
408
409 /* read records */
410 if (monmsg->pos <= mon_rec_end(monmsg)) {
411 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
412 + 1);
413 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
414 count);
415 if (ret)
416 return -EFAULT;
417 monmsg->pos += count;
418 if (monmsg->pos > mon_rec_end(monmsg))
419 mon_next_mca(monmsg);
420 goto out_copy;
421 }
422reply:
423 ret = mon_send_reply(monmsg, monpriv);
424 return ret;
425
426out_copy:
427 *ppos += count;
428 return count;
429}
430
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800431static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct mon_private *monpriv = filp->private_data;
434
435 poll_wait(filp, &mon_read_wait_queue, p);
436 if (unlikely(atomic_read(&monpriv->iucv_severed)))
437 return POLLERR;
438 if (atomic_read(&monpriv->read_ready))
439 return POLLIN | POLLRDNORM;
440 return 0;
441}
442
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800443static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .owner = THIS_MODULE,
445 .open = &mon_open,
446 .release = &mon_close,
447 .read = &mon_read,
448 .poll = &mon_poll,
449};
450
451static struct miscdevice mon_dev = {
452 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 .fops = &mon_fops,
454 .minor = MISC_DYNAMIC_MINOR,
455};
456
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200457
458/******************************************************************************
459 * suspend / resume *
460 *****************************************************************************/
461static int monreader_freeze(struct device *dev)
462{
Heiko Carstens99357742009-07-07 16:37:04 +0200463 struct mon_private *monpriv = dev_get_drvdata(dev);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200464 int rc;
465
466 if (!monpriv)
467 return 0;
468 if (monpriv->path) {
469 rc = iucv_path_sever(monpriv->path, user_data_sever);
470 if (rc)
471 pr_warning("Disconnecting the z/VM *MONITOR system "
472 "service failed with rc=%i\n", rc);
473 iucv_path_free(monpriv->path);
474 }
475 atomic_set(&monpriv->iucv_severed, 0);
476 atomic_set(&monpriv->iucv_connected, 0);
477 atomic_set(&monpriv->read_ready, 0);
478 atomic_set(&monpriv->msglim_count, 0);
479 monpriv->write_index = 0;
480 monpriv->read_index = 0;
481 monpriv->path = NULL;
482 return 0;
483}
484
485static int monreader_thaw(struct device *dev)
486{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200487 struct mon_private *monpriv = dev_get_drvdata(dev);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200488 int rc;
489
490 if (!monpriv)
491 return 0;
492 rc = -ENOMEM;
493 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
494 if (!monpriv->path)
495 goto out;
496 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
497 MON_SERVICE, NULL, user_data_connect, monpriv);
498 if (rc) {
499 pr_err("Connecting to the z/VM *MONITOR system service "
500 "failed with rc=%i\n", rc);
501 goto out_path;
502 }
503 wait_event(mon_conn_wait_queue,
504 atomic_read(&monpriv->iucv_connected) ||
505 atomic_read(&monpriv->iucv_severed));
506 if (atomic_read(&monpriv->iucv_severed))
507 goto out_path;
508 return 0;
509out_path:
510 rc = -EIO;
511 iucv_path_free(monpriv->path);
512 monpriv->path = NULL;
513out:
514 atomic_set(&monpriv->iucv_severed, 1);
515 return rc;
516}
517
518static int monreader_restore(struct device *dev)
519{
520 int rc;
521
522 segment_unload(mon_dcss_name);
523 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
524 &mon_dcss_start, &mon_dcss_end);
525 if (rc < 0) {
526 segment_warning(rc, mon_dcss_name);
527 panic("fatal monreader resume error: no monitor dcss\n");
528 }
529 return monreader_thaw(dev);
530}
531
Alexey Dobriyan47145212009-12-14 18:00:08 -0800532static const struct dev_pm_ops monreader_pm_ops = {
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200533 .freeze = monreader_freeze,
534 .thaw = monreader_thaw,
535 .restore = monreader_restore,
536};
537
538static struct device_driver monreader_driver = {
539 .name = "monreader",
540 .bus = &iucv_bus,
541 .pm = &monreader_pm_ops,
542};
543
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/******************************************************************************
546 * module init/exit *
547 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800548static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 int rc;
551
552 if (!MACHINE_IS_VM) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100553 pr_err("The z/VM *MONITOR record device driver cannot be "
554 "loaded without z/VM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENODEV;
556 }
557
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800558 /*
559 * Register with IUCV and connect to *MONITOR service
560 */
561 rc = iucv_register(&monreader_iucv_handler, 1);
562 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100563 pr_err("The z/VM *MONITOR record device driver failed to "
564 "register with IUCV\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800565 return rc;
566 }
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800567
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200568 rc = driver_register(&monreader_driver);
569 if (rc)
570 goto out_iucv;
571 monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL);
572 if (!monreader_device)
573 goto out_driver;
574 dev_set_name(monreader_device, "monreader-dev");
575 monreader_device->bus = &iucv_bus;
576 monreader_device->parent = iucv_root;
577 monreader_device->driver = &monreader_driver;
578 monreader_device->release = (void (*)(struct device *))kfree;
579 rc = device_register(monreader_device);
580 if (rc) {
Sebastian Ottc6304932009-09-11 10:28:38 +0200581 put_device(monreader_device);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200582 goto out_driver;
583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 rc = segment_type(mon_dcss_name);
586 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200587 segment_warning(rc, mon_dcss_name);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200588 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 if (rc != SEG_TYPE_SC) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100591 pr_err("The specified *MONITOR DCSS %s does not have the "
592 "required type SC\n", mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800593 rc = -EINVAL;
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200594 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
598 &mon_dcss_start, &mon_dcss_end);
599 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200600 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800601 rc = -EINVAL;
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200602 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
605
Gerald Schaefer19634032009-12-07 12:52:17 +0100606 /*
607 * misc_register() has to be the last action in module_init(), because
608 * file operations will be available right after this.
609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 rc = misc_register(&mon_dev);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200611 if (rc < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614
615out:
616 segment_unload(mon_dcss_name);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200617out_device:
618 device_unregister(monreader_device);
619out_driver:
620 driver_unregister(&monreader_driver);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800621out_iucv:
622 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return rc;
624}
625
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800626static void __exit mon_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 segment_unload(mon_dcss_name);
629 WARN_ON(misc_deregister(&mon_dev) != 0);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200630 device_unregister(monreader_device);
631 driver_unregister(&monreader_driver);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800632 iucv_unregister(&monreader_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return;
634}
635
636
637module_init(mon_init);
638module_exit(mon_exit);
639
640module_param_string(mondcss, mon_dcss_name, 9, 0444);
641MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
642 "service, max. 8 chars. Default is MONDCSS");
643
644MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
645MODULE_DESCRIPTION("Character device driver for reading z/VM "
646 "monitor service records.");
647MODULE_LICENSE("GPL");