Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Character device driver for reading z/VM *MONITOR service records.
  4 *
  5 * Copyright IBM Corp. 2004, 2009
  6 *
  7 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  8 */
  9
 10#define KMSG_COMPONENT "monreader"
 11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 12
 13#include <linux/module.h>
 14#include <linux/moduleparam.h>
 15#include <linux/init.h>
 16#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>
 23#include <linux/poll.h>
 
 24#include <linux/slab.h>
 25#include <net/iucv/iucv.h>
 26#include <linux/uaccess.h>
 27#include <asm/ebcdic.h>
 28#include <asm/extmem.h>
 29
 30
 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;
 42	struct iucv_message msg;
 43	char msglim_reached;
 44	char replied_msglim;
 45};
 46
 47struct mon_private {
 48	struct iucv_path *path;
 49	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
 66static u8 user_data_connect[16] = {
 67	/* Version code, must be 0x01 for shared mode */
 68	0x01,
 69	/* what to collect */
 70	MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
 71	/* DCSS name in EBCDIC, 8 bytes padded with blanks */
 72	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 73	0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 74};
 75
 76static u8 user_data_sever[16] = {
 77	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 78	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 79};
 80
 
 
 81/******************************************************************************
 82 *                             helper functions                               *
 83 *****************************************************************************/
 84/*
 85 * Create the 8 bytes EBCDIC DCSS segment name from
 86 * an ASCII name, incl. padding
 87 */
 88static void dcss_mkname(char *ascii_name, char *ebcdic_name)
 89{
 90	int i;
 91
 92	for (i = 0; i < 8; i++) {
 93		if (ascii_name[i] == '\0')
 94			break;
 95		ebcdic_name[i] = toupper(ascii_name[i]);
 96	}
 97	for (; i < 8; i++)
 98		ebcdic_name[i] = ' ';
 99	ASCEBC(ebcdic_name, 8);
100}
101
102static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
103{
104	return *(u32 *) &monmsg->msg.rmmsg;
105}
106
107static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
108{
109	return *(u32 *) &monmsg->msg.rmmsg[4];
110}
111
112static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
113{
114	return *((u8 *)__va(mon_mca_start(monmsg)) + monmsg->mca_offset + index);
115}
116
117static inline u32 mon_mca_size(struct mon_msg *monmsg)
118{
119	return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
120}
121
122static inline u32 mon_rec_start(struct mon_msg *monmsg)
123{
124	return *((u32 *)(__va(mon_mca_start(monmsg)) + monmsg->mca_offset + 4));
125}
126
127static inline u32 mon_rec_end(struct mon_msg *monmsg)
128{
129	return *((u32 *)(__va(mon_mca_start(monmsg)) + monmsg->mca_offset + 8));
130}
131
132static int mon_check_mca(struct mon_msg *monmsg)
133{
134	if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
135	    (mon_rec_start(monmsg) < mon_dcss_start) ||
136	    (mon_rec_end(monmsg) > mon_dcss_end) ||
137	    (mon_mca_type(monmsg, 0) == 0) ||
138	    (mon_mca_size(monmsg) % 12 != 0) ||
139	    (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
140	    (mon_mca_end(monmsg) > mon_dcss_end) ||
141	    (mon_mca_start(monmsg) < mon_dcss_start) ||
142	    ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
143		return -EINVAL;
144	return 0;
145}
146
147static int mon_send_reply(struct mon_msg *monmsg,
148			  struct mon_private *monpriv)
149{
150	int rc;
151
152	rc = iucv_message_reply(monpriv->path, &monmsg->msg,
153				IUCV_IPRMDATA, NULL, 0);
154	atomic_dec(&monpriv->msglim_count);
155	if (likely(!monmsg->msglim_reached)) {
156		monmsg->pos = 0;
157		monmsg->mca_offset = 0;
158		monpriv->read_index = (monpriv->read_index + 1) %
159				      MON_MSGLIM;
160		atomic_dec(&monpriv->read_ready);
161	} else
162		monmsg->replied_msglim = 1;
163	if (rc) {
164		pr_err("Reading monitor data failed with rc=%i\n", rc);
165		return -EIO;
166	}
167	return 0;
168}
169
170static void mon_free_mem(struct mon_private *monpriv)
171{
172	int i;
173
174	for (i = 0; i < MON_MSGLIM; i++)
175		kfree(monpriv->msg_array[i]);
176	kfree(monpriv);
177}
178
179static struct mon_private *mon_alloc_mem(void)
180{
181	int i;
182	struct mon_private *monpriv;
183
184	monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
185	if (!monpriv)
186		return NULL;
187	for (i = 0; i < MON_MSGLIM; i++) {
188		monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
189						    GFP_KERNEL);
190		if (!monpriv->msg_array[i]) {
191			mon_free_mem(monpriv);
192			return NULL;
193		}
194	}
195	return monpriv;
196}
197
198static inline void mon_next_mca(struct mon_msg *monmsg)
199{
200	if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
201		return;
202	monmsg->mca_offset += 12;
203	monmsg->pos = 0;
204}
205
206static struct mon_msg *mon_next_message(struct mon_private *monpriv)
207{
208	struct mon_msg *monmsg;
209
210	if (!atomic_read(&monpriv->read_ready))
211		return NULL;
212	monmsg = monpriv->msg_array[monpriv->read_index];
213	if (unlikely(monmsg->replied_msglim)) {
214		monmsg->replied_msglim = 0;
215		monmsg->msglim_reached = 0;
216		monmsg->pos = 0;
217		monmsg->mca_offset = 0;
218		monpriv->read_index = (monpriv->read_index + 1) %
219				      MON_MSGLIM;
220		atomic_dec(&monpriv->read_ready);
221		return ERR_PTR(-EOVERFLOW);
222	}
223	return monmsg;
224}
225
226
227/******************************************************************************
228 *                               IUCV handler                                 *
229 *****************************************************************************/
230static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
231{
232	struct mon_private *monpriv = path->private;
233
234	atomic_set(&monpriv->iucv_connected, 1);
235	wake_up(&mon_conn_wait_queue);
236}
237
238static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
239{
240	struct mon_private *monpriv = path->private;
241
242	pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
243	       ipuser[0]);
244	iucv_path_sever(path, NULL);
245	atomic_set(&monpriv->iucv_severed, 1);
246	wake_up(&mon_conn_wait_queue);
247	wake_up_interruptible(&mon_read_wait_queue);
248}
249
250static void mon_iucv_message_pending(struct iucv_path *path,
251				     struct iucv_message *msg)
252{
253	struct mon_private *monpriv = path->private;
254
255	memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
256	       msg, sizeof(*msg));
257	if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
258		pr_warn("The read queue for monitor data is full\n");
259		monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
260	}
261	monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
262	atomic_inc(&monpriv->read_ready);
263	wake_up_interruptible(&mon_read_wait_queue);
264}
265
266static struct iucv_handler monreader_iucv_handler = {
267	.path_complete	 = mon_iucv_path_complete,
268	.path_severed	 = mon_iucv_path_severed,
269	.message_pending = mon_iucv_message_pending,
270};
271
272/******************************************************************************
273 *                               file operations                              *
274 *****************************************************************************/
275static int mon_open(struct inode *inode, struct file *filp)
276{
277	struct mon_private *monpriv;
278	int rc;
279
280	/*
281	 * only one user allowed
282	 */
283	rc = -EBUSY;
284	if (test_and_set_bit(MON_IN_USE, &mon_in_use))
285		goto out;
286
287	rc = -ENOMEM;
288	monpriv = mon_alloc_mem();
289	if (!monpriv)
290		goto out_use;
291
292	/*
293	 * Connect to *MONITOR service
294	 */
295	monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
296	if (!monpriv->path)
297		goto out_priv;
298	rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
299			       MON_SERVICE, NULL, user_data_connect, monpriv);
300	if (rc) {
301		pr_err("Connecting to the z/VM *MONITOR system service "
302		       "failed with rc=%i\n", rc);
303		rc = -EIO;
304		goto out_path;
305	}
306	/*
307	 * Wait for connection confirmation
308	 */
309	wait_event(mon_conn_wait_queue,
310		   atomic_read(&monpriv->iucv_connected) ||
311		   atomic_read(&monpriv->iucv_severed));
312	if (atomic_read(&monpriv->iucv_severed)) {
313		atomic_set(&monpriv->iucv_severed, 0);
314		atomic_set(&monpriv->iucv_connected, 0);
315		rc = -EIO;
316		goto out_path;
317	}
318	filp->private_data = monpriv;
 
319	return nonseekable_open(inode, filp);
320
321out_path:
322	iucv_path_free(monpriv->path);
323out_priv:
324	mon_free_mem(monpriv);
325out_use:
326	clear_bit(MON_IN_USE, &mon_in_use);
327out:
328	return rc;
329}
330
331static int mon_close(struct inode *inode, struct file *filp)
332{
333	int rc, i;
334	struct mon_private *monpriv = filp->private_data;
335
336	/*
337	 * Close IUCV connection and unregister
338	 */
339	if (monpriv->path) {
340		rc = iucv_path_sever(monpriv->path, user_data_sever);
341		if (rc)
342			pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
343				rc);
344		iucv_path_free(monpriv->path);
345	}
346
347	atomic_set(&monpriv->iucv_severed, 0);
348	atomic_set(&monpriv->iucv_connected, 0);
349	atomic_set(&monpriv->read_ready, 0);
350	atomic_set(&monpriv->msglim_count, 0);
351	monpriv->write_index  = 0;
352	monpriv->read_index   = 0;
 
353
354	for (i = 0; i < MON_MSGLIM; i++)
355		kfree(monpriv->msg_array[i]);
356	kfree(monpriv);
357	clear_bit(MON_IN_USE, &mon_in_use);
358	return 0;
359}
360
361static ssize_t mon_read(struct file *filp, char __user *data,
362			size_t count, loff_t *ppos)
363{
364	struct mon_private *monpriv = filp->private_data;
365	struct mon_msg *monmsg;
366	int ret;
367	u32 mce_start;
368
369	monmsg = mon_next_message(monpriv);
370	if (IS_ERR(monmsg))
371		return PTR_ERR(monmsg);
372
373	if (!monmsg) {
374		if (filp->f_flags & O_NONBLOCK)
375			return -EAGAIN;
376		ret = wait_event_interruptible(mon_read_wait_queue,
377					atomic_read(&monpriv->read_ready) ||
378					atomic_read(&monpriv->iucv_severed));
379		if (ret)
380			return ret;
381		if (unlikely(atomic_read(&monpriv->iucv_severed)))
382			return -EIO;
383		monmsg = monpriv->msg_array[monpriv->read_index];
384	}
385
386	if (!monmsg->pos)
387		monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
388	if (mon_check_mca(monmsg))
389		goto reply;
390
391	/* read monitor control element (12 bytes) first */
392	mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
393	if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
394		count = min(count, (size_t) mce_start + 12 - monmsg->pos);
395		ret = copy_to_user(data, __va(monmsg->pos), count);
 
396		if (ret)
397			return -EFAULT;
398		monmsg->pos += count;
399		if (monmsg->pos == mce_start + 12)
400			monmsg->pos = mon_rec_start(monmsg);
401		goto out_copy;
402	}
403
404	/* read records */
405	if (monmsg->pos <= mon_rec_end(monmsg)) {
406		count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
407					    + 1);
408		ret = copy_to_user(data, __va(monmsg->pos), count);
 
409		if (ret)
410			return -EFAULT;
411		monmsg->pos += count;
412		if (monmsg->pos > mon_rec_end(monmsg))
413			mon_next_mca(monmsg);
414		goto out_copy;
415	}
416reply:
417	ret = mon_send_reply(monmsg, monpriv);
418	return ret;
419
420out_copy:
421	*ppos += count;
422	return count;
423}
424
425static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p)
426{
427	struct mon_private *monpriv = filp->private_data;
428
429	poll_wait(filp, &mon_read_wait_queue, p);
430	if (unlikely(atomic_read(&monpriv->iucv_severed)))
431		return EPOLLERR;
432	if (atomic_read(&monpriv->read_ready))
433		return EPOLLIN | EPOLLRDNORM;
434	return 0;
435}
436
437static const struct file_operations mon_fops = {
438	.owner   = THIS_MODULE,
439	.open    = &mon_open,
440	.release = &mon_close,
441	.read    = &mon_read,
442	.poll    = &mon_poll,
443	.llseek  = noop_llseek,
444};
445
446static struct miscdevice mon_dev = {
447	.name       = "monreader",
448	.fops       = &mon_fops,
449	.minor      = MISC_DYNAMIC_MINOR,
450};
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452/******************************************************************************
453 *                              module init/exit                              *
454 *****************************************************************************/
455static int __init mon_init(void)
456{
457	int rc;
458
459	if (!MACHINE_IS_VM) {
460		pr_err("The z/VM *MONITOR record device driver cannot be "
461		       "loaded without z/VM\n");
462		return -ENODEV;
463	}
464
465	/*
466	 * Register with IUCV and connect to *MONITOR service
467	 */
468	rc = iucv_register(&monreader_iucv_handler, 1);
469	if (rc) {
470		pr_err("The z/VM *MONITOR record device driver failed to "
471		       "register with IUCV\n");
472		return rc;
473	}
474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475	rc = segment_type(mon_dcss_name);
476	if (rc < 0) {
477		segment_warning(rc, mon_dcss_name);
478		goto out_iucv;
479	}
480	if (rc != SEG_TYPE_SC) {
481		pr_err("The specified *MONITOR DCSS %s does not have the "
482		       "required type SC\n", mon_dcss_name);
483		rc = -EINVAL;
484		goto out_iucv;
485	}
486
487	rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
488			  &mon_dcss_start, &mon_dcss_end);
489	if (rc < 0) {
490		segment_warning(rc, mon_dcss_name);
491		rc = -EINVAL;
492		goto out_iucv;
493	}
494	dcss_mkname(mon_dcss_name, &user_data_connect[8]);
495
496	/*
497	 * misc_register() has to be the last action in module_init(), because
498	 * file operations will be available right after this.
499	 */
500	rc = misc_register(&mon_dev);
501	if (rc < 0 )
502		goto out;
503	return 0;
504
505out:
506	segment_unload(mon_dcss_name);
 
 
 
 
507out_iucv:
508	iucv_unregister(&monreader_iucv_handler, 1);
509	return rc;
510}
511
512static void __exit mon_exit(void)
513{
514	segment_unload(mon_dcss_name);
515	misc_deregister(&mon_dev);
 
 
516	iucv_unregister(&monreader_iucv_handler, 1);
517	return;
518}
519
520
521module_init(mon_init);
522module_exit(mon_exit);
523
524module_param_string(mondcss, mon_dcss_name, 9, 0444);
525MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
526		 "service, max. 8 chars. Default is MONDCSS");
527
528MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
529MODULE_DESCRIPTION("Character device driver for reading z/VM "
530		   "monitor service records.");
531MODULE_LICENSE("GPL");
v4.6
 
  1/*
  2 * Character device driver for reading z/VM *MONITOR service records.
  3 *
  4 * Copyright IBM Corp. 2004, 2009
  5 *
  6 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  7 */
  8
  9#define KMSG_COMPONENT "monreader"
 10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
 11
 12#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>
 22#include <linux/poll.h>
 23#include <linux/device.h>
 24#include <linux/slab.h>
 25#include <net/iucv/iucv.h>
 26#include <asm/uaccess.h>
 27#include <asm/ebcdic.h>
 28#include <asm/extmem.h>
 29
 30
 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;
 42	struct iucv_message msg;
 43	char msglim_reached;
 44	char replied_msglim;
 45};
 46
 47struct mon_private {
 48	struct iucv_path *path;
 49	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
 66static 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
 81static struct device *monreader_device;
 82
 83/******************************************************************************
 84 *                             helper functions                               *
 85 *****************************************************************************/
 86/*
 87 * Create the 8 bytes EBCDIC DCSS segment name from
 88 * an ASCII name, incl. padding
 89 */
 90static void dcss_mkname(char *ascii_name, char *ebcdic_name)
 91{
 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
104static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
105{
106	return *(u32 *) &monmsg->msg.rmmsg;
107}
108
109static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
110{
111	return *(u32 *) &monmsg->msg.rmmsg[4];
112}
113
114static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
115{
116	return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
117}
118
119static inline u32 mon_mca_size(struct mon_msg *monmsg)
120{
121	return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
122}
123
124static inline u32 mon_rec_start(struct mon_msg *monmsg)
125{
126	return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
127}
128
129static inline u32 mon_rec_end(struct mon_msg *monmsg)
130{
131	return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
132}
133
134static int mon_check_mca(struct mon_msg *monmsg)
135{
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)))
145		return -EINVAL;
146	return 0;
147}
148
149static int mon_send_reply(struct mon_msg *monmsg,
150			  struct mon_private *monpriv)
151{
152	int rc;
153
154	rc = iucv_message_reply(monpriv->path, &monmsg->msg,
155				IUCV_IPRMDATA, NULL, 0);
156	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) {
166		pr_err("Reading monitor data failed with rc=%i\n", rc);
167		return -EIO;
168	}
169	return 0;
170}
171
172static void mon_free_mem(struct mon_private *monpriv)
173{
174	int i;
175
176	for (i = 0; i < MON_MSGLIM; i++)
177		kfree(monpriv->msg_array[i]);
178	kfree(monpriv);
179}
180
181static struct mon_private *mon_alloc_mem(void)
182{
183	int i;
184	struct mon_private *monpriv;
185
186	monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
187	if (!monpriv)
188		return NULL;
189	for (i = 0; i < MON_MSGLIM; i++) {
190		monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
191						    GFP_KERNEL);
192		if (!monpriv->msg_array[i]) {
193			mon_free_mem(monpriv);
194			return NULL;
195		}
196	}
197	return monpriv;
198}
199
200static inline void mon_next_mca(struct mon_msg *monmsg)
201{
202	if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
203		return;
204	monmsg->mca_offset += 12;
205	monmsg->pos = 0;
206}
207
208static struct mon_msg *mon_next_message(struct mon_private *monpriv)
209{
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;
220		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 *****************************************************************************/
232static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
233{
234	struct mon_private *monpriv = path->private;
235
236	atomic_set(&monpriv->iucv_connected, 1);
237	wake_up(&mon_conn_wait_queue);
238}
239
240static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
241{
242	struct mon_private *monpriv = path->private;
243
244	pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
245	       ipuser[0]);
246	iucv_path_sever(path, NULL);
247	atomic_set(&monpriv->iucv_severed, 1);
248	wake_up(&mon_conn_wait_queue);
249	wake_up_interruptible(&mon_read_wait_queue);
250}
251
252static void mon_iucv_message_pending(struct iucv_path *path,
253				     struct iucv_message *msg)
254{
255	struct mon_private *monpriv = path->private;
256
257	memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
258	       msg, sizeof(*msg));
259	if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
260		pr_warn("The read queue for monitor data is full\n");
261		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
268static 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,
272};
273
274/******************************************************************************
275 *                               file operations                              *
276 *****************************************************************************/
277static int mon_open(struct inode *inode, struct file *filp)
278{
279	struct mon_private *monpriv;
280	int rc;
281
282	/*
283	 * only one user allowed
284	 */
285	rc = -EBUSY;
286	if (test_and_set_bit(MON_IN_USE, &mon_in_use))
287		goto out;
288
289	rc = -ENOMEM;
290	monpriv = mon_alloc_mem();
291	if (!monpriv)
292		goto out_use;
293
294	/*
295	 * Connect to *MONITOR service
296	 */
297	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);
302	if (rc) {
303		pr_err("Connecting to the z/VM *MONITOR system service "
304		       "failed with rc=%i\n", rc);
305		rc = -EIO;
306		goto out_path;
307	}
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;
318		goto out_path;
319	}
320	filp->private_data = monpriv;
321	dev_set_drvdata(monreader_device, monpriv);
322	return nonseekable_open(inode, filp);
323
324out_path:
325	iucv_path_free(monpriv->path);
326out_priv:
327	mon_free_mem(monpriv);
328out_use:
329	clear_bit(MON_IN_USE, &mon_in_use);
330out:
331	return rc;
332}
333
334static int mon_close(struct inode *inode, struct file *filp)
335{
336	int rc, i;
337	struct mon_private *monpriv = filp->private_data;
338
339	/*
340	 * Close IUCV connection and unregister
341	 */
342	if (monpriv->path) {
343		rc = iucv_path_sever(monpriv->path, user_data_sever);
344		if (rc)
345			pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
346				rc);
347		iucv_path_free(monpriv->path);
348	}
349
350	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;
356	dev_set_drvdata(monreader_device, NULL);
357
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
365static ssize_t mon_read(struct file *filp, char __user *data,
366			size_t count, loff_t *ppos)
367{
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
390	if (!monmsg->pos)
391		monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
392	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
431static unsigned int mon_poll(struct file *filp, struct poll_table_struct *p)
432{
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
443static const struct file_operations mon_fops = {
444	.owner   = THIS_MODULE,
445	.open    = &mon_open,
446	.release = &mon_close,
447	.read    = &mon_read,
448	.poll    = &mon_poll,
449	.llseek  = noop_llseek,
450};
451
452static struct miscdevice mon_dev = {
453	.name       = "monreader",
454	.fops       = &mon_fops,
455	.minor      = MISC_DYNAMIC_MINOR,
456};
457
458
459/******************************************************************************
460 *				suspend / resume			      *
461 *****************************************************************************/
462static int monreader_freeze(struct device *dev)
463{
464	struct mon_private *monpriv = dev_get_drvdata(dev);
465	int rc;
466
467	if (!monpriv)
468		return 0;
469	if (monpriv->path) {
470		rc = iucv_path_sever(monpriv->path, user_data_sever);
471		if (rc)
472			pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
473				rc);
474		iucv_path_free(monpriv->path);
475	}
476	atomic_set(&monpriv->iucv_severed, 0);
477	atomic_set(&monpriv->iucv_connected, 0);
478	atomic_set(&monpriv->read_ready, 0);
479	atomic_set(&monpriv->msglim_count, 0);
480	monpriv->write_index  = 0;
481	monpriv->read_index   = 0;
482	monpriv->path = NULL;
483	return 0;
484}
485
486static int monreader_thaw(struct device *dev)
487{
488	struct mon_private *monpriv = dev_get_drvdata(dev);
489	int rc;
490
491	if (!monpriv)
492		return 0;
493	rc = -ENOMEM;
494	monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
495	if (!monpriv->path)
496		goto out;
497	rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
498			       MON_SERVICE, NULL, user_data_connect, monpriv);
499	if (rc) {
500		pr_err("Connecting to the z/VM *MONITOR system service "
501		       "failed with rc=%i\n", rc);
502		goto out_path;
503	}
504	wait_event(mon_conn_wait_queue,
505		   atomic_read(&monpriv->iucv_connected) ||
506		   atomic_read(&monpriv->iucv_severed));
507	if (atomic_read(&monpriv->iucv_severed))
508		goto out_path;
509	return 0;
510out_path:
511	rc = -EIO;
512	iucv_path_free(monpriv->path);
513	monpriv->path = NULL;
514out:
515	atomic_set(&monpriv->iucv_severed, 1);
516	return rc;
517}
518
519static int monreader_restore(struct device *dev)
520{
521	int rc;
522
523	segment_unload(mon_dcss_name);
524	rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
525			  &mon_dcss_start, &mon_dcss_end);
526	if (rc < 0) {
527		segment_warning(rc, mon_dcss_name);
528		panic("fatal monreader resume error: no monitor dcss\n");
529	}
530	return monreader_thaw(dev);
531}
532
533static const struct dev_pm_ops monreader_pm_ops = {
534	.freeze  = monreader_freeze,
535	.thaw	 = monreader_thaw,
536	.restore = monreader_restore,
537};
538
539static struct device_driver monreader_driver = {
540	.name = "monreader",
541	.bus  = &iucv_bus,
542	.pm   = &monreader_pm_ops,
543};
544
545
546/******************************************************************************
547 *                              module init/exit                              *
548 *****************************************************************************/
549static int __init mon_init(void)
550{
551	int rc;
552
553	if (!MACHINE_IS_VM) {
554		pr_err("The z/VM *MONITOR record device driver cannot be "
555		       "loaded without z/VM\n");
556		return -ENODEV;
557	}
558
559	/*
560	 * Register with IUCV and connect to *MONITOR service
561	 */
562	rc = iucv_register(&monreader_iucv_handler, 1);
563	if (rc) {
564		pr_err("The z/VM *MONITOR record device driver failed to "
565		       "register with IUCV\n");
566		return rc;
567	}
568
569	rc = driver_register(&monreader_driver);
570	if (rc)
571		goto out_iucv;
572	monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL);
573	if (!monreader_device) {
574		rc = -ENOMEM;
575		goto out_driver;
576	}
577
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) {
585		put_device(monreader_device);
586		goto out_driver;
587	}
588
589	rc = segment_type(mon_dcss_name);
590	if (rc < 0) {
591		segment_warning(rc, mon_dcss_name);
592		goto out_device;
593	}
594	if (rc != SEG_TYPE_SC) {
595		pr_err("The specified *MONITOR DCSS %s does not have the "
596		       "required type SC\n", mon_dcss_name);
597		rc = -EINVAL;
598		goto out_device;
599	}
600
601	rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
602			  &mon_dcss_start, &mon_dcss_end);
603	if (rc < 0) {
604		segment_warning(rc, mon_dcss_name);
605		rc = -EINVAL;
606		goto out_device;
607	}
608	dcss_mkname(mon_dcss_name, &user_data_connect[8]);
609
610	/*
611	 * misc_register() has to be the last action in module_init(), because
612	 * file operations will be available right after this.
613	 */
614	rc = misc_register(&mon_dev);
615	if (rc < 0 )
616		goto out;
617	return 0;
618
619out:
620	segment_unload(mon_dcss_name);
621out_device:
622	device_unregister(monreader_device);
623out_driver:
624	driver_unregister(&monreader_driver);
625out_iucv:
626	iucv_unregister(&monreader_iucv_handler, 1);
627	return rc;
628}
629
630static void __exit mon_exit(void)
631{
632	segment_unload(mon_dcss_name);
633	misc_deregister(&mon_dev);
634	device_unregister(monreader_device);
635	driver_unregister(&monreader_driver);
636	iucv_unregister(&monreader_iucv_handler, 1);
637	return;
638}
639
640
641module_init(mon_init);
642module_exit(mon_exit);
643
644module_param_string(mondcss, mon_dcss_name, 9, 0444);
645MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
646		 "service, max. 8 chars. Default is MONDCSS");
647
648MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
649MODULE_DESCRIPTION("Character device driver for reading z/VM "
650		   "monitor service records.");
651MODULE_LICENSE("GPL");