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/machine.h>
28 #include <asm/ebcdic.h>
29 #include <asm/extmem.h>
30
31
32 #define MON_COLLECT_SAMPLE 0x80
33 #define MON_COLLECT_EVENT 0x40
34 #define MON_SERVICE "*MONITOR"
35 #define MON_IN_USE 0x01
36 #define MON_MSGLIM 255
37
38 static char mon_dcss_name[9] = "MONDCSS\0";
39
40 struct mon_msg {
41 u32 pos;
42 u32 mca_offset;
43 struct iucv_message msg;
44 char msglim_reached;
45 char replied_msglim;
46 };
47
48 struct mon_private {
49 struct iucv_path *path;
50 struct mon_msg *msg_array[MON_MSGLIM];
51 unsigned int write_index;
52 unsigned int read_index;
53 atomic_t msglim_count;
54 atomic_t read_ready;
55 atomic_t iucv_connected;
56 atomic_t iucv_severed;
57 };
58
59 static unsigned long mon_in_use = 0;
60
61 static unsigned long mon_dcss_start;
62 static unsigned long mon_dcss_end;
63
64 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
65 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
66
67 static u8 user_data_connect[16] = {
68 /* Version code, must be 0x01 for shared mode */
69 0x01,
70 /* what to collect */
71 MON_COLLECT_SAMPLE | MON_COLLECT_EVENT,
72 /* DCSS name in EBCDIC, 8 bytes padded with blanks */
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 };
76
77 static u8 user_data_sever[16] = {
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80 };
81
82 /******************************************************************************
83 * helper functions *
84 *****************************************************************************/
85 /*
86 * Create the 8 bytes EBCDIC DCSS segment name from
87 * an ASCII name, incl. padding
88 */
dcss_mkname(char * ascii_name,char * ebcdic_name)89 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
90 {
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
mon_mca_start(struct mon_msg * monmsg)103 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
104 {
105 return *(u32 *) &monmsg->msg.rmmsg;
106 }
107
mon_mca_end(struct mon_msg * monmsg)108 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
109 {
110 return *(u32 *) &monmsg->msg.rmmsg[4];
111 }
112
mon_mca_type(struct mon_msg * monmsg,u8 index)113 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
114 {
115 return *((u8 *)__va(mon_mca_start(monmsg)) + monmsg->mca_offset + index);
116 }
117
mon_mca_size(struct mon_msg * monmsg)118 static inline u32 mon_mca_size(struct mon_msg *monmsg)
119 {
120 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
121 }
122
mon_rec_start(struct mon_msg * monmsg)123 static inline u32 mon_rec_start(struct mon_msg *monmsg)
124 {
125 return *((u32 *)(__va(mon_mca_start(monmsg)) + monmsg->mca_offset + 4));
126 }
127
mon_rec_end(struct mon_msg * monmsg)128 static inline u32 mon_rec_end(struct mon_msg *monmsg)
129 {
130 return *((u32 *)(__va(mon_mca_start(monmsg)) + monmsg->mca_offset + 8));
131 }
132
mon_check_mca(struct mon_msg * monmsg)133 static int mon_check_mca(struct mon_msg *monmsg)
134 {
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)))
144 return -EINVAL;
145 return 0;
146 }
147
mon_send_reply(struct mon_msg * monmsg,struct mon_private * monpriv)148 static int mon_send_reply(struct mon_msg *monmsg,
149 struct mon_private *monpriv)
150 {
151 int rc;
152
153 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
154 IUCV_IPRMDATA, NULL, 0);
155 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) {
165 pr_err("Reading monitor data failed with rc=%i\n", rc);
166 return -EIO;
167 }
168 return 0;
169 }
170
mon_free_mem(struct mon_private * monpriv)171 static void mon_free_mem(struct mon_private *monpriv)
172 {
173 int i;
174
175 for (i = 0; i < MON_MSGLIM; i++)
176 kfree(monpriv->msg_array[i]);
177 kfree(monpriv);
178 }
179
mon_alloc_mem(void)180 static struct mon_private *mon_alloc_mem(void)
181 {
182 int i;
183 struct mon_private *monpriv;
184
185 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
186 if (!monpriv)
187 return NULL;
188 for (i = 0; i < MON_MSGLIM; i++) {
189 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
190 GFP_KERNEL);
191 if (!monpriv->msg_array[i]) {
192 mon_free_mem(monpriv);
193 return NULL;
194 }
195 }
196 return monpriv;
197 }
198
mon_next_mca(struct mon_msg * monmsg)199 static inline void mon_next_mca(struct mon_msg *monmsg)
200 {
201 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
202 return;
203 monmsg->mca_offset += 12;
204 monmsg->pos = 0;
205 }
206
mon_next_message(struct mon_private * monpriv)207 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
208 {
209 struct mon_msg *monmsg;
210
211 if (!atomic_read(&monpriv->read_ready))
212 return NULL;
213 monmsg = monpriv->msg_array[monpriv->read_index];
214 if (unlikely(monmsg->replied_msglim)) {
215 monmsg->replied_msglim = 0;
216 monmsg->msglim_reached = 0;
217 monmsg->pos = 0;
218 monmsg->mca_offset = 0;
219 monpriv->read_index = (monpriv->read_index + 1) %
220 MON_MSGLIM;
221 atomic_dec(&monpriv->read_ready);
222 return ERR_PTR(-EOVERFLOW);
223 }
224 return monmsg;
225 }
226
227
228 /******************************************************************************
229 * IUCV handler *
230 *****************************************************************************/
mon_iucv_path_complete(struct iucv_path * path,u8 * ipuser)231 static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
232 {
233 struct mon_private *monpriv = path->private;
234
235 atomic_set(&monpriv->iucv_connected, 1);
236 wake_up(&mon_conn_wait_queue);
237 }
238
mon_iucv_path_severed(struct iucv_path * path,u8 * ipuser)239 static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
240 {
241 struct mon_private *monpriv = path->private;
242
243 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
244 ipuser[0]);
245 iucv_path_sever(path, NULL);
246 atomic_set(&monpriv->iucv_severed, 1);
247 wake_up(&mon_conn_wait_queue);
248 wake_up_interruptible(&mon_read_wait_queue);
249 }
250
mon_iucv_message_pending(struct iucv_path * path,struct iucv_message * msg)251 static void mon_iucv_message_pending(struct iucv_path *path,
252 struct iucv_message *msg)
253 {
254 struct mon_private *monpriv = path->private;
255
256 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
257 msg, sizeof(*msg));
258 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
259 pr_warn("The read queue for monitor data is full\n");
260 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
261 }
262 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
263 atomic_inc(&monpriv->read_ready);
264 wake_up_interruptible(&mon_read_wait_queue);
265 }
266
267 static struct iucv_handler monreader_iucv_handler = {
268 .path_complete = mon_iucv_path_complete,
269 .path_severed = mon_iucv_path_severed,
270 .message_pending = mon_iucv_message_pending,
271 };
272
273 /******************************************************************************
274 * file operations *
275 *****************************************************************************/
mon_open(struct inode * inode,struct file * filp)276 static int mon_open(struct inode *inode, struct file *filp)
277 {
278 struct mon_private *monpriv;
279 int rc;
280
281 /*
282 * only one user allowed
283 */
284 rc = -EBUSY;
285 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
286 goto out;
287
288 rc = -ENOMEM;
289 monpriv = mon_alloc_mem();
290 if (!monpriv)
291 goto out_use;
292
293 /*
294 * Connect to *MONITOR service
295 */
296 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
297 if (!monpriv->path)
298 goto out_priv;
299 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
300 MON_SERVICE, NULL, user_data_connect, monpriv);
301 if (rc) {
302 pr_err("Connecting to the z/VM *MONITOR system service "
303 "failed with rc=%i\n", rc);
304 rc = -EIO;
305 goto out_path;
306 }
307 /*
308 * Wait for connection confirmation
309 */
310 wait_event(mon_conn_wait_queue,
311 atomic_read(&monpriv->iucv_connected) ||
312 atomic_read(&monpriv->iucv_severed));
313 if (atomic_read(&monpriv->iucv_severed)) {
314 atomic_set(&monpriv->iucv_severed, 0);
315 atomic_set(&monpriv->iucv_connected, 0);
316 rc = -EIO;
317 goto out_path;
318 }
319 filp->private_data = monpriv;
320 return nonseekable_open(inode, filp);
321
322 out_path:
323 iucv_path_free(monpriv->path);
324 out_priv:
325 mon_free_mem(monpriv);
326 out_use:
327 clear_bit(MON_IN_USE, &mon_in_use);
328 out:
329 return rc;
330 }
331
mon_close(struct inode * inode,struct file * filp)332 static int mon_close(struct inode *inode, struct file *filp)
333 {
334 int rc, i;
335 struct mon_private *monpriv = filp->private_data;
336
337 /*
338 * Close IUCV connection and unregister
339 */
340 if (monpriv->path) {
341 rc = iucv_path_sever(monpriv->path, user_data_sever);
342 if (rc)
343 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
344 rc);
345 iucv_path_free(monpriv->path);
346 }
347
348 atomic_set(&monpriv->iucv_severed, 0);
349 atomic_set(&monpriv->iucv_connected, 0);
350 atomic_set(&monpriv->read_ready, 0);
351 atomic_set(&monpriv->msglim_count, 0);
352 monpriv->write_index = 0;
353 monpriv->read_index = 0;
354
355 for (i = 0; i < MON_MSGLIM; i++)
356 kfree(monpriv->msg_array[i]);
357 kfree(monpriv);
358 clear_bit(MON_IN_USE, &mon_in_use);
359 return 0;
360 }
361
mon_read(struct file * filp,char __user * data,size_t count,loff_t * ppos)362 static ssize_t mon_read(struct file *filp, char __user *data,
363 size_t count, loff_t *ppos)
364 {
365 struct mon_private *monpriv = filp->private_data;
366 struct mon_msg *monmsg;
367 int ret;
368 u32 mce_start;
369
370 monmsg = mon_next_message(monpriv);
371 if (IS_ERR(monmsg))
372 return PTR_ERR(monmsg);
373
374 if (!monmsg) {
375 if (filp->f_flags & O_NONBLOCK)
376 return -EAGAIN;
377 ret = wait_event_interruptible(mon_read_wait_queue,
378 atomic_read(&monpriv->read_ready) ||
379 atomic_read(&monpriv->iucv_severed));
380 if (ret)
381 return ret;
382 if (unlikely(atomic_read(&monpriv->iucv_severed)))
383 return -EIO;
384 monmsg = monpriv->msg_array[monpriv->read_index];
385 }
386
387 if (!monmsg->pos)
388 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
389 if (mon_check_mca(monmsg))
390 goto reply;
391
392 /* read monitor control element (12 bytes) first */
393 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
394 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
395 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
396 ret = copy_to_user(data, __va(monmsg->pos), count);
397 if (ret)
398 return -EFAULT;
399 monmsg->pos += count;
400 if (monmsg->pos == mce_start + 12)
401 monmsg->pos = mon_rec_start(monmsg);
402 goto out_copy;
403 }
404
405 /* read records */
406 if (monmsg->pos <= mon_rec_end(monmsg)) {
407 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
408 + 1);
409 ret = copy_to_user(data, __va(monmsg->pos), count);
410 if (ret)
411 return -EFAULT;
412 monmsg->pos += count;
413 if (monmsg->pos > mon_rec_end(monmsg))
414 mon_next_mca(monmsg);
415 goto out_copy;
416 }
417 reply:
418 ret = mon_send_reply(monmsg, monpriv);
419 return ret;
420
421 out_copy:
422 *ppos += count;
423 return count;
424 }
425
mon_poll(struct file * filp,struct poll_table_struct * p)426 static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p)
427 {
428 struct mon_private *monpriv = filp->private_data;
429
430 poll_wait(filp, &mon_read_wait_queue, p);
431 if (unlikely(atomic_read(&monpriv->iucv_severed)))
432 return EPOLLERR;
433 if (atomic_read(&monpriv->read_ready))
434 return EPOLLIN | EPOLLRDNORM;
435 return 0;
436 }
437
438 static const struct file_operations mon_fops = {
439 .owner = THIS_MODULE,
440 .open = &mon_open,
441 .release = &mon_close,
442 .read = &mon_read,
443 .poll = &mon_poll,
444 .llseek = noop_llseek,
445 };
446
447 static struct miscdevice mon_dev = {
448 .name = "monreader",
449 .fops = &mon_fops,
450 .minor = MISC_DYNAMIC_MINOR,
451 };
452
453 /******************************************************************************
454 * module init/exit *
455 *****************************************************************************/
mon_init(void)456 static int __init mon_init(void)
457 {
458 int rc;
459
460 if (!machine_is_vm()) {
461 pr_err("The z/VM *MONITOR record device driver cannot be "
462 "loaded without z/VM\n");
463 return -ENODEV;
464 }
465
466 /*
467 * Register with IUCV and connect to *MONITOR service
468 */
469 rc = iucv_register(&monreader_iucv_handler, 1);
470 if (rc) {
471 pr_err("The z/VM *MONITOR record device driver failed to "
472 "register with IUCV\n");
473 return rc;
474 }
475
476 rc = segment_type(mon_dcss_name);
477 if (rc < 0) {
478 segment_warning(rc, mon_dcss_name);
479 goto out_iucv;
480 }
481 if (rc != SEG_TYPE_SC) {
482 pr_err("The specified *MONITOR DCSS %s does not have the "
483 "required type SC\n", mon_dcss_name);
484 rc = -EINVAL;
485 goto out_iucv;
486 }
487
488 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
489 &mon_dcss_start, &mon_dcss_end);
490 if (rc < 0) {
491 segment_warning(rc, mon_dcss_name);
492 rc = -EINVAL;
493 goto out_iucv;
494 }
495 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
496
497 /*
498 * misc_register() has to be the last action in module_init(), because
499 * file operations will be available right after this.
500 */
501 rc = misc_register(&mon_dev);
502 if (rc < 0 )
503 goto out;
504 return 0;
505
506 out:
507 segment_unload(mon_dcss_name);
508 out_iucv:
509 iucv_unregister(&monreader_iucv_handler, 1);
510 return rc;
511 }
512
mon_exit(void)513 static void __exit mon_exit(void)
514 {
515 segment_unload(mon_dcss_name);
516 misc_deregister(&mon_dev);
517 iucv_unregister(&monreader_iucv_handler, 1);
518 return;
519 }
520
521
522 module_init(mon_init);
523 module_exit(mon_exit);
524
525 module_param_string(mondcss, mon_dcss_name, 9, 0444);
526 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
527 "service, max. 8 chars. Default is MONDCSS");
528
529 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
530 MODULE_DESCRIPTION("Character device driver for reading z/VM "
531 "monitor service records.");
532 MODULE_LICENSE("GPL");
533