^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Character device driver for writing z/VM *MONITOR service records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 2006, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define KMSG_COMPONENT "monwriter"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/ebcdic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/appldata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/monwriter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MONWRITE_MAX_DATALEN 4010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int mon_max_bufs = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int mon_buf_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct mon_buf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct monwrite_hdr hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int diag_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static LIST_HEAD(mon_priv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct mon_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct list_head priv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct monwrite_hdr hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) size_t hdr_to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) size_t data_to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct mon_buf *current_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct mutex thread_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * helper functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct appldata_parameter_list *parm_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct appldata_product_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) id = kmalloc(sizeof(*id), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!id || !parm_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) memcpy(id->prod_nr, "LNXAPPL", 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) id->prod_fn = myhdr->applid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) id->record_nr = myhdr->record_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) id->version_nr = myhdr->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) id->release_nr = myhdr->release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) id->mod_lvl = myhdr->mod_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) rc = appldata_asm(parm_list, id, fcn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) (void *) buffer, myhdr->datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (rc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pr_err("Writing monitor data failed with rc=%i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) rc = (rc == 5) ? -EPERM : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) kfree(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) kfree(parm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct monwrite_hdr *monhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct mon_buf *entry, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) list_for_each_entry_safe(entry, next, &monpriv->list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if ((entry->hdr.mon_function == monhdr->mon_function ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) entry->hdr.applid == monhdr->applid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) entry->hdr.record_num == monhdr->record_num &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) entry->hdr.version == monhdr->version &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) entry->hdr.release == monhdr->release &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) entry->hdr.mod_level == monhdr->mod_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int monwrite_new_hdr(struct mon_private *monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct monwrite_hdr *monhdr = &monpriv->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct mon_buf *monbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) monhdr->mon_function > MONWRITE_START_CONFIG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) monhdr->hdrlen != sizeof(struct monwrite_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) monbuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (monhdr->mon_function != MONWRITE_GEN_EVENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) monbuf = monwrite_find_hdr(monpriv, monhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (monbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) monhdr->datalen = monbuf->hdr.datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rc = monwrite_diag(monhdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) APPLDATA_STOP_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) list_del(&monbuf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) mon_buf_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) kfree(monbuf->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) kfree(monbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) monbuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (mon_buf_count >= mon_max_bufs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!monbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) monbuf->data = kzalloc(monhdr->datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!monbuf->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) kfree(monbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) monbuf->hdr = *monhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) list_add_tail(&monbuf->list, &monpriv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (monhdr->mon_function != MONWRITE_GEN_EVENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mon_buf_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) monpriv->current_buf = monbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int monwrite_new_data(struct mon_private *monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct monwrite_hdr *monhdr = &monpriv->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct mon_buf *monbuf = monpriv->current_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) switch (monhdr->mon_function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) case MONWRITE_START_INTERVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!monbuf->diag_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) rc = monwrite_diag(monhdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) APPLDATA_START_INTERVAL_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) monbuf->diag_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) case MONWRITE_START_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!monbuf->diag_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) rc = monwrite_diag(monhdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) APPLDATA_START_CONFIG_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) monbuf->diag_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case MONWRITE_GEN_EVENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) rc = monwrite_diag(monhdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) APPLDATA_GEN_EVENT_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) list_del(&monpriv->current_buf->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) kfree(monpriv->current_buf->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) kfree(monpriv->current_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) monpriv->current_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* monhdr->mon_function is checked in monwrite_new_hdr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * file operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int monwrite_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct mon_private *monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!monpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) INIT_LIST_HEAD(&monpriv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) monpriv->hdr_to_read = sizeof(monpriv->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) mutex_init(&monpriv->thread_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) filp->private_data = monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) list_add_tail(&monpriv->priv_list, &mon_priv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return nonseekable_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int monwrite_close(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct mon_private *monpriv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct mon_buf *entry, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) list_for_each_entry_safe(entry, next, &monpriv->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) monwrite_diag(&entry->hdr, entry->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) APPLDATA_STOP_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) mon_buf_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) kfree(entry->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) list_del(&monpriv->priv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kfree(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static ssize_t monwrite_write(struct file *filp, const char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct mon_private *monpriv = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) size_t len, written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) void *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) mutex_lock(&monpriv->thread_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) for (written = 0; written < count; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (monpriv->hdr_to_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) len = min(count - written, monpriv->hdr_to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) to = (char *) &monpriv->hdr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) sizeof(monpriv->hdr) - monpriv->hdr_to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (copy_from_user(to, data + written, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) monpriv->hdr_to_read -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) written += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (monpriv->hdr_to_read > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) rc = monwrite_new_hdr(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) monpriv->data_to_read = monpriv->current_buf ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) monpriv->current_buf->hdr.datalen : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (monpriv->data_to_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) len = min(count - written, monpriv->data_to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) to = monpriv->current_buf->data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) monpriv->hdr.datalen - monpriv->data_to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (copy_from_user(to, data + written, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) monpriv->data_to_read -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) written += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (monpriv->data_to_read > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) rc = monwrite_new_data(monpriv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) monpriv->hdr_to_read = sizeof(monpriv->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) mutex_unlock(&monpriv->thread_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) monpriv->data_to_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) mutex_unlock(&monpriv->thread_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static const struct file_operations monwrite_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .open = &monwrite_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .release = &monwrite_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .write = &monwrite_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static struct miscdevice mon_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .name = "monwriter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .fops = &monwrite_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * suspend/resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int monwriter_freeze(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct mon_private *monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct mon_buf *monbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) list_for_each_entry(monbuf, &monpriv->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (monbuf->hdr.mon_function != MONWRITE_GEN_EVENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) monwrite_diag(&monbuf->hdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) APPLDATA_STOP_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int monwriter_restore(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct mon_private *monpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct mon_buf *monbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) list_for_each_entry(monpriv, &mon_priv_list, priv_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) list_for_each_entry(monbuf, &monpriv->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (monbuf->hdr.mon_function == MONWRITE_START_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) monwrite_diag(&monbuf->hdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) APPLDATA_START_INTERVAL_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (monbuf->hdr.mon_function == MONWRITE_START_CONFIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) monwrite_diag(&monbuf->hdr, monbuf->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) APPLDATA_START_CONFIG_REC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static int monwriter_thaw(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return monwriter_restore(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static const struct dev_pm_ops monwriter_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .freeze = monwriter_freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .thaw = monwriter_thaw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .restore = monwriter_restore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static struct platform_driver monwriter_pdrv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .name = "monwriter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .pm = &monwriter_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static struct platform_device *monwriter_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * module init/exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int __init mon_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (!MACHINE_IS_VM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) rc = platform_driver_register(&monwriter_pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) monwriter_pdev = platform_device_register_simple("monwriter", -1, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (IS_ERR(monwriter_pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) rc = PTR_ERR(monwriter_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto out_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * misc_register() has to be the last action in module_init(), because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * file operations will be available right after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) rc = misc_register(&mon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) out_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) platform_device_unregister(monwriter_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) out_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) platform_driver_unregister(&monwriter_pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static void __exit mon_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) misc_deregister(&mon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) platform_device_unregister(monwriter_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) platform_driver_unregister(&monwriter_pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) module_init(mon_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) module_exit(mon_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) module_param_named(max_bufs, mon_max_bufs, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) "that can be active at one time");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) MODULE_DESCRIPTION("Character device driver for writing z/VM "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) "APPLDATA monitor records.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_LICENSE("GPL");