blob: 66e21dd23154b4c5c27b0c254a3bfd348cc88d48 [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>
Arnd Bergmann6ce46a42008-05-20 19:16:17 +020015#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/miscdevice.h>
20#include <linux/ctype.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020023#include <linux/poll.h>
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +020024#include <linux/device.h>
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +020025#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
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +020081static struct device *monreader_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/******************************************************************************
84 * helper functions *
85 *****************************************************************************/
86/*
87 * Create the 8 bytes EBCDIC DCSS segment name from
88 * an ASCII name, incl. padding
89 */
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +020090static void dcss_mkname(char *ascii_name, char *ebcdic_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int i;
93
94 for (i = 0; i < 8; i++) {
95 if (ascii_name[i] == '\0')
96 break;
97 ebcdic_name[i] = toupper(ascii_name[i]);
98 };
99 for (; i < 8; i++)
100 ebcdic_name[i] = ' ';
101 ASCEBC(ebcdic_name, 8);
102}
103
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800104static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800106 return *(u32 *) &monmsg->msg.rmmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800109static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800111 return *(u32 *) &monmsg->msg.rmmsg[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800114static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
117}
118
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800119static inline u32 mon_mca_size(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
122}
123
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800124static inline u32 mon_rec_start(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
127}
128
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800129static inline u32 mon_rec_end(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
132}
133
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200134static int mon_check_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
137 (mon_rec_start(monmsg) < mon_dcss_start) ||
138 (mon_rec_end(monmsg) > mon_dcss_end) ||
139 (mon_mca_type(monmsg, 0) == 0) ||
140 (mon_mca_size(monmsg) % 12 != 0) ||
141 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
142 (mon_mca_end(monmsg) > mon_dcss_end) ||
143 (mon_mca_start(monmsg) < mon_dcss_start) ||
144 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 0;
147}
148
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200149static int mon_send_reply(struct mon_msg *monmsg,
150 struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int rc;
153
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800154 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
155 IUCV_IPRMDATA, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 atomic_dec(&monpriv->msglim_count);
157 if (likely(!monmsg->msglim_reached)) {
158 monmsg->pos = 0;
159 monmsg->mca_offset = 0;
160 monpriv->read_index = (monpriv->read_index + 1) %
161 MON_MSGLIM;
162 atomic_dec(&monpriv->read_ready);
163 } else
164 monmsg->replied_msglim = 1;
165 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100166 pr_err("Reading monitor data failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -EIO;
168 }
169 return 0;
170}
171
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200172static void mon_free_mem(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800174 int i;
175
176 for (i = 0; i < MON_MSGLIM; i++)
177 if (monpriv->msg_array[i])
178 kfree(monpriv->msg_array[i]);
179 kfree(monpriv);
180}
181
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200182static struct mon_private *mon_alloc_mem(void)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800183{
184 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct mon_private *monpriv;
186
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800187 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200188 if (!monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 for (i = 0; i < MON_MSGLIM; i++) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800191 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 GFP_KERNEL);
193 if (!monpriv->msg_array[i]) {
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800194 mon_free_mem(monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return NULL;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 return monpriv;
199}
200
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800201static inline void mon_next_mca(struct mon_msg *monmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
204 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 monmsg->mca_offset += 12;
206 monmsg->pos = 0;
207}
208
Martin Schwidefsky9b0c4552007-05-10 15:45:44 +0200209static struct mon_msg *mon_next_message(struct mon_private *monpriv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct mon_msg *monmsg;
212
213 if (!atomic_read(&monpriv->read_ready))
214 return NULL;
215 monmsg = monpriv->msg_array[monpriv->read_index];
216 if (unlikely(monmsg->replied_msglim)) {
217 monmsg->replied_msglim = 0;
218 monmsg->msglim_reached = 0;
219 monmsg->pos = 0;
220 monmsg->mca_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 monpriv->read_index = (monpriv->read_index + 1) %
222 MON_MSGLIM;
223 atomic_dec(&monpriv->read_ready);
224 return ERR_PTR(-EOVERFLOW);
225 }
226 return monmsg;
227}
228
229
230/******************************************************************************
231 * IUCV handler *
232 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800233static void mon_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800235 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 atomic_set(&monpriv->iucv_connected, 1);
238 wake_up(&mon_conn_wait_queue);
239}
240
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800241static void mon_iucv_path_severed(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
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100245 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
246 ipuser[0]);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800247 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 atomic_set(&monpriv->iucv_severed, 1);
249 wake_up(&mon_conn_wait_queue);
250 wake_up_interruptible(&mon_read_wait_queue);
251}
252
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800253static void mon_iucv_message_pending(struct iucv_path *path,
254 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800256 struct mon_private *monpriv = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800258 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
259 msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100261 pr_warning("The read queue for monitor data is full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
263 }
264 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
265 atomic_inc(&monpriv->read_ready);
266 wake_up_interruptible(&mon_read_wait_queue);
267}
268
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800269static struct iucv_handler monreader_iucv_handler = {
270 .path_complete = mon_iucv_path_complete,
271 .path_severed = mon_iucv_path_severed,
272 .message_pending = mon_iucv_message_pending,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
275/******************************************************************************
276 * file operations *
277 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800278static int mon_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct mon_private *monpriv;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800281 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /*
284 * only one user allowed
285 */
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200286 lock_kernel();
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800287 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800291 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 monpriv = mon_alloc_mem();
293 if (!monpriv)
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800294 goto out_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800297 * Connect to *MONITOR service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800299 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
300 if (!monpriv->path)
301 goto out_priv;
302 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
303 MON_SERVICE, NULL, user_data_connect, monpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100305 pr_err("Connecting to the z/VM *MONITOR system service "
306 "failed with rc=%i\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800308 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 /*
311 * Wait for connection confirmation
312 */
313 wait_event(mon_conn_wait_queue,
314 atomic_read(&monpriv->iucv_connected) ||
315 atomic_read(&monpriv->iucv_severed));
316 if (atomic_read(&monpriv->iucv_severed)) {
317 atomic_set(&monpriv->iucv_severed, 0);
318 atomic_set(&monpriv->iucv_connected, 0);
319 rc = -EIO;
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800320 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 filp->private_data = monpriv;
Heiko Carstens99357742009-07-07 16:37:04 +0200323 dev_set_drvdata(monreader_device, monpriv);
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200324 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return nonseekable_open(inode, filp);
326
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800327out_path:
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200328 iucv_path_free(monpriv->path);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800329out_priv:
330 mon_free_mem(monpriv);
331out_use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 clear_bit(MON_IN_USE, &mon_in_use);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800333out:
Arnd Bergmann6ce46a42008-05-20 19:16:17 +0200334 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return rc;
336}
337
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800338static int mon_close(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int rc, i;
341 struct mon_private *monpriv = filp->private_data;
342
343 /*
344 * Close IUCV connection and unregister
345 */
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200346 if (monpriv->path) {
347 rc = iucv_path_sever(monpriv->path, user_data_sever);
348 if (rc)
349 pr_warning("Disconnecting the z/VM *MONITOR system "
350 "service failed with rc=%i\n", rc);
351 iucv_path_free(monpriv->path);
352 }
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;
Gerald Schaeferccaf6552009-11-13 15:43:51 +0100360 dev_set_drvdata(monreader_device, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 for (i = 0; i < MON_MSGLIM; i++)
363 kfree(monpriv->msg_array[i]);
364 kfree(monpriv);
365 clear_bit(MON_IN_USE, &mon_in_use);
366 return 0;
367}
368
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800369static ssize_t mon_read(struct file *filp, char __user *data,
370 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct mon_private *monpriv = filp->private_data;
373 struct mon_msg *monmsg;
374 int ret;
375 u32 mce_start;
376
377 monmsg = mon_next_message(monpriv);
378 if (IS_ERR(monmsg))
379 return PTR_ERR(monmsg);
380
381 if (!monmsg) {
382 if (filp->f_flags & O_NONBLOCK)
383 return -EAGAIN;
384 ret = wait_event_interruptible(mon_read_wait_queue,
385 atomic_read(&monpriv->read_ready) ||
386 atomic_read(&monpriv->iucv_severed));
387 if (ret)
388 return ret;
389 if (unlikely(atomic_read(&monpriv->iucv_severed)))
390 return -EIO;
391 monmsg = monpriv->msg_array[monpriv->read_index];
392 }
393
Gerald Schaefer2ca5b6e2008-07-14 09:59:35 +0200394 if (!monmsg->pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (mon_check_mca(monmsg))
397 goto reply;
398
399 /* read monitor control element (12 bytes) first */
400 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
401 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
402 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
403 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
404 count);
405 if (ret)
406 return -EFAULT;
407 monmsg->pos += count;
408 if (monmsg->pos == mce_start + 12)
409 monmsg->pos = mon_rec_start(monmsg);
410 goto out_copy;
411 }
412
413 /* read records */
414 if (monmsg->pos <= mon_rec_end(monmsg)) {
415 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
416 + 1);
417 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
418 count);
419 if (ret)
420 return -EFAULT;
421 monmsg->pos += count;
422 if (monmsg->pos > mon_rec_end(monmsg))
423 mon_next_mca(monmsg);
424 goto out_copy;
425 }
426reply:
427 ret = mon_send_reply(monmsg, monpriv);
428 return ret;
429
430out_copy:
431 *ppos += count;
432 return count;
433}
434
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800435static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct mon_private *monpriv = filp->private_data;
438
439 poll_wait(filp, &mon_read_wait_queue, p);
440 if (unlikely(atomic_read(&monpriv->iucv_severed)))
441 return POLLERR;
442 if (atomic_read(&monpriv->read_ready))
443 return POLLIN | POLLRDNORM;
444 return 0;
445}
446
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800447static const struct file_operations mon_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .owner = THIS_MODULE,
449 .open = &mon_open,
450 .release = &mon_close,
451 .read = &mon_read,
452 .poll = &mon_poll,
453};
454
455static struct miscdevice mon_dev = {
456 .name = "monreader",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .fops = &mon_fops,
458 .minor = MISC_DYNAMIC_MINOR,
459};
460
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200461
462/******************************************************************************
463 * suspend / resume *
464 *****************************************************************************/
465static int monreader_freeze(struct device *dev)
466{
Heiko Carstens99357742009-07-07 16:37:04 +0200467 struct mon_private *monpriv = dev_get_drvdata(dev);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200468 int rc;
469
470 if (!monpriv)
471 return 0;
472 if (monpriv->path) {
473 rc = iucv_path_sever(monpriv->path, user_data_sever);
474 if (rc)
475 pr_warning("Disconnecting the z/VM *MONITOR system "
476 "service failed with rc=%i\n", rc);
477 iucv_path_free(monpriv->path);
478 }
479 atomic_set(&monpriv->iucv_severed, 0);
480 atomic_set(&monpriv->iucv_connected, 0);
481 atomic_set(&monpriv->read_ready, 0);
482 atomic_set(&monpriv->msglim_count, 0);
483 monpriv->write_index = 0;
484 monpriv->read_index = 0;
485 monpriv->path = NULL;
486 return 0;
487}
488
489static int monreader_thaw(struct device *dev)
490{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200491 struct mon_private *monpriv = dev_get_drvdata(dev);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200492 int rc;
493
494 if (!monpriv)
495 return 0;
496 rc = -ENOMEM;
497 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
498 if (!monpriv->path)
499 goto out;
500 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
501 MON_SERVICE, NULL, user_data_connect, monpriv);
502 if (rc) {
503 pr_err("Connecting to the z/VM *MONITOR system service "
504 "failed with rc=%i\n", rc);
505 goto out_path;
506 }
507 wait_event(mon_conn_wait_queue,
508 atomic_read(&monpriv->iucv_connected) ||
509 atomic_read(&monpriv->iucv_severed));
510 if (atomic_read(&monpriv->iucv_severed))
511 goto out_path;
512 return 0;
513out_path:
514 rc = -EIO;
515 iucv_path_free(monpriv->path);
516 monpriv->path = NULL;
517out:
518 atomic_set(&monpriv->iucv_severed, 1);
519 return rc;
520}
521
522static int monreader_restore(struct device *dev)
523{
524 int rc;
525
526 segment_unload(mon_dcss_name);
527 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
528 &mon_dcss_start, &mon_dcss_end);
529 if (rc < 0) {
530 segment_warning(rc, mon_dcss_name);
531 panic("fatal monreader resume error: no monitor dcss\n");
532 }
533 return monreader_thaw(dev);
534}
535
536static struct dev_pm_ops monreader_pm_ops = {
537 .freeze = monreader_freeze,
538 .thaw = monreader_thaw,
539 .restore = monreader_restore,
540};
541
542static struct device_driver monreader_driver = {
543 .name = "monreader",
544 .bus = &iucv_bus,
545 .pm = &monreader_pm_ops,
546};
547
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/******************************************************************************
550 * module init/exit *
551 *****************************************************************************/
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800552static int __init mon_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int rc;
555
556 if (!MACHINE_IS_VM) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100557 pr_err("The z/VM *MONITOR record device driver cannot be "
558 "loaded without z/VM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENODEV;
560 }
561
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800562 /*
563 * Register with IUCV and connect to *MONITOR service
564 */
565 rc = iucv_register(&monreader_iucv_handler, 1);
566 if (rc) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100567 pr_err("The z/VM *MONITOR record device driver failed to "
568 "register with IUCV\n");
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800569 return rc;
570 }
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800571
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200572 rc = driver_register(&monreader_driver);
573 if (rc)
574 goto out_iucv;
575 monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL);
576 if (!monreader_device)
577 goto out_driver;
578 dev_set_name(monreader_device, "monreader-dev");
579 monreader_device->bus = &iucv_bus;
580 monreader_device->parent = iucv_root;
581 monreader_device->driver = &monreader_driver;
582 monreader_device->release = (void (*)(struct device *))kfree;
583 rc = device_register(monreader_device);
584 if (rc) {
Sebastian Ottc6304932009-09-11 10:28:38 +0200585 put_device(monreader_device);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200586 goto out_driver;
587 }
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = segment_type(mon_dcss_name);
590 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200591 segment_warning(rc, mon_dcss_name);
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200592 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 if (rc != SEG_TYPE_SC) {
Gerald Schaefera4f5a292008-12-25 13:39:42 +0100595 pr_err("The specified *MONITOR DCSS %s does not have the "
596 "required type SC\n", mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800597 rc = -EINVAL;
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200598 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
602 &mon_dcss_start, &mon_dcss_end);
603 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200604 segment_warning(rc, mon_dcss_name);
Martin Schwidefskyc667aac2007-02-08 13:38:11 -0800605 rc = -EINVAL;
Gerald Schaefer2b1e3e52009-06-16 10:30:50 +0200606 goto out_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
609
610 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");