staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / s390 / char / monreader.c
blob7bc616b253f1684b72c9dd8dc9286fa77456368f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Character device driver for reading z/VM *MONITOR service records.
5 * Copyright IBM Corp. 2004, 2009
7 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
8 */
10 #define KMSG_COMPONENT "monreader"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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/device.h>
25 #include <linux/slab.h>
26 #include <net/iucv/iucv.h>
27 #include <linux/uaccess.h>
28 #include <asm/ebcdic.h>
29 #include <asm/extmem.h>
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
38 static char mon_dcss_name[9] = "MONDCSS\0";
40 struct mon_msg {
41 u32 pos;
42 u32 mca_offset;
43 struct iucv_message msg;
44 char msglim_reached;
45 char replied_msglim;
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;
59 static unsigned long mon_in_use = 0;
61 static unsigned long mon_dcss_start;
62 static unsigned long mon_dcss_end;
64 static DECLARE_WAIT_QUEUE_HEAD(mon_read_wait_queue);
65 static DECLARE_WAIT_QUEUE_HEAD(mon_conn_wait_queue);
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,
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,
82 static struct device *monreader_device;
84 /******************************************************************************
85 * helper functions *
86 *****************************************************************************/
88 * Create the 8 bytes EBCDIC DCSS segment name from
89 * an ASCII name, incl. padding
91 static void dcss_mkname(char *ascii_name, char *ebcdic_name)
93 int i;
95 for (i = 0; i < 8; i++) {
96 if (ascii_name[i] == '\0')
97 break;
98 ebcdic_name[i] = toupper(ascii_name[i]);
100 for (; i < 8; i++)
101 ebcdic_name[i] = ' ';
102 ASCEBC(ebcdic_name, 8);
105 static inline unsigned long mon_mca_start(struct mon_msg *monmsg)
107 return *(u32 *) &monmsg->msg.rmmsg;
110 static inline unsigned long mon_mca_end(struct mon_msg *monmsg)
112 return *(u32 *) &monmsg->msg.rmmsg[4];
115 static inline u8 mon_mca_type(struct mon_msg *monmsg, u8 index)
117 return *((u8 *) mon_mca_start(monmsg) + monmsg->mca_offset + index);
120 static inline u32 mon_mca_size(struct mon_msg *monmsg)
122 return mon_mca_end(monmsg) - mon_mca_start(monmsg) + 1;
125 static inline u32 mon_rec_start(struct mon_msg *monmsg)
127 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 4));
130 static inline u32 mon_rec_end(struct mon_msg *monmsg)
132 return *((u32 *) (mon_mca_start(monmsg) + monmsg->mca_offset + 8));
135 static int mon_check_mca(struct mon_msg *monmsg)
137 if ((mon_rec_end(monmsg) <= mon_rec_start(monmsg)) ||
138 (mon_rec_start(monmsg) < mon_dcss_start) ||
139 (mon_rec_end(monmsg) > mon_dcss_end) ||
140 (mon_mca_type(monmsg, 0) == 0) ||
141 (mon_mca_size(monmsg) % 12 != 0) ||
142 (mon_mca_end(monmsg) <= mon_mca_start(monmsg)) ||
143 (mon_mca_end(monmsg) > mon_dcss_end) ||
144 (mon_mca_start(monmsg) < mon_dcss_start) ||
145 ((mon_mca_type(monmsg, 1) == 0) && (mon_mca_type(monmsg, 2) == 0)))
146 return -EINVAL;
147 return 0;
150 static int mon_send_reply(struct mon_msg *monmsg,
151 struct mon_private *monpriv)
153 int rc;
155 rc = iucv_message_reply(monpriv->path, &monmsg->msg,
156 IUCV_IPRMDATA, NULL, 0);
157 atomic_dec(&monpriv->msglim_count);
158 if (likely(!monmsg->msglim_reached)) {
159 monmsg->pos = 0;
160 monmsg->mca_offset = 0;
161 monpriv->read_index = (monpriv->read_index + 1) %
162 MON_MSGLIM;
163 atomic_dec(&monpriv->read_ready);
164 } else
165 monmsg->replied_msglim = 1;
166 if (rc) {
167 pr_err("Reading monitor data failed with rc=%i\n", rc);
168 return -EIO;
170 return 0;
173 static void mon_free_mem(struct mon_private *monpriv)
175 int i;
177 for (i = 0; i < MON_MSGLIM; i++)
178 kfree(monpriv->msg_array[i]);
179 kfree(monpriv);
182 static struct mon_private *mon_alloc_mem(void)
184 int i;
185 struct mon_private *monpriv;
187 monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
188 if (!monpriv)
189 return NULL;
190 for (i = 0; i < MON_MSGLIM; i++) {
191 monpriv->msg_array[i] = kzalloc(sizeof(struct mon_msg),
192 GFP_KERNEL);
193 if (!monpriv->msg_array[i]) {
194 mon_free_mem(monpriv);
195 return NULL;
198 return monpriv;
201 static inline void mon_next_mca(struct mon_msg *monmsg)
203 if (likely((mon_mca_size(monmsg) - monmsg->mca_offset) == 12))
204 return;
205 monmsg->mca_offset += 12;
206 monmsg->pos = 0;
209 static struct mon_msg *mon_next_message(struct mon_private *monpriv)
211 struct mon_msg *monmsg;
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;
221 monpriv->read_index = (monpriv->read_index + 1) %
222 MON_MSGLIM;
223 atomic_dec(&monpriv->read_ready);
224 return ERR_PTR(-EOVERFLOW);
226 return monmsg;
230 /******************************************************************************
231 * IUCV handler *
232 *****************************************************************************/
233 static void mon_iucv_path_complete(struct iucv_path *path, u8 *ipuser)
235 struct mon_private *monpriv = path->private;
237 atomic_set(&monpriv->iucv_connected, 1);
238 wake_up(&mon_conn_wait_queue);
241 static void mon_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
243 struct mon_private *monpriv = path->private;
245 pr_err("z/VM *MONITOR system service disconnected with rc=%i\n",
246 ipuser[0]);
247 iucv_path_sever(path, NULL);
248 atomic_set(&monpriv->iucv_severed, 1);
249 wake_up(&mon_conn_wait_queue);
250 wake_up_interruptible(&mon_read_wait_queue);
253 static void mon_iucv_message_pending(struct iucv_path *path,
254 struct iucv_message *msg)
256 struct mon_private *monpriv = path->private;
258 memcpy(&monpriv->msg_array[monpriv->write_index]->msg,
259 msg, sizeof(*msg));
260 if (atomic_inc_return(&monpriv->msglim_count) == MON_MSGLIM) {
261 pr_warn("The read queue for monitor data is full\n");
262 monpriv->msg_array[monpriv->write_index]->msglim_reached = 1;
264 monpriv->write_index = (monpriv->write_index + 1) % MON_MSGLIM;
265 atomic_inc(&monpriv->read_ready);
266 wake_up_interruptible(&mon_read_wait_queue);
269 static 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,
275 /******************************************************************************
276 * file operations *
277 *****************************************************************************/
278 static int mon_open(struct inode *inode, struct file *filp)
280 struct mon_private *monpriv;
281 int rc;
284 * only one user allowed
286 rc = -EBUSY;
287 if (test_and_set_bit(MON_IN_USE, &mon_in_use))
288 goto out;
290 rc = -ENOMEM;
291 monpriv = mon_alloc_mem();
292 if (!monpriv)
293 goto out_use;
296 * Connect to *MONITOR service
298 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
299 if (!monpriv->path)
300 goto out_priv;
301 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
302 MON_SERVICE, NULL, user_data_connect, monpriv);
303 if (rc) {
304 pr_err("Connecting to the z/VM *MONITOR system service "
305 "failed with rc=%i\n", rc);
306 rc = -EIO;
307 goto out_path;
310 * Wait for connection confirmation
312 wait_event(mon_conn_wait_queue,
313 atomic_read(&monpriv->iucv_connected) ||
314 atomic_read(&monpriv->iucv_severed));
315 if (atomic_read(&monpriv->iucv_severed)) {
316 atomic_set(&monpriv->iucv_severed, 0);
317 atomic_set(&monpriv->iucv_connected, 0);
318 rc = -EIO;
319 goto out_path;
321 filp->private_data = monpriv;
322 dev_set_drvdata(monreader_device, monpriv);
323 return nonseekable_open(inode, filp);
325 out_path:
326 iucv_path_free(monpriv->path);
327 out_priv:
328 mon_free_mem(monpriv);
329 out_use:
330 clear_bit(MON_IN_USE, &mon_in_use);
331 out:
332 return rc;
335 static int mon_close(struct inode *inode, struct file *filp)
337 int rc, i;
338 struct mon_private *monpriv = filp->private_data;
341 * Close IUCV connection and unregister
343 if (monpriv->path) {
344 rc = iucv_path_sever(monpriv->path, user_data_sever);
345 if (rc)
346 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
347 rc);
348 iucv_path_free(monpriv->path);
351 atomic_set(&monpriv->iucv_severed, 0);
352 atomic_set(&monpriv->iucv_connected, 0);
353 atomic_set(&monpriv->read_ready, 0);
354 atomic_set(&monpriv->msglim_count, 0);
355 monpriv->write_index = 0;
356 monpriv->read_index = 0;
357 dev_set_drvdata(monreader_device, NULL);
359 for (i = 0; i < MON_MSGLIM; i++)
360 kfree(monpriv->msg_array[i]);
361 kfree(monpriv);
362 clear_bit(MON_IN_USE, &mon_in_use);
363 return 0;
366 static ssize_t mon_read(struct file *filp, char __user *data,
367 size_t count, loff_t *ppos)
369 struct mon_private *monpriv = filp->private_data;
370 struct mon_msg *monmsg;
371 int ret;
372 u32 mce_start;
374 monmsg = mon_next_message(monpriv);
375 if (IS_ERR(monmsg))
376 return PTR_ERR(monmsg);
378 if (!monmsg) {
379 if (filp->f_flags & O_NONBLOCK)
380 return -EAGAIN;
381 ret = wait_event_interruptible(mon_read_wait_queue,
382 atomic_read(&monpriv->read_ready) ||
383 atomic_read(&monpriv->iucv_severed));
384 if (ret)
385 return ret;
386 if (unlikely(atomic_read(&monpriv->iucv_severed)))
387 return -EIO;
388 monmsg = monpriv->msg_array[monpriv->read_index];
391 if (!monmsg->pos)
392 monmsg->pos = mon_mca_start(monmsg) + monmsg->mca_offset;
393 if (mon_check_mca(monmsg))
394 goto reply;
396 /* read monitor control element (12 bytes) first */
397 mce_start = mon_mca_start(monmsg) + monmsg->mca_offset;
398 if ((monmsg->pos >= mce_start) && (monmsg->pos < mce_start + 12)) {
399 count = min(count, (size_t) mce_start + 12 - monmsg->pos);
400 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
401 count);
402 if (ret)
403 return -EFAULT;
404 monmsg->pos += count;
405 if (monmsg->pos == mce_start + 12)
406 monmsg->pos = mon_rec_start(monmsg);
407 goto out_copy;
410 /* read records */
411 if (monmsg->pos <= mon_rec_end(monmsg)) {
412 count = min(count, (size_t) mon_rec_end(monmsg) - monmsg->pos
413 + 1);
414 ret = copy_to_user(data, (void *) (unsigned long) monmsg->pos,
415 count);
416 if (ret)
417 return -EFAULT;
418 monmsg->pos += count;
419 if (monmsg->pos > mon_rec_end(monmsg))
420 mon_next_mca(monmsg);
421 goto out_copy;
423 reply:
424 ret = mon_send_reply(monmsg, monpriv);
425 return ret;
427 out_copy:
428 *ppos += count;
429 return count;
432 static __poll_t mon_poll(struct file *filp, struct poll_table_struct *p)
434 struct mon_private *monpriv = filp->private_data;
436 poll_wait(filp, &mon_read_wait_queue, p);
437 if (unlikely(atomic_read(&monpriv->iucv_severed)))
438 return EPOLLERR;
439 if (atomic_read(&monpriv->read_ready))
440 return EPOLLIN | EPOLLRDNORM;
441 return 0;
444 static const struct file_operations mon_fops = {
445 .owner = THIS_MODULE,
446 .open = &mon_open,
447 .release = &mon_close,
448 .read = &mon_read,
449 .poll = &mon_poll,
450 .llseek = noop_llseek,
453 static struct miscdevice mon_dev = {
454 .name = "monreader",
455 .fops = &mon_fops,
456 .minor = MISC_DYNAMIC_MINOR,
460 /******************************************************************************
461 * suspend / resume *
462 *****************************************************************************/
463 static int monreader_freeze(struct device *dev)
465 struct mon_private *monpriv = dev_get_drvdata(dev);
466 int rc;
468 if (!monpriv)
469 return 0;
470 if (monpriv->path) {
471 rc = iucv_path_sever(monpriv->path, user_data_sever);
472 if (rc)
473 pr_warn("Disconnecting the z/VM *MONITOR system service failed with rc=%i\n",
474 rc);
475 iucv_path_free(monpriv->path);
477 atomic_set(&monpriv->iucv_severed, 0);
478 atomic_set(&monpriv->iucv_connected, 0);
479 atomic_set(&monpriv->read_ready, 0);
480 atomic_set(&monpriv->msglim_count, 0);
481 monpriv->write_index = 0;
482 monpriv->read_index = 0;
483 monpriv->path = NULL;
484 return 0;
487 static int monreader_thaw(struct device *dev)
489 struct mon_private *monpriv = dev_get_drvdata(dev);
490 int rc;
492 if (!monpriv)
493 return 0;
494 rc = -ENOMEM;
495 monpriv->path = iucv_path_alloc(MON_MSGLIM, IUCV_IPRMDATA, GFP_KERNEL);
496 if (!monpriv->path)
497 goto out;
498 rc = iucv_path_connect(monpriv->path, &monreader_iucv_handler,
499 MON_SERVICE, NULL, user_data_connect, monpriv);
500 if (rc) {
501 pr_err("Connecting to the z/VM *MONITOR system service "
502 "failed with rc=%i\n", rc);
503 goto out_path;
505 wait_event(mon_conn_wait_queue,
506 atomic_read(&monpriv->iucv_connected) ||
507 atomic_read(&monpriv->iucv_severed));
508 if (atomic_read(&monpriv->iucv_severed))
509 goto out_path;
510 return 0;
511 out_path:
512 rc = -EIO;
513 iucv_path_free(monpriv->path);
514 monpriv->path = NULL;
515 out:
516 atomic_set(&monpriv->iucv_severed, 1);
517 return rc;
520 static int monreader_restore(struct device *dev)
522 int rc;
524 segment_unload(mon_dcss_name);
525 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
526 &mon_dcss_start, &mon_dcss_end);
527 if (rc < 0) {
528 segment_warning(rc, mon_dcss_name);
529 panic("fatal monreader resume error: no monitor dcss\n");
531 return monreader_thaw(dev);
534 static const struct dev_pm_ops monreader_pm_ops = {
535 .freeze = monreader_freeze,
536 .thaw = monreader_thaw,
537 .restore = monreader_restore,
540 static struct device_driver monreader_driver = {
541 .name = "monreader",
542 .bus = &iucv_bus,
543 .pm = &monreader_pm_ops,
547 /******************************************************************************
548 * module init/exit *
549 *****************************************************************************/
550 static int __init mon_init(void)
552 int rc;
554 if (!MACHINE_IS_VM) {
555 pr_err("The z/VM *MONITOR record device driver cannot be "
556 "loaded without z/VM\n");
557 return -ENODEV;
561 * Register with IUCV and connect to *MONITOR service
563 rc = iucv_register(&monreader_iucv_handler, 1);
564 if (rc) {
565 pr_err("The z/VM *MONITOR record device driver failed to "
566 "register with IUCV\n");
567 return rc;
570 rc = driver_register(&monreader_driver);
571 if (rc)
572 goto out_iucv;
573 monreader_device = kzalloc(sizeof(struct device), GFP_KERNEL);
574 if (!monreader_device) {
575 rc = -ENOMEM;
576 goto out_driver;
579 dev_set_name(monreader_device, "monreader-dev");
580 monreader_device->bus = &iucv_bus;
581 monreader_device->parent = iucv_root;
582 monreader_device->driver = &monreader_driver;
583 monreader_device->release = (void (*)(struct device *))kfree;
584 rc = device_register(monreader_device);
585 if (rc) {
586 put_device(monreader_device);
587 goto out_driver;
590 rc = segment_type(mon_dcss_name);
591 if (rc < 0) {
592 segment_warning(rc, mon_dcss_name);
593 goto out_device;
595 if (rc != SEG_TYPE_SC) {
596 pr_err("The specified *MONITOR DCSS %s does not have the "
597 "required type SC\n", mon_dcss_name);
598 rc = -EINVAL;
599 goto out_device;
602 rc = segment_load(mon_dcss_name, SEGMENT_SHARED,
603 &mon_dcss_start, &mon_dcss_end);
604 if (rc < 0) {
605 segment_warning(rc, mon_dcss_name);
606 rc = -EINVAL;
607 goto out_device;
609 dcss_mkname(mon_dcss_name, &user_data_connect[8]);
612 * misc_register() has to be the last action in module_init(), because
613 * file operations will be available right after this.
615 rc = misc_register(&mon_dev);
616 if (rc < 0 )
617 goto out;
618 return 0;
620 out:
621 segment_unload(mon_dcss_name);
622 out_device:
623 device_unregister(monreader_device);
624 out_driver:
625 driver_unregister(&monreader_driver);
626 out_iucv:
627 iucv_unregister(&monreader_iucv_handler, 1);
628 return rc;
631 static void __exit mon_exit(void)
633 segment_unload(mon_dcss_name);
634 misc_deregister(&mon_dev);
635 device_unregister(monreader_device);
636 driver_unregister(&monreader_driver);
637 iucv_unregister(&monreader_iucv_handler, 1);
638 return;
642 module_init(mon_init);
643 module_exit(mon_exit);
645 module_param_string(mondcss, mon_dcss_name, 9, 0444);
646 MODULE_PARM_DESC(mondcss, "Name of DCSS segment to be used for *MONITOR "
647 "service, max. 8 chars. Default is MONDCSS");
649 MODULE_AUTHOR("Gerald Schaefer <geraldsc@de.ibm.com>");
650 MODULE_DESCRIPTION("Character device driver for reading z/VM "
651 "monitor service records.");
652 MODULE_LICENSE("GPL");