monwriter.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * drivers/s390/char/monwriter.c
  3. *
  4. * Character device driver for writing z/VM *MONITOR service records.
  5. *
  6. * Copyright (C) IBM Corp. 2006
  7. *
  8. * Author(s): Melissa Howland <Melissa.Howland@us.ibm.com>
  9. */
  10. #define KMSG_COMPONENT "monwriter"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/ctype.h>
  21. #include <linux/poll.h>
  22. #include <linux/mutex.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/ebcdic.h>
  25. #include <asm/io.h>
  26. #include <asm/appldata.h>
  27. #include <asm/monwriter.h>
  28. #define MONWRITE_MAX_DATALEN 4010
  29. static int mon_max_bufs = 255;
  30. static int mon_buf_count;
  31. struct mon_buf {
  32. struct list_head list;
  33. struct monwrite_hdr hdr;
  34. int diag_done;
  35. char *data;
  36. };
  37. struct mon_private {
  38. struct list_head list;
  39. struct monwrite_hdr hdr;
  40. size_t hdr_to_read;
  41. size_t data_to_read;
  42. struct mon_buf *current_buf;
  43. struct mutex thread_mutex;
  44. };
  45. /*
  46. * helper functions
  47. */
  48. static int monwrite_diag(struct monwrite_hdr *myhdr, char *buffer, int fcn)
  49. {
  50. struct appldata_product_id id;
  51. int rc;
  52. strcpy(id.prod_nr, "LNXAPPL");
  53. id.prod_fn = myhdr->applid;
  54. id.record_nr = myhdr->record_num;
  55. id.version_nr = myhdr->version;
  56. id.release_nr = myhdr->release;
  57. id.mod_lvl = myhdr->mod_level;
  58. rc = appldata_asm(&id, fcn, (void *) buffer, myhdr->datalen);
  59. if (rc <= 0)
  60. return rc;
  61. pr_err("Writing monitor data failed with rc=%i\n", rc);
  62. if (rc == 5)
  63. return -EPERM;
  64. return -EINVAL;
  65. }
  66. static struct mon_buf *monwrite_find_hdr(struct mon_private *monpriv,
  67. struct monwrite_hdr *monhdr)
  68. {
  69. struct mon_buf *entry, *next;
  70. list_for_each_entry_safe(entry, next, &monpriv->list, list)
  71. if ((entry->hdr.mon_function == monhdr->mon_function ||
  72. monhdr->mon_function == MONWRITE_STOP_INTERVAL) &&
  73. entry->hdr.applid == monhdr->applid &&
  74. entry->hdr.record_num == monhdr->record_num &&
  75. entry->hdr.version == monhdr->version &&
  76. entry->hdr.release == monhdr->release &&
  77. entry->hdr.mod_level == monhdr->mod_level)
  78. return entry;
  79. return NULL;
  80. }
  81. static int monwrite_new_hdr(struct mon_private *monpriv)
  82. {
  83. struct monwrite_hdr *monhdr = &monpriv->hdr;
  84. struct mon_buf *monbuf;
  85. int rc;
  86. if (monhdr->datalen > MONWRITE_MAX_DATALEN ||
  87. monhdr->mon_function > MONWRITE_START_CONFIG ||
  88. monhdr->hdrlen != sizeof(struct monwrite_hdr))
  89. return -EINVAL;
  90. monbuf = NULL;
  91. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  92. monbuf = monwrite_find_hdr(monpriv, monhdr);
  93. if (monbuf) {
  94. if (monhdr->mon_function == MONWRITE_STOP_INTERVAL) {
  95. monhdr->datalen = monbuf->hdr.datalen;
  96. rc = monwrite_diag(monhdr, monbuf->data,
  97. APPLDATA_STOP_REC);
  98. list_del(&monbuf->list);
  99. mon_buf_count--;
  100. kfree(monbuf->data);
  101. kfree(monbuf);
  102. monbuf = NULL;
  103. }
  104. } else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
  105. if (mon_buf_count >= mon_max_bufs)
  106. return -ENOSPC;
  107. monbuf = kzalloc(sizeof(struct mon_buf), GFP_KERNEL);
  108. if (!monbuf)
  109. return -ENOMEM;
  110. monbuf->data = kzalloc(monhdr->datalen,
  111. GFP_KERNEL | GFP_DMA);
  112. if (!monbuf->data) {
  113. kfree(monbuf);
  114. return -ENOMEM;
  115. }
  116. monbuf->hdr = *monhdr;
  117. list_add_tail(&monbuf->list, &monpriv->list);
  118. if (monhdr->mon_function != MONWRITE_GEN_EVENT)
  119. mon_buf_count++;
  120. }
  121. monpriv->current_buf = monbuf;
  122. return 0;
  123. }
  124. static int monwrite_new_data(struct mon_private *monpriv)
  125. {
  126. struct monwrite_hdr *monhdr = &monpriv->hdr;
  127. struct mon_buf *monbuf = monpriv->current_buf;
  128. int rc = 0;
  129. switch (monhdr->mon_function) {
  130. case MONWRITE_START_INTERVAL:
  131. if (!monbuf->diag_done) {
  132. rc = monwrite_diag(monhdr, monbuf->data,
  133. APPLDATA_START_INTERVAL_REC);
  134. monbuf->diag_done = 1;
  135. }
  136. break;
  137. case MONWRITE_START_CONFIG:
  138. if (!monbuf->diag_done) {
  139. rc = monwrite_diag(monhdr, monbuf->data,
  140. APPLDATA_START_CONFIG_REC);
  141. monbuf->diag_done = 1;
  142. }
  143. break;
  144. case MONWRITE_GEN_EVENT:
  145. rc = monwrite_diag(monhdr, monbuf->data,
  146. APPLDATA_GEN_EVENT_REC);
  147. list_del(&monpriv->current_buf->list);
  148. kfree(monpriv->current_buf->data);
  149. kfree(monpriv->current_buf);
  150. monpriv->current_buf = NULL;
  151. break;
  152. default:
  153. /* monhdr->mon_function is checked in monwrite_new_hdr */
  154. BUG();
  155. }
  156. return rc;
  157. }
  158. /*
  159. * file operations
  160. */
  161. static int monwrite_open(struct inode *inode, struct file *filp)
  162. {
  163. struct mon_private *monpriv;
  164. monpriv = kzalloc(sizeof(struct mon_private), GFP_KERNEL);
  165. if (!monpriv)
  166. return -ENOMEM;
  167. lock_kernel();
  168. INIT_LIST_HEAD(&monpriv->list);
  169. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  170. mutex_init(&monpriv->thread_mutex);
  171. filp->private_data = monpriv;
  172. unlock_kernel();
  173. return nonseekable_open(inode, filp);
  174. }
  175. static int monwrite_close(struct inode *inode, struct file *filp)
  176. {
  177. struct mon_private *monpriv = filp->private_data;
  178. struct mon_buf *entry, *next;
  179. list_for_each_entry_safe(entry, next, &monpriv->list, list) {
  180. if (entry->hdr.mon_function != MONWRITE_GEN_EVENT)
  181. monwrite_diag(&entry->hdr, entry->data,
  182. APPLDATA_STOP_REC);
  183. mon_buf_count--;
  184. list_del(&entry->list);
  185. kfree(entry->data);
  186. kfree(entry);
  187. }
  188. kfree(monpriv);
  189. return 0;
  190. }
  191. static ssize_t monwrite_write(struct file *filp, const char __user *data,
  192. size_t count, loff_t *ppos)
  193. {
  194. struct mon_private *monpriv = filp->private_data;
  195. size_t len, written;
  196. void *to;
  197. int rc;
  198. mutex_lock(&monpriv->thread_mutex);
  199. for (written = 0; written < count; ) {
  200. if (monpriv->hdr_to_read) {
  201. len = min(count - written, monpriv->hdr_to_read);
  202. to = (char *) &monpriv->hdr +
  203. sizeof(monpriv->hdr) - monpriv->hdr_to_read;
  204. if (copy_from_user(to, data + written, len)) {
  205. rc = -EFAULT;
  206. goto out_error;
  207. }
  208. monpriv->hdr_to_read -= len;
  209. written += len;
  210. if (monpriv->hdr_to_read > 0)
  211. continue;
  212. rc = monwrite_new_hdr(monpriv);
  213. if (rc)
  214. goto out_error;
  215. monpriv->data_to_read = monpriv->current_buf ?
  216. monpriv->current_buf->hdr.datalen : 0;
  217. }
  218. if (monpriv->data_to_read) {
  219. len = min(count - written, monpriv->data_to_read);
  220. to = monpriv->current_buf->data +
  221. monpriv->hdr.datalen - monpriv->data_to_read;
  222. if (copy_from_user(to, data + written, len)) {
  223. rc = -EFAULT;
  224. goto out_error;
  225. }
  226. monpriv->data_to_read -= len;
  227. written += len;
  228. if (monpriv->data_to_read > 0)
  229. continue;
  230. rc = monwrite_new_data(monpriv);
  231. if (rc)
  232. goto out_error;
  233. }
  234. monpriv->hdr_to_read = sizeof(monpriv->hdr);
  235. }
  236. mutex_unlock(&monpriv->thread_mutex);
  237. return written;
  238. out_error:
  239. monpriv->data_to_read = 0;
  240. monpriv->hdr_to_read = sizeof(struct monwrite_hdr);
  241. mutex_unlock(&monpriv->thread_mutex);
  242. return rc;
  243. }
  244. static const struct file_operations monwrite_fops = {
  245. .owner = THIS_MODULE,
  246. .open = &monwrite_open,
  247. .release = &monwrite_close,
  248. .write = &monwrite_write,
  249. };
  250. static struct miscdevice mon_dev = {
  251. .name = "monwriter",
  252. .fops = &monwrite_fops,
  253. .minor = MISC_DYNAMIC_MINOR,
  254. };
  255. /*
  256. * module init/exit
  257. */
  258. static int __init mon_init(void)
  259. {
  260. if (MACHINE_IS_VM)
  261. return misc_register(&mon_dev);
  262. else
  263. return -ENODEV;
  264. }
  265. static void __exit mon_exit(void)
  266. {
  267. WARN_ON(misc_deregister(&mon_dev) != 0);
  268. }
  269. module_init(mon_init);
  270. module_exit(mon_exit);
  271. module_param_named(max_bufs, mon_max_bufs, int, 0644);
  272. MODULE_PARM_DESC(max_bufs, "Maximum number of sample monitor data buffers "
  273. "that can be active at one time");
  274. MODULE_AUTHOR("Melissa Howland <Melissa.Howland@us.ibm.com>");
  275. MODULE_DESCRIPTION("Character device driver for writing z/VM "
  276. "APPLDATA monitor records.");
  277. MODULE_LICENSE("GPL");